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 //===----------------------------------------------------------------------===//
19 #include "X86InstrInfo.h"
20 #include "X86Subtarget.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 #define DEBUG_TYPE "x86-pad-short-functions"
35 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
38 struct VisitedBBInfo {
39 // HasReturn - Whether the BB contains a return instruction
42 // Cycles - Number of cycles until return if HasReturn is true, otherwise
43 // number of cycles until end of the BB
46 VisitedBBInfo() : HasReturn(false), Cycles(0) {}
47 VisitedBBInfo(bool HasReturn, unsigned int Cycles)
48 : HasReturn(HasReturn), Cycles(Cycles) {}
51 struct PadShortFunc : public MachineFunctionPass {
53 PadShortFunc() : MachineFunctionPass(ID)
54 , Threshold(4), STI(nullptr), TII(nullptr) {}
56 bool runOnMachineFunction(MachineFunction &MF) override;
58 const char *getPassName() const override {
59 return "X86 Atom pad short functions";
63 void findReturns(MachineBasicBlock *MBB,
64 unsigned int Cycles = 0);
66 bool cyclesUntilReturn(MachineBasicBlock *MBB,
67 unsigned int &Cycles);
69 void addPadding(MachineBasicBlock *MBB,
70 MachineBasicBlock::iterator &MBBI,
71 unsigned int NOOPsToAdd);
73 const unsigned int Threshold;
75 // ReturnBBs - Maps basic blocks that return to the minimum number of
76 // cycles until the return, starting from the entry block.
77 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
79 // VisitedBBs - Cache of previously visited BBs.
80 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
82 const X86Subtarget *STI;
83 const TargetInstrInfo *TII;
86 char PadShortFunc::ID = 0;
89 FunctionPass *llvm::createX86PadShortFunctions() {
90 return new PadShortFunc();
93 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
94 /// NOOP instructions before early exits.
95 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
96 if (MF.getFunction()->optForSize()) {
100 STI = &MF.getSubtarget<X86Subtarget>();
101 if (!STI->padShortFunctions())
104 TII = STI->getInstrInfo();
106 // Search through basic blocks and mark the ones that have early returns
109 findReturns(&MF.front());
111 bool MadeChange = false;
113 MachineBasicBlock *MBB;
114 unsigned int Cycles = 0;
116 // Pad the identified basic blocks with NOOPs
117 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
118 I != ReturnBBs.end(); ++I) {
122 if (Cycles < Threshold) {
123 // BB ends in a return. Skip over any DBG_VALUE instructions
124 // trailing the terminator.
125 assert(MBB->size() > 0 &&
126 "Basic block should contain at least a RET but is empty");
127 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
129 while (ReturnLoc->isDebugValue())
131 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
132 "Basic block does not end with RET");
134 addPadding(MBB, ReturnLoc, Threshold - Cycles);
143 /// findReturn - Starting at MBB, follow control flow and add all
144 /// basic blocks that contain a return to ReturnBBs.
145 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
146 // If this BB has a return, note how many cycles it takes to get there.
147 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
148 if (Cycles >= Threshold)
152 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
156 // Follow branches in BB and look for returns
157 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
158 I != MBB->succ_end(); ++I) {
161 findReturns(*I, Cycles);
165 /// cyclesUntilReturn - return true if the MBB has a return instruction,
166 /// and return false otherwise.
167 /// Cycles will be incremented by the number of cycles taken to reach the
168 /// return or the end of the BB, whichever occurs first.
169 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
170 unsigned int &Cycles) {
171 // Return cached result if BB was previously visited
172 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
173 = VisitedBBs.find(MBB);
174 if (it != VisitedBBs.end()) {
175 VisitedBBInfo BBInfo = it->second;
176 Cycles += BBInfo.Cycles;
177 return BBInfo.HasReturn;
180 unsigned int CyclesToEnd = 0;
182 for (MachineBasicBlock::iterator MBBI = MBB->begin();
183 MBBI != MBB->end(); ++MBBI) {
184 MachineInstr *MI = MBBI;
185 // Mark basic blocks with a return instruction. Calls to other
186 // functions do not count because the called function will be padded,
188 if (MI->isReturn() && !MI->isCall()) {
189 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
190 Cycles += CyclesToEnd;
194 CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI);
197 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
198 Cycles += CyclesToEnd;
202 /// addPadding - Add the given number of NOOP instructions to the function
203 /// just prior to the return at MBBI
204 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
205 MachineBasicBlock::iterator &MBBI,
206 unsigned int NOOPsToAdd) {
207 DebugLoc DL = MBBI->getDebugLoc();
209 while (NOOPsToAdd-- > 0) {
210 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
211 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));