66 #define DEBUG_TYPE "speculative-execution"
72 cl::desc(
"Speculative execution is not applied to basic blocks where "
73 "the cost of the instructions to speculatively execute "
74 "exceeds this limit."));
82 cl::desc(
"Speculative execution is not applied to basic blocks where the "
83 "number of instructions that would not be speculatively executed "
84 "exceeds this limit."));
105 "Speculatively execute instructions",
false,
false)
110 void SpeculativeExecution::getAnalysisUsage(
AnalysisUsage &AU)
const {
114 bool SpeculativeExecution::runOnFunction(
Function &
F) {
115 if (skipOptnoneFunction(F))
118 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
120 bool Changed =
false;
122 Changed |= runOnBasicBlock(B);
127 bool SpeculativeExecution::runOnBasicBlock(
BasicBlock &B) {
137 if (&B == &Succ0 || &B == &Succ1 || &Succ0 == &Succ1) {
144 return considerHoistingFromTo(Succ0, B);
150 return considerHoistingFromTo(Succ1, B);
162 if (Succ1.
size() == 1)
163 return considerHoistingFromTo(Succ0, B);
164 if (Succ0.
size() == 1)
165 return considerHoistingFromTo(Succ1, B);
174 case Instruction::GetElementPtr:
175 case Instruction::Add:
176 case Instruction::Mul:
180 case Instruction::Shl:
181 case Instruction::Sub:
182 case Instruction::LShr:
183 case Instruction::AShr:
185 case Instruction::ZExt:
186 case Instruction::SExt:
194 bool SpeculativeExecution::considerHoistingFromTo(
BasicBlock &FromBlock,
197 const auto AllPrecedingUsesFromBlockHoisted = [&NotHoisted](
User *U) {
198 for (
Value* V : U->operand_values()) {
200 if (NotHoisted.
count(
I) > 0)
207 unsigned TotalSpeculationCost = 0;
208 for (
auto&
I : FromBlock) {
211 AllPrecedingUsesFromBlockHoisted(&
I)) {
212 TotalSpeculationCost += Cost;
222 if (TotalSpeculationCost == 0)
225 for (
auto I = FromBlock.begin();
I != FromBlock.end();) {
230 if (!NotHoisted.
count(Current)) {
240 return new SpeculativeExecution();
FunctionPass * createSpeculativeExecutionPass()
speculative Speculatively execute instructions
speculative Speculatively execute false
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
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
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
BranchInst - Conditional or Unconditional Branch instruction.
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.
INITIALIZE_PASS_BEGIN(SpeculativeExecution,"speculative-execution","Speculatively execute instructions", false, false) INITIALIZE_PASS_END(SpeculativeExecution
Represent the analysis usage information of a pass.
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.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
static unsigned ComputeSpeculationCost(const Instruction *I, const TargetTransformInfo &TTI)
Module.h This file contains the declarations for the Module class.
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.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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 Value Representation.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...