1f22ef01cSRoman Divacky //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // Peephole optimize the CFG.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky
14d88c1a5aSDimitry Andric #include "llvm/ADT/APInt.h"
15d88c1a5aSDimitry Andric #include "llvm/ADT/ArrayRef.h"
167ae0e2c9SDimitry Andric #include "llvm/ADT/DenseMap.h"
17d88c1a5aSDimitry Andric #include "llvm/ADT/Optional.h"
18db17bf38SDimitry Andric #include "llvm/ADT/STLExtras.h"
197d523365SDimitry Andric #include "llvm/ADT/SetOperations.h"
207ae0e2c9SDimitry Andric #include "llvm/ADT/SetVector.h"
217ae0e2c9SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
227ae0e2c9SDimitry Andric #include "llvm/ADT/SmallVector.h"
237ae0e2c9SDimitry Andric #include "llvm/ADT/Statistic.h"
242cab237bSDimitry Andric #include "llvm/ADT/StringRef.h"
257a7e6055SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
26f785676fSDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
274d0b32cdSDimitry Andric #include "llvm/Analysis/EHPersonalities.h"
282754fe60SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
29139f7f9bSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
304ba319b5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
31bd5abe19SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
322cab237bSDimitry Andric #include "llvm/IR/Attributes.h"
33d88c1a5aSDimitry Andric #include "llvm/IR/BasicBlock.h"
3491bc56edSDimitry Andric #include "llvm/IR/CFG.h"
35db17bf38SDimitry Andric #include "llvm/IR/CallSite.h"
36d88c1a5aSDimitry Andric #include "llvm/IR/Constant.h"
3791bc56edSDimitry Andric #include "llvm/IR/ConstantRange.h"
38139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
39139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
40139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
412cab237bSDimitry Andric #include "llvm/IR/Function.h"
42d88c1a5aSDimitry Andric #include "llvm/IR/GlobalValue.h"
43139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
44139f7f9bSDimitry Andric #include "llvm/IR/IRBuilder.h"
45d88c1a5aSDimitry Andric #include "llvm/IR/InstrTypes.h"
46d88c1a5aSDimitry Andric #include "llvm/IR/Instruction.h"
47139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h"
48139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
49d88c1a5aSDimitry Andric #include "llvm/IR/Intrinsics.h"
50139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
51139f7f9bSDimitry Andric #include "llvm/IR/MDBuilder.h"
52139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h"
53139f7f9bSDimitry Andric #include "llvm/IR/Module.h"
5491bc56edSDimitry Andric #include "llvm/IR/NoFolder.h"
55139f7f9bSDimitry Andric #include "llvm/IR/Operator.h"
5691bc56edSDimitry Andric #include "llvm/IR/PatternMatch.h"
57139f7f9bSDimitry Andric #include "llvm/IR/Type.h"
582cab237bSDimitry Andric #include "llvm/IR/Use.h"
59d88c1a5aSDimitry Andric #include "llvm/IR/User.h"
60d88c1a5aSDimitry Andric #include "llvm/IR/Value.h"
61d88c1a5aSDimitry Andric #include "llvm/Support/Casting.h"
622754fe60SDimitry Andric #include "llvm/Support/CommandLine.h"
632754fe60SDimitry Andric #include "llvm/Support/Debug.h"
64d88c1a5aSDimitry Andric #include "llvm/Support/ErrorHandling.h"
6551690af2SDimitry Andric #include "llvm/Support/KnownBits.h"
66d88c1a5aSDimitry Andric #include "llvm/Support/MathExtras.h"
672754fe60SDimitry Andric #include "llvm/Support/raw_ostream.h"
687ae0e2c9SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
6939d628a0SDimitry Andric #include "llvm/Transforms/Utils/ValueMapper.h"
70f22ef01cSRoman Divacky #include <algorithm>
71d88c1a5aSDimitry Andric #include <cassert>
72d88c1a5aSDimitry Andric #include <climits>
73d88c1a5aSDimitry Andric #include <cstddef>
74d88c1a5aSDimitry Andric #include <cstdint>
75d88c1a5aSDimitry Andric #include <iterator>
76f22ef01cSRoman Divacky #include <map>
77139f7f9bSDimitry Andric #include <set>
782cab237bSDimitry Andric #include <tuple>
79d88c1a5aSDimitry Andric #include <utility>
80d88c1a5aSDimitry Andric #include <vector>
81d88c1a5aSDimitry Andric
82f22ef01cSRoman Divacky using namespace llvm;
83f785676fSDimitry Andric using namespace PatternMatch;
84f22ef01cSRoman Divacky
8591bc56edSDimitry Andric #define DEBUG_TYPE "simplifycfg"
8691bc56edSDimitry Andric
87ff0cc061SDimitry Andric // Chosen as 2 so as to be cheap, but still to have enough power to fold
88ff0cc061SDimitry Andric // a select, so the "clamp" idiom (of a min followed by a max) will be caught.
89ff0cc061SDimitry Andric // To catch this, we need to fold a compare and a select, hence '2' being the
90ff0cc061SDimitry Andric // minimum reasonable default.
913ca95b02SDimitry Andric static cl::opt<unsigned> PHINodeFoldingThreshold(
923ca95b02SDimitry Andric "phi-node-folding-threshold", cl::Hidden, cl::init(2),
933ca95b02SDimitry Andric cl::desc(
943ca95b02SDimitry Andric "Control the amount of phi node folding to perform (default = 2)"));
953b0f4066SDimitry Andric
963ca95b02SDimitry Andric static cl::opt<bool> DupRet(
973ca95b02SDimitry Andric "simplifycfg-dup-ret", cl::Hidden, cl::init(false),
982754fe60SDimitry Andric cl::desc("Duplicate return instructions into unconditional branches"));
992754fe60SDimitry Andric
1003861d79fSDimitry Andric static cl::opt<bool>
1013861d79fSDimitry Andric SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true),
1023861d79fSDimitry Andric cl::desc("Sink common instructions down to the end block"));
1033861d79fSDimitry Andric
10491bc56edSDimitry Andric static cl::opt<bool> HoistCondStores(
10591bc56edSDimitry Andric "simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
10691bc56edSDimitry Andric cl::desc("Hoist conditional stores if an unconditional store precedes"));
107284c1978SDimitry Andric
1087d523365SDimitry Andric static cl::opt<bool> MergeCondStores(
1097d523365SDimitry Andric "simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true),
1107d523365SDimitry Andric cl::desc("Hoist conditional stores even if an unconditional store does not "
1117d523365SDimitry Andric "precede - hoist multiple conditional stores into a single "
1127d523365SDimitry Andric "predicated store"));
1137d523365SDimitry Andric
1147d523365SDimitry Andric static cl::opt<bool> MergeCondStoresAggressively(
1157d523365SDimitry Andric "simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false),
1167d523365SDimitry Andric cl::desc("When merging conditional stores, do so even if the resultant "
1177d523365SDimitry Andric "basic blocks are unlikely to be if-converted as a result"));
1187d523365SDimitry Andric
1197d523365SDimitry Andric static cl::opt<bool> SpeculateOneExpensiveInst(
1207d523365SDimitry Andric "speculate-one-expensive-inst", cl::Hidden, cl::init(true),
1217d523365SDimitry Andric cl::desc("Allow exactly one expensive instruction to be speculatively "
1227d523365SDimitry Andric "executed"));
1237d523365SDimitry Andric
124a8bcc4d8SDimitry Andric static cl::opt<unsigned> MaxSpeculationDepth(
125a8bcc4d8SDimitry Andric "max-speculation-depth", cl::Hidden, cl::init(10),
126a8bcc4d8SDimitry Andric cl::desc("Limit maximum recursion depth when calculating costs of "
127a8bcc4d8SDimitry Andric "speculatively executed instructions"));
128a8bcc4d8SDimitry Andric
1293861d79fSDimitry Andric STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
1303ca95b02SDimitry Andric STATISTIC(NumLinearMaps,
1313ca95b02SDimitry Andric "Number of switch instructions turned into linear mapping");
1323ca95b02SDimitry Andric STATISTIC(NumLookupTables,
1333ca95b02SDimitry Andric "Number of switch instructions turned into lookup tables");
1343ca95b02SDimitry Andric STATISTIC(
1353ca95b02SDimitry Andric NumLookupTablesHoles,
1363ca95b02SDimitry Andric "Number of switch instructions turned into lookup tables (holes checked)");
13739d628a0SDimitry Andric STATISTIC(NumTableCmpReuses, "Number of reused switch table lookup compares");
1383ca95b02SDimitry Andric STATISTIC(NumSinkCommons,
1393ca95b02SDimitry Andric "Number of common instructions sunk down to the end block");
140f22ef01cSRoman Divacky STATISTIC(NumSpeculations, "Number of speculative executed instructions");
141f22ef01cSRoman Divacky
142f22ef01cSRoman Divacky namespace {
143d88c1a5aSDimitry Andric
14439d628a0SDimitry Andric // The first field contains the value that the switch produces when a certain
1457d523365SDimitry Andric // case group is selected, and the second field is a vector containing the
1467d523365SDimitry Andric // cases composing the case group.
1472cab237bSDimitry Andric using SwitchCaseResultVectorTy =
1482cab237bSDimitry Andric SmallVector<std::pair<Constant *, SmallVector<ConstantInt *, 4>>, 2>;
1492cab237bSDimitry Andric
15039d628a0SDimitry Andric // The first field contains the phi node that generates a result of the switch
1517d523365SDimitry Andric // and the second field contains the value generated for a certain case in the
1527d523365SDimitry Andric // switch for that PHI.
1532cab237bSDimitry Andric using SwitchCaseResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
15439d628a0SDimitry Andric
1557ae0e2c9SDimitry Andric /// ValueEqualityComparisonCase - Represents a case of a switch.
1567ae0e2c9SDimitry Andric struct ValueEqualityComparisonCase {
1577ae0e2c9SDimitry Andric ConstantInt *Value;
1587ae0e2c9SDimitry Andric BasicBlock *Dest;
1597ae0e2c9SDimitry Andric
ValueEqualityComparisonCase__anone609cfb00111::ValueEqualityComparisonCase1607ae0e2c9SDimitry Andric ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
1617ae0e2c9SDimitry Andric : Value(Value), Dest(Dest) {}
1627ae0e2c9SDimitry Andric
operator <__anone609cfb00111::ValueEqualityComparisonCase1637ae0e2c9SDimitry Andric bool operator<(ValueEqualityComparisonCase RHS) const {
1647ae0e2c9SDimitry Andric // Comparing pointers is ok as we only rely on the order for uniquing.
1657ae0e2c9SDimitry Andric return Value < RHS.Value;
1667ae0e2c9SDimitry Andric }
1673861d79fSDimitry Andric
operator ==__anone609cfb00111::ValueEqualityComparisonCase1683861d79fSDimitry Andric bool operator==(BasicBlock *RHSDest) const { return Dest == RHSDest; }
1697ae0e2c9SDimitry Andric };
1707ae0e2c9SDimitry Andric
171f22ef01cSRoman Divacky class SimplifyCFGOpt {
172139f7f9bSDimitry Andric const TargetTransformInfo &TTI;
173ff0cc061SDimitry Andric const DataLayout &DL;
1743ca95b02SDimitry Andric SmallPtrSetImpl<BasicBlock *> *LoopHeaders;
1752cab237bSDimitry Andric const SimplifyCFGOptions &Options;
176*b5893f02SDimitry Andric bool Resimplify;
1772cab237bSDimitry Andric
178*b5893f02SDimitry Andric Value *isValueEqualityComparison(Instruction *TI);
1793ca95b02SDimitry Andric BasicBlock *GetValueEqualityComparisonCases(
180*b5893f02SDimitry Andric Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
181*b5893f02SDimitry Andric bool SimplifyEqualityComparisonWithOnlyPredecessor(Instruction *TI,
182bd5abe19SDimitry Andric BasicBlock *Pred,
183bd5abe19SDimitry Andric IRBuilder<> &Builder);
184*b5893f02SDimitry Andric bool FoldValueComparisonIntoPredecessors(Instruction *TI,
185bd5abe19SDimitry Andric IRBuilder<> &Builder);
186f22ef01cSRoman Divacky
187bd5abe19SDimitry Andric bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
188dff0c46cSDimitry Andric bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
189444ed5c5SDimitry Andric bool SimplifySingleResume(ResumeInst *RI);
190444ed5c5SDimitry Andric bool SimplifyCommonResume(ResumeInst *RI);
1917d523365SDimitry Andric bool SimplifyCleanupReturn(CleanupReturnInst *RI);
1922754fe60SDimitry Andric bool SimplifyUnreachable(UnreachableInst *UI);
193bd5abe19SDimitry Andric bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
1942754fe60SDimitry Andric bool SimplifyIndirectBr(IndirectBrInst *IBI);
195bd5abe19SDimitry Andric bool SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder);
196bd5abe19SDimitry Andric bool SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder);
1972754fe60SDimitry Andric
198*b5893f02SDimitry Andric bool tryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
199*b5893f02SDimitry Andric IRBuilder<> &Builder);
200*b5893f02SDimitry Andric
201f22ef01cSRoman Divacky public:
SimplifyCFGOpt(const TargetTransformInfo & TTI,const DataLayout & DL,SmallPtrSetImpl<BasicBlock * > * LoopHeaders,const SimplifyCFGOptions & Opts)202ff0cc061SDimitry Andric SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout &DL,
2037a7e6055SDimitry Andric SmallPtrSetImpl<BasicBlock *> *LoopHeaders,
2042cab237bSDimitry Andric const SimplifyCFGOptions &Opts)
2052cab237bSDimitry Andric : TTI(TTI), DL(DL), LoopHeaders(LoopHeaders), Options(Opts) {}
206d88c1a5aSDimitry Andric
207f22ef01cSRoman Divacky bool run(BasicBlock *BB);
208*b5893f02SDimitry Andric bool simplifyOnce(BasicBlock *BB);
209*b5893f02SDimitry Andric
210*b5893f02SDimitry Andric // Helper to set Resimplify and return change indication.
requestResimplify()211*b5893f02SDimitry Andric bool requestResimplify() {
212*b5893f02SDimitry Andric Resimplify = true;
213*b5893f02SDimitry Andric return true;
214*b5893f02SDimitry Andric }
215f22ef01cSRoman Divacky };
216d88c1a5aSDimitry Andric
217d88c1a5aSDimitry Andric } // end anonymous namespace
218f22ef01cSRoman Divacky
2193dac3a9bSDimitry Andric /// Return true if it is safe to merge these two
220f22ef01cSRoman Divacky /// terminator instructions together.
221d88c1a5aSDimitry Andric static bool
SafeToMergeTerminators(Instruction * SI1,Instruction * SI2,SmallSetVector<BasicBlock *,4> * FailBlocks=nullptr)222*b5893f02SDimitry Andric SafeToMergeTerminators(Instruction *SI1, Instruction *SI2,
223d88c1a5aSDimitry Andric SmallSetVector<BasicBlock *, 4> *FailBlocks = nullptr) {
2243ca95b02SDimitry Andric if (SI1 == SI2)
2253ca95b02SDimitry Andric return false; // Can't merge with self!
226f22ef01cSRoman Divacky
227f22ef01cSRoman Divacky // It is not safe to merge these two switch instructions if they have a common
228f22ef01cSRoman Divacky // successor, and if that successor has a PHI node, and if *that* PHI node has
229f22ef01cSRoman Divacky // conflicting incoming values from the two switch blocks.
230f22ef01cSRoman Divacky BasicBlock *SI1BB = SI1->getParent();
231f22ef01cSRoman Divacky BasicBlock *SI2BB = SI2->getParent();
232f22ef01cSRoman Divacky
233d88c1a5aSDimitry Andric SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
234d88c1a5aSDimitry Andric bool Fail = false;
2353ca95b02SDimitry Andric for (BasicBlock *Succ : successors(SI2BB))
2363ca95b02SDimitry Andric if (SI1Succs.count(Succ))
2373ca95b02SDimitry Andric for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
238f22ef01cSRoman Divacky PHINode *PN = cast<PHINode>(BBI);
239f22ef01cSRoman Divacky if (PN->getIncomingValueForBlock(SI1BB) !=
240d88c1a5aSDimitry Andric PN->getIncomingValueForBlock(SI2BB)) {
241d88c1a5aSDimitry Andric if (FailBlocks)
242d88c1a5aSDimitry Andric FailBlocks->insert(Succ);
243d88c1a5aSDimitry Andric Fail = true;
244d88c1a5aSDimitry Andric }
245f22ef01cSRoman Divacky }
246f22ef01cSRoman Divacky
247d88c1a5aSDimitry Andric return !Fail;
248f22ef01cSRoman Divacky }
249f22ef01cSRoman Divacky
2503dac3a9bSDimitry Andric /// Return true if it is safe and profitable to merge these two terminator
2513dac3a9bSDimitry Andric /// instructions together, where SI1 is an unconditional branch. PhiNodes will
2523dac3a9bSDimitry Andric /// store all PHI nodes in common successors.
2533ca95b02SDimitry Andric static bool
isProfitableToFoldUnconditional(BranchInst * SI1,BranchInst * SI2,Instruction * Cond,SmallVectorImpl<PHINode * > & PhiNodes)2543ca95b02SDimitry Andric isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2,
2557ae0e2c9SDimitry Andric Instruction *Cond,
2567ae0e2c9SDimitry Andric SmallVectorImpl<PHINode *> &PhiNodes) {
2573ca95b02SDimitry Andric if (SI1 == SI2)
2583ca95b02SDimitry Andric return false; // Can't merge with self!
2597ae0e2c9SDimitry Andric assert(SI1->isUnconditional() && SI2->isConditional());
2607ae0e2c9SDimitry Andric
2617ae0e2c9SDimitry Andric // We fold the unconditional branch if we can easily update all PHI nodes in
2627ae0e2c9SDimitry Andric // common successors:
2637ae0e2c9SDimitry Andric // 1> We have a constant incoming value for the conditional branch;
2647ae0e2c9SDimitry Andric // 2> We have "Cond" as the incoming value for the unconditional branch;
2657ae0e2c9SDimitry Andric // 3> SI2->getCondition() and Cond have same operands.
2667ae0e2c9SDimitry Andric CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
2673ca95b02SDimitry Andric if (!Ci2)
2683ca95b02SDimitry Andric return false;
2697ae0e2c9SDimitry Andric if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
2707ae0e2c9SDimitry Andric Cond->getOperand(1) == Ci2->getOperand(1)) &&
2717ae0e2c9SDimitry Andric !(Cond->getOperand(0) == Ci2->getOperand(1) &&
2727ae0e2c9SDimitry Andric Cond->getOperand(1) == Ci2->getOperand(0)))
2737ae0e2c9SDimitry Andric return false;
2747ae0e2c9SDimitry Andric
2757ae0e2c9SDimitry Andric BasicBlock *SI1BB = SI1->getParent();
2767ae0e2c9SDimitry Andric BasicBlock *SI2BB = SI2->getParent();
2777ae0e2c9SDimitry Andric SmallPtrSet<BasicBlock *, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
2783ca95b02SDimitry Andric for (BasicBlock *Succ : successors(SI2BB))
2793ca95b02SDimitry Andric if (SI1Succs.count(Succ))
2803ca95b02SDimitry Andric for (BasicBlock::iterator BBI = Succ->begin(); isa<PHINode>(BBI); ++BBI) {
2817ae0e2c9SDimitry Andric PHINode *PN = cast<PHINode>(BBI);
2827ae0e2c9SDimitry Andric if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
2837ae0e2c9SDimitry Andric !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
2847ae0e2c9SDimitry Andric return false;
2857ae0e2c9SDimitry Andric PhiNodes.push_back(PN);
2867ae0e2c9SDimitry Andric }
2877ae0e2c9SDimitry Andric return true;
2887ae0e2c9SDimitry Andric }
2897ae0e2c9SDimitry Andric
2903dac3a9bSDimitry Andric /// Update PHI nodes in Succ to indicate that there will now be entries in it
2913dac3a9bSDimitry Andric /// from the 'NewPred' block. The values that will be flowing into the PHI nodes
2923dac3a9bSDimitry Andric /// will be the same as those coming in from ExistPred, an existing predecessor
2933dac3a9bSDimitry Andric /// of Succ.
AddPredecessorToBlock(BasicBlock * Succ,BasicBlock * NewPred,BasicBlock * ExistPred)294f22ef01cSRoman Divacky static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
295f22ef01cSRoman Divacky BasicBlock *ExistPred) {
29630785c0eSDimitry Andric for (PHINode &PN : Succ->phis())
29730785c0eSDimitry Andric PN.addIncoming(PN.getIncomingValueForBlock(ExistPred), NewPred);
298f22ef01cSRoman Divacky }
299f22ef01cSRoman Divacky
3003dac3a9bSDimitry Andric /// Compute an abstract "cost" of speculating the given instruction,
3013dac3a9bSDimitry Andric /// which is assumed to be safe to speculate. TCC_Free means cheap,
3023dac3a9bSDimitry Andric /// TCC_Basic means less cheap, and TCC_Expensive means prohibitively
303ff0cc061SDimitry Andric /// expensive.
ComputeSpeculationCost(const User * I,const TargetTransformInfo & TTI)304ff0cc061SDimitry Andric static unsigned ComputeSpeculationCost(const User *I,
305ff0cc061SDimitry Andric const TargetTransformInfo &TTI) {
306ff0cc061SDimitry Andric assert(isSafeToSpeculativelyExecute(I) &&
307dff0c46cSDimitry Andric "Instruction is not safe to speculatively execute!");
308ff0cc061SDimitry Andric return TTI.getUserCost(I);
309dff0c46cSDimitry Andric }
3107d523365SDimitry Andric
3113dac3a9bSDimitry Andric /// If we have a merge point of an "if condition" as accepted above,
3123dac3a9bSDimitry Andric /// return true if the specified value dominates the block. We
313f22ef01cSRoman Divacky /// don't handle the true generality of domination here, just a special case
314f22ef01cSRoman Divacky /// which works well enough for us.
315f22ef01cSRoman Divacky ///
316f22ef01cSRoman Divacky /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
3173b0f4066SDimitry Andric /// see if V (which must be an instruction) and its recursive operands
3183b0f4066SDimitry Andric /// that do not dominate BB have a combined cost lower than CostRemaining and
3193b0f4066SDimitry Andric /// are non-trapping. If both are true, the instruction is inserted into the
3203b0f4066SDimitry Andric /// set and true is returned.
3213b0f4066SDimitry Andric ///
3223b0f4066SDimitry Andric /// The cost for most non-trapping instructions is defined as 1 except for
3233b0f4066SDimitry Andric /// Select whose cost is 2.
3243b0f4066SDimitry Andric ///
3253b0f4066SDimitry Andric /// After this function returns, CostRemaining is decreased by the cost of
3263b0f4066SDimitry Andric /// V plus its non-dominating operands. If that cost is greater than
3273b0f4066SDimitry Andric /// CostRemaining, false is returned and CostRemaining is undefined.
DominatesMergePoint(Value * V,BasicBlock * BB,SmallPtrSetImpl<Instruction * > & AggressiveInsts,unsigned & CostRemaining,const TargetTransformInfo & TTI,unsigned Depth=0)328f22ef01cSRoman Divacky static bool DominatesMergePoint(Value *V, BasicBlock *BB,
329*b5893f02SDimitry Andric SmallPtrSetImpl<Instruction *> &AggressiveInsts,
33091bc56edSDimitry Andric unsigned &CostRemaining,
3317d523365SDimitry Andric const TargetTransformInfo &TTI,
3327d523365SDimitry Andric unsigned Depth = 0) {
333a8bcc4d8SDimitry Andric // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
334a8bcc4d8SDimitry Andric // so limit the recursion depth.
335a8bcc4d8SDimitry Andric // TODO: While this recursion limit does prevent pathological behavior, it
336a8bcc4d8SDimitry Andric // would be better to track visited instructions to avoid cycles.
337a8bcc4d8SDimitry Andric if (Depth == MaxSpeculationDepth)
338a8bcc4d8SDimitry Andric return false;
339a8bcc4d8SDimitry Andric
340f22ef01cSRoman Divacky Instruction *I = dyn_cast<Instruction>(V);
341f22ef01cSRoman Divacky if (!I) {
342f22ef01cSRoman Divacky // Non-instructions all dominate instructions, but not all constantexprs
343f22ef01cSRoman Divacky // can be executed unconditionally.
344f22ef01cSRoman Divacky if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
345f22ef01cSRoman Divacky if (C->canTrap())
346f22ef01cSRoman Divacky return false;
347f22ef01cSRoman Divacky return true;
348f22ef01cSRoman Divacky }
349f22ef01cSRoman Divacky BasicBlock *PBB = I->getParent();
350f22ef01cSRoman Divacky
351f22ef01cSRoman Divacky // We don't want to allow weird loops that might have the "if condition" in
352f22ef01cSRoman Divacky // the bottom of this block.
3533ca95b02SDimitry Andric if (PBB == BB)
3543ca95b02SDimitry Andric return false;
355f22ef01cSRoman Divacky
356f22ef01cSRoman Divacky // If this instruction is defined in a block that contains an unconditional
357f22ef01cSRoman Divacky // branch to BB, then it must be in the 'conditional' part of the "if
3582754fe60SDimitry Andric // statement". If not, it definitely dominates the region.
3592754fe60SDimitry Andric BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
36091bc56edSDimitry Andric if (!BI || BI->isConditional() || BI->getSuccessor(0) != BB)
3612754fe60SDimitry Andric return true;
3622754fe60SDimitry Andric
3633b0f4066SDimitry Andric // If we have seen this instruction before, don't count it again.
364*b5893f02SDimitry Andric if (AggressiveInsts.count(I))
3653ca95b02SDimitry Andric return true;
3663b0f4066SDimitry Andric
367f22ef01cSRoman Divacky // Okay, it looks like the instruction IS in the "condition". Check to
368f22ef01cSRoman Divacky // see if it's a cheap instruction to unconditionally compute, and if it
369f22ef01cSRoman Divacky // only uses stuff defined outside of the condition. If so, hoist it out.
370ff0cc061SDimitry Andric if (!isSafeToSpeculativelyExecute(I))
371f22ef01cSRoman Divacky return false;
372f22ef01cSRoman Divacky
373ff0cc061SDimitry Andric unsigned Cost = ComputeSpeculationCost(I, TTI);
374f22ef01cSRoman Divacky
3757d523365SDimitry Andric // Allow exactly one instruction to be speculated regardless of its cost
3767d523365SDimitry Andric // (as long as it is safe to do so).
3777d523365SDimitry Andric // This is intended to flatten the CFG even if the instruction is a division
3787d523365SDimitry Andric // or other expensive operation. The speculation of an expensive instruction
3797d523365SDimitry Andric // is expected to be undone in CodeGenPrepare if the speculation has not
3807d523365SDimitry Andric // enabled further IR optimizations.
3817d523365SDimitry Andric if (Cost > CostRemaining &&
382*b5893f02SDimitry Andric (!SpeculateOneExpensiveInst || !AggressiveInsts.empty() || Depth > 0))
3833b0f4066SDimitry Andric return false;
3843b0f4066SDimitry Andric
3857d523365SDimitry Andric // Avoid unsigned wrap.
3867d523365SDimitry Andric CostRemaining = (Cost > CostRemaining) ? 0 : CostRemaining - Cost;
3873b0f4066SDimitry Andric
3883b0f4066SDimitry Andric // Okay, we can only really hoist these out if their operands do
3893b0f4066SDimitry Andric // not take us over the cost threshold.
390f22ef01cSRoman Divacky for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
3917d523365SDimitry Andric if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining, TTI,
3927d523365SDimitry Andric Depth + 1))
393f22ef01cSRoman Divacky return false;
394f22ef01cSRoman Divacky // Okay, it's safe to do this! Remember this instruction.
395*b5893f02SDimitry Andric AggressiveInsts.insert(I);
396f22ef01cSRoman Divacky return true;
397f22ef01cSRoman Divacky }
398f22ef01cSRoman Divacky
3993dac3a9bSDimitry Andric /// Extract ConstantInt from value, looking through IntToPtr
400f22ef01cSRoman Divacky /// and PointerNullValue. Return NULL if value is not a constant int.
GetConstantInt(Value * V,const DataLayout & DL)401ff0cc061SDimitry Andric static ConstantInt *GetConstantInt(Value *V, const DataLayout &DL) {
402f22ef01cSRoman Divacky // Normal constant int.
403f22ef01cSRoman Divacky ConstantInt *CI = dyn_cast<ConstantInt>(V);
404ff0cc061SDimitry Andric if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy())
405f22ef01cSRoman Divacky return CI;
406f22ef01cSRoman Divacky
407f22ef01cSRoman Divacky // This is some kind of pointer constant. Turn it into a pointer-sized
408f22ef01cSRoman Divacky // ConstantInt if possible.
409ff0cc061SDimitry Andric IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
410f22ef01cSRoman Divacky
411f22ef01cSRoman Divacky // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
412f22ef01cSRoman Divacky if (isa<ConstantPointerNull>(V))
413f22ef01cSRoman Divacky return ConstantInt::get(PtrTy, 0);
414f22ef01cSRoman Divacky
415f22ef01cSRoman Divacky // IntToPtr const int.
416f22ef01cSRoman Divacky if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
417f22ef01cSRoman Divacky if (CE->getOpcode() == Instruction::IntToPtr)
418f22ef01cSRoman Divacky if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
419f22ef01cSRoman Divacky // The constant is very likely to have the right type already.
420f22ef01cSRoman Divacky if (CI->getType() == PtrTy)
421f22ef01cSRoman Divacky return CI;
422f22ef01cSRoman Divacky else
4233ca95b02SDimitry Andric return cast<ConstantInt>(
4243ca95b02SDimitry Andric ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
425f22ef01cSRoman Divacky }
42691bc56edSDimitry Andric return nullptr;
427f22ef01cSRoman Divacky }
428f22ef01cSRoman Divacky
42939d628a0SDimitry Andric namespace {
4302754fe60SDimitry Andric
43139d628a0SDimitry Andric /// Given a chain of or (||) or and (&&) comparison of a value against a
43239d628a0SDimitry Andric /// constant, this will try to recover the information required for a switch
43339d628a0SDimitry Andric /// structure.
43439d628a0SDimitry Andric /// It will depth-first traverse the chain of comparison, seeking for patterns
43539d628a0SDimitry Andric /// like %a == 12 or %a < 4 and combine them to produce a set of integer
43639d628a0SDimitry Andric /// representing the different cases for the switch.
43739d628a0SDimitry Andric /// Note that if the chain is composed of '||' it will build the set of elements
43839d628a0SDimitry Andric /// that matches the comparisons (i.e. any of this value validate the chain)
43939d628a0SDimitry Andric /// while for a chain of '&&' it will build the set elements that make the test
44039d628a0SDimitry Andric /// fail.
44139d628a0SDimitry Andric struct ConstantComparesGatherer {
442ff0cc061SDimitry Andric const DataLayout &DL;
4432cab237bSDimitry Andric
4442cab237bSDimitry Andric /// Value found for the switch comparison
4452cab237bSDimitry Andric Value *CompValue = nullptr;
4462cab237bSDimitry Andric
4472cab237bSDimitry Andric /// Extra clause to be checked before the switch
4482cab237bSDimitry Andric Value *Extra = nullptr;
4492cab237bSDimitry Andric
4502cab237bSDimitry Andric /// Set of integers to match in switch
4512cab237bSDimitry Andric SmallVector<ConstantInt *, 8> Vals;
4522cab237bSDimitry Andric
4532cab237bSDimitry Andric /// Number of comparisons matched in the and/or chain
4542cab237bSDimitry Andric unsigned UsedICmps = 0;
45539d628a0SDimitry Andric
45639d628a0SDimitry Andric /// Construct and compute the result for the comparison instruction Cond
ConstantComparesGatherer__anone609cfb00211::ConstantComparesGatherer4572cab237bSDimitry Andric ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
458ff0cc061SDimitry Andric gather(Cond);
45939d628a0SDimitry Andric }
46039d628a0SDimitry Andric
461ff0cc061SDimitry Andric ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
46239d628a0SDimitry Andric ConstantComparesGatherer &
463ff0cc061SDimitry Andric operator=(const ConstantComparesGatherer &) = delete;
46439d628a0SDimitry Andric
46539d628a0SDimitry Andric private:
46639d628a0SDimitry Andric /// Try to set the current value used for the comparison, it succeeds only if
46739d628a0SDimitry Andric /// it wasn't set before or if the new value is the same as the old one
setValueOnce__anone609cfb00211::ConstantComparesGatherer46839d628a0SDimitry Andric bool setValueOnce(Value *NewVal) {
4693ca95b02SDimitry Andric if (CompValue && CompValue != NewVal)
4703ca95b02SDimitry Andric return false;
47139d628a0SDimitry Andric CompValue = NewVal;
47239d628a0SDimitry Andric return (CompValue != nullptr);
47339d628a0SDimitry Andric }
47439d628a0SDimitry Andric
47539d628a0SDimitry Andric /// Try to match Instruction "I" as a comparison against a constant and
47639d628a0SDimitry Andric /// populates the array Vals with the set of values that match (or do not
47739d628a0SDimitry Andric /// match depending on isEQ).
47839d628a0SDimitry Andric /// Return false on failure. On success, the Value the comparison matched
47939d628a0SDimitry Andric /// against is placed in CompValue.
48039d628a0SDimitry Andric /// If CompValue is already set, the function is expected to fail if a match
48139d628a0SDimitry Andric /// is found but the value compared to is different.
matchInstruction__anone609cfb00211::ConstantComparesGatherer482ff0cc061SDimitry Andric bool matchInstruction(Instruction *I, bool isEQ) {
4832754fe60SDimitry Andric // If this is an icmp against a constant, handle this as one of the cases.
48439d628a0SDimitry Andric ICmpInst *ICI;
48539d628a0SDimitry Andric ConstantInt *C;
48639d628a0SDimitry Andric if (!((ICI = dyn_cast<ICmpInst>(I)) &&
48739d628a0SDimitry Andric (C = GetConstantInt(I->getOperand(1), DL)))) {
48839d628a0SDimitry Andric return false;
48939d628a0SDimitry Andric }
49039d628a0SDimitry Andric
491f785676fSDimitry Andric Value *RHSVal;
4923ca95b02SDimitry Andric const APInt *RHSC;
493f785676fSDimitry Andric
49439d628a0SDimitry Andric // Pattern match a special case
4953ca95b02SDimitry Andric // (x & ~2^z) == y --> x == y || x == y|2^z
496f785676fSDimitry Andric // This undoes a transformation done by instcombine to fuse 2 compares.
49739d628a0SDimitry Andric if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
4983ca95b02SDimitry Andric // It's a little bit hard to see why the following transformations are
4993ca95b02SDimitry Andric // correct. Here is a CVC3 program to verify them for 64-bit values:
5003ca95b02SDimitry Andric
5013ca95b02SDimitry Andric /*
5023ca95b02SDimitry Andric ONE : BITVECTOR(64) = BVZEROEXTEND(0bin1, 63);
5033ca95b02SDimitry Andric x : BITVECTOR(64);
5043ca95b02SDimitry Andric y : BITVECTOR(64);
5053ca95b02SDimitry Andric z : BITVECTOR(64);
5063ca95b02SDimitry Andric mask : BITVECTOR(64) = BVSHL(ONE, z);
5073ca95b02SDimitry Andric QUERY( (y & ~mask = y) =>
5083ca95b02SDimitry Andric ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
5093ca95b02SDimitry Andric );
5103ca95b02SDimitry Andric QUERY( (y | mask = y) =>
5113ca95b02SDimitry Andric ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
5123ca95b02SDimitry Andric );
5133ca95b02SDimitry Andric */
5143ca95b02SDimitry Andric
5153ca95b02SDimitry Andric // Please note that each pattern must be a dual implication (<--> or
5163ca95b02SDimitry Andric // iff). One directional implication can create spurious matches. If the
5173ca95b02SDimitry Andric // implication is only one-way, an unsatisfiable condition on the left
5183ca95b02SDimitry Andric // side can imply a satisfiable condition on the right side. Dual
5193ca95b02SDimitry Andric // implication ensures that satisfiable conditions are transformed to
5203ca95b02SDimitry Andric // other satisfiable conditions and unsatisfiable conditions are
5213ca95b02SDimitry Andric // transformed to other unsatisfiable conditions.
5223ca95b02SDimitry Andric
5233ca95b02SDimitry Andric // Here is a concrete example of a unsatisfiable condition on the left
5243ca95b02SDimitry Andric // implying a satisfiable condition on the right:
5253ca95b02SDimitry Andric //
5263ca95b02SDimitry Andric // mask = (1 << z)
5273ca95b02SDimitry Andric // (x & ~mask) == y --> (x == y || x == (y | mask))
5283ca95b02SDimitry Andric //
5293ca95b02SDimitry Andric // Substituting y = 3, z = 0 yields:
5303ca95b02SDimitry Andric // (x & -2) == 3 --> (x == 3 || x == 2)
5313ca95b02SDimitry Andric
5323ca95b02SDimitry Andric // Pattern match a special case:
5333ca95b02SDimitry Andric /*
5343ca95b02SDimitry Andric QUERY( (y & ~mask = y) =>
5353ca95b02SDimitry Andric ((x & ~mask = y) <=> (x = y OR x = (y | mask)))
5363ca95b02SDimitry Andric );
5373ca95b02SDimitry Andric */
538f785676fSDimitry Andric if (match(ICI->getOperand(0),
5393ca95b02SDimitry Andric m_And(m_Value(RHSVal), m_APInt(RHSC)))) {
5403ca95b02SDimitry Andric APInt Mask = ~*RHSC;
5413ca95b02SDimitry Andric if (Mask.isPowerOf2() && (C->getValue() & ~Mask) == C->getValue()) {
5423ca95b02SDimitry Andric // If we already have a value for the switch, it has to match!
5433ca95b02SDimitry Andric if (!setValueOnce(RHSVal))
5443ca95b02SDimitry Andric return false;
5453ca95b02SDimitry Andric
5463ca95b02SDimitry Andric Vals.push_back(C);
5473ca95b02SDimitry Andric Vals.push_back(
5483ca95b02SDimitry Andric ConstantInt::get(C->getContext(),
5493ca95b02SDimitry Andric C->getValue() | Mask));
5503ca95b02SDimitry Andric UsedICmps++;
5513ca95b02SDimitry Andric return true;
5523ca95b02SDimitry Andric }
5533ca95b02SDimitry Andric }
5543ca95b02SDimitry Andric
5553ca95b02SDimitry Andric // Pattern match a special case:
5563ca95b02SDimitry Andric /*
5573ca95b02SDimitry Andric QUERY( (y | mask = y) =>
5583ca95b02SDimitry Andric ((x | mask = y) <=> (x = y OR x = (y & ~mask)))
5593ca95b02SDimitry Andric );
5603ca95b02SDimitry Andric */
5613ca95b02SDimitry Andric if (match(ICI->getOperand(0),
5623ca95b02SDimitry Andric m_Or(m_Value(RHSVal), m_APInt(RHSC)))) {
5633ca95b02SDimitry Andric APInt Mask = *RHSC;
5643ca95b02SDimitry Andric if (Mask.isPowerOf2() && (C->getValue() | Mask) == C->getValue()) {
56539d628a0SDimitry Andric // If we already have a value for the switch, it has to match!
56639d628a0SDimitry Andric if (!setValueOnce(RHSVal))
56739d628a0SDimitry Andric return false;
56839d628a0SDimitry Andric
569f785676fSDimitry Andric Vals.push_back(C);
57039d628a0SDimitry Andric Vals.push_back(ConstantInt::get(C->getContext(),
5713ca95b02SDimitry Andric C->getValue() & ~Mask));
572f785676fSDimitry Andric UsedICmps++;
57339d628a0SDimitry Andric return true;
574f785676fSDimitry Andric }
575f785676fSDimitry Andric }
576f785676fSDimitry Andric
57739d628a0SDimitry Andric // If we already have a value for the switch, it has to match!
57839d628a0SDimitry Andric if (!setValueOnce(ICI->getOperand(0)))
57939d628a0SDimitry Andric return false;
58039d628a0SDimitry Andric
5812754fe60SDimitry Andric UsedICmps++;
5822754fe60SDimitry Andric Vals.push_back(C);
58339d628a0SDimitry Andric return ICI->getOperand(0);
584f22ef01cSRoman Divacky }
5852754fe60SDimitry Andric
58639d628a0SDimitry Andric // If we have "x ult 3", for example, then we can add 0,1,2 to the set.
587ff0cc061SDimitry Andric ConstantRange Span = ConstantRange::makeAllowedICmpRegion(
588ff0cc061SDimitry Andric ICI->getPredicate(), C->getValue());
5892754fe60SDimitry Andric
590f785676fSDimitry Andric // Shift the range if the compare is fed by an add. This is the range
591f785676fSDimitry Andric // compare idiom as emitted by instcombine.
59239d628a0SDimitry Andric Value *CandidateVal = I->getOperand(0);
5933ca95b02SDimitry Andric if (match(I->getOperand(0), m_Add(m_Value(RHSVal), m_APInt(RHSC)))) {
5943ca95b02SDimitry Andric Span = Span.subtract(*RHSC);
59539d628a0SDimitry Andric CandidateVal = RHSVal;
59639d628a0SDimitry Andric }
597f785676fSDimitry Andric
59839d628a0SDimitry Andric // If this is an and/!= check, then we are looking to build the set of
59939d628a0SDimitry Andric // value that *don't* pass the and chain. I.e. to turn "x ugt 2" into
6002754fe60SDimitry Andric // x != 0 && x != 1.
6012754fe60SDimitry Andric if (!isEQ)
6022754fe60SDimitry Andric Span = Span.inverse();
6032754fe60SDimitry Andric
6042754fe60SDimitry Andric // If there are a ton of values, we don't want to make a ginormous switch.
6050f5676f4SDimitry Andric if (Span.isSizeLargerThan(8) || Span.isEmptySet()) {
60639d628a0SDimitry Andric return false;
60739d628a0SDimitry Andric }
6082754fe60SDimitry Andric
60939d628a0SDimitry Andric // If we already have a value for the switch, it has to match!
61039d628a0SDimitry Andric if (!setValueOnce(CandidateVal))
61139d628a0SDimitry Andric return false;
61239d628a0SDimitry Andric
61339d628a0SDimitry Andric // Add all values from the range to the set
6142754fe60SDimitry Andric for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
61539d628a0SDimitry Andric Vals.push_back(ConstantInt::get(I->getContext(), Tmp));
61639d628a0SDimitry Andric
6172754fe60SDimitry Andric UsedICmps++;
61839d628a0SDimitry Andric return true;
619f22ef01cSRoman Divacky }
620f22ef01cSRoman Divacky
6213dac3a9bSDimitry Andric /// Given a potentially 'or'd or 'and'd together collection of icmp
62239d628a0SDimitry Andric /// eq/ne/lt/gt instructions that compare a value against a constant, extract
62339d628a0SDimitry Andric /// the value being compared, and stick the list constants into the Vals
62439d628a0SDimitry Andric /// vector.
62539d628a0SDimitry Andric /// One "Extra" case is allowed to differ from the other.
gather__anone609cfb00211::ConstantComparesGatherer626ff0cc061SDimitry Andric void gather(Value *V) {
62739d628a0SDimitry Andric Instruction *I = dyn_cast<Instruction>(V);
62839d628a0SDimitry Andric bool isEQ = (I->getOpcode() == Instruction::Or);
6292754fe60SDimitry Andric
63039d628a0SDimitry Andric // Keep a stack (SmallVector for efficiency) for depth-first traversal
63139d628a0SDimitry Andric SmallVector<Value *, 8> DFT;
6323ca95b02SDimitry Andric SmallPtrSet<Value *, 8> Visited;
63339d628a0SDimitry Andric
63439d628a0SDimitry Andric // Initialize
6353ca95b02SDimitry Andric Visited.insert(V);
63639d628a0SDimitry Andric DFT.push_back(V);
63739d628a0SDimitry Andric
63839d628a0SDimitry Andric while (!DFT.empty()) {
63939d628a0SDimitry Andric V = DFT.pop_back_val();
64039d628a0SDimitry Andric
64139d628a0SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V)) {
64239d628a0SDimitry Andric // If it is a || (or && depending on isEQ), process the operands.
64339d628a0SDimitry Andric if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) {
6443ca95b02SDimitry Andric if (Visited.insert(I->getOperand(1)).second)
64539d628a0SDimitry Andric DFT.push_back(I->getOperand(1));
6463ca95b02SDimitry Andric if (Visited.insert(I->getOperand(0)).second)
64739d628a0SDimitry Andric DFT.push_back(I->getOperand(0));
64839d628a0SDimitry Andric continue;
649f22ef01cSRoman Divacky }
6502754fe60SDimitry Andric
65139d628a0SDimitry Andric // Try to match the current instruction
652ff0cc061SDimitry Andric if (matchInstruction(I, isEQ))
65339d628a0SDimitry Andric // Match succeed, continue the loop
65439d628a0SDimitry Andric continue;
655f22ef01cSRoman Divacky }
6562754fe60SDimitry Andric
65739d628a0SDimitry Andric // One element of the sequence of || (or &&) could not be match as a
65839d628a0SDimitry Andric // comparison against the same value as the others.
65939d628a0SDimitry Andric // We allow only one "Extra" case to be checked before the switch
66039d628a0SDimitry Andric if (!Extra) {
66139d628a0SDimitry Andric Extra = V;
66239d628a0SDimitry Andric continue;
663f22ef01cSRoman Divacky }
66439d628a0SDimitry Andric // Failed to parse a proper sequence, abort now
66539d628a0SDimitry Andric CompValue = nullptr;
66639d628a0SDimitry Andric break;
667f22ef01cSRoman Divacky }
66839d628a0SDimitry Andric }
66939d628a0SDimitry Andric };
670d88c1a5aSDimitry Andric
671d88c1a5aSDimitry Andric } // end anonymous namespace
672f22ef01cSRoman Divacky
EraseTerminatorAndDCECond(Instruction * TI)673*b5893f02SDimitry Andric static void EraseTerminatorAndDCECond(Instruction *TI) {
67491bc56edSDimitry Andric Instruction *Cond = nullptr;
675f22ef01cSRoman Divacky if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
676f22ef01cSRoman Divacky Cond = dyn_cast<Instruction>(SI->getCondition());
677f22ef01cSRoman Divacky } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
678f22ef01cSRoman Divacky if (BI->isConditional())
679f22ef01cSRoman Divacky Cond = dyn_cast<Instruction>(BI->getCondition());
6802754fe60SDimitry Andric } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
6812754fe60SDimitry Andric Cond = dyn_cast<Instruction>(IBI->getAddress());
682f22ef01cSRoman Divacky }
683f22ef01cSRoman Divacky
684f22ef01cSRoman Divacky TI->eraseFromParent();
6853ca95b02SDimitry Andric if (Cond)
6863ca95b02SDimitry Andric RecursivelyDeleteTriviallyDeadInstructions(Cond);
687f22ef01cSRoman Divacky }
688f22ef01cSRoman Divacky
6893dac3a9bSDimitry Andric /// Return true if the specified terminator checks
690f22ef01cSRoman Divacky /// to see if a value is equal to constant integer value.
isValueEqualityComparison(Instruction * TI)691*b5893f02SDimitry Andric Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
69291bc56edSDimitry Andric Value *CV = nullptr;
693f22ef01cSRoman Divacky if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
694f22ef01cSRoman Divacky // Do not permit merging of large switch instructions into their
695f22ef01cSRoman Divacky // predecessors unless there is only one predecessor.
696*b5893f02SDimitry Andric if (!SI->getParent()->hasNPredecessorsOrMore(128 / SI->getNumSuccessors()))
697f22ef01cSRoman Divacky CV = SI->getCondition();
698f22ef01cSRoman Divacky } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
699f22ef01cSRoman Divacky if (BI->isConditional() && BI->getCondition()->hasOneUse())
700ff0cc061SDimitry Andric if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
70191bc56edSDimitry Andric if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
702f22ef01cSRoman Divacky CV = ICI->getOperand(0);
703ff0cc061SDimitry Andric }
704f22ef01cSRoman Divacky
705f22ef01cSRoman Divacky // Unwrap any lossless ptrtoint cast.
706ff0cc061SDimitry Andric if (CV) {
707f785676fSDimitry Andric if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
708f785676fSDimitry Andric Value *Ptr = PTII->getPointerOperand();
709ff0cc061SDimitry Andric if (PTII->getType() == DL.getIntPtrType(Ptr->getType()))
710f785676fSDimitry Andric CV = Ptr;
711f785676fSDimitry Andric }
712f785676fSDimitry Andric }
713f22ef01cSRoman Divacky return CV;
714f22ef01cSRoman Divacky }
715f22ef01cSRoman Divacky
7163dac3a9bSDimitry Andric /// Given a value comparison instruction,
717f22ef01cSRoman Divacky /// decode all of the 'cases' that it represents and return the 'default' block.
GetValueEqualityComparisonCases(Instruction * TI,std::vector<ValueEqualityComparisonCase> & Cases)7183ca95b02SDimitry Andric BasicBlock *SimplifyCFGOpt::GetValueEqualityComparisonCases(
719*b5893f02SDimitry Andric Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
720f22ef01cSRoman Divacky if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
721f22ef01cSRoman Divacky Cases.reserve(SI->getNumCases());
7227a7e6055SDimitry Andric for (auto Case : SI->cases())
7237a7e6055SDimitry Andric Cases.push_back(ValueEqualityComparisonCase(Case.getCaseValue(),
7247a7e6055SDimitry Andric Case.getCaseSuccessor()));
725f22ef01cSRoman Divacky return SI->getDefaultDest();
726f22ef01cSRoman Divacky }
727f22ef01cSRoman Divacky
728f22ef01cSRoman Divacky BranchInst *BI = cast<BranchInst>(TI);
729f22ef01cSRoman Divacky ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
7307ae0e2c9SDimitry Andric BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
7313ca95b02SDimitry Andric Cases.push_back(ValueEqualityComparisonCase(
7323ca95b02SDimitry Andric GetConstantInt(ICI->getOperand(1), DL), Succ));
733f22ef01cSRoman Divacky return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
734f22ef01cSRoman Divacky }
735f22ef01cSRoman Divacky
7363dac3a9bSDimitry Andric /// Given a vector of bb/value pairs, remove any entries
737f22ef01cSRoman Divacky /// in the list that match the specified block.
7383ca95b02SDimitry Andric static void
EliminateBlockCases(BasicBlock * BB,std::vector<ValueEqualityComparisonCase> & Cases)7393ca95b02SDimitry Andric EliminateBlockCases(BasicBlock *BB,
7407ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> &Cases) {
7413861d79fSDimitry Andric Cases.erase(std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
742f22ef01cSRoman Divacky }
743f22ef01cSRoman Divacky
7443dac3a9bSDimitry Andric /// Return true if there are any keys in C1 that exist in C2 as well.
ValuesOverlap(std::vector<ValueEqualityComparisonCase> & C1,std::vector<ValueEqualityComparisonCase> & C2)7453ca95b02SDimitry Andric static bool ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
7467ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> &C2) {
7477ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
748f22ef01cSRoman Divacky
749f22ef01cSRoman Divacky // Make V1 be smaller than V2.
750f22ef01cSRoman Divacky if (V1->size() > V2->size())
751f22ef01cSRoman Divacky std::swap(V1, V2);
752f22ef01cSRoman Divacky
753d88c1a5aSDimitry Andric if (V1->empty())
7543ca95b02SDimitry Andric return false;
755f22ef01cSRoman Divacky if (V1->size() == 1) {
756f22ef01cSRoman Divacky // Just scan V2.
7577ae0e2c9SDimitry Andric ConstantInt *TheVal = (*V1)[0].Value;
758f22ef01cSRoman Divacky for (unsigned i = 0, e = V2->size(); i != e; ++i)
7597ae0e2c9SDimitry Andric if (TheVal == (*V2)[i].Value)
760f22ef01cSRoman Divacky return true;
761f22ef01cSRoman Divacky }
762f22ef01cSRoman Divacky
763f22ef01cSRoman Divacky // Otherwise, just sort both lists and compare element by element.
7642754fe60SDimitry Andric array_pod_sort(V1->begin(), V1->end());
7652754fe60SDimitry Andric array_pod_sort(V2->begin(), V2->end());
766f22ef01cSRoman Divacky unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
767f22ef01cSRoman Divacky while (i1 != e1 && i2 != e2) {
7687ae0e2c9SDimitry Andric if ((*V1)[i1].Value == (*V2)[i2].Value)
769f22ef01cSRoman Divacky return true;
7707ae0e2c9SDimitry Andric if ((*V1)[i1].Value < (*V2)[i2].Value)
771f22ef01cSRoman Divacky ++i1;
772f22ef01cSRoman Divacky else
773f22ef01cSRoman Divacky ++i2;
774f22ef01cSRoman Divacky }
775f22ef01cSRoman Divacky return false;
776f22ef01cSRoman Divacky }
777f22ef01cSRoman Divacky
7782cab237bSDimitry Andric // Set branch weights on SwitchInst. This sets the metadata if there is at
7792cab237bSDimitry Andric // least one non-zero weight.
setBranchWeights(SwitchInst * SI,ArrayRef<uint32_t> Weights)7802cab237bSDimitry Andric static void setBranchWeights(SwitchInst *SI, ArrayRef<uint32_t> Weights) {
7812cab237bSDimitry Andric // Check that there is at least one non-zero weight. Otherwise, pass
7822cab237bSDimitry Andric // nullptr to setMetadata which will erase the existing metadata.
7832cab237bSDimitry Andric MDNode *N = nullptr;
7842cab237bSDimitry Andric if (llvm::any_of(Weights, [](uint32_t W) { return W != 0; }))
7852cab237bSDimitry Andric N = MDBuilder(SI->getParent()->getContext()).createBranchWeights(Weights);
7862cab237bSDimitry Andric SI->setMetadata(LLVMContext::MD_prof, N);
7872cab237bSDimitry Andric }
7882cab237bSDimitry Andric
7892cab237bSDimitry Andric // Similar to the above, but for branch and select instructions that take
7902cab237bSDimitry Andric // exactly 2 weights.
setBranchWeights(Instruction * I,uint32_t TrueWeight,uint32_t FalseWeight)7912cab237bSDimitry Andric static void setBranchWeights(Instruction *I, uint32_t TrueWeight,
7922cab237bSDimitry Andric uint32_t FalseWeight) {
7932cab237bSDimitry Andric assert(isa<BranchInst>(I) || isa<SelectInst>(I));
7942cab237bSDimitry Andric // Check that there is at least one non-zero weight. Otherwise, pass
7952cab237bSDimitry Andric // nullptr to setMetadata which will erase the existing metadata.
7962cab237bSDimitry Andric MDNode *N = nullptr;
7972cab237bSDimitry Andric if (TrueWeight || FalseWeight)
7982cab237bSDimitry Andric N = MDBuilder(I->getParent()->getContext())
7992cab237bSDimitry Andric .createBranchWeights(TrueWeight, FalseWeight);
8002cab237bSDimitry Andric I->setMetadata(LLVMContext::MD_prof, N);
8012cab237bSDimitry Andric }
8022cab237bSDimitry Andric
8033dac3a9bSDimitry Andric /// If TI is known to be a terminator instruction and its block is known to
8043dac3a9bSDimitry Andric /// only have a single predecessor block, check to see if that predecessor is
8053dac3a9bSDimitry Andric /// also a value comparison with the same value, and if that comparison
8063dac3a9bSDimitry Andric /// determines the outcome of this comparison. If so, simplify TI. This does a
8073dac3a9bSDimitry Andric /// very limited form of jump threading.
SimplifyEqualityComparisonWithOnlyPredecessor(Instruction * TI,BasicBlock * Pred,IRBuilder<> & Builder)8083ca95b02SDimitry Andric bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
809*b5893f02SDimitry Andric Instruction *TI, BasicBlock *Pred, IRBuilder<> &Builder) {
810f22ef01cSRoman Divacky Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
8113ca95b02SDimitry Andric if (!PredVal)
8123ca95b02SDimitry Andric return false; // Not a value comparison in predecessor.
813f22ef01cSRoman Divacky
814f22ef01cSRoman Divacky Value *ThisVal = isValueEqualityComparison(TI);
815f22ef01cSRoman Divacky assert(ThisVal && "This isn't a value comparison!!");
8163ca95b02SDimitry Andric if (ThisVal != PredVal)
8173ca95b02SDimitry Andric return false; // Different predicates.
818f22ef01cSRoman Divacky
8193861d79fSDimitry Andric // TODO: Preserve branch weight metadata, similarly to how
8203861d79fSDimitry Andric // FoldValueComparisonIntoPredecessors preserves it.
8213861d79fSDimitry Andric
822f22ef01cSRoman Divacky // Find out information about when control will move from Pred to TI's block.
8237ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> PredCases;
8243ca95b02SDimitry Andric BasicBlock *PredDef =
8253ca95b02SDimitry Andric GetValueEqualityComparisonCases(Pred->getTerminator(), PredCases);
826f22ef01cSRoman Divacky EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
827f22ef01cSRoman Divacky
828f22ef01cSRoman Divacky // Find information about how control leaves this block.
8297ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> ThisCases;
830f22ef01cSRoman Divacky BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
831f22ef01cSRoman Divacky EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
832f22ef01cSRoman Divacky
833f22ef01cSRoman Divacky // If TI's block is the default block from Pred's comparison, potentially
834f22ef01cSRoman Divacky // simplify TI based on this knowledge.
835f22ef01cSRoman Divacky if (PredDef == TI->getParent()) {
836f22ef01cSRoman Divacky // If we are here, we know that the value is none of those cases listed in
837f22ef01cSRoman Divacky // PredCases. If there are any cases in ThisCases that are in PredCases, we
838f22ef01cSRoman Divacky // can simplify TI.
8392754fe60SDimitry Andric if (!ValuesOverlap(PredCases, ThisCases))
8402754fe60SDimitry Andric return false;
8412754fe60SDimitry Andric
842f22ef01cSRoman Divacky if (isa<BranchInst>(TI)) {
843f22ef01cSRoman Divacky // Okay, one of the successors of this condbr is dead. Convert it to a
844f22ef01cSRoman Divacky // uncond br.
845f22ef01cSRoman Divacky assert(ThisCases.size() == 1 && "Branch can only have one case!");
846f22ef01cSRoman Divacky // Insert the new branch.
847bd5abe19SDimitry Andric Instruction *NI = Builder.CreateBr(ThisDef);
848f22ef01cSRoman Divacky (void)NI;
849f22ef01cSRoman Divacky
850f22ef01cSRoman Divacky // Remove PHI node entries for the dead edge.
8517ae0e2c9SDimitry Andric ThisCases[0].Dest->removePredecessor(TI->getParent());
852f22ef01cSRoman Divacky
8534ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
8543ca95b02SDimitry Andric << "Through successor TI: " << *TI << "Leaving: " << *NI
8553ca95b02SDimitry Andric << "\n");
856f22ef01cSRoman Divacky
857*b5893f02SDimitry Andric EraseTerminatorAndDCECond(TI);
858f22ef01cSRoman Divacky return true;
8592754fe60SDimitry Andric }
860f22ef01cSRoman Divacky
861f22ef01cSRoman Divacky SwitchInst *SI = cast<SwitchInst>(TI);
862f22ef01cSRoman Divacky // Okay, TI has cases that are statically dead, prune them away.
863f22ef01cSRoman Divacky SmallPtrSet<Constant *, 16> DeadCases;
864f22ef01cSRoman Divacky for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
8657ae0e2c9SDimitry Andric DeadCases.insert(PredCases[i].Value);
866f22ef01cSRoman Divacky
8674ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
868f22ef01cSRoman Divacky << "Through successor TI: " << *TI);
869f22ef01cSRoman Divacky
8703861d79fSDimitry Andric // Collect branch weights into a vector.
8713861d79fSDimitry Andric SmallVector<uint32_t, 8> Weights;
8723861d79fSDimitry Andric MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
8733861d79fSDimitry Andric bool HasWeight = MD && (MD->getNumOperands() == 2 + SI->getNumCases());
8743861d79fSDimitry Andric if (HasWeight)
8753861d79fSDimitry Andric for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
8763861d79fSDimitry Andric ++MD_i) {
87739d628a0SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
8783861d79fSDimitry Andric Weights.push_back(CI->getValue().getZExtValue());
8793861d79fSDimitry Andric }
880dff0c46cSDimitry Andric for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
881dff0c46cSDimitry Andric --i;
8827a7e6055SDimitry Andric if (DeadCases.count(i->getCaseValue())) {
8833861d79fSDimitry Andric if (HasWeight) {
8847a7e6055SDimitry Andric std::swap(Weights[i->getCaseIndex() + 1], Weights.back());
8853861d79fSDimitry Andric Weights.pop_back();
8863861d79fSDimitry Andric }
8877a7e6055SDimitry Andric i->getCaseSuccessor()->removePredecessor(TI->getParent());
888f22ef01cSRoman Divacky SI->removeCase(i);
889f22ef01cSRoman Divacky }
890dff0c46cSDimitry Andric }
8913861d79fSDimitry Andric if (HasWeight && Weights.size() >= 2)
8922cab237bSDimitry Andric setBranchWeights(SI, Weights);
893f22ef01cSRoman Divacky
8944ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Leaving: " << *TI << "\n");
895f22ef01cSRoman Divacky return true;
896f22ef01cSRoman Divacky }
897f22ef01cSRoman Divacky
898f22ef01cSRoman Divacky // Otherwise, TI's block must correspond to some matched value. Find out
899f22ef01cSRoman Divacky // which value (or set of values) this is.
90091bc56edSDimitry Andric ConstantInt *TIV = nullptr;
901f22ef01cSRoman Divacky BasicBlock *TIBB = TI->getParent();
902f22ef01cSRoman Divacky for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
9037ae0e2c9SDimitry Andric if (PredCases[i].Dest == TIBB) {
90491bc56edSDimitry Andric if (TIV)
905f22ef01cSRoman Divacky return false; // Cannot handle multiple values coming to this block.
9067ae0e2c9SDimitry Andric TIV = PredCases[i].Value;
907f22ef01cSRoman Divacky }
908f22ef01cSRoman Divacky assert(TIV && "No edge from pred to succ?");
909f22ef01cSRoman Divacky
910f22ef01cSRoman Divacky // Okay, we found the one constant that our value can be if we get into TI's
911f22ef01cSRoman Divacky // BB. Find out which successor will unconditionally be branched to.
91291bc56edSDimitry Andric BasicBlock *TheRealDest = nullptr;
913f22ef01cSRoman Divacky for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
9147ae0e2c9SDimitry Andric if (ThisCases[i].Value == TIV) {
9157ae0e2c9SDimitry Andric TheRealDest = ThisCases[i].Dest;
916f22ef01cSRoman Divacky break;
917f22ef01cSRoman Divacky }
918f22ef01cSRoman Divacky
919f22ef01cSRoman Divacky // If not handled by any explicit cases, it is handled by the default case.
9203ca95b02SDimitry Andric if (!TheRealDest)
9213ca95b02SDimitry Andric TheRealDest = ThisDef;
922f22ef01cSRoman Divacky
923f22ef01cSRoman Divacky // Remove PHI node entries for dead edges.
924f22ef01cSRoman Divacky BasicBlock *CheckEdge = TheRealDest;
9253ca95b02SDimitry Andric for (BasicBlock *Succ : successors(TIBB))
9263ca95b02SDimitry Andric if (Succ != CheckEdge)
9273ca95b02SDimitry Andric Succ->removePredecessor(TIBB);
928f22ef01cSRoman Divacky else
92991bc56edSDimitry Andric CheckEdge = nullptr;
930f22ef01cSRoman Divacky
931f22ef01cSRoman Divacky // Insert the new branch.
932bd5abe19SDimitry Andric Instruction *NI = Builder.CreateBr(TheRealDest);
933f22ef01cSRoman Divacky (void)NI;
934f22ef01cSRoman Divacky
9354ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
9363ca95b02SDimitry Andric << "Through successor TI: " << *TI << "Leaving: " << *NI
9373ca95b02SDimitry Andric << "\n");
938f22ef01cSRoman Divacky
939*b5893f02SDimitry Andric EraseTerminatorAndDCECond(TI);
940f22ef01cSRoman Divacky return true;
941f22ef01cSRoman Divacky }
942f22ef01cSRoman Divacky
943f22ef01cSRoman Divacky namespace {
944d88c1a5aSDimitry Andric
9453dac3a9bSDimitry Andric /// This class implements a stable ordering of constant
946f22ef01cSRoman Divacky /// integers that does not depend on their address. This is important for
947f22ef01cSRoman Divacky /// applications that sort ConstantInt's to ensure uniqueness.
948f22ef01cSRoman Divacky struct ConstantIntOrdering {
operator ()__anone609cfb00411::ConstantIntOrdering949f22ef01cSRoman Divacky bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
950f22ef01cSRoman Divacky return LHS->getValue().ult(RHS->getValue());
951f22ef01cSRoman Divacky }
952f22ef01cSRoman Divacky };
953d88c1a5aSDimitry Andric
954d88c1a5aSDimitry Andric } // end anonymous namespace
955f22ef01cSRoman Divacky
ConstantIntSortPredicate(ConstantInt * const * P1,ConstantInt * const * P2)956f785676fSDimitry Andric static int ConstantIntSortPredicate(ConstantInt *const *P1,
957f785676fSDimitry Andric ConstantInt *const *P2) {
958f785676fSDimitry Andric const ConstantInt *LHS = *P1;
959f785676fSDimitry Andric const ConstantInt *RHS = *P2;
9603ca95b02SDimitry Andric if (LHS == RHS)
9612754fe60SDimitry Andric return 0;
9623ca95b02SDimitry Andric return LHS->getValue().ult(RHS->getValue()) ? 1 : -1;
9632754fe60SDimitry Andric }
9642754fe60SDimitry Andric
HasBranchWeights(const Instruction * I)9653861d79fSDimitry Andric static inline bool HasBranchWeights(const Instruction *I) {
9663861d79fSDimitry Andric MDNode *ProfMD = I->getMetadata(LLVMContext::MD_prof);
9673861d79fSDimitry Andric if (ProfMD && ProfMD->getOperand(0))
9683861d79fSDimitry Andric if (MDString *MDS = dyn_cast<MDString>(ProfMD->getOperand(0)))
9693861d79fSDimitry Andric return MDS->getString().equals("branch_weights");
9703861d79fSDimitry Andric
9713861d79fSDimitry Andric return false;
9723861d79fSDimitry Andric }
9733861d79fSDimitry Andric
974*b5893f02SDimitry Andric /// Get Weights of a given terminator, the default weight is at the front
9753861d79fSDimitry Andric /// of the vector. If TI is a conditional eq, we need to swap the branch-weight
9763861d79fSDimitry Andric /// metadata.
GetBranchWeights(Instruction * TI,SmallVectorImpl<uint64_t> & Weights)977*b5893f02SDimitry Andric static void GetBranchWeights(Instruction *TI,
9783861d79fSDimitry Andric SmallVectorImpl<uint64_t> &Weights) {
9793861d79fSDimitry Andric MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
9803861d79fSDimitry Andric assert(MD);
9813861d79fSDimitry Andric for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
98239d628a0SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
9833861d79fSDimitry Andric Weights.push_back(CI->getValue().getZExtValue());
9843861d79fSDimitry Andric }
9853861d79fSDimitry Andric
9863861d79fSDimitry Andric // If TI is a conditional eq, the default case is the false case,
9873861d79fSDimitry Andric // and the corresponding branch-weight data is at index 2. We swap the
9883861d79fSDimitry Andric // default weight to be the first entry.
9893861d79fSDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
9903861d79fSDimitry Andric assert(Weights.size() == 2);
9913861d79fSDimitry Andric ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
9923861d79fSDimitry Andric if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
9933861d79fSDimitry Andric std::swap(Weights.front(), Weights.back());
9943861d79fSDimitry Andric }
9953861d79fSDimitry Andric }
9963861d79fSDimitry Andric
99791bc56edSDimitry Andric /// Keep halving the weights until all can fit in uint32_t.
FitWeights(MutableArrayRef<uint64_t> Weights)9983861d79fSDimitry Andric static void FitWeights(MutableArrayRef<uint64_t> Weights) {
99991bc56edSDimitry Andric uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
100091bc56edSDimitry Andric if (Max > UINT_MAX) {
100191bc56edSDimitry Andric unsigned Offset = 32 - countLeadingZeros(Max);
100291bc56edSDimitry Andric for (uint64_t &I : Weights)
100391bc56edSDimitry Andric I >>= Offset;
10043861d79fSDimitry Andric }
10053861d79fSDimitry Andric }
10063861d79fSDimitry Andric
10073dac3a9bSDimitry Andric /// The specified terminator is a value equality comparison instruction
10083dac3a9bSDimitry Andric /// (either a switch or a branch on "X == c").
1009f22ef01cSRoman Divacky /// See if any of the predecessors of the terminator block are value comparisons
1010f22ef01cSRoman Divacky /// on the same value. If so, and if safe to do so, fold them together.
FoldValueComparisonIntoPredecessors(Instruction * TI,IRBuilder<> & Builder)1011*b5893f02SDimitry Andric bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(Instruction *TI,
1012bd5abe19SDimitry Andric IRBuilder<> &Builder) {
1013f22ef01cSRoman Divacky BasicBlock *BB = TI->getParent();
1014f22ef01cSRoman Divacky Value *CV = isValueEqualityComparison(TI); // CondVal
1015f22ef01cSRoman Divacky assert(CV && "Not a comparison?");
1016f22ef01cSRoman Divacky bool Changed = false;
1017f22ef01cSRoman Divacky
1018f22ef01cSRoman Divacky SmallVector<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
1019f22ef01cSRoman Divacky while (!Preds.empty()) {
1020f22ef01cSRoman Divacky BasicBlock *Pred = Preds.pop_back_val();
1021f22ef01cSRoman Divacky
1022f22ef01cSRoman Divacky // See if the predecessor is a comparison with the same value.
1023*b5893f02SDimitry Andric Instruction *PTI = Pred->getTerminator();
1024f22ef01cSRoman Divacky Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
1025f22ef01cSRoman Divacky
1026d88c1a5aSDimitry Andric if (PCV == CV && TI != PTI) {
1027d88c1a5aSDimitry Andric SmallSetVector<BasicBlock*, 4> FailBlocks;
1028d88c1a5aSDimitry Andric if (!SafeToMergeTerminators(TI, PTI, &FailBlocks)) {
1029d88c1a5aSDimitry Andric for (auto *Succ : FailBlocks) {
10307a7e6055SDimitry Andric if (!SplitBlockPredecessors(Succ, TI->getParent(), ".fold.split"))
1031d88c1a5aSDimitry Andric return false;
1032d88c1a5aSDimitry Andric }
1033d88c1a5aSDimitry Andric }
1034d88c1a5aSDimitry Andric
1035f22ef01cSRoman Divacky // Figure out which 'cases' to copy from SI to PSI.
10367ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> BBCases;
1037f22ef01cSRoman Divacky BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
1038f22ef01cSRoman Divacky
10397ae0e2c9SDimitry Andric std::vector<ValueEqualityComparisonCase> PredCases;
1040f22ef01cSRoman Divacky BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
1041f22ef01cSRoman Divacky
1042f22ef01cSRoman Divacky // Based on whether the default edge from PTI goes to BB or not, fill in
1043f22ef01cSRoman Divacky // PredCases and PredDefault with the new switch cases we would like to
1044f22ef01cSRoman Divacky // build.
1045f22ef01cSRoman Divacky SmallVector<BasicBlock *, 8> NewSuccessors;
1046f22ef01cSRoman Divacky
10473861d79fSDimitry Andric // Update the branch weight metadata along the way
10483861d79fSDimitry Andric SmallVector<uint64_t, 8> Weights;
10493861d79fSDimitry Andric bool PredHasWeights = HasBranchWeights(PTI);
10503861d79fSDimitry Andric bool SuccHasWeights = HasBranchWeights(TI);
10513861d79fSDimitry Andric
10523861d79fSDimitry Andric if (PredHasWeights) {
10533861d79fSDimitry Andric GetBranchWeights(PTI, Weights);
1054139f7f9bSDimitry Andric // branch-weight metadata is inconsistent here.
10553861d79fSDimitry Andric if (Weights.size() != 1 + PredCases.size())
10563861d79fSDimitry Andric PredHasWeights = SuccHasWeights = false;
10573861d79fSDimitry Andric } else if (SuccHasWeights)
10583861d79fSDimitry Andric // If there are no predecessor weights but there are successor weights,
10593861d79fSDimitry Andric // populate Weights with 1, which will later be scaled to the sum of
10603861d79fSDimitry Andric // successor's weights
10613861d79fSDimitry Andric Weights.assign(1 + PredCases.size(), 1);
10623861d79fSDimitry Andric
10633861d79fSDimitry Andric SmallVector<uint64_t, 8> SuccWeights;
10643861d79fSDimitry Andric if (SuccHasWeights) {
10653861d79fSDimitry Andric GetBranchWeights(TI, SuccWeights);
1066139f7f9bSDimitry Andric // branch-weight metadata is inconsistent here.
10673861d79fSDimitry Andric if (SuccWeights.size() != 1 + BBCases.size())
10683861d79fSDimitry Andric PredHasWeights = SuccHasWeights = false;
10693861d79fSDimitry Andric } else if (PredHasWeights)
10703861d79fSDimitry Andric SuccWeights.assign(1 + BBCases.size(), 1);
10713861d79fSDimitry Andric
1072f22ef01cSRoman Divacky if (PredDefault == BB) {
1073f22ef01cSRoman Divacky // If this is the default destination from PTI, only the edges in TI
1074f22ef01cSRoman Divacky // that don't occur in PTI, or that branch to BB will be activated.
1075f22ef01cSRoman Divacky std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1076f22ef01cSRoman Divacky for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
10777ae0e2c9SDimitry Andric if (PredCases[i].Dest != BB)
10787ae0e2c9SDimitry Andric PTIHandled.insert(PredCases[i].Value);
1079f22ef01cSRoman Divacky else {
1080f22ef01cSRoman Divacky // The default destination is BB, we don't need explicit targets.
1081f22ef01cSRoman Divacky std::swap(PredCases[i], PredCases.back());
10823861d79fSDimitry Andric
10833861d79fSDimitry Andric if (PredHasWeights || SuccHasWeights) {
10843861d79fSDimitry Andric // Increase weight for the default case.
10853861d79fSDimitry Andric Weights[0] += Weights[i + 1];
10863861d79fSDimitry Andric std::swap(Weights[i + 1], Weights.back());
10873861d79fSDimitry Andric Weights.pop_back();
10883861d79fSDimitry Andric }
10893861d79fSDimitry Andric
1090f22ef01cSRoman Divacky PredCases.pop_back();
10913ca95b02SDimitry Andric --i;
10923ca95b02SDimitry Andric --e;
1093f22ef01cSRoman Divacky }
1094f22ef01cSRoman Divacky
1095f22ef01cSRoman Divacky // Reconstruct the new switch statement we will be building.
1096f22ef01cSRoman Divacky if (PredDefault != BBDefault) {
1097f22ef01cSRoman Divacky PredDefault->removePredecessor(Pred);
1098f22ef01cSRoman Divacky PredDefault = BBDefault;
1099f22ef01cSRoman Divacky NewSuccessors.push_back(BBDefault);
1100f22ef01cSRoman Divacky }
11013861d79fSDimitry Andric
11023861d79fSDimitry Andric unsigned CasesFromPred = Weights.size();
11033861d79fSDimitry Andric uint64_t ValidTotalSuccWeight = 0;
1104f22ef01cSRoman Divacky for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
11057ae0e2c9SDimitry Andric if (!PTIHandled.count(BBCases[i].Value) &&
11067ae0e2c9SDimitry Andric BBCases[i].Dest != BBDefault) {
1107f22ef01cSRoman Divacky PredCases.push_back(BBCases[i]);
11087ae0e2c9SDimitry Andric NewSuccessors.push_back(BBCases[i].Dest);
11093861d79fSDimitry Andric if (SuccHasWeights || PredHasWeights) {
11103861d79fSDimitry Andric // The default weight is at index 0, so weight for the ith case
11113861d79fSDimitry Andric // should be at index i+1. Scale the cases from successor by
11123861d79fSDimitry Andric // PredDefaultWeight (Weights[0]).
11133861d79fSDimitry Andric Weights.push_back(Weights[0] * SuccWeights[i + 1]);
11143861d79fSDimitry Andric ValidTotalSuccWeight += SuccWeights[i + 1];
11153861d79fSDimitry Andric }
1116f22ef01cSRoman Divacky }
1117f22ef01cSRoman Divacky
11183861d79fSDimitry Andric if (SuccHasWeights || PredHasWeights) {
11193861d79fSDimitry Andric ValidTotalSuccWeight += SuccWeights[0];
11203861d79fSDimitry Andric // Scale the cases from predecessor by ValidTotalSuccWeight.
11213861d79fSDimitry Andric for (unsigned i = 1; i < CasesFromPred; ++i)
11223861d79fSDimitry Andric Weights[i] *= ValidTotalSuccWeight;
11233861d79fSDimitry Andric // Scale the default weight by SuccDefaultWeight (SuccWeights[0]).
11243861d79fSDimitry Andric Weights[0] *= SuccWeights[0];
11253861d79fSDimitry Andric }
1126f22ef01cSRoman Divacky } else {
1127f22ef01cSRoman Divacky // If this is not the default destination from PSI, only the edges
1128f22ef01cSRoman Divacky // in SI that occur in PSI with a destination of BB will be
1129f22ef01cSRoman Divacky // activated.
1130f22ef01cSRoman Divacky std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
11313861d79fSDimitry Andric std::map<ConstantInt *, uint64_t> WeightsForHandled;
1132f22ef01cSRoman Divacky for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
11337ae0e2c9SDimitry Andric if (PredCases[i].Dest == BB) {
11347ae0e2c9SDimitry Andric PTIHandled.insert(PredCases[i].Value);
11353861d79fSDimitry Andric
11363861d79fSDimitry Andric if (PredHasWeights || SuccHasWeights) {
11373861d79fSDimitry Andric WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
11383861d79fSDimitry Andric std::swap(Weights[i + 1], Weights.back());
11393861d79fSDimitry Andric Weights.pop_back();
11403861d79fSDimitry Andric }
11413861d79fSDimitry Andric
1142f22ef01cSRoman Divacky std::swap(PredCases[i], PredCases.back());
1143f22ef01cSRoman Divacky PredCases.pop_back();
11443ca95b02SDimitry Andric --i;
11453ca95b02SDimitry Andric --e;
1146f22ef01cSRoman Divacky }
1147f22ef01cSRoman Divacky
1148f22ef01cSRoman Divacky // Okay, now we know which constants were sent to BB from the
1149f22ef01cSRoman Divacky // predecessor. Figure out where they will all go now.
1150f22ef01cSRoman Divacky for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
11517ae0e2c9SDimitry Andric if (PTIHandled.count(BBCases[i].Value)) {
1152f22ef01cSRoman Divacky // If this is one we are capable of getting...
11533861d79fSDimitry Andric if (PredHasWeights || SuccHasWeights)
11543861d79fSDimitry Andric Weights.push_back(WeightsForHandled[BBCases[i].Value]);
1155f22ef01cSRoman Divacky PredCases.push_back(BBCases[i]);
11567ae0e2c9SDimitry Andric NewSuccessors.push_back(BBCases[i].Dest);
11573ca95b02SDimitry Andric PTIHandled.erase(
11583ca95b02SDimitry Andric BBCases[i].Value); // This constant is taken care of
1159f22ef01cSRoman Divacky }
1160f22ef01cSRoman Divacky
1161f22ef01cSRoman Divacky // If there are any constants vectored to BB that TI doesn't handle,
1162f22ef01cSRoman Divacky // they must go to the default destination of TI.
11633ca95b02SDimitry Andric for (ConstantInt *I : PTIHandled) {
11643861d79fSDimitry Andric if (PredHasWeights || SuccHasWeights)
11653ca95b02SDimitry Andric Weights.push_back(WeightsForHandled[I]);
11663ca95b02SDimitry Andric PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
1167f22ef01cSRoman Divacky NewSuccessors.push_back(BBDefault);
1168f22ef01cSRoman Divacky }
1169f22ef01cSRoman Divacky }
1170f22ef01cSRoman Divacky
1171f22ef01cSRoman Divacky // Okay, at this point, we know which new successor Pred will get. Make
1172f22ef01cSRoman Divacky // sure we update the number of entries in the PHI nodes for these
1173f22ef01cSRoman Divacky // successors.
11747d523365SDimitry Andric for (BasicBlock *NewSuccessor : NewSuccessors)
11757d523365SDimitry Andric AddPredecessorToBlock(NewSuccessor, Pred, BB);
1176f22ef01cSRoman Divacky
1177bd5abe19SDimitry Andric Builder.SetInsertPoint(PTI);
1178f22ef01cSRoman Divacky // Convert pointer to int before we switch.
1179f22ef01cSRoman Divacky if (CV->getType()->isPointerTy()) {
1180ff0cc061SDimitry Andric CV = Builder.CreatePtrToInt(CV, DL.getIntPtrType(CV->getType()),
1181bd5abe19SDimitry Andric "magicptr");
1182f22ef01cSRoman Divacky }
1183f22ef01cSRoman Divacky
1184f22ef01cSRoman Divacky // Now that the successors are updated, create the new Switch instruction.
11853ca95b02SDimitry Andric SwitchInst *NewSI =
11863ca95b02SDimitry Andric Builder.CreateSwitch(CV, PredDefault, PredCases.size());
1187bd5abe19SDimitry Andric NewSI->setDebugLoc(PTI->getDebugLoc());
11887d523365SDimitry Andric for (ValueEqualityComparisonCase &V : PredCases)
11897d523365SDimitry Andric NewSI->addCase(V.Value, V.Dest);
1190f22ef01cSRoman Divacky
11913861d79fSDimitry Andric if (PredHasWeights || SuccHasWeights) {
11923861d79fSDimitry Andric // Halve the weights if any of them cannot fit in an uint32_t
11933861d79fSDimitry Andric FitWeights(Weights);
11943861d79fSDimitry Andric
11953861d79fSDimitry Andric SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
11963861d79fSDimitry Andric
11972cab237bSDimitry Andric setBranchWeights(NewSI, MDWeights);
11983861d79fSDimitry Andric }
11993861d79fSDimitry Andric
1200*b5893f02SDimitry Andric EraseTerminatorAndDCECond(PTI);
1201f22ef01cSRoman Divacky
1202f22ef01cSRoman Divacky // Okay, last check. If BB is still a successor of PSI, then we must
1203f22ef01cSRoman Divacky // have an infinite loop case. If so, add an infinitely looping block
1204f22ef01cSRoman Divacky // to handle the case to preserve the behavior of the code.
120591bc56edSDimitry Andric BasicBlock *InfLoopBlock = nullptr;
1206f22ef01cSRoman Divacky for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
1207f22ef01cSRoman Divacky if (NewSI->getSuccessor(i) == BB) {
120891bc56edSDimitry Andric if (!InfLoopBlock) {
1209f22ef01cSRoman Divacky // Insert it at the end of the function, because it's either code,
1210f22ef01cSRoman Divacky // or it won't matter if it's hot. :)
12113ca95b02SDimitry Andric InfLoopBlock = BasicBlock::Create(BB->getContext(), "infloop",
12123ca95b02SDimitry Andric BB->getParent());
1213f22ef01cSRoman Divacky BranchInst::Create(InfLoopBlock, InfLoopBlock);
1214f22ef01cSRoman Divacky }
1215f22ef01cSRoman Divacky NewSI->setSuccessor(i, InfLoopBlock);
1216f22ef01cSRoman Divacky }
1217f22ef01cSRoman Divacky
1218f22ef01cSRoman Divacky Changed = true;
1219f22ef01cSRoman Divacky }
1220f22ef01cSRoman Divacky }
1221f22ef01cSRoman Divacky return Changed;
1222f22ef01cSRoman Divacky }
1223f22ef01cSRoman Divacky
12243dac3a9bSDimitry Andric // If we would need to insert a select that uses the value of this invoke
12253dac3a9bSDimitry Andric // (comments in HoistThenElseCodeToIf explain why we would need to do this), we
12263dac3a9bSDimitry Andric // can't hoist the invoke, as there is nowhere to put the select in this case.
isSafeToHoistInvoke(BasicBlock * BB1,BasicBlock * BB2,Instruction * I1,Instruction * I2)1227f22ef01cSRoman Divacky static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
1228f22ef01cSRoman Divacky Instruction *I1, Instruction *I2) {
12293ca95b02SDimitry Andric for (BasicBlock *Succ : successors(BB1)) {
123030785c0eSDimitry Andric for (const PHINode &PN : Succ->phis()) {
123130785c0eSDimitry Andric Value *BB1V = PN.getIncomingValueForBlock(BB1);
123230785c0eSDimitry Andric Value *BB2V = PN.getIncomingValueForBlock(BB2);
1233f22ef01cSRoman Divacky if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1234f22ef01cSRoman Divacky return false;
1235f22ef01cSRoman Divacky }
1236f22ef01cSRoman Divacky }
1237f22ef01cSRoman Divacky }
1238f22ef01cSRoman Divacky return true;
1239f22ef01cSRoman Divacky }
1240f22ef01cSRoman Divacky
124139d628a0SDimitry Andric static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I);
124239d628a0SDimitry Andric
12433dac3a9bSDimitry Andric /// Given a conditional branch that goes to BB1 and BB2, hoist any common code
12443dac3a9bSDimitry Andric /// in the two blocks up into the branch block. The caller of this function
12453dac3a9bSDimitry Andric /// guarantees that BI's block dominates BB1 and BB2.
HoistThenElseCodeToIf(BranchInst * BI,const TargetTransformInfo & TTI)1246ff0cc061SDimitry Andric static bool HoistThenElseCodeToIf(BranchInst *BI,
1247ff0cc061SDimitry Andric const TargetTransformInfo &TTI) {
1248f22ef01cSRoman Divacky // This does very trivial matching, with limited scanning, to find identical
1249f22ef01cSRoman Divacky // instructions in the two blocks. In particular, we don't want to get into
1250f22ef01cSRoman Divacky // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As
1251f22ef01cSRoman Divacky // such, we currently just scan for obviously identical instructions in an
1252f22ef01cSRoman Divacky // identical order.
1253f22ef01cSRoman Divacky BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
1254f22ef01cSRoman Divacky BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
1255f22ef01cSRoman Divacky
1256f22ef01cSRoman Divacky BasicBlock::iterator BB1_Itr = BB1->begin();
1257f22ef01cSRoman Divacky BasicBlock::iterator BB2_Itr = BB2->begin();
1258f22ef01cSRoman Divacky
12597d523365SDimitry Andric Instruction *I1 = &*BB1_Itr++, *I2 = &*BB2_Itr++;
12603b0f4066SDimitry Andric // Skip debug info if it is not identical.
12613b0f4066SDimitry Andric DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
12623b0f4066SDimitry Andric DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
12633b0f4066SDimitry Andric if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1264f22ef01cSRoman Divacky while (isa<DbgInfoIntrinsic>(I1))
12657d523365SDimitry Andric I1 = &*BB1_Itr++;
1266f22ef01cSRoman Divacky while (isa<DbgInfoIntrinsic>(I2))
12677d523365SDimitry Andric I2 = &*BB2_Itr++;
12683b0f4066SDimitry Andric }
12693b0f4066SDimitry Andric if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
1270f22ef01cSRoman Divacky (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
1271f22ef01cSRoman Divacky return false;
1272f22ef01cSRoman Divacky
1273f22ef01cSRoman Divacky BasicBlock *BIParent = BI->getParent();
1274f22ef01cSRoman Divacky
1275f785676fSDimitry Andric bool Changed = false;
1276f22ef01cSRoman Divacky do {
1277f22ef01cSRoman Divacky // If we are hoisting the terminator instruction, don't move one (making a
1278f22ef01cSRoman Divacky // broken BB), instead clone it, and remove BI.
1279*b5893f02SDimitry Andric if (I1->isTerminator())
1280f22ef01cSRoman Divacky goto HoistTerminator;
1281f22ef01cSRoman Divacky
128230785c0eSDimitry Andric // If we're going to hoist a call, make sure that the two instructions we're
128330785c0eSDimitry Andric // commoning/hoisting are both marked with musttail, or neither of them is
128430785c0eSDimitry Andric // marked as such. Otherwise, we might end up in a situation where we hoist
128530785c0eSDimitry Andric // from a block where the terminator is a `ret` to a block where the terminator
128630785c0eSDimitry Andric // is a `br`, and `musttail` calls expect to be followed by a return.
128730785c0eSDimitry Andric auto *C1 = dyn_cast<CallInst>(I1);
128830785c0eSDimitry Andric auto *C2 = dyn_cast<CallInst>(I2);
128930785c0eSDimitry Andric if (C1 && C2)
129030785c0eSDimitry Andric if (C1->isMustTailCall() != C2->isMustTailCall())
129130785c0eSDimitry Andric return Changed;
129230785c0eSDimitry Andric
1293ff0cc061SDimitry Andric if (!TTI.isProfitableToHoist(I1) || !TTI.isProfitableToHoist(I2))
1294ff0cc061SDimitry Andric return Changed;
1295ff0cc061SDimitry Andric
12964ba319b5SDimitry Andric if (isa<DbgInfoIntrinsic>(I1) || isa<DbgInfoIntrinsic>(I2)) {
12974ba319b5SDimitry Andric assert (isa<DbgInfoIntrinsic>(I1) && isa<DbgInfoIntrinsic>(I2));
12984ba319b5SDimitry Andric // The debug location is an integral part of a debug info intrinsic
12994ba319b5SDimitry Andric // and can't be separated from it or replaced. Instead of attempting
13004ba319b5SDimitry Andric // to merge locations, simply hoist both copies of the intrinsic.
13014ba319b5SDimitry Andric BIParent->getInstList().splice(BI->getIterator(),
13024ba319b5SDimitry Andric BB1->getInstList(), I1);
13034ba319b5SDimitry Andric BIParent->getInstList().splice(BI->getIterator(),
13044ba319b5SDimitry Andric BB2->getInstList(), I2);
13054ba319b5SDimitry Andric Changed = true;
13064ba319b5SDimitry Andric } else {
1307f22ef01cSRoman Divacky // For a normal instruction, we just move one to right before the branch,
1308f22ef01cSRoman Divacky // then replace all uses of the other with the first. Finally, we remove
1309f22ef01cSRoman Divacky // the now redundant second instruction.
13104ba319b5SDimitry Andric BIParent->getInstList().splice(BI->getIterator(),
13114ba319b5SDimitry Andric BB1->getInstList(), I1);
1312f22ef01cSRoman Divacky if (!I2->use_empty())
1313f22ef01cSRoman Divacky I2->replaceAllUsesWith(I1);
1314d88c1a5aSDimitry Andric I1->andIRFlags(I2);
13153ca95b02SDimitry Andric unsigned KnownIDs[] = {LLVMContext::MD_tbaa,
13163ca95b02SDimitry Andric LLVMContext::MD_range,
13173ca95b02SDimitry Andric LLVMContext::MD_fpmath,
13183ca95b02SDimitry Andric LLVMContext::MD_invariant_load,
13193ca95b02SDimitry Andric LLVMContext::MD_nonnull,
13203ca95b02SDimitry Andric LLVMContext::MD_invariant_group,
13213ca95b02SDimitry Andric LLVMContext::MD_align,
13223ca95b02SDimitry Andric LLVMContext::MD_dereferenceable,
13233ca95b02SDimitry Andric LLVMContext::MD_dereferenceable_or_null,
1324*b5893f02SDimitry Andric LLVMContext::MD_mem_parallel_loop_access,
1325*b5893f02SDimitry Andric LLVMContext::MD_access_group};
1326*b5893f02SDimitry Andric combineMetadata(I1, I2, KnownIDs, true);
1327d88c1a5aSDimitry Andric
1328f1a29dd3SDimitry Andric // I1 and I2 are being combined into a single instruction. Its debug
1329f1a29dd3SDimitry Andric // location is the merged locations of the original instructions.
13302cab237bSDimitry Andric I1->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1331d88c1a5aSDimitry Andric
13322754fe60SDimitry Andric I2->eraseFromParent();
1333f785676fSDimitry Andric Changed = true;
13344ba319b5SDimitry Andric }
1335f22ef01cSRoman Divacky
13367d523365SDimitry Andric I1 = &*BB1_Itr++;
13377d523365SDimitry Andric I2 = &*BB2_Itr++;
13383b0f4066SDimitry Andric // Skip debug info if it is not identical.
13393b0f4066SDimitry Andric DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
13403b0f4066SDimitry Andric DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
13413b0f4066SDimitry Andric if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
1342f22ef01cSRoman Divacky while (isa<DbgInfoIntrinsic>(I1))
13437d523365SDimitry Andric I1 = &*BB1_Itr++;
1344f22ef01cSRoman Divacky while (isa<DbgInfoIntrinsic>(I2))
13457d523365SDimitry Andric I2 = &*BB2_Itr++;
13463b0f4066SDimitry Andric }
13473b0f4066SDimitry Andric } while (I1->isIdenticalToWhenDefined(I2));
1348f22ef01cSRoman Divacky
1349f22ef01cSRoman Divacky return true;
1350f22ef01cSRoman Divacky
1351f22ef01cSRoman Divacky HoistTerminator:
1352f22ef01cSRoman Divacky // It may not be possible to hoist an invoke.
1353f22ef01cSRoman Divacky if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
1354f785676fSDimitry Andric return Changed;
1355f785676fSDimitry Andric
13563ca95b02SDimitry Andric for (BasicBlock *Succ : successors(BB1)) {
135730785c0eSDimitry Andric for (PHINode &PN : Succ->phis()) {
135830785c0eSDimitry Andric Value *BB1V = PN.getIncomingValueForBlock(BB1);
135930785c0eSDimitry Andric Value *BB2V = PN.getIncomingValueForBlock(BB2);
1360f785676fSDimitry Andric if (BB1V == BB2V)
1361f785676fSDimitry Andric continue;
1362f785676fSDimitry Andric
136339d628a0SDimitry Andric // Check for passingValueIsAlwaysUndefined here because we would rather
136439d628a0SDimitry Andric // eliminate undefined control flow then converting it to a select.
136530785c0eSDimitry Andric if (passingValueIsAlwaysUndefined(BB1V, &PN) ||
136630785c0eSDimitry Andric passingValueIsAlwaysUndefined(BB2V, &PN))
136739d628a0SDimitry Andric return Changed;
136839d628a0SDimitry Andric
1369ff0cc061SDimitry Andric if (isa<ConstantExpr>(BB1V) && !isSafeToSpeculativelyExecute(BB1V))
1370f785676fSDimitry Andric return Changed;
1371ff0cc061SDimitry Andric if (isa<ConstantExpr>(BB2V) && !isSafeToSpeculativelyExecute(BB2V))
1372f785676fSDimitry Andric return Changed;
1373f785676fSDimitry Andric }
1374f785676fSDimitry Andric }
1375f22ef01cSRoman Divacky
1376f22ef01cSRoman Divacky // Okay, it is safe to hoist the terminator.
1377f22ef01cSRoman Divacky Instruction *NT = I1->clone();
13787d523365SDimitry Andric BIParent->getInstList().insert(BI->getIterator(), NT);
1379f22ef01cSRoman Divacky if (!NT->getType()->isVoidTy()) {
1380f22ef01cSRoman Divacky I1->replaceAllUsesWith(NT);
1381f22ef01cSRoman Divacky I2->replaceAllUsesWith(NT);
1382f22ef01cSRoman Divacky NT->takeName(I1);
1383f22ef01cSRoman Divacky }
1384f22ef01cSRoman Divacky
1385*b5893f02SDimitry Andric // Ensure terminator gets a debug location, even an unknown one, in case
1386*b5893f02SDimitry Andric // it involves inlinable calls.
1387*b5893f02SDimitry Andric NT->applyMergedLocation(I1->getDebugLoc(), I2->getDebugLoc());
1388*b5893f02SDimitry Andric
1389*b5893f02SDimitry Andric // PHIs created below will adopt NT's merged DebugLoc.
13903ca95b02SDimitry Andric IRBuilder<NoFolder> Builder(NT);
1391*b5893f02SDimitry Andric
1392f22ef01cSRoman Divacky // Hoisting one of the terminators from our successor is a great thing.
1393f22ef01cSRoman Divacky // Unfortunately, the successors of the if/else blocks may have PHI nodes in
1394f22ef01cSRoman Divacky // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
1395f22ef01cSRoman Divacky // nodes, so we insert select instruction to compute the final result.
1396f22ef01cSRoman Divacky std::map<std::pair<Value *, Value *>, SelectInst *> InsertedSelects;
13973ca95b02SDimitry Andric for (BasicBlock *Succ : successors(BB1)) {
139830785c0eSDimitry Andric for (PHINode &PN : Succ->phis()) {
139930785c0eSDimitry Andric Value *BB1V = PN.getIncomingValueForBlock(BB1);
140030785c0eSDimitry Andric Value *BB2V = PN.getIncomingValueForBlock(BB2);
14013ca95b02SDimitry Andric if (BB1V == BB2V)
14023ca95b02SDimitry Andric continue;
14032754fe60SDimitry Andric
1404f22ef01cSRoman Divacky // These values do not agree. Insert a select instruction before NT
1405f22ef01cSRoman Divacky // that determines the right value.
1406f22ef01cSRoman Divacky SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
140791bc56edSDimitry Andric if (!SI)
14083ca95b02SDimitry Andric SI = cast<SelectInst>(
14093ca95b02SDimitry Andric Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
14103ca95b02SDimitry Andric BB1V->getName() + "." + BB2V->getName(), BI));
1411bd5abe19SDimitry Andric
1412f22ef01cSRoman Divacky // Make the PHI node use the select for all incoming values for BB1/BB2
141330785c0eSDimitry Andric for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
141430785c0eSDimitry Andric if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
141530785c0eSDimitry Andric PN.setIncomingValue(i, SI);
1416f22ef01cSRoman Divacky }
1417f22ef01cSRoman Divacky }
1418f22ef01cSRoman Divacky
1419f22ef01cSRoman Divacky // Update any PHI nodes in our new successors.
14203ca95b02SDimitry Andric for (BasicBlock *Succ : successors(BB1))
14213ca95b02SDimitry Andric AddPredecessorToBlock(Succ, BIParent, BB1);
1422f22ef01cSRoman Divacky
1423*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
1424f22ef01cSRoman Divacky return true;
1425f22ef01cSRoman Divacky }
1426f22ef01cSRoman Divacky
1427d88c1a5aSDimitry Andric // All instructions in Insts belong to different blocks that all unconditionally
1428d88c1a5aSDimitry Andric // branch to a common successor. Analyze each instruction and return true if it
1429d88c1a5aSDimitry Andric // would be possible to sink them into their successor, creating one common
1430d88c1a5aSDimitry Andric // instruction instead. For every value that would be required to be provided by
1431d88c1a5aSDimitry Andric // PHI node (because an operand varies in each input block), add to PHIOperands.
canSinkInstructions(ArrayRef<Instruction * > Insts,DenseMap<Instruction *,SmallVector<Value *,4>> & PHIOperands)1432d88c1a5aSDimitry Andric static bool canSinkInstructions(
1433d88c1a5aSDimitry Andric ArrayRef<Instruction *> Insts,
1434d88c1a5aSDimitry Andric DenseMap<Instruction *, SmallVector<Value *, 4>> &PHIOperands) {
1435d88c1a5aSDimitry Andric // Prune out obviously bad instructions to move. Any non-store instruction
1436d88c1a5aSDimitry Andric // must have exactly one use, and we check later that use is by a single,
1437d88c1a5aSDimitry Andric // common PHI instruction in the successor.
1438d88c1a5aSDimitry Andric for (auto *I : Insts) {
1439d88c1a5aSDimitry Andric // These instructions may change or break semantics if moved.
1440d88c1a5aSDimitry Andric if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
1441d88c1a5aSDimitry Andric I->getType()->isTokenTy())
1442d88c1a5aSDimitry Andric return false;
144398221d2eSDimitry Andric
144498221d2eSDimitry Andric // Conservatively return false if I is an inline-asm instruction. Sinking
144598221d2eSDimitry Andric // and merging inline-asm instructions can potentially create arguments
144698221d2eSDimitry Andric // that cannot satisfy the inline-asm constraints.
144798221d2eSDimitry Andric if (const auto *C = dyn_cast<CallInst>(I))
144898221d2eSDimitry Andric if (C->isInlineAsm())
144998221d2eSDimitry Andric return false;
145098221d2eSDimitry Andric
1451d88c1a5aSDimitry Andric // Everything must have only one use too, apart from stores which
1452d88c1a5aSDimitry Andric // have no uses.
1453d88c1a5aSDimitry Andric if (!isa<StoreInst>(I) && !I->hasOneUse())
1454d88c1a5aSDimitry Andric return false;
1455d88c1a5aSDimitry Andric }
1456d88c1a5aSDimitry Andric
1457d88c1a5aSDimitry Andric const Instruction *I0 = Insts.front();
1458d88c1a5aSDimitry Andric for (auto *I : Insts)
1459d88c1a5aSDimitry Andric if (!I->isSameOperationAs(I0))
1460d88c1a5aSDimitry Andric return false;
1461d88c1a5aSDimitry Andric
1462d88c1a5aSDimitry Andric // All instructions in Insts are known to be the same opcode. If they aren't
1463d88c1a5aSDimitry Andric // stores, check the only user of each is a PHI or in the same block as the
1464d88c1a5aSDimitry Andric // instruction, because if a user is in the same block as an instruction
1465d88c1a5aSDimitry Andric // we're contemplating sinking, it must already be determined to be sinkable.
1466d88c1a5aSDimitry Andric if (!isa<StoreInst>(I0)) {
1467d88c1a5aSDimitry Andric auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1468d88c1a5aSDimitry Andric auto *Succ = I0->getParent()->getTerminator()->getSuccessor(0);
1469d88c1a5aSDimitry Andric if (!all_of(Insts, [&PNUse,&Succ](const Instruction *I) -> bool {
1470d88c1a5aSDimitry Andric auto *U = cast<Instruction>(*I->user_begin());
1471d88c1a5aSDimitry Andric return (PNUse &&
1472d88c1a5aSDimitry Andric PNUse->getParent() == Succ &&
1473d88c1a5aSDimitry Andric PNUse->getIncomingValueForBlock(I->getParent()) == I) ||
1474d88c1a5aSDimitry Andric U->getParent() == I->getParent();
1475d88c1a5aSDimitry Andric }))
1476d88c1a5aSDimitry Andric return false;
1477d88c1a5aSDimitry Andric }
1478d88c1a5aSDimitry Andric
1479d88c1a5aSDimitry Andric // Because SROA can't handle speculating stores of selects, try not
1480d88c1a5aSDimitry Andric // to sink loads or stores of allocas when we'd have to create a PHI for
1481d88c1a5aSDimitry Andric // the address operand. Also, because it is likely that loads or stores
1482d88c1a5aSDimitry Andric // of allocas will disappear when Mem2Reg/SROA is run, don't sink them.
1483d88c1a5aSDimitry Andric // This can cause code churn which can have unintended consequences down
1484d88c1a5aSDimitry Andric // the line - see https://llvm.org/bugs/show_bug.cgi?id=30244.
1485d88c1a5aSDimitry Andric // FIXME: This is a workaround for a deficiency in SROA - see
1486d88c1a5aSDimitry Andric // https://llvm.org/bugs/show_bug.cgi?id=30188
14877a7e6055SDimitry Andric if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) {
1488d88c1a5aSDimitry Andric return isa<AllocaInst>(I->getOperand(1));
1489d88c1a5aSDimitry Andric }))
1490d88c1a5aSDimitry Andric return false;
14917a7e6055SDimitry Andric if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) {
1492d88c1a5aSDimitry Andric return isa<AllocaInst>(I->getOperand(0));
1493d88c1a5aSDimitry Andric }))
1494d88c1a5aSDimitry Andric return false;
1495d88c1a5aSDimitry Andric
14967a7e6055SDimitry Andric for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
14977a7e6055SDimitry Andric if (I0->getOperand(OI)->getType()->isTokenTy())
14987a7e6055SDimitry Andric // Don't touch any operand of token type.
14997a7e6055SDimitry Andric return false;
15007a7e6055SDimitry Andric
1501d88c1a5aSDimitry Andric auto SameAsI0 = [&I0, OI](const Instruction *I) {
1502d88c1a5aSDimitry Andric assert(I->getNumOperands() == I0->getNumOperands());
1503d88c1a5aSDimitry Andric return I->getOperand(OI) == I0->getOperand(OI);
1504d88c1a5aSDimitry Andric };
1505d88c1a5aSDimitry Andric if (!all_of(Insts, SameAsI0)) {
1506d88c1a5aSDimitry Andric if (!canReplaceOperandWithVariable(I0, OI))
1507d88c1a5aSDimitry Andric // We can't create a PHI from this GEP.
1508d88c1a5aSDimitry Andric return false;
1509d88c1a5aSDimitry Andric // Don't create indirect calls! The called value is the final operand.
1510d88c1a5aSDimitry Andric if ((isa<CallInst>(I0) || isa<InvokeInst>(I0)) && OI == OE - 1) {
1511d88c1a5aSDimitry Andric // FIXME: if the call was *already* indirect, we should do this.
1512d88c1a5aSDimitry Andric return false;
1513d88c1a5aSDimitry Andric }
1514d88c1a5aSDimitry Andric for (auto *I : Insts)
1515d88c1a5aSDimitry Andric PHIOperands[I].push_back(I->getOperand(OI));
1516d88c1a5aSDimitry Andric }
1517d88c1a5aSDimitry Andric }
1518d88c1a5aSDimitry Andric return true;
1519d88c1a5aSDimitry Andric }
1520d88c1a5aSDimitry Andric
1521d88c1a5aSDimitry Andric // Assuming canSinkLastInstruction(Blocks) has returned true, sink the last
1522d88c1a5aSDimitry Andric // instruction of every block in Blocks to their common successor, commoning
1523d88c1a5aSDimitry Andric // into one instruction.
sinkLastInstruction(ArrayRef<BasicBlock * > Blocks)1524d88c1a5aSDimitry Andric static bool sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
1525d88c1a5aSDimitry Andric auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
1526d88c1a5aSDimitry Andric
1527d88c1a5aSDimitry Andric // canSinkLastInstruction returning true guarantees that every block has at
1528d88c1a5aSDimitry Andric // least one non-terminator instruction.
1529d88c1a5aSDimitry Andric SmallVector<Instruction*,4> Insts;
1530d88c1a5aSDimitry Andric for (auto *BB : Blocks) {
1531d88c1a5aSDimitry Andric Instruction *I = BB->getTerminator();
1532d88c1a5aSDimitry Andric do {
1533d88c1a5aSDimitry Andric I = I->getPrevNode();
1534d88c1a5aSDimitry Andric } while (isa<DbgInfoIntrinsic>(I) && I != &BB->front());
1535d88c1a5aSDimitry Andric if (!isa<DbgInfoIntrinsic>(I))
1536d88c1a5aSDimitry Andric Insts.push_back(I);
1537d88c1a5aSDimitry Andric }
1538d88c1a5aSDimitry Andric
1539d88c1a5aSDimitry Andric // The only checking we need to do now is that all users of all instructions
1540d88c1a5aSDimitry Andric // are the same PHI node. canSinkLastInstruction should have checked this but
1541d88c1a5aSDimitry Andric // it is slightly over-aggressive - it gets confused by commutative instructions
1542d88c1a5aSDimitry Andric // so double-check it here.
1543d88c1a5aSDimitry Andric Instruction *I0 = Insts.front();
1544d88c1a5aSDimitry Andric if (!isa<StoreInst>(I0)) {
1545d88c1a5aSDimitry Andric auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1546d88c1a5aSDimitry Andric if (!all_of(Insts, [&PNUse](const Instruction *I) -> bool {
1547d88c1a5aSDimitry Andric auto *U = cast<Instruction>(*I->user_begin());
1548d88c1a5aSDimitry Andric return U == PNUse;
1549d88c1a5aSDimitry Andric }))
1550d88c1a5aSDimitry Andric return false;
1551d88c1a5aSDimitry Andric }
1552d88c1a5aSDimitry Andric
1553d88c1a5aSDimitry Andric // We don't need to do any more checking here; canSinkLastInstruction should
1554d88c1a5aSDimitry Andric // have done it all for us.
1555d88c1a5aSDimitry Andric SmallVector<Value*, 4> NewOperands;
1556d88c1a5aSDimitry Andric for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
1557d88c1a5aSDimitry Andric // This check is different to that in canSinkLastInstruction. There, we
1558d88c1a5aSDimitry Andric // cared about the global view once simplifycfg (and instcombine) have
1559d88c1a5aSDimitry Andric // completed - it takes into account PHIs that become trivially
1560d88c1a5aSDimitry Andric // simplifiable. However here we need a more local view; if an operand
1561d88c1a5aSDimitry Andric // differs we create a PHI and rely on instcombine to clean up the very
1562d88c1a5aSDimitry Andric // small mess we may make.
1563d88c1a5aSDimitry Andric bool NeedPHI = any_of(Insts, [&I0, O](const Instruction *I) {
1564d88c1a5aSDimitry Andric return I->getOperand(O) != I0->getOperand(O);
1565d88c1a5aSDimitry Andric });
1566d88c1a5aSDimitry Andric if (!NeedPHI) {
1567d88c1a5aSDimitry Andric NewOperands.push_back(I0->getOperand(O));
1568d88c1a5aSDimitry Andric continue;
1569d88c1a5aSDimitry Andric }
1570d88c1a5aSDimitry Andric
1571d88c1a5aSDimitry Andric // Create a new PHI in the successor block and populate it.
1572d88c1a5aSDimitry Andric auto *Op = I0->getOperand(O);
1573d88c1a5aSDimitry Andric assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
1574d88c1a5aSDimitry Andric auto *PN = PHINode::Create(Op->getType(), Insts.size(),
1575d88c1a5aSDimitry Andric Op->getName() + ".sink", &BBEnd->front());
1576d88c1a5aSDimitry Andric for (auto *I : Insts)
1577d88c1a5aSDimitry Andric PN->addIncoming(I->getOperand(O), I->getParent());
1578d88c1a5aSDimitry Andric NewOperands.push_back(PN);
1579d88c1a5aSDimitry Andric }
1580d88c1a5aSDimitry Andric
1581d88c1a5aSDimitry Andric // Arbitrarily use I0 as the new "common" instruction; remap its operands
1582d88c1a5aSDimitry Andric // and move it to the start of the successor block.
1583d88c1a5aSDimitry Andric for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
1584d88c1a5aSDimitry Andric I0->getOperandUse(O).set(NewOperands[O]);
1585d88c1a5aSDimitry Andric I0->moveBefore(&*BBEnd->getFirstInsertionPt());
1586d88c1a5aSDimitry Andric
15878e0f8b8cSDimitry Andric // Update metadata and IR flags, and merge debug locations.
1588d88c1a5aSDimitry Andric for (auto *I : Insts)
1589d88c1a5aSDimitry Andric if (I != I0) {
15902cab237bSDimitry Andric // The debug location for the "common" instruction is the merged locations
15912cab237bSDimitry Andric // of all the commoned instructions. We start with the original location
15922cab237bSDimitry Andric // of the "common" instruction and iteratively merge each location in the
15932cab237bSDimitry Andric // loop below.
15942cab237bSDimitry Andric // This is an N-way merge, which will be inefficient if I0 is a CallInst.
15952cab237bSDimitry Andric // However, as N-way merge for CallInst is rare, so we use simplified API
15962cab237bSDimitry Andric // instead of using complex API for N-way merge.
15972cab237bSDimitry Andric I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
1598*b5893f02SDimitry Andric combineMetadataForCSE(I0, I, true);
1599d88c1a5aSDimitry Andric I0->andIRFlags(I);
1600d88c1a5aSDimitry Andric }
1601d88c1a5aSDimitry Andric
1602d88c1a5aSDimitry Andric if (!isa<StoreInst>(I0)) {
1603d88c1a5aSDimitry Andric // canSinkLastInstruction checked that all instructions were used by
1604d88c1a5aSDimitry Andric // one and only one PHI node. Find that now, RAUW it to our common
1605d88c1a5aSDimitry Andric // instruction and nuke it.
1606d88c1a5aSDimitry Andric assert(I0->hasOneUse());
1607d88c1a5aSDimitry Andric auto *PN = cast<PHINode>(*I0->user_begin());
1608d88c1a5aSDimitry Andric PN->replaceAllUsesWith(I0);
1609d88c1a5aSDimitry Andric PN->eraseFromParent();
1610d88c1a5aSDimitry Andric }
1611d88c1a5aSDimitry Andric
1612d88c1a5aSDimitry Andric // Finally nuke all instructions apart from the common instruction.
1613d88c1a5aSDimitry Andric for (auto *I : Insts)
1614d88c1a5aSDimitry Andric if (I != I0)
1615d88c1a5aSDimitry Andric I->eraseFromParent();
1616d88c1a5aSDimitry Andric
1617d88c1a5aSDimitry Andric return true;
1618d88c1a5aSDimitry Andric }
1619d88c1a5aSDimitry Andric
1620d88c1a5aSDimitry Andric namespace {
1621d88c1a5aSDimitry Andric
1622d88c1a5aSDimitry Andric // LockstepReverseIterator - Iterates through instructions
1623d88c1a5aSDimitry Andric // in a set of blocks in reverse order from the first non-terminator.
1624d88c1a5aSDimitry Andric // For example (assume all blocks have size n):
1625d88c1a5aSDimitry Andric // LockstepReverseIterator I([B1, B2, B3]);
1626d88c1a5aSDimitry Andric // *I-- = [B1[n], B2[n], B3[n]];
1627d88c1a5aSDimitry Andric // *I-- = [B1[n-1], B2[n-1], B3[n-1]];
1628d88c1a5aSDimitry Andric // *I-- = [B1[n-2], B2[n-2], B3[n-2]];
1629d88c1a5aSDimitry Andric // ...
1630d88c1a5aSDimitry Andric class LockstepReverseIterator {
1631d88c1a5aSDimitry Andric ArrayRef<BasicBlock*> Blocks;
1632d88c1a5aSDimitry Andric SmallVector<Instruction*,4> Insts;
1633d88c1a5aSDimitry Andric bool Fail;
16342cab237bSDimitry Andric
1635d88c1a5aSDimitry Andric public:
LockstepReverseIterator(ArrayRef<BasicBlock * > Blocks)16362cab237bSDimitry Andric LockstepReverseIterator(ArrayRef<BasicBlock*> Blocks) : Blocks(Blocks) {
1637d88c1a5aSDimitry Andric reset();
1638d88c1a5aSDimitry Andric }
1639d88c1a5aSDimitry Andric
reset()1640d88c1a5aSDimitry Andric void reset() {
1641d88c1a5aSDimitry Andric Fail = false;
1642d88c1a5aSDimitry Andric Insts.clear();
1643d88c1a5aSDimitry Andric for (auto *BB : Blocks) {
1644d88c1a5aSDimitry Andric Instruction *Inst = BB->getTerminator();
1645d88c1a5aSDimitry Andric for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1646d88c1a5aSDimitry Andric Inst = Inst->getPrevNode();
1647d88c1a5aSDimitry Andric if (!Inst) {
1648d88c1a5aSDimitry Andric // Block wasn't big enough.
1649d88c1a5aSDimitry Andric Fail = true;
1650d88c1a5aSDimitry Andric return;
1651d88c1a5aSDimitry Andric }
1652d88c1a5aSDimitry Andric Insts.push_back(Inst);
1653d88c1a5aSDimitry Andric }
1654d88c1a5aSDimitry Andric }
1655d88c1a5aSDimitry Andric
isValid() const1656d88c1a5aSDimitry Andric bool isValid() const {
1657d88c1a5aSDimitry Andric return !Fail;
1658d88c1a5aSDimitry Andric }
1659d88c1a5aSDimitry Andric
operator --()1660d88c1a5aSDimitry Andric void operator--() {
1661d88c1a5aSDimitry Andric if (Fail)
1662d88c1a5aSDimitry Andric return;
1663d88c1a5aSDimitry Andric for (auto *&Inst : Insts) {
1664d88c1a5aSDimitry Andric for (Inst = Inst->getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1665d88c1a5aSDimitry Andric Inst = Inst->getPrevNode();
1666d88c1a5aSDimitry Andric // Already at beginning of block.
1667d88c1a5aSDimitry Andric if (!Inst) {
1668d88c1a5aSDimitry Andric Fail = true;
1669d88c1a5aSDimitry Andric return;
1670d88c1a5aSDimitry Andric }
1671d88c1a5aSDimitry Andric }
1672d88c1a5aSDimitry Andric }
1673d88c1a5aSDimitry Andric
operator *() const1674d88c1a5aSDimitry Andric ArrayRef<Instruction*> operator * () const {
1675d88c1a5aSDimitry Andric return Insts;
1676d88c1a5aSDimitry Andric }
1677d88c1a5aSDimitry Andric };
1678d88c1a5aSDimitry Andric
1679d88c1a5aSDimitry Andric } // end anonymous namespace
1680d88c1a5aSDimitry Andric
1681da09e106SDimitry Andric /// Check whether BB's predecessors end with unconditional branches. If it is
1682da09e106SDimitry Andric /// true, sink any common code from the predecessors to BB.
1683da09e106SDimitry Andric /// We also allow one predecessor to end with conditional branch (but no more
1684da09e106SDimitry Andric /// than one).
SinkCommonCodeFromPredecessors(BasicBlock * BB)1685da09e106SDimitry Andric static bool SinkCommonCodeFromPredecessors(BasicBlock *BB) {
1686d88c1a5aSDimitry Andric // We support two situations:
1687d88c1a5aSDimitry Andric // (1) all incoming arcs are unconditional
1688d88c1a5aSDimitry Andric // (2) one incoming arc is conditional
1689d88c1a5aSDimitry Andric //
1690d88c1a5aSDimitry Andric // (2) is very common in switch defaults and
1691d88c1a5aSDimitry Andric // else-if patterns;
1692d88c1a5aSDimitry Andric //
1693d88c1a5aSDimitry Andric // if (a) f(1);
1694d88c1a5aSDimitry Andric // else if (b) f(2);
1695d88c1a5aSDimitry Andric //
1696d88c1a5aSDimitry Andric // produces:
1697d88c1a5aSDimitry Andric //
1698d88c1a5aSDimitry Andric // [if]
1699d88c1a5aSDimitry Andric // / \
1700d88c1a5aSDimitry Andric // [f(1)] [if]
1701d88c1a5aSDimitry Andric // | | \
17027a7e6055SDimitry Andric // | | |
1703d88c1a5aSDimitry Andric // | [f(2)]|
1704d88c1a5aSDimitry Andric // \ | /
1705d88c1a5aSDimitry Andric // [ end ]
1706d88c1a5aSDimitry Andric //
1707d88c1a5aSDimitry Andric // [end] has two unconditional predecessor arcs and one conditional. The
1708d88c1a5aSDimitry Andric // conditional refers to the implicit empty 'else' arc. This conditional
1709d88c1a5aSDimitry Andric // arc can also be caused by an empty default block in a switch.
1710d88c1a5aSDimitry Andric //
1711d88c1a5aSDimitry Andric // In this case, we attempt to sink code from all *unconditional* arcs.
1712d88c1a5aSDimitry Andric // If we can sink instructions from these arcs (determined during the scan
1713d88c1a5aSDimitry Andric // phase below) we insert a common successor for all unconditional arcs and
1714d88c1a5aSDimitry Andric // connect that to [end], to enable sinking:
1715d88c1a5aSDimitry Andric //
1716d88c1a5aSDimitry Andric // [if]
1717d88c1a5aSDimitry Andric // / \
1718d88c1a5aSDimitry Andric // [x(1)] [if]
1719d88c1a5aSDimitry Andric // | | \
1720d88c1a5aSDimitry Andric // | | \
1721d88c1a5aSDimitry Andric // | [x(2)] |
1722d88c1a5aSDimitry Andric // \ / |
1723d88c1a5aSDimitry Andric // [sink.split] |
1724d88c1a5aSDimitry Andric // \ /
1725d88c1a5aSDimitry Andric // [ end ]
1726d88c1a5aSDimitry Andric //
1727d88c1a5aSDimitry Andric SmallVector<BasicBlock*,4> UnconditionalPreds;
1728d88c1a5aSDimitry Andric Instruction *Cond = nullptr;
1729da09e106SDimitry Andric for (auto *B : predecessors(BB)) {
1730d88c1a5aSDimitry Andric auto *T = B->getTerminator();
1731d88c1a5aSDimitry Andric if (isa<BranchInst>(T) && cast<BranchInst>(T)->isUnconditional())
1732d88c1a5aSDimitry Andric UnconditionalPreds.push_back(B);
1733d88c1a5aSDimitry Andric else if ((isa<BranchInst>(T) || isa<SwitchInst>(T)) && !Cond)
1734d88c1a5aSDimitry Andric Cond = T;
1735d88c1a5aSDimitry Andric else
17363861d79fSDimitry Andric return false;
17373861d79fSDimitry Andric }
1738d88c1a5aSDimitry Andric if (UnconditionalPreds.size() < 2)
17393861d79fSDimitry Andric return false;
17403861d79fSDimitry Andric
17413861d79fSDimitry Andric bool Changed = false;
1742d88c1a5aSDimitry Andric // We take a two-step approach to tail sinking. First we scan from the end of
1743d88c1a5aSDimitry Andric // each block upwards in lockstep. If the n'th instruction from the end of each
1744d88c1a5aSDimitry Andric // block can be sunk, those instructions are added to ValuesToSink and we
1745d88c1a5aSDimitry Andric // carry on. If we can sink an instruction but need to PHI-merge some operands
1746d88c1a5aSDimitry Andric // (because they're not identical in each instruction) we add these to
1747d88c1a5aSDimitry Andric // PHIOperands.
1748d88c1a5aSDimitry Andric unsigned ScanIdx = 0;
1749d88c1a5aSDimitry Andric SmallPtrSet<Value*,4> InstructionsToSink;
1750d88c1a5aSDimitry Andric DenseMap<Instruction*, SmallVector<Value*,4>> PHIOperands;
1751d88c1a5aSDimitry Andric LockstepReverseIterator LRI(UnconditionalPreds);
1752d88c1a5aSDimitry Andric while (LRI.isValid() &&
1753d88c1a5aSDimitry Andric canSinkInstructions(*LRI, PHIOperands)) {
17544ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SINK: instruction can be sunk: " << *(*LRI)[0]
17554ba319b5SDimitry Andric << "\n");
1756d88c1a5aSDimitry Andric InstructionsToSink.insert((*LRI).begin(), (*LRI).end());
1757d88c1a5aSDimitry Andric ++ScanIdx;
1758d88c1a5aSDimitry Andric --LRI;
1759d88c1a5aSDimitry Andric }
1760d88c1a5aSDimitry Andric
1761d88c1a5aSDimitry Andric auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
1762d88c1a5aSDimitry Andric unsigned NumPHIdValues = 0;
1763d88c1a5aSDimitry Andric for (auto *I : *LRI)
1764d88c1a5aSDimitry Andric for (auto *V : PHIOperands[I])
1765d88c1a5aSDimitry Andric if (InstructionsToSink.count(V) == 0)
1766d88c1a5aSDimitry Andric ++NumPHIdValues;
17674ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SINK: #phid values: " << NumPHIdValues << "\n");
1768d88c1a5aSDimitry Andric unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.size();
1769d88c1a5aSDimitry Andric if ((NumPHIdValues % UnconditionalPreds.size()) != 0)
1770d88c1a5aSDimitry Andric NumPHIInsts++;
1771d88c1a5aSDimitry Andric
1772d88c1a5aSDimitry Andric return NumPHIInsts <= 1;
1773d88c1a5aSDimitry Andric };
1774d88c1a5aSDimitry Andric
1775d88c1a5aSDimitry Andric if (ScanIdx > 0 && Cond) {
1776d88c1a5aSDimitry Andric // Check if we would actually sink anything first! This mutates the CFG and
1777d88c1a5aSDimitry Andric // adds an extra block. The goal in doing this is to allow instructions that
1778d88c1a5aSDimitry Andric // couldn't be sunk before to be sunk - obviously, speculatable instructions
1779d88c1a5aSDimitry Andric // (such as trunc, add) can be sunk and predicated already. So we check that
1780d88c1a5aSDimitry Andric // we're going to sink at least one non-speculatable instruction.
1781d88c1a5aSDimitry Andric LRI.reset();
1782d88c1a5aSDimitry Andric unsigned Idx = 0;
1783d88c1a5aSDimitry Andric bool Profitable = false;
1784d88c1a5aSDimitry Andric while (ProfitableToSinkInstruction(LRI) && Idx < ScanIdx) {
1785d88c1a5aSDimitry Andric if (!isSafeToSpeculativelyExecute((*LRI)[0])) {
1786d88c1a5aSDimitry Andric Profitable = true;
1787d88c1a5aSDimitry Andric break;
1788d88c1a5aSDimitry Andric }
1789d88c1a5aSDimitry Andric --LRI;
1790d88c1a5aSDimitry Andric ++Idx;
1791d88c1a5aSDimitry Andric }
1792d88c1a5aSDimitry Andric if (!Profitable)
1793d88c1a5aSDimitry Andric return false;
1794d88c1a5aSDimitry Andric
17954ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SINK: Splitting edge\n");
1796d88c1a5aSDimitry Andric // We have a conditional edge and we're going to sink some instructions.
1797d88c1a5aSDimitry Andric // Insert a new block postdominating all blocks we're going to sink from.
1798da09e106SDimitry Andric if (!SplitBlockPredecessors(BB, UnconditionalPreds, ".sink.split"))
1799d88c1a5aSDimitry Andric // Edges couldn't be split.
1800d88c1a5aSDimitry Andric return false;
1801d88c1a5aSDimitry Andric Changed = true;
1802d88c1a5aSDimitry Andric }
1803d88c1a5aSDimitry Andric
1804d88c1a5aSDimitry Andric // Now that we've analyzed all potential sinking candidates, perform the
1805d88c1a5aSDimitry Andric // actual sink. We iteratively sink the last non-terminator of the source
1806d88c1a5aSDimitry Andric // blocks into their common successor unless doing so would require too
1807d88c1a5aSDimitry Andric // many PHI instructions to be generated (currently only one PHI is allowed
1808d88c1a5aSDimitry Andric // per sunk instruction).
1809d88c1a5aSDimitry Andric //
1810d88c1a5aSDimitry Andric // We can use InstructionsToSink to discount values needing PHI-merging that will
1811d88c1a5aSDimitry Andric // actually be sunk in a later iteration. This allows us to be more
1812d88c1a5aSDimitry Andric // aggressive in what we sink. This does allow a false positive where we
1813d88c1a5aSDimitry Andric // sink presuming a later value will also be sunk, but stop half way through
1814d88c1a5aSDimitry Andric // and never actually sink it which means we produce more PHIs than intended.
1815d88c1a5aSDimitry Andric // This is unlikely in practice though.
1816d88c1a5aSDimitry Andric for (unsigned SinkIdx = 0; SinkIdx != ScanIdx; ++SinkIdx) {
18174ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SINK: Sink: "
1818d88c1a5aSDimitry Andric << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
1819d88c1a5aSDimitry Andric << "\n");
1820d88c1a5aSDimitry Andric
1821d88c1a5aSDimitry Andric // Because we've sunk every instruction in turn, the current instruction to
1822d88c1a5aSDimitry Andric // sink is always at index 0.
1823d88c1a5aSDimitry Andric LRI.reset();
1824d88c1a5aSDimitry Andric if (!ProfitableToSinkInstruction(LRI)) {
1825d88c1a5aSDimitry Andric // Too many PHIs would be created.
18264ba319b5SDimitry Andric LLVM_DEBUG(
18274ba319b5SDimitry Andric dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
1828d88c1a5aSDimitry Andric break;
1829d88c1a5aSDimitry Andric }
1830d88c1a5aSDimitry Andric
1831d88c1a5aSDimitry Andric if (!sinkLastInstruction(UnconditionalPreds))
18323861d79fSDimitry Andric return Changed;
18333861d79fSDimitry Andric NumSinkCommons++;
18343861d79fSDimitry Andric Changed = true;
18353861d79fSDimitry Andric }
18363861d79fSDimitry Andric return Changed;
18373861d79fSDimitry Andric }
18383861d79fSDimitry Andric
18394ba319b5SDimitry Andric /// Determine if we can hoist sink a sole store instruction out of a
1840284c1978SDimitry Andric /// conditional block.
1841284c1978SDimitry Andric ///
1842284c1978SDimitry Andric /// We are looking for code like the following:
1843284c1978SDimitry Andric /// BrBB:
1844284c1978SDimitry Andric /// store i32 %add, i32* %arrayidx2
1845284c1978SDimitry Andric /// ... // No other stores or function calls (we could be calling a memory
1846284c1978SDimitry Andric /// ... // function).
1847284c1978SDimitry Andric /// %cmp = icmp ult %x, %y
1848284c1978SDimitry Andric /// br i1 %cmp, label %EndBB, label %ThenBB
1849284c1978SDimitry Andric /// ThenBB:
1850284c1978SDimitry Andric /// store i32 %add5, i32* %arrayidx2
1851284c1978SDimitry Andric /// br label EndBB
1852284c1978SDimitry Andric /// EndBB:
1853284c1978SDimitry Andric /// ...
1854284c1978SDimitry Andric /// We are going to transform this into:
1855284c1978SDimitry Andric /// BrBB:
1856284c1978SDimitry Andric /// store i32 %add, i32* %arrayidx2
1857284c1978SDimitry Andric /// ... //
1858284c1978SDimitry Andric /// %cmp = icmp ult %x, %y
1859284c1978SDimitry Andric /// %add.add5 = select i1 %cmp, i32 %add, %add5
1860284c1978SDimitry Andric /// store i32 %add.add5, i32* %arrayidx2
1861284c1978SDimitry Andric /// ...
1862284c1978SDimitry Andric ///
1863284c1978SDimitry Andric /// \return The pointer to the value of the previous store if the store can be
1864284c1978SDimitry Andric /// hoisted into the predecessor block. 0 otherwise.
isSafeToSpeculateStore(Instruction * I,BasicBlock * BrBB,BasicBlock * StoreBB,BasicBlock * EndBB)1865f785676fSDimitry Andric static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
1866284c1978SDimitry Andric BasicBlock *StoreBB, BasicBlock *EndBB) {
1867284c1978SDimitry Andric StoreInst *StoreToHoist = dyn_cast<StoreInst>(I);
1868284c1978SDimitry Andric if (!StoreToHoist)
186991bc56edSDimitry Andric return nullptr;
1870284c1978SDimitry Andric
1871284c1978SDimitry Andric // Volatile or atomic.
1872284c1978SDimitry Andric if (!StoreToHoist->isSimple())
187391bc56edSDimitry Andric return nullptr;
1874284c1978SDimitry Andric
1875284c1978SDimitry Andric Value *StorePtr = StoreToHoist->getPointerOperand();
1876284c1978SDimitry Andric
1877284c1978SDimitry Andric // Look for a store to the same pointer in BrBB.
18783ca95b02SDimitry Andric unsigned MaxNumInstToLookAt = 9;
18794ba319b5SDimitry Andric for (Instruction &CurI : reverse(BrBB->instructionsWithoutDebug())) {
18803ca95b02SDimitry Andric if (!MaxNumInstToLookAt)
18813ca95b02SDimitry Andric break;
18823ca95b02SDimitry Andric --MaxNumInstToLookAt;
1883284c1978SDimitry Andric
1884d88c1a5aSDimitry Andric // Could be calling an instruction that affects memory like free().
18853ca95b02SDimitry Andric if (CurI.mayHaveSideEffects() && !isa<StoreInst>(CurI))
188691bc56edSDimitry Andric return nullptr;
1887284c1978SDimitry Andric
18883ca95b02SDimitry Andric if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
1889284c1978SDimitry Andric // Found the previous store make sure it stores to the same location.
18903ca95b02SDimitry Andric if (SI->getPointerOperand() == StorePtr)
1891284c1978SDimitry Andric // Found the previous store, return its value operand.
1892284c1978SDimitry Andric return SI->getValueOperand();
189391bc56edSDimitry Andric return nullptr; // Unknown store.
1894284c1978SDimitry Andric }
18953ca95b02SDimitry Andric }
1896284c1978SDimitry Andric
189791bc56edSDimitry Andric return nullptr;
1898284c1978SDimitry Andric }
1899284c1978SDimitry Andric
19004ba319b5SDimitry Andric /// Speculate a conditional basic block flattening the CFG.
1901dff0c46cSDimitry Andric ///
1902139f7f9bSDimitry Andric /// Note that this is a very risky transform currently. Speculating
1903139f7f9bSDimitry Andric /// instructions like this is most often not desirable. Instead, there is an MI
1904139f7f9bSDimitry Andric /// pass which can do it with full awareness of the resource constraints.
1905139f7f9bSDimitry Andric /// However, some cases are "obvious" and we should do directly. An example of
1906139f7f9bSDimitry Andric /// this is speculating a single, reasonably cheap instruction.
1907139f7f9bSDimitry Andric ///
1908139f7f9bSDimitry Andric /// There is only one distinct advantage to flattening the CFG at the IR level:
1909139f7f9bSDimitry Andric /// it makes very common but simplistic optimizations such as are common in
1910139f7f9bSDimitry Andric /// instcombine and the DAG combiner more powerful by removing CFG edges and
1911139f7f9bSDimitry Andric /// modeling their effects with easier to reason about SSA value graphs.
1912139f7f9bSDimitry Andric ///
1913139f7f9bSDimitry Andric ///
1914139f7f9bSDimitry Andric /// An illustration of this transform is turning this IR:
1915139f7f9bSDimitry Andric /// \code
1916dff0c46cSDimitry Andric /// BB:
1917139f7f9bSDimitry Andric /// %cmp = icmp ult %x, %y
1918139f7f9bSDimitry Andric /// br i1 %cmp, label %EndBB, label %ThenBB
1919139f7f9bSDimitry Andric /// ThenBB:
1920139f7f9bSDimitry Andric /// %sub = sub %x, %y
1921dff0c46cSDimitry Andric /// br label BB2
1922139f7f9bSDimitry Andric /// EndBB:
1923139f7f9bSDimitry Andric /// %phi = phi [ %sub, %ThenBB ], [ 0, %EndBB ]
1924139f7f9bSDimitry Andric /// ...
1925139f7f9bSDimitry Andric /// \endcode
1926139f7f9bSDimitry Andric ///
1927139f7f9bSDimitry Andric /// Into this IR:
1928139f7f9bSDimitry Andric /// \code
1929dff0c46cSDimitry Andric /// BB:
1930139f7f9bSDimitry Andric /// %cmp = icmp ult %x, %y
1931139f7f9bSDimitry Andric /// %sub = sub %x, %y
1932139f7f9bSDimitry Andric /// %cond = select i1 %cmp, 0, %sub
1933139f7f9bSDimitry Andric /// ...
1934139f7f9bSDimitry Andric /// \endcode
1935139f7f9bSDimitry Andric ///
1936139f7f9bSDimitry Andric /// \returns true if the conditional block is removed.
SpeculativelyExecuteBB(BranchInst * BI,BasicBlock * ThenBB,const TargetTransformInfo & TTI)193791bc56edSDimitry Andric static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,
1938ff0cc061SDimitry Andric const TargetTransformInfo &TTI) {
1939f22ef01cSRoman Divacky // Be conservative for now. FP select instruction can often be expensive.
1940f22ef01cSRoman Divacky Value *BrCond = BI->getCondition();
19412754fe60SDimitry Andric if (isa<FCmpInst>(BrCond))
1942f22ef01cSRoman Divacky return false;
1943f22ef01cSRoman Divacky
1944139f7f9bSDimitry Andric BasicBlock *BB = BI->getParent();
1945139f7f9bSDimitry Andric BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
1946139f7f9bSDimitry Andric
1947139f7f9bSDimitry Andric // If ThenBB is actually on the false edge of the conditional branch, remember
1948f22ef01cSRoman Divacky // to swap the select operands later.
1949f22ef01cSRoman Divacky bool Invert = false;
1950139f7f9bSDimitry Andric if (ThenBB != BI->getSuccessor(0)) {
1951139f7f9bSDimitry Andric assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
1952f22ef01cSRoman Divacky Invert = true;
1953f22ef01cSRoman Divacky }
1954139f7f9bSDimitry Andric assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
1955f22ef01cSRoman Divacky
1956*b5893f02SDimitry Andric // Keep a count of how many times instructions are used within ThenBB when
1957*b5893f02SDimitry Andric // they are candidates for sinking into ThenBB. Specifically:
1958139f7f9bSDimitry Andric // - They are defined in BB, and
1959139f7f9bSDimitry Andric // - They have no side effects, and
1960*b5893f02SDimitry Andric // - All of their uses are in ThenBB.
1961139f7f9bSDimitry Andric SmallDenseMap<Instruction *, unsigned, 4> SinkCandidateUseCounts;
1962dff0c46cSDimitry Andric
19632cab237bSDimitry Andric SmallVector<Instruction *, 4> SpeculatedDbgIntrinsics;
19642cab237bSDimitry Andric
1965139f7f9bSDimitry Andric unsigned SpeculationCost = 0;
196691bc56edSDimitry Andric Value *SpeculatedStoreValue = nullptr;
196791bc56edSDimitry Andric StoreInst *SpeculatedStore = nullptr;
1968139f7f9bSDimitry Andric for (BasicBlock::iterator BBI = ThenBB->begin(),
196991bc56edSDimitry Andric BBE = std::prev(ThenBB->end());
1970139f7f9bSDimitry Andric BBI != BBE; ++BBI) {
19717d523365SDimitry Andric Instruction *I = &*BBI;
1972139f7f9bSDimitry Andric // Skip debug info.
19732cab237bSDimitry Andric if (isa<DbgInfoIntrinsic>(I)) {
19742cab237bSDimitry Andric SpeculatedDbgIntrinsics.push_back(I);
1975dff0c46cSDimitry Andric continue;
19762cab237bSDimitry Andric }
1977dff0c46cSDimitry Andric
1978ff0cc061SDimitry Andric // Only speculatively execute a single instruction (not counting the
1979139f7f9bSDimitry Andric // terminator) for now.
1980139f7f9bSDimitry Andric ++SpeculationCost;
1981139f7f9bSDimitry Andric if (SpeculationCost > 1)
1982f22ef01cSRoman Divacky return false;
1983139f7f9bSDimitry Andric
1984139f7f9bSDimitry Andric // Don't hoist the instruction if it's unsafe or expensive.
1985ff0cc061SDimitry Andric if (!isSafeToSpeculativelyExecute(I) &&
1986ff0cc061SDimitry Andric !(HoistCondStores && (SpeculatedStoreValue = isSafeToSpeculateStore(
1987ff0cc061SDimitry Andric I, BB, ThenBB, EndBB))))
1988139f7f9bSDimitry Andric return false;
1989284c1978SDimitry Andric if (!SpeculatedStoreValue &&
1990ff0cc061SDimitry Andric ComputeSpeculationCost(I, TTI) >
1991ff0cc061SDimitry Andric PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
1992139f7f9bSDimitry Andric return false;
1993139f7f9bSDimitry Andric
1994284c1978SDimitry Andric // Store the store speculation candidate.
1995284c1978SDimitry Andric if (SpeculatedStoreValue)
1996284c1978SDimitry Andric SpeculatedStore = cast<StoreInst>(I);
1997284c1978SDimitry Andric
1998139f7f9bSDimitry Andric // Do not hoist the instruction if any of its operands are defined but not
1999284c1978SDimitry Andric // used in BB. The transformation will prevent the operand from
2000139f7f9bSDimitry Andric // being sunk into the use block.
20013ca95b02SDimitry Andric for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
2002139f7f9bSDimitry Andric Instruction *OpI = dyn_cast<Instruction>(*i);
20033ca95b02SDimitry Andric if (!OpI || OpI->getParent() != BB || OpI->mayHaveSideEffects())
2004139f7f9bSDimitry Andric continue; // Not a candidate for sinking.
2005139f7f9bSDimitry Andric
2006139f7f9bSDimitry Andric ++SinkCandidateUseCounts[OpI];
2007139f7f9bSDimitry Andric }
2008139f7f9bSDimitry Andric }
2009139f7f9bSDimitry Andric
2010*b5893f02SDimitry Andric // Consider any sink candidates which are only used in ThenBB as costs for
2011139f7f9bSDimitry Andric // speculation. Note, while we iterate over a DenseMap here, we are summing
2012139f7f9bSDimitry Andric // and so iteration order isn't significant.
20133ca95b02SDimitry Andric for (SmallDenseMap<Instruction *, unsigned, 4>::iterator
20143ca95b02SDimitry Andric I = SinkCandidateUseCounts.begin(),
20153ca95b02SDimitry Andric E = SinkCandidateUseCounts.end();
2016139f7f9bSDimitry Andric I != E; ++I)
2017*b5893f02SDimitry Andric if (I->first->hasNUses(I->second)) {
2018139f7f9bSDimitry Andric ++SpeculationCost;
2019139f7f9bSDimitry Andric if (SpeculationCost > 1)
2020139f7f9bSDimitry Andric return false;
2021139f7f9bSDimitry Andric }
2022139f7f9bSDimitry Andric
2023139f7f9bSDimitry Andric // Check that the PHI nodes can be converted to selects.
2024139f7f9bSDimitry Andric bool HaveRewritablePHIs = false;
202530785c0eSDimitry Andric for (PHINode &PN : EndBB->phis()) {
202630785c0eSDimitry Andric Value *OrigV = PN.getIncomingValueForBlock(BB);
202730785c0eSDimitry Andric Value *ThenV = PN.getIncomingValueForBlock(ThenBB);
2028139f7f9bSDimitry Andric
2029f785676fSDimitry Andric // FIXME: Try to remove some of the duplication with HoistThenElseCodeToIf.
2030139f7f9bSDimitry Andric // Skip PHIs which are trivial.
2031139f7f9bSDimitry Andric if (ThenV == OrigV)
2032139f7f9bSDimitry Andric continue;
2033139f7f9bSDimitry Andric
203439d628a0SDimitry Andric // Don't convert to selects if we could remove undefined behavior instead.
203530785c0eSDimitry Andric if (passingValueIsAlwaysUndefined(OrigV, &PN) ||
203630785c0eSDimitry Andric passingValueIsAlwaysUndefined(ThenV, &PN))
203739d628a0SDimitry Andric return false;
203839d628a0SDimitry Andric
2039139f7f9bSDimitry Andric HaveRewritablePHIs = true;
2040f785676fSDimitry Andric ConstantExpr *OrigCE = dyn_cast<ConstantExpr>(OrigV);
2041f785676fSDimitry Andric ConstantExpr *ThenCE = dyn_cast<ConstantExpr>(ThenV);
2042f785676fSDimitry Andric if (!OrigCE && !ThenCE)
2043139f7f9bSDimitry Andric continue; // Known safe and cheap.
2044139f7f9bSDimitry Andric
2045ff0cc061SDimitry Andric if ((ThenCE && !isSafeToSpeculativelyExecute(ThenCE)) ||
2046ff0cc061SDimitry Andric (OrigCE && !isSafeToSpeculativelyExecute(OrigCE)))
2047dff0c46cSDimitry Andric return false;
2048ff0cc061SDimitry Andric unsigned OrigCost = OrigCE ? ComputeSpeculationCost(OrigCE, TTI) : 0;
2049ff0cc061SDimitry Andric unsigned ThenCost = ThenCE ? ComputeSpeculationCost(ThenCE, TTI) : 0;
20503ca95b02SDimitry Andric unsigned MaxCost =
20513ca95b02SDimitry Andric 2 * PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic;
2052ff0cc061SDimitry Andric if (OrigCost + ThenCost > MaxCost)
2053f22ef01cSRoman Divacky return false;
2054f22ef01cSRoman Divacky
2055139f7f9bSDimitry Andric // Account for the cost of an unfolded ConstantExpr which could end up
2056139f7f9bSDimitry Andric // getting expanded into Instructions.
2057139f7f9bSDimitry Andric // FIXME: This doesn't account for how many operations are combined in the
2058139f7f9bSDimitry Andric // constant expression.
2059139f7f9bSDimitry Andric ++SpeculationCost;
2060139f7f9bSDimitry Andric if (SpeculationCost > 1)
2061139f7f9bSDimitry Andric return false;
2062f22ef01cSRoman Divacky }
2063dff0c46cSDimitry Andric
2064dff0c46cSDimitry Andric // If there are no PHIs to process, bail early. This helps ensure idempotence
2065dff0c46cSDimitry Andric // as well.
2066284c1978SDimitry Andric if (!HaveRewritablePHIs && !(HoistCondStores && SpeculatedStoreValue))
2067dff0c46cSDimitry Andric return false;
2068dff0c46cSDimitry Andric
2069dff0c46cSDimitry Andric // If we get here, we can hoist the instruction and if-convert.
20704ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
2071dff0c46cSDimitry Andric
2072284c1978SDimitry Andric // Insert a select of the value of the speculated store.
2073284c1978SDimitry Andric if (SpeculatedStoreValue) {
20743ca95b02SDimitry Andric IRBuilder<NoFolder> Builder(BI);
2075284c1978SDimitry Andric Value *TrueV = SpeculatedStore->getValueOperand();
2076284c1978SDimitry Andric Value *FalseV = SpeculatedStoreValue;
2077284c1978SDimitry Andric if (Invert)
2078284c1978SDimitry Andric std::swap(TrueV, FalseV);
20793ca95b02SDimitry Andric Value *S = Builder.CreateSelect(
20802cab237bSDimitry Andric BrCond, TrueV, FalseV, "spec.store.select", BI);
2081284c1978SDimitry Andric SpeculatedStore->setOperand(0, S);
20822cab237bSDimitry Andric SpeculatedStore->applyMergedLocation(BI->getDebugLoc(),
20832cab237bSDimitry Andric SpeculatedStore->getDebugLoc());
2084284c1978SDimitry Andric }
2085284c1978SDimitry Andric
20867d523365SDimitry Andric // Metadata can be dependent on the condition we are hoisting above.
20877d523365SDimitry Andric // Conservatively strip all metadata on the instruction.
20887d523365SDimitry Andric for (auto &I : *ThenBB)
20897d523365SDimitry Andric I.dropUnknownNonDebugMetadata();
20907d523365SDimitry Andric
2091139f7f9bSDimitry Andric // Hoist the instructions.
20927d523365SDimitry Andric BB->getInstList().splice(BI->getIterator(), ThenBB->getInstList(),
20937d523365SDimitry Andric ThenBB->begin(), std::prev(ThenBB->end()));
2094dff0c46cSDimitry Andric
2095dff0c46cSDimitry Andric // Insert selects and rewrite the PHI operands.
20963ca95b02SDimitry Andric IRBuilder<NoFolder> Builder(BI);
209730785c0eSDimitry Andric for (PHINode &PN : EndBB->phis()) {
209830785c0eSDimitry Andric unsigned OrigI = PN.getBasicBlockIndex(BB);
209930785c0eSDimitry Andric unsigned ThenI = PN.getBasicBlockIndex(ThenBB);
210030785c0eSDimitry Andric Value *OrigV = PN.getIncomingValue(OrigI);
210130785c0eSDimitry Andric Value *ThenV = PN.getIncomingValue(ThenI);
2102139f7f9bSDimitry Andric
2103139f7f9bSDimitry Andric // Skip PHIs which are trivial.
2104139f7f9bSDimitry Andric if (OrigV == ThenV)
2105139f7f9bSDimitry Andric continue;
2106f22ef01cSRoman Divacky
2107f22ef01cSRoman Divacky // Create a select whose true value is the speculatively executed value and
2108139f7f9bSDimitry Andric // false value is the preexisting value. Swap them if the branch
2109139f7f9bSDimitry Andric // destinations were inverted.
2110139f7f9bSDimitry Andric Value *TrueV = ThenV, *FalseV = OrigV;
2111f22ef01cSRoman Divacky if (Invert)
2112139f7f9bSDimitry Andric std::swap(TrueV, FalseV);
21133ca95b02SDimitry Andric Value *V = Builder.CreateSelect(
21142cab237bSDimitry Andric BrCond, TrueV, FalseV, "spec.select", BI);
211530785c0eSDimitry Andric PN.setIncomingValue(OrigI, V);
211630785c0eSDimitry Andric PN.setIncomingValue(ThenI, V);
2117f22ef01cSRoman Divacky }
2118f22ef01cSRoman Divacky
21192cab237bSDimitry Andric // Remove speculated dbg intrinsics.
21202cab237bSDimitry Andric // FIXME: Is it possible to do this in a more elegant way? Moving/merging the
21212cab237bSDimitry Andric // dbg value for the different flows and inserting it after the select.
21222cab237bSDimitry Andric for (Instruction *I : SpeculatedDbgIntrinsics)
21232cab237bSDimitry Andric I->eraseFromParent();
21242cab237bSDimitry Andric
2125f22ef01cSRoman Divacky ++NumSpeculations;
2126f22ef01cSRoman Divacky return true;
2127f22ef01cSRoman Divacky }
2128f22ef01cSRoman Divacky
21293dac3a9bSDimitry Andric /// Return true if we can thread a branch across this block.
BlockIsSimpleEnoughToThreadThrough(BasicBlock * BB)2130f22ef01cSRoman Divacky static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
2131f22ef01cSRoman Divacky unsigned Size = 0;
2132f22ef01cSRoman Divacky
21334ba319b5SDimitry Andric for (Instruction &I : BB->instructionsWithoutDebug()) {
21343ca95b02SDimitry Andric if (Size > 10)
21353ca95b02SDimitry Andric return false; // Don't clone large BB's.
2136f22ef01cSRoman Divacky ++Size;
2137f22ef01cSRoman Divacky
2138f22ef01cSRoman Divacky // We can only support instructions that do not define values that are
2139f22ef01cSRoman Divacky // live outside of the current basic block.
21404ba319b5SDimitry Andric for (User *U : I.users()) {
214191bc56edSDimitry Andric Instruction *UI = cast<Instruction>(U);
21423ca95b02SDimitry Andric if (UI->getParent() != BB || isa<PHINode>(UI))
21433ca95b02SDimitry Andric return false;
2144f22ef01cSRoman Divacky }
2145f22ef01cSRoman Divacky
2146f22ef01cSRoman Divacky // Looks ok, continue checking.
2147f22ef01cSRoman Divacky }
2148f22ef01cSRoman Divacky
2149f22ef01cSRoman Divacky return true;
2150f22ef01cSRoman Divacky }
2151f22ef01cSRoman Divacky
21523dac3a9bSDimitry Andric /// If we have a conditional branch on a PHI node value that is defined in the
21533dac3a9bSDimitry Andric /// same block as the branch and if any PHI entries are constants, thread edges
21543dac3a9bSDimitry Andric /// corresponding to that entry to be branches to their ultimate destination.
FoldCondBranchOnPHI(BranchInst * BI,const DataLayout & DL,AssumptionCache * AC)21557a7e6055SDimitry Andric static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL,
21567a7e6055SDimitry Andric AssumptionCache *AC) {
2157f22ef01cSRoman Divacky BasicBlock *BB = BI->getParent();
2158f22ef01cSRoman Divacky PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
2159f22ef01cSRoman Divacky // NOTE: we currently cannot transform this case if the PHI node is used
2160f22ef01cSRoman Divacky // outside of the block.
2161f22ef01cSRoman Divacky if (!PN || PN->getParent() != BB || !PN->hasOneUse())
2162f22ef01cSRoman Divacky return false;
2163f22ef01cSRoman Divacky
2164f22ef01cSRoman Divacky // Degenerate case of a single entry PHI.
2165f22ef01cSRoman Divacky if (PN->getNumIncomingValues() == 1) {
2166f22ef01cSRoman Divacky FoldSingleEntryPHINodes(PN->getParent());
2167f22ef01cSRoman Divacky return true;
2168f22ef01cSRoman Divacky }
2169f22ef01cSRoman Divacky
2170f22ef01cSRoman Divacky // Now we know that this block has multiple preds and two succs.
21713ca95b02SDimitry Andric if (!BlockIsSimpleEnoughToThreadThrough(BB))
21723ca95b02SDimitry Andric return false;
2173f22ef01cSRoman Divacky
21743ca95b02SDimitry Andric // Can't fold blocks that contain noduplicate or convergent calls.
2175d88c1a5aSDimitry Andric if (any_of(*BB, [](const Instruction &I) {
21763ca95b02SDimitry Andric const CallInst *CI = dyn_cast<CallInst>(&I);
21773ca95b02SDimitry Andric return CI && (CI->cannotDuplicate() || CI->isConvergent());
21783ca95b02SDimitry Andric }))
21793ca95b02SDimitry Andric return false;
2180f785676fSDimitry Andric
2181f22ef01cSRoman Divacky // Okay, this is a simple enough basic block. See if any phi values are
2182f22ef01cSRoman Divacky // constants.
2183f22ef01cSRoman Divacky for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
21842754fe60SDimitry Andric ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
21853ca95b02SDimitry Andric if (!CB || !CB->getType()->isIntegerTy(1))
21863ca95b02SDimitry Andric continue;
21872754fe60SDimitry Andric
2188f22ef01cSRoman Divacky // Okay, we now know that all edges from PredBB should be revectored to
2189f22ef01cSRoman Divacky // branch to RealDest.
2190f22ef01cSRoman Divacky BasicBlock *PredBB = PN->getIncomingBlock(i);
2191f22ef01cSRoman Divacky BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
2192f22ef01cSRoman Divacky
21933ca95b02SDimitry Andric if (RealDest == BB)
21943ca95b02SDimitry Andric continue; // Skip self loops.
2195bd5abe19SDimitry Andric // Skip if the predecessor's terminator is an indirect branch.
21963ca95b02SDimitry Andric if (isa<IndirectBrInst>(PredBB->getTerminator()))
21973ca95b02SDimitry Andric continue;
2198f22ef01cSRoman Divacky
2199f22ef01cSRoman Divacky // The dest block might have PHI nodes, other predecessors and other
2200f22ef01cSRoman Divacky // difficult cases. Instead of being smart about this, just insert a new
2201f22ef01cSRoman Divacky // block that jumps to the destination block, effectively splitting
2202f22ef01cSRoman Divacky // the edge we are about to create.
22033ca95b02SDimitry Andric BasicBlock *EdgeBB =
22043ca95b02SDimitry Andric BasicBlock::Create(BB->getContext(), RealDest->getName() + ".critedge",
2205f22ef01cSRoman Divacky RealDest->getParent(), RealDest);
2206f22ef01cSRoman Divacky BranchInst::Create(RealDest, EdgeBB);
22072754fe60SDimitry Andric
22082754fe60SDimitry Andric // Update PHI nodes.
22092754fe60SDimitry Andric AddPredecessorToBlock(RealDest, EdgeBB, BB);
2210f22ef01cSRoman Divacky
2211f22ef01cSRoman Divacky // BB may have instructions that are being threaded over. Clone these
2212f22ef01cSRoman Divacky // instructions into EdgeBB. We know that there will be no uses of the
2213f22ef01cSRoman Divacky // cloned instructions outside of EdgeBB.
2214f22ef01cSRoman Divacky BasicBlock::iterator InsertPt = EdgeBB->begin();
22152754fe60SDimitry Andric DenseMap<Value *, Value *> TranslateMap; // Track translated values.
2216f22ef01cSRoman Divacky for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
2217f22ef01cSRoman Divacky if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
2218f22ef01cSRoman Divacky TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
22192754fe60SDimitry Andric continue;
22202754fe60SDimitry Andric }
2221f22ef01cSRoman Divacky // Clone the instruction.
2222f22ef01cSRoman Divacky Instruction *N = BBI->clone();
22233ca95b02SDimitry Andric if (BBI->hasName())
22243ca95b02SDimitry Andric N->setName(BBI->getName() + ".c");
2225f22ef01cSRoman Divacky
2226f22ef01cSRoman Divacky // Update operands due to translation.
22273ca95b02SDimitry Andric for (User::op_iterator i = N->op_begin(), e = N->op_end(); i != e; ++i) {
22282754fe60SDimitry Andric DenseMap<Value *, Value *>::iterator PI = TranslateMap.find(*i);
2229f22ef01cSRoman Divacky if (PI != TranslateMap.end())
2230f22ef01cSRoman Divacky *i = PI->second;
2231f22ef01cSRoman Divacky }
2232f22ef01cSRoman Divacky
2233f22ef01cSRoman Divacky // Check for trivial simplification.
2234f37b6182SDimitry Andric if (Value *V = SimplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
22353ca95b02SDimitry Andric if (!BBI->use_empty())
22367d523365SDimitry Andric TranslateMap[&*BBI] = V;
22373ca95b02SDimitry Andric if (!N->mayHaveSideEffects()) {
2238d8866befSDimitry Andric N->deleteValue(); // Instruction folded away, don't need actual inst
22393ca95b02SDimitry Andric N = nullptr;
22403ca95b02SDimitry Andric }
2241f22ef01cSRoman Divacky } else {
2242f22ef01cSRoman Divacky if (!BBI->use_empty())
22437d523365SDimitry Andric TranslateMap[&*BBI] = N;
2244f22ef01cSRoman Divacky }
22453ca95b02SDimitry Andric // Insert the new instruction into its new home.
22463ca95b02SDimitry Andric if (N)
22473ca95b02SDimitry Andric EdgeBB->getInstList().insert(InsertPt, N);
22487a7e6055SDimitry Andric
22497a7e6055SDimitry Andric // Register the new instruction with the assumption cache if necessary.
22507a7e6055SDimitry Andric if (auto *II = dyn_cast_or_null<IntrinsicInst>(N))
22517a7e6055SDimitry Andric if (II->getIntrinsicID() == Intrinsic::assume)
22527a7e6055SDimitry Andric AC->registerAssumption(II);
2253f22ef01cSRoman Divacky }
2254f22ef01cSRoman Divacky
2255f22ef01cSRoman Divacky // Loop over all of the edges from PredBB to BB, changing them to branch
2256f22ef01cSRoman Divacky // to EdgeBB instead.
2257*b5893f02SDimitry Andric Instruction *PredBBTI = PredBB->getTerminator();
2258f22ef01cSRoman Divacky for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
2259f22ef01cSRoman Divacky if (PredBBTI->getSuccessor(i) == BB) {
2260f22ef01cSRoman Divacky BB->removePredecessor(PredBB);
2261f22ef01cSRoman Divacky PredBBTI->setSuccessor(i, EdgeBB);
2262f22ef01cSRoman Divacky }
2263f22ef01cSRoman Divacky
2264f22ef01cSRoman Divacky // Recurse, simplifying any other constants.
2265*b5893f02SDimitry Andric return FoldCondBranchOnPHI(BI, DL, AC) || true;
2266f22ef01cSRoman Divacky }
2267f22ef01cSRoman Divacky
2268f22ef01cSRoman Divacky return false;
2269f22ef01cSRoman Divacky }
2270f22ef01cSRoman Divacky
22713dac3a9bSDimitry Andric /// Given a BB that starts with the specified two-entry PHI node,
22723dac3a9bSDimitry Andric /// see if we can eliminate it.
FoldTwoEntryPHINode(PHINode * PN,const TargetTransformInfo & TTI,const DataLayout & DL)2273ff0cc061SDimitry Andric static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
2274ff0cc061SDimitry Andric const DataLayout &DL) {
2275f22ef01cSRoman Divacky // Ok, this is a two entry PHI node. Check to see if this is a simple "if
2276f22ef01cSRoman Divacky // statement", which has a very simple dominance structure. Basically, we
2277f22ef01cSRoman Divacky // are trying to find the condition that is being branched on, which
2278f22ef01cSRoman Divacky // subsequently causes this merge to happen. We really want control
2279f22ef01cSRoman Divacky // dependence information for this check, but simplifycfg can't keep it up
2280f22ef01cSRoman Divacky // to date, and this catches most of the cases we care about anyway.
2281f22ef01cSRoman Divacky BasicBlock *BB = PN->getParent();
22824ba319b5SDimitry Andric const Function *Fn = BB->getParent();
22834ba319b5SDimitry Andric if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
22844ba319b5SDimitry Andric return false;
22854ba319b5SDimitry Andric
2286f22ef01cSRoman Divacky BasicBlock *IfTrue, *IfFalse;
2287f22ef01cSRoman Divacky Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
22882754fe60SDimitry Andric if (!IfCond ||
22892754fe60SDimitry Andric // Don't bother if the branch will be constant folded trivially.
22902754fe60SDimitry Andric isa<ConstantInt>(IfCond))
22912754fe60SDimitry Andric return false;
2292f22ef01cSRoman Divacky
2293f22ef01cSRoman Divacky // Okay, we found that we can merge this two-entry phi node into a select.
2294f22ef01cSRoman Divacky // Doing so would require us to fold *all* two entry phi nodes in this block.
2295f22ef01cSRoman Divacky // At some point this becomes non-profitable (particularly if the target
2296f22ef01cSRoman Divacky // doesn't support cmov's). Only do this transformation if there are two or
2297f22ef01cSRoman Divacky // fewer PHI nodes in this block.
2298f22ef01cSRoman Divacky unsigned NumPhis = 0;
2299f22ef01cSRoman Divacky for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
2300f22ef01cSRoman Divacky if (NumPhis > 2)
2301f22ef01cSRoman Divacky return false;
2302f22ef01cSRoman Divacky
2303f22ef01cSRoman Divacky // Loop over the PHI's seeing if we can promote them all to select
2304f22ef01cSRoman Divacky // instructions. While we are at it, keep track of the instructions
2305f22ef01cSRoman Divacky // that need to be moved to the dominating block.
23062754fe60SDimitry Andric SmallPtrSet<Instruction *, 4> AggressiveInsts;
23073b0f4066SDimitry Andric unsigned MaxCostVal0 = PHINodeFoldingThreshold,
23083b0f4066SDimitry Andric MaxCostVal1 = PHINodeFoldingThreshold;
2309ff0cc061SDimitry Andric MaxCostVal0 *= TargetTransformInfo::TCC_Basic;
2310ff0cc061SDimitry Andric MaxCostVal1 *= TargetTransformInfo::TCC_Basic;
2311f22ef01cSRoman Divacky
23122754fe60SDimitry Andric for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
23132754fe60SDimitry Andric PHINode *PN = cast<PHINode>(II++);
2314f37b6182SDimitry Andric if (Value *V = SimplifyInstruction(PN, {DL, PN})) {
23152754fe60SDimitry Andric PN->replaceAllUsesWith(V);
23162754fe60SDimitry Andric PN->eraseFromParent();
23172754fe60SDimitry Andric continue;
23182754fe60SDimitry Andric }
23192754fe60SDimitry Andric
2320*b5893f02SDimitry Andric if (!DominatesMergePoint(PN->getIncomingValue(0), BB, AggressiveInsts,
2321ff0cc061SDimitry Andric MaxCostVal0, TTI) ||
2322*b5893f02SDimitry Andric !DominatesMergePoint(PN->getIncomingValue(1), BB, AggressiveInsts,
2323ff0cc061SDimitry Andric MaxCostVal1, TTI))
2324f22ef01cSRoman Divacky return false;
2325f22ef01cSRoman Divacky }
23262754fe60SDimitry Andric
23277ae0e2c9SDimitry Andric // If we folded the first phi, PN dangles at this point. Refresh it. If
23282754fe60SDimitry Andric // we ran out of PHIs then we simplified them all.
23292754fe60SDimitry Andric PN = dyn_cast<PHINode>(BB->begin());
23303ca95b02SDimitry Andric if (!PN)
23313ca95b02SDimitry Andric return true;
23322754fe60SDimitry Andric
23332754fe60SDimitry Andric // Don't fold i1 branches on PHIs which contain binary operators. These can
23342754fe60SDimitry Andric // often be turned into switches and other things.
23352754fe60SDimitry Andric if (PN->getType()->isIntegerTy(1) &&
23362754fe60SDimitry Andric (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
23372754fe60SDimitry Andric isa<BinaryOperator>(PN->getIncomingValue(1)) ||
23382754fe60SDimitry Andric isa<BinaryOperator>(IfCond)))
23392754fe60SDimitry Andric return false;
2340f22ef01cSRoman Divacky
23413ca95b02SDimitry Andric // If all PHI nodes are promotable, check to make sure that all instructions
23423ca95b02SDimitry Andric // in the predecessor blocks can be promoted as well. If not, we won't be able
23433ca95b02SDimitry Andric // to get rid of the control flow, so it's not worth promoting to select
23443ca95b02SDimitry Andric // instructions.
234591bc56edSDimitry Andric BasicBlock *DomBlock = nullptr;
23462754fe60SDimitry Andric BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
23472754fe60SDimitry Andric BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
23482754fe60SDimitry Andric if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
234991bc56edSDimitry Andric IfBlock1 = nullptr;
23502754fe60SDimitry Andric } else {
23512754fe60SDimitry Andric DomBlock = *pred_begin(IfBlock1);
2352*b5893f02SDimitry Andric for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I)
23537d523365SDimitry Andric if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2354f22ef01cSRoman Divacky // This is not an aggressive instruction that we can promote.
23553ca95b02SDimitry Andric // Because of this, we won't be able to get rid of the control flow, so
23563ca95b02SDimitry Andric // the xform is not worth it.
2357f22ef01cSRoman Divacky return false;
2358f22ef01cSRoman Divacky }
2359f22ef01cSRoman Divacky }
2360f22ef01cSRoman Divacky
23612754fe60SDimitry Andric if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
236291bc56edSDimitry Andric IfBlock2 = nullptr;
23632754fe60SDimitry Andric } else {
23642754fe60SDimitry Andric DomBlock = *pred_begin(IfBlock2);
2365*b5893f02SDimitry Andric for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I)
23667d523365SDimitry Andric if (!AggressiveInsts.count(&*I) && !isa<DbgInfoIntrinsic>(I)) {
2367f22ef01cSRoman Divacky // This is not an aggressive instruction that we can promote.
23683ca95b02SDimitry Andric // Because of this, we won't be able to get rid of the control flow, so
23693ca95b02SDimitry Andric // the xform is not worth it.
2370f22ef01cSRoman Divacky return false;
2371f22ef01cSRoman Divacky }
2372f22ef01cSRoman Divacky }
2373f22ef01cSRoman Divacky
23744ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond
23754ba319b5SDimitry Andric << " T: " << IfTrue->getName()
23764ba319b5SDimitry Andric << " F: " << IfFalse->getName() << "\n");
23772754fe60SDimitry Andric
2378f22ef01cSRoman Divacky // If we can still promote the PHI nodes after this gauntlet of tests,
2379f22ef01cSRoman Divacky // do all of the PHI's now.
23802754fe60SDimitry Andric Instruction *InsertPt = DomBlock->getTerminator();
23813ca95b02SDimitry Andric IRBuilder<NoFolder> Builder(InsertPt);
2382f22ef01cSRoman Divacky
2383f22ef01cSRoman Divacky // Move all 'aggressive' instructions, which are defined in the
2384f22ef01cSRoman Divacky // conditional parts of the if's up to the dominating block.
2385*b5893f02SDimitry Andric if (IfBlock1)
2386*b5893f02SDimitry Andric hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock1);
2387*b5893f02SDimitry Andric if (IfBlock2)
2388*b5893f02SDimitry Andric hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock2);
2389f22ef01cSRoman Divacky
2390f22ef01cSRoman Divacky while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
2391f22ef01cSRoman Divacky // Change the PHI node into a select instruction.
23922754fe60SDimitry Andric Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
23932754fe60SDimitry Andric Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
2394f22ef01cSRoman Divacky
23953ca95b02SDimitry Andric Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt);
23963ca95b02SDimitry Andric PN->replaceAllUsesWith(Sel);
23973ca95b02SDimitry Andric Sel->takeName(PN);
23982754fe60SDimitry Andric PN->eraseFromParent();
2399f22ef01cSRoman Divacky }
24002754fe60SDimitry Andric
24012754fe60SDimitry Andric // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
24022754fe60SDimitry Andric // has been flattened. Change DomBlock to jump directly to our new block to
24032754fe60SDimitry Andric // avoid other simplifycfg's kicking in on the diamond.
2404*b5893f02SDimitry Andric Instruction *OldTI = DomBlock->getTerminator();
2405bd5abe19SDimitry Andric Builder.SetInsertPoint(OldTI);
2406bd5abe19SDimitry Andric Builder.CreateBr(BB);
24072754fe60SDimitry Andric OldTI->eraseFromParent();
2408f22ef01cSRoman Divacky return true;
2409f22ef01cSRoman Divacky }
2410f22ef01cSRoman Divacky
24113dac3a9bSDimitry Andric /// If we found a conditional branch that goes to two returning blocks,
24123dac3a9bSDimitry Andric /// try to merge them together into one return,
2413f22ef01cSRoman Divacky /// introducing a select if the return values disagree.
SimplifyCondBranchToTwoReturns(BranchInst * BI,IRBuilder<> & Builder)2414bd5abe19SDimitry Andric static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
2415bd5abe19SDimitry Andric IRBuilder<> &Builder) {
2416f22ef01cSRoman Divacky assert(BI->isConditional() && "Must be a conditional branch");
2417f22ef01cSRoman Divacky BasicBlock *TrueSucc = BI->getSuccessor(0);
2418f22ef01cSRoman Divacky BasicBlock *FalseSucc = BI->getSuccessor(1);
2419f22ef01cSRoman Divacky ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
2420f22ef01cSRoman Divacky ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
2421f22ef01cSRoman Divacky
2422f22ef01cSRoman Divacky // Check to ensure both blocks are empty (just a return) or optionally empty
2423f22ef01cSRoman Divacky // with PHI nodes. If there are other instructions, merging would cause extra
2424f22ef01cSRoman Divacky // computation on one path or the other.
24252754fe60SDimitry Andric if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
2426f22ef01cSRoman Divacky return false;
24272754fe60SDimitry Andric if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
2428f22ef01cSRoman Divacky return false;
2429f22ef01cSRoman Divacky
2430bd5abe19SDimitry Andric Builder.SetInsertPoint(BI);
2431f22ef01cSRoman Divacky // Okay, we found a branch that is going to two return nodes. If
2432f22ef01cSRoman Divacky // there is no return value for this function, just change the
2433f22ef01cSRoman Divacky // branch into a return.
2434f22ef01cSRoman Divacky if (FalseRet->getNumOperands() == 0) {
2435f22ef01cSRoman Divacky TrueSucc->removePredecessor(BI->getParent());
2436f22ef01cSRoman Divacky FalseSucc->removePredecessor(BI->getParent());
2437bd5abe19SDimitry Andric Builder.CreateRetVoid();
2438*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
2439f22ef01cSRoman Divacky return true;
2440f22ef01cSRoman Divacky }
2441f22ef01cSRoman Divacky
2442f22ef01cSRoman Divacky // Otherwise, figure out what the true and false return values are
2443f22ef01cSRoman Divacky // so we can insert a new select instruction.
2444f22ef01cSRoman Divacky Value *TrueValue = TrueRet->getReturnValue();
2445f22ef01cSRoman Divacky Value *FalseValue = FalseRet->getReturnValue();
2446f22ef01cSRoman Divacky
2447f22ef01cSRoman Divacky // Unwrap any PHI nodes in the return blocks.
2448f22ef01cSRoman Divacky if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
2449f22ef01cSRoman Divacky if (TVPN->getParent() == TrueSucc)
2450f22ef01cSRoman Divacky TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
2451f22ef01cSRoman Divacky if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
2452f22ef01cSRoman Divacky if (FVPN->getParent() == FalseSucc)
2453f22ef01cSRoman Divacky FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
2454f22ef01cSRoman Divacky
2455f22ef01cSRoman Divacky // In order for this transformation to be safe, we must be able to
2456f22ef01cSRoman Divacky // unconditionally execute both operands to the return. This is
2457f22ef01cSRoman Divacky // normally the case, but we could have a potentially-trapping
2458f22ef01cSRoman Divacky // constant expression that prevents this transformation from being
2459f22ef01cSRoman Divacky // safe.
2460f22ef01cSRoman Divacky if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
2461f22ef01cSRoman Divacky if (TCV->canTrap())
2462f22ef01cSRoman Divacky return false;
2463f22ef01cSRoman Divacky if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
2464f22ef01cSRoman Divacky if (FCV->canTrap())
2465f22ef01cSRoman Divacky return false;
2466f22ef01cSRoman Divacky
2467f22ef01cSRoman Divacky // Okay, we collected all the mapped values and checked them for sanity, and
2468f22ef01cSRoman Divacky // defined to really do this transformation. First, update the CFG.
2469f22ef01cSRoman Divacky TrueSucc->removePredecessor(BI->getParent());
2470f22ef01cSRoman Divacky FalseSucc->removePredecessor(BI->getParent());
2471f22ef01cSRoman Divacky
2472f22ef01cSRoman Divacky // Insert select instructions where needed.
2473f22ef01cSRoman Divacky Value *BrCond = BI->getCondition();
2474f22ef01cSRoman Divacky if (TrueValue) {
2475f22ef01cSRoman Divacky // Insert a select if the results differ.
2476f22ef01cSRoman Divacky if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
2477f22ef01cSRoman Divacky } else if (isa<UndefValue>(TrueValue)) {
2478f22ef01cSRoman Divacky TrueValue = FalseValue;
2479f22ef01cSRoman Divacky } else {
24803ca95b02SDimitry Andric TrueValue =
24813ca95b02SDimitry Andric Builder.CreateSelect(BrCond, TrueValue, FalseValue, "retval", BI);
2482f22ef01cSRoman Divacky }
2483f22ef01cSRoman Divacky }
2484f22ef01cSRoman Divacky
24853ca95b02SDimitry Andric Value *RI =
24863ca95b02SDimitry Andric !TrueValue ? Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
2487bd5abe19SDimitry Andric
2488f22ef01cSRoman Divacky (void)RI;
2489f22ef01cSRoman Divacky
24904ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
24914ba319b5SDimitry Andric << "\n " << *BI << "NewRet = " << *RI << "TRUEBLOCK: "
24924ba319b5SDimitry Andric << *TrueSucc << "FALSEBLOCK: " << *FalseSucc);
2493f22ef01cSRoman Divacky
2494*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
2495f22ef01cSRoman Divacky
2496f22ef01cSRoman Divacky return true;
2497f22ef01cSRoman Divacky }
2498f22ef01cSRoman Divacky
24993dac3a9bSDimitry Andric /// Return true if the given instruction is available
25007ae0e2c9SDimitry Andric /// in its predecessor block. If yes, the instruction will be removed.
tryCSEWithPredecessor(Instruction * Inst,BasicBlock * PB)25014ba319b5SDimitry Andric static bool tryCSEWithPredecessor(Instruction *Inst, BasicBlock *PB) {
25027ae0e2c9SDimitry Andric if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
25037ae0e2c9SDimitry Andric return false;
25043ca95b02SDimitry Andric for (Instruction &I : *PB) {
25053ca95b02SDimitry Andric Instruction *PBI = &I;
25067ae0e2c9SDimitry Andric // Check whether Inst and PBI generate the same value.
25077ae0e2c9SDimitry Andric if (Inst->isIdenticalTo(PBI)) {
25087ae0e2c9SDimitry Andric Inst->replaceAllUsesWith(PBI);
25097ae0e2c9SDimitry Andric Inst->eraseFromParent();
25107ae0e2c9SDimitry Andric return true;
25117ae0e2c9SDimitry Andric }
25127ae0e2c9SDimitry Andric }
25137ae0e2c9SDimitry Andric return false;
25147ae0e2c9SDimitry Andric }
2515dff0c46cSDimitry Andric
25163ca95b02SDimitry Andric /// Return true if either PBI or BI has branch weight available, and store
25173ca95b02SDimitry Andric /// the weights in {Pred|Succ}{True|False}Weight. If one of PBI and BI does
25183ca95b02SDimitry Andric /// not have branch weight, use 1:1 as its weight.
extractPredSuccWeights(BranchInst * PBI,BranchInst * BI,uint64_t & PredTrueWeight,uint64_t & PredFalseWeight,uint64_t & SuccTrueWeight,uint64_t & SuccFalseWeight)25193ca95b02SDimitry Andric static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI,
25203ca95b02SDimitry Andric uint64_t &PredTrueWeight,
25213ca95b02SDimitry Andric uint64_t &PredFalseWeight,
25223ca95b02SDimitry Andric uint64_t &SuccTrueWeight,
25233ca95b02SDimitry Andric uint64_t &SuccFalseWeight) {
25243ca95b02SDimitry Andric bool PredHasWeights =
25253ca95b02SDimitry Andric PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
25263ca95b02SDimitry Andric bool SuccHasWeights =
25273ca95b02SDimitry Andric BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
25283ca95b02SDimitry Andric if (PredHasWeights || SuccHasWeights) {
25293ca95b02SDimitry Andric if (!PredHasWeights)
25303ca95b02SDimitry Andric PredTrueWeight = PredFalseWeight = 1;
25313ca95b02SDimitry Andric if (!SuccHasWeights)
25323ca95b02SDimitry Andric SuccTrueWeight = SuccFalseWeight = 1;
25333ca95b02SDimitry Andric return true;
25343ca95b02SDimitry Andric } else {
25353ca95b02SDimitry Andric return false;
25363ca95b02SDimitry Andric }
25373ca95b02SDimitry Andric }
25383ca95b02SDimitry Andric
25393dac3a9bSDimitry Andric /// If this basic block is simple enough, and if a predecessor branches to us
25403dac3a9bSDimitry Andric /// and one of our successors, fold the block into the predecessor and use
25413dac3a9bSDimitry Andric /// logical operations to pick the right destination.
FoldBranchToCommonDest(BranchInst * BI,unsigned BonusInstThreshold)2542ff0cc061SDimitry Andric bool llvm::FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold) {
2543f22ef01cSRoman Divacky BasicBlock *BB = BI->getParent();
2544bd5abe19SDimitry Andric
2545*b5893f02SDimitry Andric const unsigned PredCount = pred_size(BB);
2546*b5893f02SDimitry Andric
254791bc56edSDimitry Andric Instruction *Cond = nullptr;
25487ae0e2c9SDimitry Andric if (BI->isConditional())
25497ae0e2c9SDimitry Andric Cond = dyn_cast<Instruction>(BI->getCondition());
25507ae0e2c9SDimitry Andric else {
25517ae0e2c9SDimitry Andric // For unconditional branch, check for a simple CFG pattern, where
25527ae0e2c9SDimitry Andric // BB has a single predecessor and BB's successor is also its predecessor's
25532cab237bSDimitry Andric // successor. If such pattern exists, check for CSE between BB and its
25547ae0e2c9SDimitry Andric // predecessor.
25557ae0e2c9SDimitry Andric if (BasicBlock *PB = BB->getSinglePredecessor())
25567ae0e2c9SDimitry Andric if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
25577ae0e2c9SDimitry Andric if (PBI->isConditional() &&
25587ae0e2c9SDimitry Andric (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
25597ae0e2c9SDimitry Andric BI->getSuccessor(0) == PBI->getSuccessor(1))) {
25604ba319b5SDimitry Andric for (auto I = BB->instructionsWithoutDebug().begin(),
25614ba319b5SDimitry Andric E = BB->instructionsWithoutDebug().end();
25624ba319b5SDimitry Andric I != E;) {
25637d523365SDimitry Andric Instruction *Curr = &*I++;
25647ae0e2c9SDimitry Andric if (isa<CmpInst>(Curr)) {
25657ae0e2c9SDimitry Andric Cond = Curr;
25667ae0e2c9SDimitry Andric break;
25677ae0e2c9SDimitry Andric }
25687ae0e2c9SDimitry Andric // Quit if we can't remove this instruction.
25694ba319b5SDimitry Andric if (!tryCSEWithPredecessor(Curr, PB))
25707ae0e2c9SDimitry Andric return false;
25717ae0e2c9SDimitry Andric }
25727ae0e2c9SDimitry Andric }
25737ae0e2c9SDimitry Andric
257491bc56edSDimitry Andric if (!Cond)
25757ae0e2c9SDimitry Andric return false;
25767ae0e2c9SDimitry Andric }
25777ae0e2c9SDimitry Andric
257891bc56edSDimitry Andric if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2579ffd1746dSEd Schouten Cond->getParent() != BB || !Cond->hasOneUse())
2580ffd1746dSEd Schouten return false;
2581f22ef01cSRoman Divacky
2582f22ef01cSRoman Divacky // Make sure the instruction after the condition is the cond branch.
25837d523365SDimitry Andric BasicBlock::iterator CondIt = ++Cond->getIterator();
25843b0f4066SDimitry Andric
258591bc56edSDimitry Andric // Ignore dbg intrinsics.
25863ca95b02SDimitry Andric while (isa<DbgInfoIntrinsic>(CondIt))
25873ca95b02SDimitry Andric ++CondIt;
25883b0f4066SDimitry Andric
25893b0f4066SDimitry Andric if (&*CondIt != BI)
2590f22ef01cSRoman Divacky return false;
2591f22ef01cSRoman Divacky
259239d628a0SDimitry Andric // Only allow this transformation if computing the condition doesn't involve
259339d628a0SDimitry Andric // too many instructions and these involved instructions can be executed
259439d628a0SDimitry Andric // unconditionally. We denote all involved instructions except the condition
259539d628a0SDimitry Andric // as "bonus instructions", and only allow this transformation when the
2596*b5893f02SDimitry Andric // number of the bonus instructions we'll need to create when cloning into
2597*b5893f02SDimitry Andric // each predecessor does not exceed a certain threshold.
259839d628a0SDimitry Andric unsigned NumBonusInsts = 0;
25993ca95b02SDimitry Andric for (auto I = BB->begin(); Cond != &*I; ++I) {
260039d628a0SDimitry Andric // Ignore dbg intrinsics.
260139d628a0SDimitry Andric if (isa<DbgInfoIntrinsic>(I))
260239d628a0SDimitry Andric continue;
26037d523365SDimitry Andric if (!I->hasOneUse() || !isSafeToSpeculativelyExecute(&*I))
260439d628a0SDimitry Andric return false;
260539d628a0SDimitry Andric // I has only one use and can be executed unconditionally.
260639d628a0SDimitry Andric Instruction *User = dyn_cast<Instruction>(I->user_back());
260739d628a0SDimitry Andric if (User == nullptr || User->getParent() != BB)
260839d628a0SDimitry Andric return false;
260939d628a0SDimitry Andric // I is used in the same BB. Since BI uses Cond and doesn't have more slots
261039d628a0SDimitry Andric // to use any other instruction, User must be an instruction between next(I)
261139d628a0SDimitry Andric // and Cond.
2612*b5893f02SDimitry Andric
2613*b5893f02SDimitry Andric // Account for the cost of duplicating this instruction into each
2614*b5893f02SDimitry Andric // predecessor.
2615*b5893f02SDimitry Andric NumBonusInsts += PredCount;
261639d628a0SDimitry Andric // Early exits once we reach the limit.
261739d628a0SDimitry Andric if (NumBonusInsts > BonusInstThreshold)
261839d628a0SDimitry Andric return false;
261939d628a0SDimitry Andric }
262039d628a0SDimitry Andric
2621f22ef01cSRoman Divacky // Cond is known to be a compare or binary operator. Check to make sure that
2622f22ef01cSRoman Divacky // neither operand is a potentially-trapping constant expression.
2623f22ef01cSRoman Divacky if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
2624f22ef01cSRoman Divacky if (CE->canTrap())
2625f22ef01cSRoman Divacky return false;
2626f22ef01cSRoman Divacky if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
2627f22ef01cSRoman Divacky if (CE->canTrap())
2628f22ef01cSRoman Divacky return false;
2629f22ef01cSRoman Divacky
2630f22ef01cSRoman Divacky // Finally, don't infinitely unroll conditional loops.
2631f22ef01cSRoman Divacky BasicBlock *TrueDest = BI->getSuccessor(0);
263291bc56edSDimitry Andric BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : nullptr;
2633f22ef01cSRoman Divacky if (TrueDest == BB || FalseDest == BB)
2634f22ef01cSRoman Divacky return false;
2635f22ef01cSRoman Divacky
2636f22ef01cSRoman Divacky for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
2637f22ef01cSRoman Divacky BasicBlock *PredBlock = *PI;
2638f22ef01cSRoman Divacky BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
2639f22ef01cSRoman Divacky
2640f22ef01cSRoman Divacky // Check that we have two conditional branches. If there is a PHI node in
2641f22ef01cSRoman Divacky // the common successor, verify that the same value flows in from both
2642f22ef01cSRoman Divacky // blocks.
26437ae0e2c9SDimitry Andric SmallVector<PHINode *, 4> PHIs;
264491bc56edSDimitry Andric if (!PBI || PBI->isUnconditional() ||
26453ca95b02SDimitry Andric (BI->isConditional() && !SafeToMergeTerminators(BI, PBI)) ||
26467ae0e2c9SDimitry Andric (!BI->isConditional() &&
26477ae0e2c9SDimitry Andric !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
26483b0f4066SDimitry Andric continue;
26493b0f4066SDimitry Andric
26503b0f4066SDimitry Andric // Determine if the two branches share a common destination.
26513861d79fSDimitry Andric Instruction::BinaryOps Opc = Instruction::BinaryOpsEnd;
26523b0f4066SDimitry Andric bool InvertPredCond = false;
26533b0f4066SDimitry Andric
26547ae0e2c9SDimitry Andric if (BI->isConditional()) {
26553ca95b02SDimitry Andric if (PBI->getSuccessor(0) == TrueDest) {
26563b0f4066SDimitry Andric Opc = Instruction::Or;
26573ca95b02SDimitry Andric } else if (PBI->getSuccessor(1) == FalseDest) {
26583b0f4066SDimitry Andric Opc = Instruction::And;
26593ca95b02SDimitry Andric } else if (PBI->getSuccessor(0) == FalseDest) {
26603ca95b02SDimitry Andric Opc = Instruction::And;
26613ca95b02SDimitry Andric InvertPredCond = true;
26623ca95b02SDimitry Andric } else if (PBI->getSuccessor(1) == TrueDest) {
26633ca95b02SDimitry Andric Opc = Instruction::Or;
26643ca95b02SDimitry Andric InvertPredCond = true;
26653ca95b02SDimitry Andric } else {
2666f22ef01cSRoman Divacky continue;
26673ca95b02SDimitry Andric }
26687ae0e2c9SDimitry Andric } else {
26697ae0e2c9SDimitry Andric if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
26707ae0e2c9SDimitry Andric continue;
26717ae0e2c9SDimitry Andric }
2672f22ef01cSRoman Divacky
26734ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
2674bd5abe19SDimitry Andric IRBuilder<> Builder(PBI);
2675f22ef01cSRoman Divacky
2676f22ef01cSRoman Divacky // If we need to invert the condition in the pred block to match, do so now.
2677f22ef01cSRoman Divacky if (InvertPredCond) {
26782754fe60SDimitry Andric Value *NewCond = PBI->getCondition();
26792754fe60SDimitry Andric
26802754fe60SDimitry Andric if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
26812754fe60SDimitry Andric CmpInst *CI = cast<CmpInst>(NewCond);
26822754fe60SDimitry Andric CI->setPredicate(CI->getInversePredicate());
26832754fe60SDimitry Andric } else {
26843ca95b02SDimitry Andric NewCond =
26853ca95b02SDimitry Andric Builder.CreateNot(NewCond, PBI->getCondition()->getName() + ".not");
26862754fe60SDimitry Andric }
26872754fe60SDimitry Andric
2688f22ef01cSRoman Divacky PBI->setCondition(NewCond);
2689dff0c46cSDimitry Andric PBI->swapSuccessors();
2690f22ef01cSRoman Divacky }
2691f22ef01cSRoman Divacky
269239d628a0SDimitry Andric // If we have bonus instructions, clone them into the predecessor block.
26933dac3a9bSDimitry Andric // Note that there may be multiple predecessor blocks, so we cannot move
269439d628a0SDimitry Andric // bonus instructions to a predecessor block.
269539d628a0SDimitry Andric ValueToValueMapTy VMap; // maps original values to cloned values
269639d628a0SDimitry Andric // We already make sure Cond is the last instruction before BI. Therefore,
26973dac3a9bSDimitry Andric // all instructions before Cond other than DbgInfoIntrinsic are bonus
269839d628a0SDimitry Andric // instructions.
26993ca95b02SDimitry Andric for (auto BonusInst = BB->begin(); Cond != &*BonusInst; ++BonusInst) {
270039d628a0SDimitry Andric if (isa<DbgInfoIntrinsic>(BonusInst))
270139d628a0SDimitry Andric continue;
270239d628a0SDimitry Andric Instruction *NewBonusInst = BonusInst->clone();
270339d628a0SDimitry Andric RemapInstruction(NewBonusInst, VMap,
27043ca95b02SDimitry Andric RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
27057d523365SDimitry Andric VMap[&*BonusInst] = NewBonusInst;
270691bc56edSDimitry Andric
270791bc56edSDimitry Andric // If we moved a load, we cannot any longer claim any knowledge about
270891bc56edSDimitry Andric // its potential value. The previous information might have been valid
270991bc56edSDimitry Andric // only given the branch precondition.
271091bc56edSDimitry Andric // For an analogous reason, we must also drop all the metadata whose
271191bc56edSDimitry Andric // semantics we don't understand.
27127d523365SDimitry Andric NewBonusInst->dropUnknownNonDebugMetadata();
271391bc56edSDimitry Andric
27147d523365SDimitry Andric PredBlock->getInstList().insert(PBI->getIterator(), NewBonusInst);
27157d523365SDimitry Andric NewBonusInst->takeName(&*BonusInst);
2716ffd1746dSEd Schouten BonusInst->setName(BonusInst->getName() + ".old");
2717ffd1746dSEd Schouten }
2718ffd1746dSEd Schouten
2719f22ef01cSRoman Divacky // Clone Cond into the predecessor basic block, and or/and the
2720f22ef01cSRoman Divacky // two conditions together.
2721*b5893f02SDimitry Andric Instruction *CondInPred = Cond->clone();
2722*b5893f02SDimitry Andric RemapInstruction(CondInPred, VMap,
27233ca95b02SDimitry Andric RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
2724*b5893f02SDimitry Andric PredBlock->getInstList().insert(PBI->getIterator(), CondInPred);
2725*b5893f02SDimitry Andric CondInPred->takeName(Cond);
2726*b5893f02SDimitry Andric Cond->setName(CondInPred->getName() + ".old");
2727f22ef01cSRoman Divacky
27287ae0e2c9SDimitry Andric if (BI->isConditional()) {
27293ca95b02SDimitry Andric Instruction *NewCond = cast<Instruction>(
2730*b5893f02SDimitry Andric Builder.CreateBinOp(Opc, PBI->getCondition(), CondInPred, "or.cond"));
2731f22ef01cSRoman Divacky PBI->setCondition(NewCond);
27327ae0e2c9SDimitry Andric
27333861d79fSDimitry Andric uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
27343ca95b02SDimitry Andric bool HasWeights =
27353ca95b02SDimitry Andric extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
27363ca95b02SDimitry Andric SuccTrueWeight, SuccFalseWeight);
27373861d79fSDimitry Andric SmallVector<uint64_t, 8> NewWeights;
27383861d79fSDimitry Andric
2739f22ef01cSRoman Divacky if (PBI->getSuccessor(0) == BB) {
27403ca95b02SDimitry Andric if (HasWeights) {
27413861d79fSDimitry Andric // PBI: br i1 %x, BB, FalseDest
27423861d79fSDimitry Andric // BI: br i1 %y, TrueDest, FalseDest
27433861d79fSDimitry Andric // TrueWeight is TrueWeight for PBI * TrueWeight for BI.
27443861d79fSDimitry Andric NewWeights.push_back(PredTrueWeight * SuccTrueWeight);
27453861d79fSDimitry Andric // FalseWeight is FalseWeight for PBI * TotalWeight for BI +
27463861d79fSDimitry Andric // TrueWeight for PBI * FalseWeight for BI.
27473861d79fSDimitry Andric // We assume that total weights of a BranchInst can fit into 32 bits.
27483861d79fSDimitry Andric // Therefore, we will not have overflow using 64-bit arithmetic.
27493ca95b02SDimitry Andric NewWeights.push_back(PredFalseWeight *
27503ca95b02SDimitry Andric (SuccFalseWeight + SuccTrueWeight) +
27513ca95b02SDimitry Andric PredTrueWeight * SuccFalseWeight);
27523861d79fSDimitry Andric }
2753f22ef01cSRoman Divacky AddPredecessorToBlock(TrueDest, PredBlock, BB);
2754f22ef01cSRoman Divacky PBI->setSuccessor(0, TrueDest);
2755f22ef01cSRoman Divacky }
2756f22ef01cSRoman Divacky if (PBI->getSuccessor(1) == BB) {
27573ca95b02SDimitry Andric if (HasWeights) {
27583861d79fSDimitry Andric // PBI: br i1 %x, TrueDest, BB
27593861d79fSDimitry Andric // BI: br i1 %y, TrueDest, FalseDest
27603861d79fSDimitry Andric // TrueWeight is TrueWeight for PBI * TotalWeight for BI +
27613861d79fSDimitry Andric // FalseWeight for PBI * TrueWeight for BI.
27623ca95b02SDimitry Andric NewWeights.push_back(PredTrueWeight *
27633ca95b02SDimitry Andric (SuccFalseWeight + SuccTrueWeight) +
27643ca95b02SDimitry Andric PredFalseWeight * SuccTrueWeight);
27653861d79fSDimitry Andric // FalseWeight is FalseWeight for PBI * FalseWeight for BI.
27663861d79fSDimitry Andric NewWeights.push_back(PredFalseWeight * SuccFalseWeight);
27673861d79fSDimitry Andric }
2768f22ef01cSRoman Divacky AddPredecessorToBlock(FalseDest, PredBlock, BB);
2769f22ef01cSRoman Divacky PBI->setSuccessor(1, FalseDest);
2770f22ef01cSRoman Divacky }
27713861d79fSDimitry Andric if (NewWeights.size() == 2) {
27723861d79fSDimitry Andric // Halve the weights if any of them cannot fit in an uint32_t
27733861d79fSDimitry Andric FitWeights(NewWeights);
27743861d79fSDimitry Andric
27753ca95b02SDimitry Andric SmallVector<uint32_t, 8> MDWeights(NewWeights.begin(),
27763ca95b02SDimitry Andric NewWeights.end());
27772cab237bSDimitry Andric setBranchWeights(PBI, MDWeights[0], MDWeights[1]);
27783861d79fSDimitry Andric } else
277991bc56edSDimitry Andric PBI->setMetadata(LLVMContext::MD_prof, nullptr);
27807ae0e2c9SDimitry Andric } else {
27817ae0e2c9SDimitry Andric // Update PHI nodes in the common successors.
27827ae0e2c9SDimitry Andric for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
27837ae0e2c9SDimitry Andric ConstantInt *PBI_C = cast<ConstantInt>(
27847ae0e2c9SDimitry Andric PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
27857ae0e2c9SDimitry Andric assert(PBI_C->getType()->isIntegerTy(1));
278691bc56edSDimitry Andric Instruction *MergedCond = nullptr;
27877ae0e2c9SDimitry Andric if (PBI->getSuccessor(0) == TrueDest) {
27887ae0e2c9SDimitry Andric // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
27897ae0e2c9SDimitry Andric // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
27907ae0e2c9SDimitry Andric // is false: !PBI_Cond and BI_Value
27913ca95b02SDimitry Andric Instruction *NotCond = cast<Instruction>(
27923ca95b02SDimitry Andric Builder.CreateNot(PBI->getCondition(), "not.cond"));
27933ca95b02SDimitry Andric MergedCond = cast<Instruction>(
2794*b5893f02SDimitry Andric Builder.CreateBinOp(Instruction::And, NotCond, CondInPred,
2795*b5893f02SDimitry Andric "and.cond"));
27967ae0e2c9SDimitry Andric if (PBI_C->isOne())
27973ca95b02SDimitry Andric MergedCond = cast<Instruction>(Builder.CreateBinOp(
27983ca95b02SDimitry Andric Instruction::Or, PBI->getCondition(), MergedCond, "or.cond"));
27997ae0e2c9SDimitry Andric } else {
28007ae0e2c9SDimitry Andric // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
28017ae0e2c9SDimitry Andric // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
28027ae0e2c9SDimitry Andric // is false: PBI_Cond and BI_Value
28033ca95b02SDimitry Andric MergedCond = cast<Instruction>(Builder.CreateBinOp(
2804*b5893f02SDimitry Andric Instruction::And, PBI->getCondition(), CondInPred, "and.cond"));
28057ae0e2c9SDimitry Andric if (PBI_C->isOne()) {
28063ca95b02SDimitry Andric Instruction *NotCond = cast<Instruction>(
28073ca95b02SDimitry Andric Builder.CreateNot(PBI->getCondition(), "not.cond"));
28083ca95b02SDimitry Andric MergedCond = cast<Instruction>(Builder.CreateBinOp(
28093ca95b02SDimitry Andric Instruction::Or, NotCond, MergedCond, "or.cond"));
28107ae0e2c9SDimitry Andric }
28117ae0e2c9SDimitry Andric }
28127ae0e2c9SDimitry Andric // Update PHI Node.
28137ae0e2c9SDimitry Andric PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
28147ae0e2c9SDimitry Andric MergedCond);
28157ae0e2c9SDimitry Andric }
28167ae0e2c9SDimitry Andric // Change PBI from Conditional to Unconditional.
28177ae0e2c9SDimitry Andric BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
2818*b5893f02SDimitry Andric EraseTerminatorAndDCECond(PBI);
28197ae0e2c9SDimitry Andric PBI = New_PBI;
28207ae0e2c9SDimitry Andric }
28213b0f4066SDimitry Andric
2822d88c1a5aSDimitry Andric // If BI was a loop latch, it may have had associated loop metadata.
2823d88c1a5aSDimitry Andric // We need to copy it to the new latch, that is, PBI.
2824d88c1a5aSDimitry Andric if (MDNode *LoopMD = BI->getMetadata(LLVMContext::MD_loop))
2825d88c1a5aSDimitry Andric PBI->setMetadata(LLVMContext::MD_loop, LoopMD);
2826d88c1a5aSDimitry Andric
2827dff0c46cSDimitry Andric // TODO: If BB is reachable from all paths through PredBlock, then we
2828dff0c46cSDimitry Andric // could replace PBI's branch probabilities with BI's.
2829dff0c46cSDimitry Andric
28303b0f4066SDimitry Andric // Copy any debug value intrinsics into the end of PredBlock.
28313ca95b02SDimitry Andric for (Instruction &I : *BB)
28323ca95b02SDimitry Andric if (isa<DbgInfoIntrinsic>(I))
28333ca95b02SDimitry Andric I.clone()->insertBefore(PBI);
28343b0f4066SDimitry Andric
2835f22ef01cSRoman Divacky return true;
2836f22ef01cSRoman Divacky }
2837f22ef01cSRoman Divacky return false;
2838f22ef01cSRoman Divacky }
2839f22ef01cSRoman Divacky
28407d523365SDimitry Andric // If there is only one store in BB1 and BB2, return it, otherwise return
28417d523365SDimitry Andric // nullptr.
findUniqueStoreInBlocks(BasicBlock * BB1,BasicBlock * BB2)28427d523365SDimitry Andric static StoreInst *findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2) {
28437d523365SDimitry Andric StoreInst *S = nullptr;
28447d523365SDimitry Andric for (auto *BB : {BB1, BB2}) {
28457d523365SDimitry Andric if (!BB)
28467d523365SDimitry Andric continue;
28477d523365SDimitry Andric for (auto &I : *BB)
28487d523365SDimitry Andric if (auto *SI = dyn_cast<StoreInst>(&I)) {
28497d523365SDimitry Andric if (S)
28507d523365SDimitry Andric // Multiple stores seen.
28517d523365SDimitry Andric return nullptr;
28527d523365SDimitry Andric else
28537d523365SDimitry Andric S = SI;
28547d523365SDimitry Andric }
28557d523365SDimitry Andric }
28567d523365SDimitry Andric return S;
28577d523365SDimitry Andric }
28587d523365SDimitry Andric
ensureValueAvailableInSuccessor(Value * V,BasicBlock * BB,Value * AlternativeV=nullptr)28597d523365SDimitry Andric static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
28607d523365SDimitry Andric Value *AlternativeV = nullptr) {
28617d523365SDimitry Andric // PHI is going to be a PHI node that allows the value V that is defined in
28627d523365SDimitry Andric // BB to be referenced in BB's only successor.
28637d523365SDimitry Andric //
28647d523365SDimitry Andric // If AlternativeV is nullptr, the only value we care about in PHI is V. It
28657d523365SDimitry Andric // doesn't matter to us what the other operand is (it'll never get used). We
28667d523365SDimitry Andric // could just create a new PHI with an undef incoming value, but that could
28677d523365SDimitry Andric // increase register pressure if EarlyCSE/InstCombine can't fold it with some
28687d523365SDimitry Andric // other PHI. So here we directly look for some PHI in BB's successor with V
28697d523365SDimitry Andric // as an incoming operand. If we find one, we use it, else we create a new
28707d523365SDimitry Andric // one.
28717d523365SDimitry Andric //
28727d523365SDimitry Andric // If AlternativeV is not nullptr, we care about both incoming values in PHI.
28737d523365SDimitry Andric // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
28747d523365SDimitry Andric // where OtherBB is the single other predecessor of BB's only successor.
28757d523365SDimitry Andric PHINode *PHI = nullptr;
28767d523365SDimitry Andric BasicBlock *Succ = BB->getSingleSuccessor();
28777d523365SDimitry Andric
28787d523365SDimitry Andric for (auto I = Succ->begin(); isa<PHINode>(I); ++I)
28797d523365SDimitry Andric if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
28807d523365SDimitry Andric PHI = cast<PHINode>(I);
28817d523365SDimitry Andric if (!AlternativeV)
28827d523365SDimitry Andric break;
28837d523365SDimitry Andric
2884*b5893f02SDimitry Andric assert(Succ->hasNPredecessors(2));
28857d523365SDimitry Andric auto PredI = pred_begin(Succ);
28867d523365SDimitry Andric BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
28877d523365SDimitry Andric if (PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
28887d523365SDimitry Andric break;
28897d523365SDimitry Andric PHI = nullptr;
28907d523365SDimitry Andric }
28917d523365SDimitry Andric if (PHI)
28927d523365SDimitry Andric return PHI;
28937d523365SDimitry Andric
28947d523365SDimitry Andric // If V is not an instruction defined in BB, just return it.
28957d523365SDimitry Andric if (!AlternativeV &&
28967d523365SDimitry Andric (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB))
28977d523365SDimitry Andric return V;
28987d523365SDimitry Andric
28997d523365SDimitry Andric PHI = PHINode::Create(V->getType(), 2, "simplifycfg.merge", &Succ->front());
29007d523365SDimitry Andric PHI->addIncoming(V, BB);
29017d523365SDimitry Andric for (BasicBlock *PredBB : predecessors(Succ))
29027d523365SDimitry Andric if (PredBB != BB)
29033ca95b02SDimitry Andric PHI->addIncoming(
29043ca95b02SDimitry Andric AlternativeV ? AlternativeV : UndefValue::get(V->getType()), PredBB);
29057d523365SDimitry Andric return PHI;
29067d523365SDimitry Andric }
29077d523365SDimitry Andric
mergeConditionalStoreToAddress(BasicBlock * PTB,BasicBlock * PFB,BasicBlock * QTB,BasicBlock * QFB,BasicBlock * PostBB,Value * Address,bool InvertPCond,bool InvertQCond,const DataLayout & DL)29087d523365SDimitry Andric static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
29097d523365SDimitry Andric BasicBlock *QTB, BasicBlock *QFB,
29107d523365SDimitry Andric BasicBlock *PostBB, Value *Address,
29112cab237bSDimitry Andric bool InvertPCond, bool InvertQCond,
29122cab237bSDimitry Andric const DataLayout &DL) {
29137d523365SDimitry Andric auto IsaBitcastOfPointerType = [](const Instruction &I) {
29147d523365SDimitry Andric return Operator::getOpcode(&I) == Instruction::BitCast &&
29157d523365SDimitry Andric I.getType()->isPointerTy();
29167d523365SDimitry Andric };
29177d523365SDimitry Andric
29187d523365SDimitry Andric // If we're not in aggressive mode, we only optimize if we have some
29197d523365SDimitry Andric // confidence that by optimizing we'll allow P and/or Q to be if-converted.
29207d523365SDimitry Andric auto IsWorthwhile = [&](BasicBlock *BB) {
29217d523365SDimitry Andric if (!BB)
29227d523365SDimitry Andric return true;
29237d523365SDimitry Andric // Heuristic: if the block can be if-converted/phi-folded and the
29247d523365SDimitry Andric // instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
29257d523365SDimitry Andric // thread this store.
29267d523365SDimitry Andric unsigned N = 0;
29274ba319b5SDimitry Andric for (auto &I : BB->instructionsWithoutDebug()) {
29287d523365SDimitry Andric // Cheap instructions viable for folding.
29297d523365SDimitry Andric if (isa<BinaryOperator>(I) || isa<GetElementPtrInst>(I) ||
29307d523365SDimitry Andric isa<StoreInst>(I))
29317d523365SDimitry Andric ++N;
29327d523365SDimitry Andric // Free instructions.
2933*b5893f02SDimitry Andric else if (I.isTerminator() || IsaBitcastOfPointerType(I))
29347d523365SDimitry Andric continue;
29357d523365SDimitry Andric else
29367d523365SDimitry Andric return false;
29377d523365SDimitry Andric }
29382cab237bSDimitry Andric // The store we want to merge is counted in N, so add 1 to make sure
29392cab237bSDimitry Andric // we're counting the instructions that would be left.
29402cab237bSDimitry Andric return N <= (PHINodeFoldingThreshold + 1);
29417d523365SDimitry Andric };
29427d523365SDimitry Andric
29433ca95b02SDimitry Andric if (!MergeCondStoresAggressively &&
29443ca95b02SDimitry Andric (!IsWorthwhile(PTB) || !IsWorthwhile(PFB) || !IsWorthwhile(QTB) ||
29457d523365SDimitry Andric !IsWorthwhile(QFB)))
29467d523365SDimitry Andric return false;
29477d523365SDimitry Andric
29487d523365SDimitry Andric // For every pointer, there must be exactly two stores, one coming from
29497d523365SDimitry Andric // PTB or PFB, and the other from QTB or QFB. We don't support more than one
29507d523365SDimitry Andric // store (to any address) in PTB,PFB or QTB,QFB.
29517d523365SDimitry Andric // FIXME: We could relax this restriction with a bit more work and performance
29527d523365SDimitry Andric // testing.
29537d523365SDimitry Andric StoreInst *PStore = findUniqueStoreInBlocks(PTB, PFB);
29547d523365SDimitry Andric StoreInst *QStore = findUniqueStoreInBlocks(QTB, QFB);
29557d523365SDimitry Andric if (!PStore || !QStore)
29567d523365SDimitry Andric return false;
29577d523365SDimitry Andric
29587d523365SDimitry Andric // Now check the stores are compatible.
29597d523365SDimitry Andric if (!QStore->isUnordered() || !PStore->isUnordered())
29607d523365SDimitry Andric return false;
29617d523365SDimitry Andric
29627d523365SDimitry Andric // Check that sinking the store won't cause program behavior changes. Sinking
29637d523365SDimitry Andric // the store out of the Q blocks won't change any behavior as we're sinking
29647d523365SDimitry Andric // from a block to its unconditional successor. But we're moving a store from
29657d523365SDimitry Andric // the P blocks down through the middle block (QBI) and past both QFB and QTB.
29667d523365SDimitry Andric // So we need to check that there are no aliasing loads or stores in
29677d523365SDimitry Andric // QBI, QTB and QFB. We also need to check there are no conflicting memory
29687d523365SDimitry Andric // operations between PStore and the end of its parent block.
29697d523365SDimitry Andric //
29707d523365SDimitry Andric // The ideal way to do this is to query AliasAnalysis, but we don't
29717d523365SDimitry Andric // preserve AA currently so that is dangerous. Be super safe and just
29727d523365SDimitry Andric // check there are no other memory operations at all.
29737d523365SDimitry Andric for (auto &I : *QFB->getSinglePredecessor())
29747d523365SDimitry Andric if (I.mayReadOrWriteMemory())
29757d523365SDimitry Andric return false;
29767d523365SDimitry Andric for (auto &I : *QFB)
29777d523365SDimitry Andric if (&I != QStore && I.mayReadOrWriteMemory())
29787d523365SDimitry Andric return false;
29797d523365SDimitry Andric if (QTB)
29807d523365SDimitry Andric for (auto &I : *QTB)
29817d523365SDimitry Andric if (&I != QStore && I.mayReadOrWriteMemory())
29827d523365SDimitry Andric return false;
29837d523365SDimitry Andric for (auto I = BasicBlock::iterator(PStore), E = PStore->getParent()->end();
29847d523365SDimitry Andric I != E; ++I)
29857d523365SDimitry Andric if (&*I != PStore && I->mayReadOrWriteMemory())
29867d523365SDimitry Andric return false;
29877d523365SDimitry Andric
29884ba319b5SDimitry Andric // If PostBB has more than two predecessors, we need to split it so we can
29894ba319b5SDimitry Andric // sink the store.
29904ba319b5SDimitry Andric if (std::next(pred_begin(PostBB), 2) != pred_end(PostBB)) {
29914ba319b5SDimitry Andric // We know that QFB's only successor is PostBB. And QFB has a single
29924ba319b5SDimitry Andric // predecessor. If QTB exists, then its only successor is also PostBB.
29934ba319b5SDimitry Andric // If QTB does not exist, then QFB's only predecessor has a conditional
29944ba319b5SDimitry Andric // branch to QFB and PostBB.
29954ba319b5SDimitry Andric BasicBlock *TruePred = QTB ? QTB : QFB->getSinglePredecessor();
29964ba319b5SDimitry Andric BasicBlock *NewBB = SplitBlockPredecessors(PostBB, { QFB, TruePred},
29974ba319b5SDimitry Andric "condstore.split");
29984ba319b5SDimitry Andric if (!NewBB)
29994ba319b5SDimitry Andric return false;
30004ba319b5SDimitry Andric PostBB = NewBB;
30014ba319b5SDimitry Andric }
30024ba319b5SDimitry Andric
30037d523365SDimitry Andric // OK, we're going to sink the stores to PostBB. The store has to be
30047d523365SDimitry Andric // conditional though, so first create the predicate.
30057d523365SDimitry Andric Value *PCond = cast<BranchInst>(PFB->getSinglePredecessor()->getTerminator())
30067d523365SDimitry Andric ->getCondition();
30077d523365SDimitry Andric Value *QCond = cast<BranchInst>(QFB->getSinglePredecessor()->getTerminator())
30087d523365SDimitry Andric ->getCondition();
30097d523365SDimitry Andric
30107d523365SDimitry Andric Value *PPHI = ensureValueAvailableInSuccessor(PStore->getValueOperand(),
30117d523365SDimitry Andric PStore->getParent());
30127d523365SDimitry Andric Value *QPHI = ensureValueAvailableInSuccessor(QStore->getValueOperand(),
30137d523365SDimitry Andric QStore->getParent(), PPHI);
30147d523365SDimitry Andric
30157d523365SDimitry Andric IRBuilder<> QB(&*PostBB->getFirstInsertionPt());
30167d523365SDimitry Andric
30177d523365SDimitry Andric Value *PPred = PStore->getParent() == PTB ? PCond : QB.CreateNot(PCond);
30187d523365SDimitry Andric Value *QPred = QStore->getParent() == QTB ? QCond : QB.CreateNot(QCond);
30197d523365SDimitry Andric
30207d523365SDimitry Andric if (InvertPCond)
30217d523365SDimitry Andric PPred = QB.CreateNot(PPred);
30227d523365SDimitry Andric if (InvertQCond)
30237d523365SDimitry Andric QPred = QB.CreateNot(QPred);
30247d523365SDimitry Andric Value *CombinedPred = QB.CreateOr(PPred, QPred);
30257d523365SDimitry Andric
30267d523365SDimitry Andric auto *T =
30277d523365SDimitry Andric SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(), false);
30287d523365SDimitry Andric QB.SetInsertPoint(T);
30297d523365SDimitry Andric StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI, Address));
30307d523365SDimitry Andric AAMDNodes AAMD;
30317d523365SDimitry Andric PStore->getAAMetadata(AAMD, /*Merge=*/false);
30327d523365SDimitry Andric PStore->getAAMetadata(AAMD, /*Merge=*/true);
30337d523365SDimitry Andric SI->setAAMetadata(AAMD);
30342cab237bSDimitry Andric unsigned PAlignment = PStore->getAlignment();
30352cab237bSDimitry Andric unsigned QAlignment = QStore->getAlignment();
30362cab237bSDimitry Andric unsigned TypeAlignment =
30372cab237bSDimitry Andric DL.getABITypeAlignment(SI->getValueOperand()->getType());
30382cab237bSDimitry Andric unsigned MinAlignment;
30392cab237bSDimitry Andric unsigned MaxAlignment;
30402cab237bSDimitry Andric std::tie(MinAlignment, MaxAlignment) = std::minmax(PAlignment, QAlignment);
30412cab237bSDimitry Andric // Choose the minimum alignment. If we could prove both stores execute, we
30422cab237bSDimitry Andric // could use biggest one. In this case, though, we only know that one of the
30432cab237bSDimitry Andric // stores executes. And we don't know it's safe to take the alignment from a
30442cab237bSDimitry Andric // store that doesn't execute.
30452cab237bSDimitry Andric if (MinAlignment != 0) {
30462cab237bSDimitry Andric // Choose the minimum of all non-zero alignments.
30472cab237bSDimitry Andric SI->setAlignment(MinAlignment);
30482cab237bSDimitry Andric } else if (MaxAlignment != 0) {
30492cab237bSDimitry Andric // Choose the minimal alignment between the non-zero alignment and the ABI
30502cab237bSDimitry Andric // default alignment for the type of the stored value.
30512cab237bSDimitry Andric SI->setAlignment(std::min(MaxAlignment, TypeAlignment));
30522cab237bSDimitry Andric } else {
30532cab237bSDimitry Andric // If both alignments are zero, use ABI default alignment for the type of
30542cab237bSDimitry Andric // the stored value.
30552cab237bSDimitry Andric SI->setAlignment(TypeAlignment);
30562cab237bSDimitry Andric }
30577d523365SDimitry Andric
30587d523365SDimitry Andric QStore->eraseFromParent();
30597d523365SDimitry Andric PStore->eraseFromParent();
30607d523365SDimitry Andric
30617d523365SDimitry Andric return true;
30627d523365SDimitry Andric }
30637d523365SDimitry Andric
mergeConditionalStores(BranchInst * PBI,BranchInst * QBI,const DataLayout & DL)30642cab237bSDimitry Andric static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI,
30652cab237bSDimitry Andric const DataLayout &DL) {
30667d523365SDimitry Andric // The intention here is to find diamonds or triangles (see below) where each
30677d523365SDimitry Andric // conditional block contains a store to the same address. Both of these
30687d523365SDimitry Andric // stores are conditional, so they can't be unconditionally sunk. But it may
30697d523365SDimitry Andric // be profitable to speculatively sink the stores into one merged store at the
30707d523365SDimitry Andric // end, and predicate the merged store on the union of the two conditions of
30717d523365SDimitry Andric // PBI and QBI.
30727d523365SDimitry Andric //
30737d523365SDimitry Andric // This can reduce the number of stores executed if both of the conditions are
30747d523365SDimitry Andric // true, and can allow the blocks to become small enough to be if-converted.
30757d523365SDimitry Andric // This optimization will also chain, so that ladders of test-and-set
30767d523365SDimitry Andric // sequences can be if-converted away.
30777d523365SDimitry Andric //
30787d523365SDimitry Andric // We only deal with simple diamonds or triangles:
30797d523365SDimitry Andric //
30807d523365SDimitry Andric // PBI or PBI or a combination of the two
30817d523365SDimitry Andric // / \ | \
30827d523365SDimitry Andric // PTB PFB | PFB
30837d523365SDimitry Andric // \ / | /
30847d523365SDimitry Andric // QBI QBI
30857d523365SDimitry Andric // / \ | \
30867d523365SDimitry Andric // QTB QFB | QFB
30877d523365SDimitry Andric // \ / | /
30887d523365SDimitry Andric // PostBB PostBB
30897d523365SDimitry Andric //
30907d523365SDimitry Andric // We model triangles as a type of diamond with a nullptr "true" block.
30917d523365SDimitry Andric // Triangles are canonicalized so that the fallthrough edge is represented by
30927d523365SDimitry Andric // a true condition, as in the diagram above.
30937d523365SDimitry Andric BasicBlock *PTB = PBI->getSuccessor(0);
30947d523365SDimitry Andric BasicBlock *PFB = PBI->getSuccessor(1);
30957d523365SDimitry Andric BasicBlock *QTB = QBI->getSuccessor(0);
30967d523365SDimitry Andric BasicBlock *QFB = QBI->getSuccessor(1);
30977d523365SDimitry Andric BasicBlock *PostBB = QFB->getSingleSuccessor();
30987d523365SDimitry Andric
309951690af2SDimitry Andric // Make sure we have a good guess for PostBB. If QTB's only successor is
310051690af2SDimitry Andric // QFB, then QFB is a better PostBB.
310151690af2SDimitry Andric if (QTB->getSingleSuccessor() == QFB)
310251690af2SDimitry Andric PostBB = QFB;
310351690af2SDimitry Andric
310451690af2SDimitry Andric // If we couldn't find a good PostBB, stop.
310551690af2SDimitry Andric if (!PostBB)
310651690af2SDimitry Andric return false;
310751690af2SDimitry Andric
31087d523365SDimitry Andric bool InvertPCond = false, InvertQCond = false;
31097d523365SDimitry Andric // Canonicalize fallthroughs to the true branches.
31107d523365SDimitry Andric if (PFB == QBI->getParent()) {
31117d523365SDimitry Andric std::swap(PFB, PTB);
31127d523365SDimitry Andric InvertPCond = true;
31137d523365SDimitry Andric }
31147d523365SDimitry Andric if (QFB == PostBB) {
31157d523365SDimitry Andric std::swap(QFB, QTB);
31167d523365SDimitry Andric InvertQCond = true;
31177d523365SDimitry Andric }
31187d523365SDimitry Andric
31197d523365SDimitry Andric // From this point on we can assume PTB or QTB may be fallthroughs but PFB
31207d523365SDimitry Andric // and QFB may not. Model fallthroughs as a nullptr block.
31217d523365SDimitry Andric if (PTB == QBI->getParent())
31227d523365SDimitry Andric PTB = nullptr;
31237d523365SDimitry Andric if (QTB == PostBB)
31247d523365SDimitry Andric QTB = nullptr;
31257d523365SDimitry Andric
31267d523365SDimitry Andric // Legality bailouts. We must have at least the non-fallthrough blocks and
31277d523365SDimitry Andric // the post-dominating block, and the non-fallthroughs must only have one
31287d523365SDimitry Andric // predecessor.
31297d523365SDimitry Andric auto HasOnePredAndOneSucc = [](BasicBlock *BB, BasicBlock *P, BasicBlock *S) {
31303ca95b02SDimitry Andric return BB->getSinglePredecessor() == P && BB->getSingleSuccessor() == S;
31317d523365SDimitry Andric };
313251690af2SDimitry Andric if (!HasOnePredAndOneSucc(PFB, PBI->getParent(), QBI->getParent()) ||
31337d523365SDimitry Andric !HasOnePredAndOneSucc(QFB, QBI->getParent(), PostBB))
31347d523365SDimitry Andric return false;
31357d523365SDimitry Andric if ((PTB && !HasOnePredAndOneSucc(PTB, PBI->getParent(), QBI->getParent())) ||
31367d523365SDimitry Andric (QTB && !HasOnePredAndOneSucc(QTB, QBI->getParent(), PostBB)))
31377d523365SDimitry Andric return false;
31384ba319b5SDimitry Andric if (!QBI->getParent()->hasNUses(2))
31397d523365SDimitry Andric return false;
31407d523365SDimitry Andric
31417d523365SDimitry Andric // OK, this is a sequence of two diamonds or triangles.
31427d523365SDimitry Andric // Check if there are stores in PTB or PFB that are repeated in QTB or QFB.
31437d523365SDimitry Andric SmallPtrSet<Value *, 4> PStoreAddresses, QStoreAddresses;
31447d523365SDimitry Andric for (auto *BB : {PTB, PFB}) {
31457d523365SDimitry Andric if (!BB)
31467d523365SDimitry Andric continue;
31477d523365SDimitry Andric for (auto &I : *BB)
31487d523365SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(&I))
31497d523365SDimitry Andric PStoreAddresses.insert(SI->getPointerOperand());
31507d523365SDimitry Andric }
31517d523365SDimitry Andric for (auto *BB : {QTB, QFB}) {
31527d523365SDimitry Andric if (!BB)
31537d523365SDimitry Andric continue;
31547d523365SDimitry Andric for (auto &I : *BB)
31557d523365SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(&I))
31567d523365SDimitry Andric QStoreAddresses.insert(SI->getPointerOperand());
31577d523365SDimitry Andric }
31587d523365SDimitry Andric
31597d523365SDimitry Andric set_intersect(PStoreAddresses, QStoreAddresses);
31607d523365SDimitry Andric // set_intersect mutates PStoreAddresses in place. Rename it here to make it
31617d523365SDimitry Andric // clear what it contains.
31627d523365SDimitry Andric auto &CommonAddresses = PStoreAddresses;
31637d523365SDimitry Andric
31647d523365SDimitry Andric bool Changed = false;
31657d523365SDimitry Andric for (auto *Address : CommonAddresses)
31667d523365SDimitry Andric Changed |= mergeConditionalStoreToAddress(
31672cab237bSDimitry Andric PTB, PFB, QTB, QFB, PostBB, Address, InvertPCond, InvertQCond, DL);
31687d523365SDimitry Andric return Changed;
31697d523365SDimitry Andric }
31707d523365SDimitry Andric
31713dac3a9bSDimitry Andric /// If we have a conditional branch as a predecessor of another block,
31723dac3a9bSDimitry Andric /// this function tries to simplify it. We know
3173f22ef01cSRoman Divacky /// that PBI and BI are both conditional branches, and BI is in one of the
3174f22ef01cSRoman Divacky /// successor blocks of PBI - PBI branches to BI.
SimplifyCondBranchToCondBranch(BranchInst * PBI,BranchInst * BI,const DataLayout & DL)31757d523365SDimitry Andric static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
31767d523365SDimitry Andric const DataLayout &DL) {
3177f22ef01cSRoman Divacky assert(PBI->isConditional() && BI->isConditional());
3178f22ef01cSRoman Divacky BasicBlock *BB = BI->getParent();
3179f22ef01cSRoman Divacky
3180f22ef01cSRoman Divacky // If this block ends with a branch instruction, and if there is a
3181f22ef01cSRoman Divacky // predecessor that ends on a branch of the same condition, make
3182f22ef01cSRoman Divacky // this conditional branch redundant.
3183f22ef01cSRoman Divacky if (PBI->getCondition() == BI->getCondition() &&
3184f22ef01cSRoman Divacky PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3185f22ef01cSRoman Divacky // Okay, the outcome of this conditional branch is statically
3186f22ef01cSRoman Divacky // knowable. If this block had a single pred, handle specially.
3187f22ef01cSRoman Divacky if (BB->getSinglePredecessor()) {
3188f22ef01cSRoman Divacky // Turn this into a branch on constant.
3189f22ef01cSRoman Divacky bool CondIsTrue = PBI->getSuccessor(0) == BB;
31903ca95b02SDimitry Andric BI->setCondition(
31913ca95b02SDimitry Andric ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue));
3192f22ef01cSRoman Divacky return true; // Nuke the branch on constant.
3193f22ef01cSRoman Divacky }
3194f22ef01cSRoman Divacky
3195f22ef01cSRoman Divacky // Otherwise, if there are multiple predecessors, insert a PHI that merges
3196f22ef01cSRoman Divacky // in the constant and simplify the block result. Subsequent passes of
3197f22ef01cSRoman Divacky // simplifycfg will thread the block.
3198f22ef01cSRoman Divacky if (BlockIsSimpleEnoughToThreadThrough(BB)) {
31993b0f4066SDimitry Andric pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
32007d523365SDimitry Andric PHINode *NewPN = PHINode::Create(
32017d523365SDimitry Andric Type::getInt1Ty(BB->getContext()), std::distance(PB, PE),
32027d523365SDimitry Andric BI->getCondition()->getName() + ".pr", &BB->front());
3203f22ef01cSRoman Divacky // Okay, we're going to insert the PHI node. Since PBI is not the only
3204f22ef01cSRoman Divacky // predecessor, compute the PHI'd conditional value for all of the preds.
3205f22ef01cSRoman Divacky // Any predecessor where the condition is not computable we keep symbolic.
32063b0f4066SDimitry Andric for (pred_iterator PI = PB; PI != PE; ++PI) {
3207ffd1746dSEd Schouten BasicBlock *P = *PI;
32083ca95b02SDimitry Andric if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && PBI != BI &&
32093ca95b02SDimitry Andric PBI->isConditional() && PBI->getCondition() == BI->getCondition() &&
3210f22ef01cSRoman Divacky PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
3211f22ef01cSRoman Divacky bool CondIsTrue = PBI->getSuccessor(0) == BB;
32123ca95b02SDimitry Andric NewPN->addIncoming(
32133ca95b02SDimitry Andric ConstantInt::get(Type::getInt1Ty(BB->getContext()), CondIsTrue),
32143ca95b02SDimitry Andric P);
3215f22ef01cSRoman Divacky } else {
3216ffd1746dSEd Schouten NewPN->addIncoming(BI->getCondition(), P);
3217ffd1746dSEd Schouten }
3218f22ef01cSRoman Divacky }
3219f22ef01cSRoman Divacky
3220f22ef01cSRoman Divacky BI->setCondition(NewPN);
3221f22ef01cSRoman Divacky return true;
3222f22ef01cSRoman Divacky }
3223f22ef01cSRoman Divacky }
3224f22ef01cSRoman Divacky
32257d523365SDimitry Andric if (auto *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
32267d523365SDimitry Andric if (CE->canTrap())
32277d523365SDimitry Andric return false;
32287d523365SDimitry Andric
32297d523365SDimitry Andric // If both branches are conditional and both contain stores to the same
32307d523365SDimitry Andric // address, remove the stores from the conditionals and create a conditional
32317d523365SDimitry Andric // merged store at the end.
32322cab237bSDimitry Andric if (MergeCondStores && mergeConditionalStores(PBI, BI, DL))
32337d523365SDimitry Andric return true;
32347d523365SDimitry Andric
3235f22ef01cSRoman Divacky // If this is a conditional branch in an empty block, and if any
323691bc56edSDimitry Andric // predecessors are a conditional branch to one of our destinations,
3237f22ef01cSRoman Divacky // fold the conditions into logical ops and one cond br.
32384ba319b5SDimitry Andric
3239f22ef01cSRoman Divacky // Ignore dbg intrinsics.
32404ba319b5SDimitry Andric if (&*BB->instructionsWithoutDebug().begin() != BI)
3241f22ef01cSRoman Divacky return false;
3242f22ef01cSRoman Divacky
3243f22ef01cSRoman Divacky int PBIOp, BIOp;
32443ca95b02SDimitry Andric if (PBI->getSuccessor(0) == BI->getSuccessor(0)) {
32453ca95b02SDimitry Andric PBIOp = 0;
32463ca95b02SDimitry Andric BIOp = 0;
32473ca95b02SDimitry Andric } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) {
32483ca95b02SDimitry Andric PBIOp = 0;
32493ca95b02SDimitry Andric BIOp = 1;
32503ca95b02SDimitry Andric } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) {
32513ca95b02SDimitry Andric PBIOp = 1;
32523ca95b02SDimitry Andric BIOp = 0;
32533ca95b02SDimitry Andric } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) {
32543ca95b02SDimitry Andric PBIOp = 1;
32553ca95b02SDimitry Andric BIOp = 1;
32563ca95b02SDimitry Andric } else {
3257f22ef01cSRoman Divacky return false;
32583ca95b02SDimitry Andric }
3259f22ef01cSRoman Divacky
3260f22ef01cSRoman Divacky // Check to make sure that the other destination of this branch
3261f22ef01cSRoman Divacky // isn't BB itself. If so, this is an infinite loop that will
3262f22ef01cSRoman Divacky // keep getting unwound.
3263f22ef01cSRoman Divacky if (PBI->getSuccessor(PBIOp) == BB)
3264f22ef01cSRoman Divacky return false;
3265f22ef01cSRoman Divacky
3266f22ef01cSRoman Divacky // Do not perform this transformation if it would require
3267f22ef01cSRoman Divacky // insertion of a large number of select instructions. For targets
3268f22ef01cSRoman Divacky // without predication/cmovs, this is a big pessimization.
3269f22ef01cSRoman Divacky
327091bc56edSDimitry Andric // Also do not perform this transformation if any phi node in the common
327191bc56edSDimitry Andric // destination block can trap when reached by BB or PBB (PR17073). In that
327291bc56edSDimitry Andric // case, it would be unsafe to hoist the operation into a select instruction.
327391bc56edSDimitry Andric
327491bc56edSDimitry Andric BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
3275f22ef01cSRoman Divacky unsigned NumPhis = 0;
32763ca95b02SDimitry Andric for (BasicBlock::iterator II = CommonDest->begin(); isa<PHINode>(II);
32773ca95b02SDimitry Andric ++II, ++NumPhis) {
3278f22ef01cSRoman Divacky if (NumPhis > 2) // Disable this xform.
3279f22ef01cSRoman Divacky return false;
3280f22ef01cSRoman Divacky
328191bc56edSDimitry Andric PHINode *PN = cast<PHINode>(II);
328291bc56edSDimitry Andric Value *BIV = PN->getIncomingValueForBlock(BB);
328391bc56edSDimitry Andric if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BIV))
328491bc56edSDimitry Andric if (CE->canTrap())
328591bc56edSDimitry Andric return false;
328691bc56edSDimitry Andric
328791bc56edSDimitry Andric unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
328891bc56edSDimitry Andric Value *PBIV = PN->getIncomingValue(PBBIdx);
328991bc56edSDimitry Andric if (ConstantExpr *CE = dyn_cast<ConstantExpr>(PBIV))
329091bc56edSDimitry Andric if (CE->canTrap())
329191bc56edSDimitry Andric return false;
329291bc56edSDimitry Andric }
329391bc56edSDimitry Andric
3294f22ef01cSRoman Divacky // Finally, if everything is ok, fold the branches to logical ops.
3295f22ef01cSRoman Divacky BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
3296f22ef01cSRoman Divacky
32974ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
3298f22ef01cSRoman Divacky << "AND: " << *BI->getParent());
3299f22ef01cSRoman Divacky
3300f22ef01cSRoman Divacky // If OtherDest *is* BB, then BB is a basic block with a single conditional
3301f22ef01cSRoman Divacky // branch in it, where one edge (OtherDest) goes back to itself but the other
3302f22ef01cSRoman Divacky // exits. We don't *know* that the program avoids the infinite loop
3303f22ef01cSRoman Divacky // (even though that seems likely). If we do this xform naively, we'll end up
3304f22ef01cSRoman Divacky // recursively unpeeling the loop. Since we know that (after the xform is
3305f22ef01cSRoman Divacky // done) that the block *is* infinite if reached, we just make it an obviously
3306f22ef01cSRoman Divacky // infinite loop with no cond branch.
3307f22ef01cSRoman Divacky if (OtherDest == BB) {
3308f22ef01cSRoman Divacky // Insert it at the end of the function, because it's either code,
3309f22ef01cSRoman Divacky // or it won't matter if it's hot. :)
33103ca95b02SDimitry Andric BasicBlock *InfLoopBlock =
33113ca95b02SDimitry Andric BasicBlock::Create(BB->getContext(), "infloop", BB->getParent());
3312f22ef01cSRoman Divacky BranchInst::Create(InfLoopBlock, InfLoopBlock);
3313f22ef01cSRoman Divacky OtherDest = InfLoopBlock;
3314f22ef01cSRoman Divacky }
3315f22ef01cSRoman Divacky
33164ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3317f22ef01cSRoman Divacky
3318f22ef01cSRoman Divacky // BI may have other predecessors. Because of this, we leave
3319f22ef01cSRoman Divacky // it alone, but modify PBI.
3320f22ef01cSRoman Divacky
3321f22ef01cSRoman Divacky // Make sure we get to CommonDest on True&True directions.
3322f22ef01cSRoman Divacky Value *PBICond = PBI->getCondition();
33233ca95b02SDimitry Andric IRBuilder<NoFolder> Builder(PBI);
3324f22ef01cSRoman Divacky if (PBIOp)
3325bd5abe19SDimitry Andric PBICond = Builder.CreateNot(PBICond, PBICond->getName() + ".not");
3326bd5abe19SDimitry Andric
3327f22ef01cSRoman Divacky Value *BICond = BI->getCondition();
3328f22ef01cSRoman Divacky if (BIOp)
3329bd5abe19SDimitry Andric BICond = Builder.CreateNot(BICond, BICond->getName() + ".not");
3330bd5abe19SDimitry Andric
3331f22ef01cSRoman Divacky // Merge the conditions.
3332bd5abe19SDimitry Andric Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
3333f22ef01cSRoman Divacky
3334f22ef01cSRoman Divacky // Modify PBI to branch on the new condition to the new dests.
3335f22ef01cSRoman Divacky PBI->setCondition(Cond);
3336f22ef01cSRoman Divacky PBI->setSuccessor(0, CommonDest);
3337f22ef01cSRoman Divacky PBI->setSuccessor(1, OtherDest);
3338f22ef01cSRoman Divacky
33393861d79fSDimitry Andric // Update branch weight for PBI.
33403861d79fSDimitry Andric uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
33413ca95b02SDimitry Andric uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
33423ca95b02SDimitry Andric bool HasWeights =
33433ca95b02SDimitry Andric extractPredSuccWeights(PBI, BI, PredTrueWeight, PredFalseWeight,
33443ca95b02SDimitry Andric SuccTrueWeight, SuccFalseWeight);
33453ca95b02SDimitry Andric if (HasWeights) {
33463ca95b02SDimitry Andric PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
33473ca95b02SDimitry Andric PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
33483ca95b02SDimitry Andric SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
33493ca95b02SDimitry Andric SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
33503861d79fSDimitry Andric // The weight to CommonDest should be PredCommon * SuccTotal +
33513861d79fSDimitry Andric // PredOther * SuccCommon.
33523861d79fSDimitry Andric // The weight to OtherDest should be PredOther * SuccOther.
3353ff0cc061SDimitry Andric uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
3354ff0cc061SDimitry Andric PredOther * SuccCommon,
3355ff0cc061SDimitry Andric PredOther * SuccOther};
33563861d79fSDimitry Andric // Halve the weights if any of them cannot fit in an uint32_t
33573861d79fSDimitry Andric FitWeights(NewWeights);
33583861d79fSDimitry Andric
33592cab237bSDimitry Andric setBranchWeights(PBI, NewWeights[0], NewWeights[1]);
33603861d79fSDimitry Andric }
33613861d79fSDimitry Andric
3362f22ef01cSRoman Divacky // OtherDest may have phi nodes. If so, add an entry from PBI's
3363f22ef01cSRoman Divacky // block that are identical to the entries for BI's block.
33642754fe60SDimitry Andric AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
3365f22ef01cSRoman Divacky
3366f22ef01cSRoman Divacky // We know that the CommonDest already had an edge from PBI to
3367f22ef01cSRoman Divacky // it. If it has PHIs though, the PHIs may have different
3368f22ef01cSRoman Divacky // entries for BB and PBI's BB. If so, insert a select to make
3369f22ef01cSRoman Divacky // them agree.
337030785c0eSDimitry Andric for (PHINode &PN : CommonDest->phis()) {
337130785c0eSDimitry Andric Value *BIV = PN.getIncomingValueForBlock(BB);
337230785c0eSDimitry Andric unsigned PBBIdx = PN.getBasicBlockIndex(PBI->getParent());
337330785c0eSDimitry Andric Value *PBIV = PN.getIncomingValue(PBBIdx);
3374f22ef01cSRoman Divacky if (BIV != PBIV) {
3375f22ef01cSRoman Divacky // Insert a select in PBI to pick the right value.
33763ca95b02SDimitry Andric SelectInst *NV = cast<SelectInst>(
33773ca95b02SDimitry Andric Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
337830785c0eSDimitry Andric PN.setIncomingValue(PBBIdx, NV);
33793ca95b02SDimitry Andric // Although the select has the same condition as PBI, the original branch
33803ca95b02SDimitry Andric // weights for PBI do not apply to the new select because the select's
33813ca95b02SDimitry Andric // 'logical' edges are incoming edges of the phi that is eliminated, not
33823ca95b02SDimitry Andric // the outgoing edges of PBI.
33833ca95b02SDimitry Andric if (HasWeights) {
33843ca95b02SDimitry Andric uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
33853ca95b02SDimitry Andric uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
33863ca95b02SDimitry Andric uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
33873ca95b02SDimitry Andric uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
33883ca95b02SDimitry Andric // The weight to PredCommonDest should be PredCommon * SuccTotal.
33893ca95b02SDimitry Andric // The weight to PredOtherDest should be PredOther * SuccCommon.
33903ca95b02SDimitry Andric uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
33913ca95b02SDimitry Andric PredOther * SuccCommon};
33923ca95b02SDimitry Andric
33933ca95b02SDimitry Andric FitWeights(NewWeights);
33943ca95b02SDimitry Andric
33952cab237bSDimitry Andric setBranchWeights(NV, NewWeights[0], NewWeights[1]);
33963ca95b02SDimitry Andric }
3397f22ef01cSRoman Divacky }
3398f22ef01cSRoman Divacky }
3399f22ef01cSRoman Divacky
34004ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "INTO: " << *PBI->getParent());
34014ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << *PBI->getParent()->getParent());
3402f22ef01cSRoman Divacky
3403f22ef01cSRoman Divacky // This basic block is probably dead. We know it has at least
3404f22ef01cSRoman Divacky // one fewer predecessor.
3405f22ef01cSRoman Divacky return true;
3406f22ef01cSRoman Divacky }
3407f22ef01cSRoman Divacky
34083dac3a9bSDimitry Andric // Simplifies a terminator by replacing it with a branch to TrueBB if Cond is
34093dac3a9bSDimitry Andric // true or to FalseBB if Cond is false.
34102754fe60SDimitry Andric // Takes care of updating the successors and removing the old terminator.
34112754fe60SDimitry Andric // Also makes sure not to introduce new successors by assuming that edges to
34122754fe60SDimitry Andric // non-successor TrueBBs and FalseBBs aren't reachable.
SimplifyTerminatorOnSelect(Instruction * OldTerm,Value * Cond,BasicBlock * TrueBB,BasicBlock * FalseBB,uint32_t TrueWeight,uint32_t FalseWeight)3413*b5893f02SDimitry Andric static bool SimplifyTerminatorOnSelect(Instruction *OldTerm, Value *Cond,
34143861d79fSDimitry Andric BasicBlock *TrueBB, BasicBlock *FalseBB,
34153861d79fSDimitry Andric uint32_t TrueWeight,
34163861d79fSDimitry Andric uint32_t FalseWeight) {
34172754fe60SDimitry Andric // Remove any superfluous successor edges from the CFG.
34182754fe60SDimitry Andric // First, figure out which successors to preserve.
34192754fe60SDimitry Andric // If TrueBB and FalseBB are equal, only try to preserve one copy of that
34202754fe60SDimitry Andric // successor.
34212754fe60SDimitry Andric BasicBlock *KeepEdge1 = TrueBB;
342291bc56edSDimitry Andric BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : nullptr;
3423f22ef01cSRoman Divacky
34242754fe60SDimitry Andric // Then remove the rest.
3425*b5893f02SDimitry Andric for (BasicBlock *Succ : successors(OldTerm)) {
34262754fe60SDimitry Andric // Make sure only to keep exactly one copy of each edge.
34272754fe60SDimitry Andric if (Succ == KeepEdge1)
342891bc56edSDimitry Andric KeepEdge1 = nullptr;
34292754fe60SDimitry Andric else if (Succ == KeepEdge2)
343091bc56edSDimitry Andric KeepEdge2 = nullptr;
34312754fe60SDimitry Andric else
34327d523365SDimitry Andric Succ->removePredecessor(OldTerm->getParent(),
34337d523365SDimitry Andric /*DontDeleteUselessPHIs=*/true);
34342754fe60SDimitry Andric }
3435f22ef01cSRoman Divacky
3436bd5abe19SDimitry Andric IRBuilder<> Builder(OldTerm);
3437bd5abe19SDimitry Andric Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
3438bd5abe19SDimitry Andric
34392754fe60SDimitry Andric // Insert an appropriate new terminator.
344091bc56edSDimitry Andric if (!KeepEdge1 && !KeepEdge2) {
34412754fe60SDimitry Andric if (TrueBB == FalseBB)
34422754fe60SDimitry Andric // We were only looking for one successor, and it was present.
34432754fe60SDimitry Andric // Create an unconditional branch to it.
3444bd5abe19SDimitry Andric Builder.CreateBr(TrueBB);
34453861d79fSDimitry Andric else {
34462754fe60SDimitry Andric // We found both of the successors we were looking for.
34472754fe60SDimitry Andric // Create a conditional branch sharing the condition of the select.
34483861d79fSDimitry Andric BranchInst *NewBI = Builder.CreateCondBr(Cond, TrueBB, FalseBB);
34493861d79fSDimitry Andric if (TrueWeight != FalseWeight)
34502cab237bSDimitry Andric setBranchWeights(NewBI, TrueWeight, FalseWeight);
34513861d79fSDimitry Andric }
34522754fe60SDimitry Andric } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
34532754fe60SDimitry Andric // Neither of the selected blocks were successors, so this
34542754fe60SDimitry Andric // terminator must be unreachable.
34552754fe60SDimitry Andric new UnreachableInst(OldTerm->getContext(), OldTerm);
34562754fe60SDimitry Andric } else {
34572754fe60SDimitry Andric // One of the selected values was a successor, but the other wasn't.
34582754fe60SDimitry Andric // Insert an unconditional branch to the one that was found;
34592754fe60SDimitry Andric // the edge to the one that wasn't must be unreachable.
346091bc56edSDimitry Andric if (!KeepEdge1)
34612754fe60SDimitry Andric // Only TrueBB was found.
3462bd5abe19SDimitry Andric Builder.CreateBr(TrueBB);
34632754fe60SDimitry Andric else
34642754fe60SDimitry Andric // Only FalseBB was found.
3465bd5abe19SDimitry Andric Builder.CreateBr(FalseBB);
34662754fe60SDimitry Andric }
34672754fe60SDimitry Andric
3468*b5893f02SDimitry Andric EraseTerminatorAndDCECond(OldTerm);
3469f22ef01cSRoman Divacky return true;
3470f22ef01cSRoman Divacky }
3471f22ef01cSRoman Divacky
34723dac3a9bSDimitry Andric // Replaces
34733b0f4066SDimitry Andric // (switch (select cond, X, Y)) on constant X, Y
34743b0f4066SDimitry Andric // with a branch - conditional if X and Y lead to distinct BBs,
34753b0f4066SDimitry Andric // unconditional otherwise.
SimplifySwitchOnSelect(SwitchInst * SI,SelectInst * Select)34763b0f4066SDimitry Andric static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) {
34773b0f4066SDimitry Andric // Check for constant integer values in the select.
34783b0f4066SDimitry Andric ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
34793b0f4066SDimitry Andric ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
34803b0f4066SDimitry Andric if (!TrueVal || !FalseVal)
34813b0f4066SDimitry Andric return false;
34823b0f4066SDimitry Andric
34833b0f4066SDimitry Andric // Find the relevant condition and destinations.
34843b0f4066SDimitry Andric Value *Condition = Select->getCondition();
34857a7e6055SDimitry Andric BasicBlock *TrueBB = SI->findCaseValue(TrueVal)->getCaseSuccessor();
34867a7e6055SDimitry Andric BasicBlock *FalseBB = SI->findCaseValue(FalseVal)->getCaseSuccessor();
34873b0f4066SDimitry Andric
34883861d79fSDimitry Andric // Get weight for TrueBB and FalseBB.
34893861d79fSDimitry Andric uint32_t TrueWeight = 0, FalseWeight = 0;
34903861d79fSDimitry Andric SmallVector<uint64_t, 8> Weights;
34913861d79fSDimitry Andric bool HasWeights = HasBranchWeights(SI);
34923861d79fSDimitry Andric if (HasWeights) {
34933861d79fSDimitry Andric GetBranchWeights(SI, Weights);
34943861d79fSDimitry Andric if (Weights.size() == 1 + SI->getNumCases()) {
34953ca95b02SDimitry Andric TrueWeight =
34967a7e6055SDimitry Andric (uint32_t)Weights[SI->findCaseValue(TrueVal)->getSuccessorIndex()];
34973ca95b02SDimitry Andric FalseWeight =
34987a7e6055SDimitry Andric (uint32_t)Weights[SI->findCaseValue(FalseVal)->getSuccessorIndex()];
34993861d79fSDimitry Andric }
35003861d79fSDimitry Andric }
35013861d79fSDimitry Andric
35023b0f4066SDimitry Andric // Perform the actual simplification.
35033ca95b02SDimitry Andric return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
35043ca95b02SDimitry Andric FalseWeight);
35053b0f4066SDimitry Andric }
35063b0f4066SDimitry Andric
35073dac3a9bSDimitry Andric // Replaces
35082754fe60SDimitry Andric // (indirectbr (select cond, blockaddress(@fn, BlockA),
35092754fe60SDimitry Andric // blockaddress(@fn, BlockB)))
35102754fe60SDimitry Andric // with
35112754fe60SDimitry Andric // (br cond, BlockA, BlockB).
SimplifyIndirectBrOnSelect(IndirectBrInst * IBI,SelectInst * SI)35122754fe60SDimitry Andric static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) {
35132754fe60SDimitry Andric // Check that both operands of the select are block addresses.
35142754fe60SDimitry Andric BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
35152754fe60SDimitry Andric BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
35162754fe60SDimitry Andric if (!TBA || !FBA)
35172754fe60SDimitry Andric return false;
3518f22ef01cSRoman Divacky
35192754fe60SDimitry Andric // Extract the actual blocks.
35202754fe60SDimitry Andric BasicBlock *TrueBB = TBA->getBasicBlock();
35212754fe60SDimitry Andric BasicBlock *FalseBB = FBA->getBasicBlock();
3522f22ef01cSRoman Divacky
35232754fe60SDimitry Andric // Perform the actual simplification.
35243ca95b02SDimitry Andric return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB, 0,
35253ca95b02SDimitry Andric 0);
35262754fe60SDimitry Andric }
3527f22ef01cSRoman Divacky
35283dac3a9bSDimitry Andric /// This is called when we find an icmp instruction
35293dac3a9bSDimitry Andric /// (a seteq/setne with a constant) as the only instruction in a
35302754fe60SDimitry Andric /// block that ends with an uncond branch. We are looking for a very specific
35312754fe60SDimitry Andric /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
35322754fe60SDimitry Andric /// this case, we merge the first two "or's of icmp" into a switch, but then the
35332754fe60SDimitry Andric /// default value goes to an uncond block with a seteq in it, we get something
35342754fe60SDimitry Andric /// like:
35352754fe60SDimitry Andric ///
35362754fe60SDimitry Andric /// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
35372754fe60SDimitry Andric /// DEFAULT:
35382754fe60SDimitry Andric /// %tmp = icmp eq i8 %A, 92
35392754fe60SDimitry Andric /// br label %end
35402754fe60SDimitry Andric /// end:
35412754fe60SDimitry Andric /// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
35422754fe60SDimitry Andric ///
35432754fe60SDimitry Andric /// We prefer to split the edge to 'end' so that there is a true/false entry to
35442754fe60SDimitry Andric /// the PHI, merging the third icmp into the switch.
tryToSimplifyUncondBranchWithICmpInIt(ICmpInst * ICI,IRBuilder<> & Builder)3545*b5893f02SDimitry Andric bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
3546*b5893f02SDimitry Andric ICmpInst *ICI, IRBuilder<> &Builder) {
35472754fe60SDimitry Andric BasicBlock *BB = ICI->getParent();
3548bd5abe19SDimitry Andric
35492754fe60SDimitry Andric // If the block has any PHIs in it or the icmp has multiple uses, it is too
35502754fe60SDimitry Andric // complex.
35513ca95b02SDimitry Andric if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse())
35523ca95b02SDimitry Andric return false;
35532754fe60SDimitry Andric
35542754fe60SDimitry Andric Value *V = ICI->getOperand(0);
35552754fe60SDimitry Andric ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
35562754fe60SDimitry Andric
35572754fe60SDimitry Andric // The pattern we're looking for is where our only predecessor is a switch on
35582754fe60SDimitry Andric // 'V' and this block is the default case for the switch. In this case we can
35592754fe60SDimitry Andric // fold the compared value into the switch to simplify things.
35602754fe60SDimitry Andric BasicBlock *Pred = BB->getSinglePredecessor();
35613ca95b02SDimitry Andric if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
35623ca95b02SDimitry Andric return false;
35632754fe60SDimitry Andric
35642754fe60SDimitry Andric SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
35652754fe60SDimitry Andric if (SI->getCondition() != V)
35662754fe60SDimitry Andric return false;
35672754fe60SDimitry Andric
35682754fe60SDimitry Andric // If BB is reachable on a non-default case, then we simply know the value of
35692754fe60SDimitry Andric // V in this block. Substitute it and constant fold the icmp instruction
35702754fe60SDimitry Andric // away.
35712754fe60SDimitry Andric if (SI->getDefaultDest() != BB) {
35722754fe60SDimitry Andric ConstantInt *VVal = SI->findCaseDest(BB);
35732754fe60SDimitry Andric assert(VVal && "Should have a unique destination value");
35742754fe60SDimitry Andric ICI->setOperand(0, VVal);
35752754fe60SDimitry Andric
3576f37b6182SDimitry Andric if (Value *V = SimplifyInstruction(ICI, {DL, ICI})) {
35772754fe60SDimitry Andric ICI->replaceAllUsesWith(V);
35782754fe60SDimitry Andric ICI->eraseFromParent();
35792754fe60SDimitry Andric }
35802754fe60SDimitry Andric // BB is now empty, so it is likely to simplify away.
3581*b5893f02SDimitry Andric return requestResimplify();
35822754fe60SDimitry Andric }
35832754fe60SDimitry Andric
35842754fe60SDimitry Andric // Ok, the block is reachable from the default dest. If the constant we're
35852754fe60SDimitry Andric // comparing exists in one of the other edges, then we can constant fold ICI
35862754fe60SDimitry Andric // and zap it.
3587dff0c46cSDimitry Andric if (SI->findCaseValue(Cst) != SI->case_default()) {
35882754fe60SDimitry Andric Value *V;
35892754fe60SDimitry Andric if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
35902754fe60SDimitry Andric V = ConstantInt::getFalse(BB->getContext());
35912754fe60SDimitry Andric else
35922754fe60SDimitry Andric V = ConstantInt::getTrue(BB->getContext());
35932754fe60SDimitry Andric
35942754fe60SDimitry Andric ICI->replaceAllUsesWith(V);
35952754fe60SDimitry Andric ICI->eraseFromParent();
35962754fe60SDimitry Andric // BB is now empty, so it is likely to simplify away.
3597*b5893f02SDimitry Andric return requestResimplify();
35982754fe60SDimitry Andric }
35992754fe60SDimitry Andric
36002754fe60SDimitry Andric // The use of the icmp has to be in the 'end' block, by the only PHI node in
36012754fe60SDimitry Andric // the block.
36022754fe60SDimitry Andric BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
360391bc56edSDimitry Andric PHINode *PHIUse = dyn_cast<PHINode>(ICI->user_back());
360491bc56edSDimitry Andric if (PHIUse == nullptr || PHIUse != &SuccBlock->front() ||
36052754fe60SDimitry Andric isa<PHINode>(++BasicBlock::iterator(PHIUse)))
36062754fe60SDimitry Andric return false;
36072754fe60SDimitry Andric
36082754fe60SDimitry Andric // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
36092754fe60SDimitry Andric // true in the PHI.
36102754fe60SDimitry Andric Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
36112754fe60SDimitry Andric Constant *NewCst = ConstantInt::getFalse(BB->getContext());
36122754fe60SDimitry Andric
36132754fe60SDimitry Andric if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
36142754fe60SDimitry Andric std::swap(DefaultCst, NewCst);
36152754fe60SDimitry Andric
36162754fe60SDimitry Andric // Replace ICI (which is used by the PHI for the default value) with true or
36172754fe60SDimitry Andric // false depending on if it is EQ or NE.
36182754fe60SDimitry Andric ICI->replaceAllUsesWith(DefaultCst);
36192754fe60SDimitry Andric ICI->eraseFromParent();
36202754fe60SDimitry Andric
36212754fe60SDimitry Andric // Okay, the switch goes to this block on a default value. Add an edge from
36222754fe60SDimitry Andric // the switch to the merge point on the compared value.
36233ca95b02SDimitry Andric BasicBlock *NewBB =
36243ca95b02SDimitry Andric BasicBlock::Create(BB->getContext(), "switch.edge", BB->getParent(), BB);
36253861d79fSDimitry Andric SmallVector<uint64_t, 8> Weights;
36263861d79fSDimitry Andric bool HasWeights = HasBranchWeights(SI);
36273861d79fSDimitry Andric if (HasWeights) {
36283861d79fSDimitry Andric GetBranchWeights(SI, Weights);
36293861d79fSDimitry Andric if (Weights.size() == 1 + SI->getNumCases()) {
36303861d79fSDimitry Andric // Split weight for default case to case for "Cst".
36313861d79fSDimitry Andric Weights[0] = (Weights[0] + 1) >> 1;
36323861d79fSDimitry Andric Weights.push_back(Weights[0]);
36333861d79fSDimitry Andric
36343861d79fSDimitry Andric SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
36352cab237bSDimitry Andric setBranchWeights(SI, MDWeights);
36363861d79fSDimitry Andric }
36373861d79fSDimitry Andric }
36382754fe60SDimitry Andric SI->addCase(Cst, NewBB);
36392754fe60SDimitry Andric
36402754fe60SDimitry Andric // NewBB branches to the phi block, add the uncond branch and the phi entry.
3641bd5abe19SDimitry Andric Builder.SetInsertPoint(NewBB);
3642bd5abe19SDimitry Andric Builder.SetCurrentDebugLocation(SI->getDebugLoc());
3643bd5abe19SDimitry Andric Builder.CreateBr(SuccBlock);
36442754fe60SDimitry Andric PHIUse->addIncoming(NewCst, NewBB);
36452754fe60SDimitry Andric return true;
36462754fe60SDimitry Andric }
36472754fe60SDimitry Andric
36483dac3a9bSDimitry Andric /// The specified branch is a conditional branch.
36492754fe60SDimitry Andric /// Check to see if it is branching on an or/and chain of icmp instructions, and
36502754fe60SDimitry Andric /// fold it into a switch instruction if so.
SimplifyBranchOnICmpChain(BranchInst * BI,IRBuilder<> & Builder,const DataLayout & DL)3651ff0cc061SDimitry Andric static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder,
3652ff0cc061SDimitry Andric const DataLayout &DL) {
36532754fe60SDimitry Andric Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
36543ca95b02SDimitry Andric if (!Cond)
36553ca95b02SDimitry Andric return false;
36562754fe60SDimitry Andric
36572754fe60SDimitry Andric // Change br (X == 0 | X == 1), T, F into a switch instruction.
36582754fe60SDimitry Andric // If this is a bunch of seteq's or'd together, or if it's a bunch of
36592754fe60SDimitry Andric // 'setne's and'ed together, collect them.
36602754fe60SDimitry Andric
366139d628a0SDimitry Andric // Try to gather values from a chain of and/or to be turned into a switch
366239d628a0SDimitry Andric ConstantComparesGatherer ConstantCompare(Cond, DL);
366339d628a0SDimitry Andric // Unpack the result
366439d628a0SDimitry Andric SmallVectorImpl<ConstantInt *> &Values = ConstantCompare.Vals;
366539d628a0SDimitry Andric Value *CompVal = ConstantCompare.CompValue;
366639d628a0SDimitry Andric unsigned UsedICmps = ConstantCompare.UsedICmps;
366739d628a0SDimitry Andric Value *ExtraCase = ConstantCompare.Extra;
36682754fe60SDimitry Andric
36692754fe60SDimitry Andric // If we didn't have a multiply compared value, fail.
36703ca95b02SDimitry Andric if (!CompVal)
36713ca95b02SDimitry Andric return false;
36722754fe60SDimitry Andric
36732754fe60SDimitry Andric // Avoid turning single icmps into a switch.
36742754fe60SDimitry Andric if (UsedICmps <= 1)
36752754fe60SDimitry Andric return false;
36762754fe60SDimitry Andric
367739d628a0SDimitry Andric bool TrueWhenEqual = (Cond->getOpcode() == Instruction::Or);
367839d628a0SDimitry Andric
36792754fe60SDimitry Andric // There might be duplicate constants in the list, which the switch
36802754fe60SDimitry Andric // instruction can't handle, remove them now.
36812754fe60SDimitry Andric array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
36822754fe60SDimitry Andric Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
36832754fe60SDimitry Andric
36842754fe60SDimitry Andric // If Extra was used, we require at least two switch values to do the
36857d523365SDimitry Andric // transformation. A switch with one value is just a conditional branch.
36863ca95b02SDimitry Andric if (ExtraCase && Values.size() < 2)
36873ca95b02SDimitry Andric return false;
36882754fe60SDimitry Andric
36893861d79fSDimitry Andric // TODO: Preserve branch weight metadata, similarly to how
36903861d79fSDimitry Andric // FoldValueComparisonIntoPredecessors preserves it.
36913861d79fSDimitry Andric
36922754fe60SDimitry Andric // Figure out which block is which destination.
36932754fe60SDimitry Andric BasicBlock *DefaultBB = BI->getSuccessor(1);
36942754fe60SDimitry Andric BasicBlock *EdgeBB = BI->getSuccessor(0);
36953ca95b02SDimitry Andric if (!TrueWhenEqual)
36963ca95b02SDimitry Andric std::swap(DefaultBB, EdgeBB);
36972754fe60SDimitry Andric
36982754fe60SDimitry Andric BasicBlock *BB = BI->getParent();
36992754fe60SDimitry Andric
37004ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
37013ca95b02SDimitry Andric << " cases into SWITCH. BB is:\n"
37023ca95b02SDimitry Andric << *BB);
37032754fe60SDimitry Andric
37042754fe60SDimitry Andric // If there are any extra values that couldn't be folded into the switch
37052754fe60SDimitry Andric // then we evaluate them with an explicit branch first. Split the block
37062754fe60SDimitry Andric // right before the condbr to handle it.
37072754fe60SDimitry Andric if (ExtraCase) {
37087d523365SDimitry Andric BasicBlock *NewBB =
37097d523365SDimitry Andric BB->splitBasicBlock(BI->getIterator(), "switch.early.test");
37102754fe60SDimitry Andric // Remove the uncond branch added to the old block.
3711*b5893f02SDimitry Andric Instruction *OldTI = BB->getTerminator();
3712bd5abe19SDimitry Andric Builder.SetInsertPoint(OldTI);
37132754fe60SDimitry Andric
37142754fe60SDimitry Andric if (TrueWhenEqual)
3715bd5abe19SDimitry Andric Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
37162754fe60SDimitry Andric else
3717bd5abe19SDimitry Andric Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
37182754fe60SDimitry Andric
37192754fe60SDimitry Andric OldTI->eraseFromParent();
37202754fe60SDimitry Andric
37212754fe60SDimitry Andric // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
37222754fe60SDimitry Andric // for the edge we just added.
37232754fe60SDimitry Andric AddPredecessorToBlock(EdgeBB, BB, NewBB);
37242754fe60SDimitry Andric
37254ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
37262754fe60SDimitry Andric << "\nEXTRABB = " << *BB);
37272754fe60SDimitry Andric BB = NewBB;
37282754fe60SDimitry Andric }
37292754fe60SDimitry Andric
3730bd5abe19SDimitry Andric Builder.SetInsertPoint(BI);
37312754fe60SDimitry Andric // Convert pointer to int before we switch.
37322754fe60SDimitry Andric if (CompVal->getType()->isPointerTy()) {
3733ff0cc061SDimitry Andric CompVal = Builder.CreatePtrToInt(
3734ff0cc061SDimitry Andric CompVal, DL.getIntPtrType(CompVal->getType()), "magicptr");
37352754fe60SDimitry Andric }
37362754fe60SDimitry Andric
37372754fe60SDimitry Andric // Create the new switch instruction now.
3738bd5abe19SDimitry Andric SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
37392754fe60SDimitry Andric
37402754fe60SDimitry Andric // Add all of the 'cases' to the switch instruction.
37412754fe60SDimitry Andric for (unsigned i = 0, e = Values.size(); i != e; ++i)
37422754fe60SDimitry Andric New->addCase(Values[i], EdgeBB);
37432754fe60SDimitry Andric
37442754fe60SDimitry Andric // We added edges from PI to the EdgeBB. As such, if there were any
37452754fe60SDimitry Andric // PHI nodes in EdgeBB, they need entries to be added corresponding to
37462754fe60SDimitry Andric // the number of edges added.
37473ca95b02SDimitry Andric for (BasicBlock::iterator BBI = EdgeBB->begin(); isa<PHINode>(BBI); ++BBI) {
37482754fe60SDimitry Andric PHINode *PN = cast<PHINode>(BBI);
37492754fe60SDimitry Andric Value *InVal = PN->getIncomingValueForBlock(BB);
37502754fe60SDimitry Andric for (unsigned i = 0, e = Values.size() - 1; i != e; ++i)
37512754fe60SDimitry Andric PN->addIncoming(InVal, BB);
37522754fe60SDimitry Andric }
37532754fe60SDimitry Andric
37542754fe60SDimitry Andric // Erase the old branch instruction.
3755*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
37562754fe60SDimitry Andric
37574ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
37582754fe60SDimitry Andric return true;
37592754fe60SDimitry Andric }
37602754fe60SDimitry Andric
SimplifyResume(ResumeInst * RI,IRBuilder<> & Builder)37616122f3e6SDimitry Andric bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
3762444ed5c5SDimitry Andric if (isa<PHINode>(RI->getValue()))
3763444ed5c5SDimitry Andric return SimplifyCommonResume(RI);
3764444ed5c5SDimitry Andric else if (isa<LandingPadInst>(RI->getParent()->getFirstNonPHI()) &&
3765444ed5c5SDimitry Andric RI->getValue() == RI->getParent()->getFirstNonPHI())
3766444ed5c5SDimitry Andric // The resume must unwind the exception that caused control to branch here.
3767444ed5c5SDimitry Andric return SimplifySingleResume(RI);
3768444ed5c5SDimitry Andric
3769444ed5c5SDimitry Andric return false;
3770444ed5c5SDimitry Andric }
3771444ed5c5SDimitry Andric
3772444ed5c5SDimitry Andric // Simplify resume that is shared by several landing pads (phi of landing pad).
SimplifyCommonResume(ResumeInst * RI)3773444ed5c5SDimitry Andric bool SimplifyCFGOpt::SimplifyCommonResume(ResumeInst *RI) {
3774444ed5c5SDimitry Andric BasicBlock *BB = RI->getParent();
3775444ed5c5SDimitry Andric
3776444ed5c5SDimitry Andric // Check that there are no other instructions except for debug intrinsics
3777444ed5c5SDimitry Andric // between the phi of landing pads (RI->getValue()) and resume instruction.
3778444ed5c5SDimitry Andric BasicBlock::iterator I = cast<Instruction>(RI->getValue())->getIterator(),
3779444ed5c5SDimitry Andric E = RI->getIterator();
3780444ed5c5SDimitry Andric while (++I != E)
3781444ed5c5SDimitry Andric if (!isa<DbgInfoIntrinsic>(I))
3782444ed5c5SDimitry Andric return false;
3783444ed5c5SDimitry Andric
378451690af2SDimitry Andric SmallSetVector<BasicBlock *, 4> TrivialUnwindBlocks;
3785444ed5c5SDimitry Andric auto *PhiLPInst = cast<PHINode>(RI->getValue());
3786444ed5c5SDimitry Andric
3787444ed5c5SDimitry Andric // Check incoming blocks to see if any of them are trivial.
37883ca95b02SDimitry Andric for (unsigned Idx = 0, End = PhiLPInst->getNumIncomingValues(); Idx != End;
37893ca95b02SDimitry Andric Idx++) {
3790444ed5c5SDimitry Andric auto *IncomingBB = PhiLPInst->getIncomingBlock(Idx);
3791444ed5c5SDimitry Andric auto *IncomingValue = PhiLPInst->getIncomingValue(Idx);
3792444ed5c5SDimitry Andric
3793444ed5c5SDimitry Andric // If the block has other successors, we can not delete it because
3794444ed5c5SDimitry Andric // it has other dependents.
3795444ed5c5SDimitry Andric if (IncomingBB->getUniqueSuccessor() != BB)
3796444ed5c5SDimitry Andric continue;
3797444ed5c5SDimitry Andric
37983ca95b02SDimitry Andric auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHI());
3799444ed5c5SDimitry Andric // Not the landing pad that caused the control to branch here.
3800444ed5c5SDimitry Andric if (IncomingValue != LandingPad)
3801444ed5c5SDimitry Andric continue;
3802444ed5c5SDimitry Andric
3803444ed5c5SDimitry Andric bool isTrivial = true;
3804444ed5c5SDimitry Andric
3805444ed5c5SDimitry Andric I = IncomingBB->getFirstNonPHI()->getIterator();
3806444ed5c5SDimitry Andric E = IncomingBB->getTerminator()->getIterator();
3807444ed5c5SDimitry Andric while (++I != E)
3808444ed5c5SDimitry Andric if (!isa<DbgInfoIntrinsic>(I)) {
3809444ed5c5SDimitry Andric isTrivial = false;
3810444ed5c5SDimitry Andric break;
3811444ed5c5SDimitry Andric }
3812444ed5c5SDimitry Andric
3813444ed5c5SDimitry Andric if (isTrivial)
3814444ed5c5SDimitry Andric TrivialUnwindBlocks.insert(IncomingBB);
3815444ed5c5SDimitry Andric }
3816444ed5c5SDimitry Andric
3817444ed5c5SDimitry Andric // If no trivial unwind blocks, don't do any simplifications.
38183ca95b02SDimitry Andric if (TrivialUnwindBlocks.empty())
38193ca95b02SDimitry Andric return false;
3820444ed5c5SDimitry Andric
3821444ed5c5SDimitry Andric // Turn all invokes that unwind here into calls.
3822444ed5c5SDimitry Andric for (auto *TrivialBB : TrivialUnwindBlocks) {
3823444ed5c5SDimitry Andric // Blocks that will be simplified should be removed from the phi node.
3824444ed5c5SDimitry Andric // Note there could be multiple edges to the resume block, and we need
3825444ed5c5SDimitry Andric // to remove them all.
3826444ed5c5SDimitry Andric while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
3827444ed5c5SDimitry Andric BB->removePredecessor(TrivialBB, true);
3828444ed5c5SDimitry Andric
3829444ed5c5SDimitry Andric for (pred_iterator PI = pred_begin(TrivialBB), PE = pred_end(TrivialBB);
3830444ed5c5SDimitry Andric PI != PE;) {
3831444ed5c5SDimitry Andric BasicBlock *Pred = *PI++;
3832444ed5c5SDimitry Andric removeUnwindEdge(Pred);
3833444ed5c5SDimitry Andric }
3834444ed5c5SDimitry Andric
3835444ed5c5SDimitry Andric // In each SimplifyCFG run, only the current processed block can be erased.
3836444ed5c5SDimitry Andric // Otherwise, it will break the iteration of SimplifyCFG pass. So instead
3837444ed5c5SDimitry Andric // of erasing TrivialBB, we only remove the branch to the common resume
3838444ed5c5SDimitry Andric // block so that we can later erase the resume block since it has no
3839444ed5c5SDimitry Andric // predecessors.
3840444ed5c5SDimitry Andric TrivialBB->getTerminator()->eraseFromParent();
3841444ed5c5SDimitry Andric new UnreachableInst(RI->getContext(), TrivialBB);
3842444ed5c5SDimitry Andric }
3843444ed5c5SDimitry Andric
3844444ed5c5SDimitry Andric // Delete the resume block if all its predecessors have been removed.
3845444ed5c5SDimitry Andric if (pred_empty(BB))
3846444ed5c5SDimitry Andric BB->eraseFromParent();
3847444ed5c5SDimitry Andric
3848444ed5c5SDimitry Andric return !TrivialUnwindBlocks.empty();
3849444ed5c5SDimitry Andric }
3850444ed5c5SDimitry Andric
3851444ed5c5SDimitry Andric // Simplify resume that is only used by a single (non-phi) landing pad.
SimplifySingleResume(ResumeInst * RI)3852444ed5c5SDimitry Andric bool SimplifyCFGOpt::SimplifySingleResume(ResumeInst *RI) {
38536122f3e6SDimitry Andric BasicBlock *BB = RI->getParent();
38546122f3e6SDimitry Andric LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI());
3855444ed5c5SDimitry Andric assert(RI->getValue() == LPInst &&
3856444ed5c5SDimitry Andric "Resume must unwind the exception that caused control to here");
38576122f3e6SDimitry Andric
38586122f3e6SDimitry Andric // Check that there are no other instructions except for debug intrinsics.
38597d523365SDimitry Andric BasicBlock::iterator I = LPInst->getIterator(), E = RI->getIterator();
38606122f3e6SDimitry Andric while (++I != E)
38616122f3e6SDimitry Andric if (!isa<DbgInfoIntrinsic>(I))
38626122f3e6SDimitry Andric return false;
38636122f3e6SDimitry Andric
38646122f3e6SDimitry Andric // Turn all invokes that unwind here into calls and delete the basic block.
38656122f3e6SDimitry Andric for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
38667d523365SDimitry Andric BasicBlock *Pred = *PI++;
38677d523365SDimitry Andric removeUnwindEdge(Pred);
38686122f3e6SDimitry Andric }
38696122f3e6SDimitry Andric
38706122f3e6SDimitry Andric // The landingpad is now unreachable. Zap it.
38713ca95b02SDimitry Andric if (LoopHeaders)
38723ca95b02SDimitry Andric LoopHeaders->erase(BB);
3873*b5893f02SDimitry Andric BB->eraseFromParent();
3874ff0cc061SDimitry Andric return true;
38756122f3e6SDimitry Andric }
38766122f3e6SDimitry Andric
removeEmptyCleanup(CleanupReturnInst * RI)38773ca95b02SDimitry Andric static bool removeEmptyCleanup(CleanupReturnInst *RI) {
38787d523365SDimitry Andric // If this is a trivial cleanup pad that executes no instructions, it can be
38797d523365SDimitry Andric // eliminated. If the cleanup pad continues to the caller, any predecessor
38807d523365SDimitry Andric // that is an EH pad will be updated to continue to the caller and any
38817d523365SDimitry Andric // predecessor that terminates with an invoke instruction will have its invoke
38827d523365SDimitry Andric // instruction converted to a call instruction. If the cleanup pad being
38837d523365SDimitry Andric // simplified does not continue to the caller, each predecessor will be
38847d523365SDimitry Andric // updated to continue to the unwind destination of the cleanup pad being
38857d523365SDimitry Andric // simplified.
38867d523365SDimitry Andric BasicBlock *BB = RI->getParent();
38877d523365SDimitry Andric CleanupPadInst *CPInst = RI->getCleanupPad();
38887d523365SDimitry Andric if (CPInst->getParent() != BB)
38897d523365SDimitry Andric // This isn't an empty cleanup.
38907d523365SDimitry Andric return false;
38917d523365SDimitry Andric
38923ca95b02SDimitry Andric // We cannot kill the pad if it has multiple uses. This typically arises
38933ca95b02SDimitry Andric // from unreachable basic blocks.
38943ca95b02SDimitry Andric if (!CPInst->hasOneUse())
38957d523365SDimitry Andric return false;
38967d523365SDimitry Andric
38973ca95b02SDimitry Andric // Check that there are no other instructions except for benign intrinsics.
38983ca95b02SDimitry Andric BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator();
38993ca95b02SDimitry Andric while (++I != E) {
39003ca95b02SDimitry Andric auto *II = dyn_cast<IntrinsicInst>(I);
39013ca95b02SDimitry Andric if (!II)
39023ca95b02SDimitry Andric return false;
39033ca95b02SDimitry Andric
39043ca95b02SDimitry Andric Intrinsic::ID IntrinsicID = II->getIntrinsicID();
39053ca95b02SDimitry Andric switch (IntrinsicID) {
39063ca95b02SDimitry Andric case Intrinsic::dbg_declare:
39073ca95b02SDimitry Andric case Intrinsic::dbg_value:
39084ba319b5SDimitry Andric case Intrinsic::dbg_label:
39093ca95b02SDimitry Andric case Intrinsic::lifetime_end:
39103ca95b02SDimitry Andric break;
39113ca95b02SDimitry Andric default:
39123ca95b02SDimitry Andric return false;
39133ca95b02SDimitry Andric }
39143ca95b02SDimitry Andric }
39153ca95b02SDimitry Andric
39167d523365SDimitry Andric // If the cleanup return we are simplifying unwinds to the caller, this will
39177d523365SDimitry Andric // set UnwindDest to nullptr.
39187d523365SDimitry Andric BasicBlock *UnwindDest = RI->getUnwindDest();
39197d523365SDimitry Andric Instruction *DestEHPad = UnwindDest ? UnwindDest->getFirstNonPHI() : nullptr;
39207d523365SDimitry Andric
39217d523365SDimitry Andric // We're about to remove BB from the control flow. Before we do, sink any
39227d523365SDimitry Andric // PHINodes into the unwind destination. Doing this before changing the
39237d523365SDimitry Andric // control flow avoids some potentially slow checks, since we can currently
39247d523365SDimitry Andric // be certain that UnwindDest and BB have no common predecessors (since they
39257d523365SDimitry Andric // are both EH pads).
39267d523365SDimitry Andric if (UnwindDest) {
39277d523365SDimitry Andric // First, go through the PHI nodes in UnwindDest and update any nodes that
39287d523365SDimitry Andric // reference the block we are removing
39297d523365SDimitry Andric for (BasicBlock::iterator I = UnwindDest->begin(),
39307d523365SDimitry Andric IE = DestEHPad->getIterator();
39317d523365SDimitry Andric I != IE; ++I) {
39327d523365SDimitry Andric PHINode *DestPN = cast<PHINode>(I);
39337d523365SDimitry Andric
39347d523365SDimitry Andric int Idx = DestPN->getBasicBlockIndex(BB);
39357d523365SDimitry Andric // Since BB unwinds to UnwindDest, it has to be in the PHI node.
39367d523365SDimitry Andric assert(Idx != -1);
39377d523365SDimitry Andric // This PHI node has an incoming value that corresponds to a control
39387d523365SDimitry Andric // path through the cleanup pad we are removing. If the incoming
39397d523365SDimitry Andric // value is in the cleanup pad, it must be a PHINode (because we
39407d523365SDimitry Andric // verified above that the block is otherwise empty). Otherwise, the
39417d523365SDimitry Andric // value is either a constant or a value that dominates the cleanup
39427d523365SDimitry Andric // pad being removed.
39437d523365SDimitry Andric //
39447d523365SDimitry Andric // Because BB and UnwindDest are both EH pads, all of their
39457d523365SDimitry Andric // predecessors must unwind to these blocks, and since no instruction
39467d523365SDimitry Andric // can have multiple unwind destinations, there will be no overlap in
39477d523365SDimitry Andric // incoming blocks between SrcPN and DestPN.
39487d523365SDimitry Andric Value *SrcVal = DestPN->getIncomingValue(Idx);
39497d523365SDimitry Andric PHINode *SrcPN = dyn_cast<PHINode>(SrcVal);
39507d523365SDimitry Andric
39517d523365SDimitry Andric // Remove the entry for the block we are deleting.
39527d523365SDimitry Andric DestPN->removeIncomingValue(Idx, false);
39537d523365SDimitry Andric
39547d523365SDimitry Andric if (SrcPN && SrcPN->getParent() == BB) {
39557d523365SDimitry Andric // If the incoming value was a PHI node in the cleanup pad we are
39567d523365SDimitry Andric // removing, we need to merge that PHI node's incoming values into
39577d523365SDimitry Andric // DestPN.
39587d523365SDimitry Andric for (unsigned SrcIdx = 0, SrcE = SrcPN->getNumIncomingValues();
39597d523365SDimitry Andric SrcIdx != SrcE; ++SrcIdx) {
39607d523365SDimitry Andric DestPN->addIncoming(SrcPN->getIncomingValue(SrcIdx),
39617d523365SDimitry Andric SrcPN->getIncomingBlock(SrcIdx));
39627d523365SDimitry Andric }
39637d523365SDimitry Andric } else {
39647d523365SDimitry Andric // Otherwise, the incoming value came from above BB and
39657d523365SDimitry Andric // so we can just reuse it. We must associate all of BB's
39667d523365SDimitry Andric // predecessors with this value.
39677d523365SDimitry Andric for (auto *pred : predecessors(BB)) {
39687d523365SDimitry Andric DestPN->addIncoming(SrcVal, pred);
39697d523365SDimitry Andric }
39707d523365SDimitry Andric }
39717d523365SDimitry Andric }
39727d523365SDimitry Andric
39737d523365SDimitry Andric // Sink any remaining PHI nodes directly into UnwindDest.
39747d523365SDimitry Andric Instruction *InsertPt = DestEHPad;
39757d523365SDimitry Andric for (BasicBlock::iterator I = BB->begin(),
39767d523365SDimitry Andric IE = BB->getFirstNonPHI()->getIterator();
39777d523365SDimitry Andric I != IE;) {
39787d523365SDimitry Andric // The iterator must be incremented here because the instructions are
39797d523365SDimitry Andric // being moved to another block.
39807d523365SDimitry Andric PHINode *PN = cast<PHINode>(I++);
39817d523365SDimitry Andric if (PN->use_empty())
39827d523365SDimitry Andric // If the PHI node has no uses, just leave it. It will be erased
39837d523365SDimitry Andric // when we erase BB below.
39847d523365SDimitry Andric continue;
39857d523365SDimitry Andric
39867d523365SDimitry Andric // Otherwise, sink this PHI node into UnwindDest.
39877d523365SDimitry Andric // Any predecessors to UnwindDest which are not already represented
39887d523365SDimitry Andric // must be back edges which inherit the value from the path through
39897d523365SDimitry Andric // BB. In this case, the PHI value must reference itself.
39907d523365SDimitry Andric for (auto *pred : predecessors(UnwindDest))
39917d523365SDimitry Andric if (pred != BB)
39927d523365SDimitry Andric PN->addIncoming(PN, pred);
39937d523365SDimitry Andric PN->moveBefore(InsertPt);
39947d523365SDimitry Andric }
39957d523365SDimitry Andric }
39967d523365SDimitry Andric
39977d523365SDimitry Andric for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
39987d523365SDimitry Andric // The iterator must be updated here because we are removing this pred.
39997d523365SDimitry Andric BasicBlock *PredBB = *PI++;
40007d523365SDimitry Andric if (UnwindDest == nullptr) {
40017d523365SDimitry Andric removeUnwindEdge(PredBB);
40027d523365SDimitry Andric } else {
4003*b5893f02SDimitry Andric Instruction *TI = PredBB->getTerminator();
40047d523365SDimitry Andric TI->replaceUsesOfWith(BB, UnwindDest);
40057d523365SDimitry Andric }
40067d523365SDimitry Andric }
40077d523365SDimitry Andric
40087d523365SDimitry Andric // The cleanup pad is now unreachable. Zap it.
40097d523365SDimitry Andric BB->eraseFromParent();
40107d523365SDimitry Andric return true;
40117d523365SDimitry Andric }
40127d523365SDimitry Andric
40133ca95b02SDimitry Andric // Try to merge two cleanuppads together.
mergeCleanupPad(CleanupReturnInst * RI)40143ca95b02SDimitry Andric static bool mergeCleanupPad(CleanupReturnInst *RI) {
40153ca95b02SDimitry Andric // Skip any cleanuprets which unwind to caller, there is nothing to merge
40163ca95b02SDimitry Andric // with.
40173ca95b02SDimitry Andric BasicBlock *UnwindDest = RI->getUnwindDest();
40183ca95b02SDimitry Andric if (!UnwindDest)
40193ca95b02SDimitry Andric return false;
40203ca95b02SDimitry Andric
40213ca95b02SDimitry Andric // This cleanupret isn't the only predecessor of this cleanuppad, it wouldn't
40223ca95b02SDimitry Andric // be safe to merge without code duplication.
40233ca95b02SDimitry Andric if (UnwindDest->getSinglePredecessor() != RI->getParent())
40243ca95b02SDimitry Andric return false;
40253ca95b02SDimitry Andric
40263ca95b02SDimitry Andric // Verify that our cleanuppad's unwind destination is another cleanuppad.
40273ca95b02SDimitry Andric auto *SuccessorCleanupPad = dyn_cast<CleanupPadInst>(&UnwindDest->front());
40283ca95b02SDimitry Andric if (!SuccessorCleanupPad)
40293ca95b02SDimitry Andric return false;
40303ca95b02SDimitry Andric
40313ca95b02SDimitry Andric CleanupPadInst *PredecessorCleanupPad = RI->getCleanupPad();
40323ca95b02SDimitry Andric // Replace any uses of the successor cleanupad with the predecessor pad
40333ca95b02SDimitry Andric // The only cleanuppad uses should be this cleanupret, it's cleanupret and
40343ca95b02SDimitry Andric // funclet bundle operands.
40353ca95b02SDimitry Andric SuccessorCleanupPad->replaceAllUsesWith(PredecessorCleanupPad);
40363ca95b02SDimitry Andric // Remove the old cleanuppad.
40373ca95b02SDimitry Andric SuccessorCleanupPad->eraseFromParent();
40383ca95b02SDimitry Andric // Now, we simply replace the cleanupret with a branch to the unwind
40393ca95b02SDimitry Andric // destination.
40403ca95b02SDimitry Andric BranchInst::Create(UnwindDest, RI->getParent());
40413ca95b02SDimitry Andric RI->eraseFromParent();
40423ca95b02SDimitry Andric
40433ca95b02SDimitry Andric return true;
40443ca95b02SDimitry Andric }
40453ca95b02SDimitry Andric
SimplifyCleanupReturn(CleanupReturnInst * RI)40463ca95b02SDimitry Andric bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) {
40473ca95b02SDimitry Andric // It is possible to transiantly have an undef cleanuppad operand because we
40483ca95b02SDimitry Andric // have deleted some, but not all, dead blocks.
40493ca95b02SDimitry Andric // Eventually, this block will be deleted.
40503ca95b02SDimitry Andric if (isa<UndefValue>(RI->getOperand(0)))
40513ca95b02SDimitry Andric return false;
40523ca95b02SDimitry Andric
40533ca95b02SDimitry Andric if (mergeCleanupPad(RI))
40543ca95b02SDimitry Andric return true;
40553ca95b02SDimitry Andric
40563ca95b02SDimitry Andric if (removeEmptyCleanup(RI))
40573ca95b02SDimitry Andric return true;
40583ca95b02SDimitry Andric
40593ca95b02SDimitry Andric return false;
40603ca95b02SDimitry Andric }
40613ca95b02SDimitry Andric
SimplifyReturn(ReturnInst * RI,IRBuilder<> & Builder)4062bd5abe19SDimitry Andric bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
40632754fe60SDimitry Andric BasicBlock *BB = RI->getParent();
40643ca95b02SDimitry Andric if (!BB->getFirstNonPHIOrDbg()->isTerminator())
40653ca95b02SDimitry Andric return false;
40662754fe60SDimitry Andric
4067f22ef01cSRoman Divacky // Find predecessors that end with branches.
4068f22ef01cSRoman Divacky SmallVector<BasicBlock *, 8> UncondBranchPreds;
4069f22ef01cSRoman Divacky SmallVector<BranchInst *, 8> CondBranchPreds;
4070f22ef01cSRoman Divacky for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
4071ffd1746dSEd Schouten BasicBlock *P = *PI;
4072*b5893f02SDimitry Andric Instruction *PTI = P->getTerminator();
4073f22ef01cSRoman Divacky if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
4074f22ef01cSRoman Divacky if (BI->isUnconditional())
4075ffd1746dSEd Schouten UncondBranchPreds.push_back(P);
4076f22ef01cSRoman Divacky else
4077f22ef01cSRoman Divacky CondBranchPreds.push_back(BI);
4078f22ef01cSRoman Divacky }
4079f22ef01cSRoman Divacky }
4080f22ef01cSRoman Divacky
4081f22ef01cSRoman Divacky // If we found some, do the transformation!
40822754fe60SDimitry Andric if (!UncondBranchPreds.empty() && DupRet) {
4083f22ef01cSRoman Divacky while (!UncondBranchPreds.empty()) {
4084f22ef01cSRoman Divacky BasicBlock *Pred = UncondBranchPreds.pop_back_val();
40854ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "FOLDING: " << *BB
4086f22ef01cSRoman Divacky << "INTO UNCOND BRANCH PRED: " << *Pred);
40872754fe60SDimitry Andric (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
4088f22ef01cSRoman Divacky }
4089f22ef01cSRoman Divacky
4090f22ef01cSRoman Divacky // If we eliminated all predecessors of the block, delete the block now.
40913ca95b02SDimitry Andric if (pred_empty(BB)) {
4092f22ef01cSRoman Divacky // We know there are no successors, so just nuke the block.
40933ca95b02SDimitry Andric if (LoopHeaders)
40943ca95b02SDimitry Andric LoopHeaders->erase(BB);
4095*b5893f02SDimitry Andric BB->eraseFromParent();
40963ca95b02SDimitry Andric }
4097f22ef01cSRoman Divacky
4098f22ef01cSRoman Divacky return true;
4099f22ef01cSRoman Divacky }
4100f22ef01cSRoman Divacky
4101f22ef01cSRoman Divacky // Check out all of the conditional branches going to this return
4102f22ef01cSRoman Divacky // instruction. If any of them just select between returns, change the
4103f22ef01cSRoman Divacky // branch itself into a select/return pair.
4104f22ef01cSRoman Divacky while (!CondBranchPreds.empty()) {
4105f22ef01cSRoman Divacky BranchInst *BI = CondBranchPreds.pop_back_val();
4106f22ef01cSRoman Divacky
4107f22ef01cSRoman Divacky // Check to see if the non-BB successor is also a return block.
4108f22ef01cSRoman Divacky if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
4109f22ef01cSRoman Divacky isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
4110bd5abe19SDimitry Andric SimplifyCondBranchToTwoReturns(BI, Builder))
4111f22ef01cSRoman Divacky return true;
4112f22ef01cSRoman Divacky }
41132754fe60SDimitry Andric return false;
4114f22ef01cSRoman Divacky }
41152754fe60SDimitry Andric
SimplifyUnreachable(UnreachableInst * UI)41162754fe60SDimitry Andric bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
41172754fe60SDimitry Andric BasicBlock *BB = UI->getParent();
4118f22ef01cSRoman Divacky
41192754fe60SDimitry Andric bool Changed = false;
4120f22ef01cSRoman Divacky
4121f22ef01cSRoman Divacky // If there are any instructions immediately before the unreachable that can
4122f22ef01cSRoman Divacky // be removed, do so.
41237d523365SDimitry Andric while (UI->getIterator() != BB->begin()) {
41247d523365SDimitry Andric BasicBlock::iterator BBI = UI->getIterator();
4125f22ef01cSRoman Divacky --BBI;
41266122f3e6SDimitry Andric // Do not delete instructions that can have side effects which might cause
41276122f3e6SDimitry Andric // the unreachable to not be reachable; specifically, calls and volatile
41286122f3e6SDimitry Andric // operations may have this effect.
41293ca95b02SDimitry Andric if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
41303ca95b02SDimitry Andric break;
4131f22ef01cSRoman Divacky
41326122f3e6SDimitry Andric if (BBI->mayHaveSideEffects()) {
41334d0b32cdSDimitry Andric if (auto *SI = dyn_cast<StoreInst>(BBI)) {
4134f22ef01cSRoman Divacky if (SI->isVolatile())
4135f22ef01cSRoman Divacky break;
41364d0b32cdSDimitry Andric } else if (auto *LI = dyn_cast<LoadInst>(BBI)) {
4137f22ef01cSRoman Divacky if (LI->isVolatile())
4138f22ef01cSRoman Divacky break;
41394d0b32cdSDimitry Andric } else if (auto *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
41406122f3e6SDimitry Andric if (RMWI->isVolatile())
41416122f3e6SDimitry Andric break;
41424d0b32cdSDimitry Andric } else if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
41436122f3e6SDimitry Andric if (CXI->isVolatile())
41446122f3e6SDimitry Andric break;
41454d0b32cdSDimitry Andric } else if (isa<CatchPadInst>(BBI)) {
41464d0b32cdSDimitry Andric // A catchpad may invoke exception object constructors and such, which
41474d0b32cdSDimitry Andric // in some languages can be arbitrary code, so be conservative by
41484d0b32cdSDimitry Andric // default.
41494d0b32cdSDimitry Andric // For CoreCLR, it just involves a type test, so can be removed.
41504d0b32cdSDimitry Andric if (classifyEHPersonality(BB->getParent()->getPersonalityFn()) !=
41514d0b32cdSDimitry Andric EHPersonality::CoreCLR)
41524d0b32cdSDimitry Andric break;
41536122f3e6SDimitry Andric } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
41546122f3e6SDimitry Andric !isa<LandingPadInst>(BBI)) {
41556122f3e6SDimitry Andric break;
41566122f3e6SDimitry Andric }
41576122f3e6SDimitry Andric // Note that deleting LandingPad's here is in fact okay, although it
41586122f3e6SDimitry Andric // involves a bit of subtle reasoning. If this inst is a LandingPad,
41596122f3e6SDimitry Andric // all the predecessors of this block will be the unwind edges of Invokes,
41606122f3e6SDimitry Andric // and we can therefore guarantee this block will be erased.
41616122f3e6SDimitry Andric }
4162f22ef01cSRoman Divacky
41633b0f4066SDimitry Andric // Delete this instruction (any uses are guaranteed to be dead)
41643b0f4066SDimitry Andric if (!BBI->use_empty())
41653b0f4066SDimitry Andric BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
41662754fe60SDimitry Andric BBI->eraseFromParent();
4167f22ef01cSRoman Divacky Changed = true;
4168f22ef01cSRoman Divacky }
4169f22ef01cSRoman Divacky
4170f22ef01cSRoman Divacky // If the unreachable instruction is the first in the block, take a gander
4171f22ef01cSRoman Divacky // at all of the predecessors of this instruction, and simplify them.
41723ca95b02SDimitry Andric if (&BB->front() != UI)
41733ca95b02SDimitry Andric return Changed;
41742754fe60SDimitry Andric
4175f22ef01cSRoman Divacky SmallVector<BasicBlock *, 8> Preds(pred_begin(BB), pred_end(BB));
4176f22ef01cSRoman Divacky for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
4177*b5893f02SDimitry Andric Instruction *TI = Preds[i]->getTerminator();
4178bd5abe19SDimitry Andric IRBuilder<> Builder(TI);
41794d0b32cdSDimitry Andric if (auto *BI = dyn_cast<BranchInst>(TI)) {
4180f22ef01cSRoman Divacky if (BI->isUnconditional()) {
4181f22ef01cSRoman Divacky if (BI->getSuccessor(0) == BB) {
4182f22ef01cSRoman Divacky new UnreachableInst(TI->getContext(), TI);
4183f22ef01cSRoman Divacky TI->eraseFromParent();
4184f22ef01cSRoman Divacky Changed = true;
4185f22ef01cSRoman Divacky }
4186f22ef01cSRoman Divacky } else {
4187f22ef01cSRoman Divacky if (BI->getSuccessor(0) == BB) {
4188bd5abe19SDimitry Andric Builder.CreateBr(BI->getSuccessor(1));
4189*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
4190f22ef01cSRoman Divacky } else if (BI->getSuccessor(1) == BB) {
4191bd5abe19SDimitry Andric Builder.CreateBr(BI->getSuccessor(0));
4192*b5893f02SDimitry Andric EraseTerminatorAndDCECond(BI);
4193f22ef01cSRoman Divacky Changed = true;
4194f22ef01cSRoman Divacky }
4195f22ef01cSRoman Divacky }
41964d0b32cdSDimitry Andric } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
41977a7e6055SDimitry Andric for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
41987a7e6055SDimitry Andric if (i->getCaseSuccessor() != BB) {
41997a7e6055SDimitry Andric ++i;
42007a7e6055SDimitry Andric continue;
42017a7e6055SDimitry Andric }
4202f22ef01cSRoman Divacky BB->removePredecessor(SI->getParent());
42037a7e6055SDimitry Andric i = SI->removeCase(i);
42047a7e6055SDimitry Andric e = SI->case_end();
4205f22ef01cSRoman Divacky Changed = true;
4206f22ef01cSRoman Divacky }
42074d0b32cdSDimitry Andric } else if (auto *II = dyn_cast<InvokeInst>(TI)) {
42084d0b32cdSDimitry Andric if (II->getUnwindDest() == BB) {
42097d523365SDimitry Andric removeUnwindEdge(TI->getParent());
42107d523365SDimitry Andric Changed = true;
42114d0b32cdSDimitry Andric }
42124d0b32cdSDimitry Andric } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
42134d0b32cdSDimitry Andric if (CSI->getUnwindDest() == BB) {
42144d0b32cdSDimitry Andric removeUnwindEdge(TI->getParent());
42154d0b32cdSDimitry Andric Changed = true;
42164d0b32cdSDimitry Andric continue;
42174d0b32cdSDimitry Andric }
42184d0b32cdSDimitry Andric
42194d0b32cdSDimitry Andric for (CatchSwitchInst::handler_iterator I = CSI->handler_begin(),
42204d0b32cdSDimitry Andric E = CSI->handler_end();
42214d0b32cdSDimitry Andric I != E; ++I) {
42224d0b32cdSDimitry Andric if (*I == BB) {
42234d0b32cdSDimitry Andric CSI->removeHandler(I);
42244d0b32cdSDimitry Andric --I;
42254d0b32cdSDimitry Andric --E;
42264d0b32cdSDimitry Andric Changed = true;
42274d0b32cdSDimitry Andric }
42284d0b32cdSDimitry Andric }
42294d0b32cdSDimitry Andric if (CSI->getNumHandlers() == 0) {
42304d0b32cdSDimitry Andric BasicBlock *CatchSwitchBB = CSI->getParent();
42314d0b32cdSDimitry Andric if (CSI->hasUnwindDest()) {
42324d0b32cdSDimitry Andric // Redirect preds to the unwind dest
42334d0b32cdSDimitry Andric CatchSwitchBB->replaceAllUsesWith(CSI->getUnwindDest());
42344d0b32cdSDimitry Andric } else {
42354d0b32cdSDimitry Andric // Rewrite all preds to unwind to caller (or from invoke to call).
42364d0b32cdSDimitry Andric SmallVector<BasicBlock *, 8> EHPreds(predecessors(CatchSwitchBB));
42374d0b32cdSDimitry Andric for (BasicBlock *EHPred : EHPreds)
42384d0b32cdSDimitry Andric removeUnwindEdge(EHPred);
42394d0b32cdSDimitry Andric }
42404d0b32cdSDimitry Andric // The catchswitch is no longer reachable.
42414d0b32cdSDimitry Andric new UnreachableInst(CSI->getContext(), CSI);
42424d0b32cdSDimitry Andric CSI->eraseFromParent();
42434d0b32cdSDimitry Andric Changed = true;
42444d0b32cdSDimitry Andric }
42457d523365SDimitry Andric } else if (isa<CleanupReturnInst>(TI)) {
42467d523365SDimitry Andric new UnreachableInst(TI->getContext(), TI);
42477d523365SDimitry Andric TI->eraseFromParent();
4248f22ef01cSRoman Divacky Changed = true;
4249f22ef01cSRoman Divacky }
4250f22ef01cSRoman Divacky }
4251f22ef01cSRoman Divacky
4252f22ef01cSRoman Divacky // If this block is now dead, remove it.
42533ca95b02SDimitry Andric if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
4254f22ef01cSRoman Divacky // We know there are no successors, so just nuke the block.
42553ca95b02SDimitry Andric if (LoopHeaders)
42563ca95b02SDimitry Andric LoopHeaders->erase(BB);
4257*b5893f02SDimitry Andric BB->eraseFromParent();
4258f22ef01cSRoman Divacky return true;
4259f22ef01cSRoman Divacky }
42602754fe60SDimitry Andric
42612754fe60SDimitry Andric return Changed;
4262f22ef01cSRoman Divacky }
42632754fe60SDimitry Andric
CasesAreContiguous(SmallVectorImpl<ConstantInt * > & Cases)4264ff0cc061SDimitry Andric static bool CasesAreContiguous(SmallVectorImpl<ConstantInt *> &Cases) {
4265ff0cc061SDimitry Andric assert(Cases.size() >= 1);
42662754fe60SDimitry Andric
42672754fe60SDimitry Andric array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
4268ff0cc061SDimitry Andric for (size_t I = 1, E = Cases.size(); I != E; ++I) {
42692754fe60SDimitry Andric if (Cases[I - 1]->getValue() != Cases[I]->getValue() + 1)
42702754fe60SDimitry Andric return false;
42712754fe60SDimitry Andric }
4272ff0cc061SDimitry Andric return true;
4273ff0cc061SDimitry Andric }
42742754fe60SDimitry Andric
4275ff0cc061SDimitry Andric /// Turn a switch with two reachable destinations into an integer range
4276ff0cc061SDimitry Andric /// comparison and branch.
TurnSwitchRangeIntoICmp(SwitchInst * SI,IRBuilder<> & Builder)4277ff0cc061SDimitry Andric static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) {
4278ff0cc061SDimitry Andric assert(SI->getNumCases() > 1 && "Degenerate switch?");
4279ff0cc061SDimitry Andric
4280ff0cc061SDimitry Andric bool HasDefault =
4281ff0cc061SDimitry Andric !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
4282ff0cc061SDimitry Andric
4283ff0cc061SDimitry Andric // Partition the cases into two sets with different destinations.
4284ff0cc061SDimitry Andric BasicBlock *DestA = HasDefault ? SI->getDefaultDest() : nullptr;
4285ff0cc061SDimitry Andric BasicBlock *DestB = nullptr;
4286ff0cc061SDimitry Andric SmallVector<ConstantInt *, 16> CasesA;
4287ff0cc061SDimitry Andric SmallVector<ConstantInt *, 16> CasesB;
4288ff0cc061SDimitry Andric
42897a7e6055SDimitry Andric for (auto Case : SI->cases()) {
42907a7e6055SDimitry Andric BasicBlock *Dest = Case.getCaseSuccessor();
42913ca95b02SDimitry Andric if (!DestA)
42923ca95b02SDimitry Andric DestA = Dest;
4293ff0cc061SDimitry Andric if (Dest == DestA) {
42947a7e6055SDimitry Andric CasesA.push_back(Case.getCaseValue());
4295ff0cc061SDimitry Andric continue;
4296ff0cc061SDimitry Andric }
42973ca95b02SDimitry Andric if (!DestB)
42983ca95b02SDimitry Andric DestB = Dest;
4299ff0cc061SDimitry Andric if (Dest == DestB) {
43007a7e6055SDimitry Andric CasesB.push_back(Case.getCaseValue());
4301ff0cc061SDimitry Andric continue;
4302ff0cc061SDimitry Andric }
4303ff0cc061SDimitry Andric return false; // More than two destinations.
4304ff0cc061SDimitry Andric }
4305ff0cc061SDimitry Andric
43063ca95b02SDimitry Andric assert(DestA && DestB &&
43073ca95b02SDimitry Andric "Single-destination switch should have been folded.");
4308ff0cc061SDimitry Andric assert(DestA != DestB);
4309ff0cc061SDimitry Andric assert(DestB != SI->getDefaultDest());
4310ff0cc061SDimitry Andric assert(!CasesB.empty() && "There must be non-default cases.");
4311ff0cc061SDimitry Andric assert(!CasesA.empty() || HasDefault);
4312ff0cc061SDimitry Andric
4313ff0cc061SDimitry Andric // Figure out if one of the sets of cases form a contiguous range.
4314ff0cc061SDimitry Andric SmallVectorImpl<ConstantInt *> *ContiguousCases = nullptr;
4315ff0cc061SDimitry Andric BasicBlock *ContiguousDest = nullptr;
4316ff0cc061SDimitry Andric BasicBlock *OtherDest = nullptr;
4317ff0cc061SDimitry Andric if (!CasesA.empty() && CasesAreContiguous(CasesA)) {
4318ff0cc061SDimitry Andric ContiguousCases = &CasesA;
4319ff0cc061SDimitry Andric ContiguousDest = DestA;
4320ff0cc061SDimitry Andric OtherDest = DestB;
4321ff0cc061SDimitry Andric } else if (CasesAreContiguous(CasesB)) {
4322ff0cc061SDimitry Andric ContiguousCases = &CasesB;
4323ff0cc061SDimitry Andric ContiguousDest = DestB;
4324ff0cc061SDimitry Andric OtherDest = DestA;
4325ff0cc061SDimitry Andric } else
4326ff0cc061SDimitry Andric return false;
4327ff0cc061SDimitry Andric
4328ff0cc061SDimitry Andric // Start building the compare and branch.
4329ff0cc061SDimitry Andric
4330ff0cc061SDimitry Andric Constant *Offset = ConstantExpr::getNeg(ContiguousCases->back());
43313ca95b02SDimitry Andric Constant *NumCases =
43323ca95b02SDimitry Andric ConstantInt::get(Offset->getType(), ContiguousCases->size());
43332754fe60SDimitry Andric
43342754fe60SDimitry Andric Value *Sub = SI->getCondition();
43352754fe60SDimitry Andric if (!Offset->isNullValue())
4336bd5abe19SDimitry Andric Sub = Builder.CreateAdd(Sub, Offset, Sub->getName() + ".off");
4337ff0cc061SDimitry Andric
4338284c1978SDimitry Andric Value *Cmp;
4339284c1978SDimitry Andric // If NumCases overflowed, then all possible values jump to the successor.
4340ff0cc061SDimitry Andric if (NumCases->isNullValue() && !ContiguousCases->empty())
4341284c1978SDimitry Andric Cmp = ConstantInt::getTrue(SI->getContext());
4342284c1978SDimitry Andric else
4343284c1978SDimitry Andric Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
4344ff0cc061SDimitry Andric BranchInst *NewBI = Builder.CreateCondBr(Cmp, ContiguousDest, OtherDest);
43452754fe60SDimitry Andric
43463861d79fSDimitry Andric // Update weight for the newly-created conditional branch.
4347ff0cc061SDimitry Andric if (HasBranchWeights(SI)) {
43483861d79fSDimitry Andric SmallVector<uint64_t, 8> Weights;
43493861d79fSDimitry Andric GetBranchWeights(SI, Weights);
43503861d79fSDimitry Andric if (Weights.size() == 1 + SI->getNumCases()) {
4351ff0cc061SDimitry Andric uint64_t TrueWeight = 0;
4352ff0cc061SDimitry Andric uint64_t FalseWeight = 0;
4353ff0cc061SDimitry Andric for (size_t I = 0, E = Weights.size(); I != E; ++I) {
4354ff0cc061SDimitry Andric if (SI->getSuccessor(I) == ContiguousDest)
4355ff0cc061SDimitry Andric TrueWeight += Weights[I];
4356ff0cc061SDimitry Andric else
4357ff0cc061SDimitry Andric FalseWeight += Weights[I];
4358ff0cc061SDimitry Andric }
4359ff0cc061SDimitry Andric while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
4360ff0cc061SDimitry Andric TrueWeight /= 2;
4361ff0cc061SDimitry Andric FalseWeight /= 2;
4362ff0cc061SDimitry Andric }
43632cab237bSDimitry Andric setBranchWeights(NewBI, TrueWeight, FalseWeight);
43643861d79fSDimitry Andric }
43653861d79fSDimitry Andric }
43663861d79fSDimitry Andric
4367ff0cc061SDimitry Andric // Prune obsolete incoming values off the successors' PHI nodes.
4368ff0cc061SDimitry Andric for (auto BBI = ContiguousDest->begin(); isa<PHINode>(BBI); ++BBI) {
4369ff0cc061SDimitry Andric unsigned PreviousEdges = ContiguousCases->size();
43703ca95b02SDimitry Andric if (ContiguousDest == SI->getDefaultDest())
43713ca95b02SDimitry Andric ++PreviousEdges;
4372ff0cc061SDimitry Andric for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
43732754fe60SDimitry Andric cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
43742754fe60SDimitry Andric }
4375ff0cc061SDimitry Andric for (auto BBI = OtherDest->begin(); isa<PHINode>(BBI); ++BBI) {
4376ff0cc061SDimitry Andric unsigned PreviousEdges = SI->getNumCases() - ContiguousCases->size();
43773ca95b02SDimitry Andric if (OtherDest == SI->getDefaultDest())
43783ca95b02SDimitry Andric ++PreviousEdges;
4379ff0cc061SDimitry Andric for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
4380ff0cc061SDimitry Andric cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
4381ff0cc061SDimitry Andric }
4382ff0cc061SDimitry Andric
4383ff0cc061SDimitry Andric // Drop the switch.
43842754fe60SDimitry Andric SI->eraseFromParent();
43852754fe60SDimitry Andric
43862754fe60SDimitry Andric return true;
43872754fe60SDimitry Andric }
43882754fe60SDimitry Andric
43893dac3a9bSDimitry Andric /// Compute masked bits for the condition of a switch
4390bd5abe19SDimitry Andric /// and use it to remove dead cases.
eliminateDeadSwitchCases(SwitchInst * SI,AssumptionCache * AC,const DataLayout & DL)43912cab237bSDimitry Andric static bool eliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
4392ff0cc061SDimitry Andric const DataLayout &DL) {
4393bd5abe19SDimitry Andric Value *Cond = SI->getCondition();
4394f785676fSDimitry Andric unsigned Bits = Cond->getType()->getIntegerBitWidth();
4395302affcbSDimitry Andric KnownBits Known = computeKnownBits(Cond, DL, 0, AC, SI);
4396bd5abe19SDimitry Andric
43973ca95b02SDimitry Andric // We can also eliminate cases by determining that their values are outside of
43983ca95b02SDimitry Andric // the limited range of the condition based on how many significant (non-sign)
43993ca95b02SDimitry Andric // bits are in the condition value.
44003ca95b02SDimitry Andric unsigned ExtraSignBits = ComputeNumSignBits(Cond, DL, 0, AC, SI) - 1;
44013ca95b02SDimitry Andric unsigned MaxSignificantBitsInCond = Bits - ExtraSignBits;
44023ca95b02SDimitry Andric
4403bd5abe19SDimitry Andric // Gather dead cases.
4404bd5abe19SDimitry Andric SmallVector<ConstantInt *, 8> DeadCases;
44053ca95b02SDimitry Andric for (auto &Case : SI->cases()) {
4406d8866befSDimitry Andric const APInt &CaseVal = Case.getCaseValue()->getValue();
440751690af2SDimitry Andric if (Known.Zero.intersects(CaseVal) || !Known.One.isSubsetOf(CaseVal) ||
44083ca95b02SDimitry Andric (CaseVal.getMinSignedBits() > MaxSignificantBitsInCond)) {
44093ca95b02SDimitry Andric DeadCases.push_back(Case.getCaseValue());
44104ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SimplifyCFG: switch case " << CaseVal
44114ba319b5SDimitry Andric << " is dead.\n");
4412bd5abe19SDimitry Andric }
4413bd5abe19SDimitry Andric }
4414bd5abe19SDimitry Andric
44157d523365SDimitry Andric // If we can prove that the cases must cover all possible values, the
44167d523365SDimitry Andric // default destination becomes dead and we can remove it. If we know some
44177d523365SDimitry Andric // of the bits in the value, we can use that to more precisely compute the
44187d523365SDimitry Andric // number of possible unique case values.
44197d523365SDimitry Andric bool HasDefault =
44207d523365SDimitry Andric !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
44213ca95b02SDimitry Andric const unsigned NumUnknownBits =
442251690af2SDimitry Andric Bits - (Known.Zero | Known.One).countPopulation();
44237d523365SDimitry Andric assert(NumUnknownBits <= Bits);
44247d523365SDimitry Andric if (HasDefault && DeadCases.empty() &&
44257d523365SDimitry Andric NumUnknownBits < 64 /* avoid overflow */ &&
44267d523365SDimitry Andric SI->getNumCases() == (1ULL << NumUnknownBits)) {
44274ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
44283ca95b02SDimitry Andric BasicBlock *NewDefault =
44293ca95b02SDimitry Andric SplitBlockPredecessors(SI->getDefaultDest(), SI->getParent(), "");
44307d523365SDimitry Andric SI->setDefaultDest(&*NewDefault);
44317d523365SDimitry Andric SplitBlock(&*NewDefault, &NewDefault->front());
44327d523365SDimitry Andric auto *OldTI = NewDefault->getTerminator();
44337d523365SDimitry Andric new UnreachableInst(SI->getContext(), OldTI);
4434*b5893f02SDimitry Andric EraseTerminatorAndDCECond(OldTI);
44357d523365SDimitry Andric return true;
44367d523365SDimitry Andric }
44377d523365SDimitry Andric
44383861d79fSDimitry Andric SmallVector<uint64_t, 8> Weights;
44393861d79fSDimitry Andric bool HasWeight = HasBranchWeights(SI);
44403861d79fSDimitry Andric if (HasWeight) {
44413861d79fSDimitry Andric GetBranchWeights(SI, Weights);
44423861d79fSDimitry Andric HasWeight = (Weights.size() == 1 + SI->getNumCases());
44433861d79fSDimitry Andric }
44443861d79fSDimitry Andric
4445bd5abe19SDimitry Andric // Remove dead cases from the switch.
44463ca95b02SDimitry Andric for (ConstantInt *DeadCase : DeadCases) {
44477a7e6055SDimitry Andric SwitchInst::CaseIt CaseI = SI->findCaseValue(DeadCase);
44487a7e6055SDimitry Andric assert(CaseI != SI->case_default() &&
4449dff0c46cSDimitry Andric "Case was not found. Probably mistake in DeadCases forming.");
44503861d79fSDimitry Andric if (HasWeight) {
44517a7e6055SDimitry Andric std::swap(Weights[CaseI->getCaseIndex() + 1], Weights.back());
44523861d79fSDimitry Andric Weights.pop_back();
44533861d79fSDimitry Andric }
44543861d79fSDimitry Andric
4455bd5abe19SDimitry Andric // Prune unused values from PHI nodes.
44567a7e6055SDimitry Andric CaseI->getCaseSuccessor()->removePredecessor(SI->getParent());
44577a7e6055SDimitry Andric SI->removeCase(CaseI);
4458bd5abe19SDimitry Andric }
445991bc56edSDimitry Andric if (HasWeight && Weights.size() >= 2) {
44603861d79fSDimitry Andric SmallVector<uint32_t, 8> MDWeights(Weights.begin(), Weights.end());
44612cab237bSDimitry Andric setBranchWeights(SI, MDWeights);
44623861d79fSDimitry Andric }
4463bd5abe19SDimitry Andric
4464bd5abe19SDimitry Andric return !DeadCases.empty();
4465bd5abe19SDimitry Andric }
4466bd5abe19SDimitry Andric
44673dac3a9bSDimitry Andric /// If BB would be eligible for simplification by
44683dac3a9bSDimitry Andric /// TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
446917a519f9SDimitry Andric /// by an unconditional branch), look at the phi node for BB in the successor
447017a519f9SDimitry Andric /// block and see if the incoming value is equal to CaseValue. If so, return
447117a519f9SDimitry Andric /// the phi node, and set PhiIndex to BB's index in the phi node.
FindPHIForConditionForwarding(ConstantInt * CaseValue,BasicBlock * BB,int * PhiIndex)447217a519f9SDimitry Andric static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
44733ca95b02SDimitry Andric BasicBlock *BB, int *PhiIndex) {
447417a519f9SDimitry Andric if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
447591bc56edSDimitry Andric return nullptr; // BB must be empty to be a candidate for simplification.
447617a519f9SDimitry Andric if (!BB->getSinglePredecessor())
447791bc56edSDimitry Andric return nullptr; // BB must be dominated by the switch.
447817a519f9SDimitry Andric
447917a519f9SDimitry Andric BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
448017a519f9SDimitry Andric if (!Branch || !Branch->isUnconditional())
448191bc56edSDimitry Andric return nullptr; // Terminator must be unconditional branch.
448217a519f9SDimitry Andric
448317a519f9SDimitry Andric BasicBlock *Succ = Branch->getSuccessor(0);
448417a519f9SDimitry Andric
448530785c0eSDimitry Andric for (PHINode &PHI : Succ->phis()) {
448630785c0eSDimitry Andric int Idx = PHI.getBasicBlockIndex(BB);
448717a519f9SDimitry Andric assert(Idx >= 0 && "PHI has no entry for predecessor?");
448817a519f9SDimitry Andric
448930785c0eSDimitry Andric Value *InValue = PHI.getIncomingValue(Idx);
44903ca95b02SDimitry Andric if (InValue != CaseValue)
44913ca95b02SDimitry Andric continue;
449217a519f9SDimitry Andric
449317a519f9SDimitry Andric *PhiIndex = Idx;
449430785c0eSDimitry Andric return &PHI;
449517a519f9SDimitry Andric }
449617a519f9SDimitry Andric
449791bc56edSDimitry Andric return nullptr;
449817a519f9SDimitry Andric }
449917a519f9SDimitry Andric
45003dac3a9bSDimitry Andric /// Try to forward the condition of a switch instruction to a phi node
45013dac3a9bSDimitry Andric /// dominated by the switch, if that would mean that some of the destination
45022cab237bSDimitry Andric /// blocks of the switch can be folded away. Return true if a change is made.
ForwardSwitchConditionToPHI(SwitchInst * SI)450317a519f9SDimitry Andric static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
45042cab237bSDimitry Andric using ForwardingNodesMap = DenseMap<PHINode *, SmallVector<int, 4>>;
450517a519f9SDimitry Andric
45062cab237bSDimitry Andric ForwardingNodesMap ForwardingNodes;
45072cab237bSDimitry Andric BasicBlock *SwitchBlock = SI->getParent();
45082cab237bSDimitry Andric bool Changed = false;
45092cab237bSDimitry Andric for (auto &Case : SI->cases()) {
45107a7e6055SDimitry Andric ConstantInt *CaseValue = Case.getCaseValue();
45117a7e6055SDimitry Andric BasicBlock *CaseDest = Case.getCaseSuccessor();
451217a519f9SDimitry Andric
45132cab237bSDimitry Andric // Replace phi operands in successor blocks that are using the constant case
45142cab237bSDimitry Andric // value rather than the switch condition variable:
45152cab237bSDimitry Andric // switchbb:
45162cab237bSDimitry Andric // switch i32 %x, label %default [
45172cab237bSDimitry Andric // i32 17, label %succ
45182cab237bSDimitry Andric // ...
45192cab237bSDimitry Andric // succ:
45202cab237bSDimitry Andric // %r = phi i32 ... [ 17, %switchbb ] ...
45212cab237bSDimitry Andric // -->
45222cab237bSDimitry Andric // %r = phi i32 ... [ %x, %switchbb ] ...
452317a519f9SDimitry Andric
452430785c0eSDimitry Andric for (PHINode &Phi : CaseDest->phis()) {
45252cab237bSDimitry Andric // This only works if there is exactly 1 incoming edge from the switch to
45262cab237bSDimitry Andric // a phi. If there is >1, that means multiple cases of the switch map to 1
45272cab237bSDimitry Andric // value in the phi, and that phi value is not the switch condition. Thus,
45282cab237bSDimitry Andric // this transform would not make sense (the phi would be invalid because
45292cab237bSDimitry Andric // a phi can't have different incoming values from the same block).
453030785c0eSDimitry Andric int SwitchBBIdx = Phi.getBasicBlockIndex(SwitchBlock);
453130785c0eSDimitry Andric if (Phi.getIncomingValue(SwitchBBIdx) == CaseValue &&
453230785c0eSDimitry Andric count(Phi.blocks(), SwitchBlock) == 1) {
453330785c0eSDimitry Andric Phi.setIncomingValue(SwitchBBIdx, SI->getCondition());
45342cab237bSDimitry Andric Changed = true;
45352cab237bSDimitry Andric }
453617a519f9SDimitry Andric }
453717a519f9SDimitry Andric
45382cab237bSDimitry Andric // Collect phi nodes that are indirectly using this switch's case constants.
45392cab237bSDimitry Andric int PhiIdx;
45402cab237bSDimitry Andric if (auto *Phi = FindPHIForConditionForwarding(CaseValue, CaseDest, &PhiIdx))
45412cab237bSDimitry Andric ForwardingNodes[Phi].push_back(PhiIdx);
45422cab237bSDimitry Andric }
454317a519f9SDimitry Andric
45442cab237bSDimitry Andric for (auto &ForwardingNode : ForwardingNodes) {
45452cab237bSDimitry Andric PHINode *Phi = ForwardingNode.first;
45462cab237bSDimitry Andric SmallVectorImpl<int> &Indexes = ForwardingNode.second;
45473ca95b02SDimitry Andric if (Indexes.size() < 2)
45483ca95b02SDimitry Andric continue;
454917a519f9SDimitry Andric
45502cab237bSDimitry Andric for (int Index : Indexes)
45512cab237bSDimitry Andric Phi->setIncomingValue(Index, SI->getCondition());
455217a519f9SDimitry Andric Changed = true;
455317a519f9SDimitry Andric }
455417a519f9SDimitry Andric
455517a519f9SDimitry Andric return Changed;
455617a519f9SDimitry Andric }
455717a519f9SDimitry Andric
45583dac3a9bSDimitry Andric /// Return true if the backend will be able to handle
45593861d79fSDimitry Andric /// initializing an array of constants like C.
ValidLookupTableConstant(Constant * C,const TargetTransformInfo & TTI)4560d88c1a5aSDimitry Andric static bool ValidLookupTableConstant(Constant *C, const TargetTransformInfo &TTI) {
456191bc56edSDimitry Andric if (C->isThreadDependent())
456291bc56edSDimitry Andric return false;
456391bc56edSDimitry Andric if (C->isDLLImportDependent())
456491bc56edSDimitry Andric return false;
456591bc56edSDimitry Andric
4566d88c1a5aSDimitry Andric if (!isa<ConstantFP>(C) && !isa<ConstantInt>(C) &&
4567d88c1a5aSDimitry Andric !isa<ConstantPointerNull>(C) && !isa<GlobalValue>(C) &&
4568d88c1a5aSDimitry Andric !isa<UndefValue>(C) && !isa<ConstantExpr>(C))
4569d88c1a5aSDimitry Andric return false;
45703861d79fSDimitry Andric
4571d88c1a5aSDimitry Andric if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
4572d88c1a5aSDimitry Andric if (!CE->isGEPWithNoNotionalOverIndexing())
4573d88c1a5aSDimitry Andric return false;
4574d88c1a5aSDimitry Andric if (!ValidLookupTableConstant(CE->getOperand(0), TTI))
4575d88c1a5aSDimitry Andric return false;
4576d88c1a5aSDimitry Andric }
4577d88c1a5aSDimitry Andric
4578d88c1a5aSDimitry Andric if (!TTI.shouldBuildLookupTablesForConstant(C))
4579d88c1a5aSDimitry Andric return false;
4580d88c1a5aSDimitry Andric
4581d88c1a5aSDimitry Andric return true;
45823861d79fSDimitry Andric }
45833861d79fSDimitry Andric
45843dac3a9bSDimitry Andric /// If V is a Constant, return it. Otherwise, try to look up
45853861d79fSDimitry Andric /// its constant value in ConstantPool, returning 0 if it's not there.
45863ca95b02SDimitry Andric static Constant *
LookupConstant(Value * V,const SmallDenseMap<Value *,Constant * > & ConstantPool)45873ca95b02SDimitry Andric LookupConstant(Value *V,
45883861d79fSDimitry Andric const SmallDenseMap<Value *, Constant *> &ConstantPool) {
45893861d79fSDimitry Andric if (Constant *C = dyn_cast<Constant>(V))
45903861d79fSDimitry Andric return C;
45913861d79fSDimitry Andric return ConstantPool.lookup(V);
45923861d79fSDimitry Andric }
45933861d79fSDimitry Andric
45943dac3a9bSDimitry Andric /// Try to fold instruction I into a constant. This works for
45953861d79fSDimitry Andric /// simple instructions such as binary operations where both operands are
45963861d79fSDimitry Andric /// constant or can be replaced by constants from the ConstantPool. Returns the
45973861d79fSDimitry Andric /// resulting constant on success, 0 otherwise.
4598f785676fSDimitry Andric static Constant *
ConstantFold(Instruction * I,const DataLayout & DL,const SmallDenseMap<Value *,Constant * > & ConstantPool)4599ff0cc061SDimitry Andric ConstantFold(Instruction *I, const DataLayout &DL,
4600ff0cc061SDimitry Andric const SmallDenseMap<Value *, Constant *> &ConstantPool) {
46013861d79fSDimitry Andric if (SelectInst *Select = dyn_cast<SelectInst>(I)) {
46023861d79fSDimitry Andric Constant *A = LookupConstant(Select->getCondition(), ConstantPool);
46033861d79fSDimitry Andric if (!A)
460491bc56edSDimitry Andric return nullptr;
46053861d79fSDimitry Andric if (A->isAllOnesValue())
46063861d79fSDimitry Andric return LookupConstant(Select->getTrueValue(), ConstantPool);
46073861d79fSDimitry Andric if (A->isNullValue())
46083861d79fSDimitry Andric return LookupConstant(Select->getFalseValue(), ConstantPool);
460991bc56edSDimitry Andric return nullptr;
46103861d79fSDimitry Andric }
46113861d79fSDimitry Andric
4612f785676fSDimitry Andric SmallVector<Constant *, 4> COps;
4613f785676fSDimitry Andric for (unsigned N = 0, E = I->getNumOperands(); N != E; ++N) {
4614f785676fSDimitry Andric if (Constant *A = LookupConstant(I->getOperand(N), ConstantPool))
4615f785676fSDimitry Andric COps.push_back(A);
4616f785676fSDimitry Andric else
461791bc56edSDimitry Andric return nullptr;
46183861d79fSDimitry Andric }
46193861d79fSDimitry Andric
4620ff0cc061SDimitry Andric if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
4621f785676fSDimitry Andric return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
4622f785676fSDimitry Andric COps[1], DL);
4623ff0cc061SDimitry Andric }
4624f785676fSDimitry Andric
46253ca95b02SDimitry Andric return ConstantFoldInstOperands(I, COps, DL);
46263861d79fSDimitry Andric }
46273861d79fSDimitry Andric
46283dac3a9bSDimitry Andric /// Try to determine the resulting constant values in phi nodes
46293861d79fSDimitry Andric /// at the common destination basic block, *CommonDest, for one of the case
46303861d79fSDimitry Andric /// destionations CaseDest corresponding to value CaseVal (0 for the default
46313861d79fSDimitry Andric /// case), of a switch instruction SI.
4632f785676fSDimitry Andric static bool
GetCaseResults(SwitchInst * SI,ConstantInt * CaseVal,BasicBlock * CaseDest,BasicBlock ** CommonDest,SmallVectorImpl<std::pair<PHINode *,Constant * >> & Res,const DataLayout & DL,const TargetTransformInfo & TTI)4633ff0cc061SDimitry Andric GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
46343861d79fSDimitry Andric BasicBlock **CommonDest,
4635f785676fSDimitry Andric SmallVectorImpl<std::pair<PHINode *, Constant *>> &Res,
4636d88c1a5aSDimitry Andric const DataLayout &DL, const TargetTransformInfo &TTI) {
46373861d79fSDimitry Andric // The block from which we enter the common destination.
46383861d79fSDimitry Andric BasicBlock *Pred = SI->getParent();
46393861d79fSDimitry Andric
46403861d79fSDimitry Andric // If CaseDest is empty except for some side-effect free instructions through
46413861d79fSDimitry Andric // which we can constant-propagate the CaseVal, continue to its successor.
46423861d79fSDimitry Andric SmallDenseMap<Value *, Constant *> ConstantPool;
46433861d79fSDimitry Andric ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
46444ba319b5SDimitry Andric for (Instruction &I :CaseDest->instructionsWithoutDebug()) {
4645*b5893f02SDimitry Andric if (I.isTerminator()) {
46463861d79fSDimitry Andric // If the terminator is a simple branch, continue to the next block.
4647*b5893f02SDimitry Andric if (I.getNumSuccessors() != 1 || I.isExceptionalTerminator())
46483861d79fSDimitry Andric return false;
46493861d79fSDimitry Andric Pred = CaseDest;
4650*b5893f02SDimitry Andric CaseDest = I.getSuccessor(0);
46514ba319b5SDimitry Andric } else if (Constant *C = ConstantFold(&I, DL, ConstantPool)) {
46523861d79fSDimitry Andric // Instruction is side-effect free and constant.
465339d628a0SDimitry Andric
465439d628a0SDimitry Andric // If the instruction has uses outside this block or a phi node slot for
465539d628a0SDimitry Andric // the block, it is not safe to bypass the instruction since it would then
465639d628a0SDimitry Andric // no longer dominate all its uses.
46574ba319b5SDimitry Andric for (auto &Use : I.uses()) {
465839d628a0SDimitry Andric User *User = Use.getUser();
465939d628a0SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(User))
466039d628a0SDimitry Andric if (I->getParent() == CaseDest)
466139d628a0SDimitry Andric continue;
466239d628a0SDimitry Andric if (PHINode *Phi = dyn_cast<PHINode>(User))
466339d628a0SDimitry Andric if (Phi->getIncomingBlock(Use) == CaseDest)
466439d628a0SDimitry Andric continue;
466539d628a0SDimitry Andric return false;
466639d628a0SDimitry Andric }
466739d628a0SDimitry Andric
46684ba319b5SDimitry Andric ConstantPool.insert(std::make_pair(&I, C));
46693861d79fSDimitry Andric } else {
46703861d79fSDimitry Andric break;
46713861d79fSDimitry Andric }
46723861d79fSDimitry Andric }
46733861d79fSDimitry Andric
46743861d79fSDimitry Andric // If we did not have a CommonDest before, use the current one.
46753861d79fSDimitry Andric if (!*CommonDest)
46763861d79fSDimitry Andric *CommonDest = CaseDest;
46773861d79fSDimitry Andric // If the destination isn't the common one, abort.
46783861d79fSDimitry Andric if (CaseDest != *CommonDest)
46792754fe60SDimitry Andric return false;
46802754fe60SDimitry Andric
46813861d79fSDimitry Andric // Get the values for this case from phi nodes in the destination block.
468230785c0eSDimitry Andric for (PHINode &PHI : (*CommonDest)->phis()) {
468330785c0eSDimitry Andric int Idx = PHI.getBasicBlockIndex(Pred);
46843861d79fSDimitry Andric if (Idx == -1)
46853861d79fSDimitry Andric continue;
46863861d79fSDimitry Andric
46873ca95b02SDimitry Andric Constant *ConstVal =
468830785c0eSDimitry Andric LookupConstant(PHI.getIncomingValue(Idx), ConstantPool);
46893861d79fSDimitry Andric if (!ConstVal)
46903861d79fSDimitry Andric return false;
46913861d79fSDimitry Andric
46923861d79fSDimitry Andric // Be conservative about which kinds of constants we support.
4693d88c1a5aSDimitry Andric if (!ValidLookupTableConstant(ConstVal, TTI))
46943861d79fSDimitry Andric return false;
46953861d79fSDimitry Andric
469630785c0eSDimitry Andric Res.push_back(std::make_pair(&PHI, ConstVal));
46973861d79fSDimitry Andric }
46983861d79fSDimitry Andric
469991bc56edSDimitry Andric return Res.size() > 0;
47003861d79fSDimitry Andric }
47013861d79fSDimitry Andric
47023dac3a9bSDimitry Andric // Helper function used to add CaseVal to the list of cases that generate
47034ba319b5SDimitry Andric // Result. Returns the updated number of cases that generate this result.
MapCaseToResult(ConstantInt * CaseVal,SwitchCaseResultVectorTy & UniqueResults,Constant * Result)47044ba319b5SDimitry Andric static uintptr_t MapCaseToResult(ConstantInt *CaseVal,
470539d628a0SDimitry Andric SwitchCaseResultVectorTy &UniqueResults,
470639d628a0SDimitry Andric Constant *Result) {
470739d628a0SDimitry Andric for (auto &I : UniqueResults) {
470839d628a0SDimitry Andric if (I.first == Result) {
470939d628a0SDimitry Andric I.second.push_back(CaseVal);
47104ba319b5SDimitry Andric return I.second.size();
471139d628a0SDimitry Andric }
471239d628a0SDimitry Andric }
47133ca95b02SDimitry Andric UniqueResults.push_back(
47143ca95b02SDimitry Andric std::make_pair(Result, SmallVector<ConstantInt *, 4>(1, CaseVal)));
47154ba319b5SDimitry Andric return 1;
471639d628a0SDimitry Andric }
471739d628a0SDimitry Andric
47183dac3a9bSDimitry Andric // Helper function that initializes a map containing
471939d628a0SDimitry Andric // results for the PHI node of the common destination block for a switch
472039d628a0SDimitry Andric // instruction. Returns false if multiple PHI nodes have been found or if
472139d628a0SDimitry Andric // there is not a common destination block for the switch.
47224ba319b5SDimitry Andric static bool
InitializeUniqueCases(SwitchInst * SI,PHINode * & PHI,BasicBlock * & CommonDest,SwitchCaseResultVectorTy & UniqueResults,Constant * & DefaultResult,const DataLayout & DL,const TargetTransformInfo & TTI,uintptr_t MaxUniqueResults,uintptr_t MaxCasesPerResult)47234ba319b5SDimitry Andric InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest,
472439d628a0SDimitry Andric SwitchCaseResultVectorTy &UniqueResults,
47254ba319b5SDimitry Andric Constant *&DefaultResult, const DataLayout &DL,
47264ba319b5SDimitry Andric const TargetTransformInfo &TTI,
47274ba319b5SDimitry Andric uintptr_t MaxUniqueResults, uintptr_t MaxCasesPerResult) {
472839d628a0SDimitry Andric for (auto &I : SI->cases()) {
472939d628a0SDimitry Andric ConstantInt *CaseVal = I.getCaseValue();
473039d628a0SDimitry Andric
473139d628a0SDimitry Andric // Resulting value at phi nodes for this case value.
473239d628a0SDimitry Andric SwitchCaseResultsTy Results;
473339d628a0SDimitry Andric if (!GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
4734d88c1a5aSDimitry Andric DL, TTI))
473539d628a0SDimitry Andric return false;
473639d628a0SDimitry Andric
47374ba319b5SDimitry Andric // Only one value per case is permitted.
473839d628a0SDimitry Andric if (Results.size() > 1)
473939d628a0SDimitry Andric return false;
47404ba319b5SDimitry Andric
47414ba319b5SDimitry Andric // Add the case->result mapping to UniqueResults.
47424ba319b5SDimitry Andric const uintptr_t NumCasesForResult =
474339d628a0SDimitry Andric MapCaseToResult(CaseVal, UniqueResults, Results.begin()->second);
474439d628a0SDimitry Andric
47454ba319b5SDimitry Andric // Early out if there are too many cases for this result.
47464ba319b5SDimitry Andric if (NumCasesForResult > MaxCasesPerResult)
47474ba319b5SDimitry Andric return false;
47484ba319b5SDimitry Andric
47494ba319b5SDimitry Andric // Early out if there are too many unique results.
47504ba319b5SDimitry Andric if (UniqueResults.size() > MaxUniqueResults)
47514ba319b5SDimitry Andric return false;
47524ba319b5SDimitry Andric
475339d628a0SDimitry Andric // Check the PHI consistency.
475439d628a0SDimitry Andric if (!PHI)
475539d628a0SDimitry Andric PHI = Results[0].first;
475639d628a0SDimitry Andric else if (PHI != Results[0].first)
475739d628a0SDimitry Andric return false;
475839d628a0SDimitry Andric }
475939d628a0SDimitry Andric // Find the default result value.
476039d628a0SDimitry Andric SmallVector<std::pair<PHINode *, Constant *>, 1> DefaultResults;
476139d628a0SDimitry Andric BasicBlock *DefaultDest = SI->getDefaultDest();
476239d628a0SDimitry Andric GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest, DefaultResults,
4763d88c1a5aSDimitry Andric DL, TTI);
476439d628a0SDimitry Andric // If the default value is not found abort unless the default destination
476539d628a0SDimitry Andric // is unreachable.
476639d628a0SDimitry Andric DefaultResult =
476739d628a0SDimitry Andric DefaultResults.size() == 1 ? DefaultResults.begin()->second : nullptr;
476839d628a0SDimitry Andric if ((!DefaultResult &&
476939d628a0SDimitry Andric !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())))
477039d628a0SDimitry Andric return false;
477139d628a0SDimitry Andric
477239d628a0SDimitry Andric return true;
477339d628a0SDimitry Andric }
477439d628a0SDimitry Andric
47753dac3a9bSDimitry Andric // Helper function that checks if it is possible to transform a switch with only
47763dac3a9bSDimitry Andric // two cases (or two cases + default) that produces a result into a select.
477739d628a0SDimitry Andric // Example:
477839d628a0SDimitry Andric // switch (a) {
477939d628a0SDimitry Andric // case 10: %0 = icmp eq i32 %a, 10
478039d628a0SDimitry Andric // return 10; %1 = select i1 %0, i32 10, i32 4
478139d628a0SDimitry Andric // case 20: ----> %2 = icmp eq i32 %a, 20
478239d628a0SDimitry Andric // return 2; %3 = select i1 %2, i32 2, i32 %1
478339d628a0SDimitry Andric // default:
478439d628a0SDimitry Andric // return 4;
478539d628a0SDimitry Andric // }
ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy & ResultVector,Constant * DefaultResult,Value * Condition,IRBuilder<> & Builder)47863ca95b02SDimitry Andric static Value *ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector,
478739d628a0SDimitry Andric Constant *DefaultResult, Value *Condition,
478839d628a0SDimitry Andric IRBuilder<> &Builder) {
478939d628a0SDimitry Andric assert(ResultVector.size() == 2 &&
479039d628a0SDimitry Andric "We should have exactly two unique results at this point");
479139d628a0SDimitry Andric // If we are selecting between only two cases transform into a simple
479239d628a0SDimitry Andric // select or a two-way select if default is possible.
479339d628a0SDimitry Andric if (ResultVector[0].second.size() == 1 &&
479439d628a0SDimitry Andric ResultVector[1].second.size() == 1) {
479539d628a0SDimitry Andric ConstantInt *const FirstCase = ResultVector[0].second[0];
479639d628a0SDimitry Andric ConstantInt *const SecondCase = ResultVector[1].second[0];
479739d628a0SDimitry Andric
479839d628a0SDimitry Andric bool DefaultCanTrigger = DefaultResult;
479939d628a0SDimitry Andric Value *SelectValue = ResultVector[1].first;
480039d628a0SDimitry Andric if (DefaultCanTrigger) {
480139d628a0SDimitry Andric Value *const ValueCompare =
480239d628a0SDimitry Andric Builder.CreateICmpEQ(Condition, SecondCase, "switch.selectcmp");
480339d628a0SDimitry Andric SelectValue = Builder.CreateSelect(ValueCompare, ResultVector[1].first,
480439d628a0SDimitry Andric DefaultResult, "switch.select");
480539d628a0SDimitry Andric }
480639d628a0SDimitry Andric Value *const ValueCompare =
480739d628a0SDimitry Andric Builder.CreateICmpEQ(Condition, FirstCase, "switch.selectcmp");
48083ca95b02SDimitry Andric return Builder.CreateSelect(ValueCompare, ResultVector[0].first,
48093ca95b02SDimitry Andric SelectValue, "switch.select");
481039d628a0SDimitry Andric }
481139d628a0SDimitry Andric
481239d628a0SDimitry Andric return nullptr;
481339d628a0SDimitry Andric }
481439d628a0SDimitry Andric
48153dac3a9bSDimitry Andric // Helper function to cleanup a switch instruction that has been converted into
48163dac3a9bSDimitry Andric // a select, fixing up PHI nodes and basic blocks.
RemoveSwitchAfterSelectConversion(SwitchInst * SI,PHINode * PHI,Value * SelectValue,IRBuilder<> & Builder)481739d628a0SDimitry Andric static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI,
481839d628a0SDimitry Andric Value *SelectValue,
481939d628a0SDimitry Andric IRBuilder<> &Builder) {
482039d628a0SDimitry Andric BasicBlock *SelectBB = SI->getParent();
482139d628a0SDimitry Andric while (PHI->getBasicBlockIndex(SelectBB) >= 0)
482239d628a0SDimitry Andric PHI->removeIncomingValue(SelectBB);
482339d628a0SDimitry Andric PHI->addIncoming(SelectValue, SelectBB);
482439d628a0SDimitry Andric
482539d628a0SDimitry Andric Builder.CreateBr(PHI->getParent());
482639d628a0SDimitry Andric
482739d628a0SDimitry Andric // Remove the switch.
482839d628a0SDimitry Andric for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
482939d628a0SDimitry Andric BasicBlock *Succ = SI->getSuccessor(i);
483039d628a0SDimitry Andric
483139d628a0SDimitry Andric if (Succ == PHI->getParent())
483239d628a0SDimitry Andric continue;
483339d628a0SDimitry Andric Succ->removePredecessor(SelectBB);
483439d628a0SDimitry Andric }
483539d628a0SDimitry Andric SI->eraseFromParent();
483639d628a0SDimitry Andric }
483739d628a0SDimitry Andric
48383dac3a9bSDimitry Andric /// If the switch is only used to initialize one or more
483939d628a0SDimitry Andric /// phi nodes in a common successor block with only two different
484039d628a0SDimitry Andric /// constant values, replace the switch with select.
switchToSelect(SwitchInst * SI,IRBuilder<> & Builder,const DataLayout & DL,const TargetTransformInfo & TTI)48412cab237bSDimitry Andric static bool switchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
48422cab237bSDimitry Andric const DataLayout &DL,
4843d88c1a5aSDimitry Andric const TargetTransformInfo &TTI) {
484439d628a0SDimitry Andric Value *const Cond = SI->getCondition();
484539d628a0SDimitry Andric PHINode *PHI = nullptr;
484639d628a0SDimitry Andric BasicBlock *CommonDest = nullptr;
484739d628a0SDimitry Andric Constant *DefaultResult;
484839d628a0SDimitry Andric SwitchCaseResultVectorTy UniqueResults;
484939d628a0SDimitry Andric // Collect all the cases that will deliver the same value from the switch.
4850ff0cc061SDimitry Andric if (!InitializeUniqueCases(SI, PHI, CommonDest, UniqueResults, DefaultResult,
48514ba319b5SDimitry Andric DL, TTI, 2, 1))
485239d628a0SDimitry Andric return false;
485339d628a0SDimitry Andric // Selects choose between maximum two values.
485439d628a0SDimitry Andric if (UniqueResults.size() != 2)
485539d628a0SDimitry Andric return false;
485639d628a0SDimitry Andric assert(PHI != nullptr && "PHI for value select not found");
485739d628a0SDimitry Andric
485839d628a0SDimitry Andric Builder.SetInsertPoint(SI);
48593ca95b02SDimitry Andric Value *SelectValue =
48603ca95b02SDimitry Andric ConvertTwoCaseSwitch(UniqueResults, DefaultResult, Cond, Builder);
486139d628a0SDimitry Andric if (SelectValue) {
486239d628a0SDimitry Andric RemoveSwitchAfterSelectConversion(SI, PHI, SelectValue, Builder);
486339d628a0SDimitry Andric return true;
486439d628a0SDimitry Andric }
486539d628a0SDimitry Andric // The switch couldn't be converted into a select.
486639d628a0SDimitry Andric return false;
486739d628a0SDimitry Andric }
486839d628a0SDimitry Andric
48693861d79fSDimitry Andric namespace {
4870d88c1a5aSDimitry Andric
48713dac3a9bSDimitry Andric /// This class represents a lookup table that can be used to replace a switch.
48723861d79fSDimitry Andric class SwitchLookupTable {
48733861d79fSDimitry Andric public:
48743dac3a9bSDimitry Andric /// Create a lookup table to use as a switch replacement with the contents
48753dac3a9bSDimitry Andric /// of Values, using DefaultValue to fill any holes in the table.
4876ff0cc061SDimitry Andric SwitchLookupTable(
4877ff0cc061SDimitry Andric Module &M, uint64_t TableSize, ConstantInt *Offset,
4878f785676fSDimitry Andric const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
4879a580b014SDimitry Andric Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName);
48803861d79fSDimitry Andric
48813dac3a9bSDimitry Andric /// Build instructions with Builder to retrieve the value at
48823861d79fSDimitry Andric /// the position given by Index in the lookup table.
48833861d79fSDimitry Andric Value *BuildLookup(Value *Index, IRBuilder<> &Builder);
48843861d79fSDimitry Andric
48853dac3a9bSDimitry Andric /// Return true if a table with TableSize elements of
48863861d79fSDimitry Andric /// type ElementType would fit in a target-legal register.
4887ff0cc061SDimitry Andric static bool WouldFitInRegister(const DataLayout &DL, uint64_t TableSize,
48887d523365SDimitry Andric Type *ElementType);
48893861d79fSDimitry Andric
48903861d79fSDimitry Andric private:
48913861d79fSDimitry Andric // Depending on the contents of the table, it can be represented in
48923861d79fSDimitry Andric // different ways.
48933861d79fSDimitry Andric enum {
48943861d79fSDimitry Andric // For tables where each element contains the same value, we just have to
48953861d79fSDimitry Andric // store that single value and return it for each lookup.
48963861d79fSDimitry Andric SingleValueKind,
48973861d79fSDimitry Andric
489839d628a0SDimitry Andric // For tables where there is a linear relationship between table index
489939d628a0SDimitry Andric // and values. We calculate the result with a simple multiplication
490039d628a0SDimitry Andric // and addition instead of a table lookup.
490139d628a0SDimitry Andric LinearMapKind,
490239d628a0SDimitry Andric
49033861d79fSDimitry Andric // For small tables with integer elements, we can pack them into a bitmap
49043861d79fSDimitry Andric // that fits into a target-legal register. Values are retrieved by
49053861d79fSDimitry Andric // shift and mask operations.
49063861d79fSDimitry Andric BitMapKind,
49073861d79fSDimitry Andric
49083861d79fSDimitry Andric // The table is stored as an array of values. Values are retrieved by load
49093861d79fSDimitry Andric // instructions from the table.
49103861d79fSDimitry Andric ArrayKind
49113861d79fSDimitry Andric } Kind;
49123861d79fSDimitry Andric
49133861d79fSDimitry Andric // For SingleValueKind, this is the single value.
49142cab237bSDimitry Andric Constant *SingleValue = nullptr;
49153861d79fSDimitry Andric
49163861d79fSDimitry Andric // For BitMapKind, this is the bitmap.
49172cab237bSDimitry Andric ConstantInt *BitMap = nullptr;
49182cab237bSDimitry Andric IntegerType *BitMapElementTy = nullptr;
49193861d79fSDimitry Andric
492039d628a0SDimitry Andric // For LinearMapKind, these are the constants used to derive the value.
49212cab237bSDimitry Andric ConstantInt *LinearOffset = nullptr;
49222cab237bSDimitry Andric ConstantInt *LinearMultiplier = nullptr;
492339d628a0SDimitry Andric
49243861d79fSDimitry Andric // For ArrayKind, this is the array.
49252cab237bSDimitry Andric GlobalVariable *Array = nullptr;
49263861d79fSDimitry Andric };
4927d88c1a5aSDimitry Andric
4928d88c1a5aSDimitry Andric } // end anonymous namespace
49293861d79fSDimitry Andric
SwitchLookupTable(Module & M,uint64_t TableSize,ConstantInt * Offset,const SmallVectorImpl<std::pair<ConstantInt *,Constant * >> & Values,Constant * DefaultValue,const DataLayout & DL,const StringRef & FuncName)4930ff0cc061SDimitry Andric SwitchLookupTable::SwitchLookupTable(
4931ff0cc061SDimitry Andric Module &M, uint64_t TableSize, ConstantInt *Offset,
4932f785676fSDimitry Andric const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values,
49332cab237bSDimitry Andric Constant *DefaultValue, const DataLayout &DL, const StringRef &FuncName) {
49343861d79fSDimitry Andric assert(Values.size() && "Can't build lookup table without values!");
49353861d79fSDimitry Andric assert(TableSize >= Values.size() && "Can't fit values in table!");
49363861d79fSDimitry Andric
49373861d79fSDimitry Andric // If all values in the table are equal, this is that value.
49383861d79fSDimitry Andric SingleValue = Values.begin()->second;
49393861d79fSDimitry Andric
494091bc56edSDimitry Andric Type *ValueType = Values.begin()->second->getType();
494191bc56edSDimitry Andric
49423861d79fSDimitry Andric // Build up the table contents.
49433861d79fSDimitry Andric SmallVector<Constant *, 64> TableContents(TableSize);
49443861d79fSDimitry Andric for (size_t I = 0, E = Values.size(); I != E; ++I) {
49453861d79fSDimitry Andric ConstantInt *CaseVal = Values[I].first;
49463861d79fSDimitry Andric Constant *CaseRes = Values[I].second;
494791bc56edSDimitry Andric assert(CaseRes->getType() == ValueType);
49483861d79fSDimitry Andric
49493ca95b02SDimitry Andric uint64_t Idx = (CaseVal->getValue() - Offset->getValue()).getLimitedValue();
49503861d79fSDimitry Andric TableContents[Idx] = CaseRes;
49513861d79fSDimitry Andric
49523861d79fSDimitry Andric if (CaseRes != SingleValue)
495391bc56edSDimitry Andric SingleValue = nullptr;
49543861d79fSDimitry Andric }
49553861d79fSDimitry Andric
49563861d79fSDimitry Andric // Fill in any holes in the table with the default result.
49573861d79fSDimitry Andric if (Values.size() < TableSize) {
495891bc56edSDimitry Andric assert(DefaultValue &&
495991bc56edSDimitry Andric "Need a default value to fill the lookup table holes.");
496091bc56edSDimitry Andric assert(DefaultValue->getType() == ValueType);
49613861d79fSDimitry Andric for (uint64_t I = 0; I < TableSize; ++I) {
49623861d79fSDimitry Andric if (!TableContents[I])
49633861d79fSDimitry Andric TableContents[I] = DefaultValue;
49643861d79fSDimitry Andric }
49653861d79fSDimitry Andric
49663861d79fSDimitry Andric if (DefaultValue != SingleValue)
496791bc56edSDimitry Andric SingleValue = nullptr;
49683861d79fSDimitry Andric }
49693861d79fSDimitry Andric
49703861d79fSDimitry Andric // If each element in the table contains the same value, we only need to store
49713861d79fSDimitry Andric // that single value.
49723861d79fSDimitry Andric if (SingleValue) {
49733861d79fSDimitry Andric Kind = SingleValueKind;
49743861d79fSDimitry Andric return;
49753861d79fSDimitry Andric }
49763861d79fSDimitry Andric
497739d628a0SDimitry Andric // Check if we can derive the value with a linear transformation from the
497839d628a0SDimitry Andric // table index.
497939d628a0SDimitry Andric if (isa<IntegerType>(ValueType)) {
498039d628a0SDimitry Andric bool LinearMappingPossible = true;
498139d628a0SDimitry Andric APInt PrevVal;
498239d628a0SDimitry Andric APInt DistToPrev;
498339d628a0SDimitry Andric assert(TableSize >= 2 && "Should be a SingleValue table.");
498439d628a0SDimitry Andric // Check if there is the same distance between two consecutive values.
498539d628a0SDimitry Andric for (uint64_t I = 0; I < TableSize; ++I) {
498639d628a0SDimitry Andric ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
498739d628a0SDimitry Andric if (!ConstVal) {
498839d628a0SDimitry Andric // This is an undef. We could deal with it, but undefs in lookup tables
498939d628a0SDimitry Andric // are very seldom. It's probably not worth the additional complexity.
499039d628a0SDimitry Andric LinearMappingPossible = false;
499139d628a0SDimitry Andric break;
499239d628a0SDimitry Andric }
4993d8866befSDimitry Andric const APInt &Val = ConstVal->getValue();
499439d628a0SDimitry Andric if (I != 0) {
499539d628a0SDimitry Andric APInt Dist = Val - PrevVal;
499639d628a0SDimitry Andric if (I == 1) {
499739d628a0SDimitry Andric DistToPrev = Dist;
499839d628a0SDimitry Andric } else if (Dist != DistToPrev) {
499939d628a0SDimitry Andric LinearMappingPossible = false;
500039d628a0SDimitry Andric break;
500139d628a0SDimitry Andric }
500239d628a0SDimitry Andric }
500339d628a0SDimitry Andric PrevVal = Val;
500439d628a0SDimitry Andric }
500539d628a0SDimitry Andric if (LinearMappingPossible) {
500639d628a0SDimitry Andric LinearOffset = cast<ConstantInt>(TableContents[0]);
500739d628a0SDimitry Andric LinearMultiplier = ConstantInt::get(M.getContext(), DistToPrev);
500839d628a0SDimitry Andric Kind = LinearMapKind;
500939d628a0SDimitry Andric ++NumLinearMaps;
501039d628a0SDimitry Andric return;
501139d628a0SDimitry Andric }
501239d628a0SDimitry Andric }
501339d628a0SDimitry Andric
50143861d79fSDimitry Andric // If the type is integer and the table fits in a register, build a bitmap.
501591bc56edSDimitry Andric if (WouldFitInRegister(DL, TableSize, ValueType)) {
501691bc56edSDimitry Andric IntegerType *IT = cast<IntegerType>(ValueType);
50173861d79fSDimitry Andric APInt TableInt(TableSize * IT->getBitWidth(), 0);
50183861d79fSDimitry Andric for (uint64_t I = TableSize; I > 0; --I) {
50193861d79fSDimitry Andric TableInt <<= IT->getBitWidth();
50203861d79fSDimitry Andric // Insert values into the bitmap. Undef values are set to zero.
50213861d79fSDimitry Andric if (!isa<UndefValue>(TableContents[I - 1])) {
50223861d79fSDimitry Andric ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
50233861d79fSDimitry Andric TableInt |= Val->getValue().zext(TableInt.getBitWidth());
50243861d79fSDimitry Andric }
50253861d79fSDimitry Andric }
50263861d79fSDimitry Andric BitMap = ConstantInt::get(M.getContext(), TableInt);
50273861d79fSDimitry Andric BitMapElementTy = IT;
50283861d79fSDimitry Andric Kind = BitMapKind;
50293861d79fSDimitry Andric ++NumBitMaps;
50303861d79fSDimitry Andric return;
50313861d79fSDimitry Andric }
50323861d79fSDimitry Andric
50333861d79fSDimitry Andric // Store the table in an array.
503491bc56edSDimitry Andric ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
50353861d79fSDimitry Andric Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
50363861d79fSDimitry Andric
50373861d79fSDimitry Andric Array = new GlobalVariable(M, ArrayTy, /*constant=*/true,
50383ca95b02SDimitry Andric GlobalVariable::PrivateLinkage, Initializer,
5039a580b014SDimitry Andric "switch.table." + FuncName);
50403ca95b02SDimitry Andric Array->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
5041*b5893f02SDimitry Andric // Set the alignment to that of an array items. We will be only loading one
5042*b5893f02SDimitry Andric // value out of it.
5043*b5893f02SDimitry Andric Array->setAlignment(DL.getPrefTypeAlignment(ValueType));
50443861d79fSDimitry Andric Kind = ArrayKind;
50453861d79fSDimitry Andric }
50463861d79fSDimitry Andric
BuildLookup(Value * Index,IRBuilder<> & Builder)50473861d79fSDimitry Andric Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {
50483861d79fSDimitry Andric switch (Kind) {
50493861d79fSDimitry Andric case SingleValueKind:
50503861d79fSDimitry Andric return SingleValue;
505139d628a0SDimitry Andric case LinearMapKind: {
505239d628a0SDimitry Andric // Derive the result value from the input value.
505339d628a0SDimitry Andric Value *Result = Builder.CreateIntCast(Index, LinearMultiplier->getType(),
505439d628a0SDimitry Andric false, "switch.idx.cast");
505539d628a0SDimitry Andric if (!LinearMultiplier->isOne())
505639d628a0SDimitry Andric Result = Builder.CreateMul(Result, LinearMultiplier, "switch.idx.mult");
505739d628a0SDimitry Andric if (!LinearOffset->isZero())
505839d628a0SDimitry Andric Result = Builder.CreateAdd(Result, LinearOffset, "switch.offset");
505939d628a0SDimitry Andric return Result;
506039d628a0SDimitry Andric }
50613861d79fSDimitry Andric case BitMapKind: {
50623861d79fSDimitry Andric // Type of the bitmap (e.g. i59).
50633861d79fSDimitry Andric IntegerType *MapTy = BitMap->getType();
50643861d79fSDimitry Andric
50653861d79fSDimitry Andric // Cast Index to the same type as the bitmap.
50663861d79fSDimitry Andric // Note: The Index is <= the number of elements in the table, so
50673861d79fSDimitry Andric // truncating it to the width of the bitmask is safe.
50683861d79fSDimitry Andric Value *ShiftAmt = Builder.CreateZExtOrTrunc(Index, MapTy, "switch.cast");
50693861d79fSDimitry Andric
50703861d79fSDimitry Andric // Multiply the shift amount by the element width.
50713ca95b02SDimitry Andric ShiftAmt = Builder.CreateMul(
50723ca95b02SDimitry Andric ShiftAmt, ConstantInt::get(MapTy, BitMapElementTy->getBitWidth()),
50733861d79fSDimitry Andric "switch.shiftamt");
50743861d79fSDimitry Andric
50753861d79fSDimitry Andric // Shift down.
50763ca95b02SDimitry Andric Value *DownShifted =
50773ca95b02SDimitry Andric Builder.CreateLShr(BitMap, ShiftAmt, "switch.downshift");
50783861d79fSDimitry Andric // Mask off.
50793ca95b02SDimitry Andric return Builder.CreateTrunc(DownShifted, BitMapElementTy, "switch.masked");
50803861d79fSDimitry Andric }
50813861d79fSDimitry Andric case ArrayKind: {
508291bc56edSDimitry Andric // Make sure the table index will not overflow when treated as signed.
508391bc56edSDimitry Andric IntegerType *IT = cast<IntegerType>(Index->getType());
50843ca95b02SDimitry Andric uint64_t TableSize =
50853ca95b02SDimitry Andric Array->getInitializer()->getType()->getArrayNumElements();
508691bc56edSDimitry Andric if (TableSize > (1ULL << (IT->getBitWidth() - 1)))
50873ca95b02SDimitry Andric Index = Builder.CreateZExt(
50883ca95b02SDimitry Andric Index, IntegerType::get(IT->getContext(), IT->getBitWidth() + 1),
508991bc56edSDimitry Andric "switch.tableidx.zext");
509091bc56edSDimitry Andric
50913861d79fSDimitry Andric Value *GEPIndices[] = {Builder.getInt32(0), Index};
5092ff0cc061SDimitry Andric Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
5093ff0cc061SDimitry Andric GEPIndices, "switch.gep");
50943861d79fSDimitry Andric return Builder.CreateLoad(GEP, "switch.load");
50953861d79fSDimitry Andric }
50963861d79fSDimitry Andric }
50973861d79fSDimitry Andric llvm_unreachable("Unknown lookup table kind!");
50983861d79fSDimitry Andric }
50993861d79fSDimitry Andric
WouldFitInRegister(const DataLayout & DL,uint64_t TableSize,Type * ElementType)5100ff0cc061SDimitry Andric bool SwitchLookupTable::WouldFitInRegister(const DataLayout &DL,
51013861d79fSDimitry Andric uint64_t TableSize,
51027d523365SDimitry Andric Type *ElementType) {
51037d523365SDimitry Andric auto *IT = dyn_cast<IntegerType>(ElementType);
51043861d79fSDimitry Andric if (!IT)
51053861d79fSDimitry Andric return false;
51063861d79fSDimitry Andric // FIXME: If the type is wider than it needs to be, e.g. i8 but all values
51073861d79fSDimitry Andric // are <= 15, we could try to narrow the type.
51083861d79fSDimitry Andric
51093861d79fSDimitry Andric // Avoid overflow, fitsInLegalInteger uses unsigned int for the width.
51103861d79fSDimitry Andric if (TableSize >= UINT_MAX / IT->getBitWidth())
51113861d79fSDimitry Andric return false;
5112ff0cc061SDimitry Andric return DL.fitsInLegalInteger(TableSize * IT->getBitWidth());
51133861d79fSDimitry Andric }
51143861d79fSDimitry Andric
51153dac3a9bSDimitry Andric /// Determine whether a lookup table should be built for this switch, based on
51163dac3a9bSDimitry Andric /// the number of cases, size of the table, and the types of the results.
5117ff0cc061SDimitry Andric static bool
ShouldBuildLookupTable(SwitchInst * SI,uint64_t TableSize,const TargetTransformInfo & TTI,const DataLayout & DL,const SmallDenseMap<PHINode *,Type * > & ResultTypes)5118ff0cc061SDimitry Andric ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize,
5119ff0cc061SDimitry Andric const TargetTransformInfo &TTI, const DataLayout &DL,
51203861d79fSDimitry Andric const SmallDenseMap<PHINode *, Type *> &ResultTypes) {
5121139f7f9bSDimitry Andric if (SI->getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
5122139f7f9bSDimitry Andric return false; // TableSize overflowed, or mul below might overflow.
5123139f7f9bSDimitry Andric
5124139f7f9bSDimitry Andric bool AllTablesFitInRegister = true;
5125139f7f9bSDimitry Andric bool HasIllegalType = false;
512639d628a0SDimitry Andric for (const auto &I : ResultTypes) {
512739d628a0SDimitry Andric Type *Ty = I.second;
5128139f7f9bSDimitry Andric
5129139f7f9bSDimitry Andric // Saturate this flag to true.
5130139f7f9bSDimitry Andric HasIllegalType = HasIllegalType || !TTI.isTypeLegal(Ty);
5131139f7f9bSDimitry Andric
5132139f7f9bSDimitry Andric // Saturate this flag to false.
51333ca95b02SDimitry Andric AllTablesFitInRegister =
51343ca95b02SDimitry Andric AllTablesFitInRegister &&
513591bc56edSDimitry Andric SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
5136139f7f9bSDimitry Andric
5137139f7f9bSDimitry Andric // If both flags saturate, we're done. NOTE: This *only* works with
5138139f7f9bSDimitry Andric // saturating flags, and all flags have to saturate first due to the
5139139f7f9bSDimitry Andric // non-deterministic behavior of iterating over a dense map.
5140139f7f9bSDimitry Andric if (HasIllegalType && !AllTablesFitInRegister)
5141139f7f9bSDimitry Andric break;
5142139f7f9bSDimitry Andric }
5143139f7f9bSDimitry Andric
5144139f7f9bSDimitry Andric // If each table would fit in a register, we should build it anyway.
5145139f7f9bSDimitry Andric if (AllTablesFitInRegister)
5146139f7f9bSDimitry Andric return true;
5147139f7f9bSDimitry Andric
5148139f7f9bSDimitry Andric // Don't build a table that doesn't fit in-register if it has illegal types.
5149139f7f9bSDimitry Andric if (HasIllegalType)
5150139f7f9bSDimitry Andric return false;
5151139f7f9bSDimitry Andric
51523861d79fSDimitry Andric // The table density should be at least 40%. This is the same criterion as for
51533861d79fSDimitry Andric // jump tables, see SelectionDAGBuilder::handleJTSwitchCase.
51543861d79fSDimitry Andric // FIXME: Find the best cut-off.
5155139f7f9bSDimitry Andric return SI->getNumCases() * 10 >= TableSize * 4;
51563861d79fSDimitry Andric }
51573861d79fSDimitry Andric
515839d628a0SDimitry Andric /// Try to reuse the switch table index compare. Following pattern:
515939d628a0SDimitry Andric /// \code
516039d628a0SDimitry Andric /// if (idx < tablesize)
516139d628a0SDimitry Andric /// r = table[idx]; // table does not contain default_value
516239d628a0SDimitry Andric /// else
516339d628a0SDimitry Andric /// r = default_value;
516439d628a0SDimitry Andric /// if (r != default_value)
516539d628a0SDimitry Andric /// ...
516639d628a0SDimitry Andric /// \endcode
516739d628a0SDimitry Andric /// Is optimized to:
516839d628a0SDimitry Andric /// \code
516939d628a0SDimitry Andric /// cond = idx < tablesize;
517039d628a0SDimitry Andric /// if (cond)
517139d628a0SDimitry Andric /// r = table[idx];
517239d628a0SDimitry Andric /// else
517339d628a0SDimitry Andric /// r = default_value;
517439d628a0SDimitry Andric /// if (cond)
517539d628a0SDimitry Andric /// ...
517639d628a0SDimitry Andric /// \endcode
517739d628a0SDimitry Andric /// Jump threading will then eliminate the second if(cond).
reuseTableCompare(User * PhiUser,BasicBlock * PhiBlock,BranchInst * RangeCheckBranch,Constant * DefaultValue,const SmallVectorImpl<std::pair<ConstantInt *,Constant * >> & Values)51783ca95b02SDimitry Andric static void reuseTableCompare(
51793ca95b02SDimitry Andric User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch,
51803ca95b02SDimitry Andric Constant *DefaultValue,
518139d628a0SDimitry Andric const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
518239d628a0SDimitry Andric ICmpInst *CmpInst = dyn_cast<ICmpInst>(PhiUser);
518339d628a0SDimitry Andric if (!CmpInst)
518439d628a0SDimitry Andric return;
518539d628a0SDimitry Andric
518639d628a0SDimitry Andric // We require that the compare is in the same block as the phi so that jump
518739d628a0SDimitry Andric // threading can do its work afterwards.
518839d628a0SDimitry Andric if (CmpInst->getParent() != PhiBlock)
518939d628a0SDimitry Andric return;
519039d628a0SDimitry Andric
519139d628a0SDimitry Andric Constant *CmpOp1 = dyn_cast<Constant>(CmpInst->getOperand(1));
519239d628a0SDimitry Andric if (!CmpOp1)
519339d628a0SDimitry Andric return;
519439d628a0SDimitry Andric
519539d628a0SDimitry Andric Value *RangeCmp = RangeCheckBranch->getCondition();
519639d628a0SDimitry Andric Constant *TrueConst = ConstantInt::getTrue(RangeCmp->getType());
519739d628a0SDimitry Andric Constant *FalseConst = ConstantInt::getFalse(RangeCmp->getType());
519839d628a0SDimitry Andric
519939d628a0SDimitry Andric // Check if the compare with the default value is constant true or false.
520039d628a0SDimitry Andric Constant *DefaultConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
520139d628a0SDimitry Andric DefaultValue, CmpOp1, true);
520239d628a0SDimitry Andric if (DefaultConst != TrueConst && DefaultConst != FalseConst)
520339d628a0SDimitry Andric return;
520439d628a0SDimitry Andric
520539d628a0SDimitry Andric // Check if the compare with the case values is distinct from the default
520639d628a0SDimitry Andric // compare result.
520739d628a0SDimitry Andric for (auto ValuePair : Values) {
520839d628a0SDimitry Andric Constant *CaseConst = ConstantExpr::getICmp(CmpInst->getPredicate(),
520939d628a0SDimitry Andric ValuePair.second, CmpOp1, true);
52102cab237bSDimitry Andric if (!CaseConst || CaseConst == DefaultConst || isa<UndefValue>(CaseConst))
521139d628a0SDimitry Andric return;
521239d628a0SDimitry Andric assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
521339d628a0SDimitry Andric "Expect true or false as compare result.");
521439d628a0SDimitry Andric }
521539d628a0SDimitry Andric
521639d628a0SDimitry Andric // Check if the branch instruction dominates the phi node. It's a simple
521739d628a0SDimitry Andric // dominance check, but sufficient for our needs.
521839d628a0SDimitry Andric // Although this check is invariant in the calling loops, it's better to do it
521939d628a0SDimitry Andric // at this late stage. Practically we do it at most once for a switch.
522039d628a0SDimitry Andric BasicBlock *BranchBlock = RangeCheckBranch->getParent();
522139d628a0SDimitry Andric for (auto PI = pred_begin(PhiBlock), E = pred_end(PhiBlock); PI != E; ++PI) {
522239d628a0SDimitry Andric BasicBlock *Pred = *PI;
522339d628a0SDimitry Andric if (Pred != BranchBlock && Pred->getUniquePredecessor() != BranchBlock)
522439d628a0SDimitry Andric return;
522539d628a0SDimitry Andric }
522639d628a0SDimitry Andric
522739d628a0SDimitry Andric if (DefaultConst == FalseConst) {
522839d628a0SDimitry Andric // The compare yields the same result. We can replace it.
522939d628a0SDimitry Andric CmpInst->replaceAllUsesWith(RangeCmp);
523039d628a0SDimitry Andric ++NumTableCmpReuses;
523139d628a0SDimitry Andric } else {
523239d628a0SDimitry Andric // The compare yields the same result, just inverted. We can replace it.
52333ca95b02SDimitry Andric Value *InvertedTableCmp = BinaryOperator::CreateXor(
52343ca95b02SDimitry Andric RangeCmp, ConstantInt::get(RangeCmp->getType(), 1), "inverted.cmp",
523539d628a0SDimitry Andric RangeCheckBranch);
523639d628a0SDimitry Andric CmpInst->replaceAllUsesWith(InvertedTableCmp);
523739d628a0SDimitry Andric ++NumTableCmpReuses;
523839d628a0SDimitry Andric }
523939d628a0SDimitry Andric }
524039d628a0SDimitry Andric
52413dac3a9bSDimitry Andric /// If the switch is only used to initialize one or more phi nodes in a common
52423dac3a9bSDimitry Andric /// successor block with different constant values, replace the switch with
52433dac3a9bSDimitry Andric /// lookup tables.
SwitchToLookupTable(SwitchInst * SI,IRBuilder<> & Builder,const DataLayout & DL,const TargetTransformInfo & TTI)5244ff0cc061SDimitry Andric static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
5245ff0cc061SDimitry Andric const DataLayout &DL,
5246ff0cc061SDimitry Andric const TargetTransformInfo &TTI) {
52473861d79fSDimitry Andric assert(SI->getNumCases() > 1 && "Degenerate switch?");
52483861d79fSDimitry Andric
52492cab237bSDimitry Andric Function *Fn = SI->getParent()->getParent();
52502cab237bSDimitry Andric // Only build lookup table when we have a target that supports it or the
52512cab237bSDimitry Andric // attribute is not set.
52522cab237bSDimitry Andric if (!TTI.shouldBuildLookupTables() ||
52532cab237bSDimitry Andric (Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true"))
52543861d79fSDimitry Andric return false;
52553861d79fSDimitry Andric
52563861d79fSDimitry Andric // FIXME: If the switch is too sparse for a lookup table, perhaps we could
52573861d79fSDimitry Andric // split off a dense part and build a lookup table for that.
52583861d79fSDimitry Andric
52593861d79fSDimitry Andric // FIXME: This creates arrays of GEPs to constant strings, which means each
52603861d79fSDimitry Andric // GEP needs a runtime relocation in PIC code. We should just build one big
52613861d79fSDimitry Andric // string and lookup indices into that.
52623861d79fSDimitry Andric
52633ca95b02SDimitry Andric // Ignore switches with less than three cases. Lookup tables will not make
52642cab237bSDimitry Andric // them faster, so we don't analyze them.
526591bc56edSDimitry Andric if (SI->getNumCases() < 3)
52663861d79fSDimitry Andric return false;
52673861d79fSDimitry Andric
52683861d79fSDimitry Andric // Figure out the corresponding result for each case value and phi node in the
52698f0fd8f6SDimitry Andric // common destination, as well as the min and max case values.
5270*b5893f02SDimitry Andric assert(!empty(SI->cases()));
52713861d79fSDimitry Andric SwitchInst::CaseIt CI = SI->case_begin();
52727a7e6055SDimitry Andric ConstantInt *MinCaseVal = CI->getCaseValue();
52737a7e6055SDimitry Andric ConstantInt *MaxCaseVal = CI->getCaseValue();
52743861d79fSDimitry Andric
527591bc56edSDimitry Andric BasicBlock *CommonDest = nullptr;
52762cab237bSDimitry Andric
52772cab237bSDimitry Andric using ResultListTy = SmallVector<std::pair<ConstantInt *, Constant *>, 4>;
52783861d79fSDimitry Andric SmallDenseMap<PHINode *, ResultListTy> ResultLists;
52792cab237bSDimitry Andric
52803861d79fSDimitry Andric SmallDenseMap<PHINode *, Constant *> DefaultResults;
52813861d79fSDimitry Andric SmallDenseMap<PHINode *, Type *> ResultTypes;
52823861d79fSDimitry Andric SmallVector<PHINode *, 4> PHIs;
52833861d79fSDimitry Andric
52843861d79fSDimitry Andric for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
52857a7e6055SDimitry Andric ConstantInt *CaseVal = CI->getCaseValue();
52863861d79fSDimitry Andric if (CaseVal->getValue().slt(MinCaseVal->getValue()))
52873861d79fSDimitry Andric MinCaseVal = CaseVal;
52883861d79fSDimitry Andric if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
52893861d79fSDimitry Andric MaxCaseVal = CaseVal;
52903861d79fSDimitry Andric
52913861d79fSDimitry Andric // Resulting value at phi nodes for this case value.
52922cab237bSDimitry Andric using ResultsTy = SmallVector<std::pair<PHINode *, Constant *>, 4>;
52933861d79fSDimitry Andric ResultsTy Results;
52947a7e6055SDimitry Andric if (!GetCaseResults(SI, CaseVal, CI->getCaseSuccessor(), &CommonDest,
5295d88c1a5aSDimitry Andric Results, DL, TTI))
52963861d79fSDimitry Andric return false;
52973861d79fSDimitry Andric
52983861d79fSDimitry Andric // Append the result from this case to the list for each phi.
529939d628a0SDimitry Andric for (const auto &I : Results) {
530039d628a0SDimitry Andric PHINode *PHI = I.first;
530139d628a0SDimitry Andric Constant *Value = I.second;
530239d628a0SDimitry Andric if (!ResultLists.count(PHI))
530339d628a0SDimitry Andric PHIs.push_back(PHI);
530439d628a0SDimitry Andric ResultLists[PHI].push_back(std::make_pair(CaseVal, Value));
53053861d79fSDimitry Andric }
53063861d79fSDimitry Andric }
53073861d79fSDimitry Andric
530891bc56edSDimitry Andric // Keep track of the result types.
530939d628a0SDimitry Andric for (PHINode *PHI : PHIs) {
531091bc56edSDimitry Andric ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
531191bc56edSDimitry Andric }
531291bc56edSDimitry Andric
531391bc56edSDimitry Andric uint64_t NumResults = ResultLists[PHIs[0]].size();
531491bc56edSDimitry Andric APInt RangeSpread = MaxCaseVal->getValue() - MinCaseVal->getValue();
531591bc56edSDimitry Andric uint64_t TableSize = RangeSpread.getLimitedValue() + 1;
531691bc56edSDimitry Andric bool TableHasHoles = (NumResults < TableSize);
531791bc56edSDimitry Andric
531891bc56edSDimitry Andric // If the table has holes, we need a constant result for the default case
531991bc56edSDimitry Andric // or a bitmask that fits in a register.
53203861d79fSDimitry Andric SmallVector<std::pair<PHINode *, Constant *>, 4> DefaultResultsList;
5321d88c1a5aSDimitry Andric bool HasDefaultResults =
5322d88c1a5aSDimitry Andric GetCaseResults(SI, nullptr, SI->getDefaultDest(), &CommonDest,
5323d88c1a5aSDimitry Andric DefaultResultsList, DL, TTI);
532439d628a0SDimitry Andric
532591bc56edSDimitry Andric bool NeedMask = (TableHasHoles && !HasDefaultResults);
532691bc56edSDimitry Andric if (NeedMask) {
532791bc56edSDimitry Andric // As an extra penalty for the validity test we require more cases.
532891bc56edSDimitry Andric if (SI->getNumCases() < 4) // FIXME: Find best threshold value (benchmark).
53293861d79fSDimitry Andric return false;
5330ff0cc061SDimitry Andric if (!DL.fitsInLegalInteger(TableSize))
533191bc56edSDimitry Andric return false;
533291bc56edSDimitry Andric }
533391bc56edSDimitry Andric
533439d628a0SDimitry Andric for (const auto &I : DefaultResultsList) {
533539d628a0SDimitry Andric PHINode *PHI = I.first;
533639d628a0SDimitry Andric Constant *Result = I.second;
53373861d79fSDimitry Andric DefaultResults[PHI] = Result;
53383861d79fSDimitry Andric }
53393861d79fSDimitry Andric
534091bc56edSDimitry Andric if (!ShouldBuildLookupTable(SI, TableSize, TTI, DL, ResultTypes))
53413861d79fSDimitry Andric return false;
53423861d79fSDimitry Andric
53433861d79fSDimitry Andric // Create the BB that does the lookups.
53443861d79fSDimitry Andric Module &Mod = *CommonDest->getParent()->getParent();
53453ca95b02SDimitry Andric BasicBlock *LookupBB = BasicBlock::Create(
53463ca95b02SDimitry Andric Mod.getContext(), "switch.lookup", CommonDest->getParent(), CommonDest);
53473861d79fSDimitry Andric
5348f785676fSDimitry Andric // Compute the table index value.
53493861d79fSDimitry Andric Builder.SetInsertPoint(SI);
53502cab237bSDimitry Andric Value *TableIndex;
53512cab237bSDimitry Andric if (MinCaseVal->isNullValue())
53522cab237bSDimitry Andric TableIndex = SI->getCondition();
53532cab237bSDimitry Andric else
53542cab237bSDimitry Andric TableIndex = Builder.CreateSub(SI->getCondition(), MinCaseVal,
53552cab237bSDimitry Andric "switch.tableidx");
5356f785676fSDimitry Andric
5357f785676fSDimitry Andric // Compute the maximum table size representable by the integer type we are
5358f785676fSDimitry Andric // switching upon.
5359f785676fSDimitry Andric unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();
5360f785676fSDimitry Andric uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
5361f785676fSDimitry Andric assert(MaxTableSize >= TableSize &&
5362f785676fSDimitry Andric "It is impossible for a switch to have more entries than the max "
5363f785676fSDimitry Andric "representable value of its input integer type's size.");
5364f785676fSDimitry Andric
5365ff0cc061SDimitry Andric // If the default destination is unreachable, or if the lookup table covers
5366ff0cc061SDimitry Andric // all values of the conditional variable, branch directly to the lookup table
5367ff0cc061SDimitry Andric // BB. Otherwise, check that the condition is within the case range.
5368ff0cc061SDimitry Andric const bool DefaultIsReachable =
5369ff0cc061SDimitry Andric !isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
5370ff0cc061SDimitry Andric const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
537139d628a0SDimitry Andric BranchInst *RangeCheckBranch = nullptr;
537239d628a0SDimitry Andric
5373ff0cc061SDimitry Andric if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5374f785676fSDimitry Andric Builder.CreateBr(LookupBB);
5375ff0cc061SDimitry Andric // Note: We call removeProdecessor later since we need to be able to get the
5376ff0cc061SDimitry Andric // PHI value for the default case in case we're using a bit mask.
5377f785676fSDimitry Andric } else {
53783ca95b02SDimitry Andric Value *Cmp = Builder.CreateICmpULT(
53793ca95b02SDimitry Andric TableIndex, ConstantInt::get(MinCaseVal->getType(), TableSize));
53803ca95b02SDimitry Andric RangeCheckBranch =
53813ca95b02SDimitry Andric Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());
5382f785676fSDimitry Andric }
53833861d79fSDimitry Andric
53843861d79fSDimitry Andric // Populate the BB that does the lookups.
53853861d79fSDimitry Andric Builder.SetInsertPoint(LookupBB);
538691bc56edSDimitry Andric
538791bc56edSDimitry Andric if (NeedMask) {
53882cab237bSDimitry Andric // Before doing the lookup, we do the hole check. The LookupBB is therefore
53892cab237bSDimitry Andric // re-purposed to do the hole check, and we create a new LookupBB.
539091bc56edSDimitry Andric BasicBlock *MaskBB = LookupBB;
539191bc56edSDimitry Andric MaskBB->setName("switch.hole_check");
53923ca95b02SDimitry Andric LookupBB = BasicBlock::Create(Mod.getContext(), "switch.lookup",
53933ca95b02SDimitry Andric CommonDest->getParent(), CommonDest);
539491bc56edSDimitry Andric
53952cab237bSDimitry Andric // Make the mask's bitwidth at least 8-bit and a power-of-2 to avoid
539639d628a0SDimitry Andric // unnecessary illegal types.
539739d628a0SDimitry Andric uint64_t TableSizePowOf2 = NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
539839d628a0SDimitry Andric APInt MaskInt(TableSizePowOf2, 0);
539939d628a0SDimitry Andric APInt One(TableSizePowOf2, 1);
540091bc56edSDimitry Andric // Build bitmask; fill in a 1 bit for every case.
540191bc56edSDimitry Andric const ResultListTy &ResultList = ResultLists[PHIs[0]];
540291bc56edSDimitry Andric for (size_t I = 0, E = ResultList.size(); I != E; ++I) {
54033ca95b02SDimitry Andric uint64_t Idx = (ResultList[I].first->getValue() - MinCaseVal->getValue())
54043ca95b02SDimitry Andric .getLimitedValue();
540591bc56edSDimitry Andric MaskInt |= One << Idx;
540691bc56edSDimitry Andric }
540791bc56edSDimitry Andric ConstantInt *TableMask = ConstantInt::get(Mod.getContext(), MaskInt);
540891bc56edSDimitry Andric
540991bc56edSDimitry Andric // Get the TableIndex'th bit of the bitmask.
541091bc56edSDimitry Andric // If this bit is 0 (meaning hole) jump to the default destination,
541191bc56edSDimitry Andric // else continue with table lookup.
541291bc56edSDimitry Andric IntegerType *MapTy = TableMask->getType();
54133ca95b02SDimitry Andric Value *MaskIndex =
54143ca95b02SDimitry Andric Builder.CreateZExtOrTrunc(TableIndex, MapTy, "switch.maskindex");
54153ca95b02SDimitry Andric Value *Shifted = Builder.CreateLShr(TableMask, MaskIndex, "switch.shifted");
54163ca95b02SDimitry Andric Value *LoBit = Builder.CreateTrunc(
54173ca95b02SDimitry Andric Shifted, Type::getInt1Ty(Mod.getContext()), "switch.lobit");
541891bc56edSDimitry Andric Builder.CreateCondBr(LoBit, LookupBB, SI->getDefaultDest());
541991bc56edSDimitry Andric
542091bc56edSDimitry Andric Builder.SetInsertPoint(LookupBB);
542191bc56edSDimitry Andric AddPredecessorToBlock(SI->getDefaultDest(), MaskBB, SI->getParent());
542291bc56edSDimitry Andric }
542391bc56edSDimitry Andric
5424ff0cc061SDimitry Andric if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
54252cab237bSDimitry Andric // We cached PHINodes in PHIs. To avoid accessing deleted PHINodes later,
5426ff0cc061SDimitry Andric // do not delete PHINodes here.
5427ff0cc061SDimitry Andric SI->getDefaultDest()->removePredecessor(SI->getParent(),
5428ff0cc061SDimitry Andric /*DontDeleteUselessPHIs=*/true);
5429ff0cc061SDimitry Andric }
5430ff0cc061SDimitry Andric
54313861d79fSDimitry Andric bool ReturnedEarly = false;
54324ba319b5SDimitry Andric for (PHINode *PHI : PHIs) {
543339d628a0SDimitry Andric const ResultListTy &ResultList = ResultLists[PHI];
54343861d79fSDimitry Andric
543591bc56edSDimitry Andric // If using a bitmask, use any value to fill the lookup table holes.
543691bc56edSDimitry Andric Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
54372cab237bSDimitry Andric StringRef FuncName = Fn->getName();
5438a580b014SDimitry Andric SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL,
5439a580b014SDimitry Andric FuncName);
54403861d79fSDimitry Andric
54413861d79fSDimitry Andric Value *Result = Table.BuildLookup(TableIndex, Builder);
54423861d79fSDimitry Andric
54433861d79fSDimitry Andric // If the result is used to return immediately from the function, we want to
54443861d79fSDimitry Andric // do that right here.
544591bc56edSDimitry Andric if (PHI->hasOneUse() && isa<ReturnInst>(*PHI->user_begin()) &&
544691bc56edSDimitry Andric PHI->user_back() == CommonDest->getFirstNonPHIOrDbg()) {
54473861d79fSDimitry Andric Builder.CreateRet(Result);
54483861d79fSDimitry Andric ReturnedEarly = true;
54493861d79fSDimitry Andric break;
54503861d79fSDimitry Andric }
54513861d79fSDimitry Andric
545239d628a0SDimitry Andric // Do a small peephole optimization: re-use the switch table compare if
545339d628a0SDimitry Andric // possible.
545439d628a0SDimitry Andric if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
545539d628a0SDimitry Andric BasicBlock *PhiBlock = PHI->getParent();
545639d628a0SDimitry Andric // Search for compare instructions which use the phi.
545739d628a0SDimitry Andric for (auto *User : PHI->users()) {
545839d628a0SDimitry Andric reuseTableCompare(User, PhiBlock, RangeCheckBranch, DV, ResultList);
545939d628a0SDimitry Andric }
546039d628a0SDimitry Andric }
546139d628a0SDimitry Andric
54623861d79fSDimitry Andric PHI->addIncoming(Result, LookupBB);
54633861d79fSDimitry Andric }
54643861d79fSDimitry Andric
54653861d79fSDimitry Andric if (!ReturnedEarly)
54663861d79fSDimitry Andric Builder.CreateBr(CommonDest);
54673861d79fSDimitry Andric
54683861d79fSDimitry Andric // Remove the switch.
5469f785676fSDimitry Andric for (unsigned i = 0, e = SI->getNumSuccessors(); i < e; ++i) {
54703861d79fSDimitry Andric BasicBlock *Succ = SI->getSuccessor(i);
5471f785676fSDimitry Andric
5472f785676fSDimitry Andric if (Succ == SI->getDefaultDest())
5473f785676fSDimitry Andric continue;
54743861d79fSDimitry Andric Succ->removePredecessor(SI->getParent());
54753861d79fSDimitry Andric }
54763861d79fSDimitry Andric SI->eraseFromParent();
54773861d79fSDimitry Andric
54783861d79fSDimitry Andric ++NumLookupTables;
547991bc56edSDimitry Andric if (NeedMask)
548091bc56edSDimitry Andric ++NumLookupTablesHoles;
54813861d79fSDimitry Andric return true;
54823861d79fSDimitry Andric }
54833861d79fSDimitry Andric
isSwitchDense(ArrayRef<int64_t> Values)5484d88c1a5aSDimitry Andric static bool isSwitchDense(ArrayRef<int64_t> Values) {
5485d88c1a5aSDimitry Andric // See also SelectionDAGBuilder::isDense(), which this function was based on.
5486d88c1a5aSDimitry Andric uint64_t Diff = (uint64_t)Values.back() - (uint64_t)Values.front();
5487d88c1a5aSDimitry Andric uint64_t Range = Diff + 1;
5488d88c1a5aSDimitry Andric uint64_t NumCases = Values.size();
5489d88c1a5aSDimitry Andric // 40% is the default density for building a jump table in optsize/minsize mode.
5490d88c1a5aSDimitry Andric uint64_t MinDensity = 40;
5491d88c1a5aSDimitry Andric
5492d88c1a5aSDimitry Andric return NumCases * 100 >= Range * MinDensity;
5493d88c1a5aSDimitry Andric }
5494d88c1a5aSDimitry Andric
54952cab237bSDimitry Andric /// Try to transform a switch that has "holes" in it to a contiguous sequence
54962cab237bSDimitry Andric /// of cases.
54972cab237bSDimitry Andric ///
54982cab237bSDimitry Andric /// A switch such as: switch(i) {case 5: case 9: case 13: case 17:} can be
54992cab237bSDimitry Andric /// range-reduced to: switch ((i-5) / 4) {case 0: case 1: case 2: case 3:}.
55002cab237bSDimitry Andric ///
55012cab237bSDimitry Andric /// This converts a sparse switch into a dense switch which allows better
55022cab237bSDimitry Andric /// lowering and could also allow transforming into a lookup table.
ReduceSwitchRange(SwitchInst * SI,IRBuilder<> & Builder,const DataLayout & DL,const TargetTransformInfo & TTI)5503d88c1a5aSDimitry Andric static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder,
5504d88c1a5aSDimitry Andric const DataLayout &DL,
5505d88c1a5aSDimitry Andric const TargetTransformInfo &TTI) {
5506d88c1a5aSDimitry Andric auto *CondTy = cast<IntegerType>(SI->getCondition()->getType());
5507d88c1a5aSDimitry Andric if (CondTy->getIntegerBitWidth() > 64 ||
5508d88c1a5aSDimitry Andric !DL.fitsInLegalInteger(CondTy->getIntegerBitWidth()))
5509d88c1a5aSDimitry Andric return false;
5510d88c1a5aSDimitry Andric // Only bother with this optimization if there are more than 3 switch cases;
5511d88c1a5aSDimitry Andric // SDAG will only bother creating jump tables for 4 or more cases.
5512d88c1a5aSDimitry Andric if (SI->getNumCases() < 4)
5513d88c1a5aSDimitry Andric return false;
5514d88c1a5aSDimitry Andric
5515d88c1a5aSDimitry Andric // This transform is agnostic to the signedness of the input or case values. We
5516d88c1a5aSDimitry Andric // can treat the case values as signed or unsigned. We can optimize more common
5517d88c1a5aSDimitry Andric // cases such as a sequence crossing zero {-4,0,4,8} if we interpret case values
5518d88c1a5aSDimitry Andric // as signed.
5519d88c1a5aSDimitry Andric SmallVector<int64_t,4> Values;
5520d88c1a5aSDimitry Andric for (auto &C : SI->cases())
5521d88c1a5aSDimitry Andric Values.push_back(C.getCaseValue()->getValue().getSExtValue());
5522*b5893f02SDimitry Andric llvm::sort(Values);
5523d88c1a5aSDimitry Andric
5524d88c1a5aSDimitry Andric // If the switch is already dense, there's nothing useful to do here.
5525d88c1a5aSDimitry Andric if (isSwitchDense(Values))
5526d88c1a5aSDimitry Andric return false;
5527d88c1a5aSDimitry Andric
5528d88c1a5aSDimitry Andric // First, transform the values such that they start at zero and ascend.
5529d88c1a5aSDimitry Andric int64_t Base = Values[0];
5530d88c1a5aSDimitry Andric for (auto &V : Values)
55312cab237bSDimitry Andric V -= (uint64_t)(Base);
5532d88c1a5aSDimitry Andric
5533d88c1a5aSDimitry Andric // Now we have signed numbers that have been shifted so that, given enough
5534d88c1a5aSDimitry Andric // precision, there are no negative values. Since the rest of the transform
5535d88c1a5aSDimitry Andric // is bitwise only, we switch now to an unsigned representation.
5536d88c1a5aSDimitry Andric uint64_t GCD = 0;
5537d88c1a5aSDimitry Andric for (auto &V : Values)
5538d88c1a5aSDimitry Andric GCD = GreatestCommonDivisor64(GCD, (uint64_t)V);
5539d88c1a5aSDimitry Andric
5540d88c1a5aSDimitry Andric // This transform can be done speculatively because it is so cheap - it results
5541d88c1a5aSDimitry Andric // in a single rotate operation being inserted. This can only happen if the
5542d88c1a5aSDimitry Andric // factor extracted is a power of 2.
5543d88c1a5aSDimitry Andric // FIXME: If the GCD is an odd number we can multiply by the multiplicative
5544d88c1a5aSDimitry Andric // inverse of GCD and then perform this transform.
5545d88c1a5aSDimitry Andric // FIXME: It's possible that optimizing a switch on powers of two might also
5546d88c1a5aSDimitry Andric // be beneficial - flag values are often powers of two and we could use a CLZ
5547d88c1a5aSDimitry Andric // as the key function.
5548d88c1a5aSDimitry Andric if (GCD <= 1 || !isPowerOf2_64(GCD))
5549d88c1a5aSDimitry Andric // No common divisor found or too expensive to compute key function.
5550d88c1a5aSDimitry Andric return false;
5551d88c1a5aSDimitry Andric
5552d88c1a5aSDimitry Andric unsigned Shift = Log2_64(GCD);
5553d88c1a5aSDimitry Andric for (auto &V : Values)
5554d88c1a5aSDimitry Andric V = (int64_t)((uint64_t)V >> Shift);
5555d88c1a5aSDimitry Andric
5556d88c1a5aSDimitry Andric if (!isSwitchDense(Values))
5557d88c1a5aSDimitry Andric // Transform didn't create a dense switch.
5558d88c1a5aSDimitry Andric return false;
5559d88c1a5aSDimitry Andric
5560d88c1a5aSDimitry Andric // The obvious transform is to shift the switch condition right and emit a
5561d88c1a5aSDimitry Andric // check that the condition actually cleanly divided by GCD, i.e.
5562d88c1a5aSDimitry Andric // C & (1 << Shift - 1) == 0
5563d88c1a5aSDimitry Andric // inserting a new CFG edge to handle the case where it didn't divide cleanly.
5564d88c1a5aSDimitry Andric //
5565d88c1a5aSDimitry Andric // A cheaper way of doing this is a simple ROTR(C, Shift). This performs the
5566d88c1a5aSDimitry Andric // shift and puts the shifted-off bits in the uppermost bits. If any of these
5567d88c1a5aSDimitry Andric // are nonzero then the switch condition will be very large and will hit the
5568d88c1a5aSDimitry Andric // default case.
5569d88c1a5aSDimitry Andric
5570d88c1a5aSDimitry Andric auto *Ty = cast<IntegerType>(SI->getCondition()->getType());
5571d88c1a5aSDimitry Andric Builder.SetInsertPoint(SI);
5572d88c1a5aSDimitry Andric auto *ShiftC = ConstantInt::get(Ty, Shift);
5573d88c1a5aSDimitry Andric auto *Sub = Builder.CreateSub(SI->getCondition(), ConstantInt::get(Ty, Base));
5574d88c1a5aSDimitry Andric auto *LShr = Builder.CreateLShr(Sub, ShiftC);
5575d88c1a5aSDimitry Andric auto *Shl = Builder.CreateShl(Sub, Ty->getBitWidth() - Shift);
5576d88c1a5aSDimitry Andric auto *Rot = Builder.CreateOr(LShr, Shl);
5577d88c1a5aSDimitry Andric SI->replaceUsesOfWith(SI->getCondition(), Rot);
5578d88c1a5aSDimitry Andric
55797a7e6055SDimitry Andric for (auto Case : SI->cases()) {
55807a7e6055SDimitry Andric auto *Orig = Case.getCaseValue();
5581d88c1a5aSDimitry Andric auto Sub = Orig->getValue() - APInt(Ty->getBitWidth(), Base);
55827a7e6055SDimitry Andric Case.setValue(
5583d88c1a5aSDimitry Andric cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue()))));
5584d88c1a5aSDimitry Andric }
5585d88c1a5aSDimitry Andric return true;
5586d88c1a5aSDimitry Andric }
5587d88c1a5aSDimitry Andric
SimplifySwitch(SwitchInst * SI,IRBuilder<> & Builder)55883861d79fSDimitry Andric bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
55892754fe60SDimitry Andric BasicBlock *BB = SI->getParent();
55902754fe60SDimitry Andric
55913861d79fSDimitry Andric if (isValueEqualityComparison(SI)) {
55922754fe60SDimitry Andric // If we only have one predecessor, and if it is a branch on this value,
55932754fe60SDimitry Andric // see if that predecessor totally determines the outcome of this switch.
55942754fe60SDimitry Andric if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
5595bd5abe19SDimitry Andric if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
5596*b5893f02SDimitry Andric return requestResimplify();
55972754fe60SDimitry Andric
55983b0f4066SDimitry Andric Value *Cond = SI->getCondition();
55993b0f4066SDimitry Andric if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
56003b0f4066SDimitry Andric if (SimplifySwitchOnSelect(SI, Select))
5601*b5893f02SDimitry Andric return requestResimplify();
56023b0f4066SDimitry Andric
56032754fe60SDimitry Andric // If the block only contains the switch, see if we can fold the block
56042754fe60SDimitry Andric // away into any preds.
56054ba319b5SDimitry Andric if (SI == &*BB->instructionsWithoutDebug().begin())
5606bd5abe19SDimitry Andric if (FoldValueComparisonIntoPredecessors(SI, Builder))
5607*b5893f02SDimitry Andric return requestResimplify();
56083861d79fSDimitry Andric }
56092754fe60SDimitry Andric
56102754fe60SDimitry Andric // Try to transform the switch into an icmp and a branch.
5611bd5abe19SDimitry Andric if (TurnSwitchRangeIntoICmp(SI, Builder))
5612*b5893f02SDimitry Andric return requestResimplify();
5613bd5abe19SDimitry Andric
5614bd5abe19SDimitry Andric // Remove unreachable cases.
56152cab237bSDimitry Andric if (eliminateDeadSwitchCases(SI, Options.AC, DL))
5616*b5893f02SDimitry Andric return requestResimplify();
561739d628a0SDimitry Andric
56182cab237bSDimitry Andric if (switchToSelect(SI, Builder, DL, TTI))
5619*b5893f02SDimitry Andric return requestResimplify();
56202754fe60SDimitry Andric
56212cab237bSDimitry Andric if (Options.ForwardSwitchCondToPhi && ForwardSwitchConditionToPHI(SI))
5622*b5893f02SDimitry Andric return requestResimplify();
562317a519f9SDimitry Andric
56242cab237bSDimitry Andric // The conversion from switch to lookup tables results in difficult-to-analyze
56252cab237bSDimitry Andric // code and makes pruning branches much harder. This is a problem if the
56262cab237bSDimitry Andric // switch expression itself can still be restricted as a result of inlining or
56272cab237bSDimitry Andric // CVP. Therefore, only apply this transformation during late stages of the
56282cab237bSDimitry Andric // optimisation pipeline.
56292cab237bSDimitry Andric if (Options.ConvertSwitchToLookupTable &&
56302cab237bSDimitry Andric SwitchToLookupTable(SI, Builder, DL, TTI))
5631*b5893f02SDimitry Andric return requestResimplify();
56323861d79fSDimitry Andric
5633d88c1a5aSDimitry Andric if (ReduceSwitchRange(SI, Builder, DL, TTI))
5634*b5893f02SDimitry Andric return requestResimplify();
5635d88c1a5aSDimitry Andric
56362754fe60SDimitry Andric return false;
56372754fe60SDimitry Andric }
56382754fe60SDimitry Andric
SimplifyIndirectBr(IndirectBrInst * IBI)56392754fe60SDimitry Andric bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) {
56402754fe60SDimitry Andric BasicBlock *BB = IBI->getParent();
56412754fe60SDimitry Andric bool Changed = false;
56422754fe60SDimitry Andric
5643e580952dSDimitry Andric // Eliminate redundant destinations.
5644e580952dSDimitry Andric SmallPtrSet<Value *, 8> Succs;
5645e580952dSDimitry Andric for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
5646e580952dSDimitry Andric BasicBlock *Dest = IBI->getDestination(i);
564739d628a0SDimitry Andric if (!Dest->hasAddressTaken() || !Succs.insert(Dest).second) {
5648e580952dSDimitry Andric Dest->removePredecessor(BB);
5649e580952dSDimitry Andric IBI->removeDestination(i);
56503ca95b02SDimitry Andric --i;
56513ca95b02SDimitry Andric --e;
5652e580952dSDimitry Andric Changed = true;
5653e580952dSDimitry Andric }
5654e580952dSDimitry Andric }
5655e580952dSDimitry Andric
5656e580952dSDimitry Andric if (IBI->getNumDestinations() == 0) {
5657e580952dSDimitry Andric // If the indirectbr has no successors, change it to unreachable.
5658e580952dSDimitry Andric new UnreachableInst(IBI->getContext(), IBI);
5659*b5893f02SDimitry Andric EraseTerminatorAndDCECond(IBI);
56602754fe60SDimitry Andric return true;
56612754fe60SDimitry Andric }
56622754fe60SDimitry Andric
56632754fe60SDimitry Andric if (IBI->getNumDestinations() == 1) {
5664e580952dSDimitry Andric // If the indirectbr has one successor, change it to a direct branch.
5665e580952dSDimitry Andric BranchInst::Create(IBI->getDestination(0), IBI);
5666*b5893f02SDimitry Andric EraseTerminatorAndDCECond(IBI);
56672754fe60SDimitry Andric return true;
56682754fe60SDimitry Andric }
56692754fe60SDimitry Andric
56702754fe60SDimitry Andric if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
56712754fe60SDimitry Andric if (SimplifyIndirectBrOnSelect(IBI, SI))
5672*b5893f02SDimitry Andric return requestResimplify();
56732754fe60SDimitry Andric }
56742754fe60SDimitry Andric return Changed;
56752754fe60SDimitry Andric }
56762754fe60SDimitry Andric
5677ff0cc061SDimitry Andric /// Given an block with only a single landing pad and a unconditional branch
5678ff0cc061SDimitry Andric /// try to find another basic block which this one can be merged with. This
5679ff0cc061SDimitry Andric /// handles cases where we have multiple invokes with unique landing pads, but
5680ff0cc061SDimitry Andric /// a shared handler.
5681ff0cc061SDimitry Andric ///
5682ff0cc061SDimitry Andric /// We specifically choose to not worry about merging non-empty blocks
5683ff0cc061SDimitry Andric /// here. That is a PRE/scheduling problem and is best solved elsewhere. In
5684ff0cc061SDimitry Andric /// practice, the optimizer produces empty landing pad blocks quite frequently
5685ff0cc061SDimitry Andric /// when dealing with exception dense code. (see: instcombine, gvn, if-else
5686ff0cc061SDimitry Andric /// sinking in this file)
5687ff0cc061SDimitry Andric ///
5688ff0cc061SDimitry Andric /// This is primarily a code size optimization. We need to avoid performing
5689ff0cc061SDimitry Andric /// any transform which might inhibit optimization (such as our ability to
5690ff0cc061SDimitry Andric /// specialize a particular handler via tail commoning). We do this by not
5691ff0cc061SDimitry Andric /// merging any blocks which require us to introduce a phi. Since the same
56924ba319b5SDimitry Andric /// values are flowing through both blocks, we don't lose any ability to
5693ff0cc061SDimitry Andric /// specialize. If anything, we make such specialization more likely.
5694ff0cc061SDimitry Andric ///
5695ff0cc061SDimitry Andric /// TODO - This transformation could remove entries from a phi in the target
5696ff0cc061SDimitry Andric /// block when the inputs in the phi are the same for the two blocks being
5697ff0cc061SDimitry Andric /// merged. In some cases, this could result in removal of the PHI entirely.
TryToMergeLandingPad(LandingPadInst * LPad,BranchInst * BI,BasicBlock * BB)5698ff0cc061SDimitry Andric static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI,
5699ff0cc061SDimitry Andric BasicBlock *BB) {
5700ff0cc061SDimitry Andric auto Succ = BB->getUniqueSuccessor();
5701ff0cc061SDimitry Andric assert(Succ);
5702ff0cc061SDimitry Andric // If there's a phi in the successor block, we'd likely have to introduce
5703ff0cc061SDimitry Andric // a phi into the merged landing pad block.
5704ff0cc061SDimitry Andric if (isa<PHINode>(*Succ->begin()))
5705ff0cc061SDimitry Andric return false;
5706ff0cc061SDimitry Andric
5707ff0cc061SDimitry Andric for (BasicBlock *OtherPred : predecessors(Succ)) {
5708ff0cc061SDimitry Andric if (BB == OtherPred)
5709ff0cc061SDimitry Andric continue;
5710ff0cc061SDimitry Andric BasicBlock::iterator I = OtherPred->begin();
5711ff0cc061SDimitry Andric LandingPadInst *LPad2 = dyn_cast<LandingPadInst>(I);
5712ff0cc061SDimitry Andric if (!LPad2 || !LPad2->isIdenticalTo(LPad))
5713ff0cc061SDimitry Andric continue;
57142cab237bSDimitry Andric for (++I; isa<DbgInfoIntrinsic>(I); ++I)
57152cab237bSDimitry Andric ;
5716ff0cc061SDimitry Andric BranchInst *BI2 = dyn_cast<BranchInst>(I);
5717ff0cc061SDimitry Andric if (!BI2 || !BI2->isIdenticalTo(BI))
5718ff0cc061SDimitry Andric continue;
5719ff0cc061SDimitry Andric
57203ca95b02SDimitry Andric // We've found an identical block. Update our predecessors to take that
5721ff0cc061SDimitry Andric // path instead and make ourselves dead.
57224ba319b5SDimitry Andric SmallPtrSet<BasicBlock *, 16> Preds;
5723ff0cc061SDimitry Andric Preds.insert(pred_begin(BB), pred_end(BB));
5724ff0cc061SDimitry Andric for (BasicBlock *Pred : Preds) {
5725ff0cc061SDimitry Andric InvokeInst *II = cast<InvokeInst>(Pred->getTerminator());
57263ca95b02SDimitry Andric assert(II->getNormalDest() != BB && II->getUnwindDest() == BB &&
57273ca95b02SDimitry Andric "unexpected successor");
5728ff0cc061SDimitry Andric II->setUnwindDest(OtherPred);
5729ff0cc061SDimitry Andric }
5730ff0cc061SDimitry Andric
5731ff0cc061SDimitry Andric // The debug info in OtherPred doesn't cover the merged control flow that
5732ff0cc061SDimitry Andric // used to go through BB. We need to delete it or update it.
57333ca95b02SDimitry Andric for (auto I = OtherPred->begin(), E = OtherPred->end(); I != E;) {
57343ca95b02SDimitry Andric Instruction &Inst = *I;
57353ca95b02SDimitry Andric I++;
5736ff0cc061SDimitry Andric if (isa<DbgInfoIntrinsic>(Inst))
5737ff0cc061SDimitry Andric Inst.eraseFromParent();
5738ff0cc061SDimitry Andric }
5739ff0cc061SDimitry Andric
57404ba319b5SDimitry Andric SmallPtrSet<BasicBlock *, 16> Succs;
5741ff0cc061SDimitry Andric Succs.insert(succ_begin(BB), succ_end(BB));
5742ff0cc061SDimitry Andric for (BasicBlock *Succ : Succs) {
5743ff0cc061SDimitry Andric Succ->removePredecessor(BB);
5744ff0cc061SDimitry Andric }
5745ff0cc061SDimitry Andric
5746ff0cc061SDimitry Andric IRBuilder<> Builder(BI);
5747ff0cc061SDimitry Andric Builder.CreateUnreachable();
5748ff0cc061SDimitry Andric BI->eraseFromParent();
5749ff0cc061SDimitry Andric return true;
5750ff0cc061SDimitry Andric }
5751ff0cc061SDimitry Andric return false;
5752ff0cc061SDimitry Andric }
5753ff0cc061SDimitry Andric
SimplifyUncondBranch(BranchInst * BI,IRBuilder<> & Builder)57543ca95b02SDimitry Andric bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI,
57553ca95b02SDimitry Andric IRBuilder<> &Builder) {
57562754fe60SDimitry Andric BasicBlock *BB = BI->getParent();
575737cd60a3SDimitry Andric BasicBlock *Succ = BI->getSuccessor(0);
57582754fe60SDimitry Andric
57592754fe60SDimitry Andric // If the Terminator is the only non-phi instruction, simplify the block.
57602cab237bSDimitry Andric // If LoopHeader is provided, check if the block or its successor is a loop
57612cab237bSDimitry Andric // header. (This is for early invocations before loop simplify and
576237cd60a3SDimitry Andric // vectorization to keep canonical loop forms for nested loops. These blocks
576337cd60a3SDimitry Andric // can be eliminated when the pass is invoked later in the back-end.)
57644ba319b5SDimitry Andric // Note that if BB has only one predecessor then we do not introduce new
57654ba319b5SDimitry Andric // backedge, so we can eliminate BB.
576637cd60a3SDimitry Andric bool NeedCanonicalLoop =
57672cab237bSDimitry Andric Options.NeedCanonicalLoop &&
5768*b5893f02SDimitry Andric (LoopHeaders && BB->hasNPredecessorsOrMore(2) &&
57694ba319b5SDimitry Andric (LoopHeaders->count(BB) || LoopHeaders->count(Succ)));
57707d523365SDimitry Andric BasicBlock::iterator I = BB->getFirstNonPHIOrDbg()->getIterator();
57712754fe60SDimitry Andric if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
577237cd60a3SDimitry Andric !NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB))
57732754fe60SDimitry Andric return true;
57742754fe60SDimitry Andric
57752cab237bSDimitry Andric // If the only instruction in the block is a seteq/setne comparison against a
57762cab237bSDimitry Andric // constant, try to simplify the block.
57772754fe60SDimitry Andric if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
57782754fe60SDimitry Andric if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
57792754fe60SDimitry Andric for (++I; isa<DbgInfoIntrinsic>(I); ++I)
57802754fe60SDimitry Andric ;
5781dff0c46cSDimitry Andric if (I->isTerminator() &&
5782*b5893f02SDimitry Andric tryToSimplifyUncondBranchWithICmpInIt(ICI, Builder))
5783ff0cc061SDimitry Andric return true;
5784ff0cc061SDimitry Andric }
5785ff0cc061SDimitry Andric
5786ff0cc061SDimitry Andric // See if we can merge an empty landing pad block with another which is
5787ff0cc061SDimitry Andric // equivalent.
5788ff0cc061SDimitry Andric if (LandingPadInst *LPad = dyn_cast<LandingPadInst>(I)) {
57892cab237bSDimitry Andric for (++I; isa<DbgInfoIntrinsic>(I); ++I)
57902cab237bSDimitry Andric ;
57913ca95b02SDimitry Andric if (I->isTerminator() && TryToMergeLandingPad(LPad, BI, BB))
57922754fe60SDimitry Andric return true;
57932754fe60SDimitry Andric }
57942754fe60SDimitry Andric
57957ae0e2c9SDimitry Andric // If this basic block is ONLY a compare and a branch, and if a predecessor
57967ae0e2c9SDimitry Andric // branches to us and our successor, fold the comparison into the
57977ae0e2c9SDimitry Andric // predecessor and use logical operations to update the incoming value
57987ae0e2c9SDimitry Andric // for PHI nodes in common successor.
57992cab237bSDimitry Andric if (FoldBranchToCommonDest(BI, Options.BonusInstThreshold))
5800*b5893f02SDimitry Andric return requestResimplify();
58012754fe60SDimitry Andric return false;
58022754fe60SDimitry Andric }
58032754fe60SDimitry Andric
allPredecessorsComeFromSameSource(BasicBlock * BB)58047d523365SDimitry Andric static BasicBlock *allPredecessorsComeFromSameSource(BasicBlock *BB) {
58057d523365SDimitry Andric BasicBlock *PredPred = nullptr;
58067d523365SDimitry Andric for (auto *P : predecessors(BB)) {
58077d523365SDimitry Andric BasicBlock *PPred = P->getSinglePredecessor();
58087d523365SDimitry Andric if (!PPred || (PredPred && PredPred != PPred))
58097d523365SDimitry Andric return nullptr;
58107d523365SDimitry Andric PredPred = PPred;
58117d523365SDimitry Andric }
58127d523365SDimitry Andric return PredPred;
58137d523365SDimitry Andric }
58142754fe60SDimitry Andric
SimplifyCondBranch(BranchInst * BI,IRBuilder<> & Builder)5815bd5abe19SDimitry Andric bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
58162754fe60SDimitry Andric BasicBlock *BB = BI->getParent();
58174ba319b5SDimitry Andric const Function *Fn = BB->getParent();
58184ba319b5SDimitry Andric if (Fn && Fn->hasFnAttribute(Attribute::OptForFuzzing))
58194ba319b5SDimitry Andric return false;
58202754fe60SDimitry Andric
58212754fe60SDimitry Andric // Conditional branch
58222754fe60SDimitry Andric if (isValueEqualityComparison(BI)) {
58232754fe60SDimitry Andric // If we only have one predecessor, and if it is a branch on this value,
58242754fe60SDimitry Andric // see if that predecessor totally determines the outcome of this
58252754fe60SDimitry Andric // switch.
58262754fe60SDimitry Andric if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
5827bd5abe19SDimitry Andric if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
5828*b5893f02SDimitry Andric return requestResimplify();
58292754fe60SDimitry Andric
58302754fe60SDimitry Andric // This block must be empty, except for the setcond inst, if it exists.
58312754fe60SDimitry Andric // Ignore dbg intrinsics.
58324ba319b5SDimitry Andric auto I = BB->instructionsWithoutDebug().begin();
58332754fe60SDimitry Andric if (&*I == BI) {
5834bd5abe19SDimitry Andric if (FoldValueComparisonIntoPredecessors(BI, Builder))
5835*b5893f02SDimitry Andric return requestResimplify();
58362754fe60SDimitry Andric } else if (&*I == cast<Instruction>(BI->getCondition())) {
58372754fe60SDimitry Andric ++I;
5838bd5abe19SDimitry Andric if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
5839*b5893f02SDimitry Andric return requestResimplify();
5840e580952dSDimitry Andric }
5841f22ef01cSRoman Divacky }
5842f22ef01cSRoman Divacky
58432754fe60SDimitry Andric // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
5844ff0cc061SDimitry Andric if (SimplifyBranchOnICmpChain(BI, Builder, DL))
58452754fe60SDimitry Andric return true;
58462754fe60SDimitry Andric
5847*b5893f02SDimitry Andric // If this basic block has dominating predecessor blocks and the dominating
5848*b5893f02SDimitry Andric // blocks' conditions imply BI's condition, we know the direction of BI.
5849*b5893f02SDimitry Andric Optional<bool> Imp = isImpliedByDomCondition(BI->getCondition(), BI, DL);
5850*b5893f02SDimitry Andric if (Imp) {
58513ca95b02SDimitry Andric // Turn this into a branch on constant.
58523ca95b02SDimitry Andric auto *OldCond = BI->getCondition();
5853*b5893f02SDimitry Andric ConstantInt *TorF = *Imp ? ConstantInt::getTrue(BB->getContext())
58543ca95b02SDimitry Andric : ConstantInt::getFalse(BB->getContext());
5855*b5893f02SDimitry Andric BI->setCondition(TorF);
58563ca95b02SDimitry Andric RecursivelyDeleteTriviallyDeadInstructions(OldCond);
5857*b5893f02SDimitry Andric return requestResimplify();
58583ca95b02SDimitry Andric }
58593ca95b02SDimitry Andric
5860dff0c46cSDimitry Andric // If this basic block is ONLY a compare and a branch, and if a predecessor
5861dff0c46cSDimitry Andric // branches to us and one of our successors, fold the comparison into the
5862dff0c46cSDimitry Andric // predecessor and use logical operations to pick the right destination.
58632cab237bSDimitry Andric if (FoldBranchToCommonDest(BI, Options.BonusInstThreshold))
5864*b5893f02SDimitry Andric return requestResimplify();
5865dff0c46cSDimitry Andric
58662754fe60SDimitry Andric // We have a conditional branch to two blocks that are only reachable
58672754fe60SDimitry Andric // from BI. We know that the condbr dominates the two blocks, so see if
58682754fe60SDimitry Andric // there is any identical code in the "then" and "else" blocks. If so, we
58692754fe60SDimitry Andric // can hoist it up to the branching block.
587091bc56edSDimitry Andric if (BI->getSuccessor(0)->getSinglePredecessor()) {
587191bc56edSDimitry Andric if (BI->getSuccessor(1)->getSinglePredecessor()) {
5872ff0cc061SDimitry Andric if (HoistThenElseCodeToIf(BI, TTI))
5873*b5893f02SDimitry Andric return requestResimplify();
58742754fe60SDimitry Andric } else {
58752754fe60SDimitry Andric // If Successor #1 has multiple preds, we may be able to conditionally
587691bc56edSDimitry Andric // execute Successor #0 if it branches to Successor #1.
5877*b5893f02SDimitry Andric Instruction *Succ0TI = BI->getSuccessor(0)->getTerminator();
58782754fe60SDimitry Andric if (Succ0TI->getNumSuccessors() == 1 &&
58792754fe60SDimitry Andric Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
5880ff0cc061SDimitry Andric if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0), TTI))
5881*b5893f02SDimitry Andric return requestResimplify();
58822754fe60SDimitry Andric }
588391bc56edSDimitry Andric } else if (BI->getSuccessor(1)->getSinglePredecessor()) {
58842754fe60SDimitry Andric // If Successor #0 has multiple preds, we may be able to conditionally
588591bc56edSDimitry Andric // execute Successor #1 if it branches to Successor #0.
5886*b5893f02SDimitry Andric Instruction *Succ1TI = BI->getSuccessor(1)->getTerminator();
58872754fe60SDimitry Andric if (Succ1TI->getNumSuccessors() == 1 &&
58882754fe60SDimitry Andric Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
5889ff0cc061SDimitry Andric if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1), TTI))
5890*b5893f02SDimitry Andric return requestResimplify();
58912754fe60SDimitry Andric }
58922754fe60SDimitry Andric
58932754fe60SDimitry Andric // If this is a branch on a phi node in the current block, thread control
58942754fe60SDimitry Andric // through this block if any PHI node entries are constants.
58952754fe60SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
58962754fe60SDimitry Andric if (PN->getParent() == BI->getParent())
58972cab237bSDimitry Andric if (FoldCondBranchOnPHI(BI, DL, Options.AC))
5898*b5893f02SDimitry Andric return requestResimplify();
58992754fe60SDimitry Andric
59002754fe60SDimitry Andric // Scan predecessor blocks for conditional branches.
59012754fe60SDimitry Andric for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
59022754fe60SDimitry Andric if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
59032754fe60SDimitry Andric if (PBI != BI && PBI->isConditional())
59047d523365SDimitry Andric if (SimplifyCondBranchToCondBranch(PBI, BI, DL))
5905*b5893f02SDimitry Andric return requestResimplify();
59067d523365SDimitry Andric
59077d523365SDimitry Andric // Look for diamond patterns.
59087d523365SDimitry Andric if (MergeCondStores)
59097d523365SDimitry Andric if (BasicBlock *PrevBB = allPredecessorsComeFromSameSource(BB))
59107d523365SDimitry Andric if (BranchInst *PBI = dyn_cast<BranchInst>(PrevBB->getTerminator()))
59117d523365SDimitry Andric if (PBI != BI && PBI->isConditional())
59122cab237bSDimitry Andric if (mergeConditionalStores(PBI, BI, DL))
5913*b5893f02SDimitry Andric return requestResimplify();
59142754fe60SDimitry Andric
59152754fe60SDimitry Andric return false;
59162754fe60SDimitry Andric }
59172754fe60SDimitry Andric
59186122f3e6SDimitry Andric /// Check if passing a value to an instruction will cause undefined behavior.
passingValueIsAlwaysUndefined(Value * V,Instruction * I)59196122f3e6SDimitry Andric static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
59206122f3e6SDimitry Andric Constant *C = dyn_cast<Constant>(V);
59216122f3e6SDimitry Andric if (!C)
59226122f3e6SDimitry Andric return false;
59236122f3e6SDimitry Andric
59243861d79fSDimitry Andric if (I->use_empty())
59256122f3e6SDimitry Andric return false;
59266122f3e6SDimitry Andric
59273ca95b02SDimitry Andric if (C->isNullValue() || isa<UndefValue>(C)) {
59283861d79fSDimitry Andric // Only look at the first use, avoid hurting compile time with long uselists
592991bc56edSDimitry Andric User *Use = *I->user_begin();
59306122f3e6SDimitry Andric
59316122f3e6SDimitry Andric // Now make sure that there are no instructions in between that can alter
59326122f3e6SDimitry Andric // control flow (eg. calls)
5933d88c1a5aSDimitry Andric for (BasicBlock::iterator
5934d88c1a5aSDimitry Andric i = ++BasicBlock::iterator(I),
5935d88c1a5aSDimitry Andric UI = BasicBlock::iterator(dyn_cast<Instruction>(Use));
5936d88c1a5aSDimitry Andric i != UI; ++i)
59376122f3e6SDimitry Andric if (i == I->getParent()->end() || i->mayHaveSideEffects())
59386122f3e6SDimitry Andric return false;
59396122f3e6SDimitry Andric
59406122f3e6SDimitry Andric // Look through GEPs. A load from a GEP derived from NULL is still undefined
59416122f3e6SDimitry Andric if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
59426122f3e6SDimitry Andric if (GEP->getPointerOperand() == I)
59436122f3e6SDimitry Andric return passingValueIsAlwaysUndefined(V, GEP);
59446122f3e6SDimitry Andric
59456122f3e6SDimitry Andric // Look through bitcasts.
59466122f3e6SDimitry Andric if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
59476122f3e6SDimitry Andric return passingValueIsAlwaysUndefined(V, BC);
59486122f3e6SDimitry Andric
59496122f3e6SDimitry Andric // Load from null is undefined.
59506122f3e6SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(Use))
5951139f7f9bSDimitry Andric if (!LI->isVolatile())
59524ba319b5SDimitry Andric return !NullPointerIsDefined(LI->getFunction(),
59534ba319b5SDimitry Andric LI->getPointerAddressSpace());
59546122f3e6SDimitry Andric
59556122f3e6SDimitry Andric // Store to null is undefined.
59566122f3e6SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(Use))
5957139f7f9bSDimitry Andric if (!SI->isVolatile())
59584ba319b5SDimitry Andric return (!NullPointerIsDefined(SI->getFunction(),
59594ba319b5SDimitry Andric SI->getPointerAddressSpace())) &&
59603ca95b02SDimitry Andric SI->getPointerOperand() == I;
59613ca95b02SDimitry Andric
59623ca95b02SDimitry Andric // A call to null is undefined.
59633ca95b02SDimitry Andric if (auto CS = CallSite(Use))
59644ba319b5SDimitry Andric return !NullPointerIsDefined(CS->getFunction()) &&
59654ba319b5SDimitry Andric CS.getCalledValue() == I;
59666122f3e6SDimitry Andric }
59676122f3e6SDimitry Andric return false;
59686122f3e6SDimitry Andric }
59696122f3e6SDimitry Andric
59706122f3e6SDimitry Andric /// If BB has an incoming value that will always trigger undefined behavior
5971dff0c46cSDimitry Andric /// (eg. null pointer dereference), remove the branch leading here.
removeUndefIntroducingPredecessor(BasicBlock * BB)59726122f3e6SDimitry Andric static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
597330785c0eSDimitry Andric for (PHINode &PHI : BB->phis())
597430785c0eSDimitry Andric for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i)
597530785c0eSDimitry Andric if (passingValueIsAlwaysUndefined(PHI.getIncomingValue(i), &PHI)) {
5976*b5893f02SDimitry Andric Instruction *T = PHI.getIncomingBlock(i)->getTerminator();
59776122f3e6SDimitry Andric IRBuilder<> Builder(T);
59786122f3e6SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
597930785c0eSDimitry Andric BB->removePredecessor(PHI.getIncomingBlock(i));
59806122f3e6SDimitry Andric // Turn uncoditional branches into unreachables and remove the dead
59816122f3e6SDimitry Andric // destination from conditional branches.
59826122f3e6SDimitry Andric if (BI->isUnconditional())
59836122f3e6SDimitry Andric Builder.CreateUnreachable();
59846122f3e6SDimitry Andric else
59853ca95b02SDimitry Andric Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1)
59863ca95b02SDimitry Andric : BI->getSuccessor(0));
59876122f3e6SDimitry Andric BI->eraseFromParent();
59886122f3e6SDimitry Andric return true;
59896122f3e6SDimitry Andric }
59906122f3e6SDimitry Andric // TODO: SwitchInst.
59916122f3e6SDimitry Andric }
59926122f3e6SDimitry Andric
59936122f3e6SDimitry Andric return false;
59946122f3e6SDimitry Andric }
59956122f3e6SDimitry Andric
simplifyOnce(BasicBlock * BB)5996*b5893f02SDimitry Andric bool SimplifyCFGOpt::simplifyOnce(BasicBlock *BB) {
59972754fe60SDimitry Andric bool Changed = false;
59982754fe60SDimitry Andric
59992754fe60SDimitry Andric assert(BB && BB->getParent() && "Block not embedded in function!");
60002754fe60SDimitry Andric assert(BB->getTerminator() && "Degenerate basic block encountered!");
60012754fe60SDimitry Andric
60022754fe60SDimitry Andric // Remove basic blocks that have no predecessors (except the entry block)...
60032754fe60SDimitry Andric // or that just have themself as a predecessor. These are unreachable.
60043ca95b02SDimitry Andric if ((pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) ||
60052754fe60SDimitry Andric BB->getSinglePredecessor() == BB) {
60064ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing BB: \n" << *BB);
60072754fe60SDimitry Andric DeleteDeadBlock(BB);
60082754fe60SDimitry Andric return true;
60092754fe60SDimitry Andric }
60102754fe60SDimitry Andric
60112754fe60SDimitry Andric // Check to see if we can constant propagate this terminator instruction
60122754fe60SDimitry Andric // away...
6013bd5abe19SDimitry Andric Changed |= ConstantFoldTerminator(BB, true);
60142754fe60SDimitry Andric
60152754fe60SDimitry Andric // Check for and eliminate duplicate PHI nodes in this block.
60162754fe60SDimitry Andric Changed |= EliminateDuplicatePHINodes(BB);
60172754fe60SDimitry Andric
60186122f3e6SDimitry Andric // Check for and remove branches that will always cause undefined behavior.
60196122f3e6SDimitry Andric Changed |= removeUndefIntroducingPredecessor(BB);
60206122f3e6SDimitry Andric
6021f22ef01cSRoman Divacky // Merge basic blocks into their predecessor if there is only one distinct
6022f22ef01cSRoman Divacky // pred, and if there is only one distinct successor of the predecessor, and
6023f22ef01cSRoman Divacky // if there are no PHI nodes.
6024f22ef01cSRoman Divacky if (MergeBlockIntoPredecessor(BB))
6025f22ef01cSRoman Divacky return true;
6026f22ef01cSRoman Divacky
6027da09e106SDimitry Andric if (SinkCommon && Options.SinkCommonInsts)
6028da09e106SDimitry Andric Changed |= SinkCommonCodeFromPredecessors(BB);
6029da09e106SDimitry Andric
6030bd5abe19SDimitry Andric IRBuilder<> Builder(BB);
6031bd5abe19SDimitry Andric
60322754fe60SDimitry Andric // If there is a trivial two-entry PHI node in this basic block, and we can
60332754fe60SDimitry Andric // eliminate it, do so now.
60342cab237bSDimitry Andric if (auto *PN = dyn_cast<PHINode>(BB->begin()))
60352754fe60SDimitry Andric if (PN->getNumIncomingValues() == 2)
6036ff0cc061SDimitry Andric Changed |= FoldTwoEntryPHINode(PN, TTI, DL);
6037f22ef01cSRoman Divacky
6038bd5abe19SDimitry Andric Builder.SetInsertPoint(BB->getTerminator());
60392cab237bSDimitry Andric if (auto *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
60402754fe60SDimitry Andric if (BI->isUnconditional()) {
60413ca95b02SDimitry Andric if (SimplifyUncondBranch(BI, Builder))
60423ca95b02SDimitry Andric return true;
6043f22ef01cSRoman Divacky } else {
60443ca95b02SDimitry Andric if (SimplifyCondBranch(BI, Builder))
60453ca95b02SDimitry Andric return true;
6046f22ef01cSRoman Divacky }
60472cab237bSDimitry Andric } else if (auto *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
60483ca95b02SDimitry Andric if (SimplifyReturn(RI, Builder))
60493ca95b02SDimitry Andric return true;
60502cab237bSDimitry Andric } else if (auto *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
60513ca95b02SDimitry Andric if (SimplifyResume(RI, Builder))
60523ca95b02SDimitry Andric return true;
60532cab237bSDimitry Andric } else if (auto *RI = dyn_cast<CleanupReturnInst>(BB->getTerminator())) {
60543ca95b02SDimitry Andric if (SimplifyCleanupReturn(RI))
60553ca95b02SDimitry Andric return true;
60562cab237bSDimitry Andric } else if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
60573ca95b02SDimitry Andric if (SimplifySwitch(SI, Builder))
60583ca95b02SDimitry Andric return true;
60592cab237bSDimitry Andric } else if (auto *UI = dyn_cast<UnreachableInst>(BB->getTerminator())) {
60603ca95b02SDimitry Andric if (SimplifyUnreachable(UI))
60613ca95b02SDimitry Andric return true;
60622cab237bSDimitry Andric } else if (auto *IBI = dyn_cast<IndirectBrInst>(BB->getTerminator())) {
60633ca95b02SDimitry Andric if (SimplifyIndirectBr(IBI))
60643ca95b02SDimitry Andric return true;
6065f22ef01cSRoman Divacky }
6066f22ef01cSRoman Divacky
6067f22ef01cSRoman Divacky return Changed;
6068f22ef01cSRoman Divacky }
6069f22ef01cSRoman Divacky
run(BasicBlock * BB)6070*b5893f02SDimitry Andric bool SimplifyCFGOpt::run(BasicBlock *BB) {
6071*b5893f02SDimitry Andric bool Changed = false;
6072*b5893f02SDimitry Andric
6073*b5893f02SDimitry Andric // Repeated simplify BB as long as resimplification is requested.
6074*b5893f02SDimitry Andric do {
6075*b5893f02SDimitry Andric Resimplify = false;
6076*b5893f02SDimitry Andric
6077*b5893f02SDimitry Andric // Perform one round of simplifcation. Resimplify flag will be set if
6078*b5893f02SDimitry Andric // another iteration is requested.
6079*b5893f02SDimitry Andric Changed |= simplifyOnce(BB);
6080*b5893f02SDimitry Andric } while (Resimplify);
6081*b5893f02SDimitry Andric
6082*b5893f02SDimitry Andric return Changed;
6083*b5893f02SDimitry Andric }
6084*b5893f02SDimitry Andric
simplifyCFG(BasicBlock * BB,const TargetTransformInfo & TTI,const SimplifyCFGOptions & Options,SmallPtrSetImpl<BasicBlock * > * LoopHeaders)60852cab237bSDimitry Andric bool llvm::simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
60862cab237bSDimitry Andric const SimplifyCFGOptions &Options,
60872cab237bSDimitry Andric SmallPtrSetImpl<BasicBlock *> *LoopHeaders) {
60882cab237bSDimitry Andric return SimplifyCFGOpt(TTI, BB->getModule()->getDataLayout(), LoopHeaders,
60892cab237bSDimitry Andric Options)
60903ca95b02SDimitry Andric .run(BB);
6091f22ef01cSRoman Divacky }
6092