blob: bd8137730b299cdc5a168767972384f6c4636583 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000022#include "llvm/Analysis/LoopInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/MRegisterInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Chris Lattnercd3245a2006-12-19 22:41:21 +000039STATISTIC(numIntervals, "Number of original intervals");
40STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Chris Lattnercd3245a2006-12-19 22:41:21 +000041STATISTIC(numFolded , "Number of loads/stores folded into instructions");
42
Devang Patel19974732007-05-03 01:11:54 +000043char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000044namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000045 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000046}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047
Chris Lattnerf7da2c72006-08-24 22:43:55 +000048void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000049 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000050 AU.addRequired<LiveVariables>();
51 AU.addPreservedID(PHIEliminationID);
52 AU.addRequiredID(PHIEliminationID);
53 AU.addRequiredID(TwoAddressInstructionPassID);
54 AU.addRequired<LoopInfo>();
55 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000056}
57
Chris Lattnerf7da2c72006-08-24 22:43:55 +000058void LiveIntervals::releaseMemory() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 mi2iMap_.clear();
60 i2miMap_.clear();
61 r2iMap_.clear();
Evan Cheng549f27d32007-08-13 23:45:17 +000062 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
63 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000064}
65
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000066/// runOnMachineFunction - Register allocate the whole function
67///
68bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mf_ = &fn;
70 tm_ = &fn.getTarget();
71 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000072 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000074 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000075
Chris Lattner428b92e2006-09-15 03:57:23 +000076 // Number MachineInstrs and MachineBasicBlocks.
77 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000078 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000079
80 unsigned MIIndex = 0;
81 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
82 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000083 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000084
Chris Lattner428b92e2006-09-15 03:57:23 +000085 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
86 I != E; ++I) {
87 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000088 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000089 i2miMap_.push_back(I);
90 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000091 }
Evan Cheng549f27d32007-08-13 23:45:17 +000092
93 // Set the MBB2IdxMap entry for this MBB.
94 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Chris Lattner428b92e2006-09-15 03:57:23 +000095 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +000096
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000097 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000098
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000099 numIntervals += getNumIntervals();
100
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000101 DOUT << "********** INTERVALS **********\n";
102 for (iterator I = begin(), E = end(); I != E; ++I) {
103 I->second.print(DOUT, mri_);
104 DOUT << "\n";
105 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000107 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000108 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000109 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000110}
111
Chris Lattner70ca3582004-09-30 15:59:17 +0000112/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000113void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000114 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000115 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000116 I->second.print(DOUT, mri_);
117 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000118 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000119
120 O << "********** MACHINEINSTRS **********\n";
121 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
122 mbbi != mbbe; ++mbbi) {
123 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
124 for (MachineBasicBlock::iterator mii = mbbi->begin(),
125 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000126 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000127 }
128 }
129}
130
David Greene25133302007-06-08 17:18:56 +0000131// Not called?
Bill Wendling01352aa2006-11-16 02:41:50 +0000132/// CreateNewLiveInterval - Create a new live interval with the given live
133/// ranges. The new live interval will have an infinite spill weight.
134LiveInterval&
135LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI,
136 const std::vector<LiveRange> &LRs) {
137 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg);
138
139 // Create a new virtual register for the spill interval.
140 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC);
141
142 // Replace the old virtual registers in the machine operands with the shiny
143 // new one.
144 for (std::vector<LiveRange>::const_iterator
145 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
146 unsigned Index = getBaseIndex(I->start);
147 unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM;
148
149 for (; Index != End; Index += InstrSlots::NUM) {
150 // Skip deleted instructions
151 while (Index != End && !getInstructionFromIndex(Index))
152 Index += InstrSlots::NUM;
153
154 if (Index == End) break;
155
156 MachineInstr *MI = getInstructionFromIndex(Index);
157
Bill Wendlingbeeb77f2006-11-16 07:35:18 +0000158 for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) {
Bill Wendling01352aa2006-11-16 02:41:50 +0000159 MachineOperand &MOp = MI->getOperand(J);
David Greene25133302007-06-08 17:18:56 +0000160 if (MOp.isRegister() && MOp.getReg() == LI->reg)
Bill Wendling01352aa2006-11-16 02:41:50 +0000161 MOp.setReg(NewVReg);
162 }
163 }
164 }
165
166 LiveInterval &NewLI = getOrCreateInterval(NewVReg);
167
168 // The spill weight is now infinity as it cannot be spilled again
169 NewLI.weight = float(HUGE_VAL);
170
171 for (std::vector<LiveRange>::const_iterator
172 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000173 DOUT << " Adding live range " << *I << " to new interval\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000174 NewLI.addRange(*I);
175 }
176
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000177 DOUT << "Created new live interval " << NewLI << "\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000178 return NewLI;
179}
180
Evan Chengbf105c82006-11-03 03:04:46 +0000181/// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to
182/// two addr elimination.
183static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg,
184 const TargetInstrInfo *TII) {
185 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
186 MachineOperand &MO1 = MI->getOperand(i);
187 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
188 for (unsigned j = i+1; j < e; ++j) {
189 MachineOperand &MO2 = MI->getOperand(j);
190 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Evan Cheng51cdcd12006-12-07 01:21:59 +0000191 MI->getInstrDescriptor()->
192 getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Chengbf105c82006-11-03 03:04:46 +0000193 return true;
194 }
195 }
196 }
197 return false;
198}
199
Evan Cheng549f27d32007-08-13 23:45:17 +0000200/// isReMaterializable - Returns true if the definition MI of the specified
201/// val# of the specified interval is re-materializable.
202bool LiveIntervals::isReMaterializable(const LiveInterval &li, unsigned ValNum,
203 MachineInstr *MI) {
204 if (tii_->isTriviallyReMaterializable(MI))
205 return true;
206
207 int FrameIdx = 0;
208 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
209 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
210 return false;
211
212 // This is a load from fixed stack slot. It can be rematerialized unless it's
213 // re-defined by a two-address instruction.
214 for (unsigned i = 0, e = li.getNumValNums(); i != e; ++i) {
215 if (i == ValNum)
216 continue;
217 unsigned DefIdx = li.getDefForValNum(i);
218 if (DefIdx == ~1U)
219 continue; // Dead val#.
220 MachineInstr *DefMI = (DefIdx == ~0u)
221 ? NULL : getInstructionFromIndex(DefIdx);
222 if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_))
223 return false;
224 }
225 return true;
226}
227
228bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
229 unsigned index, unsigned i,
230 int slot, unsigned reg) {
231 MachineInstr *fmi = mri_->foldMemoryOperand(MI, i, slot);
232 if (fmi) {
233 // Attempt to fold the memory reference into the instruction. If
234 // we can do this, we don't need to insert spill code.
235 if (lv_)
236 lv_->instructionChanged(MI, fmi);
237 MachineBasicBlock &MBB = *MI->getParent();
238 vrm.virtFolded(reg, MI, i, fmi);
239 mi2iMap_.erase(MI);
240 i2miMap_[index/InstrSlots::NUM] = fmi;
241 mi2iMap_[fmi] = index;
242 MI = MBB.insert(MBB.erase(MI), fmi);
243 ++numFolded;
244 return true;
245 }
246 return false;
247}
248
249std::vector<LiveInterval*> LiveIntervals::
250addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
251 // since this is called after the analysis is done we don't know if
252 // LiveVariables is available
253 lv_ = getAnalysisToUpdate<LiveVariables>();
254
255 std::vector<LiveInterval*> added;
256
257 assert(li.weight != HUGE_VALF &&
258 "attempt to spill already spilled interval!");
259
260 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
261 li.print(DOUT, mri_);
262 DOUT << '\n';
263
264 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
265
266 unsigned NumValNums = li.getNumValNums();
267 SmallVector<MachineInstr*, 4> ReMatDefs;
268 ReMatDefs.resize(NumValNums, NULL);
269 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
270 ReMatOrigDefs.resize(NumValNums, NULL);
271 SmallVector<int, 4> ReMatIds;
272 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
273 BitVector ReMatDelete(NumValNums);
274 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
275
276 bool NeedStackSlot = false;
277 for (unsigned i = 0; i != NumValNums; ++i) {
278 unsigned DefIdx = li.getDefForValNum(i);
279 if (DefIdx == ~1U)
280 continue; // Dead val#.
281 // Is the def for the val# rematerializable?
282 MachineInstr *DefMI = (DefIdx == ~0u)
283 ? NULL : getInstructionFromIndex(DefIdx);
284 if (DefMI && isReMaterializable(li, i, DefMI)) {
285 // Remember how to remat the def of this val#.
286 ReMatOrigDefs[i] = DefMI;
287 // Original def may be modified so we have to make a copy here. vrm must
288 // delete these!
289 ReMatDefs[i] = DefMI = DefMI->clone();
290 vrm.setVirtIsReMaterialized(reg, DefMI);
291
292 bool CanDelete = true;
293 const SmallVector<unsigned, 4> &kills = li.getKillsForValNum(i);
294 for (unsigned j = 0, ee = kills.size(); j != ee; ++j) {
295 unsigned KillIdx = kills[j];
296 MachineInstr *KillMI = (KillIdx & 1)
297 ? NULL : getInstructionFromIndex(KillIdx);
298 // Kill is a phi node, not all of its uses can be rematerialized.
299 // It must not be deleted.
300 if (!KillMI) {
301 CanDelete = false;
302 // Need a stack slot if there is any live range where uses cannot be
303 // rematerialized.
304 NeedStackSlot = true;
305 break;
306 }
307 }
308
309 if (CanDelete)
310 ReMatDelete.set(i);
311 } else {
312 // Need a stack slot if there is any live range where uses cannot be
313 // rematerialized.
314 NeedStackSlot = true;
315 }
316 }
317
318 // One stack slot per live interval.
319 if (NeedStackSlot)
320 slot = vrm.assignVirt2StackSlot(reg);
321
322 for (LiveInterval::Ranges::const_iterator
323 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
324 MachineInstr *DefMI = ReMatDefs[I->ValId];
325 MachineInstr *OrigDefMI = ReMatOrigDefs[I->ValId];
326 bool DefIsReMat = DefMI != NULL;
327 bool CanDelete = ReMatDelete[I->ValId];
328 int LdSlot = 0;
329 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
330 unsigned index = getBaseIndex(I->start);
331 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
332 for (; index != end; index += InstrSlots::NUM) {
333 // skip deleted instructions
334 while (index != end && !getInstructionFromIndex(index))
335 index += InstrSlots::NUM;
336 if (index == end) break;
337
338 MachineInstr *MI = getInstructionFromIndex(index);
339
340 RestartInstruction:
341 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
342 MachineOperand& mop = MI->getOperand(i);
343 if (mop.isRegister() && mop.getReg() == li.reg) {
344 if (DefIsReMat) {
345 // If this is the rematerializable definition MI itself and
346 // all of its uses are rematerialized, simply delete it.
347 if (MI == OrigDefMI) {
348 if (CanDelete) {
349 RemoveMachineInstrFromMaps(MI);
350 MI->eraseFromParent();
351 break;
352 } else if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg))
353 // Folding the load/store can completely change the instruction
354 // in unpredictable ways, rescan it from the beginning.
355 goto RestartInstruction;
356 } else if (isLoadSS &&
357 tryFoldMemoryOperand(MI, vrm, index, i, LdSlot, li.reg)){
358 // FIXME: Other rematerializable loads can be folded as well.
359 // Folding the load/store can completely change the
360 // instruction in unpredictable ways, rescan it from
361 // the beginning.
362 goto RestartInstruction;
363 }
364 } else {
365 if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg))
366 // Folding the load/store can completely change the instruction in
367 // unpredictable ways, rescan it from the beginning.
368 goto RestartInstruction;
369 }
370
371 // Create a new virtual register for the spill interval.
372 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc);
373
374 // Scan all of the operands of this instruction rewriting operands
375 // to use NewVReg instead of li.reg as appropriate. We do this for
376 // two reasons:
377 //
378 // 1. If the instr reads the same spilled vreg multiple times, we
379 // want to reuse the NewVReg.
380 // 2. If the instr is a two-addr instruction, we are required to
381 // keep the src/dst regs pinned.
382 //
383 // Keep track of whether we replace a use and/or def so that we can
384 // create the spill interval with the appropriate range.
385 mop.setReg(NewVReg);
386
387 bool HasUse = mop.isUse();
388 bool HasDef = mop.isDef();
389 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
390 if (MI->getOperand(j).isReg() &&
391 MI->getOperand(j).getReg() == li.reg) {
392 MI->getOperand(j).setReg(NewVReg);
393 HasUse |= MI->getOperand(j).isUse();
394 HasDef |= MI->getOperand(j).isDef();
395 }
396 }
397
398 vrm.grow();
399 if (DefIsReMat) {
400 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
401 if (ReMatIds[I->ValId] == VirtRegMap::MAX_STACK_SLOT) {
402 // Each valnum may have its own remat id.
403 ReMatIds[I->ValId] = vrm.assignVirtReMatId(NewVReg);
404 } else {
405 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->ValId]);
406 }
407 if (!CanDelete || (HasUse && HasDef)) {
408 // If this is a two-addr instruction then its use operands are
409 // rematerializable but its def is not. It should be assigned a
410 // stack slot.
411 vrm.assignVirt2StackSlot(NewVReg, slot);
412 }
413 } else {
414 vrm.assignVirt2StackSlot(NewVReg, slot);
415 }
416
417 // create a new register interval for this spill / remat.
418 LiveInterval &nI = getOrCreateInterval(NewVReg);
419 assert(nI.empty());
420
421 // the spill weight is now infinity as it
422 // cannot be spilled again
423 nI.weight = HUGE_VALF;
424
425 if (HasUse) {
426 LiveRange LR(getLoadIndex(index), getUseIndex(index),
427 nI.getNextValue(~0U, 0));
428 DOUT << " +" << LR;
429 nI.addRange(LR);
430 }
431 if (HasDef) {
432 LiveRange LR(getDefIndex(index), getStoreIndex(index),
433 nI.getNextValue(~0U, 0));
434 DOUT << " +" << LR;
435 nI.addRange(LR);
436 }
437
438 added.push_back(&nI);
439
440 // update live variables if it is available
441 if (lv_)
442 lv_->addVirtualRegisterKilled(NewVReg, MI);
443
444 DOUT << "\t\t\t\tadded new interval: ";
445 nI.print(DOUT, mri_);
446 DOUT << '\n';
447 }
448 }
449 }
450 }
451
452 return added;
453}
454
455void LiveIntervals::printRegName(unsigned reg) const {
456 if (MRegisterInfo::isPhysicalRegister(reg))
457 cerr << mri_->getName(reg);
458 else
459 cerr << "%reg" << reg;
460}
461
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000462void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000463 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000464 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000465 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000466 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000467 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000468
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000469 // Virtual registers may be defined multiple times (due to phi
470 // elimination and 2-addr elimination). Much of what we do only has to be
471 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000472 // time we see a vreg.
473 if (interval.empty()) {
474 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000475 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000476 unsigned ValNum;
477 unsigned SrcReg, DstReg;
478 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chenga8d94f12007-08-07 23:49:57 +0000479 ValNum = interval.getNextValue(defIndex, 0);
Chris Lattner91725b72006-08-31 05:54:43 +0000480 else
481 ValNum = interval.getNextValue(defIndex, SrcReg);
482
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000483 assert(ValNum == 0 && "First value in interval is not 0?");
484 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000485
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000486 // Loop over all of the blocks that the vreg is defined in. There are
487 // two cases we have to handle here. The most common case is a vreg
488 // whose lifetime is contained within a basic block. In this case there
489 // will be a single kill, in MBB, which comes after the definition.
490 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
491 // FIXME: what about dead vars?
492 unsigned killIdx;
493 if (vi.Kills[0] != mi)
494 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
495 else
496 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000497
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000498 // If the kill happens after the definition, we have an intra-block
499 // live range.
500 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000501 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000502 "Shouldn't be alive across any blocks!");
503 LiveRange LR(defIndex, killIdx, ValNum);
504 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000505 DOUT << " +" << LR << "\n";
Evan Cheng8df78602007-08-08 03:00:28 +0000506 interval.addKillForValNum(ValNum, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000507 return;
508 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000509 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000510
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511 // The other case we handle is when a virtual register lives to the end
512 // of the defining block, potentially live across some blocks, then is
513 // live into some number of blocks, but gets killed. Start by adding a
514 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000515 LiveRange NewLR(defIndex,
516 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
517 ValNum);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000518 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000519 interval.addRange(NewLR);
520
521 // Iterate over all of the blocks that the variable is completely
522 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
523 // live interval.
524 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
525 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000526 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
527 if (!MBB->empty()) {
528 LiveRange LR(getMBBStartIdx(i),
529 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000530 ValNum);
531 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000532 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000533 }
534 }
535 }
536
537 // Finally, this virtual register is live from the start of any killing
538 // block to the 'use' slot of the killing instruction.
539 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
540 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000541 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000542 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng8df78602007-08-08 03:00:28 +0000543 killIdx, ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 interval.addRange(LR);
Evan Cheng8df78602007-08-08 03:00:28 +0000545 interval.addKillForValNum(ValNum, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000546 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 }
548
549 } else {
550 // If this is the second time we see a virtual register definition, it
551 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000552 // the result of two address elimination, then the vreg is one of the
553 // def-and-use register operand.
554 if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000555 // If this is a two-address definition, then we have already processed
556 // the live range. The only problem is that we didn't realize there
557 // are actually two values in the live interval. Because of this we
558 // need to take the LiveRegion that defines this register and split it
559 // into two values.
560 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000561 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000562
Evan Cheng4f8ff162007-08-11 00:59:19 +0000563 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
564 unsigned OldEnd = OldLR->end;
565
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000566 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000567 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000569
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000570 // Two-address vregs should always only be redefined once. This means
571 // that at this point, there should be exactly one value number in it.
572 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
573
Chris Lattner91725b72006-08-31 05:54:43 +0000574 // The new value number (#1) is defined by the instruction we claimed
575 // defined value #0.
576 unsigned ValNo = interval.getNextValue(0, 0);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000577 interval.copyValNumInfo(ValNo, 0);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000578
Chris Lattner91725b72006-08-31 05:54:43 +0000579 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000580 interval.setDefForValNum(0, RedefIndex);
581 interval.setSrcRegForValNum(0, 0);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000582
583 // Add the new live interval which replaces the range for the input copy.
584 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000585 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000586 interval.addRange(LR);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000587 interval.addKillForValNum(ValNo, RedefIndex);
Evan Cheng4f8ff162007-08-11 00:59:19 +0000588 interval.removeKillForValNum(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000589
590 // If this redefinition is dead, we need to add a dummy unit live
591 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000592 if (lv_->RegisterDefIsDead(mi, interval.reg))
593 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000595 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000596 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597
598 } else {
599 // Otherwise, this must be because of phi elimination. If this is the
600 // first redefinition of the vreg that we have seen, go back and change
601 // the live range in the PHI block to be a different value number.
602 if (interval.containsOneValue()) {
603 assert(vi.Kills.size() == 1 &&
604 "PHI elimination vreg should have one kill, the PHI itself!");
605
606 // Remove the old range that we now know has an incorrect number.
607 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000608 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000609 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000610 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000611 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000612 interval.removeRange(Start, End);
Evan Cheng549f27d32007-08-13 23:45:17 +0000613 interval.addKillForValNum(0, Start-1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000614 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000616 // Replace the interval with one of a NEW value number. Note that this
617 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chenga8d94f12007-08-07 23:49:57 +0000618 LiveRange LR(Start, End, interval.getNextValue(~0, 0));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000619 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000620 interval.addRange(LR);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000621 interval.addKillForValNum(LR.ValId, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000622 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000623 }
624
625 // In the case of PHI elimination, each variable definition is only
626 // live until the end of the block. We've already taken care of the
627 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000628 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000629
630 unsigned ValNum;
631 unsigned SrcReg, DstReg;
632 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chenga8d94f12007-08-07 23:49:57 +0000633 ValNum = interval.getNextValue(defIndex, 0);
Chris Lattner91725b72006-08-31 05:54:43 +0000634 else
635 ValNum = interval.getNextValue(defIndex, SrcReg);
636
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000637 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
638 LiveRange LR(defIndex, killIndex, ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000639 interval.addRange(LR);
Evan Cheng549f27d32007-08-13 23:45:17 +0000640 interval.addKillForValNum(ValNum, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000641 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 }
643 }
644
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000645 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000646}
647
Chris Lattnerf35fef72004-07-23 21:24:19 +0000648void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000649 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000650 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000651 LiveInterval &interval,
652 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000653 // A physical register cannot be live across basic block, so its
654 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000655 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000656
Chris Lattner6b128bd2006-09-03 08:07:11 +0000657 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000658 unsigned start = getDefIndex(baseIndex);
659 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000660
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000661 // If it is not used after definition, it is considered dead at
662 // the instruction defining it. Hence its interval is:
663 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000664 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000665 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000666 end = getDefIndex(start) + 1;
667 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000668 }
669
670 // If it is not dead on definition, it must be killed by a
671 // subsequent instruction. Hence its interval is:
672 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000673 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000674 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000675 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000676 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000677 end = getUseIndex(baseIndex) + 1;
678 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000679 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
680 // Another instruction redefines the register before it is ever read.
681 // Then the register is essentially dead at the instruction that defines
682 // it. Hence its interval is:
683 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000684 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000685 end = getDefIndex(start) + 1;
686 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000687 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000688 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000689
690 // The only case we should have a dead physreg here without a killing or
691 // instruction where we know it's dead is if it is live-in to the function
692 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000693 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000694 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000695
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000696exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000697 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000698
Evan Cheng24a3cc42007-04-25 07:30:23 +0000699 // Already exists? Extend old live interval.
700 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
701 unsigned Id = (OldLR != interval.end())
Evan Chenga8d94f12007-08-07 23:49:57 +0000702 ? OldLR->ValId : interval.getNextValue(start, SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000703 LiveRange LR(start, end, Id);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 interval.addRange(LR);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000705 interval.addKillForValNum(LR.ValId, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000706 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000707}
708
Chris Lattnerf35fef72004-07-23 21:24:19 +0000709void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
710 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000711 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000712 unsigned reg) {
713 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000714 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000715 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000716 unsigned SrcReg, DstReg;
717 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
718 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000719 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000720 // Def of a register also defines its sub-registers.
721 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
722 // Avoid processing some defs more than once.
723 if (!MI->findRegisterDefOperand(*AS))
724 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000725 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000726}
727
Evan Chengb371f452007-02-19 21:49:54 +0000728void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000729 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000730 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000731 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
732
733 // Look for kills, if it reaches a def before it's killed, then it shouldn't
734 // be considered a livein.
735 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000736 unsigned baseIndex = MIIdx;
737 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000738 unsigned end = start;
739 while (mi != MBB->end()) {
740 if (lv_->KillsRegister(mi, interval.reg)) {
741 DOUT << " killed";
742 end = getUseIndex(baseIndex) + 1;
743 goto exit;
744 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
745 // Another instruction redefines the register before it is ever read.
746 // Then the register is essentially dead at the instruction that defines
747 // it. Hence its interval is:
748 // [defSlot(def), defSlot(def)+1)
749 DOUT << " dead";
750 end = getDefIndex(start) + 1;
751 goto exit;
752 }
753
754 baseIndex += InstrSlots::NUM;
755 ++mi;
756 }
757
758exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000759 // Live-in register might not be used at all.
760 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000761 if (isAlias) {
762 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000763 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000764 } else {
765 DOUT << " live through";
766 end = baseIndex;
767 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000768 }
769
Evan Chenga8d94f12007-08-07 23:49:57 +0000770 LiveRange LR(start, end, interval.getNextValue(start, 0));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000771 interval.addRange(LR);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000772 interval.addKillForValNum(LR.ValId, end);
773 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000774}
775
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000776/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000777/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000778/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000779/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000780void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000781 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
782 << "********** Function: "
783 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000784 // Track the index of the current machine instr.
785 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000786 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
787 MBBI != E; ++MBBI) {
788 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000789 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000790
Chris Lattner428b92e2006-09-15 03:57:23 +0000791 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000792
793 if (MBB->livein_begin() != MBB->livein_end()) {
Evan Chengb371f452007-02-19 21:49:54 +0000794 // Create intervals for live-ins to this BB first.
795 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000796 LE = MBB->livein_end(); LI != LE; ++LI) {
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000797 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000798 // Multiple live-ins can alias the same register.
799 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
800 if (!hasInterval(*AS))
Evan Cheng292da942007-06-27 18:47:28 +0000801 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
802 true);
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000803 }
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000804 }
805
Chris Lattner428b92e2006-09-15 03:57:23 +0000806 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000807 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000808
Evan Cheng438f7bc2006-11-10 08:43:01 +0000809 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000810 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
811 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000812 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000813 if (MO.isRegister() && MO.getReg() && MO.isDef())
814 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000815 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000816
817 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000818 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000819 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000820}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000821
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000822LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000823 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000824 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000825 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000826}