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 #include "X86InstrInfo.h"
19 #include "X86Subtarget.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/TargetSchedule.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
31 #define DEBUG_TYPE "x86-pad-short-functions"
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)
54 bool runOnMachineFunction(MachineFunction &MF) override;
56 MachineFunctionProperties getRequiredProperties() const override {
57 return MachineFunctionProperties().set(
58 MachineFunctionProperties::Property::NoVRegs);
61 StringRef getPassName() const override {
62 return "X86 Atom pad short functions";
66 void findReturns(MachineBasicBlock *MBB,
67 unsigned int Cycles = 0);
69 bool cyclesUntilReturn(MachineBasicBlock *MBB,
70 unsigned int &Cycles);
72 void addPadding(MachineBasicBlock *MBB,
73 MachineBasicBlock::iterator &MBBI,
74 unsigned int NOOPsToAdd);
76 const unsigned int Threshold;
78 // ReturnBBs - Maps basic blocks that return to the minimum number of
79 // cycles until the return, starting from the entry block.
80 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
82 // VisitedBBs - Cache of previously visited BBs.
83 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
88 char PadShortFunc::ID = 0;
91 FunctionPass *llvm::createX86PadShortFunctions() {
92 return new PadShortFunc();
95 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
96 /// NOOP instructions before early exits.
97 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
98 if (skipFunction(MF.getFunction()))
101 if (MF.getFunction().optForSize())
104 if (!MF.getSubtarget<X86Subtarget>().padShortFunctions())
107 TSM.init(&MF.getSubtarget());
109 // Search through basic blocks and mark the ones that have early returns
112 findReturns(&MF.front());
114 bool MadeChange = false;
116 MachineBasicBlock *MBB;
117 unsigned int Cycles = 0;
119 // Pad the identified basic blocks with NOOPs
120 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
121 I != ReturnBBs.end(); ++I) {
125 if (Cycles < Threshold) {
126 // BB ends in a return. Skip over any DBG_VALUE instructions
127 // trailing the terminator.
128 assert(MBB->size() > 0 &&
129 "Basic block should contain at least a RET but is empty");
130 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
132 while (ReturnLoc->isDebugInstr())
134 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
135 "Basic block does not end with RET");
137 addPadding(MBB, ReturnLoc, Threshold - Cycles);
146 /// findReturn - Starting at MBB, follow control flow and add all
147 /// basic blocks that contain a return to ReturnBBs.
148 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
149 // If this BB has a return, note how many cycles it takes to get there.
150 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
151 if (Cycles >= Threshold)
155 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
159 // Follow branches in BB and look for returns
160 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
161 I != MBB->succ_end(); ++I) {
164 findReturns(*I, Cycles);
168 /// cyclesUntilReturn - return true if the MBB has a return instruction,
169 /// and return false otherwise.
170 /// Cycles will be incremented by the number of cycles taken to reach the
171 /// return or the end of the BB, whichever occurs first.
172 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
173 unsigned int &Cycles) {
174 // Return cached result if BB was previously visited
175 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
176 = VisitedBBs.find(MBB);
177 if (it != VisitedBBs.end()) {
178 VisitedBBInfo BBInfo = it->second;
179 Cycles += BBInfo.Cycles;
180 return BBInfo.HasReturn;
183 unsigned int CyclesToEnd = 0;
185 for (MachineInstr &MI : *MBB) {
186 // Mark basic blocks with a return instruction. Calls to other
187 // functions do not count because the called function will be padded,
189 if (MI.isReturn() && !MI.isCall()) {
190 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
191 Cycles += CyclesToEnd;
195 CyclesToEnd += TSM.computeInstrLatency(&MI);
198 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
199 Cycles += CyclesToEnd;
203 /// addPadding - Add the given number of NOOP instructions to the function
204 /// just prior to the return at MBBI
205 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
206 MachineBasicBlock::iterator &MBBI,
207 unsigned int NOOPsToAdd) {
208 DebugLoc DL = MBBI->getDebugLoc();
209 unsigned IssueWidth = TSM.getIssueWidth();
211 for (unsigned i = 0, e = IssueWidth * NOOPsToAdd; i != e; ++i)
212 BuildMI(*MBB, MBBI, DL, TSM.getInstrInfo()->get(X86::NOOP));