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 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "x86-pad-short-functions"
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"
33 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
36 struct PadShortFunc : public MachineFunctionPass {
38 PadShortFunc() : MachineFunctionPass(ID)
39 , Threshold(4), TM(0), TII(0) {}
41 virtual bool runOnMachineFunction(MachineFunction &MF);
43 virtual const char *getPassName() const {
44 return "X86 Atom pad short functions";
48 void findReturns(MachineBasicBlock *MBB,
49 unsigned int Cycles = 0);
51 bool cyclesUntilReturn(MachineBasicBlock *MBB,
53 MachineBasicBlock::iterator *Location = 0);
55 void addPadding(MachineBasicBlock *MBB,
56 MachineBasicBlock::iterator &MBBI,
57 unsigned int NOOPsToAdd);
59 const unsigned int Threshold;
60 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
62 const TargetMachine *TM;
63 const TargetInstrInfo *TII;
66 char PadShortFunc::ID = 0;
69 FunctionPass *llvm::createX86PadShortFunctions() {
70 return new PadShortFunc();
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);
83 TII = TM->getInstrInfo();
85 // Search through basic blocks and mark the ones that have early returns
87 findReturns(MF.begin());
89 bool MadeChange = false;
91 MachineBasicBlock::iterator ReturnLoc;
92 MachineBasicBlock *MBB;
93 unsigned int Cycles = 0;
94 unsigned int BBCycles;
96 // Pad the identified basic blocks with NOOPs
97 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
98 I != ReturnBBs.end(); ++I) {
102 if (Cycles < Threshold) {
103 if (!cyclesUntilReturn(MBB, BBCycles, &ReturnLoc))
106 addPadding(MBB, ReturnLoc, Threshold - Cycles);
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)
124 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
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);
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,
148 if (MI->isReturn() && !MI->isCall()) {
154 Cycles += TII->getInstrLatency(TM->getInstrItineraryData(), MI);
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();
167 while (NOOPsToAdd-- > 0) {
168 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
169 BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));