35#define DEBUG_TYPE "x86-pad-short-functions"
37STATISTIC(NumBBsPadded,
"Number of basic blocks padded");
40 struct VisitedBBInfo {
42 bool HasReturn =
false;
46 unsigned int Cycles = 0;
48 VisitedBBInfo() =
default;
49 VisitedBBInfo(
bool HasReturn,
unsigned int Cycles)
50 : HasReturn(HasReturn), Cycles(Cycles) {}
55 X86PadShortFunctionsLegacy() : MachineFunctionPass(ID) {}
59 void getAnalysisUsage(AnalysisUsage &AU)
const override {
61 AU.
addRequired<LazyMachineBlockFrequencyInfoPass>();
66 MachineFunctionProperties getRequiredProperties()
const override {
67 return MachineFunctionProperties().setNoVRegs();
70 StringRef getPassName()
const override {
71 return "X86 Atom pad short functions";
75 class X86PadShortFunctionsImpl {
77 bool runOnMachineFunction(
79 llvm::function_ref<MachineBlockFrequencyInfo *()> GetMBFI);
82 void findReturns(MachineBasicBlock *
MBB,
83 unsigned int Cycles = 0);
85 bool cyclesUntilReturn(MachineBasicBlock *
MBB,
86 unsigned int &Cycles);
90 unsigned int NOOPsToAdd);
92 const unsigned int Threshold = 4;
96 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
99 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
101 TargetSchedModel TSM;
104 char X86PadShortFunctionsLegacy::ID = 0;
108 return new X86PadShortFunctionsLegacy();
111bool X86PadShortFunctionsLegacy::runOnMachineFunction(
MachineFunction &MF) {
115 X86PadShortFunctionsImpl Impl;
116 return Impl.runOnMachineFunction(
118 [&]() {
return &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); },
120 return &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
127 X86PadShortFunctionsImpl Impl;
128 bool Changed = Impl.runOnMachineFunction(
132 .getCachedResult<ProfileSummaryAnalysis>(
144bool X86PadShortFunctionsImpl::runOnMachineFunction(
165 findReturns(&MF.
front());
167 bool MadeChange =
false;
170 for (
const auto &ReturnBB : ReturnBBs) {
172 unsigned Cycles = ReturnBB.second;
177 if (Cycles < Threshold) {
181 "Basic block should contain at least a RET but is empty");
184 while (ReturnLoc->isDebugInstr())
186 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
187 "Basic block does not end with RET");
200void X86PadShortFunctionsImpl::findReturns(MachineBasicBlock *
MBB,
201 unsigned int Cycles) {
204 if (Cycles >= Threshold)
208 unsigned int &NumCycles = ReturnBBs[
MBB];
209 NumCycles = std::max(NumCycles, Cycles);
216 findReturns(Succ, Cycles);
223bool X86PadShortFunctionsImpl::cyclesUntilReturn(MachineBasicBlock *
MBB,
224 unsigned int &Cycles) {
228 VisitedBBInfo BBInfo = It->second;
229 Cycles += BBInfo.Cycles;
230 return BBInfo.HasReturn;
233 unsigned int CyclesToEnd = 0;
235 for (MachineInstr &
MI : *
MBB) {
239 if (
MI.isReturn() && !
MI.isCall()) {
240 It->second = VisitedBBInfo(
true, CyclesToEnd);
241 Cycles += CyclesToEnd;
245 CyclesToEnd += TSM.computeInstrLatency(&
MI);
248 It->second = VisitedBBInfo(
false, CyclesToEnd);
249 Cycles += CyclesToEnd;
255void X86PadShortFunctionsImpl::addPadding(MachineBasicBlock *
MBB,
257 unsigned int NOOPsToAdd) {
261 for (
unsigned i = 0, e = IssueWidth * NOOPsToAdd; i !=
e; ++i)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static void addPadding(BinaryStreamWriter &Writer)
static bool hasReturn(const MachineBasicBlock &MBB)
Returns true if MBB contains an instruction that returns.
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Module * getParent()
Get the module that this global value is contained inside of...
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
iterator_range< succ_iterator > successors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo getter.
unsigned getIssueWidth() const
Maximum number of micro-ops that may be scheduled per cycle.
LLVM_ABI void init(const TargetSubtargetInfo *TSInfo, bool EnableSModel=true, bool EnableSItins=true)
Initialize the machine model for instruction scheduling.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createX86PadShortFunctionsLegacyPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.