blob: db6740c1fbcd80a057ad405f40ec646866884b71 [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
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +000021#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000022#include "llvm/ADT/SmallPtrSet.h"
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +000023#include "llvm/CodeGen/LiveInterval.h"
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000024
25namespace llvm {
26
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000027class AliasAnalysis;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000028class LiveIntervals;
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +000029class MachineLoopInfo;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000030class MachineRegisterInfo;
31class VirtRegMap;
32
33class LiveRangeEdit {
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000034public:
35 /// Callback methods for LiveRangeEdit owners.
36 struct Delegate {
37 /// Called immediately before erasing a dead machine instruction.
38 virtual void LRE_WillEraseInstruction(MachineInstr *MI) {}
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +000039
40 /// Called when a virtual register is no longer used. Return false to defer
41 /// its deletion from LiveIntervals.
42 virtual bool LRE_CanEraseVirtReg(unsigned) { return true; }
43
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +000044 /// Called before shrinking the live range of a virtual register.
45 virtual void LRE_WillShrinkVirtReg(unsigned) {}
46
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +000047 /// Called after cloning a virtual register.
48 /// This is used for new registers representing connected components of Old.
49 virtual void LRE_DidCloneVirtReg(unsigned New, unsigned Old) {}
50
Matt Beaumont-Gayab2ee2e2011-03-09 04:02:15 +000051 virtual ~Delegate() {}
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000052 };
53
54private:
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000055 LiveInterval &parent_;
56 SmallVectorImpl<LiveInterval*> &newRegs_;
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000057 Delegate *const delegate_;
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000058 const SmallVectorImpl<LiveInterval*> *uselessRegs_;
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000059
60 /// firstNew_ - Index of the first register added to newRegs_.
61 const unsigned firstNew_;
62
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000063 /// scannedRemattable_ - true when remattable values have been identified.
64 bool scannedRemattable_;
65
66 /// remattable_ - Values defined by remattable instructions as identified by
67 /// tii.isTriviallyReMaterializable().
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000068 SmallPtrSet<const VNInfo*,4> remattable_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000069
70 /// rematted_ - Values that were actually rematted, and so need to have their
71 /// live range trimmed or entirely removed.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +000072 SmallPtrSet<const VNInfo*,4> rematted_;
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +000073
74 /// scanRemattable - Identify the parent_ values that may rematerialize.
75 void scanRemattable(LiveIntervals &lis,
76 const TargetInstrInfo &tii,
77 AliasAnalysis *aa);
78
79 /// allUsesAvailableAt - Return true if all registers used by OrigMI at
80 /// OrigIdx are also available with the same value at UseIdx.
81 bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx,
82 SlotIndex UseIdx, LiveIntervals &lis);
83
Jakob Stoklund Olesen35200192011-04-05 20:20:26 +000084 /// foldAsLoad - If LI has a single use and a single def that can be folded as
85 /// a load, eliminate the register by folding the def into the use.
86 bool foldAsLoad(LiveInterval *LI, SmallVectorImpl<MachineInstr*> &Dead,
87 MachineRegisterInfo&, LiveIntervals&, const TargetInstrInfo&);
88
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +000089public:
90 /// Create a LiveRangeEdit for breaking down parent into smaller pieces.
91 /// @param parent The register being spilled or split.
92 /// @param newRegs List to receive any new registers created. This needn't be
93 /// empty initially, any existing registers are ignored.
94 /// @param uselessRegs List of registers that can't be used when
95 /// rematerializing values because they are about to be removed.
96 LiveRangeEdit(LiveInterval &parent,
97 SmallVectorImpl<LiveInterval*> &newRegs,
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +000098 Delegate *delegate = 0,
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +000099 const SmallVectorImpl<LiveInterval*> *uselessRegs = 0)
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000100 : parent_(parent), newRegs_(newRegs),
101 delegate_(delegate),
102 uselessRegs_(uselessRegs),
103 firstNew_(newRegs.size()),
104 scannedRemattable_(false) {}
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000105
106 LiveInterval &getParent() const { return parent_; }
107 unsigned getReg() const { return parent_.reg; }
108
109 /// Iterator for accessing the new registers added by this edit.
110 typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
111 iterator begin() const { return newRegs_.begin()+firstNew_; }
112 iterator end() const { return newRegs_.end(); }
Jakob Stoklund Olesen3a0e0712010-10-26 22:36:09 +0000113 unsigned size() const { return newRegs_.size()-firstNew_; }
Eric Christopher0f438112011-02-03 06:18:29 +0000114 bool empty() const { return size() == 0; }
Jakob Stoklund Olesendb4eec32010-10-29 18:21:18 +0000115 LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000116
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +0000117 ArrayRef<LiveInterval*> regs() const {
118 return ArrayRef<LiveInterval*>(newRegs_).slice(firstNew_);
119 }
120
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +0000121 /// FIXME: Temporary accessors until we can get rid of
122 /// LiveIntervals::AddIntervalsForSpills
123 SmallVectorImpl<LiveInterval*> *getNewVRegs() { return &newRegs_; }
124 const SmallVectorImpl<LiveInterval*> *getUselessVRegs() {
125 return uselessRegs_;
126 }
127
Jakob Stoklund Olesene9c50732011-03-26 22:16:41 +0000128 /// createFrom - Create a new virtual register based on OldReg.
129 LiveInterval &createFrom(unsigned OldReg, LiveIntervals&, VirtRegMap&);
130
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000131 /// create - Create a new register with the same class and original slot as
Jakob Stoklund Olesen2a0180f2010-10-15 00:16:55 +0000132 /// parent.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000133 LiveInterval &create(LiveIntervals &LIS, VirtRegMap &VRM) {
134 return createFrom(getReg(), LIS, VRM);
135 }
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000136
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000137 /// anyRematerializable - Return true if any parent values may be
138 /// rematerializable.
Jakob Stoklund Olesen1973b3e2011-03-07 22:42:16 +0000139 /// This function must be called before any rematerialization is attempted.
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000140 bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
141 AliasAnalysis*);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000142
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000143 /// checkRematerializable - Manually add VNI to the list of rematerializable
144 /// values if DefMI may be rematerializable.
Jakob Stoklund Olesen3b7d9172011-04-20 22:14:20 +0000145 bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI,
Jakob Stoklund Olesen2ef661b2011-03-29 03:12:02 +0000146 const TargetInstrInfo&, AliasAnalysis*);
147
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000148 /// Remat - Information needed to rematerialize at a specific location.
149 struct Remat {
150 VNInfo *ParentVNI; // parent_'s value at the remat location.
151 MachineInstr *OrigMI; // Instruction defining ParentVNI.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000152 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000153 };
154
155 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
156 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
157 /// When cheapAsAMove is set, only cheap remats are allowed.
Jakob Stoklund Olesenb80e9732010-11-10 01:05:12 +0000158 bool canRematerializeAt(Remat &RM,
159 SlotIndex UseIdx,
160 bool cheapAsAMove,
161 LiveIntervals &lis);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000162
163 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
164 /// instruction into MBB before MI. The new instruction is mapped, but
165 /// liveness is not updated.
166 /// Return the SlotIndex of the new instruction.
167 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
168 MachineBasicBlock::iterator MI,
169 unsigned DestReg,
170 const Remat &RM,
171 LiveIntervals&,
172 const TargetInstrInfo&,
Jakob Stoklund Olesenbb30dd42011-05-02 05:29:58 +0000173 const TargetRegisterInfo&,
174 bool Late = false);
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000175
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000176 /// markRematerialized - explicitly mark a value as rematerialized after doing
177 /// it manually.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000178 void markRematerialized(const VNInfo *ParentVNI) {
Jakob Stoklund Olesen83d1ba52010-12-18 03:04:14 +0000179 rematted_.insert(ParentVNI);
180 }
181
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000182 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
Jakob Stoklund Olesen46703532011-03-02 23:05:19 +0000183 bool didRematerialize(const VNInfo *ParentVNI) const {
Jakob Stoklund Olesen080c3162010-10-20 22:00:51 +0000184 return rematted_.count(ParentVNI);
185 }
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000186
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000187 /// eraseVirtReg - Notify the delegate that Reg is no longer in use, and try
188 /// to erase it from LIS.
189 void eraseVirtReg(unsigned Reg, LiveIntervals &LIS);
190
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000191 /// eliminateDeadDefs - Try to delete machine instructions that are now dead
192 /// (allDefsAreDead returns true). This may cause live intervals to be trimmed
193 /// and further dead efs to be eliminated.
194 void eliminateDeadDefs(SmallVectorImpl<MachineInstr*> &Dead,
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000195 LiveIntervals&, VirtRegMap&,
Jakob Stoklund Olesen58817992011-03-08 22:46:11 +0000196 const TargetInstrInfo&);
197
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +0000198 /// calculateRegClassAndHint - Recompute register class and hint for each new
199 /// register.
200 void calculateRegClassAndHint(MachineFunction&, LiveIntervals&,
201 const MachineLoopInfo&);
Jakob Stoklund Olesena17768f2010-10-14 23:49:52 +0000202};
203
204}
205
206#endif