blob: 5ed24a3be232a7f1544c13aa43b3b770c1a45de2 [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
Evan Chengbc165e42007-08-16 07:24:22 +000039namespace {
40 // Hidden options for help debugging.
41 cl::opt<bool> DisableReMat("disable-rematerialization",
42 cl::init(false), cl::Hidden);
43}
44
Chris Lattnercd3245a2006-12-19 22:41:21 +000045STATISTIC(numIntervals, "Number of original intervals");
46STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(numFolded , "Number of loads/stores folded into instructions");
48
Devang Patel19974732007-05-03 01:11:54 +000049char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000050namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000051 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000052}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000053
Chris Lattnerf7da2c72006-08-24 22:43:55 +000054void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000055 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000056 AU.addRequired<LiveVariables>();
57 AU.addPreservedID(PHIEliminationID);
58 AU.addRequiredID(PHIEliminationID);
59 AU.addRequiredID(TwoAddressInstructionPassID);
60 AU.addRequired<LoopInfo>();
61 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062}
63
Chris Lattnerf7da2c72006-08-24 22:43:55 +000064void LiveIntervals::releaseMemory() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 mi2iMap_.clear();
66 i2miMap_.clear();
67 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000068 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
69 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000070 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
71 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000072}
73
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000074/// runOnMachineFunction - Register allocate the whole function
75///
76bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 mf_ = &fn;
78 tm_ = &fn.getTarget();
79 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000080 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000081 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000082 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000083
Chris Lattner428b92e2006-09-15 03:57:23 +000084 // Number MachineInstrs and MachineBasicBlocks.
85 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000086 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000087
88 unsigned MIIndex = 0;
89 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
90 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000091 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000092
Chris Lattner428b92e2006-09-15 03:57:23 +000093 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
94 I != E; ++I) {
95 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000097 i2miMap_.push_back(I);
98 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000099 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000100
101 // Set the MBB2IdxMap entry for this MBB.
102 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Chris Lattner428b92e2006-09-15 03:57:23 +0000103 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000104
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000106
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000107 numIntervals += getNumIntervals();
108
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000109 DOUT << "********** INTERVALS **********\n";
110 for (iterator I = begin(), E = end(); I != E; ++I) {
111 I->second.print(DOUT, mri_);
112 DOUT << "\n";
113 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000114
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000116 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000117 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000118}
119
Chris Lattner70ca3582004-09-30 15:59:17 +0000120/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000121void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000122 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000123 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000124 I->second.print(DOUT, mri_);
125 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000126 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000127
128 O << "********** MACHINEINSTRS **********\n";
129 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
130 mbbi != mbbe; ++mbbi) {
131 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
132 for (MachineBasicBlock::iterator mii = mbbi->begin(),
133 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000134 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000135 }
136 }
137}
138
Evan Cheng549f27d32007-08-13 23:45:17 +0000139/// isReMaterializable - Returns true if the definition MI of the specified
140/// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000141bool LiveIntervals::isReMaterializable(const LiveInterval &li,
142 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengbc165e42007-08-16 07:24:22 +0000143 if (DisableReMat)
144 return false;
145
Evan Cheng549f27d32007-08-13 23:45:17 +0000146 if (tii_->isTriviallyReMaterializable(MI))
147 return true;
148
149 int FrameIdx = 0;
150 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
151 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
152 return false;
153
154 // This is a load from fixed stack slot. It can be rematerialized unless it's
155 // re-defined by a two-address instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000156 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
157 i != e; ++i) {
158 const VNInfo *VNI = *i;
159 if (VNI == ValNo)
Evan Cheng549f27d32007-08-13 23:45:17 +0000160 continue;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000161 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000162 if (DefIdx == ~1U)
163 continue; // Dead val#.
164 MachineInstr *DefMI = (DefIdx == ~0u)
165 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000166 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
Evan Cheng549f27d32007-08-13 23:45:17 +0000167 return false;
168 }
169 return true;
170}
171
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000172/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
173/// slot / to reg or any rematerialized load into ith operand of specified
174/// MI. If it is successul, MI is updated with the newly created MI and
175/// returns true.
Evan Cheng549f27d32007-08-13 23:45:17 +0000176bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000177 MachineInstr *DefMI,
Evan Cheng549f27d32007-08-13 23:45:17 +0000178 unsigned index, unsigned i,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000179 bool isSS, int slot, unsigned reg) {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000180 MachineInstr *fmi = isSS
181 ? mri_->foldMemoryOperand(MI, i, slot)
182 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng549f27d32007-08-13 23:45:17 +0000183 if (fmi) {
184 // Attempt to fold the memory reference into the instruction. If
185 // we can do this, we don't need to insert spill code.
186 if (lv_)
187 lv_->instructionChanged(MI, fmi);
188 MachineBasicBlock &MBB = *MI->getParent();
189 vrm.virtFolded(reg, MI, i, fmi);
190 mi2iMap_.erase(MI);
191 i2miMap_[index/InstrSlots::NUM] = fmi;
192 mi2iMap_[fmi] = index;
193 MI = MBB.insert(MBB.erase(MI), fmi);
194 ++numFolded;
195 return true;
196 }
197 return false;
198}
199
200std::vector<LiveInterval*> LiveIntervals::
201addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
202 // since this is called after the analysis is done we don't know if
203 // LiveVariables is available
204 lv_ = getAnalysisToUpdate<LiveVariables>();
205
206 std::vector<LiveInterval*> added;
207
208 assert(li.weight != HUGE_VALF &&
209 "attempt to spill already spilled interval!");
210
211 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
212 li.print(DOUT, mri_);
213 DOUT << '\n';
214
Evan Cheng32dfbea2007-10-12 08:50:34 +0000215 SSARegMap *RegMap = mf_->getSSARegMap();
216 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000217
218 unsigned NumValNums = li.getNumValNums();
219 SmallVector<MachineInstr*, 4> ReMatDefs;
220 ReMatDefs.resize(NumValNums, NULL);
221 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
222 ReMatOrigDefs.resize(NumValNums, NULL);
223 SmallVector<int, 4> ReMatIds;
224 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
225 BitVector ReMatDelete(NumValNums);
226 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
227
228 bool NeedStackSlot = false;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000229 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
230 i != e; ++i) {
231 const VNInfo *VNI = *i;
232 unsigned VN = VNI->id;
233 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000234 if (DefIdx == ~1U)
235 continue; // Dead val#.
236 // Is the def for the val# rematerializable?
237 MachineInstr *DefMI = (DefIdx == ~0u)
238 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000239 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000240 // Remember how to remat the def of this val#.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000241 ReMatOrigDefs[VN] = DefMI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000242 // Original def may be modified so we have to make a copy here. vrm must
243 // delete these!
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000244 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng549f27d32007-08-13 23:45:17 +0000245 vrm.setVirtIsReMaterialized(reg, DefMI);
246
247 bool CanDelete = true;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000248 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
249 unsigned KillIdx = VNI->kills[j];
Evan Cheng549f27d32007-08-13 23:45:17 +0000250 MachineInstr *KillMI = (KillIdx & 1)
251 ? NULL : getInstructionFromIndex(KillIdx);
252 // Kill is a phi node, not all of its uses can be rematerialized.
253 // It must not be deleted.
254 if (!KillMI) {
255 CanDelete = false;
256 // Need a stack slot if there is any live range where uses cannot be
257 // rematerialized.
258 NeedStackSlot = true;
259 break;
260 }
261 }
262
263 if (CanDelete)
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000264 ReMatDelete.set(VN);
Evan Cheng549f27d32007-08-13 23:45:17 +0000265 } else {
266 // Need a stack slot if there is any live range where uses cannot be
267 // rematerialized.
268 NeedStackSlot = true;
269 }
270 }
271
272 // One stack slot per live interval.
273 if (NeedStackSlot)
274 slot = vrm.assignVirt2StackSlot(reg);
275
276 for (LiveInterval::Ranges::const_iterator
277 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000278 MachineInstr *DefMI = ReMatDefs[I->valno->id];
279 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000280 bool DefIsReMat = DefMI != NULL;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000281 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000282 int LdSlot = 0;
283 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000284 bool isLoad = isLoadSS ||
285 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng549f27d32007-08-13 23:45:17 +0000286 unsigned index = getBaseIndex(I->start);
287 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
288 for (; index != end; index += InstrSlots::NUM) {
289 // skip deleted instructions
290 while (index != end && !getInstructionFromIndex(index))
291 index += InstrSlots::NUM;
292 if (index == end) break;
293
294 MachineInstr *MI = getInstructionFromIndex(index);
295
296 RestartInstruction:
297 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
298 MachineOperand& mop = MI->getOperand(i);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000299 if (!mop.isRegister())
300 continue;
301 unsigned Reg = mop.getReg();
302 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
303 continue;
304 bool isSubReg = RegMap->isSubRegister(Reg);
305 unsigned SubIdx = 0;
306 if (isSubReg) {
307 SubIdx = RegMap->getSubRegisterIndex(Reg);
308 Reg = RegMap->getSuperRegister(Reg);
309 }
310 if (Reg != li.reg)
311 continue;
312
313 bool TryFold = !DefIsReMat;
314 bool FoldSS = true;
315 int FoldSlot = slot;
316 if (DefIsReMat) {
317 // If this is the rematerializable definition MI itself and
318 // all of its uses are rematerialized, simply delete it.
319 if (MI == OrigDefMI && CanDelete) {
320 RemoveMachineInstrFromMaps(MI);
321 MI->eraseFromParent();
322 break;
Evan Cheng549f27d32007-08-13 23:45:17 +0000323 }
324
Evan Cheng32dfbea2007-10-12 08:50:34 +0000325 // If def for this use can't be rematerialized, then try folding.
326 TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
327 if (isLoad) {
328 // Try fold loads (from stack slot, constant pool, etc.) into uses.
329 FoldSS = isLoadSS;
330 FoldSlot = LdSlot;
Evan Cheng549f27d32007-08-13 23:45:17 +0000331 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000332 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000333
Evan Cheng32dfbea2007-10-12 08:50:34 +0000334 // FIXME: fold subreg use
335 if (!isSubReg && TryFold &&
336 tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
337 // Folding the load/store can completely change the instruction in
338 // unpredictable ways, rescan it from the beginning.
339 goto RestartInstruction;
340
341 // Create a new virtual register for the spill interval.
342 unsigned NewVReg = RegMap->createVirtualRegister(rc);
343 if (isSubReg)
344 RegMap->setIsSubRegister(NewVReg, NewVReg, SubIdx);
345
346 // Scan all of the operands of this instruction rewriting operands
347 // to use NewVReg instead of li.reg as appropriate. We do this for
348 // two reasons:
349 //
350 // 1. If the instr reads the same spilled vreg multiple times, we
351 // want to reuse the NewVReg.
352 // 2. If the instr is a two-addr instruction, we are required to
353 // keep the src/dst regs pinned.
354 //
355 // Keep track of whether we replace a use and/or def so that we can
356 // create the spill interval with the appropriate range.
357 mop.setReg(NewVReg);
358
359 bool HasUse = mop.isUse();
360 bool HasDef = mop.isDef();
361 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
362 if (MI->getOperand(j).isRegister() &&
363 MI->getOperand(j).getReg() == li.reg) {
364 MI->getOperand(j).setReg(NewVReg);
365 HasUse |= MI->getOperand(j).isUse();
366 HasDef |= MI->getOperand(j).isDef();
367 }
368 }
369
370 vrm.grow();
371 if (DefIsReMat) {
372 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
373 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
374 // Each valnum may have its own remat id.
375 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000376 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000377 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
378 }
379 if (!CanDelete || (HasUse && HasDef)) {
380 // If this is a two-addr instruction then its use operands are
381 // rematerializable but its def is not. It should be assigned a
382 // stack slot.
Evan Cheng549f27d32007-08-13 23:45:17 +0000383 vrm.assignVirt2StackSlot(NewVReg, slot);
384 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000385 } else {
386 vrm.assignVirt2StackSlot(NewVReg, slot);
Evan Cheng549f27d32007-08-13 23:45:17 +0000387 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000388
389 // create a new register interval for this spill / remat.
390 LiveInterval &nI = getOrCreateInterval(NewVReg);
391 assert(nI.empty());
392
393 // the spill weight is now infinity as it
394 // cannot be spilled again
395 nI.weight = HUGE_VALF;
396
397 if (HasUse) {
398 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
399 nI.getNextValue(~0U, 0, VNInfoAllocator));
400 DOUT << " +" << LR;
401 nI.addRange(LR);
402 }
403 if (HasDef) {
404 LiveRange LR(getDefIndex(index), getStoreIndex(index),
405 nI.getNextValue(~0U, 0, VNInfoAllocator));
406 DOUT << " +" << LR;
407 nI.addRange(LR);
408 }
409
410 added.push_back(&nI);
411
412 // update live variables if it is available
413 if (lv_)
414 lv_->addVirtualRegisterKilled(NewVReg, MI);
415
416 DOUT << "\t\t\t\tadded new interval: ";
417 nI.print(DOUT, mri_);
418 DOUT << '\n';
Evan Cheng549f27d32007-08-13 23:45:17 +0000419 }
420 }
421 }
422
423 return added;
424}
425
426void LiveIntervals::printRegName(unsigned reg) const {
427 if (MRegisterInfo::isPhysicalRegister(reg))
428 cerr << mri_->getName(reg);
429 else
430 cerr << "%reg" << reg;
431}
432
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000433void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000434 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000435 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000436 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000437 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000438 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000439
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000440 // Virtual registers may be defined multiple times (due to phi
441 // elimination and 2-addr elimination). Much of what we do only has to be
442 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 // time we see a vreg.
444 if (interval.empty()) {
445 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000446 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000447 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000448 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000449 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000450 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000451 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
452 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
453 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
454 VNInfoAllocator);
455 else
456 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000457
458 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000459
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000460 // Loop over all of the blocks that the vreg is defined in. There are
461 // two cases we have to handle here. The most common case is a vreg
462 // whose lifetime is contained within a basic block. In this case there
463 // will be a single kill, in MBB, which comes after the definition.
464 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
465 // FIXME: what about dead vars?
466 unsigned killIdx;
467 if (vi.Kills[0] != mi)
468 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
469 else
470 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000471
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000472 // If the kill happens after the definition, we have an intra-block
473 // live range.
474 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000475 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000476 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000477 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000478 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000479 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000480 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000481 return;
482 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000483 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000484
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000485 // The other case we handle is when a virtual register lives to the end
486 // of the defining block, potentially live across some blocks, then is
487 // live into some number of blocks, but gets killed. Start by adding a
488 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000489 LiveRange NewLR(defIndex,
490 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000491 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000492 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000493 interval.addRange(NewLR);
494
495 // Iterate over all of the blocks that the variable is completely
496 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
497 // live interval.
498 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
499 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000500 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
501 if (!MBB->empty()) {
502 LiveRange LR(getMBBStartIdx(i),
503 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000504 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000505 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000506 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000507 }
508 }
509 }
510
511 // Finally, this virtual register is live from the start of any killing
512 // block to the 'use' slot of the killing instruction.
513 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
514 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000515 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000516 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000517 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000518 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000519 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000520 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000521 }
522
523 } else {
524 // If this is the second time we see a virtual register definition, it
525 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000526 // the result of two address elimination, then the vreg is one of the
527 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000528 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 // If this is a two-address definition, then we have already processed
530 // the live range. The only problem is that we didn't realize there
531 // are actually two values in the live interval. Because of this we
532 // need to take the LiveRegion that defines this register and split it
533 // into two values.
534 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000535 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536
Evan Cheng4f8ff162007-08-11 00:59:19 +0000537 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000538 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000539 unsigned OldEnd = OldLR->end;
540
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000542 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000544
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000545 // Two-address vregs should always only be redefined once. This means
546 // that at this point, there should be exactly one value number in it.
547 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
548
Chris Lattner91725b72006-08-31 05:54:43 +0000549 // The new value number (#1) is defined by the instruction we claimed
550 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000551 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
552 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000553
Chris Lattner91725b72006-08-31 05:54:43 +0000554 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000555 OldValNo->def = RedefIndex;
556 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000557
558 // Add the new live interval which replaces the range for the input copy.
559 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000560 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000561 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000562 interval.addKill(ValNo, RedefIndex);
563 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564
565 // If this redefinition is dead, we need to add a dummy unit live
566 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000567 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000568 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000569
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000570 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000571 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000572
573 } else {
574 // Otherwise, this must be because of phi elimination. If this is the
575 // first redefinition of the vreg that we have seen, go back and change
576 // the live range in the PHI block to be a different value number.
577 if (interval.containsOneValue()) {
578 assert(vi.Kills.size() == 1 &&
579 "PHI elimination vreg should have one kill, the PHI itself!");
580
581 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000582 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000583 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000584 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000585 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000586 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000587 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000588 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000589 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000590 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000591
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000592 // Replace the interval with one of a NEW value number. Note that this
593 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000594 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000595 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000596 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000597 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000598 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000599 }
600
601 // In the case of PHI elimination, each variable definition is only
602 // live until the end of the block. We've already taken care of the
603 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000604 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000605
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000606 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000607 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000608 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000609 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000610 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
611 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
612 VNInfoAllocator);
613 else
614 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000615
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000616 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000617 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000618 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000619 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000620 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000621 }
622 }
623
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000624 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000625}
626
Chris Lattnerf35fef72004-07-23 21:24:19 +0000627void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000628 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000629 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000630 LiveInterval &interval,
631 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000632 // A physical register cannot be live across basic block, so its
633 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000634 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000635
Chris Lattner6b128bd2006-09-03 08:07:11 +0000636 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000637 unsigned start = getDefIndex(baseIndex);
638 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000639
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 // If it is not used after definition, it is considered dead at
641 // the instruction defining it. Hence its interval is:
642 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000643 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000644 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000645 end = getDefIndex(start) + 1;
646 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 }
648
649 // If it is not dead on definition, it must be killed by a
650 // subsequent instruction. Hence its interval is:
651 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000652 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000653 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000654 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000655 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000656 end = getUseIndex(baseIndex) + 1;
657 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000658 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
659 // Another instruction redefines the register before it is ever read.
660 // Then the register is essentially dead at the instruction that defines
661 // it. Hence its interval is:
662 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000663 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000664 end = getDefIndex(start) + 1;
665 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000666 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000667 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000668
669 // The only case we should have a dead physreg here without a killing or
670 // instruction where we know it's dead is if it is live-in to the function
671 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000672 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000673 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000674
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000675exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000676 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000677
Evan Cheng24a3cc42007-04-25 07:30:23 +0000678 // Already exists? Extend old live interval.
679 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000680 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000681 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000682 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000684 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000685 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000686}
687
Chris Lattnerf35fef72004-07-23 21:24:19 +0000688void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
689 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000690 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000691 unsigned reg) {
692 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000693 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000694 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000695 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000696 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
697 SrcReg = MI->getOperand(1).getReg();
698 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000699 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000700 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000701 // Def of a register also defines its sub-registers.
702 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
703 // Avoid processing some defs more than once.
704 if (!MI->findRegisterDefOperand(*AS))
705 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000706 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000707}
708
Evan Chengb371f452007-02-19 21:49:54 +0000709void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000710 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000711 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000712 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
713
714 // Look for kills, if it reaches a def before it's killed, then it shouldn't
715 // be considered a livein.
716 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000717 unsigned baseIndex = MIIdx;
718 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000719 unsigned end = start;
720 while (mi != MBB->end()) {
721 if (lv_->KillsRegister(mi, interval.reg)) {
722 DOUT << " killed";
723 end = getUseIndex(baseIndex) + 1;
724 goto exit;
725 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
726 // Another instruction redefines the register before it is ever read.
727 // Then the register is essentially dead at the instruction that defines
728 // it. Hence its interval is:
729 // [defSlot(def), defSlot(def)+1)
730 DOUT << " dead";
731 end = getDefIndex(start) + 1;
732 goto exit;
733 }
734
735 baseIndex += InstrSlots::NUM;
736 ++mi;
737 }
738
739exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000740 // Live-in register might not be used at all.
741 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000742 if (isAlias) {
743 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000744 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000745 } else {
746 DOUT << " live through";
747 end = baseIndex;
748 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000749 }
750
Evan Chengf3bb2e62007-09-05 21:46:51 +0000751 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000752 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000753 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000754 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000755}
756
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000757/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000758/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000759/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000760/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000761void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000762 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
763 << "********** Function: "
764 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000765 // Track the index of the current machine instr.
766 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000767 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
768 MBBI != E; ++MBBI) {
769 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000770 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000771
Chris Lattner428b92e2006-09-15 03:57:23 +0000772 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000773
Dan Gohmancb406c22007-10-03 19:26:29 +0000774 // Create intervals for live-ins to this BB first.
775 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
776 LE = MBB->livein_end(); LI != LE; ++LI) {
777 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
778 // Multiple live-ins can alias the same register.
779 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
780 if (!hasInterval(*AS))
781 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
782 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000783 }
784
Chris Lattner428b92e2006-09-15 03:57:23 +0000785 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000786 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000787
Evan Cheng438f7bc2006-11-10 08:43:01 +0000788 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000789 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
790 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000791 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000792 if (MO.isRegister() && MO.getReg() && MO.isDef())
793 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000794 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000795
796 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000797 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000798 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000799}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000800
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000801LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000802 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000803 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000804 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000805}