blob: 5c4697850aeeea64e61340f433da2bca14921596 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
2//
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"
19#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "VirtRegMap.h"
21#include "llvm/Value.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/MRegisterInfo.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/STLExtras.h"
34#include <algorithm>
35#include <cmath>
36using namespace llvm;
37
Evan Chengafc07f82007-08-16 07:24:22 +000038namespace {
39 // Hidden options for help debugging.
40 cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
42}
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044STATISTIC(numIntervals, "Number of original intervals");
45STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
46STATISTIC(numFolded , "Number of loads/stores folded into instructions");
47
48char LiveIntervals::ID = 0;
49namespace {
50 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
51}
52
53void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.addPreserved<LiveVariables>();
55 AU.addRequired<LiveVariables>();
56 AU.addPreservedID(PHIEliminationID);
57 AU.addRequiredID(PHIEliminationID);
58 AU.addRequiredID(TwoAddressInstructionPassID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 MachineFunctionPass::getAnalysisUsage(AU);
60}
61
62void LiveIntervals::releaseMemory() {
Evan Cheng94262e42007-10-17 02:10:22 +000063 Idx2MBBMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 mi2iMap_.clear();
65 i2miMap_.clear();
66 r2iMap_.clear();
Evan Cheng27344d42007-09-06 01:07:24 +000067 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
68 VNInfoAllocator.Reset();
Evan Cheng1204d172007-08-13 23:45:17 +000069 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
70 delete ClonedMIs[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071}
72
Evan Cheng94262e42007-10-17 02:10:22 +000073namespace llvm {
74 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
75 return V < IM.first;
76 }
77
78 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
79 return IM.first < V;
80 }
81
82 struct Idx2MBBCompare {
83 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
84 return LHS.first < RHS.first;
85 }
86 };
87}
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089/// runOnMachineFunction - Register allocate the whole function
90///
91bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
92 mf_ = &fn;
93 tm_ = &fn.getTarget();
94 mri_ = tm_->getRegisterInfo();
95 tii_ = tm_->getInstrInfo();
96 lv_ = &getAnalysis<LiveVariables>();
97 allocatableRegs_ = mri_->getAllocatableSet(fn);
98
99 // Number MachineInstrs and MachineBasicBlocks.
100 // Initialize MBB indexes to a sentinal.
Evan Cheng1204d172007-08-13 23:45:17 +0000101 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 unsigned MIIndex = 0;
104 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
105 MBB != E; ++MBB) {
Evan Cheng1204d172007-08-13 23:45:17 +0000106 unsigned StartIdx = MIIndex;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
109 I != E; ++I) {
110 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
111 assert(inserted && "multiple MachineInstr -> index mappings");
112 i2miMap_.push_back(I);
113 MIIndex += InstrSlots::NUM;
114 }
Evan Cheng1204d172007-08-13 23:45:17 +0000115
116 // Set the MBB2IdxMap entry for this MBB.
117 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng94262e42007-10-17 02:10:22 +0000118 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 }
Evan Cheng94262e42007-10-17 02:10:22 +0000120 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 computeIntervals();
123
124 numIntervals += getNumIntervals();
125
126 DOUT << "********** INTERVALS **********\n";
127 for (iterator I = begin(), E = end(); I != E; ++I) {
128 I->second.print(DOUT, mri_);
129 DOUT << "\n";
130 }
131
132 numIntervalsAfter += getNumIntervals();
133 DEBUG(dump());
134 return true;
135}
136
137/// print - Implement the dump method.
138void LiveIntervals::print(std::ostream &O, const Module* ) const {
139 O << "********** INTERVALS **********\n";
140 for (const_iterator I = begin(), E = end(); I != E; ++I) {
141 I->second.print(DOUT, mri_);
142 DOUT << "\n";
143 }
144
145 O << "********** MACHINEINSTRS **********\n";
146 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
147 mbbi != mbbe; ++mbbi) {
148 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
149 for (MachineBasicBlock::iterator mii = mbbi->begin(),
150 mie = mbbi->end(); mii != mie; ++mii) {
151 O << getInstructionIndex(mii) << '\t' << *mii;
152 }
153 }
154}
155
Evan Cheng1204d172007-08-13 23:45:17 +0000156/// isReMaterializable - Returns true if the definition MI of the specified
157/// val# of the specified interval is re-materializable.
Evan Cheng983b81d2007-08-29 20:45:00 +0000158bool LiveIntervals::isReMaterializable(const LiveInterval &li,
159 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengafc07f82007-08-16 07:24:22 +0000160 if (DisableReMat)
161 return false;
162
Evan Cheng1204d172007-08-13 23:45:17 +0000163 if (tii_->isTriviallyReMaterializable(MI))
164 return true;
165
166 int FrameIdx = 0;
167 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
168 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
169 return false;
170
171 // This is a load from fixed stack slot. It can be rematerialized unless it's
172 // re-defined by a two-address instruction.
Evan Cheng983b81d2007-08-29 20:45:00 +0000173 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
174 i != e; ++i) {
175 const VNInfo *VNI = *i;
176 if (VNI == ValNo)
Evan Cheng1204d172007-08-13 23:45:17 +0000177 continue;
Evan Cheng983b81d2007-08-29 20:45:00 +0000178 unsigned DefIdx = VNI->def;
Evan Cheng1204d172007-08-13 23:45:17 +0000179 if (DefIdx == ~1U)
180 continue; // Dead val#.
181 MachineInstr *DefMI = (DefIdx == ~0u)
182 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng687d1082007-10-12 08:50:34 +0000183 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
Evan Cheng1204d172007-08-13 23:45:17 +0000184 return false;
185 }
186 return true;
187}
188
Evan Cheng03225432007-08-30 05:53:02 +0000189/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
190/// slot / to reg or any rematerialized load into ith operand of specified
191/// MI. If it is successul, MI is updated with the newly created MI and
192/// returns true.
Evan Cheng1204d172007-08-13 23:45:17 +0000193bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng687d1082007-10-12 08:50:34 +0000194 MachineInstr *DefMI,
Evan Cheng1204d172007-08-13 23:45:17 +0000195 unsigned index, unsigned i,
Evan Cheng687d1082007-10-12 08:50:34 +0000196 bool isSS, int slot, unsigned reg) {
Evan Cheng03225432007-08-30 05:53:02 +0000197 MachineInstr *fmi = isSS
198 ? mri_->foldMemoryOperand(MI, i, slot)
199 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng1204d172007-08-13 23:45:17 +0000200 if (fmi) {
201 // Attempt to fold the memory reference into the instruction. If
202 // we can do this, we don't need to insert spill code.
203 if (lv_)
204 lv_->instructionChanged(MI, fmi);
205 MachineBasicBlock &MBB = *MI->getParent();
206 vrm.virtFolded(reg, MI, i, fmi);
207 mi2iMap_.erase(MI);
208 i2miMap_[index/InstrSlots::NUM] = fmi;
209 mi2iMap_[fmi] = index;
210 MI = MBB.insert(MBB.erase(MI), fmi);
211 ++numFolded;
212 return true;
213 }
214 return false;
215}
216
217std::vector<LiveInterval*> LiveIntervals::
218addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
219 // since this is called after the analysis is done we don't know if
220 // LiveVariables is available
221 lv_ = getAnalysisToUpdate<LiveVariables>();
222
223 std::vector<LiveInterval*> added;
224
225 assert(li.weight != HUGE_VALF &&
226 "attempt to spill already spilled interval!");
227
228 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
229 li.print(DOUT, mri_);
230 DOUT << '\n';
231
Evan Cheng687d1082007-10-12 08:50:34 +0000232 SSARegMap *RegMap = mf_->getSSARegMap();
233 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
Evan Cheng1204d172007-08-13 23:45:17 +0000234
235 unsigned NumValNums = li.getNumValNums();
236 SmallVector<MachineInstr*, 4> ReMatDefs;
237 ReMatDefs.resize(NumValNums, NULL);
238 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
239 ReMatOrigDefs.resize(NumValNums, NULL);
240 SmallVector<int, 4> ReMatIds;
241 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
242 BitVector ReMatDelete(NumValNums);
243 unsigned slot = VirtRegMap::MAX_STACK_SLOT;
244
245 bool NeedStackSlot = false;
Evan Cheng983b81d2007-08-29 20:45:00 +0000246 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
247 i != e; ++i) {
248 const VNInfo *VNI = *i;
249 unsigned VN = VNI->id;
250 unsigned DefIdx = VNI->def;
Evan Cheng1204d172007-08-13 23:45:17 +0000251 if (DefIdx == ~1U)
252 continue; // Dead val#.
253 // Is the def for the val# rematerializable?
254 MachineInstr *DefMI = (DefIdx == ~0u)
255 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng983b81d2007-08-29 20:45:00 +0000256 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng1204d172007-08-13 23:45:17 +0000257 // Remember how to remat the def of this val#.
Evan Cheng983b81d2007-08-29 20:45:00 +0000258 ReMatOrigDefs[VN] = DefMI;
Evan Cheng1204d172007-08-13 23:45:17 +0000259 // Original def may be modified so we have to make a copy here. vrm must
260 // delete these!
Evan Cheng983b81d2007-08-29 20:45:00 +0000261 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng1204d172007-08-13 23:45:17 +0000262 vrm.setVirtIsReMaterialized(reg, DefMI);
263
264 bool CanDelete = true;
Evan Cheng983b81d2007-08-29 20:45:00 +0000265 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
266 unsigned KillIdx = VNI->kills[j];
Evan Cheng1204d172007-08-13 23:45:17 +0000267 MachineInstr *KillMI = (KillIdx & 1)
268 ? NULL : getInstructionFromIndex(KillIdx);
269 // Kill is a phi node, not all of its uses can be rematerialized.
270 // It must not be deleted.
271 if (!KillMI) {
272 CanDelete = false;
273 // Need a stack slot if there is any live range where uses cannot be
274 // rematerialized.
275 NeedStackSlot = true;
276 break;
277 }
278 }
279
280 if (CanDelete)
Evan Cheng983b81d2007-08-29 20:45:00 +0000281 ReMatDelete.set(VN);
Evan Cheng1204d172007-08-13 23:45:17 +0000282 } else {
283 // Need a stack slot if there is any live range where uses cannot be
284 // rematerialized.
285 NeedStackSlot = true;
286 }
287 }
288
289 // One stack slot per live interval.
290 if (NeedStackSlot)
291 slot = vrm.assignVirt2StackSlot(reg);
292
293 for (LiveInterval::Ranges::const_iterator
294 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng983b81d2007-08-29 20:45:00 +0000295 MachineInstr *DefMI = ReMatDefs[I->valno->id];
296 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng1204d172007-08-13 23:45:17 +0000297 bool DefIsReMat = DefMI != NULL;
Evan Cheng983b81d2007-08-29 20:45:00 +0000298 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng1204d172007-08-13 23:45:17 +0000299 int LdSlot = 0;
300 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng03225432007-08-30 05:53:02 +0000301 bool isLoad = isLoadSS ||
302 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng1204d172007-08-13 23:45:17 +0000303 unsigned index = getBaseIndex(I->start);
304 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
305 for (; index != end; index += InstrSlots::NUM) {
306 // skip deleted instructions
307 while (index != end && !getInstructionFromIndex(index))
308 index += InstrSlots::NUM;
309 if (index == end) break;
310
311 MachineInstr *MI = getInstructionFromIndex(index);
312
313 RestartInstruction:
314 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
315 MachineOperand& mop = MI->getOperand(i);
Evan Cheng687d1082007-10-12 08:50:34 +0000316 if (!mop.isRegister())
317 continue;
318 unsigned Reg = mop.getReg();
Evan Chenga5e40242007-11-07 08:08:25 +0000319 unsigned RegI = Reg;
Evan Cheng687d1082007-10-12 08:50:34 +0000320 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
321 continue;
322 bool isSubReg = RegMap->isSubRegister(Reg);
323 unsigned SubIdx = 0;
324 if (isSubReg) {
325 SubIdx = RegMap->getSubRegisterIndex(Reg);
326 Reg = RegMap->getSuperRegister(Reg);
327 }
328 if (Reg != li.reg)
329 continue;
330
331 bool TryFold = !DefIsReMat;
332 bool FoldSS = true;
333 int FoldSlot = slot;
334 if (DefIsReMat) {
335 // If this is the rematerializable definition MI itself and
336 // all of its uses are rematerialized, simply delete it.
337 if (MI == OrigDefMI && CanDelete) {
338 RemoveMachineInstrFromMaps(MI);
339 MI->eraseFromParent();
340 break;
Evan Cheng1204d172007-08-13 23:45:17 +0000341 }
342
Evan Cheng687d1082007-10-12 08:50:34 +0000343 // If def for this use can't be rematerialized, then try folding.
344 TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
345 if (isLoad) {
346 // Try fold loads (from stack slot, constant pool, etc.) into uses.
347 FoldSS = isLoadSS;
348 FoldSlot = LdSlot;
Evan Cheng1204d172007-08-13 23:45:17 +0000349 }
Evan Cheng687d1082007-10-12 08:50:34 +0000350 }
Evan Cheng1204d172007-08-13 23:45:17 +0000351
Evan Cheng687d1082007-10-12 08:50:34 +0000352 // FIXME: fold subreg use
353 if (!isSubReg && TryFold &&
354 tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
355 // Folding the load/store can completely change the instruction in
356 // unpredictable ways, rescan it from the beginning.
357 goto RestartInstruction;
358
359 // Create a new virtual register for the spill interval.
360 unsigned NewVReg = RegMap->createVirtualRegister(rc);
361 if (isSubReg)
362 RegMap->setIsSubRegister(NewVReg, NewVReg, SubIdx);
363
364 // Scan all of the operands of this instruction rewriting operands
365 // to use NewVReg instead of li.reg as appropriate. We do this for
366 // two reasons:
367 //
368 // 1. If the instr reads the same spilled vreg multiple times, we
369 // want to reuse the NewVReg.
370 // 2. If the instr is a two-addr instruction, we are required to
371 // keep the src/dst regs pinned.
372 //
373 // Keep track of whether we replace a use and/or def so that we can
374 // create the spill interval with the appropriate range.
375 mop.setReg(NewVReg);
376
377 bool HasUse = mop.isUse();
378 bool HasDef = mop.isDef();
379 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Cheng9a9614f2007-11-06 08:50:44 +0000380 if (!MI->getOperand(j).isRegister())
381 continue;
382 unsigned RegJ = MI->getOperand(j).getReg();
Evan Cheng945923c2007-11-06 21:12:10 +0000383 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
384 continue;
Evan Chenga5e40242007-11-07 08:08:25 +0000385 if (RegJ == RegI) {
Evan Cheng687d1082007-10-12 08:50:34 +0000386 MI->getOperand(j).setReg(NewVReg);
387 HasUse |= MI->getOperand(j).isUse();
388 HasDef |= MI->getOperand(j).isDef();
389 }
390 }
391
392 vrm.grow();
393 if (DefIsReMat) {
394 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
395 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
396 // Each valnum may have its own remat id.
397 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng1204d172007-08-13 23:45:17 +0000398 } else {
Evan Cheng687d1082007-10-12 08:50:34 +0000399 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
400 }
401 if (!CanDelete || (HasUse && HasDef)) {
402 // If this is a two-addr instruction then its use operands are
403 // rematerializable but its def is not. It should be assigned a
404 // stack slot.
Evan Cheng1204d172007-08-13 23:45:17 +0000405 vrm.assignVirt2StackSlot(NewVReg, slot);
406 }
Evan Cheng687d1082007-10-12 08:50:34 +0000407 } else {
408 vrm.assignVirt2StackSlot(NewVReg, slot);
Evan Cheng1204d172007-08-13 23:45:17 +0000409 }
Evan Cheng687d1082007-10-12 08:50:34 +0000410
411 // create a new register interval for this spill / remat.
412 LiveInterval &nI = getOrCreateInterval(NewVReg);
413 assert(nI.empty());
414
415 // the spill weight is now infinity as it
416 // cannot be spilled again
417 nI.weight = HUGE_VALF;
418
419 if (HasUse) {
420 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
421 nI.getNextValue(~0U, 0, VNInfoAllocator));
422 DOUT << " +" << LR;
423 nI.addRange(LR);
424 }
425 if (HasDef) {
426 LiveRange LR(getDefIndex(index), getStoreIndex(index),
427 nI.getNextValue(~0U, 0, VNInfoAllocator));
428 DOUT << " +" << LR;
429 nI.addRange(LR);
430 }
431
432 added.push_back(&nI);
433
434 // update live variables if it is available
435 if (lv_)
436 lv_->addVirtualRegisterKilled(NewVReg, MI);
437
438 DOUT << "\t\t\t\tadded new interval: ";
439 nI.print(DOUT, mri_);
440 DOUT << '\n';
Evan Cheng1204d172007-08-13 23:45:17 +0000441 }
442 }
443 }
444
445 return added;
446}
447
Evan Chengc4c75f52007-11-03 07:20:12 +0000448/// conflictsWithPhysRegDef - Returns true if the specified register
449/// is defined during the duration of the specified interval.
450bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
451 VirtRegMap &vrm, unsigned reg) {
452 for (LiveInterval::Ranges::const_iterator
453 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
454 for (unsigned index = getBaseIndex(I->start),
455 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
456 index += InstrSlots::NUM) {
457 // skip deleted instructions
458 while (index != end && !getInstructionFromIndex(index))
459 index += InstrSlots::NUM;
460 if (index == end) break;
461
462 MachineInstr *MI = getInstructionFromIndex(index);
463 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
464 MachineOperand& mop = MI->getOperand(i);
465 if (!mop.isRegister() || !mop.isDef())
466 continue;
467 unsigned PhysReg = mop.getReg();
468 if (PhysReg == 0)
469 continue;
470 if (MRegisterInfo::isVirtualRegister(PhysReg))
471 PhysReg = vrm.getPhys(PhysReg);
Evan Chengccfa6922007-11-05 00:59:10 +0000472 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc4c75f52007-11-03 07:20:12 +0000473 return true;
474 }
475 }
476 }
477
478 return false;
479}
480
Evan Cheng1204d172007-08-13 23:45:17 +0000481void LiveIntervals::printRegName(unsigned reg) const {
482 if (MRegisterInfo::isPhysicalRegister(reg))
483 cerr << mri_->getName(reg);
484 else
485 cerr << "%reg" << reg;
486}
487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
489 MachineBasicBlock::iterator mi,
490 unsigned MIIdx,
491 LiveInterval &interval) {
492 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
493 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
494
495 // 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
498 // time we see a vreg.
499 if (interval.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 // Get the Idx of the defining instructions.
501 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng983b81d2007-08-29 20:45:00 +0000502 VNInfo *ValNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 unsigned SrcReg, DstReg;
Evan Cheng687d1082007-10-12 08:50:34 +0000504 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Cheng319802c2007-09-05 21:46:51 +0000505 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng982f2512007-10-12 17:16:50 +0000506 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng687d1082007-10-12 08:50:34 +0000507 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
508 VNInfoAllocator);
509 else
510 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng983b81d2007-08-29 20:45:00 +0000511
512 assert(ValNo->id == 0 && "First value in interval is not 0?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513
514 // Loop over all of the blocks that the vreg is defined in. There are
515 // two cases we have to handle here. The most common case is a vreg
516 // whose lifetime is contained within a basic block. In this case there
517 // will be a single kill, in MBB, which comes after the definition.
518 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
519 // FIXME: what about dead vars?
520 unsigned killIdx;
521 if (vi.Kills[0] != mi)
522 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
523 else
524 killIdx = defIndex+1;
525
526 // If the kill happens after the definition, we have an intra-block
527 // live range.
528 if (killIdx > defIndex) {
529 assert(vi.AliveBlocks.none() &&
530 "Shouldn't be alive across any blocks!");
Evan Cheng983b81d2007-08-29 20:45:00 +0000531 LiveRange LR(defIndex, killIdx, ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532 interval.addRange(LR);
533 DOUT << " +" << LR << "\n";
Evan Cheng319802c2007-09-05 21:46:51 +0000534 interval.addKill(ValNo, killIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 return;
536 }
537 }
538
539 // The other case we handle is when a virtual register lives to the end
540 // of the defining block, potentially live across some blocks, then is
541 // live into some number of blocks, but gets killed. Start by adding a
542 // range that goes from this definition to the end of the defining block.
543 LiveRange NewLR(defIndex,
544 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng983b81d2007-08-29 20:45:00 +0000545 ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 DOUT << " +" << NewLR;
547 interval.addRange(NewLR);
548
549 // Iterate over all of the blocks that the variable is completely
550 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
551 // live interval.
552 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
553 if (vi.AliveBlocks[i]) {
554 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
555 if (!MBB->empty()) {
556 LiveRange LR(getMBBStartIdx(i),
557 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng983b81d2007-08-29 20:45:00 +0000558 ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 interval.addRange(LR);
560 DOUT << " +" << LR;
561 }
562 }
563 }
564
565 // Finally, this virtual register is live from the start of any killing
566 // block to the 'use' slot of the killing instruction.
567 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
568 MachineInstr *Kill = vi.Kills[i];
Evan Cheng58c2b762007-08-08 03:00:28 +0000569 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng983b81d2007-08-29 20:45:00 +0000571 killIdx, ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000573 interval.addKill(ValNo, killIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 DOUT << " +" << LR;
575 }
576
577 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 // If this is the second time we see a virtual register definition, it
579 // must be due to phi elimination or two addr elimination. If this is
580 // the result of two address elimination, then the vreg is one of the
581 // def-and-use register operand.
Evan Cheng687d1082007-10-12 08:50:34 +0000582 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 // If this is a two-address definition, then we have already processed
584 // the live range. The only problem is that we didn't realize there
585 // are actually two values in the live interval. Because of this we
586 // need to take the LiveRegion that defines this register and split it
587 // into two values.
588 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
589 unsigned RedefIndex = getDefIndex(MIIdx);
590
Evan Cheng816a7f32007-08-11 00:59:19 +0000591 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng983b81d2007-08-29 20:45:00 +0000592 VNInfo *OldValNo = OldLR->valno;
Evan Cheng816a7f32007-08-11 00:59:19 +0000593 unsigned OldEnd = OldLR->end;
594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 // Delete the initial value, which should be short and continuous,
596 // because the 2-addr copy must be in the same MBB as the redef.
597 interval.removeRange(DefIndex, RedefIndex);
598
599 // Two-address vregs should always only be redefined once. This means
600 // that at this point, there should be exactly one value number in it.
601 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
602
603 // The new value number (#1) is defined by the instruction we claimed
604 // defined value #0.
Evan Cheng319802c2007-09-05 21:46:51 +0000605 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
606 interval.copyValNumInfo(ValNo, OldValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607
608 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng983b81d2007-08-29 20:45:00 +0000609 OldValNo->def = RedefIndex;
610 OldValNo->reg = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611
612 // Add the new live interval which replaces the range for the input copy.
613 LiveRange LR(DefIndex, RedefIndex, ValNo);
614 DOUT << " replace range with " << LR;
615 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000616 interval.addKill(ValNo, RedefIndex);
617 interval.removeKills(ValNo, RedefIndex, OldEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618
619 // If this redefinition is dead, we need to add a dummy unit live
620 // range covering the def slot.
621 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng983b81d2007-08-29 20:45:00 +0000622 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623
624 DOUT << " RESULT: ";
625 interval.print(DOUT, mri_);
626
627 } else {
628 // Otherwise, this must be because of phi elimination. If this is the
629 // first redefinition of the vreg that we have seen, go back and change
630 // the live range in the PHI block to be a different value number.
631 if (interval.containsOneValue()) {
632 assert(vi.Kills.size() == 1 &&
633 "PHI elimination vreg should have one kill, the PHI itself!");
634
635 // Remove the old range that we now know has an incorrect number.
Evan Cheng319802c2007-09-05 21:46:51 +0000636 VNInfo *VNI = interval.getValNumInfo(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 MachineInstr *Killer = vi.Kills[0];
638 unsigned Start = getMBBStartIdx(Killer->getParent());
639 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
640 DOUT << " Removing [" << Start << "," << End << "] from: ";
641 interval.print(DOUT, mri_); DOUT << "\n";
642 interval.removeRange(Start, End);
Evan Cheng319802c2007-09-05 21:46:51 +0000643 interval.addKill(VNI, Start+1); // odd # means phi node
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 DOUT << " RESULT: "; interval.print(DOUT, mri_);
645
646 // Replace the interval with one of a NEW value number. Note that this
647 // value number isn't actually defined by an instruction, weird huh? :)
Evan Cheng319802c2007-09-05 21:46:51 +0000648 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 DOUT << " replace range with " << LR;
650 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000651 interval.addKill(LR.valno, End);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 DOUT << " RESULT: "; interval.print(DOUT, mri_);
653 }
654
655 // In the case of PHI elimination, each variable definition is only
656 // live until the end of the block. We've already taken care of the
657 // rest of the live range.
658 unsigned defIndex = getDefIndex(MIIdx);
659
Evan Cheng983b81d2007-08-29 20:45:00 +0000660 VNInfo *ValNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 unsigned SrcReg, DstReg;
Evan Cheng687d1082007-10-12 08:50:34 +0000662 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Cheng319802c2007-09-05 21:46:51 +0000663 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng687d1082007-10-12 08:50:34 +0000664 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
665 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
666 VNInfoAllocator);
667 else
668 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669
Evan Cheng0f727342007-08-08 07:03:29 +0000670 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng983b81d2007-08-29 20:45:00 +0000671 LiveRange LR(defIndex, killIndex, ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000673 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 DOUT << " +" << LR;
675 }
676 }
677
678 DOUT << '\n';
679}
680
681void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
682 MachineBasicBlock::iterator mi,
683 unsigned MIIdx,
684 LiveInterval &interval,
685 unsigned SrcReg) {
686 // A physical register cannot be live across basic block, so its
687 // lifetime must end somewhere in its defining basic block.
688 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
689
690 unsigned baseIndex = MIIdx;
691 unsigned start = getDefIndex(baseIndex);
692 unsigned end = start;
693
694 // If it is not used after definition, it is considered dead at
695 // the instruction defining it. Hence its interval is:
696 // [defSlot(def), defSlot(def)+1)
697 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
698 DOUT << " dead";
699 end = getDefIndex(start) + 1;
700 goto exit;
701 }
702
703 // If it is not dead on definition, it must be killed by a
704 // subsequent instruction. Hence its interval is:
705 // [defSlot(def), useSlot(kill)+1)
706 while (++mi != MBB->end()) {
707 baseIndex += InstrSlots::NUM;
708 if (lv_->KillsRegister(mi, interval.reg)) {
709 DOUT << " killed";
710 end = getUseIndex(baseIndex) + 1;
711 goto exit;
712 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
713 // Another instruction redefines the register before it is ever read.
714 // Then the register is essentially dead at the instruction that defines
715 // it. Hence its interval is:
716 // [defSlot(def), defSlot(def)+1)
717 DOUT << " dead";
718 end = getDefIndex(start) + 1;
719 goto exit;
720 }
721 }
722
723 // The only case we should have a dead physreg here without a killing or
724 // instruction where we know it's dead is if it is live-in to the function
725 // and never used.
726 assert(!SrcReg && "physreg was not killed in defining block!");
727 end = getDefIndex(start) + 1; // It's dead.
728
729exit:
730 assert(start < end && "did not find end of interval?");
731
732 // Already exists? Extend old live interval.
733 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng983b81d2007-08-29 20:45:00 +0000734 VNInfo *ValNo = (OldLR != interval.end())
Evan Cheng319802c2007-09-05 21:46:51 +0000735 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng983b81d2007-08-29 20:45:00 +0000736 LiveRange LR(start, end, ValNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000738 interval.addKill(LR.valno, end);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 DOUT << " +" << LR << '\n';
740}
741
742void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
743 MachineBasicBlock::iterator MI,
744 unsigned MIIdx,
745 unsigned reg) {
746 if (MRegisterInfo::isVirtualRegister(reg))
747 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
748 else if (allocatableRegs_[reg]) {
749 unsigned SrcReg, DstReg;
Evan Cheng687d1082007-10-12 08:50:34 +0000750 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
751 SrcReg = MI->getOperand(1).getReg();
752 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 SrcReg = 0;
754 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
755 // Def of a register also defines its sub-registers.
756 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
757 // Avoid processing some defs more than once.
758 if (!MI->findRegisterDefOperand(*AS))
759 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
760 }
761}
762
763void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
764 unsigned MIIdx,
765 LiveInterval &interval, bool isAlias) {
766 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
767
768 // Look for kills, if it reaches a def before it's killed, then it shouldn't
769 // be considered a livein.
770 MachineBasicBlock::iterator mi = MBB->begin();
771 unsigned baseIndex = MIIdx;
772 unsigned start = baseIndex;
773 unsigned end = start;
774 while (mi != MBB->end()) {
775 if (lv_->KillsRegister(mi, interval.reg)) {
776 DOUT << " killed";
777 end = getUseIndex(baseIndex) + 1;
778 goto exit;
779 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
780 // Another instruction redefines the register before it is ever read.
781 // Then the register is essentially dead at the instruction that defines
782 // it. Hence its interval is:
783 // [defSlot(def), defSlot(def)+1)
784 DOUT << " dead";
785 end = getDefIndex(start) + 1;
786 goto exit;
787 }
788
789 baseIndex += InstrSlots::NUM;
790 ++mi;
791 }
792
793exit:
794 // Live-in register might not be used at all.
795 if (end == MIIdx) {
796 if (isAlias) {
797 DOUT << " dead";
798 end = getDefIndex(MIIdx) + 1;
799 } else {
800 DOUT << " live through";
801 end = baseIndex;
802 }
803 }
804
Evan Cheng319802c2007-09-05 21:46:51 +0000805 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 interval.addRange(LR);
Evan Cheng319802c2007-09-05 21:46:51 +0000807 interval.addKill(LR.valno, end);
Evan Cheng0f727342007-08-08 07:03:29 +0000808 DOUT << " +" << LR << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809}
810
811/// computeIntervals - computes the live intervals for virtual
812/// registers. for some ordering of the machine instructions [1,N] a
813/// live interval is an interval [i, j) where 1 <= i <= j < N for
814/// which a variable is live
815void LiveIntervals::computeIntervals() {
816 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
817 << "********** Function: "
818 << ((Value*)mf_->getFunction())->getName() << '\n';
819 // Track the index of the current machine instr.
820 unsigned MIIndex = 0;
821 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
822 MBBI != E; ++MBBI) {
823 MachineBasicBlock *MBB = MBBI;
824 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
825
826 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
827
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000828 // Create intervals for live-ins to this BB first.
829 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
830 LE = MBB->livein_end(); LI != LE; ++LI) {
831 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
832 // Multiple live-ins can alias the same register.
833 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
834 if (!hasInterval(*AS))
835 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
836 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 }
838
839 for (; MI != miEnd; ++MI) {
840 DOUT << MIIndex << "\t" << *MI;
841
842 // Handle defs.
843 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
844 MachineOperand &MO = MI->getOperand(i);
845 // handle register defs - build intervals
846 if (MO.isRegister() && MO.getReg() && MO.isDef())
847 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
848 }
849
850 MIIndex += InstrSlots::NUM;
851 }
852 }
853}
854
Evan Cheng94262e42007-10-17 02:10:22 +0000855bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Cheng12d6fcb2007-10-17 06:53:44 +0000856 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng94262e42007-10-17 02:10:22 +0000857 std::vector<IdxMBBPair>::const_iterator I =
858 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
859
860 bool ResVal = false;
861 while (I != Idx2MBBMap.end()) {
862 if (LR.end <= I->first)
863 break;
864 MBBs.push_back(I->second);
865 ResVal = true;
866 ++I;
867 }
868 return ResVal;
869}
870
871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872LiveInterval LiveIntervals::createInterval(unsigned reg) {
873 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
874 HUGE_VALF : 0.0F;
875 return LiveInterval(reg, Weight);
876}