llvm.org GIT mirror llvm / 12bb766
Fix for PR3016: detect the tricky case, where there are unfoldable references to a PHI node in the block being folded, and disable the transformation in that case. The correct transformation of such PHI nodes depends on whether BB dominates Succ, and dominance is expensive to compute here. (Alternatively, it's possible to check whether any uses are live, but that's also essentially a dominance calculation. Another alternative is to use reg2mem, but it probably isn't a good idea to use that in simplifycfg.) Also, remove some incorrect code from CanPropagatePredecessorsForPHIs which is made unnecessary with this patch: it didn't consider the case where a PHI node in BB has multiple uses. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79174 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Friedman 10 years ago
2 changed file(s) with 37 addition(s) and 60 deletion(s). Raw diff Collapse all Expand all
9191 // is always safe
9292 if (Succ->getSinglePredecessor()) return true;
9393
94 typedef SmallPtrSet InstrSet;
95 InstrSet BBPHIs;
96
97 // Make a list of all phi nodes in BB
98 BasicBlock::iterator BBI = BB->begin();
99 while (isa(*BBI)) BBPHIs.insert(BBI++);
100
10194 // Make a list of the predecessors of BB
10295 typedef SmallPtrSet BlockSet;
10396 BlockSet BBPreds(pred_begin(BB), pred_end(BB));
134127 return false;
135128 }
136129 }
137 // Remove this phinode from the list of phis in BB, since it has been
138 // handled.
139 BBPHIs.erase(BBPN);
140130 } else {
141131 Value* Val = PN->getIncomingValueForBlock(BB);
142132 for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
154144 }
155145 }
156146
157 // If there are any other phi nodes in BB that don't have a phi node in Succ
158 // to merge with, they must be moved to Succ completely. However, for any
159 // predecessors of Succ, branches will be added to the phi node that just
160 // point to itself. So, for any common predecessors, this must not cause
161 // conflicts.
162 for (InstrSet::iterator I = BBPHIs.begin(), E = BBPHIs.end();
163 I != E; I++) {
164 PHINode *PN = cast(*I);
165 for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
166 PI != PE; PI++)
167 if (PN->getIncomingValueForBlock(*PI) != PN) {
168 DEBUG(errs() << "Can't fold, phi node " << PN->getName() << " in "
169 << BB->getName() << " is conflicting with regard to common "
170 << "predecessor " << (*PI)->getName() << "\n");
171 return false;
172 }
173 }
174
175147 return true;
176148 }
177149
183155 // Check to see if merging these blocks would cause conflicts for any of the
184156 // phi nodes in BB or Succ. If not, we can safely merge.
185157 if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
186
158
159 // Check for cases where Succ has multiple predecessors and a PHI node in BB
160 // has uses which will not disappear when the PHI nodes are merged. It is
161 // possible to handle such cases, but difficult: it requires checking whether
162 // BB dominates Succ, which is non-trivial to calculate in the case where
163 // Succ has multiple predecessors. Also, it requires checking whether
164 // constructing the necessary self-referential PHI node doesn't intoduce any
165 // conflicts; this isn't too difficult, but the previous code for doing this
166 // was incorrect.
167 //
168 // Note that if this check finds a live use, BB dominates Succ, so BB is
169 // something like a loop pre-header (or rarely, a part of an irreducible CFG);
170 // folding the branch isn't profitable in that case anyway.
171 if (!Succ->getSinglePredecessor()) {
172 BasicBlock::iterator BBI = BB->begin();
173 while (isa(*BBI)) {
174 for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
175 UI != E; ++UI) {
176 if (PHINode* PN = dyn_cast(*UI)) {
177 if (PN->getIncomingBlock(UI) != BB)
178 return false;
179 } else {
180 return false;
181 }
182 }
183 ++BBI;
184 }
185 }
186
187187 DOUT << "Killing Trivial BB: \n" << *BB;
188188
189189 if (isa(Succ->begin())) {
218218 }
219219 }
220220
221 if (isa(&BB->front())) {
222 SmallVector
223 OldSuccPreds(pred_begin(Succ), pred_end(Succ));
224
225 // Move all PHI nodes in BB to Succ if they are alive, otherwise
226 // delete them.
227 while (PHINode *PN = dyn_cast(&BB->front())) {
228 if (PN->use_empty()) {
229 // Just remove the dead phi. This happens if Succ's PHIs were the only
230 // users of the PHI nodes.
231 PN->eraseFromParent();
232 continue;
233 }
234
235 // The instruction is alive, so this means that BB must dominate all
236 // predecessors of Succ (Since all uses of the PN are after its
237 // definition, so in Succ or a block dominated by Succ. If a predecessor
238 // of Succ would not be dominated by BB, PN would violate the def before
239 // use SSA demand). Therefore, we can simply move the phi node to the
240 // next block.
221 while (PHINode *PN = dyn_cast(&BB->front())) {
222 if (Succ->getSinglePredecessor()) {
223 // BB is the only predecessor of Succ, so Succ will end up with exactly
224 // the same predecessors BB had.
241225 Succ->getInstList().splice(Succ->begin(),
242226 BB->getInstList(), BB->begin());
243
244 // We need to add new entries for the PHI node to account for
245 // predecessors of Succ that the PHI node does not take into
246 // account. At this point, since we know that BB dominated succ and all
247 // of its predecessors, this means that we should any newly added
248 // incoming edges should use the PHI node itself as the value for these
249 // edges, because they are loop back edges.
250 for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i)
251 if (OldSuccPreds[i] != BB)
252 PN->addIncoming(PN, OldSuccPreds[i]);
227 } else {
228 // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
229 assert(PN->use_empty() && "There shouldn't be any uses here!");
230 PN->eraseFromParent();
253231 }
254232 }
255233
0 ; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis
1 ; XFAIL: *
21 ; PR3016
32 ; Dead use caused invariant violation.
43