1f4a2713aSLionel Sambuc //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the pass which will pad short functions to prevent
11f4a2713aSLionel Sambuc // a stall if a function returns before the return address is ready. This
12f4a2713aSLionel Sambuc // is needed for some Intel Atom processors.
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc #include <algorithm>
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc #include "X86.h"
19f4a2713aSLionel Sambuc #include "X86InstrInfo.h"
20*0a6a1f1dSLionel Sambuc #include "X86Subtarget.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
22f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunctionPass.h"
23f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
24f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
25f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h"
26f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
28f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
29f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc using namespace llvm;
32f4a2713aSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "x86-pad-short-functions"
34*0a6a1f1dSLionel Sambuc
35f4a2713aSLionel Sambuc STATISTIC(NumBBsPadded, "Number of basic blocks padded");
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc namespace {
38f4a2713aSLionel Sambuc struct VisitedBBInfo {
39f4a2713aSLionel Sambuc // HasReturn - Whether the BB contains a return instruction
40f4a2713aSLionel Sambuc bool HasReturn;
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc // Cycles - Number of cycles until return if HasReturn is true, otherwise
43f4a2713aSLionel Sambuc // number of cycles until end of the BB
44f4a2713aSLionel Sambuc unsigned int Cycles;
45f4a2713aSLionel Sambuc
VisitedBBInfo__anon8081915f0111::VisitedBBInfo46f4a2713aSLionel Sambuc VisitedBBInfo() : HasReturn(false), Cycles(0) {}
VisitedBBInfo__anon8081915f0111::VisitedBBInfo47f4a2713aSLionel Sambuc VisitedBBInfo(bool HasReturn, unsigned int Cycles)
48f4a2713aSLionel Sambuc : HasReturn(HasReturn), Cycles(Cycles) {}
49f4a2713aSLionel Sambuc };
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc struct PadShortFunc : public MachineFunctionPass {
52f4a2713aSLionel Sambuc static char ID;
PadShortFunc__anon8081915f0111::PadShortFunc53f4a2713aSLionel Sambuc PadShortFunc() : MachineFunctionPass(ID)
54*0a6a1f1dSLionel Sambuc , Threshold(4), TM(nullptr), TII(nullptr) {}
55f4a2713aSLionel Sambuc
56*0a6a1f1dSLionel Sambuc bool runOnMachineFunction(MachineFunction &MF) override;
57f4a2713aSLionel Sambuc
getPassName__anon8081915f0111::PadShortFunc58*0a6a1f1dSLionel Sambuc const char *getPassName() const override {
59f4a2713aSLionel Sambuc return "X86 Atom pad short functions";
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc private:
63f4a2713aSLionel Sambuc void findReturns(MachineBasicBlock *MBB,
64f4a2713aSLionel Sambuc unsigned int Cycles = 0);
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc bool cyclesUntilReturn(MachineBasicBlock *MBB,
67f4a2713aSLionel Sambuc unsigned int &Cycles);
68f4a2713aSLionel Sambuc
69f4a2713aSLionel Sambuc void addPadding(MachineBasicBlock *MBB,
70f4a2713aSLionel Sambuc MachineBasicBlock::iterator &MBBI,
71f4a2713aSLionel Sambuc unsigned int NOOPsToAdd);
72f4a2713aSLionel Sambuc
73f4a2713aSLionel Sambuc const unsigned int Threshold;
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc // ReturnBBs - Maps basic blocks that return to the minimum number of
76f4a2713aSLionel Sambuc // cycles until the return, starting from the entry block.
77f4a2713aSLionel Sambuc DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc // VisitedBBs - Cache of previously visited BBs.
80f4a2713aSLionel Sambuc DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc const TargetMachine *TM;
83f4a2713aSLionel Sambuc const TargetInstrInfo *TII;
84f4a2713aSLionel Sambuc };
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc char PadShortFunc::ID = 0;
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc
createX86PadShortFunctions()89f4a2713aSLionel Sambuc FunctionPass *llvm::createX86PadShortFunctions() {
90f4a2713aSLionel Sambuc return new PadShortFunc();
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc
93f4a2713aSLionel Sambuc /// runOnMachineFunction - Loop over all of the basic blocks, inserting
94f4a2713aSLionel Sambuc /// NOOP instructions before early exits.
runOnMachineFunction(MachineFunction & MF)95f4a2713aSLionel Sambuc bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
96f4a2713aSLionel Sambuc const AttributeSet &FnAttrs = MF.getFunction()->getAttributes();
97f4a2713aSLionel Sambuc if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
98f4a2713aSLionel Sambuc Attribute::OptimizeForSize) ||
99f4a2713aSLionel Sambuc FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
100f4a2713aSLionel Sambuc Attribute::MinSize)) {
101f4a2713aSLionel Sambuc return false;
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc TM = &MF.getTarget();
105*0a6a1f1dSLionel Sambuc if (!TM->getSubtarget<X86Subtarget>().padShortFunctions())
106*0a6a1f1dSLionel Sambuc return false;
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc TII = TM->getSubtargetImpl()->getInstrInfo();
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc // Search through basic blocks and mark the ones that have early returns
111f4a2713aSLionel Sambuc ReturnBBs.clear();
112f4a2713aSLionel Sambuc VisitedBBs.clear();
113f4a2713aSLionel Sambuc findReturns(MF.begin());
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc bool MadeChange = false;
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc MachineBasicBlock *MBB;
118f4a2713aSLionel Sambuc unsigned int Cycles = 0;
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // Pad the identified basic blocks with NOOPs
121f4a2713aSLionel Sambuc for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
122f4a2713aSLionel Sambuc I != ReturnBBs.end(); ++I) {
123f4a2713aSLionel Sambuc MBB = I->first;
124f4a2713aSLionel Sambuc Cycles = I->second;
125f4a2713aSLionel Sambuc
126f4a2713aSLionel Sambuc if (Cycles < Threshold) {
127f4a2713aSLionel Sambuc // BB ends in a return. Skip over any DBG_VALUE instructions
128f4a2713aSLionel Sambuc // trailing the terminator.
129f4a2713aSLionel Sambuc assert(MBB->size() > 0 &&
130f4a2713aSLionel Sambuc "Basic block should contain at least a RET but is empty");
131f4a2713aSLionel Sambuc MachineBasicBlock::iterator ReturnLoc = --MBB->end();
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc while (ReturnLoc->isDebugValue())
134f4a2713aSLionel Sambuc --ReturnLoc;
135f4a2713aSLionel Sambuc assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
136f4a2713aSLionel Sambuc "Basic block does not end with RET");
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc addPadding(MBB, ReturnLoc, Threshold - Cycles);
139f4a2713aSLionel Sambuc NumBBsPadded++;
140f4a2713aSLionel Sambuc MadeChange = true;
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc return MadeChange;
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc /// findReturn - Starting at MBB, follow control flow and add all
148f4a2713aSLionel Sambuc /// basic blocks that contain a return to ReturnBBs.
findReturns(MachineBasicBlock * MBB,unsigned int Cycles)149f4a2713aSLionel Sambuc void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
150f4a2713aSLionel Sambuc // If this BB has a return, note how many cycles it takes to get there.
151f4a2713aSLionel Sambuc bool hasReturn = cyclesUntilReturn(MBB, Cycles);
152f4a2713aSLionel Sambuc if (Cycles >= Threshold)
153f4a2713aSLionel Sambuc return;
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc if (hasReturn) {
156f4a2713aSLionel Sambuc ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
157f4a2713aSLionel Sambuc return;
158f4a2713aSLionel Sambuc }
159f4a2713aSLionel Sambuc
160f4a2713aSLionel Sambuc // Follow branches in BB and look for returns
161f4a2713aSLionel Sambuc for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
162f4a2713aSLionel Sambuc I != MBB->succ_end(); ++I) {
163f4a2713aSLionel Sambuc if (*I == MBB)
164f4a2713aSLionel Sambuc continue;
165f4a2713aSLionel Sambuc findReturns(*I, Cycles);
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc /// cyclesUntilReturn - return true if the MBB has a return instruction,
170f4a2713aSLionel Sambuc /// and return false otherwise.
171f4a2713aSLionel Sambuc /// Cycles will be incremented by the number of cycles taken to reach the
172f4a2713aSLionel Sambuc /// return or the end of the BB, whichever occurs first.
cyclesUntilReturn(MachineBasicBlock * MBB,unsigned int & Cycles)173f4a2713aSLionel Sambuc bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
174f4a2713aSLionel Sambuc unsigned int &Cycles) {
175f4a2713aSLionel Sambuc // Return cached result if BB was previously visited
176f4a2713aSLionel Sambuc DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
177f4a2713aSLionel Sambuc = VisitedBBs.find(MBB);
178f4a2713aSLionel Sambuc if (it != VisitedBBs.end()) {
179f4a2713aSLionel Sambuc VisitedBBInfo BBInfo = it->second;
180f4a2713aSLionel Sambuc Cycles += BBInfo.Cycles;
181f4a2713aSLionel Sambuc return BBInfo.HasReturn;
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc
184f4a2713aSLionel Sambuc unsigned int CyclesToEnd = 0;
185f4a2713aSLionel Sambuc
186f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator MBBI = MBB->begin();
187f4a2713aSLionel Sambuc MBBI != MBB->end(); ++MBBI) {
188f4a2713aSLionel Sambuc MachineInstr *MI = MBBI;
189f4a2713aSLionel Sambuc // Mark basic blocks with a return instruction. Calls to other
190f4a2713aSLionel Sambuc // functions do not count because the called function will be padded,
191f4a2713aSLionel Sambuc // if necessary.
192f4a2713aSLionel Sambuc if (MI->isReturn() && !MI->isCall()) {
193f4a2713aSLionel Sambuc VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
194f4a2713aSLionel Sambuc Cycles += CyclesToEnd;
195f4a2713aSLionel Sambuc return true;
196f4a2713aSLionel Sambuc }
197f4a2713aSLionel Sambuc
198*0a6a1f1dSLionel Sambuc CyclesToEnd += TII->getInstrLatency(
199*0a6a1f1dSLionel Sambuc TM->getSubtargetImpl()->getInstrItineraryData(), MI);
200f4a2713aSLionel Sambuc }
201f4a2713aSLionel Sambuc
202f4a2713aSLionel Sambuc VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
203f4a2713aSLionel Sambuc Cycles += CyclesToEnd;
204f4a2713aSLionel Sambuc return false;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc
207f4a2713aSLionel Sambuc /// addPadding - Add the given number of NOOP instructions to the function
208f4a2713aSLionel Sambuc /// just prior to the return at MBBI
addPadding(MachineBasicBlock * MBB,MachineBasicBlock::iterator & MBBI,unsigned int NOOPsToAdd)209f4a2713aSLionel Sambuc void PadShortFunc::addPadding(MachineBasicBlock *MBB,
210f4a2713aSLionel Sambuc MachineBasicBlock::iterator &MBBI,
211f4a2713aSLionel Sambuc unsigned int NOOPsToAdd) {
212f4a2713aSLionel Sambuc DebugLoc DL = MBBI->getDebugLoc();
213f4a2713aSLionel Sambuc
214f4a2713aSLionel Sambuc while (NOOPsToAdd-- > 0) {
215f4a2713aSLionel Sambuc BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
216f4a2713aSLionel Sambuc BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc }
219