llvm.org GIT mirror llvm / 795cb48
Enhance SCEV's brute force loop analysis to handle multiple PHI nodes in the loop header when computing the trip count. With this, we now constant evaluate: struct ListNode { const struct ListNode *next; int i; }; static const struct ListNode node1 = {0, 1}; static const struct ListNode node2 = {&node1, 2}; static const struct ListNode node3 = {&node2, 3}; int test() { int sum = 0; for (const struct ListNode *n = &node3; n != 0; n = n->next) sum += n->i; return sum; } git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142781 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
2 changed file(s) with 65 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
48814881 // That's the only form we support here.
48824882 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
48834883
4884 DenseMap CurrentIterVals;
4885 BasicBlock *Header = L->getHeader();
4886 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
4887
48844888 // One entry must be a constant (coming in from outside of the loop), and the
48854889 // second must be derived from the same PHI.
48864890 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4887 Constant *StartCST =
4888 dyn_cast(PN->getIncomingValue(!SecondIsBackedge));
4889 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant.
4890
4891 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
4892 if (getConstantEvolvingPHI(BEValue, L) != PN &&
4893 !isa(BEValue))
4894 return getCouldNotCompute(); // Not derived from same PHI.
4891 PHINode *PHI = 0;
4892 for (BasicBlock::iterator I = Header->begin();
4893 (PHI = dyn_cast(I)); ++I) {
4894 Constant *StartCST =
4895 dyn_cast(PHI->getIncomingValue(!SecondIsBackedge));
4896 if (StartCST == 0) continue;
4897 CurrentIterVals[PHI] = StartCST;
4898 }
4899 if (!CurrentIterVals.count(PN))
4900 return getCouldNotCompute();
48954901
48964902 // Okay, we find a PHI node that defines the trip count of this loop. Execute
48974903 // the loop symbolically to determine when the condition gets a value of
48984904 // "ExitWhen".
4899 unsigned IterationNum = 0;
4900 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
4901 for (Constant *PHIVal = StartCST;
4902 IterationNum != MaxIterations; ++IterationNum) {
4903 DenseMap PHIValMap;
4904 PHIValMap[PN] = PHIVal;
4905
4906 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
4907 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
49054908 ConstantInt *CondVal =
4906 dyn_cast_or_null(EvaluateExpression(Cond, L, PHIValMap, TD));
4909 dyn_cast_or_null(EvaluateExpression(Cond, L,
4910 CurrentIterVals, TD));
49074911
49084912 // Couldn't symbolically evaluate.
49094913 if (!CondVal) return getCouldNotCompute();
49134917 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
49144918 }
49154919
4916 // Compute the value of the PHI node for the next iteration.
4917 Constant *NextPHI = EvaluateExpression(BEValue, L, PHIValMap, TD);
4918 if (NextPHI == 0 || NextPHI == PHIVal)
4919 return getCouldNotCompute();// Couldn't evaluate or not making progress...
4920 PHIVal = NextPHI;
4920 // Update all the PHI nodes for the next iteration.
4921 DenseMap NextIterVals;
4922 for (DenseMap::const_iterator
4923 I = CurrentIterVals.begin(), E = CurrentIterVals.end(); I != E; ++I){
4924 PHINode *PHI = dyn_cast(I->first);
4925 if (!PHI) continue;
4926 Constant *&NextPHI = NextIterVals[PHI];
4927 if (NextPHI) continue; // Already computed!
4928
4929 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4930 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4931 }
4932 CurrentIterVals.swap(NextIterVals);
49214933 }
49224934
49234935 // Too many iterations were needed to evaluate.
0 ; RUN: opt -analyze -scalar-evolution < %s 2>&1 | FileCheck %s
1 ; PR11034
21
32 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
43 target triple = "i386-pc-linux-gnu"
65 @arr1 = internal unnamed_addr constant [50 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50], align 4
76 @arr2 = internal unnamed_addr constant [50 x i32] [i32 49, i32 48, i32 47, i32 46, i32 45, i32 44, i32 43, i32 42, i32 41, i32 40, i32 39, i32 38, i32 37, i32 36, i32 35, i32 34, i32 33, i32 32, i32 31, i32 30, i32 29, i32 28, i32 27, i32 26, i32 25, i32 24, i32 23, i32 22, i32 21, i32 20, i32 19, i32 18, i32 17, i32 16, i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0], align 4
87
8 ; PR11034
99 define i32 @test1() nounwind readnone {
1010 ; CHECK: test1
1111 entry:
3030 for.end: ; preds = %for.body
3131 ret i32 %add2
3232 }
33
34
35 %struct.ListNode = type { %struct.ListNode*, i32 }
36
37 @node5 = internal constant { %struct.ListNode*, i32, [4 x i8] } { %struct.ListNode* bitcast ({ %struct.ListNode*, i32, [4 x i8] }* @node4 to %struct.ListNode*), i32 4, [4 x i8] undef }, align 8
38 @node4 = internal constant { %struct.ListNode*, i32, [4 x i8] } { %struct.ListNode* bitcast ({ %struct.ListNode*, i32, [4 x i8] }* @node3 to %struct.ListNode*), i32 3, [4 x i8] undef }, align 8
39 @node3 = internal constant { %struct.ListNode*, i32, [4 x i8] } { %struct.ListNode* bitcast ({ %struct.ListNode*, i32, [4 x i8] }* @node2 to %struct.ListNode*), i32 2, [4 x i8] undef }, align 8
40 @node2 = internal constant { %struct.ListNode*, i32, [4 x i8] } { %struct.ListNode* bitcast ({ %struct.ListNode*, i32, [4 x i8] }* @node1 to %struct.ListNode*), i32 1, [4 x i8] undef }, align 8
41 @node1 = internal constant { %struct.ListNode*, i32, [4 x i8] } { %struct.ListNode* null, i32 0, [4 x i8] undef }, align 8
42
43 define i32 @test2() nounwind uwtable readonly {
44 ; CHECK: test2
45 entry:
46 br label %for.body
47
48 for.body: ; preds = %entry, %for.body
49 %sum.02 = phi i32 [ 0, %entry ], [ %add, %for.body ]
50 ; CHECK: --> %sum.02{{ *}}Exits: 10
51 %n.01 = phi %struct.ListNode* [ bitcast ({ %struct.ListNode*, i32, [4 x i8] }* @node5 to %struct.ListNode*), %entry ], [ %1, %for.body ]
52 ; CHECK: --> %n.01{{ *}}Exits: @node1
53 %i = getelementptr inbounds %struct.ListNode* %n.01, i64 0, i32 1
54 %0 = load i32* %i, align 4
55 %add = add nsw i32 %0, %sum.02
56 %next = getelementptr inbounds %struct.ListNode* %n.01, i64 0, i32 0
57 %1 = load %struct.ListNode** %next, align 8
58 ; CHECK: --> %1{{ *}}Exits: 0
59 %cmp = icmp eq %struct.ListNode* %1, null
60 br i1 %cmp, label %for.end, label %for.body
61
62 for.end: ; preds = %for.body
63 ret i32 %add
64 }