blob: 9f0bce8296f84c8cf3dc8b6072ab93f7a9db843c [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";
209 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneref054362004-10-01 19:01:39 +0000210 O << " " << I->second << "\n";
Chris Lattner70ca3582004-09-30 15:59:17 +0000211
212 O << "********** MACHINEINSTRS **********\n";
213 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
214 mbbi != mbbe; ++mbbi) {
215 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
216 for (MachineBasicBlock::iterator mii = mbbi->begin(),
217 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000218 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000219 }
220 }
221}
222
Chris Lattner70ca3582004-09-30 15:59:17 +0000223std::vector<LiveInterval*> LiveIntervals::
224addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) {
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000225 // since this is called after the analysis is done we don't know if
226 // LiveVariables is available
227 lv_ = getAnalysisToUpdate<LiveVariables>();
228
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000229 std::vector<LiveInterval*> added;
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000230
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000231 assert(li.weight != HUGE_VAL &&
232 "attempt to spill already spilled interval!");
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000233
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000234 DEBUG(std::cerr << "\t\t\t\tadding intervals for spills for interval: "
235 << li << '\n');
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000236
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000237 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000238
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 for (LiveInterval::Ranges::const_iterator
240 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
241 unsigned index = getBaseIndex(i->start);
242 unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM;
243 for (; index != end; index += InstrSlots::NUM) {
244 // skip deleted instructions
245 while (index != end && !getInstructionFromIndex(index))
246 index += InstrSlots::NUM;
247 if (index == end) break;
Chris Lattner8640f4e2004-07-19 15:16:53 +0000248
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 MachineBasicBlock::iterator mi = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000250
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000251 for_operand:
252 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
253 MachineOperand& mop = mi->getOperand(i);
254 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattner477e4552004-09-30 16:10:45 +0000255 // First thing, attempt to fold the memory reference into the
256 // instruction. If we can do this, we don't need to insert spill
257 // code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000258 if (MachineInstr* fmi = mri_->foldMemoryOperand(mi, i, slot)) {
259 if (lv_)
260 lv_->instructionChanged(mi, fmi);
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000261 vrm.virtFolded(li.reg, mi, i, fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 mi2iMap_.erase(mi);
263 i2miMap_[index/InstrSlots::NUM] = fmi;
264 mi2iMap_[fmi] = index;
Chris Lattner477e4552004-09-30 16:10:45 +0000265 MachineBasicBlock &MBB = *mi->getParent();
266 mi = MBB.insert(MBB.erase(mi), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000267 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000268
269 // Folding the load/store can completely change the instruction in
270 // unpredictable ways, rescan it from the beginning.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271 goto for_operand;
Chris Lattner477e4552004-09-30 16:10:45 +0000272 } else {
Chris Lattner70ca3582004-09-30 15:59:17 +0000273 // This is tricky. We need to add information in the interval about
274 // the spill code so we have to use our extra load/store slots.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000275 //
Chris Lattner70ca3582004-09-30 15:59:17 +0000276 // If we have a use we are going to have a load so we start the
277 // interval from the load slot onwards. Otherwise we start from the
278 // def slot.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000279 unsigned start = (mop.isUse() ?
280 getLoadIndex(index) :
281 getDefIndex(index));
Chris Lattner70ca3582004-09-30 15:59:17 +0000282 // If we have a def we are going to have a store right after it so
283 // we end the interval after the use of the next
284 // instruction. Otherwise we end after the use of this instruction.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 unsigned end = 1 + (mop.isDef() ?
286 getStoreIndex(index) :
287 getUseIndex(index));
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000288
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000289 // create a new register for this spill
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000290 unsigned nReg = mf_->getSSARegMap()->createVirtualRegister(rc);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 mi->SetMachineOperandReg(i, nReg);
292 vrm.grow();
293 vrm.assignVirt2StackSlot(nReg, slot);
294 LiveInterval& nI = getOrCreateInterval(nReg);
295 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000296
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 // the spill weight is now infinity as it
298 // cannot be spilled again
Chris Lattner28696be2005-01-08 19:55:00 +0000299 nI.weight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000300 LiveRange LR(start, end, nI.getNextValue());
301 DEBUG(std::cerr << " +" << LR);
302 nI.addRange(LR);
303 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000304
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000305 // update live variables if it is available
306 if (lv_)
307 lv_->addVirtualRegisterKilled(nReg, mi);
308 DEBUG(std::cerr << "\t\t\t\tadded new interval: " << nI << '\n');
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000310 }
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 Evlogimenos26f5a692004-05-30 07:24:39 +0000314
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000315 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000316}
317
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000318void LiveIntervals::printRegName(unsigned reg) const
319{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320 if (MRegisterInfo::isPhysicalRegister(reg))
321 std::cerr << mri_->getName(reg);
322 else
323 std::cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000324}
325
326void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
327 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000328 LiveInterval& interval)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000329{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
331 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000332
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000333 // Virtual registers may be defined multiple times (due to phi
334 // elimination and 2-addr elimination). Much of what we do only has to be
335 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336 // time we see a vreg.
337 if (interval.empty()) {
338 // Get the Idx of the defining instructions.
339 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Chris Lattner6097d132004-07-19 02:15:56 +0000340
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341 unsigned ValNum = interval.getNextValue();
342 assert(ValNum == 0 && "First value in interval is not 0?");
343 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000344
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 // Loop over all of the blocks that the vreg is defined in. There are
346 // two cases we have to handle here. The most common case is a vreg
347 // whose lifetime is contained within a basic block. In this case there
348 // will be a single kill, in MBB, which comes after the definition.
349 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
350 // FIXME: what about dead vars?
351 unsigned killIdx;
352 if (vi.Kills[0] != mi)
353 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
354 else
355 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000356
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357 // If the kill happens after the definition, we have an intra-block
358 // live range.
359 if (killIdx > defIndex) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000360 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 "Shouldn't be alive across any blocks!");
362 LiveRange LR(defIndex, killIdx, ValNum);
363 interval.addRange(LR);
364 DEBUG(std::cerr << " +" << LR << "\n");
365 return;
366 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000367 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000368
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000369 // The other case we handle is when a virtual register lives to the end
370 // of the defining block, potentially live across some blocks, then is
371 // live into some number of blocks, but gets killed. Start by adding a
372 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000373 LiveRange NewLR(defIndex,
374 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
375 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000376 DEBUG(std::cerr << " +" << NewLR);
377 interval.addRange(NewLR);
378
379 // Iterate over all of the blocks that the variable is completely
380 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
381 // live interval.
382 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
383 if (vi.AliveBlocks[i]) {
384 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
385 if (!mbb->empty()) {
386 LiveRange LR(getInstructionIndex(&mbb->front()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000387 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000388 ValNum);
389 interval.addRange(LR);
390 DEBUG(std::cerr << " +" << LR);
391 }
392 }
393 }
394
395 // Finally, this virtual register is live from the start of any killing
396 // block to the 'use' slot of the killing instruction.
397 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
398 MachineInstr *Kill = vi.Kills[i];
399 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000400 getUseIndex(getInstructionIndex(Kill))+1,
401 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000402 interval.addRange(LR);
403 DEBUG(std::cerr << " +" << LR);
404 }
405
406 } else {
407 // If this is the second time we see a virtual register definition, it
408 // must be due to phi elimination or two addr elimination. If this is
409 // the result of two address elimination, then the vreg is the first
410 // operand, and is a def-and-use.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000411 if (mi->getOperand(0).isRegister() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000412 mi->getOperand(0).getReg() == interval.reg &&
413 mi->getOperand(0).isDef() && mi->getOperand(0).isUse()) {
414 // If this is a two-address definition, then we have already processed
415 // the live range. The only problem is that we didn't realize there
416 // are actually two values in the live interval. Because of this we
417 // need to take the LiveRegion that defines this register and split it
418 // into two values.
419 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
420 unsigned RedefIndex = getDefIndex(getInstructionIndex(mi));
421
422 // Delete the initial value, which should be short and continuous,
423 // becuase the 2-addr copy must be in the same MBB as the redef.
424 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000425
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 LiveRange LR(DefIndex, RedefIndex, interval.getNextValue());
427 DEBUG(std::cerr << " replace range with " << LR);
428 interval.addRange(LR);
429
430 // If this redefinition is dead, we need to add a dummy unit live
431 // range covering the def slot.
432 for (LiveVariables::killed_iterator KI = lv_->dead_begin(mi),
433 E = lv_->dead_end(mi); KI != E; ++KI)
434 if (KI->second == interval.reg) {
435 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
436 break;
437 }
438
439 DEBUG(std::cerr << "RESULT: " << interval);
440
441 } else {
442 // Otherwise, this must be because of phi elimination. If this is the
443 // first redefinition of the vreg that we have seen, go back and change
444 // the live range in the PHI block to be a different value number.
445 if (interval.containsOneValue()) {
446 assert(vi.Kills.size() == 1 &&
447 "PHI elimination vreg should have one kill, the PHI itself!");
448
449 // Remove the old range that we now know has an incorrect number.
450 MachineInstr *Killer = vi.Kills[0];
451 unsigned Start = getInstructionIndex(Killer->getParent()->begin());
452 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
453 DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "
454 << interval << "\n");
455 interval.removeRange(Start, End);
456 DEBUG(std::cerr << "RESULT: " << interval);
457
458 // Replace the interval with one of a NEW value number.
459 LiveRange LR(Start, End, interval.getNextValue());
460 DEBUG(std::cerr << " replace range with " << LR);
461 interval.addRange(LR);
462 DEBUG(std::cerr << "RESULT: " << interval);
463 }
464
465 // In the case of PHI elimination, each variable definition is only
466 // live until the end of the block. We've already taken care of the
467 // rest of the live range.
468 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000469 LiveRange LR(defIndex,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000470 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
471 interval.getNextValue());
472 interval.addRange(LR);
473 DEBUG(std::cerr << " +" << LR);
474 }
475 }
476
477 DEBUG(std::cerr << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000478}
479
Chris Lattnerf35fef72004-07-23 21:24:19 +0000480void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000481 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000482 LiveInterval& interval,
483 unsigned SrcReg, unsigned DestReg)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000484{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000485 // A physical register cannot be live across basic block, so its
486 // lifetime must end somewhere in its defining basic block.
487 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
488 typedef LiveVariables::killed_iterator KillIter;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000489
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000490 unsigned baseIndex = getInstructionIndex(mi);
491 unsigned start = getDefIndex(baseIndex);
492 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000494 // If it is not used after definition, it is considered dead at
495 // the instruction defining it. Hence its interval is:
496 // [defSlot(def), defSlot(def)+1)
497 for (KillIter ki = lv_->dead_begin(mi), ke = lv_->dead_end(mi);
498 ki != ke; ++ki) {
499 if (interval.reg == ki->second) {
500 DEBUG(std::cerr << " dead");
501 end = getDefIndex(start) + 1;
502 goto exit;
503 }
504 }
505
506 // If it is not dead on definition, it must be killed by a
507 // subsequent instruction. Hence its interval is:
508 // [defSlot(def), useSlot(kill)+1)
509 while (true) {
510 ++mi;
511 assert(mi != MBB->end() && "physreg was not killed in defining block!");
512 baseIndex += InstrSlots::NUM;
513 for (KillIter ki = lv_->killed_begin(mi), ke = lv_->killed_end(mi);
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000514 ki != ke; ++ki) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000515 if (interval.reg == ki->second) {
516 DEBUG(std::cerr << " killed");
517 end = getUseIndex(baseIndex) + 1;
518 goto exit;
519 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000520 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000521 }
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000522
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000523exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000524 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000525
526 // Finally, if this is defining a new range for the physical register, and if
527 // that physreg is just a copy from a vreg, and if THAT vreg was a copy from
528 // the physreg, then the new fragment has the same value as the one copied
529 // into the vreg.
530 if (interval.reg == DestReg && !interval.empty() &&
Chris Lattnere97568c2005-03-10 20:59:51 +0000531 MRegisterInfo::isVirtualRegister(SrcReg)) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000532
533 // Get the live interval for the vreg, see if it is defined by a copy.
534 LiveInterval &SrcInterval = getOrCreateInterval(SrcReg);
535
536 if (SrcInterval.containsOneValue()) {
537 assert(!SrcInterval.empty() && "Can't contain a value and be empty!");
538
539 // Get the first index of the first range. Though the interval may have
540 // multiple liveranges in it, we only check the first.
541 unsigned StartIdx = SrcInterval.begin()->start;
542 MachineInstr *SrcDefMI = getInstructionFromIndex(StartIdx);
543
544 // Check to see if the vreg was defined by a copy instruction, and that
545 // the source was this physreg.
546 unsigned VRegSrcSrc, VRegSrcDest;
547 if (tii_->isMoveInstr(*SrcDefMI, VRegSrcSrc, VRegSrcDest) &&
548 SrcReg == VRegSrcDest && VRegSrcSrc == DestReg) {
549 // Okay, now we know that the vreg was defined by a copy from this
550 // physreg. Find the value number being copied and use it as the value
551 // for this range.
552 const LiveRange *DefRange = interval.getLiveRangeContaining(StartIdx-1);
553 if (DefRange) {
554 LiveRange LR(start, end, DefRange->ValId);
555 interval.addRange(LR);
556 DEBUG(std::cerr << " +" << LR << '\n');
557 return;
558 }
559 }
560 }
561 }
562
563
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 LiveRange LR(start, end, interval.getNextValue());
565 interval.addRange(LR);
566 DEBUG(std::cerr << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000567}
568
Chris Lattnerf35fef72004-07-23 21:24:19 +0000569void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
570 MachineBasicBlock::iterator MI,
571 unsigned reg) {
572 if (MRegisterInfo::isVirtualRegister(reg))
573 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000574 else if (allocatableRegs_[reg]) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000575 unsigned SrcReg = 0, DestReg = 0;
576 bool IsMove = tii_->isMoveInstr(*MI, SrcReg, DestReg);
577
578 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg),
579 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000580 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattnerf768bba2005-03-09 23:05:19 +0000581 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS),
582 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000583 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000584}
585
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000586/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000587/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000588/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000589/// which a variable is live
590void LiveIntervals::computeIntervals()
591{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000592 DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n");
593 DEBUG(std::cerr << "********** Function: "
594 << ((Value*)mf_->getFunction())->getName() << '\n');
Chris Lattner799a9192005-04-09 16:17:50 +0000595 bool IgnoreFirstInstr = mf_->livein_begin() != mf_->livein_end();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000596
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000597 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000598 I != E; ++I) {
599 MachineBasicBlock* mbb = I;
600 DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000601
Chris Lattner799a9192005-04-09 16:17:50 +0000602 MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
603 if (IgnoreFirstInstr) { ++mi; IgnoreFirstInstr = false; }
604 for (; mi != miEnd; ++mi) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000605 const TargetInstrDescriptor& tid =
606 tm_->getInstrInfo()->get(mi->getOpcode());
Chris Lattner477e4552004-09-30 16:10:45 +0000607 DEBUG(std::cerr << getInstructionIndex(mi) << "\t" << *mi);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000608
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000609 // handle implicit defs
610 for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
611 handleRegisterDef(mbb, mi, *id);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000612
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000613 // handle explicit defs
614 for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
615 MachineOperand& mop = mi->getOperand(i);
616 // handle register defs - build intervals
617 if (mop.isRegister() && mop.getReg() && mop.isDef())
618 handleRegisterDef(mbb, mi, mop.getReg());
619 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000620 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000621 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000622}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000623
Chris Lattner1c5c0442004-07-19 14:08:10 +0000624void LiveIntervals::joinIntervalsInMachineBB(MachineBasicBlock *MBB) {
Chris Lattner7ac2d312004-07-24 02:59:07 +0000625 DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000626
Chris Lattner7ac2d312004-07-24 02:59:07 +0000627 for (MachineBasicBlock::iterator mi = MBB->begin(), mie = MBB->end();
628 mi != mie; ++mi) {
629 DEBUG(std::cerr << getInstructionIndex(mi) << '\t' << *mi);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000630
Chris Lattner7ac2d312004-07-24 02:59:07 +0000631 // we only join virtual registers with allocatable
632 // physical registers since we do not have liveness information
633 // on not allocatable physical registers
634 unsigned regA, regB;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000635 if (tii_->isMoveInstr(*mi, regA, regB) &&
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000636 (MRegisterInfo::isVirtualRegister(regA) || allocatableRegs_[regA]) &&
637 (MRegisterInfo::isVirtualRegister(regB) || allocatableRegs_[regB])) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000638
Chris Lattner7ac2d312004-07-24 02:59:07 +0000639 // Get representative registers.
640 regA = rep(regA);
641 regB = rep(regB);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000642
Chris Lattner7ac2d312004-07-24 02:59:07 +0000643 // If they are already joined we continue.
644 if (regA == regB)
645 continue;
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000646
Chris Lattner7ac2d312004-07-24 02:59:07 +0000647 // If they are both physical registers, we cannot join them.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000648 if (MRegisterInfo::isPhysicalRegister(regA) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000649 MRegisterInfo::isPhysicalRegister(regB))
650 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000651
Chris Lattner7ac2d312004-07-24 02:59:07 +0000652 // If they are not of the same register class, we cannot join them.
653 if (differingRegisterClasses(regA, regB))
654 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000655
Chris Lattner7ac2d312004-07-24 02:59:07 +0000656 LiveInterval &IntA = getInterval(regA);
657 LiveInterval &IntB = getInterval(regB);
658 assert(IntA.reg == regA && IntB.reg == regB &&
659 "Register mapping is horribly broken!");
Chris Lattner060913c2004-07-24 04:32:22 +0000660
661 DEBUG(std::cerr << "\t\tInspecting " << IntA << " and " << IntB << ": ");
662
Chris Lattner4df98e52004-07-24 03:32:06 +0000663 // If two intervals contain a single value and are joined by a copy, it
664 // does not matter if the intervals overlap, they can always be joined.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000665 bool TriviallyJoinable =
666 IntA.containsOneValue() && IntB.containsOneValue();
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000667
Chris Lattner7ac2d312004-07-24 02:59:07 +0000668 unsigned MIDefIdx = getDefIndex(getInstructionIndex(mi));
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000669 if ((TriviallyJoinable || IntB.joinable(IntA, MIDefIdx)) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000670 !overlapsAliases(&IntA, &IntB)) {
671 IntB.join(IntA, MIDefIdx);
Chris Lattner1c5c0442004-07-19 14:08:10 +0000672
Chris Lattner7ac2d312004-07-24 02:59:07 +0000673 if (!MRegisterInfo::isPhysicalRegister(regA)) {
Chris Lattner4df98e52004-07-24 03:32:06 +0000674 r2iMap_.erase(regA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000675 r2rMap_[regA] = regB;
676 } else {
677 // Otherwise merge the data structures the other way so we don't lose
678 // the physreg information.
679 r2rMap_[regB] = regA;
680 IntB.reg = regA;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000681 IntA.swap(IntB);
Chris Lattner4df98e52004-07-24 03:32:06 +0000682 r2iMap_.erase(regB);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000683 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000684 DEBUG(std::cerr << "Joined. Result = " << IntB << "\n");
685 ++numJoins;
686 } else {
687 DEBUG(std::cerr << "Interference!\n");
688 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000689 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000690 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000691}
692
Chris Lattnercc0d1562004-07-19 14:40:29 +0000693namespace {
694 // DepthMBBCompare - Comparison predicate that sort first based on the loop
695 // depth of the basic block (the unsigned), and then on the MBB number.
696 struct DepthMBBCompare {
697 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
698 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
699 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000700 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +0000702 }
703 };
704}
Chris Lattner1c5c0442004-07-19 14:08:10 +0000705
Chris Lattnercc0d1562004-07-19 14:40:29 +0000706void LiveIntervals::joinIntervals() {
707 DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n");
708
709 const LoopInfo &LI = getAnalysis<LoopInfo>();
710 if (LI.begin() == LI.end()) {
711 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +0000712 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
713 I != E; ++I)
714 joinIntervalsInMachineBB(I);
Chris Lattnercc0d1562004-07-19 14:40:29 +0000715 } else {
716 // Otherwise, join intervals in inner loops before other intervals.
717 // Unfortunately we can't just iterate over loop hierarchy here because
718 // there may be more MBB's than BB's. Collect MBB's for sorting.
719 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
720 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
721 I != E; ++I)
722 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
723
724 // Sort by loop depth.
725 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
726
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000727 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +0000728 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
729 joinIntervalsInMachineBB(MBBs[i].second);
730 }
Chris Lattnerc83e40d2004-07-25 03:24:11 +0000731
732 DEBUG(std::cerr << "*** Register mapping ***\n");
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000733 DEBUG(for (int i = 0, e = r2rMap_.size(); i != e; ++i)
734 if (r2rMap_[i])
735 std::cerr << " reg " << i << " -> reg " << r2rMap_[i] << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000736}
737
Chris Lattner7ac2d312004-07-24 02:59:07 +0000738/// Return true if the two specified registers belong to different register
739/// classes. The registers may be either phys or virt regs.
740bool LiveIntervals::differingRegisterClasses(unsigned RegA,
741 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000742
Chris Lattner7ac2d312004-07-24 02:59:07 +0000743 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000744 if (MRegisterInfo::isPhysicalRegister(RegA)) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000745 assert(MRegisterInfo::isVirtualRegister(RegB) &&
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000746 "Shouldn't consider two physregs!");
747 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
748 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000749
750 // Compare against the regclass for the second reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000751 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000752 if (MRegisterInfo::isVirtualRegister(RegB))
753 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
754 else
Chris Lattnerd0d0a1a2004-08-24 17:48:29 +0000755 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000756}
757
758bool LiveIntervals::overlapsAliases(const LiveInterval *LHS,
759 const LiveInterval *RHS) const {
760 if (!MRegisterInfo::isPhysicalRegister(LHS->reg)) {
761 if (!MRegisterInfo::isPhysicalRegister(RHS->reg))
762 return false; // vreg-vreg merge has no aliases!
763 std::swap(LHS, RHS);
764 }
765
766 assert(MRegisterInfo::isPhysicalRegister(LHS->reg) &&
767 MRegisterInfo::isVirtualRegister(RHS->reg) &&
768 "first interval must describe a physical register");
769
Chris Lattner4df98e52004-07-24 03:32:06 +0000770 for (const unsigned *AS = mri_->getAliasSet(LHS->reg); *AS; ++AS)
771 if (RHS->overlaps(getInterval(*AS)))
772 return true;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000773
Chris Lattner4df98e52004-07-24 03:32:06 +0000774 return false;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000775}
776
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000777LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000778 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Chris Lattner28696be2005-01-08 19:55:00 +0000779 (float)HUGE_VAL :0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000780 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000781}