blob: 25642263b0d88512767b4ec5049b280880d2218b [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 Lattnera3b8b5c2004-07-23 17:56:30 +000019#include "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>
Misha Brukman08a6c762004-09-03 18:25:53 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
39namespace {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000040 RegisterAnalysis<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000041
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 Statistic<> numIntervals
43 ("liveintervals", "Number of original intervals");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000044
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000045 Statistic<> numIntervalsAfter
46 ("liveintervals", "Number of intervals after coalescing");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000047
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000048 Statistic<> numJoins
49 ("liveintervals", "Number of interval joins performed");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000050
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000051 Statistic<> numPeep
52 ("liveintervals", "Number of identity moves eliminated after coalescing");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000053
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000054 Statistic<> numFolded
55 ("liveintervals", "Number of loads/stores folded into instructions");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000056
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000057 cl::opt<bool>
58 EnableJoining("join-liveintervals",
59 cl::desc("Join compatible live intervals"),
60 cl::init(true));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000061};
62
63void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const
64{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 AU.addRequired<LiveVariables>();
66 AU.addPreservedID(PHIEliminationID);
67 AU.addRequiredID(PHIEliminationID);
68 AU.addRequiredID(TwoAddressInstructionPassID);
69 AU.addRequired<LoopInfo>();
70 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000071}
72
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000073void LiveIntervals::releaseMemory()
74{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000075 mi2iMap_.clear();
76 i2miMap_.clear();
77 r2iMap_.clear();
78 r2rMap_.clear();
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000079}
80
81
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000082/// runOnMachineFunction - Register allocate the whole function
83///
84bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 mf_ = &fn;
86 tm_ = &fn.getTarget();
87 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000088 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000089 lv_ = &getAnalysis<LiveVariables>();
Alkis Evlogimenos53278012004-08-26 22:22:38 +000090 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenos2c4f7b52004-09-09 19:24:38 +000091 r2rMap_.grow(mf_->getSSARegMap()->getLastVirtReg());
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000092
Chris Lattner799a9192005-04-09 16:17:50 +000093 // If this function has any live ins, insert a dummy instruction at the
94 // beginning of the function that we will pretend "defines" the values. This
95 // is to make the interval analysis simpler by providing a number.
96 if (fn.livein_begin() != fn.livein_end()) {
Chris Lattner712ad0c2005-05-13 07:08:07 +000097 unsigned FirstLiveIn = fn.livein_begin()->first;
Chris Lattner799a9192005-04-09 16:17:50 +000098
99 // Find a reg class that contains this live in.
100 const TargetRegisterClass *RC = 0;
101 for (MRegisterInfo::regclass_iterator RCI = mri_->regclass_begin(),
102 E = mri_->regclass_end(); RCI != E; ++RCI)
103 if ((*RCI)->contains(FirstLiveIn)) {
104 RC = *RCI;
105 break;
106 }
107
108 MachineInstr *OldFirstMI = fn.begin()->begin();
109 mri_->copyRegToReg(*fn.begin(), fn.begin()->begin(),
110 FirstLiveIn, FirstLiveIn, RC);
111 assert(OldFirstMI != fn.begin()->begin() &&
112 "copyRetToReg didn't insert anything!");
113 }
114
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 // number MachineInstrs
116 unsigned miIndex = 0;
117 for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end();
118 mbb != mbbEnd; ++mbb)
119 for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
120 mi != miEnd; ++mi) {
121 bool inserted = mi2iMap_.insert(std::make_pair(mi, miIndex)).second;
122 assert(inserted && "multiple MachineInstr -> index mappings");
123 i2miMap_.push_back(mi);
124 miIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000125 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000126
Chris Lattner799a9192005-04-09 16:17:50 +0000127 // Note intervals due to live-in values.
128 if (fn.livein_begin() != fn.livein_end()) {
129 MachineBasicBlock *Entry = fn.begin();
Chris Lattner712ad0c2005-05-13 07:08:07 +0000130 for (MachineFunction::livein_iterator I = fn.livein_begin(),
Chris Lattner799a9192005-04-09 16:17:50 +0000131 E = fn.livein_end(); I != E; ++I) {
132 handlePhysicalRegisterDef(Entry, Entry->begin(),
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000133 getOrCreateInterval(I->first), 0, 0, true);
Chris Lattner712ad0c2005-05-13 07:08:07 +0000134 for (const unsigned* AS = mri_->getAliasSet(I->first); *AS; ++AS)
Chris Lattner799a9192005-04-09 16:17:50 +0000135 handlePhysicalRegisterDef(Entry, Entry->begin(),
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000136 getOrCreateInterval(*AS), 0, 0, true);
Chris Lattner799a9192005-04-09 16:17:50 +0000137 }
138 }
139
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000141
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000142 numIntervals += getNumIntervals();
143
Chris Lattner38135af2005-05-14 05:34:15 +0000144 DEBUG(std::cerr << "********** INTERVALS **********\n";
145 for (iterator I = begin(), E = end(); I != E; ++I) {
146 I->second.print(std::cerr, mri_);
147 std::cerr << "\n";
148 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000149
150 // join intervals if requested
151 if (EnableJoining) joinIntervals();
152
153 numIntervalsAfter += getNumIntervals();
154
155 // perform a final pass over the instructions and compute spill
156 // weights, coalesce virtual registers and remove identity moves
157 const LoopInfo& loopInfo = getAnalysis<LoopInfo>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000158
159 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
160 mbbi != mbbe; ++mbbi) {
161 MachineBasicBlock* mbb = mbbi;
162 unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
163
164 for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
165 mii != mie; ) {
166 // if the move will be an identity move delete it
167 unsigned srcReg, dstReg, RegRep;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000168 if (tii_->isMoveInstr(*mii, srcReg, dstReg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000169 (RegRep = rep(srcReg)) == rep(dstReg)) {
170 // remove from def list
171 LiveInterval &interval = getOrCreateInterval(RegRep);
172 // remove index -> MachineInstr and
173 // MachineInstr -> index mappings
174 Mi2IndexMap::iterator mi2i = mi2iMap_.find(mii);
175 if (mi2i != mi2iMap_.end()) {
176 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
177 mi2iMap_.erase(mi2i);
178 }
179 mii = mbbi->erase(mii);
180 ++numPeep;
181 }
182 else {
183 for (unsigned i = 0; i < mii->getNumOperands(); ++i) {
184 const MachineOperand& mop = mii->getOperand(i);
185 if (mop.isRegister() && mop.getReg() &&
186 MRegisterInfo::isVirtualRegister(mop.getReg())) {
187 // replace register with representative register
188 unsigned reg = rep(mop.getReg());
189 mii->SetMachineOperandReg(i, reg);
190
191 LiveInterval &RegInt = getInterval(reg);
192 RegInt.weight +=
Chris Lattner7a36ae82004-10-25 18:40:47 +0000193 (mop.isUse() + mop.isDef()) * pow(10.0F, (int)loopDepth);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000194 }
195 }
196 ++mii;
197 }
198 }
199 }
200
Chris Lattner70ca3582004-09-30 15:59:17 +0000201 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000202 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000203}
204
Chris Lattner70ca3582004-09-30 15:59:17 +0000205/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000206void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000207 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000208 for (const_iterator I = begin(), E = end(); I != E; ++I) {
209 I->second.print(std::cerr, mri_);
210 std::cerr << "\n";
211 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000212
213 O << "********** MACHINEINSTRS **********\n";
214 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
215 mbbi != mbbe; ++mbbi) {
216 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
217 for (MachineBasicBlock::iterator mii = mbbi->begin(),
218 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000219 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000220 }
221 }
222}
223
Chris Lattner70ca3582004-09-30 15:59:17 +0000224std::vector<LiveInterval*> LiveIntervals::
225addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) {
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000226 // since this is called after the analysis is done we don't know if
227 // LiveVariables is available
228 lv_ = getAnalysisToUpdate<LiveVariables>();
229
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000230 std::vector<LiveInterval*> added;
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000231
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000232 assert(li.weight != HUGE_VAL &&
233 "attempt to spill already spilled interval!");
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000234
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000235 DEBUG(std::cerr << "\t\t\t\tadding intervals for spills for interval: "
236 << li << '\n');
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000237
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000239
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000240 for (LiveInterval::Ranges::const_iterator
241 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
242 unsigned index = getBaseIndex(i->start);
243 unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM;
244 for (; index != end; index += InstrSlots::NUM) {
245 // skip deleted instructions
246 while (index != end && !getInstructionFromIndex(index))
247 index += InstrSlots::NUM;
248 if (index == end) break;
Chris Lattner8640f4e2004-07-19 15:16:53 +0000249
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 MachineBasicBlock::iterator mi = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000251
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000252 for_operand:
253 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
254 MachineOperand& mop = mi->getOperand(i);
255 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattner477e4552004-09-30 16:10:45 +0000256 // First thing, attempt to fold the memory reference into the
257 // instruction. If we can do this, we don't need to insert spill
258 // code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000259 if (MachineInstr* fmi = mri_->foldMemoryOperand(mi, i, slot)) {
260 if (lv_)
261 lv_->instructionChanged(mi, fmi);
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000262 vrm.virtFolded(li.reg, mi, i, fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000263 mi2iMap_.erase(mi);
264 i2miMap_[index/InstrSlots::NUM] = fmi;
265 mi2iMap_[fmi] = index;
Chris Lattner477e4552004-09-30 16:10:45 +0000266 MachineBasicBlock &MBB = *mi->getParent();
267 mi = MBB.insert(MBB.erase(mi), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000268 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000269
270 // Folding the load/store can completely change the instruction in
271 // unpredictable ways, rescan it from the beginning.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000272 goto for_operand;
Chris Lattner477e4552004-09-30 16:10:45 +0000273 } else {
Chris Lattner70ca3582004-09-30 15:59:17 +0000274 // This is tricky. We need to add information in the interval about
275 // the spill code so we have to use our extra load/store slots.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 //
Chris Lattner70ca3582004-09-30 15:59:17 +0000277 // If we have a use we are going to have a load so we start the
278 // interval from the load slot onwards. Otherwise we start from the
279 // def slot.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 unsigned start = (mop.isUse() ?
281 getLoadIndex(index) :
282 getDefIndex(index));
Chris Lattner70ca3582004-09-30 15:59:17 +0000283 // If we have a def we are going to have a store right after it so
284 // we end the interval after the use of the next
285 // instruction. Otherwise we end after the use of this instruction.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 unsigned end = 1 + (mop.isDef() ?
287 getStoreIndex(index) :
288 getUseIndex(index));
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000289
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 // create a new register for this spill
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000291 unsigned nReg = mf_->getSSARegMap()->createVirtualRegister(rc);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000292 mi->SetMachineOperandReg(i, nReg);
293 vrm.grow();
294 vrm.assignVirt2StackSlot(nReg, slot);
295 LiveInterval& nI = getOrCreateInterval(nReg);
296 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000297
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298 // the spill weight is now infinity as it
299 // cannot be spilled again
Chris Lattner28696be2005-01-08 19:55:00 +0000300 nI.weight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000301 LiveRange LR(start, end, nI.getNextValue());
302 DEBUG(std::cerr << " +" << LR);
303 nI.addRange(LR);
304 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000305
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000306 // update live variables if it is available
307 if (lv_)
308 lv_->addVirtualRegisterKilled(nReg, mi);
309 DEBUG(std::cerr << "\t\t\t\tadded new interval: " << nI << '\n');
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000311 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000312 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000313 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000315
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000316 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000317}
318
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000319void LiveIntervals::printRegName(unsigned reg) const
320{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000321 if (MRegisterInfo::isPhysicalRegister(reg))
322 std::cerr << mri_->getName(reg);
323 else
324 std::cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000325}
326
327void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
328 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000329 LiveInterval& interval)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000330{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000331 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
332 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000333
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000334 // Virtual registers may be defined multiple times (due to phi
335 // elimination and 2-addr elimination). Much of what we do only has to be
336 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337 // time we see a vreg.
338 if (interval.empty()) {
339 // Get the Idx of the defining instructions.
340 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Chris Lattner6097d132004-07-19 02:15:56 +0000341
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000342 unsigned ValNum = interval.getNextValue();
343 assert(ValNum == 0 && "First value in interval is not 0?");
344 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000345
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346 // Loop over all of the blocks that the vreg is defined in. There are
347 // two cases we have to handle here. The most common case is a vreg
348 // whose lifetime is contained within a basic block. In this case there
349 // will be a single kill, in MBB, which comes after the definition.
350 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
351 // FIXME: what about dead vars?
352 unsigned killIdx;
353 if (vi.Kills[0] != mi)
354 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
355 else
356 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000357
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000358 // If the kill happens after the definition, we have an intra-block
359 // live range.
360 if (killIdx > defIndex) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000361 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000362 "Shouldn't be alive across any blocks!");
363 LiveRange LR(defIndex, killIdx, ValNum);
364 interval.addRange(LR);
365 DEBUG(std::cerr << " +" << LR << "\n");
366 return;
367 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000368 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000369
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000370 // The other case we handle is when a virtual register lives to the end
371 // of the defining block, potentially live across some blocks, then is
372 // live into some number of blocks, but gets killed. Start by adding a
373 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000374 LiveRange NewLR(defIndex,
375 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
376 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000377 DEBUG(std::cerr << " +" << NewLR);
378 interval.addRange(NewLR);
379
380 // Iterate over all of the blocks that the variable is completely
381 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
382 // live interval.
383 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
384 if (vi.AliveBlocks[i]) {
385 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
386 if (!mbb->empty()) {
387 LiveRange LR(getInstructionIndex(&mbb->front()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000388 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389 ValNum);
390 interval.addRange(LR);
391 DEBUG(std::cerr << " +" << LR);
392 }
393 }
394 }
395
396 // Finally, this virtual register is live from the start of any killing
397 // block to the 'use' slot of the killing instruction.
398 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
399 MachineInstr *Kill = vi.Kills[i];
400 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000401 getUseIndex(getInstructionIndex(Kill))+1,
402 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000403 interval.addRange(LR);
404 DEBUG(std::cerr << " +" << LR);
405 }
406
407 } else {
408 // If this is the second time we see a virtual register definition, it
409 // must be due to phi elimination or two addr elimination. If this is
410 // the result of two address elimination, then the vreg is the first
411 // operand, and is a def-and-use.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000412 if (mi->getOperand(0).isRegister() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000413 mi->getOperand(0).getReg() == interval.reg &&
414 mi->getOperand(0).isDef() && mi->getOperand(0).isUse()) {
415 // If this is a two-address definition, then we have already processed
416 // the live range. The only problem is that we didn't realize there
417 // are actually two values in the live interval. Because of this we
418 // need to take the LiveRegion that defines this register and split it
419 // into two values.
420 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
421 unsigned RedefIndex = getDefIndex(getInstructionIndex(mi));
422
423 // Delete the initial value, which should be short and continuous,
424 // becuase the 2-addr copy must be in the same MBB as the redef.
425 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000426
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 LiveRange LR(DefIndex, RedefIndex, interval.getNextValue());
428 DEBUG(std::cerr << " replace range with " << LR);
429 interval.addRange(LR);
430
431 // If this redefinition is dead, we need to add a dummy unit live
432 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000433 if (lv_->RegisterDefIsDead(mi, interval.reg))
434 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435
436 DEBUG(std::cerr << "RESULT: " << interval);
437
438 } else {
439 // Otherwise, this must be because of phi elimination. If this is the
440 // first redefinition of the vreg that we have seen, go back and change
441 // the live range in the PHI block to be a different value number.
442 if (interval.containsOneValue()) {
443 assert(vi.Kills.size() == 1 &&
444 "PHI elimination vreg should have one kill, the PHI itself!");
445
446 // Remove the old range that we now know has an incorrect number.
447 MachineInstr *Killer = vi.Kills[0];
448 unsigned Start = getInstructionIndex(Killer->getParent()->begin());
449 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
450 DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "
451 << interval << "\n");
452 interval.removeRange(Start, End);
453 DEBUG(std::cerr << "RESULT: " << interval);
454
455 // Replace the interval with one of a NEW value number.
456 LiveRange LR(Start, End, interval.getNextValue());
457 DEBUG(std::cerr << " replace range with " << LR);
458 interval.addRange(LR);
459 DEBUG(std::cerr << "RESULT: " << interval);
460 }
461
462 // In the case of PHI elimination, each variable definition is only
463 // live until the end of the block. We've already taken care of the
464 // rest of the live range.
465 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000466 LiveRange LR(defIndex,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000467 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
468 interval.getNextValue());
469 interval.addRange(LR);
470 DEBUG(std::cerr << " +" << LR);
471 }
472 }
473
474 DEBUG(std::cerr << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000475}
476
Chris Lattnerf35fef72004-07-23 21:24:19 +0000477void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000478 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000479 LiveInterval& interval,
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000480 unsigned SrcReg, unsigned DestReg,
481 bool isLiveIn)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000482{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000483 // A physical register cannot be live across basic block, so its
484 // lifetime must end somewhere in its defining basic block.
485 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
486 typedef LiveVariables::killed_iterator KillIter;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000487
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000488 unsigned baseIndex = getInstructionIndex(mi);
489 unsigned start = getDefIndex(baseIndex);
490 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000491
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000492 // If it is not used after definition, it is considered dead at
493 // the instruction defining it. Hence its interval is:
494 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000495 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
496 DEBUG(std::cerr << " dead");
497 end = getDefIndex(start) + 1;
498 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000499 }
500
501 // If it is not dead on definition, it must be killed by a
502 // subsequent instruction. Hence its interval is:
503 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000504 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000505 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000506 if (lv_->KillsRegister(mi, interval.reg)) {
507 DEBUG(std::cerr << " killed");
508 end = getUseIndex(baseIndex) + 1;
509 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000510 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000512
513 // The only case we should have a dead physreg here without a killing or
514 // instruction where we know it's dead is if it is live-in to the function
515 // and never used.
516 assert(isLiveIn && "physreg was not killed in defining block!");
517 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000518
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000519exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000520 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000521
522 // Finally, if this is defining a new range for the physical register, and if
523 // that physreg is just a copy from a vreg, and if THAT vreg was a copy from
524 // the physreg, then the new fragment has the same value as the one copied
525 // into the vreg.
526 if (interval.reg == DestReg && !interval.empty() &&
Chris Lattnere97568c2005-03-10 20:59:51 +0000527 MRegisterInfo::isVirtualRegister(SrcReg)) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000528
529 // Get the live interval for the vreg, see if it is defined by a copy.
530 LiveInterval &SrcInterval = getOrCreateInterval(SrcReg);
531
532 if (SrcInterval.containsOneValue()) {
533 assert(!SrcInterval.empty() && "Can't contain a value and be empty!");
534
535 // Get the first index of the first range. Though the interval may have
536 // multiple liveranges in it, we only check the first.
537 unsigned StartIdx = SrcInterval.begin()->start;
538 MachineInstr *SrcDefMI = getInstructionFromIndex(StartIdx);
539
540 // Check to see if the vreg was defined by a copy instruction, and that
541 // the source was this physreg.
542 unsigned VRegSrcSrc, VRegSrcDest;
543 if (tii_->isMoveInstr(*SrcDefMI, VRegSrcSrc, VRegSrcDest) &&
544 SrcReg == VRegSrcDest && VRegSrcSrc == DestReg) {
545 // Okay, now we know that the vreg was defined by a copy from this
546 // physreg. Find the value number being copied and use it as the value
547 // for this range.
548 const LiveRange *DefRange = interval.getLiveRangeContaining(StartIdx-1);
549 if (DefRange) {
550 LiveRange LR(start, end, DefRange->ValId);
551 interval.addRange(LR);
552 DEBUG(std::cerr << " +" << LR << '\n');
553 return;
554 }
555 }
556 }
557 }
558
559
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000560 LiveRange LR(start, end, interval.getNextValue());
561 interval.addRange(LR);
562 DEBUG(std::cerr << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000563}
564
Chris Lattnerf35fef72004-07-23 21:24:19 +0000565void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
566 MachineBasicBlock::iterator MI,
567 unsigned reg) {
568 if (MRegisterInfo::isVirtualRegister(reg))
569 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000570 else if (allocatableRegs_[reg]) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000571 unsigned SrcReg = 0, DestReg = 0;
572 bool IsMove = tii_->isMoveInstr(*MI, SrcReg, DestReg);
573
574 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg),
575 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000576 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattnerf768bba2005-03-09 23:05:19 +0000577 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS),
578 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000579 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000580}
581
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000582/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000583/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000584/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000585/// which a variable is live
586void LiveIntervals::computeIntervals()
587{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000588 DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n");
589 DEBUG(std::cerr << "********** Function: "
590 << ((Value*)mf_->getFunction())->getName() << '\n');
Chris Lattner799a9192005-04-09 16:17:50 +0000591 bool IgnoreFirstInstr = mf_->livein_begin() != mf_->livein_end();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000592
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000593 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 I != E; ++I) {
595 MachineBasicBlock* mbb = I;
596 DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000597
Chris Lattner799a9192005-04-09 16:17:50 +0000598 MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
599 if (IgnoreFirstInstr) { ++mi; IgnoreFirstInstr = false; }
600 for (; mi != miEnd; ++mi) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000601 const TargetInstrDescriptor& tid =
602 tm_->getInstrInfo()->get(mi->getOpcode());
Chris Lattner477e4552004-09-30 16:10:45 +0000603 DEBUG(std::cerr << getInstructionIndex(mi) << "\t" << *mi);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000604
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000605 // handle implicit defs
606 for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
607 handleRegisterDef(mbb, mi, *id);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000608
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000609 // handle explicit defs
610 for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
611 MachineOperand& mop = mi->getOperand(i);
612 // handle register defs - build intervals
613 if (mop.isRegister() && mop.getReg() && mop.isDef())
614 handleRegisterDef(mbb, mi, mop.getReg());
615 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000616 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000617 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000618}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000619
Chris Lattner1c5c0442004-07-19 14:08:10 +0000620void LiveIntervals::joinIntervalsInMachineBB(MachineBasicBlock *MBB) {
Chris Lattner7ac2d312004-07-24 02:59:07 +0000621 DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000622
Chris Lattner7ac2d312004-07-24 02:59:07 +0000623 for (MachineBasicBlock::iterator mi = MBB->begin(), mie = MBB->end();
624 mi != mie; ++mi) {
625 DEBUG(std::cerr << getInstructionIndex(mi) << '\t' << *mi);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000626
Chris Lattner7ac2d312004-07-24 02:59:07 +0000627 // we only join virtual registers with allocatable
628 // physical registers since we do not have liveness information
629 // on not allocatable physical registers
630 unsigned regA, regB;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000631 if (tii_->isMoveInstr(*mi, regA, regB) &&
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000632 (MRegisterInfo::isVirtualRegister(regA) || allocatableRegs_[regA]) &&
633 (MRegisterInfo::isVirtualRegister(regB) || allocatableRegs_[regB])) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000634
Chris Lattner7ac2d312004-07-24 02:59:07 +0000635 // Get representative registers.
636 regA = rep(regA);
637 regB = rep(regB);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000638
Chris Lattner7ac2d312004-07-24 02:59:07 +0000639 // If they are already joined we continue.
640 if (regA == regB)
641 continue;
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000642
Chris Lattner7ac2d312004-07-24 02:59:07 +0000643 // If they are both physical registers, we cannot join them.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000644 if (MRegisterInfo::isPhysicalRegister(regA) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000645 MRegisterInfo::isPhysicalRegister(regB))
646 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000647
Chris Lattner7ac2d312004-07-24 02:59:07 +0000648 // If they are not of the same register class, we cannot join them.
649 if (differingRegisterClasses(regA, regB))
650 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000651
Chris Lattner7ac2d312004-07-24 02:59:07 +0000652 LiveInterval &IntA = getInterval(regA);
653 LiveInterval &IntB = getInterval(regB);
654 assert(IntA.reg == regA && IntB.reg == regB &&
655 "Register mapping is horribly broken!");
Chris Lattner060913c2004-07-24 04:32:22 +0000656
657 DEBUG(std::cerr << "\t\tInspecting " << IntA << " and " << IntB << ": ");
658
Chris Lattner4df98e52004-07-24 03:32:06 +0000659 // If two intervals contain a single value and are joined by a copy, it
660 // does not matter if the intervals overlap, they can always be joined.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000661 bool TriviallyJoinable =
662 IntA.containsOneValue() && IntB.containsOneValue();
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000663
Chris Lattner7ac2d312004-07-24 02:59:07 +0000664 unsigned MIDefIdx = getDefIndex(getInstructionIndex(mi));
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000665 if ((TriviallyJoinable || IntB.joinable(IntA, MIDefIdx)) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000666 !overlapsAliases(&IntA, &IntB)) {
667 IntB.join(IntA, MIDefIdx);
Chris Lattnercef21c32005-07-27 23:11:25 +0000668 DEBUG(std::cerr << "Joined. Result = " << IntB << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000669
Chris Lattner7ac2d312004-07-24 02:59:07 +0000670 if (!MRegisterInfo::isPhysicalRegister(regA)) {
Chris Lattner4df98e52004-07-24 03:32:06 +0000671 r2iMap_.erase(regA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000672 r2rMap_[regA] = regB;
673 } else {
674 // Otherwise merge the data structures the other way so we don't lose
675 // the physreg information.
676 r2rMap_[regB] = regA;
677 IntB.reg = regA;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000678 IntA.swap(IntB);
Chris Lattner4df98e52004-07-24 03:32:06 +0000679 r2iMap_.erase(regB);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000680 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000681 ++numJoins;
682 } else {
683 DEBUG(std::cerr << "Interference!\n");
684 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000685 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000686 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000687}
688
Chris Lattnercc0d1562004-07-19 14:40:29 +0000689namespace {
690 // DepthMBBCompare - Comparison predicate that sort first based on the loop
691 // depth of the basic block (the unsigned), and then on the MBB number.
692 struct DepthMBBCompare {
693 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
694 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
695 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000696 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000697 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +0000698 }
699 };
700}
Chris Lattner1c5c0442004-07-19 14:08:10 +0000701
Chris Lattnercc0d1562004-07-19 14:40:29 +0000702void LiveIntervals::joinIntervals() {
703 DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n");
704
705 const LoopInfo &LI = getAnalysis<LoopInfo>();
706 if (LI.begin() == LI.end()) {
707 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +0000708 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
709 I != E; ++I)
710 joinIntervalsInMachineBB(I);
Chris Lattnercc0d1562004-07-19 14:40:29 +0000711 } else {
712 // Otherwise, join intervals in inner loops before other intervals.
713 // Unfortunately we can't just iterate over loop hierarchy here because
714 // there may be more MBB's than BB's. Collect MBB's for sorting.
715 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
716 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
717 I != E; ++I)
718 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
719
720 // Sort by loop depth.
721 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
722
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000723 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +0000724 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
725 joinIntervalsInMachineBB(MBBs[i].second);
726 }
Chris Lattnerc83e40d2004-07-25 03:24:11 +0000727
728 DEBUG(std::cerr << "*** Register mapping ***\n");
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000729 DEBUG(for (int i = 0, e = r2rMap_.size(); i != e; ++i)
730 if (r2rMap_[i])
731 std::cerr << " reg " << i << " -> reg " << r2rMap_[i] << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000732}
733
Chris Lattner7ac2d312004-07-24 02:59:07 +0000734/// Return true if the two specified registers belong to different register
735/// classes. The registers may be either phys or virt regs.
736bool LiveIntervals::differingRegisterClasses(unsigned RegA,
737 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000738
Chris Lattner7ac2d312004-07-24 02:59:07 +0000739 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000740 if (MRegisterInfo::isPhysicalRegister(RegA)) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000741 assert(MRegisterInfo::isVirtualRegister(RegB) &&
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000742 "Shouldn't consider two physregs!");
743 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
744 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000745
746 // Compare against the regclass for the second reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000747 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000748 if (MRegisterInfo::isVirtualRegister(RegB))
749 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
750 else
Chris Lattnerd0d0a1a2004-08-24 17:48:29 +0000751 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000752}
753
754bool LiveIntervals::overlapsAliases(const LiveInterval *LHS,
755 const LiveInterval *RHS) const {
756 if (!MRegisterInfo::isPhysicalRegister(LHS->reg)) {
757 if (!MRegisterInfo::isPhysicalRegister(RHS->reg))
758 return false; // vreg-vreg merge has no aliases!
759 std::swap(LHS, RHS);
760 }
761
762 assert(MRegisterInfo::isPhysicalRegister(LHS->reg) &&
763 MRegisterInfo::isVirtualRegister(RHS->reg) &&
764 "first interval must describe a physical register");
765
Chris Lattner4df98e52004-07-24 03:32:06 +0000766 for (const unsigned *AS = mri_->getAliasSet(LHS->reg); *AS; ++AS)
767 if (RHS->overlaps(getInterval(*AS)))
768 return true;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000769
Chris Lattner4df98e52004-07-24 03:32:06 +0000770 return false;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000771}
772
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000773LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000774 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Chris Lattner28696be2005-01-08 19:55:00 +0000775 (float)HUGE_VAL :0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000776 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000777}