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