llvm.org GIT mirror llvm / 3837218
Extend ScalarEvolution's executesAtLeastOnce logic to be able to continue past the first conditional branch when looking for a relevant test. This helps it avoid using MAX expressions in loop trip counts in more cases. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54697 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 12 years ago
2 changed file(s) with 96 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
27082708 SCEV *LHS, SCEV *RHS) {
27092709 BasicBlock *Preheader = L->getLoopPreheader();
27102710 BasicBlock *PreheaderDest = L->getHeader();
2711 if (Preheader == 0) return false;
2712
2713 BranchInst *LoopEntryPredicate =
2714 dyn_cast(Preheader->getTerminator());
2715 if (!LoopEntryPredicate) return false;
2716
2717 // This might be a critical edge broken out. If the loop preheader ends in
2718 // an unconditional branch to the loop, check to see if the preheader has a
2719 // single predecessor, and if so, look for its terminator.
2720 while (LoopEntryPredicate->isUnconditional()) {
2721 PreheaderDest = Preheader;
2722 Preheader = Preheader->getSinglePredecessor();
2723 if (!Preheader) return false; // Multiple preds.
2724
2725 LoopEntryPredicate =
2711
2712 // Starting at the preheader, climb up the predecessor chain, as long as
2713 // there are unique predecessors, looking for a conditional branch that
2714 // protects the loop.
2715 //
2716 // This is a conservative apporoximation of a climb of the
2717 // control-dependence predecessors.
2718
2719 for (; Preheader; PreheaderDest = Preheader,
2720 Preheader = Preheader->getSinglePredecessor()) {
2721
2722 BranchInst *LoopEntryPredicate =
27262723 dyn_cast(Preheader->getTerminator());
2727 if (!LoopEntryPredicate) return false;
2728 }
2729
2730 ICmpInst *ICI = dyn_cast(LoopEntryPredicate->getCondition());
2731 if (!ICI) return false;
2732
2733 // Now that we found a conditional branch that dominates the loop, check to
2734 // see if it is the comparison we are looking for.
2735 Value *PreCondLHS = ICI->getOperand(0);
2736 Value *PreCondRHS = ICI->getOperand(1);
2737 ICmpInst::Predicate Cond;
2738 if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest)
2739 Cond = ICI->getPredicate();
2740 else
2741 Cond = ICI->getInversePredicate();
2742
2743 switch (Cond) {
2744 case ICmpInst::ICMP_UGT:
2745 if (isSigned) return false;
2746 std::swap(PreCondLHS, PreCondRHS);
2747 Cond = ICmpInst::ICMP_ULT;
2748 break;
2749 case ICmpInst::ICMP_SGT:
2750 if (!isSigned) return false;
2751 std::swap(PreCondLHS, PreCondRHS);
2752 Cond = ICmpInst::ICMP_SLT;
2753 break;
2754 case ICmpInst::ICMP_ULT:
2755 if (isSigned) return false;
2756 break;
2757 case ICmpInst::ICMP_SLT:
2758 if (!isSigned) return false;
2759 break;
2760 default:
2761 return false;
2762 }
2763
2764 if (!PreCondLHS->getType()->isInteger()) return false;
2765
2766 SCEVHandle PreCondLHSSCEV = getSCEV(PreCondLHS);
2767 SCEVHandle PreCondRHSSCEV = getSCEV(PreCondRHS);
2768 return (LHS == PreCondLHSSCEV && RHS == PreCondRHSSCEV) ||
2769 (LHS == SE.getNotSCEV(PreCondRHSSCEV) &&
2770 RHS == SE.getNotSCEV(PreCondLHSSCEV));
2724 if (!LoopEntryPredicate ||
2725 LoopEntryPredicate->isUnconditional())
2726 continue;
2727
2728 ICmpInst *ICI = dyn_cast(LoopEntryPredicate->getCondition());
2729 if (!ICI) continue;
2730
2731 // Now that we found a conditional branch that dominates the loop, check to
2732 // see if it is the comparison we are looking for.
2733 Value *PreCondLHS = ICI->getOperand(0);
2734 Value *PreCondRHS = ICI->getOperand(1);
2735 ICmpInst::Predicate Cond;
2736 if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest)
2737 Cond = ICI->getPredicate();
2738 else
2739 Cond = ICI->getInversePredicate();
2740
2741 switch (Cond) {
2742 case ICmpInst::ICMP_UGT:
2743 if (isSigned) continue;
2744 std::swap(PreCondLHS, PreCondRHS);
2745 Cond = ICmpInst::ICMP_ULT;
2746 break;
2747 case ICmpInst::ICMP_SGT:
2748 if (!isSigned) continue;
2749 std::swap(PreCondLHS, PreCondRHS);
2750 Cond = ICmpInst::ICMP_SLT;
2751 break;
2752 case ICmpInst::ICMP_ULT:
2753 if (isSigned) continue;
2754 break;
2755 case ICmpInst::ICMP_SLT:
2756 if (!isSigned) continue;
2757 break;
2758 default:
2759 continue;
2760 }
2761
2762 if (!PreCondLHS->getType()->isInteger()) continue;
2763
2764 SCEVHandle PreCondLHSSCEV = getSCEV(PreCondLHS);
2765 SCEVHandle PreCondRHSSCEV = getSCEV(PreCondRHS);
2766 if ((LHS == PreCondLHSSCEV && RHS == PreCondRHSSCEV) ||
2767 (LHS == SE.getNotSCEV(PreCondRHSSCEV) &&
2768 RHS == SE.getNotSCEV(PreCondLHSSCEV)))
2769 return true;
2770 }
2771
2772 return false;
27712773 }
27722774
27732775 /// HowManyLessThans - Return the number of times a backedge containing the
0 ; RUN: llvm-as < %s | opt -scalar-evolution -analyze | grep {Loop bb3: ( -1 + %n) iterations!}
1
2 ; We don't want to use a max in the trip count expression in
3 ; this testcase.
4
5 define void @foo(i32 %n, i32* %p, i32* %q) nounwind {
6 entry:
7 icmp sgt i32 %n, 0
8 br i1 %0, label %bb, label %return
9
10 bb:
11 load i32* %q, align 4
12 icmp eq i32 %1, 0
13 br i1 %2, label %return, label %bb3.preheader
14
15 bb3.preheader:
16 br label %bb3
17
18 bb3:
19 %i.0 = phi i32 [ %7, %bb3 ], [ 0, %bb3.preheader ]
20 getelementptr i32* %p, i32 %i.0
21 load i32* %3, align 4
22 add i32 %4, 1
23 getelementptr i32* %p, i32 %i.0
24 store i32 %5, i32* %6, align 4
25 add i32 %i.0, 1
26 icmp slt i32 %7, %n
27 br i1 %8, label %bb3, label %return.loopexit
28
29 return.loopexit:
30 br label %return
31
32 return:
33 ret void
34 }