LLVM API Documentation

Spiller.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/Spiller.cpp -  Spiller -------------------------------===//
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 #include "Spiller.h"
00011 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00012 #include "llvm/CodeGen/LiveRangeEdit.h"
00013 #include "llvm/CodeGen/LiveStackAnalysis.h"
00014 #include "llvm/CodeGen/MachineFrameInfo.h"
00015 #include "llvm/CodeGen/MachineFunction.h"
00016 #include "llvm/CodeGen/MachineInstrBuilder.h"
00017 #include "llvm/CodeGen/MachineLoopInfo.h"
00018 #include "llvm/CodeGen/MachineRegisterInfo.h"
00019 #include "llvm/CodeGen/VirtRegMap.h"
00020 #include "llvm/Support/CommandLine.h"
00021 #include "llvm/Support/Debug.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 #include "llvm/Support/raw_ostream.h"
00024 #include "llvm/Target/TargetInstrInfo.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 
00027 using namespace llvm;
00028 
00029 #define DEBUG_TYPE "spiller"
00030 
00031 namespace {
00032   enum SpillerName { trivial, inline_ };
00033 }
00034 
00035 static cl::opt<SpillerName>
00036 spillerOpt("spiller",
00037            cl::desc("Spiller to use: (default: standard)"),
00038            cl::Prefix,
00039            cl::values(clEnumVal(trivial,   "trivial spiller"),
00040                       clEnumValN(inline_,  "inline", "inline spiller"),
00041                       clEnumValEnd),
00042            cl::init(trivial));
00043 
00044 // Spiller virtual destructor implementation.
00045 Spiller::~Spiller() {}
00046 
00047 namespace {
00048 
00049 /// Utility class for spillers.
00050 class SpillerBase : public Spiller {
00051 protected:
00052   MachineFunctionPass *pass;
00053   MachineFunction *mf;
00054   VirtRegMap *vrm;
00055   LiveIntervals *lis;
00056   MachineFrameInfo *mfi;
00057   MachineRegisterInfo *mri;
00058   const TargetInstrInfo *tii;
00059   const TargetRegisterInfo *tri;
00060 
00061   /// Construct a spiller base.
00062   SpillerBase(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
00063     : pass(&pass), mf(&mf), vrm(&vrm)
00064   {
00065     lis = &pass.getAnalysis<LiveIntervals>();
00066     mfi = mf.getFrameInfo();
00067     mri = &mf.getRegInfo();
00068     tii = mf.getSubtarget().getInstrInfo();
00069     tri = mf.getSubtarget().getRegisterInfo();
00070   }
00071 
00072   /// Add spill ranges for every use/def of the live interval, inserting loads
00073   /// immediately before each use, and stores after each def. No folding or
00074   /// remat is attempted.
00075   void trivialSpillEverywhere(LiveRangeEdit& LRE) {
00076     LiveInterval* li = &LRE.getParent();
00077 
00078     DEBUG(dbgs() << "Spilling everywhere " << *li << "\n");
00079 
00080     assert(li->weight != llvm::huge_valf &&
00081            "Attempting to spill already spilled value.");
00082 
00083     assert(!TargetRegisterInfo::isStackSlot(li->reg) &&
00084            "Trying to spill a stack slot.");
00085 
00086     DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n");
00087 
00088     const TargetRegisterClass *trc = mri->getRegClass(li->reg);
00089     unsigned ss = vrm->assignVirt2StackSlot(li->reg);
00090 
00091     // Iterate over reg uses/defs.
00092     for (MachineRegisterInfo::reg_instr_iterator
00093          regItr = mri->reg_instr_begin(li->reg);
00094          regItr != mri->reg_instr_end();) {
00095 
00096       // Grab the use/def instr.
00097       MachineInstr *mi = &*regItr;
00098 
00099       DEBUG(dbgs() << "  Processing " << *mi);
00100 
00101       // Step regItr to the next use/def instr.
00102       ++regItr;
00103 
00104       // Collect uses & defs for this instr.
00105       SmallVector<unsigned, 2> indices;
00106       bool hasUse = false;
00107       bool hasDef = false;
00108       for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
00109         MachineOperand &op = mi->getOperand(i);
00110         if (!op.isReg() || op.getReg() != li->reg)
00111           continue;
00112         hasUse |= mi->getOperand(i).isUse();
00113         hasDef |= mi->getOperand(i).isDef();
00114         indices.push_back(i);
00115       }
00116 
00117       // Create a new virtual register for the load and/or store.
00118       unsigned NewVReg = LRE.create();
00119 
00120       // Update the reg operands & kill flags.
00121       for (unsigned i = 0; i < indices.size(); ++i) {
00122         unsigned mopIdx = indices[i];
00123         MachineOperand &mop = mi->getOperand(mopIdx);
00124         mop.setReg(NewVReg);
00125         if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) {
00126           mop.setIsKill(true);
00127         }
00128       }
00129       assert(hasUse || hasDef);
00130 
00131       // Insert reload if necessary.
00132       MachineBasicBlock::iterator miItr(mi);
00133       if (hasUse) {
00134         MachineInstrSpan MIS(miItr);
00135 
00136         tii->loadRegFromStackSlot(*mi->getParent(), miItr, NewVReg, ss, trc,
00137                                   tri);
00138         lis->InsertMachineInstrRangeInMaps(MIS.begin(), miItr);
00139       }
00140 
00141       // Insert store if necessary.
00142       if (hasDef) {
00143         MachineInstrSpan MIS(miItr);
00144 
00145         tii->storeRegToStackSlot(*mi->getParent(), std::next(miItr), NewVReg,
00146                                  true, ss, trc, tri);
00147         lis->InsertMachineInstrRangeInMaps(std::next(miItr), MIS.end());
00148       }
00149     }
00150   }
00151 };
00152 
00153 } // end anonymous namespace
00154 
00155 namespace {
00156 
00157 /// Spills any live range using the spill-everywhere method with no attempt at
00158 /// folding.
00159 class TrivialSpiller : public SpillerBase {
00160 public:
00161 
00162   TrivialSpiller(MachineFunctionPass &pass, MachineFunction &mf,
00163                  VirtRegMap &vrm)
00164     : SpillerBase(pass, mf, vrm) {}
00165 
00166   void spill(LiveRangeEdit &LRE) override {
00167     // Ignore spillIs - we don't use it.
00168     trivialSpillEverywhere(LRE);
00169   }
00170 };
00171 
00172 } // end anonymous namespace
00173 
00174 void Spiller::anchor() { }
00175 
00176 llvm::Spiller* llvm::createSpiller(MachineFunctionPass &pass,
00177                                    MachineFunction &mf,
00178                                    VirtRegMap &vrm) {
00179   switch (spillerOpt) {
00180   case trivial: return new TrivialSpiller(pass, mf, vrm);
00181   case inline_: return createInlineSpiller(pass, mf, vrm);
00182   }
00183   llvm_unreachable("Invalid spiller optimization");
00184 }