blob: b242a9c6942fd9a99e5745d84b720fd0e2b5c5ae [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.addPreserved<LiveVariables>();
66 AU.addRequired<LiveVariables>();
67 AU.addPreservedID(PHIEliminationID);
68 AU.addRequiredID(PHIEliminationID);
69 AU.addRequiredID(TwoAddressInstructionPassID);
70 AU.addRequired<LoopInfo>();
71 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000072}
73
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000074void LiveIntervals::releaseMemory()
75{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000076 mi2iMap_.clear();
77 i2miMap_.clear();
78 r2iMap_.clear();
79 r2rMap_.clear();
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000080}
81
82
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000083/// runOnMachineFunction - Register allocate the whole function
84///
85bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 mf_ = &fn;
87 tm_ = &fn.getTarget();
88 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000089 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 lv_ = &getAnalysis<LiveVariables>();
Alkis Evlogimenos53278012004-08-26 22:22:38 +000091 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenos2c4f7b52004-09-09 19:24:38 +000092 r2rMap_.grow(mf_->getSSARegMap()->getLastVirtReg());
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000093
Chris Lattner799a9192005-04-09 16:17:50 +000094 // If this function has any live ins, insert a dummy instruction at the
95 // beginning of the function that we will pretend "defines" the values. This
96 // is to make the interval analysis simpler by providing a number.
97 if (fn.livein_begin() != fn.livein_end()) {
Chris Lattner712ad0c2005-05-13 07:08:07 +000098 unsigned FirstLiveIn = fn.livein_begin()->first;
Chris Lattner799a9192005-04-09 16:17:50 +000099
100 // Find a reg class that contains this live in.
101 const TargetRegisterClass *RC = 0;
102 for (MRegisterInfo::regclass_iterator RCI = mri_->regclass_begin(),
103 E = mri_->regclass_end(); RCI != E; ++RCI)
104 if ((*RCI)->contains(FirstLiveIn)) {
105 RC = *RCI;
106 break;
107 }
108
109 MachineInstr *OldFirstMI = fn.begin()->begin();
110 mri_->copyRegToReg(*fn.begin(), fn.begin()->begin(),
111 FirstLiveIn, FirstLiveIn, RC);
112 assert(OldFirstMI != fn.begin()->begin() &&
113 "copyRetToReg didn't insert anything!");
114 }
115
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116 // number MachineInstrs
117 unsigned miIndex = 0;
118 for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end();
119 mbb != mbbEnd; ++mbb)
120 for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
121 mi != miEnd; ++mi) {
122 bool inserted = mi2iMap_.insert(std::make_pair(mi, miIndex)).second;
123 assert(inserted && "multiple MachineInstr -> index mappings");
124 i2miMap_.push_back(mi);
125 miIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000126 }
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000127
Chris Lattner799a9192005-04-09 16:17:50 +0000128 // Note intervals due to live-in values.
129 if (fn.livein_begin() != fn.livein_end()) {
130 MachineBasicBlock *Entry = fn.begin();
Chris Lattner712ad0c2005-05-13 07:08:07 +0000131 for (MachineFunction::livein_iterator I = fn.livein_begin(),
Chris Lattner799a9192005-04-09 16:17:50 +0000132 E = fn.livein_end(); I != E; ++I) {
133 handlePhysicalRegisterDef(Entry, Entry->begin(),
Chris Lattner712ad0c2005-05-13 07:08:07 +0000134 getOrCreateInterval(I->first), 0, 0);
135 for (const unsigned* AS = mri_->getAliasSet(I->first); *AS; ++AS)
Chris Lattner799a9192005-04-09 16:17:50 +0000136 handlePhysicalRegisterDef(Entry, Entry->begin(),
137 getOrCreateInterval(*AS), 0, 0);
138 }
139 }
140
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000142
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000143 numIntervals += getNumIntervals();
144
Chris Lattner38135af2005-05-14 05:34:15 +0000145 DEBUG(std::cerr << "********** INTERVALS **********\n";
146 for (iterator I = begin(), E = end(); I != E; ++I) {
147 I->second.print(std::cerr, mri_);
148 std::cerr << "\n";
149 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000150
151 // join intervals if requested
152 if (EnableJoining) joinIntervals();
153
154 numIntervalsAfter += getNumIntervals();
155
156 // perform a final pass over the instructions and compute spill
157 // weights, coalesce virtual registers and remove identity moves
158 const LoopInfo& loopInfo = getAnalysis<LoopInfo>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000159
160 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
161 mbbi != mbbe; ++mbbi) {
162 MachineBasicBlock* mbb = mbbi;
163 unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
164
165 for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
166 mii != mie; ) {
167 // if the move will be an identity move delete it
168 unsigned srcReg, dstReg, RegRep;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000169 if (tii_->isMoveInstr(*mii, srcReg, dstReg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000170 (RegRep = rep(srcReg)) == rep(dstReg)) {
171 // remove from def list
172 LiveInterval &interval = getOrCreateInterval(RegRep);
173 // remove index -> MachineInstr and
174 // MachineInstr -> index mappings
175 Mi2IndexMap::iterator mi2i = mi2iMap_.find(mii);
176 if (mi2i != mi2iMap_.end()) {
177 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
178 mi2iMap_.erase(mi2i);
179 }
180 mii = mbbi->erase(mii);
181 ++numPeep;
182 }
183 else {
184 for (unsigned i = 0; i < mii->getNumOperands(); ++i) {
185 const MachineOperand& mop = mii->getOperand(i);
186 if (mop.isRegister() && mop.getReg() &&
187 MRegisterInfo::isVirtualRegister(mop.getReg())) {
188 // replace register with representative register
189 unsigned reg = rep(mop.getReg());
190 mii->SetMachineOperandReg(i, reg);
191
192 LiveInterval &RegInt = getInterval(reg);
193 RegInt.weight +=
Chris Lattner7a36ae82004-10-25 18:40:47 +0000194 (mop.isUse() + mop.isDef()) * pow(10.0F, (int)loopDepth);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000195 }
196 }
197 ++mii;
198 }
199 }
200 }
201
Chris Lattner70ca3582004-09-30 15:59:17 +0000202 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000203 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000204}
205
Chris Lattner70ca3582004-09-30 15:59:17 +0000206/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000207void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000208 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000209 for (const_iterator I = begin(), E = end(); I != E; ++I) {
210 I->second.print(std::cerr, mri_);
211 std::cerr << "\n";
212 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000213
214 O << "********** MACHINEINSTRS **********\n";
215 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
216 mbbi != mbbe; ++mbbi) {
217 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
218 for (MachineBasicBlock::iterator mii = mbbi->begin(),
219 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000220 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000221 }
222 }
223}
224
Chris Lattner70ca3582004-09-30 15:59:17 +0000225std::vector<LiveInterval*> LiveIntervals::
226addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) {
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000227 // since this is called after the analysis is done we don't know if
228 // LiveVariables is available
229 lv_ = getAnalysisToUpdate<LiveVariables>();
230
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000231 std::vector<LiveInterval*> added;
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000232
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000233 assert(li.weight != HUGE_VAL &&
234 "attempt to spill already spilled interval!");
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000235
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000236 DEBUG(std::cerr << "\t\t\t\tadding intervals for spills for interval: "
237 << li << '\n');
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000238
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000240
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000241 for (LiveInterval::Ranges::const_iterator
242 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
243 unsigned index = getBaseIndex(i->start);
244 unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM;
245 for (; index != end; index += InstrSlots::NUM) {
246 // skip deleted instructions
247 while (index != end && !getInstructionFromIndex(index))
248 index += InstrSlots::NUM;
249 if (index == end) break;
Chris Lattner8640f4e2004-07-19 15:16:53 +0000250
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000251 MachineBasicBlock::iterator mi = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000252
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000253 for_operand:
254 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
255 MachineOperand& mop = mi->getOperand(i);
256 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattner477e4552004-09-30 16:10:45 +0000257 // First thing, attempt to fold the memory reference into the
258 // instruction. If we can do this, we don't need to insert spill
259 // code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000260 if (MachineInstr* fmi = mri_->foldMemoryOperand(mi, i, slot)) {
261 if (lv_)
262 lv_->instructionChanged(mi, fmi);
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000263 vrm.virtFolded(li.reg, mi, i, fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000264 mi2iMap_.erase(mi);
265 i2miMap_[index/InstrSlots::NUM] = fmi;
266 mi2iMap_[fmi] = index;
Chris Lattner477e4552004-09-30 16:10:45 +0000267 MachineBasicBlock &MBB = *mi->getParent();
268 mi = MBB.insert(MBB.erase(mi), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000269 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000270
271 // Folding the load/store can completely change the instruction in
272 // unpredictable ways, rescan it from the beginning.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000273 goto for_operand;
Chris Lattner477e4552004-09-30 16:10:45 +0000274 } else {
Chris Lattner70ca3582004-09-30 15:59:17 +0000275 // This is tricky. We need to add information in the interval about
276 // the spill code so we have to use our extra load/store slots.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000277 //
Chris Lattner70ca3582004-09-30 15:59:17 +0000278 // If we have a use we are going to have a load so we start the
279 // interval from the load slot onwards. Otherwise we start from the
280 // def slot.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000281 unsigned start = (mop.isUse() ?
282 getLoadIndex(index) :
283 getDefIndex(index));
Chris Lattner70ca3582004-09-30 15:59:17 +0000284 // If we have a def we are going to have a store right after it so
285 // we end the interval after the use of the next
286 // instruction. Otherwise we end after the use of this instruction.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 unsigned end = 1 + (mop.isDef() ?
288 getStoreIndex(index) :
289 getUseIndex(index));
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000290
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 // create a new register for this spill
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000292 unsigned nReg = mf_->getSSARegMap()->createVirtualRegister(rc);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000293 mi->SetMachineOperandReg(i, nReg);
294 vrm.grow();
295 vrm.assignVirt2StackSlot(nReg, slot);
296 LiveInterval& nI = getOrCreateInterval(nReg);
297 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000298
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 // the spill weight is now infinity as it
300 // cannot be spilled again
Chris Lattner28696be2005-01-08 19:55:00 +0000301 nI.weight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 LiveRange LR(start, end, nI.getNextValue());
303 DEBUG(std::cerr << " +" << LR);
304 nI.addRange(LR);
305 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000306
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000307 // update live variables if it is available
308 if (lv_)
309 lv_->addVirtualRegisterKilled(nReg, mi);
310 DEBUG(std::cerr << "\t\t\t\tadded new interval: " << nI << '\n');
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000312 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000314 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000315 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000316
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000318}
319
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000320void LiveIntervals::printRegName(unsigned reg) const
321{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000322 if (MRegisterInfo::isPhysicalRegister(reg))
323 std::cerr << mri_->getName(reg);
324 else
325 std::cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000326}
327
328void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
329 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000330 LiveInterval& interval)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000331{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
333 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000334
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000335 // Virtual registers may be defined multiple times (due to phi
336 // elimination and 2-addr elimination). Much of what we do only has to be
337 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000338 // time we see a vreg.
339 if (interval.empty()) {
340 // Get the Idx of the defining instructions.
341 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Chris Lattner6097d132004-07-19 02:15:56 +0000342
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000343 unsigned ValNum = interval.getNextValue();
344 assert(ValNum == 0 && "First value in interval is not 0?");
345 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000346
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347 // Loop over all of the blocks that the vreg is defined in. There are
348 // two cases we have to handle here. The most common case is a vreg
349 // whose lifetime is contained within a basic block. In this case there
350 // will be a single kill, in MBB, which comes after the definition.
351 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
352 // FIXME: what about dead vars?
353 unsigned killIdx;
354 if (vi.Kills[0] != mi)
355 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
356 else
357 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000358
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000359 // If the kill happens after the definition, we have an intra-block
360 // live range.
361 if (killIdx > defIndex) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000362 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000363 "Shouldn't be alive across any blocks!");
364 LiveRange LR(defIndex, killIdx, ValNum);
365 interval.addRange(LR);
366 DEBUG(std::cerr << " +" << LR << "\n");
367 return;
368 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000369 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000370
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 // The other case we handle is when a virtual register lives to the end
372 // of the defining block, potentially live across some blocks, then is
373 // live into some number of blocks, but gets killed. Start by adding a
374 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000375 LiveRange NewLR(defIndex,
376 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
377 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000378 DEBUG(std::cerr << " +" << NewLR);
379 interval.addRange(NewLR);
380
381 // Iterate over all of the blocks that the variable is completely
382 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
383 // live interval.
384 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
385 if (vi.AliveBlocks[i]) {
386 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
387 if (!mbb->empty()) {
388 LiveRange LR(getInstructionIndex(&mbb->front()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000389 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000390 ValNum);
391 interval.addRange(LR);
392 DEBUG(std::cerr << " +" << LR);
393 }
394 }
395 }
396
397 // Finally, this virtual register is live from the start of any killing
398 // block to the 'use' slot of the killing instruction.
399 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
400 MachineInstr *Kill = vi.Kills[i];
401 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000402 getUseIndex(getInstructionIndex(Kill))+1,
403 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000404 interval.addRange(LR);
405 DEBUG(std::cerr << " +" << LR);
406 }
407
408 } else {
409 // If this is the second time we see a virtual register definition, it
410 // must be due to phi elimination or two addr elimination. If this is
411 // the result of two address elimination, then the vreg is the first
412 // operand, and is a def-and-use.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000413 if (mi->getOperand(0).isRegister() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000414 mi->getOperand(0).getReg() == interval.reg &&
415 mi->getOperand(0).isDef() && mi->getOperand(0).isUse()) {
416 // If this is a two-address definition, then we have already processed
417 // the live range. The only problem is that we didn't realize there
418 // are actually two values in the live interval. Because of this we
419 // need to take the LiveRegion that defines this register and split it
420 // into two values.
421 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
422 unsigned RedefIndex = getDefIndex(getInstructionIndex(mi));
423
424 // Delete the initial value, which should be short and continuous,
425 // becuase the 2-addr copy must be in the same MBB as the redef.
426 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000427
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428 LiveRange LR(DefIndex, RedefIndex, interval.getNextValue());
429 DEBUG(std::cerr << " replace range with " << LR);
430 interval.addRange(LR);
431
432 // If this redefinition is dead, we need to add a dummy unit live
433 // range covering the def slot.
434 for (LiveVariables::killed_iterator KI = lv_->dead_begin(mi),
435 E = lv_->dead_end(mi); KI != E; ++KI)
436 if (KI->second == interval.reg) {
437 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
438 break;
439 }
440
441 DEBUG(std::cerr << "RESULT: " << interval);
442
443 } else {
444 // Otherwise, this must be because of phi elimination. If this is the
445 // first redefinition of the vreg that we have seen, go back and change
446 // the live range in the PHI block to be a different value number.
447 if (interval.containsOneValue()) {
448 assert(vi.Kills.size() == 1 &&
449 "PHI elimination vreg should have one kill, the PHI itself!");
450
451 // Remove the old range that we now know has an incorrect number.
452 MachineInstr *Killer = vi.Kills[0];
453 unsigned Start = getInstructionIndex(Killer->getParent()->begin());
454 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
455 DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "
456 << interval << "\n");
457 interval.removeRange(Start, End);
458 DEBUG(std::cerr << "RESULT: " << interval);
459
460 // Replace the interval with one of a NEW value number.
461 LiveRange LR(Start, End, interval.getNextValue());
462 DEBUG(std::cerr << " replace range with " << LR);
463 interval.addRange(LR);
464 DEBUG(std::cerr << "RESULT: " << interval);
465 }
466
467 // In the case of PHI elimination, each variable definition is only
468 // live until the end of the block. We've already taken care of the
469 // rest of the live range.
470 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000471 LiveRange LR(defIndex,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000472 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
473 interval.getNextValue());
474 interval.addRange(LR);
475 DEBUG(std::cerr << " +" << LR);
476 }
477 }
478
479 DEBUG(std::cerr << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000480}
481
Chris Lattnerf35fef72004-07-23 21:24:19 +0000482void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000483 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000484 LiveInterval& interval,
485 unsigned SrcReg, unsigned DestReg)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000486{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000487 // A physical register cannot be live across basic block, so its
488 // lifetime must end somewhere in its defining basic block.
489 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
490 typedef LiveVariables::killed_iterator KillIter;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000491
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000492 unsigned baseIndex = getInstructionIndex(mi);
493 unsigned start = getDefIndex(baseIndex);
494 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000495
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000496 // If it is not used after definition, it is considered dead at
497 // the instruction defining it. Hence its interval is:
498 // [defSlot(def), defSlot(def)+1)
499 for (KillIter ki = lv_->dead_begin(mi), ke = lv_->dead_end(mi);
500 ki != ke; ++ki) {
501 if (interval.reg == ki->second) {
502 DEBUG(std::cerr << " dead");
503 end = getDefIndex(start) + 1;
504 goto exit;
505 }
506 }
507
508 // If it is not dead on definition, it must be killed by a
509 // subsequent instruction. Hence its interval is:
510 // [defSlot(def), useSlot(kill)+1)
511 while (true) {
512 ++mi;
513 assert(mi != MBB->end() && "physreg was not killed in defining block!");
514 baseIndex += InstrSlots::NUM;
515 for (KillIter ki = lv_->killed_begin(mi), ke = lv_->killed_end(mi);
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000516 ki != ke; ++ki) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000517 if (interval.reg == ki->second) {
518 DEBUG(std::cerr << " killed");
519 end = getUseIndex(baseIndex) + 1;
520 goto exit;
521 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000522 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 }
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000524
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000525exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000526 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000527
528 // Finally, if this is defining a new range for the physical register, and if
529 // that physreg is just a copy from a vreg, and if THAT vreg was a copy from
530 // the physreg, then the new fragment has the same value as the one copied
531 // into the vreg.
532 if (interval.reg == DestReg && !interval.empty() &&
Chris Lattnere97568c2005-03-10 20:59:51 +0000533 MRegisterInfo::isVirtualRegister(SrcReg)) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000534
535 // Get the live interval for the vreg, see if it is defined by a copy.
536 LiveInterval &SrcInterval = getOrCreateInterval(SrcReg);
537
538 if (SrcInterval.containsOneValue()) {
539 assert(!SrcInterval.empty() && "Can't contain a value and be empty!");
540
541 // Get the first index of the first range. Though the interval may have
542 // multiple liveranges in it, we only check the first.
543 unsigned StartIdx = SrcInterval.begin()->start;
544 MachineInstr *SrcDefMI = getInstructionFromIndex(StartIdx);
545
546 // Check to see if the vreg was defined by a copy instruction, and that
547 // the source was this physreg.
548 unsigned VRegSrcSrc, VRegSrcDest;
549 if (tii_->isMoveInstr(*SrcDefMI, VRegSrcSrc, VRegSrcDest) &&
550 SrcReg == VRegSrcDest && VRegSrcSrc == DestReg) {
551 // Okay, now we know that the vreg was defined by a copy from this
552 // physreg. Find the value number being copied and use it as the value
553 // for this range.
554 const LiveRange *DefRange = interval.getLiveRangeContaining(StartIdx-1);
555 if (DefRange) {
556 LiveRange LR(start, end, DefRange->ValId);
557 interval.addRange(LR);
558 DEBUG(std::cerr << " +" << LR << '\n');
559 return;
560 }
561 }
562 }
563 }
564
565
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000566 LiveRange LR(start, end, interval.getNextValue());
567 interval.addRange(LR);
568 DEBUG(std::cerr << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000569}
570
Chris Lattnerf35fef72004-07-23 21:24:19 +0000571void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
572 MachineBasicBlock::iterator MI,
573 unsigned reg) {
574 if (MRegisterInfo::isVirtualRegister(reg))
575 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000576 else if (allocatableRegs_[reg]) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000577 unsigned SrcReg = 0, DestReg = 0;
578 bool IsMove = tii_->isMoveInstr(*MI, SrcReg, DestReg);
579
580 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg),
581 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000582 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattnerf768bba2005-03-09 23:05:19 +0000583 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS),
584 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000585 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000586}
587
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000588/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000589/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000590/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000591/// which a variable is live
592void LiveIntervals::computeIntervals()
593{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n");
595 DEBUG(std::cerr << "********** Function: "
596 << ((Value*)mf_->getFunction())->getName() << '\n');
Chris Lattner799a9192005-04-09 16:17:50 +0000597 bool IgnoreFirstInstr = mf_->livein_begin() != mf_->livein_end();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000598
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000599 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000600 I != E; ++I) {
601 MachineBasicBlock* mbb = I;
602 DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000603
Chris Lattner799a9192005-04-09 16:17:50 +0000604 MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
605 if (IgnoreFirstInstr) { ++mi; IgnoreFirstInstr = false; }
606 for (; mi != miEnd; ++mi) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000607 const TargetInstrDescriptor& tid =
608 tm_->getInstrInfo()->get(mi->getOpcode());
Chris Lattner477e4552004-09-30 16:10:45 +0000609 DEBUG(std::cerr << getInstructionIndex(mi) << "\t" << *mi);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000610
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000611 // handle implicit defs
612 for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
613 handleRegisterDef(mbb, mi, *id);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000614
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615 // handle explicit defs
616 for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
617 MachineOperand& mop = mi->getOperand(i);
618 // handle register defs - build intervals
619 if (mop.isRegister() && mop.getReg() && mop.isDef())
620 handleRegisterDef(mbb, mi, mop.getReg());
621 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000622 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000623 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000624}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000625
Chris Lattner1c5c0442004-07-19 14:08:10 +0000626void LiveIntervals::joinIntervalsInMachineBB(MachineBasicBlock *MBB) {
Chris Lattner7ac2d312004-07-24 02:59:07 +0000627 DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000628
Chris Lattner7ac2d312004-07-24 02:59:07 +0000629 for (MachineBasicBlock::iterator mi = MBB->begin(), mie = MBB->end();
630 mi != mie; ++mi) {
631 DEBUG(std::cerr << getInstructionIndex(mi) << '\t' << *mi);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000632
Chris Lattner7ac2d312004-07-24 02:59:07 +0000633 // we only join virtual registers with allocatable
634 // physical registers since we do not have liveness information
635 // on not allocatable physical registers
636 unsigned regA, regB;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000637 if (tii_->isMoveInstr(*mi, regA, regB) &&
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000638 (MRegisterInfo::isVirtualRegister(regA) || allocatableRegs_[regA]) &&
639 (MRegisterInfo::isVirtualRegister(regB) || allocatableRegs_[regB])) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000640
Chris Lattner7ac2d312004-07-24 02:59:07 +0000641 // Get representative registers.
642 regA = rep(regA);
643 regB = rep(regB);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000644
Chris Lattner7ac2d312004-07-24 02:59:07 +0000645 // If they are already joined we continue.
646 if (regA == regB)
647 continue;
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000648
Chris Lattner7ac2d312004-07-24 02:59:07 +0000649 // If they are both physical registers, we cannot join them.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000650 if (MRegisterInfo::isPhysicalRegister(regA) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000651 MRegisterInfo::isPhysicalRegister(regB))
652 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000653
Chris Lattner7ac2d312004-07-24 02:59:07 +0000654 // If they are not of the same register class, we cannot join them.
655 if (differingRegisterClasses(regA, regB))
656 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000657
Chris Lattner7ac2d312004-07-24 02:59:07 +0000658 LiveInterval &IntA = getInterval(regA);
659 LiveInterval &IntB = getInterval(regB);
660 assert(IntA.reg == regA && IntB.reg == regB &&
661 "Register mapping is horribly broken!");
Chris Lattner060913c2004-07-24 04:32:22 +0000662
663 DEBUG(std::cerr << "\t\tInspecting " << IntA << " and " << IntB << ": ");
664
Chris Lattner4df98e52004-07-24 03:32:06 +0000665 // If two intervals contain a single value and are joined by a copy, it
666 // does not matter if the intervals overlap, they can always be joined.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000667 bool TriviallyJoinable =
668 IntA.containsOneValue() && IntB.containsOneValue();
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000669
Chris Lattner7ac2d312004-07-24 02:59:07 +0000670 unsigned MIDefIdx = getDefIndex(getInstructionIndex(mi));
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000671 if ((TriviallyJoinable || IntB.joinable(IntA, MIDefIdx)) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000672 !overlapsAliases(&IntA, &IntB)) {
673 IntB.join(IntA, MIDefIdx);
Chris Lattner1c5c0442004-07-19 14:08:10 +0000674
Chris Lattner7ac2d312004-07-24 02:59:07 +0000675 if (!MRegisterInfo::isPhysicalRegister(regA)) {
Chris Lattner4df98e52004-07-24 03:32:06 +0000676 r2iMap_.erase(regA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000677 r2rMap_[regA] = regB;
678 } else {
679 // Otherwise merge the data structures the other way so we don't lose
680 // the physreg information.
681 r2rMap_[regB] = regA;
682 IntB.reg = regA;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000683 IntA.swap(IntB);
Chris Lattner4df98e52004-07-24 03:32:06 +0000684 r2iMap_.erase(regB);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000685 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000686 DEBUG(std::cerr << "Joined. Result = " << IntB << "\n");
687 ++numJoins;
688 } else {
689 DEBUG(std::cerr << "Interference!\n");
690 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000691 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000692 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000693}
694
Chris Lattnercc0d1562004-07-19 14:40:29 +0000695namespace {
696 // DepthMBBCompare - Comparison predicate that sort first based on the loop
697 // depth of the basic block (the unsigned), and then on the MBB number.
698 struct DepthMBBCompare {
699 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
700 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
701 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000702 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +0000704 }
705 };
706}
Chris Lattner1c5c0442004-07-19 14:08:10 +0000707
Chris Lattnercc0d1562004-07-19 14:40:29 +0000708void LiveIntervals::joinIntervals() {
709 DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n");
710
711 const LoopInfo &LI = getAnalysis<LoopInfo>();
712 if (LI.begin() == LI.end()) {
713 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +0000714 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
715 I != E; ++I)
716 joinIntervalsInMachineBB(I);
Chris Lattnercc0d1562004-07-19 14:40:29 +0000717 } else {
718 // Otherwise, join intervals in inner loops before other intervals.
719 // Unfortunately we can't just iterate over loop hierarchy here because
720 // there may be more MBB's than BB's. Collect MBB's for sorting.
721 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
722 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
723 I != E; ++I)
724 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
725
726 // Sort by loop depth.
727 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
728
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000729 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +0000730 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
731 joinIntervalsInMachineBB(MBBs[i].second);
732 }
Chris Lattnerc83e40d2004-07-25 03:24:11 +0000733
734 DEBUG(std::cerr << "*** Register mapping ***\n");
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000735 DEBUG(for (int i = 0, e = r2rMap_.size(); i != e; ++i)
736 if (r2rMap_[i])
737 std::cerr << " reg " << i << " -> reg " << r2rMap_[i] << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000738}
739
Chris Lattner7ac2d312004-07-24 02:59:07 +0000740/// Return true if the two specified registers belong to different register
741/// classes. The registers may be either phys or virt regs.
742bool LiveIntervals::differingRegisterClasses(unsigned RegA,
743 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000744
Chris Lattner7ac2d312004-07-24 02:59:07 +0000745 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000746 if (MRegisterInfo::isPhysicalRegister(RegA)) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000747 assert(MRegisterInfo::isVirtualRegister(RegB) &&
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000748 "Shouldn't consider two physregs!");
749 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
750 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000751
752 // Compare against the regclass for the second reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000753 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000754 if (MRegisterInfo::isVirtualRegister(RegB))
755 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
756 else
Chris Lattnerd0d0a1a2004-08-24 17:48:29 +0000757 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000758}
759
760bool LiveIntervals::overlapsAliases(const LiveInterval *LHS,
761 const LiveInterval *RHS) const {
762 if (!MRegisterInfo::isPhysicalRegister(LHS->reg)) {
763 if (!MRegisterInfo::isPhysicalRegister(RHS->reg))
764 return false; // vreg-vreg merge has no aliases!
765 std::swap(LHS, RHS);
766 }
767
768 assert(MRegisterInfo::isPhysicalRegister(LHS->reg) &&
769 MRegisterInfo::isVirtualRegister(RHS->reg) &&
770 "first interval must describe a physical register");
771
Chris Lattner4df98e52004-07-24 03:32:06 +0000772 for (const unsigned *AS = mri_->getAliasSet(LHS->reg); *AS; ++AS)
773 if (RHS->overlaps(getInterval(*AS)))
774 return true;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000775
Chris Lattner4df98e52004-07-24 03:32:06 +0000776 return false;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000777}
778
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000779LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000780 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Chris Lattner28696be2005-01-08 19:55:00 +0000781 (float)HUGE_VAL :0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000782 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000783}