1 ///===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ---------===//
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 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Sequence.h"
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Analysis/AssumptionCache.h"
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/CFG.h"
21 #include "llvm/Analysis/CodeMetrics.h"
22 #include "llvm/Analysis/DomTreeUpdater.h"
23 #include "llvm/Analysis/GuardUtils.h"
24 #include "llvm/Analysis/LoopAnalysisManager.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/LoopIterator.h"
27 #include "llvm/Analysis/MemorySSA.h"
28 #include "llvm/Analysis/MemorySSAUpdater.h"
29 #include "llvm/Analysis/MustExecute.h"
30 #include "llvm/Analysis/ProfileSummaryInfo.h"
31 #include "llvm/Analysis/ScalarEvolution.h"
32 #include "llvm/Analysis/TargetTransformInfo.h"
33 #include "llvm/Analysis/ValueTracking.h"
34 #include "llvm/IR/BasicBlock.h"
35 #include "llvm/IR/Constant.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/Dominators.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/IRBuilder.h"
40 #include "llvm/IR/InstrTypes.h"
41 #include "llvm/IR/Instruction.h"
42 #include "llvm/IR/Instructions.h"
43 #include "llvm/IR/IntrinsicInst.h"
44 #include "llvm/IR/Module.h"
45 #include "llvm/IR/PatternMatch.h"
46 #include "llvm/IR/ProfDataUtils.h"
47 #include "llvm/IR/Use.h"
48 #include "llvm/IR/Value.h"
49 #include "llvm/Support/Casting.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/GenericDomTree.h"
54 #include "llvm/Support/InstructionCost.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Transforms/Scalar/LoopPassManager.h"
57 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
58 #include "llvm/Transforms/Utils/Cloning.h"
59 #include "llvm/Transforms/Utils/Local.h"
60 #include "llvm/Transforms/Utils/LoopUtils.h"
61 #include "llvm/Transforms/Utils/ValueMapper.h"
69 #define DEBUG_TYPE "simple-loop-unswitch"
72 using namespace llvm::PatternMatch
;
74 STATISTIC(NumBranches
, "Number of branches unswitched");
75 STATISTIC(NumSwitches
, "Number of switches unswitched");
76 STATISTIC(NumSelects
, "Number of selects turned into branches for unswitching");
77 STATISTIC(NumGuards
, "Number of guards turned into branches for unswitching");
78 STATISTIC(NumTrivial
, "Number of unswitches that are trivial");
80 NumCostMultiplierSkipped
,
81 "Number of unswitch candidates that had their cost multiplier skipped");
82 STATISTIC(NumInvariantConditionsInjected
,
83 "Number of invariant conditions injected and unswitched");
85 static cl::opt
<bool> EnableNonTrivialUnswitch(
86 "enable-nontrivial-unswitch", cl::init(false), cl::Hidden
,
87 cl::desc("Forcibly enables non-trivial loop unswitching rather than "
88 "following the configuration passed into the pass."));
91 UnswitchThreshold("unswitch-threshold", cl::init(50), cl::Hidden
,
92 cl::desc("The cost threshold for unswitching a loop."));
94 static cl::opt
<bool> EnableUnswitchCostMultiplier(
95 "enable-unswitch-cost-multiplier", cl::init(true), cl::Hidden
,
96 cl::desc("Enable unswitch cost multiplier that prohibits exponential "
97 "explosion in nontrivial unswitch."));
98 static cl::opt
<int> UnswitchSiblingsToplevelDiv(
99 "unswitch-siblings-toplevel-div", cl::init(2), cl::Hidden
,
100 cl::desc("Toplevel siblings divisor for cost multiplier."));
101 static cl::opt
<int> UnswitchNumInitialUnscaledCandidates(
102 "unswitch-num-initial-unscaled-candidates", cl::init(8), cl::Hidden
,
103 cl::desc("Number of unswitch candidates that are ignored when calculating "
104 "cost multiplier."));
105 static cl::opt
<bool> UnswitchGuards(
106 "simple-loop-unswitch-guards", cl::init(true), cl::Hidden
,
107 cl::desc("If enabled, simple loop unswitching will also consider "
108 "llvm.experimental.guard intrinsics as unswitch candidates."));
109 static cl::opt
<bool> DropNonTrivialImplicitNullChecks(
110 "simple-loop-unswitch-drop-non-trivial-implicit-null-checks",
111 cl::init(false), cl::Hidden
,
112 cl::desc("If enabled, drop make.implicit metadata in unswitched implicit "
113 "null checks to save time analyzing if we can keep it."));
114 static cl::opt
<unsigned>
115 MSSAThreshold("simple-loop-unswitch-memoryssa-threshold",
116 cl::desc("Max number of memory uses to explore during "
117 "partial unswitching analysis"),
118 cl::init(100), cl::Hidden
);
119 static cl::opt
<bool> FreezeLoopUnswitchCond(
120 "freeze-loop-unswitch-cond", cl::init(true), cl::Hidden
,
121 cl::desc("If enabled, the freeze instruction will be added to condition "
122 "of loop unswitch to prevent miscompilation."));
124 static cl::opt
<bool> InjectInvariantConditions(
125 "simple-loop-unswitch-inject-invariant-conditions", cl::Hidden
,
126 cl::desc("Whether we should inject new invariants and unswitch them to "
127 "eliminate some existing (non-invariant) conditions."),
130 static cl::opt
<unsigned> InjectInvariantConditionHotnesThreshold(
131 "simple-loop-unswitch-inject-invariant-condition-hotness-threshold",
132 cl::Hidden
, cl::desc("Only try to inject loop invariant conditions and "
133 "unswitch on them to eliminate branches that are "
134 "not-taken 1/<this option> times or less."),
137 AnalysisKey
ShouldRunExtraSimpleLoopUnswitch::Key
;
142 BasicBlock
*InLoopSucc
;
144 CompareDesc(BranchInst
*Term
, Value
*Invariant
, BasicBlock
*InLoopSucc
)
145 : Term(Term
), Invariant(Invariant
), InLoopSucc(InLoopSucc
) {}
148 struct InjectedInvariant
{
149 ICmpInst::Predicate Pred
;
152 BasicBlock
*InLoopSucc
;
154 InjectedInvariant(ICmpInst::Predicate Pred
, Value
*LHS
, Value
*RHS
,
155 BasicBlock
*InLoopSucc
)
156 : Pred(Pred
), LHS(LHS
), RHS(RHS
), InLoopSucc(InLoopSucc
) {}
159 struct NonTrivialUnswitchCandidate
{
160 Instruction
*TI
= nullptr;
161 TinyPtrVector
<Value
*> Invariants
;
162 std::optional
<InstructionCost
> Cost
;
163 std::optional
<InjectedInvariant
> PendingInjection
;
164 NonTrivialUnswitchCandidate(
165 Instruction
*TI
, ArrayRef
<Value
*> Invariants
,
166 std::optional
<InstructionCost
> Cost
= std::nullopt
,
167 std::optional
<InjectedInvariant
> PendingInjection
= std::nullopt
)
168 : TI(TI
), Invariants(Invariants
), Cost(Cost
),
169 PendingInjection(PendingInjection
) {};
171 bool hasPendingInjection() const { return PendingInjection
.has_value(); }
173 } // end anonymous namespace.
175 // Helper to skip (select x, true, false), which matches both a logical AND and
176 // OR and can confuse code that tries to determine if \p Cond is either a
177 // logical AND or OR but not both.
178 static Value
*skipTrivialSelect(Value
*Cond
) {
180 while (match(Cond
, m_Select(m_Value(CondNext
), m_One(), m_Zero())))
185 /// Collect all of the loop invariant input values transitively used by the
186 /// homogeneous instruction graph from a given root.
188 /// This essentially walks from a root recursively through loop variant operands
189 /// which have perform the same logical operation (AND or OR) and finds all
190 /// inputs which are loop invariant. For some operations these can be
191 /// re-associated and unswitched out of the loop entirely.
192 static TinyPtrVector
<Value
*>
193 collectHomogenousInstGraphLoopInvariants(const Loop
&L
, Instruction
&Root
,
194 const LoopInfo
&LI
) {
195 assert(!L
.isLoopInvariant(&Root
) &&
196 "Only need to walk the graph if root itself is not invariant.");
197 TinyPtrVector
<Value
*> Invariants
;
199 bool IsRootAnd
= match(&Root
, m_LogicalAnd());
200 bool IsRootOr
= match(&Root
, m_LogicalOr());
202 // Build a worklist and recurse through operators collecting invariants.
203 SmallVector
<Instruction
*, 4> Worklist
;
204 SmallPtrSet
<Instruction
*, 8> Visited
;
205 Worklist
.push_back(&Root
);
206 Visited
.insert(&Root
);
208 Instruction
&I
= *Worklist
.pop_back_val();
209 for (Value
*OpV
: I
.operand_values()) {
210 // Skip constants as unswitching isn't interesting for them.
211 if (isa
<Constant
>(OpV
))
214 // Add it to our result if loop invariant.
215 if (L
.isLoopInvariant(OpV
)) {
216 Invariants
.push_back(OpV
);
220 // If not an instruction with the same opcode, nothing we can do.
221 Instruction
*OpI
= dyn_cast
<Instruction
>(skipTrivialSelect(OpV
));
223 if (OpI
&& ((IsRootAnd
&& match(OpI
, m_LogicalAnd())) ||
224 (IsRootOr
&& match(OpI
, m_LogicalOr())))) {
225 // Visit this operand.
226 if (Visited
.insert(OpI
).second
)
227 Worklist
.push_back(OpI
);
230 } while (!Worklist
.empty());
235 static void replaceLoopInvariantUses(const Loop
&L
, Value
*Invariant
,
236 Constant
&Replacement
) {
237 assert(!isa
<Constant
>(Invariant
) && "Why are we unswitching on a constant?");
239 // Replace uses of LIC in the loop with the given constant.
240 // We use make_early_inc_range as set invalidates the iterator.
241 for (Use
&U
: llvm::make_early_inc_range(Invariant
->uses())) {
242 Instruction
*UserI
= dyn_cast
<Instruction
>(U
.getUser());
244 // Replace this use within the loop body.
245 if (UserI
&& L
.contains(UserI
))
250 /// Check that all the LCSSA PHI nodes in the loop exit block have trivial
251 /// incoming values along this edge.
252 static bool areLoopExitPHIsLoopInvariant(const Loop
&L
,
253 const BasicBlock
&ExitingBB
,
254 const BasicBlock
&ExitBB
) {
255 for (const Instruction
&I
: ExitBB
) {
256 auto *PN
= dyn_cast
<PHINode
>(&I
);
258 // No more PHIs to check.
261 // If the incoming value for this edge isn't loop invariant the unswitch
263 if (!L
.isLoopInvariant(PN
->getIncomingValueForBlock(&ExitingBB
)))
266 llvm_unreachable("Basic blocks should never be empty!");
269 /// Copy a set of loop invariant values \p ToDuplicate and insert them at the
270 /// end of \p BB and conditionally branch on the copied condition. We only
271 /// branch on a single value.
272 static void buildPartialUnswitchConditionalBranch(
273 BasicBlock
&BB
, ArrayRef
<Value
*> Invariants
, bool Direction
,
274 BasicBlock
&UnswitchedSucc
, BasicBlock
&NormalSucc
, bool InsertFreeze
,
275 const Instruction
*I
, AssumptionCache
*AC
, const DominatorTree
&DT
) {
276 IRBuilder
<> IRB(&BB
);
278 SmallVector
<Value
*> FrozenInvariants
;
279 for (Value
*Inv
: Invariants
) {
280 if (InsertFreeze
&& !isGuaranteedNotToBeUndefOrPoison(Inv
, AC
, I
, &DT
))
281 Inv
= IRB
.CreateFreeze(Inv
, Inv
->getName() + ".fr");
282 FrozenInvariants
.push_back(Inv
);
285 Value
*Cond
= Direction
? IRB
.CreateOr(FrozenInvariants
)
286 : IRB
.CreateAnd(FrozenInvariants
);
287 IRB
.CreateCondBr(Cond
, Direction
? &UnswitchedSucc
: &NormalSucc
,
288 Direction
? &NormalSucc
: &UnswitchedSucc
);
291 /// Copy a set of loop invariant values, and conditionally branch on them.
292 static void buildPartialInvariantUnswitchConditionalBranch(
293 BasicBlock
&BB
, ArrayRef
<Value
*> ToDuplicate
, bool Direction
,
294 BasicBlock
&UnswitchedSucc
, BasicBlock
&NormalSucc
, Loop
&L
,
295 MemorySSAUpdater
*MSSAU
) {
296 ValueToValueMapTy VMap
;
297 for (auto *Val
: reverse(ToDuplicate
)) {
298 Instruction
*Inst
= cast
<Instruction
>(Val
);
299 Instruction
*NewInst
= Inst
->clone();
300 NewInst
->insertInto(&BB
, BB
.end());
301 RemapInstruction(NewInst
, VMap
,
302 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
308 MemorySSA
*MSSA
= MSSAU
->getMemorySSA();
310 dyn_cast_or_null
<MemoryUse
>(MSSA
->getMemoryAccess(Inst
))) {
311 auto *DefiningAccess
= MemUse
->getDefiningAccess();
312 // Get the first defining access before the loop.
313 while (L
.contains(DefiningAccess
->getBlock())) {
314 // If the defining access is a MemoryPhi, get the incoming
315 // value for the pre-header as defining access.
316 if (auto *MemPhi
= dyn_cast
<MemoryPhi
>(DefiningAccess
))
318 MemPhi
->getIncomingValueForBlock(L
.getLoopPreheader());
320 DefiningAccess
= cast
<MemoryDef
>(DefiningAccess
)->getDefiningAccess();
322 MSSAU
->createMemoryAccessInBB(NewInst
, DefiningAccess
,
323 NewInst
->getParent(),
324 MemorySSA::BeforeTerminator
);
328 IRBuilder
<> IRB(&BB
);
329 Value
*Cond
= VMap
[ToDuplicate
[0]];
330 IRB
.CreateCondBr(Cond
, Direction
? &UnswitchedSucc
: &NormalSucc
,
331 Direction
? &NormalSucc
: &UnswitchedSucc
);
334 /// Rewrite the PHI nodes in an unswitched loop exit basic block.
336 /// Requires that the loop exit and unswitched basic block are the same, and
337 /// that the exiting block was a unique predecessor of that block. Rewrites the
338 /// PHI nodes in that block such that what were LCSSA PHI nodes become trivial
339 /// PHI nodes from the old preheader that now contains the unswitched
341 static void rewritePHINodesForUnswitchedExitBlock(BasicBlock
&UnswitchedBB
,
342 BasicBlock
&OldExitingBB
,
344 for (PHINode
&PN
: UnswitchedBB
.phis()) {
345 // When the loop exit is directly unswitched we just need to update the
346 // incoming basic block. We loop to handle weird cases with repeated
347 // incoming blocks, but expect to typically only have one operand here.
348 for (auto i
: seq
<int>(0, PN
.getNumOperands())) {
349 assert(PN
.getIncomingBlock(i
) == &OldExitingBB
&&
350 "Found incoming block different from unique predecessor!");
351 PN
.setIncomingBlock(i
, &OldPH
);
356 /// Rewrite the PHI nodes in the loop exit basic block and the split off
357 /// unswitched block.
359 /// Because the exit block remains an exit from the loop, this rewrites the
360 /// LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI
361 /// nodes into the unswitched basic block to select between the value in the
362 /// old preheader and the loop exit.
363 static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock
&ExitBB
,
364 BasicBlock
&UnswitchedBB
,
365 BasicBlock
&OldExitingBB
,
368 assert(&ExitBB
!= &UnswitchedBB
&&
369 "Must have different loop exit and unswitched blocks!");
370 BasicBlock::iterator InsertPt
= UnswitchedBB
.begin();
371 for (PHINode
&PN
: ExitBB
.phis()) {
372 auto *NewPN
= PHINode::Create(PN
.getType(), /*NumReservedValues*/ 2,
373 PN
.getName() + ".split");
374 NewPN
->insertBefore(InsertPt
);
376 // Walk backwards over the old PHI node's inputs to minimize the cost of
377 // removing each one. We have to do this weird loop manually so that we
378 // create the same number of new incoming edges in the new PHI as we expect
379 // each case-based edge to be included in the unswitched switch in some
381 // FIXME: This is really, really gross. It would be much cleaner if LLVM
382 // allowed us to create a single entry for a predecessor block without
383 // having separate entries for each "edge" even though these edges are
384 // required to produce identical results.
385 for (int i
= PN
.getNumIncomingValues() - 1; i
>= 0; --i
) {
386 if (PN
.getIncomingBlock(i
) != &OldExitingBB
)
389 Value
*Incoming
= PN
.getIncomingValue(i
);
391 // No more edge from the old exiting block to the exit block.
392 PN
.removeIncomingValue(i
);
394 NewPN
->addIncoming(Incoming
, &OldPH
);
397 // Now replace the old PHI with the new one and wire the old one in as an
398 // input to the new one.
399 PN
.replaceAllUsesWith(NewPN
);
400 NewPN
->addIncoming(&PN
, &ExitBB
);
404 /// Hoist the current loop up to the innermost loop containing a remaining exit.
406 /// Because we've removed an exit from the loop, we may have changed the set of
407 /// loops reachable and need to move the current loop up the loop nest or even
408 /// to an entirely separate nest.
409 static void hoistLoopToNewParent(Loop
&L
, BasicBlock
&Preheader
,
410 DominatorTree
&DT
, LoopInfo
&LI
,
411 MemorySSAUpdater
*MSSAU
, ScalarEvolution
*SE
) {
412 // If the loop is already at the top level, we can't hoist it anywhere.
413 Loop
*OldParentL
= L
.getParentLoop();
417 SmallVector
<BasicBlock
*, 4> Exits
;
418 L
.getExitBlocks(Exits
);
419 Loop
*NewParentL
= nullptr;
420 for (auto *ExitBB
: Exits
)
421 if (Loop
*ExitL
= LI
.getLoopFor(ExitBB
))
422 if (!NewParentL
|| NewParentL
->contains(ExitL
))
425 if (NewParentL
== OldParentL
)
428 // The new parent loop (if different) should always contain the old one.
430 assert(NewParentL
->contains(OldParentL
) &&
431 "Can only hoist this loop up the nest!");
433 // The preheader will need to move with the body of this loop. However,
434 // because it isn't in this loop we also need to update the primary loop map.
435 assert(OldParentL
== LI
.getLoopFor(&Preheader
) &&
436 "Parent loop of this loop should contain this loop's preheader!");
437 LI
.changeLoopFor(&Preheader
, NewParentL
);
439 // Remove this loop from its old parent.
440 OldParentL
->removeChildLoop(&L
);
442 // Add the loop either to the new parent or as a top-level loop.
444 NewParentL
->addChildLoop(&L
);
446 LI
.addTopLevelLoop(&L
);
448 // Remove this loops blocks from the old parent and every other loop up the
449 // nest until reaching the new parent. Also update all of these
450 // no-longer-containing loops to reflect the nesting change.
451 for (Loop
*OldContainingL
= OldParentL
; OldContainingL
!= NewParentL
;
452 OldContainingL
= OldContainingL
->getParentLoop()) {
453 llvm::erase_if(OldContainingL
->getBlocksVector(),
454 [&](const BasicBlock
*BB
) {
455 return BB
== &Preheader
|| L
.contains(BB
);
458 OldContainingL
->getBlocksSet().erase(&Preheader
);
459 for (BasicBlock
*BB
: L
.blocks())
460 OldContainingL
->getBlocksSet().erase(BB
);
462 // Because we just hoisted a loop out of this one, we have essentially
463 // created new exit paths from it. That means we need to form LCSSA PHI
464 // nodes for values used in the no-longer-nested loop.
465 formLCSSA(*OldContainingL
, DT
, &LI
, SE
);
467 // We shouldn't need to form dedicated exits because the exit introduced
468 // here is the (just split by unswitching) preheader. However, after trivial
469 // unswitching it is possible to get new non-dedicated exits out of parent
470 // loop so let's conservatively form dedicated exit blocks and figure out
471 // if we can optimize later.
472 formDedicatedExitBlocks(OldContainingL
, &DT
, &LI
, MSSAU
,
473 /*PreserveLCSSA*/ true);
477 // Return the top-most loop containing ExitBB and having ExitBB as exiting block
478 // or the loop containing ExitBB, if there is no parent loop containing ExitBB
480 static Loop
*getTopMostExitingLoop(const BasicBlock
*ExitBB
,
481 const LoopInfo
&LI
) {
482 Loop
*TopMost
= LI
.getLoopFor(ExitBB
);
483 Loop
*Current
= TopMost
;
485 if (Current
->isLoopExiting(ExitBB
))
487 Current
= Current
->getParentLoop();
492 /// Unswitch a trivial branch if the condition is loop invariant.
494 /// This routine should only be called when loop code leading to the branch has
495 /// been validated as trivial (no side effects). This routine checks if the
496 /// condition is invariant and one of the successors is a loop exit. This
497 /// allows us to unswitch without duplicating the loop, making it trivial.
499 /// If this routine fails to unswitch the branch it returns false.
501 /// If the branch can be unswitched, this routine splits the preheader and
502 /// hoists the branch above that split. Preserves loop simplified form
503 /// (splitting the exit block as necessary). It simplifies the branch within
504 /// the loop to an unconditional branch but doesn't remove it entirely. Further
505 /// cleanup can be done with some simplifycfg like pass.
507 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
508 /// invalidated by this.
509 static bool unswitchTrivialBranch(Loop
&L
, BranchInst
&BI
, DominatorTree
&DT
,
510 LoopInfo
&LI
, ScalarEvolution
*SE
,
511 MemorySSAUpdater
*MSSAU
) {
512 assert(BI
.isConditional() && "Can only unswitch a conditional branch!");
513 LLVM_DEBUG(dbgs() << " Trying to unswitch branch: " << BI
<< "\n");
515 // The loop invariant values that we want to unswitch.
516 TinyPtrVector
<Value
*> Invariants
;
518 // When true, we're fully unswitching the branch rather than just unswitching
519 // some input conditions to the branch.
520 bool FullUnswitch
= false;
522 Value
*Cond
= skipTrivialSelect(BI
.getCondition());
523 if (L
.isLoopInvariant(Cond
)) {
524 Invariants
.push_back(Cond
);
527 if (auto *CondInst
= dyn_cast
<Instruction
>(Cond
))
528 Invariants
= collectHomogenousInstGraphLoopInvariants(L
, *CondInst
, LI
);
529 if (Invariants
.empty()) {
530 LLVM_DEBUG(dbgs() << " Couldn't find invariant inputs!\n");
535 // Check that one of the branch's successors exits, and which one.
536 bool ExitDirection
= true;
537 int LoopExitSuccIdx
= 0;
538 auto *LoopExitBB
= BI
.getSuccessor(0);
539 if (L
.contains(LoopExitBB
)) {
540 ExitDirection
= false;
542 LoopExitBB
= BI
.getSuccessor(1);
543 if (L
.contains(LoopExitBB
)) {
544 LLVM_DEBUG(dbgs() << " Branch doesn't exit the loop!\n");
548 auto *ContinueBB
= BI
.getSuccessor(1 - LoopExitSuccIdx
);
549 auto *ParentBB
= BI
.getParent();
550 if (!areLoopExitPHIsLoopInvariant(L
, *ParentBB
, *LoopExitBB
)) {
551 LLVM_DEBUG(dbgs() << " Loop exit PHI's aren't loop-invariant!\n");
555 // When unswitching only part of the branch's condition, we need the exit
556 // block to be reached directly from the partially unswitched input. This can
557 // be done when the exit block is along the true edge and the branch condition
558 // is a graph of `or` operations, or the exit block is along the false edge
559 // and the condition is a graph of `and` operations.
561 if (ExitDirection
? !match(Cond
, m_LogicalOr())
562 : !match(Cond
, m_LogicalAnd())) {
563 LLVM_DEBUG(dbgs() << " Branch condition is in improper form for "
564 "non-full unswitch!\n");
570 dbgs() << " unswitching trivial invariant conditions for: " << BI
572 for (Value
*Invariant
: Invariants
) {
573 dbgs() << " " << *Invariant
<< " == true";
574 if (Invariant
!= Invariants
.back())
580 // If we have scalar evolutions, we need to invalidate them including this
581 // loop, the loop containing the exit block and the topmost parent loop
582 // exiting via LoopExitBB.
584 if (const Loop
*ExitL
= getTopMostExitingLoop(LoopExitBB
, LI
))
585 SE
->forgetLoop(ExitL
);
587 // Forget the entire nest as this exits the entire nest.
588 SE
->forgetTopmostLoop(&L
);
589 SE
->forgetBlockAndLoopDispositions();
592 if (MSSAU
&& VerifyMemorySSA
)
593 MSSAU
->getMemorySSA()->verifyMemorySSA();
595 // Split the preheader, so that we know that there is a safe place to insert
596 // the conditional branch. We will change the preheader to have a conditional
597 // branch on LoopCond.
598 BasicBlock
*OldPH
= L
.getLoopPreheader();
599 BasicBlock
*NewPH
= SplitEdge(OldPH
, L
.getHeader(), &DT
, &LI
, MSSAU
);
601 // Now that we have a place to insert the conditional branch, create a place
602 // to branch to: this is the exit block out of the loop that we are
603 // unswitching. We need to split this if there are other loop predecessors.
604 // Because the loop is in simplified form, *any* other predecessor is enough.
605 BasicBlock
*UnswitchedBB
;
606 if (FullUnswitch
&& LoopExitBB
->getUniquePredecessor()) {
607 assert(LoopExitBB
->getUniquePredecessor() == BI
.getParent() &&
608 "A branch's parent isn't a predecessor!");
609 UnswitchedBB
= LoopExitBB
;
612 SplitBlock(LoopExitBB
, LoopExitBB
->begin(), &DT
, &LI
, MSSAU
, "", false);
615 if (MSSAU
&& VerifyMemorySSA
)
616 MSSAU
->getMemorySSA()->verifyMemorySSA();
618 // Actually move the invariant uses into the unswitched position. If possible,
619 // we do this by moving the instructions, but when doing partial unswitching
620 // we do it by building a new merge of the values in the unswitched position.
621 OldPH
->getTerminator()->eraseFromParent();
623 // If fully unswitching, we can use the existing branch instruction.
624 // Splice it into the old PH to gate reaching the new preheader and re-point
626 BI
.moveBefore(*OldPH
, OldPH
->end());
627 BI
.setCondition(Cond
);
629 // Temporarily clone the terminator, to make MSSA update cheaper by
630 // separating "insert edge" updates from "remove edge" ones.
631 BI
.clone()->insertInto(ParentBB
, ParentBB
->end());
633 // Create a new unconditional branch that will continue the loop as a new
635 Instruction
*NewBI
= BranchInst::Create(ContinueBB
, ParentBB
);
636 NewBI
->setDebugLoc(BI
.getDebugLoc());
638 BI
.setSuccessor(LoopExitSuccIdx
, UnswitchedBB
);
639 BI
.setSuccessor(1 - LoopExitSuccIdx
, NewPH
);
641 // Only unswitching a subset of inputs to the condition, so we will need to
642 // build a new branch that merges the invariant inputs.
644 assert(match(skipTrivialSelect(BI
.getCondition()), m_LogicalOr()) &&
645 "Must have an `or` of `i1`s or `select i1 X, true, Y`s for the "
648 assert(match(skipTrivialSelect(BI
.getCondition()), m_LogicalAnd()) &&
649 "Must have an `and` of `i1`s or `select i1 X, Y, false`s for the"
651 buildPartialUnswitchConditionalBranch(
652 *OldPH
, Invariants
, ExitDirection
, *UnswitchedBB
, *NewPH
,
653 FreezeLoopUnswitchCond
, OldPH
->getTerminator(), nullptr, DT
);
656 // Update the dominator tree with the added edge.
657 DT
.insertEdge(OldPH
, UnswitchedBB
);
659 // After the dominator tree was updated with the added edge, update MemorySSA
662 SmallVector
<CFGUpdate
, 1> Updates
;
663 Updates
.push_back({cfg::UpdateKind::Insert
, OldPH
, UnswitchedBB
});
664 MSSAU
->applyInsertUpdates(Updates
, DT
);
667 // Finish updating dominator tree and memory ssa for full unswitch.
670 Instruction
*Term
= ParentBB
->getTerminator();
671 // Remove the cloned branch instruction and create unconditional branch
673 Instruction
*NewBI
= BranchInst::Create(ContinueBB
, ParentBB
);
674 NewBI
->setDebugLoc(Term
->getDebugLoc());
675 Term
->eraseFromParent();
676 MSSAU
->removeEdge(ParentBB
, LoopExitBB
);
678 DT
.deleteEdge(ParentBB
, LoopExitBB
);
681 if (MSSAU
&& VerifyMemorySSA
)
682 MSSAU
->getMemorySSA()->verifyMemorySSA();
684 // Rewrite the relevant PHI nodes.
685 if (UnswitchedBB
== LoopExitBB
)
686 rewritePHINodesForUnswitchedExitBlock(*UnswitchedBB
, *ParentBB
, *OldPH
);
688 rewritePHINodesForExitAndUnswitchedBlocks(*LoopExitBB
, *UnswitchedBB
,
689 *ParentBB
, *OldPH
, FullUnswitch
);
691 // The constant we can replace all of our invariants with inside the loop
692 // body. If any of the invariants have a value other than this the loop won't
694 ConstantInt
*Replacement
= ExitDirection
695 ? ConstantInt::getFalse(BI
.getContext())
696 : ConstantInt::getTrue(BI
.getContext());
698 // Since this is an i1 condition we can also trivially replace uses of it
699 // within the loop with a constant.
700 for (Value
*Invariant
: Invariants
)
701 replaceLoopInvariantUses(L
, Invariant
, *Replacement
);
703 // If this was full unswitching, we may have changed the nesting relationship
704 // for this loop so hoist it to its correct parent if needed.
706 hoistLoopToNewParent(L
, *NewPH
, DT
, LI
, MSSAU
, SE
);
708 if (MSSAU
&& VerifyMemorySSA
)
709 MSSAU
->getMemorySSA()->verifyMemorySSA();
711 LLVM_DEBUG(dbgs() << " done: unswitching trivial branch...\n");
717 /// Unswitch a trivial switch if the condition is loop invariant.
719 /// This routine should only be called when loop code leading to the switch has
720 /// been validated as trivial (no side effects). This routine checks if the
721 /// condition is invariant and that at least one of the successors is a loop
722 /// exit. This allows us to unswitch without duplicating the loop, making it
725 /// If this routine fails to unswitch the switch it returns false.
727 /// If the switch can be unswitched, this routine splits the preheader and
728 /// copies the switch above that split. If the default case is one of the
729 /// exiting cases, it copies the non-exiting cases and points them at the new
730 /// preheader. If the default case is not exiting, it copies the exiting cases
731 /// and points the default at the preheader. It preserves loop simplified form
732 /// (splitting the exit blocks as necessary). It simplifies the switch within
733 /// the loop by removing now-dead cases. If the default case is one of those
734 /// unswitched, it replaces its destination with a new basic block containing
735 /// only unreachable. Such basic blocks, while technically loop exits, are not
736 /// considered for unswitching so this is a stable transform and the same
737 /// switch will not be revisited. If after unswitching there is only a single
738 /// in-loop successor, the switch is further simplified to an unconditional
739 /// branch. Still more cleanup can be done with some simplifycfg like pass.
741 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
742 /// invalidated by this.
743 static bool unswitchTrivialSwitch(Loop
&L
, SwitchInst
&SI
, DominatorTree
&DT
,
744 LoopInfo
&LI
, ScalarEvolution
*SE
,
745 MemorySSAUpdater
*MSSAU
) {
746 LLVM_DEBUG(dbgs() << " Trying to unswitch switch: " << SI
<< "\n");
747 Value
*LoopCond
= SI
.getCondition();
749 // If this isn't switching on an invariant condition, we can't unswitch it.
750 if (!L
.isLoopInvariant(LoopCond
))
753 auto *ParentBB
= SI
.getParent();
755 // The same check must be used both for the default and the exit cases. We
756 // should never leave edges from the switch instruction to a basic block that
757 // we are unswitching, hence the condition used to determine the default case
758 // needs to also be used to populate ExitCaseIndices, which is then used to
759 // remove cases from the switch.
760 auto IsTriviallyUnswitchableExitBlock
= [&](BasicBlock
&BBToCheck
) {
761 // BBToCheck is not an exit block if it is inside loop L.
762 if (L
.contains(&BBToCheck
))
764 // BBToCheck is not trivial to unswitch if its phis aren't loop invariant.
765 if (!areLoopExitPHIsLoopInvariant(L
, *ParentBB
, BBToCheck
))
767 // We do not unswitch a block that only has an unreachable statement, as
768 // it's possible this is a previously unswitched block. Only unswitch if
769 // either the terminator is not unreachable, or, if it is, it's not the only
770 // instruction in the block.
771 auto *TI
= BBToCheck
.getTerminator();
772 bool isUnreachable
= isa
<UnreachableInst
>(TI
);
773 return !isUnreachable
|| BBToCheck
.getFirstNonPHIOrDbg() != TI
;
776 SmallVector
<int, 4> ExitCaseIndices
;
777 for (auto Case
: SI
.cases())
778 if (IsTriviallyUnswitchableExitBlock(*Case
.getCaseSuccessor()))
779 ExitCaseIndices
.push_back(Case
.getCaseIndex());
780 BasicBlock
*DefaultExitBB
= nullptr;
781 SwitchInstProfUpdateWrapper::CaseWeightOpt DefaultCaseWeight
=
782 SwitchInstProfUpdateWrapper::getSuccessorWeight(SI
, 0);
783 if (IsTriviallyUnswitchableExitBlock(*SI
.getDefaultDest())) {
784 DefaultExitBB
= SI
.getDefaultDest();
785 } else if (ExitCaseIndices
.empty())
788 LLVM_DEBUG(dbgs() << " unswitching trivial switch...\n");
790 if (MSSAU
&& VerifyMemorySSA
)
791 MSSAU
->getMemorySSA()->verifyMemorySSA();
793 // We may need to invalidate SCEVs for the outermost loop reached by any of
798 // Check the loop containing this exit.
799 Loop
*ExitL
= getTopMostExitingLoop(DefaultExitBB
, LI
);
800 if (!ExitL
|| ExitL
->contains(OuterL
))
803 for (unsigned Index
: ExitCaseIndices
) {
804 auto CaseI
= SI
.case_begin() + Index
;
805 // Compute the outer loop from this exit.
806 Loop
*ExitL
= getTopMostExitingLoop(CaseI
->getCaseSuccessor(), LI
);
807 if (!ExitL
|| ExitL
->contains(OuterL
))
813 SE
->forgetLoop(OuterL
);
815 SE
->forgetTopmostLoop(&L
);
819 // Clear out the default destination temporarily to allow accurate
820 // predecessor lists to be examined below.
821 SI
.setDefaultDest(nullptr);
824 // Store the exit cases into a separate data structure and remove them from
826 SmallVector
<std::tuple
<ConstantInt
*, BasicBlock
*,
827 SwitchInstProfUpdateWrapper::CaseWeightOpt
>,
829 ExitCases
.reserve(ExitCaseIndices
.size());
830 SwitchInstProfUpdateWrapper
SIW(SI
);
831 // We walk the case indices backwards so that we remove the last case first
832 // and don't disrupt the earlier indices.
833 for (unsigned Index
: reverse(ExitCaseIndices
)) {
834 auto CaseI
= SI
.case_begin() + Index
;
835 // Save the value of this case.
836 auto W
= SIW
.getSuccessorWeight(CaseI
->getSuccessorIndex());
837 ExitCases
.emplace_back(CaseI
->getCaseValue(), CaseI
->getCaseSuccessor(), W
);
838 // Delete the unswitched cases.
839 SIW
.removeCase(CaseI
);
842 // Check if after this all of the remaining cases point at the same
844 BasicBlock
*CommonSuccBB
= nullptr;
845 if (SI
.getNumCases() > 0 &&
846 all_of(drop_begin(SI
.cases()), [&SI
](const SwitchInst::CaseHandle
&Case
) {
847 return Case
.getCaseSuccessor() == SI
.case_begin()->getCaseSuccessor();
849 CommonSuccBB
= SI
.case_begin()->getCaseSuccessor();
850 if (!DefaultExitBB
) {
851 // If we're not unswitching the default, we need it to match any cases to
852 // have a common successor or if we have no cases it is the common
854 if (SI
.getNumCases() == 0)
855 CommonSuccBB
= SI
.getDefaultDest();
856 else if (SI
.getDefaultDest() != CommonSuccBB
)
857 CommonSuccBB
= nullptr;
860 // Split the preheader, so that we know that there is a safe place to insert
862 BasicBlock
*OldPH
= L
.getLoopPreheader();
863 BasicBlock
*NewPH
= SplitEdge(OldPH
, L
.getHeader(), &DT
, &LI
, MSSAU
);
864 OldPH
->getTerminator()->eraseFromParent();
866 // Now add the unswitched switch. This new switch instruction inherits the
867 // debug location of the old switch, because it semantically replace the old
869 auto *NewSI
= SwitchInst::Create(LoopCond
, NewPH
, ExitCases
.size(), OldPH
);
870 NewSI
->setDebugLoc(SIW
->getDebugLoc());
871 SwitchInstProfUpdateWrapper
NewSIW(*NewSI
);
873 // Rewrite the IR for the unswitched basic blocks. This requires two steps.
874 // First, we split any exit blocks with remaining in-loop predecessors. Then
875 // we update the PHIs in one of two ways depending on if there was a split.
876 // We walk in reverse so that we split in the same order as the cases
877 // appeared. This is purely for convenience of reading the resulting IR, but
878 // it doesn't cost anything really.
879 SmallPtrSet
<BasicBlock
*, 2> UnswitchedExitBBs
;
880 SmallDenseMap
<BasicBlock
*, BasicBlock
*, 2> SplitExitBBMap
;
881 // Handle the default exit if necessary.
882 // FIXME: It'd be great if we could merge this with the loop below but LLVM's
883 // ranges aren't quite powerful enough yet.
885 if (pred_empty(DefaultExitBB
)) {
886 UnswitchedExitBBs
.insert(DefaultExitBB
);
887 rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB
, *ParentBB
, *OldPH
);
890 SplitBlock(DefaultExitBB
, DefaultExitBB
->begin(), &DT
, &LI
, MSSAU
);
891 rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB
, *SplitBB
,
893 /*FullUnswitch*/ true);
894 DefaultExitBB
= SplitExitBBMap
[DefaultExitBB
] = SplitBB
;
897 // Note that we must use a reference in the for loop so that we update the
899 for (auto &ExitCase
: reverse(ExitCases
)) {
900 // Grab a reference to the exit block in the pair so that we can update it.
901 BasicBlock
*ExitBB
= std::get
<1>(ExitCase
);
903 // If this case is the last edge into the exit block, we can simply reuse it
904 // as it will no longer be a loop exit. No mapping necessary.
905 if (pred_empty(ExitBB
)) {
906 // Only rewrite once.
907 if (UnswitchedExitBBs
.insert(ExitBB
).second
)
908 rewritePHINodesForUnswitchedExitBlock(*ExitBB
, *ParentBB
, *OldPH
);
912 // Otherwise we need to split the exit block so that we retain an exit
913 // block from the loop and a target for the unswitched condition.
914 BasicBlock
*&SplitExitBB
= SplitExitBBMap
[ExitBB
];
916 // If this is the first time we see this, do the split and remember it.
917 SplitExitBB
= SplitBlock(ExitBB
, ExitBB
->begin(), &DT
, &LI
, MSSAU
);
918 rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB
, *SplitExitBB
,
920 /*FullUnswitch*/ true);
922 // Update the case pair to point to the split block.
923 std::get
<1>(ExitCase
) = SplitExitBB
;
926 // Now add the unswitched cases. We do this in reverse order as we built them
928 for (auto &ExitCase
: reverse(ExitCases
)) {
929 ConstantInt
*CaseVal
= std::get
<0>(ExitCase
);
930 BasicBlock
*UnswitchedBB
= std::get
<1>(ExitCase
);
932 NewSIW
.addCase(CaseVal
, UnswitchedBB
, std::get
<2>(ExitCase
));
935 // If the default was unswitched, re-point it and add explicit cases for
936 // entering the loop.
938 NewSIW
->setDefaultDest(DefaultExitBB
);
939 NewSIW
.setSuccessorWeight(0, DefaultCaseWeight
);
941 // We removed all the exit cases, so we just copy the cases to the
942 // unswitched switch.
943 for (const auto &Case
: SI
.cases())
944 NewSIW
.addCase(Case
.getCaseValue(), NewPH
,
945 SIW
.getSuccessorWeight(Case
.getSuccessorIndex()));
946 } else if (DefaultCaseWeight
) {
947 // We have to set branch weight of the default case.
948 uint64_t SW
= *DefaultCaseWeight
;
949 for (const auto &Case
: SI
.cases()) {
950 auto W
= SIW
.getSuccessorWeight(Case
.getSuccessorIndex());
952 "case weight must be defined as default case weight is defined");
955 NewSIW
.setSuccessorWeight(0, SW
);
958 // If we ended up with a common successor for every path through the switch
959 // after unswitching, rewrite it to an unconditional branch to make it easy
960 // to recognize. Otherwise we potentially have to recognize the default case
961 // pointing at unreachable and other complexity.
963 BasicBlock
*BB
= SI
.getParent();
964 // We may have had multiple edges to this common successor block, so remove
965 // them as predecessors. We skip the first one, either the default or the
966 // actual first case.
967 bool SkippedFirst
= DefaultExitBB
== nullptr;
968 for (auto Case
: SI
.cases()) {
969 assert(Case
.getCaseSuccessor() == CommonSuccBB
&&
970 "Non-common successor!");
976 CommonSuccBB
->removePredecessor(BB
,
977 /*KeepOneInputPHIs*/ true);
979 // Now nuke the switch and replace it with a direct branch.
980 Instruction
*NewBI
= BranchInst::Create(CommonSuccBB
, BB
);
981 NewBI
->setDebugLoc(SIW
->getDebugLoc());
982 SIW
.eraseFromParent();
983 } else if (DefaultExitBB
) {
984 assert(SI
.getNumCases() > 0 &&
985 "If we had no cases we'd have a common successor!");
986 // Move the last case to the default successor. This is valid as if the
987 // default got unswitched it cannot be reached. This has the advantage of
988 // being simple and keeping the number of edges from this switch to
989 // successors the same, and avoiding any PHI update complexity.
990 auto LastCaseI
= std::prev(SI
.case_end());
992 SI
.setDefaultDest(LastCaseI
->getCaseSuccessor());
993 SIW
.setSuccessorWeight(
994 0, SIW
.getSuccessorWeight(LastCaseI
->getSuccessorIndex()));
995 SIW
.removeCase(LastCaseI
);
998 // Walk the unswitched exit blocks and the unswitched split blocks and update
999 // the dominator tree based on the CFG edits. While we are walking unordered
1000 // containers here, the API for applyUpdates takes an unordered list of
1001 // updates and requires them to not contain duplicates.
1002 SmallVector
<DominatorTree::UpdateType
, 4> DTUpdates
;
1003 for (auto *UnswitchedExitBB
: UnswitchedExitBBs
) {
1004 DTUpdates
.push_back({DT
.Delete
, ParentBB
, UnswitchedExitBB
});
1005 DTUpdates
.push_back({DT
.Insert
, OldPH
, UnswitchedExitBB
});
1007 for (auto SplitUnswitchedPair
: SplitExitBBMap
) {
1008 DTUpdates
.push_back({DT
.Delete
, ParentBB
, SplitUnswitchedPair
.first
});
1009 DTUpdates
.push_back({DT
.Insert
, OldPH
, SplitUnswitchedPair
.second
});
1013 MSSAU
->applyUpdates(DTUpdates
, DT
, /*UpdateDT=*/true);
1014 if (VerifyMemorySSA
)
1015 MSSAU
->getMemorySSA()->verifyMemorySSA();
1017 DT
.applyUpdates(DTUpdates
);
1020 assert(DT
.verify(DominatorTree::VerificationLevel::Fast
));
1022 // We may have changed the nesting relationship for this loop so hoist it to
1023 // its correct parent if needed.
1024 hoistLoopToNewParent(L
, *NewPH
, DT
, LI
, MSSAU
, SE
);
1026 if (MSSAU
&& VerifyMemorySSA
)
1027 MSSAU
->getMemorySSA()->verifyMemorySSA();
1031 LLVM_DEBUG(dbgs() << " done: unswitching trivial switch...\n");
1035 /// This routine scans the loop to find a branch or switch which occurs before
1036 /// any side effects occur. These can potentially be unswitched without
1037 /// duplicating the loop. If a branch or switch is successfully unswitched the
1038 /// scanning continues to see if subsequent branches or switches have become
1039 /// trivial. Once all trivial candidates have been unswitched, this routine
1042 /// The return value indicates whether anything was unswitched (and therefore
1045 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
1046 /// invalidated by this.
1047 static bool unswitchAllTrivialConditions(Loop
&L
, DominatorTree
&DT
,
1048 LoopInfo
&LI
, ScalarEvolution
*SE
,
1049 MemorySSAUpdater
*MSSAU
) {
1050 bool Changed
= false;
1052 // If loop header has only one reachable successor we should keep looking for
1053 // trivial condition candidates in the successor as well. An alternative is
1054 // to constant fold conditions and merge successors into loop header (then we
1055 // only need to check header's terminator). The reason for not doing this in
1056 // LoopUnswitch pass is that it could potentially break LoopPassManager's
1057 // invariants. Folding dead branches could either eliminate the current loop
1058 // or make other loops unreachable. LCSSA form might also not be preserved
1059 // after deleting branches. The following code keeps traversing loop header's
1060 // successors until it finds the trivial condition candidate (condition that
1061 // is not a constant). Since unswitching generates branches with constant
1062 // conditions, this scenario could be very common in practice.
1063 BasicBlock
*CurrentBB
= L
.getHeader();
1064 SmallPtrSet
<BasicBlock
*, 8> Visited
;
1065 Visited
.insert(CurrentBB
);
1067 // Check if there are any side-effecting instructions (e.g. stores, calls,
1068 // volatile loads) in the part of the loop that the code *would* execute
1069 // without unswitching.
1070 if (MSSAU
) // Possible early exit with MSSA
1071 if (auto *Defs
= MSSAU
->getMemorySSA()->getBlockDefs(CurrentBB
))
1072 if (!isa
<MemoryPhi
>(*Defs
->begin()) || (++Defs
->begin() != Defs
->end()))
1074 if (llvm::any_of(*CurrentBB
,
1075 [](Instruction
&I
) { return I
.mayHaveSideEffects(); }))
1078 Instruction
*CurrentTerm
= CurrentBB
->getTerminator();
1080 if (auto *SI
= dyn_cast
<SwitchInst
>(CurrentTerm
)) {
1081 // Don't bother trying to unswitch past a switch with a constant
1082 // condition. This should be removed prior to running this pass by
1084 if (isa
<Constant
>(SI
->getCondition()))
1087 if (!unswitchTrivialSwitch(L
, *SI
, DT
, LI
, SE
, MSSAU
))
1088 // Couldn't unswitch this one so we're done.
1091 // Mark that we managed to unswitch something.
1094 // If unswitching turned the terminator into an unconditional branch then
1095 // we can continue. The unswitching logic specifically works to fold any
1096 // cases it can into an unconditional branch to make it easier to
1098 auto *BI
= dyn_cast
<BranchInst
>(CurrentBB
->getTerminator());
1099 if (!BI
|| BI
->isConditional())
1102 CurrentBB
= BI
->getSuccessor(0);
1106 auto *BI
= dyn_cast
<BranchInst
>(CurrentTerm
);
1108 // We do not understand other terminator instructions.
1111 // Don't bother trying to unswitch past an unconditional branch or a branch
1112 // with a constant value. These should be removed by simplifycfg prior to
1113 // running this pass.
1114 if (!BI
->isConditional() ||
1115 isa
<Constant
>(skipTrivialSelect(BI
->getCondition())))
1118 // Found a trivial condition candidate: non-foldable conditional branch. If
1119 // we fail to unswitch this, we can't do anything else that is trivial.
1120 if (!unswitchTrivialBranch(L
, *BI
, DT
, LI
, SE
, MSSAU
))
1123 // Mark that we managed to unswitch something.
1126 // If we only unswitched some of the conditions feeding the branch, we won't
1127 // have collapsed it to a single successor.
1128 BI
= cast
<BranchInst
>(CurrentBB
->getTerminator());
1129 if (BI
->isConditional())
1132 // Follow the newly unconditional branch into its successor.
1133 CurrentBB
= BI
->getSuccessor(0);
1135 // When continuing, if we exit the loop or reach a previous visited block,
1136 // then we can not reach any trivial condition candidates (unfoldable
1137 // branch instructions or switch instructions) and no unswitch can happen.
1138 } while (L
.contains(CurrentBB
) && Visited
.insert(CurrentBB
).second
);
1143 /// Build the cloned blocks for an unswitched copy of the given loop.
1145 /// The cloned blocks are inserted before the loop preheader (`LoopPH`) and
1146 /// after the split block (`SplitBB`) that will be used to select between the
1147 /// cloned and original loop.
1149 /// This routine handles cloning all of the necessary loop blocks and exit
1150 /// blocks including rewriting their instructions and the relevant PHI nodes.
1151 /// Any loop blocks or exit blocks which are dominated by a different successor
1152 /// than the one for this clone of the loop blocks can be trivially skipped. We
1153 /// use the `DominatingSucc` map to determine whether a block satisfies that
1154 /// property with a simple map lookup.
1156 /// It also correctly creates the unconditional branch in the cloned
1157 /// unswitched parent block to only point at the unswitched successor.
1159 /// This does not handle most of the necessary updates to `LoopInfo`. Only exit
1160 /// block splitting is correctly reflected in `LoopInfo`, essentially all of
1161 /// the cloned blocks (and their loops) are left without full `LoopInfo`
1162 /// updates. This also doesn't fully update `DominatorTree`. It adds the cloned
1163 /// blocks to them but doesn't create the cloned `DominatorTree` structure and
1164 /// instead the caller must recompute an accurate DT. It *does* correctly
1165 /// update the `AssumptionCache` provided in `AC`.
1166 static BasicBlock
*buildClonedLoopBlocks(
1167 Loop
&L
, BasicBlock
*LoopPH
, BasicBlock
*SplitBB
,
1168 ArrayRef
<BasicBlock
*> ExitBlocks
, BasicBlock
*ParentBB
,
1169 BasicBlock
*UnswitchedSuccBB
, BasicBlock
*ContinueSuccBB
,
1170 const SmallDenseMap
<BasicBlock
*, BasicBlock
*, 16> &DominatingSucc
,
1171 ValueToValueMapTy
&VMap
,
1172 SmallVectorImpl
<DominatorTree::UpdateType
> &DTUpdates
, AssumptionCache
&AC
,
1173 DominatorTree
&DT
, LoopInfo
&LI
, MemorySSAUpdater
*MSSAU
,
1174 ScalarEvolution
*SE
) {
1175 SmallVector
<BasicBlock
*, 4> NewBlocks
;
1176 NewBlocks
.reserve(L
.getNumBlocks() + ExitBlocks
.size());
1178 // We will need to clone a bunch of blocks, wrap up the clone operation in
1180 auto CloneBlock
= [&](BasicBlock
*OldBB
) {
1181 // Clone the basic block and insert it before the new preheader.
1182 BasicBlock
*NewBB
= CloneBasicBlock(OldBB
, VMap
, ".us", OldBB
->getParent());
1183 NewBB
->moveBefore(LoopPH
);
1185 // Record this block and the mapping.
1186 NewBlocks
.push_back(NewBB
);
1187 VMap
[OldBB
] = NewBB
;
1192 // We skip cloning blocks when they have a dominating succ that is not the
1193 // succ we are cloning for.
1194 auto SkipBlock
= [&](BasicBlock
*BB
) {
1195 auto It
= DominatingSucc
.find(BB
);
1196 return It
!= DominatingSucc
.end() && It
->second
!= UnswitchedSuccBB
;
1199 // First, clone the preheader.
1200 auto *ClonedPH
= CloneBlock(LoopPH
);
1202 // Then clone all the loop blocks, skipping the ones that aren't necessary.
1203 for (auto *LoopBB
: L
.blocks())
1204 if (!SkipBlock(LoopBB
))
1207 // Split all the loop exit edges so that when we clone the exit blocks, if
1208 // any of the exit blocks are *also* a preheader for some other loop, we
1209 // don't create multiple predecessors entering the loop header.
1210 for (auto *ExitBB
: ExitBlocks
) {
1211 if (SkipBlock(ExitBB
))
1214 // When we are going to clone an exit, we don't need to clone all the
1215 // instructions in the exit block and we want to ensure we have an easy
1216 // place to merge the CFG, so split the exit first. This is always safe to
1217 // do because there cannot be any non-loop predecessors of a loop exit in
1218 // loop simplified form.
1219 auto *MergeBB
= SplitBlock(ExitBB
, ExitBB
->begin(), &DT
, &LI
, MSSAU
);
1221 // Rearrange the names to make it easier to write test cases by having the
1222 // exit block carry the suffix rather than the merge block carrying the
1224 MergeBB
->takeName(ExitBB
);
1225 ExitBB
->setName(Twine(MergeBB
->getName()) + ".split");
1227 // Now clone the original exit block.
1228 auto *ClonedExitBB
= CloneBlock(ExitBB
);
1229 assert(ClonedExitBB
->getTerminator()->getNumSuccessors() == 1 &&
1230 "Exit block should have been split to have one successor!");
1231 assert(ClonedExitBB
->getTerminator()->getSuccessor(0) == MergeBB
&&
1232 "Cloned exit block has the wrong successor!");
1234 // Remap any cloned instructions and create a merge phi node for them.
1235 for (auto ZippedInsts
: llvm::zip_first(
1236 llvm::make_range(ExitBB
->begin(), std::prev(ExitBB
->end())),
1237 llvm::make_range(ClonedExitBB
->begin(),
1238 std::prev(ClonedExitBB
->end())))) {
1239 Instruction
&I
= std::get
<0>(ZippedInsts
);
1240 Instruction
&ClonedI
= std::get
<1>(ZippedInsts
);
1242 // The only instructions in the exit block should be PHI nodes and
1243 // potentially a landing pad.
1245 (isa
<PHINode
>(I
) || isa
<LandingPadInst
>(I
) || isa
<CatchPadInst
>(I
)) &&
1246 "Bad instruction in exit block!");
1247 // We should have a value map between the instruction and its clone.
1248 assert(VMap
.lookup(&I
) == &ClonedI
&& "Mismatch in the value map!");
1250 // Forget SCEVs based on exit phis in case SCEV looked through the phi.
1252 if (auto *PN
= dyn_cast
<PHINode
>(&I
))
1253 SE
->forgetLcssaPhiWithNewPredecessor(&L
, PN
);
1255 BasicBlock::iterator InsertPt
= MergeBB
->getFirstInsertionPt();
1258 PHINode::Create(I
.getType(), /*NumReservedValues*/ 2, ".us-phi");
1259 MergePN
->insertBefore(InsertPt
);
1260 MergePN
->setDebugLoc(InsertPt
->getDebugLoc());
1261 I
.replaceAllUsesWith(MergePN
);
1262 MergePN
->addIncoming(&I
, ExitBB
);
1263 MergePN
->addIncoming(&ClonedI
, ClonedExitBB
);
1267 // Rewrite the instructions in the cloned blocks to refer to the instructions
1268 // in the cloned blocks. We have to do this as a second pass so that we have
1269 // everything available. Also, we have inserted new instructions which may
1270 // include assume intrinsics, so we update the assumption cache while
1272 Module
*M
= ClonedPH
->getParent()->getParent();
1273 for (auto *ClonedBB
: NewBlocks
)
1274 for (Instruction
&I
: *ClonedBB
) {
1275 RemapDbgRecordRange(M
, I
.getDbgRecordRange(), VMap
,
1276 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
1277 RemapInstruction(&I
, VMap
,
1278 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
1279 if (auto *II
= dyn_cast
<AssumeInst
>(&I
))
1280 AC
.registerAssumption(II
);
1283 // Update any PHI nodes in the cloned successors of the skipped blocks to not
1284 // have spurious incoming values.
1285 for (auto *LoopBB
: L
.blocks())
1286 if (SkipBlock(LoopBB
))
1287 for (auto *SuccBB
: successors(LoopBB
))
1288 if (auto *ClonedSuccBB
= cast_or_null
<BasicBlock
>(VMap
.lookup(SuccBB
)))
1289 for (PHINode
&PN
: ClonedSuccBB
->phis())
1290 PN
.removeIncomingValue(LoopBB
, /*DeletePHIIfEmpty*/ false);
1292 // Remove the cloned parent as a predecessor of any successor we ended up
1293 // cloning other than the unswitched one.
1294 auto *ClonedParentBB
= cast
<BasicBlock
>(VMap
.lookup(ParentBB
));
1295 for (auto *SuccBB
: successors(ParentBB
)) {
1296 if (SuccBB
== UnswitchedSuccBB
)
1299 auto *ClonedSuccBB
= cast_or_null
<BasicBlock
>(VMap
.lookup(SuccBB
));
1303 ClonedSuccBB
->removePredecessor(ClonedParentBB
,
1304 /*KeepOneInputPHIs*/ true);
1307 // Replace the cloned branch with an unconditional branch to the cloned
1308 // unswitched successor.
1309 auto *ClonedSuccBB
= cast
<BasicBlock
>(VMap
.lookup(UnswitchedSuccBB
));
1310 Instruction
*ClonedTerminator
= ClonedParentBB
->getTerminator();
1311 // Trivial Simplification. If Terminator is a conditional branch and
1312 // condition becomes dead - erase it.
1313 Value
*ClonedConditionToErase
= nullptr;
1314 if (auto *BI
= dyn_cast
<BranchInst
>(ClonedTerminator
))
1315 ClonedConditionToErase
= BI
->getCondition();
1316 else if (auto *SI
= dyn_cast
<SwitchInst
>(ClonedTerminator
))
1317 ClonedConditionToErase
= SI
->getCondition();
1319 Instruction
*BI
= BranchInst::Create(ClonedSuccBB
, ClonedParentBB
);
1320 BI
->setDebugLoc(ClonedTerminator
->getDebugLoc());
1321 ClonedTerminator
->eraseFromParent();
1323 if (ClonedConditionToErase
)
1324 RecursivelyDeleteTriviallyDeadInstructions(ClonedConditionToErase
, nullptr,
1327 // If there are duplicate entries in the PHI nodes because of multiple edges
1328 // to the unswitched successor, we need to nuke all but one as we replaced it
1329 // with a direct branch.
1330 for (PHINode
&PN
: ClonedSuccBB
->phis()) {
1332 // Loop over the incoming operands backwards so we can easily delete as we
1333 // go without invalidating the index.
1334 for (int i
= PN
.getNumOperands() - 1; i
>= 0; --i
) {
1335 if (PN
.getIncomingBlock(i
) != ClonedParentBB
)
1341 PN
.removeIncomingValue(i
, /*DeletePHIIfEmpty*/ false);
1345 // Record the domtree updates for the new blocks.
1346 SmallPtrSet
<BasicBlock
*, 4> SuccSet
;
1347 for (auto *ClonedBB
: NewBlocks
) {
1348 for (auto *SuccBB
: successors(ClonedBB
))
1349 if (SuccSet
.insert(SuccBB
).second
)
1350 DTUpdates
.push_back({DominatorTree::Insert
, ClonedBB
, SuccBB
});
1357 /// Recursively clone the specified loop and all of its children.
1359 /// The target parent loop for the clone should be provided, or can be null if
1360 /// the clone is a top-level loop. While cloning, all the blocks are mapped
1361 /// with the provided value map. The entire original loop must be present in
1362 /// the value map. The cloned loop is returned.
1363 static Loop
*cloneLoopNest(Loop
&OrigRootL
, Loop
*RootParentL
,
1364 const ValueToValueMapTy
&VMap
, LoopInfo
&LI
) {
1365 auto AddClonedBlocksToLoop
= [&](Loop
&OrigL
, Loop
&ClonedL
) {
1366 assert(ClonedL
.getBlocks().empty() && "Must start with an empty loop!");
1367 ClonedL
.reserveBlocks(OrigL
.getNumBlocks());
1368 for (auto *BB
: OrigL
.blocks()) {
1369 auto *ClonedBB
= cast
<BasicBlock
>(VMap
.lookup(BB
));
1370 ClonedL
.addBlockEntry(ClonedBB
);
1371 if (LI
.getLoopFor(BB
) == &OrigL
)
1372 LI
.changeLoopFor(ClonedBB
, &ClonedL
);
1376 // We specially handle the first loop because it may get cloned into
1377 // a different parent and because we most commonly are cloning leaf loops.
1378 Loop
*ClonedRootL
= LI
.AllocateLoop();
1380 RootParentL
->addChildLoop(ClonedRootL
);
1382 LI
.addTopLevelLoop(ClonedRootL
);
1383 AddClonedBlocksToLoop(OrigRootL
, *ClonedRootL
);
1385 if (OrigRootL
.isInnermost())
1388 // If we have a nest, we can quickly clone the entire loop nest using an
1389 // iterative approach because it is a tree. We keep the cloned parent in the
1390 // data structure to avoid repeatedly querying through a map to find it.
1391 SmallVector
<std::pair
<Loop
*, Loop
*>, 16> LoopsToClone
;
1392 // Build up the loops to clone in reverse order as we'll clone them from the
1394 for (Loop
*ChildL
: llvm::reverse(OrigRootL
))
1395 LoopsToClone
.push_back({ClonedRootL
, ChildL
});
1397 Loop
*ClonedParentL
, *L
;
1398 std::tie(ClonedParentL
, L
) = LoopsToClone
.pop_back_val();
1399 Loop
*ClonedL
= LI
.AllocateLoop();
1400 ClonedParentL
->addChildLoop(ClonedL
);
1401 AddClonedBlocksToLoop(*L
, *ClonedL
);
1402 for (Loop
*ChildL
: llvm::reverse(*L
))
1403 LoopsToClone
.push_back({ClonedL
, ChildL
});
1404 } while (!LoopsToClone
.empty());
1409 /// Build the cloned loops of an original loop from unswitching.
1411 /// Because unswitching simplifies the CFG of the loop, this isn't a trivial
1412 /// operation. We need to re-verify that there even is a loop (as the backedge
1413 /// may not have been cloned), and even if there are remaining backedges the
1414 /// backedge set may be different. However, we know that each child loop is
1415 /// undisturbed, we only need to find where to place each child loop within
1416 /// either any parent loop or within a cloned version of the original loop.
1418 /// Because child loops may end up cloned outside of any cloned version of the
1419 /// original loop, multiple cloned sibling loops may be created. All of them
1420 /// are returned so that the newly introduced loop nest roots can be
1422 static void buildClonedLoops(Loop
&OrigL
, ArrayRef
<BasicBlock
*> ExitBlocks
,
1423 const ValueToValueMapTy
&VMap
, LoopInfo
&LI
,
1424 SmallVectorImpl
<Loop
*> &NonChildClonedLoops
) {
1425 Loop
*ClonedL
= nullptr;
1427 auto *OrigPH
= OrigL
.getLoopPreheader();
1428 auto *OrigHeader
= OrigL
.getHeader();
1430 auto *ClonedPH
= cast
<BasicBlock
>(VMap
.lookup(OrigPH
));
1431 auto *ClonedHeader
= cast
<BasicBlock
>(VMap
.lookup(OrigHeader
));
1433 // We need to know the loops of the cloned exit blocks to even compute the
1434 // accurate parent loop. If we only clone exits to some parent of the
1435 // original parent, we want to clone into that outer loop. We also keep track
1436 // of the loops that our cloned exit blocks participate in.
1437 Loop
*ParentL
= nullptr;
1438 SmallVector
<BasicBlock
*, 4> ClonedExitsInLoops
;
1439 SmallDenseMap
<BasicBlock
*, Loop
*, 16> ExitLoopMap
;
1440 ClonedExitsInLoops
.reserve(ExitBlocks
.size());
1441 for (auto *ExitBB
: ExitBlocks
)
1442 if (auto *ClonedExitBB
= cast_or_null
<BasicBlock
>(VMap
.lookup(ExitBB
)))
1443 if (Loop
*ExitL
= LI
.getLoopFor(ExitBB
)) {
1444 ExitLoopMap
[ClonedExitBB
] = ExitL
;
1445 ClonedExitsInLoops
.push_back(ClonedExitBB
);
1446 if (!ParentL
|| (ParentL
!= ExitL
&& ParentL
->contains(ExitL
)))
1449 assert((!ParentL
|| ParentL
== OrigL
.getParentLoop() ||
1450 ParentL
->contains(OrigL
.getParentLoop())) &&
1451 "The computed parent loop should always contain (or be) the parent of "
1452 "the original loop.");
1454 // We build the set of blocks dominated by the cloned header from the set of
1455 // cloned blocks out of the original loop. While not all of these will
1456 // necessarily be in the cloned loop, it is enough to establish that they
1457 // aren't in unreachable cycles, etc.
1458 SmallSetVector
<BasicBlock
*, 16> ClonedLoopBlocks
;
1459 for (auto *BB
: OrigL
.blocks())
1460 if (auto *ClonedBB
= cast_or_null
<BasicBlock
>(VMap
.lookup(BB
)))
1461 ClonedLoopBlocks
.insert(ClonedBB
);
1463 // Rebuild the set of blocks that will end up in the cloned loop. We may have
1464 // skipped cloning some region of this loop which can in turn skip some of
1465 // the backedges so we have to rebuild the blocks in the loop based on the
1466 // backedges that remain after cloning.
1467 SmallVector
<BasicBlock
*, 16> Worklist
;
1468 SmallPtrSet
<BasicBlock
*, 16> BlocksInClonedLoop
;
1469 for (auto *Pred
: predecessors(ClonedHeader
)) {
1470 // The only possible non-loop header predecessor is the preheader because
1471 // we know we cloned the loop in simplified form.
1472 if (Pred
== ClonedPH
)
1475 // Because the loop was in simplified form, the only non-loop predecessor
1476 // should be the preheader.
1477 assert(ClonedLoopBlocks
.count(Pred
) && "Found a predecessor of the loop "
1478 "header other than the preheader "
1479 "that is not part of the loop!");
1481 // Insert this block into the loop set and on the first visit (and if it
1482 // isn't the header we're currently walking) put it into the worklist to
1484 if (BlocksInClonedLoop
.insert(Pred
).second
&& Pred
!= ClonedHeader
)
1485 Worklist
.push_back(Pred
);
1488 // If we had any backedges then there *is* a cloned loop. Put the header into
1489 // the loop set and then walk the worklist backwards to find all the blocks
1490 // that remain within the loop after cloning.
1491 if (!BlocksInClonedLoop
.empty()) {
1492 BlocksInClonedLoop
.insert(ClonedHeader
);
1494 while (!Worklist
.empty()) {
1495 BasicBlock
*BB
= Worklist
.pop_back_val();
1496 assert(BlocksInClonedLoop
.count(BB
) &&
1497 "Didn't put block into the loop set!");
1499 // Insert any predecessors that are in the possible set into the cloned
1500 // set, and if the insert is successful, add them to the worklist. Note
1501 // that we filter on the blocks that are definitely reachable via the
1502 // backedge to the loop header so we may prune out dead code within the
1504 for (auto *Pred
: predecessors(BB
))
1505 if (ClonedLoopBlocks
.count(Pred
) &&
1506 BlocksInClonedLoop
.insert(Pred
).second
)
1507 Worklist
.push_back(Pred
);
1510 ClonedL
= LI
.AllocateLoop();
1512 ParentL
->addBasicBlockToLoop(ClonedPH
, LI
);
1513 ParentL
->addChildLoop(ClonedL
);
1515 LI
.addTopLevelLoop(ClonedL
);
1517 NonChildClonedLoops
.push_back(ClonedL
);
1519 ClonedL
->reserveBlocks(BlocksInClonedLoop
.size());
1520 // We don't want to just add the cloned loop blocks based on how we
1521 // discovered them. The original order of blocks was carefully built in
1522 // a way that doesn't rely on predecessor ordering. Rather than re-invent
1523 // that logic, we just re-walk the original blocks (and those of the child
1524 // loops) and filter them as we add them into the cloned loop.
1525 for (auto *BB
: OrigL
.blocks()) {
1526 auto *ClonedBB
= cast_or_null
<BasicBlock
>(VMap
.lookup(BB
));
1527 if (!ClonedBB
|| !BlocksInClonedLoop
.count(ClonedBB
))
1530 // Directly add the blocks that are only in this loop.
1531 if (LI
.getLoopFor(BB
) == &OrigL
) {
1532 ClonedL
->addBasicBlockToLoop(ClonedBB
, LI
);
1536 // We want to manually add it to this loop and parents.
1537 // Registering it with LoopInfo will happen when we clone the top
1538 // loop for this block.
1539 for (Loop
*PL
= ClonedL
; PL
; PL
= PL
->getParentLoop())
1540 PL
->addBlockEntry(ClonedBB
);
1543 // Now add each child loop whose header remains within the cloned loop. All
1544 // of the blocks within the loop must satisfy the same constraints as the
1545 // header so once we pass the header checks we can just clone the entire
1547 for (Loop
*ChildL
: OrigL
) {
1548 auto *ClonedChildHeader
=
1549 cast_or_null
<BasicBlock
>(VMap
.lookup(ChildL
->getHeader()));
1550 if (!ClonedChildHeader
|| !BlocksInClonedLoop
.count(ClonedChildHeader
))
1554 // We should never have a cloned child loop header but fail to have
1555 // all of the blocks for that child loop.
1556 for (auto *ChildLoopBB
: ChildL
->blocks())
1557 assert(BlocksInClonedLoop
.count(
1558 cast
<BasicBlock
>(VMap
.lookup(ChildLoopBB
))) &&
1559 "Child cloned loop has a header within the cloned outer "
1560 "loop but not all of its blocks!");
1563 cloneLoopNest(*ChildL
, ClonedL
, VMap
, LI
);
1567 // Now that we've handled all the components of the original loop that were
1568 // cloned into a new loop, we still need to handle anything from the original
1569 // loop that wasn't in a cloned loop.
1571 // Figure out what blocks are left to place within any loop nest containing
1572 // the unswitched loop. If we never formed a loop, the cloned PH is one of
1574 SmallPtrSet
<BasicBlock
*, 16> UnloopedBlockSet
;
1575 if (BlocksInClonedLoop
.empty())
1576 UnloopedBlockSet
.insert(ClonedPH
);
1577 for (auto *ClonedBB
: ClonedLoopBlocks
)
1578 if (!BlocksInClonedLoop
.count(ClonedBB
))
1579 UnloopedBlockSet
.insert(ClonedBB
);
1581 // Copy the cloned exits and sort them in ascending loop depth, we'll work
1582 // backwards across these to process them inside out. The order shouldn't
1583 // matter as we're just trying to build up the map from inside-out; we use
1584 // the map in a more stably ordered way below.
1585 auto OrderedClonedExitsInLoops
= ClonedExitsInLoops
;
1586 llvm::sort(OrderedClonedExitsInLoops
, [&](BasicBlock
*LHS
, BasicBlock
*RHS
) {
1587 return ExitLoopMap
.lookup(LHS
)->getLoopDepth() <
1588 ExitLoopMap
.lookup(RHS
)->getLoopDepth();
1591 // Populate the existing ExitLoopMap with everything reachable from each
1592 // exit, starting from the inner most exit.
1593 while (!UnloopedBlockSet
.empty() && !OrderedClonedExitsInLoops
.empty()) {
1594 assert(Worklist
.empty() && "Didn't clear worklist!");
1596 BasicBlock
*ExitBB
= OrderedClonedExitsInLoops
.pop_back_val();
1597 Loop
*ExitL
= ExitLoopMap
.lookup(ExitBB
);
1599 // Walk the CFG back until we hit the cloned PH adding everything reachable
1600 // and in the unlooped set to this exit block's loop.
1601 Worklist
.push_back(ExitBB
);
1603 BasicBlock
*BB
= Worklist
.pop_back_val();
1604 // We can stop recursing at the cloned preheader (if we get there).
1608 for (BasicBlock
*PredBB
: predecessors(BB
)) {
1609 // If this pred has already been moved to our set or is part of some
1610 // (inner) loop, no update needed.
1611 if (!UnloopedBlockSet
.erase(PredBB
)) {
1613 (BlocksInClonedLoop
.count(PredBB
) || ExitLoopMap
.count(PredBB
)) &&
1614 "Predecessor not mapped to a loop!");
1618 // We just insert into the loop set here. We'll add these blocks to the
1619 // exit loop after we build up the set in an order that doesn't rely on
1620 // predecessor order (which in turn relies on use list order).
1621 bool Inserted
= ExitLoopMap
.insert({PredBB
, ExitL
}).second
;
1623 assert(Inserted
&& "Should only visit an unlooped block once!");
1625 // And recurse through to its predecessors.
1626 Worklist
.push_back(PredBB
);
1628 } while (!Worklist
.empty());
1631 // Now that the ExitLoopMap gives as mapping for all the non-looping cloned
1632 // blocks to their outer loops, walk the cloned blocks and the cloned exits
1633 // in their original order adding them to the correct loop.
1635 // We need a stable insertion order. We use the order of the original loop
1636 // order and map into the correct parent loop.
1637 for (auto *BB
: llvm::concat
<BasicBlock
*const>(
1638 ArrayRef(ClonedPH
), ClonedLoopBlocks
, ClonedExitsInLoops
))
1639 if (Loop
*OuterL
= ExitLoopMap
.lookup(BB
))
1640 OuterL
->addBasicBlockToLoop(BB
, LI
);
1643 for (auto &BBAndL
: ExitLoopMap
) {
1644 auto *BB
= BBAndL
.first
;
1645 auto *OuterL
= BBAndL
.second
;
1646 assert(LI
.getLoopFor(BB
) == OuterL
&&
1647 "Failed to put all blocks into outer loops!");
1651 // Now that all the blocks are placed into the correct containing loop in the
1652 // absence of child loops, find all the potentially cloned child loops and
1653 // clone them into whatever outer loop we placed their header into.
1654 for (Loop
*ChildL
: OrigL
) {
1655 auto *ClonedChildHeader
=
1656 cast_or_null
<BasicBlock
>(VMap
.lookup(ChildL
->getHeader()));
1657 if (!ClonedChildHeader
|| BlocksInClonedLoop
.count(ClonedChildHeader
))
1661 for (auto *ChildLoopBB
: ChildL
->blocks())
1662 assert(VMap
.count(ChildLoopBB
) &&
1663 "Cloned a child loop header but not all of that loops blocks!");
1666 NonChildClonedLoops
.push_back(cloneLoopNest(
1667 *ChildL
, ExitLoopMap
.lookup(ClonedChildHeader
), VMap
, LI
));
1672 deleteDeadClonedBlocks(Loop
&L
, ArrayRef
<BasicBlock
*> ExitBlocks
,
1673 ArrayRef
<std::unique_ptr
<ValueToValueMapTy
>> VMaps
,
1674 DominatorTree
&DT
, MemorySSAUpdater
*MSSAU
) {
1675 // Find all the dead clones, and remove them from their successors.
1676 SmallVector
<BasicBlock
*, 16> DeadBlocks
;
1677 for (BasicBlock
*BB
: llvm::concat
<BasicBlock
*const>(L
.blocks(), ExitBlocks
))
1678 for (const auto &VMap
: VMaps
)
1679 if (BasicBlock
*ClonedBB
= cast_or_null
<BasicBlock
>(VMap
->lookup(BB
)))
1680 if (!DT
.isReachableFromEntry(ClonedBB
)) {
1681 for (BasicBlock
*SuccBB
: successors(ClonedBB
))
1682 SuccBB
->removePredecessor(ClonedBB
);
1683 DeadBlocks
.push_back(ClonedBB
);
1686 // Remove all MemorySSA in the dead blocks
1688 SmallSetVector
<BasicBlock
*, 8> DeadBlockSet(DeadBlocks
.begin(),
1690 MSSAU
->removeBlocks(DeadBlockSet
);
1693 // Drop any remaining references to break cycles.
1694 for (BasicBlock
*BB
: DeadBlocks
)
1695 BB
->dropAllReferences();
1696 // Erase them from the IR.
1697 for (BasicBlock
*BB
: DeadBlocks
)
1698 BB
->eraseFromParent();
1701 static void deleteDeadBlocksFromLoop(Loop
&L
,
1702 SmallVectorImpl
<BasicBlock
*> &ExitBlocks
,
1703 DominatorTree
&DT
, LoopInfo
&LI
,
1704 MemorySSAUpdater
*MSSAU
,
1705 ScalarEvolution
*SE
,
1706 LPMUpdater
&LoopUpdater
) {
1707 // Find all the dead blocks tied to this loop, and remove them from their
1709 SmallSetVector
<BasicBlock
*, 8> DeadBlockSet
;
1711 // Start with loop/exit blocks and get a transitive closure of reachable dead
1713 SmallVector
<BasicBlock
*, 16> DeathCandidates(ExitBlocks
.begin(),
1715 DeathCandidates
.append(L
.blocks().begin(), L
.blocks().end());
1716 while (!DeathCandidates
.empty()) {
1717 auto *BB
= DeathCandidates
.pop_back_val();
1718 if (!DeadBlockSet
.count(BB
) && !DT
.isReachableFromEntry(BB
)) {
1719 for (BasicBlock
*SuccBB
: successors(BB
)) {
1720 SuccBB
->removePredecessor(BB
);
1721 DeathCandidates
.push_back(SuccBB
);
1723 DeadBlockSet
.insert(BB
);
1727 // Remove all MemorySSA in the dead blocks
1729 MSSAU
->removeBlocks(DeadBlockSet
);
1731 // Filter out the dead blocks from the exit blocks list so that it can be
1732 // used in the caller.
1733 llvm::erase_if(ExitBlocks
,
1734 [&](BasicBlock
*BB
) { return DeadBlockSet
.count(BB
); });
1736 // Walk from this loop up through its parents removing all of the dead blocks.
1737 for (Loop
*ParentL
= &L
; ParentL
; ParentL
= ParentL
->getParentLoop()) {
1738 for (auto *BB
: DeadBlockSet
)
1739 ParentL
->getBlocksSet().erase(BB
);
1740 llvm::erase_if(ParentL
->getBlocksVector(),
1741 [&](BasicBlock
*BB
) { return DeadBlockSet
.count(BB
); });
1744 // Now delete the dead child loops. This raw delete will clear them
1746 llvm::erase_if(L
.getSubLoopsVector(), [&](Loop
*ChildL
) {
1747 if (!DeadBlockSet
.count(ChildL
->getHeader()))
1750 assert(llvm::all_of(ChildL
->blocks(),
1751 [&](BasicBlock
*ChildBB
) {
1752 return DeadBlockSet
.count(ChildBB
);
1754 "If the child loop header is dead all blocks in the child loop must "
1755 "be dead as well!");
1756 LoopUpdater
.markLoopAsDeleted(*ChildL
, ChildL
->getName());
1758 SE
->forgetBlockAndLoopDispositions();
1763 // Remove the loop mappings for the dead blocks and drop all the references
1764 // from these blocks to others to handle cyclic references as we start
1765 // deleting the blocks themselves.
1766 for (auto *BB
: DeadBlockSet
) {
1767 // Check that the dominator tree has already been updated.
1768 assert(!DT
.getNode(BB
) && "Should already have cleared domtree!");
1769 LI
.changeLoopFor(BB
, nullptr);
1770 // Drop all uses of the instructions to make sure we won't have dangling
1771 // uses in other blocks.
1774 I
.replaceAllUsesWith(PoisonValue::get(I
.getType()));
1775 BB
->dropAllReferences();
1778 // Actually delete the blocks now that they've been fully unhooked from the
1780 for (auto *BB
: DeadBlockSet
)
1781 BB
->eraseFromParent();
1784 /// Recompute the set of blocks in a loop after unswitching.
1786 /// This walks from the original headers predecessors to rebuild the loop. We
1787 /// take advantage of the fact that new blocks can't have been added, and so we
1788 /// filter by the original loop's blocks. This also handles potentially
1789 /// unreachable code that we don't want to explore but might be found examining
1790 /// the predecessors of the header.
1792 /// If the original loop is no longer a loop, this will return an empty set. If
1793 /// it remains a loop, all the blocks within it will be added to the set
1794 /// (including those blocks in inner loops).
1795 static SmallPtrSet
<const BasicBlock
*, 16> recomputeLoopBlockSet(Loop
&L
,
1797 SmallPtrSet
<const BasicBlock
*, 16> LoopBlockSet
;
1799 auto *PH
= L
.getLoopPreheader();
1800 auto *Header
= L
.getHeader();
1802 // A worklist to use while walking backwards from the header.
1803 SmallVector
<BasicBlock
*, 16> Worklist
;
1805 // First walk the predecessors of the header to find the backedges. This will
1806 // form the basis of our walk.
1807 for (auto *Pred
: predecessors(Header
)) {
1808 // Skip the preheader.
1812 // Because the loop was in simplified form, the only non-loop predecessor
1813 // is the preheader.
1814 assert(L
.contains(Pred
) && "Found a predecessor of the loop header other "
1815 "than the preheader that is not part of the "
1818 // Insert this block into the loop set and on the first visit and, if it
1819 // isn't the header we're currently walking, put it into the worklist to
1821 if (LoopBlockSet
.insert(Pred
).second
&& Pred
!= Header
)
1822 Worklist
.push_back(Pred
);
1825 // If no backedges were found, we're done.
1826 if (LoopBlockSet
.empty())
1827 return LoopBlockSet
;
1829 // We found backedges, recurse through them to identify the loop blocks.
1830 while (!Worklist
.empty()) {
1831 BasicBlock
*BB
= Worklist
.pop_back_val();
1832 assert(LoopBlockSet
.count(BB
) && "Didn't put block into the loop set!");
1834 // No need to walk past the header.
1838 // Because we know the inner loop structure remains valid we can use the
1839 // loop structure to jump immediately across the entire nested loop.
1840 // Further, because it is in loop simplified form, we can directly jump
1841 // to its preheader afterward.
1842 if (Loop
*InnerL
= LI
.getLoopFor(BB
))
1844 assert(L
.contains(InnerL
) &&
1845 "Should not reach a loop *outside* this loop!");
1846 // The preheader is the only possible predecessor of the loop so
1847 // insert it into the set and check whether it was already handled.
1848 auto *InnerPH
= InnerL
->getLoopPreheader();
1849 assert(L
.contains(InnerPH
) && "Cannot contain an inner loop block "
1850 "but not contain the inner loop "
1852 if (!LoopBlockSet
.insert(InnerPH
).second
)
1853 // The only way to reach the preheader is through the loop body
1854 // itself so if it has been visited the loop is already handled.
1857 // Insert all of the blocks (other than those already present) into
1858 // the loop set. We expect at least the block that led us to find the
1859 // inner loop to be in the block set, but we may also have other loop
1860 // blocks if they were already enqueued as predecessors of some other
1861 // outer loop block.
1862 for (auto *InnerBB
: InnerL
->blocks()) {
1863 if (InnerBB
== BB
) {
1864 assert(LoopBlockSet
.count(InnerBB
) &&
1865 "Block should already be in the set!");
1869 LoopBlockSet
.insert(InnerBB
);
1872 // Add the preheader to the worklist so we will continue past the
1874 Worklist
.push_back(InnerPH
);
1878 // Insert any predecessors that were in the original loop into the new
1879 // set, and if the insert is successful, add them to the worklist.
1880 for (auto *Pred
: predecessors(BB
))
1881 if (L
.contains(Pred
) && LoopBlockSet
.insert(Pred
).second
)
1882 Worklist
.push_back(Pred
);
1885 assert(LoopBlockSet
.count(Header
) && "Cannot fail to add the header!");
1887 // We've found all the blocks participating in the loop, return our completed
1889 return LoopBlockSet
;
1892 /// Rebuild a loop after unswitching removes some subset of blocks and edges.
1894 /// The removal may have removed some child loops entirely but cannot have
1895 /// disturbed any remaining child loops. However, they may need to be hoisted
1896 /// to the parent loop (or to be top-level loops). The original loop may be
1897 /// completely removed.
1899 /// The sibling loops resulting from this update are returned. If the original
1900 /// loop remains a valid loop, it will be the first entry in this list with all
1901 /// of the newly sibling loops following it.
1903 /// Returns true if the loop remains a loop after unswitching, and false if it
1904 /// is no longer a loop after unswitching (and should not continue to be
1906 static bool rebuildLoopAfterUnswitch(Loop
&L
, ArrayRef
<BasicBlock
*> ExitBlocks
,
1908 SmallVectorImpl
<Loop
*> &HoistedLoops
,
1909 ScalarEvolution
*SE
) {
1910 auto *PH
= L
.getLoopPreheader();
1912 // Compute the actual parent loop from the exit blocks. Because we may have
1913 // pruned some exits the loop may be different from the original parent.
1914 Loop
*ParentL
= nullptr;
1915 SmallVector
<Loop
*, 4> ExitLoops
;
1916 SmallVector
<BasicBlock
*, 4> ExitsInLoops
;
1917 ExitsInLoops
.reserve(ExitBlocks
.size());
1918 for (auto *ExitBB
: ExitBlocks
)
1919 if (Loop
*ExitL
= LI
.getLoopFor(ExitBB
)) {
1920 ExitLoops
.push_back(ExitL
);
1921 ExitsInLoops
.push_back(ExitBB
);
1922 if (!ParentL
|| (ParentL
!= ExitL
&& ParentL
->contains(ExitL
)))
1926 // Recompute the blocks participating in this loop. This may be empty if it
1927 // is no longer a loop.
1928 auto LoopBlockSet
= recomputeLoopBlockSet(L
, LI
);
1930 // If we still have a loop, we need to re-set the loop's parent as the exit
1931 // block set changing may have moved it within the loop nest. Note that this
1932 // can only happen when this loop has a parent as it can only hoist the loop
1934 if (!LoopBlockSet
.empty() && L
.getParentLoop() != ParentL
) {
1935 // Remove this loop's (original) blocks from all of the intervening loops.
1936 for (Loop
*IL
= L
.getParentLoop(); IL
!= ParentL
;
1937 IL
= IL
->getParentLoop()) {
1938 IL
->getBlocksSet().erase(PH
);
1939 for (auto *BB
: L
.blocks())
1940 IL
->getBlocksSet().erase(BB
);
1941 llvm::erase_if(IL
->getBlocksVector(), [&](BasicBlock
*BB
) {
1942 return BB
== PH
|| L
.contains(BB
);
1946 LI
.changeLoopFor(PH
, ParentL
);
1947 L
.getParentLoop()->removeChildLoop(&L
);
1949 ParentL
->addChildLoop(&L
);
1951 LI
.addTopLevelLoop(&L
);
1954 // Now we update all the blocks which are no longer within the loop.
1955 auto &Blocks
= L
.getBlocksVector();
1957 LoopBlockSet
.empty()
1959 : std::stable_partition(
1960 Blocks
.begin(), Blocks
.end(),
1961 [&](BasicBlock
*BB
) { return LoopBlockSet
.count(BB
); });
1963 // Before we erase the list of unlooped blocks, build a set of them.
1964 SmallPtrSet
<BasicBlock
*, 16> UnloopedBlocks(BlocksSplitI
, Blocks
.end());
1965 if (LoopBlockSet
.empty())
1966 UnloopedBlocks
.insert(PH
);
1968 // Now erase these blocks from the loop.
1969 for (auto *BB
: make_range(BlocksSplitI
, Blocks
.end()))
1970 L
.getBlocksSet().erase(BB
);
1971 Blocks
.erase(BlocksSplitI
, Blocks
.end());
1973 // Sort the exits in ascending loop depth, we'll work backwards across these
1974 // to process them inside out.
1975 llvm::stable_sort(ExitsInLoops
, [&](BasicBlock
*LHS
, BasicBlock
*RHS
) {
1976 return LI
.getLoopDepth(LHS
) < LI
.getLoopDepth(RHS
);
1979 // We'll build up a set for each exit loop.
1980 SmallPtrSet
<BasicBlock
*, 16> NewExitLoopBlocks
;
1981 Loop
*PrevExitL
= L
.getParentLoop(); // The deepest possible exit loop.
1983 auto RemoveUnloopedBlocksFromLoop
=
1984 [](Loop
&L
, SmallPtrSetImpl
<BasicBlock
*> &UnloopedBlocks
) {
1985 for (auto *BB
: UnloopedBlocks
)
1986 L
.getBlocksSet().erase(BB
);
1987 llvm::erase_if(L
.getBlocksVector(), [&](BasicBlock
*BB
) {
1988 return UnloopedBlocks
.count(BB
);
1992 SmallVector
<BasicBlock
*, 16> Worklist
;
1993 while (!UnloopedBlocks
.empty() && !ExitsInLoops
.empty()) {
1994 assert(Worklist
.empty() && "Didn't clear worklist!");
1995 assert(NewExitLoopBlocks
.empty() && "Didn't clear loop set!");
1997 // Grab the next exit block, in decreasing loop depth order.
1998 BasicBlock
*ExitBB
= ExitsInLoops
.pop_back_val();
1999 Loop
&ExitL
= *LI
.getLoopFor(ExitBB
);
2000 assert(ExitL
.contains(&L
) && "Exit loop must contain the inner loop!");
2002 // Erase all of the unlooped blocks from the loops between the previous
2003 // exit loop and this exit loop. This works because the ExitInLoops list is
2004 // sorted in increasing order of loop depth and thus we visit loops in
2005 // decreasing order of loop depth.
2006 for (; PrevExitL
!= &ExitL
; PrevExitL
= PrevExitL
->getParentLoop())
2007 RemoveUnloopedBlocksFromLoop(*PrevExitL
, UnloopedBlocks
);
2009 // Walk the CFG back until we hit the cloned PH adding everything reachable
2010 // and in the unlooped set to this exit block's loop.
2011 Worklist
.push_back(ExitBB
);
2013 BasicBlock
*BB
= Worklist
.pop_back_val();
2014 // We can stop recursing at the cloned preheader (if we get there).
2018 for (BasicBlock
*PredBB
: predecessors(BB
)) {
2019 // If this pred has already been moved to our set or is part of some
2020 // (inner) loop, no update needed.
2021 if (!UnloopedBlocks
.erase(PredBB
)) {
2022 assert((NewExitLoopBlocks
.count(PredBB
) ||
2023 ExitL
.contains(LI
.getLoopFor(PredBB
))) &&
2024 "Predecessor not in a nested loop (or already visited)!");
2028 // We just insert into the loop set here. We'll add these blocks to the
2029 // exit loop after we build up the set in a deterministic order rather
2030 // than the predecessor-influenced visit order.
2031 bool Inserted
= NewExitLoopBlocks
.insert(PredBB
).second
;
2033 assert(Inserted
&& "Should only visit an unlooped block once!");
2035 // And recurse through to its predecessors.
2036 Worklist
.push_back(PredBB
);
2038 } while (!Worklist
.empty());
2040 // If blocks in this exit loop were directly part of the original loop (as
2041 // opposed to a child loop) update the map to point to this exit loop. This
2042 // just updates a map and so the fact that the order is unstable is fine.
2043 for (auto *BB
: NewExitLoopBlocks
)
2044 if (Loop
*BBL
= LI
.getLoopFor(BB
))
2045 if (BBL
== &L
|| !L
.contains(BBL
))
2046 LI
.changeLoopFor(BB
, &ExitL
);
2048 // We will remove the remaining unlooped blocks from this loop in the next
2049 // iteration or below.
2050 NewExitLoopBlocks
.clear();
2053 // Any remaining unlooped blocks are no longer part of any loop unless they
2054 // are part of some child loop.
2055 for (; PrevExitL
; PrevExitL
= PrevExitL
->getParentLoop())
2056 RemoveUnloopedBlocksFromLoop(*PrevExitL
, UnloopedBlocks
);
2057 for (auto *BB
: UnloopedBlocks
)
2058 if (Loop
*BBL
= LI
.getLoopFor(BB
))
2059 if (BBL
== &L
|| !L
.contains(BBL
))
2060 LI
.changeLoopFor(BB
, nullptr);
2062 // Sink all the child loops whose headers are no longer in the loop set to
2063 // the parent (or to be top level loops). We reach into the loop and directly
2064 // update its subloop vector to make this batch update efficient.
2065 auto &SubLoops
= L
.getSubLoopsVector();
2066 auto SubLoopsSplitI
=
2067 LoopBlockSet
.empty()
2069 : std::stable_partition(
2070 SubLoops
.begin(), SubLoops
.end(), [&](Loop
*SubL
) {
2071 return LoopBlockSet
.count(SubL
->getHeader());
2073 for (auto *HoistedL
: make_range(SubLoopsSplitI
, SubLoops
.end())) {
2074 HoistedLoops
.push_back(HoistedL
);
2075 HoistedL
->setParentLoop(nullptr);
2077 // To compute the new parent of this hoisted loop we look at where we
2078 // placed the preheader above. We can't lookup the header itself because we
2079 // retained the mapping from the header to the hoisted loop. But the
2080 // preheader and header should have the exact same new parent computed
2081 // based on the set of exit blocks from the original loop as the preheader
2082 // is a predecessor of the header and so reached in the reverse walk. And
2083 // because the loops were all in simplified form the preheader of the
2084 // hoisted loop can't be part of some *other* loop.
2085 if (auto *NewParentL
= LI
.getLoopFor(HoistedL
->getLoopPreheader()))
2086 NewParentL
->addChildLoop(HoistedL
);
2088 LI
.addTopLevelLoop(HoistedL
);
2090 SubLoops
.erase(SubLoopsSplitI
, SubLoops
.end());
2092 // Actually delete the loop if nothing remained within it.
2093 if (Blocks
.empty()) {
2094 assert(SubLoops
.empty() &&
2095 "Failed to remove all subloops from the original loop!");
2096 if (Loop
*ParentL
= L
.getParentLoop())
2097 ParentL
->removeChildLoop(llvm::find(*ParentL
, &L
));
2099 LI
.removeLoop(llvm::find(LI
, &L
));
2100 // markLoopAsDeleted for L should be triggered by the caller (it is
2101 // typically done within postUnswitch).
2103 SE
->forgetBlockAndLoopDispositions();
2111 /// Helper to visit a dominator subtree, invoking a callable on each node.
2113 /// Returning false at any point will stop walking past that node of the tree.
2114 template <typename CallableT
>
2115 void visitDomSubTree(DominatorTree
&DT
, BasicBlock
*BB
, CallableT Callable
) {
2116 SmallVector
<DomTreeNode
*, 4> DomWorklist
;
2117 DomWorklist
.push_back(DT
[BB
]);
2119 SmallPtrSet
<DomTreeNode
*, 4> Visited
;
2120 Visited
.insert(DT
[BB
]);
2123 DomTreeNode
*N
= DomWorklist
.pop_back_val();
2126 if (!Callable(N
->getBlock()))
2129 // Accumulate the child nodes.
2130 for (DomTreeNode
*ChildN
: *N
) {
2131 assert(Visited
.insert(ChildN
).second
&&
2132 "Cannot visit a node twice when walking a tree!");
2133 DomWorklist
.push_back(ChildN
);
2135 } while (!DomWorklist
.empty());
2138 void postUnswitch(Loop
&L
, LPMUpdater
&U
, StringRef LoopName
,
2139 bool CurrentLoopValid
, bool PartiallyInvariant
,
2140 bool InjectedCondition
, ArrayRef
<Loop
*> NewLoops
) {
2141 // If we did a non-trivial unswitch, we have added new (cloned) loops.
2142 if (!NewLoops
.empty())
2143 U
.addSiblingLoops(NewLoops
);
2145 // If the current loop remains valid, we should revisit it to catch any
2146 // other unswitch opportunities. Otherwise, we need to mark it as deleted.
2147 if (CurrentLoopValid
) {
2148 if (PartiallyInvariant
) {
2149 // Mark the new loop as partially unswitched, to avoid unswitching on
2150 // the same condition again.
2151 auto &Context
= L
.getHeader()->getContext();
2152 MDNode
*DisableUnswitchMD
= MDNode::get(
2154 MDString::get(Context
, "llvm.loop.unswitch.partial.disable"));
2155 MDNode
*NewLoopID
= makePostTransformationMetadata(
2156 Context
, L
.getLoopID(), {"llvm.loop.unswitch.partial"},
2157 {DisableUnswitchMD
});
2158 L
.setLoopID(NewLoopID
);
2159 } else if (InjectedCondition
) {
2160 // Do the same for injection of invariant conditions.
2161 auto &Context
= L
.getHeader()->getContext();
2162 MDNode
*DisableUnswitchMD
= MDNode::get(
2164 MDString::get(Context
, "llvm.loop.unswitch.injection.disable"));
2165 MDNode
*NewLoopID
= makePostTransformationMetadata(
2166 Context
, L
.getLoopID(), {"llvm.loop.unswitch.injection"},
2167 {DisableUnswitchMD
});
2168 L
.setLoopID(NewLoopID
);
2170 U
.revisitCurrentLoop();
2172 U
.markLoopAsDeleted(L
, LoopName
);
2175 static void unswitchNontrivialInvariants(
2176 Loop
&L
, Instruction
&TI
, ArrayRef
<Value
*> Invariants
,
2177 IVConditionInfo
&PartialIVInfo
, DominatorTree
&DT
, LoopInfo
&LI
,
2178 AssumptionCache
&AC
, ScalarEvolution
*SE
, MemorySSAUpdater
*MSSAU
,
2179 LPMUpdater
&LoopUpdater
, bool InsertFreeze
, bool InjectedCondition
) {
2180 auto *ParentBB
= TI
.getParent();
2181 BranchInst
*BI
= dyn_cast
<BranchInst
>(&TI
);
2182 SwitchInst
*SI
= BI
? nullptr : cast
<SwitchInst
>(&TI
);
2184 // Save the current loop name in a variable so that we can report it even
2185 // after it has been deleted.
2186 std::string
LoopName(L
.getName());
2188 // We can only unswitch switches, conditional branches with an invariant
2189 // condition, or combining invariant conditions with an instruction or
2190 // partially invariant instructions.
2191 assert((SI
|| (BI
&& BI
->isConditional())) &&
2192 "Can only unswitch switches and conditional branch!");
2193 bool PartiallyInvariant
= !PartialIVInfo
.InstToDuplicate
.empty();
2195 SI
|| (skipTrivialSelect(BI
->getCondition()) == Invariants
[0] &&
2196 !PartiallyInvariant
);
2198 assert(Invariants
.size() == 1 &&
2199 "Cannot have other invariants with full unswitching!");
2201 assert(isa
<Instruction
>(skipTrivialSelect(BI
->getCondition())) &&
2202 "Partial unswitching requires an instruction as the condition!");
2204 if (MSSAU
&& VerifyMemorySSA
)
2205 MSSAU
->getMemorySSA()->verifyMemorySSA();
2207 // Constant and BBs tracking the cloned and continuing successor. When we are
2208 // unswitching the entire condition, this can just be trivially chosen to
2209 // unswitch towards `true`. However, when we are unswitching a set of
2210 // invariants combined with `and` or `or` or partially invariant instructions,
2211 // the combining operation determines the best direction to unswitch: we want
2212 // to unswitch the direction that will collapse the branch.
2213 bool Direction
= true;
2215 if (!FullUnswitch
) {
2216 Value
*Cond
= skipTrivialSelect(BI
->getCondition());
2218 assert(((match(Cond
, m_LogicalAnd()) ^ match(Cond
, m_LogicalOr())) ||
2219 PartiallyInvariant
) &&
2220 "Only `or`, `and`, an `select`, partially invariant instructions "
2221 "can combine invariants being unswitched.");
2222 if (!match(Cond
, m_LogicalOr())) {
2223 if (match(Cond
, m_LogicalAnd()) ||
2224 (PartiallyInvariant
&& !PartialIVInfo
.KnownValue
->isOneValue())) {
2231 BasicBlock
*RetainedSuccBB
=
2232 BI
? BI
->getSuccessor(1 - ClonedSucc
) : SI
->getDefaultDest();
2233 SmallSetVector
<BasicBlock
*, 4> UnswitchedSuccBBs
;
2235 UnswitchedSuccBBs
.insert(BI
->getSuccessor(ClonedSucc
));
2237 for (auto Case
: SI
->cases())
2238 if (Case
.getCaseSuccessor() != RetainedSuccBB
)
2239 UnswitchedSuccBBs
.insert(Case
.getCaseSuccessor());
2241 assert(!UnswitchedSuccBBs
.count(RetainedSuccBB
) &&
2242 "Should not unswitch the same successor we are retaining!");
2244 // The branch should be in this exact loop. Any inner loop's invariant branch
2245 // should be handled by unswitching that inner loop. The caller of this
2246 // routine should filter out any candidates that remain (but were skipped for
2247 // whatever reason).
2248 assert(LI
.getLoopFor(ParentBB
) == &L
&& "Branch in an inner loop!");
2250 // Compute the parent loop now before we start hacking on things.
2251 Loop
*ParentL
= L
.getParentLoop();
2252 // Get blocks in RPO order for MSSA update, before changing the CFG.
2253 LoopBlocksRPO
LBRPO(&L
);
2257 // Compute the outer-most loop containing one of our exit blocks. This is the
2258 // furthest up our loopnest which can be mutated, which we will use below to
2260 Loop
*OuterExitL
= &L
;
2261 SmallVector
<BasicBlock
*, 4> ExitBlocks
;
2262 L
.getUniqueExitBlocks(ExitBlocks
);
2263 for (auto *ExitBB
: ExitBlocks
) {
2264 // ExitBB can be an exit block for several levels in the loop nest. Make
2265 // sure we find the top most.
2266 Loop
*NewOuterExitL
= getTopMostExitingLoop(ExitBB
, LI
);
2267 if (!NewOuterExitL
) {
2268 // We exited the entire nest with this block, so we're done.
2269 OuterExitL
= nullptr;
2272 if (NewOuterExitL
!= OuterExitL
&& NewOuterExitL
->contains(OuterExitL
))
2273 OuterExitL
= NewOuterExitL
;
2276 // At this point, we're definitely going to unswitch something so invalidate
2277 // any cached information in ScalarEvolution for the outer most loop
2278 // containing an exit block and all nested loops.
2281 SE
->forgetLoop(OuterExitL
);
2283 SE
->forgetTopmostLoop(&L
);
2284 SE
->forgetBlockAndLoopDispositions();
2287 // If the edge from this terminator to a successor dominates that successor,
2288 // store a map from each block in its dominator subtree to it. This lets us
2289 // tell when cloning for a particular successor if a block is dominated by
2290 // some *other* successor with a single data structure. We use this to
2291 // significantly reduce cloning.
2292 SmallDenseMap
<BasicBlock
*, BasicBlock
*, 16> DominatingSucc
;
2293 for (auto *SuccBB
: llvm::concat
<BasicBlock
*const>(ArrayRef(RetainedSuccBB
),
2295 if (SuccBB
->getUniquePredecessor() ||
2296 llvm::all_of(predecessors(SuccBB
), [&](BasicBlock
*PredBB
) {
2297 return PredBB
== ParentBB
|| DT
.dominates(SuccBB
, PredBB
);
2299 visitDomSubTree(DT
, SuccBB
, [&](BasicBlock
*BB
) {
2300 DominatingSucc
[BB
] = SuccBB
;
2304 // Split the preheader, so that we know that there is a safe place to insert
2305 // the conditional branch. We will change the preheader to have a conditional
2306 // branch on LoopCond. The original preheader will become the split point
2307 // between the unswitched versions, and we will have a new preheader for the
2309 BasicBlock
*SplitBB
= L
.getLoopPreheader();
2310 BasicBlock
*LoopPH
= SplitEdge(SplitBB
, L
.getHeader(), &DT
, &LI
, MSSAU
);
2312 // Keep track of the dominator tree updates needed.
2313 SmallVector
<DominatorTree::UpdateType
, 4> DTUpdates
;
2315 // Clone the loop for each unswitched successor.
2316 SmallVector
<std::unique_ptr
<ValueToValueMapTy
>, 4> VMaps
;
2317 VMaps
.reserve(UnswitchedSuccBBs
.size());
2318 SmallDenseMap
<BasicBlock
*, BasicBlock
*, 4> ClonedPHs
;
2319 for (auto *SuccBB
: UnswitchedSuccBBs
) {
2320 VMaps
.emplace_back(new ValueToValueMapTy());
2321 ClonedPHs
[SuccBB
] = buildClonedLoopBlocks(
2322 L
, LoopPH
, SplitBB
, ExitBlocks
, ParentBB
, SuccBB
, RetainedSuccBB
,
2323 DominatingSucc
, *VMaps
.back(), DTUpdates
, AC
, DT
, LI
, MSSAU
, SE
);
2326 // Drop metadata if we may break its semantics by moving this instr into the
2328 if (TI
.getMetadata(LLVMContext::MD_make_implicit
)) {
2329 if (DropNonTrivialImplicitNullChecks
)
2330 // Do not spend time trying to understand if we can keep it, just drop it
2331 // to save compile time.
2332 TI
.setMetadata(LLVMContext::MD_make_implicit
, nullptr);
2334 // It is only legal to preserve make.implicit metadata if we are
2335 // guaranteed no reach implicit null check after following this branch.
2336 ICFLoopSafetyInfo SafetyInfo
;
2337 SafetyInfo
.computeLoopSafetyInfo(&L
);
2338 if (!SafetyInfo
.isGuaranteedToExecute(TI
, &DT
, &L
))
2339 TI
.setMetadata(LLVMContext::MD_make_implicit
, nullptr);
2343 // The stitching of the branched code back together depends on whether we're
2344 // doing full unswitching or not with the exception that we always want to
2345 // nuke the initial terminator placed in the split block.
2346 SplitBB
->getTerminator()->eraseFromParent();
2348 // Keep a clone of the terminator for MSSA updates.
2349 Instruction
*NewTI
= TI
.clone();
2350 NewTI
->insertInto(ParentBB
, ParentBB
->end());
2352 // Splice the terminator from the original loop and rewrite its
2354 TI
.moveBefore(*SplitBB
, SplitBB
->end());
2357 // First wire up the moved terminator to the preheaders.
2359 BasicBlock
*ClonedPH
= ClonedPHs
.begin()->second
;
2360 BI
->setSuccessor(ClonedSucc
, ClonedPH
);
2361 BI
->setSuccessor(1 - ClonedSucc
, LoopPH
);
2362 Value
*Cond
= skipTrivialSelect(BI
->getCondition());
2364 // We don't give any debug location to the new freeze, because the
2365 // BI (`dyn_cast<BranchInst>(TI)`) is an in-loop instruction hoisted
2367 Cond
= new FreezeInst(Cond
, Cond
->getName() + ".fr", BI
->getIterator());
2369 BI
->setCondition(Cond
);
2370 DTUpdates
.push_back({DominatorTree::Insert
, SplitBB
, ClonedPH
});
2372 assert(SI
&& "Must either be a branch or switch!");
2374 // Walk the cases and directly update their successors.
2375 assert(SI
->getDefaultDest() == RetainedSuccBB
&&
2376 "Not retaining default successor!");
2377 SI
->setDefaultDest(LoopPH
);
2378 for (const auto &Case
: SI
->cases())
2379 if (Case
.getCaseSuccessor() == RetainedSuccBB
)
2380 Case
.setSuccessor(LoopPH
);
2382 Case
.setSuccessor(ClonedPHs
.find(Case
.getCaseSuccessor())->second
);
2385 SI
->setCondition(new FreezeInst(SI
->getCondition(),
2386 SI
->getCondition()->getName() + ".fr",
2387 SI
->getIterator()));
2389 // We need to use the set to populate domtree updates as even when there
2390 // are multiple cases pointing at the same successor we only want to
2391 // remove and insert one edge in the domtree.
2392 for (BasicBlock
*SuccBB
: UnswitchedSuccBBs
)
2393 DTUpdates
.push_back(
2394 {DominatorTree::Insert
, SplitBB
, ClonedPHs
.find(SuccBB
)->second
});
2398 DT
.applyUpdates(DTUpdates
);
2401 // Remove all but one edge to the retained block and all unswitched
2402 // blocks. This is to avoid having duplicate entries in the cloned Phis,
2403 // when we know we only keep a single edge for each case.
2404 MSSAU
->removeDuplicatePhiEdgesBetween(ParentBB
, RetainedSuccBB
);
2405 for (BasicBlock
*SuccBB
: UnswitchedSuccBBs
)
2406 MSSAU
->removeDuplicatePhiEdgesBetween(ParentBB
, SuccBB
);
2408 for (auto &VMap
: VMaps
)
2409 MSSAU
->updateForClonedLoop(LBRPO
, ExitBlocks
, *VMap
,
2410 /*IgnoreIncomingWithNoClones=*/true);
2411 MSSAU
->updateExitBlocksForClonedLoop(ExitBlocks
, VMaps
, DT
);
2413 // Remove all edges to unswitched blocks.
2414 for (BasicBlock
*SuccBB
: UnswitchedSuccBBs
)
2415 MSSAU
->removeEdge(ParentBB
, SuccBB
);
2418 // Now unhook the successor relationship as we'll be replacing
2419 // the terminator with a direct branch. This is much simpler for branches
2420 // than switches so we handle those first.
2422 // Remove the parent as a predecessor of the unswitched successor.
2423 assert(UnswitchedSuccBBs
.size() == 1 &&
2424 "Only one possible unswitched block for a branch!");
2425 BasicBlock
*UnswitchedSuccBB
= *UnswitchedSuccBBs
.begin();
2426 UnswitchedSuccBB
->removePredecessor(ParentBB
,
2427 /*KeepOneInputPHIs*/ true);
2428 DTUpdates
.push_back({DominatorTree::Delete
, ParentBB
, UnswitchedSuccBB
});
2430 // Note that we actually want to remove the parent block as a predecessor
2431 // of *every* case successor. The case successor is either unswitched,
2432 // completely eliminating an edge from the parent to that successor, or it
2433 // is a duplicate edge to the retained successor as the retained successor
2434 // is always the default successor and as we'll replace this with a direct
2435 // branch we no longer need the duplicate entries in the PHI nodes.
2436 SwitchInst
*NewSI
= cast
<SwitchInst
>(NewTI
);
2437 assert(NewSI
->getDefaultDest() == RetainedSuccBB
&&
2438 "Not retaining default successor!");
2439 for (const auto &Case
: NewSI
->cases())
2440 Case
.getCaseSuccessor()->removePredecessor(
2442 /*KeepOneInputPHIs*/ true);
2444 // We need to use the set to populate domtree updates as even when there
2445 // are multiple cases pointing at the same successor we only want to
2446 // remove and insert one edge in the domtree.
2447 for (BasicBlock
*SuccBB
: UnswitchedSuccBBs
)
2448 DTUpdates
.push_back({DominatorTree::Delete
, ParentBB
, SuccBB
});
2451 // Create a new unconditional branch to the continuing block (as opposed to
2453 Instruction
*NewBI
= BranchInst::Create(RetainedSuccBB
, ParentBB
);
2454 NewBI
->setDebugLoc(NewTI
->getDebugLoc());
2456 // After MSSAU update, remove the cloned terminator instruction NewTI.
2457 NewTI
->eraseFromParent();
2459 assert(BI
&& "Only branches have partial unswitching.");
2460 assert(UnswitchedSuccBBs
.size() == 1 &&
2461 "Only one possible unswitched block for a branch!");
2462 BasicBlock
*ClonedPH
= ClonedPHs
.begin()->second
;
2463 // When doing a partial unswitch, we have to do a bit more work to build up
2464 // the branch in the split block.
2465 if (PartiallyInvariant
)
2466 buildPartialInvariantUnswitchConditionalBranch(
2467 *SplitBB
, Invariants
, Direction
, *ClonedPH
, *LoopPH
, L
, MSSAU
);
2469 buildPartialUnswitchConditionalBranch(
2470 *SplitBB
, Invariants
, Direction
, *ClonedPH
, *LoopPH
,
2471 FreezeLoopUnswitchCond
, BI
, &AC
, DT
);
2473 DTUpdates
.push_back({DominatorTree::Insert
, SplitBB
, ClonedPH
});
2476 DT
.applyUpdates(DTUpdates
);
2479 // Perform MSSA cloning updates.
2480 for (auto &VMap
: VMaps
)
2481 MSSAU
->updateForClonedLoop(LBRPO
, ExitBlocks
, *VMap
,
2482 /*IgnoreIncomingWithNoClones=*/true);
2483 MSSAU
->updateExitBlocksForClonedLoop(ExitBlocks
, VMaps
, DT
);
2487 // Apply the updates accumulated above to get an up-to-date dominator tree.
2488 DT
.applyUpdates(DTUpdates
);
2490 // Now that we have an accurate dominator tree, first delete the dead cloned
2491 // blocks so that we can accurately build any cloned loops. It is important to
2492 // not delete the blocks from the original loop yet because we still want to
2493 // reference the original loop to understand the cloned loop's structure.
2494 deleteDeadClonedBlocks(L
, ExitBlocks
, VMaps
, DT
, MSSAU
);
2496 // Build the cloned loop structure itself. This may be substantially
2497 // different from the original structure due to the simplified CFG. This also
2498 // handles inserting all the cloned blocks into the correct loops.
2499 SmallVector
<Loop
*, 4> NonChildClonedLoops
;
2500 for (std::unique_ptr
<ValueToValueMapTy
> &VMap
: VMaps
)
2501 buildClonedLoops(L
, ExitBlocks
, *VMap
, LI
, NonChildClonedLoops
);
2503 // Now that our cloned loops have been built, we can update the original loop.
2504 // First we delete the dead blocks from it and then we rebuild the loop
2505 // structure taking these deletions into account.
2506 deleteDeadBlocksFromLoop(L
, ExitBlocks
, DT
, LI
, MSSAU
, SE
, LoopUpdater
);
2508 if (MSSAU
&& VerifyMemorySSA
)
2509 MSSAU
->getMemorySSA()->verifyMemorySSA();
2511 SmallVector
<Loop
*, 4> HoistedLoops
;
2513 rebuildLoopAfterUnswitch(L
, ExitBlocks
, LI
, HoistedLoops
, SE
);
2515 if (MSSAU
&& VerifyMemorySSA
)
2516 MSSAU
->getMemorySSA()->verifyMemorySSA();
2518 // This transformation has a high risk of corrupting the dominator tree, and
2519 // the below steps to rebuild loop structures will result in hard to debug
2520 // errors in that case so verify that the dominator tree is sane first.
2521 // FIXME: Remove this when the bugs stop showing up and rely on existing
2522 // verification steps.
2523 assert(DT
.verify(DominatorTree::VerificationLevel::Fast
));
2525 if (BI
&& !PartiallyInvariant
) {
2526 // If we unswitched a branch which collapses the condition to a known
2527 // constant we want to replace all the uses of the invariants within both
2528 // the original and cloned blocks. We do this here so that we can use the
2529 // now updated dominator tree to identify which side the users are on.
2530 assert(UnswitchedSuccBBs
.size() == 1 &&
2531 "Only one possible unswitched block for a branch!");
2532 BasicBlock
*ClonedPH
= ClonedPHs
.begin()->second
;
2534 // When considering multiple partially-unswitched invariants
2535 // we cant just go replace them with constants in both branches.
2537 // For 'AND' we infer that true branch ("continue") means true
2538 // for each invariant operand.
2539 // For 'OR' we can infer that false branch ("continue") means false
2540 // for each invariant operand.
2541 // So it happens that for multiple-partial case we dont replace
2542 // in the unswitched branch.
2543 bool ReplaceUnswitched
=
2544 FullUnswitch
|| (Invariants
.size() == 1) || PartiallyInvariant
;
2546 ConstantInt
*UnswitchedReplacement
=
2547 Direction
? ConstantInt::getTrue(BI
->getContext())
2548 : ConstantInt::getFalse(BI
->getContext());
2549 ConstantInt
*ContinueReplacement
=
2550 Direction
? ConstantInt::getFalse(BI
->getContext())
2551 : ConstantInt::getTrue(BI
->getContext());
2552 for (Value
*Invariant
: Invariants
) {
2553 assert(!isa
<Constant
>(Invariant
) &&
2554 "Should not be replacing constant values!");
2555 // Use make_early_inc_range here as set invalidates the iterator.
2556 for (Use
&U
: llvm::make_early_inc_range(Invariant
->uses())) {
2557 Instruction
*UserI
= dyn_cast
<Instruction
>(U
.getUser());
2561 // Replace it with the 'continue' side if in the main loop body, and the
2562 // unswitched if in the cloned blocks.
2563 if (DT
.dominates(LoopPH
, UserI
->getParent()))
2564 U
.set(ContinueReplacement
);
2565 else if (ReplaceUnswitched
&&
2566 DT
.dominates(ClonedPH
, UserI
->getParent()))
2567 U
.set(UnswitchedReplacement
);
2572 // We can change which blocks are exit blocks of all the cloned sibling
2573 // loops, the current loop, and any parent loops which shared exit blocks
2574 // with the current loop. As a consequence, we need to re-form LCSSA for
2575 // them. But we shouldn't need to re-form LCSSA for any child loops.
2576 // FIXME: This could be made more efficient by tracking which exit blocks are
2577 // new, and focusing on them, but that isn't likely to be necessary.
2579 // In order to reasonably rebuild LCSSA we need to walk inside-out across the
2580 // loop nest and update every loop that could have had its exits changed. We
2581 // also need to cover any intervening loops. We add all of these loops to
2582 // a list and sort them by loop depth to achieve this without updating
2583 // unnecessary loops.
2584 auto UpdateLoop
= [&](Loop
&UpdateL
) {
2586 UpdateL
.verifyLoop();
2587 for (Loop
*ChildL
: UpdateL
) {
2588 ChildL
->verifyLoop();
2589 assert(ChildL
->isRecursivelyLCSSAForm(DT
, LI
) &&
2590 "Perturbed a child loop's LCSSA form!");
2593 // First build LCSSA for this loop so that we can preserve it when
2594 // forming dedicated exits. We don't want to perturb some other loop's
2595 // LCSSA while doing that CFG edit.
2596 formLCSSA(UpdateL
, DT
, &LI
, SE
);
2598 // For loops reached by this loop's original exit blocks we may
2599 // introduced new, non-dedicated exits. At least try to re-form dedicated
2600 // exits for these loops. This may fail if they couldn't have dedicated
2601 // exits to start with.
2602 formDedicatedExitBlocks(&UpdateL
, &DT
, &LI
, MSSAU
, /*PreserveLCSSA*/ true);
2605 // For non-child cloned loops and hoisted loops, we just need to update LCSSA
2606 // and we can do it in any order as they don't nest relative to each other.
2608 // Also check if any of the loops we have updated have become top-level loops
2609 // as that will necessitate widening the outer loop scope.
2610 for (Loop
*UpdatedL
:
2611 llvm::concat
<Loop
*>(NonChildClonedLoops
, HoistedLoops
)) {
2612 UpdateLoop(*UpdatedL
);
2613 if (UpdatedL
->isOutermost())
2614 OuterExitL
= nullptr;
2618 if (L
.isOutermost())
2619 OuterExitL
= nullptr;
2622 // If the original loop had exit blocks, walk up through the outer most loop
2623 // of those exit blocks to update LCSSA and form updated dedicated exits.
2624 if (OuterExitL
!= &L
)
2625 for (Loop
*OuterL
= ParentL
; OuterL
!= OuterExitL
;
2626 OuterL
= OuterL
->getParentLoop())
2627 UpdateLoop(*OuterL
);
2630 // Verify the entire loop structure to catch any incorrect updates before we
2631 // progress in the pass pipeline.
2635 // Now that we've unswitched something, make callbacks to report the changes.
2636 // For that we need to merge together the updated loops and the cloned loops
2637 // and check whether the original loop survived.
2638 SmallVector
<Loop
*, 4> SibLoops
;
2639 for (Loop
*UpdatedL
: llvm::concat
<Loop
*>(NonChildClonedLoops
, HoistedLoops
))
2640 if (UpdatedL
->getParentLoop() == ParentL
)
2641 SibLoops
.push_back(UpdatedL
);
2642 postUnswitch(L
, LoopUpdater
, LoopName
, IsStillLoop
, PartiallyInvariant
,
2643 InjectedCondition
, SibLoops
);
2645 if (MSSAU
&& VerifyMemorySSA
)
2646 MSSAU
->getMemorySSA()->verifyMemorySSA();
2654 /// Recursively compute the cost of a dominator subtree based on the per-block
2655 /// cost map provided.
2657 /// The recursive computation is memozied into the provided DT-indexed cost map
2658 /// to allow querying it for most nodes in the domtree without it becoming
2660 static InstructionCost
computeDomSubtreeCost(
2662 const SmallDenseMap
<BasicBlock
*, InstructionCost
, 4> &BBCostMap
,
2663 SmallDenseMap
<DomTreeNode
*, InstructionCost
, 4> &DTCostMap
) {
2664 // Don't accumulate cost (or recurse through) blocks not in our block cost
2665 // map and thus not part of the duplication cost being considered.
2666 auto BBCostIt
= BBCostMap
.find(N
.getBlock());
2667 if (BBCostIt
== BBCostMap
.end())
2670 // Lookup this node to see if we already computed its cost.
2671 auto DTCostIt
= DTCostMap
.find(&N
);
2672 if (DTCostIt
!= DTCostMap
.end())
2673 return DTCostIt
->second
;
2675 // If not, we have to compute it. We can't use insert above and update
2676 // because computing the cost may insert more things into the map.
2677 InstructionCost Cost
= std::accumulate(
2678 N
.begin(), N
.end(), BBCostIt
->second
,
2679 [&](InstructionCost Sum
, DomTreeNode
*ChildN
) -> InstructionCost
{
2680 return Sum
+ computeDomSubtreeCost(*ChildN
, BBCostMap
, DTCostMap
);
2682 bool Inserted
= DTCostMap
.insert({&N
, Cost
}).second
;
2684 assert(Inserted
&& "Should not insert a node while visiting children!");
2688 /// Turns a select instruction into implicit control flow branch,
2689 /// making the following replacement:
2692 /// --code before select--
2693 /// select %cond, %trueval, %falseval
2694 /// --code after select--
2699 /// --code before select--
2700 /// br i1 %cond, label %then, label %tail
2706 /// phi [ %trueval, %then ], [ %falseval, %head]
2709 /// It also makes all relevant DT and LI updates, so that all structures are in
2710 /// valid state after this transform.
2711 static BranchInst
*turnSelectIntoBranch(SelectInst
*SI
, DominatorTree
&DT
,
2712 LoopInfo
&LI
, MemorySSAUpdater
*MSSAU
,
2713 AssumptionCache
*AC
) {
2714 LLVM_DEBUG(dbgs() << "Turning " << *SI
<< " into a branch.\n");
2715 BasicBlock
*HeadBB
= SI
->getParent();
2717 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
2718 SplitBlockAndInsertIfThen(SI
->getCondition(), SI
, false,
2719 SI
->getMetadata(LLVMContext::MD_prof
), &DTU
, &LI
);
2720 auto *CondBr
= cast
<BranchInst
>(HeadBB
->getTerminator());
2721 BasicBlock
*ThenBB
= CondBr
->getSuccessor(0),
2722 *TailBB
= CondBr
->getSuccessor(1);
2724 MSSAU
->moveAllAfterSpliceBlocks(HeadBB
, TailBB
, SI
);
2727 PHINode::Create(SI
->getType(), 2, "unswitched.select", SI
->getIterator());
2728 Phi
->addIncoming(SI
->getTrueValue(), ThenBB
);
2729 Phi
->addIncoming(SI
->getFalseValue(), HeadBB
);
2730 Phi
->setDebugLoc(SI
->getDebugLoc());
2731 SI
->replaceAllUsesWith(Phi
);
2732 SI
->eraseFromParent();
2734 if (MSSAU
&& VerifyMemorySSA
)
2735 MSSAU
->getMemorySSA()->verifyMemorySSA();
2741 /// Turns a llvm.experimental.guard intrinsic into implicit control flow branch,
2742 /// making the following replacement:
2744 /// --code before guard--
2745 /// call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
2746 /// --code after guard--
2750 /// --code before guard--
2751 /// br i1 %cond, label %guarded, label %deopt
2754 /// --code after guard--
2757 /// call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
2760 /// It also makes all relevant DT and LI updates, so that all structures are in
2761 /// valid state after this transform.
2762 static BranchInst
*turnGuardIntoBranch(IntrinsicInst
*GI
, Loop
&L
,
2763 DominatorTree
&DT
, LoopInfo
&LI
,
2764 MemorySSAUpdater
*MSSAU
) {
2765 SmallVector
<DominatorTree::UpdateType
, 4> DTUpdates
;
2766 LLVM_DEBUG(dbgs() << "Turning " << *GI
<< " into a branch.\n");
2767 BasicBlock
*CheckBB
= GI
->getParent();
2769 if (MSSAU
&& VerifyMemorySSA
)
2770 MSSAU
->getMemorySSA()->verifyMemorySSA();
2772 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
2773 Instruction
*DeoptBlockTerm
=
2774 SplitBlockAndInsertIfThen(GI
->getArgOperand(0), GI
, true,
2775 GI
->getMetadata(LLVMContext::MD_prof
), &DTU
, &LI
);
2776 BranchInst
*CheckBI
= cast
<BranchInst
>(CheckBB
->getTerminator());
2777 // SplitBlockAndInsertIfThen inserts control flow that branches to
2778 // DeoptBlockTerm if the condition is true. We want the opposite.
2779 CheckBI
->swapSuccessors();
2781 BasicBlock
*GuardedBlock
= CheckBI
->getSuccessor(0);
2782 GuardedBlock
->setName("guarded");
2783 CheckBI
->getSuccessor(1)->setName("deopt");
2784 BasicBlock
*DeoptBlock
= CheckBI
->getSuccessor(1);
2787 MSSAU
->moveAllAfterSpliceBlocks(CheckBB
, GuardedBlock
, GI
);
2789 GI
->moveBefore(DeoptBlockTerm
);
2790 GI
->setArgOperand(0, ConstantInt::getFalse(GI
->getContext()));
2793 MemoryDef
*MD
= cast
<MemoryDef
>(MSSAU
->getMemorySSA()->getMemoryAccess(GI
));
2794 MSSAU
->moveToPlace(MD
, DeoptBlock
, MemorySSA::BeforeTerminator
);
2795 if (VerifyMemorySSA
)
2796 MSSAU
->getMemorySSA()->verifyMemorySSA();
2805 /// Cost multiplier is a way to limit potentially exponential behavior
2806 /// of loop-unswitch. Cost is multipied in proportion of 2^number of unswitch
2807 /// candidates available. Also accounting for the number of "sibling" loops with
2808 /// the idea to account for previous unswitches that already happened on this
2809 /// cluster of loops. There was an attempt to keep this formula simple,
2810 /// just enough to limit the worst case behavior. Even if it is not that simple
2811 /// now it is still not an attempt to provide a detailed heuristic size
2814 /// TODO: Make a proper accounting of "explosion" effect for all kinds of
2815 /// unswitch candidates, making adequate predictions instead of wild guesses.
2816 /// That requires knowing not just the number of "remaining" candidates but
2817 /// also costs of unswitching for each of these candidates.
2818 static int CalculateUnswitchCostMultiplier(
2819 const Instruction
&TI
, const Loop
&L
, const LoopInfo
&LI
,
2820 const DominatorTree
&DT
,
2821 ArrayRef
<NonTrivialUnswitchCandidate
> UnswitchCandidates
) {
2823 // Guards and other exiting conditions do not contribute to exponential
2824 // explosion as soon as they dominate the latch (otherwise there might be
2825 // another path to the latch remaining that does not allow to eliminate the
2826 // loop copy on unswitch).
2827 const BasicBlock
*Latch
= L
.getLoopLatch();
2828 const BasicBlock
*CondBlock
= TI
.getParent();
2829 if (DT
.dominates(CondBlock
, Latch
) &&
2831 (TI
.isTerminator() &&
2832 llvm::count_if(successors(&TI
), [&L
](const BasicBlock
*SuccBB
) {
2833 return L
.contains(SuccBB
);
2835 NumCostMultiplierSkipped
++;
2839 auto *ParentL
= L
.getParentLoop();
2840 int SiblingsCount
= (ParentL
? ParentL
->getSubLoopsVector().size()
2841 : std::distance(LI
.begin(), LI
.end()));
2842 // Count amount of clones that all the candidates might cause during
2843 // unswitching. Branch/guard/select counts as 1, switch counts as log2 of its
2845 int UnswitchedClones
= 0;
2846 for (const auto &Candidate
: UnswitchCandidates
) {
2847 const Instruction
*CI
= Candidate
.TI
;
2848 const BasicBlock
*CondBlock
= CI
->getParent();
2849 bool SkipExitingSuccessors
= DT
.dominates(CondBlock
, Latch
);
2850 if (isa
<SelectInst
>(CI
)) {
2855 if (!SkipExitingSuccessors
)
2859 int NonExitingSuccessors
=
2860 llvm::count_if(successors(CondBlock
),
2861 [SkipExitingSuccessors
, &L
](const BasicBlock
*SuccBB
) {
2862 return !SkipExitingSuccessors
|| L
.contains(SuccBB
);
2864 UnswitchedClones
+= Log2_32(NonExitingSuccessors
);
2867 // Ignore up to the "unscaled candidates" number of unswitch candidates
2868 // when calculating the power-of-two scaling of the cost. The main idea
2869 // with this control is to allow a small number of unswitches to happen
2870 // and rely more on siblings multiplier (see below) when the number
2871 // of candidates is small.
2872 unsigned ClonesPower
=
2873 std::max(UnswitchedClones
- (int)UnswitchNumInitialUnscaledCandidates
, 0);
2875 // Allowing top-level loops to spread a bit more than nested ones.
2876 int SiblingsMultiplier
=
2877 std::max((ParentL
? SiblingsCount
2878 : SiblingsCount
/ (int)UnswitchSiblingsToplevelDiv
),
2880 // Compute the cost multiplier in a way that won't overflow by saturating
2881 // at an upper bound.
2883 if (ClonesPower
> Log2_32(UnswitchThreshold
) ||
2884 SiblingsMultiplier
> UnswitchThreshold
)
2885 CostMultiplier
= UnswitchThreshold
;
2887 CostMultiplier
= std::min(SiblingsMultiplier
* (1 << ClonesPower
),
2888 (int)UnswitchThreshold
);
2890 LLVM_DEBUG(dbgs() << " Computed multiplier " << CostMultiplier
2891 << " (siblings " << SiblingsMultiplier
<< " * clones "
2892 << (1 << ClonesPower
) << ")"
2893 << " for unswitch candidate: " << TI
<< "\n");
2894 return CostMultiplier
;
2897 static bool collectUnswitchCandidates(
2898 SmallVectorImpl
<NonTrivialUnswitchCandidate
> &UnswitchCandidates
,
2899 IVConditionInfo
&PartialIVInfo
, Instruction
*&PartialIVCondBranch
,
2900 const Loop
&L
, const LoopInfo
&LI
, AAResults
&AA
,
2901 const MemorySSAUpdater
*MSSAU
) {
2902 assert(UnswitchCandidates
.empty() && "Should be!");
2904 auto AddUnswitchCandidatesForInst
= [&](Instruction
*I
, Value
*Cond
) {
2905 Cond
= skipTrivialSelect(Cond
);
2906 if (isa
<Constant
>(Cond
))
2908 if (L
.isLoopInvariant(Cond
)) {
2909 UnswitchCandidates
.push_back({I
, {Cond
}});
2912 if (match(Cond
, m_CombineOr(m_LogicalAnd(), m_LogicalOr()))) {
2913 TinyPtrVector
<Value
*> Invariants
=
2914 collectHomogenousInstGraphLoopInvariants(
2915 L
, *static_cast<Instruction
*>(Cond
), LI
);
2916 if (!Invariants
.empty())
2917 UnswitchCandidates
.push_back({I
, std::move(Invariants
)});
2921 // Whether or not we should also collect guards in the loop.
2922 bool CollectGuards
= false;
2923 if (UnswitchGuards
) {
2924 auto *GuardDecl
= Intrinsic::getDeclarationIfExists(
2925 L
.getHeader()->getParent()->getParent(), Intrinsic::experimental_guard
);
2926 if (GuardDecl
&& !GuardDecl
->use_empty())
2927 CollectGuards
= true;
2930 for (auto *BB
: L
.blocks()) {
2931 if (LI
.getLoopFor(BB
) != &L
)
2934 for (auto &I
: *BB
) {
2935 if (auto *SI
= dyn_cast
<SelectInst
>(&I
)) {
2936 auto *Cond
= SI
->getCondition();
2937 // Do not unswitch vector selects and logical and/or selects
2938 if (Cond
->getType()->isIntegerTy(1) && !SI
->getType()->isIntegerTy(1))
2939 AddUnswitchCandidatesForInst(SI
, Cond
);
2940 } else if (CollectGuards
&& isGuard(&I
)) {
2942 skipTrivialSelect(cast
<IntrinsicInst
>(&I
)->getArgOperand(0));
2943 // TODO: Support AND, OR conditions and partial unswitching.
2944 if (!isa
<Constant
>(Cond
) && L
.isLoopInvariant(Cond
))
2945 UnswitchCandidates
.push_back({&I
, {Cond
}});
2949 if (auto *SI
= dyn_cast
<SwitchInst
>(BB
->getTerminator())) {
2950 // We can only consider fully loop-invariant switch conditions as we need
2951 // to completely eliminate the switch after unswitching.
2952 if (!isa
<Constant
>(SI
->getCondition()) &&
2953 L
.isLoopInvariant(SI
->getCondition()) && !BB
->getUniqueSuccessor())
2954 UnswitchCandidates
.push_back({SI
, {SI
->getCondition()}});
2958 auto *BI
= dyn_cast
<BranchInst
>(BB
->getTerminator());
2959 if (!BI
|| !BI
->isConditional() ||
2960 BI
->getSuccessor(0) == BI
->getSuccessor(1))
2963 AddUnswitchCandidatesForInst(BI
, BI
->getCondition());
2966 if (MSSAU
&& !findOptionMDForLoop(&L
, "llvm.loop.unswitch.partial.disable") &&
2967 !any_of(UnswitchCandidates
, [&L
](auto &TerminatorAndInvariants
) {
2968 return TerminatorAndInvariants
.TI
== L
.getHeader()->getTerminator();
2970 MemorySSA
*MSSA
= MSSAU
->getMemorySSA();
2971 if (auto Info
= hasPartialIVCondition(L
, MSSAThreshold
, *MSSA
, AA
)) {
2973 dbgs() << "simple-loop-unswitch: Found partially invariant condition "
2974 << *Info
->InstToDuplicate
[0] << "\n");
2975 PartialIVInfo
= *Info
;
2976 PartialIVCondBranch
= L
.getHeader()->getTerminator();
2977 TinyPtrVector
<Value
*> ValsToDuplicate
;
2978 llvm::append_range(ValsToDuplicate
, Info
->InstToDuplicate
);
2979 UnswitchCandidates
.push_back(
2980 {L
.getHeader()->getTerminator(), std::move(ValsToDuplicate
)});
2983 return !UnswitchCandidates
.empty();
2986 /// Tries to canonicalize condition described by:
2988 /// br (LHS pred RHS), label IfTrue, label IfFalse
2990 /// into its equivalent where `Pred` is something that we support for injected
2991 /// invariants (so far it is limited to ult), LHS in canonicalized form is
2992 /// non-invariant and RHS is an invariant.
2993 static void canonicalizeForInvariantConditionInjection(
2994 ICmpInst::Predicate
&Pred
, Value
*&LHS
, Value
*&RHS
, BasicBlock
*&IfTrue
,
2995 BasicBlock
*&IfFalse
, const Loop
&L
) {
2996 if (!L
.contains(IfTrue
)) {
2997 Pred
= ICmpInst::getInversePredicate(Pred
);
2998 std::swap(IfTrue
, IfFalse
);
3001 // Move loop-invariant argument to RHS position.
3002 if (L
.isLoopInvariant(LHS
)) {
3003 Pred
= ICmpInst::getSwappedPredicate(Pred
);
3004 std::swap(LHS
, RHS
);
3007 if (Pred
== ICmpInst::ICMP_SGE
&& match(RHS
, m_Zero())) {
3008 // Turn "x >=s 0" into "x <u UMIN_INT"
3009 Pred
= ICmpInst::ICMP_ULT
;
3010 RHS
= ConstantInt::get(
3012 APInt::getSignedMinValue(RHS
->getType()->getIntegerBitWidth()));
3016 /// Returns true, if predicate described by ( \p Pred, \p LHS, \p RHS )
3017 /// succeeding into blocks ( \p IfTrue, \p IfFalse) can be optimized by
3018 /// injecting a loop-invariant condition.
3019 static bool shouldTryInjectInvariantCondition(
3020 const ICmpInst::Predicate Pred
, const Value
*LHS
, const Value
*RHS
,
3021 const BasicBlock
*IfTrue
, const BasicBlock
*IfFalse
, const Loop
&L
) {
3022 if (L
.isLoopInvariant(LHS
) || !L
.isLoopInvariant(RHS
))
3024 // TODO: Support other predicates.
3025 if (Pred
!= ICmpInst::ICMP_ULT
)
3027 // TODO: Support non-loop-exiting branches?
3028 if (!L
.contains(IfTrue
) || L
.contains(IfFalse
))
3030 // FIXME: For some reason this causes problems with MSSA updates, need to
3031 // investigate why. So far, just don't unswitch latch.
3032 if (L
.getHeader() == IfTrue
)
3037 /// Returns true, if metadata on \p BI allows us to optimize branching into \p
3038 /// TakenSucc via injection of invariant conditions. The branch should be not
3039 /// enough and not previously unswitched, the information about this comes from
3041 bool shouldTryInjectBasingOnMetadata(const BranchInst
*BI
,
3042 const BasicBlock
*TakenSucc
) {
3043 SmallVector
<uint32_t> Weights
;
3044 if (!extractBranchWeights(*BI
, Weights
))
3046 unsigned T
= InjectInvariantConditionHotnesThreshold
;
3047 BranchProbability
LikelyTaken(T
- 1, T
);
3049 assert(Weights
.size() == 2 && "Unexpected profile data!");
3050 size_t Idx
= BI
->getSuccessor(0) == TakenSucc
? 0 : 1;
3051 auto Num
= Weights
[Idx
];
3052 auto Denom
= Weights
[0] + Weights
[1];
3053 // Degenerate or overflowed metadata.
3054 if (Denom
== 0 || Num
> Denom
)
3056 BranchProbability
ActualTaken(Num
, Denom
);
3057 if (LikelyTaken
> ActualTaken
)
3062 /// Materialize pending invariant condition of the given candidate into IR. The
3063 /// injected loop-invariant condition implies the original loop-variant branch
3064 /// condition, so the materialization turns
3068 /// br i1 %variant_cond, label InLoopSucc, label OutOfLoopSucc
3073 /// %invariant_cond = LHS pred RHS
3076 /// br i1 %invariant_cond, label InLoopSucc, label OriginalCheck
3078 /// br i1 %variant_cond, label InLoopSucc, label OutOfLoopSucc
3080 static NonTrivialUnswitchCandidate
3081 injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate
, Loop
&L
,
3082 DominatorTree
&DT
, LoopInfo
&LI
,
3083 AssumptionCache
&AC
, MemorySSAUpdater
*MSSAU
) {
3084 assert(Candidate
.hasPendingInjection() && "Nothing to inject!");
3085 BasicBlock
*Preheader
= L
.getLoopPreheader();
3086 assert(Preheader
&& "Loop is not in simplified form?");
3087 assert(LI
.getLoopFor(Candidate
.TI
->getParent()) == &L
&&
3088 "Unswitching branch of inner loop!");
3090 auto Pred
= Candidate
.PendingInjection
->Pred
;
3091 auto *LHS
= Candidate
.PendingInjection
->LHS
;
3092 auto *RHS
= Candidate
.PendingInjection
->RHS
;
3093 auto *InLoopSucc
= Candidate
.PendingInjection
->InLoopSucc
;
3094 auto *TI
= cast
<BranchInst
>(Candidate
.TI
);
3095 auto *BB
= Candidate
.TI
->getParent();
3096 auto *OutOfLoopSucc
= InLoopSucc
== TI
->getSuccessor(0) ? TI
->getSuccessor(1)
3097 : TI
->getSuccessor(0);
3098 // FIXME: Remove this once limitation on successors is lifted.
3099 assert(L
.contains(InLoopSucc
) && "Not supported yet!");
3100 assert(!L
.contains(OutOfLoopSucc
) && "Not supported yet!");
3101 auto &Ctx
= BB
->getContext();
3103 IRBuilder
<> Builder(Preheader
->getTerminator());
3104 assert(ICmpInst::isUnsigned(Pred
) && "Not supported yet!");
3105 if (LHS
->getType() != RHS
->getType()) {
3106 if (LHS
->getType()->getIntegerBitWidth() <
3107 RHS
->getType()->getIntegerBitWidth())
3108 LHS
= Builder
.CreateZExt(LHS
, RHS
->getType(), LHS
->getName() + ".wide");
3110 RHS
= Builder
.CreateZExt(RHS
, LHS
->getType(), RHS
->getName() + ".wide");
3112 // Do not use builder here: CreateICmp may simplify this into a constant and
3113 // unswitching will break. Better optimize it away later.
3114 auto *InjectedCond
=
3115 ICmpInst::Create(Instruction::ICmp
, Pred
, LHS
, RHS
, "injected.cond",
3116 Preheader
->getTerminator()->getIterator());
3118 BasicBlock
*CheckBlock
= BasicBlock::Create(Ctx
, BB
->getName() + ".check",
3119 BB
->getParent(), InLoopSucc
);
3120 Builder
.SetInsertPoint(TI
);
3122 Builder
.CreateCondBr(InjectedCond
, InLoopSucc
, CheckBlock
);
3124 Builder
.SetInsertPoint(CheckBlock
);
3125 Builder
.CreateCondBr(TI
->getCondition(), TI
->getSuccessor(0),
3126 TI
->getSuccessor(1));
3127 TI
->eraseFromParent();
3130 for (auto &I
: *InLoopSucc
) {
3131 auto *PN
= dyn_cast
<PHINode
>(&I
);
3134 auto *Inc
= PN
->getIncomingValueForBlock(BB
);
3135 PN
->addIncoming(Inc
, CheckBlock
);
3137 OutOfLoopSucc
->replacePhiUsesWith(BB
, CheckBlock
);
3139 SmallVector
<DominatorTree::UpdateType
, 4> DTUpdates
= {
3140 { DominatorTree::Insert
, BB
, CheckBlock
},
3141 { DominatorTree::Insert
, CheckBlock
, InLoopSucc
},
3142 { DominatorTree::Insert
, CheckBlock
, OutOfLoopSucc
},
3143 { DominatorTree::Delete
, BB
, OutOfLoopSucc
}
3146 DT
.applyUpdates(DTUpdates
);
3148 MSSAU
->applyUpdates(DTUpdates
, DT
);
3149 L
.addBasicBlockToLoop(CheckBlock
, LI
);
3154 if (MSSAU
&& VerifyMemorySSA
)
3155 MSSAU
->getMemorySSA()->verifyMemorySSA();
3158 // TODO: In fact, cost of unswitching a new invariant candidate is *slightly*
3159 // higher because we have just inserted a new block. Need to think how to
3160 // adjust the cost of injected candidates when it was first computed.
3161 LLVM_DEBUG(dbgs() << "Injected a new loop-invariant branch " << *InvariantBr
3162 << " and considering it for unswitching.");
3163 ++NumInvariantConditionsInjected
;
3164 return NonTrivialUnswitchCandidate(InvariantBr
, { InjectedCond
},
3168 /// Given chain of loop branch conditions looking like:
3169 /// br (Variant < Invariant1)
3170 /// br (Variant < Invariant2)
3171 /// br (Variant < Invariant3)
3173 /// collect set of invariant conditions on which we want to unswitch, which
3175 /// Invariant1 <= Invariant2
3176 /// Invariant2 <= Invariant3
3178 /// Though they might not immediately exist in the IR, we can still inject them.
3179 static bool insertCandidatesWithPendingInjections(
3180 SmallVectorImpl
<NonTrivialUnswitchCandidate
> &UnswitchCandidates
, Loop
&L
,
3181 ICmpInst::Predicate Pred
, ArrayRef
<CompareDesc
> Compares
,
3182 const DominatorTree
&DT
) {
3184 assert(ICmpInst::isRelational(Pred
));
3185 assert(ICmpInst::isStrictPredicate(Pred
));
3186 if (Compares
.size() < 2)
3188 ICmpInst::Predicate NonStrictPred
= ICmpInst::getNonStrictPredicate(Pred
);
3189 for (auto Prev
= Compares
.begin(), Next
= Compares
.begin() + 1;
3190 Next
!= Compares
.end(); ++Prev
, ++Next
) {
3191 Value
*LHS
= Next
->Invariant
;
3192 Value
*RHS
= Prev
->Invariant
;
3193 BasicBlock
*InLoopSucc
= Prev
->InLoopSucc
;
3194 InjectedInvariant
ToInject(NonStrictPred
, LHS
, RHS
, InLoopSucc
);
3195 NonTrivialUnswitchCandidate
Candidate(Prev
->Term
, { LHS
, RHS
},
3196 std::nullopt
, std::move(ToInject
));
3197 UnswitchCandidates
.push_back(std::move(Candidate
));
3202 /// Collect unswitch candidates by invariant conditions that are not immediately
3203 /// present in the loop. However, they can be injected into the code if we
3204 /// decide it's profitable.
3205 /// An example of such conditions is following:
3209 /// if (! x <u C1) break;
3210 /// if (! x <u C2) break;
3214 /// We can unswitch by condition "C1 <=u C2". If that is true, then "x <u C1 <=
3215 /// C2" automatically implies "x <u C2", so we can get rid of one of
3216 /// loop-variant checks in unswitched loop version.
3217 static bool collectUnswitchCandidatesWithInjections(
3218 SmallVectorImpl
<NonTrivialUnswitchCandidate
> &UnswitchCandidates
,
3219 IVConditionInfo
&PartialIVInfo
, Instruction
*&PartialIVCondBranch
, Loop
&L
,
3220 const DominatorTree
&DT
, const LoopInfo
&LI
, AAResults
&AA
,
3221 const MemorySSAUpdater
*MSSAU
) {
3222 if (!InjectInvariantConditions
)
3225 if (!DT
.isReachableFromEntry(L
.getHeader()))
3227 auto *Latch
= L
.getLoopLatch();
3228 // Need to have a single latch and a preheader.
3231 assert(L
.getLoopPreheader() && "Must have a preheader!");
3233 DenseMap
<Value
*, SmallVector
<CompareDesc
, 4> > CandidatesULT
;
3234 // Traverse the conditions that dominate latch (and therefore dominate each
3236 for (auto *DTN
= DT
.getNode(Latch
); L
.contains(DTN
->getBlock());
3237 DTN
= DTN
->getIDom()) {
3238 ICmpInst::Predicate Pred
;
3239 Value
*LHS
= nullptr, *RHS
= nullptr;
3240 BasicBlock
*IfTrue
= nullptr, *IfFalse
= nullptr;
3241 auto *BB
= DTN
->getBlock();
3242 // Ignore inner loops.
3243 if (LI
.getLoopFor(BB
) != &L
)
3245 auto *Term
= BB
->getTerminator();
3246 if (!match(Term
, m_Br(m_ICmp(Pred
, m_Value(LHS
), m_Value(RHS
)),
3247 m_BasicBlock(IfTrue
), m_BasicBlock(IfFalse
))))
3249 if (!LHS
->getType()->isIntegerTy())
3251 canonicalizeForInvariantConditionInjection(Pred
, LHS
, RHS
, IfTrue
, IfFalse
,
3253 if (!shouldTryInjectInvariantCondition(Pred
, LHS
, RHS
, IfTrue
, IfFalse
, L
))
3255 if (!shouldTryInjectBasingOnMetadata(cast
<BranchInst
>(Term
), IfTrue
))
3257 // Strip ZEXT for unsigned predicate.
3258 // TODO: once signed predicates are supported, also strip SEXT.
3259 CompareDesc
Desc(cast
<BranchInst
>(Term
), RHS
, IfTrue
);
3260 while (auto *Zext
= dyn_cast
<ZExtInst
>(LHS
))
3261 LHS
= Zext
->getOperand(0);
3262 CandidatesULT
[LHS
].push_back(Desc
);
3266 for (auto &It
: CandidatesULT
)
3267 Found
|= insertCandidatesWithPendingInjections(
3268 UnswitchCandidates
, L
, ICmpInst::ICMP_ULT
, It
.second
, DT
);
3272 static bool isSafeForNoNTrivialUnswitching(Loop
&L
, LoopInfo
&LI
) {
3273 if (!L
.isSafeToClone())
3275 for (auto *BB
: L
.blocks())
3276 for (auto &I
: *BB
) {
3277 if (I
.getType()->isTokenTy() && I
.isUsedOutsideOfBlock(BB
))
3279 if (auto *CB
= dyn_cast
<CallBase
>(&I
)) {
3280 assert(!CB
->cannotDuplicate() && "Checked by L.isSafeToClone().");
3281 if (CB
->isConvergent())
3286 // Check if there are irreducible CFG cycles in this loop. If so, we cannot
3287 // easily unswitch non-trivial edges out of the loop. Doing so might turn the
3288 // irreducible control flow into reducible control flow and introduce new
3289 // loops "out of thin air". If we ever discover important use cases for doing
3290 // this, we can add support to loop unswitch, but it is a lot of complexity
3291 // for what seems little or no real world benefit.
3292 LoopBlocksRPO
RPOT(&L
);
3294 if (containsIrreducibleCFG
<const BasicBlock
*>(RPOT
, LI
))
3297 SmallVector
<BasicBlock
*, 4> ExitBlocks
;
3298 L
.getUniqueExitBlocks(ExitBlocks
);
3299 // We cannot unswitch if exit blocks contain a cleanuppad/catchswitch
3300 // instruction as we don't know how to split those exit blocks.
3301 // FIXME: We should teach SplitBlock to handle this and remove this
3303 for (auto *ExitBB
: ExitBlocks
) {
3304 auto *I
= ExitBB
->getFirstNonPHI();
3305 if (isa
<CleanupPadInst
>(I
) || isa
<CatchSwitchInst
>(I
)) {
3306 LLVM_DEBUG(dbgs() << "Cannot unswitch because of cleanuppad/catchswitch "
3315 static NonTrivialUnswitchCandidate
findBestNonTrivialUnswitchCandidate(
3316 ArrayRef
<NonTrivialUnswitchCandidate
> UnswitchCandidates
, const Loop
&L
,
3317 const DominatorTree
&DT
, const LoopInfo
&LI
, AssumptionCache
&AC
,
3318 const TargetTransformInfo
&TTI
, const IVConditionInfo
&PartialIVInfo
) {
3319 // Given that unswitching these terminators will require duplicating parts of
3320 // the loop, so we need to be able to model that cost. Compute the ephemeral
3321 // values and set up a data structure to hold per-BB costs. We cache each
3322 // block's cost so that we don't recompute this when considering different
3323 // subsets of the loop for duplication during unswitching.
3324 SmallPtrSet
<const Value
*, 4> EphValues
;
3325 CodeMetrics::collectEphemeralValues(&L
, &AC
, EphValues
);
3326 SmallDenseMap
<BasicBlock
*, InstructionCost
, 4> BBCostMap
;
3328 // Compute the cost of each block, as well as the total loop cost. Also, bail
3329 // out if we see instructions which are incompatible with loop unswitching
3330 // (convergent, noduplicate, or cross-basic-block tokens).
3331 // FIXME: We might be able to safely handle some of these in non-duplicated
3333 TargetTransformInfo::TargetCostKind CostKind
=
3334 L
.getHeader()->getParent()->hasMinSize()
3335 ? TargetTransformInfo::TCK_CodeSize
3336 : TargetTransformInfo::TCK_SizeAndLatency
;
3337 InstructionCost LoopCost
= 0;
3338 for (auto *BB
: L
.blocks()) {
3339 InstructionCost Cost
= 0;
3340 for (auto &I
: *BB
) {
3341 if (EphValues
.count(&I
))
3343 Cost
+= TTI
.getInstructionCost(&I
, CostKind
);
3345 assert(Cost
>= 0 && "Must not have negative costs!");
3347 assert(LoopCost
>= 0 && "Must not have negative loop costs!");
3348 BBCostMap
[BB
] = Cost
;
3350 LLVM_DEBUG(dbgs() << " Total loop cost: " << LoopCost
<< "\n");
3352 // Now we find the best candidate by searching for the one with the following
3353 // properties in order:
3355 // 1) An unswitching cost below the threshold
3356 // 2) The smallest number of duplicated unswitch candidates (to avoid
3357 // creating redundant subsequent unswitching)
3358 // 3) The smallest cost after unswitching.
3360 // We prioritize reducing fanout of unswitch candidates provided the cost
3361 // remains below the threshold because this has a multiplicative effect.
3363 // This requires memoizing each dominator subtree to avoid redundant work.
3365 // FIXME: Need to actually do the number of candidates part above.
3366 SmallDenseMap
<DomTreeNode
*, InstructionCost
, 4> DTCostMap
;
3367 // Given a terminator which might be unswitched, computes the non-duplicated
3368 // cost for that terminator.
3369 auto ComputeUnswitchedCost
= [&](Instruction
&TI
,
3370 bool FullUnswitch
) -> InstructionCost
{
3371 // Unswitching selects unswitches the entire loop.
3372 if (isa
<SelectInst
>(TI
))
3375 BasicBlock
&BB
= *TI
.getParent();
3376 SmallPtrSet
<BasicBlock
*, 4> Visited
;
3378 InstructionCost Cost
= 0;
3379 for (BasicBlock
*SuccBB
: successors(&BB
)) {
3380 // Don't count successors more than once.
3381 if (!Visited
.insert(SuccBB
).second
)
3384 // If this is a partial unswitch candidate, then it must be a conditional
3385 // branch with a condition of either `or`, `and`, their corresponding
3386 // select forms or partially invariant instructions. In that case, one of
3387 // the successors is necessarily duplicated, so don't even try to remove
3389 if (!FullUnswitch
) {
3390 auto &BI
= cast
<BranchInst
>(TI
);
3391 Value
*Cond
= skipTrivialSelect(BI
.getCondition());
3392 if (match(Cond
, m_LogicalAnd())) {
3393 if (SuccBB
== BI
.getSuccessor(1))
3395 } else if (match(Cond
, m_LogicalOr())) {
3396 if (SuccBB
== BI
.getSuccessor(0))
3398 } else if ((PartialIVInfo
.KnownValue
->isOneValue() &&
3399 SuccBB
== BI
.getSuccessor(0)) ||
3400 (!PartialIVInfo
.KnownValue
->isOneValue() &&
3401 SuccBB
== BI
.getSuccessor(1)))
3405 // This successor's domtree will not need to be duplicated after
3406 // unswitching if the edge to the successor dominates it (and thus the
3407 // entire tree). This essentially means there is no other path into this
3408 // subtree and so it will end up live in only one clone of the loop.
3409 if (SuccBB
->getUniquePredecessor() ||
3410 llvm::all_of(predecessors(SuccBB
), [&](BasicBlock
*PredBB
) {
3411 return PredBB
== &BB
|| DT
.dominates(SuccBB
, PredBB
);
3413 Cost
+= computeDomSubtreeCost(*DT
[SuccBB
], BBCostMap
, DTCostMap
);
3414 assert(Cost
<= LoopCost
&&
3415 "Non-duplicated cost should never exceed total loop cost!");
3419 // Now scale the cost by the number of unique successors minus one. We
3420 // subtract one because there is already at least one copy of the entire
3421 // loop. This is computing the new cost of unswitching a condition.
3422 // Note that guards always have 2 unique successors that are implicit and
3423 // will be materialized if we decide to unswitch it.
3424 int SuccessorsCount
= isGuard(&TI
) ? 2 : Visited
.size();
3425 assert(SuccessorsCount
> 1 &&
3426 "Cannot unswitch a condition without multiple distinct successors!");
3427 return (LoopCost
- Cost
) * (SuccessorsCount
- 1);
3430 std::optional
<NonTrivialUnswitchCandidate
> Best
;
3431 for (auto &Candidate
: UnswitchCandidates
) {
3432 Instruction
&TI
= *Candidate
.TI
;
3433 ArrayRef
<Value
*> Invariants
= Candidate
.Invariants
;
3434 BranchInst
*BI
= dyn_cast
<BranchInst
>(&TI
);
3436 !BI
|| Candidate
.hasPendingInjection() ||
3437 (Invariants
.size() == 1 &&
3438 Invariants
[0] == skipTrivialSelect(BI
->getCondition()));
3439 InstructionCost CandidateCost
= ComputeUnswitchedCost(TI
, FullUnswitch
);
3440 // Calculate cost multiplier which is a tool to limit potentially
3441 // exponential behavior of loop-unswitch.
3442 if (EnableUnswitchCostMultiplier
) {
3443 int CostMultiplier
=
3444 CalculateUnswitchCostMultiplier(TI
, L
, LI
, DT
, UnswitchCandidates
);
3446 (CostMultiplier
> 0 && CostMultiplier
<= UnswitchThreshold
) &&
3447 "cost multiplier needs to be in the range of 1..UnswitchThreshold");
3448 CandidateCost
*= CostMultiplier
;
3449 LLVM_DEBUG(dbgs() << " Computed cost of " << CandidateCost
3450 << " (multiplier: " << CostMultiplier
<< ")"
3451 << " for unswitch candidate: " << TI
<< "\n");
3453 LLVM_DEBUG(dbgs() << " Computed cost of " << CandidateCost
3454 << " for unswitch candidate: " << TI
<< "\n");
3457 if (!Best
|| CandidateCost
< Best
->Cost
) {
3459 Best
->Cost
= CandidateCost
;
3462 assert(Best
&& "Must be!");
3466 // Insert a freeze on an unswitched branch if all is true:
3467 // 1. freeze-loop-unswitch-cond option is true
3468 // 2. The branch may not execute in the loop pre-transformation. If a branch may
3469 // not execute and could cause UB, it would always cause UB if it is hoisted outside
3470 // of the loop. Insert a freeze to prevent this case.
3471 // 3. The branch condition may be poison or undef
3472 static bool shouldInsertFreeze(Loop
&L
, Instruction
&TI
, DominatorTree
&DT
,
3473 AssumptionCache
&AC
) {
3474 assert(isa
<BranchInst
>(TI
) || isa
<SwitchInst
>(TI
));
3475 if (!FreezeLoopUnswitchCond
)
3478 ICFLoopSafetyInfo SafetyInfo
;
3479 SafetyInfo
.computeLoopSafetyInfo(&L
);
3480 if (SafetyInfo
.isGuaranteedToExecute(TI
, &DT
, &L
))
3484 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(&TI
))
3485 Cond
= skipTrivialSelect(BI
->getCondition());
3487 Cond
= skipTrivialSelect(cast
<SwitchInst
>(&TI
)->getCondition());
3488 return !isGuaranteedNotToBeUndefOrPoison(
3489 Cond
, &AC
, L
.getLoopPreheader()->getTerminator(), &DT
);
3492 static bool unswitchBestCondition(Loop
&L
, DominatorTree
&DT
, LoopInfo
&LI
,
3493 AssumptionCache
&AC
, AAResults
&AA
,
3494 TargetTransformInfo
&TTI
, ScalarEvolution
*SE
,
3495 MemorySSAUpdater
*MSSAU
,
3496 LPMUpdater
&LoopUpdater
) {
3497 // Collect all invariant conditions within this loop (as opposed to an inner
3498 // loop which would be handled when visiting that inner loop).
3499 SmallVector
<NonTrivialUnswitchCandidate
, 4> UnswitchCandidates
;
3500 IVConditionInfo PartialIVInfo
;
3501 Instruction
*PartialIVCondBranch
= nullptr;
3502 collectUnswitchCandidates(UnswitchCandidates
, PartialIVInfo
,
3503 PartialIVCondBranch
, L
, LI
, AA
, MSSAU
);
3504 if (!findOptionMDForLoop(&L
, "llvm.loop.unswitch.injection.disable"))
3505 collectUnswitchCandidatesWithInjections(UnswitchCandidates
, PartialIVInfo
,
3506 PartialIVCondBranch
, L
, DT
, LI
, AA
,
3508 // If we didn't find any candidates, we're done.
3509 if (UnswitchCandidates
.empty())
3513 dbgs() << "Considering " << UnswitchCandidates
.size()
3514 << " non-trivial loop invariant conditions for unswitching.\n");
3516 NonTrivialUnswitchCandidate Best
= findBestNonTrivialUnswitchCandidate(
3517 UnswitchCandidates
, L
, DT
, LI
, AC
, TTI
, PartialIVInfo
);
3519 assert(Best
.TI
&& "Failed to find loop unswitch candidate");
3520 assert(Best
.Cost
&& "Failed to compute cost");
3522 if (*Best
.Cost
>= UnswitchThreshold
) {
3523 LLVM_DEBUG(dbgs() << "Cannot unswitch, lowest cost found: " << *Best
.Cost
3528 bool InjectedCondition
= false;
3529 if (Best
.hasPendingInjection()) {
3530 Best
= injectPendingInvariantConditions(Best
, L
, DT
, LI
, AC
, MSSAU
);
3531 InjectedCondition
= true;
3533 assert(!Best
.hasPendingInjection() &&
3534 "All injections should have been done by now!");
3536 if (Best
.TI
!= PartialIVCondBranch
)
3537 PartialIVInfo
.InstToDuplicate
.clear();
3540 if (auto *SI
= dyn_cast
<SelectInst
>(Best
.TI
)) {
3541 // If the best candidate is a select, turn it into a branch. Select
3542 // instructions with a poison conditional do not propagate poison, but
3543 // branching on poison causes UB. Insert a freeze on the select
3544 // conditional to prevent UB after turning the select into a branch.
3545 InsertFreeze
= !isGuaranteedNotToBeUndefOrPoison(
3546 SI
->getCondition(), &AC
, L
.getLoopPreheader()->getTerminator(), &DT
);
3547 Best
.TI
= turnSelectIntoBranch(SI
, DT
, LI
, MSSAU
, &AC
);
3549 // If the best candidate is a guard, turn it into a branch.
3550 if (isGuard(Best
.TI
))
3552 turnGuardIntoBranch(cast
<IntrinsicInst
>(Best
.TI
), L
, DT
, LI
, MSSAU
);
3553 InsertFreeze
= shouldInsertFreeze(L
, *Best
.TI
, DT
, AC
);
3556 LLVM_DEBUG(dbgs() << " Unswitching non-trivial (cost = " << Best
.Cost
3557 << ") terminator: " << *Best
.TI
<< "\n");
3558 unswitchNontrivialInvariants(L
, *Best
.TI
, Best
.Invariants
, PartialIVInfo
, DT
,
3559 LI
, AC
, SE
, MSSAU
, LoopUpdater
, InsertFreeze
,
3564 /// Unswitch control flow predicated on loop invariant conditions.
3566 /// This first hoists all branches or switches which are trivial (IE, do not
3567 /// require duplicating any part of the loop) out of the loop body. It then
3568 /// looks at other loop invariant control flows and tries to unswitch those as
3569 /// well by cloning the loop if the result is small enough.
3571 /// The `DT`, `LI`, `AC`, `AA`, `TTI` parameters are required analyses that are
3572 /// also updated based on the unswitch. The `MSSA` analysis is also updated if
3573 /// valid (i.e. its use is enabled).
3575 /// If either `NonTrivial` is true or the flag `EnableNonTrivialUnswitch` is
3576 /// true, we will attempt to do non-trivial unswitching as well as trivial
3579 /// The `postUnswitch` function will be run after unswitching is complete
3580 /// with information on whether or not the provided loop remains a loop and
3581 /// a list of new sibling loops created.
3583 /// If `SE` is non-null, we will update that analysis based on the unswitching
3585 static bool unswitchLoop(Loop
&L
, DominatorTree
&DT
, LoopInfo
&LI
,
3586 AssumptionCache
&AC
, AAResults
&AA
,
3587 TargetTransformInfo
&TTI
, bool Trivial
,
3588 bool NonTrivial
, ScalarEvolution
*SE
,
3589 MemorySSAUpdater
*MSSAU
, ProfileSummaryInfo
*PSI
,
3590 BlockFrequencyInfo
*BFI
, LPMUpdater
&LoopUpdater
) {
3591 assert(L
.isRecursivelyLCSSAForm(DT
, LI
) &&
3592 "Loops must be in LCSSA form before unswitching.");
3594 // Must be in loop simplified form: we need a preheader and dedicated exits.
3595 if (!L
.isLoopSimplifyForm())
3598 // Try trivial unswitch first before loop over other basic blocks in the loop.
3599 if (Trivial
&& unswitchAllTrivialConditions(L
, DT
, LI
, SE
, MSSAU
)) {
3600 // If we unswitched successfully we will want to clean up the loop before
3601 // processing it further so just mark it as unswitched and return.
3602 postUnswitch(L
, LoopUpdater
, L
.getName(),
3603 /*CurrentLoopValid*/ true, /*PartiallyInvariant*/ false,
3604 /*InjectedCondition*/ false, {});
3608 const Function
*F
= L
.getHeader()->getParent();
3610 // Check whether we should continue with non-trivial conditions.
3611 // EnableNonTrivialUnswitch: Global variable that forces non-trivial
3612 // unswitching for testing and debugging.
3613 // NonTrivial: Parameter that enables non-trivial unswitching for this
3614 // invocation of the transform. But this should be allowed only
3615 // for targets without branch divergence.
3617 // FIXME: If divergence analysis becomes available to a loop
3618 // transform, we should allow unswitching for non-trivial uniform
3619 // branches even on targets that have divergence.
3620 // https://bugs.llvm.org/show_bug.cgi?id=48819
3621 bool ContinueWithNonTrivial
=
3622 EnableNonTrivialUnswitch
|| (NonTrivial
&& !TTI
.hasBranchDivergence(F
));
3623 if (!ContinueWithNonTrivial
)
3626 // Skip non-trivial unswitching for optsize functions.
3627 if (F
->hasOptSize())
3630 // Returns true if Loop L's loop nest is cold, i.e. if the headers of L,
3631 // of the loops L is nested in, and of the loops nested in L are all cold.
3632 auto IsLoopNestCold
= [&](const Loop
*L
) {
3633 // Check L and all of its parent loops.
3636 if (!PSI
->isColdBlock(Parent
->getHeader(), BFI
))
3638 Parent
= Parent
->getParentLoop();
3640 // Next check all loops nested within L.
3641 SmallVector
<const Loop
*, 4> Worklist
;
3642 Worklist
.insert(Worklist
.end(), L
->getSubLoops().begin(),
3643 L
->getSubLoops().end());
3644 while (!Worklist
.empty()) {
3645 auto *CurLoop
= Worklist
.pop_back_val();
3646 if (!PSI
->isColdBlock(CurLoop
->getHeader(), BFI
))
3648 Worklist
.insert(Worklist
.end(), CurLoop
->getSubLoops().begin(),
3649 CurLoop
->getSubLoops().end());
3654 // Skip cold loops in cold loop nests, as unswitching them brings little
3655 // benefit but increases the code size
3656 if (PSI
&& PSI
->hasProfileSummary() && BFI
&& IsLoopNestCold(&L
)) {
3657 LLVM_DEBUG(dbgs() << " Skip cold loop: " << L
<< "\n");
3661 // Perform legality checks.
3662 if (!isSafeForNoNTrivialUnswitching(L
, LI
))
3665 // For non-trivial unswitching, because it often creates new loops, we rely on
3666 // the pass manager to iterate on the loops rather than trying to immediately
3667 // reach a fixed point. There is no substantial advantage to iterating
3668 // internally, and if any of the new loops are simplified enough to contain
3669 // trivial unswitching we want to prefer those.
3671 // Try to unswitch the best invariant condition. We prefer this full unswitch to
3672 // a partial unswitch when possible below the threshold.
3673 if (unswitchBestCondition(L
, DT
, LI
, AC
, AA
, TTI
, SE
, MSSAU
, LoopUpdater
))
3676 // No other opportunities to unswitch.
3680 PreservedAnalyses
SimpleLoopUnswitchPass::run(Loop
&L
, LoopAnalysisManager
&AM
,
3681 LoopStandardAnalysisResults
&AR
,
3683 Function
&F
= *L
.getHeader()->getParent();
3685 ProfileSummaryInfo
*PSI
= nullptr;
3686 if (auto OuterProxy
=
3687 AM
.getResult
<FunctionAnalysisManagerLoopProxy
>(L
, AR
)
3688 .getCachedResult
<ModuleAnalysisManagerFunctionProxy
>(F
))
3689 PSI
= OuterProxy
->getCachedResult
<ProfileSummaryAnalysis
>(*F
.getParent());
3690 LLVM_DEBUG(dbgs() << "Unswitching loop in " << F
.getName() << ": " << L
3693 std::optional
<MemorySSAUpdater
> MSSAU
;
3695 MSSAU
= MemorySSAUpdater(AR
.MSSA
);
3696 if (VerifyMemorySSA
)
3697 AR
.MSSA
->verifyMemorySSA();
3699 if (!unswitchLoop(L
, AR
.DT
, AR
.LI
, AR
.AC
, AR
.AA
, AR
.TTI
, Trivial
, NonTrivial
,
3700 &AR
.SE
, MSSAU
? &*MSSAU
: nullptr, PSI
, AR
.BFI
, U
))
3701 return PreservedAnalyses::all();
3703 if (AR
.MSSA
&& VerifyMemorySSA
)
3704 AR
.MSSA
->verifyMemorySSA();
3706 // Historically this pass has had issues with the dominator tree so verify it
3707 // in asserts builds.
3708 assert(AR
.DT
.verify(DominatorTree::VerificationLevel::Fast
));
3710 auto PA
= getLoopPassPreservedAnalyses();
3712 PA
.preserve
<MemorySSAAnalysis
>();
3716 void SimpleLoopUnswitchPass::printPipeline(
3717 raw_ostream
&OS
, function_ref
<StringRef(StringRef
)> MapClassName2PassName
) {
3718 static_cast<PassInfoMixin
<SimpleLoopUnswitchPass
> *>(this)->printPipeline(
3719 OS
, MapClassName2PassName
);
3722 OS
<< (NonTrivial
? "" : "no-") << "nontrivial;";
3723 OS
<< (Trivial
? "" : "no-") << "trivial";