Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 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 | |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 18 | #include "X86.h" |
| 19 | #include "X86InstrInfo.h" |
Eric Christopher | 0d5c99e | 2014-05-22 01:46:02 +0000 | [diff] [blame] | 20 | #include "X86Subtarget.h" |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "x86-pad-short-functions" |
| 34 | |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 35 | STATISTIC(NumBBsPadded, "Number of basic blocks padded"); |
| 36 | |
| 37 | namespace { |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 38 | struct VisitedBBInfo { |
| 39 | // HasReturn - Whether the BB contains a return instruction |
| 40 | bool HasReturn; |
| 41 | |
| 42 | // Cycles - Number of cycles until return if HasReturn is true, otherwise |
| 43 | // number of cycles until end of the BB |
| 44 | unsigned int Cycles; |
| 45 | |
| 46 | VisitedBBInfo() : HasReturn(false), Cycles(0) {} |
| 47 | VisitedBBInfo(bool HasReturn, unsigned int Cycles) |
| 48 | : HasReturn(HasReturn), Cycles(Cycles) {} |
| 49 | }; |
| 50 | |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 51 | struct PadShortFunc : public MachineFunctionPass { |
| 52 | static char ID; |
| 53 | PadShortFunc() : MachineFunctionPass(ID) |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame^] | 54 | , Threshold(4), STI(nullptr), TII(nullptr) {} |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 55 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 56 | bool runOnMachineFunction(MachineFunction &MF) override; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 57 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 58 | const char *getPassName() const override { |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 59 | return "X86 Atom pad short functions"; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | void findReturns(MachineBasicBlock *MBB, |
| 64 | unsigned int Cycles = 0); |
| 65 | |
| 66 | bool cyclesUntilReturn(MachineBasicBlock *MBB, |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 67 | unsigned int &Cycles); |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 68 | |
| 69 | void addPadding(MachineBasicBlock *MBB, |
| 70 | MachineBasicBlock::iterator &MBBI, |
| 71 | unsigned int NOOPsToAdd); |
| 72 | |
| 73 | const unsigned int Threshold; |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 74 | |
| 75 | // ReturnBBs - Maps basic blocks that return to the minimum number of |
| 76 | // cycles until the return, starting from the entry block. |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 77 | DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs; |
| 78 | |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 79 | // VisitedBBs - Cache of previously visited BBs. |
| 80 | DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs; |
| 81 | |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame^] | 82 | const X86Subtarget *STI; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 83 | const TargetInstrInfo *TII; |
| 84 | }; |
| 85 | |
| 86 | char PadShortFunc::ID = 0; |
| 87 | } |
| 88 | |
| 89 | FunctionPass *llvm::createX86PadShortFunctions() { |
| 90 | return new PadShortFunc(); |
| 91 | } |
| 92 | |
| 93 | /// runOnMachineFunction - Loop over all of the basic blocks, inserting |
| 94 | /// NOOP instructions before early exits. |
| 95 | bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) { |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 96 | const AttributeSet &FnAttrs = MF.getFunction()->getAttributes(); |
| 97 | if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex, |
| 98 | Attribute::OptimizeForSize) || |
| 99 | FnAttrs.hasAttribute(AttributeSet::FunctionIndex, |
| 100 | Attribute::MinSize)) { |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 101 | return false; |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 102 | } |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 103 | |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame^] | 104 | STI = &MF.getSubtarget<X86Subtarget>(); |
| 105 | if (!STI->padShortFunctions()) |
Eric Christopher | 0d5c99e | 2014-05-22 01:46:02 +0000 | [diff] [blame] | 106 | return false; |
| 107 | |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame^] | 108 | TII = STI->getInstrInfo(); |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 109 | |
| 110 | // Search through basic blocks and mark the ones that have early returns |
| 111 | ReturnBBs.clear(); |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 112 | VisitedBBs.clear(); |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 113 | findReturns(MF.begin()); |
| 114 | |
| 115 | bool MadeChange = false; |
| 116 | |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 117 | MachineBasicBlock *MBB; |
| 118 | unsigned int Cycles = 0; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 119 | |
| 120 | // Pad the identified basic blocks with NOOPs |
| 121 | for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin(); |
| 122 | I != ReturnBBs.end(); ++I) { |
| 123 | MBB = I->first; |
| 124 | Cycles = I->second; |
| 125 | |
| 126 | if (Cycles < Threshold) { |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 127 | // BB ends in a return. Skip over any DBG_VALUE instructions |
| 128 | // trailing the terminator. |
| 129 | assert(MBB->size() > 0 && |
| 130 | "Basic block should contain at least a RET but is empty"); |
| 131 | MachineBasicBlock::iterator ReturnLoc = --MBB->end(); |
| 132 | |
| 133 | while (ReturnLoc->isDebugValue()) |
| 134 | --ReturnLoc; |
| 135 | assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() && |
| 136 | "Basic block does not end with RET"); |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 137 | |
| 138 | addPadding(MBB, ReturnLoc, Threshold - Cycles); |
| 139 | NumBBsPadded++; |
| 140 | MadeChange = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return MadeChange; |
| 145 | } |
| 146 | |
| 147 | /// findReturn - Starting at MBB, follow control flow and add all |
| 148 | /// basic blocks that contain a return to ReturnBBs. |
| 149 | void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) { |
| 150 | // If this BB has a return, note how many cycles it takes to get there. |
| 151 | bool hasReturn = cyclesUntilReturn(MBB, Cycles); |
| 152 | if (Cycles >= Threshold) |
| 153 | return; |
| 154 | |
| 155 | if (hasReturn) { |
| 156 | ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // Follow branches in BB and look for returns |
| 161 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(); |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 162 | I != MBB->succ_end(); ++I) { |
| 163 | if (*I == MBB) |
| 164 | continue; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 165 | findReturns(*I, Cycles); |
| 166 | } |
| 167 | } |
| 168 | |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 169 | /// cyclesUntilReturn - return true if the MBB has a return instruction, |
| 170 | /// and return false otherwise. |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 171 | /// Cycles will be incremented by the number of cycles taken to reach the |
| 172 | /// return or the end of the BB, whichever occurs first. |
| 173 | bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB, |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 174 | unsigned int &Cycles) { |
| 175 | // Return cached result if BB was previously visited |
| 176 | DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it |
| 177 | = VisitedBBs.find(MBB); |
| 178 | if (it != VisitedBBs.end()) { |
| 179 | VisitedBBInfo BBInfo = it->second; |
| 180 | Cycles += BBInfo.Cycles; |
| 181 | return BBInfo.HasReturn; |
| 182 | } |
| 183 | |
| 184 | unsigned int CyclesToEnd = 0; |
| 185 | |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 186 | for (MachineBasicBlock::iterator MBBI = MBB->begin(); |
| 187 | MBBI != MBB->end(); ++MBBI) { |
| 188 | MachineInstr *MI = MBBI; |
| 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()) { |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 193 | VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd); |
| 194 | Cycles += CyclesToEnd; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame^] | 198 | CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI); |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Preston Gurd | 99c6990 | 2013-01-11 22:06:56 +0000 | [diff] [blame] | 201 | VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd); |
| 202 | Cycles += CyclesToEnd; |
Preston Gurd | a01daac | 2013-01-08 18:27:24 +0000 | [diff] [blame] | 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 | } |