blob: 5ada75ca016c87f91abd241f78f14c0899f02bf3 [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
David Greene25133302007-06-08 17:18:56 +0000139// Not called?
Bill Wendling01352aa2006-11-16 02:41:50 +0000140/// CreateNewLiveInterval - Create a new live interval with the given live
141/// ranges. The new live interval will have an infinite spill weight.
142LiveInterval&
143LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI,
144 const std::vector<LiveRange> &LRs) {
145 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg);
146
147 // Create a new virtual register for the spill interval.
148 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC);
149
150 // Replace the old virtual registers in the machine operands with the shiny
151 // new one.
152 for (std::vector<LiveRange>::const_iterator
153 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
154 unsigned Index = getBaseIndex(I->start);
155 unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM;
156
157 for (; Index != End; Index += InstrSlots::NUM) {
158 // Skip deleted instructions
159 while (Index != End && !getInstructionFromIndex(Index))
160 Index += InstrSlots::NUM;
161
162 if (Index == End) break;
163
164 MachineInstr *MI = getInstructionFromIndex(Index);
165
Bill Wendlingbeeb77f2006-11-16 07:35:18 +0000166 for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) {
Bill Wendling01352aa2006-11-16 02:41:50 +0000167 MachineOperand &MOp = MI->getOperand(J);
David Greene25133302007-06-08 17:18:56 +0000168 if (MOp.isRegister() && MOp.getReg() == LI->reg)
Bill Wendling01352aa2006-11-16 02:41:50 +0000169 MOp.setReg(NewVReg);
170 }
171 }
172 }
173
174 LiveInterval &NewLI = getOrCreateInterval(NewVReg);
175
176 // The spill weight is now infinity as it cannot be spilled again
177 NewLI.weight = float(HUGE_VAL);
178
179 for (std::vector<LiveRange>::const_iterator
180 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000181 DOUT << " Adding live range " << *I << " to new interval\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000182 NewLI.addRange(*I);
183 }
184
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000185 DOUT << "Created new live interval " << NewLI << "\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000186 return NewLI;
187}
188
Evan Chengbf105c82006-11-03 03:04:46 +0000189/// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to
190/// two addr elimination.
191static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg,
192 const TargetInstrInfo *TII) {
193 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
194 MachineOperand &MO1 = MI->getOperand(i);
195 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
196 for (unsigned j = i+1; j < e; ++j) {
197 MachineOperand &MO2 = MI->getOperand(j);
198 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Evan Cheng51cdcd12006-12-07 01:21:59 +0000199 MI->getInstrDescriptor()->
200 getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Chengbf105c82006-11-03 03:04:46 +0000201 return true;
202 }
203 }
204 }
205 return false;
206}
207
Evan Cheng549f27d32007-08-13 23:45:17 +0000208/// isReMaterializable - Returns true if the definition MI of the specified
209/// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000210bool LiveIntervals::isReMaterializable(const LiveInterval &li,
211 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengbc165e42007-08-16 07:24:22 +0000212 if (DisableReMat)
213 return false;
214
Evan Cheng549f27d32007-08-13 23:45:17 +0000215 if (tii_->isTriviallyReMaterializable(MI))
216 return true;
217
218 int FrameIdx = 0;
219 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
220 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
221 return false;
222
223 // This is a load from fixed stack slot. It can be rematerialized unless it's
224 // re-defined by a two-address instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000225 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
226 i != e; ++i) {
227 const VNInfo *VNI = *i;
228 if (VNI == ValNo)
Evan Cheng549f27d32007-08-13 23:45:17 +0000229 continue;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000230 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000231 if (DefIdx == ~1U)
232 continue; // Dead val#.
233 MachineInstr *DefMI = (DefIdx == ~0u)
234 ? NULL : getInstructionFromIndex(DefIdx);
235 if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_))
236 return false;
237 }
238 return true;
239}
240
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000241/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
242/// slot / to reg or any rematerialized load into ith operand of specified
243/// MI. If it is successul, MI is updated with the newly created MI and
244/// returns true.
Evan Cheng549f27d32007-08-13 23:45:17 +0000245bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
246 unsigned index, unsigned i,
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000247 bool isSS, MachineInstr *DefMI,
Evan Cheng549f27d32007-08-13 23:45:17 +0000248 int slot, unsigned reg) {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000249 MachineInstr *fmi = isSS
250 ? mri_->foldMemoryOperand(MI, i, slot)
251 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng549f27d32007-08-13 23:45:17 +0000252 if (fmi) {
253 // Attempt to fold the memory reference into the instruction. If
254 // we can do this, we don't need to insert spill code.
255 if (lv_)
256 lv_->instructionChanged(MI, fmi);
257 MachineBasicBlock &MBB = *MI->getParent();
258 vrm.virtFolded(reg, MI, i, fmi);
259 mi2iMap_.erase(MI);
260 i2miMap_[index/InstrSlots::NUM] = fmi;
261 mi2iMap_[fmi] = index;
262 MI = MBB.insert(MBB.erase(MI), fmi);
263 ++numFolded;
264 return true;
265 }
266 return false;
267}
268
269std::vector<LiveInterval*> LiveIntervals::
270addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
271 // since this is called after the analysis is done we don't know if
272 // LiveVariables is available
273 lv_ = getAnalysisToUpdate<LiveVariables>();
274
275 std::vector<LiveInterval*> added;
276
277 assert(li.weight != HUGE_VALF &&
278 "attempt to spill already spilled interval!");
279
280 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
281 li.print(DOUT, mri_);
282 DOUT << '\n';
283
284 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
285
286 unsigned NumValNums = li.getNumValNums();
287 SmallVector<MachineInstr*, 4> ReMatDefs;
288 ReMatDefs.resize(NumValNums, NULL);
289 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
290 ReMatOrigDefs.resize(NumValNums, NULL);
291 SmallVector<int, 4> ReMatIds;
292 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
293 BitVector ReMatDelete(NumValNums);
294 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
295
296 bool NeedStackSlot = false;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000297 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
298 i != e; ++i) {
299 const VNInfo *VNI = *i;
300 unsigned VN = VNI->id;
301 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000302 if (DefIdx == ~1U)
303 continue; // Dead val#.
304 // Is the def for the val# rematerializable?
305 MachineInstr *DefMI = (DefIdx == ~0u)
306 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000307 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000308 // Remember how to remat the def of this val#.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000309 ReMatOrigDefs[VN] = DefMI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000310 // Original def may be modified so we have to make a copy here. vrm must
311 // delete these!
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000312 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng549f27d32007-08-13 23:45:17 +0000313 vrm.setVirtIsReMaterialized(reg, DefMI);
314
315 bool CanDelete = true;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000316 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
317 unsigned KillIdx = VNI->kills[j];
Evan Cheng549f27d32007-08-13 23:45:17 +0000318 MachineInstr *KillMI = (KillIdx & 1)
319 ? NULL : getInstructionFromIndex(KillIdx);
320 // Kill is a phi node, not all of its uses can be rematerialized.
321 // It must not be deleted.
322 if (!KillMI) {
323 CanDelete = false;
324 // Need a stack slot if there is any live range where uses cannot be
325 // rematerialized.
326 NeedStackSlot = true;
327 break;
328 }
329 }
330
331 if (CanDelete)
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000332 ReMatDelete.set(VN);
Evan Cheng549f27d32007-08-13 23:45:17 +0000333 } else {
334 // Need a stack slot if there is any live range where uses cannot be
335 // rematerialized.
336 NeedStackSlot = true;
337 }
338 }
339
340 // One stack slot per live interval.
341 if (NeedStackSlot)
342 slot = vrm.assignVirt2StackSlot(reg);
343
344 for (LiveInterval::Ranges::const_iterator
345 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000346 MachineInstr *DefMI = ReMatDefs[I->valno->id];
347 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000348 bool DefIsReMat = DefMI != NULL;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000349 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000350 int LdSlot = 0;
351 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000352 bool isLoad = isLoadSS ||
353 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng549f27d32007-08-13 23:45:17 +0000354 unsigned index = getBaseIndex(I->start);
355 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
356 for (; index != end; index += InstrSlots::NUM) {
357 // skip deleted instructions
358 while (index != end && !getInstructionFromIndex(index))
359 index += InstrSlots::NUM;
360 if (index == end) break;
361
362 MachineInstr *MI = getInstructionFromIndex(index);
363
364 RestartInstruction:
365 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
366 MachineOperand& mop = MI->getOperand(i);
367 if (mop.isRegister() && mop.getReg() == li.reg) {
368 if (DefIsReMat) {
369 // If this is the rematerializable definition MI itself and
370 // all of its uses are rematerialized, simply delete it.
371 if (MI == OrigDefMI) {
372 if (CanDelete) {
373 RemoveMachineInstrFromMaps(MI);
374 MI->eraseFromParent();
375 break;
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000376 } else if (tryFoldMemoryOperand(MI, vrm, index, i, true,
377 DefMI, slot, li.reg)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000378 // Folding the load/store can completely change the instruction
379 // in unpredictable ways, rescan it from the beginning.
380 goto RestartInstruction;
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000381 }
382 } else if (isLoad &&
383 tryFoldMemoryOperand(MI, vrm, index, i, isLoadSS,
384 DefMI, LdSlot, li.reg))
385 // Folding the load/store can completely change the
386 // instruction in unpredictable ways, rescan it from
387 // the beginning.
388 goto RestartInstruction;
Evan Cheng549f27d32007-08-13 23:45:17 +0000389 } else {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000390 if (tryFoldMemoryOperand(MI, vrm, index, i, true, DefMI,
391 slot, li.reg))
Evan Cheng549f27d32007-08-13 23:45:17 +0000392 // Folding the load/store can completely change the instruction in
393 // unpredictable ways, rescan it from the beginning.
394 goto RestartInstruction;
395 }
396
397 // Create a new virtual register for the spill interval.
398 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc);
399
400 // Scan all of the operands of this instruction rewriting operands
401 // to use NewVReg instead of li.reg as appropriate. We do this for
402 // two reasons:
403 //
404 // 1. If the instr reads the same spilled vreg multiple times, we
405 // want to reuse the NewVReg.
406 // 2. If the instr is a two-addr instruction, we are required to
407 // keep the src/dst regs pinned.
408 //
409 // Keep track of whether we replace a use and/or def so that we can
410 // create the spill interval with the appropriate range.
411 mop.setReg(NewVReg);
412
413 bool HasUse = mop.isUse();
414 bool HasDef = mop.isDef();
415 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Dan Gohman92dfe202007-09-14 20:33:02 +0000416 if (MI->getOperand(j).isRegister() &&
Evan Cheng549f27d32007-08-13 23:45:17 +0000417 MI->getOperand(j).getReg() == li.reg) {
418 MI->getOperand(j).setReg(NewVReg);
419 HasUse |= MI->getOperand(j).isUse();
420 HasDef |= MI->getOperand(j).isDef();
421 }
422 }
423
424 vrm.grow();
425 if (DefIsReMat) {
426 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000427 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000428 // Each valnum may have its own remat id.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000429 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000430 } else {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000431 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
Evan Cheng549f27d32007-08-13 23:45:17 +0000432 }
433 if (!CanDelete || (HasUse && HasDef)) {
434 // If this is a two-addr instruction then its use operands are
435 // rematerializable but its def is not. It should be assigned a
436 // stack slot.
437 vrm.assignVirt2StackSlot(NewVReg, slot);
438 }
439 } else {
440 vrm.assignVirt2StackSlot(NewVReg, slot);
441 }
442
443 // create a new register interval for this spill / remat.
444 LiveInterval &nI = getOrCreateInterval(NewVReg);
445 assert(nI.empty());
446
447 // the spill weight is now infinity as it
448 // cannot be spilled again
449 nI.weight = HUGE_VALF;
450
451 if (HasUse) {
Evan Cheng537d5c22007-10-08 06:59:30 +0000452 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Evan Chengf3bb2e62007-09-05 21:46:51 +0000453 nI.getNextValue(~0U, 0, VNInfoAllocator));
Evan Cheng549f27d32007-08-13 23:45:17 +0000454 DOUT << " +" << LR;
455 nI.addRange(LR);
456 }
457 if (HasDef) {
458 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Evan Chengf3bb2e62007-09-05 21:46:51 +0000459 nI.getNextValue(~0U, 0, VNInfoAllocator));
Evan Cheng549f27d32007-08-13 23:45:17 +0000460 DOUT << " +" << LR;
461 nI.addRange(LR);
462 }
463
464 added.push_back(&nI);
465
466 // update live variables if it is available
467 if (lv_)
468 lv_->addVirtualRegisterKilled(NewVReg, MI);
469
470 DOUT << "\t\t\t\tadded new interval: ";
471 nI.print(DOUT, mri_);
472 DOUT << '\n';
473 }
474 }
475 }
476 }
477
478 return added;
479}
480
481void LiveIntervals::printRegName(unsigned reg) const {
482 if (MRegisterInfo::isPhysicalRegister(reg))
483 cerr << mri_->getName(reg);
484 else
485 cerr << "%reg" << reg;
486}
487
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000488void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000489 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000490 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000491 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000492 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000493 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000494
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000495 // Virtual registers may be defined multiple times (due to phi
496 // elimination and 2-addr elimination). Much of what we do only has to be
497 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000498 // time we see a vreg.
499 if (interval.empty()) {
500 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000501 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000502 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000503 unsigned SrcReg, DstReg;
504 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000505 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000506 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000507 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000508
509 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000510
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511 // Loop over all of the blocks that the vreg is defined in. There are
512 // two cases we have to handle here. The most common case is a vreg
513 // whose lifetime is contained within a basic block. In this case there
514 // will be a single kill, in MBB, which comes after the definition.
515 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
516 // FIXME: what about dead vars?
517 unsigned killIdx;
518 if (vi.Kills[0] != mi)
519 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
520 else
521 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000522
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 // If the kill happens after the definition, we have an intra-block
524 // live range.
525 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000526 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000527 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000528 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000530 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000531 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000532 return;
533 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000534 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000535
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 // The other case we handle is when a virtual register lives to the end
537 // of the defining block, potentially live across some blocks, then is
538 // live into some number of blocks, but gets killed. Start by adding a
539 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000540 LiveRange NewLR(defIndex,
541 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000542 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000543 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 interval.addRange(NewLR);
545
546 // Iterate over all of the blocks that the variable is completely
547 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
548 // live interval.
549 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
550 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000551 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
552 if (!MBB->empty()) {
553 LiveRange LR(getMBBStartIdx(i),
554 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000555 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000556 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000557 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000558 }
559 }
560 }
561
562 // Finally, this virtual register is live from the start of any killing
563 // block to the 'use' slot of the killing instruction.
564 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
565 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000566 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000567 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000568 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000569 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000570 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000571 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000572 }
573
574 } else {
575 // If this is the second time we see a virtual register definition, it
576 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000577 // the result of two address elimination, then the vreg is one of the
578 // def-and-use register operand.
579 if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000580 // If this is a two-address definition, then we have already processed
581 // the live range. The only problem is that we didn't realize there
582 // are actually two values in the live interval. Because of this we
583 // need to take the LiveRegion that defines this register and split it
584 // into two values.
585 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000586 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000587
Evan Cheng4f8ff162007-08-11 00:59:19 +0000588 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000589 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000590 unsigned OldEnd = OldLR->end;
591
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000592 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000593 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000595
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000596 // Two-address vregs should always only be redefined once. This means
597 // that at this point, there should be exactly one value number in it.
598 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
599
Chris Lattner91725b72006-08-31 05:54:43 +0000600 // The new value number (#1) is defined by the instruction we claimed
601 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000602 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
603 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000604
Chris Lattner91725b72006-08-31 05:54:43 +0000605 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000606 OldValNo->def = RedefIndex;
607 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000608
609 // Add the new live interval which replaces the range for the input copy.
610 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000611 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000612 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000613 interval.addKill(ValNo, RedefIndex);
614 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615
616 // If this redefinition is dead, we need to add a dummy unit live
617 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000618 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000619 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000620
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000621 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000622 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000623
624 } else {
625 // Otherwise, this must be because of phi elimination. If this is the
626 // first redefinition of the vreg that we have seen, go back and change
627 // the live range in the PHI block to be a different value number.
628 if (interval.containsOneValue()) {
629 assert(vi.Kills.size() == 1 &&
630 "PHI elimination vreg should have one kill, the PHI itself!");
631
632 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000633 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000634 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000635 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000636 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000637 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000638 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000639 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000640 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000641 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000643 // Replace the interval with one of a NEW value number. Note that this
644 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000645 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000646 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000648 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000649 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 }
651
652 // In the case of PHI elimination, each variable definition is only
653 // live until the end of the block. We've already taken care of the
654 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000655 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000656
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000657 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000658 unsigned SrcReg, DstReg;
659 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000660 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000661 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000662 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000663
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000664 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000665 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000666 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000667 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000668 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 }
670 }
671
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000672 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000673}
674
Chris Lattnerf35fef72004-07-23 21:24:19 +0000675void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000676 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000677 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000678 LiveInterval &interval,
679 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 // A physical register cannot be live across basic block, so its
681 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000682 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000683
Chris Lattner6b128bd2006-09-03 08:07:11 +0000684 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000685 unsigned start = getDefIndex(baseIndex);
686 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000687
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000688 // If it is not used after definition, it is considered dead at
689 // the instruction defining it. Hence its interval is:
690 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000691 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000692 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000693 end = getDefIndex(start) + 1;
694 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000695 }
696
697 // If it is not dead on definition, it must be killed by a
698 // subsequent instruction. Hence its interval is:
699 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000700 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000702 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000703 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000704 end = getUseIndex(baseIndex) + 1;
705 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000706 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
707 // Another instruction redefines the register before it is ever read.
708 // Then the register is essentially dead at the instruction that defines
709 // it. Hence its interval is:
710 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000711 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000712 end = getDefIndex(start) + 1;
713 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000714 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000715 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000716
717 // The only case we should have a dead physreg here without a killing or
718 // instruction where we know it's dead is if it is live-in to the function
719 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000720 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000721 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000722
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000723exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000724 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000725
Evan Cheng24a3cc42007-04-25 07:30:23 +0000726 // Already exists? Extend old live interval.
727 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000728 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000729 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000730 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000731 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000732 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000733 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000734}
735
Chris Lattnerf35fef72004-07-23 21:24:19 +0000736void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
737 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000738 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000739 unsigned reg) {
740 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000741 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000742 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000743 unsigned SrcReg, DstReg;
744 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
745 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000746 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000747 // Def of a register also defines its sub-registers.
748 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
749 // Avoid processing some defs more than once.
750 if (!MI->findRegisterDefOperand(*AS))
751 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000752 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000753}
754
Evan Chengb371f452007-02-19 21:49:54 +0000755void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000756 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000757 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000758 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
759
760 // Look for kills, if it reaches a def before it's killed, then it shouldn't
761 // be considered a livein.
762 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000763 unsigned baseIndex = MIIdx;
764 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000765 unsigned end = start;
766 while (mi != MBB->end()) {
767 if (lv_->KillsRegister(mi, interval.reg)) {
768 DOUT << " killed";
769 end = getUseIndex(baseIndex) + 1;
770 goto exit;
771 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
772 // Another instruction redefines the register before it is ever read.
773 // Then the register is essentially dead at the instruction that defines
774 // it. Hence its interval is:
775 // [defSlot(def), defSlot(def)+1)
776 DOUT << " dead";
777 end = getDefIndex(start) + 1;
778 goto exit;
779 }
780
781 baseIndex += InstrSlots::NUM;
782 ++mi;
783 }
784
785exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000786 // Live-in register might not be used at all.
787 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000788 if (isAlias) {
789 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000790 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000791 } else {
792 DOUT << " live through";
793 end = baseIndex;
794 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000795 }
796
Evan Chengf3bb2e62007-09-05 21:46:51 +0000797 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000798 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000799 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000800 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000801}
802
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000803/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000804/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000805/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000806/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000807void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000808 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
809 << "********** Function: "
810 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000811 // Track the index of the current machine instr.
812 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000813 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
814 MBBI != E; ++MBBI) {
815 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000816 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000817
Chris Lattner428b92e2006-09-15 03:57:23 +0000818 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000819
Dan Gohmancb406c22007-10-03 19:26:29 +0000820 // Create intervals for live-ins to this BB first.
821 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
822 LE = MBB->livein_end(); LI != LE; ++LI) {
823 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
824 // Multiple live-ins can alias the same register.
825 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
826 if (!hasInterval(*AS))
827 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
828 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000829 }
830
Chris Lattner428b92e2006-09-15 03:57:23 +0000831 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000832 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000833
Evan Cheng438f7bc2006-11-10 08:43:01 +0000834 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000835 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
836 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000837 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000838 if (MO.isRegister() && MO.getReg() && MO.isDef())
839 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000840 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000841
842 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000843 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000844 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000845}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000846
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000847LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000848 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000849 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000850 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000851}