llvm.org GIT mirror llvm / 4d52c6d
Optimize ScalarEvolution's SCEVComplexityCompare predicate: don't go scrounging through SCEVUnknown contents and SCEVNAryExpr operands; instead just do a simple deterministic comparison of the precomputed hash data. Also, since this is more precise, it eliminates the need for the slow N^2 duplicate detection code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@105540 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
3 changed file(s) with 18 addition(s) and 124 deletion(s). Raw diff Collapse all Expand all
7474
7575 /// Profile - FoldingSet support.
7676 void Profile(FoldingSetNodeID& ID) { ID = FastID; }
77
78 /// getProfile - Like Profile, but a different interface which doesn't copy.
79 const FoldingSetNodeIDRef &getProfile() const { return FastID; }
7780
7881 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
7982 /// the specified loop.
507507 if (LHS->getSCEVType() != RHS->getSCEVType())
508508 return LHS->getSCEVType() < RHS->getSCEVType();
509509
510 // Aside from the getSCEVType() ordering, the particular ordering
511 // isn't very important except that it's beneficial to be consistent,
512 // so that (a + b) and (b + a) don't end up as different expressions.
513
514 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
515 // not as complete as it could be.
516 if (const SCEVUnknown *LU = dyn_cast(LHS)) {
517 const SCEVUnknown *RU = cast(RHS);
518
519 // Order pointer values after integer values. This helps SCEVExpander
520 // form GEPs.
521 if (LU->getType()->isPointerTy() && !RU->getType()->isPointerTy())
522 return false;
523 if (RU->getType()->isPointerTy() && !LU->getType()->isPointerTy())
524 return true;
525
526 // Compare getValueID values.
527 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
528 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
529
530 // Sort arguments by their position.
531 if (const Argument *LA = dyn_cast(LU->getValue())) {
532 const Argument *RA = cast(RU->getValue());
533 return LA->getArgNo() < RA->getArgNo();
534 }
535
536 // For instructions, compare their loop depth, and their opcode.
537 // This is pretty loose.
538 if (Instruction *LV = dyn_cast(LU->getValue())) {
539 Instruction *RV = cast(RU->getValue());
540
541 // Compare loop depths.
542 if (LI->getLoopDepth(LV->getParent()) !=
543 LI->getLoopDepth(RV->getParent()))
544 return LI->getLoopDepth(LV->getParent()) <
545 LI->getLoopDepth(RV->getParent());
546
547 // Compare opcodes.
548 if (LV->getOpcode() != RV->getOpcode())
549 return LV->getOpcode() < RV->getOpcode();
550
551 // Compare the number of operands.
552 if (LV->getNumOperands() != RV->getNumOperands())
553 return LV->getNumOperands() < RV->getNumOperands();
554 }
555
556 return false;
557 }
558
559 // Compare constant values.
560 if (const SCEVConstant *LC = dyn_cast(LHS)) {
561 const SCEVConstant *RC = cast(RHS);
562 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
563 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
564 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
565 }
566
567 // Compare addrec loop depths.
568 if (const SCEVAddRecExpr *LA = dyn_cast(LHS)) {
569 const SCEVAddRecExpr *RA = cast(RHS);
570 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
571 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
572 }
573
574 // Lexicographically compare n-ary expressions.
575 if (const SCEVNAryExpr *LC = dyn_cast(LHS)) {
576 const SCEVNAryExpr *RC = cast(RHS);
577 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
578 if (i >= RC->getNumOperands())
579 return false;
580 if (operator()(LC->getOperand(i), RC->getOperand(i)))
581 return true;
582 if (operator()(RC->getOperand(i), LC->getOperand(i)))
583 return false;
584 }
585 return LC->getNumOperands() < RC->getNumOperands();
586 }
587
588 // Lexicographically compare udiv expressions.
589 if (const SCEVUDivExpr *LC = dyn_cast(LHS)) {
590 const SCEVUDivExpr *RC = cast(RHS);
591 if (operator()(LC->getLHS(), RC->getLHS()))
592 return true;
593 if (operator()(RC->getLHS(), LC->getLHS()))
594 return false;
595 if (operator()(LC->getRHS(), RC->getRHS()))
596 return true;
597 if (operator()(RC->getRHS(), LC->getRHS()))
598 return false;
599 return false;
600 }
601
602 // Compare cast expressions by operand.
603 if (const SCEVCastExpr *LC = dyn_cast(LHS)) {
604 const SCEVCastExpr *RC = cast(RHS);
605 return operator()(LC->getOperand(), RC->getOperand());
606 }
607
608 llvm_unreachable("Unknown SCEV kind!");
609 return false;
510 // Then, pick an arbitrary sort. Use the profiling data for speed.
511 const FoldingSetNodeIDRef &L = LHS->getProfile();
512 const FoldingSetNodeIDRef &R = RHS->getProfile();
513 size_t LSize = L.getSize();
514 size_t RSize = R.getSize();
515 if (LSize != RSize)
516 return LSize < RSize;
517 return memcmp(L.getData(), R.getData(),
518 LSize * sizeof(*L.getData())) < 0;
610519 }
611520 };
612521 }
624533 static void GroupByComplexity(SmallVectorImpl &Ops,
625534 LoopInfo *LI) {
626535 if (Ops.size() < 2) return; // Noop
536
537 SCEVComplexityCompare Comp(LI);
538
627539 if (Ops.size() == 2) {
628540 // This is the common case, which also happens to be trivially simple.
629541 // Special case it.
630 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
542 if (Comp(Ops[1], Ops[0]))
631543 std::swap(Ops[0], Ops[1]);
632544 return;
633545 }
634546
635 // Do the rough sort by complexity.
636 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
637
638 // Now that we are sorted by complexity, group elements of the same
639 // complexity. Note that this is, at worst, N^2, but the vector is likely to
640 // be extremely short in practice. Note that we take this approach because we
641 // do not want to depend on the addresses of the objects we are grouping.
642 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
643 const SCEV *S = Ops[i];
644 unsigned Complexity = S->getSCEVType();
645
646 // If there are any objects of the same complexity and same value as this
647 // one, group them.
648 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
649 if (Ops[j] == S) { // Found a duplicate.
650 // Move it to immediately after i'th element.
651 std::swap(Ops[i+1], Ops[j]);
652 ++i; // no need to rescan it.
653 if (i == e-2) return; // Done!
654 }
655 }
656 }
547 std::stable_sort(Ops.begin(), Ops.end(), Comp);
657548 }
658549
659550
2121 ret i32 %j.0.lcssa
2222 }
2323
24 ; CHECK: backedge-taken count is (-2147483632 + ((-1 + (-1 * %x)) smax (-1 + (-1 * %y))))
24 ; CHECK: backedge-taken count is (-2147483632 + ((-1 + (-1 * %y)) smax (-1 + (-1 * %x))))
2525