diff --git a/llvm/lib/Target/BPF/BPF.h b/llvm/lib/Target/BPF/BPF.h --- a/llvm/lib/Target/BPF/BPF.h +++ b/llvm/lib/Target/BPF/BPF.h @@ -56,6 +56,11 @@ static bool isRequired() { return true; } }; + +class BPFAdjustOptPass : public PassInfoMixin { +public: + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; } // namespace llvm #endif diff --git a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp --- a/llvm/lib/Target/BPF/BPFAdjustOpt.cpp +++ b/llvm/lib/Target/BPF/BPFAdjustOpt.cpp @@ -39,6 +39,14 @@ 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; @@ -48,13 +56,12 @@ }; public: - static char ID; - Module *Mod; + BPFAdjustOptImpl(Module *M) : M(M) {} - BPFAdjustOpt() : ModulePass(ID) {} - bool runOnModule(Module &M) override; + bool run(); private: + Module *M; SmallVector PassThroughs; void adjustBasicBlock(BasicBlock &BB); @@ -73,9 +80,10 @@ ModulePass *llvm::createBPFAdjustOpt() { return new BPFAdjustOpt(); } -bool BPFAdjustOpt::runOnModule(Module &M) { - Mod = &M; - for (Function &F : M) +bool BPFAdjustOpt::runOnModule(Module &M) { return BPFAdjustOptImpl(&M).run(); } + +bool BPFAdjustOptImpl::run() { + for (Function &F : *M) for (auto &BB : F) { adjustBasicBlock(BB); for (auto &I : BB) @@ -85,10 +93,10 @@ return insertPassThrough(); } -bool BPFAdjustOpt::insertPassThrough() { +bool BPFAdjustOptImpl::insertPassThrough() { for (auto &Info : PassThroughs) { auto *CI = BPFCoreSharedInfo::insertPassThrough( - Mod, Info.UsedInst->getParent(), Info.Input, Info.UsedInst); + M, Info.UsedInst->getParent(), Info.Input, Info.UsedInst); Info.UsedInst->setOperand(Info.OpIdx, CI); } @@ -97,7 +105,7 @@ // To avoid combining conditionals in the same basic block by // instrcombine optimization. -bool BPFAdjustOpt::serializeICMPInBB(Instruction &I) { +bool BPFAdjustOptImpl::serializeICMPInBB(Instruction &I) { // For: // comp1 = icmp ...; // comp2 = icmp ...; @@ -130,7 +138,7 @@ // To avoid combining conditionals in the same basic block by // instrcombine optimization. -bool BPFAdjustOpt::serializeICMPCrossBB(BasicBlock &BB) { +bool BPFAdjustOptImpl::serializeICMPCrossBB(BasicBlock &BB) { // For: // B1: // comp1 = icmp ...; @@ -204,7 +212,7 @@ // To avoid speculative hoisting certain computations out of // a basic block. -bool BPFAdjustOpt::avoidSpeculation(Instruction &I) { +bool BPFAdjustOptImpl::avoidSpeculation(Instruction &I) { if (auto *LdInst = dyn_cast(&I)) { if (auto *GV = dyn_cast(LdInst->getOperand(0))) { if (GV->hasAttribute(BPFCoreSharedInfo::AmaAttr) || @@ -297,14 +305,19 @@ return true; } -void BPFAdjustOpt::adjustBasicBlock(BasicBlock &BB) { +void BPFAdjustOptImpl::adjustBasicBlock(BasicBlock &BB) { if (!DisableBPFserializeICMP && serializeICMPCrossBB(BB)) return; } -void BPFAdjustOpt::adjustInst(Instruction &I) { +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(); +} diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp --- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp +++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp @@ -136,6 +136,10 @@ PassBuilder::OptimizationLevel Level) { FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true))); }); + PB.registerPipelineEarlySimplificationEPCallback( + [=](ModulePassManager &MPM, PassBuilder::OptimizationLevel) { + MPM.addPass(BPFAdjustOptPass()); + }); } void BPFPassConfig::addIRPasses() { diff --git a/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll b/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll --- a/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll +++ b/llvm/test/CodeGen/BPF/adjust-opt-icmp1.ll @@ -1,7 +1,11 @@ ; RUN: opt -O2 -mtriple=bpf-pc-linux %s | llvm-dis > %t1 ; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK %s +; RUN: opt -passes='default' -mtriple=bpf-pc-linux %s | llvm-dis > %t1 +; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK %s ; RUN: opt -O2 -mtriple=bpf-pc-linux -bpf-disable-serialize-icmp %s | llvm-dis > %t1 ; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK-DISABLE %s +; RUN: opt -passes='default' -mtriple=bpf-pc-linux -bpf-disable-serialize-icmp %s | llvm-dis > %t1 +; RUN: llc %t1 -o - | FileCheck -check-prefixes=CHECK-DISABLE %s ; ; Source: ; int foo();