LLVM API Documentation

LiveIntervalAnalysis.h
Go to the documentation of this file.
00001 //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
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 implements the LiveInterval analysis pass.  Given some numbering of
00011 // each the machine instructions (in this implemention depth-first order) an
00012 // interval [i, j) is said to be a live interval for register v if there is no
00013 // instruction with number j' > j such that v is live at j' and there is no
00014 // instruction with number i' < i such that v is live at i'. In this
00015 // implementation intervals can have holes, i.e. an interval might look like
00016 // [1,20), [50,65), [1000,1001).
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_CODEGEN_LIVEINTERVALANALYSIS_H
00021 #define LLVM_CODEGEN_LIVEINTERVALANALYSIS_H
00022 
00023 #include "llvm/ADT/IndexedMap.h"
00024 #include "llvm/ADT/SmallVector.h"
00025 #include "llvm/CodeGen/LiveInterval.h"
00026 #include "llvm/CodeGen/MachineBasicBlock.h"
00027 #include "llvm/CodeGen/MachineFunctionPass.h"
00028 #include "llvm/CodeGen/SlotIndexes.h"
00029 #include "llvm/Support/Allocator.h"
00030 #include "llvm/Target/TargetRegisterInfo.h"
00031 #include <cmath>
00032 #include <iterator>
00033 
00034 namespace llvm {
00035 
00036   class AliasAnalysis;
00037   class BitVector;
00038   class BlockFrequency;
00039   class LiveRangeCalc;
00040   class LiveVariables;
00041   class MachineDominatorTree;
00042   class MachineLoopInfo;
00043   class TargetRegisterInfo;
00044   class MachineRegisterInfo;
00045   class TargetInstrInfo;
00046   class TargetRegisterClass;
00047   class VirtRegMap;
00048   class MachineBlockFrequencyInfo;
00049 
00050   class LiveIntervals : public MachineFunctionPass {
00051     MachineFunction* MF;
00052     MachineRegisterInfo* MRI;
00053     const TargetMachine* TM;
00054     const TargetRegisterInfo* TRI;
00055     const TargetInstrInfo* TII;
00056     AliasAnalysis *AA;
00057     SlotIndexes* Indexes;
00058     MachineDominatorTree *DomTree;
00059     LiveRangeCalc *LRCalc;
00060 
00061     /// Special pool allocator for VNInfo's (LiveInterval val#).
00062     ///
00063     VNInfo::Allocator VNInfoAllocator;
00064 
00065     /// Live interval pointers for all the virtual registers.
00066     IndexedMap<LiveInterval*, VirtReg2IndexFunctor> VirtRegIntervals;
00067 
00068     /// RegMaskSlots - Sorted list of instructions with register mask operands.
00069     /// Always use the 'r' slot, RegMasks are normal clobbers, not early
00070     /// clobbers.
00071     SmallVector<SlotIndex, 8> RegMaskSlots;
00072 
00073     /// RegMaskBits - This vector is parallel to RegMaskSlots, it holds a
00074     /// pointer to the corresponding register mask.  This pointer can be
00075     /// recomputed as:
00076     ///
00077     ///   MI = Indexes->getInstructionFromIndex(RegMaskSlot[N]);
00078     ///   unsigned OpNum = findRegMaskOperand(MI);
00079     ///   RegMaskBits[N] = MI->getOperand(OpNum).getRegMask();
00080     ///
00081     /// This is kept in a separate vector partly because some standard
00082     /// libraries don't support lower_bound() with mixed objects, partly to
00083     /// improve locality when searching in RegMaskSlots.
00084     /// Also see the comment in LiveInterval::find().
00085     SmallVector<const uint32_t*, 8> RegMaskBits;
00086 
00087     /// For each basic block number, keep (begin, size) pairs indexing into the
00088     /// RegMaskSlots and RegMaskBits arrays.
00089     /// Note that basic block numbers may not be layout contiguous, that's why
00090     /// we can't just keep track of the first register mask in each basic
00091     /// block.
00092     SmallVector<std::pair<unsigned, unsigned>, 8> RegMaskBlocks;
00093 
00094     /// Keeps a live range set for each register unit to track fixed physreg
00095     /// interference.
00096     SmallVector<LiveRange*, 0> RegUnitRanges;
00097 
00098   public:
00099     static char ID; // Pass identification, replacement for typeid
00100     LiveIntervals();
00101     virtual ~LiveIntervals();
00102 
00103     // Calculate the spill weight to assign to a single instruction.
00104     static float getSpillWeight(bool isDef, bool isUse,
00105                                 const MachineBlockFrequencyInfo *MBFI,
00106                                 const MachineInstr *Instr);
00107 
00108     LiveInterval &getInterval(unsigned Reg) {
00109       if (hasInterval(Reg))
00110         return *VirtRegIntervals[Reg];
00111       else
00112         return createAndComputeVirtRegInterval(Reg);
00113     }
00114 
00115     const LiveInterval &getInterval(unsigned Reg) const {
00116       return const_cast<LiveIntervals*>(this)->getInterval(Reg);
00117     }
00118 
00119     bool hasInterval(unsigned Reg) const {
00120       return VirtRegIntervals.inBounds(Reg) && VirtRegIntervals[Reg];
00121     }
00122 
00123     // Interval creation.
00124     LiveInterval &createEmptyInterval(unsigned Reg) {
00125       assert(!hasInterval(Reg) && "Interval already exists!");
00126       VirtRegIntervals.grow(Reg);
00127       VirtRegIntervals[Reg] = createInterval(Reg);
00128       return *VirtRegIntervals[Reg];
00129     }
00130 
00131     LiveInterval &createAndComputeVirtRegInterval(unsigned Reg) {
00132       LiveInterval &LI = createEmptyInterval(Reg);
00133       computeVirtRegInterval(LI);
00134       return LI;
00135     }
00136 
00137     // Interval removal.
00138     void removeInterval(unsigned Reg) {
00139       delete VirtRegIntervals[Reg];
00140       VirtRegIntervals[Reg] = nullptr;
00141     }
00142 
00143     /// Given a register and an instruction, adds a live segment from that
00144     /// instruction to the end of its MBB.
00145     LiveInterval::Segment addSegmentToEndOfBlock(unsigned reg,
00146                                                  MachineInstr* startInst);
00147 
00148     /// shrinkToUses - After removing some uses of a register, shrink its live
00149     /// range to just the remaining uses. This method does not compute reaching
00150     /// defs for new uses, and it doesn't remove dead defs.
00151     /// Dead PHIDef values are marked as unused.
00152     /// New dead machine instructions are added to the dead vector.
00153     /// Return true if the interval may have been separated into multiple
00154     /// connected components.
00155     bool shrinkToUses(LiveInterval *li,
00156                       SmallVectorImpl<MachineInstr*> *dead = nullptr);
00157 
00158     /// \brief Walk the values in the given interval and compute which ones
00159     /// are dead.  Dead values are not deleted, however:
00160     /// - Dead PHIDef values are marked as unused.
00161     /// - New dead machine instructions are added to the dead vector.
00162     /// - CanSeparate is set to true if the interval may have been separated
00163     ///   into multiple connected components.
00164     void computeDeadValues(LiveInterval *li,
00165                            LiveRange &LR,
00166                            bool *CanSeparate,
00167                            SmallVectorImpl<MachineInstr*> *dead);
00168 
00169     /// extendToIndices - Extend the live range of LI to reach all points in
00170     /// Indices. The points in the Indices array must be jointly dominated by
00171     /// existing defs in LI. PHI-defs are added as needed to maintain SSA form.
00172     ///
00173     /// If a SlotIndex in Indices is the end index of a basic block, LI will be
00174     /// extended to be live out of the basic block.
00175     ///
00176     /// See also LiveRangeCalc::extend().
00177     void extendToIndices(LiveRange &LR, ArrayRef<SlotIndex> Indices);
00178 
00179     /// pruneValue - If an LI value is live at Kill, prune its live range by
00180     /// removing any liveness reachable from Kill. Add live range end points to
00181     /// EndPoints such that extendToIndices(LI, EndPoints) will reconstruct the
00182     /// value's live range.
00183     ///
00184     /// Calling pruneValue() and extendToIndices() can be used to reconstruct
00185     /// SSA form after adding defs to a virtual register.
00186     void pruneValue(LiveInterval *LI, SlotIndex Kill,
00187                     SmallVectorImpl<SlotIndex> *EndPoints);
00188 
00189     SlotIndexes *getSlotIndexes() const {
00190       return Indexes;
00191     }
00192 
00193     AliasAnalysis *getAliasAnalysis() const {
00194       return AA;
00195     }
00196 
00197     /// isNotInMIMap - returns true if the specified machine instr has been
00198     /// removed or was never entered in the map.
00199     bool isNotInMIMap(const MachineInstr* Instr) const {
00200       return !Indexes->hasIndex(Instr);
00201     }
00202 
00203     /// Returns the base index of the given instruction.
00204     SlotIndex getInstructionIndex(const MachineInstr *instr) const {
00205       return Indexes->getInstructionIndex(instr);
00206     }
00207 
00208     /// Returns the instruction associated with the given index.
00209     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
00210       return Indexes->getInstructionFromIndex(index);
00211     }
00212 
00213     /// Return the first index in the given basic block.
00214     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
00215       return Indexes->getMBBStartIdx(mbb);
00216     }
00217 
00218     /// Return the last index in the given basic block.
00219     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
00220       return Indexes->getMBBEndIdx(mbb);
00221     }
00222 
00223     bool isLiveInToMBB(const LiveRange &LR,
00224                        const MachineBasicBlock *mbb) const {
00225       return LR.liveAt(getMBBStartIdx(mbb));
00226     }
00227 
00228     bool isLiveOutOfMBB(const LiveRange &LR,
00229                         const MachineBasicBlock *mbb) const {
00230       return LR.liveAt(getMBBEndIdx(mbb).getPrevSlot());
00231     }
00232 
00233     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
00234       return Indexes->getMBBFromIndex(index);
00235     }
00236 
00237     void insertMBBInMaps(MachineBasicBlock *MBB) {
00238       Indexes->insertMBBInMaps(MBB);
00239       assert(unsigned(MBB->getNumber()) == RegMaskBlocks.size() &&
00240              "Blocks must be added in order.");
00241       RegMaskBlocks.push_back(std::make_pair(RegMaskSlots.size(), 0));
00242     }
00243 
00244     SlotIndex InsertMachineInstrInMaps(MachineInstr *MI) {
00245       return Indexes->insertMachineInstrInMaps(MI);
00246     }
00247 
00248     void InsertMachineInstrRangeInMaps(MachineBasicBlock::iterator B,
00249                                        MachineBasicBlock::iterator E) {
00250       for (MachineBasicBlock::iterator I = B; I != E; ++I)
00251         Indexes->insertMachineInstrInMaps(I);
00252     }
00253 
00254     void RemoveMachineInstrFromMaps(MachineInstr *MI) {
00255       Indexes->removeMachineInstrFromMaps(MI);
00256     }
00257 
00258     void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
00259       Indexes->replaceMachineInstrInMaps(MI, NewMI);
00260     }
00261 
00262     bool findLiveInMBBs(SlotIndex Start, SlotIndex End,
00263                         SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
00264       return Indexes->findLiveInMBBs(Start, End, MBBs);
00265     }
00266 
00267     VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; }
00268 
00269     void getAnalysisUsage(AnalysisUsage &AU) const override;
00270     void releaseMemory() override;
00271 
00272     /// runOnMachineFunction - pass entry point
00273     bool runOnMachineFunction(MachineFunction&) override;
00274 
00275     /// print - Implement the dump method.
00276     void print(raw_ostream &O, const Module* = nullptr) const override;
00277 
00278     /// intervalIsInOneMBB - If LI is confined to a single basic block, return
00279     /// a pointer to that block.  If LI is live in to or out of any block,
00280     /// return NULL.
00281     MachineBasicBlock *intervalIsInOneMBB(const LiveInterval &LI) const;
00282 
00283     /// Returns true if VNI is killed by any PHI-def values in LI.
00284     /// This may conservatively return true to avoid expensive computations.
00285     bool hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const;
00286 
00287     /// addKillFlags - Add kill flags to any instruction that kills a virtual
00288     /// register.
00289     void addKillFlags(const VirtRegMap*);
00290 
00291     /// handleMove - call this method to notify LiveIntervals that
00292     /// instruction 'mi' has been moved within a basic block. This will update
00293     /// the live intervals for all operands of mi. Moves between basic blocks
00294     /// are not supported.
00295     ///
00296     /// \param UpdateFlags Update live intervals for nonallocatable physregs.
00297     void handleMove(MachineInstr* MI, bool UpdateFlags = false);
00298 
00299     /// moveIntoBundle - Update intervals for operands of MI so that they
00300     /// begin/end on the SlotIndex for BundleStart.
00301     ///
00302     /// \param UpdateFlags Update live intervals for nonallocatable physregs.
00303     ///
00304     /// Requires MI and BundleStart to have SlotIndexes, and assumes
00305     /// existing liveness is accurate. BundleStart should be the first
00306     /// instruction in the Bundle.
00307     void handleMoveIntoBundle(MachineInstr* MI, MachineInstr* BundleStart,
00308                               bool UpdateFlags = false);
00309 
00310     /// repairIntervalsInRange - Update live intervals for instructions in a
00311     /// range of iterators. It is intended for use after target hooks that may
00312     /// insert or remove instructions, and is only efficient for a small number
00313     /// of instructions.
00314     ///
00315     /// OrigRegs is a vector of registers that were originally used by the
00316     /// instructions in the range between the two iterators.
00317     ///
00318     /// Currently, the only only changes that are supported are simple removal
00319     /// and addition of uses.
00320     void repairIntervalsInRange(MachineBasicBlock *MBB,
00321                                 MachineBasicBlock::iterator Begin,
00322                                 MachineBasicBlock::iterator End,
00323                                 ArrayRef<unsigned> OrigRegs);
00324 
00325     // Register mask functions.
00326     //
00327     // Machine instructions may use a register mask operand to indicate that a
00328     // large number of registers are clobbered by the instruction.  This is
00329     // typically used for calls.
00330     //
00331     // For compile time performance reasons, these clobbers are not recorded in
00332     // the live intervals for individual physical registers.  Instead,
00333     // LiveIntervalAnalysis maintains a sorted list of instructions with
00334     // register mask operands.
00335 
00336     /// getRegMaskSlots - Returns a sorted array of slot indices of all
00337     /// instructions with register mask operands.
00338     ArrayRef<SlotIndex> getRegMaskSlots() const { return RegMaskSlots; }
00339 
00340     /// getRegMaskSlotsInBlock - Returns a sorted array of slot indices of all
00341     /// instructions with register mask operands in the basic block numbered
00342     /// MBBNum.
00343     ArrayRef<SlotIndex> getRegMaskSlotsInBlock(unsigned MBBNum) const {
00344       std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
00345       return getRegMaskSlots().slice(P.first, P.second);
00346     }
00347 
00348     /// getRegMaskBits() - Returns an array of register mask pointers
00349     /// corresponding to getRegMaskSlots().
00350     ArrayRef<const uint32_t*> getRegMaskBits() const { return RegMaskBits; }
00351 
00352     /// getRegMaskBitsInBlock - Returns an array of mask pointers corresponding
00353     /// to getRegMaskSlotsInBlock(MBBNum).
00354     ArrayRef<const uint32_t*> getRegMaskBitsInBlock(unsigned MBBNum) const {
00355       std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
00356       return getRegMaskBits().slice(P.first, P.second);
00357     }
00358 
00359     /// checkRegMaskInterference - Test if LI is live across any register mask
00360     /// instructions, and compute a bit mask of physical registers that are not
00361     /// clobbered by any of them.
00362     ///
00363     /// Returns false if LI doesn't cross any register mask instructions. In
00364     /// that case, the bit vector is not filled in.
00365     bool checkRegMaskInterference(LiveInterval &LI,
00366                                   BitVector &UsableRegs);
00367 
00368     // Register unit functions.
00369     //
00370     // Fixed interference occurs when MachineInstrs use physregs directly
00371     // instead of virtual registers. This typically happens when passing
00372     // arguments to a function call, or when instructions require operands in
00373     // fixed registers.
00374     //
00375     // Each physreg has one or more register units, see MCRegisterInfo. We
00376     // track liveness per register unit to handle aliasing registers more
00377     // efficiently.
00378 
00379     /// getRegUnit - Return the live range for Unit.
00380     /// It will be computed if it doesn't exist.
00381     LiveRange &getRegUnit(unsigned Unit) {
00382       LiveRange *LR = RegUnitRanges[Unit];
00383       if (!LR) {
00384         // Compute missing ranges on demand.
00385         RegUnitRanges[Unit] = LR = new LiveRange();
00386         computeRegUnitRange(*LR, Unit);
00387       }
00388       return *LR;
00389     }
00390 
00391     /// getCachedRegUnit - Return the live range for Unit if it has already
00392     /// been computed, or NULL if it hasn't been computed yet.
00393     LiveRange *getCachedRegUnit(unsigned Unit) {
00394       return RegUnitRanges[Unit];
00395     }
00396 
00397     const LiveRange *getCachedRegUnit(unsigned Unit) const {
00398       return RegUnitRanges[Unit];
00399     }
00400 
00401   private:
00402     /// Compute live intervals for all virtual registers.
00403     void computeVirtRegs();
00404 
00405     /// Compute RegMaskSlots and RegMaskBits.
00406     void computeRegMasks();
00407 
00408     static LiveInterval* createInterval(unsigned Reg);
00409 
00410     void printInstrs(raw_ostream &O) const;
00411     void dumpInstrs() const;
00412 
00413     void computeLiveInRegUnits();
00414     void computeRegUnitRange(LiveRange&, unsigned Unit);
00415     void computeVirtRegInterval(LiveInterval&);
00416 
00417     class HMEditor;
00418   };
00419 } // End llvm namespace
00420 
00421 #endif