llvm.org GIT mirror llvm / 55d8857
NewGVN: Fix PR32838. This is a complicated bug involving two issues: 1. What do we do with phi nodes when we prove all arguments are not live? 2. When is it safe to use value leaders to determine if we can ignore an argumnet? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303453 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
3 changed file(s) with 222 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
3939 ET_Base,
4040 ET_Constant,
4141 ET_Variable,
42 ET_Dead,
4243 ET_Unknown,
4344 ET_BasicStart,
4445 ET_Basic,
514515 }
515516 };
516517
518 class DeadExpression final : public Expression {
519 public:
520 DeadExpression() : Expression(ET_Dead) {}
521 DeadExpression(const DeadExpression &) = delete;
522 DeadExpression &operator=(const DeadExpression &) = delete;
523
524 static bool classof(const Expression *E) {
525 return E->getExpressionType() == ET_Dead;
526 }
527 };
528
517529 class VariableExpression final : public Expression {
518530 private:
519531 Value *VariableValue;
528528 using ExpressionClassMap = DenseMap;
529529 ExpressionClassMap ExpressionToClass;
530530
531 // We have a single expression that represents currently DeadExpressions.
532 // For dead expressions we can prove will stay dead, we mark them with
533 // DFS number zero. However, it's possible in the case of phi nodes
534 // for us to assume/prove all arguments are dead during fixpointing.
535 // We use DeadExpression for that case.
536 DeadExpression *SingletonDeadExpression = nullptr;
537
531538 // Which values have changed as a result of leader changes.
532539 SmallPtrSet LeaderChanges;
533540
582589 Value *) const;
583590 PHIExpression *createPHIExpression(Instruction *, bool &HasBackEdge,
584591 bool &OriginalOpsConstant) const;
592 const DeadExpression *createDeadExpression() const;
585593 const VariableExpression *createVariableExpression(Value *) const;
586594 const ConstantExpression *createConstantExpression(Constant *) const;
587595 const Expression *createVariableOrConstant(Value *V) const;
854862 HasBackedge = HasBackedge || isBackedge(BB, PHIBlock);
855863 OriginalOpsConstant =
856864 OriginalOpsConstant && isa(*U);
857
858 // Don't try to transform self-defined phis.
865 // Use nullptr to distinguish between things that were
866 // originally self-defined and those that have an operand
867 // leader that is self-defined.
859868 if (*U == PN)
860 return PN;
869 return nullptr;
870 // Things in TOPClass are equivalent to everything.
871 if (ValueToClass.lookup(*U) == TOPClass)
872 return nullptr;
861873 return lookupOperandLeader(*U);
862874 });
863875 return E;
10541066 llvm_unreachable("Unhandled type of aggregate value operation");
10551067 }
10561068
1069 const DeadExpression *NewGVN::createDeadExpression() const {
1070 // DeadExpression has no arguments and all DeadExpression's are the same,
1071 // so we only need one of them.
1072 return SingletonDeadExpression;
1073 }
1074
10571075 const VariableExpression *NewGVN::createVariableExpression(Value *V) const {
10581076 auto *E = new (ExpressionAllocator) VariableExpression(V);
10591077 E->setOpcode(V->getValueID());
11251143 Value *NewGVN::lookupOperandLeader(Value *V) const {
11261144 CongruenceClass *CC = ValueToClass.lookup(V);
11271145 if (CC) {
1128 // Everything in TOP is represneted by undef, as it can be any value.
1146 // Everything in TOP is represented by undef, as it can be any value.
11291147 // We do have to make sure we get the type right though, so we can't set the
11301148 // RepLeader to undef.
11311149 if (CC == TOPClass)
15631581 // True if one of the incoming phi edges is a backedge.
15641582 bool HasBackedge = false;
15651583 // All constant tracks the state of whether all the *original* phi operands
1566 // were constant. This is really shorthand for "this phi cannot cycle due
1567 // to forward propagation", as any change in value of the phi is guaranteed
1568 // not to later change the value of the phi.
1569 // IE it can't be v = phi(undef, v+1)
1584 // This is really shorthand for "this phi cannot cycle due to forward
1585 // change in value of the phi is guaranteed not to later change the value of
1586 // the phi. IE it can't be v = phi(undef, v+1)
15701587 bool AllConstant = true;
15711588 auto *E =
15721589 cast(createPHIExpression(I, HasBackedge, AllConstant));
15741591 // See if all arguments are the same.
15751592 // We track if any were undef because they need special handling.
15761593 bool HasUndef = false;
1577 auto Filtered = make_filter_range(E->operands(), [&](const Value *Arg) {
1578 if (Arg == I)
1594 bool CycleFree = isCycleFree(cast(I));
1595 auto Filtered = make_filter_range(E->operands(), [&](Value *Arg) {
1596 if (Arg == nullptr)
1597 return false;
1598 // Original self-operands are already eliminated during expression creation.
1599 // We can only eliminate value-wise self-operands if it's cycle
1600 // free. Otherwise, eliminating the operand can cause our value to change,
1601 // which can cause us to not eliminate the operand, which changes the value
1602 // back to what it was before, cycling forever.
1603 if (CycleFree && Arg == I)
15791604 return false;
15801605 if (isa(Arg)) {
15811606 HasUndef = true;
15831608 }
15841609 return true;
15851610 });
1586 // If we are left with no operands, it's undef
1611 // If we are left with no operands, it's dead.
15871612 if (Filtered.begin() == Filtered.end()) {
1588 DEBUG(dbgs() << "Simplified PHI node " << *I << " to undef"
1589 << "\n");
1613 DEBUG(dbgs() << "No arguments of PHI node " << *I << " are live\n");
15901614 deleteExpression(E);
1591 return createConstantExpression(UndefValue::get(I->getType()));
1615 return createDeadExpression();
15921616 }
15931617 unsigned NumOps = 0;
15941618 Value *AllSameValue = *(Filtered.begin());
15951619 ++Filtered.begin();
15961620 // Can't use std::equal here, sadly, because filter.begin moves.
1597 if (llvm::all_of(Filtered, [AllSameValue, &NumOps](const Value *V) {
1621 if (llvm::all_of(Filtered, [&](Value *Arg) {
15981622 ++NumOps;
1599 return V == AllSameValue;
1623 return Arg == AllSameValue;
16001624 })) {
16011625 // In LLVM's non-standard representation of phi nodes, it's possible to have
16021626 // phi nodes with cycles (IE dependent on other phis that are .... dependent
16151639 // constants, or all operands are ignored but the undef, it also must be
16161640 // cycle free.
16171641 if (!AllConstant && HasBackedge && NumOps > 0 &&
1618 !isa(AllSameValue) && !isCycleFree(I))
1642 !isa(AllSameValue) && !CycleFree)
16191643 return E;
16201644
16211645 // Only have to check for instructions
21492173 void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
21502174 // This is guaranteed to return something, since it will at least find
21512175 // TOP.
2152 CongruenceClass *IClass = ValueToClass[I];
2176
2177 CongruenceClass *IClass = ValueToClass.lookup(I);
21532178 assert(IClass && "Should have found a IClass");
21542179 // Dead classes should have been eliminated from the mapping.
21552180 assert(!IClass->isDead() && "Found a dead class");
21582183 HashedExpression HE(E);
21592184 if (const auto *VE = dyn_cast(E)) {
21602185 EClass = ValueToClass.lookup(VE->getVariableValue());
2161 } else {
2186 } else if (isa(E)) {
2187 EClass = TOPClass;
2188 }
2189 if (!EClass) {
21622190 auto lookupResult = ExpressionToClass.insert_as({E, nullptr}, HE);
21632191
21642192 // If it's not in the value table, create a new congruence class.
30523080 bool Changed = false;
30533081 NumFuncArgs = F.arg_size();
30543082 MSSAWalker = MSSA->getWalker();
3083 SingletonDeadExpression = new (ExpressionAllocator) DeadExpression();
30553084
30563085 // Count number of instructions for sizing of hash tables, and come
30573086 // up with a global dfs numbering for instructions.
35363565 continue;
35373566 // Everything still in the TOP class is unreachable or dead.
35383567 if (CC == TOPClass) {
3539 #ifndef NDEBUG
3540 for (auto M : *CC)
3568 for (auto M : *CC) {
3569 auto *VTE = ValueToExpression.lookup(M);
3570 if (VTE && isa(VTE))
3571 markInstructionForDeletion(cast(M));
35413572 assert((!ReachableBlocks.count(cast(M)->getParent()) ||
35423573 InstructionsToErase.count(cast(M))) &&
35433574 "Everything in TOP should be unreachable or dead at this "
35443575 "point");
3545 #endif
3576 }
35463577 continue;
35473578 }
35483579
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ;RUN: opt -newgvn -S < %s | FileCheck %s
2 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-apple-macosx10.12.0"
4 ;; Ensure we don't infinite loop when all phi arguments are really unreachable or self-defined
5 define void @fn1(i64 %arg) {
6 ; CHECK-LABEL: @fn1(
7 ; CHECK-NEXT: entry:
8 ; CHECK-NEXT: br i1 undef, label [[IF_THEN:%.*]], label [[COND_TRUE:%.*]]
9 ; CHECK: if.then:
10 ; CHECK-NEXT: br i1 false, label [[FIRSTPHIBLOCK:%.*]], label [[TEMP:%.*]]
11 ; CHECK: firstphiblock:
12 ; CHECK-NEXT: br i1 undef, label %for.cond17thread-pre-split, label [[SECONDPHIBLOCK:%.*]]
13 ; CHECK: secondphiblock:
14 ; CHECK-NEXT: [[SECONDPHI:%.*]] = phi i64 [ [[THIRDPHI:%.*]], [[THIRDPHIBLOCK:%.*]] ], [ undef, [[FIRSTPHIBLOCK]] ]
15 ; CHECK-NEXT: br i1 undef, label [[FIRSTPHIBLOCK]], label [[THIRDPHIBLOCK]]
16 ; CHECK: thirdphiblock:
17 ; CHECK-NEXT: [[THIRDPHI]] = phi i64 [ [[SECONDPHI]], [[SECONDPHIBLOCK]] ], [ [[DIV:%.*]], [[COND_TRUE]] ]
18 ; CHECK-NEXT: br label [[SECONDPHIBLOCK]]
19 ; CHECK: for.cond17thread-pre-split:
20 ; CHECK-NEXT: br label [[COND_TRUE]]
21 ; CHECK: cond.true:
22 ; CHECK-NEXT: [[DIV]] = sdiv i64 [[ARG:%.*]], 4
23 ; CHECK-NEXT: br label [[THIRDPHIBLOCK]]
24 ; CHECK: temp:
25 ; CHECK-NEXT: ret void
26 ;
27 entry:
28 br i1 undef, label %if.then, label %cond.true
29 if.then:
30 br i1 false, label %firstphiblock, label %temp
31 firstphiblock:
32 %firstphi = phi i64 [ %arg, %if.then ], [ undef, %secondphiblock ]
33 br i1 undef, label %for.cond17thread-pre-split, label %secondphiblock
34 secondphiblock:
35 %secondphi = phi i64 [ %thirdphi, %thirdphiblock ], [ %firstphi, %firstphiblock ]
36 br i1 undef, label %firstphiblock, label %thirdphiblock
37 thirdphiblock:
38 %thirdphi = phi i64 [ %secondphi, %secondphiblock ], [ %div, %cond.true ]
39 br label %secondphiblock
40 for.cond17thread-pre-split:
41 br label %cond.true
42 cond.true:
43 %fourthphi = phi i64 [ %arg, %entry ], [ %firstphi, %for.cond17thread-pre-split ]
44 %div = sdiv i64 %fourthphi, 4
45 br label %thirdphiblock
46 temp:
47 ret void
48 }
49 define void @fn2(i64 %arg) {
50 ; CHECK-LABEL: @fn2(
51 ; CHECK-NEXT: entry:
52 ; CHECK-NEXT: br i1 undef, label [[IF_THEN:%.*]], label [[COND_TRUE:%.*]]
53 ; CHECK: if.then:
54 ; CHECK-NEXT: br i1 false, label [[FIRSTPHIBLOCK:%.*]], label [[TEMP:%.*]]
55 ; CHECK: firstphiblock:
56 ; CHECK-NEXT: [[FIRSTPHI:%.*]] = phi i64 [ undef, [[IF_THEN]] ], [ [[SECONDPHI:%.*]], [[SECONDPHIBLOCK:%.*]] ]
57 ; CHECK-NEXT: br i1 undef, label %for.cond17thread-pre-split, label [[SECONDPHIBLOCK]]
58 ; CHECK: secondphiblock:
59 ; CHECK-NEXT: [[SECONDPHI]] = phi i64 [ [[THIRDPHI:%.*]], [[THIRDPHIBLOCK:%.*]] ], [ [[FIRSTPHI]], [[FIRSTPHIBLOCK]] ]
60 ; CHECK-NEXT: br i1 undef, label [[FIRSTPHIBLOCK]], label [[THIRDPHIBLOCK]]
61 ; CHECK: thirdphiblock:
62 ; CHECK-NEXT: [[THIRDPHI]] = phi i64 [ [[SECONDPHI]], [[SECONDPHIBLOCK]] ], [ [[DIV:%.*]], [[COND_TRUE]] ]
63 ; CHECK-NEXT: br label [[SECONDPHIBLOCK]]
64 ; CHECK: for.cond17thread-pre-split:
65 ; CHECK-NEXT: br label [[COND_TRUE]]
66 ; CHECK: cond.true:
67 ; CHECK-NEXT: [[FOURTHPHI:%.*]] = phi i64 [ [[ARG:%.*]], [[ENTRY:%.*]] ], [ [[FIRSTPHI]], %for.cond17thread-pre-split ]
68 ; CHECK-NEXT: [[DIV]] = sdiv i64 [[FOURTHPHI]], 4
69 ; CHECK-NEXT: br label [[THIRDPHIBLOCK]]
70 ; CHECK: temp:
71 ; CHECK-NEXT: ret void
72 ;
73 entry:
74 br i1 undef, label %if.then, label %cond.true
75 if.then:
76 br i1 false, label %firstphiblock, label %temp
77 firstphiblock:
78 %firstphi = phi i64 [ %arg, %if.then ], [ %secondphi, %secondphiblock ]
79 br i1 undef, label %for.cond17thread-pre-split, label %secondphiblock
80 secondphiblock:
81 %secondphi = phi i64 [ %thirdphi, %thirdphiblock ], [ %firstphi, %firstphiblock ]
82 br i1 undef, label %firstphiblock, label %thirdphiblock
83 thirdphiblock:
84 %thirdphi = phi i64 [ %secondphi, %secondphiblock ], [ %div, %cond.true ]
85 br label %secondphiblock
86 for.cond17thread-pre-split:
87 br label %cond.true
88 cond.true:
89 %fourthphi = phi i64 [ %arg, %entry ], [ %firstphi, %for.cond17thread-pre-split ]
90 %div = sdiv i64 %fourthphi, 4
91 br label %thirdphiblock
92 temp:
93 ret void
94 }
95 @b = external global i32, align 4
96 @a = external global i32, align 4
97 define void @fn3() {
98 ; CHECK-LABEL: @fn3(
99 ; CHECK-NEXT: entry:
100 ; CHECK-NEXT: br label [[L1:%.*]]
101 ; CHECK: l1.loopexit:
102 ; CHECK-NEXT: br label [[L1]]
103 ; CHECK: l1:
104 ; CHECK-NEXT: [[F_0:%.*]] = phi i32* [ @b, [[ENTRY:%.*]] ], [ @a, [[L1_LOOPEXIT:%.*]] ]
105 ; CHECK-NEXT: br label [[FOR_COND:%.*]]
106 ; CHECK: for.cond.loopexit:
107 ; CHECK-NEXT: store i8 undef, i8* null
108 ; CHECK-NEXT: br label [[FOR_COND]]
109 ; CHECK: for.cond:
110 ; CHECK-NEXT: br i1 undef, label [[FOR_END14:%.*]], label [[FOR_COND1_PREHEADER:%.*]]
111 ; CHECK: for.cond1.preheader:
112 ; CHECK-NEXT: br label [[FOR_BODY3:%.*]]
113 ; CHECK: for.cond1:
114 ; CHECK-NEXT: br label [[L2:%.*]]
115 ; CHECK: for.body3:
116 ; CHECK-NEXT: br i1 undef, label [[FOR_COND1:%.*]], label [[L1_LOOPEXIT]]
117 ; CHECK: l2:
118 ; CHECK-NEXT: [[G_4:%.*]] = phi i32* [ @b, [[FOR_END14]] ], [ @a, [[FOR_COND1]] ]
119 ; CHECK-NEXT: [[F_2:%.*]] = phi i32* [ [[F_0]], [[FOR_END14]] ], [ @a, [[FOR_COND1]] ]
120 ; CHECK-NEXT: br label [[FOR_INC:%.*]]
121 ; CHECK: for.inc:
122 ; CHECK-NEXT: br i1 false, label [[FOR_COND_LOOPEXIT:%.*]], label [[FOR_INC]]
123 ; CHECK: for.end14:
124 ; CHECK-NEXT: br label [[L2]]
125 ;
126 entry:
127 br label %l1
128 l1.loopexit:
129 %g.223.lcssa = phi i32* [ @b, %for.body3 ]
130 br label %l1
131 l1:
132 %g.0 = phi i32* [ undef, %entry ], [ %g.223.lcssa, %l1.loopexit ]
133 %f.0 = phi i32* [ @b, %entry ], [ @a, %l1.loopexit ]
134 br label %for.cond
135 for.cond.loopexit:
136 br label %for.cond
137 for.cond:
138 %g.1 = phi i32* [ %g.0, %l1 ], [ %g.4, %for.cond.loopexit ]
139 %f.1 = phi i32* [ %f.0, %l1 ], [ %f.2, %for.cond.loopexit ]
140 br i1 undef, label %for.end14, label %for.cond1.preheader
141 for.cond1.preheader:
142 br label %for.body3
143 for.cond1:
144 br label %l2
145 for.body3:
146 br i1 undef, label %for.cond1, label %l1.loopexit
147 l2:
148 %g.4 = phi i32* [ %g.1, %for.end14 ], [ @a, %for.cond1 ]
149 %f.2 = phi i32* [ %f.1, %for.end14 ], [ @a, %for.cond1 ]
150 br label %for.inc
151 for.inc:
152 br i1 false, label %for.cond.loopexit, label %for.inc
153 for.end14:
154 br label %l2
155 }
156