1 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
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 // Peephole optimize the CFG.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/Sequence.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Analysis/AssumptionCache.h"
26 #include "llvm/Analysis/CaptureTracking.h"
27 #include "llvm/Analysis/ConstantFolding.h"
28 #include "llvm/Analysis/DomTreeUpdater.h"
29 #include "llvm/Analysis/GuardUtils.h"
30 #include "llvm/Analysis/InstructionSimplify.h"
31 #include "llvm/Analysis/MemorySSA.h"
32 #include "llvm/Analysis/MemorySSAUpdater.h"
33 #include "llvm/Analysis/TargetTransformInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/Attributes.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/Constant.h"
39 #include "llvm/IR/ConstantRange.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DataLayout.h"
42 #include "llvm/IR/DebugInfo.h"
43 #include "llvm/IR/DerivedTypes.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GlobalValue.h"
46 #include "llvm/IR/GlobalVariable.h"
47 #include "llvm/IR/IRBuilder.h"
48 #include "llvm/IR/InstrTypes.h"
49 #include "llvm/IR/Instruction.h"
50 #include "llvm/IR/Instructions.h"
51 #include "llvm/IR/IntrinsicInst.h"
52 #include "llvm/IR/LLVMContext.h"
53 #include "llvm/IR/MDBuilder.h"
54 #include "llvm/IR/Metadata.h"
55 #include "llvm/IR/Module.h"
56 #include "llvm/IR/NoFolder.h"
57 #include "llvm/IR/Operator.h"
58 #include "llvm/IR/PatternMatch.h"
59 #include "llvm/IR/ProfDataUtils.h"
60 #include "llvm/IR/Type.h"
61 #include "llvm/IR/Use.h"
62 #include "llvm/IR/User.h"
63 #include "llvm/IR/Value.h"
64 #include "llvm/IR/ValueHandle.h"
65 #include "llvm/Support/BranchProbability.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CommandLine.h"
68 #include "llvm/Support/Debug.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/KnownBits.h"
71 #include "llvm/Support/MathExtras.h"
72 #include "llvm/Support/raw_ostream.h"
73 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
74 #include "llvm/Transforms/Utils/Local.h"
75 #include "llvm/Transforms/Utils/ValueMapper.h"
90 using namespace PatternMatch
;
92 #define DEBUG_TYPE "simplifycfg"
94 cl::opt
<bool> llvm::RequireAndPreserveDomTree(
95 "simplifycfg-require-and-preserve-domtree", cl::Hidden
,
97 cl::desc("Temorary development switch used to gradually uplift SimplifyCFG "
98 "into preserving DomTree,"));
100 // Chosen as 2 so as to be cheap, but still to have enough power to fold
101 // a select, so the "clamp" idiom (of a min followed by a max) will be caught.
102 // To catch this, we need to fold a compare and a select, hence '2' being the
103 // minimum reasonable default.
104 static cl::opt
<unsigned> PHINodeFoldingThreshold(
105 "phi-node-folding-threshold", cl::Hidden
, cl::init(2),
107 "Control the amount of phi node folding to perform (default = 2)"));
109 static cl::opt
<unsigned> TwoEntryPHINodeFoldingThreshold(
110 "two-entry-phi-node-folding-threshold", cl::Hidden
, cl::init(4),
111 cl::desc("Control the maximal total instruction cost that we are willing "
112 "to speculatively execute to fold a 2-entry PHI node into a "
113 "select (default = 4)"));
116 HoistCommon("simplifycfg-hoist-common", cl::Hidden
, cl::init(true),
117 cl::desc("Hoist common instructions up to the parent block"));
119 static cl::opt
<unsigned>
120 HoistCommonSkipLimit("simplifycfg-hoist-common-skip-limit", cl::Hidden
,
122 cl::desc("Allow reordering across at most this many "
123 "instructions when hoisting"));
126 SinkCommon("simplifycfg-sink-common", cl::Hidden
, cl::init(true),
127 cl::desc("Sink common instructions down to the end block"));
129 static cl::opt
<bool> HoistCondStores(
130 "simplifycfg-hoist-cond-stores", cl::Hidden
, cl::init(true),
131 cl::desc("Hoist conditional stores if an unconditional store precedes"));
133 static cl::opt
<bool> MergeCondStores(
134 "simplifycfg-merge-cond-stores", cl::Hidden
, cl::init(true),
135 cl::desc("Hoist conditional stores even if an unconditional store does not "
136 "precede - hoist multiple conditional stores into a single "
137 "predicated store"));
139 static cl::opt
<bool> MergeCondStoresAggressively(
140 "simplifycfg-merge-cond-stores-aggressively", cl::Hidden
, cl::init(false),
141 cl::desc("When merging conditional stores, do so even if the resultant "
142 "basic blocks are unlikely to be if-converted as a result"));
144 static cl::opt
<bool> SpeculateOneExpensiveInst(
145 "speculate-one-expensive-inst", cl::Hidden
, cl::init(true),
146 cl::desc("Allow exactly one expensive instruction to be speculatively "
149 static cl::opt
<unsigned> MaxSpeculationDepth(
150 "max-speculation-depth", cl::Hidden
, cl::init(10),
151 cl::desc("Limit maximum recursion depth when calculating costs of "
152 "speculatively executed instructions"));
155 MaxSmallBlockSize("simplifycfg-max-small-block-size", cl::Hidden
,
157 cl::desc("Max size of a block which is still considered "
158 "small enough to thread through"));
160 // Two is chosen to allow one negation and a logical combine.
161 static cl::opt
<unsigned>
162 BranchFoldThreshold("simplifycfg-branch-fold-threshold", cl::Hidden
,
164 cl::desc("Maximum cost of combining conditions when "
165 "folding branches"));
167 static cl::opt
<unsigned> BranchFoldToCommonDestVectorMultiplier(
168 "simplifycfg-branch-fold-common-dest-vector-multiplier", cl::Hidden
,
170 cl::desc("Multiplier to apply to threshold when determining whether or not "
171 "to fold branch to common destination when vector operations are "
174 static cl::opt
<bool> EnableMergeCompatibleInvokes(
175 "simplifycfg-merge-compatible-invokes", cl::Hidden
, cl::init(true),
176 cl::desc("Allow SimplifyCFG to merge invokes together when appropriate"));
178 static cl::opt
<unsigned> MaxSwitchCasesPerResult(
179 "max-switch-cases-per-result", cl::Hidden
, cl::init(16),
180 cl::desc("Limit cases to analyze when converting a switch to select"));
182 STATISTIC(NumBitMaps
, "Number of switch instructions turned into bitmaps");
183 STATISTIC(NumLinearMaps
,
184 "Number of switch instructions turned into linear mapping");
185 STATISTIC(NumLookupTables
,
186 "Number of switch instructions turned into lookup tables");
188 NumLookupTablesHoles
,
189 "Number of switch instructions turned into lookup tables (holes checked)");
190 STATISTIC(NumTableCmpReuses
, "Number of reused switch table lookup compares");
191 STATISTIC(NumFoldValueComparisonIntoPredecessors
,
192 "Number of value comparisons folded into predecessor basic blocks");
193 STATISTIC(NumFoldBranchToCommonDest
,
194 "Number of branches folded into predecessor basic block");
197 "Number of common instruction 'blocks' hoisted up to the begin block");
198 STATISTIC(NumHoistCommonInstrs
,
199 "Number of common instructions hoisted up to the begin block");
200 STATISTIC(NumSinkCommonCode
,
201 "Number of common instruction 'blocks' sunk down to the end block");
202 STATISTIC(NumSinkCommonInstrs
,
203 "Number of common instructions sunk down to the end block");
204 STATISTIC(NumSpeculations
, "Number of speculative executed instructions");
205 STATISTIC(NumInvokes
,
206 "Number of invokes with empty resume blocks simplified into calls");
207 STATISTIC(NumInvokesMerged
, "Number of invokes that were merged together");
208 STATISTIC(NumInvokeSetsFormed
, "Number of invoke sets that were formed");
212 // The first field contains the value that the switch produces when a certain
213 // case group is selected, and the second field is a vector containing the
214 // cases composing the case group.
215 using SwitchCaseResultVectorTy
=
216 SmallVector
<std::pair
<Constant
*, SmallVector
<ConstantInt
*, 4>>, 2>;
218 // The first field contains the phi node that generates a result of the switch
219 // and the second field contains the value generated for a certain case in the
220 // switch for that PHI.
221 using SwitchCaseResultsTy
= SmallVector
<std::pair
<PHINode
*, Constant
*>, 4>;
223 /// ValueEqualityComparisonCase - Represents a case of a switch.
224 struct ValueEqualityComparisonCase
{
228 ValueEqualityComparisonCase(ConstantInt
*Value
, BasicBlock
*Dest
)
229 : Value(Value
), Dest(Dest
) {}
231 bool operator<(ValueEqualityComparisonCase RHS
) const {
232 // Comparing pointers is ok as we only rely on the order for uniquing.
233 return Value
< RHS
.Value
;
236 bool operator==(BasicBlock
*RHSDest
) const { return Dest
== RHSDest
; }
239 class SimplifyCFGOpt
{
240 const TargetTransformInfo
&TTI
;
242 const DataLayout
&DL
;
243 ArrayRef
<WeakVH
> LoopHeaders
;
244 const SimplifyCFGOptions
&Options
;
247 Value
*isValueEqualityComparison(Instruction
*TI
);
248 BasicBlock
*GetValueEqualityComparisonCases(
249 Instruction
*TI
, std::vector
<ValueEqualityComparisonCase
> &Cases
);
250 bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction
*TI
,
252 IRBuilder
<> &Builder
);
253 bool PerformValueComparisonIntoPredecessorFolding(Instruction
*TI
, Value
*&CV
,
255 IRBuilder
<> &Builder
);
256 bool FoldValueComparisonIntoPredecessors(Instruction
*TI
,
257 IRBuilder
<> &Builder
);
259 bool simplifyResume(ResumeInst
*RI
, IRBuilder
<> &Builder
);
260 bool simplifySingleResume(ResumeInst
*RI
);
261 bool simplifyCommonResume(ResumeInst
*RI
);
262 bool simplifyCleanupReturn(CleanupReturnInst
*RI
);
263 bool simplifyUnreachable(UnreachableInst
*UI
);
264 bool simplifySwitch(SwitchInst
*SI
, IRBuilder
<> &Builder
);
265 bool simplifyIndirectBr(IndirectBrInst
*IBI
);
266 bool simplifyBranch(BranchInst
*Branch
, IRBuilder
<> &Builder
);
267 bool simplifyUncondBranch(BranchInst
*BI
, IRBuilder
<> &Builder
);
268 bool simplifyCondBranch(BranchInst
*BI
, IRBuilder
<> &Builder
);
270 bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst
*ICI
,
271 IRBuilder
<> &Builder
);
273 bool hoistCommonCodeFromSuccessors(BasicBlock
*BB
, bool EqTermsOnly
);
274 bool hoistSuccIdenticalTerminatorToSwitchOrIf(
275 Instruction
*TI
, Instruction
*I1
,
276 SmallVectorImpl
<Instruction
*> &OtherSuccTIs
);
277 bool SpeculativelyExecuteBB(BranchInst
*BI
, BasicBlock
*ThenBB
);
278 bool SimplifyTerminatorOnSelect(Instruction
*OldTerm
, Value
*Cond
,
279 BasicBlock
*TrueBB
, BasicBlock
*FalseBB
,
280 uint32_t TrueWeight
, uint32_t FalseWeight
);
281 bool SimplifyBranchOnICmpChain(BranchInst
*BI
, IRBuilder
<> &Builder
,
282 const DataLayout
&DL
);
283 bool SimplifySwitchOnSelect(SwitchInst
*SI
, SelectInst
*Select
);
284 bool SimplifyIndirectBrOnSelect(IndirectBrInst
*IBI
, SelectInst
*SI
);
285 bool TurnSwitchRangeIntoICmp(SwitchInst
*SI
, IRBuilder
<> &Builder
);
288 SimplifyCFGOpt(const TargetTransformInfo
&TTI
, DomTreeUpdater
*DTU
,
289 const DataLayout
&DL
, ArrayRef
<WeakVH
> LoopHeaders
,
290 const SimplifyCFGOptions
&Opts
)
291 : TTI(TTI
), DTU(DTU
), DL(DL
), LoopHeaders(LoopHeaders
), Options(Opts
) {
292 assert((!DTU
|| !DTU
->hasPostDomTree()) &&
293 "SimplifyCFG is not yet capable of maintaining validity of a "
294 "PostDomTree, so don't ask for it.");
297 bool simplifyOnce(BasicBlock
*BB
);
298 bool run(BasicBlock
*BB
);
300 // Helper to set Resimplify and return change indication.
301 bool requestResimplify() {
307 } // end anonymous namespace
309 /// Return true if all the PHI nodes in the basic block \p BB
310 /// receive compatible (identical) incoming values when coming from
311 /// all of the predecessor blocks that are specified in \p IncomingBlocks.
313 /// Note that if the values aren't exactly identical, but \p EquivalenceSet
314 /// is provided, and *both* of the values are present in the set,
315 /// then they are considered equal.
316 static bool IncomingValuesAreCompatible(
317 BasicBlock
*BB
, ArrayRef
<BasicBlock
*> IncomingBlocks
,
318 SmallPtrSetImpl
<Value
*> *EquivalenceSet
= nullptr) {
319 assert(IncomingBlocks
.size() == 2 &&
320 "Only for a pair of incoming blocks at the time!");
322 // FIXME: it is okay if one of the incoming values is an `undef` value,
323 // iff the other incoming value is guaranteed to be a non-poison value.
324 // FIXME: it is okay if one of the incoming values is a `poison` value.
325 return all_of(BB
->phis(), [IncomingBlocks
, EquivalenceSet
](PHINode
&PN
) {
326 Value
*IV0
= PN
.getIncomingValueForBlock(IncomingBlocks
[0]);
327 Value
*IV1
= PN
.getIncomingValueForBlock(IncomingBlocks
[1]);
330 if (EquivalenceSet
&& EquivalenceSet
->contains(IV0
) &&
331 EquivalenceSet
->contains(IV1
))
337 /// Return true if it is safe to merge these two
338 /// terminator instructions together.
340 SafeToMergeTerminators(Instruction
*SI1
, Instruction
*SI2
,
341 SmallSetVector
<BasicBlock
*, 4> *FailBlocks
= nullptr) {
343 return false; // Can't merge with self!
345 // It is not safe to merge these two switch instructions if they have a common
346 // successor, and if that successor has a PHI node, and if *that* PHI node has
347 // conflicting incoming values from the two switch blocks.
348 BasicBlock
*SI1BB
= SI1
->getParent();
349 BasicBlock
*SI2BB
= SI2
->getParent();
351 SmallPtrSet
<BasicBlock
*, 16> SI1Succs(succ_begin(SI1BB
), succ_end(SI1BB
));
353 for (BasicBlock
*Succ
: successors(SI2BB
)) {
354 if (!SI1Succs
.count(Succ
))
356 if (IncomingValuesAreCompatible(Succ
, {SI1BB
, SI2BB
}))
360 FailBlocks
->insert(Succ
);
368 /// Update PHI nodes in Succ to indicate that there will now be entries in it
369 /// from the 'NewPred' block. The values that will be flowing into the PHI nodes
370 /// will be the same as those coming in from ExistPred, an existing predecessor
372 static void AddPredecessorToBlock(BasicBlock
*Succ
, BasicBlock
*NewPred
,
373 BasicBlock
*ExistPred
,
374 MemorySSAUpdater
*MSSAU
= nullptr) {
375 for (PHINode
&PN
: Succ
->phis())
376 PN
.addIncoming(PN
.getIncomingValueForBlock(ExistPred
), NewPred
);
378 if (auto *MPhi
= MSSAU
->getMemorySSA()->getMemoryAccess(Succ
))
379 MPhi
->addIncoming(MPhi
->getIncomingValueForBlock(ExistPred
), NewPred
);
382 /// Compute an abstract "cost" of speculating the given instruction,
383 /// which is assumed to be safe to speculate. TCC_Free means cheap,
384 /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
386 static InstructionCost
computeSpeculationCost(const User
*I
,
387 const TargetTransformInfo
&TTI
) {
388 assert((!isa
<Instruction
>(I
) ||
389 isSafeToSpeculativelyExecute(cast
<Instruction
>(I
))) &&
390 "Instruction is not safe to speculatively execute!");
391 return TTI
.getInstructionCost(I
, TargetTransformInfo::TCK_SizeAndLatency
);
394 /// If we have a merge point of an "if condition" as accepted above,
395 /// return true if the specified value dominates the block. We
396 /// don't handle the true generality of domination here, just a special case
397 /// which works well enough for us.
399 /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
400 /// see if V (which must be an instruction) and its recursive operands
401 /// that do not dominate BB have a combined cost lower than Budget and
402 /// are non-trapping. If both are true, the instruction is inserted into the
403 /// set and true is returned.
405 /// The cost for most non-trapping instructions is defined as 1 except for
406 /// Select whose cost is 2.
408 /// After this function returns, Cost is increased by the cost of
409 /// V plus its non-dominating operands. If that cost is greater than
410 /// Budget, false is returned and Cost is undefined.
411 static bool dominatesMergePoint(Value
*V
, BasicBlock
*BB
,
412 SmallPtrSetImpl
<Instruction
*> &AggressiveInsts
,
413 InstructionCost
&Cost
,
414 InstructionCost Budget
,
415 const TargetTransformInfo
&TTI
,
416 unsigned Depth
= 0) {
417 // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
418 // so limit the recursion depth.
419 // TODO: While this recursion limit does prevent pathological behavior, it
420 // would be better to track visited instructions to avoid cycles.
421 if (Depth
== MaxSpeculationDepth
)
424 Instruction
*I
= dyn_cast
<Instruction
>(V
);
426 // Non-instructions dominate all instructions and can be executed
430 BasicBlock
*PBB
= I
->getParent();
432 // We don't want to allow weird loops that might have the "if condition" in
433 // the bottom of this block.
437 // If this instruction is defined in a block that contains an unconditional
438 // branch to BB, then it must be in the 'conditional' part of the "if
439 // statement". If not, it definitely dominates the region.
440 BranchInst
*BI
= dyn_cast
<BranchInst
>(PBB
->getTerminator());
441 if (!BI
|| BI
->isConditional() || BI
->getSuccessor(0) != BB
)
444 // If we have seen this instruction before, don't count it again.
445 if (AggressiveInsts
.count(I
))
448 // Okay, it looks like the instruction IS in the "condition". Check to
449 // see if it's a cheap instruction to unconditionally compute, and if it
450 // only uses stuff defined outside of the condition. If so, hoist it out.
451 if (!isSafeToSpeculativelyExecute(I
))
454 Cost
+= computeSpeculationCost(I
, TTI
);
456 // Allow exactly one instruction to be speculated regardless of its cost
457 // (as long as it is safe to do so).
458 // This is intended to flatten the CFG even if the instruction is a division
459 // or other expensive operation. The speculation of an expensive instruction
460 // is expected to be undone in CodeGenPrepare if the speculation has not
461 // enabled further IR optimizations.
463 (!SpeculateOneExpensiveInst
|| !AggressiveInsts
.empty() || Depth
> 0 ||
467 // Okay, we can only really hoist these out if their operands do
468 // not take us over the cost threshold.
469 for (Use
&Op
: I
->operands())
470 if (!dominatesMergePoint(Op
, BB
, AggressiveInsts
, Cost
, Budget
, TTI
,
473 // Okay, it's safe to do this! Remember this instruction.
474 AggressiveInsts
.insert(I
);
478 /// Extract ConstantInt from value, looking through IntToPtr
479 /// and PointerNullValue. Return NULL if value is not a constant int.
480 static ConstantInt
*GetConstantInt(Value
*V
, const DataLayout
&DL
) {
481 // Normal constant int.
482 ConstantInt
*CI
= dyn_cast
<ConstantInt
>(V
);
483 if (CI
|| !isa
<Constant
>(V
) || !V
->getType()->isPointerTy() ||
484 DL
.isNonIntegralPointerType(V
->getType()))
487 // This is some kind of pointer constant. Turn it into a pointer-sized
488 // ConstantInt if possible.
489 IntegerType
*PtrTy
= cast
<IntegerType
>(DL
.getIntPtrType(V
->getType()));
491 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
492 if (isa
<ConstantPointerNull
>(V
))
493 return ConstantInt::get(PtrTy
, 0);
495 // IntToPtr const int.
496 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(V
))
497 if (CE
->getOpcode() == Instruction::IntToPtr
)
498 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(CE
->getOperand(0))) {
499 // The constant is very likely to have the right type already.
500 if (CI
->getType() == PtrTy
)
503 return cast
<ConstantInt
>(
504 ConstantFoldIntegerCast(CI
, PtrTy
, /*isSigned=*/false, DL
));
511 /// Given a chain of or (||) or and (&&) comparison of a value against a
512 /// constant, this will try to recover the information required for a switch
514 /// It will depth-first traverse the chain of comparison, seeking for patterns
515 /// like %a == 12 or %a < 4 and combine them to produce a set of integer
516 /// representing the different cases for the switch.
517 /// Note that if the chain is composed of '||' it will build the set of elements
518 /// that matches the comparisons (i.e. any of this value validate the chain)
519 /// while for a chain of '&&' it will build the set elements that make the test
521 struct ConstantComparesGatherer
{
522 const DataLayout
&DL
;
524 /// Value found for the switch comparison
525 Value
*CompValue
= nullptr;
527 /// Extra clause to be checked before the switch
528 Value
*Extra
= nullptr;
530 /// Set of integers to match in switch
531 SmallVector
<ConstantInt
*, 8> Vals
;
533 /// Number of comparisons matched in the and/or chain
534 unsigned UsedICmps
= 0;
536 /// Construct and compute the result for the comparison instruction Cond
537 ConstantComparesGatherer(Instruction
*Cond
, const DataLayout
&DL
) : DL(DL
) {
541 ConstantComparesGatherer(const ConstantComparesGatherer
&) = delete;
542 ConstantComparesGatherer
&
543 operator=(const ConstantComparesGatherer
&) = delete;
546 /// Try to set the current value used for the comparison, it succeeds only if
547 /// it wasn't set before or if the new value is the same as the old one
548 bool setValueOnce(Value
*NewVal
) {
549 if (CompValue
&& CompValue
!= NewVal
)
552 return (CompValue
!= nullptr);
555 /// Try to match Instruction "I" as a comparison against a constant and
556 /// populates the array Vals with the set of values that match (or do not
557 /// match depending on isEQ).
558 /// Return false on failure. On success, the Value the comparison matched
559 /// against is placed in CompValue.
560 /// If CompValue is already set, the function is expected to fail if a match
561 /// is found but the value compared to is different.
562 bool matchInstruction(Instruction
*I
, bool isEQ
) {
563 // If this is an icmp against a constant, handle this as one of the cases.
566 if (!((ICI
= dyn_cast
<ICmpInst
>(I
)) &&
567 (C
= GetConstantInt(I
->getOperand(1), DL
)))) {
574 // Pattern match a special case
575 // (x & ~2^z) == y --> x == y || x == y|2^z
576 // This undoes a transformation done by instcombine to fuse 2 compares.
577 if (ICI
->getPredicate() == (isEQ
? ICmpInst::ICMP_EQ
: ICmpInst::ICMP_NE
)) {
578 // It's a little bit hard to see why the following transformations are
579 // correct. Here is a CVC3 program to verify them for 64-bit values:
582 ONE : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
586 mask : BITVECTOR(64) = BVSHL(ONE, z);
587 QUERY( (y & ~mask = y) =>
588 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
590 QUERY( (y | mask = y) =>
591 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
595 // Please note that each pattern must be a dual implication (<--> or
596 // iff). One directional implication can create spurious matches. If the
597 // implication is only one-way, an unsatisfiable condition on the left
598 // side can imply a satisfiable condition on the right side. Dual
599 // implication ensures that satisfiable conditions are transformed to
600 // other satisfiable conditions and unsatisfiable conditions are
601 // transformed to other unsatisfiable conditions.
603 // Here is a concrete example of a unsatisfiable condition on the left
604 // implying a satisfiable condition on the right:
607 // (x & ~mask) == y --> (x == y || x == (y | mask))
609 // Substituting y = 3, z = 0 yields:
610 // (x & -2) == 3 --> (x == 3 || x == 2)
612 // Pattern match a special case:
614 QUERY( (y & ~mask = y) =>
615 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
618 if (match(ICI
->getOperand(0),
619 m_And(m_Value(RHSVal
), m_APInt(RHSC
)))) {
621 if (Mask
.isPowerOf2() && (C
->getValue() & ~Mask
) == C
->getValue()) {
622 // If we already have a value for the switch, it has to match!
623 if (!setValueOnce(RHSVal
))
628 ConstantInt::get(C
->getContext(),
629 C
->getValue() | Mask
));
635 // Pattern match a special case:
637 QUERY( (y | mask = y) =>
638 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
641 if (match(ICI
->getOperand(0),
642 m_Or(m_Value(RHSVal
), m_APInt(RHSC
)))) {
644 if (Mask
.isPowerOf2() && (C
->getValue() | Mask
) == C
->getValue()) {
645 // If we already have a value for the switch, it has to match!
646 if (!setValueOnce(RHSVal
))
650 Vals
.push_back(ConstantInt::get(C
->getContext(),
651 C
->getValue() & ~Mask
));
657 // If we already have a value for the switch, it has to match!
658 if (!setValueOnce(ICI
->getOperand(0)))
663 return ICI
->getOperand(0);
666 // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
668 ConstantRange::makeExactICmpRegion(ICI
->getPredicate(), C
->getValue());
670 // Shift the range if the compare is fed by an add. This is the range
671 // compare idiom as emitted by instcombine.
672 Value
*CandidateVal
= I
->getOperand(0);
673 if (match(I
->getOperand(0), m_Add(m_Value(RHSVal
), m_APInt(RHSC
)))) {
674 Span
= Span
.subtract(*RHSC
);
675 CandidateVal
= RHSVal
;
678 // If this is an and/!= check, then we are looking to build the set of
679 // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
682 Span
= Span
.inverse();
684 // If there are a ton of values, we don't want to make a ginormous switch.
685 if (Span
.isSizeLargerThan(8) || Span
.isEmptySet()) {
689 // If we already have a value for the switch, it has to match!
690 if (!setValueOnce(CandidateVal
))
693 // Add all values from the range to the set
694 for (APInt Tmp
= Span
.getLower(); Tmp
!= Span
.getUpper(); ++Tmp
)
695 Vals
.push_back(ConstantInt::get(I
->getContext(), Tmp
));
701 /// Given a potentially 'or'd or 'and'd together collection of icmp
702 /// eq/ne/lt/gt instructions that compare a value against a constant, extract
703 /// the value being compared, and stick the list constants into the Vals
705 /// One "Extra" case is allowed to differ from the other.
706 void gather(Value
*V
) {
707 bool isEQ
= match(V
, m_LogicalOr(m_Value(), m_Value()));
709 // Keep a stack (SmallVector for efficiency) for depth-first traversal
710 SmallVector
<Value
*, 8> DFT
;
711 SmallPtrSet
<Value
*, 8> Visited
;
717 while (!DFT
.empty()) {
718 V
= DFT
.pop_back_val();
720 if (Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
721 // If it is a || (or && depending on isEQ), process the operands.
723 if (isEQ
? match(I
, m_LogicalOr(m_Value(Op0
), m_Value(Op1
)))
724 : match(I
, m_LogicalAnd(m_Value(Op0
), m_Value(Op1
)))) {
725 if (Visited
.insert(Op1
).second
)
727 if (Visited
.insert(Op0
).second
)
733 // Try to match the current instruction
734 if (matchInstruction(I
, isEQ
))
735 // Match succeed, continue the loop
739 // One element of the sequence of || (or &&) could not be match as a
740 // comparison against the same value as the others.
741 // We allow only one "Extra" case to be checked before the switch
746 // Failed to parse a proper sequence, abort now
753 } // end anonymous namespace
755 static void EraseTerminatorAndDCECond(Instruction
*TI
,
756 MemorySSAUpdater
*MSSAU
= nullptr) {
757 Instruction
*Cond
= nullptr;
758 if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(TI
)) {
759 Cond
= dyn_cast
<Instruction
>(SI
->getCondition());
760 } else if (BranchInst
*BI
= dyn_cast
<BranchInst
>(TI
)) {
761 if (BI
->isConditional())
762 Cond
= dyn_cast
<Instruction
>(BI
->getCondition());
763 } else if (IndirectBrInst
*IBI
= dyn_cast
<IndirectBrInst
>(TI
)) {
764 Cond
= dyn_cast
<Instruction
>(IBI
->getAddress());
767 TI
->eraseFromParent();
769 RecursivelyDeleteTriviallyDeadInstructions(Cond
, nullptr, MSSAU
);
772 /// Return true if the specified terminator checks
773 /// to see if a value is equal to constant integer value.
774 Value
*SimplifyCFGOpt::isValueEqualityComparison(Instruction
*TI
) {
776 if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(TI
)) {
777 // Do not permit merging of large switch instructions into their
778 // predecessors unless there is only one predecessor.
779 if (!SI
->getParent()->hasNPredecessorsOrMore(128 / SI
->getNumSuccessors()))
780 CV
= SI
->getCondition();
781 } else if (BranchInst
*BI
= dyn_cast
<BranchInst
>(TI
))
782 if (BI
->isConditional() && BI
->getCondition()->hasOneUse())
783 if (ICmpInst
*ICI
= dyn_cast
<ICmpInst
>(BI
->getCondition())) {
784 if (ICI
->isEquality() && GetConstantInt(ICI
->getOperand(1), DL
))
785 CV
= ICI
->getOperand(0);
788 // Unwrap any lossless ptrtoint cast.
790 if (PtrToIntInst
*PTII
= dyn_cast
<PtrToIntInst
>(CV
)) {
791 Value
*Ptr
= PTII
->getPointerOperand();
792 if (PTII
->getType() == DL
.getIntPtrType(Ptr
->getType()))
799 /// Given a value comparison instruction,
800 /// decode all of the 'cases' that it represents and return the 'default' block.
801 BasicBlock
*SimplifyCFGOpt::GetValueEqualityComparisonCases(
802 Instruction
*TI
, std::vector
<ValueEqualityComparisonCase
> &Cases
) {
803 if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(TI
)) {
804 Cases
.reserve(SI
->getNumCases());
805 for (auto Case
: SI
->cases())
806 Cases
.push_back(ValueEqualityComparisonCase(Case
.getCaseValue(),
807 Case
.getCaseSuccessor()));
808 return SI
->getDefaultDest();
811 BranchInst
*BI
= cast
<BranchInst
>(TI
);
812 ICmpInst
*ICI
= cast
<ICmpInst
>(BI
->getCondition());
813 BasicBlock
*Succ
= BI
->getSuccessor(ICI
->getPredicate() == ICmpInst::ICMP_NE
);
814 Cases
.push_back(ValueEqualityComparisonCase(
815 GetConstantInt(ICI
->getOperand(1), DL
), Succ
));
816 return BI
->getSuccessor(ICI
->getPredicate() == ICmpInst::ICMP_EQ
);
819 /// Given a vector of bb/value pairs, remove any entries
820 /// in the list that match the specified block.
822 EliminateBlockCases(BasicBlock
*BB
,
823 std::vector
<ValueEqualityComparisonCase
> &Cases
) {
824 llvm::erase(Cases
, BB
);
827 /// Return true if there are any keys in C1 that exist in C2 as well.
828 static bool ValuesOverlap(std::vector
<ValueEqualityComparisonCase
> &C1
,
829 std::vector
<ValueEqualityComparisonCase
> &C2
) {
830 std::vector
<ValueEqualityComparisonCase
> *V1
= &C1
, *V2
= &C2
;
832 // Make V1 be smaller than V2.
833 if (V1
->size() > V2
->size())
838 if (V1
->size() == 1) {
840 ConstantInt
*TheVal
= (*V1
)[0].Value
;
841 for (const ValueEqualityComparisonCase
&VECC
: *V2
)
842 if (TheVal
== VECC
.Value
)
846 // Otherwise, just sort both lists and compare element by element.
847 array_pod_sort(V1
->begin(), V1
->end());
848 array_pod_sort(V2
->begin(), V2
->end());
849 unsigned i1
= 0, i2
= 0, e1
= V1
->size(), e2
= V2
->size();
850 while (i1
!= e1
&& i2
!= e2
) {
851 if ((*V1
)[i1
].Value
== (*V2
)[i2
].Value
)
853 if ((*V1
)[i1
].Value
< (*V2
)[i2
].Value
)
861 // Set branch weights on SwitchInst. This sets the metadata if there is at
862 // least one non-zero weight.
863 static void setBranchWeights(SwitchInst
*SI
, ArrayRef
<uint32_t> Weights
) {
864 // Check that there is at least one non-zero weight. Otherwise, pass
865 // nullptr to setMetadata which will erase the existing metadata.
867 if (llvm::any_of(Weights
, [](uint32_t W
) { return W
!= 0; }))
868 N
= MDBuilder(SI
->getParent()->getContext()).createBranchWeights(Weights
);
869 SI
->setMetadata(LLVMContext::MD_prof
, N
);
872 // Similar to the above, but for branch and select instructions that take
873 // exactly 2 weights.
874 static void setBranchWeights(Instruction
*I
, uint32_t TrueWeight
,
875 uint32_t FalseWeight
) {
876 assert(isa
<BranchInst
>(I
) || isa
<SelectInst
>(I
));
877 // Check that there is at least one non-zero weight. Otherwise, pass
878 // nullptr to setMetadata which will erase the existing metadata.
880 if (TrueWeight
|| FalseWeight
)
881 N
= MDBuilder(I
->getParent()->getContext())
882 .createBranchWeights(TrueWeight
, FalseWeight
);
883 I
->setMetadata(LLVMContext::MD_prof
, N
);
886 /// If TI is known to be a terminator instruction and its block is known to
887 /// only have a single predecessor block, check to see if that predecessor is
888 /// also a value comparison with the same value, and if that comparison
889 /// determines the outcome of this comparison. If so, simplify TI. This does a
890 /// very limited form of jump threading.
891 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
892 Instruction
*TI
, BasicBlock
*Pred
, IRBuilder
<> &Builder
) {
893 Value
*PredVal
= isValueEqualityComparison(Pred
->getTerminator());
895 return false; // Not a value comparison in predecessor.
897 Value
*ThisVal
= isValueEqualityComparison(TI
);
898 assert(ThisVal
&& "This isn't a value comparison!!");
899 if (ThisVal
!= PredVal
)
900 return false; // Different predicates.
902 // TODO: Preserve branch weight metadata, similarly to how
903 // FoldValueComparisonIntoPredecessors preserves it.
905 // Find out information about when control will move from Pred to TI's block.
906 std::vector
<ValueEqualityComparisonCase
> PredCases
;
907 BasicBlock
*PredDef
=
908 GetValueEqualityComparisonCases(Pred
->getTerminator(), PredCases
);
909 EliminateBlockCases(PredDef
, PredCases
); // Remove default from cases.
911 // Find information about how control leaves this block.
912 std::vector
<ValueEqualityComparisonCase
> ThisCases
;
913 BasicBlock
*ThisDef
= GetValueEqualityComparisonCases(TI
, ThisCases
);
914 EliminateBlockCases(ThisDef
, ThisCases
); // Remove default from cases.
916 // If TI's block is the default block from Pred's comparison, potentially
917 // simplify TI based on this knowledge.
918 if (PredDef
== TI
->getParent()) {
919 // If we are here, we know that the value is none of those cases listed in
920 // PredCases. If there are any cases in ThisCases that are in PredCases, we
922 if (!ValuesOverlap(PredCases
, ThisCases
))
925 if (isa
<BranchInst
>(TI
)) {
926 // Okay, one of the successors of this condbr is dead. Convert it to a
928 assert(ThisCases
.size() == 1 && "Branch can only have one case!");
929 // Insert the new branch.
930 Instruction
*NI
= Builder
.CreateBr(ThisDef
);
933 // Remove PHI node entries for the dead edge.
934 ThisCases
[0].Dest
->removePredecessor(PredDef
);
936 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred
->getTerminator()
937 << "Through successor TI: " << *TI
<< "Leaving: " << *NI
940 EraseTerminatorAndDCECond(TI
);
944 {{DominatorTree::Delete
, PredDef
, ThisCases
[0].Dest
}});
949 SwitchInstProfUpdateWrapper SI
= *cast
<SwitchInst
>(TI
);
950 // Okay, TI has cases that are statically dead, prune them away.
951 SmallPtrSet
<Constant
*, 16> DeadCases
;
952 for (unsigned i
= 0, e
= PredCases
.size(); i
!= e
; ++i
)
953 DeadCases
.insert(PredCases
[i
].Value
);
955 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred
->getTerminator()
956 << "Through successor TI: " << *TI
);
958 SmallDenseMap
<BasicBlock
*, int, 8> NumPerSuccessorCases
;
959 for (SwitchInst::CaseIt i
= SI
->case_end(), e
= SI
->case_begin(); i
!= e
;) {
961 auto *Successor
= i
->getCaseSuccessor();
963 ++NumPerSuccessorCases
[Successor
];
964 if (DeadCases
.count(i
->getCaseValue())) {
965 Successor
->removePredecessor(PredDef
);
968 --NumPerSuccessorCases
[Successor
];
973 std::vector
<DominatorTree::UpdateType
> Updates
;
974 for (const std::pair
<BasicBlock
*, int> &I
: NumPerSuccessorCases
)
976 Updates
.push_back({DominatorTree::Delete
, PredDef
, I
.first
});
977 DTU
->applyUpdates(Updates
);
980 LLVM_DEBUG(dbgs() << "Leaving: " << *TI
<< "\n");
984 // Otherwise, TI's block must correspond to some matched value. Find out
985 // which value (or set of values) this is.
986 ConstantInt
*TIV
= nullptr;
987 BasicBlock
*TIBB
= TI
->getParent();
988 for (unsigned i
= 0, e
= PredCases
.size(); i
!= e
; ++i
)
989 if (PredCases
[i
].Dest
== TIBB
) {
991 return false; // Cannot handle multiple values coming to this block.
992 TIV
= PredCases
[i
].Value
;
994 assert(TIV
&& "No edge from pred to succ?");
996 // Okay, we found the one constant that our value can be if we get into TI's
997 // BB. Find out which successor will unconditionally be branched to.
998 BasicBlock
*TheRealDest
= nullptr;
999 for (unsigned i
= 0, e
= ThisCases
.size(); i
!= e
; ++i
)
1000 if (ThisCases
[i
].Value
== TIV
) {
1001 TheRealDest
= ThisCases
[i
].Dest
;
1005 // If not handled by any explicit cases, it is handled by the default case.
1007 TheRealDest
= ThisDef
;
1009 SmallPtrSet
<BasicBlock
*, 2> RemovedSuccs
;
1011 // Remove PHI node entries for dead edges.
1012 BasicBlock
*CheckEdge
= TheRealDest
;
1013 for (BasicBlock
*Succ
: successors(TIBB
))
1014 if (Succ
!= CheckEdge
) {
1015 if (Succ
!= TheRealDest
)
1016 RemovedSuccs
.insert(Succ
);
1017 Succ
->removePredecessor(TIBB
);
1019 CheckEdge
= nullptr;
1021 // Insert the new branch.
1022 Instruction
*NI
= Builder
.CreateBr(TheRealDest
);
1025 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred
->getTerminator()
1026 << "Through successor TI: " << *TI
<< "Leaving: " << *NI
1029 EraseTerminatorAndDCECond(TI
);
1031 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
1032 Updates
.reserve(RemovedSuccs
.size());
1033 for (auto *RemovedSucc
: RemovedSuccs
)
1034 Updates
.push_back({DominatorTree::Delete
, TIBB
, RemovedSucc
});
1035 DTU
->applyUpdates(Updates
);
1042 /// This class implements a stable ordering of constant
1043 /// integers that does not depend on their address. This is important for
1044 /// applications that sort ConstantInt's to ensure uniqueness.
1045 struct ConstantIntOrdering
{
1046 bool operator()(const ConstantInt
*LHS
, const ConstantInt
*RHS
) const {
1047 return LHS
->getValue().ult(RHS
->getValue());
1051 } // end anonymous namespace
1053 static int ConstantIntSortPredicate(ConstantInt
*const *P1
,
1054 ConstantInt
*const *P2
) {
1055 const ConstantInt
*LHS
= *P1
;
1056 const ConstantInt
*RHS
= *P2
;
1059 return LHS
->getValue().ult(RHS
->getValue()) ? 1 : -1;
1062 /// Get Weights of a given terminator, the default weight is at the front
1063 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
1065 static void GetBranchWeights(Instruction
*TI
,
1066 SmallVectorImpl
<uint64_t> &Weights
) {
1067 MDNode
*MD
= TI
->getMetadata(LLVMContext::MD_prof
);
1069 for (unsigned i
= 1, e
= MD
->getNumOperands(); i
< e
; ++i
) {
1070 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(i
));
1071 Weights
.push_back(CI
->getValue().getZExtValue());
1074 // If TI is a conditional eq, the default case is the false case,
1075 // and the corresponding branch-weight data is at index 2. We swap the
1076 // default weight to be the first entry.
1077 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(TI
)) {
1078 assert(Weights
.size() == 2);
1079 ICmpInst
*ICI
= cast
<ICmpInst
>(BI
->getCondition());
1080 if (ICI
->getPredicate() == ICmpInst::ICMP_EQ
)
1081 std::swap(Weights
.front(), Weights
.back());
1085 /// Keep halving the weights until all can fit in uint32_t.
1086 static void FitWeights(MutableArrayRef
<uint64_t> Weights
) {
1087 uint64_t Max
= *std::max_element(Weights
.begin(), Weights
.end());
1088 if (Max
> UINT_MAX
) {
1089 unsigned Offset
= 32 - llvm::countl_zero(Max
);
1090 for (uint64_t &I
: Weights
)
1095 static void CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
1096 BasicBlock
*BB
, BasicBlock
*PredBlock
, ValueToValueMapTy
&VMap
) {
1097 Instruction
*PTI
= PredBlock
->getTerminator();
1099 // If we have bonus instructions, clone them into the predecessor block.
1100 // Note that there may be multiple predecessor blocks, so we cannot move
1101 // bonus instructions to a predecessor block.
1102 for (Instruction
&BonusInst
: *BB
) {
1103 if (BonusInst
.isTerminator())
1106 Instruction
*NewBonusInst
= BonusInst
.clone();
1108 if (!isa
<DbgInfoIntrinsic
>(BonusInst
) &&
1109 PTI
->getDebugLoc() != NewBonusInst
->getDebugLoc()) {
1110 // Unless the instruction has the same !dbg location as the original
1111 // branch, drop it. When we fold the bonus instructions we want to make
1112 // sure we reset their debug locations in order to avoid stepping on
1113 // dead code caused by folding dead branches.
1114 NewBonusInst
->setDebugLoc(DebugLoc());
1117 RemapInstruction(NewBonusInst
, VMap
,
1118 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
1120 // If we speculated an instruction, we need to drop any metadata that may
1121 // result in undefined behavior, as the metadata might have been valid
1122 // only given the branch precondition.
1123 // Similarly strip attributes on call parameters that may cause UB in
1124 // location the call is moved to.
1125 NewBonusInst
->dropUBImplyingAttrsAndMetadata();
1127 NewBonusInst
->insertInto(PredBlock
, PTI
->getIterator());
1128 auto Range
= NewBonusInst
->cloneDebugInfoFrom(&BonusInst
);
1129 RemapDPValueRange(NewBonusInst
->getModule(), Range
, VMap
,
1130 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
1132 if (isa
<DbgInfoIntrinsic
>(BonusInst
))
1135 NewBonusInst
->takeName(&BonusInst
);
1136 BonusInst
.setName(NewBonusInst
->getName() + ".old");
1137 VMap
[&BonusInst
] = NewBonusInst
;
1139 // Update (liveout) uses of bonus instructions,
1140 // now that the bonus instruction has been cloned into predecessor.
1141 // Note that we expect to be in a block-closed SSA form for this to work!
1142 for (Use
&U
: make_early_inc_range(BonusInst
.uses())) {
1143 auto *UI
= cast
<Instruction
>(U
.getUser());
1144 auto *PN
= dyn_cast
<PHINode
>(UI
);
1146 assert(UI
->getParent() == BB
&& BonusInst
.comesBefore(UI
) &&
1147 "If the user is not a PHI node, then it should be in the same "
1148 "block as, and come after, the original bonus instruction.");
1149 continue; // Keep using the original bonus instruction.
1151 // Is this the block-closed SSA form PHI node?
1152 if (PN
->getIncomingBlock(U
) == BB
)
1153 continue; // Great, keep using the original bonus instruction.
1154 // The only other alternative is an "use" when coming from
1155 // the predecessor block - here we should refer to the cloned bonus instr.
1156 assert(PN
->getIncomingBlock(U
) == PredBlock
&&
1157 "Not in block-closed SSA form?");
1158 U
.set(NewBonusInst
);
1163 bool SimplifyCFGOpt::PerformValueComparisonIntoPredecessorFolding(
1164 Instruction
*TI
, Value
*&CV
, Instruction
*PTI
, IRBuilder
<> &Builder
) {
1165 BasicBlock
*BB
= TI
->getParent();
1166 BasicBlock
*Pred
= PTI
->getParent();
1168 SmallVector
<DominatorTree::UpdateType
, 32> Updates
;
1170 // Figure out which 'cases' to copy from SI to PSI.
1171 std::vector
<ValueEqualityComparisonCase
> BBCases
;
1172 BasicBlock
*BBDefault
= GetValueEqualityComparisonCases(TI
, BBCases
);
1174 std::vector
<ValueEqualityComparisonCase
> PredCases
;
1175 BasicBlock
*PredDefault
= GetValueEqualityComparisonCases(PTI
, PredCases
);
1177 // Based on whether the default edge from PTI goes to BB or not, fill in
1178 // PredCases and PredDefault with the new switch cases we would like to
1180 SmallMapVector
<BasicBlock
*, int, 8> NewSuccessors
;
1182 // Update the branch weight metadata along the way
1183 SmallVector
<uint64_t, 8> Weights
;
1184 bool PredHasWeights
= hasBranchWeightMD(*PTI
);
1185 bool SuccHasWeights
= hasBranchWeightMD(*TI
);
1187 if (PredHasWeights
) {
1188 GetBranchWeights(PTI
, Weights
);
1189 // branch-weight metadata is inconsistent here.
1190 if (Weights
.size() != 1 + PredCases
.size())
1191 PredHasWeights
= SuccHasWeights
= false;
1192 } else if (SuccHasWeights
)
1193 // If there are no predecessor weights but there are successor weights,
1194 // populate Weights with 1, which will later be scaled to the sum of
1195 // successor's weights
1196 Weights
.assign(1 + PredCases
.size(), 1);
1198 SmallVector
<uint64_t, 8> SuccWeights
;
1199 if (SuccHasWeights
) {
1200 GetBranchWeights(TI
, SuccWeights
);
1201 // branch-weight metadata is inconsistent here.
1202 if (SuccWeights
.size() != 1 + BBCases
.size())
1203 PredHasWeights
= SuccHasWeights
= false;
1204 } else if (PredHasWeights
)
1205 SuccWeights
.assign(1 + BBCases
.size(), 1);
1207 if (PredDefault
== BB
) {
1208 // If this is the default destination from PTI, only the edges in TI
1209 // that don't occur in PTI, or that branch to BB will be activated.
1210 std::set
<ConstantInt
*, ConstantIntOrdering
> PTIHandled
;
1211 for (unsigned i
= 0, e
= PredCases
.size(); i
!= e
; ++i
)
1212 if (PredCases
[i
].Dest
!= BB
)
1213 PTIHandled
.insert(PredCases
[i
].Value
);
1215 // The default destination is BB, we don't need explicit targets.
1216 std::swap(PredCases
[i
], PredCases
.back());
1218 if (PredHasWeights
|| SuccHasWeights
) {
1219 // Increase weight for the default case.
1220 Weights
[0] += Weights
[i
+ 1];
1221 std::swap(Weights
[i
+ 1], Weights
.back());
1225 PredCases
.pop_back();
1230 // Reconstruct the new switch statement we will be building.
1231 if (PredDefault
!= BBDefault
) {
1232 PredDefault
->removePredecessor(Pred
);
1233 if (DTU
&& PredDefault
!= BB
)
1234 Updates
.push_back({DominatorTree::Delete
, Pred
, PredDefault
});
1235 PredDefault
= BBDefault
;
1236 ++NewSuccessors
[BBDefault
];
1239 unsigned CasesFromPred
= Weights
.size();
1240 uint64_t ValidTotalSuccWeight
= 0;
1241 for (unsigned i
= 0, e
= BBCases
.size(); i
!= e
; ++i
)
1242 if (!PTIHandled
.count(BBCases
[i
].Value
) && BBCases
[i
].Dest
!= BBDefault
) {
1243 PredCases
.push_back(BBCases
[i
]);
1244 ++NewSuccessors
[BBCases
[i
].Dest
];
1245 if (SuccHasWeights
|| PredHasWeights
) {
1246 // The default weight is at index 0, so weight for the ith case
1247 // should be at index i+1. Scale the cases from successor by
1248 // PredDefaultWeight (Weights[0]).
1249 Weights
.push_back(Weights
[0] * SuccWeights
[i
+ 1]);
1250 ValidTotalSuccWeight
+= SuccWeights
[i
+ 1];
1254 if (SuccHasWeights
|| PredHasWeights
) {
1255 ValidTotalSuccWeight
+= SuccWeights
[0];
1256 // Scale the cases from predecessor by ValidTotalSuccWeight.
1257 for (unsigned i
= 1; i
< CasesFromPred
; ++i
)
1258 Weights
[i
] *= ValidTotalSuccWeight
;
1259 // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
1260 Weights
[0] *= SuccWeights
[0];
1263 // If this is not the default destination from PSI, only the edges
1264 // in SI that occur in PSI with a destination of BB will be
1266 std::set
<ConstantInt
*, ConstantIntOrdering
> PTIHandled
;
1267 std::map
<ConstantInt
*, uint64_t> WeightsForHandled
;
1268 for (unsigned i
= 0, e
= PredCases
.size(); i
!= e
; ++i
)
1269 if (PredCases
[i
].Dest
== BB
) {
1270 PTIHandled
.insert(PredCases
[i
].Value
);
1272 if (PredHasWeights
|| SuccHasWeights
) {
1273 WeightsForHandled
[PredCases
[i
].Value
] = Weights
[i
+ 1];
1274 std::swap(Weights
[i
+ 1], Weights
.back());
1278 std::swap(PredCases
[i
], PredCases
.back());
1279 PredCases
.pop_back();
1284 // Okay, now we know which constants were sent to BB from the
1285 // predecessor. Figure out where they will all go now.
1286 for (unsigned i
= 0, e
= BBCases
.size(); i
!= e
; ++i
)
1287 if (PTIHandled
.count(BBCases
[i
].Value
)) {
1288 // If this is one we are capable of getting...
1289 if (PredHasWeights
|| SuccHasWeights
)
1290 Weights
.push_back(WeightsForHandled
[BBCases
[i
].Value
]);
1291 PredCases
.push_back(BBCases
[i
]);
1292 ++NewSuccessors
[BBCases
[i
].Dest
];
1293 PTIHandled
.erase(BBCases
[i
].Value
); // This constant is taken care of
1296 // If there are any constants vectored to BB that TI doesn't handle,
1297 // they must go to the default destination of TI.
1298 for (ConstantInt
*I
: PTIHandled
) {
1299 if (PredHasWeights
|| SuccHasWeights
)
1300 Weights
.push_back(WeightsForHandled
[I
]);
1301 PredCases
.push_back(ValueEqualityComparisonCase(I
, BBDefault
));
1302 ++NewSuccessors
[BBDefault
];
1306 // Okay, at this point, we know which new successor Pred will get. Make
1307 // sure we update the number of entries in the PHI nodes for these
1309 SmallPtrSet
<BasicBlock
*, 2> SuccsOfPred
;
1311 SuccsOfPred
= {succ_begin(Pred
), succ_end(Pred
)};
1312 Updates
.reserve(Updates
.size() + NewSuccessors
.size());
1314 for (const std::pair
<BasicBlock
*, int /*Num*/> &NewSuccessor
:
1316 for (auto I
: seq(NewSuccessor
.second
)) {
1318 AddPredecessorToBlock(NewSuccessor
.first
, Pred
, BB
);
1320 if (DTU
&& !SuccsOfPred
.contains(NewSuccessor
.first
))
1321 Updates
.push_back({DominatorTree::Insert
, Pred
, NewSuccessor
.first
});
1324 Builder
.SetInsertPoint(PTI
);
1325 // Convert pointer to int before we switch.
1326 if (CV
->getType()->isPointerTy()) {
1328 Builder
.CreatePtrToInt(CV
, DL
.getIntPtrType(CV
->getType()), "magicptr");
1331 // Now that the successors are updated, create the new Switch instruction.
1332 SwitchInst
*NewSI
= Builder
.CreateSwitch(CV
, PredDefault
, PredCases
.size());
1333 NewSI
->setDebugLoc(PTI
->getDebugLoc());
1334 for (ValueEqualityComparisonCase
&V
: PredCases
)
1335 NewSI
->addCase(V
.Value
, V
.Dest
);
1337 if (PredHasWeights
|| SuccHasWeights
) {
1338 // Halve the weights if any of them cannot fit in an uint32_t
1339 FitWeights(Weights
);
1341 SmallVector
<uint32_t, 8> MDWeights(Weights
.begin(), Weights
.end());
1343 setBranchWeights(NewSI
, MDWeights
);
1346 EraseTerminatorAndDCECond(PTI
);
1348 // Okay, last check. If BB is still a successor of PSI, then we must
1349 // have an infinite loop case. If so, add an infinitely looping block
1350 // to handle the case to preserve the behavior of the code.
1351 BasicBlock
*InfLoopBlock
= nullptr;
1352 for (unsigned i
= 0, e
= NewSI
->getNumSuccessors(); i
!= e
; ++i
)
1353 if (NewSI
->getSuccessor(i
) == BB
) {
1354 if (!InfLoopBlock
) {
1355 // Insert it at the end of the function, because it's either code,
1356 // or it won't matter if it's hot. :)
1358 BasicBlock::Create(BB
->getContext(), "infloop", BB
->getParent());
1359 BranchInst::Create(InfLoopBlock
, InfLoopBlock
);
1362 {DominatorTree::Insert
, InfLoopBlock
, InfLoopBlock
});
1364 NewSI
->setSuccessor(i
, InfLoopBlock
);
1369 Updates
.push_back({DominatorTree::Insert
, Pred
, InfLoopBlock
});
1371 Updates
.push_back({DominatorTree::Delete
, Pred
, BB
});
1373 DTU
->applyUpdates(Updates
);
1376 ++NumFoldValueComparisonIntoPredecessors
;
1380 /// The specified terminator is a value equality comparison instruction
1381 /// (either a switch or a branch on "X == c").
1382 /// See if any of the predecessors of the terminator block are value comparisons
1383 /// on the same value. If so, and if safe to do so, fold them together.
1384 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction
*TI
,
1385 IRBuilder
<> &Builder
) {
1386 BasicBlock
*BB
= TI
->getParent();
1387 Value
*CV
= isValueEqualityComparison(TI
); // CondVal
1388 assert(CV
&& "Not a comparison?");
1390 bool Changed
= false;
1392 SmallSetVector
<BasicBlock
*, 16> Preds(pred_begin(BB
), pred_end(BB
));
1393 while (!Preds
.empty()) {
1394 BasicBlock
*Pred
= Preds
.pop_back_val();
1395 Instruction
*PTI
= Pred
->getTerminator();
1397 // Don't try to fold into itself.
1401 // See if the predecessor is a comparison with the same value.
1402 Value
*PCV
= isValueEqualityComparison(PTI
); // PredCondVal
1406 SmallSetVector
<BasicBlock
*, 4> FailBlocks
;
1407 if (!SafeToMergeTerminators(TI
, PTI
, &FailBlocks
)) {
1408 for (auto *Succ
: FailBlocks
) {
1409 if (!SplitBlockPredecessors(Succ
, TI
->getParent(), ".fold.split", DTU
))
1414 PerformValueComparisonIntoPredecessorFolding(TI
, CV
, PTI
, Builder
);
1420 // If we would need to insert a select that uses the value of this invoke
1421 // (comments in hoistSuccIdenticalTerminatorToSwitchOrIf explain why we would
1422 // need to do this), we can't hoist the invoke, as there is nowhere to put the
1423 // select in this case.
1424 static bool isSafeToHoistInvoke(BasicBlock
*BB1
, BasicBlock
*BB2
,
1425 Instruction
*I1
, Instruction
*I2
) {
1426 for (BasicBlock
*Succ
: successors(BB1
)) {
1427 for (const PHINode
&PN
: Succ
->phis()) {
1428 Value
*BB1V
= PN
.getIncomingValueForBlock(BB1
);
1429 Value
*BB2V
= PN
.getIncomingValueForBlock(BB2
);
1430 if (BB1V
!= BB2V
&& (BB1V
== I1
|| BB2V
== I2
)) {
1438 // Get interesting characteristics of instructions that
1439 // `hoistCommonCodeFromSuccessors` didn't hoist. They restrict what kind of
1440 // instructions can be reordered across.
1444 SkipImplicitControlFlow
= 4
1447 static unsigned skippedInstrFlags(Instruction
*I
) {
1449 if (I
->mayReadFromMemory())
1450 Flags
|= SkipReadMem
;
1451 // We can't arbitrarily move around allocas, e.g. moving allocas (especially
1452 // inalloca) across stacksave/stackrestore boundaries.
1453 if (I
->mayHaveSideEffects() || isa
<AllocaInst
>(I
))
1454 Flags
|= SkipSideEffect
;
1455 if (!isGuaranteedToTransferExecutionToSuccessor(I
))
1456 Flags
|= SkipImplicitControlFlow
;
1460 // Returns true if it is safe to reorder an instruction across preceding
1461 // instructions in a basic block.
1462 static bool isSafeToHoistInstr(Instruction
*I
, unsigned Flags
) {
1463 // Don't reorder a store over a load.
1464 if ((Flags
& SkipReadMem
) && I
->mayWriteToMemory())
1467 // If we have seen an instruction with side effects, it's unsafe to reorder an
1468 // instruction which reads memory or itself has side effects.
1469 if ((Flags
& SkipSideEffect
) &&
1470 (I
->mayReadFromMemory() || I
->mayHaveSideEffects() || isa
<AllocaInst
>(I
)))
1473 // Reordering across an instruction which does not necessarily transfer
1474 // control to the next instruction is speculation.
1475 if ((Flags
& SkipImplicitControlFlow
) && !isSafeToSpeculativelyExecute(I
))
1478 // Hoisting of llvm.deoptimize is only legal together with the next return
1479 // instruction, which this pass is not always able to do.
1480 if (auto *CB
= dyn_cast
<CallBase
>(I
))
1481 if (CB
->getIntrinsicID() == Intrinsic::experimental_deoptimize
)
1484 // It's also unsafe/illegal to hoist an instruction above its instruction
1486 BasicBlock
*BB
= I
->getParent();
1487 for (Value
*Op
: I
->operands()) {
1488 if (auto *J
= dyn_cast
<Instruction
>(Op
))
1489 if (J
->getParent() == BB
)
1496 static bool passingValueIsAlwaysUndefined(Value
*V
, Instruction
*I
, bool PtrValueMayBeModified
= false);
1498 /// Helper function for hoistCommonCodeFromSuccessors. Return true if identical
1499 /// instructions \p I1 and \p I2 can and should be hoisted.
1500 static bool shouldHoistCommonInstructions(Instruction
*I1
, Instruction
*I2
,
1501 const TargetTransformInfo
&TTI
) {
1502 // If we're going to hoist a call, make sure that the two instructions
1503 // we're commoning/hoisting are both marked with musttail, or neither of
1504 // them is marked as such. Otherwise, we might end up in a situation where
1505 // we hoist from a block where the terminator is a `ret` to a block where
1506 // the terminator is a `br`, and `musttail` calls expect to be followed by
1508 auto *C1
= dyn_cast
<CallInst
>(I1
);
1509 auto *C2
= dyn_cast
<CallInst
>(I2
);
1511 if (C1
->isMustTailCall() != C2
->isMustTailCall())
1514 if (!TTI
.isProfitableToHoist(I1
) || !TTI
.isProfitableToHoist(I2
))
1517 // If any of the two call sites has nomerge or convergent attribute, stop
1519 if (const auto *CB1
= dyn_cast
<CallBase
>(I1
))
1520 if (CB1
->cannotMerge() || CB1
->isConvergent())
1522 if (const auto *CB2
= dyn_cast
<CallBase
>(I2
))
1523 if (CB2
->cannotMerge() || CB2
->isConvergent())
1529 /// Hoist any common code in the successor blocks up into the block. This
1530 /// function guarantees that BB dominates all successors. If EqTermsOnly is
1531 /// given, only perform hoisting in case both blocks only contain a terminator.
1532 /// In that case, only the original BI will be replaced and selects for PHIs are
1534 bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock
*BB
,
1536 // This does very trivial matching, with limited scanning, to find identical
1537 // instructions in the two blocks. In particular, we don't want to get into
1538 // O(N1*N2*...) situations here where Ni are the sizes of these successors. As
1539 // such, we currently just scan for obviously identical instructions in an
1540 // identical order, possibly separated by the same number of non-identical
1542 unsigned int SuccSize
= succ_size(BB
);
1546 // If either of the blocks has it's address taken, then we can't do this fold,
1547 // because the code we'd hoist would no longer run when we jump into the block
1549 for (auto *Succ
: successors(BB
))
1550 if (Succ
->hasAddressTaken() || !Succ
->getSinglePredecessor())
1553 auto *TI
= BB
->getTerminator();
1555 // The second of pair is a SkipFlags bitmask.
1556 using SuccIterPair
= std::pair
<BasicBlock::iterator
, unsigned>;
1557 SmallVector
<SuccIterPair
, 8> SuccIterPairs
;
1558 for (auto *Succ
: successors(BB
)) {
1559 BasicBlock::iterator SuccItr
= Succ
->begin();
1560 if (isa
<PHINode
>(*SuccItr
))
1562 SuccIterPairs
.push_back(SuccIterPair(SuccItr
, 0));
1565 // Check if only hoisting terminators is allowed. This does not add new
1566 // instructions to the hoist location.
1568 // Skip any debug intrinsics, as they are free to hoist.
1569 for (auto &SuccIter
: make_first_range(SuccIterPairs
)) {
1570 auto *INonDbg
= &*skipDebugIntrinsics(SuccIter
);
1571 if (!INonDbg
->isTerminator())
1574 // Now we know that we only need to hoist debug intrinsics and the
1575 // terminator. Let the loop below handle those 2 cases.
1578 // Count how many instructions were not hoisted so far. There's a limit on how
1579 // many instructions we skip, serving as a compilation time control as well as
1580 // preventing excessive increase of life ranges.
1581 unsigned NumSkipped
= 0;
1582 // If we find an unreachable instruction at the beginning of a basic block, we
1583 // can still hoist instructions from the rest of the basic blocks.
1584 if (SuccIterPairs
.size() > 2) {
1585 erase_if(SuccIterPairs
,
1586 [](const auto &Pair
) { return isa
<UnreachableInst
>(Pair
.first
); });
1587 if (SuccIterPairs
.size() < 2)
1591 bool Changed
= false;
1594 auto *SuccIterPairBegin
= SuccIterPairs
.begin();
1595 auto &BB1ItrPair
= *SuccIterPairBegin
++;
1596 auto OtherSuccIterPairRange
=
1597 iterator_range(SuccIterPairBegin
, SuccIterPairs
.end());
1598 auto OtherSuccIterRange
= make_first_range(OtherSuccIterPairRange
);
1600 Instruction
*I1
= &*BB1ItrPair
.first
;
1601 auto *BB1
= I1
->getParent();
1603 // Skip debug info if it is not identical.
1604 bool AllDbgInstsAreIdentical
= all_of(OtherSuccIterRange
, [I1
](auto &Iter
) {
1605 Instruction
*I2
= &*Iter
;
1606 return I1
->isIdenticalToWhenDefined(I2
);
1608 if (!AllDbgInstsAreIdentical
) {
1609 while (isa
<DbgInfoIntrinsic
>(I1
))
1610 I1
= &*++BB1ItrPair
.first
;
1611 for (auto &SuccIter
: OtherSuccIterRange
) {
1612 Instruction
*I2
= &*SuccIter
;
1613 while (isa
<DbgInfoIntrinsic
>(I2
))
1618 bool AllInstsAreIdentical
= true;
1619 bool HasTerminator
= I1
->isTerminator();
1620 for (auto &SuccIter
: OtherSuccIterRange
) {
1621 Instruction
*I2
= &*SuccIter
;
1622 HasTerminator
|= I2
->isTerminator();
1623 if (AllInstsAreIdentical
&& !I1
->isIdenticalToWhenDefined(I2
))
1624 AllInstsAreIdentical
= false;
1627 // If we are hoisting the terminator instruction, don't move one (making a
1628 // broken BB), instead clone it, and remove BI.
1629 if (HasTerminator
) {
1630 // Even if BB, which contains only one unreachable instruction, is ignored
1631 // at the beginning of the loop, we can hoist the terminator instruction.
1632 // If any instructions remain in the block, we cannot hoist terminators.
1633 if (NumSkipped
|| !AllInstsAreIdentical
)
1635 SmallVector
<Instruction
*, 8> Insts
;
1636 for (auto &SuccIter
: OtherSuccIterRange
)
1637 Insts
.push_back(&*SuccIter
);
1638 return hoistSuccIdenticalTerminatorToSwitchOrIf(TI
, I1
, Insts
) || Changed
;
1641 if (AllInstsAreIdentical
) {
1642 unsigned SkipFlagsBB1
= BB1ItrPair
.second
;
1643 AllInstsAreIdentical
=
1644 isSafeToHoistInstr(I1
, SkipFlagsBB1
) &&
1645 all_of(OtherSuccIterPairRange
, [=](const auto &Pair
) {
1646 Instruction
*I2
= &*Pair
.first
;
1647 unsigned SkipFlagsBB2
= Pair
.second
;
1648 // Even if the instructions are identical, it may not
1649 // be safe to hoist them if we have skipped over
1650 // instructions with side effects or their operands
1652 return isSafeToHoistInstr(I2
, SkipFlagsBB2
) &&
1653 shouldHoistCommonInstructions(I1
, I2
, TTI
);
1657 if (AllInstsAreIdentical
) {
1659 if (isa
<DbgInfoIntrinsic
>(I1
)) {
1660 // The debug location is an integral part of a debug info intrinsic
1661 // and can't be separated from it or replaced. Instead of attempting
1662 // to merge locations, simply hoist both copies of the intrinsic.
1663 I1
->moveBeforePreserving(TI
);
1664 for (auto &SuccIter
: OtherSuccIterRange
) {
1665 auto *I2
= &*SuccIter
++;
1666 assert(isa
<DbgInfoIntrinsic
>(I2
));
1667 I2
->moveBeforePreserving(TI
);
1670 // For a normal instruction, we just move one to right before the
1671 // branch, then replace all uses of the other with the first. Finally,
1672 // we remove the now redundant second instruction.
1673 I1
->moveBeforePreserving(TI
);
1674 BB
->splice(TI
->getIterator(), BB1
, I1
->getIterator());
1675 for (auto &SuccIter
: OtherSuccIterRange
) {
1676 Instruction
*I2
= &*SuccIter
++;
1678 if (!I2
->use_empty())
1679 I2
->replaceAllUsesWith(I1
);
1681 combineMetadataForCSE(I1
, I2
, true);
1682 // I1 and I2 are being combined into a single instruction. Its debug
1683 // location is the merged locations of the original instructions.
1684 I1
->applyMergedLocation(I1
->getDebugLoc(), I2
->getDebugLoc());
1685 I2
->eraseFromParent();
1689 NumHoistCommonCode
+= SuccIterPairs
.size();
1691 NumHoistCommonInstrs
+= SuccIterPairs
.size();
1693 if (NumSkipped
>= HoistCommonSkipLimit
)
1695 // We are about to skip over a pair of non-identical instructions. Record
1696 // if any have characteristics that would prevent reordering instructions
1698 for (auto &SuccIterPair
: SuccIterPairs
) {
1699 Instruction
*I
= &*SuccIterPair
.first
++;
1700 SuccIterPair
.second
|= skippedInstrFlags(I
);
1707 bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
1708 Instruction
*TI
, Instruction
*I1
,
1709 SmallVectorImpl
<Instruction
*> &OtherSuccTIs
) {
1711 auto *BI
= dyn_cast
<BranchInst
>(TI
);
1713 bool Changed
= false;
1714 BasicBlock
*TIParent
= TI
->getParent();
1715 BasicBlock
*BB1
= I1
->getParent();
1717 // Use only for an if statement.
1718 auto *I2
= *OtherSuccTIs
.begin();
1719 auto *BB2
= I2
->getParent();
1721 assert(OtherSuccTIs
.size() == 1);
1722 assert(BI
->getSuccessor(0) == I1
->getParent());
1723 assert(BI
->getSuccessor(1) == I2
->getParent());
1726 // In the case of an if statement, we try to hoist an invoke.
1727 // FIXME: Can we define a safety predicate for CallBr?
1728 // FIXME: Test case llvm/test/Transforms/SimplifyCFG/2009-06-15-InvokeCrash.ll
1729 // removed in 4c923b3b3fd0ac1edebf0603265ca3ba51724937 commit?
1730 if (isa
<InvokeInst
>(I1
) && (!BI
|| !isSafeToHoistInvoke(BB1
, BB2
, I1
, I2
)))
1733 // TODO: callbr hoisting currently disabled pending further study.
1734 if (isa
<CallBrInst
>(I1
))
1737 for (BasicBlock
*Succ
: successors(BB1
)) {
1738 for (PHINode
&PN
: Succ
->phis()) {
1739 Value
*BB1V
= PN
.getIncomingValueForBlock(BB1
);
1740 for (Instruction
*OtherSuccTI
: OtherSuccTIs
) {
1741 Value
*BB2V
= PN
.getIncomingValueForBlock(OtherSuccTI
->getParent());
1745 // In the case of an if statement, check for
1746 // passingValueIsAlwaysUndefined here because we would rather eliminate
1747 // undefined control flow then converting it to a select.
1748 if (!BI
|| passingValueIsAlwaysUndefined(BB1V
, &PN
) ||
1749 passingValueIsAlwaysUndefined(BB2V
, &PN
))
1755 // Okay, it is safe to hoist the terminator.
1756 Instruction
*NT
= I1
->clone();
1757 NT
->insertInto(TIParent
, TI
->getIterator());
1758 if (!NT
->getType()->isVoidTy()) {
1759 I1
->replaceAllUsesWith(NT
);
1760 for (Instruction
*OtherSuccTI
: OtherSuccTIs
)
1761 OtherSuccTI
->replaceAllUsesWith(NT
);
1765 NumHoistCommonInstrs
+= OtherSuccTIs
.size() + 1;
1767 // Ensure terminator gets a debug location, even an unknown one, in case
1768 // it involves inlinable calls.
1769 SmallVector
<DILocation
*, 4> Locs
;
1770 Locs
.push_back(I1
->getDebugLoc());
1771 for (auto *OtherSuccTI
: OtherSuccTIs
)
1772 Locs
.push_back(OtherSuccTI
->getDebugLoc());
1773 // Also clone DPValues from the existing terminator, and all others (to
1774 // duplicate existing hoisting behaviour).
1775 NT
->cloneDebugInfoFrom(I1
);
1776 for (Instruction
*OtherSuccTI
: OtherSuccTIs
)
1777 NT
->cloneDebugInfoFrom(OtherSuccTI
);
1778 NT
->setDebugLoc(DILocation::getMergedLocations(Locs
));
1780 // PHIs created below will adopt NT's merged DebugLoc.
1781 IRBuilder
<NoFolder
> Builder(NT
);
1783 // In the case of an if statement, hoisting one of the terminators from our
1784 // successor is a great thing. Unfortunately, the successors of the if/else
1785 // blocks may have PHI nodes in them. If they do, all PHI entries for BB1/BB2
1786 // must agree for all PHI nodes, so we insert select instruction to compute
1787 // the final result.
1789 std::map
<std::pair
<Value
*, Value
*>, SelectInst
*> InsertedSelects
;
1790 for (BasicBlock
*Succ
: successors(BB1
)) {
1791 for (PHINode
&PN
: Succ
->phis()) {
1792 Value
*BB1V
= PN
.getIncomingValueForBlock(BB1
);
1793 Value
*BB2V
= PN
.getIncomingValueForBlock(BB2
);
1797 // These values do not agree. Insert a select instruction before NT
1798 // that determines the right value.
1799 SelectInst
*&SI
= InsertedSelects
[std::make_pair(BB1V
, BB2V
)];
1801 // Propagate fast-math-flags from phi node to its replacement select.
1802 IRBuilder
<>::FastMathFlagGuard
FMFGuard(Builder
);
1803 if (isa
<FPMathOperator
>(PN
))
1804 Builder
.setFastMathFlags(PN
.getFastMathFlags());
1806 SI
= cast
<SelectInst
>(Builder
.CreateSelect(
1807 BI
->getCondition(), BB1V
, BB2V
,
1808 BB1V
->getName() + "." + BB2V
->getName(), BI
));
1811 // Make the PHI node use the select for all incoming values for BB1/BB2
1812 for (unsigned i
= 0, e
= PN
.getNumIncomingValues(); i
!= e
; ++i
)
1813 if (PN
.getIncomingBlock(i
) == BB1
|| PN
.getIncomingBlock(i
) == BB2
)
1814 PN
.setIncomingValue(i
, SI
);
1819 SmallVector
<DominatorTree::UpdateType
, 4> Updates
;
1821 // Update any PHI nodes in our new successors.
1822 for (BasicBlock
*Succ
: successors(BB1
)) {
1823 AddPredecessorToBlock(Succ
, TIParent
, BB1
);
1825 Updates
.push_back({DominatorTree::Insert
, TIParent
, Succ
});
1829 for (BasicBlock
*Succ
: successors(TI
))
1830 Updates
.push_back({DominatorTree::Delete
, TIParent
, Succ
});
1832 EraseTerminatorAndDCECond(TI
);
1834 DTU
->applyUpdates(Updates
);
1838 // Check lifetime markers.
1839 static bool isLifeTimeMarker(const Instruction
*I
) {
1840 if (auto II
= dyn_cast
<IntrinsicInst
>(I
)) {
1841 switch (II
->getIntrinsicID()) {
1844 case Intrinsic::lifetime_start
:
1845 case Intrinsic::lifetime_end
:
1852 // TODO: Refine this. This should avoid cases like turning constant memcpy sizes
1854 static bool replacingOperandWithVariableIsCheap(const Instruction
*I
,
1856 return !isa
<IntrinsicInst
>(I
);
1859 // All instructions in Insts belong to different blocks that all unconditionally
1860 // branch to a common successor. Analyze each instruction and return true if it
1861 // would be possible to sink them into their successor, creating one common
1862 // instruction instead. For every value that would be required to be provided by
1863 // PHI node (because an operand varies in each input block), add to PHIOperands.
1864 static bool canSinkInstructions(
1865 ArrayRef
<Instruction
*> Insts
,
1866 DenseMap
<Instruction
*, SmallVector
<Value
*, 4>> &PHIOperands
) {
1867 // Prune out obviously bad instructions to move. Each instruction must have
1868 // exactly zero or one use, and we check later that use is by a single, common
1869 // PHI instruction in the successor.
1870 bool HasUse
= !Insts
.front()->user_empty();
1871 for (auto *I
: Insts
) {
1872 // These instructions may change or break semantics if moved.
1873 if (isa
<PHINode
>(I
) || I
->isEHPad() || isa
<AllocaInst
>(I
) ||
1874 I
->getType()->isTokenTy())
1877 // Do not try to sink an instruction in an infinite loop - it can cause
1878 // this algorithm to infinite loop.
1879 if (I
->getParent()->getSingleSuccessor() == I
->getParent())
1882 // Conservatively return false if I is an inline-asm instruction. Sinking
1883 // and merging inline-asm instructions can potentially create arguments
1884 // that cannot satisfy the inline-asm constraints.
1885 // If the instruction has nomerge or convergent attribute, return false.
1886 if (const auto *C
= dyn_cast
<CallBase
>(I
))
1887 if (C
->isInlineAsm() || C
->cannotMerge() || C
->isConvergent())
1890 // Each instruction must have zero or one use.
1891 if (HasUse
&& !I
->hasOneUse())
1893 if (!HasUse
&& !I
->user_empty())
1897 const Instruction
*I0
= Insts
.front();
1898 for (auto *I
: Insts
) {
1899 if (!I
->isSameOperationAs(I0
))
1902 // swifterror pointers can only be used by a load or store; sinking a load
1903 // or store would require introducing a select for the pointer operand,
1904 // which isn't allowed for swifterror pointers.
1905 if (isa
<StoreInst
>(I
) && I
->getOperand(1)->isSwiftError())
1907 if (isa
<LoadInst
>(I
) && I
->getOperand(0)->isSwiftError())
1911 // All instructions in Insts are known to be the same opcode. If they have a
1912 // use, check that the only user is a PHI or in the same block as the
1913 // instruction, because if a user is in the same block as an instruction we're
1914 // contemplating sinking, it must already be determined to be sinkable.
1916 auto *PNUse
= dyn_cast
<PHINode
>(*I0
->user_begin());
1917 auto *Succ
= I0
->getParent()->getTerminator()->getSuccessor(0);
1918 if (!all_of(Insts
, [&PNUse
,&Succ
](const Instruction
*I
) -> bool {
1919 auto *U
= cast
<Instruction
>(*I
->user_begin());
1921 PNUse
->getParent() == Succ
&&
1922 PNUse
->getIncomingValueForBlock(I
->getParent()) == I
) ||
1923 U
->getParent() == I
->getParent();
1928 // Because SROA can't handle speculating stores of selects, try not to sink
1929 // loads, stores or lifetime markers of allocas when we'd have to create a
1930 // PHI for the address operand. Also, because it is likely that loads or
1931 // stores of allocas will disappear when Mem2Reg/SROA is run, don't sink
1933 // This can cause code churn which can have unintended consequences down
1934 // the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
1935 // FIXME: This is a workaround for a deficiency in SROA - see
1936 // https://llvm.org/bugs/show_bug.cgi?id=30188
1937 if (isa
<StoreInst
>(I0
) && any_of(Insts
, [](const Instruction
*I
) {
1938 return isa
<AllocaInst
>(I
->getOperand(1)->stripPointerCasts());
1941 if (isa
<LoadInst
>(I0
) && any_of(Insts
, [](const Instruction
*I
) {
1942 return isa
<AllocaInst
>(I
->getOperand(0)->stripPointerCasts());
1945 if (isLifeTimeMarker(I0
) && any_of(Insts
, [](const Instruction
*I
) {
1946 return isa
<AllocaInst
>(I
->getOperand(1)->stripPointerCasts());
1950 // For calls to be sinkable, they must all be indirect, or have same callee.
1951 // I.e. if we have two direct calls to different callees, we don't want to
1952 // turn that into an indirect call. Likewise, if we have an indirect call,
1953 // and a direct call, we don't actually want to have a single indirect call.
1954 if (isa
<CallBase
>(I0
)) {
1955 auto IsIndirectCall
= [](const Instruction
*I
) {
1956 return cast
<CallBase
>(I
)->isIndirectCall();
1958 bool HaveIndirectCalls
= any_of(Insts
, IsIndirectCall
);
1959 bool AllCallsAreIndirect
= all_of(Insts
, IsIndirectCall
);
1960 if (HaveIndirectCalls
) {
1961 if (!AllCallsAreIndirect
)
1964 // All callees must be identical.
1965 Value
*Callee
= nullptr;
1966 for (const Instruction
*I
: Insts
) {
1967 Value
*CurrCallee
= cast
<CallBase
>(I
)->getCalledOperand();
1969 Callee
= CurrCallee
;
1970 else if (Callee
!= CurrCallee
)
1976 for (unsigned OI
= 0, OE
= I0
->getNumOperands(); OI
!= OE
; ++OI
) {
1977 Value
*Op
= I0
->getOperand(OI
);
1978 if (Op
->getType()->isTokenTy())
1979 // Don't touch any operand of token type.
1982 auto SameAsI0
= [&I0
, OI
](const Instruction
*I
) {
1983 assert(I
->getNumOperands() == I0
->getNumOperands());
1984 return I
->getOperand(OI
) == I0
->getOperand(OI
);
1986 if (!all_of(Insts
, SameAsI0
)) {
1987 if ((isa
<Constant
>(Op
) && !replacingOperandWithVariableIsCheap(I0
, OI
)) ||
1988 !canReplaceOperandWithVariable(I0
, OI
))
1989 // We can't create a PHI from this GEP.
1991 for (auto *I
: Insts
)
1992 PHIOperands
[I
].push_back(I
->getOperand(OI
));
1998 // Assuming canSinkInstructions(Blocks) has returned true, sink the last
1999 // instruction of every block in Blocks to their common successor, commoning
2000 // into one instruction.
2001 static bool sinkLastInstruction(ArrayRef
<BasicBlock
*> Blocks
) {
2002 auto *BBEnd
= Blocks
[0]->getTerminator()->getSuccessor(0);
2004 // canSinkInstructions returning true guarantees that every block has at
2005 // least one non-terminator instruction.
2006 SmallVector
<Instruction
*,4> Insts
;
2007 for (auto *BB
: Blocks
) {
2008 Instruction
*I
= BB
->getTerminator();
2010 I
= I
->getPrevNode();
2011 } while (isa
<DbgInfoIntrinsic
>(I
) && I
!= &BB
->front());
2012 if (!isa
<DbgInfoIntrinsic
>(I
))
2016 // The only checking we need to do now is that all users of all instructions
2017 // are the same PHI node. canSinkInstructions should have checked this but
2018 // it is slightly over-aggressive - it gets confused by commutative
2019 // instructions so double-check it here.
2020 Instruction
*I0
= Insts
.front();
2021 if (!I0
->user_empty()) {
2022 auto *PNUse
= dyn_cast
<PHINode
>(*I0
->user_begin());
2023 if (!all_of(Insts
, [&PNUse
](const Instruction
*I
) -> bool {
2024 auto *U
= cast
<Instruction
>(*I
->user_begin());
2030 // We don't need to do any more checking here; canSinkInstructions should
2031 // have done it all for us.
2032 SmallVector
<Value
*, 4> NewOperands
;
2033 for (unsigned O
= 0, E
= I0
->getNumOperands(); O
!= E
; ++O
) {
2034 // This check is different to that in canSinkInstructions. There, we
2035 // cared about the global view once simplifycfg (and instcombine) have
2036 // completed - it takes into account PHIs that become trivially
2037 // simplifiable. However here we need a more local view; if an operand
2038 // differs we create a PHI and rely on instcombine to clean up the very
2039 // small mess we may make.
2040 bool NeedPHI
= any_of(Insts
, [&I0
, O
](const Instruction
*I
) {
2041 return I
->getOperand(O
) != I0
->getOperand(O
);
2044 NewOperands
.push_back(I0
->getOperand(O
));
2048 // Create a new PHI in the successor block and populate it.
2049 auto *Op
= I0
->getOperand(O
);
2050 assert(!Op
->getType()->isTokenTy() && "Can't PHI tokens!");
2052 PHINode::Create(Op
->getType(), Insts
.size(), Op
->getName() + ".sink");
2053 PN
->insertBefore(BBEnd
->begin());
2054 for (auto *I
: Insts
)
2055 PN
->addIncoming(I
->getOperand(O
), I
->getParent());
2056 NewOperands
.push_back(PN
);
2059 // Arbitrarily use I0 as the new "common" instruction; remap its operands
2060 // and move it to the start of the successor block.
2061 for (unsigned O
= 0, E
= I0
->getNumOperands(); O
!= E
; ++O
)
2062 I0
->getOperandUse(O
).set(NewOperands
[O
]);
2064 I0
->moveBefore(*BBEnd
, BBEnd
->getFirstInsertionPt());
2066 // Update metadata and IR flags, and merge debug locations.
2067 for (auto *I
: Insts
)
2069 // The debug location for the "common" instruction is the merged locations
2070 // of all the commoned instructions. We start with the original location
2071 // of the "common" instruction and iteratively merge each location in the
2073 // This is an N-way merge, which will be inefficient if I0 is a CallInst.
2074 // However, as N-way merge for CallInst is rare, so we use simplified API
2075 // instead of using complex API for N-way merge.
2076 I0
->applyMergedLocation(I0
->getDebugLoc(), I
->getDebugLoc());
2077 combineMetadataForCSE(I0
, I
, true);
2081 if (!I0
->user_empty()) {
2082 // canSinkLastInstruction checked that all instructions were used by
2083 // one and only one PHI node. Find that now, RAUW it to our common
2084 // instruction and nuke it.
2085 auto *PN
= cast
<PHINode
>(*I0
->user_begin());
2086 PN
->replaceAllUsesWith(I0
);
2087 PN
->eraseFromParent();
2090 // Finally nuke all instructions apart from the common instruction.
2091 for (auto *I
: Insts
) {
2094 // The remaining uses are debug users, replace those with the common inst.
2095 // In most (all?) cases this just introduces a use-before-def.
2096 assert(I
->user_empty() && "Inst unexpectedly still has non-dbg users");
2097 I
->replaceAllUsesWith(I0
);
2098 I
->eraseFromParent();
2106 // LockstepReverseIterator - Iterates through instructions
2107 // in a set of blocks in reverse order from the first non-terminator.
2108 // For example (assume all blocks have size n):
2109 // LockstepReverseIterator I([B1, B2, B3]);
2110 // *I-- = [B1[n], B2[n], B3[n]];
2111 // *I-- = [B1[n-1], B2[n-1], B3[n-1]];
2112 // *I-- = [B1[n-2], B2[n-2], B3[n-2]];
2114 class LockstepReverseIterator
{
2115 ArrayRef
<BasicBlock
*> Blocks
;
2116 SmallVector
<Instruction
*,4> Insts
;
2120 LockstepReverseIterator(ArrayRef
<BasicBlock
*> Blocks
) : Blocks(Blocks
) {
2127 for (auto *BB
: Blocks
) {
2128 Instruction
*Inst
= BB
->getTerminator();
2129 for (Inst
= Inst
->getPrevNode(); Inst
&& isa
<DbgInfoIntrinsic
>(Inst
);)
2130 Inst
= Inst
->getPrevNode();
2132 // Block wasn't big enough.
2136 Insts
.push_back(Inst
);
2140 bool isValid() const {
2147 for (auto *&Inst
: Insts
) {
2148 for (Inst
= Inst
->getPrevNode(); Inst
&& isa
<DbgInfoIntrinsic
>(Inst
);)
2149 Inst
= Inst
->getPrevNode();
2150 // Already at beginning of block.
2161 for (auto *&Inst
: Insts
) {
2162 for (Inst
= Inst
->getNextNode(); Inst
&& isa
<DbgInfoIntrinsic
>(Inst
);)
2163 Inst
= Inst
->getNextNode();
2164 // Already at end of block.
2172 ArrayRef
<Instruction
*> operator * () const {
2177 } // end anonymous namespace
2179 /// Check whether BB's predecessors end with unconditional branches. If it is
2180 /// true, sink any common code from the predecessors to BB.
2181 static bool SinkCommonCodeFromPredecessors(BasicBlock
*BB
,
2182 DomTreeUpdater
*DTU
) {
2183 // We support two situations:
2184 // (1) all incoming arcs are unconditional
2185 // (2) there are non-unconditional incoming arcs
2187 // (2) is very common in switch defaults and
2188 // else-if patterns;
2191 // else if (b) f(2);
2204 // [end] has two unconditional predecessor arcs and one conditional. The
2205 // conditional refers to the implicit empty 'else' arc. This conditional
2206 // arc can also be caused by an empty default block in a switch.
2208 // In this case, we attempt to sink code from all *unconditional* arcs.
2209 // If we can sink instructions from these arcs (determined during the scan
2210 // phase below) we insert a common successor for all unconditional arcs and
2211 // connect that to [end], to enable sinking:
2224 SmallVector
<BasicBlock
*,4> UnconditionalPreds
;
2225 bool HaveNonUnconditionalPredecessors
= false;
2226 for (auto *PredBB
: predecessors(BB
)) {
2227 auto *PredBr
= dyn_cast
<BranchInst
>(PredBB
->getTerminator());
2228 if (PredBr
&& PredBr
->isUnconditional())
2229 UnconditionalPreds
.push_back(PredBB
);
2231 HaveNonUnconditionalPredecessors
= true;
2233 if (UnconditionalPreds
.size() < 2)
2236 // We take a two-step approach to tail sinking. First we scan from the end of
2237 // each block upwards in lockstep. If the n'th instruction from the end of each
2238 // block can be sunk, those instructions are added to ValuesToSink and we
2239 // carry on. If we can sink an instruction but need to PHI-merge some operands
2240 // (because they're not identical in each instruction) we add these to
2243 SmallPtrSet
<Value
*,4> InstructionsToSink
;
2244 DenseMap
<Instruction
*, SmallVector
<Value
*,4>> PHIOperands
;
2245 LockstepReverseIterator
LRI(UnconditionalPreds
);
2246 while (LRI
.isValid() &&
2247 canSinkInstructions(*LRI
, PHIOperands
)) {
2248 LLVM_DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI
)[0]
2250 InstructionsToSink
.insert((*LRI
).begin(), (*LRI
).end());
2255 // If no instructions can be sunk, early-return.
2259 bool followedByDeoptOrUnreachable
= IsBlockFollowedByDeoptOrUnreachable(BB
);
2261 if (!followedByDeoptOrUnreachable
) {
2262 // Okay, we *could* sink last ScanIdx instructions. But how many can we
2263 // actually sink before encountering instruction that is unprofitable to
2265 auto ProfitableToSinkInstruction
= [&](LockstepReverseIterator
&LRI
) {
2266 unsigned NumPHIdValues
= 0;
2267 for (auto *I
: *LRI
)
2268 for (auto *V
: PHIOperands
[I
]) {
2269 if (!InstructionsToSink
.contains(V
))
2271 // FIXME: this check is overly optimistic. We may end up not sinking
2272 // said instruction, due to the very same profitability check.
2273 // See @creating_too_many_phis in sink-common-code.ll.
2275 LLVM_DEBUG(dbgs() << "SINK: #phid values: " << NumPHIdValues
<< "\n");
2276 unsigned NumPHIInsts
= NumPHIdValues
/ UnconditionalPreds
.size();
2277 if ((NumPHIdValues
% UnconditionalPreds
.size()) != 0)
2280 return NumPHIInsts
<= 1;
2283 // We've determined that we are going to sink last ScanIdx instructions,
2284 // and recorded them in InstructionsToSink. Now, some instructions may be
2285 // unprofitable to sink. But that determination depends on the instructions
2286 // that we are going to sink.
2288 // First, forward scan: find the first instruction unprofitable to sink,
2289 // recording all the ones that are profitable to sink.
2290 // FIXME: would it be better, after we detect that not all are profitable.
2291 // to either record the profitable ones, or erase the unprofitable ones?
2292 // Maybe we need to choose (at runtime) the one that will touch least
2296 SmallPtrSet
<Value
*, 4> InstructionsProfitableToSink
;
2297 while (Idx
< ScanIdx
) {
2298 if (!ProfitableToSinkInstruction(LRI
)) {
2299 // Too many PHIs would be created.
2301 dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
2304 InstructionsProfitableToSink
.insert((*LRI
).begin(), (*LRI
).end());
2309 // If no instructions can be sunk, early-return.
2313 // Did we determine that (only) some instructions are unprofitable to sink?
2314 if (Idx
< ScanIdx
) {
2315 // Okay, some instructions are unprofitable.
2317 InstructionsToSink
= InstructionsProfitableToSink
;
2319 // But, that may make other instructions unprofitable, too.
2320 // So, do a backward scan, do any earlier instructions become
2323 !ProfitableToSinkInstruction(LRI
) &&
2324 "We already know that the last instruction is unprofitable to sink");
2328 // If we detect that an instruction becomes unprofitable to sink,
2329 // all earlier instructions won't be sunk either,
2330 // so preemptively keep InstructionsProfitableToSink in sync.
2331 // FIXME: is this the most performant approach?
2332 for (auto *I
: *LRI
)
2333 InstructionsProfitableToSink
.erase(I
);
2334 if (!ProfitableToSinkInstruction(LRI
)) {
2335 // Everything starting with this instruction won't be sunk.
2337 InstructionsToSink
= InstructionsProfitableToSink
;
2344 // If no instructions can be sunk, early-return.
2349 bool Changed
= false;
2351 if (HaveNonUnconditionalPredecessors
) {
2352 if (!followedByDeoptOrUnreachable
) {
2353 // It is always legal to sink common instructions from unconditional
2354 // predecessors. However, if not all predecessors are unconditional,
2355 // this transformation might be pessimizing. So as a rule of thumb,
2356 // don't do it unless we'd sink at least one non-speculatable instruction.
2357 // See https://bugs.llvm.org/show_bug.cgi?id=30244
2360 bool Profitable
= false;
2361 while (Idx
< ScanIdx
) {
2362 if (!isSafeToSpeculativelyExecute((*LRI
)[0])) {
2373 LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
2374 // We have a conditional edge and we're going to sink some instructions.
2375 // Insert a new block postdominating all blocks we're going to sink from.
2376 if (!SplitBlockPredecessors(BB
, UnconditionalPreds
, ".sink.split", DTU
))
2377 // Edges couldn't be split.
2382 // Now that we've analyzed all potential sinking candidates, perform the
2383 // actual sink. We iteratively sink the last non-terminator of the source
2384 // blocks into their common successor unless doing so would require too
2385 // many PHI instructions to be generated (currently only one PHI is allowed
2386 // per sunk instruction).
2388 // We can use InstructionsToSink to discount values needing PHI-merging that will
2389 // actually be sunk in a later iteration. This allows us to be more
2390 // aggressive in what we sink. This does allow a false positive where we
2391 // sink presuming a later value will also be sunk, but stop half way through
2392 // and never actually sink it which means we produce more PHIs than intended.
2393 // This is unlikely in practice though.
2395 for (; SinkIdx
!= ScanIdx
; ++SinkIdx
) {
2396 LLVM_DEBUG(dbgs() << "SINK: Sink: "
2397 << *UnconditionalPreds
[0]->getTerminator()->getPrevNode()
2400 // Because we've sunk every instruction in turn, the current instruction to
2401 // sink is always at index 0.
2404 if (!sinkLastInstruction(UnconditionalPreds
)) {
2407 << "SINK: stopping here, failed to actually sink instruction!\n");
2411 NumSinkCommonInstrs
++;
2415 ++NumSinkCommonCode
;
2421 struct CompatibleSets
{
2422 using SetTy
= SmallVector
<InvokeInst
*, 2>;
2424 SmallVector
<SetTy
, 1> Sets
;
2426 static bool shouldBelongToSameSet(ArrayRef
<InvokeInst
*> Invokes
);
2428 SetTy
&getCompatibleSet(InvokeInst
*II
);
2430 void insert(InvokeInst
*II
);
2433 CompatibleSets::SetTy
&CompatibleSets::getCompatibleSet(InvokeInst
*II
) {
2434 // Perform a linear scan over all the existing sets, see if the new `invoke`
2435 // is compatible with any particular set. Since we know that all the `invokes`
2436 // within a set are compatible, only check the first `invoke` in each set.
2437 // WARNING: at worst, this has quadratic complexity.
2438 for (CompatibleSets::SetTy
&Set
: Sets
) {
2439 if (CompatibleSets::shouldBelongToSameSet({Set
.front(), II
}))
2443 // Otherwise, we either had no sets yet, or this invoke forms a new set.
2444 return Sets
.emplace_back();
2447 void CompatibleSets::insert(InvokeInst
*II
) {
2448 getCompatibleSet(II
).emplace_back(II
);
2451 bool CompatibleSets::shouldBelongToSameSet(ArrayRef
<InvokeInst
*> Invokes
) {
2452 assert(Invokes
.size() == 2 && "Always called with exactly two candidates.");
2454 // Can we theoretically merge these `invoke`s?
2455 auto IsIllegalToMerge
= [](InvokeInst
*II
) {
2456 return II
->cannotMerge() || II
->isInlineAsm();
2458 if (any_of(Invokes
, IsIllegalToMerge
))
2461 // Either both `invoke`s must be direct,
2462 // or both `invoke`s must be indirect.
2463 auto IsIndirectCall
= [](InvokeInst
*II
) { return II
->isIndirectCall(); };
2464 bool HaveIndirectCalls
= any_of(Invokes
, IsIndirectCall
);
2465 bool AllCallsAreIndirect
= all_of(Invokes
, IsIndirectCall
);
2466 if (HaveIndirectCalls
) {
2467 if (!AllCallsAreIndirect
)
2470 // All callees must be identical.
2471 Value
*Callee
= nullptr;
2472 for (InvokeInst
*II
: Invokes
) {
2473 Value
*CurrCallee
= II
->getCalledOperand();
2474 assert(CurrCallee
&& "There is always a called operand.");
2476 Callee
= CurrCallee
;
2477 else if (Callee
!= CurrCallee
)
2482 // Either both `invoke`s must not have a normal destination,
2483 // or both `invoke`s must have a normal destination,
2484 auto HasNormalDest
= [](InvokeInst
*II
) {
2485 return !isa
<UnreachableInst
>(II
->getNormalDest()->getFirstNonPHIOrDbg());
2487 if (any_of(Invokes
, HasNormalDest
)) {
2488 // Do not merge `invoke` that does not have a normal destination with one
2489 // that does have a normal destination, even though doing so would be legal.
2490 if (!all_of(Invokes
, HasNormalDest
))
2493 // All normal destinations must be identical.
2494 BasicBlock
*NormalBB
= nullptr;
2495 for (InvokeInst
*II
: Invokes
) {
2496 BasicBlock
*CurrNormalBB
= II
->getNormalDest();
2497 assert(CurrNormalBB
&& "There is always a 'continue to' basic block.");
2499 NormalBB
= CurrNormalBB
;
2500 else if (NormalBB
!= CurrNormalBB
)
2504 // In the normal destination, the incoming values for these two `invoke`s
2505 // must be compatible.
2506 SmallPtrSet
<Value
*, 16> EquivalenceSet(Invokes
.begin(), Invokes
.end());
2507 if (!IncomingValuesAreCompatible(
2508 NormalBB
, {Invokes
[0]->getParent(), Invokes
[1]->getParent()},
2514 // All unwind destinations must be identical.
2515 // We know that because we have started from said unwind destination.
2516 BasicBlock
*UnwindBB
= nullptr;
2517 for (InvokeInst
*II
: Invokes
) {
2518 BasicBlock
*CurrUnwindBB
= II
->getUnwindDest();
2519 assert(CurrUnwindBB
&& "There is always an 'unwind to' basic block.");
2521 UnwindBB
= CurrUnwindBB
;
2523 assert(UnwindBB
== CurrUnwindBB
&& "Unexpected unwind destination.");
2527 // In the unwind destination, the incoming values for these two `invoke`s
2528 // must be compatible.
2529 if (!IncomingValuesAreCompatible(
2530 Invokes
.front()->getUnwindDest(),
2531 {Invokes
[0]->getParent(), Invokes
[1]->getParent()}))
2534 // Ignoring arguments, these `invoke`s must be identical,
2535 // including operand bundles.
2536 const InvokeInst
*II0
= Invokes
.front();
2537 for (auto *II
: Invokes
.drop_front())
2538 if (!II
->isSameOperationAs(II0
))
2541 // Can we theoretically form the data operands for the merged `invoke`?
2542 auto IsIllegalToMergeArguments
= [](auto Ops
) {
2543 Use
&U0
= std::get
<0>(Ops
);
2544 Use
&U1
= std::get
<1>(Ops
);
2547 return U0
->getType()->isTokenTy() ||
2548 !canReplaceOperandWithVariable(cast
<Instruction
>(U0
.getUser()),
2551 assert(Invokes
.size() == 2 && "Always called with exactly two candidates.");
2552 if (any_of(zip(Invokes
[0]->data_ops(), Invokes
[1]->data_ops()),
2553 IsIllegalToMergeArguments
))
2561 // Merge all invokes in the provided set, all of which are compatible
2562 // as per the `CompatibleSets::shouldBelongToSameSet()`.
2563 static void MergeCompatibleInvokesImpl(ArrayRef
<InvokeInst
*> Invokes
,
2564 DomTreeUpdater
*DTU
) {
2565 assert(Invokes
.size() >= 2 && "Must have at least two invokes to merge.");
2567 SmallVector
<DominatorTree::UpdateType
, 8> Updates
;
2569 Updates
.reserve(2 + 3 * Invokes
.size());
2571 bool HasNormalDest
=
2572 !isa
<UnreachableInst
>(Invokes
[0]->getNormalDest()->getFirstNonPHIOrDbg());
2574 // Clone one of the invokes into a new basic block.
2575 // Since they are all compatible, it doesn't matter which invoke is cloned.
2576 InvokeInst
*MergedInvoke
= [&Invokes
, HasNormalDest
]() {
2577 InvokeInst
*II0
= Invokes
.front();
2578 BasicBlock
*II0BB
= II0
->getParent();
2579 BasicBlock
*InsertBeforeBlock
=
2580 II0
->getParent()->getIterator()->getNextNode();
2581 Function
*Func
= II0BB
->getParent();
2582 LLVMContext
&Ctx
= II0
->getContext();
2584 BasicBlock
*MergedInvokeBB
= BasicBlock::Create(
2585 Ctx
, II0BB
->getName() + ".invoke", Func
, InsertBeforeBlock
);
2587 auto *MergedInvoke
= cast
<InvokeInst
>(II0
->clone());
2588 // NOTE: all invokes have the same attributes, so no handling needed.
2589 MergedInvoke
->insertInto(MergedInvokeBB
, MergedInvokeBB
->end());
2591 if (!HasNormalDest
) {
2592 // This set does not have a normal destination,
2593 // so just form a new block with unreachable terminator.
2594 BasicBlock
*MergedNormalDest
= BasicBlock::Create(
2595 Ctx
, II0BB
->getName() + ".cont", Func
, InsertBeforeBlock
);
2596 new UnreachableInst(Ctx
, MergedNormalDest
);
2597 MergedInvoke
->setNormalDest(MergedNormalDest
);
2600 // The unwind destination, however, remainds identical for all invokes here.
2602 return MergedInvoke
;
2606 // Predecessor blocks that contained these invokes will now branch to
2607 // the new block that contains the merged invoke, ...
2608 for (InvokeInst
*II
: Invokes
)
2610 {DominatorTree::Insert
, II
->getParent(), MergedInvoke
->getParent()});
2612 // ... which has the new `unreachable` block as normal destination,
2613 // or unwinds to the (same for all `invoke`s in this set) `landingpad`,
2614 for (BasicBlock
*SuccBBOfMergedInvoke
: successors(MergedInvoke
))
2615 Updates
.push_back({DominatorTree::Insert
, MergedInvoke
->getParent(),
2616 SuccBBOfMergedInvoke
});
2618 // Since predecessor blocks now unconditionally branch to a new block,
2619 // they no longer branch to their original successors.
2620 for (InvokeInst
*II
: Invokes
)
2621 for (BasicBlock
*SuccOfPredBB
: successors(II
->getParent()))
2623 {DominatorTree::Delete
, II
->getParent(), SuccOfPredBB
});
2626 bool IsIndirectCall
= Invokes
[0]->isIndirectCall();
2628 // Form the merged operands for the merged invoke.
2629 for (Use
&U
: MergedInvoke
->operands()) {
2630 // Only PHI together the indirect callees and data operands.
2631 if (MergedInvoke
->isCallee(&U
)) {
2632 if (!IsIndirectCall
)
2634 } else if (!MergedInvoke
->isDataOperand(&U
))
2637 // Don't create trivial PHI's with all-identical incoming values.
2638 bool NeedPHI
= any_of(Invokes
, [&U
](InvokeInst
*II
) {
2639 return II
->getOperand(U
.getOperandNo()) != U
.get();
2644 // Form a PHI out of all the data ops under this index.
2645 PHINode
*PN
= PHINode::Create(
2646 U
->getType(), /*NumReservedValues=*/Invokes
.size(), "", MergedInvoke
);
2647 for (InvokeInst
*II
: Invokes
)
2648 PN
->addIncoming(II
->getOperand(U
.getOperandNo()), II
->getParent());
2653 // We've ensured that each PHI node has compatible (identical) incoming values
2654 // when coming from each of the `invoke`s in the current merge set,
2655 // so update the PHI nodes accordingly.
2656 for (BasicBlock
*Succ
: successors(MergedInvoke
))
2657 AddPredecessorToBlock(Succ
, /*NewPred=*/MergedInvoke
->getParent(),
2658 /*ExistPred=*/Invokes
.front()->getParent());
2660 // And finally, replace the original `invoke`s with an unconditional branch
2661 // to the block with the merged `invoke`. Also, give that merged `invoke`
2662 // the merged debugloc of all the original `invoke`s.
2663 DILocation
*MergedDebugLoc
= nullptr;
2664 for (InvokeInst
*II
: Invokes
) {
2665 // Compute the debug location common to all the original `invoke`s.
2666 if (!MergedDebugLoc
)
2667 MergedDebugLoc
= II
->getDebugLoc();
2670 DILocation::getMergedLocation(MergedDebugLoc
, II
->getDebugLoc());
2672 // And replace the old `invoke` with an unconditionally branch
2673 // to the block with the merged `invoke`.
2674 for (BasicBlock
*OrigSuccBB
: successors(II
->getParent()))
2675 OrigSuccBB
->removePredecessor(II
->getParent());
2676 BranchInst::Create(MergedInvoke
->getParent(), II
->getParent());
2677 II
->replaceAllUsesWith(MergedInvoke
);
2678 II
->eraseFromParent();
2681 MergedInvoke
->setDebugLoc(MergedDebugLoc
);
2682 ++NumInvokeSetsFormed
;
2685 DTU
->applyUpdates(Updates
);
2688 /// If this block is a `landingpad` exception handling block, categorize all
2689 /// the predecessor `invoke`s into sets, with all `invoke`s in each set
2690 /// being "mergeable" together, and then merge invokes in each set together.
2692 /// This is a weird mix of hoisting and sinking. Visually, it goes from:
2695 /// [invoke0] [invoke1]
2697 /// [cont0] [landingpad] [cont1]
2703 /// [cont] [landingpad]
2705 /// But of course we can only do that if the invokes share the `landingpad`,
2706 /// edges invoke0->cont0 and invoke1->cont1 are "compatible",
2707 /// and the invoked functions are "compatible".
2708 static bool MergeCompatibleInvokes(BasicBlock
*BB
, DomTreeUpdater
*DTU
) {
2709 if (!EnableMergeCompatibleInvokes
)
2712 bool Changed
= false;
2714 // FIXME: generalize to all exception handling blocks?
2715 if (!BB
->isLandingPad())
2718 CompatibleSets Grouper
;
2720 // Record all the predecessors of this `landingpad`. As per verifier,
2721 // the only allowed predecessor is the unwind edge of an `invoke`.
2722 // We want to group "compatible" `invokes` into the same set to be merged.
2723 for (BasicBlock
*PredBB
: predecessors(BB
))
2724 Grouper
.insert(cast
<InvokeInst
>(PredBB
->getTerminator()));
2726 // And now, merge `invoke`s that were grouped togeter.
2727 for (ArrayRef
<InvokeInst
*> Invokes
: Grouper
.Sets
) {
2728 if (Invokes
.size() < 2)
2731 MergeCompatibleInvokesImpl(Invokes
, DTU
);
2738 /// Track ephemeral values, which should be ignored for cost-modelling
2739 /// purposes. Requires walking instructions in reverse order.
2740 class EphemeralValueTracker
{
2741 SmallPtrSet
<const Instruction
*, 32> EphValues
;
2743 bool isEphemeral(const Instruction
*I
) {
2744 if (isa
<AssumeInst
>(I
))
2746 return !I
->mayHaveSideEffects() && !I
->isTerminator() &&
2747 all_of(I
->users(), [&](const User
*U
) {
2748 return EphValues
.count(cast
<Instruction
>(U
));
2753 bool track(const Instruction
*I
) {
2754 if (isEphemeral(I
)) {
2755 EphValues
.insert(I
);
2761 bool contains(const Instruction
*I
) const { return EphValues
.contains(I
); }
2765 /// Determine if we can hoist sink a sole store instruction out of a
2766 /// conditional block.
2768 /// We are looking for code like the following:
2770 /// store i32 %add, i32* %arrayidx2
2771 /// ... // No other stores or function calls (we could be calling a memory
2772 /// ... // function).
2773 /// %cmp = icmp ult %x, %y
2774 /// br i1 %cmp, label %EndBB, label %ThenBB
2776 /// store i32 %add5, i32* %arrayidx2
2780 /// We are going to transform this into:
2782 /// store i32 %add, i32* %arrayidx2
2784 /// %cmp = icmp ult %x, %y
2785 /// %add.add5 = select i1 %cmp, i32 %add, %add5
2786 /// store i32 %add.add5, i32* %arrayidx2
2789 /// \return The pointer to the value of the previous store if the store can be
2790 /// hoisted into the predecessor block. 0 otherwise.
2791 static Value
*isSafeToSpeculateStore(Instruction
*I
, BasicBlock
*BrBB
,
2792 BasicBlock
*StoreBB
, BasicBlock
*EndBB
) {
2793 StoreInst
*StoreToHoist
= dyn_cast
<StoreInst
>(I
);
2797 // Volatile or atomic.
2798 if (!StoreToHoist
->isSimple())
2801 Value
*StorePtr
= StoreToHoist
->getPointerOperand();
2802 Type
*StoreTy
= StoreToHoist
->getValueOperand()->getType();
2804 // Look for a store to the same pointer in BrBB.
2805 unsigned MaxNumInstToLookAt
= 9;
2806 // Skip pseudo probe intrinsic calls which are not really killing any memory
2808 for (Instruction
&CurI
: reverse(BrBB
->instructionsWithoutDebug(true))) {
2809 if (!MaxNumInstToLookAt
)
2811 --MaxNumInstToLookAt
;
2813 // Could be calling an instruction that affects memory like free().
2814 if (CurI
.mayWriteToMemory() && !isa
<StoreInst
>(CurI
))
2817 if (auto *SI
= dyn_cast
<StoreInst
>(&CurI
)) {
2818 // Found the previous store to same location and type. Make sure it is
2819 // simple, to avoid introducing a spurious non-atomic write after an
2821 if (SI
->getPointerOperand() == StorePtr
&&
2822 SI
->getValueOperand()->getType() == StoreTy
&& SI
->isSimple())
2823 // Found the previous store, return its value operand.
2824 return SI
->getValueOperand();
2825 return nullptr; // Unknown store.
2828 if (auto *LI
= dyn_cast
<LoadInst
>(&CurI
)) {
2829 if (LI
->getPointerOperand() == StorePtr
&& LI
->getType() == StoreTy
&&
2831 // Local objects (created by an `alloca` instruction) are always
2832 // writable, so once we are past a read from a location it is valid to
2833 // also write to that same location.
2834 // If the address of the local object never escapes the function, that
2835 // means it's never concurrently read or written, hence moving the store
2836 // from under the condition will not introduce a data race.
2837 auto *AI
= dyn_cast
<AllocaInst
>(getUnderlyingObject(StorePtr
));
2838 if (AI
&& !PointerMayBeCaptured(AI
, false, true))
2839 // Found a previous load, return it.
2842 // The load didn't work out, but we may still find a store.
2849 /// Estimate the cost of the insertion(s) and check that the PHI nodes can be
2850 /// converted to selects.
2851 static bool validateAndCostRequiredSelects(BasicBlock
*BB
, BasicBlock
*ThenBB
,
2853 unsigned &SpeculatedInstructions
,
2854 InstructionCost
&Cost
,
2855 const TargetTransformInfo
&TTI
) {
2856 TargetTransformInfo::TargetCostKind CostKind
=
2857 BB
->getParent()->hasMinSize()
2858 ? TargetTransformInfo::TCK_CodeSize
2859 : TargetTransformInfo::TCK_SizeAndLatency
;
2861 bool HaveRewritablePHIs
= false;
2862 for (PHINode
&PN
: EndBB
->phis()) {
2863 Value
*OrigV
= PN
.getIncomingValueForBlock(BB
);
2864 Value
*ThenV
= PN
.getIncomingValueForBlock(ThenBB
);
2866 // FIXME: Try to remove some of the duplication with
2867 // hoistCommonCodeFromSuccessors. Skip PHIs which are trivial.
2871 Cost
+= TTI
.getCmpSelInstrCost(Instruction::Select
, PN
.getType(), nullptr,
2872 CmpInst::BAD_ICMP_PREDICATE
, CostKind
);
2874 // Don't convert to selects if we could remove undefined behavior instead.
2875 if (passingValueIsAlwaysUndefined(OrigV
, &PN
) ||
2876 passingValueIsAlwaysUndefined(ThenV
, &PN
))
2879 HaveRewritablePHIs
= true;
2880 ConstantExpr
*OrigCE
= dyn_cast
<ConstantExpr
>(OrigV
);
2881 ConstantExpr
*ThenCE
= dyn_cast
<ConstantExpr
>(ThenV
);
2882 if (!OrigCE
&& !ThenCE
)
2883 continue; // Known cheap (FIXME: Maybe not true for aggregates).
2885 InstructionCost OrigCost
= OrigCE
? computeSpeculationCost(OrigCE
, TTI
) : 0;
2886 InstructionCost ThenCost
= ThenCE
? computeSpeculationCost(ThenCE
, TTI
) : 0;
2887 InstructionCost MaxCost
=
2888 2 * PHINodeFoldingThreshold
* TargetTransformInfo::TCC_Basic
;
2889 if (OrigCost
+ ThenCost
> MaxCost
)
2892 // Account for the cost of an unfolded ConstantExpr which could end up
2893 // getting expanded into Instructions.
2894 // FIXME: This doesn't account for how many operations are combined in the
2895 // constant expression.
2896 ++SpeculatedInstructions
;
2897 if (SpeculatedInstructions
> 1)
2901 return HaveRewritablePHIs
;
2904 /// Speculate a conditional basic block flattening the CFG.
2906 /// Note that this is a very risky transform currently. Speculating
2907 /// instructions like this is most often not desirable. Instead, there is an MI
2908 /// pass which can do it with full awareness of the resource constraints.
2909 /// However, some cases are "obvious" and we should do directly. An example of
2910 /// this is speculating a single, reasonably cheap instruction.
2912 /// There is only one distinct advantage to flattening the CFG at the IR level:
2913 /// it makes very common but simplistic optimizations such as are common in
2914 /// instcombine and the DAG combiner more powerful by removing CFG edges and
2915 /// modeling their effects with easier to reason about SSA value graphs.
2918 /// An illustration of this transform is turning this IR:
2921 /// %cmp = icmp ult %x, %y
2922 /// br i1 %cmp, label %EndBB, label %ThenBB
2924 /// %sub = sub %x, %y
2927 /// %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
2934 /// %cmp = icmp ult %x, %y
2935 /// %sub = sub %x, %y
2936 /// %cond = select i1 %cmp, 0, %sub
2940 /// \returns true if the conditional block is removed.
2941 bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst
*BI
,
2942 BasicBlock
*ThenBB
) {
2943 if (!Options
.SpeculateBlocks
)
2946 // Be conservative for now. FP select instruction can often be expensive.
2947 Value
*BrCond
= BI
->getCondition();
2948 if (isa
<FCmpInst
>(BrCond
))
2951 BasicBlock
*BB
= BI
->getParent();
2952 BasicBlock
*EndBB
= ThenBB
->getTerminator()->getSuccessor(0);
2953 InstructionCost Budget
=
2954 PHINodeFoldingThreshold
* TargetTransformInfo::TCC_Basic
;
2956 // If ThenBB is actually on the false edge of the conditional branch, remember
2957 // to swap the select operands later.
2958 bool Invert
= false;
2959 if (ThenBB
!= BI
->getSuccessor(0)) {
2960 assert(ThenBB
== BI
->getSuccessor(1) && "No edge from 'if' block?");
2963 assert(EndBB
== BI
->getSuccessor(!Invert
) && "No edge from to end block");
2965 // If the branch is non-unpredictable, and is predicted to *not* branch to
2966 // the `then` block, then avoid speculating it.
2967 if (!BI
->getMetadata(LLVMContext::MD_unpredictable
)) {
2968 uint64_t TWeight
, FWeight
;
2969 if (extractBranchWeights(*BI
, TWeight
, FWeight
) &&
2970 (TWeight
+ FWeight
) != 0) {
2971 uint64_t EndWeight
= Invert
? TWeight
: FWeight
;
2972 BranchProbability BIEndProb
=
2973 BranchProbability::getBranchProbability(EndWeight
, TWeight
+ FWeight
);
2974 BranchProbability Likely
= TTI
.getPredictableBranchThreshold();
2975 if (BIEndProb
>= Likely
)
2980 // Keep a count of how many times instructions are used within ThenBB when
2981 // they are candidates for sinking into ThenBB. Specifically:
2982 // - They are defined in BB, and
2983 // - They have no side effects, and
2984 // - All of their uses are in ThenBB.
2985 SmallDenseMap
<Instruction
*, unsigned, 4> SinkCandidateUseCounts
;
2987 SmallVector
<Instruction
*, 4> SpeculatedDbgIntrinsics
;
2989 unsigned SpeculatedInstructions
= 0;
2990 Value
*SpeculatedStoreValue
= nullptr;
2991 StoreInst
*SpeculatedStore
= nullptr;
2992 EphemeralValueTracker EphTracker
;
2993 for (Instruction
&I
: reverse(drop_end(*ThenBB
))) {
2995 if (isa
<DbgInfoIntrinsic
>(I
)) {
2996 SpeculatedDbgIntrinsics
.push_back(&I
);
3000 // Skip pseudo probes. The consequence is we lose track of the branch
3001 // probability for ThenBB, which is fine since the optimization here takes
3002 // place regardless of the branch probability.
3003 if (isa
<PseudoProbeInst
>(I
)) {
3004 // The probe should be deleted so that it will not be over-counted when
3005 // the samples collected on the non-conditional path are counted towards
3006 // the conditional path. We leave it for the counts inference algorithm to
3007 // figure out a proper count for an unknown probe.
3008 SpeculatedDbgIntrinsics
.push_back(&I
);
3012 // Ignore ephemeral values, they will be dropped by the transform.
3013 if (EphTracker
.track(&I
))
3016 // Only speculatively execute a single instruction (not counting the
3017 // terminator) for now.
3018 ++SpeculatedInstructions
;
3019 if (SpeculatedInstructions
> 1)
3022 // Don't hoist the instruction if it's unsafe or expensive.
3023 if (!isSafeToSpeculativelyExecute(&I
) &&
3024 !(HoistCondStores
&& (SpeculatedStoreValue
= isSafeToSpeculateStore(
3025 &I
, BB
, ThenBB
, EndBB
))))
3027 if (!SpeculatedStoreValue
&&
3028 computeSpeculationCost(&I
, TTI
) >
3029 PHINodeFoldingThreshold
* TargetTransformInfo::TCC_Basic
)
3032 // Store the store speculation candidate.
3033 if (SpeculatedStoreValue
)
3034 SpeculatedStore
= cast
<StoreInst
>(&I
);
3036 // Do not hoist the instruction if any of its operands are defined but not
3037 // used in BB. The transformation will prevent the operand from
3038 // being sunk into the use block.
3039 for (Use
&Op
: I
.operands()) {
3040 Instruction
*OpI
= dyn_cast
<Instruction
>(Op
);
3041 if (!OpI
|| OpI
->getParent() != BB
|| OpI
->mayHaveSideEffects())
3042 continue; // Not a candidate for sinking.
3044 ++SinkCandidateUseCounts
[OpI
];
3048 // Consider any sink candidates which are only used in ThenBB as costs for
3049 // speculation. Note, while we iterate over a DenseMap here, we are summing
3050 // and so iteration order isn't significant.
3051 for (const auto &[Inst
, Count
] : SinkCandidateUseCounts
)
3052 if (Inst
->hasNUses(Count
)) {
3053 ++SpeculatedInstructions
;
3054 if (SpeculatedInstructions
> 1)
3058 // Check that we can insert the selects and that it's not too expensive to do
3060 bool Convert
= SpeculatedStore
!= nullptr;
3061 InstructionCost Cost
= 0;
3062 Convert
|= validateAndCostRequiredSelects(BB
, ThenBB
, EndBB
,
3063 SpeculatedInstructions
,
3065 if (!Convert
|| Cost
> Budget
)
3068 // If we get here, we can hoist the instruction and if-convert.
3069 LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB
<< "\n";);
3071 // Insert a select of the value of the speculated store.
3072 if (SpeculatedStoreValue
) {
3073 IRBuilder
<NoFolder
> Builder(BI
);
3074 Value
*OrigV
= SpeculatedStore
->getValueOperand();
3075 Value
*TrueV
= SpeculatedStore
->getValueOperand();
3076 Value
*FalseV
= SpeculatedStoreValue
;
3078 std::swap(TrueV
, FalseV
);
3079 Value
*S
= Builder
.CreateSelect(
3080 BrCond
, TrueV
, FalseV
, "spec.store.select", BI
);
3081 SpeculatedStore
->setOperand(0, S
);
3082 SpeculatedStore
->applyMergedLocation(BI
->getDebugLoc(),
3083 SpeculatedStore
->getDebugLoc());
3084 // The value stored is still conditional, but the store itself is now
3085 // unconditonally executed, so we must be sure that any linked dbg.assign
3086 // intrinsics are tracking the new stored value (the result of the
3087 // select). If we don't, and the store were to be removed by another pass
3088 // (e.g. DSE), then we'd eventually end up emitting a location describing
3089 // the conditional value, unconditionally.
3091 // === Before this transformation ===
3093 // store %one, %x.dest, !DIAssignID !1
3094 // dbg.assign %one, "x", ..., !1, ...
3098 // store %two, %x.dest, !DIAssignID !2
3099 // dbg.assign %two, "x", ..., !2, ...
3101 // === After this transformation ===
3103 // store %one, %x.dest, !DIAssignID !1
3104 // dbg.assign %one, "x", ..., !1
3106 // %merge = select %cond, %two, %one
3107 // store %merge, %x.dest, !DIAssignID !2
3108 // dbg.assign %merge, "x", ..., !2
3109 auto replaceVariable
= [OrigV
, S
](auto *DbgAssign
) {
3110 if (llvm::is_contained(DbgAssign
->location_ops(), OrigV
))
3111 DbgAssign
->replaceVariableLocationOp(OrigV
, S
);
3113 for_each(at::getAssignmentMarkers(SpeculatedStore
), replaceVariable
);
3114 for_each(at::getDPVAssignmentMarkers(SpeculatedStore
), replaceVariable
);
3117 // Metadata can be dependent on the condition we are hoisting above.
3118 // Strip all UB-implying metadata on the instruction. Drop the debug loc
3119 // to avoid making it appear as if the condition is a constant, which would
3120 // be misleading while debugging.
3121 // Similarly strip attributes that maybe dependent on condition we are
3123 for (auto &I
: make_early_inc_range(*ThenBB
)) {
3124 if (!SpeculatedStoreValue
|| &I
!= SpeculatedStore
) {
3125 // Don't update the DILocation of dbg.assign intrinsics.
3126 if (!isa
<DbgAssignIntrinsic
>(&I
))
3127 I
.setDebugLoc(DebugLoc());
3129 I
.dropUBImplyingAttrsAndMetadata();
3131 // Drop ephemeral values.
3132 if (EphTracker
.contains(&I
)) {
3133 I
.replaceAllUsesWith(PoisonValue::get(I
.getType()));
3134 I
.eraseFromParent();
3138 // Hoist the instructions.
3139 // In "RemoveDIs" non-instr debug-info mode, drop DPValues attached to these
3140 // instructions, in the same way that dbg.value intrinsics are dropped at the
3141 // end of this block.
3142 for (auto &It
: make_range(ThenBB
->begin(), ThenBB
->end()))
3143 for (DPValue
&DPV
: make_early_inc_range(It
.getDbgValueRange()))
3144 if (!DPV
.isDbgAssign())
3145 It
.dropOneDbgValue(&DPV
);
3146 BB
->splice(BI
->getIterator(), ThenBB
, ThenBB
->begin(),
3147 std::prev(ThenBB
->end()));
3149 // Insert selects and rewrite the PHI operands.
3150 IRBuilder
<NoFolder
> Builder(BI
);
3151 for (PHINode
&PN
: EndBB
->phis()) {
3152 unsigned OrigI
= PN
.getBasicBlockIndex(BB
);
3153 unsigned ThenI
= PN
.getBasicBlockIndex(ThenBB
);
3154 Value
*OrigV
= PN
.getIncomingValue(OrigI
);
3155 Value
*ThenV
= PN
.getIncomingValue(ThenI
);
3157 // Skip PHIs which are trivial.
3161 // Create a select whose true value is the speculatively executed value and
3162 // false value is the pre-existing value. Swap them if the branch
3163 // destinations were inverted.
3164 Value
*TrueV
= ThenV
, *FalseV
= OrigV
;
3166 std::swap(TrueV
, FalseV
);
3167 Value
*V
= Builder
.CreateSelect(BrCond
, TrueV
, FalseV
, "spec.select", BI
);
3168 PN
.setIncomingValue(OrigI
, V
);
3169 PN
.setIncomingValue(ThenI
, V
);
3172 // Remove speculated dbg intrinsics.
3173 // FIXME: Is it possible to do this in a more elegant way? Moving/merging the
3174 // dbg value for the different flows and inserting it after the select.
3175 for (Instruction
*I
: SpeculatedDbgIntrinsics
) {
3176 // We still want to know that an assignment took place so don't remove
3177 // dbg.assign intrinsics.
3178 if (!isa
<DbgAssignIntrinsic
>(I
))
3179 I
->eraseFromParent();
3186 /// Return true if we can thread a branch across this block.
3187 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock
*BB
) {
3189 EphemeralValueTracker EphTracker
;
3191 // Walk the loop in reverse so that we can identify ephemeral values properly
3192 // (values only feeding assumes).
3193 for (Instruction
&I
: reverse(BB
->instructionsWithoutDebug(false))) {
3194 // Can't fold blocks that contain noduplicate or convergent calls.
3195 if (CallInst
*CI
= dyn_cast
<CallInst
>(&I
))
3196 if (CI
->cannotDuplicate() || CI
->isConvergent())
3199 // Ignore ephemeral values which are deleted during codegen.
3200 // We will delete Phis while threading, so Phis should not be accounted in
3202 if (!EphTracker
.track(&I
) && !isa
<PHINode
>(I
)) {
3203 if (Size
++ > MaxSmallBlockSize
)
3204 return false; // Don't clone large BB's.
3207 // We can only support instructions that do not define values that are
3208 // live outside of the current basic block.
3209 for (User
*U
: I
.users()) {
3210 Instruction
*UI
= cast
<Instruction
>(U
);
3211 if (UI
->getParent() != BB
|| isa
<PHINode
>(UI
))
3215 // Looks ok, continue checking.
3221 static ConstantInt
*getKnownValueOnEdge(Value
*V
, BasicBlock
*From
,
3223 // Don't look past the block defining the value, we might get the value from
3224 // a previous loop iteration.
3225 auto *I
= dyn_cast
<Instruction
>(V
);
3226 if (I
&& I
->getParent() == To
)
3229 // We know the value if the From block branches on it.
3230 auto *BI
= dyn_cast
<BranchInst
>(From
->getTerminator());
3231 if (BI
&& BI
->isConditional() && BI
->getCondition() == V
&&
3232 BI
->getSuccessor(0) != BI
->getSuccessor(1))
3233 return BI
->getSuccessor(0) == To
? ConstantInt::getTrue(BI
->getContext())
3234 : ConstantInt::getFalse(BI
->getContext());
3239 /// If we have a conditional branch on something for which we know the constant
3240 /// value in predecessors (e.g. a phi node in the current block), thread edges
3241 /// from the predecessor to their ultimate destination.
3242 static std::optional
<bool>
3243 FoldCondBranchOnValueKnownInPredecessorImpl(BranchInst
*BI
, DomTreeUpdater
*DTU
,
3244 const DataLayout
&DL
,
3245 AssumptionCache
*AC
) {
3246 SmallMapVector
<ConstantInt
*, SmallSetVector
<BasicBlock
*, 2>, 2> KnownValues
;
3247 BasicBlock
*BB
= BI
->getParent();
3248 Value
*Cond
= BI
->getCondition();
3249 PHINode
*PN
= dyn_cast
<PHINode
>(Cond
);
3250 if (PN
&& PN
->getParent() == BB
) {
3251 // Degenerate case of a single entry PHI.
3252 if (PN
->getNumIncomingValues() == 1) {
3253 FoldSingleEntryPHINodes(PN
->getParent());
3257 for (Use
&U
: PN
->incoming_values())
3258 if (auto *CB
= dyn_cast
<ConstantInt
>(U
))
3259 KnownValues
[CB
].insert(PN
->getIncomingBlock(U
));
3261 for (BasicBlock
*Pred
: predecessors(BB
)) {
3262 if (ConstantInt
*CB
= getKnownValueOnEdge(Cond
, Pred
, BB
))
3263 KnownValues
[CB
].insert(Pred
);
3267 if (KnownValues
.empty())
3270 // Now we know that this block has multiple preds and two succs.
3271 // Check that the block is small enough and values defined in the block are
3272 // not used outside of it.
3273 if (!BlockIsSimpleEnoughToThreadThrough(BB
))
3276 for (const auto &Pair
: KnownValues
) {
3277 // Okay, we now know that all edges from PredBB should be revectored to
3278 // branch to RealDest.
3279 ConstantInt
*CB
= Pair
.first
;
3280 ArrayRef
<BasicBlock
*> PredBBs
= Pair
.second
.getArrayRef();
3281 BasicBlock
*RealDest
= BI
->getSuccessor(!CB
->getZExtValue());
3284 continue; // Skip self loops.
3286 // Skip if the predecessor's terminator is an indirect branch.
3287 if (any_of(PredBBs
, [](BasicBlock
*PredBB
) {
3288 return isa
<IndirectBrInst
>(PredBB
->getTerminator());
3293 dbgs() << "Condition " << *Cond
<< " in " << BB
->getName()
3294 << " has value " << *Pair
.first
<< " in predecessors:\n";
3295 for (const BasicBlock
*PredBB
: Pair
.second
)
3296 dbgs() << " " << PredBB
->getName() << "\n";
3297 dbgs() << "Threading to destination " << RealDest
->getName() << ".\n";
3300 // Split the predecessors we are threading into a new edge block. We'll
3301 // clone the instructions into this block, and then redirect it to RealDest.
3302 BasicBlock
*EdgeBB
= SplitBlockPredecessors(BB
, PredBBs
, ".critedge", DTU
);
3304 // TODO: These just exist to reduce test diff, we can drop them if we like.
3305 EdgeBB
->setName(RealDest
->getName() + ".critedge");
3306 EdgeBB
->moveBefore(RealDest
);
3308 // Update PHI nodes.
3309 AddPredecessorToBlock(RealDest
, EdgeBB
, BB
);
3311 // BB may have instructions that are being threaded over. Clone these
3312 // instructions into EdgeBB. We know that there will be no uses of the
3313 // cloned instructions outside of EdgeBB.
3314 BasicBlock::iterator InsertPt
= EdgeBB
->getFirstInsertionPt();
3315 DenseMap
<Value
*, Value
*> TranslateMap
; // Track translated values.
3316 TranslateMap
[Cond
] = CB
;
3318 // RemoveDIs: track instructions that we optimise away while folding, so
3319 // that we can copy DPValues from them later.
3320 BasicBlock::iterator SrcDbgCursor
= BB
->begin();
3321 for (BasicBlock::iterator BBI
= BB
->begin(); &*BBI
!= BI
; ++BBI
) {
3322 if (PHINode
*PN
= dyn_cast
<PHINode
>(BBI
)) {
3323 TranslateMap
[PN
] = PN
->getIncomingValueForBlock(EdgeBB
);
3326 // Clone the instruction.
3327 Instruction
*N
= BBI
->clone();
3328 // Insert the new instruction into its new home.
3329 N
->insertInto(EdgeBB
, InsertPt
);
3332 N
->setName(BBI
->getName() + ".c");
3334 // Update operands due to translation.
3335 for (Use
&Op
: N
->operands()) {
3336 DenseMap
<Value
*, Value
*>::iterator PI
= TranslateMap
.find(Op
);
3337 if (PI
!= TranslateMap
.end())
3341 // Check for trivial simplification.
3342 if (Value
*V
= simplifyInstruction(N
, {DL
, nullptr, nullptr, AC
})) {
3343 if (!BBI
->use_empty())
3344 TranslateMap
[&*BBI
] = V
;
3345 if (!N
->mayHaveSideEffects()) {
3346 N
->eraseFromParent(); // Instruction folded away, don't need actual
3351 if (!BBI
->use_empty())
3352 TranslateMap
[&*BBI
] = N
;
3355 // Copy all debug-info attached to instructions from the last we
3356 // successfully clone, up to this instruction (they might have been
3358 for (; SrcDbgCursor
!= BBI
; ++SrcDbgCursor
)
3359 N
->cloneDebugInfoFrom(&*SrcDbgCursor
);
3360 SrcDbgCursor
= std::next(BBI
);
3361 // Clone debug-info on this instruction too.
3362 N
->cloneDebugInfoFrom(&*BBI
);
3364 // Register the new instruction with the assumption cache if necessary.
3365 if (auto *Assume
= dyn_cast
<AssumeInst
>(N
))
3367 AC
->registerAssumption(Assume
);
3371 for (; &*SrcDbgCursor
!= BI
; ++SrcDbgCursor
)
3372 InsertPt
->cloneDebugInfoFrom(&*SrcDbgCursor
);
3373 InsertPt
->cloneDebugInfoFrom(BI
);
3375 BB
->removePredecessor(EdgeBB
);
3376 BranchInst
*EdgeBI
= cast
<BranchInst
>(EdgeBB
->getTerminator());
3377 EdgeBI
->setSuccessor(0, RealDest
);
3378 EdgeBI
->setDebugLoc(BI
->getDebugLoc());
3381 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
3382 Updates
.push_back({DominatorTree::Delete
, EdgeBB
, BB
});
3383 Updates
.push_back({DominatorTree::Insert
, EdgeBB
, RealDest
});
3384 DTU
->applyUpdates(Updates
);
3387 // For simplicity, we created a separate basic block for the edge. Merge
3388 // it back into the predecessor if possible. This not only avoids
3389 // unnecessary SimplifyCFG iterations, but also makes sure that we don't
3390 // bypass the check for trivial cycles above.
3391 MergeBlockIntoPredecessor(EdgeBB
, DTU
);
3393 // Signal repeat, simplifying any other constants.
3394 return std::nullopt
;
3400 static bool FoldCondBranchOnValueKnownInPredecessor(BranchInst
*BI
,
3401 DomTreeUpdater
*DTU
,
3402 const DataLayout
&DL
,
3403 AssumptionCache
*AC
) {
3404 std::optional
<bool> Result
;
3405 bool EverChanged
= false;
3407 // Note that None means "we changed things, but recurse further."
3408 Result
= FoldCondBranchOnValueKnownInPredecessorImpl(BI
, DTU
, DL
, AC
);
3409 EverChanged
|= Result
== std::nullopt
|| *Result
;
3410 } while (Result
== std::nullopt
);
3414 /// Given a BB that starts with the specified two-entry PHI node,
3415 /// see if we can eliminate it.
3416 static bool FoldTwoEntryPHINode(PHINode
*PN
, const TargetTransformInfo
&TTI
,
3417 DomTreeUpdater
*DTU
, const DataLayout
&DL
) {
3418 // Ok, this is a two entry PHI node. Check to see if this is a simple "if
3419 // statement", which has a very simple dominance structure. Basically, we
3420 // are trying to find the condition that is being branched on, which
3421 // subsequently causes this merge to happen. We really want control
3422 // dependence information for this check, but simplifycfg can't keep it up
3423 // to date, and this catches most of the cases we care about anyway.
3424 BasicBlock
*BB
= PN
->getParent();
3426 BasicBlock
*IfTrue
, *IfFalse
;
3427 BranchInst
*DomBI
= GetIfCondition(BB
, IfTrue
, IfFalse
);
3430 Value
*IfCond
= DomBI
->getCondition();
3431 // Don't bother if the branch will be constant folded trivially.
3432 if (isa
<ConstantInt
>(IfCond
))
3435 BasicBlock
*DomBlock
= DomBI
->getParent();
3436 SmallVector
<BasicBlock
*, 2> IfBlocks
;
3438 PN
->blocks(), std::back_inserter(IfBlocks
), [](BasicBlock
*IfBlock
) {
3439 return cast
<BranchInst
>(IfBlock
->getTerminator())->isUnconditional();
3441 assert((IfBlocks
.size() == 1 || IfBlocks
.size() == 2) &&
3442 "Will have either one or two blocks to speculate.");
3444 // If the branch is non-unpredictable, see if we either predictably jump to
3445 // the merge bb (if we have only a single 'then' block), or if we predictably
3446 // jump to one specific 'then' block (if we have two of them).
3447 // It isn't beneficial to speculatively execute the code
3448 // from the block that we know is predictably not entered.
3449 if (!DomBI
->getMetadata(LLVMContext::MD_unpredictable
)) {
3450 uint64_t TWeight
, FWeight
;
3451 if (extractBranchWeights(*DomBI
, TWeight
, FWeight
) &&
3452 (TWeight
+ FWeight
) != 0) {
3453 BranchProbability BITrueProb
=
3454 BranchProbability::getBranchProbability(TWeight
, TWeight
+ FWeight
);
3455 BranchProbability Likely
= TTI
.getPredictableBranchThreshold();
3456 BranchProbability BIFalseProb
= BITrueProb
.getCompl();
3457 if (IfBlocks
.size() == 1) {
3458 BranchProbability BIBBProb
=
3459 DomBI
->getSuccessor(0) == BB
? BITrueProb
: BIFalseProb
;
3460 if (BIBBProb
>= Likely
)
3463 if (BITrueProb
>= Likely
|| BIFalseProb
>= Likely
)
3469 // Don't try to fold an unreachable block. For example, the phi node itself
3470 // can't be the candidate if-condition for a select that we want to form.
3471 if (auto *IfCondPhiInst
= dyn_cast
<PHINode
>(IfCond
))
3472 if (IfCondPhiInst
->getParent() == BB
)
3475 // Okay, we found that we can merge this two-entry phi node into a select.
3476 // Doing so would require us to fold *all* two entry phi nodes in this block.
3477 // At some point this becomes non-profitable (particularly if the target
3478 // doesn't support cmov's). Only do this transformation if there are two or
3479 // fewer PHI nodes in this block.
3480 unsigned NumPhis
= 0;
3481 for (BasicBlock::iterator I
= BB
->begin(); isa
<PHINode
>(I
); ++NumPhis
, ++I
)
3485 // Loop over the PHI's seeing if we can promote them all to select
3486 // instructions. While we are at it, keep track of the instructions
3487 // that need to be moved to the dominating block.
3488 SmallPtrSet
<Instruction
*, 4> AggressiveInsts
;
3489 InstructionCost Cost
= 0;
3490 InstructionCost Budget
=
3491 TwoEntryPHINodeFoldingThreshold
* TargetTransformInfo::TCC_Basic
;
3493 bool Changed
= false;
3494 for (BasicBlock::iterator II
= BB
->begin(); isa
<PHINode
>(II
);) {
3495 PHINode
*PN
= cast
<PHINode
>(II
++);
3496 if (Value
*V
= simplifyInstruction(PN
, {DL
, PN
})) {
3497 PN
->replaceAllUsesWith(V
);
3498 PN
->eraseFromParent();
3503 if (!dominatesMergePoint(PN
->getIncomingValue(0), BB
, AggressiveInsts
,
3504 Cost
, Budget
, TTI
) ||
3505 !dominatesMergePoint(PN
->getIncomingValue(1), BB
, AggressiveInsts
,
3510 // If we folded the first phi, PN dangles at this point. Refresh it. If
3511 // we ran out of PHIs then we simplified them all.
3512 PN
= dyn_cast
<PHINode
>(BB
->begin());
3516 // Return true if at least one of these is a 'not', and another is either
3517 // a 'not' too, or a constant.
3518 auto CanHoistNotFromBothValues
= [](Value
*V0
, Value
*V1
) {
3519 if (!match(V0
, m_Not(m_Value())))
3521 auto Invertible
= m_CombineOr(m_Not(m_Value()), m_AnyIntegralConstant());
3522 return match(V0
, m_Not(m_Value())) && match(V1
, Invertible
);
3525 // Don't fold i1 branches on PHIs which contain binary operators or
3526 // (possibly inverted) select form of or/ands, unless one of
3527 // the incoming values is an 'not' and another one is freely invertible.
3528 // These can often be turned into switches and other things.
3529 auto IsBinOpOrAnd
= [](Value
*V
) {
3533 m_CombineOr(m_Select(m_Value(), m_ImmConstant(), m_Value()),
3534 m_Select(m_Value(), m_Value(), m_ImmConstant()))));
3536 if (PN
->getType()->isIntegerTy(1) &&
3537 (IsBinOpOrAnd(PN
->getIncomingValue(0)) ||
3538 IsBinOpOrAnd(PN
->getIncomingValue(1)) || IsBinOpOrAnd(IfCond
)) &&
3539 !CanHoistNotFromBothValues(PN
->getIncomingValue(0),
3540 PN
->getIncomingValue(1)))
3543 // If all PHI nodes are promotable, check to make sure that all instructions
3544 // in the predecessor blocks can be promoted as well. If not, we won't be able
3545 // to get rid of the control flow, so it's not worth promoting to select
3547 for (BasicBlock
*IfBlock
: IfBlocks
)
3548 for (BasicBlock::iterator I
= IfBlock
->begin(); !I
->isTerminator(); ++I
)
3549 if (!AggressiveInsts
.count(&*I
) && !I
->isDebugOrPseudoInst()) {
3550 // This is not an aggressive instruction that we can promote.
3551 // Because of this, we won't be able to get rid of the control flow, so
3552 // the xform is not worth it.
3556 // If either of the blocks has it's address taken, we can't do this fold.
3557 if (any_of(IfBlocks
,
3558 [](BasicBlock
*IfBlock
) { return IfBlock
->hasAddressTaken(); }))
3561 LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond
3562 << " T: " << IfTrue
->getName()
3563 << " F: " << IfFalse
->getName() << "\n");
3565 // If we can still promote the PHI nodes after this gauntlet of tests,
3566 // do all of the PHI's now.
3568 // Move all 'aggressive' instructions, which are defined in the
3569 // conditional parts of the if's up to the dominating block.
3570 for (BasicBlock
*IfBlock
: IfBlocks
)
3571 hoistAllInstructionsInto(DomBlock
, DomBI
, IfBlock
);
3573 IRBuilder
<NoFolder
> Builder(DomBI
);
3574 // Propagate fast-math-flags from phi nodes to replacement selects.
3575 IRBuilder
<>::FastMathFlagGuard
FMFGuard(Builder
);
3576 while (PHINode
*PN
= dyn_cast
<PHINode
>(BB
->begin())) {
3577 if (isa
<FPMathOperator
>(PN
))
3578 Builder
.setFastMathFlags(PN
->getFastMathFlags());
3580 // Change the PHI node into a select instruction.
3581 Value
*TrueVal
= PN
->getIncomingValueForBlock(IfTrue
);
3582 Value
*FalseVal
= PN
->getIncomingValueForBlock(IfFalse
);
3584 Value
*Sel
= Builder
.CreateSelect(IfCond
, TrueVal
, FalseVal
, "", DomBI
);
3585 PN
->replaceAllUsesWith(Sel
);
3587 PN
->eraseFromParent();
3590 // At this point, all IfBlocks are empty, so our if statement
3591 // has been flattened. Change DomBlock to jump directly to our new block to
3592 // avoid other simplifycfg's kicking in on the diamond.
3593 Builder
.CreateBr(BB
);
3595 SmallVector
<DominatorTree::UpdateType
, 3> Updates
;
3597 Updates
.push_back({DominatorTree::Insert
, DomBlock
, BB
});
3598 for (auto *Successor
: successors(DomBlock
))
3599 Updates
.push_back({DominatorTree::Delete
, DomBlock
, Successor
});
3602 DomBI
->eraseFromParent();
3604 DTU
->applyUpdates(Updates
);
3609 static Value
*createLogicalOp(IRBuilderBase
&Builder
,
3610 Instruction::BinaryOps Opc
, Value
*LHS
,
3611 Value
*RHS
, const Twine
&Name
= "") {
3612 // Try to relax logical op to binary op.
3613 if (impliesPoison(RHS
, LHS
))
3614 return Builder
.CreateBinOp(Opc
, LHS
, RHS
, Name
);
3615 if (Opc
== Instruction::And
)
3616 return Builder
.CreateLogicalAnd(LHS
, RHS
, Name
);
3617 if (Opc
== Instruction::Or
)
3618 return Builder
.CreateLogicalOr(LHS
, RHS
, Name
);
3619 llvm_unreachable("Invalid logical opcode");
3622 /// Return true if either PBI or BI has branch weight available, and store
3623 /// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
3624 /// not have branch weight, use 1:1 as its weight.
3625 static bool extractPredSuccWeights(BranchInst
*PBI
, BranchInst
*BI
,
3626 uint64_t &PredTrueWeight
,
3627 uint64_t &PredFalseWeight
,
3628 uint64_t &SuccTrueWeight
,
3629 uint64_t &SuccFalseWeight
) {
3630 bool PredHasWeights
=
3631 extractBranchWeights(*PBI
, PredTrueWeight
, PredFalseWeight
);
3632 bool SuccHasWeights
=
3633 extractBranchWeights(*BI
, SuccTrueWeight
, SuccFalseWeight
);
3634 if (PredHasWeights
|| SuccHasWeights
) {
3635 if (!PredHasWeights
)
3636 PredTrueWeight
= PredFalseWeight
= 1;
3637 if (!SuccHasWeights
)
3638 SuccTrueWeight
= SuccFalseWeight
= 1;
3645 /// Determine if the two branches share a common destination and deduce a glue
3646 /// that joins the branches' conditions to arrive at the common destination if
3647 /// that would be profitable.
3648 static std::optional
<std::tuple
<BasicBlock
*, Instruction::BinaryOps
, bool>>
3649 shouldFoldCondBranchesToCommonDestination(BranchInst
*BI
, BranchInst
*PBI
,
3650 const TargetTransformInfo
*TTI
) {
3651 assert(BI
&& PBI
&& BI
->isConditional() && PBI
->isConditional() &&
3652 "Both blocks must end with a conditional branches.");
3653 assert(is_contained(predecessors(BI
->getParent()), PBI
->getParent()) &&
3654 "PredBB must be a predecessor of BB.");
3656 // We have the potential to fold the conditions together, but if the
3657 // predecessor branch is predictable, we may not want to merge them.
3658 uint64_t PTWeight
, PFWeight
;
3659 BranchProbability PBITrueProb
, Likely
;
3660 if (TTI
&& !PBI
->getMetadata(LLVMContext::MD_unpredictable
) &&
3661 extractBranchWeights(*PBI
, PTWeight
, PFWeight
) &&
3662 (PTWeight
+ PFWeight
) != 0) {
3664 BranchProbability::getBranchProbability(PTWeight
, PTWeight
+ PFWeight
);
3665 Likely
= TTI
->getPredictableBranchThreshold();
3668 if (PBI
->getSuccessor(0) == BI
->getSuccessor(0)) {
3669 // Speculate the 2nd condition unless the 1st is probably true.
3670 if (PBITrueProb
.isUnknown() || PBITrueProb
< Likely
)
3671 return {{BI
->getSuccessor(0), Instruction::Or
, false}};
3672 } else if (PBI
->getSuccessor(1) == BI
->getSuccessor(1)) {
3673 // Speculate the 2nd condition unless the 1st is probably false.
3674 if (PBITrueProb
.isUnknown() || PBITrueProb
.getCompl() < Likely
)
3675 return {{BI
->getSuccessor(1), Instruction::And
, false}};
3676 } else if (PBI
->getSuccessor(0) == BI
->getSuccessor(1)) {
3677 // Speculate the 2nd condition unless the 1st is probably true.
3678 if (PBITrueProb
.isUnknown() || PBITrueProb
< Likely
)
3679 return {{BI
->getSuccessor(1), Instruction::And
, true}};
3680 } else if (PBI
->getSuccessor(1) == BI
->getSuccessor(0)) {
3681 // Speculate the 2nd condition unless the 1st is probably false.
3682 if (PBITrueProb
.isUnknown() || PBITrueProb
.getCompl() < Likely
)
3683 return {{BI
->getSuccessor(0), Instruction::Or
, true}};
3685 return std::nullopt
;
3688 static bool performBranchToCommonDestFolding(BranchInst
*BI
, BranchInst
*PBI
,
3689 DomTreeUpdater
*DTU
,
3690 MemorySSAUpdater
*MSSAU
,
3691 const TargetTransformInfo
*TTI
) {
3692 BasicBlock
*BB
= BI
->getParent();
3693 BasicBlock
*PredBlock
= PBI
->getParent();
3695 // Determine if the two branches share a common destination.
3696 BasicBlock
*CommonSucc
;
3697 Instruction::BinaryOps Opc
;
3698 bool InvertPredCond
;
3699 std::tie(CommonSucc
, Opc
, InvertPredCond
) =
3700 *shouldFoldCondBranchesToCommonDestination(BI
, PBI
, TTI
);
3702 LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI
<< *BB
);
3704 IRBuilder
<> Builder(PBI
);
3705 // The builder is used to create instructions to eliminate the branch in BB.
3706 // If BB's terminator has !annotation metadata, add it to the new
3708 Builder
.CollectMetadataToCopy(BB
->getTerminator(),
3709 {LLVMContext::MD_annotation
});
3711 // If we need to invert the condition in the pred block to match, do so now.
3712 if (InvertPredCond
) {
3713 InvertBranch(PBI
, Builder
);
3716 BasicBlock
*UniqueSucc
=
3717 PBI
->getSuccessor(0) == BB
? BI
->getSuccessor(0) : BI
->getSuccessor(1);
3719 // Before cloning instructions, notify the successor basic block that it
3720 // is about to have a new predecessor. This will update PHI nodes,
3721 // which will allow us to update live-out uses of bonus instructions.
3722 AddPredecessorToBlock(UniqueSucc
, PredBlock
, BB
, MSSAU
);
3724 // Try to update branch weights.
3725 uint64_t PredTrueWeight
, PredFalseWeight
, SuccTrueWeight
, SuccFalseWeight
;
3726 if (extractPredSuccWeights(PBI
, BI
, PredTrueWeight
, PredFalseWeight
,
3727 SuccTrueWeight
, SuccFalseWeight
)) {
3728 SmallVector
<uint64_t, 8> NewWeights
;
3730 if (PBI
->getSuccessor(0) == BB
) {
3731 // PBI: br i1 %x, BB, FalseDest
3732 // BI: br i1 %y, UniqueSucc, FalseDest
3733 // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
3734 NewWeights
.push_back(PredTrueWeight
* SuccTrueWeight
);
3735 // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
3736 // TrueWeight for PBI * FalseWeight for BI.
3737 // We assume that total weights of a BranchInst can fit into 32 bits.
3738 // Therefore, we will not have overflow using 64-bit arithmetic.
3739 NewWeights
.push_back(PredFalseWeight
*
3740 (SuccFalseWeight
+ SuccTrueWeight
) +
3741 PredTrueWeight
* SuccFalseWeight
);
3743 // PBI: br i1 %x, TrueDest, BB
3744 // BI: br i1 %y, TrueDest, UniqueSucc
3745 // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
3746 // FalseWeight for PBI * TrueWeight for BI.
3747 NewWeights
.push_back(PredTrueWeight
* (SuccFalseWeight
+ SuccTrueWeight
) +
3748 PredFalseWeight
* SuccTrueWeight
);
3749 // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
3750 NewWeights
.push_back(PredFalseWeight
* SuccFalseWeight
);
3753 // Halve the weights if any of them cannot fit in an uint32_t
3754 FitWeights(NewWeights
);
3756 SmallVector
<uint32_t, 8> MDWeights(NewWeights
.begin(), NewWeights
.end());
3757 setBranchWeights(PBI
, MDWeights
[0], MDWeights
[1]);
3759 // TODO: If BB is reachable from all paths through PredBlock, then we
3760 // could replace PBI's branch probabilities with BI's.
3762 PBI
->setMetadata(LLVMContext::MD_prof
, nullptr);
3764 // Now, update the CFG.
3765 PBI
->setSuccessor(PBI
->getSuccessor(0) != BB
, UniqueSucc
);
3768 DTU
->applyUpdates({{DominatorTree::Insert
, PredBlock
, UniqueSucc
},
3769 {DominatorTree::Delete
, PredBlock
, BB
}});
3771 // If BI was a loop latch, it may have had associated loop metadata.
3772 // We need to copy it to the new latch, that is, PBI.
3773 if (MDNode
*LoopMD
= BI
->getMetadata(LLVMContext::MD_loop
))
3774 PBI
->setMetadata(LLVMContext::MD_loop
, LoopMD
);
3776 ValueToValueMapTy VMap
; // maps original values to cloned values
3777 CloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(BB
, PredBlock
, VMap
);
3779 Module
*M
= BB
->getModule();
3781 if (PredBlock
->IsNewDbgInfoFormat
) {
3782 PredBlock
->getTerminator()->cloneDebugInfoFrom(BB
->getTerminator());
3783 for (DPValue
&DPV
: PredBlock
->getTerminator()->getDbgValueRange()) {
3784 RemapDPValue(M
, &DPV
, VMap
,
3785 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
3789 // Now that the Cond was cloned into the predecessor basic block,
3790 // or/and the two conditions together.
3791 Value
*BICond
= VMap
[BI
->getCondition()];
3793 createLogicalOp(Builder
, Opc
, PBI
->getCondition(), BICond
, "or.cond"));
3795 ++NumFoldBranchToCommonDest
;
3799 /// Return if an instruction's type or any of its operands' types are a vector
3801 static bool isVectorOp(Instruction
&I
) {
3802 return I
.getType()->isVectorTy() || any_of(I
.operands(), [](Use
&U
) {
3803 return U
->getType()->isVectorTy();
3807 /// If this basic block is simple enough, and if a predecessor branches to us
3808 /// and one of our successors, fold the block into the predecessor and use
3809 /// logical operations to pick the right destination.
3810 bool llvm::FoldBranchToCommonDest(BranchInst
*BI
, DomTreeUpdater
*DTU
,
3811 MemorySSAUpdater
*MSSAU
,
3812 const TargetTransformInfo
*TTI
,
3813 unsigned BonusInstThreshold
) {
3814 // If this block ends with an unconditional branch,
3815 // let SpeculativelyExecuteBB() deal with it.
3816 if (!BI
->isConditional())
3819 BasicBlock
*BB
= BI
->getParent();
3820 TargetTransformInfo::TargetCostKind CostKind
=
3821 BB
->getParent()->hasMinSize() ? TargetTransformInfo::TCK_CodeSize
3822 : TargetTransformInfo::TCK_SizeAndLatency
;
3824 Instruction
*Cond
= dyn_cast
<Instruction
>(BI
->getCondition());
3827 (!isa
<CmpInst
>(Cond
) && !isa
<BinaryOperator
>(Cond
) &&
3828 !isa
<SelectInst
>(Cond
)) ||
3829 Cond
->getParent() != BB
|| !Cond
->hasOneUse())
3832 // Finally, don't infinitely unroll conditional loops.
3833 if (is_contained(successors(BB
), BB
))
3836 // With which predecessors will we want to deal with?
3837 SmallVector
<BasicBlock
*, 8> Preds
;
3838 for (BasicBlock
*PredBlock
: predecessors(BB
)) {
3839 BranchInst
*PBI
= dyn_cast
<BranchInst
>(PredBlock
->getTerminator());
3841 // Check that we have two conditional branches. If there is a PHI node in
3842 // the common successor, verify that the same value flows in from both
3844 if (!PBI
|| PBI
->isUnconditional() || !SafeToMergeTerminators(BI
, PBI
))
3847 // Determine if the two branches share a common destination.
3848 BasicBlock
*CommonSucc
;
3849 Instruction::BinaryOps Opc
;
3850 bool InvertPredCond
;
3851 if (auto Recipe
= shouldFoldCondBranchesToCommonDestination(BI
, PBI
, TTI
))
3852 std::tie(CommonSucc
, Opc
, InvertPredCond
) = *Recipe
;
3856 // Check the cost of inserting the necessary logic before performing the
3859 Type
*Ty
= BI
->getCondition()->getType();
3860 InstructionCost Cost
= TTI
->getArithmeticInstrCost(Opc
, Ty
, CostKind
);
3861 if (InvertPredCond
&& (!PBI
->getCondition()->hasOneUse() ||
3862 !isa
<CmpInst
>(PBI
->getCondition())))
3863 Cost
+= TTI
->getArithmeticInstrCost(Instruction::Xor
, Ty
, CostKind
);
3865 if (Cost
> BranchFoldThreshold
)
3869 // Ok, we do want to deal with this predecessor. Record it.
3870 Preds
.emplace_back(PredBlock
);
3873 // If there aren't any predecessors into which we can fold,
3874 // don't bother checking the cost.
3878 // Only allow this transformation if computing the condition doesn't involve
3879 // too many instructions and these involved instructions can be executed
3880 // unconditionally. We denote all involved instructions except the condition
3881 // as "bonus instructions", and only allow this transformation when the
3882 // number of the bonus instructions we'll need to create when cloning into
3883 // each predecessor does not exceed a certain threshold.
3884 unsigned NumBonusInsts
= 0;
3885 bool SawVectorOp
= false;
3886 const unsigned PredCount
= Preds
.size();
3887 for (Instruction
&I
: *BB
) {
3888 // Don't check the branch condition comparison itself.
3891 // Ignore dbg intrinsics, and the terminator.
3892 if (isa
<DbgInfoIntrinsic
>(I
) || isa
<BranchInst
>(I
))
3894 // I must be safe to execute unconditionally.
3895 if (!isSafeToSpeculativelyExecute(&I
))
3897 SawVectorOp
|= isVectorOp(I
);
3899 // Account for the cost of duplicating this instruction into each
3900 // predecessor. Ignore free instructions.
3901 if (!TTI
|| TTI
->getInstructionCost(&I
, CostKind
) !=
3902 TargetTransformInfo::TCC_Free
) {
3903 NumBonusInsts
+= PredCount
;
3905 // Early exits once we reach the limit.
3907 BonusInstThreshold
* BranchFoldToCommonDestVectorMultiplier
)
3911 auto IsBCSSAUse
= [BB
, &I
](Use
&U
) {
3912 auto *UI
= cast
<Instruction
>(U
.getUser());
3913 if (auto *PN
= dyn_cast
<PHINode
>(UI
))
3914 return PN
->getIncomingBlock(U
) == BB
;
3915 return UI
->getParent() == BB
&& I
.comesBefore(UI
);
3918 // Does this instruction require rewriting of uses?
3919 if (!all_of(I
.uses(), IsBCSSAUse
))
3923 BonusInstThreshold
*
3924 (SawVectorOp
? BranchFoldToCommonDestVectorMultiplier
: 1))
3927 // Ok, we have the budget. Perform the transformation.
3928 for (BasicBlock
*PredBlock
: Preds
) {
3929 auto *PBI
= cast
<BranchInst
>(PredBlock
->getTerminator());
3930 return performBranchToCommonDestFolding(BI
, PBI
, DTU
, MSSAU
, TTI
);
3935 // If there is only one store in BB1 and BB2, return it, otherwise return
3937 static StoreInst
*findUniqueStoreInBlocks(BasicBlock
*BB1
, BasicBlock
*BB2
) {
3938 StoreInst
*S
= nullptr;
3939 for (auto *BB
: {BB1
, BB2
}) {
3943 if (auto *SI
= dyn_cast
<StoreInst
>(&I
)) {
3945 // Multiple stores seen.
3954 static Value
*ensureValueAvailableInSuccessor(Value
*V
, BasicBlock
*BB
,
3955 Value
*AlternativeV
= nullptr) {
3956 // PHI is going to be a PHI node that allows the value V that is defined in
3957 // BB to be referenced in BB's only successor.
3959 // If AlternativeV is nullptr, the only value we care about in PHI is V. It
3960 // doesn't matter to us what the other operand is (it'll never get used). We
3961 // could just create a new PHI with an undef incoming value, but that could
3962 // increase register pressure if EarlyCSE/InstCombine can't fold it with some
3963 // other PHI. So here we directly look for some PHI in BB's successor with V
3964 // as an incoming operand. If we find one, we use it, else we create a new
3967 // If AlternativeV is not nullptr, we care about both incoming values in PHI.
3968 // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
3969 // where OtherBB is the single other predecessor of BB's only successor.
3970 PHINode
*PHI
= nullptr;
3971 BasicBlock
*Succ
= BB
->getSingleSuccessor();
3973 for (auto I
= Succ
->begin(); isa
<PHINode
>(I
); ++I
)
3974 if (cast
<PHINode
>(I
)->getIncomingValueForBlock(BB
) == V
) {
3975 PHI
= cast
<PHINode
>(I
);
3979 assert(Succ
->hasNPredecessors(2));
3980 auto PredI
= pred_begin(Succ
);
3981 BasicBlock
*OtherPredBB
= *PredI
== BB
? *++PredI
: *PredI
;
3982 if (PHI
->getIncomingValueForBlock(OtherPredBB
) == AlternativeV
)
3989 // If V is not an instruction defined in BB, just return it.
3990 if (!AlternativeV
&&
3991 (!isa
<Instruction
>(V
) || cast
<Instruction
>(V
)->getParent() != BB
))
3994 PHI
= PHINode::Create(V
->getType(), 2, "simplifycfg.merge");
3995 PHI
->insertBefore(Succ
->begin());
3996 PHI
->addIncoming(V
, BB
);
3997 for (BasicBlock
*PredBB
: predecessors(Succ
))
4000 AlternativeV
? AlternativeV
: PoisonValue::get(V
->getType()), PredBB
);
4004 static bool mergeConditionalStoreToAddress(
4005 BasicBlock
*PTB
, BasicBlock
*PFB
, BasicBlock
*QTB
, BasicBlock
*QFB
,
4006 BasicBlock
*PostBB
, Value
*Address
, bool InvertPCond
, bool InvertQCond
,
4007 DomTreeUpdater
*DTU
, const DataLayout
&DL
, const TargetTransformInfo
&TTI
) {
4008 // For every pointer, there must be exactly two stores, one coming from
4009 // PTB or PFB, and the other from QTB or QFB. We don't support more than one
4010 // store (to any address) in PTB,PFB or QTB,QFB.
4011 // FIXME: We could relax this restriction with a bit more work and performance
4013 StoreInst
*PStore
= findUniqueStoreInBlocks(PTB
, PFB
);
4014 StoreInst
*QStore
= findUniqueStoreInBlocks(QTB
, QFB
);
4015 if (!PStore
|| !QStore
)
4018 // Now check the stores are compatible.
4019 if (!QStore
->isUnordered() || !PStore
->isUnordered() ||
4020 PStore
->getValueOperand()->getType() !=
4021 QStore
->getValueOperand()->getType())
4024 // Check that sinking the store won't cause program behavior changes. Sinking
4025 // the store out of the Q blocks won't change any behavior as we're sinking
4026 // from a block to its unconditional successor. But we're moving a store from
4027 // the P blocks down through the middle block (QBI) and past both QFB and QTB.
4028 // So we need to check that there are no aliasing loads or stores in
4029 // QBI, QTB and QFB. We also need to check there are no conflicting memory
4030 // operations between PStore and the end of its parent block.
4032 // The ideal way to do this is to query AliasAnalysis, but we don't
4033 // preserve AA currently so that is dangerous. Be super safe and just
4034 // check there are no other memory operations at all.
4035 for (auto &I
: *QFB
->getSinglePredecessor())
4036 if (I
.mayReadOrWriteMemory())
4038 for (auto &I
: *QFB
)
4039 if (&I
!= QStore
&& I
.mayReadOrWriteMemory())
4042 for (auto &I
: *QTB
)
4043 if (&I
!= QStore
&& I
.mayReadOrWriteMemory())
4045 for (auto I
= BasicBlock::iterator(PStore
), E
= PStore
->getParent()->end();
4047 if (&*I
!= PStore
&& I
->mayReadOrWriteMemory())
4050 // If we're not in aggressive mode, we only optimize if we have some
4051 // confidence that by optimizing we'll allow P and/or Q to be if-converted.
4052 auto IsWorthwhile
= [&](BasicBlock
*BB
, ArrayRef
<StoreInst
*> FreeStores
) {
4055 // Heuristic: if the block can be if-converted/phi-folded and the
4056 // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
4057 // thread this store.
4058 InstructionCost Cost
= 0;
4059 InstructionCost Budget
=
4060 PHINodeFoldingThreshold
* TargetTransformInfo::TCC_Basic
;
4061 for (auto &I
: BB
->instructionsWithoutDebug(false)) {
4062 // Consider terminator instruction to be free.
4063 if (I
.isTerminator())
4065 // If this is one the stores that we want to speculate out of this BB,
4066 // then don't count it's cost, consider it to be free.
4067 if (auto *S
= dyn_cast
<StoreInst
>(&I
))
4068 if (llvm::find(FreeStores
, S
))
4070 // Else, we have a white-list of instructions that we are ak speculating.
4071 if (!isa
<BinaryOperator
>(I
) && !isa
<GetElementPtrInst
>(I
))
4072 return false; // Not in white-list - not worthwhile folding.
4073 // And finally, if this is a non-free instruction that we are okay
4074 // speculating, ensure that we consider the speculation budget.
4076 TTI
.getInstructionCost(&I
, TargetTransformInfo::TCK_SizeAndLatency
);
4078 return false; // Eagerly refuse to fold as soon as we're out of budget.
4080 assert(Cost
<= Budget
&&
4081 "When we run out of budget we will eagerly return from within the "
4082 "per-instruction loop.");
4086 const std::array
<StoreInst
*, 2> FreeStores
= {PStore
, QStore
};
4087 if (!MergeCondStoresAggressively
&&
4088 (!IsWorthwhile(PTB
, FreeStores
) || !IsWorthwhile(PFB
, FreeStores
) ||
4089 !IsWorthwhile(QTB
, FreeStores
) || !IsWorthwhile(QFB
, FreeStores
)))
4092 // If PostBB has more than two predecessors, we need to split it so we can
4094 if (std::next(pred_begin(PostBB
), 2) != pred_end(PostBB
)) {
4095 // We know that QFB's only successor is PostBB. And QFB has a single
4096 // predecessor. If QTB exists, then its only successor is also PostBB.
4097 // If QTB does not exist, then QFB's only predecessor has a conditional
4098 // branch to QFB and PostBB.
4099 BasicBlock
*TruePred
= QTB
? QTB
: QFB
->getSinglePredecessor();
4101 SplitBlockPredecessors(PostBB
, {QFB
, TruePred
}, "condstore.split", DTU
);
4107 // OK, we're going to sink the stores to PostBB. The store has to be
4108 // conditional though, so first create the predicate.
4109 Value
*PCond
= cast
<BranchInst
>(PFB
->getSinglePredecessor()->getTerminator())
4111 Value
*QCond
= cast
<BranchInst
>(QFB
->getSinglePredecessor()->getTerminator())
4114 Value
*PPHI
= ensureValueAvailableInSuccessor(PStore
->getValueOperand(),
4115 PStore
->getParent());
4116 Value
*QPHI
= ensureValueAvailableInSuccessor(QStore
->getValueOperand(),
4117 QStore
->getParent(), PPHI
);
4119 BasicBlock::iterator PostBBFirst
= PostBB
->getFirstInsertionPt();
4120 IRBuilder
<> QB(PostBB
, PostBBFirst
);
4121 QB
.SetCurrentDebugLocation(PostBBFirst
->getStableDebugLoc());
4123 Value
*PPred
= PStore
->getParent() == PTB
? PCond
: QB
.CreateNot(PCond
);
4124 Value
*QPred
= QStore
->getParent() == QTB
? QCond
: QB
.CreateNot(QCond
);
4127 PPred
= QB
.CreateNot(PPred
);
4129 QPred
= QB
.CreateNot(QPred
);
4130 Value
*CombinedPred
= QB
.CreateOr(PPred
, QPred
);
4132 BasicBlock::iterator InsertPt
= QB
.GetInsertPoint();
4133 auto *T
= SplitBlockAndInsertIfThen(CombinedPred
, InsertPt
,
4134 /*Unreachable=*/false,
4135 /*BranchWeights=*/nullptr, DTU
);
4137 QB
.SetInsertPoint(T
);
4138 StoreInst
*SI
= cast
<StoreInst
>(QB
.CreateStore(QPHI
, Address
));
4139 SI
->setAAMetadata(PStore
->getAAMetadata().merge(QStore
->getAAMetadata()));
4140 // Choose the minimum alignment. If we could prove both stores execute, we
4141 // could use biggest one. In this case, though, we only know that one of the
4142 // stores executes. And we don't know it's safe to take the alignment from a
4143 // store that doesn't execute.
4144 SI
->setAlignment(std::min(PStore
->getAlign(), QStore
->getAlign()));
4146 QStore
->eraseFromParent();
4147 PStore
->eraseFromParent();
4152 static bool mergeConditionalStores(BranchInst
*PBI
, BranchInst
*QBI
,
4153 DomTreeUpdater
*DTU
, const DataLayout
&DL
,
4154 const TargetTransformInfo
&TTI
) {
4155 // The intention here is to find diamonds or triangles (see below) where each
4156 // conditional block contains a store to the same address. Both of these
4157 // stores are conditional, so they can't be unconditionally sunk. But it may
4158 // be profitable to speculatively sink the stores into one merged store at the
4159 // end, and predicate the merged store on the union of the two conditions of
4162 // This can reduce the number of stores executed if both of the conditions are
4163 // true, and can allow the blocks to become small enough to be if-converted.
4164 // This optimization will also chain, so that ladders of test-and-set
4165 // sequences can be if-converted away.
4167 // We only deal with simple diamonds or triangles:
4169 // PBI or PBI or a combination of the two
4179 // We model triangles as a type of diamond with a nullptr "true" block.
4180 // Triangles are canonicalized so that the fallthrough edge is represented by
4181 // a true condition, as in the diagram above.
4182 BasicBlock
*PTB
= PBI
->getSuccessor(0);
4183 BasicBlock
*PFB
= PBI
->getSuccessor(1);
4184 BasicBlock
*QTB
= QBI
->getSuccessor(0);
4185 BasicBlock
*QFB
= QBI
->getSuccessor(1);
4186 BasicBlock
*PostBB
= QFB
->getSingleSuccessor();
4188 // Make sure we have a good guess for PostBB. If QTB's only successor is
4189 // QFB, then QFB is a better PostBB.
4190 if (QTB
->getSingleSuccessor() == QFB
)
4193 // If we couldn't find a good PostBB, stop.
4197 bool InvertPCond
= false, InvertQCond
= false;
4198 // Canonicalize fallthroughs to the true branches.
4199 if (PFB
== QBI
->getParent()) {
4200 std::swap(PFB
, PTB
);
4203 if (QFB
== PostBB
) {
4204 std::swap(QFB
, QTB
);
4208 // From this point on we can assume PTB or QTB may be fallthroughs but PFB
4209 // and QFB may not. Model fallthroughs as a nullptr block.
4210 if (PTB
== QBI
->getParent())
4215 // Legality bailouts. We must have at least the non-fallthrough blocks and
4216 // the post-dominating block, and the non-fallthroughs must only have one
4218 auto HasOnePredAndOneSucc
= [](BasicBlock
*BB
, BasicBlock
*P
, BasicBlock
*S
) {
4219 return BB
->getSinglePredecessor() == P
&& BB
->getSingleSuccessor() == S
;
4221 if (!HasOnePredAndOneSucc(PFB
, PBI
->getParent(), QBI
->getParent()) ||
4222 !HasOnePredAndOneSucc(QFB
, QBI
->getParent(), PostBB
))
4224 if ((PTB
&& !HasOnePredAndOneSucc(PTB
, PBI
->getParent(), QBI
->getParent())) ||
4225 (QTB
&& !HasOnePredAndOneSucc(QTB
, QBI
->getParent(), PostBB
)))
4227 if (!QBI
->getParent()->hasNUses(2))
4230 // OK, this is a sequence of two diamonds or triangles.
4231 // Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
4232 SmallPtrSet
<Value
*, 4> PStoreAddresses
, QStoreAddresses
;
4233 for (auto *BB
: {PTB
, PFB
}) {
4237 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(&I
))
4238 PStoreAddresses
.insert(SI
->getPointerOperand());
4240 for (auto *BB
: {QTB
, QFB
}) {
4244 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(&I
))
4245 QStoreAddresses
.insert(SI
->getPointerOperand());
4248 set_intersect(PStoreAddresses
, QStoreAddresses
);
4249 // set_intersect mutates PStoreAddresses in place. Rename it here to make it
4250 // clear what it contains.
4251 auto &CommonAddresses
= PStoreAddresses
;
4253 bool Changed
= false;
4254 for (auto *Address
: CommonAddresses
)
4256 mergeConditionalStoreToAddress(PTB
, PFB
, QTB
, QFB
, PostBB
, Address
,
4257 InvertPCond
, InvertQCond
, DTU
, DL
, TTI
);
4261 /// If the previous block ended with a widenable branch, determine if reusing
4262 /// the target block is profitable and legal. This will have the effect of
4263 /// "widening" PBI, but doesn't require us to reason about hosting safety.
4264 static bool tryWidenCondBranchToCondBranch(BranchInst
*PBI
, BranchInst
*BI
,
4265 DomTreeUpdater
*DTU
) {
4266 // TODO: This can be generalized in two important ways:
4267 // 1) We can allow phi nodes in IfFalseBB and simply reuse all the input
4268 // values from the PBI edge.
4269 // 2) We can sink side effecting instructions into BI's fallthrough
4270 // successor provided they doesn't contribute to computation of
4272 BasicBlock
*IfTrueBB
= PBI
->getSuccessor(0);
4273 BasicBlock
*IfFalseBB
= PBI
->getSuccessor(1);
4274 if (!isWidenableBranch(PBI
) || IfTrueBB
!= BI
->getParent() ||
4275 !BI
->getParent()->getSinglePredecessor())
4277 if (!IfFalseBB
->phis().empty())
4278 return false; // TODO
4279 // This helps avoid infinite loop with SimplifyCondBranchToCondBranch which
4280 // may undo the transform done here.
4281 // TODO: There might be a more fine-grained solution to this.
4282 if (!llvm::succ_empty(IfFalseBB
))
4284 // Use lambda to lazily compute expensive condition after cheap ones.
4285 auto NoSideEffects
= [](BasicBlock
&BB
) {
4286 return llvm::none_of(BB
, [](const Instruction
&I
) {
4287 return I
.mayWriteToMemory() || I
.mayHaveSideEffects();
4290 if (BI
->getSuccessor(1) != IfFalseBB
&& // no inf looping
4291 BI
->getSuccessor(1)->getTerminatingDeoptimizeCall() && // profitability
4292 NoSideEffects(*BI
->getParent())) {
4293 auto *OldSuccessor
= BI
->getSuccessor(1);
4294 OldSuccessor
->removePredecessor(BI
->getParent());
4295 BI
->setSuccessor(1, IfFalseBB
);
4298 {{DominatorTree::Insert
, BI
->getParent(), IfFalseBB
},
4299 {DominatorTree::Delete
, BI
->getParent(), OldSuccessor
}});
4302 if (BI
->getSuccessor(0) != IfFalseBB
&& // no inf looping
4303 BI
->getSuccessor(0)->getTerminatingDeoptimizeCall() && // profitability
4304 NoSideEffects(*BI
->getParent())) {
4305 auto *OldSuccessor
= BI
->getSuccessor(0);
4306 OldSuccessor
->removePredecessor(BI
->getParent());
4307 BI
->setSuccessor(0, IfFalseBB
);
4310 {{DominatorTree::Insert
, BI
->getParent(), IfFalseBB
},
4311 {DominatorTree::Delete
, BI
->getParent(), OldSuccessor
}});
4317 /// If we have a conditional branch as a predecessor of another block,
4318 /// this function tries to simplify it. We know
4319 /// that PBI and BI are both conditional branches, and BI is in one of the
4320 /// successor blocks of PBI - PBI branches to BI.
4321 static bool SimplifyCondBranchToCondBranch(BranchInst
*PBI
, BranchInst
*BI
,
4322 DomTreeUpdater
*DTU
,
4323 const DataLayout
&DL
,
4324 const TargetTransformInfo
&TTI
) {
4325 assert(PBI
->isConditional() && BI
->isConditional());
4326 BasicBlock
*BB
= BI
->getParent();
4328 // If this block ends with a branch instruction, and if there is a
4329 // predecessor that ends on a branch of the same condition, make
4330 // this conditional branch redundant.
4331 if (PBI
->getCondition() == BI
->getCondition() &&
4332 PBI
->getSuccessor(0) != PBI
->getSuccessor(1)) {
4333 // Okay, the outcome of this conditional branch is statically
4334 // knowable. If this block had a single pred, handle specially, otherwise
4335 // FoldCondBranchOnValueKnownInPredecessor() will handle it.
4336 if (BB
->getSinglePredecessor()) {
4337 // Turn this into a branch on constant.
4338 bool CondIsTrue
= PBI
->getSuccessor(0) == BB
;
4340 ConstantInt::get(Type::getInt1Ty(BB
->getContext()), CondIsTrue
));
4341 return true; // Nuke the branch on constant.
4345 // If the previous block ended with a widenable branch, determine if reusing
4346 // the target block is profitable and legal. This will have the effect of
4347 // "widening" PBI, but doesn't require us to reason about hosting safety.
4348 if (tryWidenCondBranchToCondBranch(PBI
, BI
, DTU
))
4351 // If both branches are conditional and both contain stores to the same
4352 // address, remove the stores from the conditionals and create a conditional
4353 // merged store at the end.
4354 if (MergeCondStores
&& mergeConditionalStores(PBI
, BI
, DTU
, DL
, TTI
))
4357 // If this is a conditional branch in an empty block, and if any
4358 // predecessors are a conditional branch to one of our destinations,
4359 // fold the conditions into logical ops and one cond br.
4361 // Ignore dbg intrinsics.
4362 if (&*BB
->instructionsWithoutDebug(false).begin() != BI
)
4366 if (PBI
->getSuccessor(0) == BI
->getSuccessor(0)) {
4369 } else if (PBI
->getSuccessor(0) == BI
->getSuccessor(1)) {
4372 } else if (PBI
->getSuccessor(1) == BI
->getSuccessor(0)) {
4375 } else if (PBI
->getSuccessor(1) == BI
->getSuccessor(1)) {
4382 // Check to make sure that the other destination of this branch
4383 // isn't BB itself. If so, this is an infinite loop that will
4384 // keep getting unwound.
4385 if (PBI
->getSuccessor(PBIOp
) == BB
)
4388 // If predecessor's branch probability to BB is too low don't merge branches.
4389 SmallVector
<uint32_t, 2> PredWeights
;
4390 if (!PBI
->getMetadata(LLVMContext::MD_unpredictable
) &&
4391 extractBranchWeights(*PBI
, PredWeights
) &&
4392 (static_cast<uint64_t>(PredWeights
[0]) + PredWeights
[1]) != 0) {
4394 BranchProbability CommonDestProb
= BranchProbability::getBranchProbability(
4396 static_cast<uint64_t>(PredWeights
[0]) + PredWeights
[1]);
4398 BranchProbability Likely
= TTI
.getPredictableBranchThreshold();
4399 if (CommonDestProb
>= Likely
)
4403 // Do not perform this transformation if it would require
4404 // insertion of a large number of select instructions. For targets
4405 // without predication/cmovs, this is a big pessimization.
4407 BasicBlock
*CommonDest
= PBI
->getSuccessor(PBIOp
);
4408 BasicBlock
*RemovedDest
= PBI
->getSuccessor(PBIOp
^ 1);
4409 unsigned NumPhis
= 0;
4410 for (BasicBlock::iterator II
= CommonDest
->begin(); isa
<PHINode
>(II
);
4412 if (NumPhis
> 2) // Disable this xform.
4416 // Finally, if everything is ok, fold the branches to logical ops.
4417 BasicBlock
*OtherDest
= BI
->getSuccessor(BIOp
^ 1);
4419 LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI
->getParent()
4420 << "AND: " << *BI
->getParent());
4422 SmallVector
<DominatorTree::UpdateType
, 5> Updates
;
4424 // If OtherDest *is* BB, then BB is a basic block with a single conditional
4425 // branch in it, where one edge (OtherDest) goes back to itself but the other
4426 // exits. We don't *know* that the program avoids the infinite loop
4427 // (even though that seems likely). If we do this xform naively, we'll end up
4428 // recursively unpeeling the loop. Since we know that (after the xform is
4429 // done) that the block *is* infinite if reached, we just make it an obviously
4430 // infinite loop with no cond branch.
4431 if (OtherDest
== BB
) {
4432 // Insert it at the end of the function, because it's either code,
4433 // or it won't matter if it's hot. :)
4434 BasicBlock
*InfLoopBlock
=
4435 BasicBlock::Create(BB
->getContext(), "infloop", BB
->getParent());
4436 BranchInst::Create(InfLoopBlock
, InfLoopBlock
);
4438 Updates
.push_back({DominatorTree::Insert
, InfLoopBlock
, InfLoopBlock
});
4439 OtherDest
= InfLoopBlock
;
4442 LLVM_DEBUG(dbgs() << *PBI
->getParent()->getParent());
4444 // BI may have other predecessors. Because of this, we leave
4445 // it alone, but modify PBI.
4447 // Make sure we get to CommonDest on True&True directions.
4448 Value
*PBICond
= PBI
->getCondition();
4449 IRBuilder
<NoFolder
> Builder(PBI
);
4451 PBICond
= Builder
.CreateNot(PBICond
, PBICond
->getName() + ".not");
4453 Value
*BICond
= BI
->getCondition();
4455 BICond
= Builder
.CreateNot(BICond
, BICond
->getName() + ".not");
4457 // Merge the conditions.
4459 createLogicalOp(Builder
, Instruction::Or
, PBICond
, BICond
, "brmerge");
4461 // Modify PBI to branch on the new condition to the new dests.
4462 PBI
->setCondition(Cond
);
4463 PBI
->setSuccessor(0, CommonDest
);
4464 PBI
->setSuccessor(1, OtherDest
);
4467 Updates
.push_back({DominatorTree::Insert
, PBI
->getParent(), OtherDest
});
4468 Updates
.push_back({DominatorTree::Delete
, PBI
->getParent(), RemovedDest
});
4470 DTU
->applyUpdates(Updates
);
4473 // Update branch weight for PBI.
4474 uint64_t PredTrueWeight
, PredFalseWeight
, SuccTrueWeight
, SuccFalseWeight
;
4475 uint64_t PredCommon
, PredOther
, SuccCommon
, SuccOther
;
4477 extractPredSuccWeights(PBI
, BI
, PredTrueWeight
, PredFalseWeight
,
4478 SuccTrueWeight
, SuccFalseWeight
);
4480 PredCommon
= PBIOp
? PredFalseWeight
: PredTrueWeight
;
4481 PredOther
= PBIOp
? PredTrueWeight
: PredFalseWeight
;
4482 SuccCommon
= BIOp
? SuccFalseWeight
: SuccTrueWeight
;
4483 SuccOther
= BIOp
? SuccTrueWeight
: SuccFalseWeight
;
4484 // The weight to CommonDest should be PredCommon * SuccTotal +
4485 // PredOther * SuccCommon.
4486 // The weight to OtherDest should be PredOther * SuccOther.
4487 uint64_t NewWeights
[2] = {PredCommon
* (SuccCommon
+ SuccOther
) +
4488 PredOther
* SuccCommon
,
4489 PredOther
* SuccOther
};
4490 // Halve the weights if any of them cannot fit in an uint32_t
4491 FitWeights(NewWeights
);
4493 setBranchWeights(PBI
, NewWeights
[0], NewWeights
[1]);
4496 // OtherDest may have phi nodes. If so, add an entry from PBI's
4497 // block that are identical to the entries for BI's block.
4498 AddPredecessorToBlock(OtherDest
, PBI
->getParent(), BB
);
4500 // We know that the CommonDest already had an edge from PBI to
4501 // it. If it has PHIs though, the PHIs may have different
4502 // entries for BB and PBI's BB. If so, insert a select to make
4504 for (PHINode
&PN
: CommonDest
->phis()) {
4505 Value
*BIV
= PN
.getIncomingValueForBlock(BB
);
4506 unsigned PBBIdx
= PN
.getBasicBlockIndex(PBI
->getParent());
4507 Value
*PBIV
= PN
.getIncomingValue(PBBIdx
);
4509 // Insert a select in PBI to pick the right value.
4510 SelectInst
*NV
= cast
<SelectInst
>(
4511 Builder
.CreateSelect(PBICond
, PBIV
, BIV
, PBIV
->getName() + ".mux"));
4512 PN
.setIncomingValue(PBBIdx
, NV
);
4513 // Although the select has the same condition as PBI, the original branch
4514 // weights for PBI do not apply to the new select because the select's
4515 // 'logical' edges are incoming edges of the phi that is eliminated, not
4516 // the outgoing edges of PBI.
4518 uint64_t PredCommon
= PBIOp
? PredFalseWeight
: PredTrueWeight
;
4519 uint64_t PredOther
= PBIOp
? PredTrueWeight
: PredFalseWeight
;
4520 uint64_t SuccCommon
= BIOp
? SuccFalseWeight
: SuccTrueWeight
;
4521 uint64_t SuccOther
= BIOp
? SuccTrueWeight
: SuccFalseWeight
;
4522 // The weight to PredCommonDest should be PredCommon * SuccTotal.
4523 // The weight to PredOtherDest should be PredOther * SuccCommon.
4524 uint64_t NewWeights
[2] = {PredCommon
* (SuccCommon
+ SuccOther
),
4525 PredOther
* SuccCommon
};
4527 FitWeights(NewWeights
);
4529 setBranchWeights(NV
, NewWeights
[0], NewWeights
[1]);
4534 LLVM_DEBUG(dbgs() << "INTO: " << *PBI
->getParent());
4535 LLVM_DEBUG(dbgs() << *PBI
->getParent()->getParent());
4537 // This basic block is probably dead. We know it has at least
4538 // one fewer predecessor.
4542 // Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
4543 // true or to FalseBB if Cond is false.
4544 // Takes care of updating the successors and removing the old terminator.
4545 // Also makes sure not to introduce new successors by assuming that edges to
4546 // non-successor TrueBBs and FalseBBs aren't reachable.
4547 bool SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction
*OldTerm
,
4548 Value
*Cond
, BasicBlock
*TrueBB
,
4549 BasicBlock
*FalseBB
,
4550 uint32_t TrueWeight
,
4551 uint32_t FalseWeight
) {
4552 auto *BB
= OldTerm
->getParent();
4553 // Remove any superfluous successor edges from the CFG.
4554 // First, figure out which successors to preserve.
4555 // If TrueBB and FalseBB are equal, only try to preserve one copy of that
4557 BasicBlock
*KeepEdge1
= TrueBB
;
4558 BasicBlock
*KeepEdge2
= TrueBB
!= FalseBB
? FalseBB
: nullptr;
4560 SmallSetVector
<BasicBlock
*, 2> RemovedSuccessors
;
4562 // Then remove the rest.
4563 for (BasicBlock
*Succ
: successors(OldTerm
)) {
4564 // Make sure only to keep exactly one copy of each edge.
4565 if (Succ
== KeepEdge1
)
4566 KeepEdge1
= nullptr;
4567 else if (Succ
== KeepEdge2
)
4568 KeepEdge2
= nullptr;
4570 Succ
->removePredecessor(BB
,
4571 /*KeepOneInputPHIs=*/true);
4573 if (Succ
!= TrueBB
&& Succ
!= FalseBB
)
4574 RemovedSuccessors
.insert(Succ
);
4578 IRBuilder
<> Builder(OldTerm
);
4579 Builder
.SetCurrentDebugLocation(OldTerm
->getDebugLoc());
4581 // Insert an appropriate new terminator.
4582 if (!KeepEdge1
&& !KeepEdge2
) {
4583 if (TrueBB
== FalseBB
) {
4584 // We were only looking for one successor, and it was present.
4585 // Create an unconditional branch to it.
4586 Builder
.CreateBr(TrueBB
);
4588 // We found both of the successors we were looking for.
4589 // Create a conditional branch sharing the condition of the select.
4590 BranchInst
*NewBI
= Builder
.CreateCondBr(Cond
, TrueBB
, FalseBB
);
4591 if (TrueWeight
!= FalseWeight
)
4592 setBranchWeights(NewBI
, TrueWeight
, FalseWeight
);
4594 } else if (KeepEdge1
&& (KeepEdge2
|| TrueBB
== FalseBB
)) {
4595 // Neither of the selected blocks were successors, so this
4596 // terminator must be unreachable.
4597 new UnreachableInst(OldTerm
->getContext(), OldTerm
);
4599 // One of the selected values was a successor, but the other wasn't.
4600 // Insert an unconditional branch to the one that was found;
4601 // the edge to the one that wasn't must be unreachable.
4603 // Only TrueBB was found.
4604 Builder
.CreateBr(TrueBB
);
4606 // Only FalseBB was found.
4607 Builder
.CreateBr(FalseBB
);
4611 EraseTerminatorAndDCECond(OldTerm
);
4614 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
4615 Updates
.reserve(RemovedSuccessors
.size());
4616 for (auto *RemovedSuccessor
: RemovedSuccessors
)
4617 Updates
.push_back({DominatorTree::Delete
, BB
, RemovedSuccessor
});
4618 DTU
->applyUpdates(Updates
);
4625 // (switch (select cond, X, Y)) on constant X, Y
4626 // with a branch - conditional if X and Y lead to distinct BBs,
4627 // unconditional otherwise.
4628 bool SimplifyCFGOpt::SimplifySwitchOnSelect(SwitchInst
*SI
,
4629 SelectInst
*Select
) {
4630 // Check for constant integer values in the select.
4631 ConstantInt
*TrueVal
= dyn_cast
<ConstantInt
>(Select
->getTrueValue());
4632 ConstantInt
*FalseVal
= dyn_cast
<ConstantInt
>(Select
->getFalseValue());
4633 if (!TrueVal
|| !FalseVal
)
4636 // Find the relevant condition and destinations.
4637 Value
*Condition
= Select
->getCondition();
4638 BasicBlock
*TrueBB
= SI
->findCaseValue(TrueVal
)->getCaseSuccessor();
4639 BasicBlock
*FalseBB
= SI
->findCaseValue(FalseVal
)->getCaseSuccessor();
4641 // Get weight for TrueBB and FalseBB.
4642 uint32_t TrueWeight
= 0, FalseWeight
= 0;
4643 SmallVector
<uint64_t, 8> Weights
;
4644 bool HasWeights
= hasBranchWeightMD(*SI
);
4646 GetBranchWeights(SI
, Weights
);
4647 if (Weights
.size() == 1 + SI
->getNumCases()) {
4649 (uint32_t)Weights
[SI
->findCaseValue(TrueVal
)->getSuccessorIndex()];
4651 (uint32_t)Weights
[SI
->findCaseValue(FalseVal
)->getSuccessorIndex()];
4655 // Perform the actual simplification.
4656 return SimplifyTerminatorOnSelect(SI
, Condition
, TrueBB
, FalseBB
, TrueWeight
,
4661 // (indirectbr (select cond, blockaddress(@fn, BlockA),
4662 // blockaddress(@fn, BlockB)))
4664 // (br cond, BlockA, BlockB).
4665 bool SimplifyCFGOpt::SimplifyIndirectBrOnSelect(IndirectBrInst
*IBI
,
4667 // Check that both operands of the select are block addresses.
4668 BlockAddress
*TBA
= dyn_cast
<BlockAddress
>(SI
->getTrueValue());
4669 BlockAddress
*FBA
= dyn_cast
<BlockAddress
>(SI
->getFalseValue());
4673 // Extract the actual blocks.
4674 BasicBlock
*TrueBB
= TBA
->getBasicBlock();
4675 BasicBlock
*FalseBB
= FBA
->getBasicBlock();
4677 // Perform the actual simplification.
4678 return SimplifyTerminatorOnSelect(IBI
, SI
->getCondition(), TrueBB
, FalseBB
, 0,
4682 /// This is called when we find an icmp instruction
4683 /// (a seteq/setne with a constant) as the only instruction in a
4684 /// block that ends with an uncond branch. We are looking for a very specific
4685 /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
4686 /// this case, we merge the first two "or's of icmp" into a switch, but then the
4687 /// default value goes to an uncond block with a seteq in it, we get something
4690 /// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
4692 /// %tmp = icmp eq i8 %A, 92
4695 /// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
4697 /// We prefer to split the edge to 'end' so that there is a true/false entry to
4698 /// the PHI, merging the third icmp into the switch.
4699 bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
4700 ICmpInst
*ICI
, IRBuilder
<> &Builder
) {
4701 BasicBlock
*BB
= ICI
->getParent();
4703 // If the block has any PHIs in it or the icmp has multiple uses, it is too
4705 if (isa
<PHINode
>(BB
->begin()) || !ICI
->hasOneUse())
4708 Value
*V
= ICI
->getOperand(0);
4709 ConstantInt
*Cst
= cast
<ConstantInt
>(ICI
->getOperand(1));
4711 // The pattern we're looking for is where our only predecessor is a switch on
4712 // 'V' and this block is the default case for the switch. In this case we can
4713 // fold the compared value into the switch to simplify things.
4714 BasicBlock
*Pred
= BB
->getSinglePredecessor();
4715 if (!Pred
|| !isa
<SwitchInst
>(Pred
->getTerminator()))
4718 SwitchInst
*SI
= cast
<SwitchInst
>(Pred
->getTerminator());
4719 if (SI
->getCondition() != V
)
4722 // If BB is reachable on a non-default case, then we simply know the value of
4723 // V in this block. Substitute it and constant fold the icmp instruction
4725 if (SI
->getDefaultDest() != BB
) {
4726 ConstantInt
*VVal
= SI
->findCaseDest(BB
);
4727 assert(VVal
&& "Should have a unique destination value");
4728 ICI
->setOperand(0, VVal
);
4730 if (Value
*V
= simplifyInstruction(ICI
, {DL
, ICI
})) {
4731 ICI
->replaceAllUsesWith(V
);
4732 ICI
->eraseFromParent();
4734 // BB is now empty, so it is likely to simplify away.
4735 return requestResimplify();
4738 // Ok, the block is reachable from the default dest. If the constant we're
4739 // comparing exists in one of the other edges, then we can constant fold ICI
4741 if (SI
->findCaseValue(Cst
) != SI
->case_default()) {
4743 if (ICI
->getPredicate() == ICmpInst::ICMP_EQ
)
4744 V
= ConstantInt::getFalse(BB
->getContext());
4746 V
= ConstantInt::getTrue(BB
->getContext());
4748 ICI
->replaceAllUsesWith(V
);
4749 ICI
->eraseFromParent();
4750 // BB is now empty, so it is likely to simplify away.
4751 return requestResimplify();
4754 // The use of the icmp has to be in the 'end' block, by the only PHI node in
4756 BasicBlock
*SuccBlock
= BB
->getTerminator()->getSuccessor(0);
4757 PHINode
*PHIUse
= dyn_cast
<PHINode
>(ICI
->user_back());
4758 if (PHIUse
== nullptr || PHIUse
!= &SuccBlock
->front() ||
4759 isa
<PHINode
>(++BasicBlock::iterator(PHIUse
)))
4762 // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
4764 Constant
*DefaultCst
= ConstantInt::getTrue(BB
->getContext());
4765 Constant
*NewCst
= ConstantInt::getFalse(BB
->getContext());
4767 if (ICI
->getPredicate() == ICmpInst::ICMP_EQ
)
4768 std::swap(DefaultCst
, NewCst
);
4770 // Replace ICI (which is used by the PHI for the default value) with true or
4771 // false depending on if it is EQ or NE.
4772 ICI
->replaceAllUsesWith(DefaultCst
);
4773 ICI
->eraseFromParent();
4775 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
4777 // Okay, the switch goes to this block on a default value. Add an edge from
4778 // the switch to the merge point on the compared value.
4780 BasicBlock::Create(BB
->getContext(), "switch.edge", BB
->getParent(), BB
);
4782 SwitchInstProfUpdateWrapper
SIW(*SI
);
4783 auto W0
= SIW
.getSuccessorWeight(0);
4784 SwitchInstProfUpdateWrapper::CaseWeightOpt NewW
;
4786 NewW
= ((uint64_t(*W0
) + 1) >> 1);
4787 SIW
.setSuccessorWeight(0, *NewW
);
4789 SIW
.addCase(Cst
, NewBB
, NewW
);
4791 Updates
.push_back({DominatorTree::Insert
, Pred
, NewBB
});
4794 // NewBB branches to the phi block, add the uncond branch and the phi entry.
4795 Builder
.SetInsertPoint(NewBB
);
4796 Builder
.SetCurrentDebugLocation(SI
->getDebugLoc());
4797 Builder
.CreateBr(SuccBlock
);
4798 PHIUse
->addIncoming(NewCst
, NewBB
);
4800 Updates
.push_back({DominatorTree::Insert
, NewBB
, SuccBlock
});
4801 DTU
->applyUpdates(Updates
);
4806 /// The specified branch is a conditional branch.
4807 /// Check to see if it is branching on an or/and chain of icmp instructions, and
4808 /// fold it into a switch instruction if so.
4809 bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst
*BI
,
4810 IRBuilder
<> &Builder
,
4811 const DataLayout
&DL
) {
4812 Instruction
*Cond
= dyn_cast
<Instruction
>(BI
->getCondition());
4816 // Change br (X == 0 | X == 1), T, F into a switch instruction.
4817 // If this is a bunch of seteq's or'd together, or if it's a bunch of
4818 // 'setne's and'ed together, collect them.
4820 // Try to gather values from a chain of and/or to be turned into a switch
4821 ConstantComparesGatherer
ConstantCompare(Cond
, DL
);
4822 // Unpack the result
4823 SmallVectorImpl
<ConstantInt
*> &Values
= ConstantCompare
.Vals
;
4824 Value
*CompVal
= ConstantCompare
.CompValue
;
4825 unsigned UsedICmps
= ConstantCompare
.UsedICmps
;
4826 Value
*ExtraCase
= ConstantCompare
.Extra
;
4828 // If we didn't have a multiply compared value, fail.
4832 // Avoid turning single icmps into a switch.
4836 bool TrueWhenEqual
= match(Cond
, m_LogicalOr(m_Value(), m_Value()));
4838 // There might be duplicate constants in the list, which the switch
4839 // instruction can't handle, remove them now.
4840 array_pod_sort(Values
.begin(), Values
.end(), ConstantIntSortPredicate
);
4841 Values
.erase(std::unique(Values
.begin(), Values
.end()), Values
.end());
4843 // If Extra was used, we require at least two switch values to do the
4844 // transformation. A switch with one value is just a conditional branch.
4845 if (ExtraCase
&& Values
.size() < 2)
4848 // TODO: Preserve branch weight metadata, similarly to how
4849 // FoldValueComparisonIntoPredecessors preserves it.
4851 // Figure out which block is which destination.
4852 BasicBlock
*DefaultBB
= BI
->getSuccessor(1);
4853 BasicBlock
*EdgeBB
= BI
->getSuccessor(0);
4855 std::swap(DefaultBB
, EdgeBB
);
4857 BasicBlock
*BB
= BI
->getParent();
4859 LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values
.size()
4860 << " cases into SWITCH. BB is:\n"
4863 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
4865 // If there are any extra values that couldn't be folded into the switch
4866 // then we evaluate them with an explicit branch first. Split the block
4867 // right before the condbr to handle it.
4869 BasicBlock
*NewBB
= SplitBlock(BB
, BI
, DTU
, /*LI=*/nullptr,
4870 /*MSSAU=*/nullptr, "switch.early.test");
4872 // Remove the uncond branch added to the old block.
4873 Instruction
*OldTI
= BB
->getTerminator();
4874 Builder
.SetInsertPoint(OldTI
);
4876 // There can be an unintended UB if extra values are Poison. Before the
4877 // transformation, extra values may not be evaluated according to the
4878 // condition, and it will not raise UB. But after transformation, we are
4879 // evaluating extra values before checking the condition, and it will raise
4880 // UB. It can be solved by adding freeze instruction to extra values.
4881 AssumptionCache
*AC
= Options
.AC
;
4883 if (!isGuaranteedNotToBeUndefOrPoison(ExtraCase
, AC
, BI
, nullptr))
4884 ExtraCase
= Builder
.CreateFreeze(ExtraCase
);
4887 Builder
.CreateCondBr(ExtraCase
, EdgeBB
, NewBB
);
4889 Builder
.CreateCondBr(ExtraCase
, NewBB
, EdgeBB
);
4891 OldTI
->eraseFromParent();
4894 Updates
.push_back({DominatorTree::Insert
, BB
, EdgeBB
});
4896 // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
4897 // for the edge we just added.
4898 AddPredecessorToBlock(EdgeBB
, BB
, NewBB
);
4900 LLVM_DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
4901 << "\nEXTRABB = " << *BB
);
4905 Builder
.SetInsertPoint(BI
);
4906 // Convert pointer to int before we switch.
4907 if (CompVal
->getType()->isPointerTy()) {
4908 CompVal
= Builder
.CreatePtrToInt(
4909 CompVal
, DL
.getIntPtrType(CompVal
->getType()), "magicptr");
4912 // Create the new switch instruction now.
4913 SwitchInst
*New
= Builder
.CreateSwitch(CompVal
, DefaultBB
, Values
.size());
4915 // Add all of the 'cases' to the switch instruction.
4916 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
4917 New
->addCase(Values
[i
], EdgeBB
);
4919 // We added edges from PI to the EdgeBB. As such, if there were any
4920 // PHI nodes in EdgeBB, they need entries to be added corresponding to
4921 // the number of edges added.
4922 for (BasicBlock::iterator BBI
= EdgeBB
->begin(); isa
<PHINode
>(BBI
); ++BBI
) {
4923 PHINode
*PN
= cast
<PHINode
>(BBI
);
4924 Value
*InVal
= PN
->getIncomingValueForBlock(BB
);
4925 for (unsigned i
= 0, e
= Values
.size() - 1; i
!= e
; ++i
)
4926 PN
->addIncoming(InVal
, BB
);
4929 // Erase the old branch instruction.
4930 EraseTerminatorAndDCECond(BI
);
4932 DTU
->applyUpdates(Updates
);
4934 LLVM_DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB
<< '\n');
4938 bool SimplifyCFGOpt::simplifyResume(ResumeInst
*RI
, IRBuilder
<> &Builder
) {
4939 if (isa
<PHINode
>(RI
->getValue()))
4940 return simplifyCommonResume(RI
);
4941 else if (isa
<LandingPadInst
>(RI
->getParent()->getFirstNonPHI()) &&
4942 RI
->getValue() == RI
->getParent()->getFirstNonPHI())
4943 // The resume must unwind the exception that caused control to branch here.
4944 return simplifySingleResume(RI
);
4949 // Check if cleanup block is empty
4950 static bool isCleanupBlockEmpty(iterator_range
<BasicBlock::iterator
> R
) {
4951 for (Instruction
&I
: R
) {
4952 auto *II
= dyn_cast
<IntrinsicInst
>(&I
);
4956 Intrinsic::ID IntrinsicID
= II
->getIntrinsicID();
4957 switch (IntrinsicID
) {
4958 case Intrinsic::dbg_declare
:
4959 case Intrinsic::dbg_value
:
4960 case Intrinsic::dbg_label
:
4961 case Intrinsic::lifetime_end
:
4970 // Simplify resume that is shared by several landing pads (phi of landing pad).
4971 bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst
*RI
) {
4972 BasicBlock
*BB
= RI
->getParent();
4974 // Check that there are no other instructions except for debug and lifetime
4975 // intrinsics between the phi's and resume instruction.
4976 if (!isCleanupBlockEmpty(
4977 make_range(RI
->getParent()->getFirstNonPHI(), BB
->getTerminator())))
4980 SmallSetVector
<BasicBlock
*, 4> TrivialUnwindBlocks
;
4981 auto *PhiLPInst
= cast
<PHINode
>(RI
->getValue());
4983 // Check incoming blocks to see if any of them are trivial.
4984 for (unsigned Idx
= 0, End
= PhiLPInst
->getNumIncomingValues(); Idx
!= End
;
4986 auto *IncomingBB
= PhiLPInst
->getIncomingBlock(Idx
);
4987 auto *IncomingValue
= PhiLPInst
->getIncomingValue(Idx
);
4989 // If the block has other successors, we can not delete it because
4990 // it has other dependents.
4991 if (IncomingBB
->getUniqueSuccessor() != BB
)
4994 auto *LandingPad
= dyn_cast
<LandingPadInst
>(IncomingBB
->getFirstNonPHI());
4995 // Not the landing pad that caused the control to branch here.
4996 if (IncomingValue
!= LandingPad
)
4999 if (isCleanupBlockEmpty(
5000 make_range(LandingPad
->getNextNode(), IncomingBB
->getTerminator())))
5001 TrivialUnwindBlocks
.insert(IncomingBB
);
5004 // If no trivial unwind blocks, don't do any simplifications.
5005 if (TrivialUnwindBlocks
.empty())
5008 // Turn all invokes that unwind here into calls.
5009 for (auto *TrivialBB
: TrivialUnwindBlocks
) {
5010 // Blocks that will be simplified should be removed from the phi node.
5011 // Note there could be multiple edges to the resume block, and we need
5012 // to remove them all.
5013 while (PhiLPInst
->getBasicBlockIndex(TrivialBB
) != -1)
5014 BB
->removePredecessor(TrivialBB
, true);
5016 for (BasicBlock
*Pred
:
5017 llvm::make_early_inc_range(predecessors(TrivialBB
))) {
5018 removeUnwindEdge(Pred
, DTU
);
5022 // In each SimplifyCFG run, only the current processed block can be erased.
5023 // Otherwise, it will break the iteration of SimplifyCFG pass. So instead
5024 // of erasing TrivialBB, we only remove the branch to the common resume
5025 // block so that we can later erase the resume block since it has no
5027 TrivialBB
->getTerminator()->eraseFromParent();
5028 new UnreachableInst(RI
->getContext(), TrivialBB
);
5030 DTU
->applyUpdates({{DominatorTree::Delete
, TrivialBB
, BB
}});
5033 // Delete the resume block if all its predecessors have been removed.
5035 DeleteDeadBlock(BB
, DTU
);
5037 return !TrivialUnwindBlocks
.empty();
5040 // Simplify resume that is only used by a single (non-phi) landing pad.
5041 bool SimplifyCFGOpt::simplifySingleResume(ResumeInst
*RI
) {
5042 BasicBlock
*BB
= RI
->getParent();
5043 auto *LPInst
= cast
<LandingPadInst
>(BB
->getFirstNonPHI());
5044 assert(RI
->getValue() == LPInst
&&
5045 "Resume must unwind the exception that caused control to here");
5047 // Check that there are no other instructions except for debug intrinsics.
5048 if (!isCleanupBlockEmpty(
5049 make_range
<Instruction
*>(LPInst
->getNextNode(), RI
)))
5052 // Turn all invokes that unwind here into calls and delete the basic block.
5053 for (BasicBlock
*Pred
: llvm::make_early_inc_range(predecessors(BB
))) {
5054 removeUnwindEdge(Pred
, DTU
);
5058 // The landingpad is now unreachable. Zap it.
5059 DeleteDeadBlock(BB
, DTU
);
5063 static bool removeEmptyCleanup(CleanupReturnInst
*RI
, DomTreeUpdater
*DTU
) {
5064 // If this is a trivial cleanup pad that executes no instructions, it can be
5065 // eliminated. If the cleanup pad continues to the caller, any predecessor
5066 // that is an EH pad will be updated to continue to the caller and any
5067 // predecessor that terminates with an invoke instruction will have its invoke
5068 // instruction converted to a call instruction. If the cleanup pad being
5069 // simplified does not continue to the caller, each predecessor will be
5070 // updated to continue to the unwind destination of the cleanup pad being
5072 BasicBlock
*BB
= RI
->getParent();
5073 CleanupPadInst
*CPInst
= RI
->getCleanupPad();
5074 if (CPInst
->getParent() != BB
)
5075 // This isn't an empty cleanup.
5078 // We cannot kill the pad if it has multiple uses. This typically arises
5079 // from unreachable basic blocks.
5080 if (!CPInst
->hasOneUse())
5083 // Check that there are no other instructions except for benign intrinsics.
5084 if (!isCleanupBlockEmpty(
5085 make_range
<Instruction
*>(CPInst
->getNextNode(), RI
)))
5088 // If the cleanup return we are simplifying unwinds to the caller, this will
5089 // set UnwindDest to nullptr.
5090 BasicBlock
*UnwindDest
= RI
->getUnwindDest();
5091 Instruction
*DestEHPad
= UnwindDest
? UnwindDest
->getFirstNonPHI() : nullptr;
5093 // We're about to remove BB from the control flow. Before we do, sink any
5094 // PHINodes into the unwind destination. Doing this before changing the
5095 // control flow avoids some potentially slow checks, since we can currently
5096 // be certain that UnwindDest and BB have no common predecessors (since they
5097 // are both EH pads).
5099 // First, go through the PHI nodes in UnwindDest and update any nodes that
5100 // reference the block we are removing
5101 for (PHINode
&DestPN
: UnwindDest
->phis()) {
5102 int Idx
= DestPN
.getBasicBlockIndex(BB
);
5103 // Since BB unwinds to UnwindDest, it has to be in the PHI node.
5105 // This PHI node has an incoming value that corresponds to a control
5106 // path through the cleanup pad we are removing. If the incoming
5107 // value is in the cleanup pad, it must be a PHINode (because we
5108 // verified above that the block is otherwise empty). Otherwise, the
5109 // value is either a constant or a value that dominates the cleanup
5110 // pad being removed.
5112 // Because BB and UnwindDest are both EH pads, all of their
5113 // predecessors must unwind to these blocks, and since no instruction
5114 // can have multiple unwind destinations, there will be no overlap in
5115 // incoming blocks between SrcPN and DestPN.
5116 Value
*SrcVal
= DestPN
.getIncomingValue(Idx
);
5117 PHINode
*SrcPN
= dyn_cast
<PHINode
>(SrcVal
);
5119 bool NeedPHITranslation
= SrcPN
&& SrcPN
->getParent() == BB
;
5120 for (auto *Pred
: predecessors(BB
)) {
5122 NeedPHITranslation
? SrcPN
->getIncomingValueForBlock(Pred
) : SrcVal
;
5123 DestPN
.addIncoming(Incoming
, Pred
);
5127 // Sink any remaining PHI nodes directly into UnwindDest.
5128 Instruction
*InsertPt
= DestEHPad
;
5129 for (PHINode
&PN
: make_early_inc_range(BB
->phis())) {
5130 if (PN
.use_empty() || !PN
.isUsedOutsideOfBlock(BB
))
5131 // If the PHI node has no uses or all of its uses are in this basic
5132 // block (meaning they are debug or lifetime intrinsics), just leave
5133 // it. It will be erased when we erase BB below.
5136 // Otherwise, sink this PHI node into UnwindDest.
5137 // Any predecessors to UnwindDest which are not already represented
5138 // must be back edges which inherit the value from the path through
5139 // BB. In this case, the PHI value must reference itself.
5140 for (auto *pred
: predecessors(UnwindDest
))
5142 PN
.addIncoming(&PN
, pred
);
5143 PN
.moveBefore(InsertPt
);
5144 // Also, add a dummy incoming value for the original BB itself,
5145 // so that the PHI is well-formed until we drop said predecessor.
5146 PN
.addIncoming(PoisonValue::get(PN
.getType()), BB
);
5150 std::vector
<DominatorTree::UpdateType
> Updates
;
5152 // We use make_early_inc_range here because we will remove all predecessors.
5153 for (BasicBlock
*PredBB
: llvm::make_early_inc_range(predecessors(BB
))) {
5154 if (UnwindDest
== nullptr) {
5156 DTU
->applyUpdates(Updates
);
5159 removeUnwindEdge(PredBB
, DTU
);
5162 BB
->removePredecessor(PredBB
);
5163 Instruction
*TI
= PredBB
->getTerminator();
5164 TI
->replaceUsesOfWith(BB
, UnwindDest
);
5166 Updates
.push_back({DominatorTree::Insert
, PredBB
, UnwindDest
});
5167 Updates
.push_back({DominatorTree::Delete
, PredBB
, BB
});
5173 DTU
->applyUpdates(Updates
);
5175 DeleteDeadBlock(BB
, DTU
);
5180 // Try to merge two cleanuppads together.
5181 static bool mergeCleanupPad(CleanupReturnInst
*RI
) {
5182 // Skip any cleanuprets which unwind to caller, there is nothing to merge
5184 BasicBlock
*UnwindDest
= RI
->getUnwindDest();
5188 // This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
5189 // be safe to merge without code duplication.
5190 if (UnwindDest
->getSinglePredecessor() != RI
->getParent())
5193 // Verify that our cleanuppad's unwind destination is another cleanuppad.
5194 auto *SuccessorCleanupPad
= dyn_cast
<CleanupPadInst
>(&UnwindDest
->front());
5195 if (!SuccessorCleanupPad
)
5198 CleanupPadInst
*PredecessorCleanupPad
= RI
->getCleanupPad();
5199 // Replace any uses of the successor cleanupad with the predecessor pad
5200 // The only cleanuppad uses should be this cleanupret, it's cleanupret and
5201 // funclet bundle operands.
5202 SuccessorCleanupPad
->replaceAllUsesWith(PredecessorCleanupPad
);
5203 // Remove the old cleanuppad.
5204 SuccessorCleanupPad
->eraseFromParent();
5205 // Now, we simply replace the cleanupret with a branch to the unwind
5207 BranchInst::Create(UnwindDest
, RI
->getParent());
5208 RI
->eraseFromParent();
5213 bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst
*RI
) {
5214 // It is possible to transiantly have an undef cleanuppad operand because we
5215 // have deleted some, but not all, dead blocks.
5216 // Eventually, this block will be deleted.
5217 if (isa
<UndefValue
>(RI
->getOperand(0)))
5220 if (mergeCleanupPad(RI
))
5223 if (removeEmptyCleanup(RI
, DTU
))
5229 // WARNING: keep in sync with InstCombinerImpl::visitUnreachableInst()!
5230 bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst
*UI
) {
5231 BasicBlock
*BB
= UI
->getParent();
5233 bool Changed
= false;
5235 // Ensure that any debug-info records that used to occur after the Unreachable
5236 // are moved to in front of it -- otherwise they'll "dangle" at the end of
5238 BB
->flushTerminatorDbgValues();
5240 // Debug-info records on the unreachable inst itself should be deleted, as
5241 // below we delete everything past the final executable instruction.
5242 UI
->dropDbgValues();
5244 // If there are any instructions immediately before the unreachable that can
5245 // be removed, do so.
5246 while (UI
->getIterator() != BB
->begin()) {
5247 BasicBlock::iterator BBI
= UI
->getIterator();
5250 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI
))
5251 break; // Can not drop any more instructions. We're done here.
5252 // Otherwise, this instruction can be freely erased,
5253 // even if it is not side-effect free.
5255 // Note that deleting EH's here is in fact okay, although it involves a bit
5256 // of subtle reasoning. If this inst is an EH, all the predecessors of this
5257 // block will be the unwind edges of Invoke/CatchSwitch/CleanupReturn,
5258 // and we can therefore guarantee this block will be erased.
5260 // If we're deleting this, we're deleting any subsequent dbg.values, so
5261 // delete DPValue records of variable information.
5262 BBI
->dropDbgValues();
5264 // Delete this instruction (any uses are guaranteed to be dead)
5265 BBI
->replaceAllUsesWith(PoisonValue::get(BBI
->getType()));
5266 BBI
->eraseFromParent();
5270 // If the unreachable instruction is the first in the block, take a gander
5271 // at all of the predecessors of this instruction, and simplify them.
5272 if (&BB
->front() != UI
)
5275 std::vector
<DominatorTree::UpdateType
> Updates
;
5277 SmallSetVector
<BasicBlock
*, 8> Preds(pred_begin(BB
), pred_end(BB
));
5278 for (unsigned i
= 0, e
= Preds
.size(); i
!= e
; ++i
) {
5279 auto *Predecessor
= Preds
[i
];
5280 Instruction
*TI
= Predecessor
->getTerminator();
5281 IRBuilder
<> Builder(TI
);
5282 if (auto *BI
= dyn_cast
<BranchInst
>(TI
)) {
5283 // We could either have a proper unconditional branch,
5284 // or a degenerate conditional branch with matching destinations.
5285 if (all_of(BI
->successors(),
5286 [BB
](auto *Successor
) { return Successor
== BB
; })) {
5287 new UnreachableInst(TI
->getContext(), TI
);
5288 TI
->eraseFromParent();
5291 assert(BI
->isConditional() && "Can't get here with an uncond branch.");
5292 Value
* Cond
= BI
->getCondition();
5293 assert(BI
->getSuccessor(0) != BI
->getSuccessor(1) &&
5294 "The destinations are guaranteed to be different here.");
5295 CallInst
*Assumption
;
5296 if (BI
->getSuccessor(0) == BB
) {
5297 Assumption
= Builder
.CreateAssumption(Builder
.CreateNot(Cond
));
5298 Builder
.CreateBr(BI
->getSuccessor(1));
5300 assert(BI
->getSuccessor(1) == BB
&& "Incorrect CFG");
5301 Assumption
= Builder
.CreateAssumption(Cond
);
5302 Builder
.CreateBr(BI
->getSuccessor(0));
5305 Options
.AC
->registerAssumption(cast
<AssumeInst
>(Assumption
));
5307 EraseTerminatorAndDCECond(BI
);
5311 Updates
.push_back({DominatorTree::Delete
, Predecessor
, BB
});
5312 } else if (auto *SI
= dyn_cast
<SwitchInst
>(TI
)) {
5313 SwitchInstProfUpdateWrapper
SU(*SI
);
5314 for (auto i
= SU
->case_begin(), e
= SU
->case_end(); i
!= e
;) {
5315 if (i
->getCaseSuccessor() != BB
) {
5319 BB
->removePredecessor(SU
->getParent());
5320 i
= SU
.removeCase(i
);
5324 // Note that the default destination can't be removed!
5325 if (DTU
&& SI
->getDefaultDest() != BB
)
5326 Updates
.push_back({DominatorTree::Delete
, Predecessor
, BB
});
5327 } else if (auto *II
= dyn_cast
<InvokeInst
>(TI
)) {
5328 if (II
->getUnwindDest() == BB
) {
5330 DTU
->applyUpdates(Updates
);
5333 auto *CI
= cast
<CallInst
>(removeUnwindEdge(TI
->getParent(), DTU
));
5334 if (!CI
->doesNotThrow())
5335 CI
->setDoesNotThrow();
5338 } else if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(TI
)) {
5339 if (CSI
->getUnwindDest() == BB
) {
5341 DTU
->applyUpdates(Updates
);
5344 removeUnwindEdge(TI
->getParent(), DTU
);
5349 for (CatchSwitchInst::handler_iterator I
= CSI
->handler_begin(),
5350 E
= CSI
->handler_end();
5353 CSI
->removeHandler(I
);
5360 Updates
.push_back({DominatorTree::Delete
, Predecessor
, BB
});
5361 if (CSI
->getNumHandlers() == 0) {
5362 if (CSI
->hasUnwindDest()) {
5363 // Redirect all predecessors of the block containing CatchSwitchInst
5364 // to instead branch to the CatchSwitchInst's unwind destination.
5366 for (auto *PredecessorOfPredecessor
: predecessors(Predecessor
)) {
5367 Updates
.push_back({DominatorTree::Insert
,
5368 PredecessorOfPredecessor
,
5369 CSI
->getUnwindDest()});
5370 Updates
.push_back({DominatorTree::Delete
,
5371 PredecessorOfPredecessor
, Predecessor
});
5374 Predecessor
->replaceAllUsesWith(CSI
->getUnwindDest());
5376 // Rewrite all preds to unwind to caller (or from invoke to call).
5378 DTU
->applyUpdates(Updates
);
5381 SmallVector
<BasicBlock
*, 8> EHPreds(predecessors(Predecessor
));
5382 for (BasicBlock
*EHPred
: EHPreds
)
5383 removeUnwindEdge(EHPred
, DTU
);
5385 // The catchswitch is no longer reachable.
5386 new UnreachableInst(CSI
->getContext(), CSI
);
5387 CSI
->eraseFromParent();
5390 } else if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(TI
)) {
5392 assert(CRI
->hasUnwindDest() && CRI
->getUnwindDest() == BB
&&
5393 "Expected to always have an unwind to BB.");
5395 Updates
.push_back({DominatorTree::Delete
, Predecessor
, BB
});
5396 new UnreachableInst(TI
->getContext(), TI
);
5397 TI
->eraseFromParent();
5403 DTU
->applyUpdates(Updates
);
5405 // If this block is now dead, remove it.
5406 if (pred_empty(BB
) && BB
!= &BB
->getParent()->getEntryBlock()) {
5407 DeleteDeadBlock(BB
, DTU
);
5414 static bool CasesAreContiguous(SmallVectorImpl
<ConstantInt
*> &Cases
) {
5415 assert(Cases
.size() >= 1);
5417 array_pod_sort(Cases
.begin(), Cases
.end(), ConstantIntSortPredicate
);
5418 for (size_t I
= 1, E
= Cases
.size(); I
!= E
; ++I
) {
5419 if (Cases
[I
- 1]->getValue() != Cases
[I
]->getValue() + 1)
5425 static void createUnreachableSwitchDefault(SwitchInst
*Switch
,
5426 DomTreeUpdater
*DTU
) {
5427 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
5428 auto *BB
= Switch
->getParent();
5429 auto *OrigDefaultBlock
= Switch
->getDefaultDest();
5430 OrigDefaultBlock
->removePredecessor(BB
);
5431 BasicBlock
*NewDefaultBlock
= BasicBlock::Create(
5432 BB
->getContext(), BB
->getName() + ".unreachabledefault", BB
->getParent(),
5434 new UnreachableInst(Switch
->getContext(), NewDefaultBlock
);
5435 Switch
->setDefaultDest(&*NewDefaultBlock
);
5437 SmallVector
<DominatorTree::UpdateType
, 2> Updates
;
5438 Updates
.push_back({DominatorTree::Insert
, BB
, &*NewDefaultBlock
});
5439 if (!is_contained(successors(BB
), OrigDefaultBlock
))
5440 Updates
.push_back({DominatorTree::Delete
, BB
, &*OrigDefaultBlock
});
5441 DTU
->applyUpdates(Updates
);
5445 /// Turn a switch into an integer range comparison and branch.
5446 /// Switches with more than 2 destinations are ignored.
5447 /// Switches with 1 destination are also ignored.
5448 bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst
*SI
,
5449 IRBuilder
<> &Builder
) {
5450 assert(SI
->getNumCases() > 1 && "Degenerate switch?");
5453 !isa
<UnreachableInst
>(SI
->getDefaultDest()->getFirstNonPHIOrDbg());
5455 auto *BB
= SI
->getParent();
5457 // Partition the cases into two sets with different destinations.
5458 BasicBlock
*DestA
= HasDefault
? SI
->getDefaultDest() : nullptr;
5459 BasicBlock
*DestB
= nullptr;
5460 SmallVector
<ConstantInt
*, 16> CasesA
;
5461 SmallVector
<ConstantInt
*, 16> CasesB
;
5463 for (auto Case
: SI
->cases()) {
5464 BasicBlock
*Dest
= Case
.getCaseSuccessor();
5467 if (Dest
== DestA
) {
5468 CasesA
.push_back(Case
.getCaseValue());
5473 if (Dest
== DestB
) {
5474 CasesB
.push_back(Case
.getCaseValue());
5477 return false; // More than two destinations.
5480 return false; // All destinations are the same and the default is unreachable
5482 assert(DestA
&& DestB
&&
5483 "Single-destination switch should have been folded.");
5484 assert(DestA
!= DestB
);
5485 assert(DestB
!= SI
->getDefaultDest());
5486 assert(!CasesB
.empty() && "There must be non-default cases.");
5487 assert(!CasesA
.empty() || HasDefault
);
5489 // Figure out if one of the sets of cases form a contiguous range.
5490 SmallVectorImpl
<ConstantInt
*> *ContiguousCases
= nullptr;
5491 BasicBlock
*ContiguousDest
= nullptr;
5492 BasicBlock
*OtherDest
= nullptr;
5493 if (!CasesA
.empty() && CasesAreContiguous(CasesA
)) {
5494 ContiguousCases
= &CasesA
;
5495 ContiguousDest
= DestA
;
5497 } else if (CasesAreContiguous(CasesB
)) {
5498 ContiguousCases
= &CasesB
;
5499 ContiguousDest
= DestB
;
5504 // Start building the compare and branch.
5506 Constant
*Offset
= ConstantExpr::getNeg(ContiguousCases
->back());
5507 Constant
*NumCases
=
5508 ConstantInt::get(Offset
->getType(), ContiguousCases
->size());
5510 Value
*Sub
= SI
->getCondition();
5511 if (!Offset
->isNullValue())
5512 Sub
= Builder
.CreateAdd(Sub
, Offset
, Sub
->getName() + ".off");
5515 // If NumCases overflowed, then all possible values jump to the successor.
5516 if (NumCases
->isNullValue() && !ContiguousCases
->empty())
5517 Cmp
= ConstantInt::getTrue(SI
->getContext());
5519 Cmp
= Builder
.CreateICmpULT(Sub
, NumCases
, "switch");
5520 BranchInst
*NewBI
= Builder
.CreateCondBr(Cmp
, ContiguousDest
, OtherDest
);
5522 // Update weight for the newly-created conditional branch.
5523 if (hasBranchWeightMD(*SI
)) {
5524 SmallVector
<uint64_t, 8> Weights
;
5525 GetBranchWeights(SI
, Weights
);
5526 if (Weights
.size() == 1 + SI
->getNumCases()) {
5527 uint64_t TrueWeight
= 0;
5528 uint64_t FalseWeight
= 0;
5529 for (size_t I
= 0, E
= Weights
.size(); I
!= E
; ++I
) {
5530 if (SI
->getSuccessor(I
) == ContiguousDest
)
5531 TrueWeight
+= Weights
[I
];
5533 FalseWeight
+= Weights
[I
];
5535 while (TrueWeight
> UINT32_MAX
|| FalseWeight
> UINT32_MAX
) {
5539 setBranchWeights(NewBI
, TrueWeight
, FalseWeight
);
5543 // Prune obsolete incoming values off the successors' PHI nodes.
5544 for (auto BBI
= ContiguousDest
->begin(); isa
<PHINode
>(BBI
); ++BBI
) {
5545 unsigned PreviousEdges
= ContiguousCases
->size();
5546 if (ContiguousDest
== SI
->getDefaultDest())
5548 for (unsigned I
= 0, E
= PreviousEdges
- 1; I
!= E
; ++I
)
5549 cast
<PHINode
>(BBI
)->removeIncomingValue(SI
->getParent());
5551 for (auto BBI
= OtherDest
->begin(); isa
<PHINode
>(BBI
); ++BBI
) {
5552 unsigned PreviousEdges
= SI
->getNumCases() - ContiguousCases
->size();
5553 if (OtherDest
== SI
->getDefaultDest())
5555 for (unsigned I
= 0, E
= PreviousEdges
- 1; I
!= E
; ++I
)
5556 cast
<PHINode
>(BBI
)->removeIncomingValue(SI
->getParent());
5559 // Clean up the default block - it may have phis or other instructions before
5560 // the unreachable terminator.
5562 createUnreachableSwitchDefault(SI
, DTU
);
5564 auto *UnreachableDefault
= SI
->getDefaultDest();
5567 SI
->eraseFromParent();
5569 if (!HasDefault
&& DTU
)
5570 DTU
->applyUpdates({{DominatorTree::Delete
, BB
, UnreachableDefault
}});
5575 /// Compute masked bits for the condition of a switch
5576 /// and use it to remove dead cases.
5577 static bool eliminateDeadSwitchCases(SwitchInst
*SI
, DomTreeUpdater
*DTU
,
5578 AssumptionCache
*AC
,
5579 const DataLayout
&DL
) {
5580 Value
*Cond
= SI
->getCondition();
5581 KnownBits Known
= computeKnownBits(Cond
, DL
, 0, AC
, SI
);
5583 // We can also eliminate cases by determining that their values are outside of
5584 // the limited range of the condition based on how many significant (non-sign)
5585 // bits are in the condition value.
5586 unsigned MaxSignificantBitsInCond
=
5587 ComputeMaxSignificantBits(Cond
, DL
, 0, AC
, SI
);
5589 // Gather dead cases.
5590 SmallVector
<ConstantInt
*, 8> DeadCases
;
5591 SmallDenseMap
<BasicBlock
*, int, 8> NumPerSuccessorCases
;
5592 SmallVector
<BasicBlock
*, 8> UniqueSuccessors
;
5593 for (const auto &Case
: SI
->cases()) {
5594 auto *Successor
= Case
.getCaseSuccessor();
5596 if (!NumPerSuccessorCases
.count(Successor
))
5597 UniqueSuccessors
.push_back(Successor
);
5598 ++NumPerSuccessorCases
[Successor
];
5600 const APInt
&CaseVal
= Case
.getCaseValue()->getValue();
5601 if (Known
.Zero
.intersects(CaseVal
) || !Known
.One
.isSubsetOf(CaseVal
) ||
5602 (CaseVal
.getSignificantBits() > MaxSignificantBitsInCond
)) {
5603 DeadCases
.push_back(Case
.getCaseValue());
5605 --NumPerSuccessorCases
[Successor
];
5606 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
5611 // If we can prove that the cases must cover all possible values, the
5612 // default destination becomes dead and we can remove it. If we know some
5613 // of the bits in the value, we can use that to more precisely compute the
5614 // number of possible unique case values.
5616 !isa
<UnreachableInst
>(SI
->getDefaultDest()->getFirstNonPHIOrDbg());
5617 const unsigned NumUnknownBits
=
5618 Known
.getBitWidth() - (Known
.Zero
| Known
.One
).popcount();
5619 assert(NumUnknownBits
<= Known
.getBitWidth());
5620 if (HasDefault
&& DeadCases
.empty() &&
5621 NumUnknownBits
< 64 /* avoid overflow */ &&
5622 SI
->getNumCases() == (1ULL << NumUnknownBits
)) {
5623 createUnreachableSwitchDefault(SI
, DTU
);
5627 if (DeadCases
.empty())
5630 SwitchInstProfUpdateWrapper
SIW(*SI
);
5631 for (ConstantInt
*DeadCase
: DeadCases
) {
5632 SwitchInst::CaseIt CaseI
= SI
->findCaseValue(DeadCase
);
5633 assert(CaseI
!= SI
->case_default() &&
5634 "Case was not found. Probably mistake in DeadCases forming.");
5635 // Prune unused values from PHI nodes.
5636 CaseI
->getCaseSuccessor()->removePredecessor(SI
->getParent());
5637 SIW
.removeCase(CaseI
);
5641 std::vector
<DominatorTree::UpdateType
> Updates
;
5642 for (auto *Successor
: UniqueSuccessors
)
5643 if (NumPerSuccessorCases
[Successor
] == 0)
5644 Updates
.push_back({DominatorTree::Delete
, SI
->getParent(), Successor
});
5645 DTU
->applyUpdates(Updates
);
5651 /// If BB would be eligible for simplification by
5652 /// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
5653 /// by an unconditional branch), look at the phi node for BB in the successor
5654 /// block and see if the incoming value is equal to CaseValue. If so, return
5655 /// the phi node, and set PhiIndex to BB's index in the phi node.
5656 static PHINode
*FindPHIForConditionForwarding(ConstantInt
*CaseValue
,
5657 BasicBlock
*BB
, int *PhiIndex
) {
5658 if (BB
->getFirstNonPHIOrDbg() != BB
->getTerminator())
5659 return nullptr; // BB must be empty to be a candidate for simplification.
5660 if (!BB
->getSinglePredecessor())
5661 return nullptr; // BB must be dominated by the switch.
5663 BranchInst
*Branch
= dyn_cast
<BranchInst
>(BB
->getTerminator());
5664 if (!Branch
|| !Branch
->isUnconditional())
5665 return nullptr; // Terminator must be unconditional branch.
5667 BasicBlock
*Succ
= Branch
->getSuccessor(0);
5669 for (PHINode
&PHI
: Succ
->phis()) {
5670 int Idx
= PHI
.getBasicBlockIndex(BB
);
5671 assert(Idx
>= 0 && "PHI has no entry for predecessor?");
5673 Value
*InValue
= PHI
.getIncomingValue(Idx
);
5674 if (InValue
!= CaseValue
)
5684 /// Try to forward the condition of a switch instruction to a phi node
5685 /// dominated by the switch, if that would mean that some of the destination
5686 /// blocks of the switch can be folded away. Return true if a change is made.
5687 static bool ForwardSwitchConditionToPHI(SwitchInst
*SI
) {
5688 using ForwardingNodesMap
= DenseMap
<PHINode
*, SmallVector
<int, 4>>;
5690 ForwardingNodesMap ForwardingNodes
;
5691 BasicBlock
*SwitchBlock
= SI
->getParent();
5692 bool Changed
= false;
5693 for (const auto &Case
: SI
->cases()) {
5694 ConstantInt
*CaseValue
= Case
.getCaseValue();
5695 BasicBlock
*CaseDest
= Case
.getCaseSuccessor();
5697 // Replace phi operands in successor blocks that are using the constant case
5698 // value rather than the switch condition variable:
5700 // switch i32 %x, label %default [
5701 // i32 17, label %succ
5704 // %r = phi i32 ... [ 17, %switchbb ] ...
5706 // %r = phi i32 ... [ %x, %switchbb ] ...
5708 for (PHINode
&Phi
: CaseDest
->phis()) {
5709 // This only works if there is exactly 1 incoming edge from the switch to
5710 // a phi. If there is >1, that means multiple cases of the switch map to 1
5711 // value in the phi, and that phi value is not the switch condition. Thus,
5712 // this transform would not make sense (the phi would be invalid because
5713 // a phi can't have different incoming values from the same block).
5714 int SwitchBBIdx
= Phi
.getBasicBlockIndex(SwitchBlock
);
5715 if (Phi
.getIncomingValue(SwitchBBIdx
) == CaseValue
&&
5716 count(Phi
.blocks(), SwitchBlock
) == 1) {
5717 Phi
.setIncomingValue(SwitchBBIdx
, SI
->getCondition());
5722 // Collect phi nodes that are indirectly using this switch's case constants.
5724 if (auto *Phi
= FindPHIForConditionForwarding(CaseValue
, CaseDest
, &PhiIdx
))
5725 ForwardingNodes
[Phi
].push_back(PhiIdx
);
5728 for (auto &ForwardingNode
: ForwardingNodes
) {
5729 PHINode
*Phi
= ForwardingNode
.first
;
5730 SmallVectorImpl
<int> &Indexes
= ForwardingNode
.second
;
5731 if (Indexes
.size() < 2)
5734 for (int Index
: Indexes
)
5735 Phi
->setIncomingValue(Index
, SI
->getCondition());
5742 /// Return true if the backend will be able to handle
5743 /// initializing an array of constants like C.
5744 static bool ValidLookupTableConstant(Constant
*C
, const TargetTransformInfo
&TTI
) {
5745 if (C
->isThreadDependent())
5747 if (C
->isDLLImportDependent())
5750 if (!isa
<ConstantFP
>(C
) && !isa
<ConstantInt
>(C
) &&
5751 !isa
<ConstantPointerNull
>(C
) && !isa
<GlobalValue
>(C
) &&
5752 !isa
<UndefValue
>(C
) && !isa
<ConstantExpr
>(C
))
5755 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
5756 // Pointer casts and in-bounds GEPs will not prohibit the backend from
5757 // materializing the array of constants.
5758 Constant
*StrippedC
= cast
<Constant
>(CE
->stripInBoundsConstantOffsets());
5759 if (StrippedC
== C
|| !ValidLookupTableConstant(StrippedC
, TTI
))
5763 if (!TTI
.shouldBuildLookupTablesForConstant(C
))
5769 /// If V is a Constant, return it. Otherwise, try to look up
5770 /// its constant value in ConstantPool, returning 0 if it's not there.
5772 LookupConstant(Value
*V
,
5773 const SmallDenseMap
<Value
*, Constant
*> &ConstantPool
) {
5774 if (Constant
*C
= dyn_cast
<Constant
>(V
))
5776 return ConstantPool
.lookup(V
);
5779 /// Try to fold instruction I into a constant. This works for
5780 /// simple instructions such as binary operations where both operands are
5781 /// constant or can be replaced by constants from the ConstantPool. Returns the
5782 /// resulting constant on success, 0 otherwise.
5784 ConstantFold(Instruction
*I
, const DataLayout
&DL
,
5785 const SmallDenseMap
<Value
*, Constant
*> &ConstantPool
) {
5786 if (SelectInst
*Select
= dyn_cast
<SelectInst
>(I
)) {
5787 Constant
*A
= LookupConstant(Select
->getCondition(), ConstantPool
);
5790 if (A
->isAllOnesValue())
5791 return LookupConstant(Select
->getTrueValue(), ConstantPool
);
5792 if (A
->isNullValue())
5793 return LookupConstant(Select
->getFalseValue(), ConstantPool
);
5797 SmallVector
<Constant
*, 4> COps
;
5798 for (unsigned N
= 0, E
= I
->getNumOperands(); N
!= E
; ++N
) {
5799 if (Constant
*A
= LookupConstant(I
->getOperand(N
), ConstantPool
))
5805 return ConstantFoldInstOperands(I
, COps
, DL
);
5808 /// Try to determine the resulting constant values in phi nodes
5809 /// at the common destination basic block, *CommonDest, for one of the case
5810 /// destionations CaseDest corresponding to value CaseVal (0 for the default
5811 /// case), of a switch instruction SI.
5813 getCaseResults(SwitchInst
*SI
, ConstantInt
*CaseVal
, BasicBlock
*CaseDest
,
5814 BasicBlock
**CommonDest
,
5815 SmallVectorImpl
<std::pair
<PHINode
*, Constant
*>> &Res
,
5816 const DataLayout
&DL
, const TargetTransformInfo
&TTI
) {
5817 // The block from which we enter the common destination.
5818 BasicBlock
*Pred
= SI
->getParent();
5820 // If CaseDest is empty except for some side-effect free instructions through
5821 // which we can constant-propagate the CaseVal, continue to its successor.
5822 SmallDenseMap
<Value
*, Constant
*> ConstantPool
;
5823 ConstantPool
.insert(std::make_pair(SI
->getCondition(), CaseVal
));
5824 for (Instruction
&I
: CaseDest
->instructionsWithoutDebug(false)) {
5825 if (I
.isTerminator()) {
5826 // If the terminator is a simple branch, continue to the next block.
5827 if (I
.getNumSuccessors() != 1 || I
.isSpecialTerminator())
5830 CaseDest
= I
.getSuccessor(0);
5831 } else if (Constant
*C
= ConstantFold(&I
, DL
, ConstantPool
)) {
5832 // Instruction is side-effect free and constant.
5834 // If the instruction has uses outside this block or a phi node slot for
5835 // the block, it is not safe to bypass the instruction since it would then
5836 // no longer dominate all its uses.
5837 for (auto &Use
: I
.uses()) {
5838 User
*User
= Use
.getUser();
5839 if (Instruction
*I
= dyn_cast
<Instruction
>(User
))
5840 if (I
->getParent() == CaseDest
)
5842 if (PHINode
*Phi
= dyn_cast
<PHINode
>(User
))
5843 if (Phi
->getIncomingBlock(Use
) == CaseDest
)
5848 ConstantPool
.insert(std::make_pair(&I
, C
));
5854 // If we did not have a CommonDest before, use the current one.
5856 *CommonDest
= CaseDest
;
5857 // If the destination isn't the common one, abort.
5858 if (CaseDest
!= *CommonDest
)
5861 // Get the values for this case from phi nodes in the destination block.
5862 for (PHINode
&PHI
: (*CommonDest
)->phis()) {
5863 int Idx
= PHI
.getBasicBlockIndex(Pred
);
5867 Constant
*ConstVal
=
5868 LookupConstant(PHI
.getIncomingValue(Idx
), ConstantPool
);
5872 // Be conservative about which kinds of constants we support.
5873 if (!ValidLookupTableConstant(ConstVal
, TTI
))
5876 Res
.push_back(std::make_pair(&PHI
, ConstVal
));
5879 return Res
.size() > 0;
5882 // Helper function used to add CaseVal to the list of cases that generate
5883 // Result. Returns the updated number of cases that generate this result.
5884 static size_t mapCaseToResult(ConstantInt
*CaseVal
,
5885 SwitchCaseResultVectorTy
&UniqueResults
,
5887 for (auto &I
: UniqueResults
) {
5888 if (I
.first
== Result
) {
5889 I
.second
.push_back(CaseVal
);
5890 return I
.second
.size();
5893 UniqueResults
.push_back(
5894 std::make_pair(Result
, SmallVector
<ConstantInt
*, 4>(1, CaseVal
)));
5898 // Helper function that initializes a map containing
5899 // results for the PHI node of the common destination block for a switch
5900 // instruction. Returns false if multiple PHI nodes have been found or if
5901 // there is not a common destination block for the switch.
5902 static bool initializeUniqueCases(SwitchInst
*SI
, PHINode
*&PHI
,
5903 BasicBlock
*&CommonDest
,
5904 SwitchCaseResultVectorTy
&UniqueResults
,
5905 Constant
*&DefaultResult
,
5906 const DataLayout
&DL
,
5907 const TargetTransformInfo
&TTI
,
5908 uintptr_t MaxUniqueResults
) {
5909 for (const auto &I
: SI
->cases()) {
5910 ConstantInt
*CaseVal
= I
.getCaseValue();
5912 // Resulting value at phi nodes for this case value.
5913 SwitchCaseResultsTy Results
;
5914 if (!getCaseResults(SI
, CaseVal
, I
.getCaseSuccessor(), &CommonDest
, Results
,
5918 // Only one value per case is permitted.
5919 if (Results
.size() > 1)
5922 // Add the case->result mapping to UniqueResults.
5923 const size_t NumCasesForResult
=
5924 mapCaseToResult(CaseVal
, UniqueResults
, Results
.begin()->second
);
5926 // Early out if there are too many cases for this result.
5927 if (NumCasesForResult
> MaxSwitchCasesPerResult
)
5930 // Early out if there are too many unique results.
5931 if (UniqueResults
.size() > MaxUniqueResults
)
5934 // Check the PHI consistency.
5936 PHI
= Results
[0].first
;
5937 else if (PHI
!= Results
[0].first
)
5940 // Find the default result value.
5941 SmallVector
<std::pair
<PHINode
*, Constant
*>, 1> DefaultResults
;
5942 BasicBlock
*DefaultDest
= SI
->getDefaultDest();
5943 getCaseResults(SI
, nullptr, SI
->getDefaultDest(), &CommonDest
, DefaultResults
,
5945 // If the default value is not found abort unless the default destination
5948 DefaultResults
.size() == 1 ? DefaultResults
.begin()->second
: nullptr;
5949 if ((!DefaultResult
&&
5950 !isa
<UnreachableInst
>(DefaultDest
->getFirstNonPHIOrDbg())))
5956 // Helper function that checks if it is possible to transform a switch with only
5957 // two cases (or two cases + default) that produces a result into a select.
5958 // TODO: Handle switches with more than 2 cases that map to the same result.
5959 static Value
*foldSwitchToSelect(const SwitchCaseResultVectorTy
&ResultVector
,
5960 Constant
*DefaultResult
, Value
*Condition
,
5961 IRBuilder
<> &Builder
) {
5962 // If we are selecting between only two cases transform into a simple
5963 // select or a two-way select if default is possible.
5965 // switch (a) { %0 = icmp eq i32 %a, 10
5966 // case 10: return 42; %1 = select i1 %0, i32 42, i32 4
5967 // case 20: return 2; ----> %2 = icmp eq i32 %a, 20
5968 // default: return 4; %3 = select i1 %2, i32 2, i32 %1
5970 if (ResultVector
.size() == 2 && ResultVector
[0].second
.size() == 1 &&
5971 ResultVector
[1].second
.size() == 1) {
5972 ConstantInt
*FirstCase
= ResultVector
[0].second
[0];
5973 ConstantInt
*SecondCase
= ResultVector
[1].second
[0];
5974 Value
*SelectValue
= ResultVector
[1].first
;
5975 if (DefaultResult
) {
5976 Value
*ValueCompare
=
5977 Builder
.CreateICmpEQ(Condition
, SecondCase
, "switch.selectcmp");
5978 SelectValue
= Builder
.CreateSelect(ValueCompare
, ResultVector
[1].first
,
5979 DefaultResult
, "switch.select");
5981 Value
*ValueCompare
=
5982 Builder
.CreateICmpEQ(Condition
, FirstCase
, "switch.selectcmp");
5983 return Builder
.CreateSelect(ValueCompare
, ResultVector
[0].first
,
5984 SelectValue
, "switch.select");
5987 // Handle the degenerate case where two cases have the same result value.
5988 if (ResultVector
.size() == 1 && DefaultResult
) {
5989 ArrayRef
<ConstantInt
*> CaseValues
= ResultVector
[0].second
;
5990 unsigned CaseCount
= CaseValues
.size();
5991 // n bits group cases map to the same result:
5992 // case 0,4 -> Cond & 0b1..1011 == 0 ? result : default
5993 // case 0,2,4,6 -> Cond & 0b1..1001 == 0 ? result : default
5994 // case 0,2,8,10 -> Cond & 0b1..0101 == 0 ? result : default
5995 if (isPowerOf2_32(CaseCount
)) {
5996 ConstantInt
*MinCaseVal
= CaseValues
[0];
5997 // Find mininal value.
5998 for (auto *Case
: CaseValues
)
5999 if (Case
->getValue().slt(MinCaseVal
->getValue()))
6002 // Mark the bits case number touched.
6003 APInt BitMask
= APInt::getZero(MinCaseVal
->getBitWidth());
6004 for (auto *Case
: CaseValues
)
6005 BitMask
|= (Case
->getValue() - MinCaseVal
->getValue());
6007 // Check if cases with the same result can cover all number
6009 if (BitMask
.popcount() == Log2_32(CaseCount
)) {
6010 if (!MinCaseVal
->isNullValue())
6011 Condition
= Builder
.CreateSub(Condition
, MinCaseVal
);
6012 Value
*And
= Builder
.CreateAnd(Condition
, ~BitMask
, "switch.and");
6013 Value
*Cmp
= Builder
.CreateICmpEQ(
6014 And
, Constant::getNullValue(And
->getType()), "switch.selectcmp");
6015 return Builder
.CreateSelect(Cmp
, ResultVector
[0].first
, DefaultResult
);
6019 // Handle the degenerate case where two cases have the same value.
6020 if (CaseValues
.size() == 2) {
6021 Value
*Cmp1
= Builder
.CreateICmpEQ(Condition
, CaseValues
[0],
6022 "switch.selectcmp.case1");
6023 Value
*Cmp2
= Builder
.CreateICmpEQ(Condition
, CaseValues
[1],
6024 "switch.selectcmp.case2");
6025 Value
*Cmp
= Builder
.CreateOr(Cmp1
, Cmp2
, "switch.selectcmp");
6026 return Builder
.CreateSelect(Cmp
, ResultVector
[0].first
, DefaultResult
);
6033 // Helper function to cleanup a switch instruction that has been converted into
6034 // a select, fixing up PHI nodes and basic blocks.
6035 static void removeSwitchAfterSelectFold(SwitchInst
*SI
, PHINode
*PHI
,
6037 IRBuilder
<> &Builder
,
6038 DomTreeUpdater
*DTU
) {
6039 std::vector
<DominatorTree::UpdateType
> Updates
;
6041 BasicBlock
*SelectBB
= SI
->getParent();
6042 BasicBlock
*DestBB
= PHI
->getParent();
6044 if (DTU
&& !is_contained(predecessors(DestBB
), SelectBB
))
6045 Updates
.push_back({DominatorTree::Insert
, SelectBB
, DestBB
});
6046 Builder
.CreateBr(DestBB
);
6048 // Remove the switch.
6050 PHI
->removeIncomingValueIf(
6051 [&](unsigned Idx
) { return PHI
->getIncomingBlock(Idx
) == SelectBB
; });
6052 PHI
->addIncoming(SelectValue
, SelectBB
);
6054 SmallPtrSet
<BasicBlock
*, 4> RemovedSuccessors
;
6055 for (unsigned i
= 0, e
= SI
->getNumSuccessors(); i
< e
; ++i
) {
6056 BasicBlock
*Succ
= SI
->getSuccessor(i
);
6060 Succ
->removePredecessor(SelectBB
);
6061 if (DTU
&& RemovedSuccessors
.insert(Succ
).second
)
6062 Updates
.push_back({DominatorTree::Delete
, SelectBB
, Succ
});
6064 SI
->eraseFromParent();
6066 DTU
->applyUpdates(Updates
);
6069 /// If a switch is only used to initialize one or more phi nodes in a common
6070 /// successor block with only two different constant values, try to replace the
6071 /// switch with a select. Returns true if the fold was made.
6072 static bool trySwitchToSelect(SwitchInst
*SI
, IRBuilder
<> &Builder
,
6073 DomTreeUpdater
*DTU
, const DataLayout
&DL
,
6074 const TargetTransformInfo
&TTI
) {
6075 Value
*const Cond
= SI
->getCondition();
6076 PHINode
*PHI
= nullptr;
6077 BasicBlock
*CommonDest
= nullptr;
6078 Constant
*DefaultResult
;
6079 SwitchCaseResultVectorTy UniqueResults
;
6080 // Collect all the cases that will deliver the same value from the switch.
6081 if (!initializeUniqueCases(SI
, PHI
, CommonDest
, UniqueResults
, DefaultResult
,
6082 DL
, TTI
, /*MaxUniqueResults*/ 2))
6085 assert(PHI
!= nullptr && "PHI for value select not found");
6086 Builder
.SetInsertPoint(SI
);
6087 Value
*SelectValue
=
6088 foldSwitchToSelect(UniqueResults
, DefaultResult
, Cond
, Builder
);
6092 removeSwitchAfterSelectFold(SI
, PHI
, SelectValue
, Builder
, DTU
);
6098 /// This class represents a lookup table that can be used to replace a switch.
6099 class SwitchLookupTable
{
6101 /// Create a lookup table to use as a switch replacement with the contents
6102 /// of Values, using DefaultValue to fill any holes in the table.
6104 Module
&M
, uint64_t TableSize
, ConstantInt
*Offset
,
6105 const SmallVectorImpl
<std::pair
<ConstantInt
*, Constant
*>> &Values
,
6106 Constant
*DefaultValue
, const DataLayout
&DL
, const StringRef
&FuncName
);
6108 /// Build instructions with Builder to retrieve the value at
6109 /// the position given by Index in the lookup table.
6110 Value
*BuildLookup(Value
*Index
, IRBuilder
<> &Builder
);
6112 /// Return true if a table with TableSize elements of
6113 /// type ElementType would fit in a target-legal register.
6114 static bool WouldFitInRegister(const DataLayout
&DL
, uint64_t TableSize
,
6118 // Depending on the contents of the table, it can be represented in
6121 // For tables where each element contains the same value, we just have to
6122 // store that single value and return it for each lookup.
6125 // For tables where there is a linear relationship between table index
6126 // and values. We calculate the result with a simple multiplication
6127 // and addition instead of a table lookup.
6130 // For small tables with integer elements, we can pack them into a bitmap
6131 // that fits into a target-legal register. Values are retrieved by
6132 // shift and mask operations.
6135 // The table is stored as an array of values. Values are retrieved by load
6136 // instructions from the table.
6140 // For SingleValueKind, this is the single value.
6141 Constant
*SingleValue
= nullptr;
6143 // For BitMapKind, this is the bitmap.
6144 ConstantInt
*BitMap
= nullptr;
6145 IntegerType
*BitMapElementTy
= nullptr;
6147 // For LinearMapKind, these are the constants used to derive the value.
6148 ConstantInt
*LinearOffset
= nullptr;
6149 ConstantInt
*LinearMultiplier
= nullptr;
6150 bool LinearMapValWrapped
= false;
6152 // For ArrayKind, this is the array.
6153 GlobalVariable
*Array
= nullptr;
6156 } // end anonymous namespace
6158 SwitchLookupTable::SwitchLookupTable(
6159 Module
&M
, uint64_t TableSize
, ConstantInt
*Offset
,
6160 const SmallVectorImpl
<std::pair
<ConstantInt
*, Constant
*>> &Values
,
6161 Constant
*DefaultValue
, const DataLayout
&DL
, const StringRef
&FuncName
) {
6162 assert(Values
.size() && "Can't build lookup table without values!");
6163 assert(TableSize
>= Values
.size() && "Can't fit values in table!");
6165 // If all values in the table are equal, this is that value.
6166 SingleValue
= Values
.begin()->second
;
6168 Type
*ValueType
= Values
.begin()->second
->getType();
6170 // Build up the table contents.
6171 SmallVector
<Constant
*, 64> TableContents(TableSize
);
6172 for (size_t I
= 0, E
= Values
.size(); I
!= E
; ++I
) {
6173 ConstantInt
*CaseVal
= Values
[I
].first
;
6174 Constant
*CaseRes
= Values
[I
].second
;
6175 assert(CaseRes
->getType() == ValueType
);
6177 uint64_t Idx
= (CaseVal
->getValue() - Offset
->getValue()).getLimitedValue();
6178 TableContents
[Idx
] = CaseRes
;
6180 if (CaseRes
!= SingleValue
)
6181 SingleValue
= nullptr;
6184 // Fill in any holes in the table with the default result.
6185 if (Values
.size() < TableSize
) {
6186 assert(DefaultValue
&&
6187 "Need a default value to fill the lookup table holes.");
6188 assert(DefaultValue
->getType() == ValueType
);
6189 for (uint64_t I
= 0; I
< TableSize
; ++I
) {
6190 if (!TableContents
[I
])
6191 TableContents
[I
] = DefaultValue
;
6194 if (DefaultValue
!= SingleValue
)
6195 SingleValue
= nullptr;
6198 // If each element in the table contains the same value, we only need to store
6199 // that single value.
6201 Kind
= SingleValueKind
;
6205 // Check if we can derive the value with a linear transformation from the
6207 if (isa
<IntegerType
>(ValueType
)) {
6208 bool LinearMappingPossible
= true;
6211 // When linear map is monotonic and signed overflow doesn't happen on
6212 // maximum index, we can attach nsw on Add and Mul.
6213 bool NonMonotonic
= false;
6214 assert(TableSize
>= 2 && "Should be a SingleValue table.");
6215 // Check if there is the same distance between two consecutive values.
6216 for (uint64_t I
= 0; I
< TableSize
; ++I
) {
6217 ConstantInt
*ConstVal
= dyn_cast
<ConstantInt
>(TableContents
[I
]);
6219 // This is an undef. We could deal with it, but undefs in lookup tables
6220 // are very seldom. It's probably not worth the additional complexity.
6221 LinearMappingPossible
= false;
6224 const APInt
&Val
= ConstVal
->getValue();
6226 APInt Dist
= Val
- PrevVal
;
6229 } else if (Dist
!= DistToPrev
) {
6230 LinearMappingPossible
= false;
6234 Dist
.isStrictlyPositive() ? Val
.sle(PrevVal
) : Val
.sgt(PrevVal
);
6238 if (LinearMappingPossible
) {
6239 LinearOffset
= cast
<ConstantInt
>(TableContents
[0]);
6240 LinearMultiplier
= ConstantInt::get(M
.getContext(), DistToPrev
);
6241 bool MayWrap
= false;
6242 APInt M
= LinearMultiplier
->getValue();
6243 (void)M
.smul_ov(APInt(M
.getBitWidth(), TableSize
- 1), MayWrap
);
6244 LinearMapValWrapped
= NonMonotonic
|| MayWrap
;
6245 Kind
= LinearMapKind
;
6251 // If the type is integer and the table fits in a register, build a bitmap.
6252 if (WouldFitInRegister(DL
, TableSize
, ValueType
)) {
6253 IntegerType
*IT
= cast
<IntegerType
>(ValueType
);
6254 APInt
TableInt(TableSize
* IT
->getBitWidth(), 0);
6255 for (uint64_t I
= TableSize
; I
> 0; --I
) {
6256 TableInt
<<= IT
->getBitWidth();
6257 // Insert values into the bitmap. Undef values are set to zero.
6258 if (!isa
<UndefValue
>(TableContents
[I
- 1])) {
6259 ConstantInt
*Val
= cast
<ConstantInt
>(TableContents
[I
- 1]);
6260 TableInt
|= Val
->getValue().zext(TableInt
.getBitWidth());
6263 BitMap
= ConstantInt::get(M
.getContext(), TableInt
);
6264 BitMapElementTy
= IT
;
6270 // Store the table in an array.
6271 ArrayType
*ArrayTy
= ArrayType::get(ValueType
, TableSize
);
6272 Constant
*Initializer
= ConstantArray::get(ArrayTy
, TableContents
);
6274 Array
= new GlobalVariable(M
, ArrayTy
, /*isConstant=*/true,
6275 GlobalVariable::PrivateLinkage
, Initializer
,
6276 "switch.table." + FuncName
);
6277 Array
->setUnnamedAddr(GlobalValue::UnnamedAddr::Global
);
6278 // Set the alignment to that of an array items. We will be only loading one
6280 Array
->setAlignment(DL
.getPrefTypeAlign(ValueType
));
6284 Value
*SwitchLookupTable::BuildLookup(Value
*Index
, IRBuilder
<> &Builder
) {
6286 case SingleValueKind
:
6288 case LinearMapKind
: {
6289 // Derive the result value from the input value.
6290 Value
*Result
= Builder
.CreateIntCast(Index
, LinearMultiplier
->getType(),
6291 false, "switch.idx.cast");
6292 if (!LinearMultiplier
->isOne())
6293 Result
= Builder
.CreateMul(Result
, LinearMultiplier
, "switch.idx.mult",
6294 /*HasNUW = */ false,
6295 /*HasNSW = */ !LinearMapValWrapped
);
6297 if (!LinearOffset
->isZero())
6298 Result
= Builder
.CreateAdd(Result
, LinearOffset
, "switch.offset",
6299 /*HasNUW = */ false,
6300 /*HasNSW = */ !LinearMapValWrapped
);
6304 // Type of the bitmap (e.g. i59).
6305 IntegerType
*MapTy
= BitMap
->getIntegerType();
6307 // Cast Index to the same type as the bitmap.
6308 // Note: The Index is <= the number of elements in the table, so
6309 // truncating it to the width of the bitmask is safe.
6310 Value
*ShiftAmt
= Builder
.CreateZExtOrTrunc(Index
, MapTy
, "switch.cast");
6312 // Multiply the shift amount by the element width. NUW/NSW can always be
6313 // set, because WouldFitInRegister guarantees Index * ShiftAmt is in
6314 // BitMap's bit width.
6315 ShiftAmt
= Builder
.CreateMul(
6316 ShiftAmt
, ConstantInt::get(MapTy
, BitMapElementTy
->getBitWidth()),
6317 "switch.shiftamt",/*HasNUW =*/true,/*HasNSW =*/true);
6320 Value
*DownShifted
=
6321 Builder
.CreateLShr(BitMap
, ShiftAmt
, "switch.downshift");
6323 return Builder
.CreateTrunc(DownShifted
, BitMapElementTy
, "switch.masked");
6326 // Make sure the table index will not overflow when treated as signed.
6327 IntegerType
*IT
= cast
<IntegerType
>(Index
->getType());
6328 uint64_t TableSize
=
6329 Array
->getInitializer()->getType()->getArrayNumElements();
6330 if (TableSize
> (1ULL << std::min(IT
->getBitWidth() - 1, 63u)))
6331 Index
= Builder
.CreateZExt(
6332 Index
, IntegerType::get(IT
->getContext(), IT
->getBitWidth() + 1),
6333 "switch.tableidx.zext");
6335 Value
*GEPIndices
[] = {Builder
.getInt32(0), Index
};
6336 Value
*GEP
= Builder
.CreateInBoundsGEP(Array
->getValueType(), Array
,
6337 GEPIndices
, "switch.gep");
6338 return Builder
.CreateLoad(
6339 cast
<ArrayType
>(Array
->getValueType())->getElementType(), GEP
,
6343 llvm_unreachable("Unknown lookup table kind!");
6346 bool SwitchLookupTable::WouldFitInRegister(const DataLayout
&DL
,
6348 Type
*ElementType
) {
6349 auto *IT
= dyn_cast
<IntegerType
>(ElementType
);
6352 // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
6353 // are <= 15, we could try to narrow the type.
6355 // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
6356 if (TableSize
>= UINT_MAX
/ IT
->getBitWidth())
6358 return DL
.fitsInLegalInteger(TableSize
* IT
->getBitWidth());
6361 static bool isTypeLegalForLookupTable(Type
*Ty
, const TargetTransformInfo
&TTI
,
6362 const DataLayout
&DL
) {
6363 // Allow any legal type.
6364 if (TTI
.isTypeLegal(Ty
))
6367 auto *IT
= dyn_cast
<IntegerType
>(Ty
);
6371 // Also allow power of 2 integer types that have at least 8 bits and fit in
6372 // a register. These types are common in frontend languages and targets
6373 // usually support loads of these types.
6374 // TODO: We could relax this to any integer that fits in a register and rely
6375 // on ABI alignment and padding in the table to allow the load to be widened.
6376 // Or we could widen the constants and truncate the load.
6377 unsigned BitWidth
= IT
->getBitWidth();
6378 return BitWidth
>= 8 && isPowerOf2_32(BitWidth
) &&
6379 DL
.fitsInLegalInteger(IT
->getBitWidth());
6382 static bool isSwitchDense(uint64_t NumCases
, uint64_t CaseRange
) {
6383 // 40% is the default density for building a jump table in optsize/minsize
6384 // mode. See also TargetLoweringBase::isSuitableForJumpTable(), which this
6385 // function was based on.
6386 const uint64_t MinDensity
= 40;
6388 if (CaseRange
>= UINT64_MAX
/ 100)
6389 return false; // Avoid multiplication overflows below.
6391 return NumCases
* 100 >= CaseRange
* MinDensity
;
6394 static bool isSwitchDense(ArrayRef
<int64_t> Values
) {
6395 uint64_t Diff
= (uint64_t)Values
.back() - (uint64_t)Values
.front();
6396 uint64_t Range
= Diff
+ 1;
6398 return false; // Overflow.
6400 return isSwitchDense(Values
.size(), Range
);
6403 /// Determine whether a lookup table should be built for this switch, based on
6404 /// the number of cases, size of the table, and the types of the results.
6405 // TODO: We could support larger than legal types by limiting based on the
6406 // number of loads required and/or table size. If the constants are small we
6407 // could use smaller table entries and extend after the load.
6409 ShouldBuildLookupTable(SwitchInst
*SI
, uint64_t TableSize
,
6410 const TargetTransformInfo
&TTI
, const DataLayout
&DL
,
6411 const SmallDenseMap
<PHINode
*, Type
*> &ResultTypes
) {
6412 if (SI
->getNumCases() > TableSize
)
6413 return false; // TableSize overflowed.
6415 bool AllTablesFitInRegister
= true;
6416 bool HasIllegalType
= false;
6417 for (const auto &I
: ResultTypes
) {
6418 Type
*Ty
= I
.second
;
6420 // Saturate this flag to true.
6421 HasIllegalType
= HasIllegalType
|| !isTypeLegalForLookupTable(Ty
, TTI
, DL
);
6423 // Saturate this flag to false.
6424 AllTablesFitInRegister
=
6425 AllTablesFitInRegister
&&
6426 SwitchLookupTable::WouldFitInRegister(DL
, TableSize
, Ty
);
6428 // If both flags saturate, we're done. NOTE: This *only* works with
6429 // saturating flags, and all flags have to saturate first due to the
6430 // non-deterministic behavior of iterating over a dense map.
6431 if (HasIllegalType
&& !AllTablesFitInRegister
)
6435 // If each table would fit in a register, we should build it anyway.
6436 if (AllTablesFitInRegister
)
6439 // Don't build a table that doesn't fit in-register if it has illegal types.
6443 return isSwitchDense(SI
->getNumCases(), TableSize
);
6446 static bool ShouldUseSwitchConditionAsTableIndex(
6447 ConstantInt
&MinCaseVal
, const ConstantInt
&MaxCaseVal
,
6448 bool HasDefaultResults
, const SmallDenseMap
<PHINode
*, Type
*> &ResultTypes
,
6449 const DataLayout
&DL
, const TargetTransformInfo
&TTI
) {
6450 if (MinCaseVal
.isNullValue())
6452 if (MinCaseVal
.isNegative() ||
6453 MaxCaseVal
.getLimitedValue() == std::numeric_limits
<uint64_t>::max() ||
6456 return all_of(ResultTypes
, [&](const auto &KV
) {
6457 return SwitchLookupTable::WouldFitInRegister(
6458 DL
, MaxCaseVal
.getLimitedValue() + 1 /* TableSize */,
6459 KV
.second
/* ResultType */);
6463 /// Try to reuse the switch table index compare. Following pattern:
6465 /// if (idx < tablesize)
6466 /// r = table[idx]; // table does not contain default_value
6468 /// r = default_value;
6469 /// if (r != default_value)
6472 /// Is optimized to:
6474 /// cond = idx < tablesize;
6478 /// r = default_value;
6482 /// Jump threading will then eliminate the second if(cond).
6483 static void reuseTableCompare(
6484 User
*PhiUser
, BasicBlock
*PhiBlock
, BranchInst
*RangeCheckBranch
,
6485 Constant
*DefaultValue
,
6486 const SmallVectorImpl
<std::pair
<ConstantInt
*, Constant
*>> &Values
) {
6487 ICmpInst
*CmpInst
= dyn_cast
<ICmpInst
>(PhiUser
);
6491 // We require that the compare is in the same block as the phi so that jump
6492 // threading can do its work afterwards.
6493 if (CmpInst
->getParent() != PhiBlock
)
6496 Constant
*CmpOp1
= dyn_cast
<Constant
>(CmpInst
->getOperand(1));
6500 Value
*RangeCmp
= RangeCheckBranch
->getCondition();
6501 Constant
*TrueConst
= ConstantInt::getTrue(RangeCmp
->getType());
6502 Constant
*FalseConst
= ConstantInt::getFalse(RangeCmp
->getType());
6504 // Check if the compare with the default value is constant true or false.
6505 Constant
*DefaultConst
= ConstantExpr::getICmp(CmpInst
->getPredicate(),
6506 DefaultValue
, CmpOp1
, true);
6507 if (DefaultConst
!= TrueConst
&& DefaultConst
!= FalseConst
)
6510 // Check if the compare with the case values is distinct from the default
6512 for (auto ValuePair
: Values
) {
6513 Constant
*CaseConst
= ConstantExpr::getICmp(CmpInst
->getPredicate(),
6514 ValuePair
.second
, CmpOp1
, true);
6515 if (!CaseConst
|| CaseConst
== DefaultConst
||
6516 (CaseConst
!= TrueConst
&& CaseConst
!= FalseConst
))
6520 // Check if the branch instruction dominates the phi node. It's a simple
6521 // dominance check, but sufficient for our needs.
6522 // Although this check is invariant in the calling loops, it's better to do it
6523 // at this late stage. Practically we do it at most once for a switch.
6524 BasicBlock
*BranchBlock
= RangeCheckBranch
->getParent();
6525 for (BasicBlock
*Pred
: predecessors(PhiBlock
)) {
6526 if (Pred
!= BranchBlock
&& Pred
->getUniquePredecessor() != BranchBlock
)
6530 if (DefaultConst
== FalseConst
) {
6531 // The compare yields the same result. We can replace it.
6532 CmpInst
->replaceAllUsesWith(RangeCmp
);
6533 ++NumTableCmpReuses
;
6535 // The compare yields the same result, just inverted. We can replace it.
6536 Value
*InvertedTableCmp
= BinaryOperator::CreateXor(
6537 RangeCmp
, ConstantInt::get(RangeCmp
->getType(), 1), "inverted.cmp",
6539 CmpInst
->replaceAllUsesWith(InvertedTableCmp
);
6540 ++NumTableCmpReuses
;
6544 /// If the switch is only used to initialize one or more phi nodes in a common
6545 /// successor block with different constant values, replace the switch with
6547 static bool SwitchToLookupTable(SwitchInst
*SI
, IRBuilder
<> &Builder
,
6548 DomTreeUpdater
*DTU
, const DataLayout
&DL
,
6549 const TargetTransformInfo
&TTI
) {
6550 assert(SI
->getNumCases() > 1 && "Degenerate switch?");
6552 BasicBlock
*BB
= SI
->getParent();
6553 Function
*Fn
= BB
->getParent();
6554 // Only build lookup table when we have a target that supports it or the
6555 // attribute is not set.
6556 if (!TTI
.shouldBuildLookupTables() ||
6557 (Fn
->getFnAttribute("no-jump-tables").getValueAsBool()))
6560 // FIXME: If the switch is too sparse for a lookup table, perhaps we could
6561 // split off a dense part and build a lookup table for that.
6563 // FIXME: This creates arrays of GEPs to constant strings, which means each
6564 // GEP needs a runtime relocation in PIC code. We should just build one big
6565 // string and lookup indices into that.
6567 // Ignore switches with less than three cases. Lookup tables will not make
6568 // them faster, so we don't analyze them.
6569 if (SI
->getNumCases() < 3)
6572 // Figure out the corresponding result for each case value and phi node in the
6573 // common destination, as well as the min and max case values.
6574 assert(!SI
->cases().empty());
6575 SwitchInst::CaseIt CI
= SI
->case_begin();
6576 ConstantInt
*MinCaseVal
= CI
->getCaseValue();
6577 ConstantInt
*MaxCaseVal
= CI
->getCaseValue();
6579 BasicBlock
*CommonDest
= nullptr;
6581 using ResultListTy
= SmallVector
<std::pair
<ConstantInt
*, Constant
*>, 4>;
6582 SmallDenseMap
<PHINode
*, ResultListTy
> ResultLists
;
6584 SmallDenseMap
<PHINode
*, Constant
*> DefaultResults
;
6585 SmallDenseMap
<PHINode
*, Type
*> ResultTypes
;
6586 SmallVector
<PHINode
*, 4> PHIs
;
6588 for (SwitchInst::CaseIt E
= SI
->case_end(); CI
!= E
; ++CI
) {
6589 ConstantInt
*CaseVal
= CI
->getCaseValue();
6590 if (CaseVal
->getValue().slt(MinCaseVal
->getValue()))
6591 MinCaseVal
= CaseVal
;
6592 if (CaseVal
->getValue().sgt(MaxCaseVal
->getValue()))
6593 MaxCaseVal
= CaseVal
;
6595 // Resulting value at phi nodes for this case value.
6596 using ResultsTy
= SmallVector
<std::pair
<PHINode
*, Constant
*>, 4>;
6598 if (!getCaseResults(SI
, CaseVal
, CI
->getCaseSuccessor(), &CommonDest
,
6602 // Append the result from this case to the list for each phi.
6603 for (const auto &I
: Results
) {
6604 PHINode
*PHI
= I
.first
;
6605 Constant
*Value
= I
.second
;
6606 if (!ResultLists
.count(PHI
))
6607 PHIs
.push_back(PHI
);
6608 ResultLists
[PHI
].push_back(std::make_pair(CaseVal
, Value
));
6612 // Keep track of the result types.
6613 for (PHINode
*PHI
: PHIs
) {
6614 ResultTypes
[PHI
] = ResultLists
[PHI
][0].second
->getType();
6617 uint64_t NumResults
= ResultLists
[PHIs
[0]].size();
6619 // If the table has holes, we need a constant result for the default case
6620 // or a bitmask that fits in a register.
6621 SmallVector
<std::pair
<PHINode
*, Constant
*>, 4> DefaultResultsList
;
6622 bool HasDefaultResults
=
6623 getCaseResults(SI
, nullptr, SI
->getDefaultDest(), &CommonDest
,
6624 DefaultResultsList
, DL
, TTI
);
6626 for (const auto &I
: DefaultResultsList
) {
6627 PHINode
*PHI
= I
.first
;
6628 Constant
*Result
= I
.second
;
6629 DefaultResults
[PHI
] = Result
;
6632 bool UseSwitchConditionAsTableIndex
= ShouldUseSwitchConditionAsTableIndex(
6633 *MinCaseVal
, *MaxCaseVal
, HasDefaultResults
, ResultTypes
, DL
, TTI
);
6635 if (UseSwitchConditionAsTableIndex
)
6636 TableSize
= MaxCaseVal
->getLimitedValue() + 1;
6639 (MaxCaseVal
->getValue() - MinCaseVal
->getValue()).getLimitedValue() + 1;
6641 bool TableHasHoles
= (NumResults
< TableSize
);
6642 bool NeedMask
= (TableHasHoles
&& !HasDefaultResults
);
6644 // As an extra penalty for the validity test we require more cases.
6645 if (SI
->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
6647 if (!DL
.fitsInLegalInteger(TableSize
))
6651 if (!ShouldBuildLookupTable(SI
, TableSize
, TTI
, DL
, ResultTypes
))
6654 std::vector
<DominatorTree::UpdateType
> Updates
;
6656 // Compute the maximum table size representable by the integer type we are
6658 unsigned CaseSize
= MinCaseVal
->getType()->getPrimitiveSizeInBits();
6659 uint64_t MaxTableSize
= CaseSize
> 63 ? UINT64_MAX
: 1ULL << CaseSize
;
6660 assert(MaxTableSize
>= TableSize
&&
6661 "It is impossible for a switch to have more entries than the max "
6662 "representable value of its input integer type's size.");
6664 // If the default destination is unreachable, or if the lookup table covers
6665 // all values of the conditional variable, branch directly to the lookup table
6666 // BB. Otherwise, check that the condition is within the case range.
6667 bool DefaultIsReachable
=
6668 !isa
<UnreachableInst
>(SI
->getDefaultDest()->getFirstNonPHIOrDbg());
6670 // Create the BB that does the lookups.
6671 Module
&Mod
= *CommonDest
->getParent()->getParent();
6672 BasicBlock
*LookupBB
= BasicBlock::Create(
6673 Mod
.getContext(), "switch.lookup", CommonDest
->getParent(), CommonDest
);
6675 // Compute the table index value.
6676 Builder
.SetInsertPoint(SI
);
6678 ConstantInt
*TableIndexOffset
;
6679 if (UseSwitchConditionAsTableIndex
) {
6680 TableIndexOffset
= ConstantInt::get(MaxCaseVal
->getIntegerType(), 0);
6681 TableIndex
= SI
->getCondition();
6683 TableIndexOffset
= MinCaseVal
;
6684 // If the default is unreachable, all case values are s>= MinCaseVal. Then
6685 // we can try to attach nsw.
6686 bool MayWrap
= true;
6687 if (!DefaultIsReachable
) {
6688 APInt Res
= MaxCaseVal
->getValue().ssub_ov(MinCaseVal
->getValue(), MayWrap
);
6692 TableIndex
= Builder
.CreateSub(SI
->getCondition(), TableIndexOffset
,
6693 "switch.tableidx", /*HasNUW =*/false,
6694 /*HasNSW =*/!MayWrap
);
6697 BranchInst
*RangeCheckBranch
= nullptr;
6699 // Grow the table to cover all possible index values to avoid the range check.
6700 // It will use the default result to fill in the table hole later, so make
6702 if (UseSwitchConditionAsTableIndex
&& HasDefaultResults
) {
6703 ConstantRange CR
= computeConstantRange(TableIndex
, /* ForSigned */ false);
6704 // Grow the table shouldn't have any size impact by checking
6705 // WouldFitInRegister.
6706 // TODO: Consider growing the table also when it doesn't fit in a register
6707 // if no optsize is specified.
6708 const uint64_t UpperBound
= CR
.getUpper().getLimitedValue();
6709 if (!CR
.isUpperWrapped() && all_of(ResultTypes
, [&](const auto &KV
) {
6710 return SwitchLookupTable::WouldFitInRegister(
6711 DL
, UpperBound
, KV
.second
/* ResultType */);
6713 // The default branch is unreachable after we enlarge the lookup table.
6714 // Adjust DefaultIsReachable to reuse code path.
6715 TableSize
= UpperBound
;
6716 DefaultIsReachable
= false;
6720 const bool GeneratingCoveredLookupTable
= (MaxTableSize
== TableSize
);
6721 if (!DefaultIsReachable
|| GeneratingCoveredLookupTable
) {
6722 Builder
.CreateBr(LookupBB
);
6724 Updates
.push_back({DominatorTree::Insert
, BB
, LookupBB
});
6725 // Note: We call removeProdecessor later since we need to be able to get the
6726 // PHI value for the default case in case we're using a bit mask.
6728 Value
*Cmp
= Builder
.CreateICmpULT(
6729 TableIndex
, ConstantInt::get(MinCaseVal
->getType(), TableSize
));
6731 Builder
.CreateCondBr(Cmp
, LookupBB
, SI
->getDefaultDest());
6733 Updates
.push_back({DominatorTree::Insert
, BB
, LookupBB
});
6736 // Populate the BB that does the lookups.
6737 Builder
.SetInsertPoint(LookupBB
);
6740 // Before doing the lookup, we do the hole check. The LookupBB is therefore
6741 // re-purposed to do the hole check, and we create a new LookupBB.
6742 BasicBlock
*MaskBB
= LookupBB
;
6743 MaskBB
->setName("switch.hole_check");
6744 LookupBB
= BasicBlock::Create(Mod
.getContext(), "switch.lookup",
6745 CommonDest
->getParent(), CommonDest
);
6747 // Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
6748 // unnecessary illegal types.
6749 uint64_t TableSizePowOf2
= NextPowerOf2(std::max(7ULL, TableSize
- 1ULL));
6750 APInt
MaskInt(TableSizePowOf2
, 0);
6751 APInt
One(TableSizePowOf2
, 1);
6752 // Build bitmask; fill in a 1 bit for every case.
6753 const ResultListTy
&ResultList
= ResultLists
[PHIs
[0]];
6754 for (size_t I
= 0, E
= ResultList
.size(); I
!= E
; ++I
) {
6755 uint64_t Idx
= (ResultList
[I
].first
->getValue() - TableIndexOffset
->getValue())
6757 MaskInt
|= One
<< Idx
;
6759 ConstantInt
*TableMask
= ConstantInt::get(Mod
.getContext(), MaskInt
);
6761 // Get the TableIndex'th bit of the bitmask.
6762 // If this bit is 0 (meaning hole) jump to the default destination,
6763 // else continue with table lookup.
6764 IntegerType
*MapTy
= TableMask
->getIntegerType();
6766 Builder
.CreateZExtOrTrunc(TableIndex
, MapTy
, "switch.maskindex");
6767 Value
*Shifted
= Builder
.CreateLShr(TableMask
, MaskIndex
, "switch.shifted");
6768 Value
*LoBit
= Builder
.CreateTrunc(
6769 Shifted
, Type::getInt1Ty(Mod
.getContext()), "switch.lobit");
6770 Builder
.CreateCondBr(LoBit
, LookupBB
, SI
->getDefaultDest());
6772 Updates
.push_back({DominatorTree::Insert
, MaskBB
, LookupBB
});
6773 Updates
.push_back({DominatorTree::Insert
, MaskBB
, SI
->getDefaultDest()});
6775 Builder
.SetInsertPoint(LookupBB
);
6776 AddPredecessorToBlock(SI
->getDefaultDest(), MaskBB
, BB
);
6779 if (!DefaultIsReachable
|| GeneratingCoveredLookupTable
) {
6780 // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
6781 // do not delete PHINodes here.
6782 SI
->getDefaultDest()->removePredecessor(BB
,
6783 /*KeepOneInputPHIs=*/true);
6785 Updates
.push_back({DominatorTree::Delete
, BB
, SI
->getDefaultDest()});
6788 for (PHINode
*PHI
: PHIs
) {
6789 const ResultListTy
&ResultList
= ResultLists
[PHI
];
6791 // If using a bitmask, use any value to fill the lookup table holes.
6792 Constant
*DV
= NeedMask
? ResultLists
[PHI
][0].second
: DefaultResults
[PHI
];
6793 StringRef FuncName
= Fn
->getName();
6794 SwitchLookupTable
Table(Mod
, TableSize
, TableIndexOffset
, ResultList
, DV
,
6797 Value
*Result
= Table
.BuildLookup(TableIndex
, Builder
);
6799 // Do a small peephole optimization: re-use the switch table compare if
6801 if (!TableHasHoles
&& HasDefaultResults
&& RangeCheckBranch
) {
6802 BasicBlock
*PhiBlock
= PHI
->getParent();
6803 // Search for compare instructions which use the phi.
6804 for (auto *User
: PHI
->users()) {
6805 reuseTableCompare(User
, PhiBlock
, RangeCheckBranch
, DV
, ResultList
);
6809 PHI
->addIncoming(Result
, LookupBB
);
6812 Builder
.CreateBr(CommonDest
);
6814 Updates
.push_back({DominatorTree::Insert
, LookupBB
, CommonDest
});
6816 // Remove the switch.
6817 SmallPtrSet
<BasicBlock
*, 8> RemovedSuccessors
;
6818 for (unsigned i
= 0, e
= SI
->getNumSuccessors(); i
< e
; ++i
) {
6819 BasicBlock
*Succ
= SI
->getSuccessor(i
);
6821 if (Succ
== SI
->getDefaultDest())
6823 Succ
->removePredecessor(BB
);
6824 if (DTU
&& RemovedSuccessors
.insert(Succ
).second
)
6825 Updates
.push_back({DominatorTree::Delete
, BB
, Succ
});
6827 SI
->eraseFromParent();
6830 DTU
->applyUpdates(Updates
);
6834 ++NumLookupTablesHoles
;
6838 /// Try to transform a switch that has "holes" in it to a contiguous sequence
6841 /// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
6842 /// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
6844 /// This converts a sparse switch into a dense switch which allows better
6845 /// lowering and could also allow transforming into a lookup table.
6846 static bool ReduceSwitchRange(SwitchInst
*SI
, IRBuilder
<> &Builder
,
6847 const DataLayout
&DL
,
6848 const TargetTransformInfo
&TTI
) {
6849 auto *CondTy
= cast
<IntegerType
>(SI
->getCondition()->getType());
6850 if (CondTy
->getIntegerBitWidth() > 64 ||
6851 !DL
.fitsInLegalInteger(CondTy
->getIntegerBitWidth()))
6853 // Only bother with this optimization if there are more than 3 switch cases;
6854 // SDAG will only bother creating jump tables for 4 or more cases.
6855 if (SI
->getNumCases() < 4)
6858 // This transform is agnostic to the signedness of the input or case values. We
6859 // can treat the case values as signed or unsigned. We can optimize more common
6860 // cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
6862 SmallVector
<int64_t,4> Values
;
6863 for (const auto &C
: SI
->cases())
6864 Values
.push_back(C
.getCaseValue()->getValue().getSExtValue());
6867 // If the switch is already dense, there's nothing useful to do here.
6868 if (isSwitchDense(Values
))
6871 // First, transform the values such that they start at zero and ascend.
6872 int64_t Base
= Values
[0];
6873 for (auto &V
: Values
)
6874 V
-= (uint64_t)(Base
);
6876 // Now we have signed numbers that have been shifted so that, given enough
6877 // precision, there are no negative values. Since the rest of the transform
6878 // is bitwise only, we switch now to an unsigned representation.
6880 // This transform can be done speculatively because it is so cheap - it
6881 // results in a single rotate operation being inserted.
6883 // countTrailingZeros(0) returns 64. As Values is guaranteed to have more than
6884 // one element and LLVM disallows duplicate cases, Shift is guaranteed to be
6886 unsigned Shift
= 64;
6887 for (auto &V
: Values
)
6888 Shift
= std::min(Shift
, (unsigned)llvm::countr_zero((uint64_t)V
));
6891 for (auto &V
: Values
)
6892 V
= (int64_t)((uint64_t)V
>> Shift
);
6894 if (!isSwitchDense(Values
))
6895 // Transform didn't create a dense switch.
6898 // The obvious transform is to shift the switch condition right and emit a
6899 // check that the condition actually cleanly divided by GCD, i.e.
6900 // C & (1 << Shift - 1) == 0
6901 // inserting a new CFG edge to handle the case where it didn't divide cleanly.
6903 // A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
6904 // shift and puts the shifted-off bits in the uppermost bits. If any of these
6905 // are nonzero then the switch condition will be very large and will hit the
6908 auto *Ty
= cast
<IntegerType
>(SI
->getCondition()->getType());
6909 Builder
.SetInsertPoint(SI
);
6911 Builder
.CreateSub(SI
->getCondition(), ConstantInt::get(Ty
, Base
));
6912 Value
*Rot
= Builder
.CreateIntrinsic(
6913 Ty
, Intrinsic::fshl
,
6914 {Sub
, Sub
, ConstantInt::get(Ty
, Ty
->getBitWidth() - Shift
)});
6915 SI
->replaceUsesOfWith(SI
->getCondition(), Rot
);
6917 for (auto Case
: SI
->cases()) {
6918 auto *Orig
= Case
.getCaseValue();
6919 auto Sub
= Orig
->getValue() - APInt(Ty
->getBitWidth(), Base
);
6920 Case
.setValue(cast
<ConstantInt
>(ConstantInt::get(Ty
, Sub
.lshr(Shift
))));
6925 /// Tries to transform switch of powers of two to reduce switch range.
6926 /// For example, switch like:
6927 /// switch (C) { case 1: case 2: case 64: case 128: }
6928 /// will be transformed to:
6929 /// switch (count_trailing_zeros(C)) { case 0: case 1: case 6: case 7: }
6931 /// This transformation allows better lowering and could allow transforming into
6933 static bool simplifySwitchOfPowersOfTwo(SwitchInst
*SI
, IRBuilder
<> &Builder
,
6934 const DataLayout
&DL
,
6935 const TargetTransformInfo
&TTI
) {
6936 Value
*Condition
= SI
->getCondition();
6937 LLVMContext
&Context
= SI
->getContext();
6938 auto *CondTy
= cast
<IntegerType
>(Condition
->getType());
6940 if (CondTy
->getIntegerBitWidth() > 64 ||
6941 !DL
.fitsInLegalInteger(CondTy
->getIntegerBitWidth()))
6944 const auto CttzIntrinsicCost
= TTI
.getIntrinsicInstrCost(
6945 IntrinsicCostAttributes(Intrinsic::cttz
, CondTy
,
6946 {Condition
, ConstantInt::getTrue(Context
)}),
6947 TTI::TCK_SizeAndLatency
);
6949 if (CttzIntrinsicCost
> TTI::TCC_Basic
)
6950 // Inserting intrinsic is too expensive.
6953 // Only bother with this optimization if there are more than 3 switch cases.
6954 // SDAG will only bother creating jump tables for 4 or more cases.
6955 if (SI
->getNumCases() < 4)
6958 // We perform this optimization only for switches with
6959 // unreachable default case.
6960 // This assumtion will save us from checking if `Condition` is a power of two.
6961 if (!isa
<UnreachableInst
>(SI
->getDefaultDest()->getFirstNonPHIOrDbg()))
6964 // Check that switch cases are powers of two.
6965 SmallVector
<uint64_t, 4> Values
;
6966 for (const auto &Case
: SI
->cases()) {
6967 uint64_t CaseValue
= Case
.getCaseValue()->getValue().getZExtValue();
6968 if (llvm::has_single_bit(CaseValue
))
6969 Values
.push_back(CaseValue
);
6974 // isSwichDense requires case values to be sorted.
6976 if (!isSwitchDense(Values
.size(), llvm::countr_zero(Values
.back()) -
6977 llvm::countr_zero(Values
.front()) + 1))
6978 // Transform is unable to generate dense switch.
6981 Builder
.SetInsertPoint(SI
);
6983 // Replace each case with its trailing zeros number.
6984 for (auto &Case
: SI
->cases()) {
6985 auto *OrigValue
= Case
.getCaseValue();
6986 Case
.setValue(ConstantInt::get(OrigValue
->getIntegerType(),
6987 OrigValue
->getValue().countr_zero()));
6990 // Replace condition with its trailing zeros number.
6991 auto *ConditionTrailingZeros
= Builder
.CreateIntrinsic(
6992 Intrinsic::cttz
, {CondTy
}, {Condition
, ConstantInt::getTrue(Context
)});
6994 SI
->setCondition(ConditionTrailingZeros
);
6999 bool SimplifyCFGOpt::simplifySwitch(SwitchInst
*SI
, IRBuilder
<> &Builder
) {
7000 BasicBlock
*BB
= SI
->getParent();
7002 if (isValueEqualityComparison(SI
)) {
7003 // If we only have one predecessor, and if it is a branch on this value,
7004 // see if that predecessor totally determines the outcome of this switch.
7005 if (BasicBlock
*OnlyPred
= BB
->getSinglePredecessor())
7006 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI
, OnlyPred
, Builder
))
7007 return requestResimplify();
7009 Value
*Cond
= SI
->getCondition();
7010 if (SelectInst
*Select
= dyn_cast
<SelectInst
>(Cond
))
7011 if (SimplifySwitchOnSelect(SI
, Select
))
7012 return requestResimplify();
7014 // If the block only contains the switch, see if we can fold the block
7015 // away into any preds.
7016 if (SI
== &*BB
->instructionsWithoutDebug(false).begin())
7017 if (FoldValueComparisonIntoPredecessors(SI
, Builder
))
7018 return requestResimplify();
7021 // Try to transform the switch into an icmp and a branch.
7022 // The conversion from switch to comparison may lose information on
7023 // impossible switch values, so disable it early in the pipeline.
7024 if (Options
.ConvertSwitchRangeToICmp
&& TurnSwitchRangeIntoICmp(SI
, Builder
))
7025 return requestResimplify();
7027 // Remove unreachable cases.
7028 if (eliminateDeadSwitchCases(SI
, DTU
, Options
.AC
, DL
))
7029 return requestResimplify();
7031 if (trySwitchToSelect(SI
, Builder
, DTU
, DL
, TTI
))
7032 return requestResimplify();
7034 if (Options
.ForwardSwitchCondToPhi
&& ForwardSwitchConditionToPHI(SI
))
7035 return requestResimplify();
7037 // The conversion from switch to lookup tables results in difficult-to-analyze
7038 // code and makes pruning branches much harder. This is a problem if the
7039 // switch expression itself can still be restricted as a result of inlining or
7040 // CVP. Therefore, only apply this transformation during late stages of the
7041 // optimisation pipeline.
7042 if (Options
.ConvertSwitchToLookupTable
&&
7043 SwitchToLookupTable(SI
, Builder
, DTU
, DL
, TTI
))
7044 return requestResimplify();
7046 if (simplifySwitchOfPowersOfTwo(SI
, Builder
, DL
, TTI
))
7047 return requestResimplify();
7049 if (ReduceSwitchRange(SI
, Builder
, DL
, TTI
))
7050 return requestResimplify();
7053 hoistCommonCodeFromSuccessors(SI
->getParent(), !Options
.HoistCommonInsts
))
7054 return requestResimplify();
7059 bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst
*IBI
) {
7060 BasicBlock
*BB
= IBI
->getParent();
7061 bool Changed
= false;
7063 // Eliminate redundant destinations.
7064 SmallPtrSet
<Value
*, 8> Succs
;
7065 SmallSetVector
<BasicBlock
*, 8> RemovedSuccs
;
7066 for (unsigned i
= 0, e
= IBI
->getNumDestinations(); i
!= e
; ++i
) {
7067 BasicBlock
*Dest
= IBI
->getDestination(i
);
7068 if (!Dest
->hasAddressTaken() || !Succs
.insert(Dest
).second
) {
7069 if (!Dest
->hasAddressTaken())
7070 RemovedSuccs
.insert(Dest
);
7071 Dest
->removePredecessor(BB
);
7072 IBI
->removeDestination(i
);
7080 std::vector
<DominatorTree::UpdateType
> Updates
;
7081 Updates
.reserve(RemovedSuccs
.size());
7082 for (auto *RemovedSucc
: RemovedSuccs
)
7083 Updates
.push_back({DominatorTree::Delete
, BB
, RemovedSucc
});
7084 DTU
->applyUpdates(Updates
);
7087 if (IBI
->getNumDestinations() == 0) {
7088 // If the indirectbr has no successors, change it to unreachable.
7089 new UnreachableInst(IBI
->getContext(), IBI
);
7090 EraseTerminatorAndDCECond(IBI
);
7094 if (IBI
->getNumDestinations() == 1) {
7095 // If the indirectbr has one successor, change it to a direct branch.
7096 BranchInst::Create(IBI
->getDestination(0), IBI
);
7097 EraseTerminatorAndDCECond(IBI
);
7101 if (SelectInst
*SI
= dyn_cast
<SelectInst
>(IBI
->getAddress())) {
7102 if (SimplifyIndirectBrOnSelect(IBI
, SI
))
7103 return requestResimplify();
7108 /// Given an block with only a single landing pad and a unconditional branch
7109 /// try to find another basic block which this one can be merged with. This
7110 /// handles cases where we have multiple invokes with unique landing pads, but
7111 /// a shared handler.
7113 /// We specifically choose to not worry about merging non-empty blocks
7114 /// here. That is a PRE/scheduling problem and is best solved elsewhere. In
7115 /// practice, the optimizer produces empty landing pad blocks quite frequently
7116 /// when dealing with exception dense code. (see: instcombine, gvn, if-else
7117 /// sinking in this file)
7119 /// This is primarily a code size optimization. We need to avoid performing
7120 /// any transform which might inhibit optimization (such as our ability to
7121 /// specialize a particular handler via tail commoning). We do this by not
7122 /// merging any blocks which require us to introduce a phi. Since the same
7123 /// values are flowing through both blocks, we don't lose any ability to
7124 /// specialize. If anything, we make such specialization more likely.
7126 /// TODO - This transformation could remove entries from a phi in the target
7127 /// block when the inputs in the phi are the same for the two blocks being
7128 /// merged. In some cases, this could result in removal of the PHI entirely.
7129 static bool TryToMergeLandingPad(LandingPadInst
*LPad
, BranchInst
*BI
,
7130 BasicBlock
*BB
, DomTreeUpdater
*DTU
) {
7131 auto Succ
= BB
->getUniqueSuccessor();
7133 // If there's a phi in the successor block, we'd likely have to introduce
7134 // a phi into the merged landing pad block.
7135 if (isa
<PHINode
>(*Succ
->begin()))
7138 for (BasicBlock
*OtherPred
: predecessors(Succ
)) {
7139 if (BB
== OtherPred
)
7141 BasicBlock::iterator I
= OtherPred
->begin();
7142 LandingPadInst
*LPad2
= dyn_cast
<LandingPadInst
>(I
);
7143 if (!LPad2
|| !LPad2
->isIdenticalTo(LPad
))
7145 for (++I
; isa
<DbgInfoIntrinsic
>(I
); ++I
)
7147 BranchInst
*BI2
= dyn_cast
<BranchInst
>(I
);
7148 if (!BI2
|| !BI2
->isIdenticalTo(BI
))
7151 std::vector
<DominatorTree::UpdateType
> Updates
;
7153 // We've found an identical block. Update our predecessors to take that
7154 // path instead and make ourselves dead.
7155 SmallSetVector
<BasicBlock
*, 16> UniquePreds(pred_begin(BB
), pred_end(BB
));
7156 for (BasicBlock
*Pred
: UniquePreds
) {
7157 InvokeInst
*II
= cast
<InvokeInst
>(Pred
->getTerminator());
7158 assert(II
->getNormalDest() != BB
&& II
->getUnwindDest() == BB
&&
7159 "unexpected successor");
7160 II
->setUnwindDest(OtherPred
);
7162 Updates
.push_back({DominatorTree::Insert
, Pred
, OtherPred
});
7163 Updates
.push_back({DominatorTree::Delete
, Pred
, BB
});
7167 // The debug info in OtherPred doesn't cover the merged control flow that
7168 // used to go through BB. We need to delete it or update it.
7169 for (Instruction
&Inst
: llvm::make_early_inc_range(*OtherPred
))
7170 if (isa
<DbgInfoIntrinsic
>(Inst
))
7171 Inst
.eraseFromParent();
7173 SmallSetVector
<BasicBlock
*, 16> UniqueSuccs(succ_begin(BB
), succ_end(BB
));
7174 for (BasicBlock
*Succ
: UniqueSuccs
) {
7175 Succ
->removePredecessor(BB
);
7177 Updates
.push_back({DominatorTree::Delete
, BB
, Succ
});
7180 IRBuilder
<> Builder(BI
);
7181 Builder
.CreateUnreachable();
7182 BI
->eraseFromParent();
7184 DTU
->applyUpdates(Updates
);
7190 bool SimplifyCFGOpt::simplifyBranch(BranchInst
*Branch
, IRBuilder
<> &Builder
) {
7191 return Branch
->isUnconditional() ? simplifyUncondBranch(Branch
, Builder
)
7192 : simplifyCondBranch(Branch
, Builder
);
7195 bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst
*BI
,
7196 IRBuilder
<> &Builder
) {
7197 BasicBlock
*BB
= BI
->getParent();
7198 BasicBlock
*Succ
= BI
->getSuccessor(0);
7200 // If the Terminator is the only non-phi instruction, simplify the block.
7201 // If LoopHeader is provided, check if the block or its successor is a loop
7202 // header. (This is for early invocations before loop simplify and
7203 // vectorization to keep canonical loop forms for nested loops. These blocks
7204 // can be eliminated when the pass is invoked later in the back-end.)
7205 // Note that if BB has only one predecessor then we do not introduce new
7206 // backedge, so we can eliminate BB.
7207 bool NeedCanonicalLoop
=
7208 Options
.NeedCanonicalLoop
&&
7209 (!LoopHeaders
.empty() && BB
->hasNPredecessorsOrMore(2) &&
7210 (is_contained(LoopHeaders
, BB
) || is_contained(LoopHeaders
, Succ
)));
7211 BasicBlock::iterator I
= BB
->getFirstNonPHIOrDbg(true)->getIterator();
7212 if (I
->isTerminator() && BB
!= &BB
->getParent()->getEntryBlock() &&
7213 !NeedCanonicalLoop
&& TryToSimplifyUncondBranchFromEmptyBlock(BB
, DTU
))
7216 // If the only instruction in the block is a seteq/setne comparison against a
7217 // constant, try to simplify the block.
7218 if (ICmpInst
*ICI
= dyn_cast
<ICmpInst
>(I
))
7219 if (ICI
->isEquality() && isa
<ConstantInt
>(ICI
->getOperand(1))) {
7220 for (++I
; isa
<DbgInfoIntrinsic
>(I
); ++I
)
7222 if (I
->isTerminator() &&
7223 tryToSimplifyUncondBranchWithICmpInIt(ICI
, Builder
))
7227 // See if we can merge an empty landing pad block with another which is
7229 if (LandingPadInst
*LPad
= dyn_cast
<LandingPadInst
>(I
)) {
7230 for (++I
; isa
<DbgInfoIntrinsic
>(I
); ++I
)
7232 if (I
->isTerminator() && TryToMergeLandingPad(LPad
, BI
, BB
, DTU
))
7236 // If this basic block is ONLY a compare and a branch, and if a predecessor
7237 // branches to us and our successor, fold the comparison into the
7238 // predecessor and use logical operations to update the incoming value
7239 // for PHI nodes in common successor.
7240 if (Options
.SpeculateBlocks
&&
7241 FoldBranchToCommonDest(BI
, DTU
, /*MSSAU=*/nullptr, &TTI
,
7242 Options
.BonusInstThreshold
))
7243 return requestResimplify();
7247 static BasicBlock
*allPredecessorsComeFromSameSource(BasicBlock
*BB
) {
7248 BasicBlock
*PredPred
= nullptr;
7249 for (auto *P
: predecessors(BB
)) {
7250 BasicBlock
*PPred
= P
->getSinglePredecessor();
7251 if (!PPred
|| (PredPred
&& PredPred
!= PPred
))
7258 bool SimplifyCFGOpt::simplifyCondBranch(BranchInst
*BI
, IRBuilder
<> &Builder
) {
7260 !isa
<ConstantInt
>(BI
->getCondition()) &&
7261 BI
->getSuccessor(0) != BI
->getSuccessor(1) &&
7262 "Tautological conditional branch should have been eliminated already.");
7264 BasicBlock
*BB
= BI
->getParent();
7265 if (!Options
.SimplifyCondBranch
||
7266 BI
->getFunction()->hasFnAttribute(Attribute::OptForFuzzing
))
7269 // Conditional branch
7270 if (isValueEqualityComparison(BI
)) {
7271 // If we only have one predecessor, and if it is a branch on this value,
7272 // see if that predecessor totally determines the outcome of this
7274 if (BasicBlock
*OnlyPred
= BB
->getSinglePredecessor())
7275 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI
, OnlyPred
, Builder
))
7276 return requestResimplify();
7278 // This block must be empty, except for the setcond inst, if it exists.
7279 // Ignore dbg and pseudo intrinsics.
7280 auto I
= BB
->instructionsWithoutDebug(true).begin();
7282 if (FoldValueComparisonIntoPredecessors(BI
, Builder
))
7283 return requestResimplify();
7284 } else if (&*I
== cast
<Instruction
>(BI
->getCondition())) {
7286 if (&*I
== BI
&& FoldValueComparisonIntoPredecessors(BI
, Builder
))
7287 return requestResimplify();
7291 // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
7292 if (SimplifyBranchOnICmpChain(BI
, Builder
, DL
))
7295 // If this basic block has dominating predecessor blocks and the dominating
7296 // blocks' conditions imply BI's condition, we know the direction of BI.
7297 std::optional
<bool> Imp
= isImpliedByDomCondition(BI
->getCondition(), BI
, DL
);
7299 // Turn this into a branch on constant.
7300 auto *OldCond
= BI
->getCondition();
7301 ConstantInt
*TorF
= *Imp
? ConstantInt::getTrue(BB
->getContext())
7302 : ConstantInt::getFalse(BB
->getContext());
7303 BI
->setCondition(TorF
);
7304 RecursivelyDeleteTriviallyDeadInstructions(OldCond
);
7305 return requestResimplify();
7308 // If this basic block is ONLY a compare and a branch, and if a predecessor
7309 // branches to us and one of our successors, fold the comparison into the
7310 // predecessor and use logical operations to pick the right destination.
7311 if (Options
.SpeculateBlocks
&&
7312 FoldBranchToCommonDest(BI
, DTU
, /*MSSAU=*/nullptr, &TTI
,
7313 Options
.BonusInstThreshold
))
7314 return requestResimplify();
7316 // We have a conditional branch to two blocks that are only reachable
7317 // from BI. We know that the condbr dominates the two blocks, so see if
7318 // there is any identical code in the "then" and "else" blocks. If so, we
7319 // can hoist it up to the branching block.
7320 if (BI
->getSuccessor(0)->getSinglePredecessor()) {
7321 if (BI
->getSuccessor(1)->getSinglePredecessor()) {
7322 if (HoistCommon
&& hoistCommonCodeFromSuccessors(
7323 BI
->getParent(), !Options
.HoistCommonInsts
))
7324 return requestResimplify();
7326 // If Successor #1 has multiple preds, we may be able to conditionally
7327 // execute Successor #0 if it branches to Successor #1.
7328 Instruction
*Succ0TI
= BI
->getSuccessor(0)->getTerminator();
7329 if (Succ0TI
->getNumSuccessors() == 1 &&
7330 Succ0TI
->getSuccessor(0) == BI
->getSuccessor(1))
7331 if (SpeculativelyExecuteBB(BI
, BI
->getSuccessor(0)))
7332 return requestResimplify();
7334 } else if (BI
->getSuccessor(1)->getSinglePredecessor()) {
7335 // If Successor #0 has multiple preds, we may be able to conditionally
7336 // execute Successor #1 if it branches to Successor #0.
7337 Instruction
*Succ1TI
= BI
->getSuccessor(1)->getTerminator();
7338 if (Succ1TI
->getNumSuccessors() == 1 &&
7339 Succ1TI
->getSuccessor(0) == BI
->getSuccessor(0))
7340 if (SpeculativelyExecuteBB(BI
, BI
->getSuccessor(1)))
7341 return requestResimplify();
7344 // If this is a branch on something for which we know the constant value in
7345 // predecessors (e.g. a phi node in the current block), thread control
7346 // through this block.
7347 if (FoldCondBranchOnValueKnownInPredecessor(BI
, DTU
, DL
, Options
.AC
))
7348 return requestResimplify();
7350 // Scan predecessor blocks for conditional branches.
7351 for (BasicBlock
*Pred
: predecessors(BB
))
7352 if (BranchInst
*PBI
= dyn_cast
<BranchInst
>(Pred
->getTerminator()))
7353 if (PBI
!= BI
&& PBI
->isConditional())
7354 if (SimplifyCondBranchToCondBranch(PBI
, BI
, DTU
, DL
, TTI
))
7355 return requestResimplify();
7357 // Look for diamond patterns.
7358 if (MergeCondStores
)
7359 if (BasicBlock
*PrevBB
= allPredecessorsComeFromSameSource(BB
))
7360 if (BranchInst
*PBI
= dyn_cast
<BranchInst
>(PrevBB
->getTerminator()))
7361 if (PBI
!= BI
&& PBI
->isConditional())
7362 if (mergeConditionalStores(PBI
, BI
, DTU
, DL
, TTI
))
7363 return requestResimplify();
7368 /// Check if passing a value to an instruction will cause undefined behavior.
7369 static bool passingValueIsAlwaysUndefined(Value
*V
, Instruction
*I
, bool PtrValueMayBeModified
) {
7370 Constant
*C
= dyn_cast
<Constant
>(V
);
7377 if (C
->isNullValue() || isa
<UndefValue
>(C
)) {
7378 // Only look at the first use, avoid hurting compile time with long uselists
7379 auto *Use
= cast
<Instruction
>(*I
->user_begin());
7380 // Bail out if Use is not in the same BB as I or Use == I or Use comes
7381 // before I in the block. The latter two can be the case if Use is a PHI
7383 if (Use
->getParent() != I
->getParent() || Use
== I
|| Use
->comesBefore(I
))
7386 // Now make sure that there are no instructions in between that can alter
7387 // control flow (eg. calls)
7389 make_range(std::next(I
->getIterator()), Use
->getIterator());
7390 if (any_of(InstrRange
, [](Instruction
&I
) {
7391 return !isGuaranteedToTransferExecutionToSuccessor(&I
);
7395 // Look through GEPs. A load from a GEP derived from NULL is still undefined
7396 if (GetElementPtrInst
*GEP
= dyn_cast
<GetElementPtrInst
>(Use
))
7397 if (GEP
->getPointerOperand() == I
) {
7398 if (!GEP
->isInBounds() || !GEP
->hasAllZeroIndices())
7399 PtrValueMayBeModified
= true;
7400 return passingValueIsAlwaysUndefined(V
, GEP
, PtrValueMayBeModified
);
7403 // Look through bitcasts.
7404 if (BitCastInst
*BC
= dyn_cast
<BitCastInst
>(Use
))
7405 return passingValueIsAlwaysUndefined(V
, BC
, PtrValueMayBeModified
);
7407 // Load from null is undefined.
7408 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(Use
))
7409 if (!LI
->isVolatile())
7410 return !NullPointerIsDefined(LI
->getFunction(),
7411 LI
->getPointerAddressSpace());
7413 // Store to null is undefined.
7414 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(Use
))
7415 if (!SI
->isVolatile())
7416 return (!NullPointerIsDefined(SI
->getFunction(),
7417 SI
->getPointerAddressSpace())) &&
7418 SI
->getPointerOperand() == I
;
7420 if (auto *CB
= dyn_cast
<CallBase
>(Use
)) {
7421 if (C
->isNullValue() && NullPointerIsDefined(CB
->getFunction()))
7423 // A call to null is undefined.
7424 if (CB
->getCalledOperand() == I
)
7427 if (C
->isNullValue()) {
7428 for (const llvm::Use
&Arg
: CB
->args())
7430 unsigned ArgIdx
= CB
->getArgOperandNo(&Arg
);
7431 if (CB
->isPassingUndefUB(ArgIdx
) &&
7432 CB
->paramHasAttr(ArgIdx
, Attribute::NonNull
)) {
7433 // Passing null to a nonnnull+noundef argument is undefined.
7434 return !PtrValueMayBeModified
;
7437 } else if (isa
<UndefValue
>(C
)) {
7438 // Passing undef to a noundef argument is undefined.
7439 for (const llvm::Use
&Arg
: CB
->args())
7441 unsigned ArgIdx
= CB
->getArgOperandNo(&Arg
);
7442 if (CB
->isPassingUndefUB(ArgIdx
)) {
7443 // Passing undef to a noundef argument is undefined.
7453 /// If BB has an incoming value that will always trigger undefined behavior
7454 /// (eg. null pointer dereference), remove the branch leading here.
7455 static bool removeUndefIntroducingPredecessor(BasicBlock
*BB
,
7456 DomTreeUpdater
*DTU
,
7457 AssumptionCache
*AC
) {
7458 for (PHINode
&PHI
: BB
->phis())
7459 for (unsigned i
= 0, e
= PHI
.getNumIncomingValues(); i
!= e
; ++i
)
7460 if (passingValueIsAlwaysUndefined(PHI
.getIncomingValue(i
), &PHI
)) {
7461 BasicBlock
*Predecessor
= PHI
.getIncomingBlock(i
);
7462 Instruction
*T
= Predecessor
->getTerminator();
7463 IRBuilder
<> Builder(T
);
7464 if (BranchInst
*BI
= dyn_cast
<BranchInst
>(T
)) {
7465 BB
->removePredecessor(Predecessor
);
7466 // Turn unconditional branches into unreachables and remove the dead
7467 // destination from conditional branches.
7468 if (BI
->isUnconditional())
7469 Builder
.CreateUnreachable();
7471 // Preserve guarding condition in assume, because it might not be
7472 // inferrable from any dominating condition.
7473 Value
*Cond
= BI
->getCondition();
7474 CallInst
*Assumption
;
7475 if (BI
->getSuccessor(0) == BB
)
7476 Assumption
= Builder
.CreateAssumption(Builder
.CreateNot(Cond
));
7478 Assumption
= Builder
.CreateAssumption(Cond
);
7480 AC
->registerAssumption(cast
<AssumeInst
>(Assumption
));
7481 Builder
.CreateBr(BI
->getSuccessor(0) == BB
? BI
->getSuccessor(1)
7482 : BI
->getSuccessor(0));
7484 BI
->eraseFromParent();
7486 DTU
->applyUpdates({{DominatorTree::Delete
, Predecessor
, BB
}});
7488 } else if (SwitchInst
*SI
= dyn_cast
<SwitchInst
>(T
)) {
7489 // Redirect all branches leading to UB into
7490 // a newly created unreachable block.
7491 BasicBlock
*Unreachable
= BasicBlock::Create(
7492 Predecessor
->getContext(), "unreachable", BB
->getParent(), BB
);
7493 Builder
.SetInsertPoint(Unreachable
);
7494 // The new block contains only one instruction: Unreachable
7495 Builder
.CreateUnreachable();
7496 for (const auto &Case
: SI
->cases())
7497 if (Case
.getCaseSuccessor() == BB
) {
7498 BB
->removePredecessor(Predecessor
);
7499 Case
.setSuccessor(Unreachable
);
7501 if (SI
->getDefaultDest() == BB
) {
7502 BB
->removePredecessor(Predecessor
);
7503 SI
->setDefaultDest(Unreachable
);
7508 { { DominatorTree::Insert
, Predecessor
, Unreachable
},
7509 { DominatorTree::Delete
, Predecessor
, BB
} });
7517 bool SimplifyCFGOpt::simplifyOnce(BasicBlock
*BB
) {
7518 bool Changed
= false;
7520 assert(BB
&& BB
->getParent() && "Block not embedded in function!");
7521 assert(BB
->getTerminator() && "Degenerate basic block encountered!");
7523 // Remove basic blocks that have no predecessors (except the entry block)...
7524 // or that just have themself as a predecessor. These are unreachable.
7525 if ((pred_empty(BB
) && BB
!= &BB
->getParent()->getEntryBlock()) ||
7526 BB
->getSinglePredecessor() == BB
) {
7527 LLVM_DEBUG(dbgs() << "Removing BB: \n" << *BB
);
7528 DeleteDeadBlock(BB
, DTU
);
7532 // Check to see if we can constant propagate this terminator instruction
7534 Changed
|= ConstantFoldTerminator(BB
, /*DeleteDeadConditions=*/true,
7535 /*TLI=*/nullptr, DTU
);
7537 // Check for and eliminate duplicate PHI nodes in this block.
7538 Changed
|= EliminateDuplicatePHINodes(BB
);
7540 // Check for and remove branches that will always cause undefined behavior.
7541 if (removeUndefIntroducingPredecessor(BB
, DTU
, Options
.AC
))
7542 return requestResimplify();
7544 // Merge basic blocks into their predecessor if there is only one distinct
7545 // pred, and if there is only one distinct successor of the predecessor, and
7546 // if there are no PHI nodes.
7547 if (MergeBlockIntoPredecessor(BB
, DTU
))
7550 if (SinkCommon
&& Options
.SinkCommonInsts
)
7551 if (SinkCommonCodeFromPredecessors(BB
, DTU
) ||
7552 MergeCompatibleInvokes(BB
, DTU
)) {
7553 // SinkCommonCodeFromPredecessors() does not automatically CSE PHI's,
7554 // so we may now how duplicate PHI's.
7555 // Let's rerun EliminateDuplicatePHINodes() first,
7556 // before FoldTwoEntryPHINode() potentially converts them into select's,
7557 // after which we'd need a whole EarlyCSE pass run to cleanup them.
7561 IRBuilder
<> Builder(BB
);
7563 if (Options
.SpeculateBlocks
&&
7564 !BB
->getParent()->hasFnAttribute(Attribute::OptForFuzzing
)) {
7565 // If there is a trivial two-entry PHI node in this basic block, and we can
7566 // eliminate it, do so now.
7567 if (auto *PN
= dyn_cast
<PHINode
>(BB
->begin()))
7568 if (PN
->getNumIncomingValues() == 2)
7569 if (FoldTwoEntryPHINode(PN
, TTI
, DTU
, DL
))
7573 Instruction
*Terminator
= BB
->getTerminator();
7574 Builder
.SetInsertPoint(Terminator
);
7575 switch (Terminator
->getOpcode()) {
7576 case Instruction::Br
:
7577 Changed
|= simplifyBranch(cast
<BranchInst
>(Terminator
), Builder
);
7579 case Instruction::Resume
:
7580 Changed
|= simplifyResume(cast
<ResumeInst
>(Terminator
), Builder
);
7582 case Instruction::CleanupRet
:
7583 Changed
|= simplifyCleanupReturn(cast
<CleanupReturnInst
>(Terminator
));
7585 case Instruction::Switch
:
7586 Changed
|= simplifySwitch(cast
<SwitchInst
>(Terminator
), Builder
);
7588 case Instruction::Unreachable
:
7589 Changed
|= simplifyUnreachable(cast
<UnreachableInst
>(Terminator
));
7591 case Instruction::IndirectBr
:
7592 Changed
|= simplifyIndirectBr(cast
<IndirectBrInst
>(Terminator
));
7599 bool SimplifyCFGOpt::run(BasicBlock
*BB
) {
7600 bool Changed
= false;
7602 // Repeated simplify BB as long as resimplification is requested.
7606 // Perform one round of simplifcation. Resimplify flag will be set if
7607 // another iteration is requested.
7608 Changed
|= simplifyOnce(BB
);
7609 } while (Resimplify
);
7614 bool llvm::simplifyCFG(BasicBlock
*BB
, const TargetTransformInfo
&TTI
,
7615 DomTreeUpdater
*DTU
, const SimplifyCFGOptions
&Options
,
7616 ArrayRef
<WeakVH
> LoopHeaders
) {
7617 return SimplifyCFGOpt(TTI
, DTU
, BB
->getModule()->getDataLayout(), LoopHeaders
,