llvm.org GIT mirror llvm / f8a9eb1
Restore commits 142790 and 142843 - they weren't breaking the build bots. Original commit messages: - Reapply r142781 with fix. Original message: 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; } - Now that we look at all the header PHIs, we need to consider all the header PHIs when deciding that the loop has stopped evolving. Fixes miscompile in the gcc torture testsuite! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142919 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
3 changed file(s) with 109 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
48434843 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
48444844 DenseMap NextIterVals;
48454845 Constant *NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4846 if (NextPHI == CurrentIterVals[PN])
4847 return RetVal = NextPHI; // Stopped evolving!
48484846 if (NextPHI == 0)
48494847 return 0; // Couldn't evaluate!
48504848 NextIterVals[PN] = NextPHI;
4849
4850 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
48514851
48524852 // Also evaluate the other PHI nodes. However, we don't get to stop if we
48534853 // cease to be able to evaluate one of them or if they stop evolving,
48574857 PHINode *PHI = dyn_cast(I->first);
48584858 if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
48594859 Constant *&NextPHI = NextIterVals[PHI];
4860 if (NextPHI) continue; // Already computed!
4861
4862 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4863 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4864 }
4860 if (!NextPHI) { // Not already computed.
4861 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4862 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4863 }
4864 if (NextPHI != I->second)
4865 StoppedEvolving = false;
4866 }
4867
4868 // If all entries in CurrentIterVals == NextIterVals then we can stop
4869 // iterating, the loop can't continue to change.
4870 if (StoppedEvolving)
4871 return RetVal = CurrentIterVals[PN];
4872
48654873 CurrentIterVals.swap(NextIterVals);
48664874 }
48674875 }
48814889 // That's the only form we support here.
48824890 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
48834891
4892 DenseMap CurrentIterVals;
4893 BasicBlock *Header = L->getHeader();
4894 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
4895
48844896 // One entry must be a constant (coming in from outside of the loop), and the
48854897 // second must be derived from the same PHI.
48864898 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.
4899 PHINode *PHI = 0;
4900 for (BasicBlock::iterator I = Header->begin();
4901 (PHI = dyn_cast(I)); ++I) {
4902 Constant *StartCST =
4903 dyn_cast(PHI->getIncomingValue(!SecondIsBackedge));
4904 if (StartCST == 0) continue;
4905 CurrentIterVals[PHI] = StartCST;
4906 }
4907 if (!CurrentIterVals.count(PN))
4908 return getCouldNotCompute();
48954909
48964910 // Okay, we find a PHI node that defines the trip count of this loop. Execute
48974911 // the loop symbolically to determine when the condition gets a value of
48984912 // "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;
4913
4914 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
4915 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
49054916 ConstantInt *CondVal =
4906 dyn_cast_or_null(EvaluateExpression(Cond, L, PHIValMap, TD));
4917 dyn_cast_or_null(EvaluateExpression(Cond, L,
4918 CurrentIterVals, TD));
49074919
49084920 // Couldn't symbolically evaluate.
49094921 if (!CondVal) return getCouldNotCompute();
49134925 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
49144926 }
49154927
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;
4928 // Update all the PHI nodes for the next iteration.
4929 DenseMap NextIterVals;
4930 for (DenseMap::const_iterator
4931 I = CurrentIterVals.begin(), E = CurrentIterVals.end(); I != E; ++I){
4932 PHINode *PHI = dyn_cast(I->first);
4933 if (!PHI || PHI->getParent() != Header) continue;
4934 Constant *&NextPHI = NextIterVals[PHI];
4935 if (NextPHI) continue; // Already computed!
4936
4937 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4938 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4939 }
4940 CurrentIterVals.swap(NextIterVals);
49214941 }
49224942
49234943 // 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 }
0 ; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s
1
2 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-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @foo.a = internal constant [8 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7], align 16
6
7 define i32 @foo() nounwind uwtable noinline {
8 entry:
9 br label %for.cond
10
11 for.cond: ; preds = %for.inc, %entry
12 %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ]
13 ; CHECK: --> %sum.0 Exits: 28
14 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
15 %cmp = icmp ult i32 %i.0, 8
16 br i1 %cmp, label %for.inc, label %for.end
17
18 for.inc: ; preds = %for.cond
19 %idxprom = sext i32 %i.0 to i64
20 %arrayidx = getelementptr inbounds [8 x i32]* @foo.a, i64 0, i64 %idxprom
21 %0 = load i32* %arrayidx, align 4
22 %add = add nsw i32 %sum.0, %0
23 %inc = add nsw i32 %i.0, 1
24 br label %for.cond
25
26 for.end: ; preds = %for.cond
27 ret i32 %sum.0
28 }