blob: 746d0d666477f62e6959818e2d812a32e3ec079f [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
18#define DEBUG_TYPE "x86-pad-short-functions"
19#include "X86.h"
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"
30
31using namespace llvm;
32
33STATISTIC(NumBBsPadded, "Number of basic blocks padded");
34
35namespace {
Preston Gurd99c69902013-01-11 22:06:56 +000036 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
Preston Gurda01daac2013-01-08 18:27:24 +000049 struct PadShortFunc : public MachineFunctionPass {
50 static char ID;
51 PadShortFunc() : MachineFunctionPass(ID)
52 , Threshold(4), TM(0), TII(0) {}
53
Craig Topper2d9361e2014-03-09 07:44:38 +000054 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurda01daac2013-01-08 18:27:24 +000055
Craig Topper2d9361e2014-03-09 07:44:38 +000056 const char *getPassName() const override {
Preston Gurda01daac2013-01-08 18:27:24 +000057 return "X86 Atom pad short functions";
58 }
59
60 private:
61 void findReturns(MachineBasicBlock *MBB,
62 unsigned int Cycles = 0);
63
64 bool cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +000065 unsigned int &Cycles);
Preston Gurda01daac2013-01-08 18:27:24 +000066
67 void addPadding(MachineBasicBlock *MBB,
68 MachineBasicBlock::iterator &MBBI,
69 unsigned int NOOPsToAdd);
70
71 const unsigned int Threshold;
Preston Gurd99c69902013-01-11 22:06:56 +000072
73 // ReturnBBs - Maps basic blocks that return to the minimum number of
74 // cycles until the return, starting from the entry block.
Preston Gurda01daac2013-01-08 18:27:24 +000075 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
76
Preston Gurd99c69902013-01-11 22:06:56 +000077 // VisitedBBs - Cache of previously visited BBs.
78 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
79
Preston Gurda01daac2013-01-08 18:27:24 +000080 const TargetMachine *TM;
81 const TargetInstrInfo *TII;
82 };
83
84 char PadShortFunc::ID = 0;
85}
86
87FunctionPass *llvm::createX86PadShortFunctions() {
88 return new PadShortFunc();
89}
90
91/// runOnMachineFunction - Loop over all of the basic blocks, inserting
92/// NOOP instructions before early exits.
93bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
Preston Gurd99c69902013-01-11 22:06:56 +000094 const AttributeSet &FnAttrs = MF.getFunction()->getAttributes();
95 if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
96 Attribute::OptimizeForSize) ||
97 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
98 Attribute::MinSize)) {
Preston Gurda01daac2013-01-08 18:27:24 +000099 return false;
Preston Gurd99c69902013-01-11 22:06:56 +0000100 }
Preston Gurda01daac2013-01-08 18:27:24 +0000101
102 TM = &MF.getTarget();
103 TII = TM->getInstrInfo();
104
105 // Search through basic blocks and mark the ones that have early returns
106 ReturnBBs.clear();
Preston Gurd99c69902013-01-11 22:06:56 +0000107 VisitedBBs.clear();
Preston Gurda01daac2013-01-08 18:27:24 +0000108 findReturns(MF.begin());
109
110 bool MadeChange = false;
111
Preston Gurda01daac2013-01-08 18:27:24 +0000112 MachineBasicBlock *MBB;
113 unsigned int Cycles = 0;
Preston Gurda01daac2013-01-08 18:27:24 +0000114
115 // Pad the identified basic blocks with NOOPs
116 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
117 I != ReturnBBs.end(); ++I) {
118 MBB = I->first;
119 Cycles = I->second;
120
121 if (Cycles < Threshold) {
Preston Gurd99c69902013-01-11 22:06:56 +0000122 // 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();
127
128 while (ReturnLoc->isDebugValue())
129 --ReturnLoc;
130 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
131 "Basic block does not end with RET");
Preston Gurda01daac2013-01-08 18:27:24 +0000132
133 addPadding(MBB, ReturnLoc, Threshold - Cycles);
134 NumBBsPadded++;
135 MadeChange = true;
136 }
137 }
138
139 return MadeChange;
140}
141
142/// findReturn - Starting at MBB, follow control flow and add all
143/// basic blocks that contain a return to ReturnBBs.
144void 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)
148 return;
149
150 if (hasReturn) {
151 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
152 return;
153 }
154
155 // Follow branches in BB and look for returns
156 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
Preston Gurd99c69902013-01-11 22:06:56 +0000157 I != MBB->succ_end(); ++I) {
158 if (*I == MBB)
159 continue;
Preston Gurda01daac2013-01-08 18:27:24 +0000160 findReturns(*I, Cycles);
161 }
162}
163
Preston Gurd99c69902013-01-11 22:06:56 +0000164/// cyclesUntilReturn - return true if the MBB has a return instruction,
165/// and return false otherwise.
Preston Gurda01daac2013-01-08 18:27:24 +0000166/// Cycles will be incremented by the number of cycles taken to reach the
167/// return or the end of the BB, whichever occurs first.
168bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +0000169 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;
177 }
178
179 unsigned int CyclesToEnd = 0;
180
Preston Gurda01daac2013-01-08 18:27:24 +0000181 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,
186 // if necessary.
187 if (MI->isReturn() && !MI->isCall()) {
Preston Gurd99c69902013-01-11 22:06:56 +0000188 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
189 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000190 return true;
191 }
192
Preston Gurd99c69902013-01-11 22:06:56 +0000193 CyclesToEnd += TII->getInstrLatency(TM->getInstrItineraryData(), MI);
Preston Gurda01daac2013-01-08 18:27:24 +0000194 }
195
Preston Gurd99c69902013-01-11 22:06:56 +0000196 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
197 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000198 return false;
199}
200
201/// addPadding - Add the given number of NOOP instructions to the function
202/// just prior to the return at MBBI
203void PadShortFunc::addPadding(MachineBasicBlock *MBB,
204 MachineBasicBlock::iterator &MBBI,
205 unsigned int NOOPsToAdd) {
206 DebugLoc DL = MBBI->getDebugLoc();
207
208 while (NOOPsToAdd-- > 0) {
209 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
210 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
211 }
212}