76 #define DEBUG_TYPE "speculative-execution"
82 cl::desc(
"Speculative execution is not applied to basic blocks where "
83 "the cost of the instructions to speculatively execute "
84 "exceeds this limit."));
92 cl::desc(
"Speculative execution is not applied to basic blocks where the "
93 "number of instructions that would not be speculatively executed "
94 "exceeds this limit."));
98 cl::desc(
"Speculative execution is applied only to targets with divergent "
99 "branches, even if the pass was configured to apply only to all "
104 class SpeculativeExecutionLegacyPass :
public FunctionPass {
107 explicit SpeculativeExecutionLegacyPass(
bool OnlyIfDivergentTarget =
false)
108 :
FunctionPass(
ID), OnlyIfDivergentTarget(OnlyIfDivergentTarget ||
110 Impl(OnlyIfDivergentTarget) {}
113 bool runOnFunction(
Function &
F)
override;
116 if (OnlyIfDivergentTarget)
117 return "Speculatively execute instructions if target has divergent "
119 return "Speculatively execute instructions";
124 const bool OnlyIfDivergentTarget;
132 "Speculatively execute instructions",
false,
false)
137 void SpeculativeExecutionLegacyPass::getAnalysisUsage(
AnalysisUsage &AU)
const {
142 bool SpeculativeExecutionLegacyPass::runOnFunction(
Function &
F) {
146 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
147 return Impl.runImpl(F, TTI);
154 DEBUG(
dbgs() <<
"Not running SpeculativeExecution because "
155 "TTI->hasBranchDivergence() is false.\n");
160 bool Changed =
false;
162 Changed |= runOnBasicBlock(
B);
167 bool SpeculativeExecutionPass::runOnBasicBlock(
BasicBlock &
B) {
177 if (&B == &Succ0 || &B == &Succ1 || &Succ0 == &Succ1) {
184 return considerHoistingFromTo(Succ0, B);
190 return considerHoistingFromTo(Succ1, B);
202 if (Succ1.
size() == 1)
203 return considerHoistingFromTo(Succ0, B);
204 if (Succ0.
size() == 1)
205 return considerHoistingFromTo(Succ1, B);
214 case Instruction::GetElementPtr:
216 case Instruction::Mul:
220 case Instruction::Shl:
221 case Instruction::Sub:
222 case Instruction::LShr:
223 case Instruction::AShr:
225 case Instruction::ZExt:
226 case Instruction::SExt:
228 case Instruction::BitCast:
229 case Instruction::PtrToInt:
230 case Instruction::IntToPtr:
231 case Instruction::AddrSpaceCast:
232 case Instruction::FPToUI:
233 case Instruction::FPToSI:
234 case Instruction::UIToFP:
235 case Instruction::SIToFP:
236 case Instruction::FPExt:
237 case Instruction::FPTrunc:
238 case Instruction::FAdd:
239 case Instruction::FSub:
240 case Instruction::FMul:
241 case Instruction::FDiv:
242 case Instruction::FRem:
243 case Instruction::ICmp:
244 case Instruction::FCmp:
252 bool SpeculativeExecutionPass::considerHoistingFromTo(
255 const auto AllPrecedingUsesFromBlockHoisted = [&NotHoisted](
User *U) {
256 for (
Value* V : U->operand_values()) {
258 if (NotHoisted.
count(
I) > 0)
265 unsigned TotalSpeculationCost = 0;
266 for (
auto&
I : FromBlock) {
269 AllPrecedingUsesFromBlockHoisted(&
I)) {
270 TotalSpeculationCost += Cost;
280 if (TotalSpeculationCost == 0)
283 for (
auto I = FromBlock.begin();
I != FromBlock.end();) {
288 if (!NotHoisted.
count(&*Current)) {
296 return new SpeculativeExecutionLegacyPass();
300 return new SpeculativeExecutionLegacyPass(
true);
304 : OnlyIfDivergentTarget(OnlyIfDivergentTarget ||
311 bool Changed =
runImpl(F, TTI);
Legacy wrapper pass to provide the GlobalsAAResult object.
FunctionPass * createSpeculativeExecutionPass()
This is the interface for a simple mod/ref and alias analysis over globals.
Analysis pass providing the TargetTransformInfo.
static unsigned ComputeSpeculationCost(const Instruction *I, const TargetTransformInfo &TTI)
speculative Speculatively execute false
#define INITIALIZE_PASS_DEPENDENCY(depName)
static cl::opt< unsigned > SpecExecMaxSpeculationCost("spec-exec-max-speculation-cost", cl::init(7), cl::Hidden, cl::desc("Speculative execution is not applied to basic blocks where ""the cost of the instructions to speculatively execute ""exceeds this limit."))
BasicBlock * getSuccessor(unsigned i) const
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
initializer< Ty > init(const Ty &Val)
A set of analyses that are preserved following a run of a transformation pass.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
INITIALIZE_PASS_BEGIN(SpeculativeExecutionLegacyPass,"speculative-execution","Speculatively execute instructions", false, false) INITIALIZE_PASS_END(SpeculativeExecutionLegacyPass
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
SpeculativeExecutionPass(bool OnlyIfDivergentTarget=false)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Module.h This file contains the declarations for the Module class.
FunctionPass * createSpeculativeExecutionIfHasBranchDivergencePass()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static cl::opt< bool > SpecExecOnlyIfDivergentTarget("spec-exec-only-if-divergent-target", cl::init(false), cl::Hidden, cl::desc("Speculative execution is applied only to targets with divergent ""branches, even if the pass was configured to apply only to all ""targets."))
static cl::opt< unsigned > SpecExecMaxNotHoisted("spec-exec-max-not-hoisted", cl::init(5), cl::Hidden, cl::desc("Speculative execution is not applied to basic blocks where the ""number of instructions that would not be speculatively executed ""exceeds this limit."))
BasicBlock * getSingleSuccessor()
Return the successor of this block if it has a single successor.
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
unsigned getNumSuccessors() const
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM Value Representation.
bool runImpl(Function &F, TargetTransformInfo *TTI)
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
A container for analyses that lazily runs them and caches their results.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)