1 //===---- LiveRangeEdit.h - Basic tools for split and spill -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The LiveRangeEdit class represents changes done to a virtual register when it
11 // is spilled or split.
13 // The parent register is never changed. Instead, a number of new virtual
14 // registers are created and added to the newRegs vector.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CODEGEN_LIVERANGEEDIT_H
19 #define LLVM_CODEGEN_LIVERANGEEDIT_H
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/ADT/SmallPtrSet.h"
28 class MachineRegisterInfo;
32 LiveInterval &parent_;
33 SmallVectorImpl<LiveInterval*> &newRegs_;
34 const SmallVectorImpl<LiveInterval*> &uselessRegs_;
36 /// firstNew_ - Index of the first register added to newRegs_.
37 const unsigned firstNew_;
39 /// scannedRemattable_ - true when remattable values have been identified.
40 bool scannedRemattable_;
42 /// remattable_ - Values defined by remattable instructions as identified by
43 /// tii.isTriviallyReMaterializable().
44 SmallPtrSet<VNInfo*,4> remattable_;
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_;
50 /// scanRemattable - Identify the parent_ values that may rematerialize.
51 void scanRemattable(LiveIntervals &lis,
52 const TargetInstrInfo &tii,
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);
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),
71 firstNew_(newRegs.size()), scannedRemattable_(false) {}
73 LiveInterval &getParent() const { return parent_; }
74 unsigned getReg() const { return parent_.reg; }
76 /// getOriginal - Return the original virtual register that parent descends
77 /// from through splitting. The original was not created by splitting.
78 unsigned getOriginal(const VirtRegMap&) const;
80 /// Iterator for accessing the new registers added by this edit.
81 typedef SmallVectorImpl<LiveInterval*>::const_iterator iterator;
82 iterator begin() const { return newRegs_.begin()+firstNew_; }
83 iterator end() const { return newRegs_.end(); }
84 unsigned size() const { return newRegs_.size()-firstNew_; }
85 bool empty() const { return size() == 0; }
86 LiveInterval *get(unsigned idx) const { return newRegs_[idx+firstNew_]; }
88 /// create - Create a new register with the same class and stack slot as
90 LiveInterval &create(MachineRegisterInfo&, LiveIntervals&, VirtRegMap&);
92 /// anyRematerializable - Return true if any parent values may be
94 /// This function must be called before ny rematerialization is attempted.
95 bool anyRematerializable(LiveIntervals&, const TargetInstrInfo&,
98 /// Remat - Information needed to rematerialize at a specific location.
100 VNInfo *ParentVNI; // parent_'s value at the remat location.
101 MachineInstr *OrigMI; // Instruction defining ParentVNI.
102 explicit Remat(VNInfo *ParentVNI) : ParentVNI(ParentVNI), OrigMI(0) {}
105 /// canRematerializeAt - Determine if ParentVNI can be rematerialized at
106 /// UseIdx. It is assumed that parent_.getVNINfoAt(UseIdx) == ParentVNI.
107 /// When cheapAsAMove is set, only cheap remats are allowed.
108 bool canRematerializeAt(Remat &RM,
113 /// rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an
114 /// instruction into MBB before MI. The new instruction is mapped, but
115 /// liveness is not updated.
116 /// Return the SlotIndex of the new instruction.
117 SlotIndex rematerializeAt(MachineBasicBlock &MBB,
118 MachineBasicBlock::iterator MI,
122 const TargetInstrInfo&,
123 const TargetRegisterInfo&);
125 /// markRematerialized - explicitly mark a value as rematerialized after doing
127 void markRematerialized(VNInfo *ParentVNI) {
128 rematted_.insert(ParentVNI);
131 /// didRematerialize - Return true if ParentVNI was rematerialized anywhere.
132 bool didRematerialize(VNInfo *ParentVNI) const {
133 return rematted_.count(ParentVNI);