@@ -158,6 +158,47 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
158
158
isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime ()))
159
159
return nullptr ;
160
160
161
+ auto *LI = Options.LI ;
162
+ SmallVector<BasicBlock *, 4 > LoopPreds;
163
+ // Check if extra modifications will be required to preserve loop-simplify
164
+ // form after splitting. If it would require splitting blocks with IndirectBr
165
+ // terminators, bail out if preserving loop-simplify form is requested.
166
+ if (LI) {
167
+ if (Loop *TIL = LI->getLoopFor (TIBB)) {
168
+
169
+ // The only that we can break LoopSimplify form by splitting a critical
170
+ // edge is if after the split there exists some edge from TIL to DestBB
171
+ // *and* the only edge into DestBB from outside of TIL is that of
172
+ // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
173
+ // is the new exit block and it has no non-loop predecessors. If the
174
+ // second isn't true, then DestBB was not in LoopSimplify form prior to
175
+ // the split as it had a non-loop predecessor. In both of these cases,
176
+ // the predecessor must be directly in TIL, not in a subloop, or again
177
+ // LoopSimplify doesn't hold.
178
+ for (pred_iterator I = pred_begin (DestBB), E = pred_end (DestBB); I != E;
179
+ ++I) {
180
+ BasicBlock *P = *I;
181
+ if (P == TIBB)
182
+ continue ; // The new block is known.
183
+ if (LI->getLoopFor (P) != TIL) {
184
+ // No need to re-simplify, it wasn't to start with.
185
+ LoopPreds.clear ();
186
+ break ;
187
+ }
188
+ LoopPreds.push_back (P);
189
+ }
190
+ // Loop-simplify form can be preserved, if we can split all in-loop
191
+ // predecessors.
192
+ if (any_of (LoopPreds, [](BasicBlock *Pred) {
193
+ return isa<IndirectBrInst>(Pred->getTerminator ());
194
+ })) {
195
+ if (Options.PreserveLoopSimplify )
196
+ return nullptr ;
197
+ LoopPreds.clear ();
198
+ }
199
+ }
200
+ }
201
+
161
202
// Create a new basic block, linking it into the CFG.
162
203
BasicBlock *NewBB = BasicBlock::Create (TI->getContext (),
163
204
TIBB->getName () + " ." + DestBB->getName () + " _crit_edge" );
@@ -212,7 +253,6 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
212
253
// If we have nothing to update, just return.
213
254
auto *DT = Options.DT ;
214
255
auto *PDT = Options.PDT ;
215
- auto *LI = Options.LI ;
216
256
auto *MSSAU = Options.MSSAU ;
217
257
if (MSSAU)
218
258
MSSAU->wireOldPredecessorsToNewImmediatePredecessor (
@@ -281,28 +321,6 @@ llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
281
321
createPHIsForSplitLoopExit (TIBB, NewBB, DestBB);
282
322
}
283
323
284
- // The only that we can break LoopSimplify form by splitting a critical
285
- // edge is if after the split there exists some edge from TIL to DestBB
286
- // *and* the only edge into DestBB from outside of TIL is that of
287
- // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
288
- // is the new exit block and it has no non-loop predecessors. If the
289
- // second isn't true, then DestBB was not in LoopSimplify form prior to
290
- // the split as it had a non-loop predecessor. In both of these cases,
291
- // the predecessor must be directly in TIL, not in a subloop, or again
292
- // LoopSimplify doesn't hold.
293
- SmallVector<BasicBlock *, 4 > LoopPreds;
294
- for (pred_iterator I = pred_begin (DestBB), E = pred_end (DestBB); I != E;
295
- ++I) {
296
- BasicBlock *P = *I;
297
- if (P == NewBB)
298
- continue ; // The new block is known.
299
- if (LI->getLoopFor (P) != TIL) {
300
- // No need to re-simplify, it wasn't to start with.
301
- LoopPreds.clear ();
302
- break ;
303
- }
304
- LoopPreds.push_back (P);
305
- }
306
324
if (!LoopPreds.empty ()) {
307
325
assert (!DestBB->isEHPad () && " We don't split edges to EH pads!" );
308
326
BasicBlock *NewExitBB = SplitBlockPredecessors (
0 commit comments