1 //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "x86-pad-short-functions"
20 #include "X86InstrInfo.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetInstrInfo.h"
33 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
36 struct VisitedBBInfo {
37 // HasReturn - Whether the BB contains a return instruction
40 // Cycles - Number of cycles until return if HasReturn is true, otherwise
41 // number of cycles until end of the BB
44 VisitedBBInfo() : HasReturn(false), Cycles(0) {}
45 VisitedBBInfo(bool HasReturn, unsigned int Cycles)
46 : HasReturn(HasReturn), Cycles(Cycles) {}
49 struct PadShortFunc : public MachineFunctionPass {
51 PadShortFunc() : MachineFunctionPass(ID)
52 , Threshold(4), TM(0), TII(0) {}
54 bool runOnMachineFunction(MachineFunction &MF) override;
56 const char *getPassName() const override {
57 return "X86 Atom pad short functions";
61 void findReturns(MachineBasicBlock *MBB,
62 unsigned int Cycles = 0);
64 bool cyclesUntilReturn(MachineBasicBlock *MBB,
65 unsigned int &Cycles);
67 void addPadding(MachineBasicBlock *MBB,
68 MachineBasicBlock::iterator &MBBI,
69 unsigned int NOOPsToAdd);
71 const unsigned int Threshold;
73 // ReturnBBs - Maps basic blocks that return to the minimum number of
74 // cycles until the return, starting from the entry block.
75 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
77 // VisitedBBs - Cache of previously visited BBs.
78 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
80 const TargetMachine *TM;
81 const TargetInstrInfo *TII;
84 char PadShortFunc::ID = 0;
87 FunctionPass *llvm::createX86PadShortFunctions() {
88 return new PadShortFunc();
91 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
92 /// NOOP instructions before early exits.
93 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
94 const AttributeSet &FnAttrs = MF.getFunction()->getAttributes();
95 if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
96 Attribute::OptimizeForSize) ||
97 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
98 Attribute::MinSize)) {
102 TM = &MF.getTarget();
103 TII = TM->getInstrInfo();
105 // Search through basic blocks and mark the ones that have early returns
108 findReturns(MF.begin());
110 bool MadeChange = false;
112 MachineBasicBlock *MBB;
113 unsigned int Cycles = 0;
115 // Pad the identified basic blocks with NOOPs
116 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
117 I != ReturnBBs.end(); ++I) {
121 if (Cycles < Threshold) {
122 // BB ends in a return. Skip over any DBG_VALUE instructions
123 // trailing the terminator.
124 assert(MBB->size() > 0 &&
125 "Basic block should contain at least a RET but is empty");
126 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
128 while (ReturnLoc->isDebugValue())
130 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
131 "Basic block does not end with RET");
133 addPadding(MBB, ReturnLoc, Threshold - Cycles);
142 /// findReturn - Starting at MBB, follow control flow and add all
143 /// basic blocks that contain a return to ReturnBBs.
144 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
145 // If this BB has a return, note how many cycles it takes to get there.
146 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
147 if (Cycles >= Threshold)
151 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
155 // Follow branches in BB and look for returns
156 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
157 I != MBB->succ_end(); ++I) {
160 findReturns(*I, Cycles);
164 /// cyclesUntilReturn - return true if the MBB has a return instruction,
165 /// and return false otherwise.
166 /// Cycles will be incremented by the number of cycles taken to reach the
167 /// return or the end of the BB, whichever occurs first.
168 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
169 unsigned int &Cycles) {
170 // Return cached result if BB was previously visited
171 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
172 = VisitedBBs.find(MBB);
173 if (it != VisitedBBs.end()) {
174 VisitedBBInfo BBInfo = it->second;
175 Cycles += BBInfo.Cycles;
176 return BBInfo.HasReturn;
179 unsigned int CyclesToEnd = 0;
181 for (MachineBasicBlock::iterator MBBI = MBB->begin();
182 MBBI != MBB->end(); ++MBBI) {
183 MachineInstr *MI = MBBI;
184 // Mark basic blocks with a return instruction. Calls to other
185 // functions do not count because the called function will be padded,
187 if (MI->isReturn() && !MI->isCall()) {
188 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
189 Cycles += CyclesToEnd;
193 CyclesToEnd += TII->getInstrLatency(TM->getInstrItineraryData(), MI);
196 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
197 Cycles += CyclesToEnd;
201 /// addPadding - Add the given number of NOOP instructions to the function
202 /// just prior to the return at MBBI
203 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
204 MachineBasicBlock::iterator &MBBI,
205 unsigned int NOOPsToAdd) {
206 DebugLoc DL = MBBI->getDebugLoc();
208 while (NOOPsToAdd-- > 0) {
209 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
210 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));