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