SpeculativeExecution.cpp [plain text]
#include "llvm/ADT/SmallSet.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
using namespace llvm;
#define DEBUG_TYPE "speculative-execution"
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."));
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."));
namespace {
class SpeculativeExecution : public FunctionPass {
public:
static char ID;
SpeculativeExecution(): FunctionPass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnFunction(Function &F) override;
private:
bool runOnBasicBlock(BasicBlock &B);
bool considerHoistingFromTo(BasicBlock &FromBlock, BasicBlock &ToBlock);
const TargetTransformInfo *TTI = nullptr;
};
}
char SpeculativeExecution::ID = 0;
INITIALIZE_PASS_BEGIN(SpeculativeExecution, "speculative-execution",
"Speculatively execute instructions", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_PASS_END(SpeculativeExecution, "speculative-execution",
"Speculatively execute instructions", false, false)
void SpeculativeExecution::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetTransformInfoWrapperPass>();
}
bool SpeculativeExecution::runOnFunction(Function &F) {
if (skipOptnoneFunction(F))
return false;
TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
bool Changed = false;
for (auto& B : F) {
Changed |= runOnBasicBlock(B);
}
return Changed;
}
bool SpeculativeExecution::runOnBasicBlock(BasicBlock &B) {
BranchInst *BI = dyn_cast<BranchInst>(B.getTerminator());
if (BI == nullptr)
return false;
if (BI->getNumSuccessors() != 2)
return false;
BasicBlock &Succ0 = *BI->getSuccessor(0);
BasicBlock &Succ1 = *BI->getSuccessor(1);
if (&B == &Succ0 || &B == &Succ1 || &Succ0 == &Succ1) {
return false;
}
if (Succ0.getSinglePredecessor() != nullptr &&
Succ0.getSingleSuccessor() == &Succ1) {
return considerHoistingFromTo(Succ0, B);
}
if (Succ1.getSinglePredecessor() != nullptr &&
Succ1.getSingleSuccessor() == &Succ0) {
return considerHoistingFromTo(Succ1, B);
}
if (Succ0.getSinglePredecessor() != nullptr &&
Succ1.getSinglePredecessor() != nullptr &&
Succ1.getSingleSuccessor() != nullptr &&
Succ1.getSingleSuccessor() != &B &&
Succ1.getSingleSuccessor() == Succ0.getSingleSuccessor()) {
if (Succ1.size() == 1) return considerHoistingFromTo(Succ0, B);
if (Succ0.size() == 1) return considerHoistingFromTo(Succ1, B);
}
return false;
}
static unsigned ComputeSpeculationCost(const Instruction *I,
const TargetTransformInfo &TTI) {
switch (Operator::getOpcode(I)) {
case Instruction::GetElementPtr:
case Instruction::Add:
case Instruction::Mul:
case Instruction::And:
case Instruction::Or:
case Instruction::Select:
case Instruction::Shl:
case Instruction::Sub:
case Instruction::LShr:
case Instruction::AShr:
case Instruction::Xor:
case Instruction::ZExt:
case Instruction::SExt:
return TTI.getUserCost(I);
default:
return UINT_MAX; }
}
bool SpeculativeExecution::considerHoistingFromTo(BasicBlock &FromBlock,
BasicBlock &ToBlock) {
SmallSet<const Instruction *, 8> NotHoisted;
const auto AllPrecedingUsesFromBlockHoisted = [&NotHoisted](User *U) {
for (Value* V : U->operand_values()) {
if (Instruction *I = dyn_cast<Instruction>(V)) {
if (NotHoisted.count(I) > 0)
return false;
}
}
return true;
};
unsigned TotalSpeculationCost = 0;
for (auto& I : FromBlock) {
const unsigned Cost = ComputeSpeculationCost(&I, *TTI);
if (Cost != UINT_MAX && isSafeToSpeculativelyExecute(&I) &&
AllPrecedingUsesFromBlockHoisted(&I)) {
TotalSpeculationCost += Cost;
if (TotalSpeculationCost > SpecExecMaxSpeculationCost)
return false; } else {
NotHoisted.insert(&I);
if (NotHoisted.size() > SpecExecMaxNotHoisted)
return false; }
}
if (TotalSpeculationCost == 0)
return false;
for (auto I = FromBlock.begin(); I != FromBlock.end();) {
auto Current = I;
++I;
if (!NotHoisted.count(&*Current)) {
Current->moveBefore(ToBlock.getTerminator());
}
}
return true;
}
namespace llvm {
FunctionPass *createSpeculativeExecutionPass() {
return new SpeculativeExecution();
}
}