blob: 929b1e7e92d50cdf8d63a1f1f52c698a30f53a17 [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 Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#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"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000034#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000036using namespace llvm;
37
Evan Chengbc165e42007-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
Chris Lattnercd3245a2006-12-19 22:41:21 +000044STATISTIC(numIntervals, "Number of original intervals");
45STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046STATISTIC(numFolded , "Number of loads/stores folded into instructions");
47
Devang Patel19974732007-05-03 01:11:54 +000048char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000049namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000050 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000051}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000052
Chris Lattnerf7da2c72006-08-24 22:43:55 +000053void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000054 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 AU.addRequired<LiveVariables>();
56 AU.addPreservedID(PHIEliminationID);
57 AU.addRequiredID(PHIEliminationID);
58 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000060}
61
Chris Lattnerf7da2c72006-08-24 22:43:55 +000062void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000063 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 mi2iMap_.clear();
65 i2miMap_.clear();
66 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000067 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
68 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000069 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
70 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000071}
72
Evan Cheng4ca980e2007-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
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000089/// runOnMachineFunction - Register allocate the whole function
90///
91bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000092 mf_ = &fn;
93 tm_ = &fn.getTarget();
94 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000095 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000097 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000098
Chris Lattner428b92e2006-09-15 03:57:23 +000099 // Number MachineInstrs and MachineBasicBlocks.
100 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000101 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000102
103 unsigned MIIndex = 0;
104 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
105 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000106 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000107
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
109 I != E; ++I) {
110 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000111 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000112 i2miMap_.push_back(I);
113 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000114 }
Evan Cheng549f27d32007-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 Cheng4ca980e2007-10-17 02:10:22 +0000118 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000119 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000120 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000121
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000123
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000124 numIntervals += getNumIntervals();
125
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000126 DOUT << "********** INTERVALS **********\n";
127 for (iterator I = begin(), E = end(); I != E; ++I) {
128 I->second.print(DOUT, mri_);
129 DOUT << "\n";
130 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000133 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000135}
136
Chris Lattner70ca3582004-09-30 15:59:17 +0000137/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000138void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000139 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000140 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000141 I->second.print(DOUT, mri_);
142 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000143 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000144
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) {
Chris Lattner477e4552004-09-30 16:10:45 +0000151 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000152 }
153 }
154}
155
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-08-29 20:45:00 +0000158bool LiveIntervals::isReMaterializable(const LiveInterval &li,
159 const VNInfo *ValNo, MachineInstr *MI) {
Evan Chengbc165e42007-08-16 07:24:22 +0000160 if (DisableReMat)
161 return false;
162
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-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 Cheng549f27d32007-08-13 23:45:17 +0000177 continue;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000178 unsigned DefIdx = VNI->def;
Evan Cheng549f27d32007-08-13 23:45:17 +0000179 if (DefIdx == ~1U)
180 continue; // Dead val#.
181 MachineInstr *DefMI = (DefIdx == ~0u)
182 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000183 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
Evan Cheng549f27d32007-08-13 23:45:17 +0000184 return false;
185 }
186 return true;
187}
188
Evan Cheng34c2a9f2007-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 Cheng549f27d32007-08-13 23:45:17 +0000193bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000194 MachineInstr *DefMI,
Evan Cheng549f27d32007-08-13 23:45:17 +0000195 unsigned index, unsigned i,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000196 bool isSS, int slot, unsigned reg) {
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000197 MachineInstr *fmi = isSS
198 ? mri_->foldMemoryOperand(MI, i, slot)
199 : mri_->foldMemoryOperand(MI, i, DefMI);
Evan Cheng549f27d32007-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 Cheng32dfbea2007-10-12 08:50:34 +0000232 SSARegMap *RegMap = mf_->getSSARegMap();
233 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-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 Cheng549f27d32007-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 Cheng7ecb38b2007-08-29 20:45:00 +0000256 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000257 // Remember how to remat the def of this val#.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000258 ReMatOrigDefs[VN] = DefMI;
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-08-29 20:45:00 +0000261 ReMatDefs[VN] = DefMI = DefMI->clone();
Evan Cheng549f27d32007-08-13 23:45:17 +0000262 vrm.setVirtIsReMaterialized(reg, DefMI);
263
264 bool CanDelete = true;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000265 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
266 unsigned KillIdx = VNI->kills[j];
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-08-29 20:45:00 +0000281 ReMatDelete.set(VN);
Evan Cheng549f27d32007-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 Cheng7ecb38b2007-08-29 20:45:00 +0000295 MachineInstr *DefMI = ReMatDefs[I->valno->id];
296 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000297 bool DefIsReMat = DefMI != NULL;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000298 bool CanDelete = ReMatDelete[I->valno->id];
Evan Cheng549f27d32007-08-13 23:45:17 +0000299 int LdSlot = 0;
300 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
Evan Cheng34c2a9f2007-08-30 05:53:02 +0000301 bool isLoad = isLoadSS ||
302 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng549f27d32007-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 Cheng32dfbea2007-10-12 08:50:34 +0000316 if (!mop.isRegister())
317 continue;
318 unsigned Reg = mop.getReg();
319 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
320 continue;
321 bool isSubReg = RegMap->isSubRegister(Reg);
322 unsigned SubIdx = 0;
323 if (isSubReg) {
324 SubIdx = RegMap->getSubRegisterIndex(Reg);
325 Reg = RegMap->getSuperRegister(Reg);
326 }
327 if (Reg != li.reg)
328 continue;
329
330 bool TryFold = !DefIsReMat;
331 bool FoldSS = true;
332 int FoldSlot = slot;
333 if (DefIsReMat) {
334 // If this is the rematerializable definition MI itself and
335 // all of its uses are rematerialized, simply delete it.
336 if (MI == OrigDefMI && CanDelete) {
337 RemoveMachineInstrFromMaps(MI);
338 MI->eraseFromParent();
339 break;
Evan Cheng549f27d32007-08-13 23:45:17 +0000340 }
341
Evan Cheng32dfbea2007-10-12 08:50:34 +0000342 // If def for this use can't be rematerialized, then try folding.
343 TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
344 if (isLoad) {
345 // Try fold loads (from stack slot, constant pool, etc.) into uses.
346 FoldSS = isLoadSS;
347 FoldSlot = LdSlot;
Evan Cheng549f27d32007-08-13 23:45:17 +0000348 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000349 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000350
Evan Cheng32dfbea2007-10-12 08:50:34 +0000351 // FIXME: fold subreg use
352 if (!isSubReg && TryFold &&
353 tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
354 // Folding the load/store can completely change the instruction in
355 // unpredictable ways, rescan it from the beginning.
356 goto RestartInstruction;
357
358 // Create a new virtual register for the spill interval.
359 unsigned NewVReg = RegMap->createVirtualRegister(rc);
360 if (isSubReg)
361 RegMap->setIsSubRegister(NewVReg, NewVReg, SubIdx);
362
363 // Scan all of the operands of this instruction rewriting operands
364 // to use NewVReg instead of li.reg as appropriate. We do this for
365 // two reasons:
366 //
367 // 1. If the instr reads the same spilled vreg multiple times, we
368 // want to reuse the NewVReg.
369 // 2. If the instr is a two-addr instruction, we are required to
370 // keep the src/dst regs pinned.
371 //
372 // Keep track of whether we replace a use and/or def so that we can
373 // create the spill interval with the appropriate range.
374 mop.setReg(NewVReg);
375
376 bool HasUse = mop.isUse();
377 bool HasDef = mop.isDef();
378 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengab847242007-11-06 08:50:44 +0000379 if (!MI->getOperand(j).isRegister())
380 continue;
381 unsigned RegJ = MI->getOperand(j).getReg();
Evan Chengbe6781b2007-11-06 21:12:10 +0000382 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
383 continue;
384 bool isSubRegJ = RegMap->isSubRegister(RegJ);
385 if (isSubRegJ) {
386 assert(!isSubReg || RegMap->getSubRegisterIndex(RegJ) == SubIdx);
Evan Chengab847242007-11-06 08:50:44 +0000387 RegJ = RegMap->getSuperRegister(RegJ);
Evan Chengbe6781b2007-11-06 21:12:10 +0000388 }
389 // Important to check "isSubRegJ == isSubReg".
390 // e.g. %reg1024 = MOVSX32rr16 %reg1025. It's possible that both
391 // registers are coalesced to the same register but only %reg1025 is
392 // a sub-register use. They should not be rewritten to the same
393 // register.
394 if (RegJ == li.reg && isSubRegJ == isSubReg) {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000395 MI->getOperand(j).setReg(NewVReg);
396 HasUse |= MI->getOperand(j).isUse();
397 HasDef |= MI->getOperand(j).isDef();
398 }
399 }
400
401 vrm.grow();
402 if (DefIsReMat) {
403 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
404 if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
405 // Each valnum may have its own remat id.
406 ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000407 } else {
Evan Cheng32dfbea2007-10-12 08:50:34 +0000408 vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
409 }
410 if (!CanDelete || (HasUse && HasDef)) {
411 // If this is a two-addr instruction then its use operands are
412 // rematerializable but its def is not. It should be assigned a
413 // stack slot.
Evan Cheng549f27d32007-08-13 23:45:17 +0000414 vrm.assignVirt2StackSlot(NewVReg, slot);
415 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000416 } else {
417 vrm.assignVirt2StackSlot(NewVReg, slot);
Evan Cheng549f27d32007-08-13 23:45:17 +0000418 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000419
420 // create a new register interval for this spill / remat.
421 LiveInterval &nI = getOrCreateInterval(NewVReg);
422 assert(nI.empty());
423
424 // the spill weight is now infinity as it
425 // cannot be spilled again
426 nI.weight = HUGE_VALF;
427
428 if (HasUse) {
429 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
430 nI.getNextValue(~0U, 0, VNInfoAllocator));
431 DOUT << " +" << LR;
432 nI.addRange(LR);
433 }
434 if (HasDef) {
435 LiveRange LR(getDefIndex(index), getStoreIndex(index),
436 nI.getNextValue(~0U, 0, VNInfoAllocator));
437 DOUT << " +" << LR;
438 nI.addRange(LR);
439 }
440
441 added.push_back(&nI);
442
443 // update live variables if it is available
444 if (lv_)
445 lv_->addVirtualRegisterKilled(NewVReg, MI);
446
447 DOUT << "\t\t\t\tadded new interval: ";
448 nI.print(DOUT, mri_);
449 DOUT << '\n';
Evan Cheng549f27d32007-08-13 23:45:17 +0000450 }
451 }
452 }
453
454 return added;
455}
456
Evan Chengc92da382007-11-03 07:20:12 +0000457/// conflictsWithPhysRegDef - Returns true if the specified register
458/// is defined during the duration of the specified interval.
459bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
460 VirtRegMap &vrm, unsigned reg) {
461 for (LiveInterval::Ranges::const_iterator
462 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
463 for (unsigned index = getBaseIndex(I->start),
464 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
465 index += InstrSlots::NUM) {
466 // skip deleted instructions
467 while (index != end && !getInstructionFromIndex(index))
468 index += InstrSlots::NUM;
469 if (index == end) break;
470
471 MachineInstr *MI = getInstructionFromIndex(index);
472 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
473 MachineOperand& mop = MI->getOperand(i);
474 if (!mop.isRegister() || !mop.isDef())
475 continue;
476 unsigned PhysReg = mop.getReg();
477 if (PhysReg == 0)
478 continue;
479 if (MRegisterInfo::isVirtualRegister(PhysReg))
480 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5f5f3b62007-11-05 00:59:10 +0000481 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000482 return true;
483 }
484 }
485 }
486
487 return false;
488}
489
Evan Cheng549f27d32007-08-13 23:45:17 +0000490void LiveIntervals::printRegName(unsigned reg) const {
491 if (MRegisterInfo::isPhysicalRegister(reg))
492 cerr << mri_->getName(reg);
493 else
494 cerr << "%reg" << reg;
495}
496
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000497void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000498 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000499 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000500 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000501 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000502 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000503
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000504 // Virtual registers may be defined multiple times (due to phi
505 // elimination and 2-addr elimination). Much of what we do only has to be
506 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000507 // time we see a vreg.
508 if (interval.empty()) {
509 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000510 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000511 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000512 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000513 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000514 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng48ff2822007-10-12 17:16:50 +0000515 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000516 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
517 VNInfoAllocator);
518 else
519 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000520
521 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000522
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 // Loop over all of the blocks that the vreg is defined in. There are
524 // two cases we have to handle here. The most common case is a vreg
525 // whose lifetime is contained within a basic block. In this case there
526 // will be a single kill, in MBB, which comes after the definition.
527 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
528 // FIXME: what about dead vars?
529 unsigned killIdx;
530 if (vi.Kills[0] != mi)
531 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
532 else
533 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000534
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000535 // If the kill happens after the definition, we have an intra-block
536 // live range.
537 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000538 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000540 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000542 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000543 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 return;
545 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000546 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000547
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000548 // The other case we handle is when a virtual register lives to the end
549 // of the defining block, potentially live across some blocks, then is
550 // live into some number of blocks, but gets killed. Start by adding a
551 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000552 LiveRange NewLR(defIndex,
553 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000554 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000555 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000556 interval.addRange(NewLR);
557
558 // Iterate over all of the blocks that the variable is completely
559 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
560 // live interval.
561 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
562 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000563 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
564 if (!MBB->empty()) {
565 LiveRange LR(getMBBStartIdx(i),
566 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000567 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000569 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000570 }
571 }
572 }
573
574 // Finally, this virtual register is live from the start of any killing
575 // block to the 'use' slot of the killing instruction.
576 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
577 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000578 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000579 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000580 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000581 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000582 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000583 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000584 }
585
586 } else {
587 // If this is the second time we see a virtual register definition, it
588 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000589 // the result of two address elimination, then the vreg is one of the
590 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000591 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000592 // If this is a two-address definition, then we have already processed
593 // the live range. The only problem is that we didn't realize there
594 // are actually two values in the live interval. Because of this we
595 // need to take the LiveRegion that defines this register and split it
596 // into two values.
597 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000598 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000599
Evan Cheng4f8ff162007-08-11 00:59:19 +0000600 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000601 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000602 unsigned OldEnd = OldLR->end;
603
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000604 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000605 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000606 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000607
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000608 // Two-address vregs should always only be redefined once. This means
609 // that at this point, there should be exactly one value number in it.
610 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
611
Chris Lattner91725b72006-08-31 05:54:43 +0000612 // The new value number (#1) is defined by the instruction we claimed
613 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000614 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
615 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000616
Chris Lattner91725b72006-08-31 05:54:43 +0000617 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000618 OldValNo->def = RedefIndex;
619 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000620
621 // Add the new live interval which replaces the range for the input copy.
622 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000623 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000624 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000625 interval.addKill(ValNo, RedefIndex);
626 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000627
628 // If this redefinition is dead, we need to add a dummy unit live
629 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000630 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000631 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000632
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000633 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000634 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000635
636 } else {
637 // Otherwise, this must be because of phi elimination. If this is the
638 // first redefinition of the vreg that we have seen, go back and change
639 // the live range in the PHI block to be a different value number.
640 if (interval.containsOneValue()) {
641 assert(vi.Kills.size() == 1 &&
642 "PHI elimination vreg should have one kill, the PHI itself!");
643
644 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000645 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000646 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000647 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000648 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000649 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000650 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000652 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000653 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000654
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000655 // Replace the interval with one of a NEW value number. Note that this
656 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000657 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000658 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000660 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000661 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000662 }
663
664 // In the case of PHI elimination, each variable definition is only
665 // live until the end of the block. We've already taken care of the
666 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000667 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000668
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000669 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000670 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000671 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000672 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000673 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
674 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
675 VNInfoAllocator);
676 else
677 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000678
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000679 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000680 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000681 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000682 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000683 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000684 }
685 }
686
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000687 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000688}
689
Chris Lattnerf35fef72004-07-23 21:24:19 +0000690void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000691 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000692 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000693 LiveInterval &interval,
694 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000695 // A physical register cannot be live across basic block, so its
696 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000697 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000698
Chris Lattner6b128bd2006-09-03 08:07:11 +0000699 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000700 unsigned start = getDefIndex(baseIndex);
701 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000702
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 // If it is not used after definition, it is considered dead at
704 // the instruction defining it. Hence its interval is:
705 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000706 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000707 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000708 end = getDefIndex(start) + 1;
709 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000710 }
711
712 // If it is not dead on definition, it must be killed by a
713 // subsequent instruction. Hence its interval is:
714 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000715 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000716 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000717 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000718 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000719 end = getUseIndex(baseIndex) + 1;
720 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000721 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
722 // Another instruction redefines the register before it is ever read.
723 // Then the register is essentially dead at the instruction that defines
724 // it. Hence its interval is:
725 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000726 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000727 end = getDefIndex(start) + 1;
728 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000729 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000730 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000731
732 // The only case we should have a dead physreg here without a killing or
733 // instruction where we know it's dead is if it is live-in to the function
734 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000735 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000736 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000737
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000738exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000739 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000740
Evan Cheng24a3cc42007-04-25 07:30:23 +0000741 // Already exists? Extend old live interval.
742 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000743 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000744 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000745 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000746 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000747 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000748 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000749}
750
Chris Lattnerf35fef72004-07-23 21:24:19 +0000751void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
752 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000753 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000754 unsigned reg) {
755 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000756 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000757 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000758 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000759 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
760 SrcReg = MI->getOperand(1).getReg();
761 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000762 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000763 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000764 // Def of a register also defines its sub-registers.
765 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
766 // Avoid processing some defs more than once.
767 if (!MI->findRegisterDefOperand(*AS))
768 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000769 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000770}
771
Evan Chengb371f452007-02-19 21:49:54 +0000772void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000773 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000774 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000775 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
776
777 // Look for kills, if it reaches a def before it's killed, then it shouldn't
778 // be considered a livein.
779 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000780 unsigned baseIndex = MIIdx;
781 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000782 unsigned end = start;
783 while (mi != MBB->end()) {
784 if (lv_->KillsRegister(mi, interval.reg)) {
785 DOUT << " killed";
786 end = getUseIndex(baseIndex) + 1;
787 goto exit;
788 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
789 // Another instruction redefines the register before it is ever read.
790 // Then the register is essentially dead at the instruction that defines
791 // it. Hence its interval is:
792 // [defSlot(def), defSlot(def)+1)
793 DOUT << " dead";
794 end = getDefIndex(start) + 1;
795 goto exit;
796 }
797
798 baseIndex += InstrSlots::NUM;
799 ++mi;
800 }
801
802exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000803 // Live-in register might not be used at all.
804 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000805 if (isAlias) {
806 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000807 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000808 } else {
809 DOUT << " live through";
810 end = baseIndex;
811 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000812 }
813
Evan Chengf3bb2e62007-09-05 21:46:51 +0000814 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000815 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000816 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000817 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000818}
819
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000820/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000821/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000822/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000823/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000824void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000825 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
826 << "********** Function: "
827 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000828 // Track the index of the current machine instr.
829 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000830 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
831 MBBI != E; ++MBBI) {
832 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000833 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000834
Chris Lattner428b92e2006-09-15 03:57:23 +0000835 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000836
Dan Gohmancb406c22007-10-03 19:26:29 +0000837 // Create intervals for live-ins to this BB first.
838 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
839 LE = MBB->livein_end(); LI != LE; ++LI) {
840 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
841 // Multiple live-ins can alias the same register.
842 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
843 if (!hasInterval(*AS))
844 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
845 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000846 }
847
Chris Lattner428b92e2006-09-15 03:57:23 +0000848 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000849 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000850
Evan Cheng438f7bc2006-11-10 08:43:01 +0000851 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000852 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
853 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000854 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000855 if (MO.isRegister() && MO.getReg() && MO.isDef())
856 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000857 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000858
859 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000860 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000861 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000862}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000863
Evan Cheng4ca980e2007-10-17 02:10:22 +0000864bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000865 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000866 std::vector<IdxMBBPair>::const_iterator I =
867 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
868
869 bool ResVal = false;
870 while (I != Idx2MBBMap.end()) {
871 if (LR.end <= I->first)
872 break;
873 MBBs.push_back(I->second);
874 ResVal = true;
875 ++I;
876 }
877 return ResVal;
878}
879
880
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000881LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000882 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000883 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000884 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000885}