blob: 04eced6d79b7d59bc659285422a08327e75dd42b [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;
28class MachineRegisterInfo;
29class VirtRegMap;
30
31class LiveRangeEdit {
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000032public:
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) {}
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +000037
38 /// Called when a virtual register is no longer used. Return false to defer
39 /// its deletion from LiveIntervals.
40 virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
41
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +000042 /// Called before shrinking the live range of a virtual register.
43 virtual void LRE_WillShrinkVirtReg(unsigned) {}
44
Matt Beaumont-Gayab2ee2e2011-03-09 04:02:15 +000045 virtual ~Delegate() {}
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000046 };
47
48private:
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000049 LiveInterval &parent_;
50 SmallVectorImpl<LiveInterval*> &newRegs_;
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000051 Delegate *const delegate_;
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000052 const SmallVectorImpl<LiveInterval*> *uselessRegs_;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000053
54 /// firstNew_ - Index of the first register added to newRegs_.
55 const unsigned firstNew_;
56
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000057 /// scannedRemattable_ - true when remattable values have been identified.
58 bool scannedRemattable_;
59
60 /// remattable_ - Values defined by remattable instructions as identified by
61 /// tii.isTriviallyReMaterializable().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000062 SmallPtrSet<const VNInfo*,4> remattable_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000063
64 /// rematted_ - Values that were actually rematted, and so need to have their
65 /// live range trimmed or entirely removed.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000066 SmallPtrSet<const VNInfo*,4> rematted_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000067
68 /// scanRemattable - Identify the parent_ values that may rematerialize.
69 void scanRemattable(LiveIntervals &lis,
70 const TargetInstrInfo &tii,
71 AliasAnalysis *aa);
72
73 /// allUsesAvailableAt - Return true if all registers used by OrigMI at
74 /// OrigIdx are also available with the same value at UseIdx.
75 bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
76 SlotIndex UseIdx, LiveIntervals &lis);
77
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000078public:
79 /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
80 /// @param parent The register being spilled or split.
81 /// @param newRegs List to receive any new registers created. This needn't be
82 /// empty initially, any existing registers are ignored.
83 /// @param uselessRegs List of registers that can't be used when
84 /// rematerializing values because they are about to be removed.
85 LiveRangeEdit(LiveInterval &parent,
86 SmallVectorImpl<LiveInterval*> &newRegs,
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +000087 Delegate *delegate = 0,
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000088 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000089 : parent_(parent), newRegs_(newRegs),
90 delegate_(delegate),
91 uselessRegs_(uselessRegs),
92 firstNew_(newRegs.size()),
93 scannedRemattable_(false) {}
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000094
95 LiveInterval &getParent() const { return parent_; }
96 unsigned getReg() const { return parent_.reg; }
97
98 /// Iterator for accessing the new registers added by this edit.
99 typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
100 iterator begin() const { return newRegs_.begin()+firstNew_; }
101 iterator end() const { return newRegs_.end(); }
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000102 unsigned size() const { return newRegs_.size()-firstNew_; }
Eric Christopher0f438112011-02-03 06:18:29 +0000103 bool empty() const { return size() == 0; }
Jakob Stoklund Olesendb4eec32010-10-29 18:21:18 +0000104 LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000105
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000106 /// FIXME: Temporary accessors until we can get rid of
107 /// LiveIntervals::AddIntervalsForSpills
108 SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
109 const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
110 return uselessRegs_;
111 }
112
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000113 /// createFrom - Create a new virtual register based on OldReg.
114 LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&);
115
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000116 /// create - Create a new register with the same class and original slot as
Jakob Stoklund Olesen2a0180f2010-10-15 00:16:55 +0000117 /// parent.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000118 LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) {
119 return createFrom(getReg(), LIS, VRM);
120 }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000121
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000122 /// anyRematerializable - Return true if any parent values may be
123 /// rematerializable.
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +0000124 /// This function must be called before any rematerialization is attempted.
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000125 bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
126 AliasAnalysis*);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000127
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000128 /// checkRematerializable - Manually add VNI to the list of rematerializable
129 /// values if DefMI may be rematerializable.
130 void checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
131 const TargetInstrInfo&, AliasAnalysis*);
132
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000133 /// Remat - Information needed to rematerialize at a specific location.
134 struct Remat {
135 VNInfo *ParentVNI; // parent_'s value at the remat location.
136 MachineInstr *OrigMI; // Instruction defining ParentVNI.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000137 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000138 };
139
140 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
141 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
142 /// When cheapAsAMove is set, only cheap remats are allowed.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000143 bool canRematerializeAt(Remat &RM,
144 SlotIndex UseIdx,
145 bool cheapAsAMove,
146 LiveIntervals &lis);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000147
148 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
149 /// instruction into MBB before MI. The new instruction is mapped, but
150 /// liveness is not updated.
151 /// Return the SlotIndex of the new instruction.
152 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
153 MachineBasicBlock::iterator MI,
154 unsigned DestReg,
155 const Remat &RM,
156 LiveIntervals&,
157 const TargetInstrInfo&,
158 const TargetRegisterInfo&);
159
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000160 /// markRematerialized - explicitly mark a value as rematerialized after doing
161 /// it manually.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000162 void markRematerialized(const VNInfo *ParentVNI) {
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000163 rematted_.insert(ParentVNI);
164 }
165
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000166 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000167 bool didRematerialize(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000168 return rematted_.count(ParentVNI);
169 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000170
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000171 /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
172 /// to erase it from LIS.
173 void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
174
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000175 /// eliminateDeadDefs - Try to delete machine instructions that are now dead
176 /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
177 /// and further dead efs to be eliminated.
178 void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000179 LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000180 const TargetInstrInfo&);
181
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000182};
183
184}
185
186#endif