31#define DEBUG_TYPE "x86-pad-short-functions"
33STATISTIC(NumBBsPadded,
"Number of basic blocks padded");
36 struct VisitedBBInfo {
38 bool HasReturn =
false;
42 unsigned int Cycles = 0;
44 VisitedBBInfo() =
default;
45 VisitedBBInfo(
bool HasReturn,
unsigned int Cycles)
46 : HasReturn(HasReturn), Cycles(Cycles) {}
64 MachineFunctionProperties::Property::NoVRegs);
68 return "X86 Atom pad short functions";
73 unsigned int Cycles = 0);
76 unsigned int &Cycles);
80 unsigned int NOOPsToAdd);
82 const unsigned int Threshold = 4;
94 char PadShortFunc::ID = 0;
98 return new PadShortFunc();
116 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
117 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
118 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
124 findReturns(&MF.
front());
126 bool MadeChange =
false;
129 for (
const auto &ReturnBB : ReturnBBs) {
131 unsigned Cycles = ReturnBB.second;
136 if (Cycles < Threshold) {
140 "Basic block should contain at least a RET but is empty");
143 while (ReturnLoc->isDebugInstr())
145 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
146 "Basic block does not end with RET");
162 if (Cycles >= Threshold)
166 ReturnBBs[
MBB] = std::max(ReturnBBs[
MBB], Cycles);
173 findReturns(Succ, Cycles);
181 unsigned int &Cycles) {
185 if (it != VisitedBBs.end()) {
186 VisitedBBInfo BBInfo = it->second;
187 Cycles += BBInfo.Cycles;
188 return BBInfo.HasReturn;
191 unsigned int CyclesToEnd = 0;
197 if (
MI.isReturn() && !
MI.isCall()) {
198 VisitedBBs[
MBB] = VisitedBBInfo(
true, CyclesToEnd);
199 Cycles += CyclesToEnd;
203 CyclesToEnd += TSM.computeInstrLatency(&
MI);
206 VisitedBBs[
MBB] = VisitedBBInfo(
false, CyclesToEnd);
207 Cycles += CyclesToEnd;
215 unsigned int NOOPsToAdd) {
217 unsigned IssueWidth = TSM.getIssueWidth();
219 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.