33#define DEBUG_TYPE "x86-pad-short-functions"
35STATISTIC(NumBBsPadded,
"Number of basic blocks padded");
38 struct VisitedBBInfo {
40 bool HasReturn =
false;
44 unsigned int Cycles = 0;
46 VisitedBBInfo() =
default;
47 VisitedBBInfo(
bool HasReturn,
unsigned int Cycles)
48 : HasReturn(HasReturn), Cycles(Cycles) {}
66 MachineFunctionProperties::Property::NoVRegs);
70 return "X86 Atom pad short functions";
75 unsigned int Cycles = 0);
78 unsigned int &Cycles);
82 unsigned int NOOPsToAdd);
84 const unsigned int Threshold = 4;
96 char PadShortFunc::ID = 0;
100 return new PadShortFunc();
118 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
119 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
120 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
126 findReturns(&MF.
front());
128 bool MadeChange =
false;
131 for (
const auto &ReturnBB : ReturnBBs) {
133 unsigned Cycles = ReturnBB.second;
140 if (Cycles < Threshold) {
144 "Basic block should contain at least a RET but is empty");
147 while (ReturnLoc->isDebugInstr())
149 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
150 "Basic block does not end with RET");
166 if (Cycles >= Threshold)
170 ReturnBBs[
MBB] = std::max(ReturnBBs[
MBB], Cycles);
177 findReturns(Succ, Cycles);
185 unsigned int &Cycles) {
189 if (it != VisitedBBs.end()) {
190 VisitedBBInfo BBInfo = it->second;
191 Cycles += BBInfo.Cycles;
192 return BBInfo.HasReturn;
195 unsigned int CyclesToEnd = 0;
201 if (
MI.isReturn() && !
MI.isCall()) {
202 VisitedBBs[
MBB] = VisitedBBInfo(
true, CyclesToEnd);
203 Cycles += CyclesToEnd;
207 CyclesToEnd += TSM.computeInstrLatency(&
MI);
210 VisitedBBs[
MBB] = VisitedBBInfo(
false, CyclesToEnd);
211 Cycles += CyclesToEnd;
219 unsigned int NOOPsToAdd) {
221 unsigned IssueWidth = TSM.getIssueWidth();
223 for (
unsigned i = 0, e = IssueWidth * NOOPsToAdd; i !=
e; ++i)
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++ -*–===//
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
iterator find(const_arg_type_t< KeyT > Val)
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
This is an alternative analysis pass to MachineBlockFrequencyInfo.
iterator_range< succ_iterator > successors()
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
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
Representation of each machine instruction.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
StringRef - Represent a constant reference to a string, i.e.
Provide an instruction scheduling machine model to CodeGen passes.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
FunctionPass * createX86PadShortFunctions()
Return a pass that pads short functions with NOOPs.