77#define DEBUG_TYPE "speculative-execution"
81class SpeculativeExecutionLegacyPass :
public FunctionPass {
84 explicit SpeculativeExecutionLegacyPass(
bool OnlyIfDivergentTarget =
false)
86 OnlyIfDivergentTarget(
87 OnlyIfDivergentTarget ||
88 ScalarOptions::Global.spec_exec_only_if_divergent_target),
89 Impl(OnlyIfDivergentTarget) {}
95 if (OnlyIfDivergentTarget)
96 return "Speculatively execute instructions if target has divergent "
98 return "Speculatively execute instructions";
103 const bool OnlyIfDivergentTarget;
109char SpeculativeExecutionLegacyPass::ID = 0;
111 "Speculatively execute instructions",
false,
false)
116void SpeculativeExecutionLegacyPass::getAnalysisUsage(
AnalysisUsage &AU)
const {
119 AU.setPreservesCFG();
122bool SpeculativeExecutionLegacyPass::runOnFunction(
Function &
F) {
126 auto *
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
131 if (OnlyIfDivergentTarget && !TTI->hasBranchDivergence(&
F)) {
133 "TTI->hasBranchDivergence() is false.\n");
145bool SpeculativeExecutionPass::runOnBasicBlock(
BasicBlock &
B) {
153 if (&
B == &Succ0 || &
B == &Succ1 || &Succ0 == &Succ1) {
160 return considerHoistingFromTo(Succ0,
B);
166 return considerHoistingFromTo(Succ1,
B);
178 if (Succ1.
size() == 1)
179 return considerHoistingFromTo(Succ0,
B);
180 if (Succ0.
size() == 1)
181 return considerHoistingFromTo(Succ1,
B);
190 case Instruction::GetElementPtr:
191 case Instruction::Add:
192 case Instruction::Mul:
193 case Instruction::And:
194 case Instruction::Or:
195 case Instruction::Select:
196 case Instruction::Shl:
197 case Instruction::Sub:
198 case Instruction::LShr:
199 case Instruction::AShr:
200 case Instruction::Xor:
201 case Instruction::ZExt:
202 case Instruction::SExt:
203 case Instruction::Call:
204 case Instruction::BitCast:
205 case Instruction::PtrToInt:
206 case Instruction::PtrToAddr:
207 case Instruction::IntToPtr:
208 case Instruction::AddrSpaceCast:
209 case Instruction::FPToUI:
210 case Instruction::FPToSI:
211 case Instruction::UIToFP:
212 case Instruction::SIToFP:
213 case Instruction::FPExt:
214 case Instruction::FPTrunc:
215 case Instruction::FAdd:
216 case Instruction::FSub:
217 case Instruction::FMul:
218 case Instruction::FDiv:
219 case Instruction::FRem:
220 case Instruction::FNeg:
221 case Instruction::ICmp:
222 case Instruction::FCmp:
223 case Instruction::Trunc:
224 case Instruction::Freeze:
225 case Instruction::ExtractElement:
226 case Instruction::InsertElement:
227 case Instruction::ShuffleVector:
228 case Instruction::ExtractValue:
229 case Instruction::InsertValue:
260bool SpeculativeExecutionPass::considerHoistingFromTo(
261 BasicBlock &FromBlock, BasicBlock &ToBlock) {
262 const ScalarOptions &Opts = ScalarOptions::Global;
263 SmallPtrSet<const Instruction *, 8> NotHoisted;
264 auto HasNoUnhoistedInstr = [&NotHoisted](
auto Values) {
272 auto AllPrecedingUsesFromBlockHoisted =
273 [&HasNoUnhoistedInstr](
const User *
U) {
274 return HasNoUnhoistedInstr(
U->operand_values());
278 unsigned NotHoistedInstCount = 0;
279 for (
const auto &
I : FromBlock) {
282 AllPrecedingUsesFromBlockHoisted(&
I)) {
283 TotalSpeculationCost +=
Cost;
284 if (TotalSpeculationCost > Opts.spec_exec_max_speculation_cost)
287 NotHoistedInstCount++;
288 if (NotHoistedInstCount > Opts.spec_exec_max_not_hoisted)
294 for (
auto I = FromBlock.begin();
I != FromBlock.end();) {
299 if (!NotHoisted.
count(&*Current)) {
301 Current->dropLocation();
308 return new SpeculativeExecutionLegacyPass();
312 return new SpeculativeExecutionLegacyPass(
true);
316 : OnlyIfDivergentTarget(
317 OnlyIfDivergentTarget ||
318 ScalarOptions::
Global.spec_exec_only_if_divergent_target) {}
335 static_cast<PassInfoMixin<SpeculativeExecutionPass> *
>(
this)->
printPipeline(
336 OS, MapClassName2PassName);
338 if (OnlyIfDivergentTarget)
339 OS <<
"only-if-divergent-target";
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
This is the interface for a simple mod/ref and alias analysis over globals.
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallPtrSet class.
static InstructionCost ComputeSpeculationCost(const Instruction *I, const TargetTransformInfo &TTI)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
LLVM Basic Block Representation.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Represents analyses that only rely on functions' control flow.
Conditional Branch instruction.
BasicBlock * getSuccessor(unsigned i) const
FunctionPass class - This class is used to implement most global optimizations.
Legacy wrapper pass to provide the GlobalsAAResult object.
static InstructionCost getInvalid(CostType Val=0)
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
LLVM_ABI bool runImpl(Function &F, TargetTransformInfo *TTI)
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI SpeculativeExecutionPass(bool OnlyIfDivergentTarget=false)
Represent a constant reference to a string, i.e.
Analysis pass providing the TargetTransformInfo.
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
@ User
could "use" a pointer
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr Values
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI FunctionPass * createSpeculativeExecutionIfHasBranchDivergencePass()
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto dyn_cast_or_null(const Y &Val)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI FunctionPass * createSpeculativeExecutionPass()
@ Global
Append to llvm.global_dtors.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.