blob: 6e516f560e5459ae6dbaa8b9b15fa43b41182394 [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
Chris Lattnercd3245a2006-12-19 22:41:21 +000039STATISTIC(numIntervals, "Number of original intervals");
40STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
41STATISTIC(numJoins , "Number of interval joins performed");
42STATISTIC(numPeep , "Number of identity moves eliminated after coalescing");
43STATISTIC(numFolded , "Number of loads/stores folded into instructions");
44
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000045namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000046 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000048 static cl::opt<bool>
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000049 EnableJoining("join-liveintervals",
Chris Lattner428b92e2006-09-15 03:57:23 +000050 cl::desc("Coallesce copies (default=true)"),
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000051 cl::init(true));
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 {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 AU.addRequired<LiveVariables>();
56 AU.addPreservedID(PHIEliminationID);
57 AU.addRequiredID(PHIEliminationID);
58 AU.addRequiredID(TwoAddressInstructionPassID);
59 AU.addRequired<LoopInfo>();
60 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000061}
62
Chris Lattnerf7da2c72006-08-24 22:43:55 +000063void LiveIntervals::releaseMemory() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 mi2iMap_.clear();
65 i2miMap_.clear();
66 r2iMap_.clear();
67 r2rMap_.clear();
Evan Cheng88d1f582007-03-01 02:03:03 +000068 JoinedLIs.clear();
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000069}
70
71
Evan Cheng99314142006-05-11 07:29:24 +000072static bool isZeroLengthInterval(LiveInterval *li) {
73 for (LiveInterval::Ranges::const_iterator
74 i = li->ranges.begin(), e = li->ranges.end(); i != e; ++i)
75 if (i->end - i->start > LiveIntervals::InstrSlots::NUM)
76 return false;
77 return true;
78}
79
80
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000081/// runOnMachineFunction - Register allocate the whole function
82///
83bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 mf_ = &fn;
85 tm_ = &fn.getTarget();
86 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000087 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000088 lv_ = &getAnalysis<LiveVariables>();
Alkis Evlogimenos53278012004-08-26 22:22:38 +000089 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenos2c4f7b52004-09-09 19:24:38 +000090 r2rMap_.grow(mf_->getSSARegMap()->getLastVirtReg());
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091
Chris Lattner428b92e2006-09-15 03:57:23 +000092 // Number MachineInstrs and MachineBasicBlocks.
93 // Initialize MBB indexes to a sentinal.
94 MBB2IdxMap.resize(mf_->getNumBlockIDs(), ~0U);
95
96 unsigned MIIndex = 0;
97 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
98 MBB != E; ++MBB) {
99 // Set the MBB2IdxMap entry for this MBB.
100 MBB2IdxMap[MBB->getNumber()] = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000101
Chris Lattner428b92e2006-09-15 03:57:23 +0000102 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
103 I != E; ++I) {
104 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000106 i2miMap_.push_back(I);
107 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000108 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000109 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000110
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000111 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000112
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113 numIntervals += getNumIntervals();
114
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000115 DOUT << "********** INTERVALS **********\n";
116 for (iterator I = begin(), E = end(); I != E; ++I) {
117 I->second.print(DOUT, mri_);
118 DOUT << "\n";
119 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120
Chris Lattner428b92e2006-09-15 03:57:23 +0000121 // Join (coallesce) intervals if requested.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 if (EnableJoining) joinIntervals();
123
124 numIntervalsAfter += getNumIntervals();
Chris Lattner428b92e2006-09-15 03:57:23 +0000125
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000126
127 // perform a final pass over the instructions and compute spill
Chris Lattnerfbecc5a2006-09-03 07:53:50 +0000128 // weights, coalesce virtual registers and remove identity moves.
Chris Lattner428b92e2006-09-15 03:57:23 +0000129 const LoopInfo &loopInfo = getAnalysis<LoopInfo>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000130
131 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
132 mbbi != mbbe; ++mbbi) {
133 MachineBasicBlock* mbb = mbbi;
134 unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
135
136 for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
137 mii != mie; ) {
138 // if the move will be an identity move delete it
139 unsigned srcReg, dstReg, RegRep;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000140 if (tii_->isMoveInstr(*mii, srcReg, dstReg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 (RegRep = rep(srcReg)) == rep(dstReg)) {
142 // remove from def list
Evan Chengb371f452007-02-19 21:49:54 +0000143 LiveInterval &RegInt = getOrCreateInterval(RegRep);
144 MachineOperand *MO = mii->findRegisterDefOperand(dstReg);
145 // If def of this move instruction is dead, remove its live range from
146 // the dstination register's live interval.
147 if (MO->isDead()) {
148 unsigned MoveIdx = getDefIndex(getInstructionIndex(mii));
149 LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx);
150 RegInt.removeRange(MLR->start, MoveIdx+1);
151 if (RegInt.empty())
152 removeInterval(RegRep);
153 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000154 RemoveMachineInstrFromMaps(mii);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000155 mii = mbbi->erase(mii);
156 ++numPeep;
157 }
158 else {
Chris Lattnerfbecc5a2006-09-03 07:53:50 +0000159 for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
160 const MachineOperand &mop = mii->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000161 if (mop.isRegister() && mop.getReg() &&
162 MRegisterInfo::isVirtualRegister(mop.getReg())) {
163 // replace register with representative register
164 unsigned reg = rep(mop.getReg());
Chris Lattnere53f4a02006-05-04 17:52:23 +0000165 mii->getOperand(i).setReg(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000166
167 LiveInterval &RegInt = getInterval(reg);
168 RegInt.weight +=
Chris Lattner7a36ae82004-10-25 18:40:47 +0000169 (mop.isUse() + mop.isDef()) * pow(10.0F, (int)loopDepth);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000170 }
171 }
172 ++mii;
173 }
174 }
175 }
176
Evan Cheng99314142006-05-11 07:29:24 +0000177 for (iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb75a6632006-11-07 07:18:40 +0000178 LiveInterval &LI = I->second;
179 if (MRegisterInfo::isVirtualRegister(LI.reg)) {
Chris Lattnerc9d94d12006-08-27 12:47:48 +0000180 // If the live interval length is essentially zero, i.e. in every live
Evan Cheng99314142006-05-11 07:29:24 +0000181 // range the use follows def immediately, it doesn't make sense to spill
182 // it and hope it will be easier to allocate for this li.
Chris Lattnerb75a6632006-11-07 07:18:40 +0000183 if (isZeroLengthInterval(&LI))
Jim Laskey7902c752006-11-07 12:25:45 +0000184 LI.weight = HUGE_VALF;
Chris Lattnerb75a6632006-11-07 07:18:40 +0000185
Chris Lattner393ebae2006-11-07 18:04:58 +0000186 // Divide the weight of the interval by its size. This encourages
187 // spilling of intervals that are large and have few uses, and
188 // discourages spilling of small intervals with many uses.
189 unsigned Size = 0;
190 for (LiveInterval::iterator II = LI.begin(), E = LI.end(); II != E;++II)
191 Size += II->end - II->start;
Chris Lattnerb75a6632006-11-07 07:18:40 +0000192
Chris Lattner393ebae2006-11-07 18:04:58 +0000193 LI.weight /= Size;
Chris Lattnerc9d94d12006-08-27 12:47:48 +0000194 }
Evan Cheng99314142006-05-11 07:29:24 +0000195 }
196
Chris Lattner70ca3582004-09-30 15:59:17 +0000197 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000198 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000199}
200
Chris Lattner70ca3582004-09-30 15:59:17 +0000201/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000202void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000203 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000204 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000205 I->second.print(DOUT, mri_);
206 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000207 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000208
209 O << "********** MACHINEINSTRS **********\n";
210 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
211 mbbi != mbbe; ++mbbi) {
212 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
213 for (MachineBasicBlock::iterator mii = mbbi->begin(),
214 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000215 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000216 }
217 }
218}
219
Bill Wendling01352aa2006-11-16 02:41:50 +0000220/// CreateNewLiveInterval - Create a new live interval with the given live
221/// ranges. The new live interval will have an infinite spill weight.
222LiveInterval&
223LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI,
224 const std::vector<LiveRange> &LRs) {
225 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg);
226
227 // Create a new virtual register for the spill interval.
228 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC);
229
230 // Replace the old virtual registers in the machine operands with the shiny
231 // new one.
232 for (std::vector<LiveRange>::const_iterator
233 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
234 unsigned Index = getBaseIndex(I->start);
235 unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM;
236
237 for (; Index != End; Index += InstrSlots::NUM) {
238 // Skip deleted instructions
239 while (Index != End && !getInstructionFromIndex(Index))
240 Index += InstrSlots::NUM;
241
242 if (Index == End) break;
243
244 MachineInstr *MI = getInstructionFromIndex(Index);
245
Bill Wendlingbeeb77f2006-11-16 07:35:18 +0000246 for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) {
Bill Wendling01352aa2006-11-16 02:41:50 +0000247 MachineOperand &MOp = MI->getOperand(J);
248 if (MOp.isRegister() && rep(MOp.getReg()) == LI->reg)
249 MOp.setReg(NewVReg);
250 }
251 }
252 }
253
254 LiveInterval &NewLI = getOrCreateInterval(NewVReg);
255
256 // The spill weight is now infinity as it cannot be spilled again
257 NewLI.weight = float(HUGE_VAL);
258
259 for (std::vector<LiveRange>::const_iterator
260 I = LRs.begin(), E = LRs.end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000261 DOUT << " Adding live range " << *I << " to new interval\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000262 NewLI.addRange(*I);
263 }
264
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000265 DOUT << "Created new live interval " << NewLI << "\n";
Bill Wendling01352aa2006-11-16 02:41:50 +0000266 return NewLI;
267}
268
Chris Lattner70ca3582004-09-30 15:59:17 +0000269std::vector<LiveInterval*> LiveIntervals::
270addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) {
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000271 // since this is called after the analysis is done we don't know if
272 // LiveVariables is available
273 lv_ = getAnalysisToUpdate<LiveVariables>();
274
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000275 std::vector<LiveInterval*> added;
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000276
Jim Laskey7902c752006-11-07 12:25:45 +0000277 assert(li.weight != HUGE_VALF &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000278 "attempt to spill already spilled interval!");
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000279
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000280 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
281 li.print(DOUT, mri_);
282 DOUT << '\n';
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000283
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000284 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000285
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 for (LiveInterval::Ranges::const_iterator
287 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
288 unsigned index = getBaseIndex(i->start);
289 unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM;
290 for (; index != end; index += InstrSlots::NUM) {
291 // skip deleted instructions
292 while (index != end && !getInstructionFromIndex(index))
293 index += InstrSlots::NUM;
294 if (index == end) break;
Chris Lattner8640f4e2004-07-19 15:16:53 +0000295
Chris Lattner3b9db832006-01-03 07:41:37 +0000296 MachineInstr *MI = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000297
Chris Lattner29268692006-09-05 02:12:02 +0000298 RestartInstruction:
Chris Lattner3b9db832006-01-03 07:41:37 +0000299 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
300 MachineOperand& mop = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000301 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattner29268692006-09-05 02:12:02 +0000302 if (MachineInstr *fmi = mri_->foldMemoryOperand(MI, i, slot)) {
Chris Lattnerb11443d2005-09-09 19:17:47 +0000303 // Attempt to fold the memory reference into the instruction. If we
304 // can do this, we don't need to insert spill code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000305 if (lv_)
Chris Lattner3b9db832006-01-03 07:41:37 +0000306 lv_->instructionChanged(MI, fmi);
Evan Cheng200370f2006-04-30 08:41:47 +0000307 MachineBasicBlock &MBB = *MI->getParent();
Chris Lattner35f27052006-05-01 21:16:03 +0000308 vrm.virtFolded(li.reg, MI, i, fmi);
Chris Lattner3b9db832006-01-03 07:41:37 +0000309 mi2iMap_.erase(MI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 i2miMap_[index/InstrSlots::NUM] = fmi;
311 mi2iMap_[fmi] = index;
Chris Lattner3b9db832006-01-03 07:41:37 +0000312 MI = MBB.insert(MBB.erase(MI), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000314 // Folding the load/store can completely change the instruction in
315 // unpredictable ways, rescan it from the beginning.
Chris Lattner29268692006-09-05 02:12:02 +0000316 goto RestartInstruction;
Chris Lattner477e4552004-09-30 16:10:45 +0000317 } else {
Chris Lattner29268692006-09-05 02:12:02 +0000318 // Create a new virtual register for the spill interval.
319 unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc);
320
321 // Scan all of the operands of this instruction rewriting operands
322 // to use NewVReg instead of li.reg as appropriate. We do this for
323 // two reasons:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 //
Chris Lattner29268692006-09-05 02:12:02 +0000325 // 1. If the instr reads the same spilled vreg multiple times, we
326 // want to reuse the NewVReg.
327 // 2. If the instr is a two-addr instruction, we are required to
328 // keep the src/dst regs pinned.
329 //
330 // Keep track of whether we replace a use and/or def so that we can
331 // create the spill interval with the appropriate range.
332 mop.setReg(NewVReg);
333
334 bool HasUse = mop.isUse();
335 bool HasDef = mop.isDef();
336 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
337 if (MI->getOperand(j).isReg() &&
338 MI->getOperand(j).getReg() == li.reg) {
339 MI->getOperand(j).setReg(NewVReg);
340 HasUse |= MI->getOperand(j).isUse();
341 HasDef |= MI->getOperand(j).isDef();
342 }
343 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000344
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 // create a new register for this spill
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346 vrm.grow();
Chris Lattner29268692006-09-05 02:12:02 +0000347 vrm.assignVirt2StackSlot(NewVReg, slot);
348 LiveInterval &nI = getOrCreateInterval(NewVReg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000350
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000351 // the spill weight is now infinity as it
352 // cannot be spilled again
Jim Laskey7902c752006-11-07 12:25:45 +0000353 nI.weight = HUGE_VALF;
Chris Lattner29268692006-09-05 02:12:02 +0000354
355 if (HasUse) {
356 LiveRange LR(getLoadIndex(index), getUseIndex(index),
357 nI.getNextValue(~0U, 0));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000358 DOUT << " +" << LR;
Chris Lattner29268692006-09-05 02:12:02 +0000359 nI.addRange(LR);
360 }
361 if (HasDef) {
362 LiveRange LR(getDefIndex(index), getStoreIndex(index),
363 nI.getNextValue(~0U, 0));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000364 DOUT << " +" << LR;
Chris Lattner29268692006-09-05 02:12:02 +0000365 nI.addRange(LR);
366 }
367
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000368 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000369
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000370 // update live variables if it is available
371 if (lv_)
Chris Lattner29268692006-09-05 02:12:02 +0000372 lv_->addVirtualRegisterKilled(NewVReg, MI);
Chris Lattnerb11443d2005-09-09 19:17:47 +0000373
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000374 DOUT << "\t\t\t\tadded new interval: ";
375 nI.print(DOUT, mri_);
376 DOUT << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000377 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000378 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000379 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000380 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000382
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000383 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000384}
385
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000386void LiveIntervals::printRegName(unsigned reg) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000387 if (MRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge8156192006-12-07 01:30:32 +0000388 cerr << mri_->getName(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000390 cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000391}
392
Evan Chengbf105c82006-11-03 03:04:46 +0000393/// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to
394/// two addr elimination.
395static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg,
396 const TargetInstrInfo *TII) {
397 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
398 MachineOperand &MO1 = MI->getOperand(i);
399 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
400 for (unsigned j = i+1; j < e; ++j) {
401 MachineOperand &MO2 = MI->getOperand(j);
402 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
Evan Cheng51cdcd12006-12-07 01:21:59 +0000403 MI->getInstrDescriptor()->
404 getOperandConstraint(j, TOI::TIED_TO) == (int)i)
Evan Chengbf105c82006-11-03 03:04:46 +0000405 return true;
406 }
407 }
408 }
409 return false;
410}
411
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000412void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000413 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000414 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000415 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000416 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000417 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000418
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000419 // Virtual registers may be defined multiple times (due to phi
420 // elimination and 2-addr elimination). Much of what we do only has to be
421 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 // time we see a vreg.
423 if (interval.empty()) {
424 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000425 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner6097d132004-07-19 02:15:56 +0000426
Chris Lattner91725b72006-08-31 05:54:43 +0000427 unsigned ValNum;
428 unsigned SrcReg, DstReg;
429 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
430 ValNum = interval.getNextValue(~0U, 0);
431 else
432 ValNum = interval.getNextValue(defIndex, SrcReg);
433
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000434 assert(ValNum == 0 && "First value in interval is not 0?");
435 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000436
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437 // Loop over all of the blocks that the vreg is defined in. There are
438 // two cases we have to handle here. The most common case is a vreg
439 // whose lifetime is contained within a basic block. In this case there
440 // will be a single kill, in MBB, which comes after the definition.
441 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
442 // FIXME: what about dead vars?
443 unsigned killIdx;
444 if (vi.Kills[0] != mi)
445 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
446 else
447 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000448
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000449 // If the kill happens after the definition, we have an intra-block
450 // live range.
451 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000452 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000453 "Shouldn't be alive across any blocks!");
454 LiveRange LR(defIndex, killIdx, ValNum);
455 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000456 DOUT << " +" << LR << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000457 return;
458 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000459 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000460
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000461 // The other case we handle is when a virtual register lives to the end
462 // of the defining block, potentially live across some blocks, then is
463 // live into some number of blocks, but gets killed. Start by adding a
464 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000465 LiveRange NewLR(defIndex,
466 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
467 ValNum);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000468 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000469 interval.addRange(NewLR);
470
471 // Iterate over all of the blocks that the variable is completely
472 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
473 // live interval.
474 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
475 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000476 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
477 if (!MBB->empty()) {
478 LiveRange LR(getMBBStartIdx(i),
479 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000480 ValNum);
481 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000482 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000483 }
484 }
485 }
486
487 // Finally, this virtual register is live from the start of any killing
488 // block to the 'use' slot of the killing instruction.
489 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
490 MachineInstr *Kill = vi.Kills[i];
Chris Lattner428b92e2006-09-15 03:57:23 +0000491 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000492 getUseIndex(getInstructionIndex(Kill))+1,
493 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000494 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000495 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000496 }
497
498 } else {
499 // If this is the second time we see a virtual register definition, it
500 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000501 // the result of two address elimination, then the vreg is one of the
502 // def-and-use register operand.
503 if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000504 // If this is a two-address definition, then we have already processed
505 // the live range. The only problem is that we didn't realize there
506 // are actually two values in the live interval. Because of this we
507 // need to take the LiveRegion that defines this register and split it
508 // into two values.
509 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000510 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511
512 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000513 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000514 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000515
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000516 // Two-address vregs should always only be redefined once. This means
517 // that at this point, there should be exactly one value number in it.
518 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
519
Chris Lattner91725b72006-08-31 05:54:43 +0000520 // The new value number (#1) is defined by the instruction we claimed
521 // defined value #0.
522 unsigned ValNo = interval.getNextValue(0, 0);
523 interval.setValueNumberInfo(1, interval.getValNumInfo(0));
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000524
Chris Lattner91725b72006-08-31 05:54:43 +0000525 // Value#0 is now defined by the 2-addr instruction.
526 interval.setValueNumberInfo(0, std::make_pair(~0U, 0U));
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000527
528 // Add the new live interval which replaces the range for the input copy.
529 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000530 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000531 interval.addRange(LR);
532
533 // If this redefinition is dead, we need to add a dummy unit live
534 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000535 if (lv_->RegisterDefIsDead(mi, interval.reg))
536 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000537
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000538 DOUT << "RESULT: ";
539 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000540
541 } else {
542 // Otherwise, this must be because of phi elimination. If this is the
543 // first redefinition of the vreg that we have seen, go back and change
544 // the live range in the PHI block to be a different value number.
545 if (interval.containsOneValue()) {
546 assert(vi.Kills.size() == 1 &&
547 "PHI elimination vreg should have one kill, the PHI itself!");
548
549 // Remove the old range that we now know has an incorrect number.
550 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000551 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000552 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000553 DOUT << "Removing [" << Start << "," << End << "] from: ";
554 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000555 interval.removeRange(Start, End);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000556 DOUT << "RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000557
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000558 // Replace the interval with one of a NEW value number. Note that this
559 // value number isn't actually defined by an instruction, weird huh? :)
Chris Lattner91725b72006-08-31 05:54:43 +0000560 LiveRange LR(Start, End, interval.getNextValue(~0U, 0));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000561 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000562 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000563 DOUT << "RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 }
565
566 // In the case of PHI elimination, each variable definition is only
567 // live until the end of the block. We've already taken care of the
568 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000569 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000570
571 unsigned ValNum;
572 unsigned SrcReg, DstReg;
573 if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
574 ValNum = interval.getNextValue(~0U, 0);
575 else
576 ValNum = interval.getNextValue(defIndex, SrcReg);
577
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000578 LiveRange LR(defIndex,
Chris Lattner91725b72006-08-31 05:54:43 +0000579 getInstructionIndex(&mbb->back()) + InstrSlots::NUM, ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000580 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000581 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000582 }
583 }
584
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000585 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000586}
587
Chris Lattnerf35fef72004-07-23 21:24:19 +0000588void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000589 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000590 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000591 LiveInterval &interval,
592 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000593 // A physical register cannot be live across basic block, so its
594 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000595 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000596
Chris Lattner6b128bd2006-09-03 08:07:11 +0000597 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000598 unsigned start = getDefIndex(baseIndex);
599 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000600
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000601 // If it is not used after definition, it is considered dead at
602 // the instruction defining it. Hence its interval is:
603 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000604 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000605 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000606 end = getDefIndex(start) + 1;
607 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000608 }
609
610 // If it is not dead on definition, it must be killed by a
611 // subsequent instruction. Hence its interval is:
612 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000613 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000615 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000616 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000617 end = getUseIndex(baseIndex) + 1;
618 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000619 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
620 // Another instruction redefines the register before it is ever read.
621 // Then the register is essentially dead at the instruction that defines
622 // it. Hence its interval is:
623 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000624 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000625 end = getDefIndex(start) + 1;
626 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000627 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000628 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000629
630 // The only case we should have a dead physreg here without a killing or
631 // instruction where we know it's dead is if it is live-in to the function
632 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000633 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000634 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000635
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000636exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000637 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000638
Chris Lattner91725b72006-08-31 05:54:43 +0000639 LiveRange LR(start, end, interval.getNextValue(SrcReg != 0 ? start : ~0U,
640 SrcReg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000641 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000642 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000643}
644
Chris Lattnerf35fef72004-07-23 21:24:19 +0000645void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
646 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000647 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000648 unsigned reg) {
649 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000650 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000651 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000652 unsigned SrcReg, DstReg;
653 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
654 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000655 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000656 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000657 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000658 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000659}
660
Evan Chengb371f452007-02-19 21:49:54 +0000661void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000662 unsigned MIIdx,
Evan Chengb371f452007-02-19 21:49:54 +0000663 LiveInterval &interval) {
664 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
665
666 // Look for kills, if it reaches a def before it's killed, then it shouldn't
667 // be considered a livein.
668 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000669 unsigned baseIndex = MIIdx;
670 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000671 unsigned end = start;
672 while (mi != MBB->end()) {
673 if (lv_->KillsRegister(mi, interval.reg)) {
674 DOUT << " killed";
675 end = getUseIndex(baseIndex) + 1;
676 goto exit;
677 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
678 // Another instruction redefines the register before it is ever read.
679 // Then the register is essentially dead at the instruction that defines
680 // it. Hence its interval is:
681 // [defSlot(def), defSlot(def)+1)
682 DOUT << " dead";
683 end = getDefIndex(start) + 1;
684 goto exit;
685 }
686
687 baseIndex += InstrSlots::NUM;
688 ++mi;
689 }
690
691exit:
692 assert(start < end && "did not find end of interval?");
693
694 LiveRange LR(start, end, interval.getNextValue(~0U, 0));
Evan Chengb371f452007-02-19 21:49:54 +0000695 DOUT << " +" << LR << '\n';
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000696 interval.addRange(LR);
Evan Chengb371f452007-02-19 21:49:54 +0000697}
698
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000699/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000700/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000701/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000702/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000703void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000704 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
705 << "********** Function: "
706 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000707 // Track the index of the current machine instr.
708 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000709 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
710 MBBI != E; ++MBBI) {
711 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000712 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000713
Chris Lattner428b92e2006-09-15 03:57:23 +0000714 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000715
716 if (MBB->livein_begin() != MBB->livein_end()) {
Evan Chengb371f452007-02-19 21:49:54 +0000717 // Create intervals for live-ins to this BB first.
718 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000719 LE = MBB->livein_end(); LI != LE; ++LI) {
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000720 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000721 for (const unsigned* AS = mri_->getAliasSet(*LI); *AS; ++AS)
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000722 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS));
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000723 }
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000724 }
725
Chris Lattner428b92e2006-09-15 03:57:23 +0000726 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000727 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000728
Evan Cheng438f7bc2006-11-10 08:43:01 +0000729 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000730 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
731 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000732 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000733 if (MO.isRegister() && MO.getReg() && MO.isDef())
734 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000735 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000736
737 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000738 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000739 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000740}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000741
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000742/// AdjustCopiesBackFrom - We found a non-trivially-coallescable copy with IntA
743/// being the source and IntB being the dest, thus this defines a value number
744/// in IntB. If the source value number (in IntA) is defined by a copy from B,
745/// see if we can merge these two pieces of B into a single value number,
746/// eliminating a copy. For example:
747///
748/// A3 = B0
749/// ...
750/// B1 = A3 <- this copy
751///
752/// In this case, B0 can be extended to where the B1 copy lives, allowing the B1
753/// value number to be replaced with B0 (which simplifies the B liveinterval).
754///
755/// This returns true if an interval was modified.
756///
757bool LiveIntervals::AdjustCopiesBackFrom(LiveInterval &IntA, LiveInterval &IntB,
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000758 MachineInstr *CopyMI) {
759 unsigned CopyIdx = getDefIndex(getInstructionIndex(CopyMI));
760
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000761 // BValNo is a value number in B that is defined by a copy from A. 'B3' in
762 // the example above.
763 LiveInterval::iterator BLR = IntB.FindLiveRangeContaining(CopyIdx);
764 unsigned BValNo = BLR->ValId;
Chris Lattneraa51a482005-10-21 06:49:50 +0000765
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000766 // Get the location that B is defined at. Two options: either this value has
767 // an unknown definition point or it is defined at CopyIdx. If unknown, we
768 // can't process it.
769 unsigned BValNoDefIdx = IntB.getInstForValNum(BValNo);
770 if (BValNoDefIdx == ~0U) return false;
771 assert(BValNoDefIdx == CopyIdx &&
772 "Copy doesn't define the value?");
Chris Lattneraa51a482005-10-21 06:49:50 +0000773
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000774 // AValNo is the value number in A that defines the copy, A0 in the example.
775 LiveInterval::iterator AValLR = IntA.FindLiveRangeContaining(CopyIdx-1);
776 unsigned AValNo = AValLR->ValId;
Chris Lattneraa51a482005-10-21 06:49:50 +0000777
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000778 // If AValNo is defined as a copy from IntB, we can potentially process this.
779
780 // Get the instruction that defines this value number.
Chris Lattner91725b72006-08-31 05:54:43 +0000781 unsigned SrcReg = IntA.getSrcRegForValNum(AValNo);
782 if (!SrcReg) return false; // Not defined by a copy.
Chris Lattneraa51a482005-10-21 06:49:50 +0000783
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000784 // If the value number is not defined by a copy instruction, ignore it.
Chris Lattneraa51a482005-10-21 06:49:50 +0000785
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000786 // If the source register comes from an interval other than IntB, we can't
787 // handle this.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000788 if (rep(SrcReg) != IntB.reg) return false;
Chris Lattner91725b72006-08-31 05:54:43 +0000789
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000790 // Get the LiveRange in IntB that this value number starts with.
Chris Lattner91725b72006-08-31 05:54:43 +0000791 unsigned AValNoInstIdx = IntA.getInstForValNum(AValNo);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000792 LiveInterval::iterator ValLR = IntB.FindLiveRangeContaining(AValNoInstIdx-1);
793
794 // Make sure that the end of the live range is inside the same block as
795 // CopyMI.
796 MachineInstr *ValLREndInst = getInstructionFromIndex(ValLR->end-1);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000797 if (!ValLREndInst ||
798 ValLREndInst->getParent() != CopyMI->getParent()) return false;
Chris Lattneraa51a482005-10-21 06:49:50 +0000799
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000800 // Okay, we now know that ValLR ends in the same block that the CopyMI
801 // live-range starts. If there are no intervening live ranges between them in
802 // IntB, we can merge them.
803 if (ValLR+1 != BLR) return false;
Chris Lattneraa51a482005-10-21 06:49:50 +0000804
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000805 DOUT << "\nExtending: "; IntB.print(DOUT, mri_);
Chris Lattnerba256032006-08-30 23:02:29 +0000806
807 // We are about to delete CopyMI, so need to remove it as the 'instruction
808 // that defines this value #'.
Chris Lattner91725b72006-08-31 05:54:43 +0000809 IntB.setValueNumberInfo(BValNo, std::make_pair(~0U, 0));
Chris Lattnerba256032006-08-30 23:02:29 +0000810
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000811 // Okay, we can merge them. We need to insert a new liverange:
812 // [ValLR.end, BLR.begin) of either value number, then we merge the
813 // two value numbers.
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000814 unsigned FillerStart = ValLR->end, FillerEnd = BLR->start;
815 IntB.addRange(LiveRange(FillerStart, FillerEnd, BValNo));
816
817 // If the IntB live range is assigned to a physical register, and if that
818 // physreg has aliases,
819 if (MRegisterInfo::isPhysicalRegister(IntB.reg)) {
820 for (const unsigned *AS = mri_->getAliasSet(IntB.reg); *AS; ++AS) {
821 LiveInterval &AliasLI = getInterval(*AS);
822 AliasLI.addRange(LiveRange(FillerStart, FillerEnd,
Chris Lattner91725b72006-08-31 05:54:43 +0000823 AliasLI.getNextValue(~0U, 0)));
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000824 }
825 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000826
827 // Okay, merge "B1" into the same value number as "B0".
828 if (BValNo != ValLR->ValId)
829 IntB.MergeValueNumberInto(BValNo, ValLR->ValId);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000830 DOUT << " result = "; IntB.print(DOUT, mri_);
831 DOUT << "\n";
Evan Cheng16191f02007-02-25 09:41:59 +0000832
833 // If the source instruction was killing the source register before the
834 // merge, unset the isKill marker given the live range has been extended.
835 MachineOperand *MOK = ValLREndInst->findRegisterUseOperand(IntB.reg, true);
836 if (MOK)
837 MOK->unsetIsKill();
Chris Lattneraa51a482005-10-21 06:49:50 +0000838
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000839 // Finally, delete the copy instruction.
840 RemoveMachineInstrFromMaps(CopyMI);
841 CopyMI->eraseFromParent();
842 ++numPeep;
Chris Lattneraa51a482005-10-21 06:49:50 +0000843 return true;
844}
845
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000846/// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
847/// which are the src/dst of the copy instruction CopyMI. This returns true
848/// if the copy was successfully coallesced away, or if it is never possible
849/// to coallesce these this copy, due to register constraints. It returns
850/// false if it is not currently possible to coallesce this interval, but
851/// it may be possible if other things get coallesced.
852bool LiveIntervals::JoinCopy(MachineInstr *CopyMI,
853 unsigned SrcReg, unsigned DstReg) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000854 DOUT << getInstructionIndex(CopyMI) << '\t' << *CopyMI;
Evan Chengb371f452007-02-19 21:49:54 +0000855
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000856 // Get representative registers.
Evan Chengb371f452007-02-19 21:49:54 +0000857 unsigned repSrcReg = rep(SrcReg);
858 unsigned repDstReg = rep(DstReg);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000859
860 // If they are already joined we continue.
Evan Chengb371f452007-02-19 21:49:54 +0000861 if (repSrcReg == repDstReg) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000862 DOUT << "\tCopy already coallesced.\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000863 return true; // Not coallescable.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000864 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000865
866 // If they are both physical registers, we cannot join them.
Evan Chengb371f452007-02-19 21:49:54 +0000867 if (MRegisterInfo::isPhysicalRegister(repSrcReg) &&
868 MRegisterInfo::isPhysicalRegister(repDstReg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000869 DOUT << "\tCan not coallesce physregs.\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000870 return true; // Not coallescable.
871 }
872
873 // We only join virtual registers with allocatable physical registers.
Evan Chengb371f452007-02-19 21:49:54 +0000874 if (MRegisterInfo::isPhysicalRegister(repSrcReg) &&
875 !allocatableRegs_[repSrcReg]) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000876 DOUT << "\tSrc reg is unallocatable physreg.\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000877 return true; // Not coallescable.
878 }
Evan Chengb371f452007-02-19 21:49:54 +0000879 if (MRegisterInfo::isPhysicalRegister(repDstReg) &&
880 !allocatableRegs_[repDstReg]) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000881 DOUT << "\tDst reg is unallocatable physreg.\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000882 return true; // Not coallescable.
883 }
884
885 // If they are not of the same register class, we cannot join them.
Evan Chengb371f452007-02-19 21:49:54 +0000886 if (differingRegisterClasses(repSrcReg, repDstReg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000887 DOUT << "\tSrc/Dest are different register classes.\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000888 return true; // Not coallescable.
889 }
890
Evan Chengb371f452007-02-19 21:49:54 +0000891 LiveInterval &SrcInt = getInterval(repSrcReg);
892 LiveInterval &DestInt = getInterval(repDstReg);
893 assert(SrcInt.reg == repSrcReg && DestInt.reg == repDstReg &&
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000894 "Register mapping is horribly broken!");
895
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000896 DOUT << "\t\tInspecting "; SrcInt.print(DOUT, mri_);
897 DOUT << " and "; DestInt.print(DOUT, mri_);
898 DOUT << ": ";
Evan Chengb371f452007-02-19 21:49:54 +0000899
900 // Check if it is necessary to propagate "isDead" property before intervals
901 // are joined.
902 MachineOperand *mopd = CopyMI->findRegisterDefOperand(DstReg);
903 bool isDead = mopd->isDead();
Evan Chengedeffb32007-02-26 21:37:37 +0000904 bool isShorten = false;
Evan Chengb371f452007-02-19 21:49:54 +0000905 unsigned SrcStart = 0;
906 unsigned SrcEnd = 0;
907 if (isDead) {
Evan Cheng48ef3982007-02-25 09:46:31 +0000908 unsigned CopyIdx = getInstructionIndex(CopyMI);
909 LiveInterval::iterator SrcLR =
910 SrcInt.FindLiveRangeContaining(getUseIndex(CopyIdx));
Evan Chengb371f452007-02-19 21:49:54 +0000911 SrcStart = SrcLR->start;
912 SrcEnd = SrcLR->end;
Evan Cheng48ef3982007-02-25 09:46:31 +0000913 // The instruction which defines the src is only truly dead if there are
914 // no intermediate uses and there isn't a use beyond the copy.
915 // FIXME: find the last use, mark is kill and shorten the live range.
Evan Chengedeffb32007-02-26 21:37:37 +0000916 if (SrcEnd > getDefIndex(CopyIdx))
Evan Chengb371f452007-02-19 21:49:54 +0000917 isDead = false;
Evan Chengedeffb32007-02-26 21:37:37 +0000918 else {
919 MachineOperand *MOU;
920 MachineInstr *LastUse =
921 lastRegisterUse(repSrcReg, SrcStart, CopyIdx, MOU);
922 if (LastUse) {
923 // Shorten the liveinterval to the end of last use.
924 MOU->setIsKill();
925 isDead = false;
926 isShorten = true;
927 SrcEnd = getUseIndex(getInstructionIndex(LastUse));
928 }
929 }
930 if (isDead)
931 isShorten = true;
Evan Chengb371f452007-02-19 21:49:54 +0000932 }
933
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000934 // Okay, attempt to join these two intervals. On failure, this returns false.
935 // Otherwise, if one of the intervals being joined is a physreg, this method
936 // always canonicalizes DestInt to be it. The output "SrcInt" will not have
937 // been modified, so we can use this information below to update aliases.
Evan Chengb371f452007-02-19 21:49:54 +0000938 if (JoinIntervals(DestInt, SrcInt)) {
939 if (isDead) {
940 // Result of the copy is dead. Propagate this property.
Evan Cheng48ef3982007-02-25 09:46:31 +0000941 if (SrcStart == 0 && MRegisterInfo::isPhysicalRegister(SrcReg)) {
Evan Chengb371f452007-02-19 21:49:54 +0000942 // Live-in to the function but dead. Remove it from MBB live-in set.
943 // JoinIntervals may end up swapping the two intervals.
Evan Chengb371f452007-02-19 21:49:54 +0000944 MachineBasicBlock *MBB = CopyMI->getParent();
945 MBB->removeLiveIn(SrcReg);
946 } else {
947 MachineInstr *SrcMI = getInstructionFromIndex(SrcStart);
948 if (SrcMI) {
Evan Chengb371f452007-02-19 21:49:54 +0000949 MachineOperand *mops = SrcMI->findRegisterDefOperand(SrcReg);
950 if (mops)
951 // FIXME: mops == NULL means SrcMI defines a subregister?
952 mops->setIsDead();
953 }
954 }
955 }
Evan Chengedeffb32007-02-26 21:37:37 +0000956
957 if (isShorten) {
958 // Shorten the live interval.
959 LiveInterval &LiveInInt = (repSrcReg == DestInt.reg) ? DestInt : SrcInt;
960 LiveInInt.removeRange(SrcStart, SrcEnd);
961 }
Evan Chengb371f452007-02-19 21:49:54 +0000962 } else {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000963 // Coallescing failed.
964
965 // If we can eliminate the copy without merging the live ranges, do so now.
966 if (AdjustCopiesBackFrom(SrcInt, DestInt, CopyMI))
967 return true;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000968
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000969 // Otherwise, we are unable to join the intervals.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000970 DOUT << "Interference!\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000971 return false;
972 }
973
Evan Chengb371f452007-02-19 21:49:54 +0000974 bool Swapped = repSrcReg == DestInt.reg;
Chris Lattnere7f729b2006-08-26 01:28:16 +0000975 if (Swapped)
Evan Chengb371f452007-02-19 21:49:54 +0000976 std::swap(repSrcReg, repDstReg);
977 assert(MRegisterInfo::isVirtualRegister(repSrcReg) &&
Chris Lattnere7f729b2006-08-26 01:28:16 +0000978 "LiveInterval::join didn't work right!");
979
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000980 // If we're about to merge live ranges into a physical register live range,
981 // we have to update any aliased register's live ranges to indicate that they
982 // have clobbered values for this range.
Evan Chengb371f452007-02-19 21:49:54 +0000983 if (MRegisterInfo::isPhysicalRegister(repDstReg)) {
984 for (const unsigned *AS = mri_->getAliasSet(repDstReg); *AS; ++AS)
Chris Lattnere7f729b2006-08-26 01:28:16 +0000985 getInterval(*AS).MergeInClobberRanges(SrcInt);
Chris Lattnerc114b2c2006-08-25 23:41:24 +0000986 }
987
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000988 DOUT << "\n\t\tJoined. Result = "; DestInt.print(DOUT, mri_);
989 DOUT << "\n";
Evan Cheng30cac022007-02-22 23:03:39 +0000990
Evan Cheng88d1f582007-03-01 02:03:03 +0000991#if 1
992 // Remember these liveintervals have been joined.
993 JoinedLIs.set(repSrcReg - MRegisterInfo::FirstVirtualRegister);
994 if (MRegisterInfo::isVirtualRegister(repDstReg))
995 JoinedLIs.set(repDstReg - MRegisterInfo::FirstVirtualRegister);
Evan Cheng30cac022007-02-22 23:03:39 +0000996
Evan Chengda2295e2007-02-23 20:40:13 +0000997 // If the intervals were swapped by Join, swap them back so that the register
998 // mapping (in the r2i map) is correct.
999 if (Swapped) SrcInt.swap(DestInt);
Evan Chengb371f452007-02-19 21:49:54 +00001000 removeInterval(repSrcReg);
1001 r2rMap_[repSrcReg] = repDstReg;
Chris Lattnere7f729b2006-08-26 01:28:16 +00001002
Chris Lattnerbfe180a2006-08-31 05:58:59 +00001003 // Finally, delete the copy instruction.
1004 RemoveMachineInstrFromMaps(CopyMI);
1005 CopyMI->eraseFromParent();
1006 ++numPeep;
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001007 ++numJoins;
1008 return true;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +00001009}
1010
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001011/// ComputeUltimateVN - Assuming we are going to join two live intervals,
1012/// compute what the resultant value numbers for each value in the input two
1013/// ranges will be. This is complicated by copies between the two which can
1014/// and will commonly cause multiple value numbers to be merged into one.
1015///
1016/// VN is the value number that we're trying to resolve. InstDefiningValue
1017/// keeps track of the new InstDefiningValue assignment for the result
1018/// LiveInterval. ThisFromOther/OtherFromThis are sets that keep track of
1019/// whether a value in this or other is a copy from the opposite set.
1020/// ThisValNoAssignments/OtherValNoAssignments keep track of value #'s that have
1021/// already been assigned.
1022///
1023/// ThisFromOther[x] - If x is defined as a copy from the other interval, this
1024/// contains the value number the copy is from.
1025///
1026static unsigned ComputeUltimateVN(unsigned VN,
Chris Lattner91725b72006-08-31 05:54:43 +00001027 SmallVector<std::pair<unsigned,
1028 unsigned>, 16> &ValueNumberInfo,
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001029 SmallVector<int, 16> &ThisFromOther,
1030 SmallVector<int, 16> &OtherFromThis,
1031 SmallVector<int, 16> &ThisValNoAssignments,
1032 SmallVector<int, 16> &OtherValNoAssignments,
1033 LiveInterval &ThisLI, LiveInterval &OtherLI) {
1034 // If the VN has already been computed, just return it.
1035 if (ThisValNoAssignments[VN] >= 0)
1036 return ThisValNoAssignments[VN];
Chris Lattner8a67f6e2006-09-01 07:00:23 +00001037// assert(ThisValNoAssignments[VN] != -2 && "Cyclic case?");
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001038
1039 // If this val is not a copy from the other val, then it must be a new value
1040 // number in the destination.
1041 int OtherValNo = ThisFromOther[VN];
1042 if (OtherValNo == -1) {
Chris Lattner91725b72006-08-31 05:54:43 +00001043 ValueNumberInfo.push_back(ThisLI.getValNumInfo(VN));
1044 return ThisValNoAssignments[VN] = ValueNumberInfo.size()-1;
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001045 }
1046
Chris Lattner8a67f6e2006-09-01 07:00:23 +00001047 // Otherwise, this *is* a copy from the RHS. If the other side has already
1048 // been computed, return it.
1049 if (OtherValNoAssignments[OtherValNo] >= 0)
1050 return ThisValNoAssignments[VN] = OtherValNoAssignments[OtherValNo];
1051
1052 // Mark this value number as currently being computed, then ask what the
1053 // ultimate value # of the other value is.
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001054 ThisValNoAssignments[VN] = -2;
1055 unsigned UltimateVN =
Chris Lattner91725b72006-08-31 05:54:43 +00001056 ComputeUltimateVN(OtherValNo, ValueNumberInfo,
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001057 OtherFromThis, ThisFromOther,
1058 OtherValNoAssignments, ThisValNoAssignments,
1059 OtherLI, ThisLI);
1060 return ThisValNoAssignments[VN] = UltimateVN;
1061}
1062
Chris Lattnerf21f0202006-09-02 05:26:59 +00001063static bool InVector(unsigned Val, const SmallVector<unsigned, 8> &V) {
1064 return std::find(V.begin(), V.end(), Val) != V.end();
1065}
1066
1067/// SimpleJoin - Attempt to joint the specified interval into this one. The
1068/// caller of this method must guarantee that the RHS only contains a single
1069/// value number and that the RHS is not defined by a copy from this
1070/// interval. This returns false if the intervals are not joinable, or it
1071/// joins them and returns true.
1072bool LiveIntervals::SimpleJoin(LiveInterval &LHS, LiveInterval &RHS) {
1073 assert(RHS.containsOneValue());
1074
1075 // Some number (potentially more than one) value numbers in the current
1076 // interval may be defined as copies from the RHS. Scan the overlapping
1077 // portions of the LHS and RHS, keeping track of this and looking for
1078 // overlapping live ranges that are NOT defined as copies. If these exist, we
1079 // cannot coallesce.
1080
1081 LiveInterval::iterator LHSIt = LHS.begin(), LHSEnd = LHS.end();
1082 LiveInterval::iterator RHSIt = RHS.begin(), RHSEnd = RHS.end();
1083
1084 if (LHSIt->start < RHSIt->start) {
1085 LHSIt = std::upper_bound(LHSIt, LHSEnd, RHSIt->start);
1086 if (LHSIt != LHS.begin()) --LHSIt;
1087 } else if (RHSIt->start < LHSIt->start) {
1088 RHSIt = std::upper_bound(RHSIt, RHSEnd, LHSIt->start);
1089 if (RHSIt != RHS.begin()) --RHSIt;
1090 }
1091
1092 SmallVector<unsigned, 8> EliminatedLHSVals;
1093
1094 while (1) {
1095 // Determine if these live intervals overlap.
1096 bool Overlaps = false;
1097 if (LHSIt->start <= RHSIt->start)
1098 Overlaps = LHSIt->end > RHSIt->start;
1099 else
1100 Overlaps = RHSIt->end > LHSIt->start;
1101
1102 // If the live intervals overlap, there are two interesting cases: if the
1103 // LHS interval is defined by a copy from the RHS, it's ok and we record
1104 // that the LHS value # is the same as the RHS. If it's not, then we cannot
1105 // coallesce these live ranges and we bail out.
1106 if (Overlaps) {
1107 // If we haven't already recorded that this value # is safe, check it.
1108 if (!InVector(LHSIt->ValId, EliminatedLHSVals)) {
1109 // Copy from the RHS?
1110 unsigned SrcReg = LHS.getSrcRegForValNum(LHSIt->ValId);
1111 if (rep(SrcReg) != RHS.reg)
1112 return false; // Nope, bail out.
1113
1114 EliminatedLHSVals.push_back(LHSIt->ValId);
1115 }
1116
1117 // We know this entire LHS live range is okay, so skip it now.
1118 if (++LHSIt == LHSEnd) break;
1119 continue;
1120 }
1121
1122 if (LHSIt->end < RHSIt->end) {
1123 if (++LHSIt == LHSEnd) break;
1124 } else {
1125 // One interesting case to check here. It's possible that we have
1126 // something like "X3 = Y" which defines a new value number in the LHS,
1127 // and is the last use of this liverange of the RHS. In this case, we
1128 // want to notice this copy (so that it gets coallesced away) even though
1129 // the live ranges don't actually overlap.
1130 if (LHSIt->start == RHSIt->end) {
1131 if (InVector(LHSIt->ValId, EliminatedLHSVals)) {
1132 // We already know that this value number is going to be merged in
1133 // if coallescing succeeds. Just skip the liverange.
1134 if (++LHSIt == LHSEnd) break;
1135 } else {
1136 // Otherwise, if this is a copy from the RHS, mark it as being merged
1137 // in.
1138 if (rep(LHS.getSrcRegForValNum(LHSIt->ValId)) == RHS.reg) {
1139 EliminatedLHSVals.push_back(LHSIt->ValId);
1140
1141 // We know this entire LHS live range is okay, so skip it now.
1142 if (++LHSIt == LHSEnd) break;
1143 }
1144 }
1145 }
1146
1147 if (++RHSIt == RHSEnd) break;
1148 }
1149 }
1150
1151 // If we got here, we know that the coallescing will be successful and that
1152 // the value numbers in EliminatedLHSVals will all be merged together. Since
1153 // the most common case is that EliminatedLHSVals has a single number, we
1154 // optimize for it: if there is more than one value, we merge them all into
1155 // the lowest numbered one, then handle the interval as if we were merging
1156 // with one value number.
1157 unsigned LHSValNo;
1158 if (EliminatedLHSVals.size() > 1) {
1159 // Loop through all the equal value numbers merging them into the smallest
1160 // one.
1161 unsigned Smallest = EliminatedLHSVals[0];
1162 for (unsigned i = 1, e = EliminatedLHSVals.size(); i != e; ++i) {
1163 if (EliminatedLHSVals[i] < Smallest) {
1164 // Merge the current notion of the smallest into the smaller one.
1165 LHS.MergeValueNumberInto(Smallest, EliminatedLHSVals[i]);
1166 Smallest = EliminatedLHSVals[i];
1167 } else {
1168 // Merge into the smallest.
1169 LHS.MergeValueNumberInto(EliminatedLHSVals[i], Smallest);
1170 }
1171 }
1172 LHSValNo = Smallest;
1173 } else {
1174 assert(!EliminatedLHSVals.empty() && "No copies from the RHS?");
1175 LHSValNo = EliminatedLHSVals[0];
1176 }
1177
1178 // Okay, now that there is a single LHS value number that we're merging the
1179 // RHS into, update the value number info for the LHS to indicate that the
1180 // value number is defined where the RHS value number was.
1181 LHS.setValueNumberInfo(LHSValNo, RHS.getValNumInfo(0));
1182
1183 // Okay, the final step is to loop over the RHS live intervals, adding them to
1184 // the LHS.
1185 LHS.MergeRangesInAsValue(RHS, LHSValNo);
1186 LHS.weight += RHS.weight;
1187
1188 return true;
1189}
1190
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001191/// JoinIntervals - Attempt to join these two intervals. On failure, this
1192/// returns false. Otherwise, if one of the intervals being joined is a
1193/// physreg, this method always canonicalizes LHS to be it. The output
1194/// "RHS" will not have been modified, so we can use this information
1195/// below to update aliases.
1196bool LiveIntervals::JoinIntervals(LiveInterval &LHS, LiveInterval &RHS) {
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001197 // Compute the final value assignment, assuming that the live ranges can be
1198 // coallesced.
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001199 SmallVector<int, 16> LHSValNoAssignments;
1200 SmallVector<int, 16> RHSValNoAssignments;
Chris Lattner91725b72006-08-31 05:54:43 +00001201 SmallVector<std::pair<unsigned,unsigned>, 16> ValueNumberInfo;
Chris Lattner238416c2006-09-01 06:10:18 +00001202
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001203 // Compute ultimate value numbers for the LHS and RHS values.
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001204 if (RHS.containsOneValue()) {
1205 // Copies from a liveinterval with a single value are simple to handle and
1206 // very common, handle the special case here. This is important, because
1207 // often RHS is small and LHS is large (e.g. a physreg).
1208
1209 // Find out if the RHS is defined as a copy from some value in the LHS.
1210 int RHSValID = -1;
1211 std::pair<unsigned,unsigned> RHSValNoInfo;
Chris Lattnerf21f0202006-09-02 05:26:59 +00001212 unsigned RHSSrcReg = RHS.getSrcRegForValNum(0);
1213 if ((RHSSrcReg == 0 || rep(RHSSrcReg) != LHS.reg)) {
1214 // If RHS is not defined as a copy from the LHS, we can use simpler and
1215 // faster checks to see if the live ranges are coallescable. This joiner
1216 // can't swap the LHS/RHS intervals though.
1217 if (!MRegisterInfo::isPhysicalRegister(RHS.reg)) {
1218 return SimpleJoin(LHS, RHS);
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001219 } else {
Chris Lattnerf21f0202006-09-02 05:26:59 +00001220 RHSValNoInfo = RHS.getValNumInfo(0);
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001221 }
1222 } else {
Chris Lattnerf21f0202006-09-02 05:26:59 +00001223 // It was defined as a copy from the LHS, find out what value # it is.
1224 unsigned ValInst = RHS.getInstForValNum(0);
1225 RHSValID = LHS.getLiveRangeContaining(ValInst-1)->ValId;
1226 RHSValNoInfo = LHS.getValNumInfo(RHSValID);
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001227 }
1228
Chris Lattnerf21f0202006-09-02 05:26:59 +00001229 LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
1230 RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001231 ValueNumberInfo.resize(LHS.getNumValNums());
1232
1233 // Okay, *all* of the values in LHS that are defined as a copy from RHS
1234 // should now get updated.
1235 for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) {
1236 if (unsigned LHSSrcReg = LHS.getSrcRegForValNum(VN)) {
1237 if (rep(LHSSrcReg) != RHS.reg) {
1238 // If this is not a copy from the RHS, its value number will be
1239 // unmodified by the coallescing.
1240 ValueNumberInfo[VN] = LHS.getValNumInfo(VN);
1241 LHSValNoAssignments[VN] = VN;
1242 } else if (RHSValID == -1) {
1243 // Otherwise, it is a copy from the RHS, and we don't already have a
1244 // value# for it. Keep the current value number, but remember it.
1245 LHSValNoAssignments[VN] = RHSValID = VN;
1246 ValueNumberInfo[VN] = RHSValNoInfo;
1247 } else {
1248 // Otherwise, use the specified value #.
1249 LHSValNoAssignments[VN] = RHSValID;
1250 if (VN != (unsigned)RHSValID)
1251 ValueNumberInfo[VN].first = ~1U;
1252 else
1253 ValueNumberInfo[VN] = RHSValNoInfo;
1254 }
1255 } else {
1256 ValueNumberInfo[VN] = LHS.getValNumInfo(VN);
1257 LHSValNoAssignments[VN] = VN;
1258 }
1259 }
1260
1261 assert(RHSValID != -1 && "Didn't find value #?");
1262 RHSValNoAssignments[0] = RHSValID;
1263
1264 } else {
Chris Lattner238416c2006-09-01 06:10:18 +00001265 // Loop over the value numbers of the LHS, seeing if any are defined from
1266 // the RHS.
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001267 SmallVector<int, 16> LHSValsDefinedFromRHS;
1268 LHSValsDefinedFromRHS.resize(LHS.getNumValNums(), -1);
1269 for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) {
1270 unsigned ValSrcReg = LHS.getSrcRegForValNum(VN);
1271 if (ValSrcReg == 0) // Src not defined by a copy?
1272 continue;
1273
Chris Lattner238416c2006-09-01 06:10:18 +00001274 // DstReg is known to be a register in the LHS interval. If the src is
1275 // from the RHS interval, we can use its value #.
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001276 if (rep(ValSrcReg) != RHS.reg)
1277 continue;
1278
1279 // Figure out the value # from the RHS.
1280 unsigned ValInst = LHS.getInstForValNum(VN);
1281 LHSValsDefinedFromRHS[VN] = RHS.getLiveRangeContaining(ValInst-1)->ValId;
1282 }
1283
Chris Lattner238416c2006-09-01 06:10:18 +00001284 // Loop over the value numbers of the RHS, seeing if any are defined from
1285 // the LHS.
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001286 SmallVector<int, 16> RHSValsDefinedFromLHS;
1287 RHSValsDefinedFromLHS.resize(RHS.getNumValNums(), -1);
1288 for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) {
1289 unsigned ValSrcReg = RHS.getSrcRegForValNum(VN);
1290 if (ValSrcReg == 0) // Src not defined by a copy?
1291 continue;
1292
Chris Lattner238416c2006-09-01 06:10:18 +00001293 // DstReg is known to be a register in the RHS interval. If the src is
1294 // from the LHS interval, we can use its value #.
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001295 if (rep(ValSrcReg) != LHS.reg)
1296 continue;
1297
1298 // Figure out the value # from the LHS.
1299 unsigned ValInst = RHS.getInstForValNum(VN);
1300 RHSValsDefinedFromLHS[VN] = LHS.getLiveRangeContaining(ValInst-1)->ValId;
1301 }
1302
Chris Lattnerf21f0202006-09-02 05:26:59 +00001303 LHSValNoAssignments.resize(LHS.getNumValNums(), -1);
1304 RHSValNoAssignments.resize(RHS.getNumValNums(), -1);
1305 ValueNumberInfo.reserve(LHS.getNumValNums() + RHS.getNumValNums());
1306
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001307 for (unsigned VN = 0, e = LHS.getNumValNums(); VN != e; ++VN) {
Chris Lattner8a67f6e2006-09-01 07:00:23 +00001308 if (LHSValNoAssignments[VN] >= 0 || LHS.getInstForValNum(VN) == ~2U)
1309 continue;
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001310 ComputeUltimateVN(VN, ValueNumberInfo,
1311 LHSValsDefinedFromRHS, RHSValsDefinedFromLHS,
1312 LHSValNoAssignments, RHSValNoAssignments, LHS, RHS);
1313 }
1314 for (unsigned VN = 0, e = RHS.getNumValNums(); VN != e; ++VN) {
Chris Lattner8a67f6e2006-09-01 07:00:23 +00001315 if (RHSValNoAssignments[VN] >= 0 || RHS.getInstForValNum(VN) == ~2U)
1316 continue;
1317 // If this value number isn't a copy from the LHS, it's a new number.
1318 if (RHSValsDefinedFromLHS[VN] == -1) {
1319 ValueNumberInfo.push_back(RHS.getValNumInfo(VN));
1320 RHSValNoAssignments[VN] = ValueNumberInfo.size()-1;
1321 continue;
1322 }
1323
Chris Lattner2ebfa0c2006-08-31 06:48:26 +00001324 ComputeUltimateVN(VN, ValueNumberInfo,
1325 RHSValsDefinedFromLHS, LHSValsDefinedFromRHS,
1326 RHSValNoAssignments, LHSValNoAssignments, RHS, LHS);
1327 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001328 }
1329
1330 // Armed with the mappings of LHS/RHS values to ultimate values, walk the
1331 // interval lists to see if these intervals are coallescable.
1332 LiveInterval::const_iterator I = LHS.begin();
1333 LiveInterval::const_iterator IE = LHS.end();
1334 LiveInterval::const_iterator J = RHS.begin();
1335 LiveInterval::const_iterator JE = RHS.end();
1336
1337 // Skip ahead until the first place of potential sharing.
1338 if (I->start < J->start) {
1339 I = std::upper_bound(I, IE, J->start);
1340 if (I != LHS.begin()) --I;
1341 } else if (J->start < I->start) {
1342 J = std::upper_bound(J, JE, I->start);
1343 if (J != RHS.begin()) --J;
1344 }
1345
1346 while (1) {
1347 // Determine if these two live ranges overlap.
1348 bool Overlaps;
1349 if (I->start < J->start) {
1350 Overlaps = I->end > J->start;
1351 } else {
1352 Overlaps = J->end > I->start;
1353 }
1354
1355 // If so, check value # info to determine if they are really different.
1356 if (Overlaps) {
1357 // If the live range overlap will map to the same value number in the
1358 // result liverange, we can still coallesce them. If not, we can't.
1359 if (LHSValNoAssignments[I->ValId] != RHSValNoAssignments[J->ValId])
1360 return false;
1361 }
1362
1363 if (I->end < J->end) {
1364 ++I;
1365 if (I == IE) break;
1366 } else {
1367 ++J;
1368 if (J == JE) break;
1369 }
1370 }
1371
1372 // If we get here, we know that we can coallesce the live ranges. Ask the
1373 // intervals to coallesce themselves now.
1374 LHS.join(RHS, &LHSValNoAssignments[0], &RHSValNoAssignments[0],
Chris Lattner91725b72006-08-31 05:54:43 +00001375 ValueNumberInfo);
Chris Lattner6d8fbef2006-08-29 23:18:15 +00001376 return true;
1377}
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001378
1379
Chris Lattnercc0d1562004-07-19 14:40:29 +00001380namespace {
1381 // DepthMBBCompare - Comparison predicate that sort first based on the loop
1382 // depth of the basic block (the unsigned), and then on the MBB number.
1383 struct DepthMBBCompare {
1384 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
1385 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
1386 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +00001387 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001388 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +00001389 }
1390 };
1391}
Chris Lattner1c5c0442004-07-19 14:08:10 +00001392
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001393
Chris Lattner1acb17c2006-09-02 05:32:53 +00001394void LiveIntervals::CopyCoallesceInMBB(MachineBasicBlock *MBB,
1395 std::vector<CopyRec> &TryAgain) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001396 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001397
1398 for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end();
1399 MII != E;) {
1400 MachineInstr *Inst = MII++;
1401
1402 // If this isn't a copy, we can't join intervals.
1403 unsigned SrcReg, DstReg;
1404 if (!tii_->isMoveInstr(*Inst, SrcReg, DstReg)) continue;
1405
Chris Lattner1acb17c2006-09-02 05:32:53 +00001406 if (!JoinCopy(Inst, SrcReg, DstReg))
1407 TryAgain.push_back(getCopyRec(Inst, SrcReg, DstReg));
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001408 }
1409}
1410
1411
Chris Lattnercc0d1562004-07-19 14:40:29 +00001412void LiveIntervals::joinIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001413 DOUT << "********** JOINING INTERVALS ***********\n";
Chris Lattnercc0d1562004-07-19 14:40:29 +00001414
Evan Cheng88d1f582007-03-01 02:03:03 +00001415 JoinedLIs.resize(getNumIntervals());
1416 JoinedLIs.reset();
1417
Chris Lattner1acb17c2006-09-02 05:32:53 +00001418 std::vector<CopyRec> TryAgainList;
Chris Lattnercc0d1562004-07-19 14:40:29 +00001419 const LoopInfo &LI = getAnalysis<LoopInfo>();
1420 if (LI.begin() == LI.end()) {
1421 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +00001422 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
1423 I != E; ++I)
Chris Lattner1acb17c2006-09-02 05:32:53 +00001424 CopyCoallesceInMBB(I, TryAgainList);
Chris Lattnercc0d1562004-07-19 14:40:29 +00001425 } else {
1426 // Otherwise, join intervals in inner loops before other intervals.
1427 // Unfortunately we can't just iterate over loop hierarchy here because
1428 // there may be more MBB's than BB's. Collect MBB's for sorting.
1429 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
1430 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
1431 I != E; ++I)
1432 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
1433
1434 // Sort by loop depth.
1435 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
1436
Alkis Evlogimenos70651572004-08-04 09:46:56 +00001437 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +00001438 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
Chris Lattner1acb17c2006-09-02 05:32:53 +00001439 CopyCoallesceInMBB(MBBs[i].second, TryAgainList);
1440 }
1441
1442 // Joining intervals can allow other intervals to be joined. Iteratively join
1443 // until we make no progress.
1444 bool ProgressMade = true;
1445 while (ProgressMade) {
1446 ProgressMade = false;
1447
1448 for (unsigned i = 0, e = TryAgainList.size(); i != e; ++i) {
1449 CopyRec &TheCopy = TryAgainList[i];
1450 if (TheCopy.MI &&
1451 JoinCopy(TheCopy.MI, TheCopy.SrcReg, TheCopy.DstReg)) {
1452 TheCopy.MI = 0; // Mark this one as done.
1453 ProgressMade = true;
1454 }
1455 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001456 }
Evan Cheng88d1f582007-03-01 02:03:03 +00001457
1458 // Some live range has been lengthened due to colaescing, eliminate the
1459 // unnecessary kills.
1460 int RegNum = JoinedLIs.find_first();
1461 while (RegNum != -1) {
1462 unsigned Reg = RegNum + MRegisterInfo::FirstVirtualRegister;
1463 unsigned repReg = rep(Reg);
1464 LiveInterval &LI = getInterval(repReg);
1465 LiveVariables::VarInfo& svi = lv_->getVarInfo(Reg);
1466 for (unsigned i = 0, e = svi.Kills.size(); i != e; ++i) {
1467 MachineInstr *Kill = svi.Kills[i];
1468 // Suppose vr1 = op vr2, x
1469 // and vr1 and vr2 are coalesced. vr2 should still be marked kill
1470 // unless it is a two-address operand.
1471 if (isRemoved(Kill) || hasRegisterDef(Kill, repReg))
1472 continue;
1473 if (LI.liveAt(getInstructionIndex(Kill) + InstrSlots::NUM))
1474 unsetRegisterKill(Kill, repReg);
1475 }
1476 RegNum = JoinedLIs.find_next(RegNum);
1477 }
Chris Lattnerf7da2c72006-08-24 22:43:55 +00001478
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001479 DOUT << "*** Register mapping ***\n";
1480 for (int i = 0, e = r2rMap_.size(); i != e; ++i)
1481 if (r2rMap_[i]) {
1482 DOUT << " reg " << i << " -> ";
1483 DEBUG(printRegName(r2rMap_[i]));
1484 DOUT << "\n";
1485 }
Chris Lattner1c5c0442004-07-19 14:08:10 +00001486}
1487
Evan Cheng647c15e2006-05-12 06:06:34 +00001488/// Return true if the two specified registers belong to different register
1489/// classes. The registers may be either phys or virt regs.
1490bool LiveIntervals::differingRegisterClasses(unsigned RegA,
1491 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +00001492
Chris Lattner7ac2d312004-07-24 02:59:07 +00001493 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +00001494 if (MRegisterInfo::isPhysicalRegister(RegA)) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001495 assert(MRegisterInfo::isVirtualRegister(RegB) &&
Chris Lattnerad3c74f2004-10-26 05:29:18 +00001496 "Shouldn't consider two physregs!");
Evan Cheng647c15e2006-05-12 06:06:34 +00001497 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
Chris Lattnerad3c74f2004-10-26 05:29:18 +00001498 }
Chris Lattner7ac2d312004-07-24 02:59:07 +00001499
1500 // Compare against the regclass for the second reg.
Evan Cheng647c15e2006-05-12 06:06:34 +00001501 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
1502 if (MRegisterInfo::isVirtualRegister(RegB))
1503 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
1504 else
1505 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +00001506}
1507
Evan Chengedeffb32007-02-26 21:37:37 +00001508/// lastRegisterUse - Returns the last use of the specific register between
1509/// cycles Start and End. It also returns the use operand by reference. It
1510/// returns NULL if there are no uses.
1511MachineInstr *
1512LiveIntervals::lastRegisterUse(unsigned Reg, unsigned Start, unsigned End,
1513 MachineOperand *&MOU) {
1514 int e = (End-1) / InstrSlots::NUM * InstrSlots::NUM;
1515 int s = Start;
1516 while (e >= s) {
Evan Chengb371f452007-02-19 21:49:54 +00001517 // Skip deleted instructions
Evan Chengedeffb32007-02-26 21:37:37 +00001518 MachineInstr *MI = getInstructionFromIndex(e);
1519 while ((e - InstrSlots::NUM) >= s && !MI) {
1520 e -= InstrSlots::NUM;
1521 MI = getInstructionFromIndex(e);
1522 }
1523 if (e < s || MI == NULL)
1524 return NULL;
Evan Chengb371f452007-02-19 21:49:54 +00001525
Evan Chengedeffb32007-02-26 21:37:37 +00001526 for (unsigned i = 0, NumOps = MI->getNumOperands(); i != NumOps; ++i) {
Evan Chengb371f452007-02-19 21:49:54 +00001527 MachineOperand &MO = MI->getOperand(i);
1528 if (MO.isReg() && MO.isUse() && MO.getReg() &&
Evan Chengedeffb32007-02-26 21:37:37 +00001529 mri_->regsOverlap(rep(MO.getReg()), Reg)) {
1530 MOU = &MO;
1531 return MI;
1532 }
Evan Chengb371f452007-02-19 21:49:54 +00001533 }
Evan Chengedeffb32007-02-26 21:37:37 +00001534
1535 e -= InstrSlots::NUM;
Evan Chengb371f452007-02-19 21:49:54 +00001536 }
1537
Evan Chengedeffb32007-02-26 21:37:37 +00001538 return NULL;
Evan Chengb371f452007-02-19 21:49:54 +00001539}
1540
Evan Cheng30cac022007-02-22 23:03:39 +00001541/// unsetRegisterKill - Unset IsKill property of all uses of specific register
1542/// of the specific instruction.
1543void LiveIntervals::unsetRegisterKill(MachineInstr *MI, unsigned Reg) {
1544 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1545 MachineOperand &MO = MI->getOperand(i);
1546 if (MO.isReg() && MO.isUse() && MO.isKill() && MO.getReg() &&
1547 mri_->regsOverlap(rep(MO.getReg()), Reg))
1548 MO.unsetIsKill();
1549 }
1550}
1551
Evan Cheng88d1f582007-03-01 02:03:03 +00001552/// hasRegisterDef - True if the instruction defines the specific register.
1553///
1554bool LiveIntervals::hasRegisterDef(MachineInstr *MI, unsigned Reg) {
1555 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1556 MachineOperand &MO = MI->getOperand(i);
1557 if (MO.isReg() && MO.isDef() &&
1558 mri_->regsOverlap(rep(MO.getReg()), Reg))
1559 return true;
1560 }
1561 return false;
1562}
1563
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +00001564LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001565 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +00001566 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +00001567 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001568}