LLVM  7.0.0svn
X86PadShortFunction.cpp
Go to the documentation of this file.
1 //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the pass which will pad short functions to prevent
11 // a stall if a function returns before the return address is ready. This
12 // is needed for some Intel Atom processors.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 
17 #include "X86.h"
18 #include "X86InstrInfo.h"
19 #include "X86Subtarget.h"
20 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Support/Debug.h"
28 
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "x86-pad-short-functions"
32 
33 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
34 
35 namespace {
36  struct VisitedBBInfo {
37  // HasReturn - Whether the BB contains a return instruction
38  bool HasReturn;
39 
40  // Cycles - Number of cycles until return if HasReturn is true, otherwise
41  // number of cycles until end of the BB
42  unsigned int Cycles;
43 
44  VisitedBBInfo() : HasReturn(false), Cycles(0) {}
45  VisitedBBInfo(bool HasReturn, unsigned int Cycles)
46  : HasReturn(HasReturn), Cycles(Cycles) {}
47  };
48 
49  struct PadShortFunc : public MachineFunctionPass {
50  static char ID;
51  PadShortFunc() : MachineFunctionPass(ID)
52  , Threshold(4), STI(nullptr), TII(nullptr) {}
53 
54  bool runOnMachineFunction(MachineFunction &MF) override;
55 
56  MachineFunctionProperties getRequiredProperties() const override {
59  }
60 
61  StringRef getPassName() const override {
62  return "X86 Atom pad short functions";
63  }
64 
65  private:
66  void findReturns(MachineBasicBlock *MBB,
67  unsigned int Cycles = 0);
68 
69  bool cyclesUntilReturn(MachineBasicBlock *MBB,
70  unsigned int &Cycles);
71 
72  void addPadding(MachineBasicBlock *MBB,
74  unsigned int NOOPsToAdd);
75 
76  const unsigned int Threshold;
77 
78  // ReturnBBs - Maps basic blocks that return to the minimum number of
79  // cycles until the return, starting from the entry block.
81 
82  // VisitedBBs - Cache of previously visited BBs.
84 
85  const X86Subtarget *STI;
86  const TargetInstrInfo *TII;
87  };
88 
89  char PadShortFunc::ID = 0;
90 }
91 
93  return new PadShortFunc();
94 }
95 
96 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
97 /// NOOP instructions before early exits.
98 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
99  if (skipFunction(MF.getFunction()))
100  return false;
101 
102  if (MF.getFunction().optForSize()) {
103  return false;
104  }
105 
106  STI = &MF.getSubtarget<X86Subtarget>();
107  if (!STI->padShortFunctions())
108  return false;
109 
110  TII = STI->getInstrInfo();
111 
112  // Search through basic blocks and mark the ones that have early returns
113  ReturnBBs.clear();
114  VisitedBBs.clear();
115  findReturns(&MF.front());
116 
117  bool MadeChange = false;
118 
119  MachineBasicBlock *MBB;
120  unsigned int Cycles = 0;
121 
122  // Pad the identified basic blocks with NOOPs
124  I != ReturnBBs.end(); ++I) {
125  MBB = I->first;
126  Cycles = I->second;
127 
128  if (Cycles < Threshold) {
129  // BB ends in a return. Skip over any DBG_VALUE instructions
130  // trailing the terminator.
131  assert(MBB->size() > 0 &&
132  "Basic block should contain at least a RET but is empty");
133  MachineBasicBlock::iterator ReturnLoc = --MBB->end();
134 
135  while (ReturnLoc->isDebugValue())
136  --ReturnLoc;
137  assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
138  "Basic block does not end with RET");
139 
140  addPadding(MBB, ReturnLoc, Threshold - Cycles);
141  NumBBsPadded++;
142  MadeChange = true;
143  }
144  }
145 
146  return MadeChange;
147 }
148 
149 /// findReturn - Starting at MBB, follow control flow and add all
150 /// basic blocks that contain a return to ReturnBBs.
151 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
152  // If this BB has a return, note how many cycles it takes to get there.
153  bool hasReturn = cyclesUntilReturn(MBB, Cycles);
154  if (Cycles >= Threshold)
155  return;
156 
157  if (hasReturn) {
158  ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
159  return;
160  }
161 
162  // Follow branches in BB and look for returns
164  I != MBB->succ_end(); ++I) {
165  if (*I == MBB)
166  continue;
167  findReturns(*I, Cycles);
168  }
169 }
170 
171 /// cyclesUntilReturn - return true if the MBB has a return instruction,
172 /// and return false otherwise.
173 /// Cycles will be incremented by the number of cycles taken to reach the
174 /// return or the end of the BB, whichever occurs first.
175 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
176  unsigned int &Cycles) {
177  // Return cached result if BB was previously visited
179  = VisitedBBs.find(MBB);
180  if (it != VisitedBBs.end()) {
181  VisitedBBInfo BBInfo = it->second;
182  Cycles += BBInfo.Cycles;
183  return BBInfo.HasReturn;
184  }
185 
186  unsigned int CyclesToEnd = 0;
187 
188  for (MachineInstr &MI : *MBB) {
189  // Mark basic blocks with a return instruction. Calls to other
190  // functions do not count because the called function will be padded,
191  // if necessary.
192  if (MI.isReturn() && !MI.isCall()) {
193  VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
194  Cycles += CyclesToEnd;
195  return true;
196  }
197 
198  CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI);
199  }
200 
201  VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
202  Cycles += CyclesToEnd;
203  return false;
204 }
205 
206 /// addPadding - Add the given number of NOOP instructions to the function
207 /// just prior to the return at MBBI
210  unsigned int NOOPsToAdd) {
211  DebugLoc DL = MBBI->getDebugLoc();
212 
213  while (NOOPsToAdd-- > 0) {
214  BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
215  BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
216  }
217 }
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
STATISTIC(NumFunctions, "Total number of functions")
A debug info location.
Definition: DebugLoc.h:34
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
FunctionPass * createX86PadShortFunctions()
Return a pass that pads short functions with NOOPs.
unsigned getInstrLatency(const InstrItineraryData *ItinData, const MachineInstr &MI, unsigned *PredCost=nullptr) const override
Compute the instruction latency of a given instruction.
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:576
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
const MachineBasicBlock & front() const
static bool hasReturn(const MachineBasicBlock &MBB)
Returns true if MBB contains an instruction that returns.
static void addPadding(BinaryStreamWriter &Writer)
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:60
#define I(x, y, z)
Definition: MD5.cpp:58
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::vector< MachineBasicBlock * >::iterator succ_iterator
Properties which a MachineFunction may have at a given point in time.