LLVM 20.0.0git
LiveIntervalCalc.cpp
Go to the documentation of this file.
1//===- LiveIntervalCalc.cpp - Calculate live interval --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementation of the LiveIntervalCalc class.
10//
11//===----------------------------------------------------------------------===//
12
21#include "llvm/MC/LaneBitmask.h"
22#include <cassert>
23
24using namespace llvm;
25
26#define DEBUG_TYPE "regalloc"
27
28// Reserve an address that indicates a value that is known to be "undef".
29static VNInfo UndefVNI(0xbad, SlotIndex());
30
31static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
32 LiveRange &LR, const MachineOperand &MO) {
33 const MachineInstr &MI = *MO.getParent();
34 SlotIndex DefIdx =
36
37 // Create the def in LR. This may find an existing def.
38 LR.createDeadDef(DefIdx, Alloc);
39}
40
41void LiveIntervalCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
43 SlotIndexes *Indexes = getIndexes();
45
46 assert(MRI && Indexes && "call reset() first");
47
48 // Step 1: Create minimal live segments for every definition of Reg.
49 // Visit all def operands. If the same instruction has multiple defs of Reg,
50 // createDeadDef() will deduplicate.
51 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
52 Register Reg = LI.reg();
53 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
54 if (!MO.isDef() && !MO.readsReg())
55 continue;
56
57 unsigned SubReg = MO.getSubReg();
58 if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
59 LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
60 : MRI->getMaxLaneMaskForVReg(Reg);
61 // If this is the first time we see a subregister def, initialize
62 // subranges by creating a copy of the main range.
63 if (!LI.hasSubRanges() && !LI.empty()) {
64 LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
65 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
66 }
67
69 *Alloc, SubMask,
70 [&MO, Indexes, Alloc](LiveInterval::SubRange &SR) {
71 if (MO.isDef())
72 createDeadDef(*Indexes, *Alloc, SR, MO);
73 },
74 *Indexes, TRI);
75 }
76
77 // Create the def in the main liverange. We do not have to do this if
78 // subranges are tracked as we recreate the main range later in this case.
79 if (MO.isDef() && !LI.hasSubRanges())
80 createDeadDef(*Indexes, *Alloc, LI, MO);
81 }
82
83 // We may have created empty live ranges for partially undefined uses, we
84 // can't keep them because we won't find defs in them later.
86
89 // Step 2: Extend live segments to all uses, constructing SSA form as
90 // necessary.
91 if (LI.hasSubRanges()) {
92 for (LiveInterval::SubRange &S : LI.subranges()) {
93 LiveIntervalCalc SubLIC;
94 SubLIC.reset(MF, Indexes, DomTree, Alloc);
95 SubLIC.extendToUses(S, Reg, S.LaneMask, &LI);
96 }
97 LI.clear();
99 } else {
101 extendToUses(LI, Reg, LaneBitmask::getAll());
102 }
103}
104
106 // First create dead defs at all defs found in subranges.
107 LiveRange &MainRange = LI;
108 assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
109 "Expect empty main liverange");
110
111 VNInfo::Allocator *Alloc = getVNAlloc();
112 for (const LiveInterval::SubRange &SR : LI.subranges()) {
113 for (const VNInfo *VNI : SR.valnos) {
114 if (!VNI->isUnused() && !VNI->isPHIDef())
115 MainRange.createDeadDef(VNI->def, *Alloc);
116 }
117 }
119 extendToUses(MainRange, LI.reg(), LaneBitmask::getAll(), &LI);
120}
121
124 SlotIndexes *Indexes = getIndexes();
125 VNInfo::Allocator *Alloc = getVNAlloc();
126 assert(MRI && Indexes && "call reset() first");
127
128 // Visit all def operands. If the same instruction has multiple defs of Reg,
129 // LR.createDeadDef() will deduplicate.
130 for (MachineOperand &MO : MRI->def_operands(Reg))
131 createDeadDef(*Indexes, *Alloc, LR, MO);
132}
133
134void LiveIntervalCalc::extendToUses(LiveRange &LR, Register Reg,
135 LaneBitmask Mask, LiveInterval *LI) {
137 SlotIndexes *Indexes = getIndexes();
139 if (LI != nullptr)
140 LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
141
142 // Visit all operands that read Reg. This may include partial defs.
143 bool IsSubRange = !Mask.all();
144 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
145 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
146 // Clear all kill flags. They will be reinserted after register allocation
147 // by LiveIntervals::addKillFlags().
148 if (MO.isUse())
149 MO.setIsKill(false);
150 // MO::readsReg returns "true" for subregister defs. This is for keeping
151 // liveness of the entire register (i.e. for the main range of the live
152 // interval). For subranges, definitions of non-overlapping subregisters
153 // do not count as uses.
154 if (!MO.readsReg() || (IsSubRange && MO.isDef()))
155 continue;
156
157 unsigned SubReg = MO.getSubReg();
158 if (SubReg != 0) {
159 LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
160 if (MO.isDef())
161 SLM = ~SLM;
162 // Ignore uses not reading the current (sub)range.
163 if ((SLM & Mask).none())
164 continue;
165 }
166
167 // Determine the actual place of the use.
168 const MachineInstr *MI = MO.getParent();
169 unsigned OpNo = (&MO - &MI->getOperand(0));
170 SlotIndex UseIdx;
171 if (MI->isPHI()) {
172 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
173 // The actual place where a phi operand is used is the end of the pred
174 // MBB. PHI operands are paired: (Reg, PredMBB).
175 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo + 1).getMBB());
176 } else {
177 // Check for early-clobber redefs.
178 bool isEarlyClobber = false;
179 unsigned DefIdx;
180 if (MO.isDef())
181 isEarlyClobber = MO.isEarlyClobber();
182 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
183 // FIXME: This would be a lot easier if tied early-clobber uses also
184 // had an early-clobber flag.
185 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
186 }
187 UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
188 }
189
190 // MI is reading Reg. We may have visited MI before if it happens to be
191 // reading Reg multiple times. That is OK, extend() is idempotent.
192 extend(LR, UseIdx, Reg, Undefs);
193 }
194}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
IRTranslator LLVM IR MI
A common definition of LaneBitmask for use in TableGen and CodeGen.
static VNInfo UndefVNI(0xbad, SlotIndex())
static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc, LiveRange &LR, const MachineOperand &MO)
unsigned const TargetRegisterInfo * TRI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
void createDeadDefs(LiveRange &LR, Register Reg)
createDeadDefs - Create a dead def in LI for every def operand of Reg.
void calculate(LiveInterval &LI, bool TrackSubRegs)
Calculates liveness for the register specified in live interval LI.
void constructMainRangeFromSubranges(LiveInterval &LI)
For live interval LI with correct SubRanges construct matching information for the main live range.
A live range for subregisters.
Definition: LiveInterval.h:694
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:687
void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
Register reg() const
Definition: LiveInterval.h:718
bool hasSubRanges() const
Returns true if subregister liveness information is available.
Definition: LiveInterval.h:810
SubRange * createSubRangeFrom(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, const LiveRange &CopyFrom)
Like createSubRange() but the new range is filled with a copy of the liveness information in CopyFrom...
Definition: LiveInterval.h:801
iterator_range< subrange_iterator > subranges()
Definition: LiveInterval.h:782
void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, std::function< void(LiveInterval::SubRange &)> Apply, const SlotIndexes &Indexes, const TargetRegisterInfo &TRI, unsigned ComposeSubRegIdx=0)
Refines the subranges to support LaneMask.
void computeSubRangeUndefs(SmallVectorImpl< SlotIndex > &Undefs, LaneBitmask LaneMask, const MachineRegisterInfo &MRI, const SlotIndexes &Indexes) const
For a given lane mask LaneMask, compute indexes at which the lane is marked undefined by subregister ...
SlotIndexes * getIndexes()
MachineDominatorTree * getDomTree()
void resetLiveOutMap()
Reset Map and Seen fields.
VNInfo::Allocator * getVNAlloc()
void reset(const MachineFunction *mf, SlotIndexes *SI, MachineDominatorTree *MDT, VNInfo::Allocator *VNIA)
reset - Prepare caches for a new set of non-overlapping live ranges.
const MachineFunction * getMachineFunction()
Some getters to expose in a read-only way some private fields to subclasses.
const MachineRegisterInfo * getRegInfo() const
void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg, ArrayRef< SlotIndex > Undefs)
Extend the live range of LR to reach Use.
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:157
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc)
createDeadDef - Make sure the range has a value defined at Def.
bool empty() const
Definition: LiveInterval.h:382
Segments segments
Definition: LiveInterval.h:203
VNInfoList valnos
Definition: LiveInterval.h:204
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isEarlyClobber() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:65
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
Definition: SlotIndexes.h:237
SlotIndexes pass.
Definition: SlotIndexes.h:297
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
Definition: SlotIndexes.h:470
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
Definition: SlotIndexes.h:379
bool empty() const
Definition: SmallVector.h:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
VNInfo - Value Number Information.
Definition: LiveInterval.h:53
bool isUnused() const
Returns true if this value is unused.
Definition: LiveInterval.h:81
SlotIndex def
The index of the defining instruction.
Definition: LiveInterval.h:61
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
Definition: LiveInterval.h:78
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static constexpr LaneBitmask getAll()
Definition: LaneBitmask.h:82