blob: 51117856af02b231f72111e1a2a553ad33c3d5a5 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
Jakob Stoklund Olesen4281e202012-01-07 07:39:47 +000018#define DEBUG_TYPE "regalloc"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "LiveRangeCalc.h"
21#include "llvm/ADT/DenseSet.h"
22#include "llvm/ADT/STLExtras.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000023#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/LiveVariables.h"
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +000025#include "llvm/CodeGen/MachineDominators.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000026#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000028#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000029#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/Value.h"
Benjamin Kramer4eed7562013-06-17 19:00:36 +000031#include "llvm/Support/BlockFrequency.h"
Jakob Stoklund Olesen3dfa38a2012-07-27 20:58:46 +000032#include "llvm/Support/CommandLine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000039#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000040#include <cmath>
Chandler Carruthd04a8d42012-12-03 16:50:05 +000041#include <limits>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000042using namespace llvm;
43
Devang Patel19974732007-05-03 01:11:54 +000044char LiveIntervals::ID = 0;
Jakob Stoklund Olesendcc44362012-08-03 22:12:54 +000045char &llvm::LiveIntervalsID = LiveIntervals::ID;
Owen Anderson2ab36d32010-10-12 19:48:12 +000046INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals",
47 "Live Interval Analysis", false, false)
Andrew Trick8dd26252012-02-10 04:10:36 +000048INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Owen Anderson2ab36d32010-10-12 19:48:12 +000049INITIALIZE_PASS_DEPENDENCY(LiveVariables)
Andrew Trick8dd26252012-02-10 04:10:36 +000050INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Owen Anderson2ab36d32010-10-12 19:48:12 +000051INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
Owen Anderson2ab36d32010-10-12 19:48:12 +000052INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
Owen Andersonce665bd2010-10-07 22:25:06 +000053 "Live Interval Analysis", false, false)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000054
Andrew Trickc6bae792013-06-21 18:33:23 +000055#ifndef NDEBUG
56static cl::opt<bool> EnablePrecomputePhysRegs(
57 "precompute-phys-liveness", cl::Hidden,
58 cl::desc("Eagerly compute live intervals for all physreg units."));
59#else
60static bool EnablePrecomputePhysRegs = false;
61#endif // NDEBUG
62
Chris Lattnerf7da2c72006-08-24 22:43:55 +000063void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000064 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000065 AU.addRequired<AliasAnalysis>();
66 AU.addPreserved<AliasAnalysis>();
Jakob Stoklund Olesenec7b25d2013-02-09 00:04:07 +000067 // LiveVariables isn't really required by this analysis, it is only required
68 // here to make sure it is live during TwoAddressInstructionPass and
69 // PHIElimination. This is temporary.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000070 AU.addRequired<LiveVariables>();
Evan Cheng148341c2010-08-17 21:00:37 +000071 AU.addPreserved<LiveVariables>();
Andrew Trickd35576b2012-02-13 20:44:42 +000072 AU.addPreservedID(MachineLoopInfoID);
Jakob Stoklund Olesenc4118452012-06-20 23:31:34 +000073 AU.addRequiredTransitiveID(MachineDominatorsID);
Bill Wendling67d65bb2008-01-04 20:54:55 +000074 AU.addPreservedID(MachineDominatorsID);
Lang Hames233a60e2009-11-03 23:52:08 +000075 AU.addPreserved<SlotIndexes>();
76 AU.addRequiredTransitive<SlotIndexes>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000078}
79
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +000080LiveIntervals::LiveIntervals() : MachineFunctionPass(ID),
81 DomTree(0), LRCalc(0) {
82 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
83}
84
85LiveIntervals::~LiveIntervals() {
86 delete LRCalc;
87}
88
Chris Lattnerf7da2c72006-08-24 22:43:55 +000089void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000090 // Free the live intervals themselves.
Jakob Stoklund Olesen7fa67842012-06-22 20:37:52 +000091 for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)
92 delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)];
93 VirtRegIntervals.clear();
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +000094 RegMaskSlots.clear();
95 RegMaskBits.clear();
Jakob Stoklund Olesen34e85d02012-02-10 01:26:29 +000096 RegMaskBlocks.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000097
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +000098 for (unsigned i = 0, e = RegUnitIntervals.size(); i != e; ++i)
99 delete RegUnitIntervals[i];
100 RegUnitIntervals.clear();
101
Benjamin Kramerce9a20b2010-06-26 11:30:59 +0000102 // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd.
103 VNInfoAllocator.Reset();
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000104}
105
Jakob Stoklund Olesen2aeef002013-08-14 17:28:46 +0000106/// runOnMachineFunction - calculates LiveIntervals
Owen Anderson80b3ce62008-05-28 20:54:50 +0000107///
108bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000109 MF = &fn;
110 MRI = &MF->getRegInfo();
111 TM = &fn.getTarget();
112 TRI = TM->getRegisterInfo();
113 TII = TM->getInstrInfo();
114 AA = &getAnalysis<AliasAnalysis>();
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000115 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenc4118452012-06-20 23:31:34 +0000116 DomTree = &getAnalysis<MachineDominatorTree>();
117 if (!LRCalc)
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000118 LRCalc = new LiveRangeCalc();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000119
Jakob Stoklund Olesen3dfa38a2012-07-27 20:58:46 +0000120 // Allocate space for all virtual registers.
121 VirtRegIntervals.resize(MRI->getNumVirtRegs());
122
Jakob Stoklund Olesenec7b25d2013-02-09 00:04:07 +0000123 computeVirtRegs();
124 computeRegMasks();
Jakob Stoklund Olesenc4118452012-06-20 23:31:34 +0000125 computeLiveInRegUnits();
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000126
Andrew Trickc6bae792013-06-21 18:33:23 +0000127 if (EnablePrecomputePhysRegs) {
128 // For stress testing, precompute live ranges of all physical register
129 // units, including reserved registers.
130 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
131 getRegUnit(i);
132 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000133 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000135}
136
Chris Lattner70ca3582004-09-30 15:59:17 +0000137/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000138void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000139 OS << "********** INTERVALS **********\n";
Jakob Stoklund Olesenf658af52012-02-14 23:46:21 +0000140
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000141 // Dump the regunits.
142 for (unsigned i = 0, e = RegUnitIntervals.size(); i != e; ++i)
143 if (LiveInterval *LI = RegUnitIntervals[i])
144 OS << PrintRegUnit(i, TRI) << " = " << *LI << '\n';
145
Jakob Stoklund Olesenf658af52012-02-14 23:46:21 +0000146 // Dump the virtregs.
Jakob Stoklund Olesen7fa67842012-06-22 20:37:52 +0000147 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
148 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
149 if (hasInterval(Reg))
150 OS << PrintReg(Reg) << " = " << getInterval(Reg) << '\n';
151 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000152
Jakob Stoklund Olesen722c9a72012-11-09 19:18:49 +0000153 OS << "RegMasks:";
154 for (unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i)
155 OS << ' ' << RegMaskSlots[i];
156 OS << '\n';
157
Evan Cheng752195e2009-09-14 21:33:42 +0000158 printInstrs(OS);
159}
160
161void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000162 OS << "********** MACHINEINSTRS **********\n";
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000163 MF->print(OS, Indexes);
Chris Lattner70ca3582004-09-30 15:59:17 +0000164}
165
Manman Renb720be62012-09-11 22:23:19 +0000166#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Evan Cheng752195e2009-09-14 21:33:42 +0000167void LiveIntervals::dumpInstrs() const {
David Greene8a342292010-01-04 22:49:02 +0000168 printInstrs(dbgs());
Evan Cheng752195e2009-09-14 21:33:42 +0000169}
Manman Ren77e300e2012-09-06 19:06:06 +0000170#endif
Evan Cheng752195e2009-09-14 21:33:42 +0000171
Owen Anderson03857b22008-08-13 21:49:13 +0000172LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000173 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +0000174 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000175}
Evan Chengf2fbca62007-11-12 06:35:08 +0000176
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000177
Jakob Stoklund Olesen3dfa38a2012-07-27 20:58:46 +0000178/// computeVirtRegInterval - Compute the live interval of a virtual register,
179/// based on defs and uses.
180void LiveIntervals::computeVirtRegInterval(LiveInterval *LI) {
181 assert(LRCalc && "LRCalc not initialized.");
182 assert(LI->empty() && "Should only compute empty intervals.");
183 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
184 LRCalc->createDeadDefs(LI);
185 LRCalc->extendToUses(LI);
186}
187
Jakob Stoklund Olesenc16bf792012-07-27 21:56:39 +0000188void LiveIntervals::computeVirtRegs() {
189 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
190 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
191 if (MRI->reg_nodbg_empty(Reg))
192 continue;
Mark Laceye742d682013-08-14 23:50:16 +0000193 createAndComputeVirtRegInterval(Reg);
Jakob Stoklund Olesenc16bf792012-07-27 21:56:39 +0000194 }
195}
196
197void LiveIntervals::computeRegMasks() {
198 RegMaskBlocks.resize(MF->getNumBlockIDs());
199
200 // Find all instructions with regmask operands.
201 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
202 MBBI != E; ++MBBI) {
203 MachineBasicBlock *MBB = MBBI;
204 std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB->getNumber()];
205 RMB.first = RegMaskSlots.size();
206 for (MachineBasicBlock::iterator MI = MBB->begin(), ME = MBB->end();
207 MI != ME; ++MI)
208 for (MIOperands MO(MI); MO.isValid(); ++MO) {
209 if (!MO->isRegMask())
210 continue;
211 RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot());
212 RegMaskBits.push_back(MO->getRegMask());
213 }
214 // Compute the number of register mask instructions in this block.
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000215 RMB.second = RegMaskSlots.size() - RMB.first;
Jakob Stoklund Olesenc16bf792012-07-27 21:56:39 +0000216 }
217}
Jakob Stoklund Olesen3dfa38a2012-07-27 20:58:46 +0000218
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000219//===----------------------------------------------------------------------===//
220// Register Unit Liveness
221//===----------------------------------------------------------------------===//
222//
223// Fixed interference typically comes from ABI boundaries: Function arguments
224// and return values are passed in fixed registers, and so are exception
225// pointers entering landing pads. Certain instructions require values to be
226// present in specific registers. That is also represented through fixed
227// interference.
228//
229
230/// computeRegUnitInterval - Compute the live interval of a register unit, based
231/// on the uses and defs of aliasing registers. The interval should be empty,
232/// or contain only dead phi-defs from ABI blocks.
233void LiveIntervals::computeRegUnitInterval(LiveInterval *LI) {
234 unsigned Unit = LI->reg;
235
236 assert(LRCalc && "LRCalc not initialized.");
237 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
238
239 // The physregs aliasing Unit are the roots and their super-registers.
240 // Create all values as dead defs before extending to uses. Note that roots
241 // may share super-registers. That's OK because createDeadDefs() is
242 // idempotent. It is very rare for a register unit to have multiple roots, so
243 // uniquing super-registers is probably not worthwhile.
244 for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
Chad Rosierb018bab2013-05-22 22:36:55 +0000245 for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
246 Supers.isValid(); ++Supers) {
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000247 if (!MRI->reg_empty(*Supers))
248 LRCalc->createDeadDefs(LI, *Supers);
249 }
250 }
251
252 // Now extend LI to reach all uses.
253 // Ignore uses of reserved registers. We only track defs of those.
254 for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
Chad Rosierb018bab2013-05-22 22:36:55 +0000255 for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
256 Supers.isValid(); ++Supers) {
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000257 unsigned Reg = *Supers;
Jakob Stoklund Olesen79004762012-10-15 22:14:34 +0000258 if (!MRI->isReserved(Reg) && !MRI->reg_empty(Reg))
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000259 LRCalc->extendToUses(LI, Reg);
260 }
261 }
262}
263
264
265/// computeLiveInRegUnits - Precompute the live ranges of any register units
266/// that are live-in to an ABI block somewhere. Register values can appear
267/// without a corresponding def when entering the entry block or a landing pad.
268///
269void LiveIntervals::computeLiveInRegUnits() {
270 RegUnitIntervals.resize(TRI->getNumRegUnits());
271 DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n");
272
273 // Keep track of the intervals allocated.
274 SmallVector<LiveInterval*, 8> NewIntvs;
275
276 // Check all basic blocks for live-ins.
277 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
278 MFI != MFE; ++MFI) {
279 const MachineBasicBlock *MBB = MFI;
280
281 // We only care about ABI blocks: Entry + landing pads.
282 if ((MFI != MF->begin() && !MBB->isLandingPad()) || MBB->livein_empty())
283 continue;
284
285 // Create phi-defs at Begin for all live-in registers.
286 SlotIndex Begin = Indexes->getMBBStartIdx(MBB);
287 DEBUG(dbgs() << Begin << "\tBB#" << MBB->getNumber());
288 for (MachineBasicBlock::livein_iterator LII = MBB->livein_begin(),
289 LIE = MBB->livein_end(); LII != LIE; ++LII) {
290 for (MCRegUnitIterator Units(*LII, TRI); Units.isValid(); ++Units) {
291 unsigned Unit = *Units;
292 LiveInterval *Intv = RegUnitIntervals[Unit];
293 if (!Intv) {
294 Intv = RegUnitIntervals[Unit] = new LiveInterval(Unit, HUGE_VALF);
295 NewIntvs.push_back(Intv);
296 }
297 VNInfo *VNI = Intv->createDeadDef(Begin, getVNInfoAllocator());
Matt Beaumont-Gay05b46f02012-06-05 23:00:03 +0000298 (void)VNI;
Jakob Stoklund Olesen34c6f982012-06-05 22:02:15 +0000299 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id);
300 }
301 }
302 DEBUG(dbgs() << '\n');
303 }
304 DEBUG(dbgs() << "Created " << NewIntvs.size() << " new intervals.\n");
305
306 // Compute the 'normal' part of the intervals.
307 for (unsigned i = 0, e = NewIntvs.size(); i != e; ++i)
308 computeRegUnitInterval(NewIntvs[i]);
309}
310
311
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000312/// shrinkToUses - After removing some uses of a register, shrink its live
313/// range to just the remaining uses. This method does not compute reaching
314/// defs for new uses, and it doesn't remove dead defs.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000315bool LiveIntervals::shrinkToUses(LiveInterval *li,
Jakob Stoklund Olesen0d8ccaa2011-03-07 23:29:10 +0000316 SmallVectorImpl<MachineInstr*> *dead) {
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000317 DEBUG(dbgs() << "Shrink: " << *li << '\n');
318 assert(TargetRegisterInfo::isVirtualRegister(li->reg)
Lang Hames567cdba2012-01-03 20:05:57 +0000319 && "Can only shrink virtual registers");
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000320 // Find all the values used, including PHI kills.
321 SmallVector<std::pair<SlotIndex, VNInfo*>, 16> WorkList;
322
Jakob Stoklund Olesen031432f2011-09-15 15:24:16 +0000323 // Blocks that have already been added to WorkList as live-out.
324 SmallPtrSet<MachineBasicBlock*, 16> LiveOut;
325
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000326 // Visit all instructions reading li->reg.
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000327 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(li->reg);
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000328 MachineInstr *UseMI = I.skipInstruction();) {
329 if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg))
330 continue;
Jakob Stoklund Olesen6c9cc212011-11-13 23:53:25 +0000331 SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot();
Jakob Stoklund Olesen97769fc2012-05-20 02:54:52 +0000332 LiveRangeQuery LRQ(*li, Idx);
333 VNInfo *VNI = LRQ.valueIn();
Jakob Stoklund Olesen9ef931e2011-03-18 03:06:04 +0000334 if (!VNI) {
335 // This shouldn't happen: readsVirtualRegister returns true, but there is
336 // no live value. It is likely caused by a target getting <undef> flags
337 // wrong.
338 DEBUG(dbgs() << Idx << '\t' << *UseMI
339 << "Warning: Instr claims to read non-existent value in "
340 << *li << '\n');
341 continue;
342 }
Jakob Stoklund Olesenf054e192011-11-14 18:45:38 +0000343 // Special case: An early-clobber tied operand reads and writes the
Jakob Stoklund Olesen97769fc2012-05-20 02:54:52 +0000344 // register one slot early.
345 if (VNInfo *DefVNI = LRQ.valueDefined())
346 Idx = DefVNI->def;
347
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000348 WorkList.push_back(std::make_pair(Idx, VNI));
349 }
350
Matthias Braun87a86052013-10-10 21:28:47 +0000351 // Create new live ranges with only minimal live segments per def.
352 LiveRange NewLR;
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000353 for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end();
354 I != E; ++I) {
355 VNInfo *VNI = *I;
356 if (VNI->isUnused())
357 continue;
Matthias Braun87a86052013-10-10 21:28:47 +0000358 NewLR.addSegment(LiveRange::Segment(VNI->def, VNI->def.getDeadSlot(), VNI));
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000359 }
360
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000361 // Keep track of the PHIs that are in use.
362 SmallPtrSet<VNInfo*, 8> UsedPHIs;
363
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000364 // Extend intervals to reach all uses in WorkList.
365 while (!WorkList.empty()) {
366 SlotIndex Idx = WorkList.back().first;
367 VNInfo *VNI = WorkList.back().second;
368 WorkList.pop_back();
Jakob Stoklund Olesen6c9cc212011-11-13 23:53:25 +0000369 const MachineBasicBlock *MBB = getMBBFromIndex(Idx.getPrevSlot());
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000370 SlotIndex BlockStart = getMBBStartIdx(MBB);
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000371
372 // Extend the live range for VNI to be live at Idx.
Matthias Braun87a86052013-10-10 21:28:47 +0000373 if (VNInfo *ExtVNI = NewLR.extendInBlock(BlockStart, Idx)) {
Nick Lewycky4b11a702011-03-02 01:43:30 +0000374 (void)ExtVNI;
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000375 assert(ExtVNI == VNI && "Unexpected existing value number");
376 // Is this a PHIDef we haven't seen before?
Jakob Stoklund Olesenc29d9b32011-03-03 00:20:51 +0000377 if (!VNI->isPHIDef() || VNI->def != BlockStart || !UsedPHIs.insert(VNI))
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000378 continue;
379 // The PHI is live, make sure the predecessors are live-out.
380 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
381 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen031432f2011-09-15 15:24:16 +0000382 if (!LiveOut.insert(*PI))
383 continue;
Jakob Stoklund Olesen6c9cc212011-11-13 23:53:25 +0000384 SlotIndex Stop = getMBBEndIdx(*PI);
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000385 // A predecessor is not required to have a live-out value for a PHI.
Jakob Stoklund Olesen6c9cc212011-11-13 23:53:25 +0000386 if (VNInfo *PVNI = li->getVNInfoBefore(Stop))
Jakob Stoklund Olesene0ab2452011-03-02 00:33:03 +0000387 WorkList.push_back(std::make_pair(Stop, PVNI));
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000388 }
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000389 continue;
390 }
391
392 // VNI is live-in to MBB.
393 DEBUG(dbgs() << " live-in at " << BlockStart << '\n');
Matthias Braun87a86052013-10-10 21:28:47 +0000394 NewLR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI));
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000395
396 // Make sure VNI is live-out from the predecessors.
397 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
398 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen031432f2011-09-15 15:24:16 +0000399 if (!LiveOut.insert(*PI))
400 continue;
Jakob Stoklund Olesen6c9cc212011-11-13 23:53:25 +0000401 SlotIndex Stop = getMBBEndIdx(*PI);
402 assert(li->getVNInfoBefore(Stop) == VNI &&
403 "Wrong value out of predecessor");
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000404 WorkList.push_back(std::make_pair(Stop, VNI));
405 }
406 }
407
408 // Handle dead values.
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000409 bool CanSeparate = false;
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000410 for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end();
411 I != E; ++I) {
412 VNInfo *VNI = *I;
413 if (VNI->isUnused())
414 continue;
Matthias Braun87a86052013-10-10 21:28:47 +0000415 LiveRange::iterator LRI = NewLR.FindSegmentContaining(VNI->def);
416 assert(LRI != NewLR.end() && "Missing segment for PHI");
417 if (LRI->end != VNI->def.getDeadSlot())
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000418 continue;
Jakob Stoklund Olesena4d34732011-03-02 00:33:01 +0000419 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000420 // This is a dead PHI. Remove it.
Jakob Stoklund Olesenb2beac22012-08-03 20:59:32 +0000421 VNI->markUnused();
Matthias Braun87a86052013-10-10 21:28:47 +0000422 NewLR.removeSegment(LRI->start, LRI->end);
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000423 DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n");
424 CanSeparate = true;
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000425 } else {
426 // This is a dead def. Make sure the instruction knows.
427 MachineInstr *MI = getInstructionFromIndex(VNI->def);
428 assert(MI && "No instruction defining live value");
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000429 MI->addRegisterDead(li->reg, TRI);
Jakob Stoklund Olesen0d8ccaa2011-03-07 23:29:10 +0000430 if (dead && MI->allDefsAreDead()) {
Jakob Stoklund Olesenc46570d2011-03-16 22:56:08 +0000431 DEBUG(dbgs() << "All defs dead: " << VNI->def << '\t' << *MI);
Jakob Stoklund Olesen0d8ccaa2011-03-07 23:29:10 +0000432 dead->push_back(MI);
433 }
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000434 }
435 }
436
Matthias Braun331de112013-10-10 21:28:43 +0000437 // Move the trimmed segments back.
Matthias Braun87a86052013-10-10 21:28:47 +0000438 li->segments.swap(NewLR.segments);
Jakob Stoklund Olesenc46570d2011-03-16 22:56:08 +0000439 DEBUG(dbgs() << "Shrunk: " << *li << '\n');
Jakob Stoklund Olesen6a3dbd32011-03-17 20:37:07 +0000440 return CanSeparate;
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000441}
442
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000443void LiveIntervals::extendToIndices(LiveInterval *LI,
444 ArrayRef<SlotIndex> Indices) {
445 assert(LRCalc && "LRCalc not initialized.");
446 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
447 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
448 LRCalc->extend(LI, Indices[i]);
449}
450
451void LiveIntervals::pruneValue(LiveInterval *LI, SlotIndex Kill,
452 SmallVectorImpl<SlotIndex> *EndPoints) {
453 LiveRangeQuery LRQ(*LI, Kill);
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000454 VNInfo *VNI = LRQ.valueOut();
455 if (!VNI)
456 return;
457
458 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill);
459 SlotIndex MBBStart, MBBEnd;
460 tie(MBBStart, MBBEnd) = Indexes->getMBBRange(KillMBB);
461
462 // If VNI isn't live out from KillMBB, the value is trivially pruned.
463 if (LRQ.endPoint() < MBBEnd) {
Matthias Braun331de112013-10-10 21:28:43 +0000464 LI->removeSegment(Kill, LRQ.endPoint());
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000465 if (EndPoints) EndPoints->push_back(LRQ.endPoint());
466 return;
467 }
468
469 // VNI is live out of KillMBB.
Matthias Braun331de112013-10-10 21:28:43 +0000470 LI->removeSegment(Kill, MBBEnd);
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000471 if (EndPoints) EndPoints->push_back(MBBEnd);
472
Jakob Stoklund Olesenaf896902012-10-13 16:15:31 +0000473 // Find all blocks that are reachable from KillMBB without leaving VNI's live
474 // range. It is possible that KillMBB itself is reachable, so start a DFS
475 // from each successor.
476 typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy;
477 VisitedTy Visited;
478 for (MachineBasicBlock::succ_iterator
479 SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();
480 SuccI != SuccE; ++SuccI) {
481 for (df_ext_iterator<MachineBasicBlock*, VisitedTy>
482 I = df_ext_begin(*SuccI, Visited), E = df_ext_end(*SuccI, Visited);
483 I != E;) {
484 MachineBasicBlock *MBB = *I;
485
486 // Check if VNI is live in to MBB.
487 tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB);
488 LiveRangeQuery LRQ(*LI, MBBStart);
489 if (LRQ.valueIn() != VNI) {
Matthias Braun331de112013-10-10 21:28:43 +0000490 // This block isn't part of the VNI segment. Prune the search.
Jakob Stoklund Olesenaf896902012-10-13 16:15:31 +0000491 I.skipChildren();
492 continue;
493 }
494
495 // Prune the search if VNI is killed in MBB.
496 if (LRQ.endPoint() < MBBEnd) {
Matthias Braun331de112013-10-10 21:28:43 +0000497 LI->removeSegment(MBBStart, LRQ.endPoint());
Jakob Stoklund Olesenaf896902012-10-13 16:15:31 +0000498 if (EndPoints) EndPoints->push_back(LRQ.endPoint());
499 I.skipChildren();
500 continue;
501 }
502
503 // VNI is live through MBB.
Matthias Braun331de112013-10-10 21:28:43 +0000504 LI->removeSegment(MBBStart, MBBEnd);
Jakob Stoklund Olesenaf896902012-10-13 16:15:31 +0000505 if (EndPoints) EndPoints->push_back(MBBEnd);
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000506 ++I;
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000507 }
Jakob Stoklund Olesen87f78642012-09-17 23:03:25 +0000508 }
509}
Jakob Stoklund Olesen11513e52011-02-08 00:03:05 +0000510
Evan Chengf2fbca62007-11-12 06:35:08 +0000511//===----------------------------------------------------------------------===//
512// Register allocator hooks.
513//
514
Jakob Stoklund Olesene617ccb2012-09-06 18:15:18 +0000515void LiveIntervals::addKillFlags(const VirtRegMap *VRM) {
516 // Keep track of regunit ranges.
517 SmallVector<std::pair<LiveInterval*, LiveInterval::iterator>, 8> RU;
518
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +0000519 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
520 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000521 if (MRI->reg_nodbg_empty(Reg))
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +0000522 continue;
Jakob Stoklund Olesen12a7be92012-06-20 23:23:59 +0000523 LiveInterval *LI = &getInterval(Reg);
Jakob Stoklund Olesene617ccb2012-09-06 18:15:18 +0000524 if (LI->empty())
525 continue;
526
527 // Find the regunit intervals for the assigned register. They may overlap
528 // the virtual register live range, cancelling any kills.
529 RU.clear();
530 for (MCRegUnitIterator Units(VRM->getPhys(Reg), TRI); Units.isValid();
531 ++Units) {
532 LiveInterval *RUInt = &getRegUnit(*Units);
533 if (RUInt->empty())
534 continue;
535 RU.push_back(std::make_pair(RUInt, RUInt->find(LI->begin()->end)));
536 }
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +0000537
Matthias Braun331de112013-10-10 21:28:43 +0000538 // Every instruction that kills Reg corresponds to a segment range end
539 // point.
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +0000540 for (LiveInterval::iterator RI = LI->begin(), RE = LI->end(); RI != RE;
541 ++RI) {
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000542 // A block index indicates an MBB edge.
543 if (RI->end.isBlock())
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +0000544 continue;
545 MachineInstr *MI = getInstructionFromIndex(RI->end);
546 if (!MI)
547 continue;
Jakob Stoklund Olesene617ccb2012-09-06 18:15:18 +0000548
Matthias Braunb1aa5e42013-10-04 16:52:58 +0000549 // Check if any of the regunits are live beyond the end of RI. That could
Jakob Stoklund Olesene617ccb2012-09-06 18:15:18 +0000550 // happen when a physreg is defined as a copy of a virtreg:
551 //
552 // %EAX = COPY %vreg5
553 // FOO %vreg5 <--- MI, cancel kill because %EAX is live.
554 // BAR %EAX<kill>
555 //
556 // There should be no kill flag on FOO when %vreg5 is rewritten as %EAX.
557 bool CancelKill = false;
558 for (unsigned u = 0, e = RU.size(); u != e; ++u) {
559 LiveInterval *RInt = RU[u].first;
560 LiveInterval::iterator &I = RU[u].second;
561 if (I == RInt->end())
562 continue;
563 I = RInt->advanceTo(I, RI->end);
564 if (I == RInt->end() || I->start >= RI->end)
565 continue;
566 // I is overlapping RI.
567 CancelKill = true;
568 break;
569 }
570 if (CancelKill)
571 MI->clearRegisterKills(Reg, NULL);
572 else
573 MI->addRegisterKilled(Reg, NULL);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +0000574 }
575 }
576}
577
Jakob Stoklund Olesenebf27502012-02-10 01:23:55 +0000578MachineBasicBlock*
579LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const {
580 // A local live range must be fully contained inside the block, meaning it is
581 // defined and killed at instructions, not at block boundaries. It is not
582 // live in or or out of any block.
583 //
584 // It is technically possible to have a PHI-defined live range identical to a
585 // single block, but we are going to return false in that case.
Lang Hames233a60e2009-11-03 23:52:08 +0000586
Jakob Stoklund Olesenebf27502012-02-10 01:23:55 +0000587 SlotIndex Start = LI.beginIndex();
588 if (Start.isBlock())
589 return NULL;
Lang Hames233a60e2009-11-03 23:52:08 +0000590
Jakob Stoklund Olesenebf27502012-02-10 01:23:55 +0000591 SlotIndex Stop = LI.endIndex();
592 if (Stop.isBlock())
593 return NULL;
Lang Hames233a60e2009-11-03 23:52:08 +0000594
Jakob Stoklund Olesenebf27502012-02-10 01:23:55 +0000595 // getMBBFromIndex doesn't need to search the MBB table when both indexes
596 // belong to proper instructions.
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000597 MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start);
598 MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop);
Jakob Stoklund Olesenebf27502012-02-10 01:23:55 +0000599 return MBB1 == MBB2 ? MBB1 : NULL;
Evan Cheng81a03822007-11-17 00:40:40 +0000600}
601
Jakob Stoklund Olesen0ab71032012-08-03 20:10:24 +0000602bool
603LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const {
604 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end();
605 I != E; ++I) {
606 const VNInfo *PHI = *I;
607 if (PHI->isUnused() || !PHI->isPHIDef())
608 continue;
609 const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def);
610 // Conservatively return true instead of scanning huge predecessor lists.
611 if (PHIMBB->pred_size() > 100)
612 return true;
613 for (MachineBasicBlock::const_pred_iterator
614 PI = PHIMBB->pred_begin(), PE = PHIMBB->pred_end(); PI != PE; ++PI)
615 if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(*PI)))
616 return true;
617 }
618 return false;
619}
620
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +0000621float
Benjamin Kramer4eed7562013-06-17 19:00:36 +0000622LiveIntervals::getSpillWeight(bool isDef, bool isUse, BlockFrequency freq) {
623 const float Scale = 1.0f / BlockFrequency::getEntryFrequency();
624 return (isDef + isUse) * (freq.getFrequency() * Scale);
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +0000625}
626
Matthias Braun87a86052013-10-10 21:28:47 +0000627LiveRange::Segment
Matthias Braun331de112013-10-10 21:28:43 +0000628LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr* startInst) {
Mark Laceye742d682013-08-14 23:50:16 +0000629 LiveInterval& Interval = createEmptyInterval(reg);
Owen Andersonc4dc1322008-06-05 17:15:43 +0000630 VNInfo* VN = Interval.getNextValue(
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000631 SlotIndex(getInstructionIndex(startInst).getRegSlot()),
Jakob Stoklund Olesen3b1088a2012-02-04 05:20:49 +0000632 getVNInfoAllocator());
Matthias Braun87a86052013-10-10 21:28:47 +0000633 LiveRange::Segment S(
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000634 SlotIndex(getInstructionIndex(startInst).getRegSlot()),
Lang Hames74ab5ee2009-12-22 00:11:50 +0000635 getMBBEndIdx(startInst->getParent()), VN);
Matthias Braun331de112013-10-10 21:28:43 +0000636 Interval.addSegment(S);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000637
Matthias Braun331de112013-10-10 21:28:43 +0000638 return S;
Owen Andersonc4dc1322008-06-05 17:15:43 +0000639}
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +0000640
641
642//===----------------------------------------------------------------------===//
643// Register mask functions
644//===----------------------------------------------------------------------===//
645
646bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI,
647 BitVector &UsableRegs) {
648 if (LI.empty())
649 return false;
Jakob Stoklund Olesen9f10ac62012-02-10 01:31:31 +0000650 LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end();
651
652 // Use a smaller arrays for local live ranges.
653 ArrayRef<SlotIndex> Slots;
654 ArrayRef<const uint32_t*> Bits;
655 if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) {
656 Slots = getRegMaskSlotsInBlock(MBB->getNumber());
657 Bits = getRegMaskBitsInBlock(MBB->getNumber());
658 } else {
659 Slots = getRegMaskSlots();
660 Bits = getRegMaskBits();
661 }
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +0000662
663 // We are going to enumerate all the register mask slots contained in LI.
664 // Start with a binary search of RegMaskSlots to find a starting point.
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +0000665 ArrayRef<SlotIndex>::iterator SlotI =
666 std::lower_bound(Slots.begin(), Slots.end(), LiveI->start);
667 ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
668
669 // No slots in range, LI begins after the last call.
670 if (SlotI == SlotE)
671 return false;
672
673 bool Found = false;
674 for (;;) {
675 assert(*SlotI >= LiveI->start);
676 // Loop over all slots overlapping this segment.
677 while (*SlotI < LiveI->end) {
678 // *SlotI overlaps LI. Collect mask bits.
679 if (!Found) {
680 // This is the first overlap. Initialize UsableRegs to all ones.
681 UsableRegs.clear();
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +0000682 UsableRegs.resize(TRI->getNumRegs(), true);
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +0000683 Found = true;
684 }
685 // Remove usable registers clobbered by this mask.
Jakob Stoklund Olesen9f10ac62012-02-10 01:31:31 +0000686 UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]);
Jakob Stoklund Olesen3fd3a842012-02-08 17:33:45 +0000687 if (++SlotI == SlotE)
688 return Found;
689 }
690 // *SlotI is beyond the current LI segment.
691 LiveI = LI.advanceTo(LiveI, *SlotI);
692 if (LiveI == LiveE)
693 return Found;
694 // Advance SlotI until it overlaps.
695 while (*SlotI < LiveI->start)
696 if (++SlotI == SlotE)
697 return Found;
698 }
699}
Lang Hames3dc7c512012-02-17 18:44:18 +0000700
701//===----------------------------------------------------------------------===//
702// IntervalUpdate class.
703//===----------------------------------------------------------------------===//
704
Lang Hamesfd6d3212012-02-21 00:00:36 +0000705// HMEditor is a toolkit used by handleMove to trim or extend live intervals.
Lang Hames3dc7c512012-02-17 18:44:18 +0000706class LiveIntervals::HMEditor {
707private:
Lang Hamesecb50622012-02-17 23:43:40 +0000708 LiveIntervals& LIS;
709 const MachineRegisterInfo& MRI;
710 const TargetRegisterInfo& TRI;
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000711 SlotIndex OldIdx;
Lang Hamesecb50622012-02-17 23:43:40 +0000712 SlotIndex NewIdx;
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000713 SmallPtrSet<LiveInterval*, 8> Updated;
Andrew Trick27c28ce2012-10-16 00:22:51 +0000714 bool UpdateFlags;
Lang Hames6aceab12012-02-19 07:13:05 +0000715
Lang Hames3dc7c512012-02-17 18:44:18 +0000716public:
Lang Hamesecb50622012-02-17 23:43:40 +0000717 HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI,
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000718 const TargetRegisterInfo& TRI,
Andrew Trick27c28ce2012-10-16 00:22:51 +0000719 SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags)
720 : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx),
721 UpdateFlags(UpdateFlags) {}
722
723 // FIXME: UpdateFlags is a workaround that creates live intervals for all
724 // physregs, even those that aren't needed for regalloc, in order to update
725 // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill
726 // flags, and postRA passes will use a live register utility instead.
727 LiveInterval *getRegUnitLI(unsigned Unit) {
728 if (UpdateFlags)
729 return &LIS.getRegUnit(Unit);
730 return LIS.getCachedRegUnit(Unit);
731 }
Lang Hames3dc7c512012-02-17 18:44:18 +0000732
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000733 /// Update all live ranges touched by MI, assuming a move from OldIdx to
734 /// NewIdx.
735 void updateAllRanges(MachineInstr *MI) {
736 DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI);
737 bool hasRegMask = false;
738 for (MIOperands MO(MI); MO.isValid(); ++MO) {
739 if (MO->isRegMask())
740 hasRegMask = true;
741 if (!MO->isReg())
Lang Hames4586d252012-02-21 22:29:38 +0000742 continue;
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000743 // Aggressively clear all kill flags.
744 // They are reinserted by VirtRegRewriter.
745 if (MO->isUse())
746 MO->setIsKill(false);
747
748 unsigned Reg = MO->getReg();
749 if (!Reg)
750 continue;
751 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
752 updateRange(LIS.getInterval(Reg));
753 continue;
754 }
755
756 // For physregs, only update the regunits that actually have a
757 // precomputed live range.
758 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units)
Andrew Trick27c28ce2012-10-16 00:22:51 +0000759 if (LiveInterval *LI = getRegUnitLI(*Units))
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000760 updateRange(*LI);
Lang Hames4586d252012-02-21 22:29:38 +0000761 }
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000762 if (hasRegMask)
763 updateRegMaskSlots();
Lang Hames6aceab12012-02-19 07:13:05 +0000764 }
765
Lang Hames55fed622012-02-19 03:00:30 +0000766private:
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000767 /// Update a single live range, assuming an instruction has been moved from
768 /// OldIdx to NewIdx.
769 void updateRange(LiveInterval &LI) {
770 if (!Updated.insert(&LI))
771 return;
772 DEBUG({
773 dbgs() << " ";
774 if (TargetRegisterInfo::isVirtualRegister(LI.reg))
775 dbgs() << PrintReg(LI.reg);
Jakob Stoklund Olesenbf833f02012-06-19 23:50:18 +0000776 else
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000777 dbgs() << PrintRegUnit(LI.reg, &TRI);
778 dbgs() << ":\t" << LI << '\n';
779 });
780 if (SlotIndex::isEarlierInstr(OldIdx, NewIdx))
781 handleMoveDown(LI);
782 else
783 handleMoveUp(LI);
784 DEBUG(dbgs() << " -->\t" << LI << '\n');
785 LI.verify();
Lang Hames3dc7c512012-02-17 18:44:18 +0000786 }
787
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000788 /// Update LI to reflect an instruction has been moved downwards from OldIdx
789 /// to NewIdx.
790 ///
791 /// 1. Live def at OldIdx:
792 /// Move def to NewIdx, assert endpoint after NewIdx.
793 ///
794 /// 2. Live def at OldIdx, killed at NewIdx:
795 /// Change to dead def at NewIdx.
796 /// (Happens when bundling def+kill together).
797 ///
798 /// 3. Dead def at OldIdx:
799 /// Move def to NewIdx, possibly across another live value.
800 ///
801 /// 4. Def at OldIdx AND at NewIdx:
Matthias Braun331de112013-10-10 21:28:43 +0000802 /// Remove segment [OldIdx;NewIdx) and value defined at OldIdx.
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000803 /// (Happens when bundling multiple defs together).
804 ///
805 /// 5. Value read at OldIdx, killed before NewIdx:
806 /// Extend kill to NewIdx.
807 ///
808 void handleMoveDown(LiveInterval &LI) {
809 // First look for a kill at OldIdx.
810 LiveInterval::iterator I = LI.find(OldIdx.getBaseIndex());
811 LiveInterval::iterator E = LI.end();
812 // Is LI even live at OldIdx?
813 if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
814 return;
Lang Hames6aceab12012-02-19 07:13:05 +0000815
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000816 // Handle a live-in value.
817 if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
818 bool isKill = SlotIndex::isSameInstr(OldIdx, I->end);
819 // If the live-in value already extends to NewIdx, there is nothing to do.
820 if (!SlotIndex::isEarlierInstr(I->end, NewIdx))
821 return;
822 // Aggressively remove all kill flags from the old kill point.
823 // Kill flags shouldn't be used while live intervals exist, they will be
824 // reinserted by VirtRegRewriter.
825 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end))
826 for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO)
827 if (MO->isReg() && MO->isUse())
828 MO->setIsKill(false);
829 // Adjust I->end to reach NewIdx. This may temporarily make LI invalid by
830 // overlapping ranges. Case 5 above.
831 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
832 // If this was a kill, there may also be a def. Otherwise we're done.
833 if (!isKill)
834 return;
835 ++I;
Lang Hames6aceab12012-02-19 07:13:05 +0000836 }
837
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000838 // Check for a def at OldIdx.
839 if (I == E || !SlotIndex::isSameInstr(OldIdx, I->start))
840 return;
841 // We have a def at OldIdx.
842 VNInfo *DefVNI = I->valno;
843 assert(DefVNI->def == I->start && "Inconsistent def");
844 DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
845 // If the defined value extends beyond NewIdx, just move the def down.
846 // This is case 1 above.
847 if (SlotIndex::isEarlierInstr(NewIdx, I->end)) {
848 I->start = DefVNI->def;
849 return;
850 }
851 // The remaining possibilities are now:
852 // 2. Live def at OldIdx, killed at NewIdx: isSameInstr(I->end, NewIdx).
853 // 3. Dead def at OldIdx: I->end = OldIdx.getDeadSlot().
854 // In either case, it is possible that there is an existing def at NewIdx.
855 assert((I->end == OldIdx.getDeadSlot() ||
856 SlotIndex::isSameInstr(I->end, NewIdx)) &&
857 "Cannot move def below kill");
858 LiveInterval::iterator NewI = LI.advanceTo(I, NewIdx.getRegSlot());
859 if (NewI != E && SlotIndex::isSameInstr(NewI->start, NewIdx)) {
860 // There is an existing def at NewIdx, case 4 above. The def at OldIdx is
861 // coalesced into that value.
862 assert(NewI->valno != DefVNI && "Multiple defs of value?");
863 LI.removeValNo(DefVNI);
864 return;
865 }
866 // There was no existing def at NewIdx. Turn *I into a dead def at NewIdx.
867 // If the def at OldIdx was dead, we allow it to be moved across other LI
868 // values. The new range should be placed immediately before NewI, move any
869 // intermediate ranges up.
870 assert(NewI != I && "Inconsistent iterators");
871 std::copy(llvm::next(I), NewI, I);
Matthias Braun331de112013-10-10 21:28:43 +0000872 *llvm::prior(NewI)
Matthias Braun87a86052013-10-10 21:28:47 +0000873 = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000874 }
875
876 /// Update LI to reflect an instruction has been moved upwards from OldIdx
877 /// to NewIdx.
878 ///
879 /// 1. Live def at OldIdx:
880 /// Hoist def to NewIdx.
881 ///
882 /// 2. Dead def at OldIdx:
883 /// Hoist def+end to NewIdx, possibly move across other values.
884 ///
885 /// 3. Dead def at OldIdx AND existing def at NewIdx:
886 /// Remove value defined at OldIdx, coalescing it with existing value.
887 ///
888 /// 4. Live def at OldIdx AND existing def at NewIdx:
889 /// Remove value defined at NewIdx, hoist OldIdx def to NewIdx.
890 /// (Happens when bundling multiple defs together).
891 ///
892 /// 5. Value killed at OldIdx:
893 /// Hoist kill to NewIdx, then scan for last kill between NewIdx and
894 /// OldIdx.
895 ///
896 void handleMoveUp(LiveInterval &LI) {
897 // First look for a kill at OldIdx.
898 LiveInterval::iterator I = LI.find(OldIdx.getBaseIndex());
899 LiveInterval::iterator E = LI.end();
900 // Is LI even live at OldIdx?
901 if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
902 return;
903
904 // Handle a live-in value.
905 if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
906 // If the live-in value isn't killed here, there is nothing to do.
907 if (!SlotIndex::isSameInstr(OldIdx, I->end))
908 return;
909 // Adjust I->end to end at NewIdx. If we are hoisting a kill above
910 // another use, we need to search for that use. Case 5 above.
911 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
912 ++I;
913 // If OldIdx also defines a value, there couldn't have been another use.
914 if (I == E || !SlotIndex::isSameInstr(I->start, OldIdx)) {
915 // No def, search for the new kill.
916 // This can never be an early clobber kill since there is no def.
917 llvm::prior(I)->end = findLastUseBefore(LI.reg).getRegSlot();
918 return;
Lang Hames6aceab12012-02-19 07:13:05 +0000919 }
920 }
921
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000922 // Now deal with the def at OldIdx.
923 assert(I != E && SlotIndex::isSameInstr(I->start, OldIdx) && "No def?");
924 VNInfo *DefVNI = I->valno;
925 assert(DefVNI->def == I->start && "Inconsistent def");
926 DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
927
928 // Check for an existing def at NewIdx.
929 LiveInterval::iterator NewI = LI.find(NewIdx.getRegSlot());
930 if (SlotIndex::isSameInstr(NewI->start, NewIdx)) {
931 assert(NewI->valno != DefVNI && "Same value defined more than once?");
932 // There is an existing def at NewIdx.
933 if (I->end.isDead()) {
934 // Case 3: Remove the dead def at OldIdx.
935 LI.removeValNo(DefVNI);
936 return;
937 }
938 // Case 4: Replace def at NewIdx with live def at OldIdx.
939 I->start = DefVNI->def;
940 LI.removeValNo(NewI->valno);
941 return;
Lang Hames6aceab12012-02-19 07:13:05 +0000942 }
943
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000944 // There is no existing def at NewIdx. Hoist DefVNI.
945 if (!I->end.isDead()) {
946 // Leave the end point of a live def.
947 I->start = DefVNI->def;
948 return;
949 }
950
951 // DefVNI is a dead def. It may have been moved across other values in LI,
952 // so move I up to NewI. Slide [NewI;I) down one position.
953 std::copy_backward(NewI, I, llvm::next(I));
Matthias Braun87a86052013-10-10 21:28:47 +0000954 *NewI = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
Lang Hames6aceab12012-02-19 07:13:05 +0000955 }
956
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000957 void updateRegMaskSlots() {
Lang Hamesecb50622012-02-17 23:43:40 +0000958 SmallVectorImpl<SlotIndex>::iterator RI =
959 std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(),
960 OldIdx);
Jakob Stoklund Olesen722c9a72012-11-09 19:18:49 +0000961 assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() &&
962 "No RegMask at OldIdx.");
963 *RI = NewIdx.getRegSlot();
964 assert((RI == LIS.RegMaskSlots.begin() ||
965 SlotIndex::isEarlierInstr(*llvm::prior(RI), *RI)) &&
966 "Cannot move regmask instruction above another call");
967 assert((llvm::next(RI) == LIS.RegMaskSlots.end() ||
968 SlotIndex::isEarlierInstr(*RI, *llvm::next(RI))) &&
969 "Cannot move regmask instruction below another call");
Lang Hamesfbc8dd32012-02-17 21:29:41 +0000970 }
Lang Hames55fed622012-02-19 03:00:30 +0000971
972 // Return the last use of reg between NewIdx and OldIdx.
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +0000973 SlotIndex findLastUseBefore(unsigned Reg) {
Lang Hames6d742cc2012-09-12 06:56:16 +0000974
975 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen778ef972013-03-08 18:08:57 +0000976 SlotIndex LastUse = NewIdx;
Lang Hames6d742cc2012-09-12 06:56:16 +0000977 for (MachineRegisterInfo::use_nodbg_iterator
978 UI = MRI.use_nodbg_begin(Reg),
979 UE = MRI.use_nodbg_end();
980 UI != UE; UI.skipInstruction()) {
981 const MachineInstr* MI = &*UI;
982 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI);
983 if (InstSlot > LastUse && InstSlot < OldIdx)
984 LastUse = InstSlot;
985 }
Jakob Stoklund Olesen778ef972013-03-08 18:08:57 +0000986 return LastUse;
Lang Hames55fed622012-02-19 03:00:30 +0000987 }
Jakob Stoklund Olesen778ef972013-03-08 18:08:57 +0000988
989 // This is a regunit interval, so scanning the use list could be very
990 // expensive. Scan upwards from OldIdx instead.
991 assert(NewIdx < OldIdx && "Expected upwards move");
992 SlotIndexes *Indexes = LIS.getSlotIndexes();
993 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx);
994
995 // OldIdx may not correspond to an instruction any longer, so set MII to
996 // point to the next instruction after OldIdx, or MBB->end().
997 MachineBasicBlock::iterator MII = MBB->end();
998 if (MachineInstr *MI = Indexes->getInstructionFromIndex(
999 Indexes->getNextNonNullIndex(OldIdx)))
1000 if (MI->getParent() == MBB)
1001 MII = MI;
1002
1003 MachineBasicBlock::iterator Begin = MBB->begin();
1004 while (MII != Begin) {
1005 if ((--MII)->isDebugValue())
1006 continue;
1007 SlotIndex Idx = Indexes->getInstructionIndex(MII);
1008
1009 // Stop searching when NewIdx is reached.
1010 if (!SlotIndex::isEarlierInstr(NewIdx, Idx))
1011 return NewIdx;
1012
1013 // Check if MII uses Reg.
1014 for (MIBundleOperands MO(MII); MO.isValid(); ++MO)
1015 if (MO->isReg() &&
1016 TargetRegisterInfo::isPhysicalRegister(MO->getReg()) &&
1017 TRI.hasRegUnit(MO->getReg(), Reg))
1018 return Idx;
1019 }
1020 // Didn't reach NewIdx. It must be the first instruction in the block.
1021 return NewIdx;
Lang Hames55fed622012-02-19 03:00:30 +00001022 }
Lang Hames3dc7c512012-02-17 18:44:18 +00001023};
1024
Andrew Trick27c28ce2012-10-16 00:22:51 +00001025void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) {
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +00001026 assert(!MI->isBundled() && "Can't handle bundled instructions yet.");
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +00001027 SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
1028 Indexes->removeMachineInstrFromMaps(MI);
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +00001029 SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI);
Lang Hamesecb50622012-02-17 23:43:40 +00001030 assert(getMBBStartIdx(MI->getParent()) <= OldIndex &&
1031 OldIndex < getMBBEndIdx(MI->getParent()) &&
Lang Hames3dc7c512012-02-17 18:44:18 +00001032 "Cannot handle moves across basic block boundaries.");
Lang Hames3dc7c512012-02-17 18:44:18 +00001033
Andrew Trick27c28ce2012-10-16 00:22:51 +00001034 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +00001035 HME.updateAllRanges(MI);
Lang Hames4586d252012-02-21 22:29:38 +00001036}
1037
Jakob Stoklund Olesenfa8becb2012-06-19 22:50:53 +00001038void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI,
Andrew Trick27c28ce2012-10-16 00:22:51 +00001039 MachineInstr* BundleStart,
1040 bool UpdateFlags) {
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +00001041 SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
Jakob Stoklund Olesen15f1d8c2012-06-04 22:39:14 +00001042 SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart);
Andrew Trick27c28ce2012-10-16 00:22:51 +00001043 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
Jakob Stoklund Olesenad5e9692012-10-12 21:31:57 +00001044 HME.updateAllRanges(MI);
Lang Hames3dc7c512012-02-17 18:44:18 +00001045}
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001046
1047void
1048LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB,
Cameron Zwarich680c98f2013-02-17 11:09:00 +00001049 MachineBasicBlock::iterator Begin,
1050 MachineBasicBlock::iterator End,
Cameron Zwarich7324d4e2013-02-17 03:48:23 +00001051 ArrayRef<unsigned> OrigRegs) {
Cameron Zwarichc5b61352013-02-20 22:10:00 +00001052 // Find anchor points, which are at the beginning/end of blocks or at
1053 // instructions that already have indexes.
1054 while (Begin != MBB->begin() && !Indexes->hasIndex(Begin))
1055 --Begin;
1056 while (End != MBB->end() && !Indexes->hasIndex(End))
1057 ++End;
1058
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001059 SlotIndex endIdx;
1060 if (End == MBB->end())
1061 endIdx = getMBBEndIdx(MBB).getPrevSlot();
Cameron Zwarich680c98f2013-02-17 11:09:00 +00001062 else
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001063 endIdx = getInstructionIndex(End);
Cameron Zwarich680c98f2013-02-17 11:09:00 +00001064
Cameron Zwarich349cf342013-02-20 06:46:41 +00001065 Indexes->repairIndexesInRange(MBB, Begin, End);
1066
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001067 for (MachineBasicBlock::iterator I = End; I != Begin;) {
1068 --I;
1069 MachineInstr *MI = I;
Cameron Zwarich79f5ab12013-02-23 10:25:25 +00001070 if (MI->isDebugValue())
1071 continue;
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001072 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1073 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1074 if (MOI->isReg() &&
1075 TargetRegisterInfo::isVirtualRegister(MOI->getReg()) &&
1076 !hasInterval(MOI->getReg())) {
Mark Laceye742d682013-08-14 23:50:16 +00001077 createAndComputeVirtRegInterval(MOI->getReg());
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001078 }
1079 }
1080 }
1081
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001082 for (unsigned i = 0, e = OrigRegs.size(); i != e; ++i) {
1083 unsigned Reg = OrigRegs[i];
1084 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1085 continue;
1086
1087 LiveInterval &LI = getInterval(Reg);
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001088 // FIXME: Should we support undefs that gain defs?
1089 if (!LI.hasAtLeastOneValue())
1090 continue;
1091
1092 LiveInterval::iterator LII = LI.find(endIdx);
1093 SlotIndex lastUseIdx;
1094 if (LII != LI.end() && LII->start < endIdx)
1095 lastUseIdx = LII->end;
1096 else
1097 --LII;
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001098
Cameron Zwarich680c98f2013-02-17 11:09:00 +00001099 for (MachineBasicBlock::iterator I = End; I != Begin;) {
1100 --I;
1101 MachineInstr *MI = I;
Cameron Zwarich79f5ab12013-02-23 10:25:25 +00001102 if (MI->isDebugValue())
1103 continue;
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001104
Cameron Zwarich79f5ab12013-02-23 10:25:25 +00001105 SlotIndex instrIdx = getInstructionIndex(MI);
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001106 bool isStartValid = getInstructionFromIndex(LII->start);
1107 bool isEndValid = getInstructionFromIndex(LII->end);
1108
1109 // FIXME: This doesn't currently handle early-clobber or multiple removed
1110 // defs inside of the region to repair.
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001111 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
1112 OE = MI->operands_end(); OI != OE; ++OI) {
1113 const MachineOperand &MO = *OI;
1114 if (!MO.isReg() || MO.getReg() != Reg)
1115 continue;
1116
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001117 if (MO.isDef()) {
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001118 if (!isStartValid) {
1119 if (LII->end.isDead()) {
1120 SlotIndex prevStart;
1121 if (LII != LI.begin())
1122 prevStart = llvm::prior(LII)->start;
1123
Matthias Braun331de112013-10-10 21:28:43 +00001124 // FIXME: This could be more efficient if there was a
1125 // removeSegment method that returned an iterator.
1126 LI.removeSegment(*LII, true);
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001127 if (prevStart.isValid())
1128 LII = LI.find(prevStart);
1129 else
1130 LII = LI.begin();
1131 } else {
1132 LII->start = instrIdx.getRegSlot();
1133 LII->valno->def = instrIdx.getRegSlot();
1134 if (MO.getSubReg() && !MO.isUndef())
1135 lastUseIdx = instrIdx.getRegSlot();
1136 else
1137 lastUseIdx = SlotIndex();
1138 continue;
1139 }
1140 }
1141
1142 if (!lastUseIdx.isValid()) {
1143 VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(),
1144 VNInfoAllocator);
Matthias Braun87a86052013-10-10 21:28:47 +00001145 LiveRange::Segment S(instrIdx.getRegSlot(),
1146 instrIdx.getDeadSlot(), VNI);
Matthias Braun331de112013-10-10 21:28:43 +00001147 LII = LI.addSegment(S);
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001148 } else if (LII->start != instrIdx.getRegSlot()) {
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001149 VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(),
1150 VNInfoAllocator);
Matthias Braun87a86052013-10-10 21:28:47 +00001151 LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI);
Matthias Braun331de112013-10-10 21:28:43 +00001152 LII = LI.addSegment(S);
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001153 }
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001154
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001155 if (MO.getSubReg() && !MO.isUndef())
1156 lastUseIdx = instrIdx.getRegSlot();
1157 else
1158 lastUseIdx = SlotIndex();
1159 } else if (MO.isUse()) {
1160 // FIXME: This should probably be handled outside of this branch,
1161 // either as part of the def case (for defs inside of the region) or
1162 // after the loop over the region.
1163 if (!isEndValid && !LII->end.isBlock())
Cameron Zwarich9030fc22013-02-20 06:46:48 +00001164 LII->end = instrIdx.getRegSlot();
Cameron Zwarich0e827eb2013-02-20 22:09:57 +00001165 if (!lastUseIdx.isValid())
1166 lastUseIdx = instrIdx.getRegSlot();
Cameron Zwarichf0b25352013-02-17 00:10:44 +00001167 }
1168 }
1169 }
1170 }
1171}