#include "BPF.h"
#include "BPFCORE.h"
#include "BPFTargetMachine.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicsBPF.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#define DEBUG_TYPE "bpf-adjust-opt"
using namespace llvm;
using namespace llvm::PatternMatch;
static cl::opt<bool>
DisableBPFserializeICMP("bpf-disable-serialize-icmp", cl::Hidden,
cl::desc("BPF: Disable Serializing ICMP insns."),
cl::init(false));
static cl::opt<bool> DisableBPFavoidSpeculation(
"bpf-disable-avoid-speculation", cl::Hidden,
cl::desc("BPF: Disable Avoiding Speculative Code Motion."),
cl::init(false));
namespace {
class BPFAdjustOpt final : public ModulePass {
public:
static char ID;
BPFAdjustOpt() : ModulePass(ID) {}
bool runOnModule(Module &M) override;
};
class BPFAdjustOptImpl {
struct PassThroughInfo {
Instruction *Input;
Instruction *UsedInst;
uint32_t OpIdx;
PassThroughInfo(Instruction *I, Instruction *U, uint32_t Idx)
: Input(I), UsedInst(U), OpIdx(Idx) {}
};
public:
BPFAdjustOptImpl(Module *M) : M(M) {}
bool run();
private:
Module *M;
SmallVector<PassThroughInfo, 16> PassThroughs;
bool adjustICmpToBuiltin();
void adjustBasicBlock(BasicBlock &BB);
bool serializeICMPCrossBB(BasicBlock &BB);
void adjustInst(Instruction &I);
bool serializeICMPInBB(Instruction &I);
bool avoidSpeculation(Instruction &I);
bool insertPassThrough();
};
}
char BPFAdjustOpt::ID = 0;
INITIALIZE_PASS(BPFAdjustOpt, "bpf-adjust-opt", "BPF Adjust Optimization",
false, false)
ModulePass *llvm::createBPFAdjustOpt() { return new BPFAdjustOpt(); }
bool BPFAdjustOpt::runOnModule(Module &M) { return BPFAdjustOptImpl(&M).run(); }
bool BPFAdjustOptImpl::run() {
bool Changed = adjustICmpToBuiltin();
for (Function &F : *M)
for (auto &BB : F) {
adjustBasicBlock(BB);
for (auto &I : BB)
adjustInst(I);
}
return insertPassThrough() || Changed;
}
bool BPFAdjustOptImpl::adjustICmpToBuiltin() {
bool Changed = false;
ICmpInst *ToBeDeleted = nullptr;
for (Function &F : *M)
for (auto &BB : F)
for (auto &I : BB) {
if (ToBeDeleted) {
ToBeDeleted->eraseFromParent();
ToBeDeleted = nullptr;
}
auto *Icmp = dyn_cast<ICmpInst>(&I);
if (!Icmp)
continue;
Value *Op0 = Icmp->getOperand(0);
if (!isa<TruncInst>(Op0))
continue;
auto ConstOp1 = dyn_cast<ConstantInt>(Icmp->getOperand(1));
if (!ConstOp1)
continue;
auto ConstOp1Val = ConstOp1->getValue().getZExtValue();
auto Op = Icmp->getPredicate();
if (Op == ICmpInst::ICMP_ULT || Op == ICmpInst::ICMP_UGE) {
if ((ConstOp1Val - 1) & ConstOp1Val)
continue;
} else if (Op == ICmpInst::ICMP_ULE || Op == ICmpInst::ICMP_UGT) {
if (ConstOp1Val & (ConstOp1Val + 1))
continue;
} else {
continue;
}
Constant *Opcode =
ConstantInt::get(Type::getInt32Ty(BB.getContext()), Op);
Function *Fn = Intrinsic::getDeclaration(
M, Intrinsic::bpf_compare, {Op0->getType(), ConstOp1->getType()});
auto *NewInst = CallInst::Create(Fn, {Opcode, Op0, ConstOp1});
BB.getInstList().insert(I.getIterator(), NewInst);
Icmp->replaceAllUsesWith(NewInst);
Changed = true;
ToBeDeleted = Icmp;
}
return Changed;
}
bool BPFAdjustOptImpl::insertPassThrough() {
for (auto &Info : PassThroughs) {
auto *CI = BPFCoreSharedInfo::insertPassThrough(
M, Info.UsedInst->getParent(), Info.Input, Info.UsedInst);
Info.UsedInst->setOperand(Info.OpIdx, CI);
}
return !PassThroughs.empty();
}
bool BPFAdjustOptImpl::serializeICMPInBB(Instruction &I) {
Value *Op0, *Op1;
if (!match(&I, m_LogicalOr(m_Value(Op0), m_Value(Op1))))
return false;
auto *Icmp1 = dyn_cast<ICmpInst>(Op0);
if (!Icmp1)
return false;
auto *Icmp2 = dyn_cast<ICmpInst>(Op1);
if (!Icmp2)
return false;
Value *Icmp1Op0 = Icmp1->getOperand(0);
Value *Icmp2Op0 = Icmp2->getOperand(0);
if (Icmp1Op0 != Icmp2Op0)
return false;
PassThroughInfo Info(Icmp1, &I, 0);
PassThroughs.push_back(Info);
return true;
}
bool BPFAdjustOptImpl::serializeICMPCrossBB(BasicBlock &BB) {
BasicBlock *B2 = BB.getSinglePredecessor();
if (!B2)
return false;
BasicBlock *B1 = B2->getSinglePredecessor();
if (!B1)
return false;
Instruction *TI = B2->getTerminator();
auto *BI = dyn_cast<BranchInst>(TI);
if (!BI || !BI->isConditional())
return false;
auto *Cond = dyn_cast<ICmpInst>(BI->getCondition());
if (!Cond || B2->getFirstNonPHI() != Cond)
return false;
Value *B2Op0 = Cond->getOperand(0);
auto Cond2Op = Cond->getPredicate();
TI = B1->getTerminator();
BI = dyn_cast<BranchInst>(TI);
if (!BI || !BI->isConditional())
return false;
Cond = dyn_cast<ICmpInst>(BI->getCondition());
if (!Cond)
return false;
Value *B1Op0 = Cond->getOperand(0);
auto Cond1Op = Cond->getPredicate();
if (B1Op0 != B2Op0)
return false;
if (Cond1Op == ICmpInst::ICMP_SGT || Cond1Op == ICmpInst::ICMP_SGE) {
if (Cond2Op != ICmpInst::ICMP_SLT && Cond2Op != ICmpInst::ICMP_SLE)
return false;
} else if (Cond1Op == ICmpInst::ICMP_SLT || Cond1Op == ICmpInst::ICMP_SLE) {
if (Cond2Op != ICmpInst::ICMP_SGT && Cond2Op != ICmpInst::ICMP_SGE)
return false;
} else if (Cond1Op == ICmpInst::ICMP_ULT || Cond1Op == ICmpInst::ICMP_ULE) {
if (Cond2Op != ICmpInst::ICMP_UGT && Cond2Op != ICmpInst::ICMP_UGE)
return false;
} else if (Cond1Op == ICmpInst::ICMP_UGT || Cond1Op == ICmpInst::ICMP_UGE) {
if (Cond2Op != ICmpInst::ICMP_ULT && Cond2Op != ICmpInst::ICMP_ULE)
return false;
} else {
return false;
}
PassThroughInfo Info(Cond, BI, 0);
PassThroughs.push_back(Info);
return true;
}
bool BPFAdjustOptImpl::avoidSpeculation(Instruction &I) {
if (auto *LdInst = dyn_cast<LoadInst>(&I)) {
if (auto *GV = dyn_cast<GlobalVariable>(LdInst->getOperand(0))) {
if (GV->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
GV->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
return false;
}
}
if (!isa<LoadInst>(&I) && !isa<CallInst>(&I))
return false;
bool isCandidate = false;
SmallVector<PassThroughInfo, 4> Candidates;
for (User *U : I.users()) {
Instruction *Inst = dyn_cast<Instruction>(U);
if (!Inst)
continue;
if (auto *Icmp1 = dyn_cast<ICmpInst>(Inst)) {
Value *Icmp1Op1 = Icmp1->getOperand(1);
if (!isa<Constant>(Icmp1Op1))
return false;
isCandidate = true;
continue;
}
if (Inst->getParent() == I.getParent())
continue;
for (auto &I2 : *Inst->getParent()) {
if (isa<CallInst>(&I2))
return false;
if (isa<LoadInst>(&I2) || isa<StoreInst>(&I2))
return false;
if (&I2 == Inst)
break;
}
if (Inst->getOpcode() == Instruction::ZExt ||
Inst->getOpcode() == Instruction::SExt) {
PassThroughInfo Info(&I, Inst, 0);
Candidates.push_back(Info);
} else if (auto *GI = dyn_cast<GetElementPtrInst>(Inst)) {
unsigned i, e;
for (i = 1, e = GI->getNumOperands(); i != e; ++i) {
Value *V = GI->getOperand(i);
if (V == &I)
break;
}
if (i == e)
continue;
PassThroughInfo Info(&I, GI, i);
Candidates.push_back(Info);
}
}
if (!isCandidate || Candidates.empty())
return false;
llvm::append_range(PassThroughs, Candidates);
return true;
}
void BPFAdjustOptImpl::adjustBasicBlock(BasicBlock &BB) {
if (!DisableBPFserializeICMP && serializeICMPCrossBB(BB))
return;
}
void BPFAdjustOptImpl::adjustInst(Instruction &I) {
if (!DisableBPFserializeICMP && serializeICMPInBB(I))
return;
if (!DisableBPFavoidSpeculation && avoidSpeculation(I))
return;
}
PreservedAnalyses BPFAdjustOptPass::run(Module &M, ModuleAnalysisManager &AM) {
return BPFAdjustOptImpl(&M).run() ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}