Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 1 | //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- C++ -*-===// |
| 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 LiveRangeEdit class represents changes done to a virtual register when it |
| 11 | // is spilled or split. |
| 12 | // |
| 13 | // The parent register is never changed. Instead, a number of new virtual |
| 14 | // registers are created and added to the newRegs vector. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H |
| 19 | #define LLVM_CODEGEN_LIVERANGEEDIT_H |
| 20 | |
| 21 | #include "llvm/CodeGen/LiveInterval.h" |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 26 | class AliasAnalysis; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 27 | class LiveIntervals; |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 28 | class MachineLoopInfo; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 29 | class MachineRegisterInfo; |
| 30 | class VirtRegMap; |
| 31 | |
| 32 | class LiveRangeEdit { |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 33 | public: |
| 34 | /// Callback methods for LiveRangeEdit owners. |
| 35 | struct Delegate { |
| 36 | /// Called immediately before erasing a dead machine instruction. |
| 37 | virtual void LRE_WillEraseInstruction(MachineInstr *MI) {} |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 38 | |
| 39 | /// Called when a virtual register is no longer used. Return false to defer |
| 40 | /// its deletion from LiveIntervals. |
| 41 | virtual bool LRE_CanEraseVirtReg(unsigned) { return true; } |
| 42 | |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 43 | /// Called before shrinking the live range of a virtual register. |
| 44 | virtual void LRE_WillShrinkVirtReg(unsigned) {} |
| 45 | |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 46 | /// Called after cloning a virtual register. |
| 47 | /// This is used for new registers representing connected components of Old. |
| 48 | virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {} |
| 49 | |
Matt Beaumont-Gay | ab2ee2e | 2011-03-09 04:02:15 +0000 | [diff] [blame] | 50 | virtual ~Delegate() {} |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | private: |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 54 | LiveInterval &parent_; |
| 55 | SmallVectorImpl<LiveInterval*> &newRegs_; |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 56 | Delegate *const delegate_; |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 57 | const SmallVectorImpl<LiveInterval*> *uselessRegs_; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 58 | |
| 59 | /// firstNew_ - Index of the first register added to newRegs_. |
| 60 | const unsigned firstNew_; |
| 61 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 62 | /// scannedRemattable_ - true when remattable values have been identified. |
| 63 | bool scannedRemattable_; |
| 64 | |
| 65 | /// remattable_ - Values defined by remattable instructions as identified by |
| 66 | /// tii.isTriviallyReMaterializable(). |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 67 | SmallPtrSet<const VNInfo*,4> remattable_; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 68 | |
| 69 | /// rematted_ - Values that were actually rematted, and so need to have their |
| 70 | /// live range trimmed or entirely removed. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 71 | SmallPtrSet<const VNInfo*,4> rematted_; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 72 | |
| 73 | /// scanRemattable - Identify the parent_ values that may rematerialize. |
| 74 | void scanRemattable(LiveIntervals &lis, |
| 75 | const TargetInstrInfo &tii, |
| 76 | AliasAnalysis *aa); |
| 77 | |
| 78 | /// allUsesAvailableAt - Return true if all registers used by OrigMI at |
| 79 | /// OrigIdx are also available with the same value at UseIdx. |
| 80 | bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx, |
| 81 | SlotIndex UseIdx, LiveIntervals &lis); |
| 82 | |
Jakob Stoklund Olesen | 3520019 | 2011-04-05 20:20:26 +0000 | [diff] [blame] | 83 | /// foldAsLoad - If LI has a single use and a single def that can be folded as |
| 84 | /// a load, eliminate the register by folding the def into the use. |
| 85 | bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead, |
| 86 | MachineRegisterInfo&, LiveIntervals&, const TargetInstrInfo&); |
| 87 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 88 | public: |
| 89 | /// Create a LiveRangeEdit for breaking down parent into smaller pieces. |
| 90 | /// @param parent The register being spilled or split. |
| 91 | /// @param newRegs List to receive any new registers created. This needn't be |
| 92 | /// empty initially, any existing registers are ignored. |
| 93 | /// @param uselessRegs List of registers that can't be used when |
| 94 | /// rematerializing values because they are about to be removed. |
| 95 | LiveRangeEdit(LiveInterval &parent, |
| 96 | SmallVectorImpl<LiveInterval*> &newRegs, |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 97 | Delegate *delegate = 0, |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 98 | const SmallVectorImpl<LiveInterval*> *uselessRegs = 0) |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 99 | : parent_(parent), newRegs_(newRegs), |
| 100 | delegate_(delegate), |
| 101 | uselessRegs_(uselessRegs), |
| 102 | firstNew_(newRegs.size()), |
| 103 | scannedRemattable_(false) {} |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 104 | |
| 105 | LiveInterval &getParent() const { return parent_; } |
| 106 | unsigned getReg() const { return parent_.reg; } |
| 107 | |
| 108 | /// Iterator for accessing the new registers added by this edit. |
| 109 | typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator; |
| 110 | iterator begin() const { return newRegs_.begin()+firstNew_; } |
| 111 | iterator end() const { return newRegs_.end(); } |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 112 | unsigned size() const { return newRegs_.size()-firstNew_; } |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 113 | bool empty() const { return size() == 0; } |
Jakob Stoklund Olesen | db4eec3 | 2010-10-29 18:21:18 +0000 | [diff] [blame] | 114 | LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; } |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 116 | /// FIXME: Temporary accessors until we can get rid of |
| 117 | /// LiveIntervals::AddIntervalsForSpills |
| 118 | SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; } |
| 119 | const SmallVectorImpl<LiveInterval*> *getUselessVRegs() { |
| 120 | return uselessRegs_; |
| 121 | } |
| 122 | |
Jakob Stoklund Olesen | e9c5073 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 123 | /// createFrom - Create a new virtual register based on OldReg. |
| 124 | LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&); |
| 125 | |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 126 | /// create - Create a new register with the same class and original slot as |
Jakob Stoklund Olesen | 2a0180f | 2010-10-15 00:16:55 +0000 | [diff] [blame] | 127 | /// parent. |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 128 | LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) { |
| 129 | return createFrom(getReg(), LIS, VRM); |
| 130 | } |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 131 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 132 | /// anyRematerializable - Return true if any parent values may be |
| 133 | /// rematerializable. |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 134 | /// This function must be called before any rematerialization is attempted. |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 135 | bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&, |
| 136 | AliasAnalysis*); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 137 | |
Jakob Stoklund Olesen | 2ef661b | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 138 | /// checkRematerializable - Manually add VNI to the list of rematerializable |
| 139 | /// values if DefMI may be rematerializable. |
| 140 | void checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI, |
| 141 | const TargetInstrInfo&, AliasAnalysis*); |
| 142 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 143 | /// Remat - Information needed to rematerialize at a specific location. |
| 144 | struct Remat { |
| 145 | VNInfo *ParentVNI; // parent_'s value at the remat location. |
| 146 | MachineInstr *OrigMI; // Instruction defining ParentVNI. |
Jakob Stoklund Olesen | b80e973 | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 147 | explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {} |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /// canRematerializeAt - Determine if ParentVNI can be rematerialized at |
| 151 | /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI. |
| 152 | /// When cheapAsAMove is set, only cheap remats are allowed. |
Jakob Stoklund Olesen | b80e973 | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 153 | bool canRematerializeAt(Remat &RM, |
| 154 | SlotIndex UseIdx, |
| 155 | bool cheapAsAMove, |
| 156 | LiveIntervals &lis); |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 157 | |
| 158 | /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an |
| 159 | /// instruction into MBB before MI. The new instruction is mapped, but |
| 160 | /// liveness is not updated. |
| 161 | /// Return the SlotIndex of the new instruction. |
| 162 | SlotIndex rematerializeAt(MachineBasicBlock &MBB, |
| 163 | MachineBasicBlock::iterator MI, |
| 164 | unsigned DestReg, |
| 165 | const Remat &RM, |
| 166 | LiveIntervals&, |
| 167 | const TargetInstrInfo&, |
| 168 | const TargetRegisterInfo&); |
| 169 | |
Jakob Stoklund Olesen | 83d1ba5 | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 170 | /// markRematerialized - explicitly mark a value as rematerialized after doing |
| 171 | /// it manually. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 172 | void markRematerialized(const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 83d1ba5 | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 173 | rematted_.insert(ParentVNI); |
| 174 | } |
| 175 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 176 | /// didRematerialize - Return true if ParentVNI was rematerialized anywhere. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 177 | bool didRematerialize(const VNInfo *ParentVNI) const { |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 178 | return rematted_.count(ParentVNI); |
| 179 | } |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 180 | |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 181 | /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try |
| 182 | /// to erase it from LIS. |
| 183 | void eraseVirtReg(unsigned Reg, LiveIntervals &LIS); |
| 184 | |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 185 | /// eliminateDeadDefs - Try to delete machine instructions that are now dead |
| 186 | /// (allDefsAreDead returns true). This may cause live intervals to be trimmed |
| 187 | /// and further dead efs to be eliminated. |
| 188 | void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead, |
Jakob Stoklund Olesen | 6a3dbd3 | 2011-03-17 20:37:07 +0000 | [diff] [blame] | 189 | LiveIntervals&, VirtRegMap&, |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 190 | const TargetInstrInfo&); |
| 191 | |
Jakob Stoklund Olesen | 6094bd8 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 192 | /// calculateRegClassAndHint - Recompute register class and hint for each new |
| 193 | /// register. |
| 194 | void calculateRegClassAndHint(MachineFunction&, LiveIntervals&, |
| 195 | const MachineLoopInfo&); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | #endif |