LLVM API Documentation

LiveRangeEdit.h
Go to the documentation of this file.
00001 //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- 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 // The LiveRangeEdit class represents changes done to a virtual register when it
00011 // is spilled or split.
00012 //
00013 // The parent register is never changed. Instead, a number of new virtual
00014 // registers are created and added to the newRegs vector.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
00019 #define LLVM_CODEGEN_LIVERANGEEDIT_H
00020 
00021 #include "llvm/ADT/ArrayRef.h"
00022 #include "llvm/ADT/SetVector.h"
00023 #include "llvm/ADT/SmallPtrSet.h"
00024 #include "llvm/CodeGen/LiveInterval.h"
00025 #include "llvm/CodeGen/MachineRegisterInfo.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Target/TargetSubtargetInfo.h"
00028 
00029 namespace llvm {
00030 
00031 class AliasAnalysis;
00032 class LiveIntervals;
00033 class MachineBlockFrequencyInfo;
00034 class MachineLoopInfo;
00035 class VirtRegMap;
00036 
00037 class LiveRangeEdit : private MachineRegisterInfo::Delegate {
00038 public:
00039   /// Callback methods for LiveRangeEdit owners.
00040   class Delegate {
00041     virtual void anchor();
00042   public:
00043     /// Called immediately before erasing a dead machine instruction.
00044     virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
00045 
00046     /// Called when a virtual register is no longer used. Return false to defer
00047     /// its deletion from LiveIntervals.
00048     virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
00049 
00050     /// Called before shrinking the live range of a virtual register.
00051     virtual void LRE_WillShrinkVirtReg(unsigned) {}
00052 
00053     /// Called after cloning a virtual register.
00054     /// This is used for new registers representing connected components of Old.
00055     virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
00056 
00057     virtual ~Delegate() {}
00058   };
00059 
00060 private:
00061   LiveInterval *Parent;
00062   SmallVectorImpl<unsigned> &NewRegs;
00063   MachineRegisterInfo &MRI;
00064   LiveIntervals &LIS;
00065   VirtRegMap *VRM;
00066   const TargetInstrInfo &TII;
00067   Delegate *const TheDelegate;
00068 
00069   /// FirstNew - Index of the first register added to NewRegs.
00070   const unsigned FirstNew;
00071 
00072   /// ScannedRemattable - true when remattable values have been identified.
00073   bool ScannedRemattable;
00074 
00075   /// Remattable - Values defined by remattable instructions as identified by
00076   /// tii.isTriviallyReMaterializable().
00077   SmallPtrSet<const VNInfo*,4> Remattable;
00078 
00079   /// Rematted - Values that were actually rematted, and so need to have their
00080   /// live range trimmed or entirely removed.
00081   SmallPtrSet<const VNInfo*,4> Rematted;
00082 
00083   /// scanRemattable - Identify the Parent values that may rematerialize.
00084   void scanRemattable(AliasAnalysis *aa);
00085 
00086   /// allUsesAvailableAt - Return true if all registers used by OrigMI at
00087   /// OrigIdx are also available with the same value at UseIdx.
00088   bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
00089                           SlotIndex UseIdx) const;
00090 
00091   /// foldAsLoad - If LI has a single use and a single def that can be folded as
00092   /// a load, eliminate the register by folding the def into the use.
00093   bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead);
00094 
00095   typedef SetVector<LiveInterval*,
00096                     SmallVector<LiveInterval*, 8>,
00097                     SmallPtrSet<LiveInterval*, 8> > ToShrinkSet;
00098   /// Helper for eliminateDeadDefs.
00099   void eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink);
00100 
00101   /// MachineRegisterInfo callback to notify when new virtual
00102   /// registers are created.
00103   void MRI_NoteNewVirtualRegister(unsigned VReg) override;
00104 
00105 public:
00106   /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
00107   /// @param parent The register being spilled or split.
00108   /// @param newRegs List to receive any new registers created. This needn't be
00109   ///                empty initially, any existing registers are ignored.
00110   /// @param MF The MachineFunction the live range edit is taking place in.
00111   /// @param lis The collection of all live intervals in this function.
00112   /// @param vrm Map of virtual registers to physical registers for this
00113   ///            function.  If NULL, no virtual register map updates will
00114   ///            be done.  This could be the case if called before Regalloc.
00115   LiveRangeEdit(LiveInterval *parent, SmallVectorImpl<unsigned> &newRegs,
00116                 MachineFunction &MF, LiveIntervals &lis, VirtRegMap *vrm,
00117                 Delegate *delegate = nullptr)
00118       : Parent(parent), NewRegs(newRegs), MRI(MF.getRegInfo()), LIS(lis),
00119         VRM(vrm), TII(*MF.getSubtarget().getInstrInfo()),
00120         TheDelegate(delegate), FirstNew(newRegs.size()),
00121         ScannedRemattable(false) {
00122     MRI.setDelegate(this);
00123   }
00124 
00125   ~LiveRangeEdit() { MRI.resetDelegate(this); }
00126 
00127   LiveInterval &getParent() const {
00128    assert(Parent && "No parent LiveInterval");
00129    return *Parent;
00130   }
00131   unsigned getReg() const { return getParent().reg; }
00132 
00133   /// Iterator for accessing the new registers added by this edit.
00134   typedef SmallVectorImpl<unsigned>::const_iterator iterator;
00135   iterator begin() const { return NewRegs.begin()+FirstNew; }
00136   iterator end() const { return NewRegs.end(); }
00137   unsigned size() const { return NewRegs.size()-FirstNew; }
00138   bool empty() const { return size() == 0; }
00139   unsigned get(unsigned idx) const { return NewRegs[idx+FirstNew]; }
00140 
00141   ArrayRef<unsigned> regs() const {
00142     return makeArrayRef(NewRegs).slice(FirstNew);
00143   }
00144 
00145   /// createEmptyIntervalFrom - Create a new empty interval based on OldReg.
00146   LiveInterval &createEmptyIntervalFrom(unsigned OldReg);
00147 
00148   /// createFrom - Create a new virtual register based on OldReg.
00149   unsigned createFrom(unsigned OldReg);
00150 
00151   /// create - Create a new register with the same class and original slot as
00152   /// parent.
00153   LiveInterval &createEmptyInterval() {
00154     return createEmptyIntervalFrom(getReg());
00155   }
00156 
00157   unsigned create() {
00158     return createFrom(getReg());
00159   }
00160 
00161   /// anyRematerializable - Return true if any parent values may be
00162   /// rematerializable.
00163   /// This function must be called before any rematerialization is attempted.
00164   bool anyRematerializable(AliasAnalysis*);
00165 
00166   /// checkRematerializable - Manually add VNI to the list of rematerializable
00167   /// values if DefMI may be rematerializable.
00168   bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
00169                              AliasAnalysis*);
00170 
00171   /// Remat - Information needed to rematerialize at a specific location.
00172   struct Remat {
00173     VNInfo *ParentVNI;      // parent_'s value at the remat location.
00174     MachineInstr *OrigMI;   // Instruction defining ParentVNI.
00175     explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(nullptr) {}
00176   };
00177 
00178   /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
00179   /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
00180   /// When cheapAsAMove is set, only cheap remats are allowed.
00181   bool canRematerializeAt(Remat &RM,
00182                           SlotIndex UseIdx,
00183                           bool cheapAsAMove);
00184 
00185   /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
00186   /// instruction into MBB before MI. The new instruction is mapped, but
00187   /// liveness is not updated.
00188   /// Return the SlotIndex of the new instruction.
00189   SlotIndex rematerializeAt(MachineBasicBlock &MBB,
00190                             MachineBasicBlock::iterator MI,
00191                             unsigned DestReg,
00192                             const Remat &RM,
00193                             const TargetRegisterInfo&,
00194                             bool Late = false);
00195 
00196   /// markRematerialized - explicitly mark a value as rematerialized after doing
00197   /// it manually.
00198   void markRematerialized(const VNInfo *ParentVNI) {
00199     Rematted.insert(ParentVNI);
00200   }
00201 
00202   /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
00203   bool didRematerialize(const VNInfo *ParentVNI) const {
00204     return Rematted.count(ParentVNI);
00205   }
00206 
00207   /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
00208   /// to erase it from LIS.
00209   void eraseVirtReg(unsigned Reg);
00210 
00211   /// eliminateDeadDefs - Try to delete machine instructions that are now dead
00212   /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
00213   /// and further dead efs to be eliminated.
00214   /// RegsBeingSpilled lists registers currently being spilled by the register
00215   /// allocator.  These registers should not be split into new intervals
00216   /// as currently those new intervals are not guaranteed to spill.
00217   void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
00218                          ArrayRef<unsigned> RegsBeingSpilled = None);
00219 
00220   /// calculateRegClassAndHint - Recompute register class and hint for each new
00221   /// register.
00222   void calculateRegClassAndHint(MachineFunction&,
00223                                 const MachineLoopInfo&,
00224                                 const MachineBlockFrequencyInfo&);
00225 };
00226 
00227 }
00228 
00229 #endif