1 //===- LiveIntervalCalc.cpp - Calculate live interval --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of the LiveIntervalCalc class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/LiveIntervalCalc.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/LiveInterval.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineOperand.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/SlotIndexes.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/MC/LaneBitmask.h"
22 #include "llvm/Support/ErrorHandling.h"
27 #define DEBUG_TYPE "regalloc"
29 // Reserve an address that indicates a value that is known to be "undef".
30 static VNInfo
UndefVNI(0xbad, SlotIndex());
32 static void createDeadDef(SlotIndexes
&Indexes
, VNInfo::Allocator
&Alloc
,
33 LiveRange
&LR
, const MachineOperand
&MO
) {
34 const MachineInstr
&MI
= *MO
.getParent();
36 Indexes
.getInstructionIndex(MI
).getRegSlot(MO
.isEarlyClobber());
38 // Create the def in LR. This may find an existing def.
39 LR
.createDeadDef(DefIdx
, Alloc
);
42 void LiveIntervalCalc::calculate(LiveInterval
&LI
, bool TrackSubRegs
) {
43 const MachineRegisterInfo
*MRI
= getRegInfo();
44 SlotIndexes
*Indexes
= getIndexes();
45 VNInfo::Allocator
*Alloc
= getVNAlloc();
47 assert(MRI
&& Indexes
&& "call reset() first");
49 // Step 1: Create minimal live segments for every definition of Reg.
50 // Visit all def operands. If the same instruction has multiple defs of Reg,
51 // createDeadDef() will deduplicate.
52 const TargetRegisterInfo
&TRI
= *MRI
->getTargetRegisterInfo();
53 Register Reg
= LI
.reg();
54 for (const MachineOperand
&MO
: MRI
->reg_nodbg_operands(Reg
)) {
55 if (!MO
.isDef() && !MO
.readsReg())
58 unsigned SubReg
= MO
.getSubReg();
59 if (LI
.hasSubRanges() || (SubReg
!= 0 && TrackSubRegs
)) {
60 LaneBitmask SubMask
= SubReg
!= 0 ? TRI
.getSubRegIndexLaneMask(SubReg
)
61 : MRI
->getMaxLaneMaskForVReg(Reg
);
62 // If this is the first time we see a subregister def, initialize
63 // subranges by creating a copy of the main range.
64 if (!LI
.hasSubRanges() && !LI
.empty()) {
65 LaneBitmask ClassMask
= MRI
->getMaxLaneMaskForVReg(Reg
);
66 LI
.createSubRangeFrom(*Alloc
, ClassMask
, LI
);
71 [&MO
, Indexes
, Alloc
](LiveInterval::SubRange
&SR
) {
73 createDeadDef(*Indexes
, *Alloc
, SR
, MO
);
78 // Create the def in the main liverange. We do not have to do this if
79 // subranges are tracked as we recreate the main range later in this case.
80 if (MO
.isDef() && !LI
.hasSubRanges())
81 createDeadDef(*Indexes
, *Alloc
, LI
, MO
);
84 // We may have created empty live ranges for partially undefined uses, we
85 // can't keep them because we won't find defs in them later.
86 LI
.removeEmptySubRanges();
88 const MachineFunction
*MF
= getMachineFunction();
89 MachineDominatorTree
*DomTree
= getDomTree();
90 // Step 2: Extend live segments to all uses, constructing SSA form as
92 if (LI
.hasSubRanges()) {
93 for (LiveInterval::SubRange
&S
: LI
.subranges()) {
94 LiveIntervalCalc SubLIC
;
95 SubLIC
.reset(MF
, Indexes
, DomTree
, Alloc
);
96 SubLIC
.extendToUses(S
, Reg
, S
.LaneMask
, &LI
);
99 constructMainRangeFromSubranges(LI
);
102 extendToUses(LI
, Reg
, LaneBitmask::getAll());
106 void LiveIntervalCalc::constructMainRangeFromSubranges(LiveInterval
&LI
) {
107 // First create dead defs at all defs found in subranges.
108 LiveRange
&MainRange
= LI
;
109 assert(MainRange
.segments
.empty() && MainRange
.valnos
.empty() &&
110 "Expect empty main liverange");
112 VNInfo::Allocator
*Alloc
= getVNAlloc();
113 for (const LiveInterval::SubRange
&SR
: LI
.subranges()) {
114 for (const VNInfo
*VNI
: SR
.valnos
) {
115 if (!VNI
->isUnused() && !VNI
->isPHIDef())
116 MainRange
.createDeadDef(VNI
->def
, *Alloc
);
120 extendToUses(MainRange
, LI
.reg(), LaneBitmask::getAll(), &LI
);
123 void LiveIntervalCalc::createDeadDefs(LiveRange
&LR
, Register Reg
) {
124 const MachineRegisterInfo
*MRI
= getRegInfo();
125 SlotIndexes
*Indexes
= getIndexes();
126 VNInfo::Allocator
*Alloc
= getVNAlloc();
127 assert(MRI
&& Indexes
&& "call reset() first");
129 // Visit all def operands. If the same instruction has multiple defs of Reg,
130 // LR.createDeadDef() will deduplicate.
131 for (MachineOperand
&MO
: MRI
->def_operands(Reg
))
132 createDeadDef(*Indexes
, *Alloc
, LR
, MO
);
135 void LiveIntervalCalc::extendToUses(LiveRange
&LR
, Register Reg
,
136 LaneBitmask Mask
, LiveInterval
*LI
) {
137 const MachineRegisterInfo
*MRI
= getRegInfo();
138 SlotIndexes
*Indexes
= getIndexes();
139 SmallVector
<SlotIndex
, 4> Undefs
;
141 LI
->computeSubRangeUndefs(Undefs
, Mask
, *MRI
, *Indexes
);
143 // Visit all operands that read Reg. This may include partial defs.
144 bool IsSubRange
= !Mask
.all();
145 const TargetRegisterInfo
&TRI
= *MRI
->getTargetRegisterInfo();
146 for (MachineOperand
&MO
: MRI
->reg_nodbg_operands(Reg
)) {
147 // Clear all kill flags. They will be reinserted after register allocation
148 // by LiveIntervals::addKillFlags().
151 // MO::readsReg returns "true" for subregister defs. This is for keeping
152 // liveness of the entire register (i.e. for the main range of the live
153 // interval). For subranges, definitions of non-overlapping subregisters
154 // do not count as uses.
155 if (!MO
.readsReg() || (IsSubRange
&& MO
.isDef()))
158 unsigned SubReg
= MO
.getSubReg();
160 LaneBitmask SLM
= TRI
.getSubRegIndexLaneMask(SubReg
);
163 // Ignore uses not reading the current (sub)range.
164 if ((SLM
& Mask
).none())
168 // Determine the actual place of the use.
169 const MachineInstr
*MI
= MO
.getParent();
170 unsigned OpNo
= (&MO
- &MI
->getOperand(0));
173 assert(!MO
.isDef() && "Cannot handle PHI def of partial register.");
174 // The actual place where a phi operand is used is the end of the pred
175 // MBB. PHI operands are paired: (Reg, PredMBB).
176 UseIdx
= Indexes
->getMBBEndIdx(MI
->getOperand(OpNo
+ 1).getMBB());
178 // Check for early-clobber redefs.
179 bool isEarlyClobber
= false;
182 isEarlyClobber
= MO
.isEarlyClobber();
183 else if (MI
->isRegTiedToDefOperand(OpNo
, &DefIdx
)) {
184 // FIXME: This would be a lot easier if tied early-clobber uses also
185 // had an early-clobber flag.
186 isEarlyClobber
= MI
->getOperand(DefIdx
).isEarlyClobber();
188 UseIdx
= Indexes
->getInstructionIndex(*MI
).getRegSlot(isEarlyClobber
);
191 // MI is reading Reg. We may have visited MI before if it happens to be
192 // reading Reg multiple times. That is OK, extend() is idempotent.
193 extend(LR
, UseIdx
, Reg
, Undefs
);