#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 <class BlockT> struct TypeMap {};
-template <> struct TypeMap<BasicBlock> {
- using BasicBlockT = BasicBlock;
- using FunctionT = Function;
-};
-template <> struct TypeMap<MachineBasicBlock> {
- using BasicBlockT = MachineBasicBlock;
- using FunctionT = MachineFunction;
-};
-
-} // end namespace afdo_detail
-
struct FlowJump;
/// A wrapper of a binary basic block.
void applyFlowInference(FlowFunction &Func);
/// Sample profile inference pass.
-template <typename BT> class SampleProfileInference {
+template <typename FT> class SampleProfileInference {
public:
- using BasicBlockT = typename afdo_detail::TypeMap<BT>::BasicBlockT;
- using FunctionT = typename afdo_detail::TypeMap<BT>::FunctionT;
+ using NodeRef = typename GraphTraits<FT *>::NodeRef;
+ using BasicBlockT = typename std::remove_pointer<NodeRef>::type;
+ using FunctionT = FT;
using Edge = std::pair<const BasicBlockT *, const BasicBlockT *>;
using BlockWeightMap = DenseMap<const BasicBlockT *, uint64_t>;
using EdgeWeightMap = DenseMap<Edge, uint64_t>;
const std::vector<const BasicBlockT *> &BasicBlocks,
BlockEdgeMap &Successors, FlowFunction &Func) {}
-template <>
-inline void SampleProfileInference<BasicBlock>::findUnlikelyJumps(
- const std::vector<const BasicBlockT *> &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<InvokeInst>(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<UnreachableInst>(SuccTI)) {
- Jump.IsUnlikely = true;
- }
- }
- }
-}
-
template <typename BT>
inline bool SampleProfileInference<BT>::isExit(const BasicBlockT *BB) {
return BB->succ_empty();
}
-template <>
-inline bool SampleProfileInference<BasicBlock>::isExit(const BasicBlock *BB) {
- return succ_empty(BB);
-}
-
} // end namespace llvm
#endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILEINFERENCE_H
extern cl::opt<bool> SampleProfileUseProfi;
-template <typename BT> class SampleProfileLoaderBaseImpl {
+template <typename FT> class SampleProfileLoaderBaseImpl {
public:
SampleProfileLoaderBaseImpl(std::string Name, std::string RemapName,
IntrusiveRefCntPtr<vfs::FileSystem> FS)
: Filename(Name), RemappingFilename(RemapName), FS(std::move(FS)) {}
void dump() { Reader->dump(); }
+ using NodeRef = typename GraphTraits<FT *>::NodeRef;
+ using BT = typename std::remove_pointer<NodeRef>::type;
using InstructionT = typename afdo_detail::IRTraits<BT>::InstructionT;
using BasicBlockT = typename afdo_detail::IRTraits<BT>::BasicBlockT;
using BlockFrequencyInfoT =
}
}
-template <typename BT>
-void SampleProfileLoaderBaseImpl<BT>::applyProfi(
+template <typename FT>
+void SampleProfileLoaderBaseImpl<FT>::applyProfi(
FunctionT &F, BlockEdgeMap &Successors, BlockWeightMap &SampleBlockWeights,
BlockWeightMap &BlockWeights, EdgeWeightMap &EdgeWeights) {
- auto Infer = SampleProfileInference<BT>(F, Successors, SampleBlockWeights);
+ auto Infer = SampleProfileInference<FT>(F, Successors, SampleBlockWeights);
Infer.apply(BlockWeights, EdgeWeights);
}
return 0;
}
-template <typename BT>
-void SampleProfileLoaderBaseImpl<BT>::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
} // namespace afdo_detail
class MIRProfileLoader final
- : public SampleProfileLoaderBaseImpl<MachineBasicBlock> {
+ : public SampleProfileLoaderBaseImpl<MachineFunction> {
public:
void setInitVals(MachineDominatorTree *MDT, MachinePostDominatorTree *MPDT,
MachineLoopInfo *MLI, MachineBlockFrequencyInfo *MBFI,
};
template <>
-void SampleProfileLoaderBaseImpl<
- MachineBasicBlock>::computeDominanceAndLoopInfo(MachineFunction &F) {}
+void SampleProfileLoaderBaseImpl<MachineFunction>::computeDominanceAndLoopInfo(
+ MachineFunction &F) {}
void MIRProfileLoader::setBranchProbs(MachineFunction &F) {
LLVM_DEBUG(dbgs() << "\nPropagation complete. Setting branch probs\n");
/// 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<BasicBlock> {
+class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
public:
SampleProfileLoader(
StringRef Name, StringRef RemapName, ThinOrFullLTOPhase LTOPhase,
};
} // end anonymous namespace
+namespace llvm {
+template <>
+inline bool SampleProfileInference<Function>::isExit(const BasicBlock *BB) {
+ return succ_empty(BB);
+}
+
+template <>
+inline void SampleProfileInference<Function>::findUnlikelyJumps(
+ const std::vector<const BasicBlockT *> &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<InvokeInst>(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<UnreachableInst>(SuccTI)) {
+ Jump.IsUnlikely = true;
+ }
+ }
+ }
+}
+
+template <>
+void SampleProfileLoaderBaseImpl<Function>::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<uint64_t> SampleProfileLoader::getInstWeight(const Instruction &Inst) {
if (FunctionSamples::ProfileIsProbeBased)
return getProbeWeight(Inst);