LLVM API Documentation

X86PadShortFunction.cpp
Go to the documentation of this file.
00001 //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the pass which will pad short functions to prevent
00011 // a stall if a function returns before the return address is ready. This
00012 // is needed for some Intel Atom processors.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include <algorithm>
00017 
00018 #include "X86.h"
00019 #include "X86InstrInfo.h"
00020 #include "X86Subtarget.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/CodeGen/MachineFunctionPass.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/CodeGen/Passes.h"
00026 #include "llvm/IR/Function.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/raw_ostream.h"
00029 #include "llvm/Target/TargetInstrInfo.h"
00030 
00031 using namespace llvm;
00032 
00033 #define DEBUG_TYPE "x86-pad-short-functions"
00034 
00035 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
00036 
00037 namespace {
00038   struct VisitedBBInfo {
00039     // HasReturn - Whether the BB contains a return instruction
00040     bool HasReturn;
00041 
00042     // Cycles - Number of cycles until return if HasReturn is true, otherwise
00043     // number of cycles until end of the BB
00044     unsigned int Cycles;
00045 
00046     VisitedBBInfo() : HasReturn(false), Cycles(0) {}
00047     VisitedBBInfo(bool HasReturn, unsigned int Cycles)
00048       : HasReturn(HasReturn), Cycles(Cycles) {}
00049   };
00050 
00051   struct PadShortFunc : public MachineFunctionPass {
00052     static char ID;
00053     PadShortFunc() : MachineFunctionPass(ID)
00054                    , Threshold(4), TM(nullptr), TII(nullptr) {}
00055 
00056     bool runOnMachineFunction(MachineFunction &MF) override;
00057 
00058     const char *getPassName() const override {
00059       return "X86 Atom pad short functions";
00060     }
00061 
00062   private:
00063     void findReturns(MachineBasicBlock *MBB,
00064                      unsigned int Cycles = 0);
00065 
00066     bool cyclesUntilReturn(MachineBasicBlock *MBB,
00067                            unsigned int &Cycles);
00068 
00069     void addPadding(MachineBasicBlock *MBB,
00070                     MachineBasicBlock::iterator &MBBI,
00071                     unsigned int NOOPsToAdd);
00072 
00073     const unsigned int Threshold;
00074 
00075     // ReturnBBs - Maps basic blocks that return to the minimum number of
00076     // cycles until the return, starting from the entry block.
00077     DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
00078 
00079     // VisitedBBs - Cache of previously visited BBs.
00080     DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
00081 
00082     const TargetMachine *TM;
00083     const TargetInstrInfo *TII;
00084   };
00085 
00086   char PadShortFunc::ID = 0;
00087 }
00088 
00089 FunctionPass *llvm::createX86PadShortFunctions() {
00090   return new PadShortFunc();
00091 }
00092 
00093 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
00094 /// NOOP instructions before early exits.
00095 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
00096   const AttributeSet &FnAttrs = MF.getFunction()->getAttributes();
00097   if (FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
00098                            Attribute::OptimizeForSize) ||
00099       FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
00100                            Attribute::MinSize)) {
00101     return false;
00102   }
00103 
00104   TM = &MF.getTarget();
00105   if (!TM->getSubtarget<X86Subtarget>().padShortFunctions())
00106     return false;
00107 
00108   TII = TM->getSubtargetImpl()->getInstrInfo();
00109 
00110   // Search through basic blocks and mark the ones that have early returns
00111   ReturnBBs.clear();
00112   VisitedBBs.clear();
00113   findReturns(MF.begin());
00114 
00115   bool MadeChange = false;
00116 
00117   MachineBasicBlock *MBB;
00118   unsigned int Cycles = 0;
00119 
00120   // Pad the identified basic blocks with NOOPs
00121   for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
00122        I != ReturnBBs.end(); ++I) {
00123     MBB = I->first;
00124     Cycles = I->second;
00125 
00126     if (Cycles < Threshold) {
00127       // BB ends in a return. Skip over any DBG_VALUE instructions
00128       // trailing the terminator.
00129       assert(MBB->size() > 0 &&
00130              "Basic block should contain at least a RET but is empty");
00131       MachineBasicBlock::iterator ReturnLoc = --MBB->end();
00132 
00133       while (ReturnLoc->isDebugValue())
00134         --ReturnLoc;
00135       assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
00136              "Basic block does not end with RET");
00137 
00138       addPadding(MBB, ReturnLoc, Threshold - Cycles);
00139       NumBBsPadded++;
00140       MadeChange = true;
00141     }
00142   }
00143 
00144   return MadeChange;
00145 }
00146 
00147 /// findReturn - Starting at MBB, follow control flow and add all
00148 /// basic blocks that contain a return to ReturnBBs.
00149 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
00150   // If this BB has a return, note how many cycles it takes to get there.
00151   bool hasReturn = cyclesUntilReturn(MBB, Cycles);
00152   if (Cycles >= Threshold)
00153     return;
00154 
00155   if (hasReturn) {
00156     ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
00157     return;
00158   }
00159 
00160   // Follow branches in BB and look for returns
00161   for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
00162        I != MBB->succ_end(); ++I) {
00163     if (*I == MBB)
00164       continue;
00165     findReturns(*I, Cycles);
00166   }
00167 }
00168 
00169 /// cyclesUntilReturn - return true if the MBB has a return instruction,
00170 /// and return false otherwise.
00171 /// Cycles will be incremented by the number of cycles taken to reach the
00172 /// return or the end of the BB, whichever occurs first.
00173 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
00174                                      unsigned int &Cycles) {
00175   // Return cached result if BB was previously visited
00176   DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
00177     = VisitedBBs.find(MBB);
00178   if (it != VisitedBBs.end()) {
00179     VisitedBBInfo BBInfo = it->second;
00180     Cycles += BBInfo.Cycles;
00181     return BBInfo.HasReturn;
00182   }
00183 
00184   unsigned int CyclesToEnd = 0;
00185 
00186   for (MachineBasicBlock::iterator MBBI = MBB->begin();
00187         MBBI != MBB->end(); ++MBBI) {
00188     MachineInstr *MI = MBBI;
00189     // Mark basic blocks with a return instruction. Calls to other
00190     // functions do not count because the called function will be padded,
00191     // if necessary.
00192     if (MI->isReturn() && !MI->isCall()) {
00193       VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
00194       Cycles += CyclesToEnd;
00195       return true;
00196     }
00197 
00198     CyclesToEnd += TII->getInstrLatency(
00199         TM->getSubtargetImpl()->getInstrItineraryData(), MI);
00200   }
00201 
00202   VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
00203   Cycles += CyclesToEnd;
00204   return false;
00205 }
00206 
00207 /// addPadding - Add the given number of NOOP instructions to the function
00208 /// just prior to the return at MBBI
00209 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
00210                               MachineBasicBlock::iterator &MBBI,
00211                               unsigned int NOOPsToAdd) {
00212   DebugLoc DL = MBBI->getDebugLoc();
00213 
00214   while (NOOPsToAdd-- > 0) {
00215     BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
00216     BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
00217   }
00218 }