1 //===- SpeculativeExecution.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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
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):
33 // which could later be optimized to
41 // Later passes sink back much of the speculated code that did not enable
42 // further optimization.
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.
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.
59 // This lets you include this pass unconditionally in the IR pass pipeline, but
60 // only enable it for relevant targets.
62 //===----------------------------------------------------------------------===//
63 #ifndef LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H
64 #define LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H
66 #include "llvm/Analysis/TargetTransformInfo.h"
67 #include "llvm/IR/PassManager.h"
70 class SpeculativeExecutionPass
71 : public PassInfoMixin<SpeculativeExecutionPass> {
73 SpeculativeExecutionPass(bool OnlyIfDivergentTarget = false);
75 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
78 bool runImpl(Function &F, TargetTransformInfo *TTI);
81 bool runOnBasicBlock(BasicBlock &B);
82 bool considerHoistingFromTo(BasicBlock &FromBlock, BasicBlock &ToBlock);
84 // If true, this pass is a nop unless the target architecture has branch
86 const bool OnlyIfDivergentTarget = false;
88 TargetTransformInfo *TTI = nullptr;
92 #endif //LLVM_TRANSFORMS_SCALAR_SPECULATIVEEXECUTION_H