blob: 143e70bda9e716f29805d05f3026106efdb54dec [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"
Eric Christopher0d5c99e2014-05-22 01:46:02 +000020#include "X86Subtarget.h"
Preston Gurda01daac2013-01-08 18:27:24 +000021#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
Chandler Carruth84e68b22014-04-22 02:41:26 +000033#define DEBUG_TYPE "x86-pad-short-functions"
34
Preston Gurda01daac2013-01-08 18:27:24 +000035STATISTIC(NumBBsPadded, "Number of basic blocks padded");
36
37namespace {
Preston Gurd99c69902013-01-11 22:06:56 +000038 struct VisitedBBInfo {
39 // HasReturn - Whether the BB contains a return instruction
40 bool HasReturn;
41
42 // Cycles - Number of cycles until return if HasReturn is true, otherwise
43 // number of cycles until end of the BB
44 unsigned int Cycles;
45
46 VisitedBBInfo() : HasReturn(false), Cycles(0) {}
47 VisitedBBInfo(bool HasReturn, unsigned int Cycles)
48 : HasReturn(HasReturn), Cycles(Cycles) {}
49 };
50
Preston Gurda01daac2013-01-08 18:27:24 +000051 struct PadShortFunc : public MachineFunctionPass {
52 static char ID;
53 PadShortFunc() : MachineFunctionPass(ID)
Eric Christopher05b81972015-02-02 17:38:43 +000054 , Threshold(4), STI(nullptr), TII(nullptr) {}
Preston Gurda01daac2013-01-08 18:27:24 +000055
Craig Topper2d9361e2014-03-09 07:44:38 +000056 bool runOnMachineFunction(MachineFunction &MF) override;
Preston Gurda01daac2013-01-08 18:27:24 +000057
Craig Topper2d9361e2014-03-09 07:44:38 +000058 const char *getPassName() const override {
Preston Gurda01daac2013-01-08 18:27:24 +000059 return "X86 Atom pad short functions";
60 }
61
62 private:
63 void findReturns(MachineBasicBlock *MBB,
64 unsigned int Cycles = 0);
65
66 bool cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +000067 unsigned int &Cycles);
Preston Gurda01daac2013-01-08 18:27:24 +000068
69 void addPadding(MachineBasicBlock *MBB,
70 MachineBasicBlock::iterator &MBBI,
71 unsigned int NOOPsToAdd);
72
73 const unsigned int Threshold;
Preston Gurd99c69902013-01-11 22:06:56 +000074
75 // ReturnBBs - Maps basic blocks that return to the minimum number of
76 // cycles until the return, starting from the entry block.
Preston Gurda01daac2013-01-08 18:27:24 +000077 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
78
Preston Gurd99c69902013-01-11 22:06:56 +000079 // VisitedBBs - Cache of previously visited BBs.
80 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
81
Eric Christopher05b81972015-02-02 17:38:43 +000082 const X86Subtarget *STI;
Preston Gurda01daac2013-01-08 18:27:24 +000083 const TargetInstrInfo *TII;
84 };
85
86 char PadShortFunc::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000087}
Preston Gurda01daac2013-01-08 18:27:24 +000088
89FunctionPass *llvm::createX86PadShortFunctions() {
90 return new PadShortFunc();
91}
92
93/// runOnMachineFunction - Loop over all of the basic blocks, inserting
94/// NOOP instructions before early exits.
95bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +000096 if (MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize) ||
97 MF.getFunction()->hasFnAttribute(Attribute::MinSize)) {
Preston Gurda01daac2013-01-08 18:27:24 +000098 return false;
Preston Gurd99c69902013-01-11 22:06:56 +000099 }
Preston Gurda01daac2013-01-08 18:27:24 +0000100
Eric Christopher05b81972015-02-02 17:38:43 +0000101 STI = &MF.getSubtarget<X86Subtarget>();
102 if (!STI->padShortFunctions())
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000103 return false;
104
Eric Christopher05b81972015-02-02 17:38:43 +0000105 TII = STI->getInstrInfo();
Preston Gurda01daac2013-01-08 18:27:24 +0000106
107 // Search through basic blocks and mark the ones that have early returns
108 ReturnBBs.clear();
Preston Gurd99c69902013-01-11 22:06:56 +0000109 VisitedBBs.clear();
Preston Gurda01daac2013-01-08 18:27:24 +0000110 findReturns(MF.begin());
111
112 bool MadeChange = false;
113
Preston Gurda01daac2013-01-08 18:27:24 +0000114 MachineBasicBlock *MBB;
115 unsigned int Cycles = 0;
Preston Gurda01daac2013-01-08 18:27:24 +0000116
117 // Pad the identified basic blocks with NOOPs
118 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
119 I != ReturnBBs.end(); ++I) {
120 MBB = I->first;
121 Cycles = I->second;
122
123 if (Cycles < Threshold) {
Preston Gurd99c69902013-01-11 22:06:56 +0000124 // BB ends in a return. Skip over any DBG_VALUE instructions
125 // trailing the terminator.
126 assert(MBB->size() > 0 &&
127 "Basic block should contain at least a RET but is empty");
128 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
129
130 while (ReturnLoc->isDebugValue())
131 --ReturnLoc;
132 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
133 "Basic block does not end with RET");
Preston Gurda01daac2013-01-08 18:27:24 +0000134
135 addPadding(MBB, ReturnLoc, Threshold - Cycles);
136 NumBBsPadded++;
137 MadeChange = true;
138 }
139 }
140
141 return MadeChange;
142}
143
144/// findReturn - Starting at MBB, follow control flow and add all
145/// basic blocks that contain a return to ReturnBBs.
146void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
147 // If this BB has a return, note how many cycles it takes to get there.
148 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
149 if (Cycles >= Threshold)
150 return;
151
152 if (hasReturn) {
153 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
154 return;
155 }
156
157 // Follow branches in BB and look for returns
158 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
Preston Gurd99c69902013-01-11 22:06:56 +0000159 I != MBB->succ_end(); ++I) {
160 if (*I == MBB)
161 continue;
Preston Gurda01daac2013-01-08 18:27:24 +0000162 findReturns(*I, Cycles);
163 }
164}
165
Preston Gurd99c69902013-01-11 22:06:56 +0000166/// cyclesUntilReturn - return true if the MBB has a return instruction,
167/// and return false otherwise.
Preston Gurda01daac2013-01-08 18:27:24 +0000168/// Cycles will be incremented by the number of cycles taken to reach the
169/// return or the end of the BB, whichever occurs first.
170bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +0000171 unsigned int &Cycles) {
172 // Return cached result if BB was previously visited
173 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
174 = VisitedBBs.find(MBB);
175 if (it != VisitedBBs.end()) {
176 VisitedBBInfo BBInfo = it->second;
177 Cycles += BBInfo.Cycles;
178 return BBInfo.HasReturn;
179 }
180
181 unsigned int CyclesToEnd = 0;
182
Preston Gurda01daac2013-01-08 18:27:24 +0000183 for (MachineBasicBlock::iterator MBBI = MBB->begin();
184 MBBI != MBB->end(); ++MBBI) {
185 MachineInstr *MI = MBBI;
186 // Mark basic blocks with a return instruction. Calls to other
187 // functions do not count because the called function will be padded,
188 // if necessary.
189 if (MI->isReturn() && !MI->isCall()) {
Preston Gurd99c69902013-01-11 22:06:56 +0000190 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
191 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000192 return true;
193 }
194
Eric Christopher05b81972015-02-02 17:38:43 +0000195 CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI);
Preston Gurda01daac2013-01-08 18:27:24 +0000196 }
197
Preston Gurd99c69902013-01-11 22:06:56 +0000198 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
199 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000200 return false;
201}
202
203/// addPadding - Add the given number of NOOP instructions to the function
204/// just prior to the return at MBBI
205void PadShortFunc::addPadding(MachineBasicBlock *MBB,
206 MachineBasicBlock::iterator &MBBI,
207 unsigned int NOOPsToAdd) {
208 DebugLoc DL = MBBI->getDebugLoc();
209
210 while (NOOPsToAdd-- > 0) {
211 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
212 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
213 }
214}