diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h --- a/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileInference.h @@ -18,30 +18,8 @@ #include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/IR/BasicBlock.h" -#include "llvm/IR/Instruction.h" -#include "llvm/IR/Instructions.h" - namespace llvm { -class Function; -class MachineBasicBlock; -class MachineFunction; - -namespace afdo_detail { - -template struct TypeMap {}; -template <> struct TypeMap { - using BasicBlockT = BasicBlock; - using FunctionT = Function; -}; -template <> struct TypeMap { - using BasicBlockT = MachineBasicBlock; - using FunctionT = MachineFunction; -}; - -} // end namespace afdo_detail - struct FlowJump; /// A wrapper of a binary basic block. @@ -140,8 +118,9 @@ /// Sample profile inference pass. template class SampleProfileInference { public: - using BasicBlockT = typename afdo_detail::TypeMap::BasicBlockT; - using FunctionT = typename afdo_detail::TypeMap::FunctionT; + using NodeRef = typename GraphTraits::NodeRef; + using BasicBlockT = typename std::remove_pointer::type; + using FunctionT = BT; using Edge = std::pair; using BlockWeightMap = DenseMap; using EdgeWeightMap = DenseMap; @@ -321,40 +300,10 @@ const std::vector &BasicBlocks, BlockEdgeMap &Successors, FlowFunction &Func) {} -template <> -inline void SampleProfileInference::findUnlikelyJumps( - const std::vector &BasicBlocks, - BlockEdgeMap &Successors, FlowFunction &Func) { - for (auto &Jump : Func.Jumps) { - const auto *BB = BasicBlocks[Jump.Source]; - const auto *Succ = BasicBlocks[Jump.Target]; - const Instruction *TI = BB->getTerminator(); - // Check if a block ends with InvokeInst and mark non-taken branch unlikely. - // In that case block Succ should be a landing pad - if (Successors[BB].size() == 2 && Successors[BB].back() == Succ) { - if (isa(TI)) { - Jump.IsUnlikely = true; - } - } - const Instruction *SuccTI = Succ->getTerminator(); - // Check if the target block contains UnreachableInst and mark it unlikely - if (SuccTI->getNumSuccessors() == 0) { - if (isa(SuccTI)) { - Jump.IsUnlikely = true; - } - } - } -} - template inline bool SampleProfileInference::isExit(const BasicBlockT *BB) { return BB->succ_empty(); } -template <> -inline bool SampleProfileInference::isExit(const BasicBlock *BB) { - return succ_empty(BB); -} - } // end namespace llvm #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCE_H diff --git a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h --- a/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h +++ b/llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h @@ -132,13 +132,15 @@ extern cl::opt SampleProfileUseProfi; -template class SampleProfileLoaderBaseImpl { +template class SampleProfileLoaderBaseImpl { public: SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName, IntrusiveRefCntPtr FS) : Filename(Name), RemappingFilename(RemapName), FS(std::move(FS)) {} void dump() { Reader->dump(); } + using NodeRef = typename GraphTraits::NodeRef; + using BT = typename std::remove_pointer::type; using InstructionT = typename afdo_detail::IRTraits::InstructionT; using BasicBlockT = typename afdo_detail::IRTraits::BasicBlockT; using BlockFrequencyInfoT = @@ -929,11 +931,11 @@ } } -template -void SampleProfileLoaderBaseImpl::applyProfi( +template +void SampleProfileLoaderBaseImpl::applyProfi( FunctionT &F, BlockEdgeMap &Successors, BlockWeightMap &SampleBlockWeights, BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights) { - auto Infer = SampleProfileInference(F, Successors, SampleBlockWeights); + auto Infer = SampleProfileInference(F, Successors, SampleBlockWeights); Infer.apply(BlockWeights, EdgeWeights); } @@ -1113,18 +1115,6 @@ return 0; } -template -void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( - FunctionT &F) { - DT.reset(new DominatorTree); - DT->recalculate(F); - - PDT.reset(new PostDominatorTree(F)); - - LI.reset(new LoopInfo); - LI->analyze(*DT); -} - #undef DEBUG_TYPE } // namespace llvm diff --git a/llvm/lib/CodeGen/MIRSampleProfile.cpp b/llvm/lib/CodeGen/MIRSampleProfile.cpp --- a/llvm/lib/CodeGen/MIRSampleProfile.cpp +++ b/llvm/lib/CodeGen/MIRSampleProfile.cpp @@ -140,7 +140,7 @@ } // namespace afdo_detail class MIRProfileLoader final - : public SampleProfileLoaderBaseImpl { + : public SampleProfileLoaderBaseImpl { public: void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT, MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI, @@ -195,8 +195,8 @@ }; template <> -void SampleProfileLoaderBaseImpl< - MachineBasicBlock>::computeDominanceAndLoopInfo(MachineFunction &F) {} +void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( + MachineFunction &F) {} void MIRProfileLoader::setBranchProbs(MachineFunction &F) { LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n"); diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -505,8 +505,7 @@ /// This pass reads profile data from the file specified by /// -sample-profile-file and annotates every affected function with the /// profile information found in that file. -class SampleProfileLoader final - : public SampleProfileLoaderBaseImpl { +class SampleProfileLoader final : public SampleProfileLoaderBaseImpl { public: SampleProfileLoader( StringRef Name, StringRef RemapName, ThinOrFullLTOPhase LTOPhase, @@ -637,6 +636,50 @@ }; } // end anonymous namespace +namespace llvm { +template <> +inline bool SampleProfileInference::isExit(const BasicBlock *BB) { + return succ_empty(BB); +} + +template <> +inline void SampleProfileInference::findUnlikelyJumps( + const std::vector &BasicBlocks, + BlockEdgeMap &Successors, FlowFunction &Func) { + for (auto &Jump : Func.Jumps) { + const auto *BB = BasicBlocks[Jump.Source]; + const auto *Succ = BasicBlocks[Jump.Target]; + const Instruction *TI = BB->getTerminator(); + // Check if a block ends with InvokeInst and mark non-taken branch unlikely. + // In that case block Succ should be a landing pad + if (Successors[BB].size() == 2 && Successors[BB].back() == Succ) { + if (isa(TI)) { + Jump.IsUnlikely = true; + } + } + const Instruction *SuccTI = Succ->getTerminator(); + // Check if the target block contains UnreachableInst and mark it unlikely + if (SuccTI->getNumSuccessors() == 0) { + if (isa(SuccTI)) { + Jump.IsUnlikely = true; + } + } + } +} + +template <> +void SampleProfileLoaderBaseImpl::computeDominanceAndLoopInfo( + Function &F) { + DT.reset(new DominatorTree); + DT->recalculate(F); + + PDT.reset(new PostDominatorTree(F)); + + LI.reset(new LoopInfo); + LI->analyze(*DT); +} +} // namespace llvm + ErrorOr SampleProfileLoader::getInstWeight(const Instruction &Inst) { if (FunctionSamples::ProfileIsProbeBased) return getProbeWeight(Inst);