blob: 92c26346242f071beca4f6401999d83c2c8b97fe [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 Cheng4ca980e2007-10-17 02:10:22 +000065 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000066 mi2iMap_.clear();
67 i2miMap_.clear();
68 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000069 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
70 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000071 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
72 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000073}
74
Evan Cheng4ca980e2007-10-17 02:10:22 +000075namespace llvm {
76 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
77 return V < IM.first;
78 }
79
80 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
81 return IM.first < V;
82 }
83
84 struct Idx2MBBCompare {
85 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
86 return LHS.first < RHS.first;
87 }
88 };
89}
90
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091/// runOnMachineFunction - Register allocate the whole function
92///
93bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000094 mf_ = &fn;
95 tm_ = &fn.getTarget();
96 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000097 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000099 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000100
Chris Lattner428b92e2006-09-15 03:57:23 +0000101 // Number MachineInstrs and MachineBasicBlocks.
102 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000103 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000104
105 unsigned MIIndex = 0;
106 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
107 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000108 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000109
Chris Lattner428b92e2006-09-15 03:57:23 +0000110 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
111 I != E; ++I) {
112 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000114 i2miMap_.push_back(I);
115 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000116 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000117
118 // Set the MBB2IdxMap entry for this MBB.
119 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000120 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000121 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000122 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000123
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000124 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000125
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000126 numIntervals += getNumIntervals();
127
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000128 DOUT << "********** INTERVALS **********\n";
129 for (iterator I = begin(), E = end(); I != E; ++I) {
130 I->second.print(DOUT, mri_);
131 DOUT << "\n";
132 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000133
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000135 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000136 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000137}
138
Chris Lattner70ca3582004-09-30 15:59:17 +0000139/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000140void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000141 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000142 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000143 I->second.print(DOUT, mri_);
144 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000145 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000146
147 O << "********** MACHINEINSTRS **********\n";
148 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
149 mbbi != mbbe; ++mbbi) {
150 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
151 for (MachineBasicBlock::iterator mii = mbbi->begin(),
152 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000153 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000154 }
155 }
156}
157
Evan Cheng549f27d32007-08-13 23:45:17 +0000158/// isReMaterializable - Returns true if the definition MI of the specified
159/// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000160bool LiveIntervals::isReMaterializable(const LiveInterval &li,
161 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengbc165e42007-08-16 07:24:22 +0000162 if (DisableReMat)
163 return false;
164
Evan Cheng549f27d32007-08-13 23:45:17 +0000165 if (tii_->isTriviallyReMaterializable(MI))
166 return true;
167
168 int FrameIdx = 0;
169 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
170 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
171 return false;
172
173 // This is a load from fixed stack slot. It can be rematerialized unless it's
174 // re-defined by a two-address instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000175 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
176 i != e; ++i) {
177 const VNInfo *VNI = *i;
178 if (VNI == ValNo)
Evan Cheng549f27d32007-08-13 23:45:17 +0000179 continue;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000180 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000181 if (DefIdx == ~1U)
182 continue; // Dead val#.
183 MachineInstr *DefMI = (DefIdx == ~0u)
184 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000185 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
Evan Cheng549f27d32007-08-13 23:45:17 +0000186 return false;
187 }
188 return true;
189}
190
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000191/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
192/// slot / to reg or any rematerialized load into ith operand of specified
193/// MI. If it is successul, MI is updated with the newly created MI and
194/// returns true.
Evan Cheng549f27d32007-08-13 23:45:17 +0000195bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000196 MachineInstr *DefMI,
Evan Cheng549f27d32007-08-13 23:45:17 +0000197 unsigned index, unsigned i,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000198 bool isSS, int slot, unsigned reg) {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000199 MachineInstr *fmi = isSS
200 ? mri_->foldMemoryOperand(MI, i, slot)
201 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng549f27d32007-08-13 23:45:17 +0000202 if (fmi) {
203 // Attempt to fold the memory reference into the instruction. If
204 // we can do this, we don't need to insert spill code.
205 if (lv_)
206 lv_->instructionChanged(MI, fmi);
207 MachineBasicBlock &MBB = *MI->getParent();
208 vrm.virtFolded(reg, MI, i, fmi);
209 mi2iMap_.erase(MI);
210 i2miMap_[index/InstrSlots::NUM] = fmi;
211 mi2iMap_[fmi] = index;
212 MI = MBB.insert(MBB.erase(MI), fmi);
213 ++numFolded;
214 return true;
215 }
216 return false;
217}
218
219std::vector<LiveInterval*> LiveIntervals::
220addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
221 // since this is called after the analysis is done we don't know if
222 // LiveVariables is available
223 lv_ = getAnalysisToUpdate<LiveVariables>();
224
225 std::vector<LiveInterval*> added;
226
227 assert(li.weight != HUGE_VALF &&
228 "attempt to spill already spilled interval!");
229
230 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
231 li.print(DOUT, mri_);
232 DOUT << '\n';
233
Evan Cheng32dfbea2007-10-12 08:50:34 +0000234 SSARegMap *RegMap = mf_->getSSARegMap();
235 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000236
237 unsigned NumValNums = li.getNumValNums();
238 SmallVector<MachineInstr*, 4> ReMatDefs;
239 ReMatDefs.resize(NumValNums, NULL);
240 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
241 ReMatOrigDefs.resize(NumValNums, NULL);
242 SmallVector<int, 4> ReMatIds;
243 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
244 BitVector ReMatDelete(NumValNums);
245 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
246
247 bool NeedStackSlot = false;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000248 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
249 i != e; ++i) {
250 const VNInfo *VNI = *i;
251 unsigned VN = VNI->id;
252 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000253 if (DefIdx == ~1U)
254 continue; // Dead val#.
255 // Is the def for the val# rematerializable?
256 MachineInstr *DefMI = (DefIdx == ~0u)
257 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000258 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000259 // Remember how to remat the def of this val#.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 ReMatOrigDefs[VN] = DefMI;
Evan Cheng549f27d32007-08-13 23:45:17 +0000261 // Original def may be modified so we have to make a copy here. vrm must
262 // delete these!
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000263 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng549f27d32007-08-13 23:45:17 +0000264 vrm.setVirtIsReMaterialized(reg, DefMI);
265
266 bool CanDelete = true;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000267 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
268 unsigned KillIdx = VNI->kills[j];
Evan Cheng549f27d32007-08-13 23:45:17 +0000269 MachineInstr *KillMI = (KillIdx & 1)
270 ? NULL : getInstructionFromIndex(KillIdx);
271 // Kill is a phi node, not all of its uses can be rematerialized.
272 // It must not be deleted.
273 if (!KillMI) {
274 CanDelete = false;
275 // Need a stack slot if there is any live range where uses cannot be
276 // rematerialized.
277 NeedStackSlot = true;
278 break;
279 }
280 }
281
282 if (CanDelete)
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000283 ReMatDelete.set(VN);
Evan Cheng549f27d32007-08-13 23:45:17 +0000284 } else {
285 // Need a stack slot if there is any live range where uses cannot be
286 // rematerialized.
287 NeedStackSlot = true;
288 }
289 }
290
291 // One stack slot per live interval.
292 if (NeedStackSlot)
293 slot = vrm.assignVirt2StackSlot(reg);
294
295 for (LiveInterval::Ranges::const_iterator
296 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000297 MachineInstr *DefMI = ReMatDefs[I->valno->id];
298 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000299 bool DefIsReMat = DefMI != NULL;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000300 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000301 int LdSlot = 0;
302 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000303 bool isLoad = isLoadSS ||
304 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng549f27d32007-08-13 23:45:17 +0000305 unsigned index = getBaseIndex(I->start);
306 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
307 for (; index != end; index += InstrSlots::NUM) {
308 // skip deleted instructions
309 while (index != end && !getInstructionFromIndex(index))
310 index += InstrSlots::NUM;
311 if (index == end) break;
312
313 MachineInstr *MI = getInstructionFromIndex(index);
314
315 RestartInstruction:
316 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
317 MachineOperand& mop = MI->getOperand(i);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000318 if (!mop.isRegister())
319 continue;
320 unsigned Reg = mop.getReg();
321 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
322 continue;
323 bool isSubReg = RegMap->isSubRegister(Reg);
324 unsigned SubIdx = 0;
325 if (isSubReg) {
326 SubIdx = RegMap->getSubRegisterIndex(Reg);
327 Reg = RegMap->getSuperRegister(Reg);
328 }
329 if (Reg != li.reg)
330 continue;
331
332 bool TryFold = !DefIsReMat;
333 bool FoldSS = true;
334 int FoldSlot = slot;
335 if (DefIsReMat) {
336 // If this is the rematerializable definition MI itself and
337 // all of its uses are rematerialized, simply delete it.
338 if (MI == OrigDefMI && CanDelete) {
339 RemoveMachineInstrFromMaps(MI);
340 MI->eraseFromParent();
341 break;
Evan Cheng549f27d32007-08-13 23:45:17 +0000342 }
343
Evan Cheng32dfbea2007-10-12 08:50:34 +0000344 // If def for this use can't be rematerialized, then try folding.
345 TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
346 if (isLoad) {
347 // Try fold loads (from stack slot, constant pool, etc.) into uses.
348 FoldSS = isLoadSS;
349 FoldSlot = LdSlot;
Evan Cheng549f27d32007-08-13 23:45:17 +0000350 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000351 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000352
Evan Cheng32dfbea2007-10-12 08:50:34 +0000353 // FIXME: fold subreg use
354 if (!isSubReg && TryFold &&
355 tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
356 // Folding the load/store can completely change the instruction in
357 // unpredictable ways, rescan it from the beginning.
358 goto RestartInstruction;
359
360 // Create a new virtual register for the spill interval.
361 unsigned NewVReg = RegMap->createVirtualRegister(rc);
362 if (isSubReg)
363 RegMap->setIsSubRegister(NewVReg, NewVReg, SubIdx);
364
365 // Scan all of the operands of this instruction rewriting operands
366 // to use NewVReg instead of li.reg as appropriate. We do this for
367 // two reasons:
368 //
369 // 1. If the instr reads the same spilled vreg multiple times, we
370 // want to reuse the NewVReg.
371 // 2. If the instr is a two-addr instruction, we are required to
372 // keep the src/dst regs pinned.
373 //
374 // Keep track of whether we replace a use and/or def so that we can
375 // create the spill interval with the appropriate range.
376 mop.setReg(NewVReg);
377
378 bool HasUse = mop.isUse();
379 bool HasDef = mop.isDef();
380 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
381 if (MI->getOperand(j).isRegister() &&
382 MI->getOperand(j).getReg() == li.reg) {
383 MI->getOperand(j).setReg(NewVReg);
384 HasUse |= MI->getOperand(j).isUse();
385 HasDef |= MI->getOperand(j).isDef();
386 }
387 }
388
389 vrm.grow();
390 if (DefIsReMat) {
391 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
392 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
393 // Each valnum may have its own remat id.
394 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000395 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000396 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
397 }
398 if (!CanDelete || (HasUse && HasDef)) {
399 // If this is a two-addr instruction then its use operands are
400 // rematerializable but its def is not. It should be assigned a
401 // stack slot.
Evan Cheng549f27d32007-08-13 23:45:17 +0000402 vrm.assignVirt2StackSlot(NewVReg, slot);
403 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000404 } else {
405 vrm.assignVirt2StackSlot(NewVReg, slot);
Evan Cheng549f27d32007-08-13 23:45:17 +0000406 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000407
408 // create a new register interval for this spill / remat.
409 LiveInterval &nI = getOrCreateInterval(NewVReg);
410 assert(nI.empty());
411
412 // the spill weight is now infinity as it
413 // cannot be spilled again
414 nI.weight = HUGE_VALF;
415
416 if (HasUse) {
417 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
418 nI.getNextValue(~0U, 0, VNInfoAllocator));
419 DOUT << " +" << LR;
420 nI.addRange(LR);
421 }
422 if (HasDef) {
423 LiveRange LR(getDefIndex(index), getStoreIndex(index),
424 nI.getNextValue(~0U, 0, VNInfoAllocator));
425 DOUT << " +" << LR;
426 nI.addRange(LR);
427 }
428
429 added.push_back(&nI);
430
431 // update live variables if it is available
432 if (lv_)
433 lv_->addVirtualRegisterKilled(NewVReg, MI);
434
435 DOUT << "\t\t\t\tadded new interval: ";
436 nI.print(DOUT, mri_);
437 DOUT << '\n';
Evan Cheng549f27d32007-08-13 23:45:17 +0000438 }
439 }
440 }
441
442 return added;
443}
444
Evan Chengc92da382007-11-03 07:20:12 +0000445/// conflictsWithPhysRegDef - Returns true if the specified register
446/// is defined during the duration of the specified interval.
447bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
448 VirtRegMap &vrm, unsigned reg) {
449 for (LiveInterval::Ranges::const_iterator
450 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
451 for (unsigned index = getBaseIndex(I->start),
452 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
453 index += InstrSlots::NUM) {
454 // skip deleted instructions
455 while (index != end && !getInstructionFromIndex(index))
456 index += InstrSlots::NUM;
457 if (index == end) break;
458
459 MachineInstr *MI = getInstructionFromIndex(index);
460 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
461 MachineOperand& mop = MI->getOperand(i);
462 if (!mop.isRegister() || !mop.isDef())
463 continue;
464 unsigned PhysReg = mop.getReg();
465 if (PhysReg == 0)
466 continue;
467 if (MRegisterInfo::isVirtualRegister(PhysReg))
468 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5f5f3b62007-11-05 00:59:10 +0000469 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000470 return true;
471 }
472 }
473 }
474
475 return false;
476}
477
Evan Cheng549f27d32007-08-13 23:45:17 +0000478void LiveIntervals::printRegName(unsigned reg) const {
479 if (MRegisterInfo::isPhysicalRegister(reg))
480 cerr << mri_->getName(reg);
481 else
482 cerr << "%reg" << reg;
483}
484
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000485void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000486 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000487 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000488 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000489 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000490 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000491
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000492 // Virtual registers may be defined multiple times (due to phi
493 // elimination and 2-addr elimination). Much of what we do only has to be
494 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000495 // time we see a vreg.
496 if (interval.empty()) {
497 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000498 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000499 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000500 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000501 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000502 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng48ff2822007-10-12 17:16:50 +0000503 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000504 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
505 VNInfoAllocator);
506 else
507 ValNo = interval.getNextValue(defIndex, 0, 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.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000579 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
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;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000659 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000660 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000661 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
662 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
663 VNInfoAllocator);
664 else
665 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000666
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000667 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000668 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000670 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000671 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000672 }
673 }
674
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000675 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000676}
677
Chris Lattnerf35fef72004-07-23 21:24:19 +0000678void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000679 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000680 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000681 LiveInterval &interval,
682 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 // A physical register cannot be live across basic block, so its
684 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000685 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000686
Chris Lattner6b128bd2006-09-03 08:07:11 +0000687 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000688 unsigned start = getDefIndex(baseIndex);
689 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000690
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000691 // If it is not used after definition, it is considered dead at
692 // the instruction defining it. Hence its interval is:
693 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000694 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000695 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000696 end = getDefIndex(start) + 1;
697 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000698 }
699
700 // If it is not dead on definition, it must be killed by a
701 // subsequent instruction. Hence its interval is:
702 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000703 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000705 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000706 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000707 end = getUseIndex(baseIndex) + 1;
708 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000709 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
710 // Another instruction redefines the register before it is ever read.
711 // Then the register is essentially dead at the instruction that defines
712 // it. Hence its interval is:
713 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000714 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000715 end = getDefIndex(start) + 1;
716 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000717 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000718 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000719
720 // The only case we should have a dead physreg here without a killing or
721 // instruction where we know it's dead is if it is live-in to the function
722 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000723 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000724 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000725
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000726exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000728
Evan Cheng24a3cc42007-04-25 07:30:23 +0000729 // Already exists? Extend old live interval.
730 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000731 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000732 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000733 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000735 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000736 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000737}
738
Chris Lattnerf35fef72004-07-23 21:24:19 +0000739void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
740 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000741 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000742 unsigned reg) {
743 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000744 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000745 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000746 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000747 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
748 SrcReg = MI->getOperand(1).getReg();
749 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000750 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000751 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000752 // Def of a register also defines its sub-registers.
753 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
754 // Avoid processing some defs more than once.
755 if (!MI->findRegisterDefOperand(*AS))
756 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000757 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000758}
759
Evan Chengb371f452007-02-19 21:49:54 +0000760void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000761 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000762 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000763 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
764
765 // Look for kills, if it reaches a def before it's killed, then it shouldn't
766 // be considered a livein.
767 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000768 unsigned baseIndex = MIIdx;
769 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000770 unsigned end = start;
771 while (mi != MBB->end()) {
772 if (lv_->KillsRegister(mi, interval.reg)) {
773 DOUT << " killed";
774 end = getUseIndex(baseIndex) + 1;
775 goto exit;
776 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
777 // Another instruction redefines the register before it is ever read.
778 // Then the register is essentially dead at the instruction that defines
779 // it. Hence its interval is:
780 // [defSlot(def), defSlot(def)+1)
781 DOUT << " dead";
782 end = getDefIndex(start) + 1;
783 goto exit;
784 }
785
786 baseIndex += InstrSlots::NUM;
787 ++mi;
788 }
789
790exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000791 // Live-in register might not be used at all.
792 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000793 if (isAlias) {
794 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000795 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000796 } else {
797 DOUT << " live through";
798 end = baseIndex;
799 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000800 }
801
Evan Chengf3bb2e62007-09-05 21:46:51 +0000802 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000803 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000804 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000805 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000806}
807
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000808/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000809/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000810/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000811/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000812void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000813 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
814 << "********** Function: "
815 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000816 // Track the index of the current machine instr.
817 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000818 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
819 MBBI != E; ++MBBI) {
820 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000821 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000822
Chris Lattner428b92e2006-09-15 03:57:23 +0000823 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000824
Dan Gohmancb406c22007-10-03 19:26:29 +0000825 // Create intervals for live-ins to this BB first.
826 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
827 LE = MBB->livein_end(); LI != LE; ++LI) {
828 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
829 // Multiple live-ins can alias the same register.
830 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
831 if (!hasInterval(*AS))
832 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
833 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000834 }
835
Chris Lattner428b92e2006-09-15 03:57:23 +0000836 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000837 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000838
Evan Cheng438f7bc2006-11-10 08:43:01 +0000839 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000840 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
841 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000842 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000843 if (MO.isRegister() && MO.getReg() && MO.isDef())
844 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000845 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000846
847 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000848 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000849 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000850}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000851
Evan Cheng4ca980e2007-10-17 02:10:22 +0000852bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000853 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000854 std::vector<IdxMBBPair>::const_iterator I =
855 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
856
857 bool ResVal = false;
858 while (I != Idx2MBBMap.end()) {
859 if (LR.end <= I->first)
860 break;
861 MBBs.push_back(I->second);
862 ResVal = true;
863 ++I;
864 }
865 return ResVal;
866}
867
868
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000869LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000870 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000871 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000872 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000873}