blob: 580895dd864864937bd2c24a01654d12923abd76 [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()) {
98 unsigned FirstLiveIn = *fn.livein_begin();
99
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();
131 for (MachineFunction::liveinout_iterator I = fn.livein_begin(),
132 E = fn.livein_end(); I != E; ++I) {
133 handlePhysicalRegisterDef(Entry, Entry->begin(),
134 getOrCreateInterval(*I), 0, 0);
135 for (const unsigned* AS = mri_->getAliasSet(*I); *AS; ++AS)
136 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
145#if 1
146 DEBUG(std::cerr << "********** INTERVALS **********\n");
147 DEBUG(for (iterator I = begin(), E = end(); I != E; ++I)
148 std::cerr << I->second << "\n");
149#endif
150
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 Lattner799a9192005-04-09 16:17:50 +0000202 // If we inserted a placeholder instruction at the entry of the block, remove
203 // it now.
204 if (fn.livein_begin() != fn.livein_end())
205 fn.begin()->erase(fn.begin()->begin());
206
Chris Lattner70ca3582004-09-30 15:59:17 +0000207 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000209}
210
Chris Lattner70ca3582004-09-30 15:59:17 +0000211/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000212void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000213 O << "********** INTERVALS **********\n";
214 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneref054362004-10-01 19:01:39 +0000215 O << " " << I->second << "\n";
Chris Lattner70ca3582004-09-30 15:59:17 +0000216
217 O << "********** MACHINEINSTRS **********\n";
218 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
219 mbbi != mbbe; ++mbbi) {
220 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
221 for (MachineBasicBlock::iterator mii = mbbi->begin(),
222 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000223 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000224 }
225 }
226}
227
Chris Lattner70ca3582004-09-30 15:59:17 +0000228std::vector<LiveInterval*> LiveIntervals::
229addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, int slot) {
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000230 // since this is called after the analysis is done we don't know if
231 // LiveVariables is available
232 lv_ = getAnalysisToUpdate<LiveVariables>();
233
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000234 std::vector<LiveInterval*> added;
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000235
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000236 assert(li.weight != HUGE_VAL &&
237 "attempt to spill already spilled interval!");
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000238
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 DEBUG(std::cerr << "\t\t\t\tadding intervals for spills for interval: "
240 << li << '\n');
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000241
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000243
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000244 for (LiveInterval::Ranges::const_iterator
245 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
246 unsigned index = getBaseIndex(i->start);
247 unsigned end = getBaseIndex(i->end-1) + InstrSlots::NUM;
248 for (; index != end; index += InstrSlots::NUM) {
249 // skip deleted instructions
250 while (index != end && !getInstructionFromIndex(index))
251 index += InstrSlots::NUM;
252 if (index == end) break;
Chris Lattner8640f4e2004-07-19 15:16:53 +0000253
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 MachineBasicBlock::iterator mi = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000255
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000256 for_operand:
257 for (unsigned i = 0; i != mi->getNumOperands(); ++i) {
258 MachineOperand& mop = mi->getOperand(i);
259 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattner477e4552004-09-30 16:10:45 +0000260 // First thing, attempt to fold the memory reference into the
261 // instruction. If we can do this, we don't need to insert spill
262 // code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000263 if (MachineInstr* fmi = mri_->foldMemoryOperand(mi, i, slot)) {
264 if (lv_)
265 lv_->instructionChanged(mi, fmi);
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000266 vrm.virtFolded(li.reg, mi, i, fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000267 mi2iMap_.erase(mi);
268 i2miMap_[index/InstrSlots::NUM] = fmi;
269 mi2iMap_[fmi] = index;
Chris Lattner477e4552004-09-30 16:10:45 +0000270 MachineBasicBlock &MBB = *mi->getParent();
271 mi = MBB.insert(MBB.erase(mi), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000272 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000273
274 // Folding the load/store can completely change the instruction in
275 // unpredictable ways, rescan it from the beginning.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 goto for_operand;
Chris Lattner477e4552004-09-30 16:10:45 +0000277 } else {
Chris Lattner70ca3582004-09-30 15:59:17 +0000278 // This is tricky. We need to add information in the interval about
279 // the spill code so we have to use our extra load/store slots.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 //
Chris Lattner70ca3582004-09-30 15:59:17 +0000281 // If we have a use we are going to have a load so we start the
282 // interval from the load slot onwards. Otherwise we start from the
283 // def slot.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000284 unsigned start = (mop.isUse() ?
285 getLoadIndex(index) :
286 getDefIndex(index));
Chris Lattner70ca3582004-09-30 15:59:17 +0000287 // If we have a def we are going to have a store right after it so
288 // we end the interval after the use of the next
289 // instruction. Otherwise we end after the use of this instruction.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 unsigned end = 1 + (mop.isDef() ?
291 getStoreIndex(index) :
292 getUseIndex(index));
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000293
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 // create a new register for this spill
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000295 unsigned nReg = mf_->getSSARegMap()->createVirtualRegister(rc);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296 mi->SetMachineOperandReg(i, nReg);
297 vrm.grow();
298 vrm.assignVirt2StackSlot(nReg, slot);
299 LiveInterval& nI = getOrCreateInterval(nReg);
300 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000301
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 // the spill weight is now infinity as it
303 // cannot be spilled again
Chris Lattner28696be2005-01-08 19:55:00 +0000304 nI.weight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000305 LiveRange LR(start, end, nI.getNextValue());
306 DEBUG(std::cerr << " +" << LR);
307 nI.addRange(LR);
308 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000309
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000310 // update live variables if it is available
311 if (lv_)
312 lv_->addVirtualRegisterKilled(nReg, mi);
313 DEBUG(std::cerr << "\t\t\t\tadded new interval: " << nI << '\n');
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000315 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000316 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000317 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000318 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000319
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000321}
322
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000323void LiveIntervals::printRegName(unsigned reg) const
324{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000325 if (MRegisterInfo::isPhysicalRegister(reg))
326 std::cerr << mri_->getName(reg);
327 else
328 std::cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000329}
330
331void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
332 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000333 LiveInterval& interval)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000334{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000335 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
336 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000337
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000338 // Virtual registers may be defined multiple times (due to phi
339 // elimination and 2-addr elimination). Much of what we do only has to be
340 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341 // time we see a vreg.
342 if (interval.empty()) {
343 // Get the Idx of the defining instructions.
344 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Chris Lattner6097d132004-07-19 02:15:56 +0000345
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346 unsigned ValNum = interval.getNextValue();
347 assert(ValNum == 0 && "First value in interval is not 0?");
348 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000349
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350 // Loop over all of the blocks that the vreg is defined in. There are
351 // two cases we have to handle here. The most common case is a vreg
352 // whose lifetime is contained within a basic block. In this case there
353 // will be a single kill, in MBB, which comes after the definition.
354 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
355 // FIXME: what about dead vars?
356 unsigned killIdx;
357 if (vi.Kills[0] != mi)
358 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
359 else
360 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000361
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000362 // If the kill happens after the definition, we have an intra-block
363 // live range.
364 if (killIdx > defIndex) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000365 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366 "Shouldn't be alive across any blocks!");
367 LiveRange LR(defIndex, killIdx, ValNum);
368 interval.addRange(LR);
369 DEBUG(std::cerr << " +" << LR << "\n");
370 return;
371 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000372 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000373
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 // The other case we handle is when a virtual register lives to the end
375 // of the defining block, potentially live across some blocks, then is
376 // live into some number of blocks, but gets killed. Start by adding a
377 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000378 LiveRange NewLR(defIndex,
379 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
380 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 DEBUG(std::cerr << " +" << NewLR);
382 interval.addRange(NewLR);
383
384 // Iterate over all of the blocks that the variable is completely
385 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
386 // live interval.
387 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
388 if (vi.AliveBlocks[i]) {
389 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
390 if (!mbb->empty()) {
391 LiveRange LR(getInstructionIndex(&mbb->front()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000392 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000393 ValNum);
394 interval.addRange(LR);
395 DEBUG(std::cerr << " +" << LR);
396 }
397 }
398 }
399
400 // Finally, this virtual register is live from the start of any killing
401 // block to the 'use' slot of the killing instruction.
402 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
403 MachineInstr *Kill = vi.Kills[i];
404 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000405 getUseIndex(getInstructionIndex(Kill))+1,
406 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000407 interval.addRange(LR);
408 DEBUG(std::cerr << " +" << LR);
409 }
410
411 } else {
412 // If this is the second time we see a virtual register definition, it
413 // must be due to phi elimination or two addr elimination. If this is
414 // the result of two address elimination, then the vreg is the first
415 // operand, and is a def-and-use.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000416 if (mi->getOperand(0).isRegister() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000417 mi->getOperand(0).getReg() == interval.reg &&
418 mi->getOperand(0).isDef() && mi->getOperand(0).isUse()) {
419 // If this is a two-address definition, then we have already processed
420 // the live range. The only problem is that we didn't realize there
421 // are actually two values in the live interval. Because of this we
422 // need to take the LiveRegion that defines this register and split it
423 // into two values.
424 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
425 unsigned RedefIndex = getDefIndex(getInstructionIndex(mi));
426
427 // Delete the initial value, which should be short and continuous,
428 // becuase the 2-addr copy must be in the same MBB as the redef.
429 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000430
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000431 LiveRange LR(DefIndex, RedefIndex, interval.getNextValue());
432 DEBUG(std::cerr << " replace range with " << LR);
433 interval.addRange(LR);
434
435 // If this redefinition is dead, we need to add a dummy unit live
436 // range covering the def slot.
437 for (LiveVariables::killed_iterator KI = lv_->dead_begin(mi),
438 E = lv_->dead_end(mi); KI != E; ++KI)
439 if (KI->second == interval.reg) {
440 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
441 break;
442 }
443
444 DEBUG(std::cerr << "RESULT: " << interval);
445
446 } else {
447 // Otherwise, this must be because of phi elimination. If this is the
448 // first redefinition of the vreg that we have seen, go back and change
449 // the live range in the PHI block to be a different value number.
450 if (interval.containsOneValue()) {
451 assert(vi.Kills.size() == 1 &&
452 "PHI elimination vreg should have one kill, the PHI itself!");
453
454 // Remove the old range that we now know has an incorrect number.
455 MachineInstr *Killer = vi.Kills[0];
456 unsigned Start = getInstructionIndex(Killer->getParent()->begin());
457 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
458 DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "
459 << interval << "\n");
460 interval.removeRange(Start, End);
461 DEBUG(std::cerr << "RESULT: " << interval);
462
463 // Replace the interval with one of a NEW value number.
464 LiveRange LR(Start, End, interval.getNextValue());
465 DEBUG(std::cerr << " replace range with " << LR);
466 interval.addRange(LR);
467 DEBUG(std::cerr << "RESULT: " << interval);
468 }
469
470 // In the case of PHI elimination, each variable definition is only
471 // live until the end of the block. We've already taken care of the
472 // rest of the live range.
473 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000474 LiveRange LR(defIndex,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000475 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
476 interval.getNextValue());
477 interval.addRange(LR);
478 DEBUG(std::cerr << " +" << LR);
479 }
480 }
481
482 DEBUG(std::cerr << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000483}
484
Chris Lattnerf35fef72004-07-23 21:24:19 +0000485void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000486 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000487 LiveInterval& interval,
488 unsigned SrcReg, unsigned DestReg)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000489{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000490 // A physical register cannot be live across basic block, so its
491 // lifetime must end somewhere in its defining basic block.
492 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
493 typedef LiveVariables::killed_iterator KillIter;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000494
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000495 unsigned baseIndex = getInstructionIndex(mi);
496 unsigned start = getDefIndex(baseIndex);
497 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000498
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000499 // If it is not used after definition, it is considered dead at
500 // the instruction defining it. Hence its interval is:
501 // [defSlot(def), defSlot(def)+1)
502 for (KillIter ki = lv_->dead_begin(mi), ke = lv_->dead_end(mi);
503 ki != ke; ++ki) {
504 if (interval.reg == ki->second) {
505 DEBUG(std::cerr << " dead");
506 end = getDefIndex(start) + 1;
507 goto exit;
508 }
509 }
510
511 // If it is not dead on definition, it must be killed by a
512 // subsequent instruction. Hence its interval is:
513 // [defSlot(def), useSlot(kill)+1)
514 while (true) {
515 ++mi;
516 assert(mi != MBB->end() && "physreg was not killed in defining block!");
517 baseIndex += InstrSlots::NUM;
518 for (KillIter ki = lv_->killed_begin(mi), ke = lv_->killed_end(mi);
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000519 ki != ke; ++ki) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000520 if (interval.reg == ki->second) {
521 DEBUG(std::cerr << " killed");
522 end = getUseIndex(baseIndex) + 1;
523 goto exit;
524 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000525 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000526 }
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000527
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000528exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000530
531 // Finally, if this is defining a new range for the physical register, and if
532 // that physreg is just a copy from a vreg, and if THAT vreg was a copy from
533 // the physreg, then the new fragment has the same value as the one copied
534 // into the vreg.
535 if (interval.reg == DestReg && !interval.empty() &&
Chris Lattnere97568c2005-03-10 20:59:51 +0000536 MRegisterInfo::isVirtualRegister(SrcReg)) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000537
538 // Get the live interval for the vreg, see if it is defined by a copy.
539 LiveInterval &SrcInterval = getOrCreateInterval(SrcReg);
540
541 if (SrcInterval.containsOneValue()) {
542 assert(!SrcInterval.empty() && "Can't contain a value and be empty!");
543
544 // Get the first index of the first range. Though the interval may have
545 // multiple liveranges in it, we only check the first.
546 unsigned StartIdx = SrcInterval.begin()->start;
547 MachineInstr *SrcDefMI = getInstructionFromIndex(StartIdx);
548
549 // Check to see if the vreg was defined by a copy instruction, and that
550 // the source was this physreg.
551 unsigned VRegSrcSrc, VRegSrcDest;
552 if (tii_->isMoveInstr(*SrcDefMI, VRegSrcSrc, VRegSrcDest) &&
553 SrcReg == VRegSrcDest && VRegSrcSrc == DestReg) {
554 // Okay, now we know that the vreg was defined by a copy from this
555 // physreg. Find the value number being copied and use it as the value
556 // for this range.
557 const LiveRange *DefRange = interval.getLiveRangeContaining(StartIdx-1);
558 if (DefRange) {
559 LiveRange LR(start, end, DefRange->ValId);
560 interval.addRange(LR);
561 DEBUG(std::cerr << " +" << LR << '\n');
562 return;
563 }
564 }
565 }
566 }
567
568
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000569 LiveRange LR(start, end, interval.getNextValue());
570 interval.addRange(LR);
571 DEBUG(std::cerr << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000572}
573
Chris Lattnerf35fef72004-07-23 21:24:19 +0000574void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
575 MachineBasicBlock::iterator MI,
576 unsigned reg) {
577 if (MRegisterInfo::isVirtualRegister(reg))
578 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000579 else if (allocatableRegs_[reg]) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000580 unsigned SrcReg = 0, DestReg = 0;
581 bool IsMove = tii_->isMoveInstr(*MI, SrcReg, DestReg);
582
583 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg),
584 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000585 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattnerf768bba2005-03-09 23:05:19 +0000586 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS),
587 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000588 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000589}
590
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000591/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000592/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000593/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000594/// which a variable is live
595void LiveIntervals::computeIntervals()
596{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597 DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n");
598 DEBUG(std::cerr << "********** Function: "
599 << ((Value*)mf_->getFunction())->getName() << '\n');
Chris Lattner799a9192005-04-09 16:17:50 +0000600 bool IgnoreFirstInstr = mf_->livein_begin() != mf_->livein_end();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000601
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000602 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000603 I != E; ++I) {
604 MachineBasicBlock* mbb = I;
605 DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000606
Chris Lattner799a9192005-04-09 16:17:50 +0000607 MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
608 if (IgnoreFirstInstr) { ++mi; IgnoreFirstInstr = false; }
609 for (; mi != miEnd; ++mi) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000610 const TargetInstrDescriptor& tid =
611 tm_->getInstrInfo()->get(mi->getOpcode());
Chris Lattner477e4552004-09-30 16:10:45 +0000612 DEBUG(std::cerr << getInstructionIndex(mi) << "\t" << *mi);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000613
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 // handle implicit defs
615 for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
616 handleRegisterDef(mbb, mi, *id);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000617
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000618 // handle explicit defs
619 for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
620 MachineOperand& mop = mi->getOperand(i);
621 // handle register defs - build intervals
622 if (mop.isRegister() && mop.getReg() && mop.isDef())
623 handleRegisterDef(mbb, mi, mop.getReg());
624 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000625 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000626 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000627}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000628
Chris Lattner1c5c0442004-07-19 14:08:10 +0000629void LiveIntervals::joinIntervalsInMachineBB(MachineBasicBlock *MBB) {
Chris Lattner7ac2d312004-07-24 02:59:07 +0000630 DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000631
Chris Lattner7ac2d312004-07-24 02:59:07 +0000632 for (MachineBasicBlock::iterator mi = MBB->begin(), mie = MBB->end();
633 mi != mie; ++mi) {
634 DEBUG(std::cerr << getInstructionIndex(mi) << '\t' << *mi);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000635
Chris Lattner7ac2d312004-07-24 02:59:07 +0000636 // we only join virtual registers with allocatable
637 // physical registers since we do not have liveness information
638 // on not allocatable physical registers
639 unsigned regA, regB;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000640 if (tii_->isMoveInstr(*mi, regA, regB) &&
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000641 (MRegisterInfo::isVirtualRegister(regA) || allocatableRegs_[regA]) &&
642 (MRegisterInfo::isVirtualRegister(regB) || allocatableRegs_[regB])) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000643
Chris Lattner7ac2d312004-07-24 02:59:07 +0000644 // Get representative registers.
645 regA = rep(regA);
646 regB = rep(regB);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000647
Chris Lattner7ac2d312004-07-24 02:59:07 +0000648 // If they are already joined we continue.
649 if (regA == regB)
650 continue;
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000651
Chris Lattner7ac2d312004-07-24 02:59:07 +0000652 // If they are both physical registers, we cannot join them.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000653 if (MRegisterInfo::isPhysicalRegister(regA) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000654 MRegisterInfo::isPhysicalRegister(regB))
655 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000656
Chris Lattner7ac2d312004-07-24 02:59:07 +0000657 // If they are not of the same register class, we cannot join them.
658 if (differingRegisterClasses(regA, regB))
659 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000660
Chris Lattner7ac2d312004-07-24 02:59:07 +0000661 LiveInterval &IntA = getInterval(regA);
662 LiveInterval &IntB = getInterval(regB);
663 assert(IntA.reg == regA && IntB.reg == regB &&
664 "Register mapping is horribly broken!");
Chris Lattner060913c2004-07-24 04:32:22 +0000665
666 DEBUG(std::cerr << "\t\tInspecting " << IntA << " and " << IntB << ": ");
667
Chris Lattner4df98e52004-07-24 03:32:06 +0000668 // If two intervals contain a single value and are joined by a copy, it
669 // does not matter if the intervals overlap, they can always be joined.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000670 bool TriviallyJoinable =
671 IntA.containsOneValue() && IntB.containsOneValue();
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000672
Chris Lattner7ac2d312004-07-24 02:59:07 +0000673 unsigned MIDefIdx = getDefIndex(getInstructionIndex(mi));
Chris Lattnerc25b55a2004-07-25 07:47:25 +0000674 if ((TriviallyJoinable || IntB.joinable(IntA, MIDefIdx)) &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000675 !overlapsAliases(&IntA, &IntB)) {
676 IntB.join(IntA, MIDefIdx);
Chris Lattner1c5c0442004-07-19 14:08:10 +0000677
Chris Lattner7ac2d312004-07-24 02:59:07 +0000678 if (!MRegisterInfo::isPhysicalRegister(regA)) {
Chris Lattner4df98e52004-07-24 03:32:06 +0000679 r2iMap_.erase(regA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000680 r2rMap_[regA] = regB;
681 } else {
682 // Otherwise merge the data structures the other way so we don't lose
683 // the physreg information.
684 r2rMap_[regB] = regA;
685 IntB.reg = regA;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000686 IntA.swap(IntB);
Chris Lattner4df98e52004-07-24 03:32:06 +0000687 r2iMap_.erase(regB);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000688 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000689 DEBUG(std::cerr << "Joined. Result = " << IntB << "\n");
690 ++numJoins;
691 } else {
692 DEBUG(std::cerr << "Interference!\n");
693 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000694 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000695 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000696}
697
Chris Lattnercc0d1562004-07-19 14:40:29 +0000698namespace {
699 // DepthMBBCompare - Comparison predicate that sort first based on the loop
700 // depth of the basic block (the unsigned), and then on the MBB number.
701 struct DepthMBBCompare {
702 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
703 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
704 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000705 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000706 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +0000707 }
708 };
709}
Chris Lattner1c5c0442004-07-19 14:08:10 +0000710
Chris Lattnercc0d1562004-07-19 14:40:29 +0000711void LiveIntervals::joinIntervals() {
712 DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n");
713
714 const LoopInfo &LI = getAnalysis<LoopInfo>();
715 if (LI.begin() == LI.end()) {
716 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +0000717 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
718 I != E; ++I)
719 joinIntervalsInMachineBB(I);
Chris Lattnercc0d1562004-07-19 14:40:29 +0000720 } else {
721 // Otherwise, join intervals in inner loops before other intervals.
722 // Unfortunately we can't just iterate over loop hierarchy here because
723 // there may be more MBB's than BB's. Collect MBB's for sorting.
724 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
725 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
726 I != E; ++I)
727 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
728
729 // Sort by loop depth.
730 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
731
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000732 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +0000733 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
734 joinIntervalsInMachineBB(MBBs[i].second);
735 }
Chris Lattnerc83e40d2004-07-25 03:24:11 +0000736
737 DEBUG(std::cerr << "*** Register mapping ***\n");
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000738 DEBUG(for (int i = 0, e = r2rMap_.size(); i != e; ++i)
739 if (r2rMap_[i])
740 std::cerr << " reg " << i << " -> reg " << r2rMap_[i] << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000741}
742
Chris Lattner7ac2d312004-07-24 02:59:07 +0000743/// Return true if the two specified registers belong to different register
744/// classes. The registers may be either phys or virt regs.
745bool LiveIntervals::differingRegisterClasses(unsigned RegA,
746 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000747
Chris Lattner7ac2d312004-07-24 02:59:07 +0000748 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000749 if (MRegisterInfo::isPhysicalRegister(RegA)) {
750 assert(MRegisterInfo::isVirtualRegister(RegB) &&
751 "Shouldn't consider two physregs!");
752 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
753 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000754
755 // Compare against the regclass for the second reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000756 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000757 if (MRegisterInfo::isVirtualRegister(RegB))
758 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
759 else
Chris Lattnerd0d0a1a2004-08-24 17:48:29 +0000760 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000761}
762
763bool LiveIntervals::overlapsAliases(const LiveInterval *LHS,
764 const LiveInterval *RHS) const {
765 if (!MRegisterInfo::isPhysicalRegister(LHS->reg)) {
766 if (!MRegisterInfo::isPhysicalRegister(RHS->reg))
767 return false; // vreg-vreg merge has no aliases!
768 std::swap(LHS, RHS);
769 }
770
771 assert(MRegisterInfo::isPhysicalRegister(LHS->reg) &&
772 MRegisterInfo::isVirtualRegister(RHS->reg) &&
773 "first interval must describe a physical register");
774
Chris Lattner4df98e52004-07-24 03:32:06 +0000775 for (const unsigned *AS = mri_->getAliasSet(LHS->reg); *AS; ++AS)
776 if (RHS->overlaps(getInterval(*AS)))
777 return true;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000778
Chris Lattner4df98e52004-07-24 03:32:06 +0000779 return false;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000780}
781
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000782LiveInterval LiveIntervals::createInterval(unsigned reg) {
Chris Lattner28696be2005-01-08 19:55:00 +0000783 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
784 (float)HUGE_VAL :0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000785 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000786}