blob: 2f93b76948547bf87318ef7cea0c97c9bb131626 [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() {
Evan Chengf3bb2e62007-09-05 21:46:51 +000065 VNInfoAllocator.Reset();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000066 mi2iMap_.clear();
67 i2miMap_.clear();
68 r2iMap_.clear();
Evan Cheng549f27d32007-08-13 23:45:17 +000069 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
70 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000071}
72
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000073/// runOnMachineFunction - Register allocate the whole function
74///
75bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000076 mf_ = &fn;
77 tm_ = &fn.getTarget();
78 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000079 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000080 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000081 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000082
Chris Lattner428b92e2006-09-15 03:57:23 +000083 // Number MachineInstrs and MachineBasicBlocks.
84 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000085 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000086
87 unsigned MIIndex = 0;
88 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
89 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000090 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000091
Chris Lattner428b92e2006-09-15 03:57:23 +000092 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
93 I != E; ++I) {
94 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000096 i2miMap_.push_back(I);
97 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000098 }
Evan Cheng549f27d32007-08-13 23:45:17 +000099
100 // Set the MBB2IdxMap entry for this MBB.
101 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Chris Lattner428b92e2006-09-15 03:57:23 +0000102 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000103
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000104 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000105
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106 numIntervals += getNumIntervals();
107
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000108 DOUT << "********** INTERVALS **********\n";
109 for (iterator I = begin(), E = end(); I != E; ++I) {
110 I->second.print(DOUT, mri_);
111 DOUT << "\n";
112 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000114 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000115 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000117}
118
Chris Lattner70ca3582004-09-30 15:59:17 +0000119/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000120void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000121 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000122 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000123 I->second.print(DOUT, mri_);
124 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000125 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000126
127 O << "********** MACHINEINSTRS **********\n";
128 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
129 mbbi != mbbe; ++mbbi) {
130 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
131 for (MachineBasicBlock::iterator mii = mbbi->begin(),
132 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000133 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000134 }
135 }
136}
137
David Greene25133302007-06-08 17:18:56 +0000138// Not called?
Bill Wendling01352aa2006-11-16 02:41:50 +0000139/// CreateNewLiveInterval - Create a new live interval with the given live
140/// ranges. The new live interval will have an infinite spill weight.
141LiveInterval&
142LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI,
143 const std::vector<LiveRange> &LRs) {
144 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg);
145
146 // Create a new virtual register for the spill interval.
147 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC);
148
149 // Replace the old virtual registers in the machine operands with the shiny
150 // new one.
151 for (std::vector<LiveRange>::const_iterator
152 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
153 unsigned Index = getBaseIndex(I->start);
154 unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM;
155
156 for (; Index != End; Index += InstrSlots::NUM) {
157 // Skip deleted instructions
158 while (Index != End && !getInstructionFromIndex(Index))
159 Index += InstrSlots::NUM;
160
161 if (Index == End) break;
162
163 MachineInstr *MI = getInstructionFromIndex(Index);
164
Bill Wendlingbeeb77f2006-11-16 07:35:18 +0000165 for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) {
Bill Wendling01352aa2006-11-16 02:41:50 +0000166 MachineOperand &MOp = MI->getOperand(J);
David Greene25133302007-06-08 17:18:56 +0000167 if (MOp.isRegister() && MOp.getReg() == LI->reg)
Bill Wendling01352aa2006-11-16 02:41:50 +0000168 MOp.setReg(NewVReg);
169 }
170 }
171 }
172
173 LiveInterval &NewLI = getOrCreateInterval(NewVReg);
174
175 // The spill weight is now infinity as it cannot be spilled again
176 NewLI.weight = float(HUGE_VAL);
177
178 for (std::vector<LiveRange>::const_iterator
179 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000180 DOUT << " Adding live range " << *I << " to new interval\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000181 NewLI.addRange(*I);
182 }
183
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000184 DOUT << "Created new live interval " << NewLI << "\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000185 return NewLI;
186}
187
Evan Chengbf105c82006-11-03 03:04:46 +0000188/// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to
189/// two addr elimination.
190static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg,
191 const TargetInstrInfo *TII) {
192 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
193 MachineOperand &MO1 = MI->getOperand(i);
194 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
195 for (unsigned j = i+1; j < e; ++j) {
196 MachineOperand &MO2 = MI->getOperand(j);
197 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Evan Cheng51cdcd12006-12-07 01:21:59 +0000198 MI->getInstrDescriptor()->
199 getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Chengbf105c82006-11-03 03:04:46 +0000200 return true;
201 }
202 }
203 }
204 return false;
205}
206
Evan Cheng549f27d32007-08-13 23:45:17 +0000207/// isReMaterializable - Returns true if the definition MI of the specified
208/// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000209bool LiveIntervals::isReMaterializable(const LiveInterval &li,
210 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengbc165e42007-08-16 07:24:22 +0000211 if (DisableReMat)
212 return false;
213
Evan Cheng549f27d32007-08-13 23:45:17 +0000214 if (tii_->isTriviallyReMaterializable(MI))
215 return true;
216
217 int FrameIdx = 0;
218 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
219 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
220 return false;
221
222 // This is a load from fixed stack slot. It can be rematerialized unless it's
223 // re-defined by a two-address instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000224 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
225 i != e; ++i) {
226 const VNInfo *VNI = *i;
227 if (VNI == ValNo)
Evan Cheng549f27d32007-08-13 23:45:17 +0000228 continue;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000229 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000230 if (DefIdx == ~1U)
231 continue; // Dead val#.
232 MachineInstr *DefMI = (DefIdx == ~0u)
233 ? NULL : getInstructionFromIndex(DefIdx);
234 if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_))
235 return false;
236 }
237 return true;
238}
239
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000240/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
241/// slot / to reg or any rematerialized load into ith operand of specified
242/// MI. If it is successul, MI is updated with the newly created MI and
243/// returns true.
Evan Cheng549f27d32007-08-13 23:45:17 +0000244bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
245 unsigned index, unsigned i,
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000246 bool isSS, MachineInstr *DefMI,
Evan Cheng549f27d32007-08-13 23:45:17 +0000247 int slot, unsigned reg) {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000248 MachineInstr *fmi = isSS
249 ? mri_->foldMemoryOperand(MI, i, slot)
250 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng549f27d32007-08-13 23:45:17 +0000251 if (fmi) {
252 // Attempt to fold the memory reference into the instruction. If
253 // we can do this, we don't need to insert spill code.
254 if (lv_)
255 lv_->instructionChanged(MI, fmi);
256 MachineBasicBlock &MBB = *MI->getParent();
257 vrm.virtFolded(reg, MI, i, fmi);
258 mi2iMap_.erase(MI);
259 i2miMap_[index/InstrSlots::NUM] = fmi;
260 mi2iMap_[fmi] = index;
261 MI = MBB.insert(MBB.erase(MI), fmi);
262 ++numFolded;
263 return true;
264 }
265 return false;
266}
267
268std::vector<LiveInterval*> LiveIntervals::
269addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
270 // since this is called after the analysis is done we don't know if
271 // LiveVariables is available
272 lv_ = getAnalysisToUpdate<LiveVariables>();
273
274 std::vector<LiveInterval*> added;
275
276 assert(li.weight != HUGE_VALF &&
277 "attempt to spill already spilled interval!");
278
279 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
280 li.print(DOUT, mri_);
281 DOUT << '\n';
282
283 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
284
285 unsigned NumValNums = li.getNumValNums();
286 SmallVector<MachineInstr*, 4> ReMatDefs;
287 ReMatDefs.resize(NumValNums, NULL);
288 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
289 ReMatOrigDefs.resize(NumValNums, NULL);
290 SmallVector<int, 4> ReMatIds;
291 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
292 BitVector ReMatDelete(NumValNums);
293 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
294
295 bool NeedStackSlot = false;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000296 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
297 i != e; ++i) {
298 const VNInfo *VNI = *i;
299 unsigned VN = VNI->id;
300 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000301 if (DefIdx == ~1U)
302 continue; // Dead val#.
303 // Is the def for the val# rematerializable?
304 MachineInstr *DefMI = (DefIdx == ~0u)
305 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000306 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000307 // Remember how to remat the def of this val#.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000308 ReMatOrigDefs[VN] = DefMI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000309 // Original def may be modified so we have to make a copy here. vrm must
310 // delete these!
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000311 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng549f27d32007-08-13 23:45:17 +0000312 vrm.setVirtIsReMaterialized(reg, DefMI);
313
314 bool CanDelete = true;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000315 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
316 unsigned KillIdx = VNI->kills[j];
Evan Cheng549f27d32007-08-13 23:45:17 +0000317 MachineInstr *KillMI = (KillIdx & 1)
318 ? NULL : getInstructionFromIndex(KillIdx);
319 // Kill is a phi node, not all of its uses can be rematerialized.
320 // It must not be deleted.
321 if (!KillMI) {
322 CanDelete = false;
323 // Need a stack slot if there is any live range where uses cannot be
324 // rematerialized.
325 NeedStackSlot = true;
326 break;
327 }
328 }
329
330 if (CanDelete)
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000331 ReMatDelete.set(VN);
Evan Cheng549f27d32007-08-13 23:45:17 +0000332 } else {
333 // Need a stack slot if there is any live range where uses cannot be
334 // rematerialized.
335 NeedStackSlot = true;
336 }
337 }
338
339 // One stack slot per live interval.
340 if (NeedStackSlot)
341 slot = vrm.assignVirt2StackSlot(reg);
342
343 for (LiveInterval::Ranges::const_iterator
344 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000345 MachineInstr *DefMI = ReMatDefs[I->valno->id];
346 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000347 bool DefIsReMat = DefMI != NULL;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000348 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000349 int LdSlot = 0;
350 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000351 bool isLoad = isLoadSS ||
352 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng549f27d32007-08-13 23:45:17 +0000353 unsigned index = getBaseIndex(I->start);
354 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
355 for (; index != end; index += InstrSlots::NUM) {
356 // skip deleted instructions
357 while (index != end && !getInstructionFromIndex(index))
358 index += InstrSlots::NUM;
359 if (index == end) break;
360
361 MachineInstr *MI = getInstructionFromIndex(index);
362
363 RestartInstruction:
364 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
365 MachineOperand& mop = MI->getOperand(i);
366 if (mop.isRegister() && mop.getReg() == li.reg) {
367 if (DefIsReMat) {
368 // If this is the rematerializable definition MI itself and
369 // all of its uses are rematerialized, simply delete it.
370 if (MI == OrigDefMI) {
371 if (CanDelete) {
372 RemoveMachineInstrFromMaps(MI);
373 MI->eraseFromParent();
374 break;
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000375 } else if (tryFoldMemoryOperand(MI, vrm, index, i, true,
376 DefMI, slot, li.reg)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000377 // Folding the load/store can completely change the instruction
378 // in unpredictable ways, rescan it from the beginning.
379 goto RestartInstruction;
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000380 }
381 } else if (isLoad &&
382 tryFoldMemoryOperand(MI, vrm, index, i, isLoadSS,
383 DefMI, LdSlot, li.reg))
384 // Folding the load/store can completely change the
385 // instruction in unpredictable ways, rescan it from
386 // the beginning.
387 goto RestartInstruction;
Evan Cheng549f27d32007-08-13 23:45:17 +0000388 } else {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000389 if (tryFoldMemoryOperand(MI, vrm, index, i, true, DefMI,
390 slot, li.reg))
Evan Cheng549f27d32007-08-13 23:45:17 +0000391 // Folding the load/store can completely change the instruction in
392 // unpredictable ways, rescan it from the beginning.
393 goto RestartInstruction;
394 }
395
396 // Create a new virtual register for the spill interval.
397 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc);
398
399 // Scan all of the operands of this instruction rewriting operands
400 // to use NewVReg instead of li.reg as appropriate. We do this for
401 // two reasons:
402 //
403 // 1. If the instr reads the same spilled vreg multiple times, we
404 // want to reuse the NewVReg.
405 // 2. If the instr is a two-addr instruction, we are required to
406 // keep the src/dst regs pinned.
407 //
408 // Keep track of whether we replace a use and/or def so that we can
409 // create the spill interval with the appropriate range.
410 mop.setReg(NewVReg);
411
412 bool HasUse = mop.isUse();
413 bool HasDef = mop.isDef();
414 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
415 if (MI->getOperand(j).isReg() &&
416 MI->getOperand(j).getReg() == li.reg) {
417 MI->getOperand(j).setReg(NewVReg);
418 HasUse |= MI->getOperand(j).isUse();
419 HasDef |= MI->getOperand(j).isDef();
420 }
421 }
422
423 vrm.grow();
424 if (DefIsReMat) {
425 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000426 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000427 // Each valnum may have its own remat id.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000428 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000429 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000430 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
Evan Cheng549f27d32007-08-13 23:45:17 +0000431 }
432 if (!CanDelete || (HasUse && HasDef)) {
433 // If this is a two-addr instruction then its use operands are
434 // rematerializable but its def is not. It should be assigned a
435 // stack slot.
436 vrm.assignVirt2StackSlot(NewVReg, slot);
437 }
438 } else {
439 vrm.assignVirt2StackSlot(NewVReg, slot);
440 }
441
442 // create a new register interval for this spill / remat.
443 LiveInterval &nI = getOrCreateInterval(NewVReg);
444 assert(nI.empty());
445
446 // the spill weight is now infinity as it
447 // cannot be spilled again
448 nI.weight = HUGE_VALF;
449
450 if (HasUse) {
451 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Evan Chengf3bb2e62007-09-05 21:46:51 +0000452 nI.getNextValue(~0U, 0, VNInfoAllocator));
Evan Cheng549f27d32007-08-13 23:45:17 +0000453 DOUT << " +" << LR;
454 nI.addRange(LR);
455 }
456 if (HasDef) {
457 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Evan Chengf3bb2e62007-09-05 21:46:51 +0000458 nI.getNextValue(~0U, 0, VNInfoAllocator));
Evan Cheng549f27d32007-08-13 23:45:17 +0000459 DOUT << " +" << LR;
460 nI.addRange(LR);
461 }
462
463 added.push_back(&nI);
464
465 // update live variables if it is available
466 if (lv_)
467 lv_->addVirtualRegisterKilled(NewVReg, MI);
468
469 DOUT << "\t\t\t\tadded new interval: ";
470 nI.print(DOUT, mri_);
471 DOUT << '\n';
472 }
473 }
474 }
475 }
476
477 return added;
478}
479
480void LiveIntervals::printRegName(unsigned reg) const {
481 if (MRegisterInfo::isPhysicalRegister(reg))
482 cerr << mri_->getName(reg);
483 else
484 cerr << "%reg" << reg;
485}
486
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000487void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000488 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000489 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000490 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000491 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000492 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000494 // Virtual registers may be defined multiple times (due to phi
495 // elimination and 2-addr elimination). Much of what we do only has to be
496 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000497 // time we see a vreg.
498 if (interval.empty()) {
499 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000500 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000501 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000502 unsigned SrcReg, DstReg;
503 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000504 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000505 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000506 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000507
508 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000509
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 // Loop over all of the blocks that the vreg is defined in. There are
511 // two cases we have to handle here. The most common case is a vreg
512 // whose lifetime is contained within a basic block. In this case there
513 // will be a single kill, in MBB, which comes after the definition.
514 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
515 // FIXME: what about dead vars?
516 unsigned killIdx;
517 if (vi.Kills[0] != mi)
518 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
519 else
520 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000521
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000522 // If the kill happens after the definition, we have an intra-block
523 // live range.
524 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000525 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000526 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000527 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000528 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000529 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000530 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000531 return;
532 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000533 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000534
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000535 // The other case we handle is when a virtual register lives to the end
536 // of the defining block, potentially live across some blocks, then is
537 // live into some number of blocks, but gets killed. Start by adding a
538 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000539 LiveRange NewLR(defIndex,
540 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000541 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000542 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 interval.addRange(NewLR);
544
545 // Iterate over all of the blocks that the variable is completely
546 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
547 // live interval.
548 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
549 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000550 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
551 if (!MBB->empty()) {
552 LiveRange LR(getMBBStartIdx(i),
553 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000554 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000555 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000556 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000557 }
558 }
559 }
560
561 // Finally, this virtual register is live from the start of any killing
562 // block to the 'use' slot of the killing instruction.
563 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
564 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000565 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000566 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000567 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000569 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000570 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000571 }
572
573 } else {
574 // If this is the second time we see a virtual register definition, it
575 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000576 // the result of two address elimination, then the vreg is one of the
577 // def-and-use register operand.
578 if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000579 // If this is a two-address definition, then we have already processed
580 // the live range. The only problem is that we didn't realize there
581 // are actually two values in the live interval. Because of this we
582 // need to take the LiveRegion that defines this register and split it
583 // into two values.
584 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000585 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000586
Evan Cheng4f8ff162007-08-11 00:59:19 +0000587 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000588 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000589 unsigned OldEnd = OldLR->end;
590
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000591 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000592 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000593 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000594
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000595 // Two-address vregs should always only be redefined once. This means
596 // that at this point, there should be exactly one value number in it.
597 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
598
Chris Lattner91725b72006-08-31 05:54:43 +0000599 // The new value number (#1) is defined by the instruction we claimed
600 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000601 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
602 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000603
Chris Lattner91725b72006-08-31 05:54:43 +0000604 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000605 OldValNo->def = RedefIndex;
606 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000607
608 // Add the new live interval which replaces the range for the input copy.
609 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000610 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000611 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000612 interval.addKill(ValNo, RedefIndex);
613 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614
615 // If this redefinition is dead, we need to add a dummy unit live
616 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000617 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000618 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000619
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000620 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000621 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000622
623 } else {
624 // Otherwise, this must be because of phi elimination. If this is the
625 // first redefinition of the vreg that we have seen, go back and change
626 // the live range in the PHI block to be a different value number.
627 if (interval.containsOneValue()) {
628 assert(vi.Kills.size() == 1 &&
629 "PHI elimination vreg should have one kill, the PHI itself!");
630
631 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000632 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000633 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000634 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000635 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000636 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000637 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000639 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000640 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000641
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000642 // Replace the interval with one of a NEW value number. Note that this
643 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000644 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000645 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000646 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000647 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000648 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000649 }
650
651 // In the case of PHI elimination, each variable definition is only
652 // live until the end of the block. We've already taken care of the
653 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000654 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000655
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000656 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000657 unsigned SrcReg, DstReg;
658 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000659 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000660 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000661 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000662
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000663 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000664 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000666 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000667 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000668 }
669 }
670
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000671 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000672}
673
Chris Lattnerf35fef72004-07-23 21:24:19 +0000674void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000675 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000676 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000677 LiveInterval &interval,
678 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 // A physical register cannot be live across basic block, so its
680 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000681 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000682
Chris Lattner6b128bd2006-09-03 08:07:11 +0000683 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000684 unsigned start = getDefIndex(baseIndex);
685 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000686
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 // If it is not used after definition, it is considered dead at
688 // the instruction defining it. Hence its interval is:
689 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000690 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000691 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000692 end = getDefIndex(start) + 1;
693 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000694 }
695
696 // If it is not dead on definition, it must be killed by a
697 // subsequent instruction. Hence its interval is:
698 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000699 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000700 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000701 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000702 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000703 end = getUseIndex(baseIndex) + 1;
704 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000705 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
706 // Another instruction redefines the register before it is ever read.
707 // Then the register is essentially dead at the instruction that defines
708 // it. Hence its interval is:
709 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000710 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000711 end = getDefIndex(start) + 1;
712 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000713 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000714 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000715
716 // The only case we should have a dead physreg here without a killing or
717 // instruction where we know it's dead is if it is live-in to the function
718 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000719 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000720 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000721
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000722exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000723 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000724
Evan Cheng24a3cc42007-04-25 07:30:23 +0000725 // Already exists? Extend old live interval.
726 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000727 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000728 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000729 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000730 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000731 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000732 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000733}
734
Chris Lattnerf35fef72004-07-23 21:24:19 +0000735void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
736 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000737 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000738 unsigned reg) {
739 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000740 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000741 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000742 unsigned SrcReg, DstReg;
743 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
744 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000745 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000746 // Def of a register also defines its sub-registers.
747 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
748 // Avoid processing some defs more than once.
749 if (!MI->findRegisterDefOperand(*AS))
750 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000751 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000752}
753
Evan Chengb371f452007-02-19 21:49:54 +0000754void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000755 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000756 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000757 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
758
759 // Look for kills, if it reaches a def before it's killed, then it shouldn't
760 // be considered a livein.
761 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000762 unsigned baseIndex = MIIdx;
763 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000764 unsigned end = start;
765 while (mi != MBB->end()) {
766 if (lv_->KillsRegister(mi, interval.reg)) {
767 DOUT << " killed";
768 end = getUseIndex(baseIndex) + 1;
769 goto exit;
770 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
771 // Another instruction redefines the register before it is ever read.
772 // Then the register is essentially dead at the instruction that defines
773 // it. Hence its interval is:
774 // [defSlot(def), defSlot(def)+1)
775 DOUT << " dead";
776 end = getDefIndex(start) + 1;
777 goto exit;
778 }
779
780 baseIndex += InstrSlots::NUM;
781 ++mi;
782 }
783
784exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000785 // Live-in register might not be used at all.
786 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000787 if (isAlias) {
788 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000789 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000790 } else {
791 DOUT << " live through";
792 end = baseIndex;
793 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000794 }
795
Evan Chengf3bb2e62007-09-05 21:46:51 +0000796 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000797 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000798 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000799 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000800}
801
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000802/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000803/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000804/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000805/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000806void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000807 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
808 << "********** Function: "
809 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000810 // Track the index of the current machine instr.
811 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000812 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
813 MBBI != E; ++MBBI) {
814 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000815 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000816
Chris Lattner428b92e2006-09-15 03:57:23 +0000817 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000818
819 if (MBB->livein_begin() != MBB->livein_end()) {
Evan Chengb371f452007-02-19 21:49:54 +0000820 // Create intervals for live-ins to this BB first.
821 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000822 LE = MBB->livein_end(); LI != LE; ++LI) {
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000823 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
Evan Cheng24a3cc42007-04-25 07:30:23 +0000824 // Multiple live-ins can alias the same register.
825 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
826 if (!hasInterval(*AS))
Evan Cheng292da942007-06-27 18:47:28 +0000827 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
828 true);
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000829 }
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000830 }
831
Chris Lattner428b92e2006-09-15 03:57:23 +0000832 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000833 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000834
Evan Cheng438f7bc2006-11-10 08:43:01 +0000835 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000836 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
837 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000838 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000839 if (MO.isRegister() && MO.getReg() && MO.isDef())
840 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000841 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000842
843 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000844 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000845 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000846}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000847
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000848LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000849 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000850 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000851 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000852}