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; |
| 28 | class MachineRegisterInfo; |
| 29 | class VirtRegMap; |
| 30 | |
| 31 | class LiveRangeEdit { |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 32 | public: |
| 33 | /// Callback methods for LiveRangeEdit owners. |
| 34 | struct Delegate { |
| 35 | /// Called immediately before erasing a dead machine instruction. |
| 36 | virtual void LRE_WillEraseInstruction(MachineInstr *MI) {} |
Matt Beaumont-Gay | ab2ee2e | 2011-03-09 04:02:15 +0000 | [diff] [blame^] | 37 | virtual ~Delegate() {} |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | private: |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 41 | LiveInterval &parent_; |
| 42 | SmallVectorImpl<LiveInterval*> &newRegs_; |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 43 | Delegate *const delegate_; |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 44 | const SmallVectorImpl<LiveInterval*> *uselessRegs_; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 45 | |
| 46 | /// firstNew_ - Index of the first register added to newRegs_. |
| 47 | const unsigned firstNew_; |
| 48 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 49 | /// scannedRemattable_ - true when remattable values have been identified. |
| 50 | bool scannedRemattable_; |
| 51 | |
| 52 | /// remattable_ - Values defined by remattable instructions as identified by |
| 53 | /// tii.isTriviallyReMaterializable(). |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 54 | SmallPtrSet<const VNInfo*,4> remattable_; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 55 | |
| 56 | /// rematted_ - Values that were actually rematted, and so need to have their |
| 57 | /// live range trimmed or entirely removed. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 58 | SmallPtrSet<const VNInfo*,4> rematted_; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 59 | |
| 60 | /// scanRemattable - Identify the parent_ values that may rematerialize. |
| 61 | void scanRemattable(LiveIntervals &lis, |
| 62 | const TargetInstrInfo &tii, |
| 63 | AliasAnalysis *aa); |
| 64 | |
| 65 | /// allUsesAvailableAt - Return true if all registers used by OrigMI at |
| 66 | /// OrigIdx are also available with the same value at UseIdx. |
| 67 | bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx, |
| 68 | SlotIndex UseIdx, LiveIntervals &lis); |
| 69 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 70 | public: |
| 71 | /// Create a LiveRangeEdit for breaking down parent into smaller pieces. |
| 72 | /// @param parent The register being spilled or split. |
| 73 | /// @param newRegs List to receive any new registers created. This needn't be |
| 74 | /// empty initially, any existing registers are ignored. |
| 75 | /// @param uselessRegs List of registers that can't be used when |
| 76 | /// rematerializing values because they are about to be removed. |
| 77 | LiveRangeEdit(LiveInterval &parent, |
| 78 | SmallVectorImpl<LiveInterval*> &newRegs, |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 79 | Delegate *delegate, |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 80 | const SmallVectorImpl<LiveInterval*> *uselessRegs = 0) |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 81 | : parent_(parent), newRegs_(newRegs), |
| 82 | delegate_(delegate), |
| 83 | uselessRegs_(uselessRegs), |
| 84 | firstNew_(newRegs.size()), |
| 85 | scannedRemattable_(false) {} |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 86 | |
| 87 | LiveInterval &getParent() const { return parent_; } |
| 88 | unsigned getReg() const { return parent_.reg; } |
| 89 | |
| 90 | /// Iterator for accessing the new registers added by this edit. |
| 91 | typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator; |
| 92 | iterator begin() const { return newRegs_.begin()+firstNew_; } |
| 93 | iterator end() const { return newRegs_.end(); } |
Jakob Stoklund Olesen | 3a0e071 | 2010-10-26 22:36:09 +0000 | [diff] [blame] | 94 | unsigned size() const { return newRegs_.size()-firstNew_; } |
Eric Christopher | 0f43811 | 2011-02-03 06:18:29 +0000 | [diff] [blame] | 95 | bool empty() const { return size() == 0; } |
Jakob Stoklund Olesen | db4eec3 | 2010-10-29 18:21:18 +0000 | [diff] [blame] | 96 | LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; } |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 97 | |
Jakob Stoklund Olesen | 2a0180f | 2010-10-15 00:16:55 +0000 | [diff] [blame] | 98 | /// create - Create a new register with the same class and stack slot as |
| 99 | /// parent. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 100 | LiveInterval &create(MachineRegisterInfo&, LiveIntervals&, VirtRegMap&); |
| 101 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 102 | /// anyRematerializable - Return true if any parent values may be |
| 103 | /// rematerializable. |
Jakob Stoklund Olesen | 1973b3e | 2011-03-07 22:42:16 +0000 | [diff] [blame] | 104 | /// This function must be called before any rematerialization is attempted. |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 105 | bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&, |
| 106 | AliasAnalysis*); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 107 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 108 | /// Remat - Information needed to rematerialize at a specific location. |
| 109 | struct Remat { |
| 110 | VNInfo *ParentVNI; // parent_'s value at the remat location. |
| 111 | MachineInstr *OrigMI; // Instruction defining ParentVNI. |
Jakob Stoklund Olesen | b80e973 | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 112 | explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {} |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /// canRematerializeAt - Determine if ParentVNI can be rematerialized at |
| 116 | /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI. |
| 117 | /// When cheapAsAMove is set, only cheap remats are allowed. |
Jakob Stoklund Olesen | b80e973 | 2010-11-10 01:05:12 +0000 | [diff] [blame] | 118 | bool canRematerializeAt(Remat &RM, |
| 119 | SlotIndex UseIdx, |
| 120 | bool cheapAsAMove, |
| 121 | LiveIntervals &lis); |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 122 | |
| 123 | /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an |
| 124 | /// instruction into MBB before MI. The new instruction is mapped, but |
| 125 | /// liveness is not updated. |
| 126 | /// Return the SlotIndex of the new instruction. |
| 127 | SlotIndex rematerializeAt(MachineBasicBlock &MBB, |
| 128 | MachineBasicBlock::iterator MI, |
| 129 | unsigned DestReg, |
| 130 | const Remat &RM, |
| 131 | LiveIntervals&, |
| 132 | const TargetInstrInfo&, |
| 133 | const TargetRegisterInfo&); |
| 134 | |
Jakob Stoklund Olesen | 83d1ba5 | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 135 | /// markRematerialized - explicitly mark a value as rematerialized after doing |
| 136 | /// it manually. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 137 | void markRematerialized(const VNInfo *ParentVNI) { |
Jakob Stoklund Olesen | 83d1ba5 | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 138 | rematted_.insert(ParentVNI); |
| 139 | } |
| 140 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 141 | /// didRematerialize - Return true if ParentVNI was rematerialized anywhere. |
Jakob Stoklund Olesen | 4670353 | 2011-03-02 23:05:19 +0000 | [diff] [blame] | 142 | bool didRematerialize(const VNInfo *ParentVNI) const { |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 143 | return rematted_.count(ParentVNI); |
| 144 | } |
Jakob Stoklund Olesen | 5881799 | 2011-03-08 22:46:11 +0000 | [diff] [blame] | 145 | |
| 146 | /// eliminateDeadDefs - Try to delete machine instructions that are now dead |
| 147 | /// (allDefsAreDead returns true). This may cause live intervals to be trimmed |
| 148 | /// and further dead efs to be eliminated. |
| 149 | void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead, |
| 150 | LiveIntervals&, |
| 151 | const TargetInstrInfo&); |
| 152 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | #endif |