1 //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Peephole optimize the CFG.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SetOperations.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Analysis/EHPersonalities.h"
27 #include "llvm/Analysis/GuardUtils.h"
28 #include "llvm/Analysis/InstructionSimplify.h"
29 #include "llvm/Analysis/MemorySSA.h"
30 #include "llvm/Analysis/MemorySSAUpdater.h"
31 #include "llvm/Analysis/TargetTransformInfo.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/IR/Attributes.h"
34 #include "llvm/IR/BasicBlock.h"
35 #include "llvm/IR/CFG.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/Local.h"
70 #include "llvm/Transforms/Utils/ValueMapper.h"
71 #include <algorithm>
72 #include <cassert>
73 #include <climits>
74 #include <cstddef>
75 #include <cstdint>
76 #include <iterator>
77 #include <map>
78 #include <set>
79 #include <tuple>
80 #include <utility>
81 #include <vector>
82 
83 using namespace llvm;
84 using namespace PatternMatch;
85 
86 #define DEBUG_TYPE "simplifycfg"
87 
88 // Chosen as 2 so as to be cheap, but still to have enough power to fold
89 // a select, so the "clamp" idiom (of a min followed by a max) will be caught.
90 // To catch this, we need to fold a compare and a select, hence '2' being the
91 // minimum reasonable default.
92 static cl::opt<unsigned> PHINodeFoldingThreshold(
93     "phi-node-folding-threshold", cl::Hidden, cl::init(2),
94     cl::desc(
95         "Control the amount of phi node folding to perform (default = 2)"));
96 
97 static cl::opt<unsigned> TwoEntryPHINodeFoldingThreshold(
98     "two-entry-phi-node-folding-threshold", cl::Hidden, cl::init(4),
99     cl::desc("Control the maximal total instruction cost that we are willing "
100              "to speculatively execute to fold a 2-entry PHI node into a "
101              "select (default = 4)"));
102 
103 static cl::opt<bool> DupRet(
104     "simplifycfg-dup-ret", cl::Hidden, cl::init(false),
105     cl::desc("Duplicate return instructions into unconditional branches"));
106 
107 static cl::opt<bool>
108     SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true),
109                cl::desc("Sink common instructions down to the end block"));
110 
111 static cl::opt<bool> HoistCondStores(
112     "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
113     cl::desc("Hoist conditional stores if an unconditional store precedes"));
114 
115 static cl::opt<bool> MergeCondStores(
116     "simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true),
117     cl::desc("Hoist conditional stores even if an unconditional store does not "
118              "precede - hoist multiple conditional stores into a single "
119              "predicated store"));
120 
121 static cl::opt<bool> MergeCondStoresAggressively(
122     "simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false),
123     cl::desc("When merging conditional stores, do so even if the resultant "
124              "basic blocks are unlikely to be if-converted as a result"));
125 
126 static cl::opt<bool> SpeculateOneExpensiveInst(
127     "speculate-one-expensive-inst", cl::Hidden, cl::init(true),
128     cl::desc("Allow exactly one expensive instruction to be speculatively "
129              "executed"));
130 
131 static cl::opt<unsigned> MaxSpeculationDepth(
132     "max-speculation-depth", cl::Hidden, cl::init(10),
133     cl::desc("Limit maximum recursion depth when calculating costs of "
134              "speculatively executed instructions"));
135 
136 static cl::opt<int>
137 MaxSmallBlockSize("simplifycfg-max-small-block-size", cl::Hidden, cl::init(10),
138                   cl::desc("Max size of a block which is still considered "
139                            "small enough to thread through"));
140 
141 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
142 STATISTIC(NumLinearMaps,
143           "Number of switch instructions turned into linear mapping");
144 STATISTIC(NumLookupTables,
145           "Number of switch instructions turned into lookup tables");
146 STATISTIC(
147     NumLookupTablesHoles,
148     "Number of switch instructions turned into lookup tables (holes checked)");
149 STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares");
150 STATISTIC(NumSinkCommons,
151           "Number of common instructions sunk down to the end block");
152 STATISTIC(NumSpeculations, "Number of speculative executed instructions");
153 
154 namespace {
155 
156 // The first field contains the value that the switch produces when a certain
157 // case group is selected, and the second field is a vector containing the
158 // cases composing the case group.
159 using SwitchCaseResultVectorTy =
160     SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>;
161 
162 // The first field contains the phi node that generates a result of the switch
163 // and the second field contains the value generated for a certain case in the
164 // switch for that PHI.
165 using SwitchCaseResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
166 
167 /// ValueEqualityComparisonCase - Represents a case of a switch.
168 struct ValueEqualityComparisonCase {
169   ConstantInt *Value;
170   BasicBlock *Dest;
171 
172   ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
173       : Value(Value), Dest(Dest) {}
174 
175   bool operator<(ValueEqualityComparisonCase RHS) const {
176     // Comparing pointers is ok as we only rely on the order for uniquing.
177     return Value < RHS.Value;
178   }
179 
180   bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
181 };
182 
183 class SimplifyCFGOpt {
184   const TargetTransformInfo &TTI;
185   const DataLayout &DL;
186   SmallPtrSetImpl<BasicBlock *> *LoopHeaders;
187   const SimplifyCFGOptions &Options;
188   bool Resimplify;
189 
190   Value *isValueEqualityComparison(Instruction *TI);
191   BasicBlock *GetValueEqualityComparisonCases(
192       Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
193   bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
194                                                      BasicBlock *Pred,
195                                                      IRBuilder<> &Builder);
196   bool FoldValueComparisonIntoPredecessors(Instruction *TI,
197                                            IRBuilder<> &Builder);
198 
199   bool simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
200   bool simplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
201   bool simplifySingleResume(ResumeInst *RI);
202   bool simplifyCommonResume(ResumeInst *RI);
203   bool simplifyCleanupReturn(CleanupReturnInst *RI);
204   bool simplifyUnreachable(UnreachableInst *UI);
205   bool simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
206   bool simplifyIndirectBr(IndirectBrInst *IBI);
207   bool simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder);
208   bool simplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder);
209   bool simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder);
210   bool SimplifyCondBranchToTwoReturns(BranchInst *BI, IRBuilder<> &Builder);
211 
212   bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
213                                              IRBuilder<> &Builder);
214 
215   bool HoistThenElseCodeToIf(BranchInst *BI, const TargetTransformInfo &TTI);
216   bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
217                               const TargetTransformInfo &TTI);
218   bool SimplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond,
219                                   BasicBlock *TrueBB, BasicBlock *FalseBB,
220                                   uint32_t TrueWeight, uint32_t FalseWeight);
221   bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder,
222                                  const DataLayout &DL);
223   bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select);
224   bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI);
225   bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder);
226 
227 public:
228   SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout &DL,
229                  SmallPtrSetImpl<BasicBlock *> *LoopHeaders,
230                  const SimplifyCFGOptions &Opts)
231       : TTI(TTI), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {}
232 
233   bool run(BasicBlock *BB);
234   bool simplifyOnce(BasicBlock *BB);
235 
236   // Helper to set Resimplify and return change indication.
237   bool requestResimplify() {
238     Resimplify = true;
239     return true;
240   }
241 };
242 
243 } // end anonymous namespace
244 
245 /// Return true if it is safe to merge these two
246 /// terminator instructions together.
247 static bool
248 SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
249                        SmallSetVector<BasicBlock *, 4> *FailBlocks = nullptr) {
250   if (SI1 == SI2)
251     return false; // Can't merge with self!
252 
253   // It is not safe to merge these two switch instructions if they have a common
254   // successor, and if that successor has a PHI node, and if *that* PHI node has
255   // conflicting incoming values from the two switch blocks.
256   BasicBlock *SI1BB = SI1->getParent();
257   BasicBlock *SI2BB = SI2->getParent();
258 
259   SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
260   bool Fail = false;
261   for (BasicBlock *Succ : successors(SI2BB))
262     if (SI1Succs.count(Succ))
263       for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
264         PHINode *PN = cast<PHINode>(BBI);
265         if (PN->getIncomingValueForBlock(SI1BB) !=
266             PN->getIncomingValueForBlock(SI2BB)) {
267           if (FailBlocks)
268             FailBlocks->insert(Succ);
269           Fail = true;
270         }
271       }
272 
273   return !Fail;
274 }
275 
276 /// Return true if it is safe and profitable to merge these two terminator
277 /// instructions together, where SI1 is an unconditional branch. PhiNodes will
278 /// store all PHI nodes in common successors.
279 static bool
280 isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2,
281                                 Instruction *Cond,
282                                 SmallVectorImpl<PHINode *> &PhiNodes) {
283   if (SI1 == SI2)
284     return false; // Can't merge with self!
285   assert(SI1->isUnconditional() && SI2->isConditional());
286 
287   // We fold the unconditional branch if we can easily update all PHI nodes in
288   // common successors:
289   // 1> We have a constant incoming value for the conditional branch;
290   // 2> We have "Cond" as the incoming value for the unconditional branch;
291   // 3> SI2->getCondition() and Cond have same operands.
292   CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
293   if (!Ci2)
294     return false;
295   if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
296         Cond->getOperand(1) == Ci2->getOperand(1)) &&
297       !(Cond->getOperand(0) == Ci2->getOperand(1) &&
298         Cond->getOperand(1) == Ci2->getOperand(0)))
299     return false;
300 
301   BasicBlock *SI1BB = SI1->getParent();
302   BasicBlock *SI2BB = SI2->getParent();
303   SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
304   for (BasicBlock *Succ : successors(SI2BB))
305     if (SI1Succs.count(Succ))
306       for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
307         PHINode *PN = cast<PHINode>(BBI);
308         if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
309             !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
310           return false;
311         PhiNodes.push_back(PN);
312       }
313   return true;
314 }
315 
316 /// Update PHI nodes in Succ to indicate that there will now be entries in it
317 /// from the 'NewPred' block. The values that will be flowing into the PHI nodes
318 /// will be the same as those coming in from ExistPred, an existing predecessor
319 /// of Succ.
320 static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
321                                   BasicBlock *ExistPred,
322                                   MemorySSAUpdater *MSSAU = nullptr) {
323   for (PHINode &PN : Succ->phis())
324     PN.addIncoming(PN.getIncomingValueForBlock(ExistPred), NewPred);
325   if (MSSAU)
326     if (auto *MPhi = MSSAU->getMemorySSA()->getMemoryAccess(Succ))
327       MPhi->addIncoming(MPhi->getIncomingValueForBlock(ExistPred), NewPred);
328 }
329 
330 /// Compute an abstract "cost" of speculating the given instruction,
331 /// which is assumed to be safe to speculate. TCC_Free means cheap,
332 /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
333 /// expensive.
334 static unsigned ComputeSpeculationCost(const User *I,
335                                        const TargetTransformInfo &TTI) {
336   assert(isSafeToSpeculativelyExecute(I) &&
337          "Instruction is not safe to speculatively execute!");
338   return TTI.getUserCost(I, TargetTransformInfo::TCK_SizeAndLatency);
339 }
340 
341 /// If we have a merge point of an "if condition" as accepted above,
342 /// return true if the specified value dominates the block.  We
343 /// don't handle the true generality of domination here, just a special case
344 /// which works well enough for us.
345 ///
346 /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
347 /// see if V (which must be an instruction) and its recursive operands
348 /// that do not dominate BB have a combined cost lower than CostRemaining and
349 /// are non-trapping.  If both are true, the instruction is inserted into the
350 /// set and true is returned.
351 ///
352 /// The cost for most non-trapping instructions is defined as 1 except for
353 /// Select whose cost is 2.
354 ///
355 /// After this function returns, CostRemaining is decreased by the cost of
356 /// V plus its non-dominating operands.  If that cost is greater than
357 /// CostRemaining, false is returned and CostRemaining is undefined.
358 static bool DominatesMergePoint(Value *V, BasicBlock *BB,
359                                 SmallPtrSetImpl<Instruction *> &AggressiveInsts,
360                                 int &BudgetRemaining,
361                                 const TargetTransformInfo &TTI,
362                                 unsigned Depth = 0) {
363   // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
364   // so limit the recursion depth.
365   // TODO: While this recursion limit does prevent pathological behavior, it
366   // would be better to track visited instructions to avoid cycles.
367   if (Depth == MaxSpeculationDepth)
368     return false;
369 
370   Instruction *I = dyn_cast<Instruction>(V);
371   if (!I) {
372     // Non-instructions all dominate instructions, but not all constantexprs
373     // can be executed unconditionally.
374     if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
375       if (C->canTrap())
376         return false;
377     return true;
378   }
379   BasicBlock *PBB = I->getParent();
380 
381   // We don't want to allow weird loops that might have the "if condition" in
382   // the bottom of this block.
383   if (PBB == BB)
384     return false;
385 
386   // If this instruction is defined in a block that contains an unconditional
387   // branch to BB, then it must be in the 'conditional' part of the "if
388   // statement".  If not, it definitely dominates the region.
389   BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
390   if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
391     return true;
392 
393   // If we have seen this instruction before, don't count it again.
394   if (AggressiveInsts.count(I))
395     return true;
396 
397   // Okay, it looks like the instruction IS in the "condition".  Check to
398   // see if it's a cheap instruction to unconditionally compute, and if it
399   // only uses stuff defined outside of the condition.  If so, hoist it out.
400   if (!isSafeToSpeculativelyExecute(I))
401     return false;
402 
403   BudgetRemaining -= ComputeSpeculationCost(I, TTI);
404 
405   // Allow exactly one instruction to be speculated regardless of its cost
406   // (as long as it is safe to do so).
407   // This is intended to flatten the CFG even if the instruction is a division
408   // or other expensive operation. The speculation of an expensive instruction
409   // is expected to be undone in CodeGenPrepare if the speculation has not
410   // enabled further IR optimizations.
411   if (BudgetRemaining < 0 &&
412       (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0))
413     return false;
414 
415   // Okay, we can only really hoist these out if their operands do
416   // not take us over the cost threshold.
417   for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
418     if (!DominatesMergePoint(*i, BB, AggressiveInsts, BudgetRemaining, TTI,
419                              Depth + 1))
420       return false;
421   // Okay, it's safe to do this!  Remember this instruction.
422   AggressiveInsts.insert(I);
423   return true;
424 }
425 
426 /// Extract ConstantInt from value, looking through IntToPtr
427 /// and PointerNullValue. Return NULL if value is not a constant int.
428 static ConstantInt *GetConstantInt(Value *V, const DataLayout &DL) {
429   // Normal constant int.
430   ConstantInt *CI = dyn_cast<ConstantInt>(V);
431   if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy())
432     return CI;
433 
434   // This is some kind of pointer constant. Turn it into a pointer-sized
435   // ConstantInt if possible.
436   IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
437 
438   // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
439   if (isa<ConstantPointerNull>(V))
440     return ConstantInt::get(PtrTy, 0);
441 
442   // IntToPtr const int.
443   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
444     if (CE->getOpcode() == Instruction::IntToPtr)
445       if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
446         // The constant is very likely to have the right type already.
447         if (CI->getType() == PtrTy)
448           return CI;
449         else
450           return cast<ConstantInt>(
451               ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
452       }
453   return nullptr;
454 }
455 
456 namespace {
457 
458 /// Given a chain of or (||) or and (&&) comparison of a value against a
459 /// constant, this will try to recover the information required for a switch
460 /// structure.
461 /// It will depth-first traverse the chain of comparison, seeking for patterns
462 /// like %a == 12 or %a < 4 and combine them to produce a set of integer
463 /// representing the different cases for the switch.
464 /// Note that if the chain is composed of '||' it will build the set of elements
465 /// that matches the comparisons (i.e. any of this value validate the chain)
466 /// while for a chain of '&&' it will build the set elements that make the test
467 /// fail.
468 struct ConstantComparesGatherer {
469   const DataLayout &DL;
470 
471   /// Value found for the switch comparison
472   Value *CompValue = nullptr;
473 
474   /// Extra clause to be checked before the switch
475   Value *Extra = nullptr;
476 
477   /// Set of integers to match in switch
478   SmallVector<ConstantInt *, 8> Vals;
479 
480   /// Number of comparisons matched in the and/or chain
481   unsigned UsedICmps = 0;
482 
483   /// Construct and compute the result for the comparison instruction Cond
484   ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
485     gather(Cond);
486   }
487 
488   ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
489   ConstantComparesGatherer &
490   operator=(const ConstantComparesGatherer &) = delete;
491 
492 private:
493   /// Try to set the current value used for the comparison, it succeeds only if
494   /// it wasn't set before or if the new value is the same as the old one
495   bool setValueOnce(Value *NewVal) {
496     if (CompValue && CompValue != NewVal)
497       return false;
498     CompValue = NewVal;
499     return (CompValue != nullptr);
500   }
501 
502   /// Try to match Instruction "I" as a comparison against a constant and
503   /// populates the array Vals with the set of values that match (or do not
504   /// match depending on isEQ).
505   /// Return false on failure. On success, the Value the comparison matched
506   /// against is placed in CompValue.
507   /// If CompValue is already set, the function is expected to fail if a match
508   /// is found but the value compared to is different.
509   bool matchInstruction(Instruction *I, bool isEQ) {
510     // If this is an icmp against a constant, handle this as one of the cases.
511     ICmpInst *ICI;
512     ConstantInt *C;
513     if (!((ICI = dyn_cast<ICmpInst>(I)) &&
514           (C = GetConstantInt(I->getOperand(1), DL)))) {
515       return false;
516     }
517 
518     Value *RHSVal;
519     const APInt *RHSC;
520 
521     // Pattern match a special case
522     // (x & ~2^z) == y --> x == y || x == y|2^z
523     // This undoes a transformation done by instcombine to fuse 2 compares.
524     if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
525       // It's a little bit hard to see why the following transformations are
526       // correct. Here is a CVC3 program to verify them for 64-bit values:
527 
528       /*
529          ONE  : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
530          x    : BITVECTOR(64);
531          y    : BITVECTOR(64);
532          z    : BITVECTOR(64);
533          mask : BITVECTOR(64) = BVSHL(ONE, z);
534          QUERY( (y & ~mask = y) =>
535                 ((x & ~mask = y) <=> (x = y OR x = (y |  mask)))
536          );
537          QUERY( (y |  mask = y) =>
538                 ((x |  mask = y) <=> (x = y OR x = (y & ~mask)))
539          );
540       */
541 
542       // Please note that each pattern must be a dual implication (<--> or
543       // iff). One directional implication can create spurious matches. If the
544       // implication is only one-way, an unsatisfiable condition on the left
545       // side can imply a satisfiable condition on the right side. Dual
546       // implication ensures that satisfiable conditions are transformed to
547       // other satisfiable conditions and unsatisfiable conditions are
548       // transformed to other unsatisfiable conditions.
549 
550       // Here is a concrete example of a unsatisfiable condition on the left
551       // implying a satisfiable condition on the right:
552       //
553       // mask = (1 << z)
554       // (x & ~mask) == y  --> (x == y || x == (y | mask))
555       //
556       // Substituting y = 3, z = 0 yields:
557       // (x & -2) == 3 --> (x == 3 || x == 2)
558 
559       // Pattern match a special case:
560       /*
561         QUERY( (y & ~mask = y) =>
562                ((x & ~mask = y) <=> (x = y OR x = (y |  mask)))
563         );
564       */
565       if (match(ICI->getOperand(0),
566                 m_And(m_Value(RHSVal), m_APInt(RHSC)))) {
567         APInt Mask = ~*RHSC;
568         if (Mask.isPowerOf2() && (C->getValue() & ~Mask) == C->getValue()) {
569           // If we already have a value for the switch, it has to match!
570           if (!setValueOnce(RHSVal))
571             return false;
572 
573           Vals.push_back(C);
574           Vals.push_back(
575               ConstantInt::get(C->getContext(),
576                                C->getValue() | Mask));
577           UsedICmps++;
578           return true;
579         }
580       }
581 
582       // Pattern match a special case:
583       /*
584         QUERY( (y |  mask = y) =>
585                ((x |  mask = y) <=> (x = y OR x = (y & ~mask)))
586         );
587       */
588       if (match(ICI->getOperand(0),
589                 m_Or(m_Value(RHSVal), m_APInt(RHSC)))) {
590         APInt Mask = *RHSC;
591         if (Mask.isPowerOf2() && (C->getValue() | Mask) == C->getValue()) {
592           // If we already have a value for the switch, it has to match!
593           if (!setValueOnce(RHSVal))
594             return false;
595 
596           Vals.push_back(C);
597           Vals.push_back(ConstantInt::get(C->getContext(),
598                                           C->getValue() & ~Mask));
599           UsedICmps++;
600           return true;
601         }
602       }
603 
604       // If we already have a value for the switch, it has to match!
605       if (!setValueOnce(ICI->getOperand(0)))
606         return false;
607 
608       UsedICmps++;
609       Vals.push_back(C);
610       return ICI->getOperand(0);
611     }
612 
613     // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
614     ConstantRange Span = ConstantRange::makeAllowedICmpRegion(
615         ICI->getPredicate(), C->getValue());
616 
617     // Shift the range if the compare is fed by an add. This is the range
618     // compare idiom as emitted by instcombine.
619     Value *CandidateVal = I->getOperand(0);
620     if (match(I->getOperand(0), m_Add(m_Value(RHSVal), m_APInt(RHSC)))) {
621       Span = Span.subtract(*RHSC);
622       CandidateVal = RHSVal;
623     }
624 
625     // If this is an and/!= check, then we are looking to build the set of
626     // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
627     // x != 0 && x != 1.
628     if (!isEQ)
629       Span = Span.inverse();
630 
631     // If there are a ton of values, we don't want to make a ginormous switch.
632     if (Span.isSizeLargerThan(8) || Span.isEmptySet()) {
633       return false;
634     }
635 
636     // If we already have a value for the switch, it has to match!
637     if (!setValueOnce(CandidateVal))
638       return false;
639 
640     // Add all values from the range to the set
641     for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
642       Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
643 
644     UsedICmps++;
645     return true;
646   }
647 
648   /// Given a potentially 'or'd or 'and'd together collection of icmp
649   /// eq/ne/lt/gt instructions that compare a value against a constant, extract
650   /// the value being compared, and stick the list constants into the Vals
651   /// vector.
652   /// One "Extra" case is allowed to differ from the other.
653   void gather(Value *V) {
654     bool isEQ = (cast<Instruction>(V)->getOpcode() == Instruction::Or);
655 
656     // Keep a stack (SmallVector for efficiency) for depth-first traversal
657     SmallVector<Value *, 8> DFT;
658     SmallPtrSet<Value *, 8> Visited;
659 
660     // Initialize
661     Visited.insert(V);
662     DFT.push_back(V);
663 
664     while (!DFT.empty()) {
665       V = DFT.pop_back_val();
666 
667       if (Instruction *I = dyn_cast<Instruction>(V)) {
668         // If it is a || (or && depending on isEQ), process the operands.
669         if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) {
670           if (Visited.insert(I->getOperand(1)).second)
671             DFT.push_back(I->getOperand(1));
672           if (Visited.insert(I->getOperand(0)).second)
673             DFT.push_back(I->getOperand(0));
674           continue;
675         }
676 
677         // Try to match the current instruction
678         if (matchInstruction(I, isEQ))
679           // Match succeed, continue the loop
680           continue;
681       }
682 
683       // One element of the sequence of || (or &&) could not be match as a
684       // comparison against the same value as the others.
685       // We allow only one "Extra" case to be checked before the switch
686       if (!Extra) {
687         Extra = V;
688         continue;
689       }
690       // Failed to parse a proper sequence, abort now
691       CompValue = nullptr;
692       break;
693     }
694   }
695 };
696 
697 } // end anonymous namespace
698 
699 static void EraseTerminatorAndDCECond(Instruction *TI,
700                                       MemorySSAUpdater *MSSAU = nullptr) {
701   Instruction *Cond = nullptr;
702   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
703     Cond = dyn_cast<Instruction>(SI->getCondition());
704   } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
705     if (BI->isConditional())
706       Cond = dyn_cast<Instruction>(BI->getCondition());
707   } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
708     Cond = dyn_cast<Instruction>(IBI->getAddress());
709   }
710 
711   TI->eraseFromParent();
712   if (Cond)
713     RecursivelyDeleteTriviallyDeadInstructions(Cond, nullptr, MSSAU);
714 }
715 
716 /// Return true if the specified terminator checks
717 /// to see if a value is equal to constant integer value.
718 Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
719   Value *CV = nullptr;
720   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
721     // Do not permit merging of large switch instructions into their
722     // predecessors unless there is only one predecessor.
723     if (!SI->getParent()->hasNPredecessorsOrMore(128 / SI->getNumSuccessors()))
724       CV = SI->getCondition();
725   } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
726     if (BI->isConditional() && BI->getCondition()->hasOneUse())
727       if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
728         if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
729           CV = ICI->getOperand(0);
730       }
731 
732   // Unwrap any lossless ptrtoint cast.
733   if (CV) {
734     if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
735       Value *Ptr = PTII->getPointerOperand();
736       if (PTII->getType() == DL.getIntPtrType(Ptr->getType()))
737         CV = Ptr;
738     }
739   }
740   return CV;
741 }
742 
743 /// Given a value comparison instruction,
744 /// decode all of the 'cases' that it represents and return the 'default' block.
745 BasicBlock *SimplifyCFGOpt::GetValueEqualityComparisonCases(
746     Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
747   if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
748     Cases.reserve(SI->getNumCases());
749     for (auto Case : SI->cases())
750       Cases.push_back(ValueEqualityComparisonCase(Case.getCaseValue(),
751                                                   Case.getCaseSuccessor()));
752     return SI->getDefaultDest();
753   }
754 
755   BranchInst *BI = cast<BranchInst>(TI);
756   ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
757   BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
758   Cases.push_back(ValueEqualityComparisonCase(
759       GetConstantInt(ICI->getOperand(1), DL), Succ));
760   return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
761 }
762 
763 /// Given a vector of bb/value pairs, remove any entries
764 /// in the list that match the specified block.
765 static void
766 EliminateBlockCases(BasicBlock *BB,
767                     std::vector<ValueEqualityComparisonCase> &Cases) {
768   Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
769 }
770 
771 /// Return true if there are any keys in C1 that exist in C2 as well.
772 static bool ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
773                           std::vector<ValueEqualityComparisonCase> &C2) {
774   std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
775 
776   // Make V1 be smaller than V2.
777   if (V1->size() > V2->size())
778     std::swap(V1, V2);
779 
780   if (V1->empty())
781     return false;
782   if (V1->size() == 1) {
783     // Just scan V2.
784     ConstantInt *TheVal = (*V1)[0].Value;
785     for (unsigned i = 0, e = V2->size(); i != e; ++i)
786       if (TheVal == (*V2)[i].Value)
787         return true;
788   }
789 
790   // Otherwise, just sort both lists and compare element by element.
791   array_pod_sort(V1->begin(), V1->end());
792   array_pod_sort(V2->begin(), V2->end());
793   unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
794   while (i1 != e1 && i2 != e2) {
795     if ((*V1)[i1].Value == (*V2)[i2].Value)
796       return true;
797     if ((*V1)[i1].Value < (*V2)[i2].Value)
798       ++i1;
799     else
800       ++i2;
801   }
802   return false;
803 }
804 
805 // Set branch weights on SwitchInst. This sets the metadata if there is at
806 // least one non-zero weight.
807 static void setBranchWeights(SwitchInst *SI, ArrayRef<uint32_t> Weights) {
808   // Check that there is at least one non-zero weight. Otherwise, pass
809   // nullptr to setMetadata which will erase the existing metadata.
810   MDNode *N = nullptr;
811   if (llvm::any_of(Weights, [](uint32_t W) { return W != 0; }))
812     N = MDBuilder(SI->getParent()->getContext()).createBranchWeights(Weights);
813   SI->setMetadata(LLVMContext::MD_prof, N);
814 }
815 
816 // Similar to the above, but for branch and select instructions that take
817 // exactly 2 weights.
818 static void setBranchWeights(Instruction *I, uint32_t TrueWeight,
819                              uint32_t FalseWeight) {
820   assert(isa<BranchInst>(I) || isa<SelectInst>(I));
821   // Check that there is at least one non-zero weight. Otherwise, pass
822   // nullptr to setMetadata which will erase the existing metadata.
823   MDNode *N = nullptr;
824   if (TrueWeight || FalseWeight)
825     N = MDBuilder(I->getParent()->getContext())
826             .createBranchWeights(TrueWeight, FalseWeight);
827   I->setMetadata(LLVMContext::MD_prof, N);
828 }
829 
830 /// If TI is known to be a terminator instruction and its block is known to
831 /// only have a single predecessor block, check to see if that predecessor is
832 /// also a value comparison with the same value, and if that comparison
833 /// determines the outcome of this comparison. If so, simplify TI. This does a
834 /// very limited form of jump threading.
835 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
836     Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
837   Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
838   if (!PredVal)
839     return false; // Not a value comparison in predecessor.
840 
841   Value *ThisVal = isValueEqualityComparison(TI);
842   assert(ThisVal && "This isn't a value comparison!!");
843   if (ThisVal != PredVal)
844     return false; // Different predicates.
845 
846   // TODO: Preserve branch weight metadata, similarly to how
847   // FoldValueComparisonIntoPredecessors preserves it.
848 
849   // Find out information about when control will move from Pred to TI's block.
850   std::vector<ValueEqualityComparisonCase> PredCases;
851   BasicBlock *PredDef =
852       GetValueEqualityComparisonCases(Pred->getTerminator(), PredCases);
853   EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
854 
855   // Find information about how control leaves this block.
856   std::vector<ValueEqualityComparisonCase> ThisCases;
857   BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
858   EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
859 
860   // If TI's block is the default block from Pred's comparison, potentially
861   // simplify TI based on this knowledge.
862   if (PredDef == TI->getParent()) {
863     // If we are here, we know that the value is none of those cases listed in
864     // PredCases.  If there are any cases in ThisCases that are in PredCases, we
865     // can simplify TI.
866     if (!ValuesOverlap(PredCases, ThisCases))
867       return false;
868 
869     if (isa<BranchInst>(TI)) {
870       // Okay, one of the successors of this condbr is dead.  Convert it to a
871       // uncond br.
872       assert(ThisCases.size() == 1 && "Branch can only have one case!");
873       // Insert the new branch.
874       Instruction *NI = Builder.CreateBr(ThisDef);
875       (void)NI;
876 
877       // Remove PHI node entries for the dead edge.
878       ThisCases[0].Dest->removePredecessor(TI->getParent());
879 
880       LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
881                         << "Through successor TI: " << *TI << "Leaving: " << *NI
882                         << "\n");
883 
884       EraseTerminatorAndDCECond(TI);
885       return true;
886     }
887 
888     SwitchInstProfUpdateWrapper SI = *cast<SwitchInst>(TI);
889     // Okay, TI has cases that are statically dead, prune them away.
890     SmallPtrSet<Constant *, 16> DeadCases;
891     for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
892       DeadCases.insert(PredCases[i].Value);
893 
894     LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
895                       << "Through successor TI: " << *TI);
896 
897     for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
898       --i;
899       if (DeadCases.count(i->getCaseValue())) {
900         i->getCaseSuccessor()->removePredecessor(TI->getParent());
901         SI.removeCase(i);
902       }
903     }
904     LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
905     return true;
906   }
907 
908   // Otherwise, TI's block must correspond to some matched value.  Find out
909   // which value (or set of values) this is.
910   ConstantInt *TIV = nullptr;
911   BasicBlock *TIBB = TI->getParent();
912   for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
913     if (PredCases[i].Dest == TIBB) {
914       if (TIV)
915         return false; // Cannot handle multiple values coming to this block.
916       TIV = PredCases[i].Value;
917     }
918   assert(TIV && "No edge from pred to succ?");
919 
920   // Okay, we found the one constant that our value can be if we get into TI's
921   // BB.  Find out which successor will unconditionally be branched to.
922   BasicBlock *TheRealDest = nullptr;
923   for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
924     if (ThisCases[i].Value == TIV) {
925       TheRealDest = ThisCases[i].Dest;
926       break;
927     }
928 
929   // If not handled by any explicit cases, it is handled by the default case.
930   if (!TheRealDest)
931     TheRealDest = ThisDef;
932 
933   // Remove PHI node entries for dead edges.
934   BasicBlock *CheckEdge = TheRealDest;
935   for (BasicBlock *Succ : successors(TIBB))
936     if (Succ != CheckEdge)
937       Succ->removePredecessor(TIBB);
938     else
939       CheckEdge = nullptr;
940 
941   // Insert the new branch.
942   Instruction *NI = Builder.CreateBr(TheRealDest);
943   (void)NI;
944 
945   LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
946                     << "Through successor TI: " << *TI << "Leaving: " << *NI
947                     << "\n");
948 
949   EraseTerminatorAndDCECond(TI);
950   return true;
951 }
952 
953 namespace {
954 
955 /// This class implements a stable ordering of constant
956 /// integers that does not depend on their address.  This is important for
957 /// applications that sort ConstantInt's to ensure uniqueness.
958 struct ConstantIntOrdering {
959   bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
960     return LHS->getValue().ult(RHS->getValue());
961   }
962 };
963 
964 } // end anonymous namespace
965 
966 static int ConstantIntSortPredicate(ConstantInt *const *P1,
967                                     ConstantInt *const *P2) {
968   const ConstantInt *LHS = *P1;
969   const ConstantInt *RHS = *P2;
970   if (LHS == RHS)
971     return 0;
972   return LHS->getValue().ult(RHS->getValue()) ? 1 : -1;
973 }
974 
975 static inline bool HasBranchWeights(const Instruction *I) {
976   MDNode *ProfMD = I->getMetadata(LLVMContext::MD_prof);
977   if (ProfMD && ProfMD->getOperand(0))
978     if (MDString *MDS = dyn_cast<MDString>(ProfMD->getOperand(0)))
979       return MDS->getString().equals("branch_weights");
980 
981   return false;
982 }
983 
984 /// Get Weights of a given terminator, the default weight is at the front
985 /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
986 /// metadata.
987 static void GetBranchWeights(Instruction *TI,
988                              SmallVectorImpl<uint64_t> &Weights) {
989   MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
990   assert(MD);
991   for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
992     ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
993     Weights.push_back(CI->getValue().getZExtValue());
994   }
995 
996   // If TI is a conditional eq, the default case is the false case,
997   // and the corresponding branch-weight data is at index 2. We swap the
998   // default weight to be the first entry.
999   if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1000     assert(Weights.size() == 2);
1001     ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
1002     if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
1003       std::swap(Weights.front(), Weights.back());
1004   }
1005 }
1006 
1007 /// Keep halving the weights until all can fit in uint32_t.
1008 static void FitWeights(MutableArrayRef<uint64_t> Weights) {
1009   uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
1010   if (Max > UINT_MAX) {
1011     unsigned Offset = 32 - countLeadingZeros(Max);
1012     for (uint64_t &I : Weights)
1013       I >>= Offset;
1014   }
1015 }
1016 
1017 /// The specified terminator is a value equality comparison instruction
1018 /// (either a switch or a branch on "X == c").
1019 /// See if any of the predecessors of the terminator block are value comparisons
1020 /// on the same value.  If so, and if safe to do so, fold them together.
1021 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
1022                                                          IRBuilder<> &Builder) {
1023   BasicBlock *BB = TI->getParent();
1024   Value *CV = isValueEqualityComparison(TI); // CondVal
1025   assert(CV && "Not a comparison?");
1026   bool Changed = false;
1027 
1028   SmallVector<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
1029   while (!Preds.empty()) {
1030     BasicBlock *Pred = Preds.pop_back_val();
1031 
1032     // See if the predecessor is a comparison with the same value.
1033     Instruction *PTI = Pred->getTerminator();
1034     Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
1035 
1036     if (PCV == CV && TI != PTI) {
1037       SmallSetVector<BasicBlock*, 4> FailBlocks;
1038       if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
1039         for (auto *Succ : FailBlocks) {
1040           if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split"))
1041             return false;
1042         }
1043       }
1044 
1045       // Figure out which 'cases' to copy from SI to PSI.
1046       std::vector<ValueEqualityComparisonCase> BBCases;
1047       BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
1048 
1049       std::vector<ValueEqualityComparisonCase> PredCases;
1050       BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
1051 
1052       // Based on whether the default edge from PTI goes to BB or not, fill in
1053       // PredCases and PredDefault with the new switch cases we would like to
1054       // build.
1055       SmallVector<BasicBlock *, 8> NewSuccessors;
1056 
1057       // Update the branch weight metadata along the way
1058       SmallVector<uint64_t, 8> Weights;
1059       bool PredHasWeights = HasBranchWeights(PTI);
1060       bool SuccHasWeights = HasBranchWeights(TI);
1061 
1062       if (PredHasWeights) {
1063         GetBranchWeights(PTI, Weights);
1064         // branch-weight metadata is inconsistent here.
1065         if (Weights.size() != 1 + PredCases.size())
1066           PredHasWeights = SuccHasWeights = false;
1067       } else if (SuccHasWeights)
1068         // If there are no predecessor weights but there are successor weights,
1069         // populate Weights with 1, which will later be scaled to the sum of
1070         // successor's weights
1071         Weights.assign(1 + PredCases.size(), 1);
1072 
1073       SmallVector<uint64_t, 8> SuccWeights;
1074       if (SuccHasWeights) {
1075         GetBranchWeights(TI, SuccWeights);
1076         // branch-weight metadata is inconsistent here.
1077         if (SuccWeights.size() != 1 + BBCases.size())
1078           PredHasWeights = SuccHasWeights = false;
1079       } else if (PredHasWeights)
1080         SuccWeights.assign(1 + BBCases.size(), 1);
1081 
1082       if (PredDefault == BB) {
1083         // If this is the default destination from PTI, only the edges in TI
1084         // that don't occur in PTI, or that branch to BB will be activated.
1085         std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1086         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1087           if (PredCases[i].Dest != BB)
1088             PTIHandled.insert(PredCases[i].Value);
1089           else {
1090             // The default destination is BB, we don't need explicit targets.
1091             std::swap(PredCases[i], PredCases.back());
1092 
1093             if (PredHasWeights || SuccHasWeights) {
1094               // Increase weight for the default case.
1095               Weights[0] += Weights[i + 1];
1096               std::swap(Weights[i + 1], Weights.back());
1097               Weights.pop_back();
1098             }
1099 
1100             PredCases.pop_back();
1101             --i;
1102             --e;
1103           }
1104 
1105         // Reconstruct the new switch statement we will be building.
1106         if (PredDefault != BBDefault) {
1107           PredDefault->removePredecessor(Pred);
1108           PredDefault = BBDefault;
1109           NewSuccessors.push_back(BBDefault);
1110         }
1111 
1112         unsigned CasesFromPred = Weights.size();
1113         uint64_t ValidTotalSuccWeight = 0;
1114         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1115           if (!PTIHandled.count(BBCases[i].Value) &&
1116               BBCases[i].Dest != BBDefault) {
1117             PredCases.push_back(BBCases[i]);
1118             NewSuccessors.push_back(BBCases[i].Dest);
1119             if (SuccHasWeights || PredHasWeights) {
1120               // The default weight is at index 0, so weight for the ith case
1121               // should be at index i+1. Scale the cases from successor by
1122               // PredDefaultWeight (Weights[0]).
1123               Weights.push_back(Weights[0] * SuccWeights[i + 1]);
1124               ValidTotalSuccWeight += SuccWeights[i + 1];
1125             }
1126           }
1127 
1128         if (SuccHasWeights || PredHasWeights) {
1129           ValidTotalSuccWeight += SuccWeights[0];
1130           // Scale the cases from predecessor by ValidTotalSuccWeight.
1131           for (unsigned i = 1; i < CasesFromPred; ++i)
1132             Weights[i] *= ValidTotalSuccWeight;
1133           // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
1134           Weights[0] *= SuccWeights[0];
1135         }
1136       } else {
1137         // If this is not the default destination from PSI, only the edges
1138         // in SI that occur in PSI with a destination of BB will be
1139         // activated.
1140         std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1141         std::map<ConstantInt *, uint64_t> WeightsForHandled;
1142         for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
1143           if (PredCases[i].Dest == BB) {
1144             PTIHandled.insert(PredCases[i].Value);
1145 
1146             if (PredHasWeights || SuccHasWeights) {
1147               WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
1148               std::swap(Weights[i + 1], Weights.back());
1149               Weights.pop_back();
1150             }
1151 
1152             std::swap(PredCases[i], PredCases.back());
1153             PredCases.pop_back();
1154             --i;
1155             --e;
1156           }
1157 
1158         // Okay, now we know which constants were sent to BB from the
1159         // predecessor.  Figure out where they will all go now.
1160         for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
1161           if (PTIHandled.count(BBCases[i].Value)) {
1162             // If this is one we are capable of getting...
1163             if (PredHasWeights || SuccHasWeights)
1164               Weights.push_back(WeightsForHandled[BBCases[i].Value]);
1165             PredCases.push_back(BBCases[i]);
1166             NewSuccessors.push_back(BBCases[i].Dest);
1167             PTIHandled.erase(
1168                 BBCases[i].Value); // This constant is taken care of
1169           }
1170 
1171         // If there are any constants vectored to BB that TI doesn't handle,
1172         // they must go to the default destination of TI.
1173         for (ConstantInt *I : PTIHandled) {
1174           if (PredHasWeights || SuccHasWeights)
1175             Weights.push_back(WeightsForHandled[I]);
1176           PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
1177           NewSuccessors.push_back(BBDefault);
1178         }
1179       }
1180 
1181       // Okay, at this point, we know which new successor Pred will get.  Make
1182       // sure we update the number of entries in the PHI nodes for these
1183       // successors.
1184       for (BasicBlock *NewSuccessor : NewSuccessors)
1185         AddPredecessorToBlock(NewSuccessor, Pred, BB);
1186 
1187       Builder.SetInsertPoint(PTI);
1188       // Convert pointer to int before we switch.
1189       if (CV->getType()->isPointerTy()) {
1190         CV = Builder.CreatePtrToInt(CV, DL.getIntPtrType(CV->getType()),
1191                                     "magicptr");
1192       }
1193 
1194       // Now that the successors are updated, create the new Switch instruction.
1195       SwitchInst *NewSI =
1196           Builder.CreateSwitch(CV, PredDefault, PredCases.size());
1197       NewSI->setDebugLoc(PTI->getDebugLoc());
1198       for (ValueEqualityComparisonCase &V : PredCases)
1199         NewSI->addCase(V.Value, V.Dest);
1200 
1201       if (PredHasWeights || SuccHasWeights) {
1202         // Halve the weights if any of them cannot fit in an uint32_t
1203         FitWeights(Weights);
1204 
1205         SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
1206 
1207         setBranchWeights(NewSI, MDWeights);
1208       }
1209 
1210       EraseTerminatorAndDCECond(PTI);
1211 
1212       // Okay, last check.  If BB is still a successor of PSI, then we must
1213       // have an infinite loop case.  If so, add an infinitely looping block
1214       // to handle the case to preserve the behavior of the code.
1215       BasicBlock *InfLoopBlock = nullptr;
1216       for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1217         if (NewSI->getSuccessor(i) == BB) {
1218           if (!InfLoopBlock) {
1219             // Insert it at the end of the function, because it's either code,
1220             // or it won't matter if it's hot. :)
1221             InfLoopBlock = BasicBlock::Create(BB->getContext(), "infloop",
1222                                               BB->getParent());
1223             BranchInst::Create(InfLoopBlock, InfLoopBlock);
1224           }
1225           NewSI->setSuccessor(i, InfLoopBlock);
1226         }
1227 
1228       Changed = true;
1229     }
1230   }
1231   return Changed;
1232 }
1233 
1234 // If we would need to insert a select that uses the value of this invoke
1235 // (comments in HoistThenElseCodeToIf explain why we would need to do this), we
1236 // can't hoist the invoke, as there is nowhere to put the select in this case.
1237 static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
1238                                 Instruction *I1, Instruction *I2) {
1239   for (BasicBlock *Succ : successors(BB1)) {
1240     for (const PHINode &PN : Succ->phis()) {
1241       Value *BB1V = PN.getIncomingValueForBlock(BB1);
1242       Value *BB2V = PN.getIncomingValueForBlock(BB2);
1243       if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1244         return false;
1245       }
1246     }
1247   }
1248   return true;
1249 }
1250 
1251 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I);
1252 
1253 /// Given a conditional branch that goes to BB1 and BB2, hoist any common code
1254 /// in the two blocks up into the branch block. The caller of this function
1255 /// guarantees that BI's block dominates BB1 and BB2.
1256 bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst *BI,
1257                                            const TargetTransformInfo &TTI) {
1258   // This does very trivial matching, with limited scanning, to find identical
1259   // instructions in the two blocks.  In particular, we don't want to get into
1260   // O(M*N) situations here where M and N are the sizes of BB1 and BB2.  As
1261   // such, we currently just scan for obviously identical instructions in an
1262   // identical order.
1263   BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
1264   BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
1265 
1266   BasicBlock::iterator BB1_Itr = BB1->begin();
1267   BasicBlock::iterator BB2_Itr = BB2->begin();
1268 
1269   Instruction *I1 = &*BB1_Itr++, *I2 = &*BB2_Itr++;
1270   // Skip debug info if it is not identical.
1271   DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1272   DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1273   if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1274     while (isa<DbgInfoIntrinsic>(I1))
1275       I1 = &*BB1_Itr++;
1276     while (isa<DbgInfoIntrinsic>(I2))
1277       I2 = &*BB2_Itr++;
1278   }
1279   // FIXME: Can we define a safety predicate for CallBr?
1280   if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
1281       (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)) ||
1282       isa<CallBrInst>(I1))
1283     return false;
1284 
1285   BasicBlock *BIParent = BI->getParent();
1286 
1287   bool Changed = false;
1288   do {
1289     // If we are hoisting the terminator instruction, don't move one (making a
1290     // broken BB), instead clone it, and remove BI.
1291     if (I1->isTerminator())
1292       goto HoistTerminator;
1293 
1294     // If we're going to hoist a call, make sure that the two instructions we're
1295     // commoning/hoisting are both marked with musttail, or neither of them is
1296     // marked as such. Otherwise, we might end up in a situation where we hoist
1297     // from a block where the terminator is a `ret` to a block where the terminator
1298     // is a `br`, and `musttail` calls expect to be followed by a return.
1299     auto *C1 = dyn_cast<CallInst>(I1);
1300     auto *C2 = dyn_cast<CallInst>(I2);
1301     if (C1 && C2)
1302       if (C1->isMustTailCall() != C2->isMustTailCall())
1303         return Changed;
1304 
1305     if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2))
1306       return Changed;
1307 
1308     // If any of the two call sites has nomerge attribute, stop hoisting.
1309     if (const auto *CB1 = dyn_cast<CallBase>(I1))
1310       if (CB1->cannotMerge())
1311         return Changed;
1312     if (const auto *CB2 = dyn_cast<CallBase>(I2))
1313       if (CB2->cannotMerge())
1314         return Changed;
1315 
1316     if (isa<DbgInfoIntrinsic>(I1) || isa<DbgInfoIntrinsic>(I2)) {
1317       assert (isa<DbgInfoIntrinsic>(I1) && isa<DbgInfoIntrinsic>(I2));
1318       // The debug location is an integral part of a debug info intrinsic
1319       // and can't be separated from it or replaced.  Instead of attempting
1320       // to merge locations, simply hoist both copies of the intrinsic.
1321       BIParent->getInstList().splice(BI->getIterator(),
1322                                      BB1->getInstList(), I1);
1323       BIParent->getInstList().splice(BI->getIterator(),
1324                                      BB2->getInstList(), I2);
1325       Changed = true;
1326     } else {
1327       // For a normal instruction, we just move one to right before the branch,
1328       // then replace all uses of the other with the first.  Finally, we remove
1329       // the now redundant second instruction.
1330       BIParent->getInstList().splice(BI->getIterator(),
1331                                      BB1->getInstList(), I1);
1332       if (!I2->use_empty())
1333         I2->replaceAllUsesWith(I1);
1334       I1->andIRFlags(I2);
1335       unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
1336                              LLVMContext::MD_range,
1337                              LLVMContext::MD_fpmath,
1338                              LLVMContext::MD_invariant_load,
1339                              LLVMContext::MD_nonnull,
1340                              LLVMContext::MD_invariant_group,
1341                              LLVMContext::MD_align,
1342                              LLVMContext::MD_dereferenceable,
1343                              LLVMContext::MD_dereferenceable_or_null,
1344                              LLVMContext::MD_mem_parallel_loop_access,
1345                              LLVMContext::MD_access_group,
1346                              LLVMContext::MD_preserve_access_index};
1347       combineMetadata(I1, I2, KnownIDs, true);
1348 
1349       // I1 and I2 are being combined into a single instruction.  Its debug
1350       // location is the merged locations of the original instructions.
1351       I1->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1352 
1353       I2->eraseFromParent();
1354       Changed = true;
1355     }
1356 
1357     I1 = &*BB1_Itr++;
1358     I2 = &*BB2_Itr++;
1359     // Skip debug info if it is not identical.
1360     DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
1361     DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
1362     if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1363       while (isa<DbgInfoIntrinsic>(I1))
1364         I1 = &*BB1_Itr++;
1365       while (isa<DbgInfoIntrinsic>(I2))
1366         I2 = &*BB2_Itr++;
1367     }
1368   } while (I1->isIdenticalToWhenDefined(I2));
1369 
1370   return true;
1371 
1372 HoistTerminator:
1373   // It may not be possible to hoist an invoke.
1374   // FIXME: Can we define a safety predicate for CallBr?
1375   if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
1376     return Changed;
1377 
1378   // TODO: callbr hoisting currently disabled pending further study.
1379   if (isa<CallBrInst>(I1))
1380     return Changed;
1381 
1382   for (BasicBlock *Succ : successors(BB1)) {
1383     for (PHINode &PN : Succ->phis()) {
1384       Value *BB1V = PN.getIncomingValueForBlock(BB1);
1385       Value *BB2V = PN.getIncomingValueForBlock(BB2);
1386       if (BB1V == BB2V)
1387         continue;
1388 
1389       // Check for passingValueIsAlwaysUndefined here because we would rather
1390       // eliminate undefined control flow then converting it to a select.
1391       if (passingValueIsAlwaysUndefined(BB1V, &PN) ||
1392           passingValueIsAlwaysUndefined(BB2V, &PN))
1393         return Changed;
1394 
1395       if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V))
1396         return Changed;
1397       if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V))
1398         return Changed;
1399     }
1400   }
1401 
1402   // Okay, it is safe to hoist the terminator.
1403   Instruction *NT = I1->clone();
1404   BIParent->getInstList().insert(BI->getIterator(), NT);
1405   if (!NT->getType()->isVoidTy()) {
1406     I1->replaceAllUsesWith(NT);
1407     I2->replaceAllUsesWith(NT);
1408     NT->takeName(I1);
1409   }
1410 
1411   // Ensure terminator gets a debug location, even an unknown one, in case
1412   // it involves inlinable calls.
1413   NT->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1414 
1415   // PHIs created below will adopt NT's merged DebugLoc.
1416   IRBuilder<NoFolder> Builder(NT);
1417 
1418   // Hoisting one of the terminators from our successor is a great thing.
1419   // Unfortunately, the successors of the if/else blocks may have PHI nodes in
1420   // them.  If they do, all PHI entries for BB1/BB2 must agree for all PHI
1421   // nodes, so we insert select instruction to compute the final result.
1422   std::map<std::pair<Value *, Value *>, SelectInst *> InsertedSelects;
1423   for (BasicBlock *Succ : successors(BB1)) {
1424     for (PHINode &PN : Succ->phis()) {
1425       Value *BB1V = PN.getIncomingValueForBlock(BB1);
1426       Value *BB2V = PN.getIncomingValueForBlock(BB2);
1427       if (BB1V == BB2V)
1428         continue;
1429 
1430       // These values do not agree.  Insert a select instruction before NT
1431       // that determines the right value.
1432       SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1433       if (!SI) {
1434         // Propagate fast-math-flags from phi node to its replacement select.
1435         IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
1436         if (isa<FPMathOperator>(PN))
1437           Builder.setFastMathFlags(PN.getFastMathFlags());
1438 
1439         SI = cast<SelectInst>(
1440             Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
1441                                  BB1V->getName() + "." + BB2V->getName(), BI));
1442       }
1443 
1444       // Make the PHI node use the select for all incoming values for BB1/BB2
1445       for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1446         if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
1447           PN.setIncomingValue(i, SI);
1448     }
1449   }
1450 
1451   // Update any PHI nodes in our new successors.
1452   for (BasicBlock *Succ : successors(BB1))
1453     AddPredecessorToBlock(Succ, BIParent, BB1);
1454 
1455   EraseTerminatorAndDCECond(BI);
1456   return true;
1457 }
1458 
1459 // Check lifetime markers.
1460 static bool isLifeTimeMarker(const Instruction *I) {
1461   if (auto II = dyn_cast<IntrinsicInst>(I)) {
1462     switch (II->getIntrinsicID()) {
1463     default:
1464       break;
1465     case Intrinsic::lifetime_start:
1466     case Intrinsic::lifetime_end:
1467       return true;
1468     }
1469   }
1470   return false;
1471 }
1472 
1473 // TODO: Refine this. This should avoid cases like turning constant memcpy sizes
1474 // into variables.
1475 static bool replacingOperandWithVariableIsCheap(const Instruction *I,
1476                                                 int OpIdx) {
1477   return !isa<IntrinsicInst>(I);
1478 }
1479 
1480 // All instructions in Insts belong to different blocks that all unconditionally
1481 // branch to a common successor. Analyze each instruction and return true if it
1482 // would be possible to sink them into their successor, creating one common
1483 // instruction instead. For every value that would be required to be provided by
1484 // PHI node (because an operand varies in each input block), add to PHIOperands.
1485 static bool canSinkInstructions(
1486     ArrayRef<Instruction *> Insts,
1487     DenseMap<Instruction *, SmallVector<Value *, 4>> &PHIOperands) {
1488   // Prune out obviously bad instructions to move. Each instruction must have
1489   // exactly zero or one use, and we check later that use is by a single, common
1490   // PHI instruction in the successor.
1491   bool HasUse = !Insts.front()->user_empty();
1492   for (auto *I : Insts) {
1493     // These instructions may change or break semantics if moved.
1494     if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
1495         I->getType()->isTokenTy())
1496       return false;
1497 
1498     // Conservatively return false if I is an inline-asm instruction. Sinking
1499     // and merging inline-asm instructions can potentially create arguments
1500     // that cannot satisfy the inline-asm constraints.
1501     // If the instruction has nomerge attribute, return false.
1502     if (const auto *C = dyn_cast<CallBase>(I))
1503       if (C->isInlineAsm() || C->cannotMerge())
1504         return false;
1505 
1506     // Each instruction must have zero or one use.
1507     if (HasUse && !I->hasOneUse())
1508       return false;
1509     if (!HasUse && !I->user_empty())
1510       return false;
1511   }
1512 
1513   const Instruction *I0 = Insts.front();
1514   for (auto *I : Insts)
1515     if (!I->isSameOperationAs(I0))
1516       return false;
1517 
1518   // All instructions in Insts are known to be the same opcode. If they have a
1519   // use, check that the only user is a PHI or in the same block as the
1520   // instruction, because if a user is in the same block as an instruction we're
1521   // contemplating sinking, it must already be determined to be sinkable.
1522   if (HasUse) {
1523     auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1524     auto *Succ = I0->getParent()->getTerminator()->getSuccessor(0);
1525     if (!all_of(Insts, [&PNUse,&Succ](const Instruction *I) -> bool {
1526           auto *U = cast<Instruction>(*I->user_begin());
1527           return (PNUse &&
1528                   PNUse->getParent() == Succ &&
1529                   PNUse->getIncomingValueForBlock(I->getParent()) == I) ||
1530                  U->getParent() == I->getParent();
1531         }))
1532       return false;
1533   }
1534 
1535   // Because SROA can't handle speculating stores of selects, try not to sink
1536   // loads, stores or lifetime markers of allocas when we'd have to create a
1537   // PHI for the address operand. Also, because it is likely that loads or
1538   // stores of allocas will disappear when Mem2Reg/SROA is run, don't sink
1539   // them.
1540   // This can cause code churn which can have unintended consequences down
1541   // the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
1542   // FIXME: This is a workaround for a deficiency in SROA - see
1543   // https://llvm.org/bugs/show_bug.cgi?id=30188
1544   if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) {
1545         return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
1546       }))
1547     return false;
1548   if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) {
1549         return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts());
1550       }))
1551     return false;
1552   if (isLifeTimeMarker(I0) && any_of(Insts, [](const Instruction *I) {
1553         return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts());
1554       }))
1555     return false;
1556 
1557   for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
1558     Value *Op = I0->getOperand(OI);
1559     if (Op->getType()->isTokenTy())
1560       // Don't touch any operand of token type.
1561       return false;
1562 
1563     auto SameAsI0 = [&I0, OI](const Instruction *I) {
1564       assert(I->getNumOperands() == I0->getNumOperands());
1565       return I->getOperand(OI) == I0->getOperand(OI);
1566     };
1567     if (!all_of(Insts, SameAsI0)) {
1568       if ((isa<Constant>(Op) && !replacingOperandWithVariableIsCheap(I0, OI)) ||
1569           !canReplaceOperandWithVariable(I0, OI))
1570         // We can't create a PHI from this GEP.
1571         return false;
1572       // Don't create indirect calls! The called value is the final operand.
1573       if (isa<CallBase>(I0) && OI == OE - 1) {
1574         // FIXME: if the call was *already* indirect, we should do this.
1575         return false;
1576       }
1577       for (auto *I : Insts)
1578         PHIOperands[I].push_back(I->getOperand(OI));
1579     }
1580   }
1581   return true;
1582 }
1583 
1584 // Assuming canSinkLastInstruction(Blocks) has returned true, sink the last
1585 // instruction of every block in Blocks to their common successor, commoning
1586 // into one instruction.
1587 static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
1588   auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
1589 
1590   // canSinkLastInstruction returning true guarantees that every block has at
1591   // least one non-terminator instruction.
1592   SmallVector<Instruction*,4> Insts;
1593   for (auto *BB : Blocks) {
1594     Instruction *I = BB->getTerminator();
1595     do {
1596       I = I->getPrevNode();
1597     } while (isa<DbgInfoIntrinsic>(I) && I != &BB->front());
1598     if (!isa<DbgInfoIntrinsic>(I))
1599       Insts.push_back(I);
1600   }
1601 
1602   // The only checking we need to do now is that all users of all instructions
1603   // are the same PHI node. canSinkLastInstruction should have checked this but
1604   // it is slightly over-aggressive - it gets confused by commutative instructions
1605   // so double-check it here.
1606   Instruction *I0 = Insts.front();
1607   if (!I0->user_empty()) {
1608     auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1609     if (!all_of(Insts, [&PNUse](const Instruction *I) -> bool {
1610           auto *U = cast<Instruction>(*I->user_begin());
1611           return U == PNUse;
1612         }))
1613       return false;
1614   }
1615 
1616   // We don't need to do any more checking here; canSinkLastInstruction should
1617   // have done it all for us.
1618   SmallVector<Value*, 4> NewOperands;
1619   for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
1620     // This check is different to that in canSinkLastInstruction. There, we
1621     // cared about the global view once simplifycfg (and instcombine) have
1622     // completed - it takes into account PHIs that become trivially
1623     // simplifiable.  However here we need a more local view; if an operand
1624     // differs we create a PHI and rely on instcombine to clean up the very
1625     // small mess we may make.
1626     bool NeedPHI = any_of(Insts, [&I0, O](const Instruction *I) {
1627       return I->getOperand(O) != I0->getOperand(O);
1628     });
1629     if (!NeedPHI) {
1630       NewOperands.push_back(I0->getOperand(O));
1631       continue;
1632     }
1633 
1634     // Create a new PHI in the successor block and populate it.
1635     auto *Op = I0->getOperand(O);
1636     assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
1637     auto *PN = PHINode::Create(Op->getType(), Insts.size(),
1638                                Op->getName() + ".sink", &BBEnd->front());
1639     for (auto *I : Insts)
1640       PN->addIncoming(I->getOperand(O), I->getParent());
1641     NewOperands.push_back(PN);
1642   }
1643 
1644   // Arbitrarily use I0 as the new "common" instruction; remap its operands
1645   // and move it to the start of the successor block.
1646   for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
1647     I0->getOperandUse(O).set(NewOperands[O]);
1648   I0->moveBefore(&*BBEnd->getFirstInsertionPt());
1649 
1650   // Update metadata and IR flags, and merge debug locations.
1651   for (auto *I : Insts)
1652     if (I != I0) {
1653       // The debug location for the "common" instruction is the merged locations
1654       // of all the commoned instructions.  We start with the original location
1655       // of the "common" instruction and iteratively merge each location in the
1656       // loop below.
1657       // This is an N-way merge, which will be inefficient if I0 is a CallInst.
1658       // However, as N-way merge for CallInst is rare, so we use simplified API
1659       // instead of using complex API for N-way merge.
1660       I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
1661       combineMetadataForCSE(I0, I, true);
1662       I0->andIRFlags(I);
1663     }
1664 
1665   if (!I0->user_empty()) {
1666     // canSinkLastInstruction checked that all instructions were used by
1667     // one and only one PHI node. Find that now, RAUW it to our common
1668     // instruction and nuke it.
1669     auto *PN = cast<PHINode>(*I0->user_begin());
1670     PN->replaceAllUsesWith(I0);
1671     PN->eraseFromParent();
1672   }
1673 
1674   // Finally nuke all instructions apart from the common instruction.
1675   for (auto *I : Insts)
1676     if (I != I0)
1677       I->eraseFromParent();
1678 
1679   return true;
1680 }
1681 
1682 namespace {
1683 
1684   // LockstepReverseIterator - Iterates through instructions
1685   // in a set of blocks in reverse order from the first non-terminator.
1686   // For example (assume all blocks have size n):
1687   //   LockstepReverseIterator I([B1, B2, B3]);
1688   //   *I-- = [B1[n], B2[n], B3[n]];
1689   //   *I-- = [B1[n-1], B2[n-1], B3[n-1]];
1690   //   *I-- = [B1[n-2], B2[n-2], B3[n-2]];
1691   //   ...
1692   class LockstepReverseIterator {
1693     ArrayRef<BasicBlock*> Blocks;
1694     SmallVector<Instruction*,4> Insts;
1695     bool Fail;
1696 
1697   public:
1698     LockstepReverseIterator(ArrayRef<BasicBlock*> Blocks) : Blocks(Blocks) {
1699       reset();
1700     }
1701 
1702     void reset() {
1703       Fail = false;
1704       Insts.clear();
1705       for (auto *BB : Blocks) {
1706         Instruction *Inst = BB->getTerminator();
1707         for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1708           Inst = Inst->getPrevNode();
1709         if (!Inst) {
1710           // Block wasn't big enough.
1711           Fail = true;
1712           return;
1713         }
1714         Insts.push_back(Inst);
1715       }
1716     }
1717 
1718     bool isValid() const {
1719       return !Fail;
1720     }
1721 
1722     void operator--() {
1723       if (Fail)
1724         return;
1725       for (auto *&Inst : Insts) {
1726         for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1727           Inst = Inst->getPrevNode();
1728         // Already at beginning of block.
1729         if (!Inst) {
1730           Fail = true;
1731           return;
1732         }
1733       }
1734     }
1735 
1736     ArrayRef<Instruction*> operator * () const {
1737       return Insts;
1738     }
1739   };
1740 
1741 } // end anonymous namespace
1742 
1743 /// Check whether BB's predecessors end with unconditional branches. If it is
1744 /// true, sink any common code from the predecessors to BB.
1745 /// We also allow one predecessor to end with conditional branch (but no more
1746 /// than one).
1747 static bool SinkCommonCodeFromPredecessors(BasicBlock *BB) {
1748   // We support two situations:
1749   //   (1) all incoming arcs are unconditional
1750   //   (2) one incoming arc is conditional
1751   //
1752   // (2) is very common in switch defaults and
1753   // else-if patterns;
1754   //
1755   //   if (a) f(1);
1756   //   else if (b) f(2);
1757   //
1758   // produces:
1759   //
1760   //       [if]
1761   //      /    \
1762   //    [f(1)] [if]
1763   //      |     | \
1764   //      |     |  |
1765   //      |  [f(2)]|
1766   //       \    | /
1767   //        [ end ]
1768   //
1769   // [end] has two unconditional predecessor arcs and one conditional. The
1770   // conditional refers to the implicit empty 'else' arc. This conditional
1771   // arc can also be caused by an empty default block in a switch.
1772   //
1773   // In this case, we attempt to sink code from all *unconditional* arcs.
1774   // If we can sink instructions from these arcs (determined during the scan
1775   // phase below) we insert a common successor for all unconditional arcs and
1776   // connect that to [end], to enable sinking:
1777   //
1778   //       [if]
1779   //      /    \
1780   //    [x(1)] [if]
1781   //      |     | \
1782   //      |     |  \
1783   //      |  [x(2)] |
1784   //       \   /    |
1785   //   [sink.split] |
1786   //         \     /
1787   //         [ end ]
1788   //
1789   SmallVector<BasicBlock*,4> UnconditionalPreds;
1790   Instruction *Cond = nullptr;
1791   for (auto *B : predecessors(BB)) {
1792     auto *T = B->getTerminator();
1793     if (isa<BranchInst>(T) && cast<BranchInst>(T)->isUnconditional())
1794       UnconditionalPreds.push_back(B);
1795     else if ((isa<BranchInst>(T) || isa<SwitchInst>(T)) && !Cond)
1796       Cond = T;
1797     else
1798       return false;
1799   }
1800   if (UnconditionalPreds.size() < 2)
1801     return false;
1802 
1803   bool Changed = false;
1804   // We take a two-step approach to tail sinking. First we scan from the end of
1805   // each block upwards in lockstep. If the n'th instruction from the end of each
1806   // block can be sunk, those instructions are added to ValuesToSink and we
1807   // carry on. If we can sink an instruction but need to PHI-merge some operands
1808   // (because they're not identical in each instruction) we add these to
1809   // PHIOperands.
1810   unsigned ScanIdx = 0;
1811   SmallPtrSet<Value*,4> InstructionsToSink;
1812   DenseMap<Instruction*, SmallVector<Value*,4>> PHIOperands;
1813   LockstepReverseIterator LRI(UnconditionalPreds);
1814   while (LRI.isValid() &&
1815          canSinkInstructions(*LRI, PHIOperands)) {
1816     LLVM_DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI)[0]
1817                       << "\n");
1818     InstructionsToSink.insert((*LRI).begin(), (*LRI).end());
1819     ++ScanIdx;
1820     --LRI;
1821   }
1822 
1823   auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
1824     unsigned NumPHIdValues = 0;
1825     for (auto *I : *LRI)
1826       for (auto *V : PHIOperands[I])
1827         if (InstructionsToSink.count(V) == 0)
1828           ++NumPHIdValues;
1829     LLVM_DEBUG(dbgs() << "SINK: #phid values: " << NumPHIdValues << "\n");
1830     unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.size();
1831     if ((NumPHIdValues % UnconditionalPreds.size()) != 0)
1832         NumPHIInsts++;
1833 
1834     return NumPHIInsts <= 1;
1835   };
1836 
1837   if (ScanIdx > 0 && Cond) {
1838     // Check if we would actually sink anything first! This mutates the CFG and
1839     // adds an extra block. The goal in doing this is to allow instructions that
1840     // couldn't be sunk before to be sunk - obviously, speculatable instructions
1841     // (such as trunc, add) can be sunk and predicated already. So we check that
1842     // we're going to sink at least one non-speculatable instruction.
1843     LRI.reset();
1844     unsigned Idx = 0;
1845     bool Profitable = false;
1846     while (ProfitableToSinkInstruction(LRI) && Idx < ScanIdx) {
1847       if (!isSafeToSpeculativelyExecute((*LRI)[0])) {
1848         Profitable = true;
1849         break;
1850       }
1851       --LRI;
1852       ++Idx;
1853     }
1854     if (!Profitable)
1855       return false;
1856 
1857     LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
1858     // We have a conditional edge and we're going to sink some instructions.
1859     // Insert a new block postdominating all blocks we're going to sink from.
1860     if (!SplitBlockPredecessors(BB, UnconditionalPreds, ".sink.split"))
1861       // Edges couldn't be split.
1862       return false;
1863     Changed = true;
1864   }
1865 
1866   // Now that we've analyzed all potential sinking candidates, perform the
1867   // actual sink. We iteratively sink the last non-terminator of the source
1868   // blocks into their common successor unless doing so would require too
1869   // many PHI instructions to be generated (currently only one PHI is allowed
1870   // per sunk instruction).
1871   //
1872   // We can use InstructionsToSink to discount values needing PHI-merging that will
1873   // actually be sunk in a later iteration. This allows us to be more
1874   // aggressive in what we sink. This does allow a false positive where we
1875   // sink presuming a later value will also be sunk, but stop half way through
1876   // and never actually sink it which means we produce more PHIs than intended.
1877   // This is unlikely in practice though.
1878   for (unsigned SinkIdx = 0; SinkIdx != ScanIdx; ++SinkIdx) {
1879     LLVM_DEBUG(dbgs() << "SINK: Sink: "
1880                       << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
1881                       << "\n");
1882 
1883     // Because we've sunk every instruction in turn, the current instruction to
1884     // sink is always at index 0.
1885     LRI.reset();
1886     if (!ProfitableToSinkInstruction(LRI)) {
1887       // Too many PHIs would be created.
1888       LLVM_DEBUG(
1889           dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
1890       break;
1891     }
1892 
1893     if (!sinkLastInstruction(UnconditionalPreds))
1894       return Changed;
1895     NumSinkCommons++;
1896     Changed = true;
1897   }
1898   return Changed;
1899 }
1900 
1901 /// Determine if we can hoist sink a sole store instruction out of a
1902 /// conditional block.
1903 ///
1904 /// We are looking for code like the following:
1905 ///   BrBB:
1906 ///     store i32 %add, i32* %arrayidx2
1907 ///     ... // No other stores or function calls (we could be calling a memory
1908 ///     ... // function).
1909 ///     %cmp = icmp ult %x, %y
1910 ///     br i1 %cmp, label %EndBB, label %ThenBB
1911 ///   ThenBB:
1912 ///     store i32 %add5, i32* %arrayidx2
1913 ///     br label EndBB
1914 ///   EndBB:
1915 ///     ...
1916 ///   We are going to transform this into:
1917 ///   BrBB:
1918 ///     store i32 %add, i32* %arrayidx2
1919 ///     ... //
1920 ///     %cmp = icmp ult %x, %y
1921 ///     %add.add5 = select i1 %cmp, i32 %add, %add5
1922 ///     store i32 %add.add5, i32* %arrayidx2
1923 ///     ...
1924 ///
1925 /// \return The pointer to the value of the previous store if the store can be
1926 ///         hoisted into the predecessor block. 0 otherwise.
1927 static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
1928                                      BasicBlock *StoreBB, BasicBlock *EndBB) {
1929   StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
1930   if (!StoreToHoist)
1931     return nullptr;
1932 
1933   // Volatile or atomic.
1934   if (!StoreToHoist->isSimple())
1935     return nullptr;
1936 
1937   Value *StorePtr = StoreToHoist->getPointerOperand();
1938 
1939   // Look for a store to the same pointer in BrBB.
1940   unsigned MaxNumInstToLookAt = 9;
1941   for (Instruction &CurI : reverse(BrBB->instructionsWithoutDebug())) {
1942     if (!MaxNumInstToLookAt)
1943       break;
1944     --MaxNumInstToLookAt;
1945 
1946     // Could be calling an instruction that affects memory like free().
1947     if (CurI.mayHaveSideEffects() && !isa<StoreInst>(CurI))
1948       return nullptr;
1949 
1950     if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
1951       // Found the previous store make sure it stores to the same location.
1952       if (SI->getPointerOperand() == StorePtr)
1953         // Found the previous store, return its value operand.
1954         return SI->getValueOperand();
1955       return nullptr; // Unknown store.
1956     }
1957   }
1958 
1959   return nullptr;
1960 }
1961 
1962 /// Speculate a conditional basic block flattening the CFG.
1963 ///
1964 /// Note that this is a very risky transform currently. Speculating
1965 /// instructions like this is most often not desirable. Instead, there is an MI
1966 /// pass which can do it with full awareness of the resource constraints.
1967 /// However, some cases are "obvious" and we should do directly. An example of
1968 /// this is speculating a single, reasonably cheap instruction.
1969 ///
1970 /// There is only one distinct advantage to flattening the CFG at the IR level:
1971 /// it makes very common but simplistic optimizations such as are common in
1972 /// instcombine and the DAG combiner more powerful by removing CFG edges and
1973 /// modeling their effects with easier to reason about SSA value graphs.
1974 ///
1975 ///
1976 /// An illustration of this transform is turning this IR:
1977 /// \code
1978 ///   BB:
1979 ///     %cmp = icmp ult %x, %y
1980 ///     br i1 %cmp, label %EndBB, label %ThenBB
1981 ///   ThenBB:
1982 ///     %sub = sub %x, %y
1983 ///     br label BB2
1984 ///   EndBB:
1985 ///     %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
1986 ///     ...
1987 /// \endcode
1988 ///
1989 /// Into this IR:
1990 /// \code
1991 ///   BB:
1992 ///     %cmp = icmp ult %x, %y
1993 ///     %sub = sub %x, %y
1994 ///     %cond = select i1 %cmp, 0, %sub
1995 ///     ...
1996 /// \endcode
1997 ///
1998 /// \returns true if the conditional block is removed.
1999 bool SimplifyCFGOpt::SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
2000                                             const TargetTransformInfo &TTI) {
2001   // Be conservative for now. FP select instruction can often be expensive.
2002   Value *BrCond = BI->getCondition();
2003   if (isa<FCmpInst>(BrCond))
2004     return false;
2005 
2006   BasicBlock *BB = BI->getParent();
2007   BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
2008 
2009   // If ThenBB is actually on the false edge of the conditional branch, remember
2010   // to swap the select operands later.
2011   bool Invert = false;
2012   if (ThenBB != BI->getSuccessor(0)) {
2013     assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
2014     Invert = true;
2015   }
2016   assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
2017 
2018   // Keep a count of how many times instructions are used within ThenBB when
2019   // they are candidates for sinking into ThenBB. Specifically:
2020   // - They are defined in BB, and
2021   // - They have no side effects, and
2022   // - All of their uses are in ThenBB.
2023   SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
2024 
2025   SmallVector<Instruction *, 4> SpeculatedDbgIntrinsics;
2026 
2027   unsigned SpeculatedInstructions = 0;
2028   Value *SpeculatedStoreValue = nullptr;
2029   StoreInst *SpeculatedStore = nullptr;
2030   for (BasicBlock::iterator BBI = ThenBB->begin(),
2031                             BBE = std::prev(ThenBB->end());
2032        BBI != BBE; ++BBI) {
2033     Instruction *I = &*BBI;
2034     // Skip debug info.
2035     if (isa<DbgInfoIntrinsic>(I)) {
2036       SpeculatedDbgIntrinsics.push_back(I);
2037       continue;
2038     }
2039 
2040     // Only speculatively execute a single instruction (not counting the
2041     // terminator) for now.
2042     ++SpeculatedInstructions;
2043     if (SpeculatedInstructions > 1)
2044       return false;
2045 
2046     // Don't hoist the instruction if it's unsafe or expensive.
2047     if (!isSafeToSpeculativelyExecute(I) &&
2048         !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore(
2049                                   I, BB, ThenBB, EndBB))))
2050       return false;
2051     if (!SpeculatedStoreValue &&
2052         ComputeSpeculationCost(I, TTI) >
2053             PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
2054       return false;
2055 
2056     // Store the store speculation candidate.
2057     if (SpeculatedStoreValue)
2058       SpeculatedStore = cast<StoreInst>(I);
2059 
2060     // Do not hoist the instruction if any of its operands are defined but not
2061     // used in BB. The transformation will prevent the operand from
2062     // being sunk into the use block.
2063     for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
2064       Instruction *OpI = dyn_cast<Instruction>(*i);
2065       if (!OpI || OpI->getParent() != BB || OpI->mayHaveSideEffects())
2066         continue; // Not a candidate for sinking.
2067 
2068       ++SinkCandidateUseCounts[OpI];
2069     }
2070   }
2071 
2072   // Consider any sink candidates which are only used in ThenBB as costs for
2073   // speculation. Note, while we iterate over a DenseMap here, we are summing
2074   // and so iteration order isn't significant.
2075   for (SmallDenseMap<Instruction *, unsigned, 4>::iterator
2076            I = SinkCandidateUseCounts.begin(),
2077            E = SinkCandidateUseCounts.end();
2078        I != E; ++I)
2079     if (I->first->hasNUses(I->second)) {
2080       ++SpeculatedInstructions;
2081       if (SpeculatedInstructions > 1)
2082         return false;
2083     }
2084 
2085   // Check that the PHI nodes can be converted to selects.
2086   bool HaveRewritablePHIs = false;
2087   for (PHINode &PN : EndBB->phis()) {
2088     Value *OrigV = PN.getIncomingValueForBlock(BB);
2089     Value *ThenV = PN.getIncomingValueForBlock(ThenBB);
2090 
2091     // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf.
2092     // Skip PHIs which are trivial.
2093     if (ThenV == OrigV)
2094       continue;
2095 
2096     // Don't convert to selects if we could remove undefined behavior instead.
2097     if (passingValueIsAlwaysUndefined(OrigV, &PN) ||
2098         passingValueIsAlwaysUndefined(ThenV, &PN))
2099       return false;
2100 
2101     HaveRewritablePHIs = true;
2102     ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
2103     ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
2104     if (!OrigCE && !ThenCE)
2105       continue; // Known safe and cheap.
2106 
2107     if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) ||
2108         (OrigCE && !isSafeToSpeculativelyExecute(OrigCE)))
2109       return false;
2110     unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0;
2111     unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0;
2112     unsigned MaxCost =
2113         2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
2114     if (OrigCost + ThenCost > MaxCost)
2115       return false;
2116 
2117     // Account for the cost of an unfolded ConstantExpr which could end up
2118     // getting expanded into Instructions.
2119     // FIXME: This doesn't account for how many operations are combined in the
2120     // constant expression.
2121     ++SpeculatedInstructions;
2122     if (SpeculatedInstructions > 1)
2123       return false;
2124   }
2125 
2126   // If there are no PHIs to process, bail early. This helps ensure idempotence
2127   // as well.
2128   if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue))
2129     return false;
2130 
2131   // If we get here, we can hoist the instruction and if-convert.
2132   LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
2133 
2134   // Insert a select of the value of the speculated store.
2135   if (SpeculatedStoreValue) {
2136     IRBuilder<NoFolder> Builder(BI);
2137     Value *TrueV = SpeculatedStore->getValueOperand();
2138     Value *FalseV = SpeculatedStoreValue;
2139     if (Invert)
2140       std::swap(TrueV, FalseV);
2141     Value *S = Builder.CreateSelect(
2142         BrCond, TrueV, FalseV, "spec.store.select", BI);
2143     SpeculatedStore->setOperand(0, S);
2144     SpeculatedStore->applyMergedLocation(BI->getDebugLoc(),
2145                                          SpeculatedStore->getDebugLoc());
2146   }
2147 
2148   // Metadata can be dependent on the condition we are hoisting above.
2149   // Conservatively strip all metadata on the instruction. Drop the debug loc
2150   // to avoid making it appear as if the condition is a constant, which would
2151   // be misleading while debugging.
2152   for (auto &I : *ThenBB) {
2153     if (!SpeculatedStoreValue || &I != SpeculatedStore)
2154       I.setDebugLoc(DebugLoc());
2155     I.dropUnknownNonDebugMetadata();
2156   }
2157 
2158   // Hoist the instructions.
2159   BB->getInstList().splice(BI->getIterator(), ThenBB->getInstList(),
2160                            ThenBB->begin(), std::prev(ThenBB->end()));
2161 
2162   // Insert selects and rewrite the PHI operands.
2163   IRBuilder<NoFolder> Builder(BI);
2164   for (PHINode &PN : EndBB->phis()) {
2165     unsigned OrigI = PN.getBasicBlockIndex(BB);
2166     unsigned ThenI = PN.getBasicBlockIndex(ThenBB);
2167     Value *OrigV = PN.getIncomingValue(OrigI);
2168     Value *ThenV = PN.getIncomingValue(ThenI);
2169 
2170     // Skip PHIs which are trivial.
2171     if (OrigV == ThenV)
2172       continue;
2173 
2174     // Create a select whose true value is the speculatively executed value and
2175     // false value is the pre-existing value. Swap them if the branch
2176     // destinations were inverted.
2177     Value *TrueV = ThenV, *FalseV = OrigV;
2178     if (Invert)
2179       std::swap(TrueV, FalseV);
2180     Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV, "spec.select", BI);
2181     PN.setIncomingValue(OrigI, V);
2182     PN.setIncomingValue(ThenI, V);
2183   }
2184 
2185   // Remove speculated dbg intrinsics.
2186   // FIXME: Is it possible to do this in a more elegant way? Moving/merging the
2187   // dbg value for the different flows and inserting it after the select.
2188   for (Instruction *I : SpeculatedDbgIntrinsics)
2189     I->eraseFromParent();
2190 
2191   ++NumSpeculations;
2192   return true;
2193 }
2194 
2195 /// Return true if we can thread a branch across this block.
2196 static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
2197   int Size = 0;
2198 
2199   for (Instruction &I : BB->instructionsWithoutDebug()) {
2200     if (Size > MaxSmallBlockSize)
2201       return false; // Don't clone large BB's.
2202     // We will delete Phis while threading, so Phis should not be accounted in
2203     // block's size
2204     if (!isa<PHINode>(I))
2205       ++Size;
2206 
2207     // We can only support instructions that do not define values that are
2208     // live outside of the current basic block.
2209     for (User *U : I.users()) {
2210       Instruction *UI = cast<Instruction>(U);
2211       if (UI->getParent() != BB || isa<PHINode>(UI))
2212         return false;
2213     }
2214 
2215     // Looks ok, continue checking.
2216   }
2217 
2218   return true;
2219 }
2220 
2221 /// If we have a conditional branch on a PHI node value that is defined in the
2222 /// same block as the branch and if any PHI entries are constants, thread edges
2223 /// corresponding to that entry to be branches to their ultimate destination.
2224 static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
2225                                 AssumptionCache *AC) {
2226   BasicBlock *BB = BI->getParent();
2227   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
2228   // NOTE: we currently cannot transform this case if the PHI node is used
2229   // outside of the block.
2230   if (!PN || PN->getParent() != BB || !PN->hasOneUse())
2231     return false;
2232 
2233   // Degenerate case of a single entry PHI.
2234   if (PN->getNumIncomingValues() == 1) {
2235     FoldSingleEntryPHINodes(PN->getParent());
2236     return true;
2237   }
2238 
2239   // Now we know that this block has multiple preds and two succs.
2240   if (!BlockIsSimpleEnoughToThreadThrough(BB))
2241     return false;
2242 
2243   // Can't fold blocks that contain noduplicate or convergent calls.
2244   if (any_of(*BB, [](const Instruction &I) {
2245         const CallInst *CI = dyn_cast<CallInst>(&I);
2246         return CI && (CI->cannotDuplicate() || CI->isConvergent());
2247       }))
2248     return false;
2249 
2250   // Okay, this is a simple enough basic block.  See if any phi values are
2251   // constants.
2252   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2253     ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
2254     if (!CB || !CB->getType()->isIntegerTy(1))
2255       continue;
2256 
2257     // Okay, we now know that all edges from PredBB should be revectored to
2258     // branch to RealDest.
2259     BasicBlock *PredBB = PN->getIncomingBlock(i);
2260     BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
2261 
2262     if (RealDest == BB)
2263       continue; // Skip self loops.
2264     // Skip if the predecessor's terminator is an indirect branch.
2265     if (isa<IndirectBrInst>(PredBB->getTerminator()))
2266       continue;
2267 
2268     // The dest block might have PHI nodes, other predecessors and other
2269     // difficult cases.  Instead of being smart about this, just insert a new
2270     // block that jumps to the destination block, effectively splitting
2271     // the edge we are about to create.
2272     BasicBlock *EdgeBB =
2273         BasicBlock::Create(BB->getContext(), RealDest->getName() + ".critedge",
2274                            RealDest->getParent(), RealDest);
2275     BranchInst *CritEdgeBranch = BranchInst::Create(RealDest, EdgeBB);
2276     CritEdgeBranch->setDebugLoc(BI->getDebugLoc());
2277 
2278     // Update PHI nodes.
2279     AddPredecessorToBlock(RealDest, EdgeBB, BB);
2280 
2281     // BB may have instructions that are being threaded over.  Clone these
2282     // instructions into EdgeBB.  We know that there will be no uses of the
2283     // cloned instructions outside of EdgeBB.
2284     BasicBlock::iterator InsertPt = EdgeBB->begin();
2285     DenseMap<Value *, Value *> TranslateMap; // Track translated values.
2286     for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
2287       if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
2288         TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
2289         continue;
2290       }
2291       // Clone the instruction.
2292       Instruction *N = BBI->clone();
2293       if (BBI->hasName())
2294         N->setName(BBI->getName() + ".c");
2295 
2296       // Update operands due to translation.
2297       for (User::op_iterator i = N->op_begin(), e = N->op_end(); i != e; ++i) {
2298         DenseMap<Value *, Value *>::iterator PI = TranslateMap.find(*i);
2299         if (PI != TranslateMap.end())
2300           *i = PI->second;
2301       }
2302 
2303       // Check for trivial simplification.
2304       if (Value *V = SimplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
2305         if (!BBI->use_empty())
2306           TranslateMap[&*BBI] = V;
2307         if (!N->mayHaveSideEffects()) {
2308           N->deleteValue(); // Instruction folded away, don't need actual inst
2309           N = nullptr;
2310         }
2311       } else {
2312         if (!BBI->use_empty())
2313           TranslateMap[&*BBI] = N;
2314       }
2315       if (N) {
2316         // Insert the new instruction into its new home.
2317         EdgeBB->getInstList().insert(InsertPt, N);
2318 
2319         // Register the new instruction with the assumption cache if necessary.
2320         if (AC && match(N, m_Intrinsic<Intrinsic::assume>()))
2321           AC->registerAssumption(cast<IntrinsicInst>(N));
2322       }
2323     }
2324 
2325     // Loop over all of the edges from PredBB to BB, changing them to branch
2326     // to EdgeBB instead.
2327     Instruction *PredBBTI = PredBB->getTerminator();
2328     for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
2329       if (PredBBTI->getSuccessor(i) == BB) {
2330         BB->removePredecessor(PredBB);
2331         PredBBTI->setSuccessor(i, EdgeBB);
2332       }
2333 
2334     // Recurse, simplifying any other constants.
2335     return FoldCondBranchOnPHI(BI, DL, AC) || true;
2336   }
2337 
2338   return false;
2339 }
2340 
2341 /// Given a BB that starts with the specified two-entry PHI node,
2342 /// see if we can eliminate it.
2343 static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
2344                                 const DataLayout &DL) {
2345   // Ok, this is a two entry PHI node.  Check to see if this is a simple "if
2346   // statement", which has a very simple dominance structure.  Basically, we
2347   // are trying to find the condition that is being branched on, which
2348   // subsequently causes this merge to happen.  We really want control
2349   // dependence information for this check, but simplifycfg can't keep it up
2350   // to date, and this catches most of the cases we care about anyway.
2351   BasicBlock *BB = PN->getParent();
2352 
2353   BasicBlock *IfTrue, *IfFalse;
2354   Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
2355   if (!IfCond ||
2356       // Don't bother if the branch will be constant folded trivially.
2357       isa<ConstantInt>(IfCond))
2358     return false;
2359 
2360   // Okay, we found that we can merge this two-entry phi node into a select.
2361   // Doing so would require us to fold *all* two entry phi nodes in this block.
2362   // At some point this becomes non-profitable (particularly if the target
2363   // doesn't support cmov's).  Only do this transformation if there are two or
2364   // fewer PHI nodes in this block.
2365   unsigned NumPhis = 0;
2366   for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
2367     if (NumPhis > 2)
2368       return false;
2369 
2370   // Loop over the PHI's seeing if we can promote them all to select
2371   // instructions.  While we are at it, keep track of the instructions
2372   // that need to be moved to the dominating block.
2373   SmallPtrSet<Instruction *, 4> AggressiveInsts;
2374   int BudgetRemaining =
2375       TwoEntryPHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
2376 
2377   for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
2378     PHINode *PN = cast<PHINode>(II++);
2379     if (Value *V = SimplifyInstruction(PN, {DL, PN})) {
2380       PN->replaceAllUsesWith(V);
2381       PN->eraseFromParent();
2382       continue;
2383     }
2384 
2385     if (!DominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts,
2386                              BudgetRemaining, TTI) ||
2387         !DominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts,
2388                              BudgetRemaining, TTI))
2389       return false;
2390   }
2391 
2392   // If we folded the first phi, PN dangles at this point.  Refresh it.  If
2393   // we ran out of PHIs then we simplified them all.
2394   PN = dyn_cast<PHINode>(BB->begin());
2395   if (!PN)
2396     return true;
2397 
2398   // Return true if at least one of these is a 'not', and another is either
2399   // a 'not' too, or a constant.
2400   auto CanHoistNotFromBothValues = [](Value *V0, Value *V1) {
2401     if (!match(V0, m_Not(m_Value())))
2402       std::swap(V0, V1);
2403     auto Invertible = m_CombineOr(m_Not(m_Value()), m_AnyIntegralConstant());
2404     return match(V0, m_Not(m_Value())) && match(V1, Invertible);
2405   };
2406 
2407   // Don't fold i1 branches on PHIs which contain binary operators, unless one
2408   // of the incoming values is an 'not' and another one is freely invertible.
2409   // These can often be turned into switches and other things.
2410   if (PN->getType()->isIntegerTy(1) &&
2411       (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
2412        isa<BinaryOperator>(PN->getIncomingValue(1)) ||
2413        isa<BinaryOperator>(IfCond)) &&
2414       !CanHoistNotFromBothValues(PN->getIncomingValue(0),
2415                                  PN->getIncomingValue(1)))
2416     return false;
2417 
2418   // If all PHI nodes are promotable, check to make sure that all instructions
2419   // in the predecessor blocks can be promoted as well. If not, we won't be able
2420   // to get rid of the control flow, so it's not worth promoting to select
2421   // instructions.
2422   BasicBlock *DomBlock = nullptr;
2423   BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
2424   BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
2425   if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
2426     IfBlock1 = nullptr;
2427   } else {
2428     DomBlock = *pred_begin(IfBlock1);
2429     for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I)
2430       if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2431         // This is not an aggressive instruction that we can promote.
2432         // Because of this, we won't be able to get rid of the control flow, so
2433         // the xform is not worth it.
2434         return false;
2435       }
2436   }
2437 
2438   if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
2439     IfBlock2 = nullptr;
2440   } else {
2441     DomBlock = *pred_begin(IfBlock2);
2442     for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I)
2443       if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2444         // This is not an aggressive instruction that we can promote.
2445         // Because of this, we won't be able to get rid of the control flow, so
2446         // the xform is not worth it.
2447         return false;
2448       }
2449   }
2450   assert(DomBlock && "Failed to find root DomBlock");
2451 
2452   LLVM_DEBUG(dbgs() << "FOUND IF CONDITION!  " << *IfCond
2453                     << "  T: " << IfTrue->getName()
2454                     << "  F: " << IfFalse->getName() << "\n");
2455 
2456   // If we can still promote the PHI nodes after this gauntlet of tests,
2457   // do all of the PHI's now.
2458   Instruction *InsertPt = DomBlock->getTerminator();
2459   IRBuilder<NoFolder> Builder(InsertPt);
2460 
2461   // Move all 'aggressive' instructions, which are defined in the
2462   // conditional parts of the if's up to the dominating block.
2463   if (IfBlock1)
2464     hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock1);
2465   if (IfBlock2)
2466     hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock2);
2467 
2468   // Propagate fast-math-flags from phi nodes to replacement selects.
2469   IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
2470   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
2471     if (isa<FPMathOperator>(PN))
2472       Builder.setFastMathFlags(PN->getFastMathFlags());
2473 
2474     // Change the PHI node into a select instruction.
2475     Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
2476     Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
2477 
2478     Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt);
2479     PN->replaceAllUsesWith(Sel);
2480     Sel->takeName(PN);
2481     PN->eraseFromParent();
2482   }
2483 
2484   // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
2485   // has been flattened.  Change DomBlock to jump directly to our new block to
2486   // avoid other simplifycfg's kicking in on the diamond.
2487   Instruction *OldTI = DomBlock->getTerminator();
2488   Builder.SetInsertPoint(OldTI);
2489   Builder.CreateBr(BB);
2490   OldTI->eraseFromParent();
2491   return true;
2492 }
2493 
2494 /// If we found a conditional branch that goes to two returning blocks,
2495 /// try to merge them together into one return,
2496 /// introducing a select if the return values disagree.
2497 bool SimplifyCFGOpt::SimplifyCondBranchToTwoReturns(BranchInst *BI,
2498                                                     IRBuilder<> &Builder) {
2499   assert(BI->isConditional() && "Must be a conditional branch");
2500   BasicBlock *TrueSucc = BI->getSuccessor(0);
2501   BasicBlock *FalseSucc = BI->getSuccessor(1);
2502   ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
2503   ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
2504 
2505   // Check to ensure both blocks are empty (just a return) or optionally empty
2506   // with PHI nodes.  If there are other instructions, merging would cause extra
2507   // computation on one path or the other.
2508   if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
2509     return false;
2510   if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
2511     return false;
2512 
2513   Builder.SetInsertPoint(BI);
2514   // Okay, we found a branch that is going to two return nodes.  If
2515   // there is no return value for this function, just change the
2516   // branch into a return.
2517   if (FalseRet->getNumOperands() == 0) {
2518     TrueSucc->removePredecessor(BI->getParent());
2519     FalseSucc->removePredecessor(BI->getParent());
2520     Builder.CreateRetVoid();
2521     EraseTerminatorAndDCECond(BI);
2522     return true;
2523   }
2524 
2525   // Otherwise, figure out what the true and false return values are
2526   // so we can insert a new select instruction.
2527   Value *TrueValue = TrueRet->getReturnValue();
2528   Value *FalseValue = FalseRet->getReturnValue();
2529 
2530   // Unwrap any PHI nodes in the return blocks.
2531   if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
2532     if (TVPN->getParent() == TrueSucc)
2533       TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
2534   if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
2535     if (FVPN->getParent() == FalseSucc)
2536       FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
2537 
2538   // In order for this transformation to be safe, we must be able to
2539   // unconditionally execute both operands to the return.  This is
2540   // normally the case, but we could have a potentially-trapping
2541   // constant expression that prevents this transformation from being
2542   // safe.
2543   if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
2544     if (TCV->canTrap())
2545       return false;
2546   if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
2547     if (FCV->canTrap())
2548       return false;
2549 
2550   // Okay, we collected all the mapped values and checked them for sanity, and
2551   // defined to really do this transformation.  First, update the CFG.
2552   TrueSucc->removePredecessor(BI->getParent());
2553   FalseSucc->removePredecessor(BI->getParent());
2554 
2555   // Insert select instructions where needed.
2556   Value *BrCond = BI->getCondition();
2557   if (TrueValue) {
2558     // Insert a select if the results differ.
2559     if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
2560     } else if (isa<UndefValue>(TrueValue)) {
2561       TrueValue = FalseValue;
2562     } else {
2563       TrueValue =
2564           Builder.CreateSelect(BrCond, TrueValue, FalseValue, "retval", BI);
2565     }
2566   }
2567 
2568   Value *RI =
2569       !TrueValue ? Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
2570 
2571   (void)RI;
2572 
2573   LLVM_DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
2574                     << "\n  " << *BI << "\nNewRet = " << *RI << "\nTRUEBLOCK: "
2575                     << *TrueSucc << "\nFALSEBLOCK: " << *FalseSucc);
2576 
2577   EraseTerminatorAndDCECond(BI);
2578 
2579   return true;
2580 }
2581 
2582 /// Return true if the given instruction is available
2583 /// in its predecessor block. If yes, the instruction will be removed.
2584 static bool tryCSEWithPredecessor(Instruction *Inst, BasicBlock *PB) {
2585   if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2586     return false;
2587   for (Instruction &I : *PB) {
2588     Instruction *PBI = &I;
2589     // Check whether Inst and PBI generate the same value.
2590     if (Inst->isIdenticalTo(PBI)) {
2591       Inst->replaceAllUsesWith(PBI);
2592       Inst->eraseFromParent();
2593       return true;
2594     }
2595   }
2596   return false;
2597 }
2598 
2599 /// Return true if either PBI or BI has branch weight available, and store
2600 /// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
2601 /// not have branch weight, use 1:1 as its weight.
2602 static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
2603                                    uint64_t &PredTrueWeight,
2604                                    uint64_t &PredFalseWeight,
2605                                    uint64_t &SuccTrueWeight,
2606                                    uint64_t &SuccFalseWeight) {
2607   bool PredHasWeights =
2608       PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2609   bool SuccHasWeights =
2610       BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2611   if (PredHasWeights || SuccHasWeights) {
2612     if (!PredHasWeights)
2613       PredTrueWeight = PredFalseWeight = 1;
2614     if (!SuccHasWeights)
2615       SuccTrueWeight = SuccFalseWeight = 1;
2616     return true;
2617   } else {
2618     return false;
2619   }
2620 }
2621 
2622 /// If this basic block is simple enough, and if a predecessor branches to us
2623 /// and one of our successors, fold the block into the predecessor and use
2624 /// logical operations to pick the right destination.
2625 bool llvm::FoldBranchToCommonDest(BranchInst *BI, MemorySSAUpdater *MSSAU,
2626                                   unsigned BonusInstThreshold) {
2627   BasicBlock *BB = BI->getParent();
2628 
2629   const unsigned PredCount = pred_size(BB);
2630 
2631   bool Changed = false;
2632 
2633   Instruction *Cond = nullptr;
2634   if (BI->isConditional())
2635     Cond = dyn_cast<Instruction>(BI->getCondition());
2636   else {
2637     // For unconditional branch, check for a simple CFG pattern, where
2638     // BB has a single predecessor and BB's successor is also its predecessor's
2639     // successor. If such pattern exists, check for CSE between BB and its
2640     // predecessor.
2641     if (BasicBlock *PB = BB->getSinglePredecessor())
2642       if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2643         if (PBI->isConditional() &&
2644             (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
2645              BI->getSuccessor(0) == PBI->getSuccessor(1))) {
2646           for (auto I = BB->instructionsWithoutDebug().begin(),
2647                     E = BB->instructionsWithoutDebug().end();
2648                I != E;) {
2649             Instruction *Curr = &*I++;
2650             if (isa<CmpInst>(Curr)) {
2651               Cond = Curr;
2652               break;
2653             }
2654             // Quit if we can't remove this instruction.
2655             if (!tryCSEWithPredecessor(Curr, PB))
2656               return Changed;
2657             Changed = true;
2658           }
2659         }
2660 
2661     if (!Cond)
2662       return Changed;
2663   }
2664 
2665   if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2666       Cond->getParent() != BB || !Cond->hasOneUse())
2667     return Changed;
2668 
2669   // Make sure the instruction after the condition is the cond branch.
2670   BasicBlock::iterator CondIt = ++Cond->getIterator();
2671 
2672   // Ignore dbg intrinsics.
2673   while (isa<DbgInfoIntrinsic>(CondIt))
2674     ++CondIt;
2675 
2676   if (&*CondIt != BI)
2677     return Changed;
2678 
2679   // Only allow this transformation if computing the condition doesn't involve
2680   // too many instructions and these involved instructions can be executed
2681   // unconditionally. We denote all involved instructions except the condition
2682   // as "bonus instructions", and only allow this transformation when the
2683   // number of the bonus instructions we'll need to create when cloning into
2684   // each predecessor does not exceed a certain threshold.
2685   unsigned NumBonusInsts = 0;
2686   for (auto I = BB->begin(); Cond != &*I; ++I) {
2687     // Ignore dbg intrinsics.
2688     if (isa<DbgInfoIntrinsic>(I))
2689       continue;
2690     if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(&*I))
2691       return Changed;
2692     // I has only one use and can be executed unconditionally.
2693     Instruction *User = dyn_cast<Instruction>(I->user_back());
2694     if (User == nullptr || User->getParent() != BB)
2695       return Changed;
2696     // I is used in the same BB. Since BI uses Cond and doesn't have more slots
2697     // to use any other instruction, User must be an instruction between next(I)
2698     // and Cond.
2699 
2700     // Account for the cost of duplicating this instruction into each
2701     // predecessor.
2702     NumBonusInsts += PredCount;
2703     // Early exits once we reach the limit.
2704     if (NumBonusInsts > BonusInstThreshold)
2705       return Changed;
2706   }
2707 
2708   // Cond is known to be a compare or binary operator.  Check to make sure that
2709   // neither operand is a potentially-trapping constant expression.
2710   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
2711     if (CE->canTrap())
2712       return Changed;
2713   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
2714     if (CE->canTrap())
2715       return Changed;
2716 
2717   // Finally, don't infinitely unroll conditional loops.
2718   BasicBlock *TrueDest = BI->getSuccessor(0);
2719   BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
2720   if (TrueDest == BB || FalseDest == BB)
2721     return Changed;
2722 
2723   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2724     BasicBlock *PredBlock = *PI;
2725     BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
2726 
2727     // Check that we have two conditional branches.  If there is a PHI node in
2728     // the common successor, verify that the same value flows in from both
2729     // blocks.
2730     SmallVector<PHINode *, 4> PHIs;
2731     if (!PBI || PBI->isUnconditional() ||
2732         (BI->isConditional() && !SafeToMergeTerminators(BI, PBI)) ||
2733         (!BI->isConditional() &&
2734          !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
2735       continue;
2736 
2737     // Determine if the two branches share a common destination.
2738     Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
2739     bool InvertPredCond = false;
2740 
2741     if (BI->isConditional()) {
2742       if (PBI->getSuccessor(0) == TrueDest) {
2743         Opc = Instruction::Or;
2744       } else if (PBI->getSuccessor(1) == FalseDest) {
2745         Opc = Instruction::And;
2746       } else if (PBI->getSuccessor(0) == FalseDest) {
2747         Opc = Instruction::And;
2748         InvertPredCond = true;
2749       } else if (PBI->getSuccessor(1) == TrueDest) {
2750         Opc = Instruction::Or;
2751         InvertPredCond = true;
2752       } else {
2753         continue;
2754       }
2755     } else {
2756       if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
2757         continue;
2758     }
2759 
2760     LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
2761     Changed = true;
2762 
2763     IRBuilder<> Builder(PBI);
2764 
2765     // If we need to invert the condition in the pred block to match, do so now.
2766     if (InvertPredCond) {
2767       Value *NewCond = PBI->getCondition();
2768 
2769       if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
2770         CmpInst *CI = cast<CmpInst>(NewCond);
2771         CI->setPredicate(CI->getInversePredicate());
2772       } else {
2773         NewCond =
2774             Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not");
2775       }
2776 
2777       PBI->setCondition(NewCond);
2778       PBI->swapSuccessors();
2779     }
2780 
2781     // If we have bonus instructions, clone them into the predecessor block.
2782     // Note that there may be multiple predecessor blocks, so we cannot move
2783     // bonus instructions to a predecessor block.
2784     ValueToValueMapTy VMap; // maps original values to cloned values
2785     // We already make sure Cond is the last instruction before BI. Therefore,
2786     // all instructions before Cond other than DbgInfoIntrinsic are bonus
2787     // instructions.
2788     for (auto BonusInst = BB->begin(); Cond != &*BonusInst; ++BonusInst) {
2789       if (isa<DbgInfoIntrinsic>(BonusInst))
2790         continue;
2791       Instruction *NewBonusInst = BonusInst->clone();
2792 
2793       // When we fold the bonus instructions we want to make sure we
2794       // reset their debug locations in order to avoid stepping on dead
2795       // code caused by folding dead branches.
2796       NewBonusInst->setDebugLoc(DebugLoc());
2797 
2798       RemapInstruction(NewBonusInst, VMap,
2799                        RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2800       VMap[&*BonusInst] = NewBonusInst;
2801 
2802       // If we moved a load, we cannot any longer claim any knowledge about
2803       // its potential value. The previous information might have been valid
2804       // only given the branch precondition.
2805       // For an analogous reason, we must also drop all the metadata whose
2806       // semantics we don't understand.
2807       NewBonusInst->dropUnknownNonDebugMetadata();
2808 
2809       PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
2810       NewBonusInst->takeName(&*BonusInst);
2811       BonusInst->setName(BonusInst->getName() + ".old");
2812     }
2813 
2814     // Clone Cond into the predecessor basic block, and or/and the
2815     // two conditions together.
2816     Instruction *CondInPred = Cond->clone();
2817 
2818     // Reset the condition debug location to avoid jumping on dead code
2819     // as the result of folding dead branches.
2820     CondInPred->setDebugLoc(DebugLoc());
2821 
2822     RemapInstruction(CondInPred, VMap,
2823                      RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2824     PredBlock->getInstList().insert(PBI->getIterator(), CondInPred);
2825     CondInPred->takeName(Cond);
2826     Cond->setName(CondInPred->getName() + ".old");
2827 
2828     if (BI->isConditional()) {
2829       Instruction *NewCond = cast<Instruction>(
2830           Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
2831       PBI->setCondition(NewCond);
2832 
2833       uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2834       bool HasWeights =
2835           extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
2836                                  SuccTrueWeight, SuccFalseWeight);
2837       SmallVector<uint64_t, 8> NewWeights;
2838 
2839       if (PBI->getSuccessor(0) == BB) {
2840         if (HasWeights) {
2841           // PBI: br i1 %x, BB, FalseDest
2842           // BI:  br i1 %y, TrueDest, FalseDest
2843           // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
2844           NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
2845           // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
2846           //               TrueWeight for PBI * FalseWeight for BI.
2847           // We assume that total weights of a BranchInst can fit into 32 bits.
2848           // Therefore, we will not have overflow using 64-bit arithmetic.
2849           NewWeights.push_back(PredFalseWeight *
2850                                    (SuccFalseWeight + SuccTrueWeight) +
2851                                PredTrueWeight * SuccFalseWeight);
2852         }
2853         AddPredecessorToBlock(TrueDest, PredBlock, BB, MSSAU);
2854         PBI->setSuccessor(0, TrueDest);
2855       }
2856       if (PBI->getSuccessor(1) == BB) {
2857         if (HasWeights) {
2858           // PBI: br i1 %x, TrueDest, BB
2859           // BI:  br i1 %y, TrueDest, FalseDest
2860           // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
2861           //              FalseWeight for PBI * TrueWeight for BI.
2862           NewWeights.push_back(PredTrueWeight *
2863                                    (SuccFalseWeight + SuccTrueWeight) +
2864                                PredFalseWeight * SuccTrueWeight);
2865           // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
2866           NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
2867         }
2868         AddPredecessorToBlock(FalseDest, PredBlock, BB, MSSAU);
2869         PBI->setSuccessor(1, FalseDest);
2870       }
2871       if (NewWeights.size() == 2) {
2872         // Halve the weights if any of them cannot fit in an uint32_t
2873         FitWeights(NewWeights);
2874 
2875         SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),
2876                                            NewWeights.end());
2877         setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
2878       } else
2879         PBI->setMetadata(LLVMContext::MD_prof, nullptr);
2880     } else {
2881       // Update PHI nodes in the common successors.
2882       for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
2883         ConstantInt *PBI_C = cast<ConstantInt>(
2884             PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
2885         assert(PBI_C->getType()->isIntegerTy(1));
2886         Instruction *MergedCond = nullptr;
2887         if (PBI->getSuccessor(0) == TrueDest) {
2888           // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
2889           // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
2890           //       is false: !PBI_Cond and BI_Value
2891           Instruction *NotCond = cast<Instruction>(
2892               Builder.CreateNot(PBI->getCondition(), "not.cond"));
2893           MergedCond = cast<Instruction>(
2894                Builder.CreateBinOp(Instruction::And, NotCond, CondInPred,
2895                                    "and.cond"));
2896           if (PBI_C->isOne())
2897             MergedCond = cast<Instruction>(Builder.CreateBinOp(
2898                 Instruction::Or, PBI->getCondition(), MergedCond, "or.cond"));
2899         } else {
2900           // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
2901           // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
2902           //       is false: PBI_Cond and BI_Value
2903           MergedCond = cast<Instruction>(Builder.CreateBinOp(
2904               Instruction::And, PBI->getCondition(), CondInPred, "and.cond"));
2905           if (PBI_C->isOne()) {
2906             Instruction *NotCond = cast<Instruction>(
2907                 Builder.CreateNot(PBI->getCondition(), "not.cond"));
2908             MergedCond = cast<Instruction>(Builder.CreateBinOp(
2909                 Instruction::Or, NotCond, MergedCond, "or.cond"));
2910           }
2911         }
2912         // Update PHI Node.
2913 	PHIs[i]->setIncomingValueForBlock(PBI->getParent(), MergedCond);
2914       }
2915 
2916       // PBI is changed to branch to TrueDest below. Remove itself from
2917       // potential phis from all other successors.
2918       if (MSSAU)
2919         MSSAU->changeCondBranchToUnconditionalTo(PBI, TrueDest);
2920 
2921       // Change PBI from Conditional to Unconditional.
2922       BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
2923       EraseTerminatorAndDCECond(PBI, MSSAU);
2924       PBI = New_PBI;
2925     }
2926 
2927     // If BI was a loop latch, it may have had associated loop metadata.
2928     // We need to copy it to the new latch, that is, PBI.
2929     if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
2930       PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
2931 
2932     // TODO: If BB is reachable from all paths through PredBlock, then we
2933     // could replace PBI's branch probabilities with BI's.
2934 
2935     // Copy any debug value intrinsics into the end of PredBlock.
2936     for (Instruction &I : *BB) {
2937       if (isa<DbgInfoIntrinsic>(I)) {
2938         Instruction *NewI = I.clone();
2939         RemapInstruction(NewI, VMap,
2940                          RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2941         NewI->insertBefore(PBI);
2942       }
2943     }
2944 
2945     return Changed;
2946   }
2947   return Changed;
2948 }
2949 
2950 // If there is only one store in BB1 and BB2, return it, otherwise return
2951 // nullptr.
2952 static StoreInst *findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2) {
2953   StoreInst *S = nullptr;
2954   for (auto *BB : {BB1, BB2}) {
2955     if (!BB)
2956       continue;
2957     for (auto &I : *BB)
2958       if (auto *SI = dyn_cast<StoreInst>(&I)) {
2959         if (S)
2960           // Multiple stores seen.
2961           return nullptr;
2962         else
2963           S = SI;
2964       }
2965   }
2966   return S;
2967 }
2968 
2969 static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
2970                                               Value *AlternativeV = nullptr) {
2971   // PHI is going to be a PHI node that allows the value V that is defined in
2972   // BB to be referenced in BB's only successor.
2973   //
2974   // If AlternativeV is nullptr, the only value we care about in PHI is V. It
2975   // doesn't matter to us what the other operand is (it'll never get used). We
2976   // could just create a new PHI with an undef incoming value, but that could
2977   // increase register pressure if EarlyCSE/InstCombine can't fold it with some
2978   // other PHI. So here we directly look for some PHI in BB's successor with V
2979   // as an incoming operand. If we find one, we use it, else we create a new
2980   // one.
2981   //
2982   // If AlternativeV is not nullptr, we care about both incoming values in PHI.
2983   // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
2984   // where OtherBB is the single other predecessor of BB's only successor.
2985   PHINode *PHI = nullptr;
2986   BasicBlock *Succ = BB->getSingleSuccessor();
2987 
2988   for (auto I = Succ->begin(); isa<PHINode>(I); ++I)
2989     if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
2990       PHI = cast<PHINode>(I);
2991       if (!AlternativeV)
2992         break;
2993 
2994       assert(Succ->hasNPredecessors(2));
2995       auto PredI = pred_begin(Succ);
2996       BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
2997       if (PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
2998         break;
2999       PHI = nullptr;
3000     }
3001   if (PHI)
3002     return PHI;
3003 
3004   // If V is not an instruction defined in BB, just return it.
3005   if (!AlternativeV &&
3006       (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB))
3007     return V;
3008 
3009   PHI = PHINode::Create(V->getType(), 2, "simplifycfg.merge", &Succ->front());
3010   PHI->addIncoming(V, BB);
3011   for (BasicBlock *PredBB : predecessors(Succ))
3012     if (PredBB != BB)
3013       PHI->addIncoming(
3014           AlternativeV ? AlternativeV : UndefValue::get(V->getType()), PredBB);
3015   return PHI;
3016 }
3017 
3018 static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
3019                                            BasicBlock *QTB, BasicBlock *QFB,
3020                                            BasicBlock *PostBB, Value *Address,
3021                                            bool InvertPCond, bool InvertQCond,
3022                                            const DataLayout &DL,
3023                                            const TargetTransformInfo &TTI) {
3024   // For every pointer, there must be exactly two stores, one coming from
3025   // PTB or PFB, and the other from QTB or QFB. We don't support more than one
3026   // store (to any address) in PTB,PFB or QTB,QFB.
3027   // FIXME: We could relax this restriction with a bit more work and performance
3028   // testing.
3029   StoreInst *PStore = findUniqueStoreInBlocks(PTB, PFB);
3030   StoreInst *QStore = findUniqueStoreInBlocks(QTB, QFB);
3031   if (!PStore || !QStore)
3032     return false;
3033 
3034   // Now check the stores are compatible.
3035   if (!QStore->isUnordered() || !PStore->isUnordered())
3036     return false;
3037 
3038   // Check that sinking the store won't cause program behavior changes. Sinking
3039   // the store out of the Q blocks won't change any behavior as we're sinking
3040   // from a block to its unconditional successor. But we're moving a store from
3041   // the P blocks down through the middle block (QBI) and past both QFB and QTB.
3042   // So we need to check that there are no aliasing loads or stores in
3043   // QBI, QTB and QFB. We also need to check there are no conflicting memory
3044   // operations between PStore and the end of its parent block.
3045   //
3046   // The ideal way to do this is to query AliasAnalysis, but we don't
3047   // preserve AA currently so that is dangerous. Be super safe and just
3048   // check there are no other memory operations at all.
3049   for (auto &I : *QFB->getSinglePredecessor())
3050     if (I.mayReadOrWriteMemory())
3051       return false;
3052   for (auto &I : *QFB)
3053     if (&I != QStore && I.mayReadOrWriteMemory())
3054       return false;
3055   if (QTB)
3056     for (auto &I : *QTB)
3057       if (&I != QStore && I.mayReadOrWriteMemory())
3058         return false;
3059   for (auto I = BasicBlock::iterator(PStore), E = PStore->getParent()->end();
3060        I != E; ++I)
3061     if (&*I != PStore && I->mayReadOrWriteMemory())
3062       return false;
3063 
3064   // If we're not in aggressive mode, we only optimize if we have some
3065   // confidence that by optimizing we'll allow P and/or Q to be if-converted.
3066   auto IsWorthwhile = [&](BasicBlock *BB, ArrayRef<StoreInst *> FreeStores) {
3067     if (!BB)
3068       return true;
3069     // Heuristic: if the block can be if-converted/phi-folded and the
3070     // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
3071     // thread this store.
3072     int BudgetRemaining =
3073         PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
3074     for (auto &I : BB->instructionsWithoutDebug()) {
3075       // Consider terminator instruction to be free.
3076       if (I.isTerminator())
3077         continue;
3078       // If this is one the stores that we want to speculate out of this BB,
3079       // then don't count it's cost, consider it to be free.
3080       if (auto *S = dyn_cast<StoreInst>(&I))
3081         if (llvm::find(FreeStores, S))
3082           continue;
3083       // Else, we have a white-list of instructions that we are ak speculating.
3084       if (!isa<BinaryOperator>(I) && !isa<GetElementPtrInst>(I))
3085         return false; // Not in white-list - not worthwhile folding.
3086       // And finally, if this is a non-free instruction that we are okay
3087       // speculating, ensure that we consider the speculation budget.
3088       BudgetRemaining -= TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
3089       if (BudgetRemaining < 0)
3090         return false; // Eagerly refuse to fold as soon as we're out of budget.
3091     }
3092     assert(BudgetRemaining >= 0 &&
3093            "When we run out of budget we will eagerly return from within the "
3094            "per-instruction loop.");
3095     return true;
3096   };
3097 
3098   const SmallVector<StoreInst *, 2> FreeStores = {PStore, QStore};
3099   if (!MergeCondStoresAggressively &&
3100       (!IsWorthwhile(PTB, FreeStores) || !IsWorthwhile(PFB, FreeStores) ||
3101        !IsWorthwhile(QTB, FreeStores) || !IsWorthwhile(QFB, FreeStores)))
3102     return false;
3103 
3104   // If PostBB has more than two predecessors, we need to split it so we can
3105   // sink the store.
3106   if (std::next(pred_begin(PostBB), 2) != pred_end(PostBB)) {
3107     // We know that QFB's only successor is PostBB. And QFB has a single
3108     // predecessor. If QTB exists, then its only successor is also PostBB.
3109     // If QTB does not exist, then QFB's only predecessor has a conditional
3110     // branch to QFB and PostBB.
3111     BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
3112     BasicBlock *NewBB = SplitBlockPredecessors(PostBB, { QFB, TruePred},
3113                                                "condstore.split");
3114     if (!NewBB)
3115       return false;
3116     PostBB = NewBB;
3117   }
3118 
3119   // OK, we're going to sink the stores to PostBB. The store has to be
3120   // conditional though, so first create the predicate.
3121   Value *PCond = cast<BranchInst>(PFB->getSinglePredecessor()->getTerminator())
3122                      ->getCondition();
3123   Value *QCond = cast<BranchInst>(QFB->getSinglePredecessor()->getTerminator())
3124                      ->getCondition();
3125 
3126   Value *PPHI = ensureValueAvailableInSuccessor(PStore->getValueOperand(),
3127                                                 PStore->getParent());
3128   Value *QPHI = ensureValueAvailableInSuccessor(QStore->getValueOperand(),
3129                                                 QStore->getParent(), PPHI);
3130 
3131   IRBuilder<> QB(&*PostBB->getFirstInsertionPt());
3132 
3133   Value *PPred = PStore->getParent() == PTB ? PCond : QB.CreateNot(PCond);
3134   Value *QPred = QStore->getParent() == QTB ? QCond : QB.CreateNot(QCond);
3135 
3136   if (InvertPCond)
3137     PPred = QB.CreateNot(PPred);
3138   if (InvertQCond)
3139     QPred = QB.CreateNot(QPred);
3140   Value *CombinedPred = QB.CreateOr(PPred, QPred);
3141 
3142   auto *T =
3143       SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(), false);
3144   QB.SetInsertPoint(T);
3145   StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI, Address));
3146   AAMDNodes AAMD;
3147   PStore->getAAMetadata(AAMD, /*Merge=*/false);
3148   PStore->getAAMetadata(AAMD, /*Merge=*/true);
3149   SI->setAAMetadata(AAMD);
3150   unsigned PAlignment = PStore->getAlignment();
3151   unsigned QAlignment = QStore->getAlignment();
3152   unsigned TypeAlignment =
3153       DL.getABITypeAlignment(SI->getValueOperand()->getType());
3154   unsigned MinAlignment;
3155   unsigned MaxAlignment;
3156   std::tie(MinAlignment, MaxAlignment) = std::minmax(PAlignment, QAlignment);
3157   // Choose the minimum alignment. If we could prove both stores execute, we
3158   // could use biggest one.  In this case, though, we only know that one of the
3159   // stores executes.  And we don't know it's safe to take the alignment from a
3160   // store that doesn't execute.
3161   if (MinAlignment != 0) {
3162     // Choose the minimum of all non-zero alignments.
3163     SI->setAlignment(Align(MinAlignment));
3164   } else if (MaxAlignment != 0) {
3165     // Choose the minimal alignment between the non-zero alignment and the ABI
3166     // default alignment for the type of the stored value.
3167     SI->setAlignment(Align(std::min(MaxAlignment, TypeAlignment)));
3168   } else {
3169     // If both alignments are zero, use ABI default alignment for the type of
3170     // the stored value.
3171     SI->setAlignment(Align(TypeAlignment));
3172   }
3173 
3174   QStore->eraseFromParent();
3175   PStore->eraseFromParent();
3176 
3177   return true;
3178 }
3179 
3180 static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI,
3181                                    const DataLayout &DL,
3182                                    const TargetTransformInfo &TTI) {
3183   // The intention here is to find diamonds or triangles (see below) where each
3184   // conditional block contains a store to the same address. Both of these
3185   // stores are conditional, so they can't be unconditionally sunk. But it may
3186   // be profitable to speculatively sink the stores into one merged store at the
3187   // end, and predicate the merged store on the union of the two conditions of
3188   // PBI and QBI.
3189   //
3190   // This can reduce the number of stores executed if both of the conditions are
3191   // true, and can allow the blocks to become small enough to be if-converted.
3192   // This optimization will also chain, so that ladders of test-and-set
3193   // sequences can be if-converted away.
3194   //
3195   // We only deal with simple diamonds or triangles:
3196   //
3197   //     PBI       or      PBI        or a combination of the two
3198   //    /   \               | \
3199   //   PTB  PFB             |  PFB
3200   //    \   /               | /
3201   //     QBI                QBI
3202   //    /  \                | \
3203   //   QTB  QFB             |  QFB
3204   //    \  /                | /
3205   //    PostBB            PostBB
3206   //
3207   // We model triangles as a type of diamond with a nullptr "true" block.
3208   // Triangles are canonicalized so that the fallthrough edge is represented by
3209   // a true condition, as in the diagram above.
3210   BasicBlock *PTB = PBI->getSuccessor(0);
3211   BasicBlock *PFB = PBI->getSuccessor(1);
3212   BasicBlock *QTB = QBI->getSuccessor(0);
3213   BasicBlock *QFB = QBI->getSuccessor(1);
3214   BasicBlock *PostBB = QFB->getSingleSuccessor();
3215 
3216   // Make sure we have a good guess for PostBB. If QTB's only successor is
3217   // QFB, then QFB is a better PostBB.
3218   if (QTB->getSingleSuccessor() == QFB)
3219     PostBB = QFB;
3220 
3221   // If we couldn't find a good PostBB, stop.
3222   if (!PostBB)
3223     return false;
3224 
3225   bool InvertPCond = false, InvertQCond = false;
3226   // Canonicalize fallthroughs to the true branches.
3227   if (PFB == QBI->getParent()) {
3228     std::swap(PFB, PTB);
3229     InvertPCond = true;
3230   }
3231   if (QFB == PostBB) {
3232     std::swap(QFB, QTB);
3233     InvertQCond = true;
3234   }
3235 
3236   // From this point on we can assume PTB or QTB may be fallthroughs but PFB
3237   // and QFB may not. Model fallthroughs as a nullptr block.
3238   if (PTB == QBI->getParent())
3239     PTB = nullptr;
3240   if (QTB == PostBB)
3241     QTB = nullptr;
3242 
3243   // Legality bailouts. We must have at least the non-fallthrough blocks and
3244   // the post-dominating block, and the non-fallthroughs must only have one
3245   // predecessor.
3246   auto HasOnePredAndOneSucc = [](BasicBlock *BB, BasicBlock *P, BasicBlock *S) {
3247     return BB->getSinglePredecessor() == P && BB->getSingleSuccessor() == S;
3248   };
3249   if (!HasOnePredAndOneSucc(PFB, PBI->getParent(), QBI->getParent()) ||
3250       !HasOnePredAndOneSucc(QFB, QBI->getParent(), PostBB))
3251     return false;
3252   if ((PTB && !HasOnePredAndOneSucc(PTB, PBI->getParent(), QBI->getParent())) ||
3253       (QTB && !HasOnePredAndOneSucc(QTB, QBI->getParent(), PostBB)))
3254     return false;
3255   if (!QBI->getParent()->hasNUses(2))
3256     return false;
3257 
3258   // OK, this is a sequence of two diamonds or triangles.
3259   // Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
3260   SmallPtrSet<Value *, 4> PStoreAddresses, QStoreAddresses;
3261   for (auto *BB : {PTB, PFB}) {
3262     if (!BB)
3263       continue;
3264     for (auto &I : *BB)
3265       if (StoreInst *SI = dyn_cast<StoreInst>(&I))
3266         PStoreAddresses.insert(SI->getPointerOperand());
3267   }
3268   for (auto *BB : {QTB, QFB}) {
3269     if (!BB)
3270       continue;
3271     for (auto &I : *BB)
3272       if (StoreInst *SI = dyn_cast<StoreInst>(&I))
3273         QStoreAddresses.insert(SI->getPointerOperand());
3274   }
3275 
3276   set_intersect(PStoreAddresses, QStoreAddresses);
3277   // set_intersect mutates PStoreAddresses in place. Rename it here to make it
3278   // clear what it contains.
3279   auto &CommonAddresses = PStoreAddresses;
3280 
3281   bool Changed = false;
3282   for (auto *Address : CommonAddresses)
3283     Changed |= mergeConditionalStoreToAddress(
3284         PTB, PFB, QTB, QFB, PostBB, Address, InvertPCond, InvertQCond, DL, TTI);
3285   return Changed;
3286 }
3287 
3288 
3289 /// If the previous block ended with a widenable branch, determine if reusing
3290 /// the target block is profitable and legal.  This will have the effect of
3291 /// "widening" PBI, but doesn't require us to reason about hosting safety.
3292 static bool tryWidenCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
3293   // TODO: This can be generalized in two important ways:
3294   // 1) We can allow phi nodes in IfFalseBB and simply reuse all the input
3295   //    values from the PBI edge.
3296   // 2) We can sink side effecting instructions into BI's fallthrough
3297   //    successor provided they doesn't contribute to computation of
3298   //    BI's condition.
3299   Value *CondWB, *WC;
3300   BasicBlock *IfTrueBB, *IfFalseBB;
3301   if (!parseWidenableBranch(PBI, CondWB, WC, IfTrueBB, IfFalseBB) ||
3302       IfTrueBB != BI->getParent() || !BI->getParent()->getSinglePredecessor())
3303     return false;
3304   if (!IfFalseBB->phis().empty())
3305     return false; // TODO
3306   // Use lambda to lazily compute expensive condition after cheap ones.
3307   auto NoSideEffects = [](BasicBlock &BB) {
3308     return !llvm::any_of(BB, [](const Instruction &I) {
3309         return I.mayWriteToMemory() || I.mayHaveSideEffects();
3310       });
3311   };
3312   if (BI->getSuccessor(1) != IfFalseBB && // no inf looping
3313       BI->getSuccessor(1)->getTerminatingDeoptimizeCall() && // profitability
3314       NoSideEffects(*BI->getParent())) {
3315     BI->getSuccessor(1)->removePredecessor(BI->getParent());
3316     BI->setSuccessor(1, IfFalseBB);
3317     return true;
3318   }
3319   if (BI->getSuccessor(0) != IfFalseBB && // no inf looping
3320       BI->getSuccessor(0)->getTerminatingDeoptimizeCall() && // profitability
3321       NoSideEffects(*BI->getParent())) {
3322     BI->getSuccessor(0)->removePredecessor(BI->getParent());
3323     BI->setSuccessor(0, IfFalseBB);
3324     return true;
3325   }
3326   return false;
3327 }
3328 
3329 /// If we have a conditional branch as a predecessor of another block,
3330 /// this function tries to simplify it.  We know
3331 /// that PBI and BI are both conditional branches, and BI is in one of the
3332 /// successor blocks of PBI - PBI branches to BI.
3333 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
3334                                            const DataLayout &DL,
3335                                            const TargetTransformInfo &TTI) {
3336   assert(PBI->isConditional() && BI->isConditional());
3337   BasicBlock *BB = BI->getParent();
3338 
3339   // If this block ends with a branch instruction, and if there is a
3340   // predecessor that ends on a branch of the same condition, make
3341   // this conditional branch redundant.
3342   if (PBI->getCondition() == BI->getCondition() &&
3343       PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3344     // Okay, the outcome of this conditional branch is statically
3345     // knowable.  If this block had a single pred, handle specially.
3346     if (BB->getSinglePredecessor()) {
3347       // Turn this into a branch on constant.
3348       bool CondIsTrue = PBI->getSuccessor(0) == BB;
3349       BI->setCondition(
3350           ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue));
3351       return true; // Nuke the branch on constant.
3352     }
3353 
3354     // Otherwise, if there are multiple predecessors, insert a PHI that merges
3355     // in the constant and simplify the block result.  Subsequent passes of
3356     // simplifycfg will thread the block.
3357     if (BlockIsSimpleEnoughToThreadThrough(BB)) {
3358       pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
3359       PHINode *NewPN = PHINode::Create(
3360           Type::getInt1Ty(BB->getContext()), std::distance(PB, PE),
3361           BI->getCondition()->getName() + ".pr", &BB->front());
3362       // Okay, we're going to insert the PHI node.  Since PBI is not the only
3363       // predecessor, compute the PHI'd conditional value for all of the preds.
3364       // Any predecessor where the condition is not computable we keep symbolic.
3365       for (pred_iterator PI = PB; PI != PE; ++PI) {
3366         BasicBlock *P = *PI;
3367         if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && PBI != BI &&
3368             PBI->isConditional() && PBI->getCondition() == BI->getCondition() &&
3369             PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3370           bool CondIsTrue = PBI->getSuccessor(0) == BB;
3371           NewPN->addIncoming(
3372               ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue),
3373               P);
3374         } else {
3375           NewPN->addIncoming(BI->getCondition(), P);
3376         }
3377       }
3378 
3379       BI->setCondition(NewPN);
3380       return true;
3381     }
3382   }
3383 
3384   // If the previous block ended with a widenable branch, determine if reusing
3385   // the target block is profitable and legal.  This will have the effect of
3386   // "widening" PBI, but doesn't require us to reason about hosting safety.
3387   if (tryWidenCondBranchToCondBranch(PBI, BI))
3388     return true;
3389 
3390   if (auto *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
3391     if (CE->canTrap())
3392       return false;
3393 
3394   // If both branches are conditional and both contain stores to the same
3395   // address, remove the stores from the conditionals and create a conditional
3396   // merged store at the end.
3397   if (MergeCondStores && mergeConditionalStores(PBI, BI, DL, TTI))
3398     return true;
3399 
3400   // If this is a conditional branch in an empty block, and if any
3401   // predecessors are a conditional branch to one of our destinations,
3402   // fold the conditions into logical ops and one cond br.
3403 
3404   // Ignore dbg intrinsics.
3405   if (&*BB->instructionsWithoutDebug().begin() != BI)
3406     return false;
3407 
3408   int PBIOp, BIOp;
3409   if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
3410     PBIOp = 0;
3411     BIOp = 0;
3412   } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
3413     PBIOp = 0;
3414     BIOp = 1;
3415   } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
3416     PBIOp = 1;
3417     BIOp = 0;
3418   } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
3419     PBIOp = 1;
3420     BIOp = 1;
3421   } else {
3422     return false;
3423   }
3424 
3425   // Check to make sure that the other destination of this branch
3426   // isn't BB itself.  If so, this is an infinite loop that will
3427   // keep getting unwound.
3428   if (PBI->getSuccessor(PBIOp) == BB)
3429     return false;
3430 
3431   // Do not perform this transformation if it would require
3432   // insertion of a large number of select instructions. For targets
3433   // without predication/cmovs, this is a big pessimization.
3434 
3435   // Also do not perform this transformation if any phi node in the common
3436   // destination block can trap when reached by BB or PBB (PR17073). In that
3437   // case, it would be unsafe to hoist the operation into a select instruction.
3438 
3439   BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
3440   unsigned NumPhis = 0;
3441   for (BasicBlock::iterator II = CommonDest->begin(); isa<PHINode>(II);
3442        ++II, ++NumPhis) {
3443     if (NumPhis > 2) // Disable this xform.
3444       return false;
3445 
3446     PHINode *PN = cast<PHINode>(II);
3447     Value *BIV = PN->getIncomingValueForBlock(BB);
3448     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV))
3449       if (CE->canTrap())
3450         return false;
3451 
3452     unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
3453     Value *PBIV = PN->getIncomingValue(PBBIdx);
3454     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV))
3455       if (CE->canTrap())
3456         return false;
3457   }
3458 
3459   // Finally, if everything is ok, fold the branches to logical ops.
3460   BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
3461 
3462   LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
3463                     << "AND: " << *BI->getParent());
3464 
3465   // If OtherDest *is* BB, then BB is a basic block with a single conditional
3466   // branch in it, where one edge (OtherDest) goes back to itself but the other
3467   // exits.  We don't *know* that the program avoids the infinite loop
3468   // (even though that seems likely).  If we do this xform naively, we'll end up
3469   // recursively unpeeling the loop.  Since we know that (after the xform is
3470   // done) that the block *is* infinite if reached, we just make it an obviously
3471   // infinite loop with no cond branch.
3472   if (OtherDest == BB) {
3473     // Insert it at the end of the function, because it's either code,
3474     // or it won't matter if it's hot. :)
3475     BasicBlock *InfLoopBlock =
3476         BasicBlock::Create(BB->getContext(), "infloop", BB->getParent());
3477     BranchInst::Create(InfLoopBlock, InfLoopBlock);
3478     OtherDest = InfLoopBlock;
3479   }
3480 
3481   LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3482 
3483   // BI may have other predecessors.  Because of this, we leave
3484   // it alone, but modify PBI.
3485 
3486   // Make sure we get to CommonDest on True&True directions.
3487   Value *PBICond = PBI->getCondition();
3488   IRBuilder<NoFolder> Builder(PBI);
3489   if (PBIOp)
3490     PBICond = Builder.CreateNot(PBICond, PBICond->getName() + ".not");
3491 
3492   Value *BICond = BI->getCondition();
3493   if (BIOp)
3494     BICond = Builder.CreateNot(BICond, BICond->getName() + ".not");
3495 
3496   // Merge the conditions.
3497   Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
3498 
3499   // Modify PBI to branch on the new condition to the new dests.
3500   PBI->setCondition(Cond);
3501   PBI->setSuccessor(0, CommonDest);
3502   PBI->setSuccessor(1, OtherDest);
3503 
3504   // Update branch weight for PBI.
3505   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3506   uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
3507   bool HasWeights =
3508       extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
3509                              SuccTrueWeight, SuccFalseWeight);
3510   if (HasWeights) {
3511     PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3512     PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3513     SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3514     SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3515     // The weight to CommonDest should be PredCommon * SuccTotal +
3516     //                                    PredOther * SuccCommon.
3517     // The weight to OtherDest should be PredOther * SuccOther.
3518     uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
3519                                   PredOther * SuccCommon,
3520                               PredOther * SuccOther};
3521     // Halve the weights if any of them cannot fit in an uint32_t
3522     FitWeights(NewWeights);
3523 
3524     setBranchWeights(PBI, NewWeights[0], NewWeights[1]);
3525   }
3526 
3527   // OtherDest may have phi nodes.  If so, add an entry from PBI's
3528   // block that are identical to the entries for BI's block.
3529   AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
3530 
3531   // We know that the CommonDest already had an edge from PBI to
3532   // it.  If it has PHIs though, the PHIs may have different
3533   // entries for BB and PBI's BB.  If so, insert a select to make
3534   // them agree.
3535   for (PHINode &PN : CommonDest->phis()) {
3536     Value *BIV = PN.getIncomingValueForBlock(BB);
3537     unsigned PBBIdx = PN.getBasicBlockIndex(PBI->getParent());
3538     Value *PBIV = PN.getIncomingValue(PBBIdx);
3539     if (BIV != PBIV) {
3540       // Insert a select in PBI to pick the right value.
3541       SelectInst *NV = cast<SelectInst>(
3542           Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
3543       PN.setIncomingValue(PBBIdx, NV);
3544       // Although the select has the same condition as PBI, the original branch
3545       // weights for PBI do not apply to the new select because the select's
3546       // 'logical' edges are incoming edges of the phi that is eliminated, not
3547       // the outgoing edges of PBI.
3548       if (HasWeights) {
3549         uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3550         uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3551         uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3552         uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3553         // The weight to PredCommonDest should be PredCommon * SuccTotal.
3554         // The weight to PredOtherDest should be PredOther * SuccCommon.
3555         uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
3556                                   PredOther * SuccCommon};
3557 
3558         FitWeights(NewWeights);
3559 
3560         setBranchWeights(NV, NewWeights[0], NewWeights[1]);
3561       }
3562     }
3563   }
3564 
3565   LLVM_DEBUG(dbgs() << "INTO: " << *PBI->getParent());
3566   LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3567 
3568   // This basic block is probably dead.  We know it has at least
3569   // one fewer predecessor.
3570   return true;
3571 }
3572 
3573 // Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
3574 // true or to FalseBB if Cond is false.
3575 // Takes care of updating the successors and removing the old terminator.
3576 // Also makes sure not to introduce new successors by assuming that edges to
3577 // non-successor TrueBBs and FalseBBs aren't reachable.
3578 bool SimplifyCFGOpt::SimplifyTerminatorOnSelect(Instruction *OldTerm,
3579                                                 Value *Cond, BasicBlock *TrueBB,
3580                                                 BasicBlock *FalseBB,
3581                                                 uint32_t TrueWeight,
3582                                                 uint32_t FalseWeight) {
3583   // Remove any superfluous successor edges from the CFG.
3584   // First, figure out which successors to preserve.
3585   // If TrueBB and FalseBB are equal, only try to preserve one copy of that
3586   // successor.
3587   BasicBlock *KeepEdge1 = TrueBB;
3588   BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
3589 
3590   // Then remove the rest.
3591   for (BasicBlock *Succ : successors(OldTerm)) {
3592     // Make sure only to keep exactly one copy of each edge.
3593     if (Succ == KeepEdge1)
3594       KeepEdge1 = nullptr;
3595     else if (Succ == KeepEdge2)
3596       KeepEdge2 = nullptr;
3597     else
3598       Succ->removePredecessor(OldTerm->getParent(),
3599                               /*KeepOneInputPHIs=*/true);
3600   }
3601 
3602   IRBuilder<> Builder(OldTerm);
3603   Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
3604 
3605   // Insert an appropriate new terminator.
3606   if (!KeepEdge1 && !KeepEdge2) {
3607     if (TrueBB == FalseBB)
3608       // We were only looking for one successor, and it was present.
3609       // Create an unconditional branch to it.
3610       Builder.CreateBr(TrueBB);
3611     else {
3612       // We found both of the successors we were looking for.
3613       // Create a conditional branch sharing the condition of the select.
3614       BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
3615       if (TrueWeight != FalseWeight)
3616         setBranchWeights(NewBI, TrueWeight, FalseWeight);
3617     }
3618   } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
3619     // Neither of the selected blocks were successors, so this
3620     // terminator must be unreachable.
3621     new UnreachableInst(OldTerm->getContext(), OldTerm);
3622   } else {
3623     // One of the selected values was a successor, but the other wasn't.
3624     // Insert an unconditional branch to the one that was found;
3625     // the edge to the one that wasn't must be unreachable.
3626     if (!KeepEdge1)
3627       // Only TrueBB was found.
3628       Builder.CreateBr(TrueBB);
3629     else
3630       // Only FalseBB was found.
3631       Builder.CreateBr(FalseBB);
3632   }
3633 
3634   EraseTerminatorAndDCECond(OldTerm);
3635   return true;
3636 }
3637 
3638 // Replaces
3639 //   (switch (select cond, X, Y)) on constant X, Y
3640 // with a branch - conditional if X and Y lead to distinct BBs,
3641 // unconditional otherwise.
3642 bool SimplifyCFGOpt::SimplifySwitchOnSelect(SwitchInst *SI,
3643                                             SelectInst *Select) {
3644   // Check for constant integer values in the select.
3645   ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
3646   ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
3647   if (!TrueVal || !FalseVal)
3648     return false;
3649 
3650   // Find the relevant condition and destinations.
3651   Value *Condition = Select->getCondition();
3652   BasicBlock *TrueBB = SI->findCaseValue(TrueVal)->getCaseSuccessor();
3653   BasicBlock *FalseBB = SI->findCaseValue(FalseVal)->getCaseSuccessor();
3654 
3655   // Get weight for TrueBB and FalseBB.
3656   uint32_t TrueWeight = 0, FalseWeight = 0;
3657   SmallVector<uint64_t, 8> Weights;
3658   bool HasWeights = HasBranchWeights(SI);
3659   if (HasWeights) {
3660     GetBranchWeights(SI, Weights);
3661     if (Weights.size() == 1 + SI->getNumCases()) {
3662       TrueWeight =
3663           (uint32_t)Weights[SI->findCaseValue(TrueVal)->getSuccessorIndex()];
3664       FalseWeight =
3665           (uint32_t)Weights[SI->findCaseValue(FalseVal)->getSuccessorIndex()];
3666     }
3667   }
3668 
3669   // Perform the actual simplification.
3670   return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
3671                                     FalseWeight);
3672 }
3673 
3674 // Replaces
3675 //   (indirectbr (select cond, blockaddress(@fn, BlockA),
3676 //                             blockaddress(@fn, BlockB)))
3677 // with
3678 //   (br cond, BlockA, BlockB).
3679 bool SimplifyCFGOpt::SimplifyIndirectBrOnSelect(IndirectBrInst *IBI,
3680                                                 SelectInst *SI) {
3681   // Check that both operands of the select are block addresses.
3682   BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
3683   BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
3684   if (!TBA || !FBA)
3685     return false;
3686 
3687   // Extract the actual blocks.
3688   BasicBlock *TrueBB = TBA->getBasicBlock();
3689   BasicBlock *FalseBB = FBA->getBasicBlock();
3690 
3691   // Perform the actual simplification.
3692   return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB, 0,
3693                                     0);
3694 }
3695 
3696 /// This is called when we find an icmp instruction
3697 /// (a seteq/setne with a constant) as the only instruction in a
3698 /// block that ends with an uncond branch.  We are looking for a very specific
3699 /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified.  In
3700 /// this case, we merge the first two "or's of icmp" into a switch, but then the
3701 /// default value goes to an uncond block with a seteq in it, we get something
3702 /// like:
3703 ///
3704 ///   switch i8 %A, label %DEFAULT [ i8 1, label %end    i8 2, label %end ]
3705 /// DEFAULT:
3706 ///   %tmp = icmp eq i8 %A, 92
3707 ///   br label %end
3708 /// end:
3709 ///   ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
3710 ///
3711 /// We prefer to split the edge to 'end' so that there is a true/false entry to
3712 /// the PHI, merging the third icmp into the switch.
3713 bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
3714     ICmpInst *ICI, IRBuilder<> &Builder) {
3715   BasicBlock *BB = ICI->getParent();
3716 
3717   // If the block has any PHIs in it or the icmp has multiple uses, it is too
3718   // complex.
3719   if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse())
3720     return false;
3721 
3722   Value *V = ICI->getOperand(0);
3723   ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
3724 
3725   // The pattern we're looking for is where our only predecessor is a switch on
3726   // 'V' and this block is the default case for the switch.  In this case we can
3727   // fold the compared value into the switch to simplify things.
3728   BasicBlock *Pred = BB->getSinglePredecessor();
3729   if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
3730     return false;
3731 
3732   SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
3733   if (SI->getCondition() != V)
3734     return false;
3735 
3736   // If BB is reachable on a non-default case, then we simply know the value of
3737   // V in this block.  Substitute it and constant fold the icmp instruction
3738   // away.
3739   if (SI->getDefaultDest() != BB) {
3740     ConstantInt *VVal = SI->findCaseDest(BB);
3741     assert(VVal && "Should have a unique destination value");
3742     ICI->setOperand(0, VVal);
3743 
3744     if (Value *V = SimplifyInstruction(ICI, {DL, ICI})) {
3745       ICI->replaceAllUsesWith(V);
3746       ICI->eraseFromParent();
3747     }
3748     // BB is now empty, so it is likely to simplify away.
3749     return requestResimplify();
3750   }
3751 
3752   // Ok, the block is reachable from the default dest.  If the constant we're
3753   // comparing exists in one of the other edges, then we can constant fold ICI
3754   // and zap it.
3755   if (SI->findCaseValue(Cst) != SI->case_default()) {
3756     Value *V;
3757     if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
3758       V = ConstantInt::getFalse(BB->getContext());
3759     else
3760       V = ConstantInt::getTrue(BB->getContext());
3761 
3762     ICI->replaceAllUsesWith(V);
3763     ICI->eraseFromParent();
3764     // BB is now empty, so it is likely to simplify away.
3765     return requestResimplify();
3766   }
3767 
3768   // The use of the icmp has to be in the 'end' block, by the only PHI node in
3769   // the block.
3770   BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
3771   PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
3772   if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
3773       isa<PHINode>(++BasicBlock::iterator(PHIUse)))
3774     return false;
3775 
3776   // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
3777   // true in the PHI.
3778   Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
3779   Constant *NewCst = ConstantInt::getFalse(BB->getContext());
3780 
3781   if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
3782     std::swap(DefaultCst, NewCst);
3783 
3784   // Replace ICI (which is used by the PHI for the default value) with true or
3785   // false depending on if it is EQ or NE.
3786   ICI->replaceAllUsesWith(DefaultCst);
3787   ICI->eraseFromParent();
3788 
3789   // Okay, the switch goes to this block on a default value.  Add an edge from
3790   // the switch to the merge point on the compared value.
3791   BasicBlock *NewBB =
3792       BasicBlock::Create(BB->getContext(), "switch.edge", BB->getParent(), BB);
3793   {
3794     SwitchInstProfUpdateWrapper SIW(*SI);
3795     auto W0 = SIW.getSuccessorWeight(0);
3796     SwitchInstProfUpdateWrapper::CaseWeightOpt NewW;
3797     if (W0) {
3798       NewW = ((uint64_t(*W0) + 1) >> 1);
3799       SIW.setSuccessorWeight(0, *NewW);
3800     }
3801     SIW.addCase(Cst, NewBB, NewW);
3802   }
3803 
3804   // NewBB branches to the phi block, add the uncond branch and the phi entry.
3805   Builder.SetInsertPoint(NewBB);
3806   Builder.SetCurrentDebugLocation(SI->getDebugLoc());
3807   Builder.CreateBr(SuccBlock);
3808   PHIUse->addIncoming(NewCst, NewBB);
3809   return true;
3810 }
3811 
3812 /// The specified branch is a conditional branch.
3813 /// Check to see if it is branching on an or/and chain of icmp instructions, and
3814 /// fold it into a switch instruction if so.
3815 bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst *BI,
3816                                                IRBuilder<> &Builder,
3817                                                const DataLayout &DL) {
3818   Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
3819   if (!Cond)
3820     return false;
3821 
3822   // Change br (X == 0 | X == 1), T, F into a switch instruction.
3823   // If this is a bunch of seteq's or'd together, or if it's a bunch of
3824   // 'setne's and'ed together, collect them.
3825 
3826   // Try to gather values from a chain of and/or to be turned into a switch
3827   ConstantComparesGatherer ConstantCompare(Cond, DL);
3828   // Unpack the result
3829   SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
3830   Value *CompVal = ConstantCompare.CompValue;
3831   unsigned UsedICmps = ConstantCompare.UsedICmps;
3832   Value *ExtraCase = ConstantCompare.Extra;
3833 
3834   // If we didn't have a multiply compared value, fail.
3835   if (!CompVal)
3836     return false;
3837 
3838   // Avoid turning single icmps into a switch.
3839   if (UsedICmps <= 1)
3840     return false;
3841 
3842   bool TrueWhenEqual = (Cond->getOpcode() == Instruction::Or);
3843 
3844   // There might be duplicate constants in the list, which the switch
3845   // instruction can't handle, remove them now.
3846   array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
3847   Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
3848 
3849   // If Extra was used, we require at least two switch values to do the
3850   // transformation.  A switch with one value is just a conditional branch.
3851   if (ExtraCase && Values.size() < 2)
3852     return false;
3853 
3854   // TODO: Preserve branch weight metadata, similarly to how
3855   // FoldValueComparisonIntoPredecessors preserves it.
3856 
3857   // Figure out which block is which destination.
3858   BasicBlock *DefaultBB = BI->getSuccessor(1);
3859   BasicBlock *EdgeBB = BI->getSuccessor(0);
3860   if (!TrueWhenEqual)
3861     std::swap(DefaultBB, EdgeBB);
3862 
3863   BasicBlock *BB = BI->getParent();
3864 
3865   // MSAN does not like undefs as branch condition which can be introduced
3866   // with "explicit branch".
3867   if (ExtraCase && BB->getParent()->hasFnAttribute(Attribute::SanitizeMemory))
3868     return false;
3869 
3870   LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
3871                     << " cases into SWITCH.  BB is:\n"
3872                     << *BB);
3873 
3874   // If there are any extra values that couldn't be folded into the switch
3875   // then we evaluate them with an explicit branch first. Split the block
3876   // right before the condbr to handle it.
3877   if (ExtraCase) {
3878     BasicBlock *NewBB =
3879         BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
3880     // Remove the uncond branch added to the old block.
3881     Instruction *OldTI = BB->getTerminator();
3882     Builder.SetInsertPoint(OldTI);
3883 
3884     if (TrueWhenEqual)
3885       Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
3886     else
3887       Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
3888 
3889     OldTI->eraseFromParent();
3890 
3891     // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
3892     // for the edge we just added.
3893     AddPredecessorToBlock(EdgeBB, BB, NewBB);
3894 
3895     LLVM_DEBUG(dbgs() << "  ** 'icmp' chain unhandled condition: " << *ExtraCase
3896                       << "\nEXTRABB = " << *BB);
3897     BB = NewBB;
3898   }
3899 
3900   Builder.SetInsertPoint(BI);
3901   // Convert pointer to int before we switch.
3902   if (CompVal->getType()->isPointerTy()) {
3903     CompVal = Builder.CreatePtrToInt(
3904         CompVal, DL.getIntPtrType(CompVal->getType()), "magicptr");
3905   }
3906 
3907   // Create the new switch instruction now.
3908   SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
3909 
3910   // Add all of the 'cases' to the switch instruction.
3911   for (unsigned i = 0, e = Values.size(); i != e; ++i)
3912     New->addCase(Values[i], EdgeBB);
3913 
3914   // We added edges from PI to the EdgeBB.  As such, if there were any
3915   // PHI nodes in EdgeBB, they need entries to be added corresponding to
3916   // the number of edges added.
3917   for (BasicBlock::iterator BBI = EdgeBB->begin(); isa<PHINode>(BBI); ++BBI) {
3918     PHINode *PN = cast<PHINode>(BBI);
3919     Value *InVal = PN->getIncomingValueForBlock(BB);
3920     for (unsigned i = 0, e = Values.size() - 1; i != e; ++i)
3921       PN->addIncoming(InVal, BB);
3922   }
3923 
3924   // Erase the old branch instruction.
3925   EraseTerminatorAndDCECond(BI);
3926 
3927   LLVM_DEBUG(dbgs() << "  ** 'icmp' chain result is:\n" << *BB << '\n');
3928   return true;
3929 }
3930 
3931 bool SimplifyCFGOpt::simplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
3932   if (isa<PHINode>(RI->getValue()))
3933     return simplifyCommonResume(RI);
3934   else if (isa<LandingPadInst>(RI->getParent()->getFirstNonPHI()) &&
3935            RI->getValue() == RI->getParent()->getFirstNonPHI())
3936     // The resume must unwind the exception that caused control to branch here.
3937     return simplifySingleResume(RI);
3938 
3939   return false;
3940 }
3941 
3942 // Simplify resume that is shared by several landing pads (phi of landing pad).
3943 bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) {
3944   BasicBlock *BB = RI->getParent();
3945 
3946   // Check that there are no other instructions except for debug intrinsics
3947   // between the phi of landing pads (RI->getValue()) and resume instruction.
3948   BasicBlock::iterator I = cast<Instruction>(RI->getValue())->getIterator(),
3949                        E = RI->getIterator();
3950   while (++I != E)
3951     if (!isa<DbgInfoIntrinsic>(I))
3952       return false;
3953 
3954   SmallSetVector<BasicBlock *, 4> TrivialUnwindBlocks;
3955   auto *PhiLPInst = cast<PHINode>(RI->getValue());
3956 
3957   // Check incoming blocks to see if any of them are trivial.
3958   for (unsigned Idx = 0, End = PhiLPInst->getNumIncomingValues(); Idx != End;
3959        Idx++) {
3960     auto *IncomingBB = PhiLPInst->getIncomingBlock(Idx);
3961     auto *IncomingValue = PhiLPInst->getIncomingValue(Idx);
3962 
3963     // If the block has other successors, we can not delete it because
3964     // it has other dependents.
3965     if (IncomingBB->getUniqueSuccessor() != BB)
3966       continue;
3967 
3968     auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHI());
3969     // Not the landing pad that caused the control to branch here.
3970     if (IncomingValue != LandingPad)
3971       continue;
3972 
3973     bool isTrivial = true;
3974 
3975     I = IncomingBB->getFirstNonPHI()->getIterator();
3976     E = IncomingBB->getTerminator()->getIterator();
3977     while (++I != E)
3978       if (!isa<DbgInfoIntrinsic>(I)) {
3979         isTrivial = false;
3980         break;
3981       }
3982 
3983     if (isTrivial)
3984       TrivialUnwindBlocks.insert(IncomingBB);
3985   }
3986 
3987   // If no trivial unwind blocks, don't do any simplifications.
3988   if (TrivialUnwindBlocks.empty())
3989     return false;
3990 
3991   // Turn all invokes that unwind here into calls.
3992   for (auto *TrivialBB : TrivialUnwindBlocks) {
3993     // Blocks that will be simplified should be removed from the phi node.
3994     // Note there could be multiple edges to the resume block, and we need
3995     // to remove them all.
3996     while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
3997       BB->removePredecessor(TrivialBB, true);
3998 
3999     for (pred_iterator PI = pred_begin(TrivialBB), PE = pred_end(TrivialBB);
4000          PI != PE;) {
4001       BasicBlock *Pred = *PI++;
4002       removeUnwindEdge(Pred);
4003     }
4004 
4005     // In each SimplifyCFG run, only the current processed block can be erased.
4006     // Otherwise, it will break the iteration of SimplifyCFG pass. So instead
4007     // of erasing TrivialBB, we only remove the branch to the common resume
4008     // block so that we can later erase the resume block since it has no
4009     // predecessors.
4010     TrivialBB->getTerminator()->eraseFromParent();
4011     new UnreachableInst(RI->getContext(), TrivialBB);
4012   }
4013 
4014   // Delete the resume block if all its predecessors have been removed.
4015   if (pred_empty(BB))
4016     BB->eraseFromParent();
4017 
4018   return !TrivialUnwindBlocks.empty();
4019 }
4020 
4021 // Check if cleanup block is empty
4022 static bool isCleanupBlockEmpty(Instruction *Inst, Instruction *RI) {
4023   BasicBlock::iterator I = Inst->getIterator(), E = RI->getIterator();
4024   while (++I != E) {
4025     auto *II = dyn_cast<IntrinsicInst>(I);
4026     if (!II)
4027       return false;
4028 
4029     Intrinsic::ID IntrinsicID = II->getIntrinsicID();
4030     switch (IntrinsicID) {
4031     case Intrinsic::dbg_declare:
4032     case Intrinsic::dbg_value:
4033     case Intrinsic::dbg_label:
4034     case Intrinsic::lifetime_end:
4035       break;
4036     default:
4037       return false;
4038     }
4039   }
4040   return true;
4041 }
4042 
4043 // Simplify resume that is only used by a single (non-phi) landing pad.
4044 bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
4045   BasicBlock *BB = RI->getParent();
4046   auto *LPInst = cast<LandingPadInst>(BB->getFirstNonPHI());
4047   assert(RI->getValue() == LPInst &&
4048          "Resume must unwind the exception that caused control to here");
4049 
4050   // Check that there are no other instructions except for debug intrinsics.
4051   if (!isCleanupBlockEmpty(LPInst, RI))
4052     return false;
4053 
4054   // Turn all invokes that unwind here into calls and delete the basic block.
4055   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
4056     BasicBlock *Pred = *PI++;
4057     removeUnwindEdge(Pred);
4058   }
4059 
4060   // The landingpad is now unreachable.  Zap it.
4061   if (LoopHeaders)
4062     LoopHeaders->erase(BB);
4063   BB->eraseFromParent();
4064   return true;
4065 }
4066 
4067 static bool removeEmptyCleanup(CleanupReturnInst *RI) {
4068   // If this is a trivial cleanup pad that executes no instructions, it can be
4069   // eliminated.  If the cleanup pad continues to the caller, any predecessor
4070   // that is an EH pad will be updated to continue to the caller and any
4071   // predecessor that terminates with an invoke instruction will have its invoke
4072   // instruction converted to a call instruction.  If the cleanup pad being
4073   // simplified does not continue to the caller, each predecessor will be
4074   // updated to continue to the unwind destination of the cleanup pad being
4075   // simplified.
4076   BasicBlock *BB = RI->getParent();
4077   CleanupPadInst *CPInst = RI->getCleanupPad();
4078   if (CPInst->getParent() != BB)
4079     // This isn't an empty cleanup.
4080     return false;
4081 
4082   // We cannot kill the pad if it has multiple uses.  This typically arises
4083   // from unreachable basic blocks.
4084   if (!CPInst->hasOneUse())
4085     return false;
4086 
4087   // Check that there are no other instructions except for benign intrinsics.
4088   if (!isCleanupBlockEmpty(CPInst, RI))
4089     return false;
4090 
4091   // If the cleanup return we are simplifying unwinds to the caller, this will
4092   // set UnwindDest to nullptr.
4093   BasicBlock *UnwindDest = RI->getUnwindDest();
4094   Instruction *DestEHPad = UnwindDest ? UnwindDest->getFirstNonPHI() : nullptr;
4095 
4096   // We're about to remove BB from the control flow.  Before we do, sink any
4097   // PHINodes into the unwind destination.  Doing this before changing the
4098   // control flow avoids some potentially slow checks, since we can currently
4099   // be certain that UnwindDest and BB have no common predecessors (since they
4100   // are both EH pads).
4101   if (UnwindDest) {
4102     // First, go through the PHI nodes in UnwindDest and update any nodes that
4103     // reference the block we are removing
4104     for (BasicBlock::iterator I = UnwindDest->begin(),
4105                               IE = DestEHPad->getIterator();
4106          I != IE; ++I) {
4107       PHINode *DestPN = cast<PHINode>(I);
4108 
4109       int Idx = DestPN->getBasicBlockIndex(BB);
4110       // Since BB unwinds to UnwindDest, it has to be in the PHI node.
4111       assert(Idx != -1);
4112       // This PHI node has an incoming value that corresponds to a control
4113       // path through the cleanup pad we are removing.  If the incoming
4114       // value is in the cleanup pad, it must be a PHINode (because we
4115       // verified above that the block is otherwise empty).  Otherwise, the
4116       // value is either a constant or a value that dominates the cleanup
4117       // pad being removed.
4118       //
4119       // Because BB and UnwindDest are both EH pads, all of their
4120       // predecessors must unwind to these blocks, and since no instruction
4121       // can have multiple unwind destinations, there will be no overlap in
4122       // incoming blocks between SrcPN and DestPN.
4123       Value *SrcVal = DestPN->getIncomingValue(Idx);
4124       PHINode *SrcPN = dyn_cast<PHINode>(SrcVal);
4125 
4126       // Remove the entry for the block we are deleting.
4127       DestPN->removeIncomingValue(Idx, false);
4128 
4129       if (SrcPN && SrcPN->getParent() == BB) {
4130         // If the incoming value was a PHI node in the cleanup pad we are
4131         // removing, we need to merge that PHI node's incoming values into
4132         // DestPN.
4133         for (unsigned SrcIdx = 0, SrcE = SrcPN->getNumIncomingValues();
4134              SrcIdx != SrcE; ++SrcIdx) {
4135           DestPN->addIncoming(SrcPN->getIncomingValue(SrcIdx),
4136                               SrcPN->getIncomingBlock(SrcIdx));
4137         }
4138       } else {
4139         // Otherwise, the incoming value came from above BB and
4140         // so we can just reuse it.  We must associate all of BB's
4141         // predecessors with this value.
4142         for (auto *pred : predecessors(BB)) {
4143           DestPN->addIncoming(SrcVal, pred);
4144         }
4145       }
4146     }
4147 
4148     // Sink any remaining PHI nodes directly into UnwindDest.
4149     Instruction *InsertPt = DestEHPad;
4150     for (BasicBlock::iterator I = BB->begin(),
4151                               IE = BB->getFirstNonPHI()->getIterator();
4152          I != IE;) {
4153       // The iterator must be incremented here because the instructions are
4154       // being moved to another block.
4155       PHINode *PN = cast<PHINode>(I++);
4156       if (PN->use_empty() || !PN->isUsedOutsideOfBlock(BB))
4157         // If the PHI node has no uses or all of its uses are in this basic
4158         // block (meaning they are debug or lifetime intrinsics), just leave
4159         // it.  It will be erased when we erase BB below.
4160         continue;
4161 
4162       // Otherwise, sink this PHI node into UnwindDest.
4163       // Any predecessors to UnwindDest which are not already represented
4164       // must be back edges which inherit the value from the path through
4165       // BB.  In this case, the PHI value must reference itself.
4166       for (auto *pred : predecessors(UnwindDest))
4167         if (pred != BB)
4168           PN->addIncoming(PN, pred);
4169       PN->moveBefore(InsertPt);
4170     }
4171   }
4172 
4173   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
4174     // The iterator must be updated here because we are removing this pred.
4175     BasicBlock *PredBB = *PI++;
4176     if (UnwindDest == nullptr) {
4177       removeUnwindEdge(PredBB);
4178     } else {
4179       Instruction *TI = PredBB->getTerminator();
4180       TI->replaceUsesOfWith(BB, UnwindDest);
4181     }
4182   }
4183 
4184   // The cleanup pad is now unreachable.  Zap it.
4185   BB->eraseFromParent();
4186   return true;
4187 }
4188 
4189 // Try to merge two cleanuppads together.
4190 static bool mergeCleanupPad(CleanupReturnInst *RI) {
4191   // Skip any cleanuprets which unwind to caller, there is nothing to merge
4192   // with.
4193   BasicBlock *UnwindDest = RI->getUnwindDest();
4194   if (!UnwindDest)
4195     return false;
4196 
4197   // This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
4198   // be safe to merge without code duplication.
4199   if (UnwindDest->getSinglePredecessor() != RI->getParent())
4200     return false;
4201 
4202   // Verify that our cleanuppad's unwind destination is another cleanuppad.
4203   auto *SuccessorCleanupPad = dyn_cast<CleanupPadInst>(&UnwindDest->front());
4204   if (!SuccessorCleanupPad)
4205     return false;
4206 
4207   CleanupPadInst *PredecessorCleanupPad = RI->getCleanupPad();
4208   // Replace any uses of the successor cleanupad with the predecessor pad
4209   // The only cleanuppad uses should be this cleanupret, it's cleanupret and
4210   // funclet bundle operands.
4211   SuccessorCleanupPad->replaceAllUsesWith(PredecessorCleanupPad);
4212   // Remove the old cleanuppad.
4213   SuccessorCleanupPad->eraseFromParent();
4214   // Now, we simply replace the cleanupret with a branch to the unwind
4215   // destination.
4216   BranchInst::Create(UnwindDest, RI->getParent());
4217   RI->eraseFromParent();
4218 
4219   return true;
4220 }
4221 
4222 bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
4223   // It is possible to transiantly have an undef cleanuppad operand because we
4224   // have deleted some, but not all, dead blocks.
4225   // Eventually, this block will be deleted.
4226   if (isa<UndefValue>(RI->getOperand(0)))
4227     return false;
4228 
4229   if (mergeCleanupPad(RI))
4230     return true;
4231 
4232   if (removeEmptyCleanup(RI))
4233     return true;
4234 
4235   return false;
4236 }
4237 
4238 bool SimplifyCFGOpt::simplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
4239   BasicBlock *BB = RI->getParent();
4240   if (!BB->getFirstNonPHIOrDbg()->isTerminator())
4241     return false;
4242 
4243   // Find predecessors that end with branches.
4244   SmallVector<BasicBlock *, 8> UncondBranchPreds;
4245   SmallVector<BranchInst *, 8> CondBranchPreds;
4246   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
4247     BasicBlock *P = *PI;
4248     Instruction *PTI = P->getTerminator();
4249     if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
4250       if (BI->isUnconditional())
4251         UncondBranchPreds.push_back(P);
4252       else
4253         CondBranchPreds.push_back(BI);
4254     }
4255   }
4256 
4257   // If we found some, do the transformation!
4258   if (!UncondBranchPreds.empty() && DupRet) {
4259     while (!UncondBranchPreds.empty()) {
4260       BasicBlock *Pred = UncondBranchPreds.pop_back_val();
4261       LLVM_DEBUG(dbgs() << "FOLDING: " << *BB
4262                         << "INTO UNCOND BRANCH PRED: " << *Pred);
4263       (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
4264     }
4265 
4266     // If we eliminated all predecessors of the block, delete the block now.
4267     if (pred_empty(BB)) {
4268       // We know there are no successors, so just nuke the block.
4269       if (LoopHeaders)
4270         LoopHeaders->erase(BB);
4271       BB->eraseFromParent();
4272     }
4273 
4274     return true;
4275   }
4276 
4277   // Check out all of the conditional branches going to this return
4278   // instruction.  If any of them just select between returns, change the
4279   // branch itself into a select/return pair.
4280   while (!CondBranchPreds.empty()) {
4281     BranchInst *BI = CondBranchPreds.pop_back_val();
4282 
4283     // Check to see if the non-BB successor is also a return block.
4284     if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
4285         isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
4286         SimplifyCondBranchToTwoReturns(BI, Builder))
4287       return true;
4288   }
4289   return false;
4290 }
4291 
4292 bool SimplifyCFGOpt::simplifyUnreachable(UnreachableInst *UI) {
4293   BasicBlock *BB = UI->getParent();
4294 
4295   bool Changed = false;
4296 
4297   // If there are any instructions immediately before the unreachable that can
4298   // be removed, do so.
4299   while (UI->getIterator() != BB->begin()) {
4300     BasicBlock::iterator BBI = UI->getIterator();
4301     --BBI;
4302     // Do not delete instructions that can have side effects which might cause
4303     // the unreachable to not be reachable; specifically, calls and volatile
4304     // operations may have this effect.
4305     if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
4306       break;
4307 
4308     if (BBI->mayHaveSideEffects()) {
4309       if (auto *SI = dyn_cast<StoreInst>(BBI)) {
4310         if (SI->isVolatile())
4311           break;
4312       } else if (auto *LI = dyn_cast<LoadInst>(BBI)) {
4313         if (LI->isVolatile())
4314           break;
4315       } else if (auto *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
4316         if (RMWI->isVolatile())
4317           break;
4318       } else if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
4319         if (CXI->isVolatile())
4320           break;
4321       } else if (isa<CatchPadInst>(BBI)) {
4322         // A catchpad may invoke exception object constructors and such, which
4323         // in some languages can be arbitrary code, so be conservative by
4324         // default.
4325         // For CoreCLR, it just involves a type test, so can be removed.
4326         if (classifyEHPersonality(BB->getParent()->getPersonalityFn()) !=
4327             EHPersonality::CoreCLR)
4328           break;
4329       } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
4330                  !isa<LandingPadInst>(BBI)) {
4331         break;
4332       }
4333       // Note that deleting LandingPad's here is in fact okay, although it
4334       // involves a bit of subtle reasoning. If this inst is a LandingPad,
4335       // all the predecessors of this block will be the unwind edges of Invokes,
4336       // and we can therefore guarantee this block will be erased.
4337     }
4338 
4339     // Delete this instruction (any uses are guaranteed to be dead)
4340     if (!BBI->use_empty())
4341       BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
4342     BBI->eraseFromParent();
4343     Changed = true;
4344   }
4345 
4346   // If the unreachable instruction is the first in the block, take a gander
4347   // at all of the predecessors of this instruction, and simplify them.
4348   if (&BB->front() != UI)
4349     return Changed;
4350 
4351   SmallVector<BasicBlock *, 8> Preds(pred_begin(BB), pred_end(BB));
4352   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
4353     Instruction *TI = Preds[i]->getTerminator();
4354     IRBuilder<> Builder(TI);
4355     if (auto *BI = dyn_cast<BranchInst>(TI)) {
4356       if (BI->isUnconditional()) {
4357         assert(BI->getSuccessor(0) == BB && "Incorrect CFG");
4358         new UnreachableInst(TI->getContext(), TI);
4359         TI->eraseFromParent();
4360         Changed = true;
4361       } else {
4362         Value* Cond = BI->getCondition();
4363         if (BI->getSuccessor(0) == BB) {
4364           Builder.CreateAssumption(Builder.CreateNot(Cond));
4365           Builder.CreateBr(BI->getSuccessor(1));
4366         } else {
4367           assert(BI->getSuccessor(1) == BB && "Incorrect CFG");
4368           Builder.CreateAssumption(Cond);
4369           Builder.CreateBr(BI->getSuccessor(0));
4370         }
4371         EraseTerminatorAndDCECond(BI);
4372         Changed = true;
4373       }
4374     } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
4375       SwitchInstProfUpdateWrapper SU(*SI);
4376       for (auto i = SU->case_begin(), e = SU->case_end(); i != e;) {
4377         if (i->getCaseSuccessor() != BB) {
4378           ++i;
4379           continue;
4380         }
4381         BB->removePredecessor(SU->getParent());
4382         i = SU.removeCase(i);
4383         e = SU->case_end();
4384         Changed = true;
4385       }
4386     } else if (auto *II = dyn_cast<InvokeInst>(TI)) {
4387       if (II->getUnwindDest() == BB) {
4388         removeUnwindEdge(TI->getParent());
4389         Changed = true;
4390       }
4391     } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4392       if (CSI->getUnwindDest() == BB) {
4393         removeUnwindEdge(TI->getParent());
4394         Changed = true;
4395         continue;
4396       }
4397 
4398       for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
4399                                              E = CSI->handler_end();
4400            I != E; ++I) {
4401         if (*I == BB) {
4402           CSI->removeHandler(I);
4403           --I;
4404           --E;
4405           Changed = true;
4406         }
4407       }
4408       if (CSI->getNumHandlers() == 0) {
4409         BasicBlock *CatchSwitchBB = CSI->getParent();
4410         if (CSI->hasUnwindDest()) {
4411           // Redirect preds to the unwind dest
4412           CatchSwitchBB->replaceAllUsesWith(CSI->getUnwindDest());
4413         } else {
4414           // Rewrite all preds to unwind to caller (or from invoke to call).
4415           SmallVector<BasicBlock *, 8> EHPreds(predecessors(CatchSwitchBB));
4416           for (BasicBlock *EHPred : EHPreds)
4417             removeUnwindEdge(EHPred);
4418         }
4419         // The catchswitch is no longer reachable.
4420         new UnreachableInst(CSI->getContext(), CSI);
4421         CSI->eraseFromParent();
4422         Changed = true;
4423       }
4424     } else if (isa<CleanupReturnInst>(TI)) {
4425       new UnreachableInst(TI->getContext(), TI);
4426       TI->eraseFromParent();
4427       Changed = true;
4428     }
4429   }
4430 
4431   // If this block is now dead, remove it.
4432   if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
4433     // We know there are no successors, so just nuke the block.
4434     if (LoopHeaders)
4435       LoopHeaders->erase(BB);
4436     BB->eraseFromParent();
4437     return true;
4438   }
4439 
4440   return Changed;
4441 }
4442 
4443 static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
4444   assert(Cases.size() >= 1);
4445 
4446   array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
4447   for (size_t I = 1, E = Cases.size(); I != E; ++I) {
4448     if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
4449       return false;
4450   }
4451   return true;
4452 }
4453 
4454 static void createUnreachableSwitchDefault(SwitchInst *Switch) {
4455   LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
4456   BasicBlock *NewDefaultBlock =
4457      SplitBlockPredecessors(Switch->getDefaultDest(), Switch->getParent(), "");
4458   Switch->setDefaultDest(&*NewDefaultBlock);
4459   SplitBlock(&*NewDefaultBlock, &NewDefaultBlock->front());
4460   auto *NewTerminator = NewDefaultBlock->getTerminator();
4461   new UnreachableInst(Switch->getContext(), NewTerminator);
4462   EraseTerminatorAndDCECond(NewTerminator);
4463 }
4464 
4465 /// Turn a switch with two reachable destinations into an integer range
4466 /// comparison and branch.
4467 bool SimplifyCFGOpt::TurnSwitchRangeIntoICmp(SwitchInst *SI,
4468                                              IRBuilder<> &Builder) {
4469   assert(SI->getNumCases() > 1 && "Degenerate switch?");
4470 
4471   bool HasDefault =
4472       !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4473 
4474   // Partition the cases into two sets with different destinations.
4475   BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
4476   BasicBlock *DestB = nullptr;
4477   SmallVector<ConstantInt *, 16> CasesA;
4478   SmallVector<ConstantInt *, 16> CasesB;
4479 
4480   for (auto Case : SI->cases()) {
4481     BasicBlock *Dest = Case.getCaseSuccessor();
4482     if (!DestA)
4483       DestA = Dest;
4484     if (Dest == DestA) {
4485       CasesA.push_back(Case.getCaseValue());
4486       continue;
4487     }
4488     if (!DestB)
4489       DestB = Dest;
4490     if (Dest == DestB) {
4491       CasesB.push_back(Case.getCaseValue());
4492       continue;
4493     }
4494     return false; // More than two destinations.
4495   }
4496 
4497   assert(DestA && DestB &&
4498          "Single-destination switch should have been folded.");
4499   assert(DestA != DestB);
4500   assert(DestB != SI->getDefaultDest());
4501   assert(!CasesB.empty() && "There must be non-default cases.");
4502   assert(!CasesA.empty() || HasDefault);
4503 
4504   // Figure out if one of the sets of cases form a contiguous range.
4505   SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr;
4506   BasicBlock *ContiguousDest = nullptr;
4507   BasicBlock *OtherDest = nullptr;
4508   if (!CasesA.empty() && CasesAreContiguous(CasesA)) {
4509     ContiguousCases = &CasesA;
4510     ContiguousDest = DestA;
4511     OtherDest = DestB;
4512   } else if (CasesAreContiguous(CasesB)) {
4513     ContiguousCases = &CasesB;
4514     ContiguousDest = DestB;
4515     OtherDest = DestA;
4516   } else
4517     return false;
4518 
4519   // Start building the compare and branch.
4520 
4521   Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back());
4522   Constant *NumCases =
4523       ConstantInt::get(Offset->getType(), ContiguousCases->size());
4524 
4525   Value *Sub = SI->getCondition();
4526   if (!Offset->isNullValue())
4527     Sub = Builder.CreateAdd(Sub, Offset, Sub->getName() + ".off");
4528 
4529   Value *Cmp;
4530   // If NumCases overflowed, then all possible values jump to the successor.
4531   if (NumCases->isNullValue() && !ContiguousCases->empty())
4532     Cmp = ConstantInt::getTrue(SI->getContext());
4533   else
4534     Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
4535   BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest);
4536 
4537   // Update weight for the newly-created conditional branch.
4538   if (HasBranchWeights(SI)) {
4539     SmallVector<uint64_t, 8> Weights;
4540     GetBranchWeights(SI, Weights);
4541     if (Weights.size() == 1 + SI->getNumCases()) {
4542       uint64_t TrueWeight = 0;
4543       uint64_t FalseWeight = 0;
4544       for (size_t I = 0, E = Weights.size(); I != E; ++I) {
4545         if (SI->getSuccessor(I) == ContiguousDest)
4546           TrueWeight += Weights[I];
4547         else
4548           FalseWeight += Weights[I];
4549       }
4550       while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
4551         TrueWeight /= 2;
4552         FalseWeight /= 2;
4553       }
4554       setBranchWeights(NewBI, TrueWeight, FalseWeight);
4555     }
4556   }
4557 
4558   // Prune obsolete incoming values off the successors' PHI nodes.
4559   for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
4560     unsigned PreviousEdges = ContiguousCases->size();
4561     if (ContiguousDest == SI->getDefaultDest())
4562       ++PreviousEdges;
4563     for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
4564       cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
4565   }
4566   for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
4567     unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size();
4568     if (OtherDest == SI->getDefaultDest())
4569       ++PreviousEdges;
4570     for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
4571       cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
4572   }
4573 
4574   // Clean up the default block - it may have phis or other instructions before
4575   // the unreachable terminator.
4576   if (!HasDefault)
4577     createUnreachableSwitchDefault(SI);
4578 
4579   // Drop the switch.
4580   SI->eraseFromParent();
4581 
4582   return true;
4583 }
4584 
4585 /// Compute masked bits for the condition of a switch
4586 /// and use it to remove dead cases.
4587 static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
4588                                      const DataLayout &DL) {
4589   Value *Cond = SI->getCondition();
4590   unsigned Bits = Cond->getType()->getIntegerBitWidth();
4591   KnownBits Known = computeKnownBits(Cond, DL, 0, AC, SI);
4592 
4593   // We can also eliminate cases by determining that their values are outside of
4594   // the limited range of the condition based on how many significant (non-sign)
4595   // bits are in the condition value.
4596   unsigned ExtraSignBits = ComputeNumSignBits(Cond, DL, 0, AC, SI) - 1;
4597   unsigned MaxSignificantBitsInCond = Bits - ExtraSignBits;
4598 
4599   // Gather dead cases.
4600   SmallVector<ConstantInt *, 8> DeadCases;
4601   for (auto &Case : SI->cases()) {
4602     const APInt &CaseVal = Case.getCaseValue()->getValue();
4603     if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
4604         (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) {
4605       DeadCases.push_back(Case.getCaseValue());
4606       LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
4607                         << " is dead.\n");
4608     }
4609   }
4610 
4611   // If we can prove that the cases must cover all possible values, the
4612   // default destination becomes dead and we can remove it.  If we know some
4613   // of the bits in the value, we can use that to more precisely compute the
4614   // number of possible unique case values.
4615   bool HasDefault =
4616       !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4617   const unsigned NumUnknownBits =
4618       Bits - (Known.Zero | Known.One).countPopulation();
4619   assert(NumUnknownBits <= Bits);
4620   if (HasDefault && DeadCases.empty() &&
4621       NumUnknownBits < 64 /* avoid overflow */ &&
4622       SI->getNumCases() == (1ULL << NumUnknownBits)) {
4623     createUnreachableSwitchDefault(SI);
4624     return true;
4625   }
4626 
4627   if (DeadCases.empty())
4628     return false;
4629 
4630   SwitchInstProfUpdateWrapper SIW(*SI);
4631   for (ConstantInt *DeadCase : DeadCases) {
4632     SwitchInst::CaseIt CaseI = SI->findCaseValue(DeadCase);
4633     assert(CaseI != SI->case_default() &&
4634            "Case was not found. Probably mistake in DeadCases forming.");
4635     // Prune unused values from PHI nodes.
4636     CaseI->getCaseSuccessor()->removePredecessor(SI->getParent());
4637     SIW.removeCase(CaseI);
4638   }
4639 
4640   return true;
4641 }
4642 
4643 /// If BB would be eligible for simplification by
4644 /// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
4645 /// by an unconditional branch), look at the phi node for BB in the successor
4646 /// block and see if the incoming value is equal to CaseValue. If so, return
4647 /// the phi node, and set PhiIndex to BB's index in the phi node.
4648 static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
4649                                               BasicBlock *BB, int *PhiIndex) {
4650   if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
4651     return nullptr; // BB must be empty to be a candidate for simplification.
4652   if (!BB->getSinglePredecessor())
4653     return nullptr; // BB must be dominated by the switch.
4654 
4655   BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
4656   if (!Branch || !Branch->isUnconditional())
4657     return nullptr; // Terminator must be unconditional branch.
4658 
4659   BasicBlock *Succ = Branch->getSuccessor(0);
4660 
4661   for (PHINode &PHI : Succ->phis()) {
4662     int Idx = PHI.getBasicBlockIndex(BB);
4663     assert(Idx >= 0 && "PHI has no entry for predecessor?");
4664 
4665     Value *InValue = PHI.getIncomingValue(Idx);
4666     if (InValue != CaseValue)
4667       continue;
4668 
4669     *PhiIndex = Idx;
4670     return &PHI;
4671   }
4672 
4673   return nullptr;
4674 }
4675 
4676 /// Try to forward the condition of a switch instruction to a phi node
4677 /// dominated by the switch, if that would mean that some of the destination
4678 /// blocks of the switch can be folded away. Return true if a change is made.
4679 static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
4680   using ForwardingNodesMap = DenseMap<PHINode *, SmallVector<int, 4>>;
4681 
4682   ForwardingNodesMap ForwardingNodes;
4683   BasicBlock *SwitchBlock = SI->getParent();
4684   bool Changed = false;
4685   for (auto &Case : SI->cases()) {
4686     ConstantInt *CaseValue = Case.getCaseValue();
4687     BasicBlock *CaseDest = Case.getCaseSuccessor();
4688 
4689     // Replace phi operands in successor blocks that are using the constant case
4690     // value rather than the switch condition variable:
4691     //   switchbb:
4692     //   switch i32 %x, label %default [
4693     //     i32 17, label %succ
4694     //   ...
4695     //   succ:
4696     //     %r = phi i32 ... [ 17, %switchbb ] ...
4697     // -->
4698     //     %r = phi i32 ... [ %x, %switchbb ] ...
4699 
4700     for (PHINode &Phi : CaseDest->phis()) {
4701       // This only works if there is exactly 1 incoming edge from the switch to
4702       // a phi. If there is >1, that means multiple cases of the switch map to 1
4703       // value in the phi, and that phi value is not the switch condition. Thus,
4704       // this transform would not make sense (the phi would be invalid because
4705       // a phi can't have different incoming values from the same block).
4706       int SwitchBBIdx = Phi.getBasicBlockIndex(SwitchBlock);
4707       if (Phi.getIncomingValue(SwitchBBIdx) == CaseValue &&
4708           count(Phi.blocks(), SwitchBlock) == 1) {
4709         Phi.setIncomingValue(SwitchBBIdx, SI->getCondition());
4710         Changed = true;
4711       }
4712     }
4713 
4714     // Collect phi nodes that are indirectly using this switch's case constants.
4715     int PhiIdx;
4716     if (auto *Phi = FindPHIForConditionForwarding(CaseValue, CaseDest, &PhiIdx))
4717       ForwardingNodes[Phi].push_back(PhiIdx);
4718   }
4719 
4720   for (auto &ForwardingNode : ForwardingNodes) {
4721     PHINode *Phi = ForwardingNode.first;
4722     SmallVectorImpl<int> &Indexes = ForwardingNode.second;
4723     if (Indexes.size() < 2)
4724       continue;
4725 
4726     for (int Index : Indexes)
4727       Phi->setIncomingValue(Index, SI->getCondition());
4728     Changed = true;
4729   }
4730 
4731   return Changed;
4732 }
4733 
4734 /// Return true if the backend will be able to handle
4735 /// initializing an array of constants like C.
4736 static bool ValidLookupTableConstant(Constant *C, const TargetTransformInfo &TTI) {
4737   if (C->isThreadDependent())
4738     return false;
4739   if (C->isDLLImportDependent())
4740     return false;
4741 
4742   if (!isa<ConstantFP>(C) && !isa<ConstantInt>(C) &&
4743       !isa<ConstantPointerNull>(C) && !isa<GlobalValue>(C) &&
4744       !isa<UndefValue>(C) && !isa<ConstantExpr>(C))
4745     return false;
4746 
4747   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
4748     if (!CE->isGEPWithNoNotionalOverIndexing())
4749       return false;
4750     if (!ValidLookupTableConstant(CE->getOperand(0), TTI))
4751       return false;
4752   }
4753 
4754   if (!TTI.shouldBuildLookupTablesForConstant(C))
4755     return false;
4756 
4757   return true;
4758 }
4759 
4760 /// If V is a Constant, return it. Otherwise, try to look up
4761 /// its constant value in ConstantPool, returning 0 if it's not there.
4762 static Constant *
4763 LookupConstant(Value *V,
4764                const SmallDenseMap<Value *, Constant *> &ConstantPool) {
4765   if (Constant *C = dyn_cast<Constant>(V))
4766     return C;
4767   return ConstantPool.lookup(V);
4768 }
4769 
4770 /// Try to fold instruction I into a constant. This works for
4771 /// simple instructions such as binary operations where both operands are
4772 /// constant or can be replaced by constants from the ConstantPool. Returns the
4773 /// resulting constant on success, 0 otherwise.
4774 static Constant *
4775 ConstantFold(Instruction *I, const DataLayout &DL,
4776              const SmallDenseMap<Value *, Constant *> &ConstantPool) {
4777   if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
4778     Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
4779     if (!A)
4780       return nullptr;
4781     if (A->isAllOnesValue())
4782       return LookupConstant(Select->getTrueValue(), ConstantPool);
4783     if (A->isNullValue())
4784       return LookupConstant(Select->getFalseValue(), ConstantPool);
4785     return nullptr;
4786   }
4787 
4788   SmallVector<Constant *, 4> COps;
4789   for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
4790     if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool))
4791       COps.push_back(A);
4792     else
4793       return nullptr;
4794   }
4795 
4796   if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
4797     return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
4798                                            COps[1], DL);
4799   }
4800 
4801   return ConstantFoldInstOperands(I, COps, DL);
4802 }
4803 
4804 /// Try to determine the resulting constant values in phi nodes
4805 /// at the common destination basic block, *CommonDest, for one of the case
4806 /// destionations CaseDest corresponding to value CaseVal (0 for the default
4807 /// case), of a switch instruction SI.
4808 static bool
4809 GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
4810                BasicBlock **CommonDest,
4811                SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
4812                const DataLayout &DL, const TargetTransformInfo &TTI) {
4813   // The block from which we enter the common destination.
4814   BasicBlock *Pred = SI->getParent();
4815 
4816   // If CaseDest is empty except for some side-effect free instructions through
4817   // which we can constant-propagate the CaseVal, continue to its successor.
4818   SmallDenseMap<Value *, Constant *> ConstantPool;
4819   ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
4820   for (Instruction &I :CaseDest->instructionsWithoutDebug()) {
4821     if (I.isTerminator()) {
4822       // If the terminator is a simple branch, continue to the next block.
4823       if (I.getNumSuccessors() != 1 || I.isExceptionalTerminator())
4824         return false;
4825       Pred = CaseDest;
4826       CaseDest = I.getSuccessor(0);
4827     } else if (Constant *C = ConstantFold(&I, DL, ConstantPool)) {
4828       // Instruction is side-effect free and constant.
4829 
4830       // If the instruction has uses outside this block or a phi node slot for
4831       // the block, it is not safe to bypass the instruction since it would then
4832       // no longer dominate all its uses.
4833       for (auto &Use : I.uses()) {
4834         User *User = Use.getUser();
4835         if (Instruction *I = dyn_cast<Instruction>(User))
4836           if (I->getParent() == CaseDest)
4837             continue;
4838         if (PHINode *Phi = dyn_cast<PHINode>(User))
4839           if (Phi->getIncomingBlock(Use) == CaseDest)
4840             continue;
4841         return false;
4842       }
4843 
4844       ConstantPool.insert(std::make_pair(&I, C));
4845     } else {
4846       break;
4847     }
4848   }
4849 
4850   // If we did not have a CommonDest before, use the current one.
4851   if (!*CommonDest)
4852     *CommonDest = CaseDest;
4853   // If the destination isn't the common one, abort.
4854   if (CaseDest != *CommonDest)
4855     return false;
4856 
4857   // Get the values for this case from phi nodes in the destination block.
4858   for (PHINode &PHI : (*CommonDest)->phis()) {
4859     int Idx = PHI.getBasicBlockIndex(Pred);
4860     if (Idx == -1)
4861       continue;
4862 
4863     Constant *ConstVal =
4864         LookupConstant(PHI.getIncomingValue(Idx), ConstantPool);
4865     if (!ConstVal)
4866       return false;
4867 
4868     // Be conservative about which kinds of constants we support.
4869     if (!ValidLookupTableConstant(ConstVal, TTI))
4870       return false;
4871 
4872     Res.push_back(std::make_pair(&PHI, ConstVal));
4873   }
4874 
4875   return Res.size() > 0;
4876 }
4877 
4878 // Helper function used to add CaseVal to the list of cases that generate
4879 // Result. Returns the updated number of cases that generate this result.
4880 static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
4881                                  SwitchCaseResultVectorTy &UniqueResults,
4882                                  Constant *Result) {
4883   for (auto &I : UniqueResults) {
4884     if (I.first == Result) {
4885       I.second.push_back(CaseVal);
4886       return I.second.size();
4887     }
4888   }
4889   UniqueResults.push_back(
4890       std::make_pair(Result, SmallVector<ConstantInt *, 4>(1, CaseVal)));
4891   return 1;
4892 }
4893 
4894 // Helper function that initializes a map containing
4895 // results for the PHI node of the common destination block for a switch
4896 // instruction. Returns false if multiple PHI nodes have been found or if
4897 // there is not a common destination block for the switch.
4898 static bool
4899 InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
4900                       SwitchCaseResultVectorTy &UniqueResults,
4901                       Constant *&DefaultResult, const DataLayout &DL,
4902                       const TargetTransformInfo &TTI,
4903                       uintptr_t MaxUniqueResults, uintptr_t MaxCasesPerResult) {
4904   for (auto &I : SI->cases()) {
4905     ConstantInt *CaseVal = I.getCaseValue();
4906 
4907     // Resulting value at phi nodes for this case value.
4908     SwitchCaseResultsTy Results;
4909     if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
4910                         DL, TTI))
4911       return false;
4912 
4913     // Only one value per case is permitted.
4914     if (Results.size() > 1)
4915       return false;
4916 
4917     // Add the case->result mapping to UniqueResults.
4918     const uintptr_t NumCasesForResult =
4919         MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
4920 
4921     // Early out if there are too many cases for this result.
4922     if (NumCasesForResult > MaxCasesPerResult)
4923       return false;
4924 
4925     // Early out if there are too many unique results.
4926     if (UniqueResults.size() > MaxUniqueResults)
4927       return false;
4928 
4929     // Check the PHI consistency.
4930     if (!PHI)
4931       PHI = Results[0].first;
4932     else if (PHI != Results[0].first)
4933       return false;
4934   }
4935   // Find the default result value.
4936   SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
4937   BasicBlock *DefaultDest = SI->getDefaultDest();
4938   GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
4939                  DL, TTI);
4940   // If the default value is not found abort unless the default destination
4941   // is unreachable.
4942   DefaultResult =
4943       DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
4944   if ((!DefaultResult &&
4945        !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
4946     return false;
4947 
4948   return true;
4949 }
4950 
4951 // Helper function that checks if it is possible to transform a switch with only
4952 // two cases (or two cases + default) that produces a result into a select.
4953 // Example:
4954 // switch (a) {
4955 //   case 10:                %0 = icmp eq i32 %a, 10
4956 //     return 10;            %1 = select i1 %0, i32 10, i32 4
4957 //   case 20:        ---->   %2 = icmp eq i32 %a, 20
4958 //     return 2;             %3 = select i1 %2, i32 2, i32 %1
4959 //   default:
4960 //     return 4;
4961 // }
4962 static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
4963                                    Constant *DefaultResult, Value *Condition,
4964                                    IRBuilder<> &Builder) {
4965   assert(ResultVector.size() == 2 &&
4966          "We should have exactly two unique results at this point");
4967   // If we are selecting between only two cases transform into a simple
4968   // select or a two-way select if default is possible.
4969   if (ResultVector[0].second.size() == 1 &&
4970       ResultVector[1].second.size() == 1) {
4971     ConstantInt *const FirstCase = ResultVector[0].second[0];
4972     ConstantInt *const SecondCase = ResultVector[1].second[0];
4973 
4974     bool DefaultCanTrigger = DefaultResult;
4975     Value *SelectValue = ResultVector[1].first;
4976     if (DefaultCanTrigger) {
4977       Value *const ValueCompare =
4978           Builder.CreateICmpEQ(Condition, SecondCase, "switch.selectcmp");
4979       SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first,
4980                                          DefaultResult, "switch.select");
4981     }
4982     Value *const ValueCompare =
4983         Builder.CreateICmpEQ(Condition, FirstCase, "switch.selectcmp");
4984     return Builder.CreateSelect(ValueCompare, ResultVector[0].first,
4985                                 SelectValue, "switch.select");
4986   }
4987 
4988   return nullptr;
4989 }
4990 
4991 // Helper function to cleanup a switch instruction that has been converted into
4992 // a select, fixing up PHI nodes and basic blocks.
4993 static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
4994                                               Value *SelectValue,
4995                                               IRBuilder<> &Builder) {
4996   BasicBlock *SelectBB = SI->getParent();
4997   while (PHI->getBasicBlockIndex(SelectBB) >= 0)
4998     PHI->removeIncomingValue(SelectBB);
4999   PHI->addIncoming(SelectValue, SelectBB);
5000 
5001   Builder.CreateBr(PHI->getParent());
5002 
5003   // Remove the switch.
5004   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
5005     BasicBlock *Succ = SI->getSuccessor(i);
5006 
5007     if (Succ == PHI->getParent())
5008       continue;
5009     Succ->removePredecessor(SelectBB);
5010   }
5011   SI->eraseFromParent();
5012 }
5013 
5014 /// If the switch is only used to initialize one or more
5015 /// phi nodes in a common successor block with only two different
5016 /// constant values, replace the switch with select.
5017 static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
5018                            const DataLayout &DL,
5019                            const TargetTransformInfo &TTI) {
5020   Value *const Cond = SI->getCondition();
5021   PHINode *PHI = nullptr;
5022   BasicBlock *CommonDest = nullptr;
5023   Constant *DefaultResult;
5024   SwitchCaseResultVectorTy UniqueResults;
5025   // Collect all the cases that will deliver the same value from the switch.
5026   if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
5027                              DL, TTI, 2, 1))
5028     return false;
5029   // Selects choose between maximum two values.
5030   if (UniqueResults.size() != 2)
5031     return false;
5032   assert(PHI != nullptr && "PHI for value select not found");
5033 
5034   Builder.SetInsertPoint(SI);
5035   Value *SelectValue =
5036       ConvertTwoCaseSwitch(UniqueResults, DefaultResult, Cond, Builder);
5037   if (SelectValue) {
5038     RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder);
5039     return true;
5040   }
5041   // The switch couldn't be converted into a select.
5042   return false;
5043 }
5044 
5045 namespace {
5046 
5047 /// This class represents a lookup table that can be used to replace a switch.
5048 class SwitchLookupTable {
5049 public:
5050   /// Create a lookup table to use as a switch replacement with the contents
5051   /// of Values, using DefaultValue to fill any holes in the table.
5052   SwitchLookupTable(
5053       Module &M, uint64_t TableSize, ConstantInt *Offset,
5054       const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
5055       Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName);
5056 
5057   /// Build instructions with Builder to retrieve the value at
5058   /// the position given by Index in the lookup table.
5059   Value *BuildLookup(Value *Index, IRBuilder<> &Builder);
5060 
5061   /// Return true if a table with TableSize elements of
5062   /// type ElementType would fit in a target-legal register.
5063   static bool WouldFitInRegister(const DataLayout &DL, uint64_t TableSize,
5064                                  Type *ElementType);
5065 
5066 private:
5067   // Depending on the contents of the table, it can be represented in
5068   // different ways.
5069   enum {
5070     // For tables where each element contains the same value, we just have to
5071     // store that single value and return it for each lookup.
5072     SingleValueKind,
5073 
5074     // For tables where there is a linear relationship between table index
5075     // and values. We calculate the result with a simple multiplication
5076     // and addition instead of a table lookup.
5077     LinearMapKind,
5078 
5079     // For small tables with integer elements, we can pack them into a bitmap
5080     // that fits into a target-legal register. Values are retrieved by
5081     // shift and mask operations.
5082     BitMapKind,
5083 
5084     // The table is stored as an array of values. Values are retrieved by load
5085     // instructions from the table.
5086     ArrayKind
5087   } Kind;
5088 
5089   // For SingleValueKind, this is the single value.
5090   Constant *SingleValue = nullptr;
5091 
5092   // For BitMapKind, this is the bitmap.
5093   ConstantInt *BitMap = nullptr;
5094   IntegerType *BitMapElementTy = nullptr;
5095 
5096   // For LinearMapKind, these are the constants used to derive the value.
5097   ConstantInt *LinearOffset = nullptr;
5098   ConstantInt *LinearMultiplier = nullptr;
5099 
5100   // For ArrayKind, this is the array.
5101   GlobalVariable *Array = nullptr;
5102 };
5103 
5104 } // end anonymous namespace
5105 
5106 SwitchLookupTable::SwitchLookupTable(
5107     Module &M, uint64_t TableSize, ConstantInt *Offset,
5108     const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
5109     Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName) {
5110   assert(Values.size() && "Can't build lookup table without values!");
5111   assert(TableSize >= Values.size() && "Can't fit values in table!");
5112 
5113   // If all values in the table are equal, this is that value.
5114   SingleValue = Values.begin()->second;
5115 
5116   Type *ValueType = Values.begin()->second->getType();
5117 
5118   // Build up the table contents.
5119   SmallVector<Constant *, 64> TableContents(TableSize);
5120   for (size_t I = 0, E = Values.size(); I != E; ++I) {
5121     ConstantInt *CaseVal = Values[I].first;
5122     Constant *CaseRes = Values[I].second;
5123     assert(CaseRes->getType() == ValueType);
5124 
5125     uint64_t Idx = (CaseVal->getValue() - Offset->getValue()).getLimitedValue();
5126     TableContents[Idx] = CaseRes;
5127 
5128     if (CaseRes != SingleValue)
5129       SingleValue = nullptr;
5130   }
5131 
5132   // Fill in any holes in the table with the default result.
5133   if (Values.size() < TableSize) {
5134     assert(DefaultValue &&
5135            "Need a default value to fill the lookup table holes.");
5136     assert(DefaultValue->getType() == ValueType);
5137     for (uint64_t I = 0; I < TableSize; ++I) {
5138       if (!TableContents[I])
5139         TableContents[I] = DefaultValue;
5140     }
5141 
5142     if (DefaultValue != SingleValue)
5143       SingleValue = nullptr;
5144   }
5145 
5146   // If each element in the table contains the same value, we only need to store
5147   // that single value.
5148   if (SingleValue) {
5149     Kind = SingleValueKind;
5150     return;
5151   }
5152 
5153   // Check if we can derive the value with a linear transformation from the
5154   // table index.
5155   if (isa<IntegerType>(ValueType)) {
5156     bool LinearMappingPossible = true;
5157     APInt PrevVal;
5158     APInt DistToPrev;
5159     assert(TableSize >= 2 && "Should be a SingleValue table.");
5160     // Check if there is the same distance between two consecutive values.
5161     for (uint64_t I = 0; I < TableSize; ++I) {
5162       ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
5163       if (!ConstVal) {
5164         // This is an undef. We could deal with it, but undefs in lookup tables
5165         // are very seldom. It's probably not worth the additional complexity.
5166         LinearMappingPossible = false;
5167         break;
5168       }
5169       const APInt &Val = ConstVal->getValue();
5170       if (I != 0) {
5171         APInt Dist = Val - PrevVal;
5172         if (I == 1) {
5173           DistToPrev = Dist;
5174         } else if (Dist != DistToPrev) {
5175           LinearMappingPossible = false;
5176           break;
5177         }
5178       }
5179       PrevVal = Val;
5180     }
5181     if (LinearMappingPossible) {
5182       LinearOffset = cast<ConstantInt>(TableContents[0]);
5183       LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
5184       Kind = LinearMapKind;
5185       ++NumLinearMaps;
5186       return;
5187     }
5188   }
5189 
5190   // If the type is integer and the table fits in a register, build a bitmap.
5191   if (WouldFitInRegister(DL, TableSize, ValueType)) {
5192     IntegerType *IT = cast<IntegerType>(ValueType);
5193     APInt TableInt(TableSize * IT->getBitWidth(), 0);
5194     for (uint64_t I = TableSize; I > 0; --I) {
5195       TableInt <<= IT->getBitWidth();
5196       // Insert values into the bitmap. Undef values are set to zero.
5197       if (!isa<UndefValue>(TableContents[I - 1])) {
5198         ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
5199         TableInt |= Val->getValue().zext(TableInt.getBitWidth());
5200       }
5201     }
5202     BitMap = ConstantInt::get(M.getContext(), TableInt);
5203     BitMapElementTy = IT;
5204     Kind = BitMapKind;
5205     ++NumBitMaps;
5206     return;
5207   }
5208 
5209   // Store the table in an array.
5210   ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
5211   Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
5212 
5213   Array = new GlobalVariable(M, ArrayTy, /*isConstant=*/true,
5214                              GlobalVariable::PrivateLinkage, Initializer,
5215                              "switch.table." + FuncName);
5216   Array->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
5217   // Set the alignment to that of an array items. We will be only loading one
5218   // value out of it.
5219   Array->setAlignment(Align(DL.getPrefTypeAlignment(ValueType)));
5220   Kind = ArrayKind;
5221 }
5222 
5223 Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
5224   switch (Kind) {
5225   case SingleValueKind:
5226     return SingleValue;
5227   case LinearMapKind: {
5228     // Derive the result value from the input value.
5229     Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
5230                                           false, "switch.idx.cast");
5231     if (!LinearMultiplier->isOne())
5232       Result = Builder.CreateMul(Result, LinearMultiplier, "switch.idx.mult");
5233     if (!LinearOffset->isZero())
5234       Result = Builder.CreateAdd(Result, LinearOffset, "switch.offset");
5235     return Result;
5236   }
5237   case BitMapKind: {
5238     // Type of the bitmap (e.g. i59).
5239     IntegerType *MapTy = BitMap->getType();
5240 
5241     // Cast Index to the same type as the bitmap.
5242     // Note: The Index is <= the number of elements in the table, so
5243     // truncating it to the width of the bitmask is safe.
5244     Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast");
5245 
5246     // Multiply the shift amount by the element width.
5247     ShiftAmt = Builder.CreateMul(
5248         ShiftAmt, ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
5249         "switch.shiftamt");
5250 
5251     // Shift down.
5252     Value *DownShifted =
5253         Builder.CreateLShr(BitMap, ShiftAmt, "switch.downshift");
5254     // Mask off.
5255     return Builder.CreateTrunc(DownShifted, BitMapElementTy, "switch.masked");
5256   }
5257   case ArrayKind: {
5258     // Make sure the table index will not overflow when treated as signed.
5259     IntegerType *IT = cast<IntegerType>(Index->getType());
5260     uint64_t TableSize =
5261         Array->getInitializer()->getType()->getArrayNumElements();
5262     if (TableSize > (1ULL << (IT->getBitWidth() - 1)))
5263       Index = Builder.CreateZExt(
5264           Index, IntegerType::get(IT->getContext(), IT->getBitWidth() + 1),
5265           "switch.tableidx.zext");
5266 
5267     Value *GEPIndices[] = {Builder.getInt32(0), Index};
5268     Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
5269                                            GEPIndices, "switch.gep");
5270     return Builder.CreateLoad(
5271         cast<ArrayType>(Array->getValueType())->getElementType(), GEP,
5272         "switch.load");
5273   }
5274   }
5275   llvm_unreachable("Unknown lookup table kind!");
5276 }
5277 
5278 bool SwitchLookupTable::WouldFitInRegister(const DataLayout &DL,
5279                                            uint64_t TableSize,
5280                                            Type *ElementType) {
5281   auto *IT = dyn_cast<IntegerType>(ElementType);
5282   if (!IT)
5283     return false;
5284   // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
5285   // are <= 15, we could try to narrow the type.
5286 
5287   // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
5288   if (TableSize >= UINT_MAX / IT->getBitWidth())
5289     return false;
5290   return DL.fitsInLegalInteger(TableSize * IT->getBitWidth());
5291 }
5292 
5293 /// Determine whether a lookup table should be built for this switch, based on
5294 /// the number of cases, size of the table, and the types of the results.
5295 static bool
5296 ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
5297                        const TargetTransformInfo &TTI, const DataLayout &DL,
5298                        const SmallDenseMap<PHINode *, Type *> &ResultTypes) {
5299   if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
5300     return false; // TableSize overflowed, or mul below might overflow.
5301 
5302   bool AllTablesFitInRegister = true;
5303   bool HasIllegalType = false;
5304   for (const auto &I : ResultTypes) {
5305     Type *Ty = I.second;
5306 
5307     // Saturate this flag to true.
5308     HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty);
5309 
5310     // Saturate this flag to false.
5311     AllTablesFitInRegister =
5312         AllTablesFitInRegister &&
5313         SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
5314 
5315     // If both flags saturate, we're done. NOTE: This *only* works with
5316     // saturating flags, and all flags have to saturate first due to the
5317     // non-deterministic behavior of iterating over a dense map.
5318     if (HasIllegalType && !AllTablesFitInRegister)
5319       break;
5320   }
5321 
5322   // If each table would fit in a register, we should build it anyway.
5323   if (AllTablesFitInRegister)
5324     return true;
5325 
5326   // Don't build a table that doesn't fit in-register if it has illegal types.
5327   if (HasIllegalType)
5328     return false;
5329 
5330   // The table density should be at least 40%. This is the same criterion as for
5331   // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
5332   // FIXME: Find the best cut-off.
5333   return SI->getNumCases() * 10 >= TableSize * 4;
5334 }
5335 
5336 /// Try to reuse the switch table index compare. Following pattern:
5337 /// \code
5338 ///     if (idx < tablesize)
5339 ///        r = table[idx]; // table does not contain default_value
5340 ///     else
5341 ///        r = default_value;
5342 ///     if (r != default_value)
5343 ///        ...
5344 /// \endcode
5345 /// Is optimized to:
5346 /// \code
5347 ///     cond = idx < tablesize;
5348 ///     if (cond)
5349 ///        r = table[idx];
5350 ///     else
5351 ///        r = default_value;
5352 ///     if (cond)
5353 ///        ...
5354 /// \endcode
5355 /// Jump threading will then eliminate the second if(cond).
5356 static void reuseTableCompare(
5357     User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch,
5358     Constant *DefaultValue,
5359     const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
5360   ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser);
5361   if (!CmpInst)
5362     return;
5363 
5364   // We require that the compare is in the same block as the phi so that jump
5365   // threading can do its work afterwards.
5366   if (CmpInst->getParent() != PhiBlock)
5367     return;
5368 
5369   Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1));
5370   if (!CmpOp1)
5371     return;
5372 
5373   Value *RangeCmp = RangeCheckBranch->getCondition();
5374   Constant *TrueConst = ConstantInt::getTrue(RangeCmp->getType());
5375   Constant *FalseConst = ConstantInt::getFalse(RangeCmp->getType());
5376 
5377   // Check if the compare with the default value is constant true or false.
5378   Constant *DefaultConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
5379                                                  DefaultValue, CmpOp1, true);
5380   if (DefaultConst != TrueConst && DefaultConst != FalseConst)
5381     return;
5382 
5383   // Check if the compare with the case values is distinct from the default
5384   // compare result.
5385   for (auto ValuePair : Values) {
5386     Constant *CaseConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
5387                                                 ValuePair.second, CmpOp1, true);
5388     if (!CaseConst || CaseConst == DefaultConst || isa<UndefValue>(CaseConst))
5389       return;
5390     assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
5391            "Expect true or false as compare result.");
5392   }
5393 
5394   // Check if the branch instruction dominates the phi node. It's a simple
5395   // dominance check, but sufficient for our needs.
5396   // Although this check is invariant in the calling loops, it's better to do it
5397   // at this late stage. Practically we do it at most once for a switch.
5398   BasicBlock *BranchBlock = RangeCheckBranch->getParent();
5399   for (auto PI = pred_begin(PhiBlock), E = pred_end(PhiBlock); PI != E; ++PI) {
5400     BasicBlock *Pred = *PI;
5401     if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
5402       return;
5403   }
5404 
5405   if (DefaultConst == FalseConst) {
5406     // The compare yields the same result. We can replace it.
5407     CmpInst->replaceAllUsesWith(RangeCmp);
5408     ++NumTableCmpReuses;
5409   } else {
5410     // The compare yields the same result, just inverted. We can replace it.
5411     Value *InvertedTableCmp = BinaryOperator::CreateXor(
5412         RangeCmp, ConstantInt::get(RangeCmp->getType(), 1), "inverted.cmp",
5413         RangeCheckBranch);
5414     CmpInst->replaceAllUsesWith(InvertedTableCmp);
5415     ++NumTableCmpReuses;
5416   }
5417 }
5418 
5419 /// If the switch is only used to initialize one or more phi nodes in a common
5420 /// successor block with different constant values, replace the switch with
5421 /// lookup tables.
5422 static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
5423                                 const DataLayout &DL,
5424                                 const TargetTransformInfo &TTI) {
5425   assert(SI->getNumCases() > 1 && "Degenerate switch?");
5426 
5427   Function *Fn = SI->getParent()->getParent();
5428   // Only build lookup table when we have a target that supports it or the
5429   // attribute is not set.
5430   if (!TTI.shouldBuildLookupTables() ||
5431       (Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true"))
5432     return false;
5433 
5434   // FIXME: If the switch is too sparse for a lookup table, perhaps we could
5435   // split off a dense part and build a lookup table for that.
5436 
5437   // FIXME: This creates arrays of GEPs to constant strings, which means each
5438   // GEP needs a runtime relocation in PIC code. We should just build one big
5439   // string and lookup indices into that.
5440 
5441   // Ignore switches with less than three cases. Lookup tables will not make
5442   // them faster, so we don't analyze them.
5443   if (SI->getNumCases() < 3)
5444     return false;
5445 
5446   // Figure out the corresponding result for each case value and phi node in the
5447   // common destination, as well as the min and max case values.
5448   assert(!SI->cases().empty());
5449   SwitchInst::CaseIt CI = SI->case_begin();
5450   ConstantInt *MinCaseVal = CI->getCaseValue();
5451   ConstantInt *MaxCaseVal = CI->getCaseValue();
5452 
5453   BasicBlock *CommonDest = nullptr;
5454 
5455   using ResultListTy = SmallVector<std::pair<ConstantInt *, Constant *>, 4>;
5456   SmallDenseMap<PHINode *, ResultListTy> ResultLists;
5457 
5458   SmallDenseMap<PHINode *, Constant *> DefaultResults;
5459   SmallDenseMap<PHINode *, Type *> ResultTypes;
5460   SmallVector<PHINode *, 4> PHIs;
5461 
5462   for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
5463     ConstantInt *CaseVal = CI->getCaseValue();
5464     if (CaseVal->getValue().slt(MinCaseVal->getValue()))
5465       MinCaseVal = CaseVal;
5466     if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
5467       MaxCaseVal = CaseVal;
5468 
5469     // Resulting value at phi nodes for this case value.
5470     using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
5471     ResultsTy Results;
5472     if (!GetCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
5473                         Results, DL, TTI))
5474       return false;
5475 
5476     // Append the result from this case to the list for each phi.
5477     for (const auto &I : Results) {
5478       PHINode *PHI = I.first;
5479       Constant *Value = I.second;
5480       if (!ResultLists.count(PHI))
5481         PHIs.push_back(PHI);
5482       ResultLists[PHI].push_back(std::make_pair(CaseVal, Value));
5483     }
5484   }
5485 
5486   // Keep track of the result types.
5487   for (PHINode *PHI : PHIs) {
5488     ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
5489   }
5490 
5491   uint64_t NumResults = ResultLists[PHIs[0]].size();
5492   APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue();
5493   uint64_t TableSize = RangeSpread.getLimitedValue() + 1;
5494   bool TableHasHoles = (NumResults < TableSize);
5495 
5496   // If the table has holes, we need a constant result for the default case
5497   // or a bitmask that fits in a register.
5498   SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
5499   bool HasDefaultResults =
5500       GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest,
5501                      DefaultResultsList, DL, TTI);
5502 
5503   bool NeedMask = (TableHasHoles && !HasDefaultResults);
5504   if (NeedMask) {
5505     // As an extra penalty for the validity test we require more cases.
5506     if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
5507       return false;
5508     if (!DL.fitsInLegalInteger(TableSize))
5509       return false;
5510   }
5511 
5512   for (const auto &I : DefaultResultsList) {
5513     PHINode *PHI = I.first;
5514     Constant *Result = I.second;
5515     DefaultResults[PHI] = Result;
5516   }
5517 
5518   if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
5519     return false;
5520 
5521   // Create the BB that does the lookups.
5522   Module &Mod = *CommonDest->getParent()->getParent();
5523   BasicBlock *LookupBB = BasicBlock::Create(
5524       Mod.getContext(), "switch.lookup", CommonDest->getParent(), CommonDest);
5525 
5526   // Compute the table index value.
5527   Builder.SetInsertPoint(SI);
5528   Value *TableIndex;
5529   if (MinCaseVal->isNullValue())
5530     TableIndex = SI->getCondition();
5531   else
5532     TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal,
5533                                    "switch.tableidx");
5534 
5535   // Compute the maximum table size representable by the integer type we are
5536   // switching upon.
5537   unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
5538   uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
5539   assert(MaxTableSize >= TableSize &&
5540          "It is impossible for a switch to have more entries than the max "
5541          "representable value of its input integer type's size.");
5542 
5543   // If the default destination is unreachable, or if the lookup table covers
5544   // all values of the conditional variable, branch directly to the lookup table
5545   // BB. Otherwise, check that the condition is within the case range.
5546   const bool DefaultIsReachable =
5547       !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5548   const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
5549   BranchInst *RangeCheckBranch = nullptr;
5550 
5551   if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5552     Builder.CreateBr(LookupBB);
5553     // Note: We call removeProdecessor later since we need to be able to get the
5554     // PHI value for the default case in case we're using a bit mask.
5555   } else {
5556     Value *Cmp = Builder.CreateICmpULT(
5557         TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
5558     RangeCheckBranch =
5559         Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
5560   }
5561 
5562   // Populate the BB that does the lookups.
5563   Builder.SetInsertPoint(LookupBB);
5564 
5565   if (NeedMask) {
5566     // Before doing the lookup, we do the hole check. The LookupBB is therefore
5567     // re-purposed to do the hole check, and we create a new LookupBB.
5568     BasicBlock *MaskBB = LookupBB;
5569     MaskBB->setName("switch.hole_check");
5570     LookupBB = BasicBlock::Create(Mod.getContext(), "switch.lookup",
5571                                   CommonDest->getParent(), CommonDest);
5572 
5573     // Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
5574     // unnecessary illegal types.
5575     uint64_t TableSizePowOf2 = NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
5576     APInt MaskInt(TableSizePowOf2, 0);
5577     APInt One(TableSizePowOf2, 1);
5578     // Build bitmask; fill in a 1 bit for every case.
5579     const ResultListTy &ResultList = ResultLists[PHIs[0]];
5580     for (size_t I = 0, E = ResultList.size(); I != E; ++I) {
5581       uint64_t Idx = (ResultList[I].first->getValue() - MinCaseVal->getValue())
5582                          .getLimitedValue();
5583       MaskInt |= One << Idx;
5584     }
5585     ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
5586 
5587     // Get the TableIndex'th bit of the bitmask.
5588     // If this bit is 0 (meaning hole) jump to the default destination,
5589     // else continue with table lookup.
5590     IntegerType *MapTy = TableMask->getType();
5591     Value *MaskIndex =
5592         Builder.CreateZExtOrTrunc(TableIndex, MapTy, "switch.maskindex");
5593     Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
5594     Value *LoBit = Builder.CreateTrunc(
5595         Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
5596     Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
5597 
5598     Builder.SetInsertPoint(LookupBB);
5599     AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
5600   }
5601 
5602   if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5603     // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
5604     // do not delete PHINodes here.
5605     SI->getDefaultDest()->removePredecessor(SI->getParent(),
5606                                             /*KeepOneInputPHIs=*/true);
5607   }
5608 
5609   bool ReturnedEarly = false;
5610   for (PHINode *PHI : PHIs) {
5611     const ResultListTy &ResultList = ResultLists[PHI];
5612 
5613     // If using a bitmask, use any value to fill the lookup table holes.
5614     Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
5615     StringRef FuncName = Fn->getName();
5616     SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL,
5617                             FuncName);
5618 
5619     Value *Result = Table.BuildLookup(TableIndex, Builder);
5620 
5621     // If the result is used to return immediately from the function, we want to
5622     // do that right here.
5623     if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) &&
5624         PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) {
5625       Builder.CreateRet(Result);
5626       ReturnedEarly = true;
5627       break;
5628     }
5629 
5630     // Do a small peephole optimization: re-use the switch table compare if
5631     // possible.
5632     if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
5633       BasicBlock *PhiBlock = PHI->getParent();
5634       // Search for compare instructions which use the phi.
5635       for (auto *User : PHI->users()) {
5636         reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList);
5637       }
5638     }
5639 
5640     PHI->addIncoming(Result, LookupBB);
5641   }
5642 
5643   if (!ReturnedEarly)
5644     Builder.CreateBr(CommonDest);
5645 
5646   // Remove the switch.
5647   for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
5648     BasicBlock *Succ = SI->getSuccessor(i);
5649 
5650     if (Succ == SI->getDefaultDest())
5651       continue;
5652     Succ->removePredecessor(SI->getParent());
5653   }
5654   SI->eraseFromParent();
5655 
5656   ++NumLookupTables;
5657   if (NeedMask)
5658     ++NumLookupTablesHoles;
5659   return true;
5660 }
5661 
5662 static bool isSwitchDense(ArrayRef<int64_t> Values) {
5663   // See also SelectionDAGBuilder::isDense(), which this function was based on.
5664   uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
5665   uint64_t Range = Diff + 1;
5666   uint64_t NumCases = Values.size();
5667   // 40% is the default density for building a jump table in optsize/minsize mode.
5668   uint64_t MinDensity = 40;
5669 
5670   return NumCases * 100 >= Range * MinDensity;
5671 }
5672 
5673 /// Try to transform a switch that has "holes" in it to a contiguous sequence
5674 /// of cases.
5675 ///
5676 /// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
5677 /// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
5678 ///
5679 /// This converts a sparse switch into a dense switch which allows better
5680 /// lowering and could also allow transforming into a lookup table.
5681 static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
5682                               const DataLayout &DL,
5683                               const TargetTransformInfo &TTI) {
5684   auto *CondTy = cast<IntegerType>(SI->getCondition()->getType());
5685   if (CondTy->getIntegerBitWidth() > 64 ||
5686       !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5687     return false;
5688   // Only bother with this optimization if there are more than 3 switch cases;
5689   // SDAG will only bother creating jump tables for 4 or more cases.
5690   if (SI->getNumCases() < 4)
5691     return false;
5692 
5693   // This transform is agnostic to the signedness of the input or case values. We
5694   // can treat the case values as signed or unsigned. We can optimize more common
5695   // cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
5696   // as signed.
5697   SmallVector<int64_t,4> Values;
5698   for (auto &C : SI->cases())
5699     Values.push_back(C.getCaseValue()->getValue().getSExtValue());
5700   llvm::sort(Values);
5701 
5702   // If the switch is already dense, there's nothing useful to do here.
5703   if (isSwitchDense(Values))
5704     return false;
5705 
5706   // First, transform the values such that they start at zero and ascend.
5707   int64_t Base = Values[0];
5708   for (auto &V : Values)
5709     V -= (uint64_t)(Base);
5710 
5711   // Now we have signed numbers that have been shifted so that, given enough
5712   // precision, there are no negative values. Since the rest of the transform
5713   // is bitwise only, we switch now to an unsigned representation.
5714 
5715   // This transform can be done speculatively because it is so cheap - it
5716   // results in a single rotate operation being inserted.
5717   // FIXME: It's possible that optimizing a switch on powers of two might also
5718   // be beneficial - flag values are often powers of two and we could use a CLZ
5719   // as the key function.
5720 
5721   // countTrailingZeros(0) returns 64. As Values is guaranteed to have more than
5722   // one element and LLVM disallows duplicate cases, Shift is guaranteed to be
5723   // less than 64.
5724   unsigned Shift = 64;
5725   for (auto &V : Values)
5726     Shift = std::min(Shift, countTrailingZeros((uint64_t)V));
5727   assert(Shift < 64);
5728   if (Shift > 0)
5729     for (auto &V : Values)
5730       V = (int64_t)((uint64_t)V >> Shift);
5731 
5732   if (!isSwitchDense(Values))
5733     // Transform didn't create a dense switch.
5734     return false;
5735 
5736   // The obvious transform is to shift the switch condition right and emit a
5737   // check that the condition actually cleanly divided by GCD, i.e.
5738   //   C & (1 << Shift - 1) == 0
5739   // inserting a new CFG edge to handle the case where it didn't divide cleanly.
5740   //
5741   // A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
5742   // shift and puts the shifted-off bits in the uppermost bits. If any of these
5743   // are nonzero then the switch condition will be very large and will hit the
5744   // default case.
5745 
5746   auto *Ty = cast<IntegerType>(SI->getCondition()->getType());
5747   Builder.SetInsertPoint(SI);
5748   auto *ShiftC = ConstantInt::get(Ty, Shift);
5749   auto *Sub = Builder.CreateSub(SI->getCondition(), ConstantInt::get(Ty, Base));
5750   auto *LShr = Builder.CreateLShr(Sub, ShiftC);
5751   auto *Shl = Builder.CreateShl(Sub, Ty->getBitWidth() - Shift);
5752   auto *Rot = Builder.CreateOr(LShr, Shl);
5753   SI->replaceUsesOfWith(SI->getCondition(), Rot);
5754 
5755   for (auto Case : SI->cases()) {
5756     auto *Orig = Case.getCaseValue();
5757     auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base);
5758     Case.setValue(
5759         cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue()))));
5760   }
5761   return true;
5762 }
5763 
5764 bool SimplifyCFGOpt::simplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
5765   BasicBlock *BB = SI->getParent();
5766 
5767   if (isValueEqualityComparison(SI)) {
5768     // If we only have one predecessor, and if it is a branch on this value,
5769     // see if that predecessor totally determines the outcome of this switch.
5770     if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
5771       if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
5772         return requestResimplify();
5773 
5774     Value *Cond = SI->getCondition();
5775     if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
5776       if (SimplifySwitchOnSelect(SI, Select))
5777         return requestResimplify();
5778 
5779     // If the block only contains the switch, see if we can fold the block
5780     // away into any preds.
5781     if (SI == &*BB->instructionsWithoutDebug().begin())
5782       if (FoldValueComparisonIntoPredecessors(SI, Builder))
5783         return requestResimplify();
5784   }
5785 
5786   // Try to transform the switch into an icmp and a branch.
5787   if (TurnSwitchRangeIntoICmp(SI, Builder))
5788     return requestResimplify();
5789 
5790   // Remove unreachable cases.
5791   if (eliminateDeadSwitchCases(SI, Options.AC, DL))
5792     return requestResimplify();
5793 
5794   if (switchToSelect(SI, Builder, DL, TTI))
5795     return requestResimplify();
5796 
5797   if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI(SI))
5798     return requestResimplify();
5799 
5800   // The conversion from switch to lookup tables results in difficult-to-analyze
5801   // code and makes pruning branches much harder. This is a problem if the
5802   // switch expression itself can still be restricted as a result of inlining or
5803   // CVP. Therefore, only apply this transformation during late stages of the
5804   // optimisation pipeline.
5805   if (Options.ConvertSwitchToLookupTable &&
5806       SwitchToLookupTable(SI, Builder, DL, TTI))
5807     return requestResimplify();
5808 
5809   if (ReduceSwitchRange(SI, Builder, DL, TTI))
5810     return requestResimplify();
5811 
5812   return false;
5813 }
5814 
5815 bool SimplifyCFGOpt::simplifyIndirectBr(IndirectBrInst *IBI) {
5816   BasicBlock *BB = IBI->getParent();
5817   bool Changed = false;
5818 
5819   // Eliminate redundant destinations.
5820   SmallPtrSet<Value *, 8> Succs;
5821   for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
5822     BasicBlock *Dest = IBI->getDestination(i);
5823     if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
5824       Dest->removePredecessor(BB);
5825       IBI->removeDestination(i);
5826       --i;
5827       --e;
5828       Changed = true;
5829     }
5830   }
5831 
5832   if (IBI->getNumDestinations() == 0) {
5833     // If the indirectbr has no successors, change it to unreachable.
5834     new UnreachableInst(IBI->getContext(), IBI);
5835     EraseTerminatorAndDCECond(IBI);
5836     return true;
5837   }
5838 
5839   if (IBI->getNumDestinations() == 1) {
5840     // If the indirectbr has one successor, change it to a direct branch.
5841     BranchInst::Create(IBI->getDestination(0), IBI);
5842     EraseTerminatorAndDCECond(IBI);
5843     return true;
5844   }
5845 
5846   if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
5847     if (SimplifyIndirectBrOnSelect(IBI, SI))
5848       return requestResimplify();
5849   }
5850   return Changed;
5851 }
5852 
5853 /// Given an block with only a single landing pad and a unconditional branch
5854 /// try to find another basic block which this one can be merged with.  This
5855 /// handles cases where we have multiple invokes with unique landing pads, but
5856 /// a shared handler.
5857 ///
5858 /// We specifically choose to not worry about merging non-empty blocks
5859 /// here.  That is a PRE/scheduling problem and is best solved elsewhere.  In
5860 /// practice, the optimizer produces empty landing pad blocks quite frequently
5861 /// when dealing with exception dense code.  (see: instcombine, gvn, if-else
5862 /// sinking in this file)
5863 ///
5864 /// This is primarily a code size optimization.  We need to avoid performing
5865 /// any transform which might inhibit optimization (such as our ability to
5866 /// specialize a particular handler via tail commoning).  We do this by not
5867 /// merging any blocks which require us to introduce a phi.  Since the same
5868 /// values are flowing through both blocks, we don't lose any ability to
5869 /// specialize.  If anything, we make such specialization more likely.
5870 ///
5871 /// TODO - This transformation could remove entries from a phi in the target
5872 /// block when the inputs in the phi are the same for the two blocks being
5873 /// merged.  In some cases, this could result in removal of the PHI entirely.
5874 static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI,
5875                                  BasicBlock *BB) {
5876   auto Succ = BB->getUniqueSuccessor();
5877   assert(Succ);
5878   // If there's a phi in the successor block, we'd likely have to introduce
5879   // a phi into the merged landing pad block.
5880   if (isa<PHINode>(*Succ->begin()))
5881     return false;
5882 
5883   for (BasicBlock *OtherPred : predecessors(Succ)) {
5884     if (BB == OtherPred)
5885       continue;
5886     BasicBlock::iterator I = OtherPred->begin();
5887     LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I);
5888     if (!LPad2 || !LPad2->isIdenticalTo(LPad))
5889       continue;
5890     for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5891       ;
5892     BranchInst *BI2 = dyn_cast<BranchInst>(I);
5893     if (!BI2 || !BI2->isIdenticalTo(BI))
5894       continue;
5895 
5896     // We've found an identical block.  Update our predecessors to take that
5897     // path instead and make ourselves dead.
5898     SmallPtrSet<BasicBlock *, 16> Preds;
5899     Preds.insert(pred_begin(BB), pred_end(BB));
5900     for (BasicBlock *Pred : Preds) {
5901       InvokeInst *II = cast<InvokeInst>(Pred->getTerminator());
5902       assert(II->getNormalDest() != BB && II->getUnwindDest() == BB &&
5903              "unexpected successor");
5904       II->setUnwindDest(OtherPred);
5905     }
5906 
5907     // The debug info in OtherPred doesn't cover the merged control flow that
5908     // used to go through BB.  We need to delete it or update it.
5909     for (auto I = OtherPred->begin(), E = OtherPred->end(); I != E;) {
5910       Instruction &Inst = *I;
5911       I++;
5912       if (isa<DbgInfoIntrinsic>(Inst))
5913         Inst.eraseFromParent();
5914     }
5915 
5916     SmallPtrSet<BasicBlock *, 16> Succs;
5917     Succs.insert(succ_begin(BB), succ_end(BB));
5918     for (BasicBlock *Succ : Succs) {
5919       Succ->removePredecessor(BB);
5920     }
5921 
5922     IRBuilder<> Builder(BI);
5923     Builder.CreateUnreachable();
5924     BI->eraseFromParent();
5925     return true;
5926   }
5927   return false;
5928 }
5929 
5930 bool SimplifyCFGOpt::simplifyBranch(BranchInst *Branch, IRBuilder<> &Builder) {
5931   return Branch->isUnconditional() ? simplifyUncondBranch(Branch, Builder)
5932                                    : simplifyCondBranch(Branch, Builder);
5933 }
5934 
5935 bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst *BI,
5936                                           IRBuilder<> &Builder) {
5937   BasicBlock *BB = BI->getParent();
5938   BasicBlock *Succ = BI->getSuccessor(0);
5939 
5940   // If the Terminator is the only non-phi instruction, simplify the block.
5941   // If LoopHeader is provided, check if the block or its successor is a loop
5942   // header. (This is for early invocations before loop simplify and
5943   // vectorization to keep canonical loop forms for nested loops. These blocks
5944   // can be eliminated when the pass is invoked later in the back-end.)
5945   // Note that if BB has only one predecessor then we do not introduce new
5946   // backedge, so we can eliminate BB.
5947   bool NeedCanonicalLoop =
5948       Options.NeedCanonicalLoop &&
5949       (LoopHeaders && BB->hasNPredecessorsOrMore(2) &&
5950        (LoopHeaders->count(BB) || LoopHeaders->count(Succ)));
5951   BasicBlock::iterator I = BB->getFirstNonPHIOrDbg()->getIterator();
5952   if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
5953       !NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB))
5954     return true;
5955 
5956   // If the only instruction in the block is a seteq/setne comparison against a
5957   // constant, try to simplify the block.
5958   if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
5959     if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
5960       for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5961         ;
5962       if (I->isTerminator() &&
5963           tryToSimplifyUncondBranchWithICmpInIt(ICI, Builder))
5964         return true;
5965     }
5966 
5967   // See if we can merge an empty landing pad block with another which is
5968   // equivalent.
5969   if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) {
5970     for (++I; isa<DbgInfoIntrinsic>(I); ++I)
5971       ;
5972     if (I->isTerminator() && TryToMergeLandingPad(LPad, BI, BB))
5973       return true;
5974   }
5975 
5976   // If this basic block is ONLY a compare and a branch, and if a predecessor
5977   // branches to us and our successor, fold the comparison into the
5978   // predecessor and use logical operations to update the incoming value
5979   // for PHI nodes in common successor.
5980   if (FoldBranchToCommonDest(BI, nullptr, Options.BonusInstThreshold))
5981     return requestResimplify();
5982   return false;
5983 }
5984 
5985 static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
5986   BasicBlock *PredPred = nullptr;
5987   for (auto *P : predecessors(BB)) {
5988     BasicBlock *PPred = P->getSinglePredecessor();
5989     if (!PPred || (PredPred && PredPred != PPred))
5990       return nullptr;
5991     PredPred = PPred;
5992   }
5993   return PredPred;
5994 }
5995 
5996 bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
5997   BasicBlock *BB = BI->getParent();
5998   if (!Options.SimplifyCondBranch)
5999     return false;
6000 
6001   // Conditional branch
6002   if (isValueEqualityComparison(BI)) {
6003     // If we only have one predecessor, and if it is a branch on this value,
6004     // see if that predecessor totally determines the outcome of this
6005     // switch.
6006     if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
6007       if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
6008         return requestResimplify();
6009 
6010     // This block must be empty, except for the setcond inst, if it exists.
6011     // Ignore dbg intrinsics.
6012     auto I = BB->instructionsWithoutDebug().begin();
6013     if (&*I == BI) {
6014       if (FoldValueComparisonIntoPredecessors(BI, Builder))
6015         return requestResimplify();
6016     } else if (&*I == cast<Instruction>(BI->getCondition())) {
6017       ++I;
6018       if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
6019         return requestResimplify();
6020     }
6021   }
6022 
6023   // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
6024   if (SimplifyBranchOnICmpChain(BI, Builder, DL))
6025     return true;
6026 
6027   // If this basic block has dominating predecessor blocks and the dominating
6028   // blocks' conditions imply BI's condition, we know the direction of BI.
6029   Optional<bool> Imp = isImpliedByDomCondition(BI->getCondition(), BI, DL);
6030   if (Imp) {
6031     // Turn this into a branch on constant.
6032     auto *OldCond = BI->getCondition();
6033     ConstantInt *TorF = *Imp ? ConstantInt::getTrue(BB->getContext())
6034                              : ConstantInt::getFalse(BB->getContext());
6035     BI->setCondition(TorF);
6036     RecursivelyDeleteTriviallyDeadInstructions(OldCond);
6037     return requestResimplify();
6038   }
6039 
6040   // If this basic block is ONLY a compare and a branch, and if a predecessor
6041   // branches to us and one of our successors, fold the comparison into the
6042   // predecessor and use logical operations to pick the right destination.
6043   if (FoldBranchToCommonDest(BI, nullptr, Options.BonusInstThreshold))
6044     return requestResimplify();
6045 
6046   // We have a conditional branch to two blocks that are only reachable
6047   // from BI.  We know that the condbr dominates the two blocks, so see if
6048   // there is any identical code in the "then" and "else" blocks.  If so, we
6049   // can hoist it up to the branching block.
6050   if (BI->getSuccessor(0)->getSinglePredecessor()) {
6051     if (BI->getSuccessor(1)->getSinglePredecessor()) {
6052       if (HoistThenElseCodeToIf(BI, TTI))
6053         return requestResimplify();
6054     } else {
6055       // If Successor #1 has multiple preds, we may be able to conditionally
6056       // execute Successor #0 if it branches to Successor #1.
6057       Instruction *Succ0TI = BI->getSuccessor(0)->getTerminator();
6058       if (Succ0TI->getNumSuccessors() == 1 &&
6059           Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
6060         if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI))
6061           return requestResimplify();
6062     }
6063   } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
6064     // If Successor #0 has multiple preds, we may be able to conditionally
6065     // execute Successor #1 if it branches to Successor #0.
6066     Instruction *Succ1TI = BI->getSuccessor(1)->getTerminator();
6067     if (Succ1TI->getNumSuccessors() == 1 &&
6068         Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
6069       if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI))
6070         return requestResimplify();
6071   }
6072 
6073   // If this is a branch on a phi node in the current block, thread control
6074   // through this block if any PHI node entries are constants.
6075   if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
6076     if (PN->getParent() == BI->getParent())
6077       if (FoldCondBranchOnPHI(BI, DL, Options.AC))
6078         return requestResimplify();
6079 
6080   // Scan predecessor blocks for conditional branches.
6081   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
6082     if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
6083       if (PBI != BI && PBI->isConditional())
6084         if (SimplifyCondBranchToCondBranch(PBI, BI, DL, TTI))
6085           return requestResimplify();
6086 
6087   // Look for diamond patterns.
6088   if (MergeCondStores)
6089     if (BasicBlock *PrevBB = allPredecessorsComeFromSameSource(BB))
6090       if (BranchInst *PBI = dyn_cast<BranchInst>(PrevBB->getTerminator()))
6091         if (PBI != BI && PBI->isConditional())
6092           if (mergeConditionalStores(PBI, BI, DL, TTI))
6093             return requestResimplify();
6094 
6095   return false;
6096 }
6097 
6098 /// Check if passing a value to an instruction will cause undefined behavior.
6099 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
6100   Constant *C = dyn_cast<Constant>(V);
6101   if (!C)
6102     return false;
6103 
6104   if (I->use_empty())
6105     return false;
6106 
6107   if (C->isNullValue() || isa<UndefValue>(C)) {
6108     // Only look at the first use, avoid hurting compile time with long uselists
6109     User *Use = *I->user_begin();
6110 
6111     // Now make sure that there are no instructions in between that can alter
6112     // control flow (eg. calls)
6113     for (BasicBlock::iterator
6114              i = ++BasicBlock::iterator(I),
6115              UI = BasicBlock::iterator(dyn_cast<Instruction>(Use));
6116          i != UI; ++i)
6117       if (i == I->getParent()->end() || i->mayHaveSideEffects())
6118         return false;
6119 
6120     // Look through GEPs. A load from a GEP derived from NULL is still undefined
6121     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
6122       if (GEP->getPointerOperand() == I)
6123         return passingValueIsAlwaysUndefined(V, GEP);
6124 
6125     // Look through bitcasts.
6126     if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
6127       return passingValueIsAlwaysUndefined(V, BC);
6128 
6129     // Load from null is undefined.
6130     if (LoadInst *LI = dyn_cast<LoadInst>(Use))
6131       if (!LI->isVolatile())
6132         return !NullPointerIsDefined(LI->getFunction(),
6133                                      LI->getPointerAddressSpace());
6134 
6135     // Store to null is undefined.
6136     if (StoreInst *SI = dyn_cast<StoreInst>(Use))
6137       if (!SI->isVolatile())
6138         return (!NullPointerIsDefined(SI->getFunction(),
6139                                       SI->getPointerAddressSpace())) &&
6140                SI->getPointerOperand() == I;
6141 
6142     // A call to null is undefined.
6143     if (auto *CB = dyn_cast<CallBase>(Use))
6144       return !NullPointerIsDefined(CB->getFunction()) &&
6145              CB->getCalledOperand() == I;
6146   }
6147   return false;
6148 }
6149 
6150 /// If BB has an incoming value that will always trigger undefined behavior
6151 /// (eg. null pointer dereference), remove the branch leading here.
6152 static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
6153   for (PHINode &PHI : BB->phis())
6154     for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
6155       if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
6156         Instruction *T = PHI.getIncomingBlock(i)->getTerminator();
6157         IRBuilder<> Builder(T);
6158         if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
6159           BB->removePredecessor(PHI.getIncomingBlock(i));
6160           // Turn uncoditional branches into unreachables and remove the dead
6161           // destination from conditional branches.
6162           if (BI->isUnconditional())
6163             Builder.CreateUnreachable();
6164           else
6165             Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
6166                                                        : BI->getSuccessor(0));
6167           BI->eraseFromParent();
6168           return true;
6169         }
6170         // TODO: SwitchInst.
6171       }
6172 
6173   return false;
6174 }
6175 
6176 bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
6177   bool Changed = false;
6178 
6179   assert(BB && BB->getParent() && "Block not embedded in function!");
6180   assert(BB->getTerminator() && "Degenerate basic block encountered!");
6181 
6182   // Remove basic blocks that have no predecessors (except the entry block)...
6183   // or that just have themself as a predecessor.  These are unreachable.
6184   if ((pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) ||
6185       BB->getSinglePredecessor() == BB) {
6186     LLVM_DEBUG(dbgs() << "Removing BB: \n" << *BB);
6187     DeleteDeadBlock(BB);
6188     return true;
6189   }
6190 
6191   // Check to see if we can constant propagate this terminator instruction
6192   // away...
6193   Changed |= ConstantFoldTerminator(BB, true);
6194 
6195   // Check for and eliminate duplicate PHI nodes in this block.
6196   Changed |= EliminateDuplicatePHINodes(BB);
6197 
6198   // Check for and remove branches that will always cause undefined behavior.
6199   Changed |= removeUndefIntroducingPredecessor(BB);
6200 
6201   // Merge basic blocks into their predecessor if there is only one distinct
6202   // pred, and if there is only one distinct successor of the predecessor, and
6203   // if there are no PHI nodes.
6204   if (MergeBlockIntoPredecessor(BB))
6205     return true;
6206 
6207   if (SinkCommon && Options.SinkCommonInsts)
6208     Changed |= SinkCommonCodeFromPredecessors(BB);
6209 
6210   IRBuilder<> Builder(BB);
6211 
6212   if (Options.FoldTwoEntryPHINode) {
6213     // If there is a trivial two-entry PHI node in this basic block, and we can
6214     // eliminate it, do so now.
6215     if (auto *PN = dyn_cast<PHINode>(BB->begin()))
6216       if (PN->getNumIncomingValues() == 2)
6217         Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
6218   }
6219 
6220   Instruction *Terminator = BB->getTerminator();
6221   Builder.SetInsertPoint(Terminator);
6222   switch (Terminator->getOpcode()) {
6223   case Instruction::Br:
6224     Changed |= simplifyBranch(cast<BranchInst>(Terminator), Builder);
6225     break;
6226   case Instruction::Ret:
6227     Changed |= simplifyReturn(cast<ReturnInst>(Terminator), Builder);
6228     break;
6229   case Instruction::Resume:
6230     Changed |= simplifyResume(cast<ResumeInst>(Terminator), Builder);
6231     break;
6232   case Instruction::CleanupRet:
6233     Changed |= simplifyCleanupReturn(cast<CleanupReturnInst>(Terminator));
6234     break;
6235   case Instruction::Switch:
6236     Changed |= simplifySwitch(cast<SwitchInst>(Terminator), Builder);
6237     break;
6238   case Instruction::Unreachable:
6239     Changed |= simplifyUnreachable(cast<UnreachableInst>(Terminator));
6240     break;
6241   case Instruction::IndirectBr:
6242     Changed |= simplifyIndirectBr(cast<IndirectBrInst>(Terminator));
6243     break;
6244   }
6245 
6246   return Changed;
6247 }
6248 
6249 bool SimplifyCFGOpt::run(BasicBlock *BB) {
6250   bool Changed = false;
6251 
6252   // Repeated simplify BB as long as resimplification is requested.
6253   do {
6254     Resimplify = false;
6255 
6256     // Perform one round of simplifcation. Resimplify flag will be set if
6257     // another iteration is requested.
6258     Changed |= simplifyOnce(BB);
6259   } while (Resimplify);
6260 
6261   return Changed;
6262 }
6263 
6264 bool llvm::simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
6265                        const SimplifyCFGOptions &Options,
6266                        SmallPtrSetImpl<BasicBlock *> *LoopHeaders) {
6267   return SimplifyCFGOpt(TTI, BB->getModule()->getDataLayout(), LoopHeaders,
6268                         Options)
6269       .run(BB);
6270 }
6271