LLVM API Documentation

InterferenceCache.cpp
Go to the documentation of this file.
00001 //===-- InterferenceCache.cpp - Caching per-block interference ---------*--===//
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 // InterferenceCache remembers per-block interference in LiveIntervalUnions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InterferenceCache.h"
00015 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include "llvm/Target/TargetRegisterInfo.h"
00018 
00019 using namespace llvm;
00020 
00021 #define DEBUG_TYPE "regalloc"
00022 
00023 // Static member used for null interference cursors.
00024 InterferenceCache::BlockInterference InterferenceCache::Cursor::NoInterference;
00025 
00026 // Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a
00027 // buffer of size NumPhysRegs to speed up alloc/clear for targets with large
00028 // reg files). Calloced memory is used for good form, and quites tools like
00029 // Valgrind too, but zero initialized memory is not required by the algorithm:
00030 // this is because PhysRegEntries works like a SparseSet and its entries are
00031 // only valid when there is a corresponding CacheEntries assignment. There is
00032 // also support for when pass managers are reused for targets with different
00033 // numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized.
00034 void InterferenceCache::reinitPhysRegEntries() {
00035   if (PhysRegEntriesCount == TRI->getNumRegs()) return;
00036   free(PhysRegEntries);
00037   PhysRegEntriesCount = TRI->getNumRegs();
00038   PhysRegEntries = (unsigned char*)
00039     calloc(PhysRegEntriesCount, sizeof(unsigned char));
00040 }
00041 
00042 void InterferenceCache::init(MachineFunction *mf,
00043                              LiveIntervalUnion *liuarray,
00044                              SlotIndexes *indexes,
00045                              LiveIntervals *lis,
00046                              const TargetRegisterInfo *tri) {
00047   MF = mf;
00048   LIUArray = liuarray;
00049   TRI = tri;
00050   reinitPhysRegEntries();
00051   for (unsigned i = 0; i != CacheEntries; ++i)
00052     Entries[i].clear(mf, indexes, lis);
00053 }
00054 
00055 InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
00056   unsigned E = PhysRegEntries[PhysReg];
00057   if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
00058     if (!Entries[E].valid(LIUArray, TRI))
00059       Entries[E].revalidate(LIUArray, TRI);
00060     return &Entries[E];
00061   }
00062   // No valid entry exists, pick the next round-robin entry.
00063   E = RoundRobin;
00064   if (++RoundRobin == CacheEntries)
00065     RoundRobin = 0;
00066   for (unsigned i = 0; i != CacheEntries; ++i) {
00067     // Skip entries that are in use.
00068     if (Entries[E].hasRefs()) {
00069       if (++E == CacheEntries)
00070         E = 0;
00071       continue;
00072     }
00073     Entries[E].reset(PhysReg, LIUArray, TRI, MF);
00074     PhysRegEntries[PhysReg] = E;
00075     return &Entries[E];
00076   }
00077   llvm_unreachable("Ran out of interference cache entries.");
00078 }
00079 
00080 /// revalidate - LIU contents have changed, update tags.
00081 void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
00082                                           const TargetRegisterInfo *TRI) {
00083   // Invalidate all block entries.
00084   ++Tag;
00085   // Invalidate all iterators.
00086   PrevPos = SlotIndex();
00087   unsigned i = 0;
00088   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
00089     RegUnits[i].VirtTag = LIUArray[*Units].getTag();
00090 }
00091 
00092 void InterferenceCache::Entry::reset(unsigned physReg,
00093                                      LiveIntervalUnion *LIUArray,
00094                                      const TargetRegisterInfo *TRI,
00095                                      const MachineFunction *MF) {
00096   assert(!hasRefs() && "Cannot reset cache entry with references");
00097   // LIU's changed, invalidate cache.
00098   ++Tag;
00099   PhysReg = physReg;
00100   Blocks.resize(MF->getNumBlockIDs());
00101 
00102   // Reset iterators.
00103   PrevPos = SlotIndex();
00104   RegUnits.clear();
00105   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
00106     RegUnits.push_back(LIUArray[*Units]);
00107     RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
00108   }
00109 }
00110 
00111 bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
00112                                      const TargetRegisterInfo *TRI) {
00113   unsigned i = 0, e = RegUnits.size();
00114   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
00115     if (i == e)
00116       return false;
00117     if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
00118       return false;
00119   }
00120   return i == e;
00121 }
00122 
00123 void InterferenceCache::Entry::update(unsigned MBBNum) {
00124   SlotIndex Start, Stop;
00125   std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
00126 
00127   // Use advanceTo only when possible.
00128   if (PrevPos != Start) {
00129     if (!PrevPos.isValid() || Start < PrevPos) {
00130       for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00131         RegUnitInfo &RUI = RegUnits[i];
00132         RUI.VirtI.find(Start);
00133         RUI.FixedI = RUI.Fixed->find(Start);
00134       }
00135     } else {
00136       for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00137         RegUnitInfo &RUI = RegUnits[i];
00138         RUI.VirtI.advanceTo(Start);
00139         if (RUI.FixedI != RUI.Fixed->end())
00140           RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
00141       }
00142     }
00143     PrevPos = Start;
00144   }
00145 
00146   MachineFunction::const_iterator MFI = MF->getBlockNumbered(MBBNum);
00147   BlockInterference *BI = &Blocks[MBBNum];
00148   ArrayRef<SlotIndex> RegMaskSlots;
00149   ArrayRef<const uint32_t*> RegMaskBits;
00150   for (;;) {
00151     BI->Tag = Tag;
00152     BI->First = BI->Last = SlotIndex();
00153 
00154     // Check for first interference from virtregs.
00155     for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00156       LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
00157       if (!I.valid())
00158         continue;
00159       SlotIndex StartI = I.start();
00160       if (StartI >= Stop)
00161         continue;
00162       if (!BI->First.isValid() || StartI < BI->First)
00163         BI->First = StartI;
00164     }
00165 
00166     // Same thing for fixed interference.
00167     for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00168       LiveInterval::const_iterator I = RegUnits[i].FixedI;
00169       LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
00170       if (I == E)
00171         continue;
00172       SlotIndex StartI = I->start;
00173       if (StartI >= Stop)
00174         continue;
00175       if (!BI->First.isValid() || StartI < BI->First)
00176         BI->First = StartI;
00177     }
00178 
00179     // Also check for register mask interference.
00180     RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
00181     RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
00182     SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
00183     for (unsigned i = 0, e = RegMaskSlots.size();
00184          i != e && RegMaskSlots[i] < Limit; ++i)
00185       if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
00186         // Register mask i clobbers PhysReg before the LIU interference.
00187         BI->First = RegMaskSlots[i];
00188         break;
00189       }
00190 
00191     PrevPos = Stop;
00192     if (BI->First.isValid())
00193       break;
00194 
00195     // No interference in this block? Go ahead and precompute the next block.
00196     if (++MFI == MF->end())
00197       return;
00198     MBBNum = MFI->getNumber();
00199     BI = &Blocks[MBBNum];
00200     if (BI->Tag == Tag)
00201       return;
00202     std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
00203   }
00204 
00205   // Check for last interference in block.
00206   for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00207     LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
00208     if (!I.valid() || I.start() >= Stop)
00209       continue;
00210     I.advanceTo(Stop);
00211     bool Backup = !I.valid() || I.start() >= Stop;
00212     if (Backup)
00213       --I;
00214     SlotIndex StopI = I.stop();
00215     if (!BI->Last.isValid() || StopI > BI->Last)
00216       BI->Last = StopI;
00217     if (Backup)
00218       ++I;
00219   }
00220 
00221   // Fixed interference.
00222   for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
00223     LiveInterval::iterator &I = RegUnits[i].FixedI;
00224     LiveRange *LR = RegUnits[i].Fixed;
00225     if (I == LR->end() || I->start >= Stop)
00226       continue;
00227     I = LR->advanceTo(I, Stop);
00228     bool Backup = I == LR->end() || I->start >= Stop;
00229     if (Backup)
00230       --I;
00231     SlotIndex StopI = I->end;
00232     if (!BI->Last.isValid() || StopI > BI->Last)
00233       BI->Last = StopI;
00234     if (Backup)
00235       ++I;
00236   }
00237 
00238   // Also check for register mask interference.
00239   SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
00240   for (unsigned i = RegMaskSlots.size();
00241        i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
00242     if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
00243       // Register mask i-1 clobbers PhysReg after the LIU interference.
00244       // Model the regmask clobber as a dead def.
00245       BI->Last = RegMaskSlots[i-1].getDeadSlot();
00246       break;
00247     }
00248 }