blob: c96dfd699890c9bb4c9e386f88be3d4de591391c [file] [log] [blame]
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +00001//===---- 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 Olesen080c3162010-10-20 22:00:51 +000022#include "llvm/ADT/SmallPtrSet.h"
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000023
24namespace llvm {
25
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000026class AliasAnalysis;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000027class LiveIntervals;
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +000028class MachineLoopInfo;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000029class MachineRegisterInfo;
30class VirtRegMap;
31
32class LiveRangeEdit {
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000033public:
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 Olesen7792e982011-03-13 01:23:11 +000038
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 Olesen1d5b8452011-03-16 22:56:16 +000043 /// Called before shrinking the live range of a virtual register.
44 virtual void LRE_WillShrinkVirtReg(unsigned) {}
45
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +000046 /// 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-Gayab2ee2e2011-03-09 04:02:15 +000050 virtual ~Delegate() {}
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000051 };
52
53private:
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000054 LiveInterval &parent_;
55 SmallVectorImpl<LiveInterval*> &newRegs_;
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000056 Delegate *const delegate_;
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000057 const SmallVectorImpl<LiveInterval*> *uselessRegs_;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000058
59 /// firstNew_ - Index of the first register added to newRegs_.
60 const unsigned firstNew_;
61
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000062 /// 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 Olesen46703532011-03-02 23:05:19 +000067 SmallPtrSet<const VNInfo*,4> remattable_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000068
69 /// rematted_ - Values that were actually rematted, and so need to have their
70 /// live range trimmed or entirely removed.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000071 SmallPtrSet<const VNInfo*,4> rematted_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000072
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 Olesena17768f2010-10-14 23:49:52 +000083public:
84 /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
85 /// @param parent The register being spilled or split.
86 /// @param newRegs List to receive any new registers created. This needn't be
87 /// empty initially, any existing registers are ignored.
88 /// @param uselessRegs List of registers that can't be used when
89 /// rematerializing values because they are about to be removed.
90 LiveRangeEdit(LiveInterval &parent,
91 SmallVectorImpl<LiveInterval*> &newRegs,
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +000092 Delegate *delegate = 0,
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000093 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000094 : parent_(parent), newRegs_(newRegs),
95 delegate_(delegate),
96 uselessRegs_(uselessRegs),
97 firstNew_(newRegs.size()),
98 scannedRemattable_(false) {}
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000099
100 LiveInterval &getParent() const { return parent_; }
101 unsigned getReg() const { return parent_.reg; }
102
103 /// Iterator for accessing the new registers added by this edit.
104 typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
105 iterator begin() const { return newRegs_.begin()+firstNew_; }
106 iterator end() const { return newRegs_.end(); }
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000107 unsigned size() const { return newRegs_.size()-firstNew_; }
Eric Christopher0f438112011-02-03 06:18:29 +0000108 bool empty() const { return size() == 0; }
Jakob Stoklund Olesendb4eec32010-10-29 18:21:18 +0000109 LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000110
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000111 /// FIXME: Temporary accessors until we can get rid of
112 /// LiveIntervals::AddIntervalsForSpills
113 SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
114 const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
115 return uselessRegs_;
116 }
117
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000118 /// createFrom - Create a new virtual register based on OldReg.
119 LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&);
120
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000121 /// create - Create a new register with the same class and original slot as
Jakob Stoklund Olesen2a0180f2010-10-15 00:16:55 +0000122 /// parent.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000123 LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) {
124 return createFrom(getReg(), LIS, VRM);
125 }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000126
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000127 /// anyRematerializable - Return true if any parent values may be
128 /// rematerializable.
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +0000129 /// This function must be called before any rematerialization is attempted.
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000130 bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
131 AliasAnalysis*);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000132
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000133 /// checkRematerializable - Manually add VNI to the list of rematerializable
134 /// values if DefMI may be rematerializable.
135 void checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
136 const TargetInstrInfo&, AliasAnalysis*);
137
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000138 /// Remat - Information needed to rematerialize at a specific location.
139 struct Remat {
140 VNInfo *ParentVNI; // parent_'s value at the remat location.
141 MachineInstr *OrigMI; // Instruction defining ParentVNI.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000142 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000143 };
144
145 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
146 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
147 /// When cheapAsAMove is set, only cheap remats are allowed.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000148 bool canRematerializeAt(Remat &RM,
149 SlotIndex UseIdx,
150 bool cheapAsAMove,
151 LiveIntervals &lis);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000152
153 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
154 /// instruction into MBB before MI. The new instruction is mapped, but
155 /// liveness is not updated.
156 /// Return the SlotIndex of the new instruction.
157 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
158 MachineBasicBlock::iterator MI,
159 unsigned DestReg,
160 const Remat &RM,
161 LiveIntervals&,
162 const TargetInstrInfo&,
163 const TargetRegisterInfo&);
164
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000165 /// markRematerialized - explicitly mark a value as rematerialized after doing
166 /// it manually.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000167 void markRematerialized(const VNInfo *ParentVNI) {
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000168 rematted_.insert(ParentVNI);
169 }
170
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000171 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000172 bool didRematerialize(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000173 return rematted_.count(ParentVNI);
174 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000175
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000176 /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
177 /// to erase it from LIS.
178 void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
179
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000180 /// eliminateDeadDefs - Try to delete machine instructions that are now dead
181 /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
182 /// and further dead efs to be eliminated.
183 void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000184 LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000185 const TargetInstrInfo&);
186
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000187 /// calculateRegClassAndHint - Recompute register class and hint for each new
188 /// register.
189 void calculateRegClassAndHint(MachineFunction&, LiveIntervals&,
190 const MachineLoopInfo&);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000191};
192
193}
194
195#endif