1 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Peephole optimize the CFG.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.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/ConstantFolding.h"
27 #include "llvm/Analysis/EHPersonalities.h"
28 #include "llvm/Analysis/InstructionSimplify.h"
29 #include "llvm/Analysis/TargetTransformInfo.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/IR/Attributes.h"
33 #include "llvm/IR/BasicBlock.h"
34 #include "llvm/IR/CFG.h"
35 #include "llvm/IR/CallSite.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/ConstantRange.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GlobalValue.h"
43 #include "llvm/IR/GlobalVariable.h"
44 #include "llvm/IR/IRBuilder.h"
45 #include "llvm/IR/InstrTypes.h"
46 #include "llvm/IR/Instruction.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/IntrinsicInst.h"
49 #include "llvm/IR/Intrinsics.h"
50 #include "llvm/IR/LLVMContext.h"
51 #include "llvm/IR/MDBuilder.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/NoFolder.h"
55 #include "llvm/IR/Operator.h"
56 #include "llvm/IR/PatternMatch.h"
57 #include "llvm/IR/Type.h"
58 #include "llvm/IR/Use.h"
59 #include "llvm/IR/User.h"
60 #include "llvm/IR/Value.h"
61 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/CommandLine.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/KnownBits.h"
66 #include "llvm/Support/MathExtras.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
69 #include "llvm/Transforms/Utils/ValueMapper.h"
83 using namespace PatternMatch;
85 #define DEBUG_TYPE "simplifycfg"
87 // Chosen as 2 so as to be cheap, but still to have enough power to fold
88 // a select, so the "clamp" idiom (of a min followed by a max) will be caught.
89 // To catch this, we need to fold a compare and a select, hence '2' being the
90 // minimum reasonable default.
91 static cl::opt<unsigned> PHINodeFoldingThreshold(
92 "phi-node-folding-threshold", cl::Hidden, cl::init(2),
94 "Control the amount of phi node folding to perform (default = 2)"));
96 static cl::opt<bool> DupRet(
97 "simplifycfg-dup-ret", cl::Hidden, cl::init(false),
98 cl::desc("Duplicate return instructions into unconditional branches"));
101 SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true),
102 cl::desc("Sink common instructions down to the end block"));
104 static cl::opt<bool> HoistCondStores(
105 "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
106 cl::desc("Hoist conditional stores if an unconditional store precedes"));
108 static cl::opt<bool> MergeCondStores(
109 "simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true),
110 cl::desc("Hoist conditional stores even if an unconditional store does not "
111 "precede - hoist multiple conditional stores into a single "
112 "predicated store"));
114 static cl::opt<bool> MergeCondStoresAggressively(
115 "simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false),
116 cl::desc("When merging conditional stores, do so even if the resultant "
117 "basic blocks are unlikely to be if-converted as a result"));
119 static cl::opt<bool> SpeculateOneExpensiveInst(
120 "speculate-one-expensive-inst", cl::Hidden, cl::init(true),
121 cl::desc("Allow exactly one expensive instruction to be speculatively "
124 static cl::opt<unsigned> MaxSpeculationDepth(
125 "max-speculation-depth", cl::Hidden, cl::init(10),
126 cl::desc("Limit maximum recursion depth when calculating costs of "
127 "speculatively executed instructions"));
129 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
130 STATISTIC(NumLinearMaps,
131 "Number of switch instructions turned into linear mapping");
132 STATISTIC(NumLookupTables,
133 "Number of switch instructions turned into lookup tables");
135 NumLookupTablesHoles,
136 "Number of switch instructions turned into lookup tables (holes checked)");
137 STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares");
138 STATISTIC(NumSinkCommons,
139 "Number of common instructions sunk down to the end block");
140 STATISTIC(NumSpeculations, "Number of speculative executed instructions");
144 // The first field contains the value that the switch produces when a certain
145 // case group is selected, and the second field is a vector containing the
146 // cases composing the case group.
147 using SwitchCaseResultVectorTy =
148 SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>;
150 // The first field contains the phi node that generates a result of the switch
151 // and the second field contains the value generated for a certain case in the
152 // switch for that PHI.
153 using SwitchCaseResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
155 /// ValueEqualityComparisonCase - Represents a case of a switch.
156 struct ValueEqualityComparisonCase {
160 ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
161 : Value(Value), Dest(Dest) {}
163 bool operator<(ValueEqualityComparisonCase RHS) const {
164 // Comparing pointers is ok as we only rely on the order for uniquing.
165 return Value < RHS.Value;
168 bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
171 class SimplifyCFGOpt {
172 const TargetTransformInfo &TTI;
173 const DataLayout &DL;
174 SmallPtrSetImpl<BasicBlock *> *LoopHeaders;
175 const SimplifyCFGOptions &Options;
178 Value *isValueEqualityComparison(Instruction *TI);
179 BasicBlock *GetValueEqualityComparisonCases(
180 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
181 bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
183 IRBuilder<> &Builder);
184 bool FoldValueComparisonIntoPredecessors(Instruction *TI,
185 IRBuilder<> &Builder);
187 bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
188 bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
189 bool SimplifySingleResume(ResumeInst *RI);
190 bool SimplifyCommonResume(ResumeInst *RI);
191 bool SimplifyCleanupReturn(CleanupReturnInst *RI);
192 bool SimplifyUnreachable(UnreachableInst *UI);
193 bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
194 bool SimplifyIndirectBr(IndirectBrInst *IBI);
195 bool SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder);
196 bool SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder);
198 bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
199 IRBuilder<> &Builder);
202 SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout &DL,
203 SmallPtrSetImpl<BasicBlock *> *LoopHeaders,
204 const SimplifyCFGOptions &Opts)
205 : TTI(TTI), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {}
207 bool run(BasicBlock *BB);
208 bool simplifyOnce(BasicBlock *BB);
210 // Helper to set Resimplify and return change indication.
211 bool requestResimplify() {
217 } // end anonymous namespace
219 /// Return true if it is safe to merge these two
220 /// terminator instructions together.
222 SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
223 SmallSetVector<BasicBlock *, 4> *FailBlocks = nullptr) {
225 return false; // Can't merge with self!
227 // It is not safe to merge these two switch instructions if they have a common
228 // successor, and if that successor has a PHI node, and if *that* PHI node has
229 // conflicting incoming values from the two switch blocks.
230 BasicBlock *SI1BB = SI1->getParent();
231 BasicBlock *SI2BB = SI2->getParent();
233 SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
235 for (BasicBlock *Succ : successors(SI2BB))
236 if (SI1Succs.count(Succ))
237 for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
238 PHINode *PN = cast<PHINode>(BBI);
239 if (PN->getIncomingValueForBlock(SI1BB) !=
240 PN->getIncomingValueForBlock(SI2BB)) {
242 FailBlocks->insert(Succ);
250 /// Return true if it is safe and profitable to merge these two terminator
251 /// instructions together, where SI1 is an unconditional branch. PhiNodes will
252 /// store all PHI nodes in common successors.
254 isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2,
256 SmallVectorImpl<PHINode *> &PhiNodes) {
258 return false; // Can't merge with self!
259 assert(SI1->isUnconditional() && SI2->isConditional());
261 // We fold the unconditional branch if we can easily update all PHI nodes in
262 // common successors:
263 // 1> We have a constant incoming value for the conditional branch;
264 // 2> We have "Cond" as the incoming value for the unconditional branch;
265 // 3> SI2->getCondition() and Cond have same operands.
266 CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
269 if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
270 Cond->getOperand(1) == Ci2->getOperand(1)) &&
271 !(Cond->getOperand(0) == Ci2->getOperand(1) &&
272 Cond->getOperand(1) == Ci2->getOperand(0)))
275 BasicBlock *SI1BB = SI1->getParent();
276 BasicBlock *SI2BB = SI2->getParent();
277 SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
278 for (BasicBlock *Succ : successors(SI2BB))
279 if (SI1Succs.count(Succ))
280 for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
281 PHINode *PN = cast<PHINode>(BBI);
282 if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
283 !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
285 PhiNodes.push_back(PN);
290 /// Update PHI nodes in Succ to indicate that there will now be entries in it
291 /// from the 'NewPred' block. The values that will be flowing into the PHI nodes
292 /// will be the same as those coming in from ExistPred, an existing predecessor
294 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
295 BasicBlock *ExistPred) {
296 for (PHINode &PN : Succ->phis())
297 PN.addIncoming(PN.getIncomingValueForBlock(ExistPred), NewPred);
300 /// Compute an abstract "cost" of speculating the given instruction,
301 /// which is assumed to be safe to speculate. TCC_Free means cheap,
302 /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
304 static unsigned ComputeSpeculationCost(const User *I,
305 const TargetTransformInfo &TTI) {
306 assert(isSafeToSpeculativelyExecute(I) &&
307 "Instruction is not safe to speculatively execute!");
308 return TTI.getUserCost(I);
311 /// If we have a merge point of an "if condition" as accepted above,
312 /// return true if the specified value dominates the block. We
313 /// don't handle the true generality of domination here, just a special case
314 /// which works well enough for us.
316 /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
317 /// see if V (which must be an instruction) and its recursive operands
318 /// that do not dominate BB have a combined cost lower than CostRemaining and
319 /// are non-trapping. If both are true, the instruction is inserted into the
320 /// set and true is returned.
322 /// The cost for most non-trapping instructions is defined as 1 except for
323 /// Select whose cost is 2.
325 /// After this function returns, CostRemaining is decreased by the cost of
326 /// V plus its non-dominating operands. If that cost is greater than
327 /// CostRemaining, false is returned and CostRemaining is undefined.
328 static bool DominatesMergePoint(Value *V, BasicBlock *BB,
329 SmallPtrSetImpl<Instruction *> &AggressiveInsts,
330 unsigned &CostRemaining,
331 const TargetTransformInfo &TTI,
332 unsigned Depth = 0) {
333 // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
334 // so limit the recursion depth.
335 // TODO: While this recursion limit does prevent pathological behavior, it
336 // would be better to track visited instructions to avoid cycles.
337 if (Depth == MaxSpeculationDepth)
340 Instruction *I = dyn_cast<Instruction>(V);
342 // Non-instructions all dominate instructions, but not all constantexprs
343 // can be executed unconditionally.
344 if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
349 BasicBlock *PBB = I->getParent();
351 // We don't want to allow weird loops that might have the "if condition" in
352 // the bottom of this block.
356 // If this instruction is defined in a block that contains an unconditional
357 // branch to BB, then it must be in the 'conditional' part of the "if
358 // statement". If not, it definitely dominates the region.
359 BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
360 if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
363 // If we have seen this instruction before, don't count it again.
364 if (AggressiveInsts.count(I))
367 // Okay, it looks like the instruction IS in the "condition". Check to
368 // see if it's a cheap instruction to unconditionally compute, and if it
369 // only uses stuff defined outside of the condition. If so, hoist it out.
370 if (!isSafeToSpeculativelyExecute(I))
373 unsigned Cost = ComputeSpeculationCost(I, TTI);
375 // Allow exactly one instruction to be speculated regardless of its cost
376 // (as long as it is safe to do so).
377 // This is intended to flatten the CFG even if the instruction is a division
378 // or other expensive operation. The speculation of an expensive instruction
379 // is expected to be undone in CodeGenPrepare if the speculation has not
380 // enabled further IR optimizations.
381 if (Cost > CostRemaining &&
382 (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0))
385 // Avoid unsigned wrap.
386 CostRemaining = (Cost > CostRemaining) ? 0 : CostRemaining - Cost;
388 // Okay, we can only really hoist these out if their operands do
389 // not take us over the cost threshold.
390 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
391 if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining, TTI,
394 // Okay, it's safe to do this! Remember this instruction.
395 AggressiveInsts.insert(I);
399 /// Extract ConstantInt from value, looking through IntToPtr
400 /// and PointerNullValue. Return NULL if value is not a constant int.
401 static ConstantInt *GetConstantInt(Value *V, const DataLayout &DL) {
402 // Normal constant int.
403 ConstantInt *CI = dyn_cast<ConstantInt>(V);
404 if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy())
407 // This is some kind of pointer constant. Turn it into a pointer-sized
408 // ConstantInt if possible.
409 IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
411 // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
412 if (isa<ConstantPointerNull>(V))
413 return ConstantInt::get(PtrTy, 0);
415 // IntToPtr const int.
416 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
417 if (CE->getOpcode() == Instruction::IntToPtr)
418 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
419 // The constant is very likely to have the right type already.
420 if (CI->getType() == PtrTy)
423 return cast<ConstantInt>(
424 ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
431 /// Given a chain of or (||) or and (&&) comparison of a value against a
432 /// constant, this will try to recover the information required for a switch
434 /// It will depth-first traverse the chain of comparison, seeking for patterns
435 /// like %a == 12 or %a < 4 and combine them to produce a set of integer
436 /// representing the different cases for the switch.
437 /// Note that if the chain is composed of '||' it will build the set of elements
438 /// that matches the comparisons (i.e. any of this value validate the chain)
439 /// while for a chain of '&&' it will build the set elements that make the test
441 struct ConstantComparesGatherer {
442 const DataLayout &DL;
444 /// Value found for the switch comparison
445 Value *CompValue = nullptr;
447 /// Extra clause to be checked before the switch
448 Value *Extra = nullptr;
450 /// Set of integers to match in switch
451 SmallVector<ConstantInt *, 8> Vals;
453 /// Number of comparisons matched in the and/or chain
454 unsigned UsedICmps = 0;
456 /// Construct and compute the result for the comparison instruction Cond
457 ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
461 ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
462 ConstantComparesGatherer &
463 operator=(const ConstantComparesGatherer &) = delete;
466 /// Try to set the current value used for the comparison, it succeeds only if
467 /// it wasn't set before or if the new value is the same as the old one
468 bool setValueOnce(Value *NewVal) {
469 if (CompValue && CompValue != NewVal)
472 return (CompValue != nullptr);
475 /// Try to match Instruction "I" as a comparison against a constant and
476 /// populates the array Vals with the set of values that match (or do not
477 /// match depending on isEQ).
478 /// Return false on failure. On success, the Value the comparison matched
479 /// against is placed in CompValue.
480 /// If CompValue is already set, the function is expected to fail if a match
481 /// is found but the value compared to is different.
482 bool matchInstruction(Instruction *I, bool isEQ) {
483 // If this is an icmp against a constant, handle this as one of the cases.
486 if (!((ICI = dyn_cast<ICmpInst>(I)) &&
487 (C = GetConstantInt(I->getOperand(1), DL)))) {
494 // Pattern match a special case
495 // (x & ~2^z) == y --> x == y || x == y|2^z
496 // This undoes a transformation done by instcombine to fuse 2 compares.
497 if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
498 // It's a little bit hard to see why the following transformations are
499 // correct. Here is a CVC3 program to verify them for 64-bit values:
502 ONE : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
506 mask : BITVECTOR(64) = BVSHL(ONE, z);
507 QUERY( (y & ~mask = y) =>
508 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
510 QUERY( (y | mask = y) =>
511 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
515 // Please note that each pattern must be a dual implication (<--> or
516 // iff). One directional implication can create spurious matches. If the
517 // implication is only one-way, an unsatisfiable condition on the left
518 // side can imply a satisfiable condition on the right side. Dual
519 // implication ensures that satisfiable conditions are transformed to
520 // other satisfiable conditions and unsatisfiable conditions are
521 // transformed to other unsatisfiable conditions.
523 // Here is a concrete example of a unsatisfiable condition on the left
524 // implying a satisfiable condition on the right:
527 // (x & ~mask) == y --> (x == y || x == (y | mask))
529 // Substituting y = 3, z = 0 yields:
530 // (x & -2) == 3 --> (x == 3 || x == 2)
532 // Pattern match a special case:
534 QUERY( (y & ~mask = y) =>
535 ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
538 if (match(ICI->getOperand(0),
539 m_And(m_Value(RHSVal), m_APInt(RHSC)))) {
541 if (Mask.isPowerOf2() && (C->getValue() & ~Mask) == C->getValue()) {
542 // If we already have a value for the switch, it has to match!
543 if (!setValueOnce(RHSVal))
548 ConstantInt::get(C->getContext(),
549 C->getValue() | Mask));
555 // Pattern match a special case:
557 QUERY( (y | mask = y) =>
558 ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
561 if (match(ICI->getOperand(0),
562 m_Or(m_Value(RHSVal), m_APInt(RHSC)))) {
564 if (Mask.isPowerOf2() && (C->getValue() | Mask) == C->getValue()) {
565 // If we already have a value for the switch, it has to match!
566 if (!setValueOnce(RHSVal))
570 Vals.push_back(ConstantInt::get(C->getContext(),
571 C->getValue() & ~Mask));
577 // If we already have a value for the switch, it has to match!
578 if (!setValueOnce(ICI->getOperand(0)))
583 return ICI->getOperand(0);
586 // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
587 ConstantRange Span = ConstantRange::makeAllowedICmpRegion(
588 ICI->getPredicate(), C->getValue());
590 // Shift the range if the compare is fed by an add. This is the range
591 // compare idiom as emitted by instcombine.
592 Value *CandidateVal = I->getOperand(0);
593 if (match(I->getOperand(0), m_Add(m_Value(RHSVal), m_APInt(RHSC)))) {
594 Span = Span.subtract(*RHSC);
595 CandidateVal = RHSVal;
598 // If this is an and/!= check, then we are looking to build the set of
599 // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
602 Span = Span.inverse();
604 // If there are a ton of values, we don't want to make a ginormous switch.
605 if (Span.isSizeLargerThan(8) || Span.isEmptySet()) {
609 // If we already have a value for the switch, it has to match!
610 if (!setValueOnce(CandidateVal))
613 // Add all values from the range to the set
614 for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
615 Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
621 /// Given a potentially 'or'd or 'and'd together collection of icmp
622 /// eq/ne/lt/gt instructions that compare a value against a constant, extract
623 /// the value being compared, and stick the list constants into the Vals
625 /// One "Extra" case is allowed to differ from the other.
626 void gather(Value *V) {
627 Instruction *I = dyn_cast<Instruction>(V);
628 bool isEQ = (I->getOpcode() == Instruction::Or);
630 // Keep a stack (SmallVector for efficiency) for depth-first traversal
631 SmallVector<Value *, 8> DFT;
632 SmallPtrSet<Value *, 8> Visited;
638 while (!DFT.empty()) {
639 V = DFT.pop_back_val();
641 if (Instruction *I = dyn_cast<Instruction>(V)) {
642 // If it is a || (or && depending on isEQ), process the operands.
643 if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) {
644 if (Visited.insert(I->getOperand(1)).second)
645 DFT.push_back(I->getOperand(1));
646 if (Visited.insert(I->getOperand(0)).second)
647 DFT.push_back(I->getOperand(0));
651 // Try to match the current instruction
652 if (matchInstruction(I, isEQ))
653 // Match succeed, continue the loop
657 // One element of the sequence of || (or &&) could not be match as a
658 // comparison against the same value as the others.
659 // We allow only one "Extra" case to be checked before the switch
664 // Failed to parse a proper sequence, abort now
671 } // end anonymous namespace
673 static void EraseTerminatorAndDCECond(Instruction *TI) {
674 Instruction *Cond = nullptr;
675 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
676 Cond = dyn_cast<Instruction>(SI->getCondition());
677 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
678 if (BI->isConditional())
679 Cond = dyn_cast<Instruction>(BI->getCondition());
680 } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
681 Cond = dyn_cast<Instruction>(IBI->getAddress());
684 TI->eraseFromParent();
686 RecursivelyDeleteTriviallyDeadInstructions(Cond);
689 /// Return true if the specified terminator checks
690 /// to see if a value is equal to constant integer value.
691 Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
693 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
694 // Do not permit merging of large switch instructions into their
695 // predecessors unless there is only one predecessor.
696 if (!SI->getParent()->hasNPredecessorsOrMore(128 / SI->getNumSuccessors()))
697 CV = SI->getCondition();
698 } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
699 if (BI->isConditional() && BI->getCondition()->hasOneUse())
700 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
701 if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
702 CV = ICI->getOperand(0);
705 // Unwrap any lossless ptrtoint cast.
707 if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
708 Value *Ptr = PTII->getPointerOperand();
709 if (PTII->getType() == DL.getIntPtrType(Ptr->getType()))
716 /// Given a value comparison instruction,
717 /// decode all of the 'cases' that it represents and return the 'default' block.
718 BasicBlock *SimplifyCFGOpt::GetValueEqualityComparisonCases(
719 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
720 if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
721 Cases.reserve(SI->getNumCases());
722 for (auto Case : SI->cases())
723 Cases.push_back(ValueEqualityComparisonCase(Case.getCaseValue(),
724 Case.getCaseSuccessor()));
725 return SI->getDefaultDest();
728 BranchInst *BI = cast<BranchInst>(TI);
729 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
730 BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
731 Cases.push_back(ValueEqualityComparisonCase(
732 GetConstantInt(ICI->getOperand(1), DL), Succ));
733 return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
736 /// Given a vector of bb/value pairs, remove any entries
737 /// in the list that match the specified block.
739 EliminateBlockCases(BasicBlock *BB,
740 std::vector<ValueEqualityComparisonCase> &Cases) {
741 Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
744 /// Return true if there are any keys in C1 that exist in C2 as well.
745 static bool ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
746 std::vector<ValueEqualityComparisonCase> &C2) {
747 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
749 // Make V1 be smaller than V2.
750 if (V1->size() > V2->size())
755 if (V1->size() == 1) {
757 ConstantInt *TheVal = (*V1)[0].Value;
758 for (unsigned i = 0, e = V2->size(); i != e; ++i)
759 if (TheVal == (*V2)[i].Value)
763 // Otherwise, just sort both lists and compare element by element.
764 array_pod_sort(V1->begin(), V1->end());
765 array_pod_sort(V2->begin(), V2->end());
766 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
767 while (i1 != e1 && i2 != e2) {
768 if ((*V1)[i1].Value == (*V2)[i2].Value)
770 if ((*V1)[i1].Value < (*V2)[i2].Value)
778 // Set branch weights on SwitchInst. This sets the metadata if there is at
779 // least one non-zero weight.
780 static void setBranchWeights(SwitchInst *SI, ArrayRef<uint32_t> Weights) {
781 // Check that there is at least one non-zero weight. Otherwise, pass
782 // nullptr to setMetadata which will erase the existing metadata.
784 if (llvm::any_of(Weights, [](uint32_t W) { return W != 0; }))
785 N = MDBuilder(SI->getParent()->getContext()).createBranchWeights(Weights);
786 SI->setMetadata(LLVMContext::MD_prof, N);
789 // Similar to the above, but for branch and select instructions that take
790 // exactly 2 weights.
791 static void setBranchWeights(Instruction *I, uint32_t TrueWeight,
792 uint32_t FalseWeight) {
793 assert(isa<BranchInst>(I) || isa<SelectInst>(I));
794 // Check that there is at least one non-zero weight. Otherwise, pass
795 // nullptr to setMetadata which will erase the existing metadata.
797 if (TrueWeight || FalseWeight)
798 N = MDBuilder(I->getParent()->getContext())
799 .createBranchWeights(TrueWeight, FalseWeight);
800 I->setMetadata(LLVMContext::MD_prof, N);
803 /// If TI is known to be a terminator instruction and its block is known to
804 /// only have a single predecessor block, check to see if that predecessor is
805 /// also a value comparison with the same value, and if that comparison
806 /// determines the outcome of this comparison. If so, simplify TI. This does a
807 /// very limited form of jump threading.
808 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
809 Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
810 Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
812 return false; // Not a value comparison in predecessor.
814 Value *ThisVal = isValueEqualityComparison(TI);
815 assert(ThisVal && "This isn't a value comparison!!");
816 if (ThisVal != PredVal)
817 return false; // Different predicates.
819 // TODO: Preserve branch weight metadata, similarly to how
820 // FoldValueComparisonIntoPredecessors preserves it.
822 // Find out information about when control will move from Pred to TI's block.
823 std::vector<ValueEqualityComparisonCase> PredCases;
824 BasicBlock *PredDef =
825 GetValueEqualityComparisonCases(Pred->getTerminator(), PredCases);
826 EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
828 // Find information about how control leaves this block.
829 std::vector<ValueEqualityComparisonCase> ThisCases;
830 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
831 EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
833 // If TI's block is the default block from Pred's comparison, potentially
834 // simplify TI based on this knowledge.
835 if (PredDef == TI->getParent()) {
836 // If we are here, we know that the value is none of those cases listed in
837 // PredCases. If there are any cases in ThisCases that are in PredCases, we
839 if (!ValuesOverlap(PredCases, ThisCases))
842 if (isa<BranchInst>(TI)) {
843 // Okay, one of the successors of this condbr is dead. Convert it to a
845 assert(ThisCases.size() == 1 && "Branch can only have one case!");
846 // Insert the new branch.
847 Instruction *NI = Builder.CreateBr(ThisDef);
850 // Remove PHI node entries for the dead edge.
851 ThisCases[0].Dest->removePredecessor(TI->getParent());
853 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
854 << "Through successor TI: " << *TI << "Leaving: " << *NI
857 EraseTerminatorAndDCECond(TI);
861 SwitchInst *SI = cast<SwitchInst>(TI);
862 // Okay, TI has cases that are statically dead, prune them away.
863 SmallPtrSet<Constant *, 16> DeadCases;
864 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
865 DeadCases.insert(PredCases[i].Value);
867 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
868 << "Through successor TI: " << *TI);
870 // Collect branch weights into a vector.
871 SmallVector<uint32_t, 8> Weights;
872 MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
873 bool HasWeight = MD && (MD->getNumOperands() == 2 + SI->getNumCases());
875 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
877 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
878 Weights.push_back(CI->getValue().getZExtValue());
880 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
882 if (DeadCases.count(i->getCaseValue())) {
884 std::swap(Weights[i->getCaseIndex() + 1], Weights.back());
887 i->getCaseSuccessor()->removePredecessor(TI->getParent());
891 if (HasWeight && Weights.size() >= 2)
892 setBranchWeights(SI, Weights);
894 LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
898 // Otherwise, TI's block must correspond to some matched value. Find out
899 // which value (or set of values) this is.
900 ConstantInt *TIV = nullptr;
901 BasicBlock *TIBB = TI->getParent();
902 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
903 if (PredCases[i].Dest == TIBB) {
905 return false; // Cannot handle multiple values coming to this block.
906 TIV = PredCases[i].Value;
908 assert(TIV && "No edge from pred to succ?");
910 // Okay, we found the one constant that our value can be if we get into TI's
911 // BB. Find out which successor will unconditionally be branched to.
912 BasicBlock *TheRealDest = nullptr;
913 for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
914 if (ThisCases[i].Value == TIV) {
915 TheRealDest = ThisCases[i].Dest;
919 // If not handled by any explicit cases, it is handled by the default case.
921 TheRealDest = ThisDef;
923 // Remove PHI node entries for dead edges.
924 BasicBlock *CheckEdge = TheRealDest;
925 for (BasicBlock *Succ : successors(TIBB))
926 if (Succ != CheckEdge)
927 Succ->removePredecessor(TIBB);
931 // Insert the new branch.
932 Instruction *NI = Builder.CreateBr(TheRealDest);
935 LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
936 << "Through successor TI: " << *TI << "Leaving: " << *NI
939 EraseTerminatorAndDCECond(TI);
945 /// This class implements a stable ordering of constant
946 /// integers that does not depend on their address. This is important for
947 /// applications that sort ConstantInt's to ensure uniqueness.
948 struct ConstantIntOrdering {
949 bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
950 return LHS->getValue().ult(RHS->getValue());
954 } // end anonymous namespace
956 static int ConstantIntSortPredicate(ConstantInt *const *P1,
957 ConstantInt *const *P2) {
958 const ConstantInt *LHS = *P1;
959 const ConstantInt *RHS = *P2;
962 return LHS->getValue().ult(RHS->getValue()) ? 1 : -1;
965 static inline bool HasBranchWeights(const Instruction *I) {
966 MDNode *ProfMD = I->getMetadata(LLVMContext::MD_prof);
967 if (ProfMD && ProfMD->getOperand(0))
968 if (MDString *MDS = dyn_cast<MDString>(ProfMD->getOperand(0)))
969 return MDS->getString().equals("branch_weights");
974 /// Get Weights of a given terminator, the default weight is at the front
975 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
977 static void GetBranchWeights(Instruction *TI,
978 SmallVectorImpl<uint64_t> &Weights) {
979 MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
981 for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
982 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
983 Weights.push_back(CI->getValue().getZExtValue());
986 // If TI is a conditional eq, the default case is the false case,
987 // and the corresponding branch-weight data is at index 2. We swap the
988 // default weight to be the first entry.
989 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
990 assert(Weights.size() == 2);
991 ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
992 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
993 std::swap(Weights.front(), Weights.back());
997 /// Keep halving the weights until all can fit in uint32_t.
998 static void FitWeights(MutableArrayRef<uint64_t> Weights) {
999 uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
1000 if (Max > UINT_MAX) {
1001 unsigned Offset = 32 - countLeadingZeros(Max);
1002 for (uint64_t &I : Weights)
1007 /// The specified terminator is a value equality comparison instruction
1008 /// (either a switch or a branch on "X == c").
1009 /// See if any of the predecessors of the terminator block are value comparisons
1010 /// on the same value. If so, and if safe to do so, fold them together.
1011 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
1012 IRBuilder<> &Builder) {
1013 BasicBlock *BB = TI->getParent();
1014 Value *CV = isValueEqualityComparison(TI); // CondVal
1015 assert(CV && "Not a comparison?");
1016 bool Changed = false;
1018 SmallVector<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
1019 while (!Preds.empty()) {
1020 BasicBlock *Pred = Preds.pop_back_val();
1022 // See if the predecessor is a comparison with the same value.
1023 Instruction *PTI = Pred->getTerminator();
1024 Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
1026 if (PCV == CV && TI != PTI) {
1027 SmallSetVector<BasicBlock*, 4> FailBlocks;
1028 if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
1029 for (auto *Succ : FailBlocks) {
1030 if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split"))
1035 // Figure out which 'cases' to copy from SI to PSI.
1036 std::vector<ValueEqualityComparisonCase> BBCases;
1037 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
1039 std::vector<ValueEqualityComparisonCase> PredCases;
1040 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
1042 // Based on whether the default edge from PTI goes to BB or not, fill in
1043 // PredCases and PredDefault with the new switch cases we would like to
1045 SmallVector<BasicBlock *, 8> NewSuccessors;
1047 // Update the branch weight metadata along the way
1048 SmallVector<uint64_t, 8> Weights;
1049 bool PredHasWeights = HasBranchWeights(PTI);
1050 bool SuccHasWeights = HasBranchWeights(TI);
1052 if (PredHasWeights) {
1053 GetBranchWeights(PTI, Weights);
1054 // branch-weight metadata is inconsistent here.
1055 if (Weights.size() != 1 + PredCases.size())
1056 PredHasWeights = SuccHasWeights = false;
1057 } else if (SuccHasWeights)
1058 // If there are no predecessor weights but there are successor weights,
1059 // populate Weights with 1, which will later be scaled to the sum of
1060 // successor's weights
1061 Weights.assign(1 + PredCases.size(), 1);
1063 SmallVector<uint64_t, 8> SuccWeights;
1064 if (SuccHasWeights) {
1065 GetBranchWeights(TI, SuccWeights);
1066 // branch-weight metadata is inconsistent here.
1067 if (SuccWeights.size() != 1 + BBCases.size())
1068 PredHasWeights = SuccHasWeights = false;
1069 } else if (PredHasWeights)
1070 SuccWeights.assign(1 + BBCases.size(), 1);
1072 if (PredDefault == BB) {
1073 // If this is the default destination from PTI, only the edges in TI
1074 // that don't occur in PTI, or that branch to BB will be activated.
1075 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1076 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1077 if (PredCases[i].Dest != BB)
1078 PTIHandled.insert(PredCases[i].Value);
1080 // The default destination is BB, we don't need explicit targets.
1081 std::swap(PredCases[i], PredCases.back());
1083 if (PredHasWeights || SuccHasWeights) {
1084 // Increase weight for the default case.
1085 Weights[0] += Weights[i + 1];
1086 std::swap(Weights[i + 1], Weights.back());
1090 PredCases.pop_back();
1095 // Reconstruct the new switch statement we will be building.
1096 if (PredDefault != BBDefault) {
1097 PredDefault->removePredecessor(Pred);
1098 PredDefault = BBDefault;
1099 NewSuccessors.push_back(BBDefault);
1102 unsigned CasesFromPred = Weights.size();
1103 uint64_t ValidTotalSuccWeight = 0;
1104 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1105 if (!PTIHandled.count(BBCases[i].Value) &&
1106 BBCases[i].Dest != BBDefault) {
1107 PredCases.push_back(BBCases[i]);
1108 NewSuccessors.push_back(BBCases[i].Dest);
1109 if (SuccHasWeights || PredHasWeights) {
1110 // The default weight is at index 0, so weight for the ith case
1111 // should be at index i+1. Scale the cases from successor by
1112 // PredDefaultWeight (Weights[0]).
1113 Weights.push_back(Weights[0] * SuccWeights[i + 1]);
1114 ValidTotalSuccWeight += SuccWeights[i + 1];
1118 if (SuccHasWeights || PredHasWeights) {
1119 ValidTotalSuccWeight += SuccWeights[0];
1120 // Scale the cases from predecessor by ValidTotalSuccWeight.
1121 for (unsigned i = 1; i < CasesFromPred; ++i)
1122 Weights[i] *= ValidTotalSuccWeight;
1123 // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
1124 Weights[0] *= SuccWeights[0];
1127 // If this is not the default destination from PSI, only the edges
1128 // in SI that occur in PSI with a destination of BB will be
1130 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1131 std::map<ConstantInt *, uint64_t> WeightsForHandled;
1132 for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1133 if (PredCases[i].Dest == BB) {
1134 PTIHandled.insert(PredCases[i].Value);
1136 if (PredHasWeights || SuccHasWeights) {
1137 WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
1138 std::swap(Weights[i + 1], Weights.back());
1142 std::swap(PredCases[i], PredCases.back());
1143 PredCases.pop_back();
1148 // Okay, now we know which constants were sent to BB from the
1149 // predecessor. Figure out where they will all go now.
1150 for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1151 if (PTIHandled.count(BBCases[i].Value)) {
1152 // If this is one we are capable of getting...
1153 if (PredHasWeights || SuccHasWeights)
1154 Weights.push_back(WeightsForHandled[BBCases[i].Value]);
1155 PredCases.push_back(BBCases[i]);
1156 NewSuccessors.push_back(BBCases[i].Dest);
1158 BBCases[i].Value); // This constant is taken care of
1161 // If there are any constants vectored to BB that TI doesn't handle,
1162 // they must go to the default destination of TI.
1163 for (ConstantInt *I : PTIHandled) {
1164 if (PredHasWeights || SuccHasWeights)
1165 Weights.push_back(WeightsForHandled[I]);
1166 PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
1167 NewSuccessors.push_back(BBDefault);
1171 // Okay, at this point, we know which new successor Pred will get. Make
1172 // sure we update the number of entries in the PHI nodes for these
1174 for (BasicBlock *NewSuccessor : NewSuccessors)
1175 AddPredecessorToBlock(NewSuccessor, Pred, BB);
1177 Builder.SetInsertPoint(PTI);
1178 // Convert pointer to int before we switch.
1179 if (CV->getType()->isPointerTy()) {
1180 CV = Builder.CreatePtrToInt(CV, DL.getIntPtrType(CV->getType()),
1184 // Now that the successors are updated, create the new Switch instruction.
1186 Builder.CreateSwitch(CV, PredDefault, PredCases.size());
1187 NewSI->setDebugLoc(PTI->getDebugLoc());
1188 for (ValueEqualityComparisonCase &V : PredCases)
1189 NewSI->addCase(V.Value, V.Dest);
1191 if (PredHasWeights || SuccHasWeights) {
1192 // Halve the weights if any of them cannot fit in an uint32_t
1193 FitWeights(Weights);
1195 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
1197 setBranchWeights(NewSI, MDWeights);
1200 EraseTerminatorAndDCECond(PTI);
1202 // Okay, last check. If BB is still a successor of PSI, then we must
1203 // have an infinite loop case. If so, add an infinitely looping block
1204 // to handle the case to preserve the behavior of the code.
1205 BasicBlock *InfLoopBlock = nullptr;
1206 for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1207 if (NewSI->getSuccessor(i) == BB) {
1208 if (!InfLoopBlock) {
1209 // Insert it at the end of the function, because it's either code,
1210 // or it won't matter if it's hot. :)
1211 InfLoopBlock = BasicBlock::Create(BB->getContext(), "infloop",
1213 BranchInst::Create(InfLoopBlock, InfLoopBlock);
1215 NewSI->setSuccessor(i, InfLoopBlock);
1224 // If we would need to insert a select that uses the value of this invoke
1225 // (comments in HoistThenElseCodeToIf explain why we would need to do this), we
1226 // can't hoist the invoke, as there is nowhere to put the select in this case.
1227 static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
1228 Instruction *I1, Instruction *I2) {
1229 for (BasicBlock *Succ : successors(BB1)) {
1230 for (const PHINode &PN : Succ->phis()) {
1231 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1232 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1233 if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1241 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I);
1243 /// Given a conditional branch that goes to BB1 and BB2, hoist any common code
1244 /// in the two blocks up into the branch block. The caller of this function
1245 /// guarantees that BI's block dominates BB1 and BB2.
1246 static bool HoistThenElseCodeToIf(BranchInst *BI,
1247 const TargetTransformInfo &TTI) {
1248 // This does very trivial matching, with limited scanning, to find identical
1249 // instructions in the two blocks. In particular, we don't want to get into
1250 // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As
1251 // such, we currently just scan for obviously identical instructions in an
1253 BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
1254 BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
1256 BasicBlock::iterator BB1_Itr = BB1->begin();
1257 BasicBlock::iterator BB2_Itr = BB2->begin();
1259 Instruction *I1 = &*BB1_Itr++, *I2 = &*BB2_Itr++;
1260 // Skip debug info if it is not identical.
1261 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1262 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1263 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1264 while (isa<DbgInfoIntrinsic>(I1))
1266 while (isa<DbgInfoIntrinsic>(I2))
1269 if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
1270 (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
1273 BasicBlock *BIParent = BI->getParent();
1275 bool Changed = false;
1277 // If we are hoisting the terminator instruction, don't move one (making a
1278 // broken BB), instead clone it, and remove BI.
1279 if (I1->isTerminator())
1280 goto HoistTerminator;
1282 // If we're going to hoist a call, make sure that the two instructions we're
1283 // commoning/hoisting are both marked with musttail, or neither of them is
1284 // marked as such. Otherwise, we might end up in a situation where we hoist
1285 // from a block where the terminator is a `ret` to a block where the terminator
1286 // is a `br`, and `musttail` calls expect to be followed by a return.
1287 auto *C1 = dyn_cast<CallInst>(I1);
1288 auto *C2 = dyn_cast<CallInst>(I2);
1290 if (C1->isMustTailCall() != C2->isMustTailCall())
1293 if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2))
1296 if (isa<DbgInfoIntrinsic>(I1) || isa<DbgInfoIntrinsic>(I2)) {
1297 assert (isa<DbgInfoIntrinsic>(I1) && isa<DbgInfoIntrinsic>(I2));
1298 // The debug location is an integral part of a debug info intrinsic
1299 // and can't be separated from it or replaced. Instead of attempting
1300 // to merge locations, simply hoist both copies of the intrinsic.
1301 BIParent->getInstList().splice(BI->getIterator(),
1302 BB1->getInstList(), I1);
1303 BIParent->getInstList().splice(BI->getIterator(),
1304 BB2->getInstList(), I2);
1307 // For a normal instruction, we just move one to right before the branch,
1308 // then replace all uses of the other with the first. Finally, we remove
1309 // the now redundant second instruction.
1310 BIParent->getInstList().splice(BI->getIterator(),
1311 BB1->getInstList(), I1);
1312 if (!I2->use_empty())
1313 I2->replaceAllUsesWith(I1);
1315 unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
1316 LLVMContext::MD_range,
1317 LLVMContext::MD_fpmath,
1318 LLVMContext::MD_invariant_load,
1319 LLVMContext::MD_nonnull,
1320 LLVMContext::MD_invariant_group,
1321 LLVMContext::MD_align,
1322 LLVMContext::MD_dereferenceable,
1323 LLVMContext::MD_dereferenceable_or_null,
1324 LLVMContext::MD_mem_parallel_loop_access,
1325 LLVMContext::MD_access_group};
1326 combineMetadata(I1, I2, KnownIDs, true);
1328 // I1 and I2 are being combined into a single instruction. Its debug
1329 // location is the merged locations of the original instructions.
1330 I1->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1332 I2->eraseFromParent();
1338 // Skip debug info if it is not identical.
1339 DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1340 DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1341 if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1342 while (isa<DbgInfoIntrinsic>(I1))
1344 while (isa<DbgInfoIntrinsic>(I2))
1347 } while (I1->isIdenticalToWhenDefined(I2));
1352 // It may not be possible to hoist an invoke.
1353 if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
1356 for (BasicBlock *Succ : successors(BB1)) {
1357 for (PHINode &PN : Succ->phis()) {
1358 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1359 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1363 // Check for passingValueIsAlwaysUndefined here because we would rather
1364 // eliminate undefined control flow then converting it to a select.
1365 if (passingValueIsAlwaysUndefined(BB1V, &PN) ||
1366 passingValueIsAlwaysUndefined(BB2V, &PN))
1369 if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V))
1371 if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V))
1376 // Okay, it is safe to hoist the terminator.
1377 Instruction *NT = I1->clone();
1378 BIParent->getInstList().insert(BI->getIterator(), NT);
1379 if (!NT->getType()->isVoidTy()) {
1380 I1->replaceAllUsesWith(NT);
1381 I2->replaceAllUsesWith(NT);
1385 // Ensure terminator gets a debug location, even an unknown one, in case
1386 // it involves inlinable calls.
1387 NT->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1389 // PHIs created below will adopt NT's merged DebugLoc.
1390 IRBuilder<NoFolder> Builder(NT);
1392 // Hoisting one of the terminators from our successor is a great thing.
1393 // Unfortunately, the successors of the if/else blocks may have PHI nodes in
1394 // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
1395 // nodes, so we insert select instruction to compute the final result.
1396 std::map<std::pair<Value *, Value *>, SelectInst *> InsertedSelects;
1397 for (BasicBlock *Succ : successors(BB1)) {
1398 for (PHINode &PN : Succ->phis()) {
1399 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1400 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1404 // These values do not agree. Insert a select instruction before NT
1405 // that determines the right value.
1406 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1408 SI = cast<SelectInst>(
1409 Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
1410 BB1V->getName() + "." + BB2V->getName(), BI));
1412 // Make the PHI node use the select for all incoming values for BB1/BB2
1413 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1414 if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
1415 PN.setIncomingValue(i, SI);
1419 // Update any PHI nodes in our new successors.
1420 for (BasicBlock *Succ : successors(BB1))
1421 AddPredecessorToBlock(Succ, BIParent, BB1);
1423 EraseTerminatorAndDCECond(BI);
1427 // All instructions in Insts belong to different blocks that all unconditionally
1428 // branch to a common successor. Analyze each instruction and return true if it
1429 // would be possible to sink them into their successor, creating one common
1430 // instruction instead. For every value that would be required to be provided by
1431 // PHI node (because an operand varies in each input block), add to PHIOperands.
1432 static bool canSinkInstructions(
1433 ArrayRef<Instruction *> Insts,
1434 DenseMap<Instruction *, SmallVector<Value *, 4>> &PHIOperands) {
1435 // Prune out obviously bad instructions to move. Any non-store instruction
1436 // must have exactly one use, and we check later that use is by a single,
1437 // common PHI instruction in the successor.
1438 for (auto *I : Insts) {
1439 // These instructions may change or break semantics if moved.
1440 if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
1441 I->getType()->isTokenTy())
1444 // Conservatively return false if I is an inline-asm instruction. Sinking
1445 // and merging inline-asm instructions can potentially create arguments
1446 // that cannot satisfy the inline-asm constraints.
1447 if (const auto *C = dyn_cast<CallInst>(I))
1448 if (C->isInlineAsm())
1451 // Everything must have only one use too, apart from stores which
1453 if (!isa<StoreInst>(I) && !I->hasOneUse())
1457 const Instruction *I0 = Insts.front();
1458 for (auto *I : Insts)
1459 if (!I->isSameOperationAs(I0))
1462 // All instructions in Insts are known to be the same opcode. If they aren't
1463 // stores, check the only user of each is a PHI or in the same block as the
1464 // instruction, because if a user is in the same block as an instruction
1465 // we're contemplating sinking, it must already be determined to be sinkable.
1466 if (!isa<StoreInst>(I0)) {
1467 auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1468 auto *Succ = I0->getParent()->getTerminator()->getSuccessor(0);
1469 if (!all_of(Insts, [&PNUse,&Succ](const Instruction *I) -> bool {
1470 auto *U = cast<Instruction>(*I->user_begin());
1472 PNUse->getParent() == Succ &&
1473 PNUse->getIncomingValueForBlock(I->getParent()) == I) ||
1474 U->getParent() == I->getParent();
1479 // Because SROA can't handle speculating stores of selects, try not
1480 // to sink loads or stores of allocas when we'd have to create a PHI for
1481 // the address operand. Also, because it is likely that loads or stores
1482 // of allocas will disappear when Mem2Reg/SROA is run, don't sink them.
1483 // This can cause code churn which can have unintended consequences down
1484 // the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
1485 // FIXME: This is a workaround for a deficiency in SROA - see
1486 // https://llvm.org/bugs/show_bug.cgi?id=30188
1487 if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) {
1488 return isa<AllocaInst>(I->getOperand(1));
1491 if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) {
1492 return isa<AllocaInst>(I->getOperand(0));
1496 for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
1497 if (I0->getOperand(OI)->getType()->isTokenTy())
1498 // Don't touch any operand of token type.
1501 auto SameAsI0 = [&I0, OI](const Instruction *I) {
1502 assert(I->getNumOperands() == I0->getNumOperands());
1503 return I->getOperand(OI) == I0->getOperand(OI);
1505 if (!all_of(Insts, SameAsI0)) {
1506 if (!canReplaceOperandWithVariable(I0, OI))
1507 // We can't create a PHI from this GEP.
1509 // Don't create indirect calls! The called value is the final operand.
1510 if ((isa<CallInst>(I0) || isa<InvokeInst>(I0)) && OI == OE - 1) {
1511 // FIXME: if the call was *already* indirect, we should do this.
1514 for (auto *I : Insts)
1515 PHIOperands[I].push_back(I->getOperand(OI));
1521 // Assuming canSinkLastInstruction(Blocks) has returned true, sink the last
1522 // instruction of every block in Blocks to their common successor, commoning
1523 // into one instruction.
1524 static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
1525 auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
1527 // canSinkLastInstruction returning true guarantees that every block has at
1528 // least one non-terminator instruction.
1529 SmallVector<Instruction*,4> Insts;
1530 for (auto *BB : Blocks) {
1531 Instruction *I = BB->getTerminator();
1533 I = I->getPrevNode();
1534 } while (isa<DbgInfoIntrinsic>(I) && I != &BB->front());
1535 if (!isa<DbgInfoIntrinsic>(I))
1539 // The only checking we need to do now is that all users of all instructions
1540 // are the same PHI node. canSinkLastInstruction should have checked this but
1541 // it is slightly over-aggressive - it gets confused by commutative instructions
1542 // so double-check it here.
1543 Instruction *I0 = Insts.front();
1544 if (!isa<StoreInst>(I0)) {
1545 auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1546 if (!all_of(Insts, [&PNUse](const Instruction *I) -> bool {
1547 auto *U = cast<Instruction>(*I->user_begin());
1553 // We don't need to do any more checking here; canSinkLastInstruction should
1554 // have done it all for us.
1555 SmallVector<Value*, 4> NewOperands;
1556 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
1557 // This check is different to that in canSinkLastInstruction. There, we
1558 // cared about the global view once simplifycfg (and instcombine) have
1559 // completed - it takes into account PHIs that become trivially
1560 // simplifiable. However here we need a more local view; if an operand
1561 // differs we create a PHI and rely on instcombine to clean up the very
1562 // small mess we may make.
1563 bool NeedPHI = any_of(Insts, [&I0, O](const Instruction *I) {
1564 return I->getOperand(O) != I0->getOperand(O);
1567 NewOperands.push_back(I0->getOperand(O));
1571 // Create a new PHI in the successor block and populate it.
1572 auto *Op = I0->getOperand(O);
1573 assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
1574 auto *PN = PHINode::Create(Op->getType(), Insts.size(),
1575 Op->getName() + ".sink", &BBEnd->front());
1576 for (auto *I : Insts)
1577 PN->addIncoming(I->getOperand(O), I->getParent());
1578 NewOperands.push_back(PN);
1581 // Arbitrarily use I0 as the new "common" instruction; remap its operands
1582 // and move it to the start of the successor block.
1583 for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
1584 I0->getOperandUse(O).set(NewOperands[O]);
1585 I0->moveBefore(&*BBEnd->getFirstInsertionPt());
1587 // Update metadata and IR flags, and merge debug locations.
1588 for (auto *I : Insts)
1590 // The debug location for the "common" instruction is the merged locations
1591 // of all the commoned instructions. We start with the original location
1592 // of the "common" instruction and iteratively merge each location in the
1594 // This is an N-way merge, which will be inefficient if I0 is a CallInst.
1595 // However, as N-way merge for CallInst is rare, so we use simplified API
1596 // instead of using complex API for N-way merge.
1597 I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
1598 combineMetadataForCSE(I0, I, true);
1602 if (!isa<StoreInst>(I0)) {
1603 // canSinkLastInstruction checked that all instructions were used by
1604 // one and only one PHI node. Find that now, RAUW it to our common
1605 // instruction and nuke it.
1606 assert(I0->hasOneUse());
1607 auto *PN = cast<PHINode>(*I0->user_begin());
1608 PN->replaceAllUsesWith(I0);
1609 PN->eraseFromParent();
1612 // Finally nuke all instructions apart from the common instruction.
1613 for (auto *I : Insts)
1615 I->eraseFromParent();
1622 // LockstepReverseIterator - Iterates through instructions
1623 // in a set of blocks in reverse order from the first non-terminator.
1624 // For example (assume all blocks have size n):
1625 // LockstepReverseIterator I([B1, B2, B3]);
1626 // *I-- = [B1[n], B2[n], B3[n]];
1627 // *I-- = [B1[n-1], B2[n-1], B3[n-1]];
1628 // *I-- = [B1[n-2], B2[n-2], B3[n-2]];
1630 class LockstepReverseIterator {
1631 ArrayRef<BasicBlock*> Blocks;
1632 SmallVector<Instruction*,4> Insts;
1636 LockstepReverseIterator(ArrayRef<BasicBlock*> Blocks) : Blocks(Blocks) {
1643 for (auto *BB : Blocks) {
1644 Instruction *Inst = BB->getTerminator();
1645 for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1646 Inst = Inst->getPrevNode();
1648 // Block wasn't big enough.
1652 Insts.push_back(Inst);
1656 bool isValid() const {
1663 for (auto *&Inst : Insts) {
1664 for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1665 Inst = Inst->getPrevNode();
1666 // Already at beginning of block.
1674 ArrayRef<Instruction*> operator * () const {
1679 } // end anonymous namespace
1681 /// Check whether BB's predecessors end with unconditional branches. If it is
1682 /// true, sink any common code from the predecessors to BB.
1683 /// We also allow one predecessor to end with conditional branch (but no more
1685 static bool SinkCommonCodeFromPredecessors(BasicBlock *BB) {
1686 // We support two situations:
1687 // (1) all incoming arcs are unconditional
1688 // (2) one incoming arc is conditional
1690 // (2) is very common in switch defaults and
1691 // else-if patterns;
1694 // else if (b) f(2);
1707 // [end] has two unconditional predecessor arcs and one conditional. The
1708 // conditional refers to the implicit empty 'else' arc. This conditional
1709 // arc can also be caused by an empty default block in a switch.
1711 // In this case, we attempt to sink code from all *unconditional* arcs.
1712 // If we can sink instructions from these arcs (determined during the scan
1713 // phase below) we insert a common successor for all unconditional arcs and
1714 // connect that to [end], to enable sinking:
1727 SmallVector<BasicBlock*,4> UnconditionalPreds;
1728 Instruction *Cond = nullptr;
1729 for (auto *B : predecessors(BB)) {
1730 auto *T = B->getTerminator();
1731 if (isa<BranchInst>(T) && cast<BranchInst>(T)->isUnconditional())
1732 UnconditionalPreds.push_back(B);
1733 else if ((isa<BranchInst>(T) || isa<SwitchInst>(T)) && !Cond)
1738 if (UnconditionalPreds.size() < 2)
1741 bool Changed = false;
1742 // We take a two-step approach to tail sinking. First we scan from the end of
1743 // each block upwards in lockstep. If the n'th instruction from the end of each
1744 // block can be sunk, those instructions are added to ValuesToSink and we
1745 // carry on. If we can sink an instruction but need to PHI-merge some operands
1746 // (because they're not identical in each instruction) we add these to
1748 unsigned ScanIdx = 0;
1749 SmallPtrSet<Value*,4> InstructionsToSink;
1750 DenseMap<Instruction*, SmallVector<Value*,4>> PHIOperands;
1751 LockstepReverseIterator LRI(UnconditionalPreds);
1752 while (LRI.isValid() &&
1753 canSinkInstructions(*LRI, PHIOperands)) {
1754 LLVM_DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI)[0]
1756 InstructionsToSink.insert((*LRI).begin(), (*LRI).end());
1761 auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
1762 unsigned NumPHIdValues = 0;
1763 for (auto *I : *LRI)
1764 for (auto *V : PHIOperands[I])
1765 if (InstructionsToSink.count(V) == 0)
1767 LLVM_DEBUG(dbgs() << "SINK: #phid values: " << NumPHIdValues << "\n");
1768 unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.size();
1769 if ((NumPHIdValues % UnconditionalPreds.size()) != 0)
1772 return NumPHIInsts <= 1;
1775 if (ScanIdx > 0 && Cond) {
1776 // Check if we would actually sink anything first! This mutates the CFG and
1777 // adds an extra block. The goal in doing this is to allow instructions that
1778 // couldn't be sunk before to be sunk - obviously, speculatable instructions
1779 // (such as trunc, add) can be sunk and predicated already. So we check that
1780 // we're going to sink at least one non-speculatable instruction.
1783 bool Profitable = false;
1784 while (ProfitableToSinkInstruction(LRI) && Idx < ScanIdx) {
1785 if (!isSafeToSpeculativelyExecute((*LRI)[0])) {
1795 LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
1796 // We have a conditional edge and we're going to sink some instructions.
1797 // Insert a new block postdominating all blocks we're going to sink from.
1798 if (!SplitBlockPredecessors(BB, UnconditionalPreds, ".sink.split"))
1799 // Edges couldn't be split.
1804 // Now that we've analyzed all potential sinking candidates, perform the
1805 // actual sink. We iteratively sink the last non-terminator of the source
1806 // blocks into their common successor unless doing so would require too
1807 // many PHI instructions to be generated (currently only one PHI is allowed
1808 // per sunk instruction).
1810 // We can use InstructionsToSink to discount values needing PHI-merging that will
1811 // actually be sunk in a later iteration. This allows us to be more
1812 // aggressive in what we sink. This does allow a false positive where we
1813 // sink presuming a later value will also be sunk, but stop half way through
1814 // and never actually sink it which means we produce more PHIs than intended.
1815 // This is unlikely in practice though.
1816 for (unsigned SinkIdx = 0; SinkIdx != ScanIdx; ++SinkIdx) {
1817 LLVM_DEBUG(dbgs() << "SINK: Sink: "
1818 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
1821 // Because we've sunk every instruction in turn, the current instruction to
1822 // sink is always at index 0.
1824 if (!ProfitableToSinkInstruction(LRI)) {
1825 // Too many PHIs would be created.
1827 dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
1831 if (!sinkLastInstruction(UnconditionalPreds))
1839 /// Determine if we can hoist sink a sole store instruction out of a
1840 /// conditional block.
1842 /// We are looking for code like the following:
1844 /// store i32 %add, i32* %arrayidx2
1845 /// ... // No other stores or function calls (we could be calling a memory
1846 /// ... // function).
1847 /// %cmp = icmp ult %x, %y
1848 /// br i1 %cmp, label %EndBB, label %ThenBB
1850 /// store i32 %add5, i32* %arrayidx2
1854 /// We are going to transform this into:
1856 /// store i32 %add, i32* %arrayidx2
1858 /// %cmp = icmp ult %x, %y
1859 /// %add.add5 = select i1 %cmp, i32 %add, %add5
1860 /// store i32 %add.add5, i32* %arrayidx2
1863 /// \return The pointer to the value of the previous store if the store can be
1864 /// hoisted into the predecessor block. 0 otherwise.
1865 static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
1866 BasicBlock *StoreBB, BasicBlock *EndBB) {
1867 StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
1871 // Volatile or atomic.
1872 if (!StoreToHoist->isSimple())
1875 Value *StorePtr = StoreToHoist->getPointerOperand();
1877 // Look for a store to the same pointer in BrBB.
1878 unsigned MaxNumInstToLookAt = 9;
1879 for (Instruction &CurI : reverse(BrBB->instructionsWithoutDebug())) {
1880 if (!MaxNumInstToLookAt)
1882 --MaxNumInstToLookAt;
1884 // Could be calling an instruction that affects memory like free().
1885 if (CurI.mayHaveSideEffects() && !isa<StoreInst>(CurI))
1888 if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
1889 // Found the previous store make sure it stores to the same location.
1890 if (SI->getPointerOperand() == StorePtr)
1891 // Found the previous store, return its value operand.
1892 return SI->getValueOperand();
1893 return nullptr; // Unknown store.
1900 /// Speculate a conditional basic block flattening the CFG.
1902 /// Note that this is a very risky transform currently. Speculating
1903 /// instructions like this is most often not desirable. Instead, there is an MI
1904 /// pass which can do it with full awareness of the resource constraints.
1905 /// However, some cases are "obvious" and we should do directly. An example of
1906 /// this is speculating a single, reasonably cheap instruction.
1908 /// There is only one distinct advantage to flattening the CFG at the IR level:
1909 /// it makes very common but simplistic optimizations such as are common in
1910 /// instcombine and the DAG combiner more powerful by removing CFG edges and
1911 /// modeling their effects with easier to reason about SSA value graphs.
1914 /// An illustration of this transform is turning this IR:
1917 /// %cmp = icmp ult %x, %y
1918 /// br i1 %cmp, label %EndBB, label %ThenBB
1920 /// %sub = sub %x, %y
1923 /// %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
1930 /// %cmp = icmp ult %x, %y
1931 /// %sub = sub %x, %y
1932 /// %cond = select i1 %cmp, 0, %sub
1936 /// \returns true if the conditional block is removed.
1937 static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
1938 const TargetTransformInfo &TTI) {
1939 // Be conservative for now. FP select instruction can often be expensive.
1940 Value *BrCond = BI->getCondition();
1941 if (isa<FCmpInst>(BrCond))
1944 BasicBlock *BB = BI->getParent();
1945 BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
1947 // If ThenBB is actually on the false edge of the conditional branch, remember
1948 // to swap the select operands later.
1949 bool Invert = false;
1950 if (ThenBB != BI->getSuccessor(0)) {
1951 assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
1954 assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
1956 // Keep a count of how many times instructions are used within ThenBB when
1957 // they are candidates for sinking into ThenBB. Specifically:
1958 // - They are defined in BB, and
1959 // - They have no side effects, and
1960 // - All of their uses are in ThenBB.
1961 SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
1963 SmallVector<Instruction *, 4> SpeculatedDbgIntrinsics;
1965 unsigned SpeculationCost = 0;
1966 Value *SpeculatedStoreValue = nullptr;
1967 StoreInst *SpeculatedStore = nullptr;
1968 for (BasicBlock::iterator BBI = ThenBB->begin(),
1969 BBE = std::prev(ThenBB->end());
1970 BBI != BBE; ++BBI) {
1971 Instruction *I = &*BBI;
1973 if (isa<DbgInfoIntrinsic>(I)) {
1974 SpeculatedDbgIntrinsics.push_back(I);
1978 // Only speculatively execute a single instruction (not counting the
1979 // terminator) for now.
1981 if (SpeculationCost > 1)
1984 // Don't hoist the instruction if it's unsafe or expensive.
1985 if (!isSafeToSpeculativelyExecute(I) &&
1986 !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore(
1987 I, BB, ThenBB, EndBB))))
1989 if (!SpeculatedStoreValue &&
1990 ComputeSpeculationCost(I, TTI) >
1991 PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
1994 // Store the store speculation candidate.
1995 if (SpeculatedStoreValue)
1996 SpeculatedStore = cast<StoreInst>(I);
1998 // Do not hoist the instruction if any of its operands are defined but not
1999 // used in BB. The transformation will prevent the operand from
2000 // being sunk into the use block.
2001 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
2002 Instruction *OpI = dyn_cast<Instruction>(*i);
2003 if (!OpI || OpI->getParent() != BB || OpI->mayHaveSideEffects())
2004 continue; // Not a candidate for sinking.
2006 ++SinkCandidateUseCounts[OpI];
2010 // Consider any sink candidates which are only used in ThenBB as costs for
2011 // speculation. Note, while we iterate over a DenseMap here, we are summing
2012 // and so iteration order isn't significant.
2013 for (SmallDenseMap<Instruction *, unsigned, 4>::iterator
2014 I = SinkCandidateUseCounts.begin(),
2015 E = SinkCandidateUseCounts.end();
2017 if (I->first->hasNUses(I->second)) {
2019 if (SpeculationCost > 1)
2023 // Check that the PHI nodes can be converted to selects.
2024 bool HaveRewritablePHIs = false;
2025 for (PHINode &PN : EndBB->phis()) {
2026 Value *OrigV = PN.getIncomingValueForBlock(BB);
2027 Value *ThenV = PN.getIncomingValueForBlock(ThenBB);
2029 // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf.
2030 // Skip PHIs which are trivial.
2034 // Don't convert to selects if we could remove undefined behavior instead.
2035 if (passingValueIsAlwaysUndefined(OrigV, &PN) ||
2036 passingValueIsAlwaysUndefined(ThenV, &PN))
2039 HaveRewritablePHIs = true;
2040 ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
2041 ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
2042 if (!OrigCE && !ThenCE)
2043 continue; // Known safe and cheap.
2045 if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) ||
2046 (OrigCE && !isSafeToSpeculativelyExecute(OrigCE)))
2048 unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0;
2049 unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0;
2051 2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
2052 if (OrigCost + ThenCost > MaxCost)
2055 // Account for the cost of an unfolded ConstantExpr which could end up
2056 // getting expanded into Instructions.
2057 // FIXME: This doesn't account for how many operations are combined in the
2058 // constant expression.
2060 if (SpeculationCost > 1)
2064 // If there are no PHIs to process, bail early. This helps ensure idempotence
2066 if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue))
2069 // If we get here, we can hoist the instruction and if-convert.
2070 LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
2072 // Insert a select of the value of the speculated store.
2073 if (SpeculatedStoreValue) {
2074 IRBuilder<NoFolder> Builder(BI);
2075 Value *TrueV = SpeculatedStore->getValueOperand();
2076 Value *FalseV = SpeculatedStoreValue;
2078 std::swap(TrueV, FalseV);
2079 Value *S = Builder.CreateSelect(
2080 BrCond, TrueV, FalseV, "spec.store.select", BI);
2081 SpeculatedStore->setOperand(0, S);
2082 SpeculatedStore->applyMergedLocation(BI->getDebugLoc(),
2083 SpeculatedStore->getDebugLoc());
2086 // Metadata can be dependent on the condition we are hoisting above.
2087 // Conservatively strip all metadata on the instruction.
2088 for (auto &I : *ThenBB)
2089 I.dropUnknownNonDebugMetadata();
2091 // Hoist the instructions.
2092 BB->getInstList().splice(BI->getIterator(), ThenBB->getInstList(),
2093 ThenBB->begin(), std::prev(ThenBB->end()));
2095 // Insert selects and rewrite the PHI operands.
2096 IRBuilder<NoFolder> Builder(BI);
2097 for (PHINode &PN : EndBB->phis()) {
2098 unsigned OrigI = PN.getBasicBlockIndex(BB);
2099 unsigned ThenI = PN.getBasicBlockIndex(ThenBB);
2100 Value *OrigV = PN.getIncomingValue(OrigI);
2101 Value *ThenV = PN.getIncomingValue(ThenI);
2103 // Skip PHIs which are trivial.
2107 // Create a select whose true value is the speculatively executed value and
2108 // false value is the preexisting value. Swap them if the branch
2109 // destinations were inverted.
2110 Value *TrueV = ThenV, *FalseV = OrigV;
2112 std::swap(TrueV, FalseV);
2113 Value *V = Builder.CreateSelect(
2114 BrCond, TrueV, FalseV, "spec.select", BI);
2115 PN.setIncomingValue(OrigI, V);
2116 PN.setIncomingValue(ThenI, V);
2119 // Remove speculated dbg intrinsics.
2120 // FIXME: Is it possible to do this in a more elegant way? Moving/merging the
2121 // dbg value for the different flows and inserting it after the select.
2122 for (Instruction *I : SpeculatedDbgIntrinsics)
2123 I->eraseFromParent();
2129 /// Return true if we can thread a branch across this block.
2130 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
2133 for (Instruction &I : BB->instructionsWithoutDebug()) {
2135 return false; // Don't clone large BB's.
2138 // We can only support instructions that do not define values that are
2139 // live outside of the current basic block.
2140 for (User *U : I.users()) {
2141 Instruction *UI = cast<Instruction>(U);
2142 if (UI->getParent() != BB || isa<PHINode>(UI))
2146 // Looks ok, continue checking.
2152 /// If we have a conditional branch on a PHI node value that is defined in the
2153 /// same block as the branch and if any PHI entries are constants, thread edges
2154 /// corresponding to that entry to be branches to their ultimate destination.
2155 static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
2156 AssumptionCache *AC) {
2157 BasicBlock *BB = BI->getParent();
2158 PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
2159 // NOTE: we currently cannot transform this case if the PHI node is used
2160 // outside of the block.
2161 if (!PN || PN->getParent() != BB || !PN->hasOneUse())
2164 // Degenerate case of a single entry PHI.
2165 if (PN->getNumIncomingValues() == 1) {
2166 FoldSingleEntryPHINodes(PN->getParent());
2170 // Now we know that this block has multiple preds and two succs.
2171 if (!BlockIsSimpleEnoughToThreadThrough(BB))
2174 // Can't fold blocks that contain noduplicate or convergent calls.
2175 if (any_of(*BB, [](const Instruction &I) {
2176 const CallInst *CI = dyn_cast<CallInst>(&I);
2177 return CI && (CI->cannotDuplicate() || CI->isConvergent());
2181 // Okay, this is a simple enough basic block. See if any phi values are
2183 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2184 ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
2185 if (!CB || !CB->getType()->isIntegerTy(1))
2188 // Okay, we now know that all edges from PredBB should be revectored to
2189 // branch to RealDest.
2190 BasicBlock *PredBB = PN->getIncomingBlock(i);
2191 BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
2194 continue; // Skip self loops.
2195 // Skip if the predecessor's terminator is an indirect branch.
2196 if (isa<IndirectBrInst>(PredBB->getTerminator()))
2199 // The dest block might have PHI nodes, other predecessors and other
2200 // difficult cases. Instead of being smart about this, just insert a new
2201 // block that jumps to the destination block, effectively splitting
2202 // the edge we are about to create.
2203 BasicBlock *EdgeBB =
2204 BasicBlock::Create(BB->getContext(), RealDest->getName() + ".critedge",
2205 RealDest->getParent(), RealDest);
2206 BranchInst::Create(RealDest, EdgeBB);
2208 // Update PHI nodes.
2209 AddPredecessorToBlock(RealDest, EdgeBB, BB);
2211 // BB may have instructions that are being threaded over. Clone these
2212 // instructions into EdgeBB. We know that there will be no uses of the
2213 // cloned instructions outside of EdgeBB.
2214 BasicBlock::iterator InsertPt = EdgeBB->begin();
2215 DenseMap<Value *, Value *> TranslateMap; // Track translated values.
2216 for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
2217 if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
2218 TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
2221 // Clone the instruction.
2222 Instruction *N = BBI->clone();
2224 N->setName(BBI->getName() + ".c");
2226 // Update operands due to translation.
2227 for (User::op_iterator i = N->op_begin(), e = N->op_end(); i != e; ++i) {
2228 DenseMap<Value *, Value *>::iterator PI = TranslateMap.find(*i);
2229 if (PI != TranslateMap.end())
2233 // Check for trivial simplification.
2234 if (Value *V = SimplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
2235 if (!BBI->use_empty())
2236 TranslateMap[&*BBI] = V;
2237 if (!N->mayHaveSideEffects()) {
2238 N->deleteValue(); // Instruction folded away, don't need actual inst
2242 if (!BBI->use_empty())
2243 TranslateMap[&*BBI] = N;
2245 // Insert the new instruction into its new home.
2247 EdgeBB->getInstList().insert(InsertPt, N);
2249 // Register the new instruction with the assumption cache if necessary.
2250 if (auto *II = dyn_cast_or_null<IntrinsicInst>(N))
2251 if (II->getIntrinsicID() == Intrinsic::assume)
2252 AC->registerAssumption(II);
2255 // Loop over all of the edges from PredBB to BB, changing them to branch
2256 // to EdgeBB instead.
2257 Instruction *PredBBTI = PredBB->getTerminator();
2258 for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
2259 if (PredBBTI->getSuccessor(i) == BB) {
2260 BB->removePredecessor(PredBB);
2261 PredBBTI->setSuccessor(i, EdgeBB);
2264 // Recurse, simplifying any other constants.
2265 return FoldCondBranchOnPHI(BI, DL, AC) || true;
2271 /// Given a BB that starts with the specified two-entry PHI node,
2272 /// see if we can eliminate it.
2273 static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
2274 const DataLayout &DL) {
2275 // Ok, this is a two entry PHI node. Check to see if this is a simple "if
2276 // statement", which has a very simple dominance structure. Basically, we
2277 // are trying to find the condition that is being branched on, which
2278 // subsequently causes this merge to happen. We really want control
2279 // dependence information for this check, but simplifycfg can't keep it up
2280 // to date, and this catches most of the cases we care about anyway.
2281 BasicBlock *BB = PN->getParent();
2282 const Function *Fn = BB->getParent();
2283 if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
2286 BasicBlock *IfTrue, *IfFalse;
2287 Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
2289 // Don't bother if the branch will be constant folded trivially.
2290 isa<ConstantInt>(IfCond))
2293 // Okay, we found that we can merge this two-entry phi node into a select.
2294 // Doing so would require us to fold *all* two entry phi nodes in this block.
2295 // At some point this becomes non-profitable (particularly if the target
2296 // doesn't support cmov's). Only do this transformation if there are two or
2297 // fewer PHI nodes in this block.
2298 unsigned NumPhis = 0;
2299 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
2303 // Loop over the PHI's seeing if we can promote them all to select
2304 // instructions. While we are at it, keep track of the instructions
2305 // that need to be moved to the dominating block.
2306 SmallPtrSet<Instruction *, 4> AggressiveInsts;
2307 unsigned MaxCostVal0 = PHINodeFoldingThreshold,
2308 MaxCostVal1 = PHINodeFoldingThreshold;
2309 MaxCostVal0 *= TargetTransformInfo::TCC_Basic;
2310 MaxCostVal1 *= TargetTransformInfo::TCC_Basic;
2312 for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
2313 PHINode *PN = cast<PHINode>(II++);
2314 if (Value *V = SimplifyInstruction(PN, {DL, PN})) {
2315 PN->replaceAllUsesWith(V);
2316 PN->eraseFromParent();
2320 if (!DominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts,
2321 MaxCostVal0, TTI) ||
2322 !DominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts,
2327 // If we folded the first phi, PN dangles at this point. Refresh it. If
2328 // we ran out of PHIs then we simplified them all.
2329 PN = dyn_cast<PHINode>(BB->begin());
2333 // Don't fold i1 branches on PHIs which contain binary operators. These can
2334 // often be turned into switches and other things.
2335 if (PN->getType()->isIntegerTy(1) &&
2336 (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
2337 isa<BinaryOperator>(PN->getIncomingValue(1)) ||
2338 isa<BinaryOperator>(IfCond)))
2341 // If all PHI nodes are promotable, check to make sure that all instructions
2342 // in the predecessor blocks can be promoted as well. If not, we won't be able
2343 // to get rid of the control flow, so it's not worth promoting to select
2345 BasicBlock *DomBlock = nullptr;
2346 BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
2347 BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
2348 if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
2351 DomBlock = *pred_begin(IfBlock1);
2352 for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I)
2353 if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2354 // This is not an aggressive instruction that we can promote.
2355 // Because of this, we won't be able to get rid of the control flow, so
2356 // the xform is not worth it.
2361 if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
2364 DomBlock = *pred_begin(IfBlock2);
2365 for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I)
2366 if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2367 // This is not an aggressive instruction that we can promote.
2368 // Because of this, we won't be able to get rid of the control flow, so
2369 // the xform is not worth it.
2374 LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond
2375 << " T: " << IfTrue->getName()
2376 << " F: " << IfFalse->getName() << "\n");
2378 // If we can still promote the PHI nodes after this gauntlet of tests,
2379 // do all of the PHI's now.
2380 Instruction *InsertPt = DomBlock->getTerminator();
2381 IRBuilder<NoFolder> Builder(InsertPt);
2383 // Move all 'aggressive' instructions, which are defined in the
2384 // conditional parts of the if's up to the dominating block.
2386 hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock1);
2388 hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock2);
2390 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
2391 // Change the PHI node into a select instruction.
2392 Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
2393 Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
2395 Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt);
2396 PN->replaceAllUsesWith(Sel);
2398 PN->eraseFromParent();
2401 // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
2402 // has been flattened. Change DomBlock to jump directly to our new block to
2403 // avoid other simplifycfg's kicking in on the diamond.
2404 Instruction *OldTI = DomBlock->getTerminator();
2405 Builder.SetInsertPoint(OldTI);
2406 Builder.CreateBr(BB);
2407 OldTI->eraseFromParent();
2411 /// If we found a conditional branch that goes to two returning blocks,
2412 /// try to merge them together into one return,
2413 /// introducing a select if the return values disagree.
2414 static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
2415 IRBuilder<> &Builder) {
2416 assert(BI->isConditional() && "Must be a conditional branch");
2417 BasicBlock *TrueSucc = BI->getSuccessor(0);
2418 BasicBlock *FalseSucc = BI->getSuccessor(1);
2419 ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
2420 ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
2422 // Check to ensure both blocks are empty (just a return) or optionally empty
2423 // with PHI nodes. If there are other instructions, merging would cause extra
2424 // computation on one path or the other.
2425 if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
2427 if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
2430 Builder.SetInsertPoint(BI);
2431 // Okay, we found a branch that is going to two return nodes. If
2432 // there is no return value for this function, just change the
2433 // branch into a return.
2434 if (FalseRet->getNumOperands() == 0) {
2435 TrueSucc->removePredecessor(BI->getParent());
2436 FalseSucc->removePredecessor(BI->getParent());
2437 Builder.CreateRetVoid();
2438 EraseTerminatorAndDCECond(BI);
2442 // Otherwise, figure out what the true and false return values are
2443 // so we can insert a new select instruction.
2444 Value *TrueValue = TrueRet->getReturnValue();
2445 Value *FalseValue = FalseRet->getReturnValue();
2447 // Unwrap any PHI nodes in the return blocks.
2448 if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
2449 if (TVPN->getParent() == TrueSucc)
2450 TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
2451 if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
2452 if (FVPN->getParent() == FalseSucc)
2453 FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
2455 // In order for this transformation to be safe, we must be able to
2456 // unconditionally execute both operands to the return. This is
2457 // normally the case, but we could have a potentially-trapping
2458 // constant expression that prevents this transformation from being
2460 if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
2463 if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
2467 // Okay, we collected all the mapped values and checked them for sanity, and
2468 // defined to really do this transformation. First, update the CFG.
2469 TrueSucc->removePredecessor(BI->getParent());
2470 FalseSucc->removePredecessor(BI->getParent());
2472 // Insert select instructions where needed.
2473 Value *BrCond = BI->getCondition();
2475 // Insert a select if the results differ.
2476 if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
2477 } else if (isa<UndefValue>(TrueValue)) {
2478 TrueValue = FalseValue;
2481 Builder.CreateSelect(BrCond, TrueValue, FalseValue, "retval", BI);
2486 !TrueValue ? Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
2490 LLVM_DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
2491 << "\n " << *BI << "NewRet = " << *RI << "TRUEBLOCK: "
2492 << *TrueSucc << "FALSEBLOCK: " << *FalseSucc);
2494 EraseTerminatorAndDCECond(BI);
2499 /// Return true if the given instruction is available
2500 /// in its predecessor block. If yes, the instruction will be removed.
2501 static bool tryCSEWithPredecessor(Instruction *Inst, BasicBlock *PB) {
2502 if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2504 for (Instruction &I : *PB) {
2505 Instruction *PBI = &I;
2506 // Check whether Inst and PBI generate the same value.
2507 if (Inst->isIdenticalTo(PBI)) {
2508 Inst->replaceAllUsesWith(PBI);
2509 Inst->eraseFromParent();
2516 /// Return true if either PBI or BI has branch weight available, and store
2517 /// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
2518 /// not have branch weight, use 1:1 as its weight.
2519 static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
2520 uint64_t &PredTrueWeight,
2521 uint64_t &PredFalseWeight,
2522 uint64_t &SuccTrueWeight,
2523 uint64_t &SuccFalseWeight) {
2524 bool PredHasWeights =
2525 PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2526 bool SuccHasWeights =
2527 BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2528 if (PredHasWeights || SuccHasWeights) {
2529 if (!PredHasWeights)
2530 PredTrueWeight = PredFalseWeight = 1;
2531 if (!SuccHasWeights)
2532 SuccTrueWeight = SuccFalseWeight = 1;
2539 /// If this basic block is simple enough, and if a predecessor branches to us
2540 /// and one of our successors, fold the block into the predecessor and use
2541 /// logical operations to pick the right destination.
2542 bool llvm::FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold) {
2543 BasicBlock *BB = BI->getParent();
2545 const unsigned PredCount = pred_size(BB);
2547 Instruction *Cond = nullptr;
2548 if (BI->isConditional())
2549 Cond = dyn_cast<Instruction>(BI->getCondition());
2551 // For unconditional branch, check for a simple CFG pattern, where
2552 // BB has a single predecessor and BB's successor is also its predecessor's
2553 // successor. If such pattern exists, check for CSE between BB and its
2555 if (BasicBlock *PB = BB->getSinglePredecessor())
2556 if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2557 if (PBI->isConditional() &&
2558 (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
2559 BI->getSuccessor(0) == PBI->getSuccessor(1))) {
2560 for (auto I = BB->instructionsWithoutDebug().begin(),
2561 E = BB->instructionsWithoutDebug().end();
2563 Instruction *Curr = &*I++;
2564 if (isa<CmpInst>(Curr)) {
2568 // Quit if we can't remove this instruction.
2569 if (!tryCSEWithPredecessor(Curr, PB))
2578 if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2579 Cond->getParent() != BB || !Cond->hasOneUse())
2582 // Make sure the instruction after the condition is the cond branch.
2583 BasicBlock::iterator CondIt = ++Cond->getIterator();
2585 // Ignore dbg intrinsics.
2586 while (isa<DbgInfoIntrinsic>(CondIt))
2592 // Only allow this transformation if computing the condition doesn't involve
2593 // too many instructions and these involved instructions can be executed
2594 // unconditionally. We denote all involved instructions except the condition
2595 // as "bonus instructions", and only allow this transformation when the
2596 // number of the bonus instructions we'll need to create when cloning into
2597 // each predecessor does not exceed a certain threshold.
2598 unsigned NumBonusInsts = 0;
2599 for (auto I = BB->begin(); Cond != &*I; ++I) {
2600 // Ignore dbg intrinsics.
2601 if (isa<DbgInfoIntrinsic>(I))
2603 if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(&*I))
2605 // I has only one use and can be executed unconditionally.
2606 Instruction *User = dyn_cast<Instruction>(I->user_back());
2607 if (User == nullptr || User->getParent() != BB)
2609 // I is used in the same BB. Since BI uses Cond and doesn't have more slots
2610 // to use any other instruction, User must be an instruction between next(I)
2613 // Account for the cost of duplicating this instruction into each
2615 NumBonusInsts += PredCount;
2616 // Early exits once we reach the limit.
2617 if (NumBonusInsts > BonusInstThreshold)
2621 // Cond is known to be a compare or binary operator. Check to make sure that
2622 // neither operand is a potentially-trapping constant expression.
2623 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
2626 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
2630 // Finally, don't infinitely unroll conditional loops.
2631 BasicBlock *TrueDest = BI->getSuccessor(0);
2632 BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
2633 if (TrueDest == BB || FalseDest == BB)
2636 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2637 BasicBlock *PredBlock = *PI;
2638 BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
2640 // Check that we have two conditional branches. If there is a PHI node in
2641 // the common successor, verify that the same value flows in from both
2643 SmallVector<PHINode *, 4> PHIs;
2644 if (!PBI || PBI->isUnconditional() ||
2645 (BI->isConditional() && !SafeToMergeTerminators(BI, PBI)) ||
2646 (!BI->isConditional() &&
2647 !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
2650 // Determine if the two branches share a common destination.
2651 Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
2652 bool InvertPredCond = false;
2654 if (BI->isConditional()) {
2655 if (PBI->getSuccessor(0) == TrueDest) {
2656 Opc = Instruction::Or;
2657 } else if (PBI->getSuccessor(1) == FalseDest) {
2658 Opc = Instruction::And;
2659 } else if (PBI->getSuccessor(0) == FalseDest) {
2660 Opc = Instruction::And;
2661 InvertPredCond = true;
2662 } else if (PBI->getSuccessor(1) == TrueDest) {
2663 Opc = Instruction::Or;
2664 InvertPredCond = true;
2669 if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
2673 LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
2674 IRBuilder<> Builder(PBI);
2676 // If we need to invert the condition in the pred block to match, do so now.
2677 if (InvertPredCond) {
2678 Value *NewCond = PBI->getCondition();
2680 if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
2681 CmpInst *CI = cast<CmpInst>(NewCond);
2682 CI->setPredicate(CI->getInversePredicate());
2685 Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not");
2688 PBI->setCondition(NewCond);
2689 PBI->swapSuccessors();
2692 // If we have bonus instructions, clone them into the predecessor block.
2693 // Note that there may be multiple predecessor blocks, so we cannot move
2694 // bonus instructions to a predecessor block.
2695 ValueToValueMapTy VMap; // maps original values to cloned values
2696 // We already make sure Cond is the last instruction before BI. Therefore,
2697 // all instructions before Cond other than DbgInfoIntrinsic are bonus
2699 for (auto BonusInst = BB->begin(); Cond != &*BonusInst; ++BonusInst) {
2700 if (isa<DbgInfoIntrinsic>(BonusInst))
2702 Instruction *NewBonusInst = BonusInst->clone();
2703 RemapInstruction(NewBonusInst, VMap,
2704 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2705 VMap[&*BonusInst] = NewBonusInst;
2707 // If we moved a load, we cannot any longer claim any knowledge about
2708 // its potential value. The previous information might have been valid
2709 // only given the branch precondition.
2710 // For an analogous reason, we must also drop all the metadata whose
2711 // semantics we don't understand.
2712 NewBonusInst->dropUnknownNonDebugMetadata();
2714 PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
2715 NewBonusInst->takeName(&*BonusInst);
2716 BonusInst->setName(BonusInst->getName() + ".old");
2719 // Clone Cond into the predecessor basic block, and or/and the
2720 // two conditions together.
2721 Instruction *CondInPred = Cond->clone();
2722 RemapInstruction(CondInPred, VMap,
2723 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2724 PredBlock->getInstList().insert(PBI->getIterator(), CondInPred);
2725 CondInPred->takeName(Cond);
2726 Cond->setName(CondInPred->getName() + ".old");
2728 if (BI->isConditional()) {
2729 Instruction *NewCond = cast<Instruction>(
2730 Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
2731 PBI->setCondition(NewCond);
2733 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2735 extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
2736 SuccTrueWeight, SuccFalseWeight);
2737 SmallVector<uint64_t, 8> NewWeights;
2739 if (PBI->getSuccessor(0) == BB) {
2741 // PBI: br i1 %x, BB, FalseDest
2742 // BI: br i1 %y, TrueDest, FalseDest
2743 // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
2744 NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
2745 // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
2746 // TrueWeight for PBI * FalseWeight for BI.
2747 // We assume that total weights of a BranchInst can fit into 32 bits.
2748 // Therefore, we will not have overflow using 64-bit arithmetic.
2749 NewWeights.push_back(PredFalseWeight *
2750 (SuccFalseWeight + SuccTrueWeight) +
2751 PredTrueWeight * SuccFalseWeight);
2753 AddPredecessorToBlock(TrueDest, PredBlock, BB);
2754 PBI->setSuccessor(0, TrueDest);
2756 if (PBI->getSuccessor(1) == BB) {
2758 // PBI: br i1 %x, TrueDest, BB
2759 // BI: br i1 %y, TrueDest, FalseDest
2760 // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
2761 // FalseWeight for PBI * TrueWeight for BI.
2762 NewWeights.push_back(PredTrueWeight *
2763 (SuccFalseWeight + SuccTrueWeight) +
2764 PredFalseWeight * SuccTrueWeight);
2765 // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
2766 NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
2768 AddPredecessorToBlock(FalseDest, PredBlock, BB);
2769 PBI->setSuccessor(1, FalseDest);
2771 if (NewWeights.size() == 2) {
2772 // Halve the weights if any of them cannot fit in an uint32_t
2773 FitWeights(NewWeights);
2775 SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),
2777 setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
2779 PBI->setMetadata(LLVMContext::MD_prof, nullptr);
2781 // Update PHI nodes in the common successors.
2782 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
2783 ConstantInt *PBI_C = cast<ConstantInt>(
2784 PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
2785 assert(PBI_C->getType()->isIntegerTy(1));
2786 Instruction *MergedCond = nullptr;
2787 if (PBI->getSuccessor(0) == TrueDest) {
2788 // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
2789 // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
2790 // is false: !PBI_Cond and BI_Value
2791 Instruction *NotCond = cast<Instruction>(
2792 Builder.CreateNot(PBI->getCondition(), "not.cond"));
2793 MergedCond = cast<Instruction>(
2794 Builder.CreateBinOp(Instruction::And, NotCond, CondInPred,
2797 MergedCond = cast<Instruction>(Builder.CreateBinOp(
2798 Instruction::Or, PBI->getCondition(), MergedCond, "or.cond"));
2800 // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
2801 // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
2802 // is false: PBI_Cond and BI_Value
2803 MergedCond = cast<Instruction>(Builder.CreateBinOp(
2804 Instruction::And, PBI->getCondition(), CondInPred, "and.cond"));
2805 if (PBI_C->isOne()) {
2806 Instruction *NotCond = cast<Instruction>(
2807 Builder.CreateNot(PBI->getCondition(), "not.cond"));
2808 MergedCond = cast<Instruction>(Builder.CreateBinOp(
2809 Instruction::Or, NotCond, MergedCond, "or.cond"));
2813 PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
2816 // Change PBI from Conditional to Unconditional.
2817 BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
2818 EraseTerminatorAndDCECond(PBI);
2822 // If BI was a loop latch, it may have had associated loop metadata.
2823 // We need to copy it to the new latch, that is, PBI.
2824 if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
2825 PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
2827 // TODO: If BB is reachable from all paths through PredBlock, then we
2828 // could replace PBI's branch probabilities with BI's.
2830 // Copy any debug value intrinsics into the end of PredBlock.
2831 for (Instruction &I : *BB)
2832 if (isa<DbgInfoIntrinsic>(I))
2833 I.clone()->insertBefore(PBI);
2840 // If there is only one store in BB1 and BB2, return it, otherwise return
2842 static StoreInst *findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2) {
2843 StoreInst *S = nullptr;
2844 for (auto *BB : {BB1, BB2}) {
2848 if (auto *SI = dyn_cast<StoreInst>(&I)) {
2850 // Multiple stores seen.
2859 static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
2860 Value *AlternativeV = nullptr) {
2861 // PHI is going to be a PHI node that allows the value V that is defined in
2862 // BB to be referenced in BB's only successor.
2864 // If AlternativeV is nullptr, the only value we care about in PHI is V. It
2865 // doesn't matter to us what the other operand is (it'll never get used). We
2866 // could just create a new PHI with an undef incoming value, but that could
2867 // increase register pressure if EarlyCSE/InstCombine can't fold it with some
2868 // other PHI. So here we directly look for some PHI in BB's successor with V
2869 // as an incoming operand. If we find one, we use it, else we create a new
2872 // If AlternativeV is not nullptr, we care about both incoming values in PHI.
2873 // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
2874 // where OtherBB is the single other predecessor of BB's only successor.
2875 PHINode *PHI = nullptr;
2876 BasicBlock *Succ = BB->getSingleSuccessor();
2878 for (auto I = Succ->begin(); isa<PHINode>(I); ++I)
2879 if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
2880 PHI = cast<PHINode>(I);
2884 assert(Succ->hasNPredecessors(2));
2885 auto PredI = pred_begin(Succ);
2886 BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
2887 if (PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
2894 // If V is not an instruction defined in BB, just return it.
2895 if (!AlternativeV &&
2896 (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB))
2899 PHI = PHINode::Create(V->getType(), 2, "simplifycfg.merge", &Succ->front());
2900 PHI->addIncoming(V, BB);
2901 for (BasicBlock *PredBB : predecessors(Succ))
2904 AlternativeV ? AlternativeV : UndefValue::get(V->getType()), PredBB);
2908 static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
2909 BasicBlock *QTB, BasicBlock *QFB,
2910 BasicBlock *PostBB, Value *Address,
2911 bool InvertPCond, bool InvertQCond,
2912 const DataLayout &DL) {
2913 auto IsaBitcastOfPointerType = [](const Instruction &I) {
2914 return Operator::getOpcode(&I) == Instruction::BitCast &&
2915 I.getType()->isPointerTy();
2918 // If we're not in aggressive mode, we only optimize if we have some
2919 // confidence that by optimizing we'll allow P and/or Q to be if-converted.
2920 auto IsWorthwhile = [&](BasicBlock *BB) {
2923 // Heuristic: if the block can be if-converted/phi-folded and the
2924 // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
2925 // thread this store.
2927 for (auto &I : BB->instructionsWithoutDebug()) {
2928 // Cheap instructions viable for folding.
2929 if (isa<BinaryOperator>(I) || isa<GetElementPtrInst>(I) ||
2932 // Free instructions.
2933 else if (I.isTerminator() || IsaBitcastOfPointerType(I))
2938 // The store we want to merge is counted in N, so add 1 to make sure
2939 // we're counting the instructions that would be left.
2940 return N <= (PHINodeFoldingThreshold + 1);
2943 if (!MergeCondStoresAggressively &&
2944 (!IsWorthwhile(PTB) || !IsWorthwhile(PFB) || !IsWorthwhile(QTB) ||
2945 !IsWorthwhile(QFB)))
2948 // For every pointer, there must be exactly two stores, one coming from
2949 // PTB or PFB, and the other from QTB or QFB. We don't support more than one
2950 // store (to any address) in PTB,PFB or QTB,QFB.
2951 // FIXME: We could relax this restriction with a bit more work and performance
2953 StoreInst *PStore = findUniqueStoreInBlocks(PTB, PFB);
2954 StoreInst *QStore = findUniqueStoreInBlocks(QTB, QFB);
2955 if (!PStore || !QStore)
2958 // Now check the stores are compatible.
2959 if (!QStore->isUnordered() || !PStore->isUnordered())
2962 // Check that sinking the store won't cause program behavior changes. Sinking
2963 // the store out of the Q blocks won't change any behavior as we're sinking
2964 // from a block to its unconditional successor. But we're moving a store from
2965 // the P blocks down through the middle block (QBI) and past both QFB and QTB.
2966 // So we need to check that there are no aliasing loads or stores in
2967 // QBI, QTB and QFB. We also need to check there are no conflicting memory
2968 // operations between PStore and the end of its parent block.
2970 // The ideal way to do this is to query AliasAnalysis, but we don't
2971 // preserve AA currently so that is dangerous. Be super safe and just
2972 // check there are no other memory operations at all.
2973 for (auto &I : *QFB->getSinglePredecessor())
2974 if (I.mayReadOrWriteMemory())
2976 for (auto &I : *QFB)
2977 if (&I != QStore && I.mayReadOrWriteMemory())
2980 for (auto &I : *QTB)
2981 if (&I != QStore && I.mayReadOrWriteMemory())
2983 for (auto I = BasicBlock::iterator(PStore), E = PStore->getParent()->end();
2985 if (&*I != PStore && I->mayReadOrWriteMemory())
2988 // If PostBB has more than two predecessors, we need to split it so we can
2990 if (std::next(pred_begin(PostBB), 2) != pred_end(PostBB)) {
2991 // We know that QFB's only successor is PostBB. And QFB has a single
2992 // predecessor. If QTB exists, then its only successor is also PostBB.
2993 // If QTB does not exist, then QFB's only predecessor has a conditional
2994 // branch to QFB and PostBB.
2995 BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
2996 BasicBlock *NewBB = SplitBlockPredecessors(PostBB, { QFB, TruePred},
3003 // OK, we're going to sink the stores to PostBB. The store has to be
3004 // conditional though, so first create the predicate.
3005 Value *PCond = cast<BranchInst>(PFB->getSinglePredecessor()->getTerminator())
3007 Value *QCond = cast<BranchInst>(QFB->getSinglePredecessor()->getTerminator())
3010 Value *PPHI = ensureValueAvailableInSuccessor(PStore->getValueOperand(),
3011 PStore->getParent());
3012 Value *QPHI = ensureValueAvailableInSuccessor(QStore->getValueOperand(),
3013 QStore->getParent(), PPHI);
3015 IRBuilder<> QB(&*PostBB->getFirstInsertionPt());
3017 Value *PPred = PStore->getParent() == PTB ? PCond : QB.CreateNot(PCond);
3018 Value *QPred = QStore->getParent() == QTB ? QCond : QB.CreateNot(QCond);
3021 PPred = QB.CreateNot(PPred);
3023 QPred = QB.CreateNot(QPred);
3024 Value *CombinedPred = QB.CreateOr(PPred, QPred);
3027 SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(), false);
3028 QB.SetInsertPoint(T);
3029 StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI, Address));
3031 PStore->getAAMetadata(AAMD, /*Merge=*/false);
3032 PStore->getAAMetadata(AAMD, /*Merge=*/true);
3033 SI->setAAMetadata(AAMD);
3034 unsigned PAlignment = PStore->getAlignment();
3035 unsigned QAlignment = QStore->getAlignment();
3036 unsigned TypeAlignment =
3037 DL.getABITypeAlignment(SI->getValueOperand()->getType());
3038 unsigned MinAlignment;
3039 unsigned MaxAlignment;
3040 std::tie(MinAlignment, MaxAlignment) = std::minmax(PAlignment, QAlignment);
3041 // Choose the minimum alignment. If we could prove both stores execute, we
3042 // could use biggest one. In this case, though, we only know that one of the
3043 // stores executes. And we don't know it's safe to take the alignment from a
3044 // store that doesn't execute.
3045 if (MinAlignment != 0) {
3046 // Choose the minimum of all non-zero alignments.
3047 SI->setAlignment(MinAlignment);
3048 } else if (MaxAlignment != 0) {
3049 // Choose the minimal alignment between the non-zero alignment and the ABI
3050 // default alignment for the type of the stored value.
3051 SI->setAlignment(std::min(MaxAlignment, TypeAlignment));
3053 // If both alignments are zero, use ABI default alignment for the type of
3054 // the stored value.
3055 SI->setAlignment(TypeAlignment);
3058 QStore->eraseFromParent();
3059 PStore->eraseFromParent();
3064 static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI,
3065 const DataLayout &DL) {
3066 // The intention here is to find diamonds or triangles (see below) where each
3067 // conditional block contains a store to the same address. Both of these
3068 // stores are conditional, so they can't be unconditionally sunk. But it may
3069 // be profitable to speculatively sink the stores into one merged store at the
3070 // end, and predicate the merged store on the union of the two conditions of
3073 // This can reduce the number of stores executed if both of the conditions are
3074 // true, and can allow the blocks to become small enough to be if-converted.
3075 // This optimization will also chain, so that ladders of test-and-set
3076 // sequences can be if-converted away.
3078 // We only deal with simple diamonds or triangles:
3080 // PBI or PBI or a combination of the two
3090 // We model triangles as a type of diamond with a nullptr "true" block.
3091 // Triangles are canonicalized so that the fallthrough edge is represented by
3092 // a true condition, as in the diagram above.
3093 BasicBlock *PTB = PBI->getSuccessor(0);
3094 BasicBlock *PFB = PBI->getSuccessor(1);
3095 BasicBlock *QTB = QBI->getSuccessor(0);
3096 BasicBlock *QFB = QBI->getSuccessor(1);
3097 BasicBlock *PostBB = QFB->getSingleSuccessor();
3099 // Make sure we have a good guess for PostBB. If QTB's only successor is
3100 // QFB, then QFB is a better PostBB.
3101 if (QTB->getSingleSuccessor() == QFB)
3104 // If we couldn't find a good PostBB, stop.
3108 bool InvertPCond = false, InvertQCond = false;
3109 // Canonicalize fallthroughs to the true branches.
3110 if (PFB == QBI->getParent()) {
3111 std::swap(PFB, PTB);
3114 if (QFB == PostBB) {
3115 std::swap(QFB, QTB);
3119 // From this point on we can assume PTB or QTB may be fallthroughs but PFB
3120 // and QFB may not. Model fallthroughs as a nullptr block.
3121 if (PTB == QBI->getParent())
3126 // Legality bailouts. We must have at least the non-fallthrough blocks and
3127 // the post-dominating block, and the non-fallthroughs must only have one
3129 auto HasOnePredAndOneSucc = [](BasicBlock *BB, BasicBlock *P, BasicBlock *S) {
3130 return BB->getSinglePredecessor() == P && BB->getSingleSuccessor() == S;
3132 if (!HasOnePredAndOneSucc(PFB, PBI->getParent(), QBI->getParent()) ||
3133 !HasOnePredAndOneSucc(QFB, QBI->getParent(), PostBB))
3135 if ((PTB && !HasOnePredAndOneSucc(PTB, PBI->getParent(), QBI->getParent())) ||
3136 (QTB && !HasOnePredAndOneSucc(QTB, QBI->getParent(), PostBB)))
3138 if (!QBI->getParent()->hasNUses(2))
3141 // OK, this is a sequence of two diamonds or triangles.
3142 // Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
3143 SmallPtrSet<Value *, 4> PStoreAddresses, QStoreAddresses;
3144 for (auto *BB : {PTB, PFB}) {
3148 if (StoreInst *SI = dyn_cast<StoreInst>(&I))
3149 PStoreAddresses.insert(SI->getPointerOperand());
3151 for (auto *BB : {QTB, QFB}) {
3155 if (StoreInst *SI = dyn_cast<StoreInst>(&I))
3156 QStoreAddresses.insert(SI->getPointerOperand());
3159 set_intersect(PStoreAddresses, QStoreAddresses);
3160 // set_intersect mutates PStoreAddresses in place. Rename it here to make it
3161 // clear what it contains.
3162 auto &CommonAddresses = PStoreAddresses;
3164 bool Changed = false;
3165 for (auto *Address : CommonAddresses)
3166 Changed |= mergeConditionalStoreToAddress(
3167 PTB, PFB, QTB, QFB, PostBB, Address, InvertPCond, InvertQCond, DL);
3171 /// If we have a conditional branch as a predecessor of another block,
3172 /// this function tries to simplify it. We know
3173 /// that PBI and BI are both conditional branches, and BI is in one of the
3174 /// successor blocks of PBI - PBI branches to BI.
3175 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
3176 const DataLayout &DL) {
3177 assert(PBI->isConditional() && BI->isConditional());
3178 BasicBlock *BB = BI->getParent();
3180 // If this block ends with a branch instruction, and if there is a
3181 // predecessor that ends on a branch of the same condition, make
3182 // this conditional branch redundant.
3183 if (PBI->getCondition() == BI->getCondition() &&
3184 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3185 // Okay, the outcome of this conditional branch is statically
3186 // knowable. If this block had a single pred, handle specially.
3187 if (BB->getSinglePredecessor()) {
3188 // Turn this into a branch on constant.
3189 bool CondIsTrue = PBI->getSuccessor(0) == BB;
3191 ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue));
3192 return true; // Nuke the branch on constant.
3195 // Otherwise, if there are multiple predecessors, insert a PHI that merges
3196 // in the constant and simplify the block result. Subsequent passes of
3197 // simplifycfg will thread the block.
3198 if (BlockIsSimpleEnoughToThreadThrough(BB)) {
3199 pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
3200 PHINode *NewPN = PHINode::Create(
3201 Type::getInt1Ty(BB->getContext()), std::distance(PB, PE),
3202 BI->getCondition()->getName() + ".pr", &BB->front());
3203 // Okay, we're going to insert the PHI node. Since PBI is not the only
3204 // predecessor, compute the PHI'd conditional value for all of the preds.
3205 // Any predecessor where the condition is not computable we keep symbolic.
3206 for (pred_iterator PI = PB; PI != PE; ++PI) {
3207 BasicBlock *P = *PI;
3208 if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && PBI != BI &&
3209 PBI->isConditional() && PBI->getCondition() == BI->getCondition() &&
3210 PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3211 bool CondIsTrue = PBI->getSuccessor(0) == BB;
3213 ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue),
3216 NewPN->addIncoming(BI->getCondition(), P);
3220 BI->setCondition(NewPN);
3225 if (auto *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
3229 // If both branches are conditional and both contain stores to the same
3230 // address, remove the stores from the conditionals and create a conditional
3231 // merged store at the end.
3232 if (MergeCondStores && mergeConditionalStores(PBI, BI, DL))
3235 // If this is a conditional branch in an empty block, and if any
3236 // predecessors are a conditional branch to one of our destinations,
3237 // fold the conditions into logical ops and one cond br.
3239 // Ignore dbg intrinsics.
3240 if (&*BB->instructionsWithoutDebug().begin() != BI)
3244 if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
3247 } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
3250 } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
3253 } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
3260 // Check to make sure that the other destination of this branch
3261 // isn't BB itself. If so, this is an infinite loop that will
3262 // keep getting unwound.
3263 if (PBI->getSuccessor(PBIOp) == BB)
3266 // Do not perform this transformation if it would require
3267 // insertion of a large number of select instructions. For targets
3268 // without predication/cmovs, this is a big pessimization.
3270 // Also do not perform this transformation if any phi node in the common
3271 // destination block can trap when reached by BB or PBB (PR17073). In that
3272 // case, it would be unsafe to hoist the operation into a select instruction.
3274 BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
3275 unsigned NumPhis = 0;
3276 for (BasicBlock::iterator II = CommonDest->begin(); isa<PHINode>(II);
3278 if (NumPhis > 2) // Disable this xform.
3281 PHINode *PN = cast<PHINode>(II);
3282 Value *BIV = PN->getIncomingValueForBlock(BB);
3283 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV))
3287 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
3288 Value *PBIV = PN->getIncomingValue(PBBIdx);
3289 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV))
3294 // Finally, if everything is ok, fold the branches to logical ops.
3295 BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
3297 LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
3298 << "AND: " << *BI->getParent());
3300 // If OtherDest *is* BB, then BB is a basic block with a single conditional
3301 // branch in it, where one edge (OtherDest) goes back to itself but the other
3302 // exits. We don't *know* that the program avoids the infinite loop
3303 // (even though that seems likely). If we do this xform naively, we'll end up
3304 // recursively unpeeling the loop. Since we know that (after the xform is
3305 // done) that the block *is* infinite if reached, we just make it an obviously
3306 // infinite loop with no cond branch.
3307 if (OtherDest == BB) {
3308 // Insert it at the end of the function, because it's either code,
3309 // or it won't matter if it's hot. :)
3310 BasicBlock *InfLoopBlock =
3311 BasicBlock::Create(BB->getContext(), "infloop", BB->getParent());
3312 BranchInst::Create(InfLoopBlock, InfLoopBlock);
3313 OtherDest = InfLoopBlock;
3316 LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3318 // BI may have other predecessors. Because of this, we leave
3319 // it alone, but modify PBI.
3321 // Make sure we get to CommonDest on True&True directions.
3322 Value *PBICond = PBI->getCondition();
3323 IRBuilder<NoFolder> Builder(PBI);
3325 PBICond = Builder.CreateNot(PBICond, PBICond->getName() + ".not");
3327 Value *BICond = BI->getCondition();
3329 BICond = Builder.CreateNot(BICond, BICond->getName() + ".not");
3331 // Merge the conditions.
3332 Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
3334 // Modify PBI to branch on the new condition to the new dests.
3335 PBI->setCondition(Cond);
3336 PBI->setSuccessor(0, CommonDest);
3337 PBI->setSuccessor(1, OtherDest);
3339 // Update branch weight for PBI.
3340 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3341 uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
3343 extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3344 SuccTrueWeight, SuccFalseWeight);
3346 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3347 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3348 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3349 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3350 // The weight to CommonDest should be PredCommon * SuccTotal +
3351 // PredOther * SuccCommon.
3352 // The weight to OtherDest should be PredOther * SuccOther.
3353 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
3354 PredOther * SuccCommon,
3355 PredOther * SuccOther};
3356 // Halve the weights if any of them cannot fit in an uint32_t
3357 FitWeights(NewWeights);
3359 setBranchWeights(PBI, NewWeights[0], NewWeights[1]);
3362 // OtherDest may have phi nodes. If so, add an entry from PBI's
3363 // block that are identical to the entries for BI's block.
3364 AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
3366 // We know that the CommonDest already had an edge from PBI to
3367 // it. If it has PHIs though, the PHIs may have different
3368 // entries for BB and PBI's BB. If so, insert a select to make
3370 for (PHINode &PN : CommonDest->phis()) {
3371 Value *BIV = PN.getIncomingValueForBlock(BB);
3372 unsigned PBBIdx = PN.getBasicBlockIndex(PBI->getParent());
3373 Value *PBIV = PN.getIncomingValue(PBBIdx);
3375 // Insert a select in PBI to pick the right value.
3376 SelectInst *NV = cast<SelectInst>(
3377 Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
3378 PN.setIncomingValue(PBBIdx, NV);
3379 // Although the select has the same condition as PBI, the original branch
3380 // weights for PBI do not apply to the new select because the select's
3381 // 'logical' edges are incoming edges of the phi that is eliminated, not
3382 // the outgoing edges of PBI.
3384 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3385 uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3386 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3387 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3388 // The weight to PredCommonDest should be PredCommon * SuccTotal.
3389 // The weight to PredOtherDest should be PredOther * SuccCommon.
3390 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
3391 PredOther * SuccCommon};
3393 FitWeights(NewWeights);
3395 setBranchWeights(NV, NewWeights[0], NewWeights[1]);
3400 LLVM_DEBUG(dbgs() << "INTO: " << *PBI->getParent());
3401 LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3403 // This basic block is probably dead. We know it has at least
3404 // one fewer predecessor.
3408 // Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
3409 // true or to FalseBB if Cond is false.
3410 // Takes care of updating the successors and removing the old terminator.
3411 // Also makes sure not to introduce new successors by assuming that edges to
3412 // non-successor TrueBBs and FalseBBs aren't reachable.
3413 static bool SimplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond,
3414 BasicBlock *TrueBB, BasicBlock *FalseBB,
3415 uint32_t TrueWeight,
3416 uint32_t FalseWeight) {
3417 // Remove any superfluous successor edges from the CFG.
3418 // First, figure out which successors to preserve.
3419 // If TrueBB and FalseBB are equal, only try to preserve one copy of that
3421 BasicBlock *KeepEdge1 = TrueBB;
3422 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
3424 // Then remove the rest.
3425 for (BasicBlock *Succ : successors(OldTerm)) {
3426 // Make sure only to keep exactly one copy of each edge.
3427 if (Succ == KeepEdge1)
3428 KeepEdge1 = nullptr;
3429 else if (Succ == KeepEdge2)
3430 KeepEdge2 = nullptr;
3432 Succ->removePredecessor(OldTerm->getParent(),
3433 /*DontDeleteUselessPHIs=*/true);
3436 IRBuilder<> Builder(OldTerm);
3437 Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
3439 // Insert an appropriate new terminator.
3440 if (!KeepEdge1 && !KeepEdge2) {
3441 if (TrueBB == FalseBB)
3442 // We were only looking for one successor, and it was present.
3443 // Create an unconditional branch to it.
3444 Builder.CreateBr(TrueBB);
3446 // We found both of the successors we were looking for.
3447 // Create a conditional branch sharing the condition of the select.
3448 BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
3449 if (TrueWeight != FalseWeight)
3450 setBranchWeights(NewBI, TrueWeight, FalseWeight);
3452 } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
3453 // Neither of the selected blocks were successors, so this
3454 // terminator must be unreachable.
3455 new UnreachableInst(OldTerm->getContext(), OldTerm);
3457 // One of the selected values was a successor, but the other wasn't.
3458 // Insert an unconditional branch to the one that was found;
3459 // the edge to the one that wasn't must be unreachable.
3461 // Only TrueBB was found.
3462 Builder.CreateBr(TrueBB);
3464 // Only FalseBB was found.
3465 Builder.CreateBr(FalseBB);
3468 EraseTerminatorAndDCECond(OldTerm);
3473 // (switch (select cond, X, Y)) on constant X, Y
3474 // with a branch - conditional if X and Y lead to distinct BBs,
3475 // unconditional otherwise.
3476 static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) {
3477 // Check for constant integer values in the select.
3478 ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
3479 ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
3480 if (!TrueVal || !FalseVal)
3483 // Find the relevant condition and destinations.
3484 Value *Condition = Select->getCondition();
3485 BasicBlock *TrueBB = SI->findCaseValue(TrueVal)->getCaseSuccessor();
3486 BasicBlock *FalseBB = SI->findCaseValue(FalseVal)->getCaseSuccessor();
3488 // Get weight for TrueBB and FalseBB.
3489 uint32_t TrueWeight = 0, FalseWeight = 0;
3490 SmallVector<uint64_t, 8> Weights;
3491 bool HasWeights = HasBranchWeights(SI);
3493 GetBranchWeights(SI, Weights);
3494 if (Weights.size() == 1 + SI->getNumCases()) {
3496 (uint32_t)Weights[SI->findCaseValue(TrueVal)->getSuccessorIndex()];
3498 (uint32_t)Weights[SI->findCaseValue(FalseVal)->getSuccessorIndex()];
3502 // Perform the actual simplification.
3503 return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
3508 // (indirectbr (select cond, blockaddress(@fn, BlockA),
3509 // blockaddress(@fn, BlockB)))
3511 // (br cond, BlockA, BlockB).
3512 static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) {
3513 // Check that both operands of the select are block addresses.
3514 BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
3515 BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
3519 // Extract the actual blocks.
3520 BasicBlock *TrueBB = TBA->getBasicBlock();
3521 BasicBlock *FalseBB = FBA->getBasicBlock();
3523 // Perform the actual simplification.
3524 return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB, 0,
3528 /// This is called when we find an icmp instruction
3529 /// (a seteq/setne with a constant) as the only instruction in a
3530 /// block that ends with an uncond branch. We are looking for a very specific
3531 /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
3532 /// this case, we merge the first two "or's of icmp" into a switch, but then the
3533 /// default value goes to an uncond block with a seteq in it, we get something
3536 /// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
3538 /// %tmp = icmp eq i8 %A, 92
3541 /// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
3543 /// We prefer to split the edge to 'end' so that there is a true/false entry to
3544 /// the PHI, merging the third icmp into the switch.
3545 bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
3546 ICmpInst *ICI, IRBuilder<> &Builder) {
3547 BasicBlock *BB = ICI->getParent();
3549 // If the block has any PHIs in it or the icmp has multiple uses, it is too
3551 if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse())
3554 Value *V = ICI->getOperand(0);
3555 ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
3557 // The pattern we're looking for is where our only predecessor is a switch on
3558 // 'V' and this block is the default case for the switch. In this case we can
3559 // fold the compared value into the switch to simplify things.
3560 BasicBlock *Pred = BB->getSinglePredecessor();
3561 if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
3564 SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
3565 if (SI->getCondition() != V)
3568 // If BB is reachable on a non-default case, then we simply know the value of
3569 // V in this block. Substitute it and constant fold the icmp instruction
3571 if (SI->getDefaultDest() != BB) {
3572 ConstantInt *VVal = SI->findCaseDest(BB);
3573 assert(VVal && "Should have a unique destination value");
3574 ICI->setOperand(0, VVal);
3576 if (Value *V = SimplifyInstruction(ICI, {DL, ICI})) {
3577 ICI->replaceAllUsesWith(V);
3578 ICI->eraseFromParent();
3580 // BB is now empty, so it is likely to simplify away.
3581 return requestResimplify();
3584 // Ok, the block is reachable from the default dest. If the constant we're
3585 // comparing exists in one of the other edges, then we can constant fold ICI
3587 if (SI->findCaseValue(Cst) != SI->case_default()) {
3589 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
3590 V = ConstantInt::getFalse(BB->getContext());
3592 V = ConstantInt::getTrue(BB->getContext());
3594 ICI->replaceAllUsesWith(V);
3595 ICI->eraseFromParent();
3596 // BB is now empty, so it is likely to simplify away.
3597 return requestResimplify();
3600 // The use of the icmp has to be in the 'end' block, by the only PHI node in
3602 BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
3603 PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
3604 if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
3605 isa<PHINode>(++BasicBlock::iterator(PHIUse)))
3608 // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
3610 Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
3611 Constant *NewCst = ConstantInt::getFalse(BB->getContext());
3613 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
3614 std::swap(DefaultCst, NewCst);
3616 // Replace ICI (which is used by the PHI for the default value) with true or
3617 // false depending on if it is EQ or NE.
3618 ICI->replaceAllUsesWith(DefaultCst);
3619 ICI->eraseFromParent();
3621 // Okay, the switch goes to this block on a default value. Add an edge from
3622 // the switch to the merge point on the compared value.
3624 BasicBlock::Create(BB->getContext(), "switch.edge", BB->getParent(), BB);
3625 SmallVector<uint64_t, 8> Weights;
3626 bool HasWeights = HasBranchWeights(SI);
3628 GetBranchWeights(SI, Weights);
3629 if (Weights.size() == 1 + SI->getNumCases()) {
3630 // Split weight for default case to case for "Cst".
3631 Weights[0] = (Weights[0] + 1) >> 1;
3632 Weights.push_back(Weights[0]);
3634 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
3635 setBranchWeights(SI, MDWeights);
3638 SI->addCase(Cst, NewBB);
3640 // NewBB branches to the phi block, add the uncond branch and the phi entry.
3641 Builder.SetInsertPoint(NewBB);
3642 Builder.SetCurrentDebugLocation(SI->getDebugLoc());
3643 Builder.CreateBr(SuccBlock);
3644 PHIUse->addIncoming(NewCst, NewBB);
3648 /// The specified branch is a conditional branch.
3649 /// Check to see if it is branching on an or/and chain of icmp instructions, and
3650 /// fold it into a switch instruction if so.
3651 static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder,
3652 const DataLayout &DL) {
3653 Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
3657 // Change br (X == 0 | X == 1), T, F into a switch instruction.
3658 // If this is a bunch of seteq's or'd together, or if it's a bunch of
3659 // 'setne's and'ed together, collect them.
3661 // Try to gather values from a chain of and/or to be turned into a switch
3662 ConstantComparesGatherer ConstantCompare(Cond, DL);
3663 // Unpack the result
3664 SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
3665 Value *CompVal = ConstantCompare.CompValue;
3666 unsigned UsedICmps = ConstantCompare.UsedICmps;
3667 Value *ExtraCase = ConstantCompare.Extra;
3669 // If we didn't have a multiply compared value, fail.
3673 // Avoid turning single icmps into a switch.
3677 bool TrueWhenEqual = (Cond->getOpcode() == Instruction::Or);
3679 // There might be duplicate constants in the list, which the switch
3680 // instruction can't handle, remove them now.
3681 array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
3682 Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
3684 // If Extra was used, we require at least two switch values to do the
3685 // transformation. A switch with one value is just a conditional branch.
3686 if (ExtraCase && Values.size() < 2)
3689 // TODO: Preserve branch weight metadata, similarly to how
3690 // FoldValueComparisonIntoPredecessors preserves it.
3692 // Figure out which block is which destination.
3693 BasicBlock *DefaultBB = BI->getSuccessor(1);
3694 BasicBlock *EdgeBB = BI->getSuccessor(0);
3696 std::swap(DefaultBB, EdgeBB);
3698 BasicBlock *BB = BI->getParent();
3700 LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
3701 << " cases into SWITCH. BB is:\n"
3704 // If there are any extra values that couldn't be folded into the switch
3705 // then we evaluate them with an explicit branch first. Split the block
3706 // right before the condbr to handle it.
3709 BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
3710 // Remove the uncond branch added to the old block.
3711 Instruction *OldTI = BB->getTerminator();
3712 Builder.SetInsertPoint(OldTI);
3715 Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
3717 Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
3719 OldTI->eraseFromParent();
3721 // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
3722 // for the edge we just added.
3723 AddPredecessorToBlock(EdgeBB, BB, NewBB);
3725 LLVM_DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
3726 << "\nEXTRABB = " << *BB);
3730 Builder.SetInsertPoint(BI);
3731 // Convert pointer to int before we switch.
3732 if (CompVal->getType()->isPointerTy()) {
3733 CompVal = Builder.CreatePtrToInt(
3734 CompVal, DL.getIntPtrType(CompVal->getType()), "magicptr");
3737 // Create the new switch instruction now.
3738 SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
3740 // Add all of the 'cases' to the switch instruction.
3741 for (unsigned i = 0, e = Values.size(); i != e; ++i)
3742 New->addCase(Values[i], EdgeBB);
3744 // We added edges from PI to the EdgeBB. As such, if there were any
3745 // PHI nodes in EdgeBB, they need entries to be added corresponding to
3746 // the number of edges added.
3747 for (BasicBlock::iterator BBI = EdgeBB->begin(); isa<PHINode>(BBI); ++BBI) {
3748 PHINode *PN = cast<PHINode>(BBI);
3749 Value *InVal = PN->getIncomingValueForBlock(BB);
3750 for (unsigned i = 0, e = Values.size() - 1; i != e; ++i)
3751 PN->addIncoming(InVal, BB);
3754 // Erase the old branch instruction.
3755 EraseTerminatorAndDCECond(BI);
3757 LLVM_DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
3761 bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
3762 if (isa<PHINode>(RI->getValue()))
3763 return SimplifyCommonResume(RI);
3764 else if (isa<LandingPadInst>(RI->getParent()->getFirstNonPHI()) &&
3765 RI->getValue() == RI->getParent()->getFirstNonPHI())
3766 // The resume must unwind the exception that caused control to branch here.
3767 return SimplifySingleResume(RI);
3772 // Simplify resume that is shared by several landing pads (phi of landing pad).
3773 bool SimplifyCFGOpt::SimplifyCommonResume(ResumeInst *RI) {
3774 BasicBlock *BB = RI->getParent();
3776 // Check that there are no other instructions except for debug intrinsics
3777 // between the phi of landing pads (RI->getValue()) and resume instruction.
3778 BasicBlock::iterator I = cast<Instruction>(RI->getValue())->getIterator(),
3779 E = RI->getIterator();
3781 if (!isa<DbgInfoIntrinsic>(I))
3784 SmallSetVector<BasicBlock *, 4> TrivialUnwindBlocks;
3785 auto *PhiLPInst = cast<PHINode>(RI->getValue());
3787 // Check incoming blocks to see if any of them are trivial.
3788 for (unsigned Idx = 0, End = PhiLPInst->getNumIncomingValues(); Idx != End;
3790 auto *IncomingBB = PhiLPInst->getIncomingBlock(Idx);
3791 auto *IncomingValue = PhiLPInst->getIncomingValue(Idx);
3793 // If the block has other successors, we can not delete it because
3794 // it has other dependents.
3795 if (IncomingBB->getUniqueSuccessor() != BB)
3798 auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHI());
3799 // Not the landing pad that caused the control to branch here.
3800 if (IncomingValue != LandingPad)
3803 bool isTrivial = true;
3805 I = IncomingBB->getFirstNonPHI()->getIterator();
3806 E = IncomingBB->getTerminator()->getIterator();
3808 if (!isa<DbgInfoIntrinsic>(I)) {
3814 TrivialUnwindBlocks.insert(IncomingBB);
3817 // If no trivial unwind blocks, don't do any simplifications.
3818 if (TrivialUnwindBlocks.empty())
3821 // Turn all invokes that unwind here into calls.
3822 for (auto *TrivialBB : TrivialUnwindBlocks) {
3823 // Blocks that will be simplified should be removed from the phi node.
3824 // Note there could be multiple edges to the resume block, and we need
3825 // to remove them all.
3826 while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
3827 BB->removePredecessor(TrivialBB, true);
3829 for (pred_iterator PI = pred_begin(TrivialBB), PE = pred_end(TrivialBB);
3831 BasicBlock *Pred = *PI++;
3832 removeUnwindEdge(Pred);
3835 // In each SimplifyCFG run, only the current processed block can be erased.
3836 // Otherwise, it will break the iteration of SimplifyCFG pass. So instead
3837 // of erasing TrivialBB, we only remove the branch to the common resume
3838 // block so that we can later erase the resume block since it has no
3840 TrivialBB->getTerminator()->eraseFromParent();
3841 new UnreachableInst(RI->getContext(), TrivialBB);
3844 // Delete the resume block if all its predecessors have been removed.
3846 BB->eraseFromParent();
3848 return !TrivialUnwindBlocks.empty();
3851 // Simplify resume that is only used by a single (non-phi) landing pad.
3852 bool SimplifyCFGOpt::SimplifySingleResume(ResumeInst *RI) {
3853 BasicBlock *BB = RI->getParent();
3854 LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI());
3855 assert(RI->getValue() == LPInst &&
3856 "Resume must unwind the exception that caused control to here");
3858 // Check that there are no other instructions except for debug intrinsics.
3859 BasicBlock::iterator I = LPInst->getIterator(), E = RI->getIterator();
3861 if (!isa<DbgInfoIntrinsic>(I))
3864 // Turn all invokes that unwind here into calls and delete the basic block.
3865 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
3866 BasicBlock *Pred = *PI++;
3867 removeUnwindEdge(Pred);
3870 // The landingpad is now unreachable. Zap it.
3872 LoopHeaders->erase(BB);
3873 BB->eraseFromParent();
3877 static bool removeEmptyCleanup(CleanupReturnInst *RI) {
3878 // If this is a trivial cleanup pad that executes no instructions, it can be
3879 // eliminated. If the cleanup pad continues to the caller, any predecessor
3880 // that is an EH pad will be updated to continue to the caller and any
3881 // predecessor that terminates with an invoke instruction will have its invoke
3882 // instruction converted to a call instruction. If the cleanup pad being
3883 // simplified does not continue to the caller, each predecessor will be
3884 // updated to continue to the unwind destination of the cleanup pad being
3886 BasicBlock *BB = RI->getParent();
3887 CleanupPadInst *CPInst = RI->getCleanupPad();
3888 if (CPInst->getParent() != BB)
3889 // This isn't an empty cleanup.
3892 // We cannot kill the pad if it has multiple uses. This typically arises
3893 // from unreachable basic blocks.
3894 if (!CPInst->hasOneUse())
3897 // Check that there are no other instructions except for benign intrinsics.
3898 BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator();
3900 auto *II = dyn_cast<IntrinsicInst>(I);
3904 Intrinsic::ID IntrinsicID = II->getIntrinsicID();
3905 switch (IntrinsicID) {
3906 case Intrinsic::dbg_declare:
3907 case Intrinsic::dbg_value:
3908 case Intrinsic::dbg_label:
3909 case Intrinsic::lifetime_end:
3916 // If the cleanup return we are simplifying unwinds to the caller, this will
3917 // set UnwindDest to nullptr.
3918 BasicBlock *UnwindDest = RI->getUnwindDest();
3919 Instruction *DestEHPad = UnwindDest ? UnwindDest->getFirstNonPHI() : nullptr;
3921 // We're about to remove BB from the control flow. Before we do, sink any
3922 // PHINodes into the unwind destination. Doing this before changing the
3923 // control flow avoids some potentially slow checks, since we can currently
3924 // be certain that UnwindDest and BB have no common predecessors (since they
3925 // are both EH pads).
3927 // First, go through the PHI nodes in UnwindDest and update any nodes that
3928 // reference the block we are removing
3929 for (BasicBlock::iterator I = UnwindDest->begin(),
3930 IE = DestEHPad->getIterator();
3932 PHINode *DestPN = cast<PHINode>(I);
3934 int Idx = DestPN->getBasicBlockIndex(BB);
3935 // Since BB unwinds to UnwindDest, it has to be in the PHI node.
3937 // This PHI node has an incoming value that corresponds to a control
3938 // path through the cleanup pad we are removing. If the incoming
3939 // value is in the cleanup pad, it must be a PHINode (because we
3940 // verified above that the block is otherwise empty). Otherwise, the
3941 // value is either a constant or a value that dominates the cleanup
3942 // pad being removed.
3944 // Because BB and UnwindDest are both EH pads, all of their
3945 // predecessors must unwind to these blocks, and since no instruction
3946 // can have multiple unwind destinations, there will be no overlap in
3947 // incoming blocks between SrcPN and DestPN.
3948 Value *SrcVal = DestPN->getIncomingValue(Idx);
3949 PHINode *SrcPN = dyn_cast<PHINode>(SrcVal);
3951 // Remove the entry for the block we are deleting.
3952 DestPN->removeIncomingValue(Idx, false);
3954 if (SrcPN && SrcPN->getParent() == BB) {
3955 // If the incoming value was a PHI node in the cleanup pad we are
3956 // removing, we need to merge that PHI node's incoming values into
3958 for (unsigned SrcIdx = 0, SrcE = SrcPN->getNumIncomingValues();
3959 SrcIdx != SrcE; ++SrcIdx) {
3960 DestPN->addIncoming(SrcPN->getIncomingValue(SrcIdx),
3961 SrcPN->getIncomingBlock(SrcIdx));
3964 // Otherwise, the incoming value came from above BB and
3965 // so we can just reuse it. We must associate all of BB's
3966 // predecessors with this value.
3967 for (auto *pred : predecessors(BB)) {
3968 DestPN->addIncoming(SrcVal, pred);
3973 // Sink any remaining PHI nodes directly into UnwindDest.
3974 Instruction *InsertPt = DestEHPad;
3975 for (BasicBlock::iterator I = BB->begin(),
3976 IE = BB->getFirstNonPHI()->getIterator();
3978 // The iterator must be incremented here because the instructions are
3979 // being moved to another block.
3980 PHINode *PN = cast<PHINode>(I++);
3981 if (PN->use_empty())
3982 // If the PHI node has no uses, just leave it. It will be erased
3983 // when we erase BB below.
3986 // Otherwise, sink this PHI node into UnwindDest.
3987 // Any predecessors to UnwindDest which are not already represented
3988 // must be back edges which inherit the value from the path through
3989 // BB. In this case, the PHI value must reference itself.
3990 for (auto *pred : predecessors(UnwindDest))
3992 PN->addIncoming(PN, pred);
3993 PN->moveBefore(InsertPt);
3997 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
3998 // The iterator must be updated here because we are removing this pred.
3999 BasicBlock *PredBB = *PI++;
4000 if (UnwindDest == nullptr) {
4001 removeUnwindEdge(PredBB);
4003 Instruction *TI = PredBB->getTerminator();
4004 TI->replaceUsesOfWith(BB, UnwindDest);
4008 // The cleanup pad is now unreachable. Zap it.
4009 BB->eraseFromParent();
4013 // Try to merge two cleanuppads together.
4014 static bool mergeCleanupPad(CleanupReturnInst *RI) {
4015 // Skip any cleanuprets which unwind to caller, there is nothing to merge
4017 BasicBlock *UnwindDest = RI->getUnwindDest();
4021 // This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
4022 // be safe to merge without code duplication.
4023 if (UnwindDest->getSinglePredecessor() != RI->getParent())
4026 // Verify that our cleanuppad's unwind destination is another cleanuppad.
4027 auto *SuccessorCleanupPad = dyn_cast<CleanupPadInst>(&UnwindDest->front());
4028 if (!SuccessorCleanupPad)
4031 CleanupPadInst *PredecessorCleanupPad = RI->getCleanupPad();
4032 // Replace any uses of the successor cleanupad with the predecessor pad
4033 // The only cleanuppad uses should be this cleanupret, it's cleanupret and
4034 // funclet bundle operands.
4035 SuccessorCleanupPad->replaceAllUsesWith(PredecessorCleanupPad);
4036 // Remove the old cleanuppad.
4037 SuccessorCleanupPad->eraseFromParent();
4038 // Now, we simply replace the cleanupret with a branch to the unwind
4040 BranchInst::Create(UnwindDest, RI->getParent());
4041 RI->eraseFromParent();
4046 bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) {
4047 // It is possible to transiantly have an undef cleanuppad operand because we
4048 // have deleted some, but not all, dead blocks.
4049 // Eventually, this block will be deleted.
4050 if (isa<UndefValue>(RI->getOperand(0)))
4053 if (mergeCleanupPad(RI))
4056 if (removeEmptyCleanup(RI))
4062 bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
4063 BasicBlock *BB = RI->getParent();
4064 if (!BB->getFirstNonPHIOrDbg()->isTerminator())
4067 // Find predecessors that end with branches.
4068 SmallVector<BasicBlock *, 8> UncondBranchPreds;
4069 SmallVector<BranchInst *, 8> CondBranchPreds;
4070 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
4071 BasicBlock *P = *PI;
4072 Instruction *PTI = P->getTerminator();
4073 if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
4074 if (BI->isUnconditional())
4075 UncondBranchPreds.push_back(P);
4077 CondBranchPreds.push_back(BI);
4081 // If we found some, do the transformation!
4082 if (!UncondBranchPreds.empty() && DupRet) {
4083 while (!UncondBranchPreds.empty()) {
4084 BasicBlock *Pred = UncondBranchPreds.pop_back_val();
4085 LLVM_DEBUG(dbgs() << "FOLDING: " << *BB
4086 << "INTO UNCOND BRANCH PRED: " << *Pred);
4087 (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
4090 // If we eliminated all predecessors of the block, delete the block now.
4091 if (pred_empty(BB)) {
4092 // We know there are no successors, so just nuke the block.
4094 LoopHeaders->erase(BB);
4095 BB->eraseFromParent();
4101 // Check out all of the conditional branches going to this return
4102 // instruction. If any of them just select between returns, change the
4103 // branch itself into a select/return pair.
4104 while (!CondBranchPreds.empty()) {
4105 BranchInst *BI = CondBranchPreds.pop_back_val();
4107 // Check to see if the non-BB successor is also a return block.
4108 if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
4109 isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
4110 SimplifyCondBranchToTwoReturns(BI, Builder))
4116 bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
4117 BasicBlock *BB = UI->getParent();
4119 bool Changed = false;
4121 // If there are any instructions immediately before the unreachable that can
4122 // be removed, do so.
4123 while (UI->getIterator() != BB->begin()) {
4124 BasicBlock::iterator BBI = UI->getIterator();
4126 // Do not delete instructions that can have side effects which might cause
4127 // the unreachable to not be reachable; specifically, calls and volatile
4128 // operations may have this effect.
4129 if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
4132 if (BBI->mayHaveSideEffects()) {
4133 if (auto *SI = dyn_cast<StoreInst>(BBI)) {
4134 if (SI->isVolatile())
4136 } else if (auto *LI = dyn_cast<LoadInst>(BBI)) {
4137 if (LI->isVolatile())
4139 } else if (auto *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
4140 if (RMWI->isVolatile())
4142 } else if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
4143 if (CXI->isVolatile())
4145 } else if (isa<CatchPadInst>(BBI)) {
4146 // A catchpad may invoke exception object constructors and such, which
4147 // in some languages can be arbitrary code, so be conservative by
4149 // For CoreCLR, it just involves a type test, so can be removed.
4150 if (classifyEHPersonality(BB->getParent()->getPersonalityFn()) !=
4151 EHPersonality::CoreCLR)
4153 } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
4154 !isa<LandingPadInst>(BBI)) {
4157 // Note that deleting LandingPad's here is in fact okay, although it
4158 // involves a bit of subtle reasoning. If this inst is a LandingPad,
4159 // all the predecessors of this block will be the unwind edges of Invokes,
4160 // and we can therefore guarantee this block will be erased.
4163 // Delete this instruction (any uses are guaranteed to be dead)
4164 if (!BBI->use_empty())
4165 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
4166 BBI->eraseFromParent();
4170 // If the unreachable instruction is the first in the block, take a gander
4171 // at all of the predecessors of this instruction, and simplify them.
4172 if (&BB->front() != UI)
4175 SmallVector<BasicBlock *, 8> Preds(pred_begin(BB), pred_end(BB));
4176 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
4177 Instruction *TI = Preds[i]->getTerminator();
4178 IRBuilder<> Builder(TI);
4179 if (auto *BI = dyn_cast<BranchInst>(TI)) {
4180 if (BI->isUnconditional()) {
4181 if (BI->getSuccessor(0) == BB) {
4182 new UnreachableInst(TI->getContext(), TI);
4183 TI->eraseFromParent();
4187 if (BI->getSuccessor(0) == BB) {
4188 Builder.CreateBr(BI->getSuccessor(1));
4189 EraseTerminatorAndDCECond(BI);
4190 } else if (BI->getSuccessor(1) == BB) {
4191 Builder.CreateBr(BI->getSuccessor(0));
4192 EraseTerminatorAndDCECond(BI);
4196 } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
4197 for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
4198 if (i->getCaseSuccessor() != BB) {
4202 BB->removePredecessor(SI->getParent());
4203 i = SI->removeCase(i);
4207 } else if (auto *II = dyn_cast<InvokeInst>(TI)) {
4208 if (II->getUnwindDest() == BB) {
4209 removeUnwindEdge(TI->getParent());
4212 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4213 if (CSI->getUnwindDest() == BB) {
4214 removeUnwindEdge(TI->getParent());
4219 for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
4220 E = CSI->handler_end();
4223 CSI->removeHandler(I);
4229 if (CSI->getNumHandlers() == 0) {
4230 BasicBlock *CatchSwitchBB = CSI->getParent();
4231 if (CSI->hasUnwindDest()) {
4232 // Redirect preds to the unwind dest
4233 CatchSwitchBB->replaceAllUsesWith(CSI->getUnwindDest());
4235 // Rewrite all preds to unwind to caller (or from invoke to call).
4236 SmallVector<BasicBlock *, 8> EHPreds(predecessors(CatchSwitchBB));
4237 for (BasicBlock *EHPred : EHPreds)
4238 removeUnwindEdge(EHPred);
4240 // The catchswitch is no longer reachable.
4241 new UnreachableInst(CSI->getContext(), CSI);
4242 CSI->eraseFromParent();
4245 } else if (isa<CleanupReturnInst>(TI)) {
4246 new UnreachableInst(TI->getContext(), TI);
4247 TI->eraseFromParent();
4252 // If this block is now dead, remove it.
4253 if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
4254 // We know there are no successors, so just nuke the block.
4256 LoopHeaders->erase(BB);
4257 BB->eraseFromParent();
4264 static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
4265 assert(Cases.size() >= 1);
4267 array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
4268 for (size_t I = 1, E = Cases.size(); I != E; ++I) {
4269 if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
4275 /// Turn a switch with two reachable destinations into an integer range
4276 /// comparison and branch.
4277 static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) {
4278 assert(SI->getNumCases() > 1 && "Degenerate switch?");
4281 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4283 // Partition the cases into two sets with different destinations.
4284 BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
4285 BasicBlock *DestB = nullptr;
4286 SmallVector<ConstantInt *, 16> CasesA;
4287 SmallVector<ConstantInt *, 16> CasesB;
4289 for (auto Case : SI->cases()) {
4290 BasicBlock *Dest = Case.getCaseSuccessor();
4293 if (Dest == DestA) {
4294 CasesA.push_back(Case.getCaseValue());
4299 if (Dest == DestB) {
4300 CasesB.push_back(Case.getCaseValue());
4303 return false; // More than two destinations.
4306 assert(DestA && DestB &&
4307 "Single-destination switch should have been folded.");
4308 assert(DestA != DestB);
4309 assert(DestB != SI->getDefaultDest());
4310 assert(!CasesB.empty() && "There must be non-default cases.");
4311 assert(!CasesA.empty() || HasDefault);
4313 // Figure out if one of the sets of cases form a contiguous range.
4314 SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr;
4315 BasicBlock *ContiguousDest = nullptr;
4316 BasicBlock *OtherDest = nullptr;
4317 if (!CasesA.empty() && CasesAreContiguous(CasesA)) {
4318 ContiguousCases = &CasesA;
4319 ContiguousDest = DestA;
4321 } else if (CasesAreContiguous(CasesB)) {
4322 ContiguousCases = &CasesB;
4323 ContiguousDest = DestB;
4328 // Start building the compare and branch.
4330 Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back());
4331 Constant *NumCases =
4332 ConstantInt::get(Offset->getType(), ContiguousCases->size());
4334 Value *Sub = SI->getCondition();
4335 if (!Offset->isNullValue())
4336 Sub = Builder.CreateAdd(Sub, Offset, Sub->getName() + ".off");
4339 // If NumCases overflowed, then all possible values jump to the successor.
4340 if (NumCases->isNullValue() && !ContiguousCases->empty())
4341 Cmp = ConstantInt::getTrue(SI->getContext());
4343 Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
4344 BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest);
4346 // Update weight for the newly-created conditional branch.
4347 if (HasBranchWeights(SI)) {
4348 SmallVector<uint64_t, 8> Weights;
4349 GetBranchWeights(SI, Weights);
4350 if (Weights.size() == 1 + SI->getNumCases()) {
4351 uint64_t TrueWeight = 0;
4352 uint64_t FalseWeight = 0;
4353 for (size_t I = 0, E = Weights.size(); I != E; ++I) {
4354 if (SI->getSuccessor(I) == ContiguousDest)
4355 TrueWeight += Weights[I];
4357 FalseWeight += Weights[I];
4359 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
4363 setBranchWeights(NewBI, TrueWeight, FalseWeight);
4367 // Prune obsolete incoming values off the successors' PHI nodes.
4368 for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
4369 unsigned PreviousEdges = ContiguousCases->size();
4370 if (ContiguousDest == SI->getDefaultDest())
4372 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
4373 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
4375 for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
4376 unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size();
4377 if (OtherDest == SI->getDefaultDest())
4379 for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
4380 cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
4384 SI->eraseFromParent();
4389 /// Compute masked bits for the condition of a switch
4390 /// and use it to remove dead cases.
4391 static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
4392 const DataLayout &DL) {
4393 Value *Cond = SI->getCondition();
4394 unsigned Bits = Cond->getType()->getIntegerBitWidth();
4395 KnownBits Known = computeKnownBits(Cond, DL, 0, AC, SI);
4397 // We can also eliminate cases by determining that their values are outside of
4398 // the limited range of the condition based on how many significant (non-sign)
4399 // bits are in the condition value.
4400 unsigned ExtraSignBits = ComputeNumSignBits(Cond, DL, 0, AC, SI) - 1;
4401 unsigned MaxSignificantBitsInCond = Bits - ExtraSignBits;
4403 // Gather dead cases.
4404 SmallVector<ConstantInt *, 8> DeadCases;
4405 for (auto &Case : SI->cases()) {
4406 const APInt &CaseVal = Case.getCaseValue()->getValue();
4407 if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
4408 (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) {
4409 DeadCases.push_back(Case.getCaseValue());
4410 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
4415 // If we can prove that the cases must cover all possible values, the
4416 // default destination becomes dead and we can remove it. If we know some
4417 // of the bits in the value, we can use that to more precisely compute the
4418 // number of possible unique case values.
4420 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4421 const unsigned NumUnknownBits =
4422 Bits - (Known.Zero | Known.One).countPopulation();
4423 assert(NumUnknownBits <= Bits);
4424 if (HasDefault && DeadCases.empty() &&
4425 NumUnknownBits < 64 /* avoid overflow */ &&
4426 SI->getNumCases() == (1ULL << NumUnknownBits)) {
4427 LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
4428 BasicBlock *NewDefault =
4429 SplitBlockPredecessors(SI->getDefaultDest(), SI->getParent(), "");
4430 SI->setDefaultDest(&*NewDefault);
4431 SplitBlock(&*NewDefault, &NewDefault->front());
4432 auto *OldTI = NewDefault->getTerminator();
4433 new UnreachableInst(SI->getContext(), OldTI);
4434 EraseTerminatorAndDCECond(OldTI);
4438 SmallVector<uint64_t, 8> Weights;
4439 bool HasWeight = HasBranchWeights(SI);
4441 GetBranchWeights(SI, Weights);
4442 HasWeight = (Weights.size() == 1 + SI->getNumCases());
4445 // Remove dead cases from the switch.
4446 for (ConstantInt *DeadCase : DeadCases) {
4447 SwitchInst::CaseIt CaseI = SI->findCaseValue(DeadCase);
4448 assert(CaseI != SI->case_default() &&
4449 "Case was not found. Probably mistake in DeadCases forming.");
4451 std::swap(Weights[CaseI->getCaseIndex() + 1], Weights.back());
4455 // Prune unused values from PHI nodes.
4456 CaseI->getCaseSuccessor()->removePredecessor(SI->getParent());
4457 SI->removeCase(CaseI);
4459 if (HasWeight && Weights.size() >= 2) {
4460 SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
4461 setBranchWeights(SI, MDWeights);
4464 return !DeadCases.empty();
4467 /// If BB would be eligible for simplification by
4468 /// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
4469 /// by an unconditional branch), look at the phi node for BB in the successor
4470 /// block and see if the incoming value is equal to CaseValue. If so, return
4471 /// the phi node, and set PhiIndex to BB's index in the phi node.
4472 static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
4473 BasicBlock *BB, int *PhiIndex) {
4474 if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
4475 return nullptr; // BB must be empty to be a candidate for simplification.
4476 if (!BB->getSinglePredecessor())
4477 return nullptr; // BB must be dominated by the switch.
4479 BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
4480 if (!Branch || !Branch->isUnconditional())
4481 return nullptr; // Terminator must be unconditional branch.
4483 BasicBlock *Succ = Branch->getSuccessor(0);
4485 for (PHINode &PHI : Succ->phis()) {
4486 int Idx = PHI.getBasicBlockIndex(BB);
4487 assert(Idx >= 0 && "PHI has no entry for predecessor?");
4489 Value *InValue = PHI.getIncomingValue(Idx);
4490 if (InValue != CaseValue)
4500 /// Try to forward the condition of a switch instruction to a phi node
4501 /// dominated by the switch, if that would mean that some of the destination
4502 /// blocks of the switch can be folded away. Return true if a change is made.
4503 static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
4504 using ForwardingNodesMap = DenseMap<PHINode *, SmallVector<int, 4>>;
4506 ForwardingNodesMap ForwardingNodes;
4507 BasicBlock *SwitchBlock = SI->getParent();
4508 bool Changed = false;
4509 for (auto &Case : SI->cases()) {
4510 ConstantInt *CaseValue = Case.getCaseValue();
4511 BasicBlock *CaseDest = Case.getCaseSuccessor();
4513 // Replace phi operands in successor blocks that are using the constant case
4514 // value rather than the switch condition variable:
4516 // switch i32 %x, label %default [
4517 // i32 17, label %succ
4520 // %r = phi i32 ... [ 17, %switchbb ] ...
4522 // %r = phi i32 ... [ %x, %switchbb ] ...
4524 for (PHINode &Phi : CaseDest->phis()) {
4525 // This only works if there is exactly 1 incoming edge from the switch to
4526 // a phi. If there is >1, that means multiple cases of the switch map to 1
4527 // value in the phi, and that phi value is not the switch condition. Thus,
4528 // this transform would not make sense (the phi would be invalid because
4529 // a phi can't have different incoming values from the same block).
4530 int SwitchBBIdx = Phi.getBasicBlockIndex(SwitchBlock);
4531 if (Phi.getIncomingValue(SwitchBBIdx) == CaseValue &&
4532 count(Phi.blocks(), SwitchBlock) == 1) {
4533 Phi.setIncomingValue(SwitchBBIdx, SI->getCondition());
4538 // Collect phi nodes that are indirectly using this switch's case constants.
4540 if (auto *Phi = FindPHIForConditionForwarding(CaseValue, CaseDest, &PhiIdx))
4541 ForwardingNodes[Phi].push_back(PhiIdx);
4544 for (auto &ForwardingNode : ForwardingNodes) {
4545 PHINode *Phi = ForwardingNode.first;
4546 SmallVectorImpl<int> &Indexes = ForwardingNode.second;
4547 if (Indexes.size() < 2)
4550 for (int Index : Indexes)
4551 Phi->setIncomingValue(Index, SI->getCondition());
4558 /// Return true if the backend will be able to handle
4559 /// initializing an array of constants like C.
4560 static bool ValidLookupTableConstant(Constant *C, const TargetTransformInfo &TTI) {
4561 if (C->isThreadDependent())
4563 if (C->isDLLImportDependent())
4566 if (!isa<ConstantFP>(C) && !isa<ConstantInt>(C) &&
4567 !isa<ConstantPointerNull>(C) && !isa<GlobalValue>(C) &&
4568 !isa<UndefValue>(C) && !isa<ConstantExpr>(C))
4571 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
4572 if (!CE->isGEPWithNoNotionalOverIndexing())
4574 if (!ValidLookupTableConstant(CE->getOperand(0), TTI))
4578 if (!TTI.shouldBuildLookupTablesForConstant(C))
4584 /// If V is a Constant, return it. Otherwise, try to look up
4585 /// its constant value in ConstantPool, returning 0 if it's not there.
4587 LookupConstant(Value *V,
4588 const SmallDenseMap<Value *, Constant *> &ConstantPool) {
4589 if (Constant *C = dyn_cast<Constant>(V))
4591 return ConstantPool.lookup(V);
4594 /// Try to fold instruction I into a constant. This works for
4595 /// simple instructions such as binary operations where both operands are
4596 /// constant or can be replaced by constants from the ConstantPool. Returns the
4597 /// resulting constant on success, 0 otherwise.
4599 ConstantFold(Instruction *I, const DataLayout &DL,
4600 const SmallDenseMap<Value *, Constant *> &ConstantPool) {
4601 if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
4602 Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
4605 if (A->isAllOnesValue())
4606 return LookupConstant(Select->getTrueValue(), ConstantPool);
4607 if (A->isNullValue())
4608 return LookupConstant(Select->getFalseValue(), ConstantPool);
4612 SmallVector<Constant *, 4> COps;
4613 for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
4614 if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool))
4620 if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
4621 return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
4625 return ConstantFoldInstOperands(I, COps, DL);
4628 /// Try to determine the resulting constant values in phi nodes
4629 /// at the common destination basic block, *CommonDest, for one of the case
4630 /// destionations CaseDest corresponding to value CaseVal (0 for the default
4631 /// case), of a switch instruction SI.
4633 GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
4634 BasicBlock **CommonDest,
4635 SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
4636 const DataLayout &DL, const TargetTransformInfo &TTI) {
4637 // The block from which we enter the common destination.
4638 BasicBlock *Pred = SI->getParent();
4640 // If CaseDest is empty except for some side-effect free instructions through
4641 // which we can constant-propagate the CaseVal, continue to its successor.
4642 SmallDenseMap<Value *, Constant *> ConstantPool;
4643 ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
4644 for (Instruction &I :CaseDest->instructionsWithoutDebug()) {
4645 if (I.isTerminator()) {
4646 // If the terminator is a simple branch, continue to the next block.
4647 if (I.getNumSuccessors() != 1 || I.isExceptionalTerminator())
4650 CaseDest = I.getSuccessor(0);
4651 } else if (Constant *C = ConstantFold(&I, DL, ConstantPool)) {
4652 // Instruction is side-effect free and constant.
4654 // If the instruction has uses outside this block or a phi node slot for
4655 // the block, it is not safe to bypass the instruction since it would then
4656 // no longer dominate all its uses.
4657 for (auto &Use : I.uses()) {
4658 User *User = Use.getUser();
4659 if (Instruction *I = dyn_cast<Instruction>(User))
4660 if (I->getParent() == CaseDest)
4662 if (PHINode *Phi = dyn_cast<PHINode>(User))
4663 if (Phi->getIncomingBlock(Use) == CaseDest)
4668 ConstantPool.insert(std::make_pair(&I, C));
4674 // If we did not have a CommonDest before, use the current one.
4676 *CommonDest = CaseDest;
4677 // If the destination isn't the common one, abort.
4678 if (CaseDest != *CommonDest)
4681 // Get the values for this case from phi nodes in the destination block.
4682 for (PHINode &PHI : (*CommonDest)->phis()) {
4683 int Idx = PHI.getBasicBlockIndex(Pred);
4687 Constant *ConstVal =
4688 LookupConstant(PHI.getIncomingValue(Idx), ConstantPool);
4692 // Be conservative about which kinds of constants we support.
4693 if (!ValidLookupTableConstant(ConstVal, TTI))
4696 Res.push_back(std::make_pair(&PHI, ConstVal));
4699 return Res.size() > 0;
4702 // Helper function used to add CaseVal to the list of cases that generate
4703 // Result. Returns the updated number of cases that generate this result.
4704 static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
4705 SwitchCaseResultVectorTy &UniqueResults,
4707 for (auto &I : UniqueResults) {
4708 if (I.first == Result) {
4709 I.second.push_back(CaseVal);
4710 return I.second.size();
4713 UniqueResults.push_back(
4714 std::make_pair(Result, SmallVector<ConstantInt *, 4>(1, CaseVal)));
4718 // Helper function that initializes a map containing
4719 // results for the PHI node of the common destination block for a switch
4720 // instruction. Returns false if multiple PHI nodes have been found or if
4721 // there is not a common destination block for the switch.
4723 InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
4724 SwitchCaseResultVectorTy &UniqueResults,
4725 Constant *&DefaultResult, const DataLayout &DL,
4726 const TargetTransformInfo &TTI,
4727 uintptr_t MaxUniqueResults, uintptr_t MaxCasesPerResult) {
4728 for (auto &I : SI->cases()) {
4729 ConstantInt *CaseVal = I.getCaseValue();
4731 // Resulting value at phi nodes for this case value.
4732 SwitchCaseResultsTy Results;
4733 if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
4737 // Only one value per case is permitted.
4738 if (Results.size() > 1)
4741 // Add the case->result mapping to UniqueResults.
4742 const uintptr_t NumCasesForResult =
4743 MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
4745 // Early out if there are too many cases for this result.
4746 if (NumCasesForResult > MaxCasesPerResult)
4749 // Early out if there are too many unique results.
4750 if (UniqueResults.size() > MaxUniqueResults)
4753 // Check the PHI consistency.
4755 PHI = Results[0].first;
4756 else if (PHI != Results[0].first)
4759 // Find the default result value.
4760 SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
4761 BasicBlock *DefaultDest = SI->getDefaultDest();
4762 GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
4764 // If the default value is not found abort unless the default destination
4767 DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
4768 if ((!DefaultResult &&
4769 !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
4775 // Helper function that checks if it is possible to transform a switch with only
4776 // two cases (or two cases + default) that produces a result into a select.
4779 // case 10: %0 = icmp eq i32 %a, 10
4780 // return 10; %1 = select i1 %0, i32 10, i32 4
4781 // case 20: ----> %2 = icmp eq i32 %a, 20
4782 // return 2; %3 = select i1 %2, i32 2, i32 %1
4786 static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
4787 Constant *DefaultResult, Value *Condition,
4788 IRBuilder<> &Builder) {
4789 assert(ResultVector.size() == 2 &&
4790 "We should have exactly two unique results at this point");
4791 // If we are selecting between only two cases transform into a simple
4792 // select or a two-way select if default is possible.
4793 if (ResultVector[0].second.size() == 1 &&
4794 ResultVector[1].second.size() == 1) {
4795 ConstantInt *const FirstCase = ResultVector[0].second[0];
4796 ConstantInt *const SecondCase = ResultVector[1].second[0];
4798 bool DefaultCanTrigger = DefaultResult;
4799 Value *SelectValue = ResultVector[1].first;
4800 if (DefaultCanTrigger) {
4801 Value *const ValueCompare =
4802 Builder.CreateICmpEQ(Condition, SecondCase, "switch.selectcmp");
4803 SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first,
4804 DefaultResult, "switch.select");
4806 Value *const ValueCompare =
4807 Builder.CreateICmpEQ(Condition, FirstCase, "switch.selectcmp");
4808 return Builder.CreateSelect(ValueCompare, ResultVector[0].first,
4809 SelectValue, "switch.select");
4815 // Helper function to cleanup a switch instruction that has been converted into
4816 // a select, fixing up PHI nodes and basic blocks.
4817 static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
4819 IRBuilder<> &Builder) {
4820 BasicBlock *SelectBB = SI->getParent();
4821 while (PHI->getBasicBlockIndex(SelectBB) >= 0)
4822 PHI->removeIncomingValue(SelectBB);
4823 PHI->addIncoming(SelectValue, SelectBB);
4825 Builder.CreateBr(PHI->getParent());
4827 // Remove the switch.
4828 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
4829 BasicBlock *Succ = SI->getSuccessor(i);
4831 if (Succ == PHI->getParent())
4833 Succ->removePredecessor(SelectBB);
4835 SI->eraseFromParent();
4838 /// If the switch is only used to initialize one or more
4839 /// phi nodes in a common successor block with only two different
4840 /// constant values, replace the switch with select.
4841 static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
4842 const DataLayout &DL,
4843 const TargetTransformInfo &TTI) {
4844 Value *const Cond = SI->getCondition();
4845 PHINode *PHI = nullptr;
4846 BasicBlock *CommonDest = nullptr;
4847 Constant *DefaultResult;
4848 SwitchCaseResultVectorTy UniqueResults;
4849 // Collect all the cases that will deliver the same value from the switch.
4850 if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
4853 // Selects choose between maximum two values.
4854 if (UniqueResults.size() != 2)
4856 assert(PHI != nullptr && "PHI for value select not found");
4858 Builder.SetInsertPoint(SI);
4859 Value *SelectValue =
4860 ConvertTwoCaseSwitch(UniqueResults, DefaultResult, Cond, Builder);
4862 RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder);
4865 // The switch couldn't be converted into a select.
4871 /// This class represents a lookup table that can be used to replace a switch.
4872 class SwitchLookupTable {
4874 /// Create a lookup table to use as a switch replacement with the contents
4875 /// of Values, using DefaultValue to fill any holes in the table.
4877 Module &M, uint64_t TableSize, ConstantInt *Offset,
4878 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
4879 Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName);
4881 /// Build instructions with Builder to retrieve the value at
4882 /// the position given by Index in the lookup table.
4883 Value *BuildLookup(Value *Index, IRBuilder<> &Builder);
4885 /// Return true if a table with TableSize elements of
4886 /// type ElementType would fit in a target-legal register.
4887 static bool WouldFitInRegister(const DataLayout &DL, uint64_t TableSize,
4891 // Depending on the contents of the table, it can be represented in
4894 // For tables where each element contains the same value, we just have to
4895 // store that single value and return it for each lookup.
4898 // For tables where there is a linear relationship between table index
4899 // and values. We calculate the result with a simple multiplication
4900 // and addition instead of a table lookup.
4903 // For small tables with integer elements, we can pack them into a bitmap
4904 // that fits into a target-legal register. Values are retrieved by
4905 // shift and mask operations.
4908 // The table is stored as an array of values. Values are retrieved by load
4909 // instructions from the table.
4913 // For SingleValueKind, this is the single value.
4914 Constant *SingleValue = nullptr;
4916 // For BitMapKind, this is the bitmap.
4917 ConstantInt *BitMap = nullptr;
4918 IntegerType *BitMapElementTy = nullptr;
4920 // For LinearMapKind, these are the constants used to derive the value.
4921 ConstantInt *LinearOffset = nullptr;
4922 ConstantInt *LinearMultiplier = nullptr;
4924 // For ArrayKind, this is the array.
4925 GlobalVariable *Array = nullptr;
4928 } // end anonymous namespace
4930 SwitchLookupTable::SwitchLookupTable(
4931 Module &M, uint64_t TableSize, ConstantInt *Offset,
4932 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
4933 Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName) {
4934 assert(Values.size() && "Can't build lookup table without values!");
4935 assert(TableSize >= Values.size() && "Can't fit values in table!");
4937 // If all values in the table are equal, this is that value.
4938 SingleValue = Values.begin()->second;
4940 Type *ValueType = Values.begin()->second->getType();
4942 // Build up the table contents.
4943 SmallVector<Constant *, 64> TableContents(TableSize);
4944 for (size_t I = 0, E = Values.size(); I != E; ++I) {
4945 ConstantInt *CaseVal = Values[I].first;
4946 Constant *CaseRes = Values[I].second;
4947 assert(CaseRes->getType() == ValueType);
4949 uint64_t Idx = (CaseVal->getValue() - Offset->getValue()).getLimitedValue();
4950 TableContents[Idx] = CaseRes;
4952 if (CaseRes != SingleValue)
4953 SingleValue = nullptr;
4956 // Fill in any holes in the table with the default result.
4957 if (Values.size() < TableSize) {
4958 assert(DefaultValue &&
4959 "Need a default value to fill the lookup table holes.");
4960 assert(DefaultValue->getType() == ValueType);
4961 for (uint64_t I = 0; I < TableSize; ++I) {
4962 if (!TableContents[I])
4963 TableContents[I] = DefaultValue;
4966 if (DefaultValue != SingleValue)
4967 SingleValue = nullptr;
4970 // If each element in the table contains the same value, we only need to store
4971 // that single value.
4973 Kind = SingleValueKind;
4977 // Check if we can derive the value with a linear transformation from the
4979 if (isa<IntegerType>(ValueType)) {
4980 bool LinearMappingPossible = true;
4983 assert(TableSize >= 2 && "Should be a SingleValue table.");
4984 // Check if there is the same distance between two consecutive values.
4985 for (uint64_t I = 0; I < TableSize; ++I) {
4986 ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
4988 // This is an undef. We could deal with it, but undefs in lookup tables
4989 // are very seldom. It's probably not worth the additional complexity.
4990 LinearMappingPossible = false;
4993 const APInt &Val = ConstVal->getValue();
4995 APInt Dist = Val - PrevVal;
4998 } else if (Dist != DistToPrev) {
4999 LinearMappingPossible = false;
5005 if (LinearMappingPossible) {
5006 LinearOffset = cast<ConstantInt>(TableContents[0]);
5007 LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
5008 Kind = LinearMapKind;
5014 // If the type is integer and the table fits in a register, build a bitmap.
5015 if (WouldFitInRegister(DL, TableSize, ValueType)) {
5016 IntegerType *IT = cast<IntegerType>(ValueType);
5017 APInt TableInt(TableSize * IT->getBitWidth(), 0);
5018 for (uint64_t I = TableSize; I > 0; --I) {
5019 TableInt <<= IT->getBitWidth();
5020 // Insert values into the bitmap. Undef values are set to zero.
5021 if (!isa<UndefValue>(TableContents[I - 1])) {
5022 ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
5023 TableInt |= Val->getValue().zext(TableInt.getBitWidth());
5026 BitMap = ConstantInt::get(M.getContext(), TableInt);
5027 BitMapElementTy = IT;
5033 // Store the table in an array.
5034 ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
5035 Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
5037 Array = new GlobalVariable(M, ArrayTy, /*constant=*/true,
5038 GlobalVariable::PrivateLinkage, Initializer,
5039 "switch.table." + FuncName);
5040 Array->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
5041 // Set the alignment to that of an array items. We will be only loading one
5043 Array->setAlignment(DL.getPrefTypeAlignment(ValueType));
5047 Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
5049 case SingleValueKind:
5051 case LinearMapKind: {
5052 // Derive the result value from the input value.
5053 Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
5054 false, "switch.idx.cast");
5055 if (!LinearMultiplier->isOne())
5056 Result = Builder.CreateMul(Result, LinearMultiplier, "switch.idx.mult");
5057 if (!LinearOffset->isZero())
5058 Result = Builder.CreateAdd(Result, LinearOffset, "switch.offset");
5062 // Type of the bitmap (e.g. i59).
5063 IntegerType *MapTy = BitMap->getType();
5065 // Cast Index to the same type as the bitmap.
5066 // Note: The Index is <= the number of elements in the table, so
5067 // truncating it to the width of the bitmask is safe.
5068 Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast");
5070 // Multiply the shift amount by the element width.
5071 ShiftAmt = Builder.CreateMul(
5072 ShiftAmt, ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
5076 Value *DownShifted =
5077 Builder.CreateLShr(BitMap, ShiftAmt, "switch.downshift");
5079 return Builder.CreateTrunc(DownShifted, BitMapElementTy, "switch.masked");
5082 // Make sure the table index will not overflow when treated as signed.
5083 IntegerType *IT = cast<IntegerType>(Index->getType());
5084 uint64_t TableSize =
5085 Array->getInitializer()->getType()->getArrayNumElements();
5086 if (TableSize > (1ULL << (IT->getBitWidth() - 1)))
5087 Index = Builder.CreateZExt(
5088 Index, IntegerType::get(IT->getContext(), IT->getBitWidth() + 1),
5089 "switch.tableidx.zext");
5091 Value *GEPIndices[] = {Builder.getInt32(0), Index};
5092 Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
5093 GEPIndices, "switch.gep");
5094 return Builder.CreateLoad(GEP, "switch.load");
5097 llvm_unreachable("Unknown lookup table kind!");
5100 bool SwitchLookupTable::WouldFitInRegister(const DataLayout &DL,
5102 Type *ElementType) {
5103 auto *IT = dyn_cast<IntegerType>(ElementType);
5106 // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
5107 // are <= 15, we could try to narrow the type.
5109 // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
5110 if (TableSize >= UINT_MAX / IT->getBitWidth())
5112 return DL.fitsInLegalInteger(TableSize * IT->getBitWidth());
5115 /// Determine whether a lookup table should be built for this switch, based on
5116 /// the number of cases, size of the table, and the types of the results.
5118 ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
5119 const TargetTransformInfo &TTI, const DataLayout &DL,
5120 const SmallDenseMap<PHINode *, Type *> &ResultTypes) {
5121 if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
5122 return false; // TableSize overflowed, or mul below might overflow.
5124 bool AllTablesFitInRegister = true;
5125 bool HasIllegalType = false;
5126 for (const auto &I : ResultTypes) {
5127 Type *Ty = I.second;
5129 // Saturate this flag to true.
5130 HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty);
5132 // Saturate this flag to false.
5133 AllTablesFitInRegister =
5134 AllTablesFitInRegister &&
5135 SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
5137 // If both flags saturate, we're done. NOTE: This *only* works with
5138 // saturating flags, and all flags have to saturate first due to the
5139 // non-deterministic behavior of iterating over a dense map.
5140 if (HasIllegalType && !AllTablesFitInRegister)
5144 // If each table would fit in a register, we should build it anyway.
5145 if (AllTablesFitInRegister)
5148 // Don't build a table that doesn't fit in-register if it has illegal types.
5152 // The table density should be at least 40%. This is the same criterion as for
5153 // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
5154 // FIXME: Find the best cut-off.
5155 return SI->getNumCases() * 10 >= TableSize * 4;
5158 /// Try to reuse the switch table index compare. Following pattern:
5160 /// if (idx < tablesize)
5161 /// r = table[idx]; // table does not contain default_value
5163 /// r = default_value;
5164 /// if (r != default_value)
5167 /// Is optimized to:
5169 /// cond = idx < tablesize;
5173 /// r = default_value;
5177 /// Jump threading will then eliminate the second if(cond).
5178 static void reuseTableCompare(
5179 User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch,
5180 Constant *DefaultValue,
5181 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
5182 ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser);
5186 // We require that the compare is in the same block as the phi so that jump
5187 // threading can do its work afterwards.
5188 if (CmpInst->getParent() != PhiBlock)
5191 Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1));
5195 Value *RangeCmp = RangeCheckBranch->getCondition();
5196 Constant *TrueConst = ConstantInt::getTrue(RangeCmp->getType());
5197 Constant *FalseConst = ConstantInt::getFalse(RangeCmp->getType());
5199 // Check if the compare with the default value is constant true or false.
5200 Constant *DefaultConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
5201 DefaultValue, CmpOp1, true);
5202 if (DefaultConst != TrueConst && DefaultConst != FalseConst)
5205 // Check if the compare with the case values is distinct from the default
5207 for (auto ValuePair : Values) {
5208 Constant *CaseConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
5209 ValuePair.second, CmpOp1, true);
5210 if (!CaseConst || CaseConst == DefaultConst || isa<UndefValue>(CaseConst))
5212 assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
5213 "Expect true or false as compare result.");
5216 // Check if the branch instruction dominates the phi node. It's a simple
5217 // dominance check, but sufficient for our needs.
5218 // Although this check is invariant in the calling loops, it's better to do it
5219 // at this late stage. Practically we do it at most once for a switch.
5220 BasicBlock *BranchBlock = RangeCheckBranch->getParent();
5221 for (auto PI = pred_begin(PhiBlock), E = pred_end(PhiBlock); PI != E; ++PI) {
5222 BasicBlock *Pred = *PI;
5223 if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
5227 if (DefaultConst == FalseConst) {
5228 // The compare yields the same result. We can replace it.
5229 CmpInst->replaceAllUsesWith(RangeCmp);
5230 ++NumTableCmpReuses;
5232 // The compare yields the same result, just inverted. We can replace it.
5233 Value *InvertedTableCmp = BinaryOperator::CreateXor(
5234 RangeCmp, ConstantInt::get(RangeCmp->getType(), 1), "inverted.cmp",
5236 CmpInst->replaceAllUsesWith(InvertedTableCmp);
5237 ++NumTableCmpReuses;
5241 /// If the switch is only used to initialize one or more phi nodes in a common
5242 /// successor block with different constant values, replace the switch with
5244 static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
5245 const DataLayout &DL,
5246 const TargetTransformInfo &TTI) {
5247 assert(SI->getNumCases() > 1 && "Degenerate switch?");
5249 Function *Fn = SI->getParent()->getParent();
5250 // Only build lookup table when we have a target that supports it or the
5251 // attribute is not set.
5252 if (!TTI.shouldBuildLookupTables() ||
5253 (Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true"))
5256 // FIXME: If the switch is too sparse for a lookup table, perhaps we could
5257 // split off a dense part and build a lookup table for that.
5259 // FIXME: This creates arrays of GEPs to constant strings, which means each
5260 // GEP needs a runtime relocation in PIC code. We should just build one big
5261 // string and lookup indices into that.
5263 // Ignore switches with less than three cases. Lookup tables will not make
5264 // them faster, so we don't analyze them.
5265 if (SI->getNumCases() < 3)
5268 // Figure out the corresponding result for each case value and phi node in the
5269 // common destination, as well as the min and max case values.
5270 assert(!empty(SI->cases()));
5271 SwitchInst::CaseIt CI = SI->case_begin();
5272 ConstantInt *MinCaseVal = CI->getCaseValue();
5273 ConstantInt *MaxCaseVal = CI->getCaseValue();
5275 BasicBlock *CommonDest = nullptr;
5277 using ResultListTy = SmallVector<std::pair<ConstantInt *, Constant *>, 4>;
5278 SmallDenseMap<PHINode *, ResultListTy> ResultLists;
5280 SmallDenseMap<PHINode *, Constant *> DefaultResults;
5281 SmallDenseMap<PHINode *, Type *> ResultTypes;
5282 SmallVector<PHINode *, 4> PHIs;
5284 for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
5285 ConstantInt *CaseVal = CI->getCaseValue();
5286 if (CaseVal->getValue().slt(MinCaseVal->getValue()))
5287 MinCaseVal = CaseVal;
5288 if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
5289 MaxCaseVal = CaseVal;
5291 // Resulting value at phi nodes for this case value.
5292 using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
5294 if (!GetCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
5298 // Append the result from this case to the list for each phi.
5299 for (const auto &I : Results) {
5300 PHINode *PHI = I.first;
5301 Constant *Value = I.second;
5302 if (!ResultLists.count(PHI))
5303 PHIs.push_back(PHI);
5304 ResultLists[PHI].push_back(std::make_pair(CaseVal, Value));
5308 // Keep track of the result types.
5309 for (PHINode *PHI : PHIs) {
5310 ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
5313 uint64_t NumResults = ResultLists[PHIs[0]].size();
5314 APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue();
5315 uint64_t TableSize = RangeSpread.getLimitedValue() + 1;
5316 bool TableHasHoles = (NumResults < TableSize);
5318 // If the table has holes, we need a constant result for the default case
5319 // or a bitmask that fits in a register.
5320 SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
5321 bool HasDefaultResults =
5322 GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest,
5323 DefaultResultsList, DL, TTI);
5325 bool NeedMask = (TableHasHoles && !HasDefaultResults);
5327 // As an extra penalty for the validity test we require more cases.
5328 if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
5330 if (!DL.fitsInLegalInteger(TableSize))
5334 for (const auto &I : DefaultResultsList) {
5335 PHINode *PHI = I.first;
5336 Constant *Result = I.second;
5337 DefaultResults[PHI] = Result;
5340 if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
5343 // Create the BB that does the lookups.
5344 Module &Mod = *CommonDest->getParent()->getParent();
5345 BasicBlock *LookupBB = BasicBlock::Create(
5346 Mod.getContext(), "switch.lookup", CommonDest->getParent(), CommonDest);
5348 // Compute the table index value.
5349 Builder.SetInsertPoint(SI);
5351 if (MinCaseVal->isNullValue())
5352 TableIndex = SI->getCondition();
5354 TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal,
5357 // Compute the maximum table size representable by the integer type we are
5359 unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
5360 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
5361 assert(MaxTableSize >= TableSize &&
5362 "It is impossible for a switch to have more entries than the max "
5363 "representable value of its input integer type's size.");
5365 // If the default destination is unreachable, or if the lookup table covers
5366 // all values of the conditional variable, branch directly to the lookup table
5367 // BB. Otherwise, check that the condition is within the case range.
5368 const bool DefaultIsReachable =
5369 !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5370 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
5371 BranchInst *RangeCheckBranch = nullptr;
5373 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5374 Builder.CreateBr(LookupBB);
5375 // Note: We call removeProdecessor later since we need to be able to get the
5376 // PHI value for the default case in case we're using a bit mask.
5378 Value *Cmp = Builder.CreateICmpULT(
5379 TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
5381 Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
5384 // Populate the BB that does the lookups.
5385 Builder.SetInsertPoint(LookupBB);
5388 // Before doing the lookup, we do the hole check. The LookupBB is therefore
5389 // re-purposed to do the hole check, and we create a new LookupBB.
5390 BasicBlock *MaskBB = LookupBB;
5391 MaskBB->setName("switch.hole_check");
5392 LookupBB = BasicBlock::Create(Mod.getContext(), "switch.lookup",
5393 CommonDest->getParent(), CommonDest);
5395 // Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
5396 // unnecessary illegal types.
5397 uint64_t TableSizePowOf2 = NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
5398 APInt MaskInt(TableSizePowOf2, 0);
5399 APInt One(TableSizePowOf2, 1);
5400 // Build bitmask; fill in a 1 bit for every case.
5401 const ResultListTy &ResultList = ResultLists[PHIs[0]];
5402 for (size_t I = 0, E = ResultList.size(); I != E; ++I) {
5403 uint64_t Idx = (ResultList[I].first->getValue() - MinCaseVal->getValue())
5405 MaskInt |= One << Idx;
5407 ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
5409 // Get the TableIndex'th bit of the bitmask.
5410 // If this bit is 0 (meaning hole) jump to the default destination,
5411 // else continue with table lookup.
5412 IntegerType *MapTy = TableMask->getType();
5414 Builder.CreateZExtOrTrunc(TableIndex, MapTy, "switch.maskindex");
5415 Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
5416 Value *LoBit = Builder.CreateTrunc(
5417 Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
5418 Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
5420 Builder.SetInsertPoint(LookupBB);
5421 AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
5424 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5425 // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
5426 // do not delete PHINodes here.
5427 SI->getDefaultDest()->removePredecessor(SI->getParent(),
5428 /*DontDeleteUselessPHIs=*/true);
5431 bool ReturnedEarly = false;
5432 for (PHINode *PHI : PHIs) {
5433 const ResultListTy &ResultList = ResultLists[PHI];
5435 // If using a bitmask, use any value to fill the lookup table holes.
5436 Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
5437 StringRef FuncName = Fn->getName();
5438 SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL,
5441 Value *Result = Table.BuildLookup(TableIndex, Builder);
5443 // If the result is used to return immediately from the function, we want to
5444 // do that right here.
5445 if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) &&
5446 PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) {
5447 Builder.CreateRet(Result);
5448 ReturnedEarly = true;
5452 // Do a small peephole optimization: re-use the switch table compare if
5454 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
5455 BasicBlock *PhiBlock = PHI->getParent();
5456 // Search for compare instructions which use the phi.
5457 for (auto *User : PHI->users()) {
5458 reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList);
5462 PHI->addIncoming(Result, LookupBB);
5466 Builder.CreateBr(CommonDest);
5468 // Remove the switch.
5469 for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
5470 BasicBlock *Succ = SI->getSuccessor(i);
5472 if (Succ == SI->getDefaultDest())
5474 Succ->removePredecessor(SI->getParent());
5476 SI->eraseFromParent();
5480 ++NumLookupTablesHoles;
5484 static bool isSwitchDense(ArrayRef<int64_t> Values) {
5485 // See also SelectionDAGBuilder::isDense(), which this function was based on.
5486 uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
5487 uint64_t Range = Diff + 1;
5488 uint64_t NumCases = Values.size();
5489 // 40% is the default density for building a jump table in optsize/minsize mode.
5490 uint64_t MinDensity = 40;
5492 return NumCases * 100 >= Range * MinDensity;
5495 /// Try to transform a switch that has "holes" in it to a contiguous sequence
5498 /// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
5499 /// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
5501 /// This converts a sparse switch into a dense switch which allows better
5502 /// lowering and could also allow transforming into a lookup table.
5503 static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
5504 const DataLayout &DL,
5505 const TargetTransformInfo &TTI) {
5506 auto *CondTy = cast<IntegerType>(SI->getCondition()->getType());
5507 if (CondTy->getIntegerBitWidth() > 64 ||
5508 !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5510 // Only bother with this optimization if there are more than 3 switch cases;
5511 // SDAG will only bother creating jump tables for 4 or more cases.
5512 if (SI->getNumCases() < 4)
5515 // This transform is agnostic to the signedness of the input or case values. We
5516 // can treat the case values as signed or unsigned. We can optimize more common
5517 // cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
5519 SmallVector<int64_t,4> Values;
5520 for (auto &C : SI->cases())
5521 Values.push_back(C.getCaseValue()->getValue().getSExtValue());
5524 // If the switch is already dense, there's nothing useful to do here.
5525 if (isSwitchDense(Values))
5528 // First, transform the values such that they start at zero and ascend.
5529 int64_t Base = Values[0];
5530 for (auto &V : Values)
5531 V -= (uint64_t)(Base);
5533 // Now we have signed numbers that have been shifted so that, given enough
5534 // precision, there are no negative values. Since the rest of the transform
5535 // is bitwise only, we switch now to an unsigned representation.
5537 for (auto &V : Values)
5538 GCD = GreatestCommonDivisor64(GCD, (uint64_t)V);
5540 // This transform can be done speculatively because it is so cheap - it results
5541 // in a single rotate operation being inserted. This can only happen if the
5542 // factor extracted is a power of 2.
5543 // FIXME: If the GCD is an odd number we can multiply by the multiplicative
5544 // inverse of GCD and then perform this transform.
5545 // FIXME: It's possible that optimizing a switch on powers of two might also
5546 // be beneficial - flag values are often powers of two and we could use a CLZ
5547 // as the key function.
5548 if (GCD <= 1 || !isPowerOf2_64(GCD))
5549 // No common divisor found or too expensive to compute key function.
5552 unsigned Shift = Log2_64(GCD);
5553 for (auto &V : Values)
5554 V = (int64_t)((uint64_t)V >> Shift);
5556 if (!isSwitchDense(Values))
5557 // Transform didn't create a dense switch.
5560 // The obvious transform is to shift the switch condition right and emit a
5561 // check that the condition actually cleanly divided by GCD, i.e.
5562 // C & (1 << Shift - 1) == 0
5563 // inserting a new CFG edge to handle the case where it didn't divide cleanly.
5565 // A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
5566 // shift and puts the shifted-off bits in the uppermost bits. If any of these
5567 // are nonzero then the switch condition will be very large and will hit the
5570 auto *Ty = cast<IntegerType>(SI->getCondition()->getType());
5571 Builder.SetInsertPoint(SI);
5572 auto *ShiftC = ConstantInt::get(Ty, Shift);
5573 auto *Sub = Builder.CreateSub(SI->getCondition(), ConstantInt::get(Ty, Base));
5574 auto *LShr = Builder.CreateLShr(Sub, ShiftC);
5575 auto *Shl = Builder.CreateShl(Sub, Ty->getBitWidth() - Shift);
5576 auto *Rot = Builder.CreateOr(LShr, Shl);
5577 SI->replaceUsesOfWith(SI->getCondition(), Rot);
5579 for (auto Case : SI->cases()) {
5580 auto *Orig = Case.getCaseValue();
5581 auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base);
5583 cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue()))));
5588 bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
5589 BasicBlock *BB = SI->getParent();
5591 if (isValueEqualityComparison(SI)) {
5592 // If we only have one predecessor, and if it is a branch on this value,
5593 // see if that predecessor totally determines the outcome of this switch.
5594 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
5595 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
5596 return requestResimplify();
5598 Value *Cond = SI->getCondition();
5599 if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
5600 if (SimplifySwitchOnSelect(SI, Select))
5601 return requestResimplify();
5603 // If the block only contains the switch, see if we can fold the block
5604 // away into any preds.
5605 if (SI == &*BB->instructionsWithoutDebug().begin())
5606 if (FoldValueComparisonIntoPredecessors(SI, Builder))
5607 return requestResimplify();
5610 // Try to transform the switch into an icmp and a branch.
5611 if (TurnSwitchRangeIntoICmp(SI, Builder))
5612 return requestResimplify();
5614 // Remove unreachable cases.
5615 if (eliminateDeadSwitchCases(SI, Options.AC, DL))
5616 return requestResimplify();
5618 if (switchToSelect(SI, Builder, DL, TTI))
5619 return requestResimplify();
5621 if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI(SI))
5622 return requestResimplify();
5624 // The conversion from switch to lookup tables results in difficult-to-analyze
5625 // code and makes pruning branches much harder. This is a problem if the
5626 // switch expression itself can still be restricted as a result of inlining or
5627 // CVP. Therefore, only apply this transformation during late stages of the
5628 // optimisation pipeline.
5629 if (Options.ConvertSwitchToLookupTable &&
5630 SwitchToLookupTable(SI, Builder, DL, TTI))
5631 return requestResimplify();
5633 if (ReduceSwitchRange(SI, Builder, DL, TTI))
5634 return requestResimplify();
5639 bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) {
5640 BasicBlock *BB = IBI->getParent();
5641 bool Changed = false;
5643 // Eliminate redundant destinations.
5644 SmallPtrSet<Value *, 8> Succs;
5645 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
5646 BasicBlock *Dest = IBI->getDestination(i);
5647 if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
5648 Dest->removePredecessor(BB);
5649 IBI->removeDestination(i);
5656 if (IBI->getNumDestinations() == 0) {
5657 // If the indirectbr has no successors, change it to unreachable.
5658 new UnreachableInst(IBI->getContext(), IBI);
5659 EraseTerminatorAndDCECond(IBI);
5663 if (IBI->getNumDestinations() == 1) {
5664 // If the indirectbr has one successor, change it to a direct branch.
5665 BranchInst::Create(IBI->getDestination(0), IBI);
5666 EraseTerminatorAndDCECond(IBI);
5670 if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
5671 if (SimplifyIndirectBrOnSelect(IBI, SI))
5672 return requestResimplify();
5677 /// Given an block with only a single landing pad and a unconditional branch
5678 /// try to find another basic block which this one can be merged with. This
5679 /// handles cases where we have multiple invokes with unique landing pads, but
5680 /// a shared handler.
5682 /// We specifically choose to not worry about merging non-empty blocks
5683 /// here. That is a PRE/scheduling problem and is best solved elsewhere. In
5684 /// practice, the optimizer produces empty landing pad blocks quite frequently
5685 /// when dealing with exception dense code. (see: instcombine, gvn, if-else
5686 /// sinking in this file)
5688 /// This is primarily a code size optimization. We need to avoid performing
5689 /// any transform which might inhibit optimization (such as our ability to
5690 /// specialize a particular handler via tail commoning). We do this by not
5691 /// merging any blocks which require us to introduce a phi. Since the same
5692 /// values are flowing through both blocks, we don't lose any ability to
5693 /// specialize. If anything, we make such specialization more likely.
5695 /// TODO - This transformation could remove entries from a phi in the target
5696 /// block when the inputs in the phi are the same for the two blocks being
5697 /// merged. In some cases, this could result in removal of the PHI entirely.
5698 static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI,
5700 auto Succ = BB->getUniqueSuccessor();
5702 // If there's a phi in the successor block, we'd likely have to introduce
5703 // a phi into the merged landing pad block.
5704 if (isa<PHINode>(*Succ->begin()))
5707 for (BasicBlock *OtherPred : predecessors(Succ)) {
5708 if (BB == OtherPred)
5710 BasicBlock::iterator I = OtherPred->begin();
5711 LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I);
5712 if (!LPad2 || !LPad2->isIdenticalTo(LPad))
5714 for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5716 BranchInst *BI2 = dyn_cast<BranchInst>(I);
5717 if (!BI2 || !BI2->isIdenticalTo(BI))
5720 // We've found an identical block. Update our predecessors to take that
5721 // path instead and make ourselves dead.
5722 SmallPtrSet<BasicBlock *, 16> Preds;
5723 Preds.insert(pred_begin(BB), pred_end(BB));
5724 for (BasicBlock *Pred : Preds) {
5725 InvokeInst *II = cast<InvokeInst>(Pred->getTerminator());
5726 assert(II->getNormalDest() != BB && II->getUnwindDest() == BB &&
5727 "unexpected successor");
5728 II->setUnwindDest(OtherPred);
5731 // The debug info in OtherPred doesn't cover the merged control flow that
5732 // used to go through BB. We need to delete it or update it.
5733 for (auto I = OtherPred->begin(), E = OtherPred->end(); I != E;) {
5734 Instruction &Inst = *I;
5736 if (isa<DbgInfoIntrinsic>(Inst))
5737 Inst.eraseFromParent();
5740 SmallPtrSet<BasicBlock *, 16> Succs;
5741 Succs.insert(succ_begin(BB), succ_end(BB));
5742 for (BasicBlock *Succ : Succs) {
5743 Succ->removePredecessor(BB);
5746 IRBuilder<> Builder(BI);
5747 Builder.CreateUnreachable();
5748 BI->eraseFromParent();
5754 bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI,
5755 IRBuilder<> &Builder) {
5756 BasicBlock *BB = BI->getParent();
5757 BasicBlock *Succ = BI->getSuccessor(0);
5759 // If the Terminator is the only non-phi instruction, simplify the block.
5760 // If LoopHeader is provided, check if the block or its successor is a loop
5761 // header. (This is for early invocations before loop simplify and
5762 // vectorization to keep canonical loop forms for nested loops. These blocks
5763 // can be eliminated when the pass is invoked later in the back-end.)
5764 // Note that if BB has only one predecessor then we do not introduce new
5765 // backedge, so we can eliminate BB.
5766 bool NeedCanonicalLoop =
5767 Options.NeedCanonicalLoop &&
5768 (LoopHeaders && BB->hasNPredecessorsOrMore(2) &&
5769 (LoopHeaders->count(BB) || LoopHeaders->count(Succ)));
5770 BasicBlock::iterator I = BB->getFirstNonPHIOrDbg()->getIterator();
5771 if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
5772 !NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB))
5775 // If the only instruction in the block is a seteq/setne comparison against a
5776 // constant, try to simplify the block.
5777 if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
5778 if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
5779 for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5781 if (I->isTerminator() &&
5782 tryToSimplifyUncondBranchWithICmpInIt(ICI, Builder))
5786 // See if we can merge an empty landing pad block with another which is
5788 if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) {
5789 for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5791 if (I->isTerminator() && TryToMergeLandingPad(LPad, BI, BB))
5795 // If this basic block is ONLY a compare and a branch, and if a predecessor
5796 // branches to us and our successor, fold the comparison into the
5797 // predecessor and use logical operations to update the incoming value
5798 // for PHI nodes in common successor.
5799 if (FoldBranchToCommonDest(BI, Options.BonusInstThreshold))
5800 return requestResimplify();
5804 static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
5805 BasicBlock *PredPred = nullptr;
5806 for (auto *P : predecessors(BB)) {
5807 BasicBlock *PPred = P->getSinglePredecessor();
5808 if (!PPred || (PredPred && PredPred != PPred))
5815 bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
5816 BasicBlock *BB = BI->getParent();
5817 const Function *Fn = BB->getParent();
5818 if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
5821 // Conditional branch
5822 if (isValueEqualityComparison(BI)) {
5823 // If we only have one predecessor, and if it is a branch on this value,
5824 // see if that predecessor totally determines the outcome of this
5826 if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
5827 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
5828 return requestResimplify();
5830 // This block must be empty, except for the setcond inst, if it exists.
5831 // Ignore dbg intrinsics.
5832 auto I = BB->instructionsWithoutDebug().begin();
5834 if (FoldValueComparisonIntoPredecessors(BI, Builder))
5835 return requestResimplify();
5836 } else if (&*I == cast<Instruction>(BI->getCondition())) {
5838 if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
5839 return requestResimplify();
5843 // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
5844 if (SimplifyBranchOnICmpChain(BI, Builder, DL))
5847 // If this basic block has dominating predecessor blocks and the dominating
5848 // blocks' conditions imply BI's condition, we know the direction of BI.
5849 Optional<bool> Imp = isImpliedByDomCondition(BI->getCondition(), BI, DL);
5851 // Turn this into a branch on constant.
5852 auto *OldCond = BI->getCondition();
5853 ConstantInt *TorF = *Imp ? ConstantInt::getTrue(BB->getContext())
5854 : ConstantInt::getFalse(BB->getContext());
5855 BI->setCondition(TorF);
5856 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
5857 return requestResimplify();
5860 // If this basic block is ONLY a compare and a branch, and if a predecessor
5861 // branches to us and one of our successors, fold the comparison into the
5862 // predecessor and use logical operations to pick the right destination.
5863 if (FoldBranchToCommonDest(BI, Options.BonusInstThreshold))
5864 return requestResimplify();
5866 // We have a conditional branch to two blocks that are only reachable
5867 // from BI. We know that the condbr dominates the two blocks, so see if
5868 // there is any identical code in the "then" and "else" blocks. If so, we
5869 // can hoist it up to the branching block.
5870 if (BI->getSuccessor(0)->getSinglePredecessor()) {
5871 if (BI->getSuccessor(1)->getSinglePredecessor()) {
5872 if (HoistThenElseCodeToIf(BI, TTI))
5873 return requestResimplify();
5875 // If Successor #1 has multiple preds, we may be able to conditionally
5876 // execute Successor #0 if it branches to Successor #1.
5877 Instruction *Succ0TI = BI->getSuccessor(0)->getTerminator();
5878 if (Succ0TI->getNumSuccessors() == 1 &&
5879 Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
5880 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI))
5881 return requestResimplify();
5883 } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
5884 // If Successor #0 has multiple preds, we may be able to conditionally
5885 // execute Successor #1 if it branches to Successor #0.
5886 Instruction *Succ1TI = BI->getSuccessor(1)->getTerminator();
5887 if (Succ1TI->getNumSuccessors() == 1 &&
5888 Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
5889 if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI))
5890 return requestResimplify();
5893 // If this is a branch on a phi node in the current block, thread control
5894 // through this block if any PHI node entries are constants.
5895 if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
5896 if (PN->getParent() == BI->getParent())
5897 if (FoldCondBranchOnPHI(BI, DL, Options.AC))
5898 return requestResimplify();
5900 // Scan predecessor blocks for conditional branches.
5901 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
5902 if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
5903 if (PBI != BI && PBI->isConditional())
5904 if (SimplifyCondBranchToCondBranch(PBI, BI, DL))
5905 return requestResimplify();
5907 // Look for diamond patterns.
5908 if (MergeCondStores)
5909 if (BasicBlock *PrevBB = allPredecessorsComeFromSameSource(BB))
5910 if (BranchInst *PBI = dyn_cast<BranchInst>(PrevBB->getTerminator()))
5911 if (PBI != BI && PBI->isConditional())
5912 if (mergeConditionalStores(PBI, BI, DL))
5913 return requestResimplify();
5918 /// Check if passing a value to an instruction will cause undefined behavior.
5919 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
5920 Constant *C = dyn_cast<Constant>(V);
5927 if (C->isNullValue() || isa<UndefValue>(C)) {
5928 // Only look at the first use, avoid hurting compile time with long uselists
5929 User *Use = *I->user_begin();
5931 // Now make sure that there are no instructions in between that can alter
5932 // control flow (eg. calls)
5933 for (BasicBlock::iterator
5934 i = ++BasicBlock::iterator(I),
5935 UI = BasicBlock::iterator(dyn_cast<Instruction>(Use));
5937 if (i == I->getParent()->end() || i->mayHaveSideEffects())
5940 // Look through GEPs. A load from a GEP derived from NULL is still undefined
5941 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
5942 if (GEP->getPointerOperand() == I)
5943 return passingValueIsAlwaysUndefined(V, GEP);
5945 // Look through bitcasts.
5946 if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
5947 return passingValueIsAlwaysUndefined(V, BC);
5949 // Load from null is undefined.
5950 if (LoadInst *LI = dyn_cast<LoadInst>(Use))
5951 if (!LI->isVolatile())
5952 return !NullPointerIsDefined(LI->getFunction(),
5953 LI->getPointerAddressSpace());
5955 // Store to null is undefined.
5956 if (StoreInst *SI = dyn_cast<StoreInst>(Use))
5957 if (!SI->isVolatile())
5958 return (!NullPointerIsDefined(SI->getFunction(),
5959 SI->getPointerAddressSpace())) &&
5960 SI->getPointerOperand() == I;
5962 // A call to null is undefined.
5963 if (auto CS = CallSite(Use))
5964 return !NullPointerIsDefined(CS->getFunction()) &&
5965 CS.getCalledValue() == I;
5970 /// If BB has an incoming value that will always trigger undefined behavior
5971 /// (eg. null pointer dereference), remove the branch leading here.
5972 static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
5973 for (PHINode &PHI : BB->phis())
5974 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
5975 if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
5976 Instruction *T = PHI.getIncomingBlock(i)->getTerminator();
5977 IRBuilder<> Builder(T);
5978 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
5979 BB->removePredecessor(PHI.getIncomingBlock(i));
5980 // Turn uncoditional branches into unreachables and remove the dead
5981 // destination from conditional branches.
5982 if (BI->isUnconditional())
5983 Builder.CreateUnreachable();
5985 Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
5986 : BI->getSuccessor(0));
5987 BI->eraseFromParent();
5990 // TODO: SwitchInst.
5996 bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
5997 bool Changed = false;
5999 assert(BB && BB->getParent() && "Block not embedded in function!");
6000 assert(BB->getTerminator() && "Degenerate basic block encountered!");
6002 // Remove basic blocks that have no predecessors (except the entry block)...
6003 // or that just have themself as a predecessor. These are unreachable.
6004 if ((pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) ||
6005 BB->getSinglePredecessor() == BB) {
6006 LLVM_DEBUG(dbgs() << "Removing BB: \n" << *BB);
6007 DeleteDeadBlock(BB);
6011 // Check to see if we can constant propagate this terminator instruction
6013 Changed |= ConstantFoldTerminator(BB, true);
6015 // Check for and eliminate duplicate PHI nodes in this block.
6016 Changed |= EliminateDuplicatePHINodes(BB);
6018 // Check for and remove branches that will always cause undefined behavior.
6019 Changed |= removeUndefIntroducingPredecessor(BB);
6021 // Merge basic blocks into their predecessor if there is only one distinct
6022 // pred, and if there is only one distinct successor of the predecessor, and
6023 // if there are no PHI nodes.
6024 if (MergeBlockIntoPredecessor(BB))
6027 if (SinkCommon && Options.SinkCommonInsts)
6028 Changed |= SinkCommonCodeFromPredecessors(BB);
6030 IRBuilder<> Builder(BB);
6032 // If there is a trivial two-entry PHI node in this basic block, and we can
6033 // eliminate it, do so now.
6034 if (auto *PN = dyn_cast<PHINode>(BB->begin()))
6035 if (PN->getNumIncomingValues() == 2)
6036 Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
6038 Builder.SetInsertPoint(BB->getTerminator());
6039 if (auto *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
6040 if (BI->isUnconditional()) {
6041 if (SimplifyUncondBranch(BI, Builder))
6044 if (SimplifyCondBranch(BI, Builder))
6047 } else if (auto *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
6048 if (SimplifyReturn(RI, Builder))
6050 } else if (auto *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
6051 if (SimplifyResume(RI, Builder))
6053 } else if (auto *RI = dyn_cast<CleanupReturnInst>(BB->getTerminator())) {
6054 if (SimplifyCleanupReturn(RI))
6056 } else if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
6057 if (SimplifySwitch(SI, Builder))
6059 } else if (auto *UI = dyn_cast<UnreachableInst>(BB->getTerminator())) {
6060 if (SimplifyUnreachable(UI))
6062 } else if (auto *IBI = dyn_cast<IndirectBrInst>(BB->getTerminator())) {
6063 if (SimplifyIndirectBr(IBI))
6070 bool SimplifyCFGOpt::run(BasicBlock *BB) {
6071 bool Changed = false;
6073 // Repeated simplify BB as long as resimplification is requested.
6077 // Perform one round of simplifcation. Resimplify flag will be set if
6078 // another iteration is requested.
6079 Changed |= simplifyOnce(BB);
6080 } while (Resimplify);
6085 bool llvm::simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
6086 const SimplifyCFGOptions &Options,
6087 SmallPtrSetImpl<BasicBlock *> *LoopHeaders) {
6088 return SimplifyCFGOpt(TTI, BB->getModule()->getDataLayout(), LoopHeaders,