llvm.org GIT mirror llvm / a334aa7
Teach ScalarEvolution how to analyze loops with multiple exit blocks, and also exit blocks with multiple conditions (combined with (bitwise) ands and ors). It's often infeasible to compute an exact trip count in such cases, but a useful upper bound can often be found. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73866 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
3 changed file(s) with 302 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
347347 /// loop will iterate.
348348 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
349349
350 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the
351 /// backedge of the specified loop will execute if it exits via the
352 /// specified block.
353 BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
354 BasicBlock *ExitingBlock);
355
356 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
357 /// backedge of the specified loop will execute if its exit condition
358 /// were a conditional branch of ExitCond, TBB, and FBB.
359 BackedgeTakenInfo
360 ComputeBackedgeTakenCountFromExitCond(const Loop *L,
361 Value *ExitCond,
362 BasicBlock *TBB,
363 BasicBlock *FBB);
364
365 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
366 /// times the backedge of the specified loop will execute if its exit
367 /// condition were a conditional branch of the ICmpInst ExitCond, TBB,
368 /// and FBB.
369 BackedgeTakenInfo
370 ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
371 ICmpInst *ExitCond,
372 BasicBlock *TBB,
373 BasicBlock *FBB);
374
350375 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
351376 /// of 'icmp op load X, cst', try to see if we can compute the trip count.
352377 SCEVHandle
519544 /// specified signed integer value and return a SCEV for the constant.
520545 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
521546
547 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
548 /// the types using zero-extension, and then perform a umax operation
549 /// with them.
550 SCEVHandle getUMaxFromMismatchedTypes(const SCEVHandle &LHS,
551 const SCEVHandle &RHS);
552
522553 /// hasSCEV - Return true if the SCEV for this value has already been
523554 /// computed.
524555 bool hasSCEV(Value *V) const;
21272127 return getTruncateExpr(V, Ty);
21282128 }
21292129
2130 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2131 /// the types using zero-extension, and then perform a umax operation
2132 /// with them.
2133 SCEVHandle ScalarEvolution::getUMaxFromMismatchedTypes(const SCEVHandle &LHS,
2134 const SCEVHandle &RHS) {
2135 SCEVHandle PromotedLHS = LHS;
2136 SCEVHandle PromotedRHS = RHS;
2137
2138 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2139 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2140 else
2141 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2142
2143 return getUMaxExpr(PromotedLHS, PromotedRHS);
2144 }
2145
21302146 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for
21312147 /// the specified instruction and replaces any references to the symbolic value
21322148 /// SymName with the specified value. This is used during PHI resolution.
27222738
27232739 // Update the value in the map.
27242740 Pair.first->second = ItCount;
2725 } else if (isa(L->getHeader()->begin())) {
2726 // Only count loops that have phi nodes as not being computable.
2727 ++NumTripCountsNotComputed;
2741 } else {
2742 if (ItCount.Max != CouldNotCompute)
2743 // Update the value in the map.
2744 Pair.first->second = ItCount;
2745 if (isa(L->getHeader()->begin()))
2746 // Only count loops that have phi nodes as not being computable.
2747 ++NumTripCountsNotComputed;
27282748 }
27292749
27302750 // Now that we know more about the trip count for this loop, forget any
27802800 /// of the specified loop will execute.
27812801 ScalarEvolution::BackedgeTakenInfo
27822802 ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
2783 // If the loop has a non-one exit block count, we can't analyze it.
2784 BasicBlock *ExitBlock = L->getExitBlock();
2785 if (!ExitBlock)
2786 return CouldNotCompute;
2787
2788 // Okay, there is one exit block. Try to find the condition that causes the
2789 // loop to be exited.
2790 BasicBlock *ExitingBlock = L->getExitingBlock();
2791 if (!ExitingBlock)
2792 return CouldNotCompute; // More than one block exiting!
2793
2794 // Okay, we've computed the exiting block. See what condition causes us to
2795 // exit.
2803 SmallVector ExitingBlocks;
2804 L->getExitingBlocks(ExitingBlocks);
2805
2806 // Examine all exits and pick the most conservative values.
2807 SCEVHandle BECount = CouldNotCompute;
2808 SCEVHandle MaxBECount = CouldNotCompute;
2809 bool CouldNotComputeBECount = false;
2810 bool CouldNotComputeMaxBECount = false;
2811 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
2812 BackedgeTakenInfo NewBTI =
2813 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
2814
2815 if (NewBTI.Exact == CouldNotCompute) {
2816 // We couldn't compute an exact value for this exit, so
2817 // we don't be able to compute an exact value for the loop.
2818 CouldNotComputeBECount = true;
2819 BECount = CouldNotCompute;
2820 } else if (!CouldNotComputeBECount) {
2821 if (BECount == CouldNotCompute)
2822 BECount = NewBTI.Exact;
2823 else {
2824 // TODO: More analysis could be done here. For example, a
2825 // loop with a short-circuiting && operator has an exact count
2826 // of the min of both sides.
2827 CouldNotComputeBECount = true;
2828 BECount = CouldNotCompute;
2829 }
2830 }
2831 if (NewBTI.Max == CouldNotCompute) {
2832 // We couldn't compute an maximum value for this exit, so
2833 // we don't be able to compute an maximum value for the loop.
2834 CouldNotComputeMaxBECount = true;
2835 MaxBECount = CouldNotCompute;
2836 } else if (!CouldNotComputeMaxBECount) {
2837 if (MaxBECount == CouldNotCompute)
2838 MaxBECount = NewBTI.Max;
2839 else
2840 MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, NewBTI.Max);
2841 }
2842 }
2843
2844 return BackedgeTakenInfo(BECount, MaxBECount);
2845 }
2846
2847 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
2848 /// of the specified loop will execute if it exits via the specified block.
2849 ScalarEvolution::BackedgeTakenInfo
2850 ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
2851 BasicBlock *ExitingBlock) {
2852
2853 // Okay, we've chosen an exiting block. See what condition causes us to
2854 // exit at this block.
27962855 //
27972856 // FIXME: we should be able to handle switch instructions (with a single exit)
27982857 BranchInst *ExitBr = dyn_cast(ExitingBlock->getTerminator());
28072866 // Currently we check for this by checking to see if the Exit branch goes to
28082867 // the loop header. If so, we know it will always execute the same number of
28092868 // times as the loop. We also handle the case where the exit block *is* the
2810 // loop header. This is common for un-rotated loops. More extensive analysis
2811 // could be done to handle more cases here.
2869 // loop header. This is common for un-rotated loops.
2870 //
2871 // If both of those tests fail, walk up the unique predecessor chain to the
2872 // header, stopping if there is an edge that doesn't exit the loop. If the
2873 // header is reached, the execution count of the branch will be equal to the
2874 // trip count of the loop.
2875 //
2876 // More extensive analysis could be done to handle more cases here.
2877 //
28122878 if (ExitBr->getSuccessor(0) != L->getHeader() &&
28132879 ExitBr->getSuccessor(1) != L->getHeader() &&
2814 ExitBr->getParent() != L->getHeader())
2815 return CouldNotCompute;
2816
2817 ICmpInst *ExitCond = dyn_cast(ExitBr->getCondition());
2880 ExitBr->getParent() != L->getHeader()) {
2881 // The simple checks failed, try climbing the unique predecessor chain
2882 // up to the header.
2883 bool Ok = false;
2884 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
2885 BasicBlock *Pred = BB->getUniquePredecessor();
2886 if (!Pred)
2887 return CouldNotCompute;
2888 TerminatorInst *PredTerm = Pred->getTerminator();
2889 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
2890 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
2891 if (PredSucc == BB)
2892 continue;
2893 // If the predecessor has a successor that isn't BB and isn't
2894 // outside the loop, assume the worst.
2895 if (L->contains(PredSucc))
2896 return CouldNotCompute;
2897 }
2898 if (Pred == L->getHeader()) {
2899 Ok = true;
2900 break;
2901 }
2902 BB = Pred;
2903 }
2904 if (!Ok)
2905 return CouldNotCompute;
2906 }
2907
2908 // Procede to the next level to examine the exit condition expression.
2909 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
2910 ExitBr->getSuccessor(0),
2911 ExitBr->getSuccessor(1));
2912 }
2913
2914 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
2915 /// backedge of the specified loop will execute if its exit condition
2916 /// were a conditional branch of ExitCond, TBB, and FBB.
2917 ScalarEvolution::BackedgeTakenInfo
2918 ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
2919 Value *ExitCond,
2920 BasicBlock *TBB,
2921 BasicBlock *FBB) {
2922 // Check if the controlling expression for this loop is an and or or. In
2923 // such cases, an exact backedge-taken count may be infeasible, but a
2924 // maximum count may still be feasible.
2925 if (BinaryOperator *BO = dyn_cast(ExitCond)) {
2926 if (BO->getOpcode() == Instruction::And) {
2927 // Recurse on the operands of the and.
2928 BackedgeTakenInfo BTI0 =
2929 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
2930 BackedgeTakenInfo BTI1 =
2931 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
2932 SCEVHandle BECount = CouldNotCompute;
2933 SCEVHandle MaxBECount = CouldNotCompute;
2934 if (L->contains(TBB)) {
2935 // Both conditions must be true for the loop to continue executing.
2936 // Choose the less conservative count.
2937 // TODO: Take the minimum of the exact counts.
2938 if (BTI0.Exact == BTI1.Exact)
2939 BECount = BTI0.Exact;
2940 // TODO: Take the minimum of the maximum counts.
2941 if (BTI0.Max == CouldNotCompute)
2942 MaxBECount = BTI1.Max;
2943 else if (BTI1.Max == CouldNotCompute)
2944 MaxBECount = BTI0.Max;
2945 else if (const SCEVConstant *C0 = dyn_cast(BTI0.Max))
2946 if (const SCEVConstant *C1 = dyn_cast(BTI1.Max))
2947 MaxBECount = getConstant(APIntOps::umin(C0->getValue()->getValue(),
2948 C1->getValue()->getValue()));
2949 } else {
2950 // Both conditions must be true for the loop to exit.
2951 assert(L->contains(FBB) && "Loop block has no successor in loop!");
2952 if (BTI0.Exact != CouldNotCompute && BTI1.Exact != CouldNotCompute)
2953 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
2954 if (BTI0.Max != CouldNotCompute && BTI1.Max != CouldNotCompute)
2955 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
2956 }
2957
2958 return BackedgeTakenInfo(BECount, MaxBECount);
2959 }
2960 if (BO->getOpcode() == Instruction::Or) {
2961 // Recurse on the operands of the or.
2962 BackedgeTakenInfo BTI0 =
2963 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
2964 BackedgeTakenInfo BTI1 =
2965 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
2966 SCEVHandle BECount = CouldNotCompute;
2967 SCEVHandle MaxBECount = CouldNotCompute;
2968 if (L->contains(FBB)) {
2969 // Both conditions must be false for the loop to continue executing.
2970 // Choose the less conservative count.
2971 // TODO: Take the minimum of the exact counts.
2972 if (BTI0.Exact == BTI1.Exact)
2973 BECount = BTI0.Exact;
2974 // TODO: Take the minimum of the maximum counts.
2975 if (BTI0.Max == CouldNotCompute)
2976 MaxBECount = BTI1.Max;
2977 else if (BTI1.Max == CouldNotCompute)
2978 MaxBECount = BTI0.Max;
2979 else if (const SCEVConstant *C0 = dyn_cast(BTI0.Max))
2980 if (const SCEVConstant *C1 = dyn_cast(BTI1.Max))
2981 MaxBECount = getConstant(APIntOps::umin(C0->getValue()->getValue(),
2982 C1->getValue()->getValue()));
2983 } else {
2984 // Both conditions must be false for the loop to exit.
2985 assert(L->contains(TBB) && "Loop block has no successor in loop!");
2986 if (BTI0.Exact != CouldNotCompute && BTI1.Exact != CouldNotCompute)
2987 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
2988 if (BTI0.Max != CouldNotCompute && BTI1.Max != CouldNotCompute)
2989 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
2990 }
2991
2992 return BackedgeTakenInfo(BECount, MaxBECount);
2993 }
2994 }
2995
2996 // With an icmp, it may be feasible to compute an exact backedge-taken count.
2997 // Procede to the next level to examine the icmp.
2998 if (ICmpInst *ExitCondICmp = dyn_cast(ExitCond))
2999 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
28183000
28193001 // If it's not an integer or pointer comparison then compute it the hard way.
2820 if (ExitCond == 0)
2821 return ComputeBackedgeTakenCountExhaustively(L, ExitBr->getCondition(),
2822 ExitBr->getSuccessor(0) == ExitBlock);
3002 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3003 }
3004
3005 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3006 /// backedge of the specified loop will execute if its exit condition
3007 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3008 ScalarEvolution::BackedgeTakenInfo
3009 ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3010 ICmpInst *ExitCond,
3011 BasicBlock *TBB,
3012 BasicBlock *FBB) {
28233013
28243014 // If the condition was exit on true, convert the condition to exit on false
28253015 ICmpInst::Predicate Cond;
2826 if (ExitBr->getSuccessor(1) == ExitBlock)
3016 if (!L->contains(FBB))
28273017 Cond = ExitCond->getPredicate();
28283018 else
28293019 Cond = ExitCond->getInversePredicate();
28333023 if (Constant *RHS = dyn_cast(ExitCond->getOperand(1))) {
28343024 SCEVHandle ItCnt =
28353025 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
2836 if (!isa(ItCnt)) return ItCnt;
3026 if (!isa(ItCnt)) {
3027 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3028 return BackedgeTakenInfo(ItCnt,
3029 isa(ItCnt) ? ItCnt :
3030 getConstant(APInt::getMaxValue(BitWidth)-1));
3031 }
28373032 }
28383033
28393034 SCEVHandle LHS = getSCEV(ExitCond->getOperand(0));
29113106 break;
29123107 }
29133108 return
2914 ComputeBackedgeTakenCountExhaustively(L, ExitCond,
2915 ExitBr->getSuccessor(0) == ExitBlock);
3109 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
29163110 }
29173111
29183112 static ConstantInt *
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output > %t
1 ; RUN: grep sext %t | count 2
2 ; RUN: not grep {(sext} %t
3
4 ; ScalarEvolution should be able to compute a maximum trip count
5 ; value sufficient to fold away both sext casts.
6
7 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
8
9 define float @t(float* %pTmp1, float* %peakWeight, float* %nrgReducePeakrate, i32 %bim) nounwind {
10 entry:
11 %tmp3 = load float* %peakWeight, align 4 ; [#uses=2]
12 %tmp2538 = icmp sgt i32 %bim, 0 ; [#uses=1]
13 br i1 %tmp2538, label %bb.nph, label %bb4
14
15 bb.nph: ; preds = %entry
16 br label %bb
17
18 bb: ; preds = %bb1, %bb.nph
19 %distERBhi.036 = phi float [ %tmp10, %bb1 ], [ 0.000000e+00, %bb.nph ] ; [#uses=1]
20 %hiPart.035 = phi i32 [ %tmp12, %bb1 ], [ 0, %bb.nph ] ; [#uses=2]
21 %peakCount.034 = phi float [ %tmp19, %bb1 ], [ %tmp3, %bb.nph ] ; [#uses=1]
22 %tmp6 = sext i32 %hiPart.035 to i64 ; [#uses=1]
23 %tmp7 = getelementptr float* %pTmp1, i64 %tmp6 ; [#uses=1]
24 %tmp8 = load float* %tmp7, align 4 ; [#uses=1]
25 %tmp10 = fadd float %tmp8, %distERBhi.036 ; [#uses=3]
26 %tmp12 = add i32 %hiPart.035, 1 ; [#uses=3]
27 %tmp15 = sext i32 %tmp12 to i64 ; [#uses=1]
28 %tmp16 = getelementptr float* %peakWeight, i64 %tmp15 ; [#uses=1]
29 %tmp17 = load float* %tmp16, align 4 ; [#uses=1]
30 %tmp19 = fadd float %tmp17, %peakCount.034 ; [#uses=2]
31 br label %bb1
32
33 bb1: ; preds = %bb
34 %tmp21 = fcmp olt float %tmp10, 2.500000e+00 ; [#uses=1]
35 %tmp25 = icmp slt i32 %tmp12, %bim ; [#uses=1]
36 %tmp27 = and i1 %tmp21, %tmp25 ; [#uses=1]
37 br i1 %tmp27, label %bb, label %bb1.bb4_crit_edge
38
39 bb1.bb4_crit_edge: ; preds = %bb1
40 br label %bb4
41
42 bb4: ; preds = %bb1.bb4_crit_edge, %entry
43 %distERBhi.0.lcssa = phi float [ %tmp10, %bb1.bb4_crit_edge ], [ 0.000000e+00, %entry ] ; [#uses=1]
44 %peakCount.0.lcssa = phi float [ %tmp19, %bb1.bb4_crit_edge ], [ %tmp3, %entry ] ; [#uses=1]
45 %tmp31 = fdiv float %peakCount.0.lcssa, %distERBhi.0.lcssa ; [#uses=1]
46 ret float %tmp31
47 }