1 //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
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
7 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
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"
37 #define DEBUG_TYPE "break-crit-edges"
39 STATISTIC(NumBroken
, "Number of blocks inserted");
42 struct BreakCriticalEdges
: public FunctionPass
{
43 static char ID
; // Pass identification, replacement for typeid
44 BreakCriticalEdges() : FunctionPass(ID
) {
45 initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
48 bool runOnFunction(Function
&F
) override
{
49 auto *DTWP
= getAnalysisIfAvailable
<DominatorTreeWrapperPass
>();
50 auto *DT
= DTWP
? &DTWP
->getDomTree() : nullptr;
52 auto *PDTWP
= getAnalysisIfAvailable
<PostDominatorTreeWrapperPass
>();
53 auto *PDT
= PDTWP
? &PDTWP
->getPostDomTree() : nullptr;
55 auto *LIWP
= getAnalysisIfAvailable
<LoopInfoWrapperPass
>();
56 auto *LI
= LIWP
? &LIWP
->getLoopInfo() : nullptr;
58 SplitAllCriticalEdges(F
, CriticalEdgeSplittingOptions(DT
, LI
, nullptr, PDT
));
63 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
64 AU
.addPreserved
<DominatorTreeWrapperPass
>();
65 AU
.addPreserved
<LoopInfoWrapperPass
>();
67 // No loop canonicalization guarantees are broken by this pass.
68 AU
.addPreservedID(LoopSimplifyID
);
73 char BreakCriticalEdges::ID
= 0;
74 INITIALIZE_PASS(BreakCriticalEdges
, "break-crit-edges",
75 "Break critical edges in CFG", false, false)
77 // Publicly exposed interface to pass...
78 char &llvm::BreakCriticalEdgesID
= BreakCriticalEdges::ID
;
79 FunctionPass
*llvm::createBreakCriticalEdgesPass() {
80 return new BreakCriticalEdges();
83 PreservedAnalyses
BreakCriticalEdgesPass::run(Function
&F
,
84 FunctionAnalysisManager
&AM
) {
85 auto *DT
= AM
.getCachedResult
<DominatorTreeAnalysis
>(F
);
86 auto *LI
= AM
.getCachedResult
<LoopAnalysis
>(F
);
87 unsigned N
= SplitAllCriticalEdges(F
, CriticalEdgeSplittingOptions(DT
, LI
));
90 return PreservedAnalyses::all();
92 PA
.preserve
<DominatorTreeAnalysis
>();
93 PA
.preserve
<LoopAnalysis
>();
97 //===----------------------------------------------------------------------===//
98 // Implementation of the external critical edge manipulation functions
99 //===----------------------------------------------------------------------===//
101 BasicBlock
*llvm::SplitCriticalEdge(Instruction
*TI
, unsigned SuccNum
,
102 const CriticalEdgeSplittingOptions
&Options
,
103 const Twine
&BBName
) {
104 if (!isCriticalEdge(TI
, SuccNum
, Options
.MergeIdenticalEdges
))
107 return SplitKnownCriticalEdge(TI
, SuccNum
, Options
, BBName
);
111 llvm::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");
117 BasicBlock
*TIBB
= TI
->getParent();
118 BasicBlock
*DestBB
= TI
->getSuccessor(SuccNum
);
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;
124 if (Options
.IgnoreUnreachableDests
&&
125 isa
<UnreachableInst
>(DestBB
->getFirstNonPHIOrDbgOrLifetime()))
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.
134 if (Loop
*TIL
= LI
->getLoopFor(TIBB
)) {
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(DestBB
)) {
147 continue; // The new block is known.
148 if (LI
->getLoopFor(P
) != TIL
) {
149 // No need to re-simplify, it wasn't to start with.
153 LoopPreds
.push_back(P
);
155 // Loop-simplify form can be preserved, if we can split all in-loop
157 if (any_of(LoopPreds
, [](BasicBlock
*Pred
) {
158 return isa
<IndirectBrInst
>(Pred
->getTerminator());
160 if (Options
.PreserveLoopSimplify
)
167 // Create a new basic block, linking it into the CFG.
168 BasicBlock
*NewBB
= nullptr;
169 if (BBName
.str() != "")
170 NewBB
= BasicBlock::Create(TI
->getContext(), BBName
);
172 NewBB
= BasicBlock::Create(TI
->getContext(), TIBB
->getName() + "." +
175 // Create our unconditional branch.
176 BranchInst
*NewBI
= BranchInst::Create(DestBB
, NewBB
);
177 NewBI
->setDebugLoc(TI
->getDebugLoc());
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(++FBBI
, NewBB
);
184 // Branch to the new block, breaking the edge.
185 TI
->setSuccessor(SuccNum
, NewBB
);
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.
191 for (BasicBlock::iterator I
= DestBB
->begin(); isa
<PHINode
>(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
>(I
);
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
202 if (PN
->getIncomingBlock(BBIdx
) != TIBB
)
203 BBIdx
= PN
->getBasicBlockIndex(TIBB
);
204 PN
->setIncomingBlock(BBIdx
, NewBB
);
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(i
) != DestBB
) continue;
215 // Remove an entry for TIBB from DestBB phi nodes.
216 DestBB
->removePredecessor(TIBB
, Options
.KeepOneInputPHIs
);
218 // We found another edge to DestBB, go to NewBB instead.
219 TI
->setSuccessor(i
, NewBB
);
223 // If we have nothing to update, just return.
224 auto *DT
= Options
.DT
;
225 auto *PDT
= Options
.PDT
;
226 auto *MSSAU
= Options
.MSSAU
;
228 MSSAU
->wireOldPredecessorsToNewImmediatePredecessor(
229 DestBB
, NewBB
, {TIBB
}, Options
.MergeIdenticalEdges
);
231 if (!DT
&& !PDT
&& !LI
)
235 // Update the DominatorTree.
238 // TIBB -------\\------> DestBB
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
244 SmallVector
<DominatorTree::UpdateType
, 3> Updates
;
245 Updates
.push_back({DominatorTree::Insert
, TIBB
, NewBB
});
246 Updates
.push_back({DominatorTree::Insert
, NewBB
, DestBB
});
247 if (!llvm::is_contained(successors(TIBB
), DestBB
))
248 Updates
.push_back({DominatorTree::Delete
, TIBB
, DestBB
});
251 DT
->applyUpdates(Updates
);
253 PDT
->applyUpdates(Updates
);
256 // Update LoopInfo if it is around.
258 if (Loop
*TIL
= LI
->getLoopFor(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(DestBB
)) {
262 if (TIL
== DestLoop
) {
263 // Both in the same loop, the NewBB joins loop.
264 DestLoop
->addBasicBlockToLoop(NewBB
, *LI
);
265 } else if (TIL
->contains(DestLoop
)) {
266 // Edge from an outer loop to an inner loop. Add to the outer loop.
267 TIL
->addBasicBlockToLoop(NewBB
, *LI
);
268 } else if (DestLoop
->contains(TIL
)) {
269 // Edge from an inner loop to an outer loop. Add to the outer loop.
270 DestLoop
->addBasicBlockToLoop(NewBB
, *LI
);
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
);
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(DestBB
)) {
286 assert(!TIL
->contains(NewBB
) &&
287 "Split point for loop exit is contained in loop!");
289 // Update LCSSA form in the newly created exit block.
290 if (Options
.PreserveLCSSA
) {
291 createPHIsForSplitLoopExit(TIBB
, NewBB
, DestBB
);
294 if (!LoopPreds
.empty()) {
295 assert(!DestBB
->isEHPad() && "We don't split edges to EH pads!");
296 BasicBlock
*NewExitBB
= SplitBlockPredecessors(
297 DestBB
, LoopPreds
, "split", DT
, LI
, MSSAU
, Options
.PreserveLCSSA
);
298 if (Options
.PreserveLCSSA
)
299 createPHIsForSplitLoopExit(LoopPreds
, NewExitBB
, DestBB
);
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.
313 findIBRPredecessor(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
:
326 case Instruction::Br
:
327 case Instruction::Switch
:
328 OtherPreds
.push_back(PredBB
);
338 bool 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
;
347 auto *IBI
= dyn_cast
<IndirectBrInst
>(BB
.getTerminator());
351 for (unsigned Succ
= 0, E
= IBI
->getNumSuccessors(); Succ
!= E
; ++Succ
)
352 Targets
.insert(IBI
->getSuccessor(Succ
));
358 bool ShouldUpdateAnalysis
= BPI
&& BFI
;
359 bool Changed
= false;
360 for (BasicBlock
*Target
: Targets
) {
361 if (IgnoreBlocksWithoutPHI
&& Target
->phis().empty())
364 SmallVector
<BasicBlock
*, 16> OtherPreds
;
365 BasicBlock
*IBRPred
= findIBRPredecessor(Target
, OtherPreds
);
366 // If we did not found an indirectbr, or the indirectbr is the only
367 // incoming edge, this isn't the kind of edge we're looking for.
368 if (!IBRPred
|| OtherPreds
.empty())
371 // Don't even think about ehpads/landingpads.
372 Instruction
*FirstNonPHI
= Target
->getFirstNonPHI();
373 if (FirstNonPHI
->isEHPad() || Target
->isLandingPad())
376 // Remember edge probabilities if needed.
377 SmallVector
<BranchProbability
, 4> EdgeProbabilities
;
378 if (ShouldUpdateAnalysis
) {
379 EdgeProbabilities
.reserve(Target
->getTerminator()->getNumSuccessors());
380 for (unsigned I
= 0, E
= Target
->getTerminator()->getNumSuccessors();
382 EdgeProbabilities
.emplace_back(BPI
->getEdgeProbability(Target
, I
));
383 BPI
->eraseBlock(Target
);
386 BasicBlock
*BodyBlock
= Target
->splitBasicBlock(FirstNonPHI
, ".split");
387 if (ShouldUpdateAnalysis
) {
388 // Copy the BFI/BPI from Target to BodyBlock.
389 BPI
->setEdgeProbability(BodyBlock
, EdgeProbabilities
);
390 BFI
->setBlockFreq(BodyBlock
, BFI
->getBlockFreq(Target
));
392 // It's possible Target was its own successor through an indirectbr.
393 // In this case, the indirectbr now comes from BodyBlock.
394 if (IBRPred
== Target
)
397 // At this point Target only has PHIs, and BodyBlock has the rest of the
398 // block's body. Create a copy of Target that will be used by the "direct"
400 ValueToValueMapTy VMap
;
401 BasicBlock
*DirectSucc
= CloneBasicBlock(Target
, VMap
, ".clone", &F
);
403 BlockFrequency BlockFreqForDirectSucc
;
404 for (BasicBlock
*Pred
: OtherPreds
) {
405 // If the target is a loop to itself, then the terminator of the split
406 // block (BodyBlock) needs to be updated.
407 BasicBlock
*Src
= Pred
!= Target
? Pred
: BodyBlock
;
408 Src
->getTerminator()->replaceUsesOfWith(Target
, DirectSucc
);
409 if (ShouldUpdateAnalysis
)
410 BlockFreqForDirectSucc
+= BFI
->getBlockFreq(Src
) *
411 BPI
->getEdgeProbability(Src
, DirectSucc
);
413 if (ShouldUpdateAnalysis
) {
414 BFI
->setBlockFreq(DirectSucc
, BlockFreqForDirectSucc
);
415 BlockFrequency NewBlockFreqForTarget
=
416 BFI
->getBlockFreq(Target
) - BlockFreqForDirectSucc
;
417 BFI
->setBlockFreq(Target
, NewBlockFreqForTarget
);
420 // Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that
421 // they are clones, so the number of PHIs are the same.
422 // (a) Remove the edge coming from IBRPred from the "Direct" PHI
423 // (b) Leave that as the only edge in the "Indirect" PHI.
424 // (c) Merge the two in the body block.
425 BasicBlock::iterator Indirect
= Target
->begin(),
426 End
= Target
->getFirstNonPHI()->getIterator();
427 BasicBlock::iterator Direct
= DirectSucc
->begin();
428 BasicBlock::iterator MergeInsert
= BodyBlock
->getFirstInsertionPt();
430 assert(&*End
== Target
->getTerminator() &&
431 "Block was expected to only contain PHIs");
433 while (Indirect
!= End
) {
434 PHINode
*DirPHI
= cast
<PHINode
>(Direct
);
435 PHINode
*IndPHI
= cast
<PHINode
>(Indirect
);
437 // Now, clean up - the direct block shouldn't get the indirect value,
439 DirPHI
->removeIncomingValue(IBRPred
);
442 // Advance the pointer here, to avoid invalidation issues when the old
446 PHINode
*NewIndPHI
= PHINode::Create(IndPHI
->getType(), 1, "ind", IndPHI
);
447 NewIndPHI
->addIncoming(IndPHI
->getIncomingValueForBlock(IBRPred
),
450 // Create a PHI in the body block, to merge the direct and indirect
452 PHINode
*MergePHI
= PHINode::Create(IndPHI
->getType(), 2, "merge");
453 MergePHI
->insertBefore(MergeInsert
);
454 MergePHI
->addIncoming(NewIndPHI
, Target
);
455 MergePHI
->addIncoming(DirPHI
, DirectSucc
);
457 IndPHI
->replaceAllUsesWith(MergePHI
);
458 IndPHI
->eraseFromParent();