llvm.org GIT mirror llvm / 2d73c37
NewGVN: Fix PR/33187. This is a bug caused by two things: 1. When there is no perfect iteration order, we can't let phi nodes put themselves in terms of things that come later in the iteration order, or we will endlessly cycle (the normal RPO algorithm clears the hashtable to avoid this issue). 2. We are sometimes erasing the wrong expression (causing pessimism) because our equality says loads and stores are the same. We introduce an exact equality function and use it when erasing to make sure we erase only identical expressions, not equivalent ones. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304807 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
6 changed file(s) with 212 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
9292 }
9393
9494 virtual bool equals(const Expression &Other) const { return true; }
95 // Return true if the two expressions are exactly the same, including the
96 // normally ignored fields.
97 virtual bool exactlyEquals(const Expression &Other) const {
98 return getExpressionType() == Other.getExpressionType() && equals(Other);
99 }
95100
96101 unsigned getOpcode() const { return Opcode; }
97102 void setOpcode(unsigned opcode) { Opcode = opcode; }
344349 void setAlignment(unsigned Align) { Alignment = Align; }
345350
346351 bool equals(const Expression &Other) const override;
352 bool exactlyEquals(const Expression &Other) const override {
353 return Expression::exactlyEquals(Other) &&
354 cast(Other).getLoadInst() == getLoadInst();
355 }
347356
348357 //
349358 // Debugging support
381390 Value *getStoredValue() const { return StoredValue; }
382391
383392 bool equals(const Expression &Other) const override;
393 bool exactlyEquals(const Expression &Other) const override {
394 return Expression::exactlyEquals(Other) &&
395 cast(Other).getStoreInst() == getStoreInst();
396 }
384397
385398 // Debugging support
386399 //
377377 };
378378
379379 namespace llvm {
380 struct ExactEqualsExpression {
381 const Expression &E;
382 explicit ExactEqualsExpression(const Expression &E) : E(E) {}
383 hash_code getComputedHash() const { return E.getComputedHash(); }
384 bool operator==(const Expression &Other) const {
385 return E.exactlyEquals(Other);
386 }
387 };
388
380389 template <> struct DenseMapInfo {
381390 static const Expression *getEmptyKey() {
382391 auto Val = static_cast(-1);
389398 return reinterpret_cast(Val);
390399 }
391400 static unsigned getHashValue(const Expression *E) {
392 return static_cast(E->getComputedHash());
393 }
401 return E->getComputedHash();
402 }
403 static unsigned getHashValue(const ExactEqualsExpression &E) {
404 return E.getComputedHash();
405 }
406 static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS) {
407 if (RHS == getTombstoneKey() || RHS == getEmptyKey())
408 return false;
409 return LHS == *RHS;
410 }
411
394412 static bool isEqual(const Expression *LHS, const Expression *RHS) {
395413 if (LHS == RHS)
396414 return true;
847865 // Things in TOPClass are equivalent to everything.
848866 if (ValueToClass.lookup(*U) == TOPClass)
849867 return false;
868 if (lookupOperandLeader(*U) == PN)
869 return false;
850870 return true;
851871 });
852872 std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
15701590
15711591 // Evaluate PHI nodes symbolically, and create an expression result.
15721592 const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I) const {
1573 // Resolve irreducible and reducible phi cycles.
1574 // FIXME: This is hopefully a temporary solution while we resolve the issues
1575 // with fixpointing self-cycles. It currently should be "guaranteed" to be
1576 // correct, but non-optimal. The SCCFinder does not, for example, take
1577 // reachability of arguments into account, etc.
1578 SCCFinder.Start(I);
1579 bool CanOptimize = true;
1580 SmallPtrSet OuterOps;
1581
1582 auto &Component = SCCFinder.getComponentFor(I);
1583 for (auto *Member : Component) {
1584 if (!isa(Member)) {
1585 CanOptimize = false;
1586 break;
1587 }
1588 for (auto &PHIOp : cast(Member)->operands())
1589 if (!isa(PHIOp) || !Component.count(cast(PHIOp)))
1590 OuterOps.insert(PHIOp);
1591 }
1592 if (CanOptimize && OuterOps.size() == 1) {
1593 DEBUG(dbgs() << "Resolving cyclic phi to value " << *(*OuterOps.begin())
1594 << "\n");
1595 return createVariableOrConstant(*OuterOps.begin());
1596 }
15971593 // True if one of the incoming phi edges is a backedge.
15981594 bool HasBackedge = false;
15991595 // All constant tracks the state of whether all the *original* phi operands
16611657 if (!someEquivalentDominates(AllSameInst, I))
16621658 return E;
16631659 }
1664
1660 // Can't simplify to something that comes later in the iteration.
1661 // Otherwise, when and if it changes congruence class, we will never catch
1662 // up. We will always be a class behind it.
1663 if (isa(AllSameValue) &&
1664 InstrToDFSNum(AllSameValue) > InstrToDFSNum(I))
1665 return E;
16651666 NumGVNPhisAllSame++;
16661667 DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
16671668 << "\n");
21572158 if (OldClass->getDefiningExpr()) {
21582159 DEBUG(dbgs() << "Erasing expression " << *OldClass->getDefiningExpr()
21592160 << " from table\n");
2160 ExpressionToClass.erase(OldClass->getDefiningExpr());
2161 // We erase it as an exact expression to make sure we don't just erase an
2162 // equivalent one.
2163 auto Iter = ExpressionToClass.find_as(
2164 ExactEqualsExpression(*OldClass->getDefiningExpr()));
2165 if (Iter != ExpressionToClass.end())
2166 ExpressionToClass.erase(Iter);
2167 #ifdef EXPENSIVE_CHECKS
2168 assert(
2169 (*OldClass->getDefiningExpr() != *E || ExpressionToClass.lookup(E)) &&
2170 "We erased the expression we just inserted, which should not happen");
2171 #endif
21612172 }
21622173 } else if (OldClass->getLeader() == I) {
21632174 // When the leader changes, the value numbering of
22712282 auto *OldE = ValueToExpression.lookup(I);
22722283 // It could just be that the old class died. We don't want to erase it if we
22732284 // just moved classes.
2274 if (OldE && isa(OldE) && *E != *OldE)
2275 ExpressionToClass.erase(OldE);
2285 if (OldE && isa(OldE) && *E != *OldE) {
2286 // Erase this as an exact expression to ensure we don't erase expressions
2287 // equivalent to it.
2288 auto Iter = ExpressionToClass.find_as(ExactEqualsExpression(*OldE));
2289 if (Iter != ExpressionToClass.end())
2290 ExpressionToClass.erase(Iter);
2291 }
22762292 }
22772293 ValueToExpression[I] = E;
22782294 }
30593075 }
30603076 updateProcessedCount(CurrBlock);
30613077 }
3078 // Reset after processing (because we may mark ourselves as touched when
3079 // we propagate equalities).
3080 TouchedInstructions.reset(InstrNum);
30623081
30633082 if (auto *MP = dyn_cast(V)) {
30643083 DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n");
30693088 llvm_unreachable("Should have been a MemoryPhi or Instruction");
30703089 }
30713090 updateProcessedCount(V);
3072 // Reset after processing (because we may mark ourselves as touched when
3073 // we propagate equalities).
3074 TouchedInstructions.reset(InstrNum);
30753091 }
30763092 }
30773093 NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
394394 ; CHECK: g:
395395 ; CHECK-NEXT: [[N:%.*]] = phi i32* [ [[H:%.*]], [[I:%.*]] ], [ null, [[B:%.*]] ]
396396 ; CHECK-NEXT: [[H]] = getelementptr i32, i32* [[N]], i64 1
397 ; CHECK-NEXT: [[J:%.*]] = icmp eq i32* %h, inttoptr (i64 32 to i32*)
397 ; CHECK-NEXT: [[J:%.*]] = icmp eq i32* [[H]], inttoptr (i64 32 to i32*)
398398 ; CHECK-NEXT: br i1 [[J]], label [[C:%.*]], label [[I]]
399399 ; CHECK: i:
400400 ; CHECK-NEXT: br i1 undef, label [[K:%.*]], label [[G]]
1616 ; CHECK-NEXT: [[INC_I:%.*]] = add nsw i32 [[REFIDXLX_0]], 1
1717 ; CHECK-NEXT: br label [[FOR_BODY8_I:%.*]]
1818 ; CHECK: for.body8.i:
19 ; CHECK-NEXT: [[NIDX_052_I:%.*]] = phi i32 [ [[INC_I]], [[IF_THEN13]] ], [ [[NIDX_052_I]], [[FOR_INC24_I:%.*]] ]
20 ; CHECK-NEXT: br i1 undef, label [[FOR_INC24_I]], label [[IF_THEN17_I:%.*]]
19 ; CHECK-NEXT: br i1 undef, label [[FOR_INC24_I:%.*]], label [[IF_THEN17_I:%.*]]
2120 ; CHECK: if.then17.i:
2221 ; CHECK-NEXT: br label [[FOR_INC24_I]]
2322 ; CHECK: for.inc24.i:
66 ; CHECK-NEXT: br label [[BODY:%.*]]
77 ; CHECK: body:
88 ; CHECK-NEXT: store i64 undef, i64* [[B:%.*]]
9 ; CHECK-NEXT: [[B2:%.*]] = load i64, i64* [[B]]
109 ; CHECK-NEXT: br i1 undef, label [[BODY]], label [[END:%.*]]
1110 ; CHECK: end:
1211 ; CHECK-NEXT: br label [[BODY]]
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ;; Ensure we don't change after value numbering by accidentally deleting the wrong expression.
2 ; RUN: opt -newgvn -S %s | FileCheck %s
3 define void @fn1() local_unnamed_addr #0 {
4 ; CHECK-LABEL: @fn1(
5 ; CHECK-NEXT: entry:
6 ; CHECK-NEXT: br label [[FOR_COND_PREHEADER:%.*]]
7 ; CHECK: while.cond:
8 ; CHECK-NEXT: br label [[FOR_COND_PREHEADER]]
9 ; CHECK: for.cond.preheader:
10 ; CHECK-NEXT: [[H_031:%.*]] = phi i32 [ 5, [[ENTRY:%.*]] ], [ [[H_127:%.*]], [[WHILE_COND:%.*]] ]
11 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
12 ; CHECK: for.body:
13 ; CHECK-NEXT: [[H_128:%.*]] = phi i32 [ [[H_031]], [[FOR_COND_PREHEADER]] ], [ [[H_2:%.*]], [[FOR_INC:%.*]] ]
14 ; CHECK-NEXT: br label [[IF_THEN:%.*]]
15 ; CHECK: if.then:
16 ; CHECK-NEXT: br i1 false, label [[L_LOOPEXIT:%.*]], label [[IF_END:%.*]]
17 ; CHECK: if.end:
18 ; CHECK-NEXT: br i1 undef, label [[FOR_INC]], label [[IF_END9:%.*]]
19 ; CHECK: if.end9:
20 ; CHECK-NEXT: br label [[FOR_INC]]
21 ; CHECK: for.inc:
22 ; CHECK-NEXT: [[H_2]] = phi i32 [ [[H_128]], [[IF_END]] ], [ 0, [[IF_END9]] ]
23 ; CHECK-NEXT: br i1 undef, label [[WHILE_COND10_LOOPEXIT:%.*]], label [[FOR_BODY]]
24 ; CHECK: while.cond10.loopexit:
25 ; CHECK-NEXT: br label [[WHILE_COND10:%.*]]
26 ; CHECK: while.cond10:
27 ; CHECK-NEXT: [[H_127]] = phi i32 [ [[H_126:%.*]], [[IF_END18:%.*]] ], [ [[H_125:%.*]], [[L:%.*]] ], [ [[H_2]], [[WHILE_COND10_LOOPEXIT]] ]
28 ; CHECK-NEXT: br i1 undef, label [[WHILE_COND]], label [[WHILE_BODY12:%.*]]
29 ; CHECK: while.body12:
30 ; CHECK-NEXT: br i1 undef, label [[IF_END18]], label [[L]]
31 ; CHECK: L.loopexit:
32 ; CHECK-NEXT: store i8 undef, i8* null
33 ; CHECK-NEXT: br label [[L]]
34 ; CHECK: L:
35 ; CHECK-NEXT: [[H_125]] = phi i32 [ [[H_127]], [[WHILE_BODY12]] ], [ undef, [[L_LOOPEXIT]] ]
36 ; CHECK-NEXT: br i1 undef, label [[WHILE_COND10]], label [[IF_END18]]
37 ; CHECK: if.end18:
38 ; CHECK-NEXT: [[H_126]] = phi i32 [ [[H_125]], [[L]] ], [ [[H_127]], [[WHILE_BODY12]] ]
39 ; CHECK-NEXT: br label [[WHILE_COND10]]
40 ;
41 entry:
42 br label %for.cond.preheader
43
44 while.cond: ; preds = %while.cond10
45 br label %for.cond.preheader
46
47 for.cond.preheader: ; preds = %while.cond, %entry
48 %h.031 = phi i32 [ 5, %entry ], [ %h.127, %while.cond ]
49 br label %for.body
50
51 for.body: ; preds = %for.inc, %for.cond.preheader
52 %h.128 = phi i32 [ %h.031, %for.cond.preheader ], [ %h.2, %for.inc ]
53 br label %if.then
54
55 if.then: ; preds = %for.body
56 br i1 false, label %L.loopexit, label %if.end
57
58 if.end: ; preds = %if.then
59 br i1 undef, label %for.inc, label %if.end9
60
61 if.end9: ; preds = %if.end
62 br label %for.inc
63
64 for.inc: ; preds = %if.end9, %if.end
65 %h.2 = phi i32 [ %h.128, %if.end ], [ 0, %if.end9 ]
66 br i1 undef, label %while.cond10.loopexit, label %for.body
67
68 while.cond10.loopexit: ; preds = %for.inc
69 %h.2.lcssa = phi i32 [ %h.2, %for.inc ]
70 br label %while.cond10
71
72 while.cond10: ; preds = %if.end18, %L, %while.cond10.loopexit
73 %h.127 = phi i32 [ %h.126, %if.end18 ], [ %h.125, %L ], [ %h.2.lcssa, %while.cond10.loopexit ]
74 br i1 undef, label %while.cond, label %while.body12
75
76 while.body12: ; preds = %while.cond10
77 br i1 undef, label %if.end18, label %L
78
79 L.loopexit: ; preds = %if.then
80 br label %L
81
82 L: ; preds = %L.loopexit, %while.body12
83 %h.125 = phi i32 [ %h.127, %while.body12 ], [ undef, %L.loopexit ]
84 br i1 undef, label %while.cond10, label %if.end18
85
86 if.end18: ; preds = %L, %while.body12
87 %h.126 = phi i32 [ %h.125, %L ], [ %h.127, %while.body12 ]
88 br label %while.cond10
89 }
90
91
92 define void @hoge() local_unnamed_addr #0 {
93 ; CHECK-LABEL: @hoge(
94 ; CHECK-NEXT: bb:
95 ; CHECK-NEXT: br label [[BB1:%.*]]
96 ; CHECK: bb1:
97 ; CHECK-NEXT: [[TMP:%.*]] = phi i64 [ 0, [[BB:%.*]] ], [ [[TMP2:%.*]], [[BB1]] ]
98 ; CHECK-NEXT: [[TMP2]] = add nuw nsw i64 [[TMP]], 1
99 ; CHECK-NEXT: br label [[BB1]]
100 ;
101 bb:
102 br label %bb1
103
104 bb1: ; preds = %bb1, %bb
105 %tmp = phi i64 [ 0, %bb ], [ %tmp2, %bb1 ]
106 %tmp2 = add nuw nsw i64 %tmp, 1
107 br label %bb1
108 }
109
110 attributes #0 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
111
112
113 source_filename = "pr33187-c.ll"
114
115 define void @a() {
116 ; CHECK-LABEL: @a(
117 ; CHECK-NEXT: b:
118 ; CHECK-NEXT: store i8* null, i8** null
119 ; CHECK-NEXT: br label [[D:%.*]]
120 ; CHECK: d:
121 ; CHECK-NEXT: [[I:%.*]] = phi i8* [ null, [[B:%.*]] ], [ [[E:%.*]], [[F:%.*]] ]
122 ; CHECK-NEXT: br i1 undef, label [[F]], label [[G:%.*]]
123 ; CHECK: g:
124 ; CHECK-NEXT: store i8* [[I]], i8** null
125 ; CHECK-NEXT: unreachable
126 ; CHECK: f:
127 ; CHECK-NEXT: [[E]] = getelementptr i8, i8* [[I]], i64 1
128 ; CHECK-NEXT: br label [[D]]
129 ;
130 b:
131 store i8* null, i8** null
132 br label %d
133
134 d: ; preds = %f, %b
135 %i = phi i8* [ null, %b ], [ %e, %f ]
136 br i1 undef, label %f, label %g
137
138 g: ; preds = %d
139 %h = phi i8* [ %i, %d ]
140 store i8* %h, i8** null
141 unreachable
142
143 f: ; preds = %d
144 %e = getelementptr i8, i8* %i, i64 1
145 br label %d
146 }
147