1*9880d681SAndroid Build Coastguard Worker //===- SpeculativeExecution.cpp ---------------------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass hoists instructions to enable speculative execution on
11*9880d681SAndroid Build Coastguard Worker // targets where branches are expensive. This is aimed at GPUs. It
12*9880d681SAndroid Build Coastguard Worker // currently works on simple if-then and if-then-else
13*9880d681SAndroid Build Coastguard Worker // patterns.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // Removing branches is not the only motivation for this
16*9880d681SAndroid Build Coastguard Worker // pass. E.g. consider this code and assume that there is no
17*9880d681SAndroid Build Coastguard Worker // addressing mode for multiplying by sizeof(*a):
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // if (b > 0)
20*9880d681SAndroid Build Coastguard Worker // c = a[i + 1]
21*9880d681SAndroid Build Coastguard Worker // if (d > 0)
22*9880d681SAndroid Build Coastguard Worker // e = a[i + 2]
23*9880d681SAndroid Build Coastguard Worker //
24*9880d681SAndroid Build Coastguard Worker // turns into
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker // p = &a[i + 1];
27*9880d681SAndroid Build Coastguard Worker // if (b > 0)
28*9880d681SAndroid Build Coastguard Worker // c = *p;
29*9880d681SAndroid Build Coastguard Worker // q = &a[i + 2];
30*9880d681SAndroid Build Coastguard Worker // if (d > 0)
31*9880d681SAndroid Build Coastguard Worker // e = *q;
32*9880d681SAndroid Build Coastguard Worker //
33*9880d681SAndroid Build Coastguard Worker // which could later be optimized to
34*9880d681SAndroid Build Coastguard Worker //
35*9880d681SAndroid Build Coastguard Worker // r = &a[i];
36*9880d681SAndroid Build Coastguard Worker // if (b > 0)
37*9880d681SAndroid Build Coastguard Worker // c = r[1];
38*9880d681SAndroid Build Coastguard Worker // if (d > 0)
39*9880d681SAndroid Build Coastguard Worker // e = r[2];
40*9880d681SAndroid Build Coastguard Worker //
41*9880d681SAndroid Build Coastguard Worker // Later passes sink back much of the speculated code that did not enable
42*9880d681SAndroid Build Coastguard Worker // further optimization.
43*9880d681SAndroid Build Coastguard Worker //
44*9880d681SAndroid Build Coastguard Worker // This pass is more aggressive than the function SpeculativeyExecuteBB in
45*9880d681SAndroid Build Coastguard Worker // SimplifyCFG. SimplifyCFG will not speculate if no selects are introduced and
46*9880d681SAndroid Build Coastguard Worker // it will speculate at most one instruction. It also will not speculate if
47*9880d681SAndroid Build Coastguard Worker // there is a value defined in the if-block that is only used in the then-block.
48*9880d681SAndroid Build Coastguard Worker // These restrictions make sense since the speculation in SimplifyCFG seems
49*9880d681SAndroid Build Coastguard Worker // aimed at introducing cheap selects, while this pass is intended to do more
50*9880d681SAndroid Build Coastguard Worker // aggressive speculation while counting on later passes to either capitalize on
51*9880d681SAndroid Build Coastguard Worker // that or clean it up.
52*9880d681SAndroid Build Coastguard Worker //
53*9880d681SAndroid Build Coastguard Worker // If the pass was created by calling
54*9880d681SAndroid Build Coastguard Worker // createSpeculativeExecutionIfHasBranchDivergencePass or the
55*9880d681SAndroid Build Coastguard Worker // -spec-exec-only-if-divergent-target option is present, this pass only has an
56*9880d681SAndroid Build Coastguard Worker // effect on targets where TargetTransformInfo::hasBranchDivergence() is true;
57*9880d681SAndroid Build Coastguard Worker // on other targets, it is a nop.
58*9880d681SAndroid Build Coastguard Worker //
59*9880d681SAndroid Build Coastguard Worker // This lets you include this pass unconditionally in the IR pass pipeline, but
60*9880d681SAndroid Build Coastguard Worker // only enable it for relevant targets.
61*9880d681SAndroid Build Coastguard Worker //
62*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
65*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/GlobalsModRef.h"
66*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
67*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
68*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
69*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
70*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
71*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
72*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker using namespace llvm;
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "speculative-execution"
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker // The risk that speculation will not pay off increases with the
79*9880d681SAndroid Build Coastguard Worker // number of instructions speculated, so we put a limit on that.
80*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> SpecExecMaxSpeculationCost(
81*9880d681SAndroid Build Coastguard Worker "spec-exec-max-speculation-cost", cl::init(7), cl::Hidden,
82*9880d681SAndroid Build Coastguard Worker cl::desc("Speculative execution is not applied to basic blocks where "
83*9880d681SAndroid Build Coastguard Worker "the cost of the instructions to speculatively execute "
84*9880d681SAndroid Build Coastguard Worker "exceeds this limit."));
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker // Speculating just a few instructions from a larger block tends not
87*9880d681SAndroid Build Coastguard Worker // to be profitable and this limit prevents that. A reason for that is
88*9880d681SAndroid Build Coastguard Worker // that small basic blocks are more likely to be candidates for
89*9880d681SAndroid Build Coastguard Worker // further optimization.
90*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned> SpecExecMaxNotHoisted(
91*9880d681SAndroid Build Coastguard Worker "spec-exec-max-not-hoisted", cl::init(5), cl::Hidden,
92*9880d681SAndroid Build Coastguard Worker cl::desc("Speculative execution is not applied to basic blocks where the "
93*9880d681SAndroid Build Coastguard Worker "number of instructions that would not be speculatively executed "
94*9880d681SAndroid Build Coastguard Worker "exceeds this limit."));
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> SpecExecOnlyIfDivergentTarget(
97*9880d681SAndroid Build Coastguard Worker "spec-exec-only-if-divergent-target", cl::init(false), cl::Hidden,
98*9880d681SAndroid Build Coastguard Worker cl::desc("Speculative execution is applied only to targets with divergent "
99*9880d681SAndroid Build Coastguard Worker "branches, even if the pass was configured to apply only to all "
100*9880d681SAndroid Build Coastguard Worker "targets."));
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker namespace {
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker class SpeculativeExecution : public FunctionPass {
105*9880d681SAndroid Build Coastguard Worker public:
106*9880d681SAndroid Build Coastguard Worker static char ID;
SpeculativeExecution(bool OnlyIfDivergentTarget=false)107*9880d681SAndroid Build Coastguard Worker explicit SpeculativeExecution(bool OnlyIfDivergentTarget = false)
108*9880d681SAndroid Build Coastguard Worker : FunctionPass(ID),
109*9880d681SAndroid Build Coastguard Worker OnlyIfDivergentTarget(OnlyIfDivergentTarget ||
110*9880d681SAndroid Build Coastguard Worker SpecExecOnlyIfDivergentTarget) {}
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override;
113*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override;
114*9880d681SAndroid Build Coastguard Worker
getPassName() const115*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
116*9880d681SAndroid Build Coastguard Worker if (OnlyIfDivergentTarget)
117*9880d681SAndroid Build Coastguard Worker return "Speculatively execute instructions if target has divergent "
118*9880d681SAndroid Build Coastguard Worker "branches";
119*9880d681SAndroid Build Coastguard Worker return "Speculatively execute instructions";
120*9880d681SAndroid Build Coastguard Worker }
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker private:
123*9880d681SAndroid Build Coastguard Worker bool runOnBasicBlock(BasicBlock &B);
124*9880d681SAndroid Build Coastguard Worker bool considerHoistingFromTo(BasicBlock &FromBlock, BasicBlock &ToBlock);
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker // If true, this pass is a nop unless the target architecture has branch
127*9880d681SAndroid Build Coastguard Worker // divergence.
128*9880d681SAndroid Build Coastguard Worker const bool OnlyIfDivergentTarget;
129*9880d681SAndroid Build Coastguard Worker const TargetTransformInfo *TTI = nullptr;
130*9880d681SAndroid Build Coastguard Worker };
131*9880d681SAndroid Build Coastguard Worker } // namespace
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker char SpeculativeExecution::ID = 0;
134*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(SpeculativeExecution, "speculative-execution",
135*9880d681SAndroid Build Coastguard Worker "Speculatively execute instructions", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)136*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
137*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(SpeculativeExecution, "speculative-execution",
138*9880d681SAndroid Build Coastguard Worker "Speculatively execute instructions", false, false)
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker void SpeculativeExecution::getAnalysisUsage(AnalysisUsage &AU) const {
141*9880d681SAndroid Build Coastguard Worker AU.addRequired<TargetTransformInfoWrapperPass>();
142*9880d681SAndroid Build Coastguard Worker AU.addPreserved<GlobalsAAWrapperPass>();
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)145*9880d681SAndroid Build Coastguard Worker bool SpeculativeExecution::runOnFunction(Function &F) {
146*9880d681SAndroid Build Coastguard Worker if (skipFunction(F))
147*9880d681SAndroid Build Coastguard Worker return false;
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
150*9880d681SAndroid Build Coastguard Worker if (OnlyIfDivergentTarget && !TTI->hasBranchDivergence()) {
151*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Not running SpeculativeExecution because "
152*9880d681SAndroid Build Coastguard Worker "TTI->hasBranchDivergence() is false.\n");
153*9880d681SAndroid Build Coastguard Worker return false;
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker bool Changed = false;
157*9880d681SAndroid Build Coastguard Worker for (auto& B : F) {
158*9880d681SAndroid Build Coastguard Worker Changed |= runOnBasicBlock(B);
159*9880d681SAndroid Build Coastguard Worker }
160*9880d681SAndroid Build Coastguard Worker return Changed;
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
runOnBasicBlock(BasicBlock & B)163*9880d681SAndroid Build Coastguard Worker bool SpeculativeExecution::runOnBasicBlock(BasicBlock &B) {
164*9880d681SAndroid Build Coastguard Worker BranchInst *BI = dyn_cast<BranchInst>(B.getTerminator());
165*9880d681SAndroid Build Coastguard Worker if (BI == nullptr)
166*9880d681SAndroid Build Coastguard Worker return false;
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker if (BI->getNumSuccessors() != 2)
169*9880d681SAndroid Build Coastguard Worker return false;
170*9880d681SAndroid Build Coastguard Worker BasicBlock &Succ0 = *BI->getSuccessor(0);
171*9880d681SAndroid Build Coastguard Worker BasicBlock &Succ1 = *BI->getSuccessor(1);
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker if (&B == &Succ0 || &B == &Succ1 || &Succ0 == &Succ1) {
174*9880d681SAndroid Build Coastguard Worker return false;
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // Hoist from if-then (triangle).
178*9880d681SAndroid Build Coastguard Worker if (Succ0.getSinglePredecessor() != nullptr &&
179*9880d681SAndroid Build Coastguard Worker Succ0.getSingleSuccessor() == &Succ1) {
180*9880d681SAndroid Build Coastguard Worker return considerHoistingFromTo(Succ0, B);
181*9880d681SAndroid Build Coastguard Worker }
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker // Hoist from if-else (triangle).
184*9880d681SAndroid Build Coastguard Worker if (Succ1.getSinglePredecessor() != nullptr &&
185*9880d681SAndroid Build Coastguard Worker Succ1.getSingleSuccessor() == &Succ0) {
186*9880d681SAndroid Build Coastguard Worker return considerHoistingFromTo(Succ1, B);
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker // Hoist from if-then-else (diamond), but only if it is equivalent to
190*9880d681SAndroid Build Coastguard Worker // an if-else or if-then due to one of the branches doing nothing.
191*9880d681SAndroid Build Coastguard Worker if (Succ0.getSinglePredecessor() != nullptr &&
192*9880d681SAndroid Build Coastguard Worker Succ1.getSinglePredecessor() != nullptr &&
193*9880d681SAndroid Build Coastguard Worker Succ1.getSingleSuccessor() != nullptr &&
194*9880d681SAndroid Build Coastguard Worker Succ1.getSingleSuccessor() != &B &&
195*9880d681SAndroid Build Coastguard Worker Succ1.getSingleSuccessor() == Succ0.getSingleSuccessor()) {
196*9880d681SAndroid Build Coastguard Worker // If a block has only one instruction, then that is a terminator
197*9880d681SAndroid Build Coastguard Worker // instruction so that the block does nothing. This does happen.
198*9880d681SAndroid Build Coastguard Worker if (Succ1.size() == 1) // equivalent to if-then
199*9880d681SAndroid Build Coastguard Worker return considerHoistingFromTo(Succ0, B);
200*9880d681SAndroid Build Coastguard Worker if (Succ0.size() == 1) // equivalent to if-else
201*9880d681SAndroid Build Coastguard Worker return considerHoistingFromTo(Succ1, B);
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker return false;
205*9880d681SAndroid Build Coastguard Worker }
206*9880d681SAndroid Build Coastguard Worker
ComputeSpeculationCost(const Instruction * I,const TargetTransformInfo & TTI)207*9880d681SAndroid Build Coastguard Worker static unsigned ComputeSpeculationCost(const Instruction *I,
208*9880d681SAndroid Build Coastguard Worker const TargetTransformInfo &TTI) {
209*9880d681SAndroid Build Coastguard Worker switch (Operator::getOpcode(I)) {
210*9880d681SAndroid Build Coastguard Worker case Instruction::GetElementPtr:
211*9880d681SAndroid Build Coastguard Worker case Instruction::Add:
212*9880d681SAndroid Build Coastguard Worker case Instruction::Mul:
213*9880d681SAndroid Build Coastguard Worker case Instruction::And:
214*9880d681SAndroid Build Coastguard Worker case Instruction::Or:
215*9880d681SAndroid Build Coastguard Worker case Instruction::Select:
216*9880d681SAndroid Build Coastguard Worker case Instruction::Shl:
217*9880d681SAndroid Build Coastguard Worker case Instruction::Sub:
218*9880d681SAndroid Build Coastguard Worker case Instruction::LShr:
219*9880d681SAndroid Build Coastguard Worker case Instruction::AShr:
220*9880d681SAndroid Build Coastguard Worker case Instruction::Xor:
221*9880d681SAndroid Build Coastguard Worker case Instruction::ZExt:
222*9880d681SAndroid Build Coastguard Worker case Instruction::SExt:
223*9880d681SAndroid Build Coastguard Worker return TTI.getUserCost(I);
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker default:
226*9880d681SAndroid Build Coastguard Worker return UINT_MAX; // Disallow anything not whitelisted.
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker }
229*9880d681SAndroid Build Coastguard Worker
considerHoistingFromTo(BasicBlock & FromBlock,BasicBlock & ToBlock)230*9880d681SAndroid Build Coastguard Worker bool SpeculativeExecution::considerHoistingFromTo(BasicBlock &FromBlock,
231*9880d681SAndroid Build Coastguard Worker BasicBlock &ToBlock) {
232*9880d681SAndroid Build Coastguard Worker SmallSet<const Instruction *, 8> NotHoisted;
233*9880d681SAndroid Build Coastguard Worker const auto AllPrecedingUsesFromBlockHoisted = [&NotHoisted](User *U) {
234*9880d681SAndroid Build Coastguard Worker for (Value* V : U->operand_values()) {
235*9880d681SAndroid Build Coastguard Worker if (Instruction *I = dyn_cast<Instruction>(V)) {
236*9880d681SAndroid Build Coastguard Worker if (NotHoisted.count(I) > 0)
237*9880d681SAndroid Build Coastguard Worker return false;
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker return true;
241*9880d681SAndroid Build Coastguard Worker };
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker unsigned TotalSpeculationCost = 0;
244*9880d681SAndroid Build Coastguard Worker for (auto& I : FromBlock) {
245*9880d681SAndroid Build Coastguard Worker const unsigned Cost = ComputeSpeculationCost(&I, *TTI);
246*9880d681SAndroid Build Coastguard Worker if (Cost != UINT_MAX && isSafeToSpeculativelyExecute(&I) &&
247*9880d681SAndroid Build Coastguard Worker AllPrecedingUsesFromBlockHoisted(&I)) {
248*9880d681SAndroid Build Coastguard Worker TotalSpeculationCost += Cost;
249*9880d681SAndroid Build Coastguard Worker if (TotalSpeculationCost > SpecExecMaxSpeculationCost)
250*9880d681SAndroid Build Coastguard Worker return false; // too much to hoist
251*9880d681SAndroid Build Coastguard Worker } else {
252*9880d681SAndroid Build Coastguard Worker NotHoisted.insert(&I);
253*9880d681SAndroid Build Coastguard Worker if (NotHoisted.size() > SpecExecMaxNotHoisted)
254*9880d681SAndroid Build Coastguard Worker return false; // too much left behind
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker }
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker if (TotalSpeculationCost == 0)
259*9880d681SAndroid Build Coastguard Worker return false; // nothing to hoist
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker for (auto I = FromBlock.begin(); I != FromBlock.end();) {
262*9880d681SAndroid Build Coastguard Worker // We have to increment I before moving Current as moving Current
263*9880d681SAndroid Build Coastguard Worker // changes the list that I is iterating through.
264*9880d681SAndroid Build Coastguard Worker auto Current = I;
265*9880d681SAndroid Build Coastguard Worker ++I;
266*9880d681SAndroid Build Coastguard Worker if (!NotHoisted.count(&*Current)) {
267*9880d681SAndroid Build Coastguard Worker Current->moveBefore(ToBlock.getTerminator());
268*9880d681SAndroid Build Coastguard Worker }
269*9880d681SAndroid Build Coastguard Worker }
270*9880d681SAndroid Build Coastguard Worker return true;
271*9880d681SAndroid Build Coastguard Worker }
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker namespace llvm {
274*9880d681SAndroid Build Coastguard Worker
createSpeculativeExecutionPass()275*9880d681SAndroid Build Coastguard Worker FunctionPass *createSpeculativeExecutionPass() {
276*9880d681SAndroid Build Coastguard Worker return new SpeculativeExecution();
277*9880d681SAndroid Build Coastguard Worker }
278*9880d681SAndroid Build Coastguard Worker
createSpeculativeExecutionIfHasBranchDivergencePass()279*9880d681SAndroid Build Coastguard Worker FunctionPass *createSpeculativeExecutionIfHasBranchDivergencePass() {
280*9880d681SAndroid Build Coastguard Worker return new SpeculativeExecution(/* OnlyIfDivergentTarget = */ true);
281*9880d681SAndroid Build Coastguard Worker }
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard Worker } // namespace llvm
284