blob: 76b318eca3c8e593a076e309efdf30d31b929570 [file] [log] [blame]
Preston Gurda01daac2013-01-08 18:27:24 +00001//===-------- 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#include <algorithm>
17
Preston Gurda01daac2013-01-08 18:27:24 +000018#include "X86.h"
19#include "X86InstrInfo.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/Function.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Target/TargetInstrInfo.h"
29
30using namespace llvm;
31
Chandler Carruth84e68b22014-04-22 02:41:26 +000032#define DEBUG_TYPE "x86-pad-short-functions"
33
Preston Gurda01daac2013-01-08 18:27:24 +000034STATISTIC(NumBBsPadded, "Number of basic blocks padded");
35
36namespace {
Preston Gurd99c69902013-01-11 22:06:56 +000037 struct VisitedBBInfo {
38 // HasReturn - Whether the BB contains a return instruction
39 bool HasReturn;
40
41 // Cycles - Number of cycles until return if HasReturn is true, otherwise
42 // number of cycles until end of the BB
43 unsigned int Cycles;
44
45 VisitedBBInfo() : HasReturn(false), Cycles(0) {}
46 VisitedBBInfo(bool HasReturn, unsigned int Cycles)
47 : HasReturn(HasReturn), Cycles(Cycles) {}
48 };
49
Preston Gurda01daac2013-01-08 18:27:24 +000050 struct PadShortFunc : public MachineFunctionPass {
51 static char ID;
52 PadShortFunc() : MachineFunctionPass(ID)
53 , Threshold(4), TM(0), TII(0) {}
54
Craig Topper2d9361e2014-03-09 07:44:38 +000055 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurda01daac2013-01-08 18:27:24 +000056
Craig Topper2d9361e2014-03-09 07:44:38 +000057 const char *getPassName() const override {
Preston Gurda01daac2013-01-08 18:27:24 +000058 return "X86 Atom pad short functions";
59 }
60
61 private:
62 void findReturns(MachineBasicBlock *MBB,
63 unsigned int Cycles = 0);
64
65 bool cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +000066 unsigned int &Cycles);
Preston Gurda01daac2013-01-08 18:27:24 +000067
68 void addPadding(MachineBasicBlock *MBB,
69 MachineBasicBlock::iterator &MBBI,
70 unsigned int NOOPsToAdd);
71
72 const unsigned int Threshold;
Preston Gurd99c69902013-01-11 22:06:56 +000073
74 // ReturnBBs - Maps basic blocks that return to the minimum number of
75 // cycles until the return, starting from the entry block.
Preston Gurda01daac2013-01-08 18:27:24 +000076 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
77
Preston Gurd99c69902013-01-11 22:06:56 +000078 // VisitedBBs - Cache of previously visited BBs.
79 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
80
Preston Gurda01daac2013-01-08 18:27:24 +000081 const TargetMachine *TM;
82 const TargetInstrInfo *TII;
83 };
84
85 char PadShortFunc::ID = 0;
86}
87
88FunctionPass *llvm::createX86PadShortFunctions() {
89 return new PadShortFunc();
90}
91
92/// runOnMachineFunction - Loop over all of the basic blocks, inserting
93/// NOOP instructions before early exits.
94bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
Preston Gurd99c69902013-01-11 22:06:56 +000095 const AttributeSet &FnAttrs = MF.getFunction()->getAttributes();
96 if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
97 Attribute::OptimizeForSize) ||
98 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
99 Attribute::MinSize)) {
Preston Gurda01daac2013-01-08 18:27:24 +0000100 return false;
Preston Gurd99c69902013-01-11 22:06:56 +0000101 }
Preston Gurda01daac2013-01-08 18:27:24 +0000102
103 TM = &MF.getTarget();
104 TII = TM->getInstrInfo();
105
106 // Search through basic blocks and mark the ones that have early returns
107 ReturnBBs.clear();
Preston Gurd99c69902013-01-11 22:06:56 +0000108 VisitedBBs.clear();
Preston Gurda01daac2013-01-08 18:27:24 +0000109 findReturns(MF.begin());
110
111 bool MadeChange = false;
112
Preston Gurda01daac2013-01-08 18:27:24 +0000113 MachineBasicBlock *MBB;
114 unsigned int Cycles = 0;
Preston Gurda01daac2013-01-08 18:27:24 +0000115
116 // Pad the identified basic blocks with NOOPs
117 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
118 I != ReturnBBs.end(); ++I) {
119 MBB = I->first;
120 Cycles = I->second;
121
122 if (Cycles < Threshold) {
Preston Gurd99c69902013-01-11 22:06:56 +0000123 // 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();
128
129 while (ReturnLoc->isDebugValue())
130 --ReturnLoc;
131 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
132 "Basic block does not end with RET");
Preston Gurda01daac2013-01-08 18:27:24 +0000133
134 addPadding(MBB, ReturnLoc, Threshold - Cycles);
135 NumBBsPadded++;
136 MadeChange = true;
137 }
138 }
139
140 return MadeChange;
141}
142
143/// findReturn - Starting at MBB, follow control flow and add all
144/// basic blocks that contain a return to ReturnBBs.
145void 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)
149 return;
150
151 if (hasReturn) {
152 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
153 return;
154 }
155
156 // Follow branches in BB and look for returns
157 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
Preston Gurd99c69902013-01-11 22:06:56 +0000158 I != MBB->succ_end(); ++I) {
159 if (*I == MBB)
160 continue;
Preston Gurda01daac2013-01-08 18:27:24 +0000161 findReturns(*I, Cycles);
162 }
163}
164
Preston Gurd99c69902013-01-11 22:06:56 +0000165/// cyclesUntilReturn - return true if the MBB has a return instruction,
166/// and return false otherwise.
Preston Gurda01daac2013-01-08 18:27:24 +0000167/// Cycles will be incremented by the number of cycles taken to reach the
168/// return or the end of the BB, whichever occurs first.
169bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +0000170 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;
178 }
179
180 unsigned int CyclesToEnd = 0;
181
Preston Gurda01daac2013-01-08 18:27:24 +0000182 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,
187 // if necessary.
188 if (MI->isReturn() && !MI->isCall()) {
Preston Gurd99c69902013-01-11 22:06:56 +0000189 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
190 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000191 return true;
192 }
193
Preston Gurd99c69902013-01-11 22:06:56 +0000194 CyclesToEnd += TII->getInstrLatency(TM->getInstrItineraryData(), MI);
Preston Gurda01daac2013-01-08 18:27:24 +0000195 }
196
Preston Gurd99c69902013-01-11 22:06:56 +0000197 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
198 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000199 return false;
200}
201
202/// addPadding - Add the given number of NOOP instructions to the function
203/// just prior to the return at MBBI
204void PadShortFunc::addPadding(MachineBasicBlock *MBB,
205 MachineBasicBlock::iterator &MBBI,
206 unsigned int NOOPsToAdd) {
207 DebugLoc DL = MBBI->getDebugLoc();
208
209 while (NOOPsToAdd-- > 0) {
210 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
211 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
212 }
213}