llvm.org GIT mirror llvm / 526c80b
Speculatively revert commits 142790 and 142843 to see if it fixes the dragonegg and llvm-gcc self-host buildbots. 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@142916 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
3 changed file(s) with 28 addition(s) and 109 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!
48464848 if (NextPHI == 0)
48474849 return 0; // Couldn't evaluate!
48484850 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) { // 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
4860 if (NextPHI) continue; // Already computed!
4861
4862 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4863 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4864 }
48734865 CurrentIterVals.swap(NextIterVals);
48744866 }
48754867 }
48894881 // That's the only form we support here.
48904882 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
48914883
4892 DenseMap CurrentIterVals;
4893 BasicBlock *Header = L->getHeader();
4894 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
4895
48964884 // One entry must be a constant (coming in from outside of the loop), and the
48974885 // second must be derived from the same PHI.
48984886 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
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();
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.
49094895
49104896 // Okay, we find a PHI node that defines the trip count of this loop. Execute
49114897 // the loop symbolically to determine when the condition gets a value of
49124898 // "ExitWhen".
4913
4914 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
4915 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
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;
49164905 ConstantInt *CondVal =
4917 dyn_cast_or_null(EvaluateExpression(Cond, L,
4918 CurrentIterVals, TD));
4906 dyn_cast_or_null(EvaluateExpression(Cond, L, PHIValMap, TD));
49194907
49204908 // Couldn't symbolically evaluate.
49214909 if (!CondVal) return getCouldNotCompute();
49254913 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
49264914 }
49274915
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);
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;
49414921 }
49424922
49434923 // Too many iterations were needed to evaluate.
0 ; RUN: opt -analyze -scalar-evolution < %s 2>&1 | FileCheck %s
1 ; PR11034
12
23 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"
34 target triple = "i386-pc-linux-gnu"
56 @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
67 @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
78
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
-29
test/Analysis/ScalarEvolution/trip-count11.ll less more
None ; 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 }