blob: 62a9aafc2cf3663982bc0a19d9917b8f8fbfc6a1 [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
Derek Schuff1dbf7a52016-04-04 17:09:25 +000058 MachineFunctionProperties getRequiredProperties() const override {
59 return MachineFunctionProperties().set(
60 MachineFunctionProperties::Property::AllVRegsAllocated);
61 }
62
Craig Topper2d9361e2014-03-09 07:44:38 +000063 const char *getPassName() const override {
Preston Gurda01daac2013-01-08 18:27:24 +000064 return "X86 Atom pad short functions";
65 }
66
67 private:
68 void findReturns(MachineBasicBlock *MBB,
69 unsigned int Cycles = 0);
70
71 bool cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +000072 unsigned int &Cycles);
Preston Gurda01daac2013-01-08 18:27:24 +000073
74 void addPadding(MachineBasicBlock *MBB,
75 MachineBasicBlock::iterator &MBBI,
76 unsigned int NOOPsToAdd);
77
78 const unsigned int Threshold;
Preston Gurd99c69902013-01-11 22:06:56 +000079
80 // ReturnBBs - Maps basic blocks that return to the minimum number of
81 // cycles until the return, starting from the entry block.
Preston Gurda01daac2013-01-08 18:27:24 +000082 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
83
Preston Gurd99c69902013-01-11 22:06:56 +000084 // VisitedBBs - Cache of previously visited BBs.
85 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
86
Eric Christopher05b81972015-02-02 17:38:43 +000087 const X86Subtarget *STI;
Preston Gurda01daac2013-01-08 18:27:24 +000088 const TargetInstrInfo *TII;
89 };
90
91 char PadShortFunc::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000092}
Preston Gurda01daac2013-01-08 18:27:24 +000093
94FunctionPass *llvm::createX86PadShortFunctions() {
95 return new PadShortFunc();
96}
97
98/// runOnMachineFunction - Loop over all of the basic blocks, inserting
99/// NOOP instructions before early exits.
100bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor2bee5ef2016-04-26 21:44:24 +0000101 if (skipFunction(*MF.getFunction()))
102 return false;
103
Sanjay Patel924879a2015-08-04 15:49:57 +0000104 if (MF.getFunction()->optForSize()) {
Preston Gurda01daac2013-01-08 18:27:24 +0000105 return false;
Preston Gurd99c69902013-01-11 22:06:56 +0000106 }
Preston Gurda01daac2013-01-08 18:27:24 +0000107
Eric Christopher05b81972015-02-02 17:38:43 +0000108 STI = &MF.getSubtarget<X86Subtarget>();
109 if (!STI->padShortFunctions())
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000110 return false;
111
Eric Christopher05b81972015-02-02 17:38:43 +0000112 TII = STI->getInstrInfo();
Preston Gurda01daac2013-01-08 18:27:24 +0000113
114 // Search through basic blocks and mark the ones that have early returns
115 ReturnBBs.clear();
Preston Gurd99c69902013-01-11 22:06:56 +0000116 VisitedBBs.clear();
Duncan P. N. Exon Smithd77de642015-10-19 21:48:29 +0000117 findReturns(&MF.front());
Preston Gurda01daac2013-01-08 18:27:24 +0000118
119 bool MadeChange = false;
120
Preston Gurda01daac2013-01-08 18:27:24 +0000121 MachineBasicBlock *MBB;
122 unsigned int Cycles = 0;
Preston Gurda01daac2013-01-08 18:27:24 +0000123
124 // Pad the identified basic blocks with NOOPs
125 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
126 I != ReturnBBs.end(); ++I) {
127 MBB = I->first;
128 Cycles = I->second;
129
130 if (Cycles < Threshold) {
Preston Gurd99c69902013-01-11 22:06:56 +0000131 // BB ends in a return. Skip over any DBG_VALUE instructions
132 // trailing the terminator.
133 assert(MBB->size() > 0 &&
134 "Basic block should contain at least a RET but is empty");
135 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
136
137 while (ReturnLoc->isDebugValue())
138 --ReturnLoc;
139 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
140 "Basic block does not end with RET");
Preston Gurda01daac2013-01-08 18:27:24 +0000141
142 addPadding(MBB, ReturnLoc, Threshold - Cycles);
143 NumBBsPadded++;
144 MadeChange = true;
145 }
146 }
147
148 return MadeChange;
149}
150
151/// findReturn - Starting at MBB, follow control flow and add all
152/// basic blocks that contain a return to ReturnBBs.
153void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
154 // If this BB has a return, note how many cycles it takes to get there.
155 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
156 if (Cycles >= Threshold)
157 return;
158
159 if (hasReturn) {
160 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
161 return;
162 }
163
164 // Follow branches in BB and look for returns
165 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
Preston Gurd99c69902013-01-11 22:06:56 +0000166 I != MBB->succ_end(); ++I) {
167 if (*I == MBB)
168 continue;
Preston Gurda01daac2013-01-08 18:27:24 +0000169 findReturns(*I, Cycles);
170 }
171}
172
Preston Gurd99c69902013-01-11 22:06:56 +0000173/// cyclesUntilReturn - return true if the MBB has a return instruction,
174/// and return false otherwise.
Preston Gurda01daac2013-01-08 18:27:24 +0000175/// Cycles will be incremented by the number of cycles taken to reach the
176/// return or the end of the BB, whichever occurs first.
177bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
Preston Gurd99c69902013-01-11 22:06:56 +0000178 unsigned int &Cycles) {
179 // Return cached result if BB was previously visited
180 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
181 = VisitedBBs.find(MBB);
182 if (it != VisitedBBs.end()) {
183 VisitedBBInfo BBInfo = it->second;
184 Cycles += BBInfo.Cycles;
185 return BBInfo.HasReturn;
186 }
187
188 unsigned int CyclesToEnd = 0;
189
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000190 for (MachineInstr &MI : *MBB) {
Preston Gurda01daac2013-01-08 18:27:24 +0000191 // Mark basic blocks with a return instruction. Calls to other
192 // functions do not count because the called function will be padded,
193 // if necessary.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000194 if (MI.isReturn() && !MI.isCall()) {
Preston Gurd99c69902013-01-11 22:06:56 +0000195 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
196 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000197 return true;
198 }
199
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000200 CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI);
Preston Gurda01daac2013-01-08 18:27:24 +0000201 }
202
Preston Gurd99c69902013-01-11 22:06:56 +0000203 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
204 Cycles += CyclesToEnd;
Preston Gurda01daac2013-01-08 18:27:24 +0000205 return false;
206}
207
208/// addPadding - Add the given number of NOOP instructions to the function
209/// just prior to the return at MBBI
210void PadShortFunc::addPadding(MachineBasicBlock *MBB,
211 MachineBasicBlock::iterator &MBBI,
212 unsigned int NOOPsToAdd) {
213 DebugLoc DL = MBBI->getDebugLoc();
214
215 while (NOOPsToAdd-- > 0) {
216 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
217 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
218 }
219}