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) {}
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 {
140 AU.setPreservesCFG();
147 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
148 return Impl.runImpl(F, TTI);
156 "TTI->hasBranchDivergence() is false.\n");
161 bool Changed =
false;
168 bool SpeculativeExecutionPass::runOnBasicBlock(
BasicBlock &
B) {
178 if (&B == &Succ0 || &B == &Succ1 || &Succ0 == &Succ1) {
185 return considerHoistingFromTo(Succ0, B);
191 return considerHoistingFromTo(Succ1, B);
203 if (Succ1.
size() == 1)
204 return considerHoistingFromTo(Succ0, B);
205 if (Succ0.
size() == 1)
206 return considerHoistingFromTo(Succ1, B);
215 case Instruction::GetElementPtr:
217 case Instruction::Mul:
218 case Instruction::And:
219 case Instruction::Or:
221 case Instruction::Shl:
222 case Instruction::Sub:
223 case Instruction::LShr:
224 case Instruction::AShr:
225 case Instruction::Xor:
226 case Instruction::ZExt:
227 case Instruction::SExt:
229 case Instruction::BitCast:
230 case Instruction::PtrToInt:
231 case Instruction::IntToPtr:
232 case Instruction::AddrSpaceCast:
233 case Instruction::FPToUI:
234 case Instruction::FPToSI:
235 case Instruction::UIToFP:
236 case Instruction::SIToFP:
237 case Instruction::FPExt:
238 case Instruction::FPTrunc:
239 case Instruction::FAdd:
240 case Instruction::FSub:
241 case Instruction::FMul:
242 case Instruction::FDiv:
243 case Instruction::FRem:
244 case Instruction::ICmp:
245 case Instruction::FCmp:
253 bool SpeculativeExecutionPass::considerHoistingFromTo(
256 const auto AllPrecedingUsesFromBlockHoisted = [&NotHoisted](
User *U) {
257 for (
Value* V : U->operand_values()) {
259 if (NotHoisted.
count(
I) > 0)
266 unsigned TotalSpeculationCost = 0;
267 for (
auto&
I : FromBlock) {
270 AllPrecedingUsesFromBlockHoisted(&
I)) {
271 TotalSpeculationCost += Cost;
281 if (TotalSpeculationCost == 0)
284 for (
auto I = FromBlock.begin();
I != FromBlock.end();) {
289 if (!NotHoisted.
count(&*Current)) {
297 return new SpeculativeExecutionLegacyPass();
301 return new SpeculativeExecutionLegacyPass(
true);
305 : OnlyIfDivergentTarget(OnlyIfDivergentTarget ||
312 bool Changed =
runImpl(F, TTI);
Legacy wrapper pass to provide the GlobalsAAResult object.
FunctionPass * createSpeculativeExecutionPass()
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
This is the interface for a simple mod/ref and alias analysis over globals.
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."))
Analysis pass providing the TargetTransformInfo.
BasicBlock * getSuccessor(unsigned i) const
static unsigned ComputeSpeculationCost(const Instruction *I, const TargetTransformInfo &TTI)
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
#define INITIALIZE_PASS_DEPENDENCY(depName)
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."))
unsigned getNumSuccessors() const
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
INITIALIZE_PASS_BEGIN(SpeculativeExecutionLegacyPass, "speculative-execution", "Speculatively execute instructions", false, false) INITIALIZE_PASS_END(SpeculativeExecutionLegacyPass
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
SpeculativeExecutionPass(bool OnlyIfDivergentTarget=false)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
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 bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, std::vector< unsigned > &renamedInOtherBB, unsigned &basicBlockNum, unsigned &VRegGapIndex, NamedVRegCursor &NVC)
Represents analyses that only rely on functions' control flow.
void preserveSet()
Mark an analysis set as preserved.
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.
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."))
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
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)