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