c22872f1280b6bd87ade0f7cea7ed15dea5ca35f
[oota-llvm.git] / lib / Target / X86 / X86PadShortFunction.cpp
1 //===-------- 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
31 using namespace llvm;
32
33 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
34
35 namespace {
36   struct PadShortFunc : public MachineFunctionPass {
37     static char ID;
38     PadShortFunc() : MachineFunctionPass(ID)
39                    , Threshold(4), TM(0), TII(0) {}
40
41     virtual bool runOnMachineFunction(MachineFunction &MF);
42
43     virtual const char *getPassName() const {
44       return "X86 Atom pad short functions";
45     }
46
47   private:
48     void findReturns(MachineBasicBlock *MBB,
49                      unsigned int Cycles = 0);
50
51     bool cyclesUntilReturn(MachineBasicBlock *MBB,
52                            unsigned int &Cycles,
53                            MachineBasicBlock::iterator *Location = 0);
54
55     void addPadding(MachineBasicBlock *MBB,
56                     MachineBasicBlock::iterator &MBBI,
57                     unsigned int NOOPsToAdd);
58
59     const unsigned int Threshold;
60     DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
61
62     const TargetMachine *TM;
63     const TargetInstrInfo *TII;
64   };
65
66   char PadShortFunc::ID = 0;
67 }
68
69 FunctionPass *llvm::createX86PadShortFunctions() {
70   return new PadShortFunc();
71 }
72
73 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
74 /// NOOP instructions before early exits.
75 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
76   bool OptForSize = MF.getFunction()->getAttributes().
77     hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
78
79   if (OptForSize)
80     return false;
81
82   TM = &MF.getTarget();
83   TII = TM->getInstrInfo();
84
85   // Search through basic blocks and mark the ones that have early returns
86   ReturnBBs.clear();
87   findReturns(MF.begin());
88
89   bool MadeChange = false;
90
91   MachineBasicBlock::iterator ReturnLoc;
92   MachineBasicBlock *MBB;
93   unsigned int Cycles = 0;
94   unsigned int BBCycles;
95
96   // Pad the identified basic blocks with NOOPs
97   for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
98        I != ReturnBBs.end(); ++I) {
99     MBB = I->first;
100     Cycles = I->second;
101
102     if (Cycles < Threshold) {
103       if (!cyclesUntilReturn(MBB, BBCycles, &ReturnLoc))
104         continue;
105
106       addPadding(MBB, ReturnLoc, Threshold - Cycles);
107       NumBBsPadded++;
108       MadeChange = true;
109     }
110   }
111
112   return MadeChange;
113 }
114
115 /// findReturn - Starting at MBB, follow control flow and add all
116 /// basic blocks that contain a return to ReturnBBs.
117 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
118   // If this BB has a return, note how many cycles it takes to get there.
119   bool hasReturn = cyclesUntilReturn(MBB, Cycles);
120   if (Cycles >= Threshold)
121     return;
122
123   if (hasReturn) {
124     ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
125     return;
126   }
127
128   // Follow branches in BB and look for returns
129   for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
130       I != MBB->succ_end(); ++I) {
131     findReturns(*I, Cycles);
132   }
133 }
134
135 /// cyclesUntilReturn - if the MBB has a return instruction, set Location
136 /// to the instruction and return true. Return false otherwise.
137 /// Cycles will be incremented by the number of cycles taken to reach the
138 /// return or the end of the BB, whichever occurs first.
139 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
140                                      unsigned int &Cycles,
141                                      MachineBasicBlock::iterator *Location) {
142   for (MachineBasicBlock::iterator MBBI = MBB->begin();
143         MBBI != MBB->end(); ++MBBI) {
144     MachineInstr *MI = MBBI;
145     // Mark basic blocks with a return instruction. Calls to other
146     // functions do not count because the called function will be padded,
147     // if necessary.
148     if (MI->isReturn() && !MI->isCall()) {
149       if (Location)
150         *Location = MBBI;
151       return true;
152     }
153
154     Cycles += TII->getInstrLatency(TM->getInstrItineraryData(), MI);
155   }
156
157   return false;
158 }
159
160 /// addPadding - Add the given number of NOOP instructions to the function
161 /// just prior to the return at MBBI
162 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
163                               MachineBasicBlock::iterator &MBBI,
164                               unsigned int NOOPsToAdd) {
165   DebugLoc DL = MBBI->getDebugLoc();
166
167   while (NOOPsToAdd-- > 0) {
168     BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
169     BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
170   }
171 }