1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DFGCSEPhase.h"
#if ENABLE(DFG_JIT)
#include "DFGGraph.h"
#include "DFGPhase.h"
namespace JSC { namespace DFG {
class CSEPhase : public Phase {
public:
CSEPhase(Graph& graph)
: Phase(graph, "common subexpression elimination")
{
// Replacements are used to implement local common subexpression elimination.
m_replacements.resize(m_graph.size());
for (unsigned i = 0; i < m_graph.size(); ++i)
m_replacements[i] = NoNode;
for (unsigned i = 0; i < LastNodeId; ++i)
m_lastSeen[i] = NoNode;
}
void run()
{
for (unsigned block = 0; block < m_graph.m_blocks.size(); ++block)
performBlockCSE(*m_graph.m_blocks[block]);
}
private:
NodeIndex canonicalize(NodeIndex nodeIndex)
{
if (nodeIndex == NoNode)
return NoNode;
if (m_graph[nodeIndex].op == ValueToInt32)
nodeIndex = m_graph[nodeIndex].child1().index();
return nodeIndex;
}
NodeIndex canonicalize(NodeUse nodeUse)
{
return canonicalize(nodeUse.indexUnchecked());
}
// Computes where the search for a candidate for CSE should start. Don't call
// this directly; call startIndex() instead as it does logging in debug mode.
NodeIndex computeStartIndexForChildren(NodeIndex child1 = NoNode, NodeIndex child2 = NoNode, NodeIndex child3 = NoNode)
{
const unsigned limit = 300;
NodeIndex start = m_start;
if (m_compileIndex - start > limit)
start = m_compileIndex - limit;
ASSERT(start >= m_start);
NodeIndex child = canonicalize(child1);
if (child == NoNode)
return start;
if (start < child)
start = child;
child = canonicalize(child2);
if (child == NoNode)
return start;
if (start < child)
start = child;
child = canonicalize(child3);
if (child == NoNode)
return start;
if (start < child)
start = child;
return start;
}
NodeIndex startIndexForChildren(NodeIndex child1 = NoNode, NodeIndex child2 = NoNode, NodeIndex child3 = NoNode)
{
NodeIndex result = computeStartIndexForChildren(child1, child2, child3);
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog(" lookback %u: ", result);
#endif
return result;
}
NodeIndex startIndex()
{
Node& node = m_graph[m_compileIndex];
return startIndexForChildren(
node.child1().indexUnchecked(),
node.child2().indexUnchecked(),
node.child3().indexUnchecked());
}
NodeIndex endIndexForPureCSE()
{
NodeIndex result = m_lastSeen[m_graph[m_compileIndex].op & NodeIdMask];
if (result == NoNode)
result = 0;
else
result++;
ASSERT(result <= m_compileIndex);
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog(" limit %u: ", result);
#endif
return result;
}
NodeIndex pureCSE(Node& node)
{
NodeIndex child1 = canonicalize(node.child1());
NodeIndex child2 = canonicalize(node.child2());
NodeIndex child3 = canonicalize(node.child3());
NodeIndex start = startIndex();
for (NodeIndex index = endIndexForPureCSE(); index-- > start;) {
Node& otherNode = m_graph[index];
if (node.op != otherNode.op)
continue;
if (node.arithNodeFlagsForCompare() != otherNode.arithNodeFlagsForCompare())
continue;
NodeIndex otherChild = canonicalize(otherNode.child1());
if (otherChild == NoNode)
return index;
if (otherChild != child1)
continue;
otherChild = canonicalize(otherNode.child2());
if (otherChild == NoNode)
return index;
if (otherChild != child2)
continue;
otherChild = canonicalize(otherNode.child3());
if (otherChild == NoNode)
return index;
if (otherChild != child3)
continue;
return index;
}
return NoNode;
}
bool isPredictedNumerical(Node& node)
{
PredictedType left = m_graph[node.child1()].prediction();
PredictedType right = m_graph[node.child2()].prediction();
return isNumberPrediction(left) && isNumberPrediction(right);
}
bool logicalNotIsPure(Node& node)
{
PredictedType prediction = m_graph[node.child1()].prediction();
return isBooleanPrediction(prediction) || !prediction;
}
bool byValIsPure(Node& node)
{
return m_graph[node.child2()].shouldSpeculateInteger()
&& ((node.op == PutByVal || node.op == PutByValAlias)
? isActionableMutableArrayPrediction(m_graph[node.child1()].prediction())
: isActionableArrayPrediction(m_graph[node.child1()].prediction()));
}
bool clobbersWorld(NodeIndex nodeIndex)
{
Node& node = m_graph[nodeIndex];
if (node.op & NodeClobbersWorld)
return true;
if (!(node.op & NodeMightClobber))
return false;
switch (node.op) {
case ValueAdd:
case CompareLess:
case CompareLessEq:
case CompareGreater:
case CompareGreaterEq:
case CompareEq:
return !isPredictedNumerical(node);
case LogicalNot:
return !logicalNotIsPure(node);
case GetByVal:
return !byValIsPure(node);
default:
ASSERT_NOT_REACHED();
return true; // If by some oddity we hit this case in release build it's safer to have CSE assume the worst.
}
}
NodeIndex impureCSE(Node& node)
{
NodeIndex child1 = canonicalize(node.child1());
NodeIndex child2 = canonicalize(node.child2());
NodeIndex child3 = canonicalize(node.child3());
NodeIndex start = startIndex();
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& otherNode = m_graph[index];
if (node.op == otherNode.op
&& node.arithNodeFlagsForCompare() == otherNode.arithNodeFlagsForCompare()) {
NodeIndex otherChild = canonicalize(otherNode.child1());
if (otherChild == NoNode)
return index;
if (otherChild == child1) {
otherChild = canonicalize(otherNode.child2());
if (otherChild == NoNode)
return index;
if (otherChild == child2) {
otherChild = canonicalize(otherNode.child3());
if (otherChild == NoNode)
return index;
if (otherChild == child3)
return index;
}
}
}
if (clobbersWorld(index))
break;
}
return NoNode;
}
NodeIndex globalVarLoadElimination(unsigned varNumber, JSGlobalObject* globalObject)
{
NodeIndex start = startIndexForChildren();
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case GetGlobalVar:
if (node.varNumber() == varNumber && codeBlock()->globalObjectFor(node.codeOrigin) == globalObject)
return index;
break;
case PutGlobalVar:
if (node.varNumber() == varNumber && codeBlock()->globalObjectFor(node.codeOrigin) == globalObject)
return node.child1().index();
break;
default:
break;
}
if (clobbersWorld(index))
break;
}
return NoNode;
}
NodeIndex getByValLoadElimination(NodeIndex child1, NodeIndex child2)
{
NodeIndex start = startIndexForChildren(child1, child2);
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case GetByVal:
if (!byValIsPure(node))
return NoNode;
if (node.child1() == child1 && canonicalize(node.child2()) == canonicalize(child2))
return index;
break;
case PutByVal:
case PutByValAlias:
if (!byValIsPure(node))
return NoNode;
if (node.child1() == child1 && canonicalize(node.child2()) == canonicalize(child2))
return node.child3().index();
// We must assume that the PutByVal will clobber the location we're getting from.
// FIXME: We can do better; if we know that the PutByVal is accessing an array of a
// different type than the GetByVal, then we know that they won't clobber each other.
return NoNode;
case PutStructure:
case PutByOffset:
// GetByVal currently always speculates that it's accessing an
// array with an integer index, which means that it's impossible
// for a structure change or a put to property storage to affect
// the GetByVal.
break;
case ArrayPush:
// A push cannot affect previously existing elements in the array.
break;
default:
if (clobbersWorld(index))
return NoNode;
break;
}
}
return NoNode;
}
bool checkFunctionElimination(JSFunction* function, NodeIndex child1)
{
NodeIndex start = startIndexForChildren(child1);
for (NodeIndex index = endIndexForPureCSE(); index-- > start;) {
Node& node = m_graph[index];
if (node.op == CheckFunction && node.child1() == child1 && node.function() == function)
return true;
}
return false;
}
bool checkStructureLoadElimination(const StructureSet& structureSet, NodeIndex child1)
{
NodeIndex start = startIndexForChildren(child1);
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case CheckStructure:
if (node.child1() == child1
&& structureSet.isSupersetOf(node.structureSet()))
return true;
break;
case PutStructure:
if (node.child1() == child1
&& structureSet.contains(node.structureTransitionData().newStructure))
return true;
if (structureSet.contains(node.structureTransitionData().previousStructure))
return false;
break;
case PutByOffset:
// Setting a property cannot change the structure.
break;
case PutByVal:
case PutByValAlias:
if (byValIsPure(node)) {
// If PutByVal speculates that it's accessing an array with an
// integer index, then it's impossible for it to cause a structure
// change.
break;
}
return false;
default:
if (clobbersWorld(index))
return false;
break;
}
}
return false;
}
NodeIndex getByOffsetLoadElimination(unsigned identifierNumber, NodeIndex child1)
{
NodeIndex start = startIndexForChildren(child1);
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case GetByOffset:
if (node.child1() == child1
&& m_graph.m_storageAccessData[node.storageAccessDataIndex()].identifierNumber == identifierNumber)
return index;
break;
case PutByOffset:
if (m_graph.m_storageAccessData[node.storageAccessDataIndex()].identifierNumber == identifierNumber) {
if (node.child2() == child1)
return node.child3().index();
return NoNode;
}
break;
case PutStructure:
// Changing the structure cannot change the outcome of a property get.
break;
case PutByVal:
case PutByValAlias:
if (byValIsPure(node)) {
// If PutByVal speculates that it's accessing an array with an
// integer index, then it's impossible for it to cause a structure
// change.
break;
}
return NoNode;
default:
if (clobbersWorld(index))
return NoNode;
break;
}
}
return NoNode;
}
NodeIndex getPropertyStorageLoadElimination(NodeIndex child1)
{
NodeIndex start = startIndexForChildren(child1);
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case GetPropertyStorage:
if (node.child1() == child1)
return index;
break;
case PutByOffset:
case PutStructure:
// Changing the structure or putting to the storage cannot
// change the property storage pointer.
break;
case PutByVal:
case PutByValAlias:
if (byValIsPure(node)) {
// If PutByVal speculates that it's accessing an array with an
// integer index, then it's impossible for it to cause a structure
// change.
break;
}
return NoNode;
default:
if (clobbersWorld(index))
return NoNode;
break;
}
}
return NoNode;
}
NodeIndex getIndexedPropertyStorageLoadElimination(NodeIndex child1, bool hasIntegerIndexPrediction)
{
NodeIndex start = startIndexForChildren(child1);
for (NodeIndex index = m_compileIndex; index-- > start;) {
Node& node = m_graph[index];
switch (node.op) {
case GetIndexedPropertyStorage: {
PredictedType basePrediction = m_graph[node.child2()].prediction();
bool nodeHasIntegerIndexPrediction = !(!(basePrediction & PredictInt32) && basePrediction);
if (node.child1() == child1 && hasIntegerIndexPrediction == nodeHasIntegerIndexPrediction)
return index;
break;
}
case PutByOffset:
case PutStructure:
// Changing the structure or putting to the storage cannot
// change the property storage pointer.
break;
case PutByValAlias:
// PutByValAlias can't change the indexed storage pointer
break;
case PutByVal:
if (isFixedIndexedStorageObjectPrediction(m_graph[node.child1()].prediction()) && byValIsPure(node))
break;
return NoNode;
default:
if (clobbersWorld(index))
return NoNode;
break;
}
}
return NoNode;
}
NodeIndex getScopeChainLoadElimination(unsigned depth)
{
NodeIndex start = startIndexForChildren();
for (NodeIndex index = endIndexForPureCSE(); index-- > start;) {
Node& node = m_graph[index];
if (node.op == GetScopeChain
&& node.scopeChainDepth() == depth)
return index;
}
return NoNode;
}
void performSubstitution(NodeUse& child, bool addRef = true)
{
// Check if this operand is actually unused.
if (!child)
return;
// Check if there is any replacement.
NodeIndex replacement = m_replacements[child.index()];
if (replacement == NoNode)
return;
child.setIndex(replacement);
// There is definitely a replacement. Assert that the replacement does not
// have a replacement.
ASSERT(m_replacements[child.index()] == NoNode);
if (addRef)
m_graph[child].ref();
}
void setReplacement(NodeIndex replacement)
{
if (replacement == NoNode)
return;
// Be safe. Don't try to perform replacements if the predictions don't
// agree.
if (m_graph[m_compileIndex].prediction() != m_graph[replacement].prediction())
return;
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog(" Replacing @%u -> @%u", m_compileIndex, replacement);
#endif
Node& node = m_graph[m_compileIndex];
node.op = Phantom;
node.setRefCount(1);
// At this point we will eliminate all references to this node.
m_replacements[m_compileIndex] = replacement;
}
void eliminate()
{
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog(" Eliminating @%u", m_compileIndex);
#endif
Node& node = m_graph[m_compileIndex];
ASSERT(node.refCount() == 1);
ASSERT(node.mustGenerate());
node.op = Phantom;
}
void performNodeCSE(Node& node)
{
bool shouldGenerate = node.shouldGenerate();
if (node.op & NodeHasVarArgs) {
for (unsigned childIdx = node.firstChild(); childIdx < node.firstChild() + node.numChildren(); childIdx++)
performSubstitution(m_graph.m_varArgChildren[childIdx], shouldGenerate);
} else {
performSubstitution(node.children.child1(), shouldGenerate);
performSubstitution(node.children.child2(), shouldGenerate);
performSubstitution(node.children.child3(), shouldGenerate);
}
if (!shouldGenerate)
return;
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog(" %s @%u: ", Graph::opName(m_graph[m_compileIndex].op), m_compileIndex);
#endif
// NOTE: there are some nodes that we deliberately don't CSE even though we
// probably could, like StrCat and ToPrimitive. That's because there is no
// evidence that doing CSE on these nodes would result in a performance
// progression. Hence considering these nodes in CSE would just mean that this
// code does more work with no win. Of course, we may want to reconsider this,
// since StrCat is trivially CSE-able. It's not trivially doable for
// ToPrimitive, but we could change that with some speculations if we really
// needed to.
switch (node.op) {
// Handle the pure nodes. These nodes never have any side-effects.
case BitAnd:
case BitOr:
case BitXor:
case BitRShift:
case BitLShift:
case BitURShift:
case ArithAdd:
case ArithSub:
case ArithMul:
case ArithMod:
case ArithDiv:
case ArithAbs:
case ArithMin:
case ArithMax:
case ArithSqrt:
case GetByteArrayLength:
case GetInt8ArrayLength:
case GetInt16ArrayLength:
case GetInt32ArrayLength:
case GetUint8ArrayLength:
case GetUint8ClampedArrayLength:
case GetUint16ArrayLength:
case GetUint32ArrayLength:
case GetFloat32ArrayLength:
case GetFloat64ArrayLength:
case GetCallee:
case GetStringLength:
case StringCharAt:
case StringCharCodeAt:
setReplacement(pureCSE(node));
break;
case GetArrayLength:
setReplacement(impureCSE(node));
break;
case GetScopeChain:
setReplacement(getScopeChainLoadElimination(node.scopeChainDepth()));
break;
// Handle nodes that are conditionally pure: these are pure, and can
// be CSE'd, so long as the prediction is the one we want.
case ValueAdd:
case CompareLess:
case CompareLessEq:
case CompareGreater:
case CompareGreaterEq:
case CompareEq: {
if (isPredictedNumerical(node)) {
NodeIndex replacementIndex = pureCSE(node);
if (replacementIndex != NoNode && isPredictedNumerical(m_graph[replacementIndex]))
setReplacement(replacementIndex);
}
break;
}
case LogicalNot: {
if (logicalNotIsPure(node)) {
NodeIndex replacementIndex = pureCSE(node);
if (replacementIndex != NoNode && logicalNotIsPure(m_graph[replacementIndex]))
setReplacement(replacementIndex);
}
break;
}
// Finally handle heap accesses. These are not quite pure, but we can still
// optimize them provided that some subtle conditions are met.
case GetGlobalVar:
setReplacement(globalVarLoadElimination(node.varNumber(), codeBlock()->globalObjectFor(node.codeOrigin)));
break;
case GetByVal:
if (byValIsPure(node))
setReplacement(getByValLoadElimination(node.child1().index(), node.child2().index()));
break;
case PutByVal:
if (byValIsPure(node) && getByValLoadElimination(node.child1().index(), node.child2().index()) != NoNode)
node.op = PutByValAlias;
break;
case CheckStructure:
if (checkStructureLoadElimination(node.structureSet(), node.child1().index()))
eliminate();
break;
case CheckFunction:
if (checkFunctionElimination(node.function(), node.child1().index()))
eliminate();
break;
case GetIndexedPropertyStorage: {
PredictedType basePrediction = m_graph[node.child2()].prediction();
bool nodeHasIntegerIndexPrediction = !(!(basePrediction & PredictInt32) && basePrediction);
setReplacement(getIndexedPropertyStorageLoadElimination(node.child1().index(), nodeHasIntegerIndexPrediction));
break;
}
case GetPropertyStorage:
setReplacement(getPropertyStorageLoadElimination(node.child1().index()));
break;
case GetByOffset:
setReplacement(getByOffsetLoadElimination(m_graph.m_storageAccessData[node.storageAccessDataIndex()].identifierNumber, node.child1().index()));
break;
default:
// do nothing.
break;
}
m_lastSeen[node.op & NodeIdMask] = m_compileIndex;
#if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
dataLog("\n");
#endif
}
void performBlockCSE(BasicBlock& block)
{
m_start = block.begin;
NodeIndex end = block.end;
for (m_compileIndex = m_start; m_compileIndex < end; ++m_compileIndex)
performNodeCSE(m_graph[m_compileIndex]);
}
NodeIndex m_start;
NodeIndex m_compileIndex;
Vector<NodeIndex, 16> m_replacements;
FixedArray<NodeIndex, LastNodeId> m_lastSeen;
};
void performCSE(Graph& graph)
{
runPhase<CSEPhase>(graph);
}
} } // namespace JSC::DFG
#endif // ENABLE(DFG_JIT)
|