1 //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
19 #define DEBUG_TYPE "x86-pad-short-functions"
21 #include "X86InstrInfo.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetInstrInfo.h"
32 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
35 struct PadShortFunc : public MachineFunctionPass {
37 PadShortFunc() : MachineFunctionPass(ID)
41 virtual bool runOnMachineFunction(MachineFunction &MF);
43 virtual const char *getPassName() const
45 return "X86 Atom pad short functions";
49 bool addPadding(MachineFunction &MF,
50 MachineBasicBlock &MBB,
51 MachineBasicBlock::iterator &MBBI,
52 unsigned int NOOPsToAdd);
54 void findReturn(MachineFunction &MF,
55 MachineBasicBlock &MBB,
58 bool cyclesUntilReturn(MachineFunction &MF,
59 MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator *Location = 0);
63 const unsigned int Threshold;
64 std::map<int, unsigned int> ReturnBBs;
67 char PadShortFunc::ID = 0;
70 FunctionPass *llvm::createX86PadShortFunctions() {
71 return new PadShortFunc();
74 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
75 /// NOOP instructions before early exits.
76 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
77 // Process all basic blocks.
80 // Search through basic blocks and mark the ones that have early returns
81 findReturn(MF, *MF.begin(), 0);
84 MachineBasicBlock::iterator ReturnLoc;
85 MachineBasicBlock *MBB;
87 unsigned int Cycles = 0;
88 unsigned int BBCycles;
90 // Pad the identified basic blocks with NOOPs
91 for (std::map<int, unsigned int>::iterator I = ReturnBBs.begin();
92 I != ReturnBBs.end(); ++I) {
96 if (Cycles < Threshold) {
97 MBB = MF.getBlockNumbered(BBNum);
98 if (!cyclesUntilReturn(MF, *MBB, BBCycles, &ReturnLoc))
101 addPadding(MF, *MBB, ReturnLoc, Threshold - Cycles);
109 /// findReturn - Starting at MBB, follow control flow and add all
110 /// basic blocks that contain a return to ReturnBBs.
111 void PadShortFunc::findReturn(MachineFunction &MF,
112 MachineBasicBlock &MBB,
115 // If this BB has a return, note how many cycles it takes to get there.
116 bool hasReturn = cyclesUntilReturn(MF, MBB, Cycles);
117 if (Cycles >= Threshold)
121 int BBNum = MBB.getNumber();
122 ReturnBBs[BBNum] = std::max(ReturnBBs[BBNum], Cycles);
127 // Follow branches in BB and look for returns
128 for (MachineBasicBlock::succ_iterator I = MBB.succ_begin();
129 I != MBB.succ_end(); ++I) {
130 findReturn(MF, **I, Cycles);
134 /// cyclesUntilReturn - if the MBB has a return instruction, set Location to
135 /// to the instruction and return true. Return false otherwise.
136 /// Cycles will be incremented by the number of cycles taken to reach the
137 /// return or the end of the BB, whichever occurs first.
138 bool PadShortFunc::cyclesUntilReturn(MachineFunction &MF,
139 MachineBasicBlock &MBB,
140 unsigned int &Cycles,
141 MachineBasicBlock::iterator *Location)
143 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
144 const TargetMachine &Target = MF.getTarget();
146 for (MachineBasicBlock::iterator MBBI = MBB.begin(); MBBI != MBB.end();
148 MachineInstr *MI = MBBI;
149 // Mark basic blocks with a return instruction. Calls to other functions
150 // do not count because the called function will be padded, if necessary
151 if (MI->isReturn() && !MI->isCall()) {
157 Cycles += TII.getInstrLatency(Target.getInstrItineraryData(), MI);
163 /// addPadding - Add the given number of NOOP instructions to the function
164 /// right before the return at MBBI
165 bool PadShortFunc::addPadding(MachineFunction &MF,
166 MachineBasicBlock &MBB,
167 MachineBasicBlock::iterator &MBBI,
168 unsigned int NOOPsToAdd)
170 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
172 DebugLoc DL = MBBI->getDebugLoc();
174 while (NOOPsToAdd-- > 0) {
175 // Since Atom has two instruction execution ports,
176 // the code emits two noops, which will be executed in parallell
178 BuildMI(MBB, MBBI, DL, TII.get(X86::NOOP));
179 BuildMI(MBB, MBBI, DL, TII.get(X86::NOOP));