Home | History | Annotate | Download | only in CodeGen
      1 //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
      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 // The inline spiller modifies the machine function directly instead of
     11 // inserting spills and restores in VirtRegMap.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #define DEBUG_TYPE "regalloc"
     16 #include "Spiller.h"
     17 #include "llvm/ADT/SetVector.h"
     18 #include "llvm/ADT/Statistic.h"
     19 #include "llvm/ADT/TinyPtrVector.h"
     20 #include "llvm/Analysis/AliasAnalysis.h"
     21 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
     22 #include "llvm/CodeGen/LiveRangeEdit.h"
     23 #include "llvm/CodeGen/LiveStackAnalysis.h"
     24 #include "llvm/CodeGen/MachineDominators.h"
     25 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
     26 #include "llvm/CodeGen/MachineFrameInfo.h"
     27 #include "llvm/CodeGen/MachineFunction.h"
     28 #include "llvm/CodeGen/MachineInstrBuilder.h"
     29 #include "llvm/CodeGen/MachineInstrBundle.h"
     30 #include "llvm/CodeGen/MachineLoopInfo.h"
     31 #include "llvm/CodeGen/MachineRegisterInfo.h"
     32 #include "llvm/CodeGen/VirtRegMap.h"
     33 #include "llvm/Support/CommandLine.h"
     34 #include "llvm/Support/Debug.h"
     35 #include "llvm/Support/raw_ostream.h"
     36 #include "llvm/Target/TargetInstrInfo.h"
     37 #include "llvm/Target/TargetMachine.h"
     38 
     39 using namespace llvm;
     40 
     41 STATISTIC(NumSpilledRanges,   "Number of spilled live ranges");
     42 STATISTIC(NumSnippets,        "Number of spilled snippets");
     43 STATISTIC(NumSpills,          "Number of spills inserted");
     44 STATISTIC(NumSpillsRemoved,   "Number of spills removed");
     45 STATISTIC(NumReloads,         "Number of reloads inserted");
     46 STATISTIC(NumReloadsRemoved,  "Number of reloads removed");
     47 STATISTIC(NumFolded,          "Number of folded stack accesses");
     48 STATISTIC(NumFoldedLoads,     "Number of folded loads");
     49 STATISTIC(NumRemats,          "Number of rematerialized defs for spilling");
     50 STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads");
     51 STATISTIC(NumHoists,          "Number of hoisted spills");
     52 
     53 static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
     54                                      cl::desc("Disable inline spill hoisting"));
     55 
     56 namespace {
     57 class InlineSpiller : public Spiller {
     58   MachineFunction &MF;
     59   LiveIntervals &LIS;
     60   LiveStacks &LSS;
     61   AliasAnalysis *AA;
     62   MachineDominatorTree &MDT;
     63   MachineLoopInfo &Loops;
     64   VirtRegMap &VRM;
     65   MachineFrameInfo &MFI;
     66   MachineRegisterInfo &MRI;
     67   const TargetInstrInfo &TII;
     68   const TargetRegisterInfo &TRI;
     69   const MachineBlockFrequencyInfo &MBFI;
     70 
     71   // Variables that are valid during spill(), but used by multiple methods.
     72   LiveRangeEdit *Edit;
     73   LiveInterval *StackInt;
     74   int StackSlot;
     75   unsigned Original;
     76 
     77   // All registers to spill to StackSlot, including the main register.
     78   SmallVector<unsigned, 8> RegsToSpill;
     79 
     80   // All COPY instructions to/from snippets.
     81   // They are ignored since both operands refer to the same stack slot.
     82   SmallPtrSet<MachineInstr*, 8> SnippetCopies;
     83 
     84   // Values that failed to remat at some point.
     85   SmallPtrSet<VNInfo*, 8> UsedValues;
     86 
     87 public:
     88   // Information about a value that was defined by a copy from a sibling
     89   // register.
     90   struct SibValueInfo {
     91     // True when all reaching defs were reloads: No spill is necessary.
     92     bool AllDefsAreReloads;
     93 
     94     // True when value is defined by an original PHI not from splitting.
     95     bool DefByOrigPHI;
     96 
     97     // True when the COPY defining this value killed its source.
     98     bool KillsSource;
     99 
    100     // The preferred register to spill.
    101     unsigned SpillReg;
    102 
    103     // The value of SpillReg that should be spilled.
    104     VNInfo *SpillVNI;
    105 
    106     // The block where SpillVNI should be spilled. Currently, this must be the
    107     // block containing SpillVNI->def.
    108     MachineBasicBlock *SpillMBB;
    109 
    110     // A defining instruction that is not a sibling copy or a reload, or NULL.
    111     // This can be used as a template for rematerialization.
    112     MachineInstr *DefMI;
    113 
    114     // List of values that depend on this one.  These values are actually the
    115     // same, but live range splitting has placed them in different registers,
    116     // or SSA update needed to insert PHI-defs to preserve SSA form.  This is
    117     // copies of the current value and phi-kills.  Usually only phi-kills cause
    118     // more than one dependent value.
    119     TinyPtrVector<VNInfo*> Deps;
    120 
    121     SibValueInfo(unsigned Reg, VNInfo *VNI)
    122       : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false),
    123         SpillReg(Reg), SpillVNI(VNI), SpillMBB(0), DefMI(0) {}
    124 
    125     // Returns true when a def has been found.
    126     bool hasDef() const { return DefByOrigPHI || DefMI; }
    127   };
    128 
    129 private:
    130   // Values in RegsToSpill defined by sibling copies.
    131   typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap;
    132   SibValueMap SibValues;
    133 
    134   // Dead defs generated during spilling.
    135   SmallVector<MachineInstr*, 8> DeadDefs;
    136 
    137   ~InlineSpiller() {}
    138 
    139 public:
    140   InlineSpiller(MachineFunctionPass &pass,
    141                 MachineFunction &mf,
    142                 VirtRegMap &vrm)
    143     : MF(mf),
    144       LIS(pass.getAnalysis<LiveIntervals>()),
    145       LSS(pass.getAnalysis<LiveStacks>()),
    146       AA(&pass.getAnalysis<AliasAnalysis>()),
    147       MDT(pass.getAnalysis<MachineDominatorTree>()),
    148       Loops(pass.getAnalysis<MachineLoopInfo>()),
    149       VRM(vrm),
    150       MFI(*mf.getFrameInfo()),
    151       MRI(mf.getRegInfo()),
    152       TII(*mf.getTarget().getInstrInfo()),
    153       TRI(*mf.getTarget().getRegisterInfo()),
    154       MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {}
    155 
    156   void spill(LiveRangeEdit &);
    157 
    158 private:
    159   bool isSnippet(const LiveInterval &SnipLI);
    160   void collectRegsToSpill();
    161 
    162   bool isRegToSpill(unsigned Reg) {
    163     return std::find(RegsToSpill.begin(),
    164                      RegsToSpill.end(), Reg) != RegsToSpill.end();
    165   }
    166 
    167   bool isSibling(unsigned Reg);
    168   MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*);
    169   void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = 0);
    170   void analyzeSiblingValues();
    171 
    172   bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI);
    173   void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
    174 
    175   void markValueUsed(LiveInterval*, VNInfo*);
    176   bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI);
    177   void reMaterializeAll();
    178 
    179   bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
    180   bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
    181                          MachineInstr *LoadMI = 0);
    182   void insertReload(LiveInterval &NewLI, SlotIndex,
    183                     MachineBasicBlock::iterator MI);
    184   void insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
    185                    SlotIndex, MachineBasicBlock::iterator MI);
    186 
    187   void spillAroundUses(unsigned Reg);
    188   void spillAll();
    189 };
    190 }
    191 
    192 namespace llvm {
    193 Spiller *createInlineSpiller(MachineFunctionPass &pass,
    194                              MachineFunction &mf,
    195                              VirtRegMap &vrm) {
    196   return new InlineSpiller(pass, mf, vrm);
    197 }
    198 }
    199 
    200 //===----------------------------------------------------------------------===//
    201 //                                Snippets
    202 //===----------------------------------------------------------------------===//
    203 
    204 // When spilling a virtual register, we also spill any snippets it is connected
    205 // to. The snippets are small live ranges that only have a single real use,
    206 // leftovers from live range splitting. Spilling them enables memory operand
    207 // folding or tightens the live range around the single use.
    208 //
    209 // This minimizes register pressure and maximizes the store-to-load distance for
    210 // spill slots which can be important in tight loops.
    211 
    212 /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
    213 /// otherwise return 0.
    214 static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) {
    215   if (!MI->isFullCopy())
    216     return 0;
    217   if (MI->getOperand(0).getReg() == Reg)
    218       return MI->getOperand(1).getReg();
    219   if (MI->getOperand(1).getReg() == Reg)
    220       return MI->getOperand(0).getReg();
    221   return 0;
    222 }
    223 
    224 /// isSnippet - Identify if a live interval is a snippet that should be spilled.
    225 /// It is assumed that SnipLI is a virtual register with the same original as
    226 /// Edit->getReg().
    227 bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
    228   unsigned Reg = Edit->getReg();
    229 
    230   // A snippet is a tiny live range with only a single instruction using it
    231   // besides copies to/from Reg or spills/fills. We accept:
    232   //
    233   //   %snip = COPY %Reg / FILL fi#
    234   //   %snip = USE %snip
    235   //   %Reg = COPY %snip / SPILL %snip, fi#
    236   //
    237   if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
    238     return false;
    239 
    240   MachineInstr *UseMI = 0;
    241 
    242   // Check that all uses satisfy our criteria.
    243   for (MachineRegisterInfo::reg_nodbg_iterator
    244          RI = MRI.reg_nodbg_begin(SnipLI.reg);
    245        MachineInstr *MI = RI.skipInstruction();) {
    246 
    247     // Allow copies to/from Reg.
    248     if (isFullCopyOf(MI, Reg))
    249       continue;
    250 
    251     // Allow stack slot loads.
    252     int FI;
    253     if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
    254       continue;
    255 
    256     // Allow stack slot stores.
    257     if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
    258       continue;
    259 
    260     // Allow a single additional instruction.
    261     if (UseMI && MI != UseMI)
    262       return false;
    263     UseMI = MI;
    264   }
    265   return true;
    266 }
    267 
    268 /// collectRegsToSpill - Collect live range snippets that only have a single
    269 /// real use.
    270 void InlineSpiller::collectRegsToSpill() {
    271   unsigned Reg = Edit->getReg();
    272 
    273   // Main register always spills.
    274   RegsToSpill.assign(1, Reg);
    275   SnippetCopies.clear();
    276 
    277   // Snippets all have the same original, so there can't be any for an original
    278   // register.
    279   if (Original == Reg)
    280     return;
    281 
    282   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(Reg);
    283        MachineInstr *MI = RI.skipInstruction();) {
    284     unsigned SnipReg = isFullCopyOf(MI, Reg);
    285     if (!isSibling(SnipReg))
    286       continue;
    287     LiveInterval &SnipLI = LIS.getInterval(SnipReg);
    288     if (!isSnippet(SnipLI))
    289       continue;
    290     SnippetCopies.insert(MI);
    291     if (isRegToSpill(SnipReg))
    292       continue;
    293     RegsToSpill.push_back(SnipReg);
    294     DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
    295     ++NumSnippets;
    296   }
    297 }
    298 
    299 
    300 //===----------------------------------------------------------------------===//
    301 //                            Sibling Values
    302 //===----------------------------------------------------------------------===//
    303 
    304 // After live range splitting, some values to be spilled may be defined by
    305 // copies from sibling registers. We trace the sibling copies back to the
    306 // original value if it still exists. We need it for rematerialization.
    307 //
    308 // Even when the value can't be rematerialized, we still want to determine if
    309 // the value has already been spilled, or we may want to hoist the spill from a
    310 // loop.
    311 
    312 bool InlineSpiller::isSibling(unsigned Reg) {
    313   return TargetRegisterInfo::isVirtualRegister(Reg) &&
    314            VRM.getOriginal(Reg) == Original;
    315 }
    316 
    317 #ifndef NDEBUG
    318 static raw_ostream &operator<<(raw_ostream &OS,
    319                                const InlineSpiller::SibValueInfo &SVI) {
    320   OS << "spill " << PrintReg(SVI.SpillReg) << ':'
    321      << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def;
    322   if (SVI.SpillMBB)
    323     OS << " in BB#" << SVI.SpillMBB->getNumber();
    324   if (SVI.AllDefsAreReloads)
    325     OS << " all-reloads";
    326   if (SVI.DefByOrigPHI)
    327     OS << " orig-phi";
    328   if (SVI.KillsSource)
    329     OS << " kill";
    330   OS << " deps[";
    331   for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i)
    332     OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def;
    333   OS << " ]";
    334   if (SVI.DefMI)
    335     OS << " def: " << *SVI.DefMI;
    336   else
    337     OS << '\n';
    338   return OS;
    339 }
    340 #endif
    341 
    342 /// propagateSiblingValue - Propagate the value in SVI to dependents if it is
    343 /// known.  Otherwise remember the dependency for later.
    344 ///
    345 /// @param SVIIter SibValues entry to propagate.
    346 /// @param VNI Dependent value, or NULL to propagate to all saved dependents.
    347 void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter,
    348                                           VNInfo *VNI) {
    349   SibValueMap::value_type *SVI = &*SVIIter;
    350 
    351   // When VNI is non-NULL, add it to SVI's deps, and only propagate to that.
    352   TinyPtrVector<VNInfo*> FirstDeps;
    353   if (VNI) {
    354     FirstDeps.push_back(VNI);
    355     SVI->second.Deps.push_back(VNI);
    356   }
    357 
    358   // Has the value been completely determined yet?  If not, defer propagation.
    359   if (!SVI->second.hasDef())
    360     return;
    361 
    362   // Work list of values to propagate.
    363   SmallSetVector<SibValueMap::value_type *, 8> WorkList;
    364   WorkList.insert(SVI);
    365 
    366   do {
    367     SVI = WorkList.pop_back_val();
    368     TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps;
    369     VNI = 0;
    370 
    371     SibValueInfo &SV = SVI->second;
    372     if (!SV.SpillMBB)
    373       SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def);
    374 
    375     DEBUG(dbgs() << "  prop to " << Deps->size() << ": "
    376                  << SVI->first->id << '@' << SVI->first->def << ":\t" << SV);
    377 
    378     assert(SV.hasDef() && "Propagating undefined value");
    379 
    380     // Should this value be propagated as a preferred spill candidate?  We don't
    381     // propagate values of registers that are about to spill.
    382     bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg);
    383     unsigned SpillDepth = ~0u;
    384 
    385     for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(),
    386          DepE = Deps->end(); DepI != DepE; ++DepI) {
    387       SibValueMap::iterator DepSVI = SibValues.find(*DepI);
    388       assert(DepSVI != SibValues.end() && "Dependent value not in SibValues");
    389       SibValueInfo &DepSV = DepSVI->second;
    390       if (!DepSV.SpillMBB)
    391         DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def);
    392 
    393       bool Changed = false;
    394 
    395       // Propagate defining instruction.
    396       if (!DepSV.hasDef()) {
    397         Changed = true;
    398         DepSV.DefMI = SV.DefMI;
    399         DepSV.DefByOrigPHI = SV.DefByOrigPHI;
    400       }
    401 
    402       // Propagate AllDefsAreReloads.  For PHI values, this computes an AND of
    403       // all predecessors.
    404       if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) {
    405         Changed = true;
    406         DepSV.AllDefsAreReloads = false;
    407       }
    408 
    409       // Propagate best spill value.
    410       if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) {
    411         if (SV.SpillMBB == DepSV.SpillMBB) {
    412           // DepSV is in the same block.  Hoist when dominated.
    413           if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) {
    414             // This is an alternative def earlier in the same MBB.
    415             // Hoist the spill as far as possible in SpillMBB. This can ease
    416             // register pressure:
    417             //
    418             //   x = def
    419             //   y = use x
    420             //   s = copy x
    421             //
    422             // Hoisting the spill of s to immediately after the def removes the
    423             // interference between x and y:
    424             //
    425             //   x = def
    426             //   spill x
    427             //   y = use x<kill>
    428             //
    429             // This hoist only helps when the DepSV copy kills its source.
    430             Changed = true;
    431             DepSV.SpillReg = SV.SpillReg;
    432             DepSV.SpillVNI = SV.SpillVNI;
    433             DepSV.SpillMBB = SV.SpillMBB;
    434           }
    435         } else {
    436           // DepSV is in a different block.
    437           if (SpillDepth == ~0u)
    438             SpillDepth = Loops.getLoopDepth(SV.SpillMBB);
    439 
    440           // Also hoist spills to blocks with smaller loop depth, but make sure
    441           // that the new value dominates.  Non-phi dependents are always
    442           // dominated, phis need checking.
    443           if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) &&
    444               (!DepSVI->first->isPHIDef() ||
    445                MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) {
    446             Changed = true;
    447             DepSV.SpillReg = SV.SpillReg;
    448             DepSV.SpillVNI = SV.SpillVNI;
    449             DepSV.SpillMBB = SV.SpillMBB;
    450           }
    451         }
    452       }
    453 
    454       if (!Changed)
    455         continue;
    456 
    457       // Something changed in DepSVI. Propagate to dependents.
    458       WorkList.insert(&*DepSVI);
    459 
    460       DEBUG(dbgs() << "  update " << DepSVI->first->id << '@'
    461             << DepSVI->first->def << " to:\t" << DepSV);
    462     }
    463   } while (!WorkList.empty());
    464 }
    465 
    466 /// traceSiblingValue - Trace a value that is about to be spilled back to the
    467 /// real defining instructions by looking through sibling copies. Always stay
    468 /// within the range of OrigVNI so the registers are known to carry the same
    469 /// value.
    470 ///
    471 /// Determine if the value is defined by all reloads, so spilling isn't
    472 /// necessary - the value is already in the stack slot.
    473 ///
    474 /// Return a defining instruction that may be a candidate for rematerialization.
    475 ///
    476 MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI,
    477                                                VNInfo *OrigVNI) {
    478   // Check if a cached value already exists.
    479   SibValueMap::iterator SVI;
    480   bool Inserted;
    481   tie(SVI, Inserted) =
    482     SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI)));
    483   if (!Inserted) {
    484     DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':'
    485                  << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second);
    486     return SVI->second.DefMI;
    487   }
    488 
    489   DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':'
    490                << UseVNI->id << '@' << UseVNI->def << '\n');
    491 
    492   // List of (Reg, VNI) that have been inserted into SibValues, but need to be
    493   // processed.
    494   SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList;
    495   WorkList.push_back(std::make_pair(UseReg, UseVNI));
    496 
    497   do {
    498     unsigned Reg;
    499     VNInfo *VNI;
    500     tie(Reg, VNI) = WorkList.pop_back_val();
    501     DEBUG(dbgs() << "  " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def
    502                  << ":\t");
    503 
    504     // First check if this value has already been computed.
    505     SVI = SibValues.find(VNI);
    506     assert(SVI != SibValues.end() && "Missing SibValues entry");
    507 
    508     // Trace through PHI-defs created by live range splitting.
    509     if (VNI->isPHIDef()) {
    510       // Stop at original PHIs.  We don't know the value at the predecessors.
    511       if (VNI->def == OrigVNI->def) {
    512         DEBUG(dbgs() << "orig phi value\n");
    513         SVI->second.DefByOrigPHI = true;
    514         SVI->second.AllDefsAreReloads = false;
    515         propagateSiblingValue(SVI);
    516         continue;
    517       }
    518 
    519       // This is a PHI inserted by live range splitting.  We could trace the
    520       // live-out value from predecessor blocks, but that search can be very
    521       // expensive if there are many predecessors and many more PHIs as
    522       // generated by tail-dup when it sees an indirectbr.  Instead, look at
    523       // all the non-PHI defs that have the same value as OrigVNI.  They must
    524       // jointly dominate VNI->def.  This is not optimal since VNI may actually
    525       // be jointly dominated by a smaller subset of defs, so there is a change
    526       // we will miss a AllDefsAreReloads optimization.
    527 
    528       // Separate all values dominated by OrigVNI into PHIs and non-PHIs.
    529       SmallVector<VNInfo*, 8> PHIs, NonPHIs;
    530       LiveInterval &LI = LIS.getInterval(Reg);
    531       LiveInterval &OrigLI = LIS.getInterval(Original);
    532 
    533       for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end();
    534            VI != VE; ++VI) {
    535         VNInfo *VNI2 = *VI;
    536         if (VNI2->isUnused())
    537           continue;
    538         if (!OrigLI.containsOneValue() &&
    539             OrigLI.getVNInfoAt(VNI2->def) != OrigVNI)
    540           continue;
    541         if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def)
    542           PHIs.push_back(VNI2);
    543         else
    544           NonPHIs.push_back(VNI2);
    545       }
    546       DEBUG(dbgs() << "split phi value, checking " << PHIs.size()
    547                    << " phi-defs, and " << NonPHIs.size()
    548                    << " non-phi/orig defs\n");
    549 
    550       // Create entries for all the PHIs.  Don't add them to the worklist, we
    551       // are processing all of them in one go here.
    552       for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
    553         SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i])));
    554 
    555       // Add every PHI as a dependent of all the non-PHIs.
    556       for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) {
    557         VNInfo *NonPHI = NonPHIs[i];
    558         // Known value? Try an insertion.
    559         tie(SVI, Inserted) =
    560           SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI)));
    561         // Add all the PHIs as dependents of NonPHI.
    562         for (unsigned pi = 0, pe = PHIs.size(); pi != pe; ++pi)
    563           SVI->second.Deps.push_back(PHIs[pi]);
    564         // This is the first time we see NonPHI, add it to the worklist.
    565         if (Inserted)
    566           WorkList.push_back(std::make_pair(Reg, NonPHI));
    567         else
    568           // Propagate to all inserted PHIs, not just VNI.
    569           propagateSiblingValue(SVI);
    570       }
    571 
    572       // Next work list item.
    573       continue;
    574     }
    575 
    576     MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
    577     assert(MI && "Missing def");
    578 
    579     // Trace through sibling copies.
    580     if (unsigned SrcReg = isFullCopyOf(MI, Reg)) {
    581       if (isSibling(SrcReg)) {
    582         LiveInterval &SrcLI = LIS.getInterval(SrcReg);
    583         LiveRangeQuery SrcQ(SrcLI, VNI->def);
    584         assert(SrcQ.valueIn() && "Copy from non-existing value");
    585         // Check if this COPY kills its source.
    586         SVI->second.KillsSource = SrcQ.isKill();
    587         VNInfo *SrcVNI = SrcQ.valueIn();
    588         DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':'
    589                      << SrcVNI->id << '@' << SrcVNI->def
    590                      << " kill=" << unsigned(SVI->second.KillsSource) << '\n');
    591         // Known sibling source value? Try an insertion.
    592         tie(SVI, Inserted) = SibValues.insert(std::make_pair(SrcVNI,
    593                                                  SibValueInfo(SrcReg, SrcVNI)));
    594         // This is the first time we see Src, add it to the worklist.
    595         if (Inserted)
    596           WorkList.push_back(std::make_pair(SrcReg, SrcVNI));
    597         propagateSiblingValue(SVI, VNI);
    598         // Next work list item.
    599         continue;
    600       }
    601     }
    602 
    603     // Track reachable reloads.
    604     SVI->second.DefMI = MI;
    605     SVI->second.SpillMBB = MI->getParent();
    606     int FI;
    607     if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) {
    608       DEBUG(dbgs() << "reload\n");
    609       propagateSiblingValue(SVI);
    610       // Next work list item.
    611       continue;
    612     }
    613 
    614     // Potential remat candidate.
    615     DEBUG(dbgs() << "def " << *MI);
    616     SVI->second.AllDefsAreReloads = false;
    617     propagateSiblingValue(SVI);
    618   } while (!WorkList.empty());
    619 
    620   // Look up the value we were looking for.  We already did this lookup at the
    621   // top of the function, but SibValues may have been invalidated.
    622   SVI = SibValues.find(UseVNI);
    623   assert(SVI != SibValues.end() && "Didn't compute requested info");
    624   DEBUG(dbgs() << "  traced to:\t" << SVI->second);
    625   return SVI->second.DefMI;
    626 }
    627 
    628 /// analyzeSiblingValues - Trace values defined by sibling copies back to
    629 /// something that isn't a sibling copy.
    630 ///
    631 /// Keep track of values that may be rematerializable.
    632 void InlineSpiller::analyzeSiblingValues() {
    633   SibValues.clear();
    634 
    635   // No siblings at all?
    636   if (Edit->getReg() == Original)
    637     return;
    638 
    639   LiveInterval &OrigLI = LIS.getInterval(Original);
    640   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
    641     unsigned Reg = RegsToSpill[i];
    642     LiveInterval &LI = LIS.getInterval(Reg);
    643     for (LiveInterval::const_vni_iterator VI = LI.vni_begin(),
    644          VE = LI.vni_end(); VI != VE; ++VI) {
    645       VNInfo *VNI = *VI;
    646       if (VNI->isUnused())
    647         continue;
    648       MachineInstr *DefMI = 0;
    649       if (!VNI->isPHIDef()) {
    650        DefMI = LIS.getInstructionFromIndex(VNI->def);
    651        assert(DefMI && "No defining instruction");
    652       }
    653       // Check possible sibling copies.
    654       if (VNI->isPHIDef() || DefMI->isCopy()) {
    655         VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
    656         assert(OrigVNI && "Def outside original live range");
    657         if (OrigVNI->def != VNI->def)
    658           DefMI = traceSiblingValue(Reg, VNI, OrigVNI);
    659       }
    660       if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) {
    661         DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@'
    662                      << VNI->def << " may remat from " << *DefMI);
    663       }
    664     }
    665   }
    666 }
    667 
    668 /// hoistSpill - Given a sibling copy that defines a value to be spilled, insert
    669 /// a spill at a better location.
    670 bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) {
    671   SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
    672   VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
    673   assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
    674   SibValueMap::iterator I = SibValues.find(VNI);
    675   if (I == SibValues.end())
    676     return false;
    677 
    678   const SibValueInfo &SVI = I->second;
    679 
    680   // Let the normal folding code deal with the boring case.
    681   if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI)
    682     return false;
    683 
    684   // SpillReg may have been deleted by remat and DCE.
    685   if (!LIS.hasInterval(SVI.SpillReg)) {
    686     DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n');
    687     SibValues.erase(I);
    688     return false;
    689   }
    690 
    691   LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg);
    692   if (!SibLI.containsValue(SVI.SpillVNI)) {
    693     DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n');
    694     SibValues.erase(I);
    695     return false;
    696   }
    697 
    698   // Conservatively extend the stack slot range to the range of the original
    699   // value. We may be able to do better with stack slot coloring by being more
    700   // careful here.
    701   assert(StackInt && "No stack slot assigned yet.");
    702   LiveInterval &OrigLI = LIS.getInterval(Original);
    703   VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
    704   StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
    705   DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
    706                << *StackInt << '\n');
    707 
    708   // Already spilled everywhere.
    709   if (SVI.AllDefsAreReloads) {
    710     DEBUG(dbgs() << "\tno spill needed: " << SVI);
    711     ++NumOmitReloadSpill;
    712     return true;
    713   }
    714   // We are going to spill SVI.SpillVNI immediately after its def, so clear out
    715   // any later spills of the same value.
    716   eliminateRedundantSpills(SibLI, SVI.SpillVNI);
    717 
    718   MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def);
    719   MachineBasicBlock::iterator MII;
    720   if (SVI.SpillVNI->isPHIDef())
    721     MII = MBB->SkipPHIsAndLabels(MBB->begin());
    722   else {
    723     MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def);
    724     assert(DefMI && "Defining instruction disappeared");
    725     MII = DefMI;
    726     ++MII;
    727   }
    728   // Insert spill without kill flag immediately after def.
    729   TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot,
    730                           MRI.getRegClass(SVI.SpillReg), &TRI);
    731   --MII; // Point to store instruction.
    732   LIS.InsertMachineInstrInMaps(MII);
    733   DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII);
    734 
    735   ++NumSpills;
    736   ++NumHoists;
    737   return true;
    738 }
    739 
    740 /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
    741 /// redundant spills of this value in SLI.reg and sibling copies.
    742 void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
    743   assert(VNI && "Missing value");
    744   SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
    745   WorkList.push_back(std::make_pair(&SLI, VNI));
    746   assert(StackInt && "No stack slot assigned yet.");
    747 
    748   do {
    749     LiveInterval *LI;
    750     tie(LI, VNI) = WorkList.pop_back_val();
    751     unsigned Reg = LI->reg;
    752     DEBUG(dbgs() << "Checking redundant spills for "
    753                  << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
    754 
    755     // Regs to spill are taken care of.
    756     if (isRegToSpill(Reg))
    757       continue;
    758 
    759     // Add all of VNI's live range to StackInt.
    760     StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
    761     DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
    762 
    763     // Find all spills and copies of VNI.
    764     for (MachineRegisterInfo::use_nodbg_iterator UI = MRI.use_nodbg_begin(Reg);
    765          MachineInstr *MI = UI.skipInstruction();) {
    766       if (!MI->isCopy() && !MI->mayStore())
    767         continue;
    768       SlotIndex Idx = LIS.getInstructionIndex(MI);
    769       if (LI->getVNInfoAt(Idx) != VNI)
    770         continue;
    771 
    772       // Follow sibling copies down the dominator tree.
    773       if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
    774         if (isSibling(DstReg)) {
    775            LiveInterval &DstLI = LIS.getInterval(DstReg);
    776            VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
    777            assert(DstVNI && "Missing defined value");
    778            assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
    779            WorkList.push_back(std::make_pair(&DstLI, DstVNI));
    780         }
    781         continue;
    782       }
    783 
    784       // Erase spills.
    785       int FI;
    786       if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
    787         DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI);
    788         // eliminateDeadDefs won't normally remove stores, so switch opcode.
    789         MI->setDesc(TII.get(TargetOpcode::KILL));
    790         DeadDefs.push_back(MI);
    791         ++NumSpillsRemoved;
    792         --NumSpills;
    793       }
    794     }
    795   } while (!WorkList.empty());
    796 }
    797 
    798 
    799 //===----------------------------------------------------------------------===//
    800 //                            Rematerialization
    801 //===----------------------------------------------------------------------===//
    802 
    803 /// markValueUsed - Remember that VNI failed to rematerialize, so its defining
    804 /// instruction cannot be eliminated. See through snippet copies
    805 void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
    806   SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
    807   WorkList.push_back(std::make_pair(LI, VNI));
    808   do {
    809     tie(LI, VNI) = WorkList.pop_back_val();
    810     if (!UsedValues.insert(VNI))
    811       continue;
    812 
    813     if (VNI->isPHIDef()) {
    814       MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
    815       for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
    816              PE = MBB->pred_end(); PI != PE; ++PI) {
    817         VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI));
    818         if (PVNI)
    819           WorkList.push_back(std::make_pair(LI, PVNI));
    820       }
    821       continue;
    822     }
    823 
    824     // Follow snippet copies.
    825     MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
    826     if (!SnippetCopies.count(MI))
    827       continue;
    828     LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
    829     assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
    830     VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
    831     assert(SnipVNI && "Snippet undefined before copy");
    832     WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
    833   } while (!WorkList.empty());
    834 }
    835 
    836 /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
    837 bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg,
    838                                      MachineBasicBlock::iterator MI) {
    839   SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
    840   VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
    841 
    842   if (!ParentVNI) {
    843     DEBUG(dbgs() << "\tadding <undef> flags: ");
    844     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
    845       MachineOperand &MO = MI->getOperand(i);
    846       if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
    847         MO.setIsUndef();
    848     }
    849     DEBUG(dbgs() << UseIdx << '\t' << *MI);
    850     return true;
    851   }
    852 
    853   if (SnippetCopies.count(MI))
    854     return false;
    855 
    856   // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy.
    857   LiveRangeEdit::Remat RM(ParentVNI);
    858   SibValueMap::const_iterator SibI = SibValues.find(ParentVNI);
    859   if (SibI != SibValues.end())
    860     RM.OrigMI = SibI->second.DefMI;
    861   if (!Edit->canRematerializeAt(RM, UseIdx, false)) {
    862     markValueUsed(&VirtReg, ParentVNI);
    863     DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI);
    864     return false;
    865   }
    866 
    867   // If the instruction also writes VirtReg.reg, it had better not require the
    868   // same register for uses and defs.
    869   SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
    870   MIBundleOperands::VirtRegInfo RI =
    871     MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
    872   if (RI.Tied) {
    873     markValueUsed(&VirtReg, ParentVNI);
    874     DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI);
    875     return false;
    876   }
    877 
    878   // Before rematerializing into a register for a single instruction, try to
    879   // fold a load into the instruction. That avoids allocating a new register.
    880   if (RM.OrigMI->canFoldAsLoad() &&
    881       foldMemoryOperand(Ops, RM.OrigMI)) {
    882     Edit->markRematerialized(RM.ParentVNI);
    883     ++NumFoldedLoads;
    884     return true;
    885   }
    886 
    887   // Alocate a new register for the remat.
    888   LiveInterval &NewLI = Edit->createFrom(Original);
    889   NewLI.markNotSpillable();
    890 
    891   // Finally we can rematerialize OrigMI before MI.
    892   SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM,
    893                                            TRI);
    894   DEBUG(dbgs() << "\tremat:  " << DefIdx << '\t'
    895                << *LIS.getInstructionFromIndex(DefIdx));
    896 
    897   // Replace operands
    898   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
    899     MachineOperand &MO = MI->getOperand(Ops[i].second);
    900     if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
    901       MO.setReg(NewLI.reg);
    902       MO.setIsKill();
    903     }
    904   }
    905   DEBUG(dbgs() << "\t        " << UseIdx << '\t' << *MI);
    906 
    907   VNInfo *DefVNI = NewLI.getNextValue(DefIdx, LIS.getVNInfoAllocator());
    908   NewLI.addRange(LiveRange(DefIdx, UseIdx.getRegSlot(), DefVNI));
    909   DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
    910   ++NumRemats;
    911   return true;
    912 }
    913 
    914 /// reMaterializeAll - Try to rematerialize as many uses as possible,
    915 /// and trim the live ranges after.
    916 void InlineSpiller::reMaterializeAll() {
    917   // analyzeSiblingValues has already tested all relevant defining instructions.
    918   if (!Edit->anyRematerializable(AA))
    919     return;
    920 
    921   UsedValues.clear();
    922 
    923   // Try to remat before all uses of snippets.
    924   bool anyRemat = false;
    925   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
    926     unsigned Reg = RegsToSpill[i];
    927     LiveInterval &LI = LIS.getInterval(Reg);
    928     for (MachineRegisterInfo::use_nodbg_iterator
    929          RI = MRI.use_nodbg_begin(Reg);
    930          MachineInstr *MI = RI.skipBundle();)
    931       anyRemat |= reMaterializeFor(LI, MI);
    932   }
    933   if (!anyRemat)
    934     return;
    935 
    936   // Remove any values that were completely rematted.
    937   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
    938     unsigned Reg = RegsToSpill[i];
    939     LiveInterval &LI = LIS.getInterval(Reg);
    940     for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
    941          I != E; ++I) {
    942       VNInfo *VNI = *I;
    943       if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
    944         continue;
    945       MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
    946       MI->addRegisterDead(Reg, &TRI);
    947       if (!MI->allDefsAreDead())
    948         continue;
    949       DEBUG(dbgs() << "All defs dead: " << *MI);
    950       DeadDefs.push_back(MI);
    951     }
    952   }
    953 
    954   // Eliminate dead code after remat. Note that some snippet copies may be
    955   // deleted here.
    956   if (DeadDefs.empty())
    957     return;
    958   DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
    959   Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
    960 
    961   // Get rid of deleted and empty intervals.
    962   unsigned ResultPos = 0;
    963   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
    964     unsigned Reg = RegsToSpill[i];
    965     if (!LIS.hasInterval(Reg))
    966       continue;
    967 
    968     LiveInterval &LI = LIS.getInterval(Reg);
    969     if (LI.empty()) {
    970       Edit->eraseVirtReg(Reg);
    971       continue;
    972     }
    973 
    974     RegsToSpill[ResultPos++] = Reg;
    975   }
    976   RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
    977   DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
    978 }
    979 
    980 
    981 //===----------------------------------------------------------------------===//
    982 //                                 Spilling
    983 //===----------------------------------------------------------------------===//
    984 
    985 /// If MI is a load or store of StackSlot, it can be removed.
    986 bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
    987   int FI = 0;
    988   unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI);
    989   bool IsLoad = InstrReg;
    990   if (!IsLoad)
    991     InstrReg = TII.isStoreToStackSlot(MI, FI);
    992 
    993   // We have a stack access. Is it the right register and slot?
    994   if (InstrReg != Reg || FI != StackSlot)
    995     return false;
    996 
    997   DEBUG(dbgs() << "Coalescing stack access: " << *MI);
    998   LIS.RemoveMachineInstrFromMaps(MI);
    999   MI->eraseFromParent();
   1000 
   1001   if (IsLoad) {
   1002     ++NumReloadsRemoved;
   1003     --NumReloads;
   1004   } else {
   1005     ++NumSpillsRemoved;
   1006     --NumSpills;
   1007   }
   1008 
   1009   return true;
   1010 }
   1011 
   1012 /// foldMemoryOperand - Try folding stack slot references in Ops into their
   1013 /// instructions.
   1014 ///
   1015 /// @param Ops    Operand indices from analyzeVirtReg().
   1016 /// @param LoadMI Load instruction to use instead of stack slot when non-null.
   1017 /// @return       True on success.
   1018 bool InlineSpiller::
   1019 foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
   1020                   MachineInstr *LoadMI) {
   1021   if (Ops.empty())
   1022     return false;
   1023   // Don't attempt folding in bundles.
   1024   MachineInstr *MI = Ops.front().first;
   1025   if (Ops.back().first != MI || MI->isBundled())
   1026     return false;
   1027 
   1028   bool WasCopy = MI->isCopy();
   1029   unsigned ImpReg = 0;
   1030 
   1031   // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
   1032   // operands.
   1033   SmallVector<unsigned, 8> FoldOps;
   1034   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
   1035     unsigned Idx = Ops[i].second;
   1036     MachineOperand &MO = MI->getOperand(Idx);
   1037     if (MO.isImplicit()) {
   1038       ImpReg = MO.getReg();
   1039       continue;
   1040     }
   1041     // FIXME: Teach targets to deal with subregs.
   1042     if (MO.getSubReg())
   1043       return false;
   1044     // We cannot fold a load instruction into a def.
   1045     if (LoadMI && MO.isDef())
   1046       return false;
   1047     // Tied use operands should not be passed to foldMemoryOperand.
   1048     if (!MI->isRegTiedToDefOperand(Idx))
   1049       FoldOps.push_back(Idx);
   1050   }
   1051 
   1052   MachineInstr *FoldMI =
   1053                 LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI)
   1054                        : TII.foldMemoryOperand(MI, FoldOps, StackSlot);
   1055   if (!FoldMI)
   1056     return false;
   1057 
   1058   // Remove LIS for any dead defs in the original MI not in FoldMI.
   1059   for (MIBundleOperands MO(MI); MO.isValid(); ++MO) {
   1060     if (!MO->isReg())
   1061       continue;
   1062     unsigned Reg = MO->getReg();
   1063     if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
   1064         MRI.isReserved(Reg)) {
   1065       continue;
   1066     }
   1067     MIBundleOperands::PhysRegInfo RI =
   1068       MIBundleOperands(FoldMI).analyzePhysReg(Reg, &TRI);
   1069     if (MO->readsReg()) {
   1070       assert(RI.Reads && "Cannot fold physreg reader");
   1071       continue;
   1072     }
   1073     if (RI.Defines)
   1074       continue;
   1075     // FoldMI does not define this physreg. Remove the LI segment.
   1076     assert(MO->isDead() && "Cannot fold physreg def");
   1077     for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) {
   1078       if (LiveInterval *LI = LIS.getCachedRegUnit(*Units)) {
   1079         SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
   1080         if (VNInfo *VNI = LI->getVNInfoAt(Idx))
   1081           LI->removeValNo(VNI);
   1082       }
   1083     }
   1084   }
   1085   LIS.ReplaceMachineInstrInMaps(MI, FoldMI);
   1086   MI->eraseFromParent();
   1087 
   1088   // TII.foldMemoryOperand may have left some implicit operands on the
   1089   // instruction.  Strip them.
   1090   if (ImpReg)
   1091     for (unsigned i = FoldMI->getNumOperands(); i; --i) {
   1092       MachineOperand &MO = FoldMI->getOperand(i - 1);
   1093       if (!MO.isReg() || !MO.isImplicit())
   1094         break;
   1095       if (MO.getReg() == ImpReg)
   1096         FoldMI->RemoveOperand(i - 1);
   1097     }
   1098 
   1099   DEBUG(dbgs() << "\tfolded:  " << LIS.getInstructionIndex(FoldMI) << '\t'
   1100                << *FoldMI);
   1101   if (!WasCopy)
   1102     ++NumFolded;
   1103   else if (Ops.front().second == 0)
   1104     ++NumSpills;
   1105   else
   1106     ++NumReloads;
   1107   return true;
   1108 }
   1109 
   1110 /// insertReload - Insert a reload of NewLI.reg before MI.
   1111 void InlineSpiller::insertReload(LiveInterval &NewLI,
   1112                                  SlotIndex Idx,
   1113                                  MachineBasicBlock::iterator MI) {
   1114   MachineBasicBlock &MBB = *MI->getParent();
   1115   TII.loadRegFromStackSlot(MBB, MI, NewLI.reg, StackSlot,
   1116                            MRI.getRegClass(NewLI.reg), &TRI);
   1117   --MI; // Point to load instruction.
   1118   SlotIndex LoadIdx = LIS.InsertMachineInstrInMaps(MI).getRegSlot();
   1119   // Some (out-of-tree) targets have EC reload instructions.
   1120   if (MachineOperand *MO = MI->findRegisterDefOperand(NewLI.reg))
   1121     if (MO->isEarlyClobber())
   1122       LoadIdx = LoadIdx.getRegSlot(true);
   1123   DEBUG(dbgs() << "\treload:  " << LoadIdx << '\t' << *MI);
   1124   VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, LIS.getVNInfoAllocator());
   1125   NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI));
   1126   ++NumReloads;
   1127 }
   1128 
   1129 /// insertSpill - Insert a spill of NewLI.reg after MI.
   1130 void InlineSpiller::insertSpill(LiveInterval &NewLI, const LiveInterval &OldLI,
   1131                                 SlotIndex Idx, MachineBasicBlock::iterator MI) {
   1132   MachineBasicBlock &MBB = *MI->getParent();
   1133   TII.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, StackSlot,
   1134                           MRI.getRegClass(NewLI.reg), &TRI);
   1135   --MI; // Point to store instruction.
   1136   SlotIndex StoreIdx = LIS.InsertMachineInstrInMaps(MI).getRegSlot();
   1137   DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI);
   1138   VNInfo *StoreVNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
   1139   NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI));
   1140   ++NumSpills;
   1141 }
   1142 
   1143 /// spillAroundUses - insert spill code around each use of Reg.
   1144 void InlineSpiller::spillAroundUses(unsigned Reg) {
   1145   DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
   1146   LiveInterval &OldLI = LIS.getInterval(Reg);
   1147 
   1148   // Iterate over instructions using Reg.
   1149   for (MachineRegisterInfo::reg_iterator RegI = MRI.reg_begin(Reg);
   1150        MachineInstr *MI = RegI.skipBundle();) {
   1151 
   1152     // Debug values are not allowed to affect codegen.
   1153     if (MI->isDebugValue()) {
   1154       // Modify DBG_VALUE now that the value is in a spill slot.
   1155       bool IsIndirect = MI->getOperand(1).isImm();
   1156       uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
   1157       const MDNode *MDPtr = MI->getOperand(2).getMetadata();
   1158       DebugLoc DL = MI->getDebugLoc();
   1159       DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
   1160       MachineBasicBlock *MBB = MI->getParent();
   1161       BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
   1162           .addFrameIndex(StackSlot).addImm(Offset).addMetadata(MDPtr);
   1163       continue;
   1164     }
   1165 
   1166     // Ignore copies to/from snippets. We'll delete them.
   1167     if (SnippetCopies.count(MI))
   1168       continue;
   1169 
   1170     // Stack slot accesses may coalesce away.
   1171     if (coalesceStackAccess(MI, Reg))
   1172       continue;
   1173 
   1174     // Analyze instruction.
   1175     SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
   1176     MIBundleOperands::VirtRegInfo RI =
   1177       MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops);
   1178 
   1179     // Find the slot index where this instruction reads and writes OldLI.
   1180     // This is usually the def slot, except for tied early clobbers.
   1181     SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
   1182     if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
   1183       if (SlotIndex::isSameInstr(Idx, VNI->def))
   1184         Idx = VNI->def;
   1185 
   1186     // Check for a sibling copy.
   1187     unsigned SibReg = isFullCopyOf(MI, Reg);
   1188     if (SibReg && isSibling(SibReg)) {
   1189       // This may actually be a copy between snippets.
   1190       if (isRegToSpill(SibReg)) {
   1191         DEBUG(dbgs() << "Found new snippet copy: " << *MI);
   1192         SnippetCopies.insert(MI);
   1193         continue;
   1194       }
   1195       if (RI.Writes) {
   1196         // Hoist the spill of a sib-reg copy.
   1197         if (hoistSpill(OldLI, MI)) {
   1198           // This COPY is now dead, the value is already in the stack slot.
   1199           MI->getOperand(0).setIsDead();
   1200           DeadDefs.push_back(MI);
   1201           continue;
   1202         }
   1203       } else {
   1204         // This is a reload for a sib-reg copy. Drop spills downstream.
   1205         LiveInterval &SibLI = LIS.getInterval(SibReg);
   1206         eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
   1207         // The COPY will fold to a reload below.
   1208       }
   1209     }
   1210 
   1211     // Attempt to fold memory ops.
   1212     if (foldMemoryOperand(Ops))
   1213       continue;
   1214 
   1215     // Allocate interval around instruction.
   1216     // FIXME: Infer regclass from instruction alone.
   1217     LiveInterval &NewLI = Edit->createFrom(Reg);
   1218     NewLI.markNotSpillable();
   1219 
   1220     if (RI.Reads)
   1221       insertReload(NewLI, Idx, MI);
   1222 
   1223     // Rewrite instruction operands.
   1224     bool hasLiveDef = false;
   1225     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
   1226       MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);
   1227       MO.setReg(NewLI.reg);
   1228       if (MO.isUse()) {
   1229         if (!Ops[i].first->isRegTiedToDefOperand(Ops[i].second))
   1230           MO.setIsKill();
   1231       } else {
   1232         if (!MO.isDead())
   1233           hasLiveDef = true;
   1234       }
   1235     }
   1236     DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI);
   1237 
   1238     // FIXME: Use a second vreg if instruction has no tied ops.
   1239     if (RI.Writes) {
   1240       if (hasLiveDef)
   1241         insertSpill(NewLI, OldLI, Idx, MI);
   1242       else {
   1243         // This instruction defines a dead value.  We don't need to spill it,
   1244         // but do create a live range for the dead value.
   1245         VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
   1246         NewLI.addRange(LiveRange(Idx, Idx.getDeadSlot(), VNI));
   1247       }
   1248     }
   1249 
   1250     DEBUG(dbgs() << "\tinterval: " << NewLI << '\n');
   1251   }
   1252 }
   1253 
   1254 /// spillAll - Spill all registers remaining after rematerialization.
   1255 void InlineSpiller::spillAll() {
   1256   // Update LiveStacks now that we are committed to spilling.
   1257   if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
   1258     StackSlot = VRM.assignVirt2StackSlot(Original);
   1259     StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
   1260     StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
   1261   } else
   1262     StackInt = &LSS.getInterval(StackSlot);
   1263 
   1264   if (Original != Edit->getReg())
   1265     VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
   1266 
   1267   assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
   1268   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
   1269     StackInt->MergeRangesInAsValue(LIS.getInterval(RegsToSpill[i]),
   1270                                    StackInt->getValNumInfo(0));
   1271   DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
   1272 
   1273   // Spill around uses of all RegsToSpill.
   1274   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
   1275     spillAroundUses(RegsToSpill[i]);
   1276 
   1277   // Hoisted spills may cause dead code.
   1278   if (!DeadDefs.empty()) {
   1279     DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
   1280     Edit->eliminateDeadDefs(DeadDefs, RegsToSpill);
   1281   }
   1282 
   1283   // Finally delete the SnippetCopies.
   1284   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) {
   1285     for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(RegsToSpill[i]);
   1286          MachineInstr *MI = RI.skipInstruction();) {
   1287       assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy");
   1288       // FIXME: Do this with a LiveRangeEdit callback.
   1289       LIS.RemoveMachineInstrFromMaps(MI);
   1290       MI->eraseFromParent();
   1291     }
   1292   }
   1293 
   1294   // Delete all spilled registers.
   1295   for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i)
   1296     Edit->eraseVirtReg(RegsToSpill[i]);
   1297 }
   1298 
   1299 void InlineSpiller::spill(LiveRangeEdit &edit) {
   1300   ++NumSpilledRanges;
   1301   Edit = &edit;
   1302   assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
   1303          && "Trying to spill a stack slot.");
   1304   // Share a stack slot among all descendants of Original.
   1305   Original = VRM.getOriginal(edit.getReg());
   1306   StackSlot = VRM.getStackSlot(Original);
   1307   StackInt = 0;
   1308 
   1309   DEBUG(dbgs() << "Inline spilling "
   1310                << MRI.getRegClass(edit.getReg())->getName()
   1311                << ':' << PrintReg(edit.getReg()) << ' ' << edit.getParent()
   1312                << "\nFrom original " << LIS.getInterval(Original) << '\n');
   1313   assert(edit.getParent().isSpillable() &&
   1314          "Attempting to spill already spilled value.");
   1315   assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
   1316 
   1317   collectRegsToSpill();
   1318   analyzeSiblingValues();
   1319   reMaterializeAll();
   1320 
   1321   // Remat may handle everything.
   1322   if (!RegsToSpill.empty())
   1323     spillAll();
   1324 
   1325   Edit->calculateRegClassAndHint(MF, Loops, MBFI);
   1326 }
   1327