1//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// BreakCriticalEdges pass - Break all of the critical edges in the CFG by
10// inserting a dummy basic block. This pass may be "required" by passes that
11// cannot deal with critical edges. For this usage, the structure type is
12// forward declared. This pass obviously invalidates the CFG, but can update
13// dominator trees.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
18#include "llvm/ADT/SetVector.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/Analysis/BlockFrequencyInfo.h"
22#include "llvm/Analysis/BranchProbabilityInfo.h"
23#include "llvm/Analysis/CFG.h"
24#include "llvm/Analysis/LoopInfo.h"
25#include "llvm/Analysis/MemorySSAUpdater.h"
26#include "llvm/Analysis/PostDominators.h"
27#include "llvm/IR/CFG.h"
28#include "llvm/IR/Dominators.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/InitializePasses.h"
31#include "llvm/Transforms/Utils.h"
32#include "llvm/Transforms/Utils/BasicBlockUtils.h"
33#include "llvm/Transforms/Utils/Cloning.h"
34#include "llvm/Transforms/Utils/ValueMapper.h"
35using namespace llvm;
36
37#define DEBUG_TYPE "break-crit-edges"
38
39STATISTIC(NumBroken, "Number of blocks inserted");
40
41namespace {
42 struct BreakCriticalEdges : public FunctionPass {
43 static char ID; // Pass identification, replacement for typeid
44 BreakCriticalEdges() : FunctionPass(ID) {
45 initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
46 }
47
48 bool runOnFunction(Function &F) override {
49 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
50 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
51
52 auto *PDTWP = getAnalysisIfAvailable<PostDominatorTreeWrapperPass>();
53 auto *PDT = PDTWP ? &PDTWP->getPostDomTree() : nullptr;
54
55 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
56 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
57 unsigned N =
58 SplitAllCriticalEdges(F, Options: CriticalEdgeSplittingOptions(DT, LI, nullptr, PDT));
59 NumBroken += N;
60 return N > 0;
61 }
62
63 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.addPreserved<DominatorTreeWrapperPass>();
65 AU.addPreserved<LoopInfoWrapperPass>();
66
67 // No loop canonicalization guarantees are broken by this pass.
68 AU.addPreservedID(ID&: LoopSimplifyID);
69 }
70 };
71}
72
73char BreakCriticalEdges::ID = 0;
74INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
75 "Break critical edges in CFG", false, false)
76
77// Publicly exposed interface to pass...
78char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
79FunctionPass *llvm::createBreakCriticalEdgesPass() {
80 return new BreakCriticalEdges();
81}
82
83PreservedAnalyses BreakCriticalEdgesPass::run(Function &F,
84 FunctionAnalysisManager &AM) {
85 auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(IR&: F);
86 auto *LI = AM.getCachedResult<LoopAnalysis>(IR&: F);
87 unsigned N = SplitAllCriticalEdges(F, Options: CriticalEdgeSplittingOptions(DT, LI));
88 NumBroken += N;
89 if (N == 0)
90 return PreservedAnalyses::all();
91 PreservedAnalyses PA;
92 PA.preserve<DominatorTreeAnalysis>();
93 PA.preserve<LoopAnalysis>();
94 return PA;
95}
96
97//===----------------------------------------------------------------------===//
98// Implementation of the external critical edge manipulation functions
99//===----------------------------------------------------------------------===//
100
101BasicBlock *llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
102 const CriticalEdgeSplittingOptions &Options,
103 const Twine &BBName) {
104 if (!isCriticalEdge(TI, SuccNum, AllowIdenticalEdges: Options.MergeIdenticalEdges))
105 return nullptr;
106
107 return SplitKnownCriticalEdge(TI, SuccNum, Options, BBName);
108}
109
110BasicBlock *
111llvm::SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum,
112 const CriticalEdgeSplittingOptions &Options,
113 const Twine &BBName) {
114 assert(!isa<IndirectBrInst>(TI) &&
115 "Cannot split critical edge from IndirectBrInst");
116
117 BasicBlock *TIBB = TI->getParent();
118 BasicBlock *DestBB = TI->getSuccessor(Idx: SuccNum);
119
120 // Splitting the critical edge to a pad block is non-trivial. Don't do
121 // it in this generic function.
122 if (DestBB->isEHPad()) return nullptr;
123
124 if (Options.IgnoreUnreachableDests &&
125 isa<UnreachableInst>(Val: DestBB->getFirstNonPHIOrDbgOrLifetime()))
126 return nullptr;
127
128 auto *LI = Options.LI;
129 SmallVector<BasicBlock *, 4> LoopPreds;
130 // Check if extra modifications will be required to preserve loop-simplify
131 // form after splitting. If it would require splitting blocks with IndirectBr
132 // terminators, bail out if preserving loop-simplify form is requested.
133 if (LI) {
134 if (Loop *TIL = LI->getLoopFor(BB: TIBB)) {
135
136 // The only way that we can break LoopSimplify form by splitting a
137 // critical edge is if after the split there exists some edge from TIL to
138 // DestBB *and* the only edge into DestBB from outside of TIL is that of
139 // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
140 // is the new exit block and it has no non-loop predecessors. If the
141 // second isn't true, then DestBB was not in LoopSimplify form prior to
142 // the split as it had a non-loop predecessor. In both of these cases,
143 // the predecessor must be directly in TIL, not in a subloop, or again
144 // LoopSimplify doesn't hold.
145 for (BasicBlock *P : predecessors(BB: DestBB)) {
146 if (P == TIBB)
147 continue; // The new block is known.
148 if (LI->getLoopFor(BB: P) != TIL) {
149 // No need to re-simplify, it wasn't to start with.
150 LoopPreds.clear();
151 break;
152 }
153 LoopPreds.push_back(Elt: P);
154 }
155 // Loop-simplify form can be preserved, if we can split all in-loop
156 // predecessors.
157 if (any_of(Range&: LoopPreds, P: [](BasicBlock *Pred) {
158 return isa<IndirectBrInst>(Val: Pred->getTerminator());
159 })) {
160 if (Options.PreserveLoopSimplify)
161 return nullptr;
162 LoopPreds.clear();
163 }
164 }
165 }
166
167 // Create a new basic block, linking it into the CFG.
168 BasicBlock *NewBB = nullptr;
169 if (BBName.str() != "")
170 NewBB = BasicBlock::Create(Context&: TI->getContext(), Name: BBName);
171 else
172 NewBB = BasicBlock::Create(Context&: TI->getContext(), Name: TIBB->getName() + "." +
173 DestBB->getName() +
174 "_crit_edge");
175 // Create our unconditional branch.
176 BranchInst *NewBI = BranchInst::Create(IfTrue: DestBB, InsertAtEnd: NewBB);
177 NewBI->setDebugLoc(TI->getDebugLoc());
178
179 // Insert the block into the function... right after the block TI lives in.
180 Function &F = *TIBB->getParent();
181 Function::iterator FBBI = TIBB->getIterator();
182 F.insert(Position: ++FBBI, BB: NewBB);
183
184 // Branch to the new block, breaking the edge.
185 TI->setSuccessor(Idx: SuccNum, BB: NewBB);
186
187 // If there are any PHI nodes in DestBB, we need to update them so that they
188 // merge incoming values from NewBB instead of from TIBB.
189 {
190 unsigned BBIdx = 0;
191 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(Val: I); ++I) {
192 // We no longer enter through TIBB, now we come in through NewBB.
193 // Revector exactly one entry in the PHI node that used to come from
194 // TIBB to come from NewBB.
195 PHINode *PN = cast<PHINode>(Val&: I);
196
197 // Reuse the previous value of BBIdx if it lines up. In cases where we
198 // have multiple phi nodes with *lots* of predecessors, this is a speed
199 // win because we don't have to scan the PHI looking for TIBB. This
200 // happens because the BB list of PHI nodes are usually in the same
201 // order.
202 if (PN->getIncomingBlock(i: BBIdx) != TIBB)
203 BBIdx = PN->getBasicBlockIndex(BB: TIBB);
204 PN->setIncomingBlock(i: BBIdx, BB: NewBB);
205 }
206 }
207
208 // If there are any other edges from TIBB to DestBB, update those to go
209 // through the split block, making those edges non-critical as well (and
210 // reducing the number of phi entries in the DestBB if relevant).
211 if (Options.MergeIdenticalEdges) {
212 for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
213 if (TI->getSuccessor(Idx: i) != DestBB) continue;
214
215 // Remove an entry for TIBB from DestBB phi nodes.
216 DestBB->removePredecessor(Pred: TIBB, KeepOneInputPHIs: Options.KeepOneInputPHIs);
217
218 // We found another edge to DestBB, go to NewBB instead.
219 TI->setSuccessor(Idx: i, BB: NewBB);
220 }
221 }
222
223 // If we have nothing to update, just return.
224 auto *DT = Options.DT;
225 auto *PDT = Options.PDT;
226 auto *MSSAU = Options.MSSAU;
227 if (MSSAU)
228 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(
229 Old: DestBB, New: NewBB, Preds: {TIBB}, IdenticalEdgesWereMerged: Options.MergeIdenticalEdges);
230
231 if (!DT && !PDT && !LI)
232 return NewBB;
233
234 if (DT || PDT) {
235 // Update the DominatorTree.
236 // ---> NewBB -----\
237 // / V
238 // TIBB -------\\------> DestBB
239 //
240 // First, inform the DT about the new path from TIBB to DestBB via NewBB,
241 // then delete the old edge from TIBB to DestBB. By doing this in that order
242 // DestBB stays reachable in the DT the whole time and its subtree doesn't
243 // get disconnected.
244 SmallVector<DominatorTree::UpdateType, 3> Updates;
245 Updates.push_back(Elt: {DominatorTree::Insert, TIBB, NewBB});
246 Updates.push_back(Elt: {DominatorTree::Insert, NewBB, DestBB});
247 if (!llvm::is_contained(Range: successors(BB: TIBB), Element: DestBB))
248 Updates.push_back(Elt: {DominatorTree::Delete, TIBB, DestBB});
249
250 if (DT)
251 DT->applyUpdates(Updates);
252 if (PDT)
253 PDT->applyUpdates(Updates);
254 }
255
256 // Update LoopInfo if it is around.
257 if (LI) {
258 if (Loop *TIL = LI->getLoopFor(BB: TIBB)) {
259 // If one or the other blocks were not in a loop, the new block is not
260 // either, and thus LI doesn't need to be updated.
261 if (Loop *DestLoop = LI->getLoopFor(BB: DestBB)) {
262 if (TIL == DestLoop) {
263 // Both in the same loop, the NewBB joins loop.
264 DestLoop->addBasicBlockToLoop(NewBB, LI&: *LI);
265 } else if (TIL->contains(L: DestLoop)) {
266 // Edge from an outer loop to an inner loop. Add to the outer loop.
267 TIL->addBasicBlockToLoop(NewBB, LI&: *LI);
268 } else if (DestLoop->contains(L: TIL)) {
269 // Edge from an inner loop to an outer loop. Add to the outer loop.
270 DestLoop->addBasicBlockToLoop(NewBB, LI&: *LI);
271 } else {
272 // Edge from two loops with no containment relation. Because these
273 // are natural loops, we know that the destination block must be the
274 // header of its loop (adding a branch into a loop elsewhere would
275 // create an irreducible loop).
276 assert(DestLoop->getHeader() == DestBB &&
277 "Should not create irreducible loops!");
278 if (Loop *P = DestLoop->getParentLoop())
279 P->addBasicBlockToLoop(NewBB, LI&: *LI);
280 }
281 }
282
283 // If TIBB is in a loop and DestBB is outside of that loop, we may need
284 // to update LoopSimplify form and LCSSA form.
285 if (!TIL->contains(BB: DestBB)) {
286 assert(!TIL->contains(NewBB) &&
287 "Split point for loop exit is contained in loop!");
288
289 // Update LCSSA form in the newly created exit block.
290 if (Options.PreserveLCSSA) {
291 createPHIsForSplitLoopExit(Preds: TIBB, SplitBB: NewBB, DestBB);
292 }
293
294 if (!LoopPreds.empty()) {
295 assert(!DestBB->isEHPad() && "We don't split edges to EH pads!");
296 BasicBlock *NewExitBB = SplitBlockPredecessors(
297 BB: DestBB, Preds: LoopPreds, Suffix: "split", DT, LI, MSSAU, PreserveLCSSA: Options.PreserveLCSSA);
298 if (Options.PreserveLCSSA)
299 createPHIsForSplitLoopExit(Preds: LoopPreds, SplitBB: NewExitBB, DestBB);
300 }
301 }
302 }
303 }
304
305 return NewBB;
306}
307
308// Return the unique indirectbr predecessor of a block. This may return null
309// even if such a predecessor exists, if it's not useful for splitting.
310// If a predecessor is found, OtherPreds will contain all other (non-indirectbr)
311// predecessors of BB.
312static BasicBlock *
313findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) {
314 // Verify we have exactly one IBR predecessor.
315 // Conservatively bail out if one of the other predecessors is not a "regular"
316 // terminator (that is, not a switch or a br).
317 BasicBlock *IBB = nullptr;
318 for (BasicBlock *PredBB : predecessors(BB)) {
319 Instruction *PredTerm = PredBB->getTerminator();
320 switch (PredTerm->getOpcode()) {
321 case Instruction::IndirectBr:
322 if (IBB)
323 return nullptr;
324 IBB = PredBB;
325 break;
326 case Instruction::Br:
327 case Instruction::Switch:
328 OtherPreds.push_back(Elt: PredBB);
329 continue;
330 default:
331 return nullptr;
332 }
333 }
334
335 return IBB;
336}
337
338bool llvm::SplitIndirectBrCriticalEdges(Function &F,
339 bool IgnoreBlocksWithoutPHI,
340 BranchProbabilityInfo *BPI,
341 BlockFrequencyInfo *BFI) {
342 // Check whether the function has any indirectbrs, and collect which blocks
343 // they may jump to. Since most functions don't have indirect branches,
344 // this lowers the common case's overhead to O(Blocks) instead of O(Edges).
345 SmallSetVector<BasicBlock *, 16> Targets;
346 for (auto &BB : F) {
347 if (isa<IndirectBrInst>(Val: BB.getTerminator()))
348 for (BasicBlock *Succ : successors(BB: &BB))
349 Targets.insert(X: Succ);
350 }
351
352 if (Targets.empty())
353 return false;
354
355 bool ShouldUpdateAnalysis = BPI && BFI;
356 bool Changed = false;
357 for (BasicBlock *Target : Targets) {
358 if (IgnoreBlocksWithoutPHI && Target->phis().empty())
359 continue;
360
361 SmallVector<BasicBlock *, 16> OtherPreds;
362 BasicBlock *IBRPred = findIBRPredecessor(BB: Target, OtherPreds);
363 // If we did not found an indirectbr, or the indirectbr is the only
364 // incoming edge, this isn't the kind of edge we're looking for.
365 if (!IBRPred || OtherPreds.empty())
366 continue;
367
368 // Don't even think about ehpads/landingpads.
369 Instruction *FirstNonPHI = Target->getFirstNonPHI();
370 if (FirstNonPHI->isEHPad() || Target->isLandingPad())
371 continue;
372
373 // Remember edge probabilities if needed.
374 SmallVector<BranchProbability, 4> EdgeProbabilities;
375 if (ShouldUpdateAnalysis) {
376 EdgeProbabilities.reserve(N: Target->getTerminator()->getNumSuccessors());
377 for (unsigned I = 0, E = Target->getTerminator()->getNumSuccessors();
378 I < E; ++I)
379 EdgeProbabilities.emplace_back(Args: BPI->getEdgeProbability(Src: Target, IndexInSuccessors: I));
380 BPI->eraseBlock(BB: Target);
381 }
382
383 BasicBlock *BodyBlock = Target->splitBasicBlock(I: FirstNonPHI, BBName: ".split");
384 if (ShouldUpdateAnalysis) {
385 // Copy the BFI/BPI from Target to BodyBlock.
386 BPI->setEdgeProbability(Src: BodyBlock, Probs: EdgeProbabilities);
387 BFI->setBlockFreq(BB: BodyBlock, Freq: BFI->getBlockFreq(BB: Target));
388 }
389 // It's possible Target was its own successor through an indirectbr.
390 // In this case, the indirectbr now comes from BodyBlock.
391 if (IBRPred == Target)
392 IBRPred = BodyBlock;
393
394 // At this point Target only has PHIs, and BodyBlock has the rest of the
395 // block's body. Create a copy of Target that will be used by the "direct"
396 // preds.
397 ValueToValueMapTy VMap;
398 BasicBlock *DirectSucc = CloneBasicBlock(BB: Target, VMap, NameSuffix: ".clone", F: &F);
399
400 BlockFrequency BlockFreqForDirectSucc;
401 for (BasicBlock *Pred : OtherPreds) {
402 // If the target is a loop to itself, then the terminator of the split
403 // block (BodyBlock) needs to be updated.
404 BasicBlock *Src = Pred != Target ? Pred : BodyBlock;
405 Src->getTerminator()->replaceUsesOfWith(From: Target, To: DirectSucc);
406 if (ShouldUpdateAnalysis)
407 BlockFreqForDirectSucc += BFI->getBlockFreq(BB: Src) *
408 BPI->getEdgeProbability(Src, Dst: DirectSucc);
409 }
410 if (ShouldUpdateAnalysis) {
411 BFI->setBlockFreq(BB: DirectSucc, Freq: BlockFreqForDirectSucc);
412 BlockFrequency NewBlockFreqForTarget =
413 BFI->getBlockFreq(BB: Target) - BlockFreqForDirectSucc;
414 BFI->setBlockFreq(BB: Target, Freq: NewBlockFreqForTarget);
415 }
416
417 // Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that
418 // they are clones, so the number of PHIs are the same.
419 // (a) Remove the edge coming from IBRPred from the "Direct" PHI
420 // (b) Leave that as the only edge in the "Indirect" PHI.
421 // (c) Merge the two in the body block.
422 BasicBlock::iterator Indirect = Target->begin(),
423 End = Target->getFirstNonPHIIt();
424 BasicBlock::iterator Direct = DirectSucc->begin();
425 BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt();
426
427 assert(&*End == Target->getTerminator() &&
428 "Block was expected to only contain PHIs");
429
430 while (Indirect != End) {
431 PHINode *DirPHI = cast<PHINode>(Val&: Direct);
432 PHINode *IndPHI = cast<PHINode>(Val&: Indirect);
433 BasicBlock::iterator InsertPt = Indirect;
434
435 // Now, clean up - the direct block shouldn't get the indirect value,
436 // and vice versa.
437 DirPHI->removeIncomingValue(BB: IBRPred);
438 Direct++;
439
440 // Advance the pointer here, to avoid invalidation issues when the old
441 // PHI is erased.
442 Indirect++;
443
444 PHINode *NewIndPHI = PHINode::Create(Ty: IndPHI->getType(), NumReservedValues: 1, NameStr: "ind", InsertBefore: InsertPt);
445 NewIndPHI->addIncoming(V: IndPHI->getIncomingValueForBlock(BB: IBRPred),
446 BB: IBRPred);
447
448 // Create a PHI in the body block, to merge the direct and indirect
449 // predecessors.
450 PHINode *MergePHI = PHINode::Create(Ty: IndPHI->getType(), NumReservedValues: 2, NameStr: "merge");
451 MergePHI->insertBefore(InsertPos: MergeInsert);
452 MergePHI->addIncoming(V: NewIndPHI, BB: Target);
453 MergePHI->addIncoming(V: DirPHI, BB: DirectSucc);
454
455 IndPHI->replaceAllUsesWith(V: MergePHI);
456 IndPHI->eraseFromParent();
457 }
458
459 Changed = true;
460 }
461
462 return Changed;
463}
464

source code of llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp