blob: c6dc2b92d44230b8257ee7c6e62feaecc381271c [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000022#include "llvm/Analysis/LoopInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/MRegisterInfo.h"
29#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Misha Brukman08a6c762004-09-03 18:25:53 +000036#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000037#include <iostream>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000038using namespace llvm;
39
40namespace {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000041 RegisterAnalysis<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000042
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000043 Statistic<> numIntervals
44 ("liveintervals", "Number of original intervals");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000045
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000046 Statistic<> numIntervalsAfter
47 ("liveintervals", "Number of intervals after coalescing");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000048
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000049 Statistic<> numJoins
50 ("liveintervals", "Number of interval joins performed");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000051
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000052 Statistic<> numPeep
53 ("liveintervals", "Number of identity moves eliminated after coalescing");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000054
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 Statistic<> numFolded
56 ("liveintervals", "Number of loads/stores folded into instructions");
Alkis Evlogimenos007726c2004-02-20 20:53:26 +000057
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 cl::opt<bool>
59 EnableJoining("join-liveintervals",
60 cl::desc("Join compatible live intervals"),
61 cl::init(true));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062};
63
64void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const
65{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000066 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 Lattner5ab6f5f2005-09-02 00:20:32 +0000134 getOrCreateInterval(I->first), 0, 0, true);
Chris Lattner712ad0c2005-05-13 07:08:07 +0000135 for (const unsigned* AS = mri_->getAliasSet(I->first); *AS; ++AS)
Chris Lattner799a9192005-04-09 16:17:50 +0000136 handlePhysicalRegisterDef(Entry, Entry->begin(),
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000137 getOrCreateInterval(*AS), 0, 0, true);
Chris Lattner799a9192005-04-09 16:17:50 +0000138 }
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
Chris Lattner3b9db832006-01-03 07:41:37 +0000251 MachineInstr *MI = getInstructionFromIndex(index);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000252
Chris Lattnerb11443d2005-09-09 19:17:47 +0000253 // NewRegLiveIn - This instruction might have multiple uses of the spilled
254 // register. In this case, for the first use, keep track of the new vreg
255 // that we reload it into. If we see a second use, reuse this vreg
256 // instead of creating live ranges for two reloads.
257 unsigned NewRegLiveIn = 0;
258
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259 for_operand:
Chris Lattner3b9db832006-01-03 07:41:37 +0000260 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
261 MachineOperand& mop = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 if (mop.isRegister() && mop.getReg() == li.reg) {
Chris Lattnerb11443d2005-09-09 19:17:47 +0000263 if (NewRegLiveIn && mop.isUse()) {
264 // We already emitted a reload of this value, reuse it for
265 // subsequent operands.
Chris Lattner3b9db832006-01-03 07:41:37 +0000266 MI->SetMachineOperandReg(i, NewRegLiveIn);
Chris Lattnerb11443d2005-09-09 19:17:47 +0000267 DEBUG(std::cerr << "\t\t\t\treused reload into reg" << NewRegLiveIn
268 << " for operand #" << i << '\n');
Chris Lattner3b9db832006-01-03 07:41:37 +0000269 } else if (MachineInstr* fmi = mri_->foldMemoryOperand(MI, i, slot)) {
Chris Lattnerb11443d2005-09-09 19:17:47 +0000270 // Attempt to fold the memory reference into the instruction. If we
271 // can do this, we don't need to insert spill code.
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000272 if (lv_)
Chris Lattner3b9db832006-01-03 07:41:37 +0000273 lv_->instructionChanged(MI, fmi);
Evan Cheng200370f2006-04-30 08:41:47 +0000274 MachineBasicBlock &MBB = *MI->getParent();
275 bool LiveOut = li.liveAt(getInstructionIndex(&MBB.back()) +
276 InstrSlots::NUM);
277 vrm.virtFolded(li.reg, MI, i, fmi, LiveOut);
Chris Lattner3b9db832006-01-03 07:41:37 +0000278 mi2iMap_.erase(MI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000279 i2miMap_[index/InstrSlots::NUM] = fmi;
280 mi2iMap_[fmi] = index;
Chris Lattner3b9db832006-01-03 07:41:37 +0000281 MI = MBB.insert(MBB.erase(MI), fmi);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000282 ++numFolded;
Chris Lattner477e4552004-09-30 16:10:45 +0000283 // Folding the load/store can completely change the instruction in
284 // unpredictable ways, rescan it from the beginning.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 goto for_operand;
Chris Lattner477e4552004-09-30 16:10:45 +0000286 } else {
Chris Lattner70ca3582004-09-30 15:59:17 +0000287 // This is tricky. We need to add information in the interval about
288 // the spill code so we have to use our extra load/store slots.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000289 //
Chris Lattner70ca3582004-09-30 15:59:17 +0000290 // If we have a use we are going to have a load so we start the
291 // interval from the load slot onwards. Otherwise we start from the
292 // def slot.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000293 unsigned start = (mop.isUse() ?
294 getLoadIndex(index) :
295 getDefIndex(index));
Chris Lattner70ca3582004-09-30 15:59:17 +0000296 // If we have a def we are going to have a store right after it so
297 // we end the interval after the use of the next
298 // instruction. Otherwise we end after the use of this instruction.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 unsigned end = 1 + (mop.isDef() ?
300 getStoreIndex(index) :
301 getUseIndex(index));
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000302
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000303 // create a new register for this spill
Chris Lattnerb11443d2005-09-09 19:17:47 +0000304 NewRegLiveIn = mf_->getSSARegMap()->createVirtualRegister(rc);
Chris Lattner3b9db832006-01-03 07:41:37 +0000305 MI->SetMachineOperandReg(i, NewRegLiveIn);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000306 vrm.grow();
Chris Lattnerb11443d2005-09-09 19:17:47 +0000307 vrm.assignVirt2StackSlot(NewRegLiveIn, slot);
308 LiveInterval& nI = getOrCreateInterval(NewRegLiveIn);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 assert(nI.empty());
Chris Lattner70ca3582004-09-30 15:59:17 +0000310
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 // the spill weight is now infinity as it
312 // cannot be spilled again
Chris Lattner28696be2005-01-08 19:55:00 +0000313 nI.weight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314 LiveRange LR(start, end, nI.getNextValue());
315 DEBUG(std::cerr << " +" << LR);
316 nI.addRange(LR);
317 added.push_back(&nI);
Chris Lattner70ca3582004-09-30 15:59:17 +0000318
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000319 // update live variables if it is available
320 if (lv_)
Chris Lattner3b9db832006-01-03 07:41:37 +0000321 lv_->addVirtualRegisterKilled(NewRegLiveIn, MI);
Chris Lattnerb11443d2005-09-09 19:17:47 +0000322
323 // If this is a live in, reuse it for subsequent live-ins. If it's
324 // a def, we can't do this.
325 if (!mop.isUse()) NewRegLiveIn = 0;
326
Alkis Evlogimenosd8d26b32004-08-27 18:59:22 +0000327 DEBUG(std::cerr << "\t\t\t\tadded new interval: " << nI << '\n');
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000329 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000331 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332 }
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +0000333
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000334 return added;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000335}
336
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000337void LiveIntervals::printRegName(unsigned reg) const
338{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339 if (MRegisterInfo::isPhysicalRegister(reg))
340 std::cerr << mri_->getName(reg);
341 else
342 std::cerr << "%reg" << reg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000343}
344
345void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
346 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000347 LiveInterval& interval)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000348{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
350 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000351
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000352 // Virtual registers may be defined multiple times (due to phi
353 // elimination and 2-addr elimination). Much of what we do only has to be
354 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000355 // time we see a vreg.
356 if (interval.empty()) {
357 // Get the Idx of the defining instructions.
358 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Chris Lattner6097d132004-07-19 02:15:56 +0000359
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000360 unsigned ValNum = interval.getNextValue();
361 assert(ValNum == 0 && "First value in interval is not 0?");
362 ValNum = 0; // Clue in the optimizer.
Chris Lattner7ac2d312004-07-24 02:59:07 +0000363
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000364 // Loop over all of the blocks that the vreg is defined in. There are
365 // two cases we have to handle here. The most common case is a vreg
366 // whose lifetime is contained within a basic block. In this case there
367 // will be a single kill, in MBB, which comes after the definition.
368 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
369 // FIXME: what about dead vars?
370 unsigned killIdx;
371 if (vi.Kills[0] != mi)
372 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
373 else
374 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000375
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000376 // If the kill happens after the definition, we have an intra-block
377 // live range.
378 if (killIdx > defIndex) {
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000379 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000380 "Shouldn't be alive across any blocks!");
381 LiveRange LR(defIndex, killIdx, ValNum);
382 interval.addRange(LR);
383 DEBUG(std::cerr << " +" << LR << "\n");
384 return;
385 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000386 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000387
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000388 // The other case we handle is when a virtual register lives to the end
389 // of the defining block, potentially live across some blocks, then is
390 // live into some number of blocks, but gets killed. Start by adding a
391 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000392 LiveRange NewLR(defIndex,
393 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
394 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000395 DEBUG(std::cerr << " +" << NewLR);
396 interval.addRange(NewLR);
397
398 // Iterate over all of the blocks that the variable is completely
399 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
400 // live interval.
401 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
402 if (vi.AliveBlocks[i]) {
403 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
404 if (!mbb->empty()) {
405 LiveRange LR(getInstructionIndex(&mbb->front()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000406 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000407 ValNum);
408 interval.addRange(LR);
409 DEBUG(std::cerr << " +" << LR);
410 }
411 }
412 }
413
414 // Finally, this virtual register is live from the start of any killing
415 // block to the 'use' slot of the killing instruction.
416 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
417 MachineInstr *Kill = vi.Kills[i];
418 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000419 getUseIndex(getInstructionIndex(Kill))+1,
420 ValNum);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421 interval.addRange(LR);
422 DEBUG(std::cerr << " +" << LR);
423 }
424
425 } else {
426 // If this is the second time we see a virtual register definition, it
427 // must be due to phi elimination or two addr elimination. If this is
428 // the result of two address elimination, then the vreg is the first
429 // operand, and is a def-and-use.
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000430 if (mi->getOperand(0).isRegister() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000431 mi->getOperand(0).getReg() == interval.reg &&
432 mi->getOperand(0).isDef() && mi->getOperand(0).isUse()) {
433 // If this is a two-address definition, then we have already processed
434 // the live range. The only problem is that we didn't realize there
435 // are actually two values in the live interval. Because of this we
436 // need to take the LiveRegion that defines this register and split it
437 // into two values.
438 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
439 unsigned RedefIndex = getDefIndex(getInstructionIndex(mi));
440
441 // Delete the initial value, which should be short and continuous,
442 // becuase the 2-addr copy must be in the same MBB as the redef.
443 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000444
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000445 LiveRange LR(DefIndex, RedefIndex, interval.getNextValue());
446 DEBUG(std::cerr << " replace range with " << LR);
447 interval.addRange(LR);
448
449 // If this redefinition is dead, we need to add a dummy unit live
450 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000451 if (lv_->RegisterDefIsDead(mi, interval.reg))
452 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000453
454 DEBUG(std::cerr << "RESULT: " << interval);
455
456 } else {
457 // Otherwise, this must be because of phi elimination. If this is the
458 // first redefinition of the vreg that we have seen, go back and change
459 // the live range in the PHI block to be a different value number.
460 if (interval.containsOneValue()) {
461 assert(vi.Kills.size() == 1 &&
462 "PHI elimination vreg should have one kill, the PHI itself!");
463
464 // Remove the old range that we now know has an incorrect number.
465 MachineInstr *Killer = vi.Kills[0];
466 unsigned Start = getInstructionIndex(Killer->getParent()->begin());
467 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
468 DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "
469 << interval << "\n");
470 interval.removeRange(Start, End);
471 DEBUG(std::cerr << "RESULT: " << interval);
472
473 // Replace the interval with one of a NEW value number.
474 LiveRange LR(Start, End, interval.getNextValue());
475 DEBUG(std::cerr << " replace range with " << LR);
476 interval.addRange(LR);
477 DEBUG(std::cerr << "RESULT: " << interval);
478 }
479
480 // In the case of PHI elimination, each variable definition is only
481 // live until the end of the block. We've already taken care of the
482 // rest of the live range.
483 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000484 LiveRange LR(defIndex,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000485 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
486 interval.getNextValue());
487 interval.addRange(LR);
488 DEBUG(std::cerr << " +" << LR);
489 }
490 }
491
492 DEBUG(std::cerr << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493}
494
Chris Lattnerf35fef72004-07-23 21:24:19 +0000495void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000496 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000497 LiveInterval& interval,
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000498 unsigned SrcReg, unsigned DestReg,
499 bool isLiveIn)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000500{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000501 // A physical register cannot be live across basic block, so its
502 // lifetime must end somewhere in its defining basic block.
503 DEBUG(std::cerr << "\t\tregister: "; printRegName(interval.reg));
504 typedef LiveVariables::killed_iterator KillIter;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000505
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000506 unsigned baseIndex = getInstructionIndex(mi);
507 unsigned start = getDefIndex(baseIndex);
508 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000509
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 // If it is not used after definition, it is considered dead at
511 // the instruction defining it. Hence its interval is:
512 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000513 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
514 DEBUG(std::cerr << " dead");
515 end = getDefIndex(start) + 1;
516 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000517 }
518
519 // If it is not dead on definition, it must be killed by a
520 // subsequent instruction. Hence its interval is:
521 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000522 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000524 if (lv_->KillsRegister(mi, interval.reg)) {
525 DEBUG(std::cerr << " killed");
526 end = getUseIndex(baseIndex) + 1;
527 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000528 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000530
531 // The only case we should have a dead physreg here without a killing or
532 // instruction where we know it's dead is if it is live-in to the function
533 // and never used.
534 assert(isLiveIn && "physreg was not killed in defining block!");
535 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000536
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000537exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000538 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000539
540 // Finally, if this is defining a new range for the physical register, and if
541 // that physreg is just a copy from a vreg, and if THAT vreg was a copy from
542 // the physreg, then the new fragment has the same value as the one copied
543 // into the vreg.
544 if (interval.reg == DestReg && !interval.empty() &&
Chris Lattnere97568c2005-03-10 20:59:51 +0000545 MRegisterInfo::isVirtualRegister(SrcReg)) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000546
547 // Get the live interval for the vreg, see if it is defined by a copy.
548 LiveInterval &SrcInterval = getOrCreateInterval(SrcReg);
549
550 if (SrcInterval.containsOneValue()) {
551 assert(!SrcInterval.empty() && "Can't contain a value and be empty!");
552
553 // Get the first index of the first range. Though the interval may have
554 // multiple liveranges in it, we only check the first.
555 unsigned StartIdx = SrcInterval.begin()->start;
556 MachineInstr *SrcDefMI = getInstructionFromIndex(StartIdx);
557
558 // Check to see if the vreg was defined by a copy instruction, and that
559 // the source was this physreg.
560 unsigned VRegSrcSrc, VRegSrcDest;
561 if (tii_->isMoveInstr(*SrcDefMI, VRegSrcSrc, VRegSrcDest) &&
562 SrcReg == VRegSrcDest && VRegSrcSrc == DestReg) {
563 // Okay, now we know that the vreg was defined by a copy from this
564 // physreg. Find the value number being copied and use it as the value
565 // for this range.
566 const LiveRange *DefRange = interval.getLiveRangeContaining(StartIdx-1);
567 if (DefRange) {
568 LiveRange LR(start, end, DefRange->ValId);
569 interval.addRange(LR);
570 DEBUG(std::cerr << " +" << LR << '\n');
571 return;
572 }
573 }
574 }
575 }
576
577
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000578 LiveRange LR(start, end, interval.getNextValue());
579 interval.addRange(LR);
580 DEBUG(std::cerr << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000581}
582
Chris Lattnerf35fef72004-07-23 21:24:19 +0000583void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
584 MachineBasicBlock::iterator MI,
585 unsigned reg) {
586 if (MRegisterInfo::isVirtualRegister(reg))
587 handleVirtualRegisterDef(MBB, MI, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000588 else if (allocatableRegs_[reg]) {
Chris Lattnerf768bba2005-03-09 23:05:19 +0000589 unsigned SrcReg = 0, DestReg = 0;
Chris Lattner60d97d42006-01-10 05:41:59 +0000590 if (!tii_->isMoveInstr(*MI, SrcReg, DestReg))
591 SrcReg = DestReg = 0;
Chris Lattnerf768bba2005-03-09 23:05:19 +0000592
593 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(reg),
594 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000595 for (const unsigned* AS = mri_->getAliasSet(reg); *AS; ++AS)
Chris Lattnerf768bba2005-03-09 23:05:19 +0000596 handlePhysicalRegisterDef(MBB, MI, getOrCreateInterval(*AS),
597 SrcReg, DestReg);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000598 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000599}
600
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000601/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000602/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000603/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000604/// which a variable is live
605void LiveIntervals::computeIntervals()
606{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000607 DEBUG(std::cerr << "********** COMPUTING LIVE INTERVALS **********\n");
608 DEBUG(std::cerr << "********** Function: "
609 << ((Value*)mf_->getFunction())->getName() << '\n');
Chris Lattner799a9192005-04-09 16:17:50 +0000610 bool IgnoreFirstInstr = mf_->livein_begin() != mf_->livein_end();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000611
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000612 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000613 I != E; ++I) {
614 MachineBasicBlock* mbb = I;
615 DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000616
Chris Lattner799a9192005-04-09 16:17:50 +0000617 MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
618 if (IgnoreFirstInstr) { ++mi; IgnoreFirstInstr = false; }
619 for (; mi != miEnd; ++mi) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000620 const TargetInstrDescriptor& tid =
621 tm_->getInstrInfo()->get(mi->getOpcode());
Chris Lattner477e4552004-09-30 16:10:45 +0000622 DEBUG(std::cerr << getInstructionIndex(mi) << "\t" << *mi);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000623
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000624 // handle implicit defs
625 for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
626 handleRegisterDef(mbb, mi, *id);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000627
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000628 // handle explicit defs
629 for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
630 MachineOperand& mop = mi->getOperand(i);
631 // handle register defs - build intervals
632 if (mop.isRegister() && mop.getReg() && mop.isDef())
633 handleRegisterDef(mbb, mi, mop.getReg());
634 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000635 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000636 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000637}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000638
Chris Lattneraa51a482005-10-21 06:49:50 +0000639/// IntA is defined as a copy from IntB and we know it only has one value
640/// number. If all of the places that IntA and IntB overlap are defined by
641/// copies from IntA to IntB, we know that these two ranges can really be
642/// merged if we adjust the value numbers. If it is safe, adjust the value
Chris Lattnerc60e6022005-10-26 18:41:41 +0000643/// numbers and return true, allowing coalescing to occur.
Chris Lattneraa51a482005-10-21 06:49:50 +0000644bool LiveIntervals::
645AdjustIfAllOverlappingRangesAreCopiesFrom(LiveInterval &IntA,
646 LiveInterval &IntB,
647 unsigned CopyIdx) {
648 std::vector<LiveRange*> Ranges;
649 IntA.getOverlapingRanges(IntB, CopyIdx, Ranges);
650
651 assert(!Ranges.empty() && "Why didn't we do a simple join of this?");
652
653 unsigned IntBRep = rep(IntB.reg);
654
655 // Check to see if all of the overlaps (entries in Ranges) are defined by a
656 // copy from IntA. If not, exit.
657 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
658 unsigned Idx = Ranges[i]->start;
659 MachineInstr *MI = getInstructionFromIndex(Idx);
660 unsigned SrcReg, DestReg;
661 if (!tii_->isMoveInstr(*MI, SrcReg, DestReg)) return false;
662
663 // If this copy isn't actually defining this range, it must be a live
664 // range spanning basic blocks or something.
665 if (rep(DestReg) != rep(IntA.reg)) return false;
666
667 // Check to see if this is coming from IntB. If not, bail out.
668 if (rep(SrcReg) != IntBRep) return false;
669 }
670
671 // Okay, we can change this one. Get the IntB value number that IntA is
672 // copied from.
673 unsigned ActualValNo = IntA.getLiveRangeContaining(CopyIdx-1)->ValId;
674
675 // Change all of the value numbers to the same as what we IntA is copied from.
676 for (unsigned i = 0, e = Ranges.size(); i != e; ++i)
677 Ranges[i]->ValId = ActualValNo;
678
679 return true;
680}
681
Chris Lattner1c5c0442004-07-19 14:08:10 +0000682void LiveIntervals::joinIntervalsInMachineBB(MachineBasicBlock *MBB) {
Chris Lattner7ac2d312004-07-24 02:59:07 +0000683 DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000684
Chris Lattner7ac2d312004-07-24 02:59:07 +0000685 for (MachineBasicBlock::iterator mi = MBB->begin(), mie = MBB->end();
686 mi != mie; ++mi) {
687 DEBUG(std::cerr << getInstructionIndex(mi) << '\t' << *mi);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000688
Chris Lattner7ac2d312004-07-24 02:59:07 +0000689 // we only join virtual registers with allocatable
690 // physical registers since we do not have liveness information
691 // on not allocatable physical registers
Chris Lattneraa51a482005-10-21 06:49:50 +0000692 unsigned SrcReg, DestReg;
693 if (tii_->isMoveInstr(*mi, SrcReg, DestReg) &&
694 (MRegisterInfo::isVirtualRegister(SrcReg) || allocatableRegs_[SrcReg])&&
695 (MRegisterInfo::isVirtualRegister(DestReg)||allocatableRegs_[DestReg])){
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000696
Chris Lattner7ac2d312004-07-24 02:59:07 +0000697 // Get representative registers.
Chris Lattneraa51a482005-10-21 06:49:50 +0000698 SrcReg = rep(SrcReg);
699 DestReg = rep(DestReg);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000700
Chris Lattner7ac2d312004-07-24 02:59:07 +0000701 // If they are already joined we continue.
Chris Lattneraa51a482005-10-21 06:49:50 +0000702 if (SrcReg == DestReg)
Chris Lattner7ac2d312004-07-24 02:59:07 +0000703 continue;
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000704
Chris Lattner7ac2d312004-07-24 02:59:07 +0000705 // If they are both physical registers, we cannot join them.
Chris Lattneraa51a482005-10-21 06:49:50 +0000706 if (MRegisterInfo::isPhysicalRegister(SrcReg) &&
707 MRegisterInfo::isPhysicalRegister(DestReg))
Chris Lattner7ac2d312004-07-24 02:59:07 +0000708 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000709
Chris Lattner7ac2d312004-07-24 02:59:07 +0000710 // If they are not of the same register class, we cannot join them.
Chris Lattneraa51a482005-10-21 06:49:50 +0000711 if (differingRegisterClasses(SrcReg, DestReg))
Chris Lattner7ac2d312004-07-24 02:59:07 +0000712 continue;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000713
Chris Lattneraa51a482005-10-21 06:49:50 +0000714 LiveInterval &SrcInt = getInterval(SrcReg);
715 LiveInterval &DestInt = getInterval(DestReg);
716 assert(SrcInt.reg == SrcReg && DestInt.reg == DestReg &&
Chris Lattner7ac2d312004-07-24 02:59:07 +0000717 "Register mapping is horribly broken!");
Chris Lattner060913c2004-07-24 04:32:22 +0000718
Chris Lattneraa51a482005-10-21 06:49:50 +0000719 DEBUG(std::cerr << "\t\tInspecting " << SrcInt << " and " << DestInt
720 << ": ");
Chris Lattner060913c2004-07-24 04:32:22 +0000721
Chris Lattner4df98e52004-07-24 03:32:06 +0000722 // If two intervals contain a single value and are joined by a copy, it
723 // does not matter if the intervals overlap, they can always be joined.
Chris Lattneraa51a482005-10-21 06:49:50 +0000724 bool Joinable = SrcInt.containsOneValue() && DestInt.containsOneValue();
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000725
Chris Lattner7ac2d312004-07-24 02:59:07 +0000726 unsigned MIDefIdx = getDefIndex(getInstructionIndex(mi));
Chris Lattneraa51a482005-10-21 06:49:50 +0000727
728 // If the intervals think that this is joinable, do so now.
729 if (!Joinable && DestInt.joinable(SrcInt, MIDefIdx))
730 Joinable = true;
Chris Lattner1c5c0442004-07-19 14:08:10 +0000731
Chris Lattneraa51a482005-10-21 06:49:50 +0000732 // If DestInt is actually a copy from SrcInt (which we know) that is used
733 // to define another value of SrcInt, we can change the other range of
734 // SrcInt to be the value of the range that defines DestInt, allowing a
Chris Lattnerc60e6022005-10-26 18:41:41 +0000735 // coalesce.
Chris Lattneraa51a482005-10-21 06:49:50 +0000736 if (!Joinable && DestInt.containsOneValue() &&
737 AdjustIfAllOverlappingRangesAreCopiesFrom(SrcInt, DestInt, MIDefIdx))
738 Joinable = true;
739
740 if (!Joinable || overlapsAliases(&SrcInt, &DestInt)) {
741 DEBUG(std::cerr << "Interference!\n");
742 } else {
743 DestInt.join(SrcInt, MIDefIdx);
744 DEBUG(std::cerr << "Joined. Result = " << DestInt << "\n");
745
746 if (!MRegisterInfo::isPhysicalRegister(SrcReg)) {
747 r2iMap_.erase(SrcReg);
748 r2rMap_[SrcReg] = DestReg;
Chris Lattner7ac2d312004-07-24 02:59:07 +0000749 } else {
750 // Otherwise merge the data structures the other way so we don't lose
751 // the physreg information.
Chris Lattneraa51a482005-10-21 06:49:50 +0000752 r2rMap_[DestReg] = SrcReg;
753 DestInt.reg = SrcReg;
754 SrcInt.swap(DestInt);
755 r2iMap_.erase(DestReg);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000756 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000757 ++numJoins;
Chris Lattner7ac2d312004-07-24 02:59:07 +0000758 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000759 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000760 }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000761}
762
Chris Lattnercc0d1562004-07-19 14:40:29 +0000763namespace {
764 // DepthMBBCompare - Comparison predicate that sort first based on the loop
765 // depth of the basic block (the unsigned), and then on the MBB number.
766 struct DepthMBBCompare {
767 typedef std::pair<unsigned, MachineBasicBlock*> DepthMBBPair;
768 bool operator()(const DepthMBBPair &LHS, const DepthMBBPair &RHS) const {
769 if (LHS.first > RHS.first) return true; // Deeper loops first
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000770 return LHS.first == RHS.first &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000771 LHS.second->getNumber() < RHS.second->getNumber();
Chris Lattnercc0d1562004-07-19 14:40:29 +0000772 }
773 };
774}
Chris Lattner1c5c0442004-07-19 14:08:10 +0000775
Chris Lattnercc0d1562004-07-19 14:40:29 +0000776void LiveIntervals::joinIntervals() {
777 DEBUG(std::cerr << "********** JOINING INTERVALS ***********\n");
778
779 const LoopInfo &LI = getAnalysis<LoopInfo>();
780 if (LI.begin() == LI.end()) {
781 // If there are no loops in the function, join intervals in function order.
Chris Lattner1c5c0442004-07-19 14:08:10 +0000782 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
783 I != E; ++I)
784 joinIntervalsInMachineBB(I);
Chris Lattnercc0d1562004-07-19 14:40:29 +0000785 } else {
786 // Otherwise, join intervals in inner loops before other intervals.
787 // Unfortunately we can't just iterate over loop hierarchy here because
788 // there may be more MBB's than BB's. Collect MBB's for sorting.
789 std::vector<std::pair<unsigned, MachineBasicBlock*> > MBBs;
790 for (MachineFunction::iterator I = mf_->begin(), E = mf_->end();
791 I != E; ++I)
792 MBBs.push_back(std::make_pair(LI.getLoopDepth(I->getBasicBlock()), I));
793
794 // Sort by loop depth.
795 std::sort(MBBs.begin(), MBBs.end(), DepthMBBCompare());
796
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000797 // Finally, join intervals in loop nest order.
Chris Lattnercc0d1562004-07-19 14:40:29 +0000798 for (unsigned i = 0, e = MBBs.size(); i != e; ++i)
799 joinIntervalsInMachineBB(MBBs[i].second);
800 }
Chris Lattnerc83e40d2004-07-25 03:24:11 +0000801
802 DEBUG(std::cerr << "*** Register mapping ***\n");
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000803 DEBUG(for (int i = 0, e = r2rMap_.size(); i != e; ++i)
804 if (r2rMap_[i])
805 std::cerr << " reg " << i << " -> reg " << r2rMap_[i] << "\n");
Chris Lattner1c5c0442004-07-19 14:08:10 +0000806}
807
Chris Lattner7ac2d312004-07-24 02:59:07 +0000808/// Return true if the two specified registers belong to different register
809/// classes. The registers may be either phys or virt regs.
810bool LiveIntervals::differingRegisterClasses(unsigned RegA,
811 unsigned RegB) const {
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000812
Chris Lattner7ac2d312004-07-24 02:59:07 +0000813 // Get the register classes for the first reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000814 if (MRegisterInfo::isPhysicalRegister(RegA)) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000815 assert(MRegisterInfo::isVirtualRegister(RegB) &&
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000816 "Shouldn't consider two physregs!");
817 return !mf_->getSSARegMap()->getRegClass(RegB)->contains(RegA);
818 }
Chris Lattner7ac2d312004-07-24 02:59:07 +0000819
820 // Compare against the regclass for the second reg.
Chris Lattnerad3c74f2004-10-26 05:29:18 +0000821 const TargetRegisterClass *RegClass = mf_->getSSARegMap()->getRegClass(RegA);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000822 if (MRegisterInfo::isVirtualRegister(RegB))
823 return RegClass != mf_->getSSARegMap()->getRegClass(RegB);
824 else
Chris Lattnerd0d0a1a2004-08-24 17:48:29 +0000825 return !RegClass->contains(RegB);
Chris Lattner7ac2d312004-07-24 02:59:07 +0000826}
827
828bool LiveIntervals::overlapsAliases(const LiveInterval *LHS,
829 const LiveInterval *RHS) const {
830 if (!MRegisterInfo::isPhysicalRegister(LHS->reg)) {
831 if (!MRegisterInfo::isPhysicalRegister(RHS->reg))
832 return false; // vreg-vreg merge has no aliases!
833 std::swap(LHS, RHS);
834 }
835
836 assert(MRegisterInfo::isPhysicalRegister(LHS->reg) &&
837 MRegisterInfo::isVirtualRegister(RHS->reg) &&
838 "first interval must describe a physical register");
839
Chris Lattner4df98e52004-07-24 03:32:06 +0000840 for (const unsigned *AS = mri_->getAliasSet(LHS->reg); *AS; ++AS)
841 if (RHS->overlaps(getInterval(*AS)))
842 return true;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000843
Chris Lattner4df98e52004-07-24 03:32:06 +0000844 return false;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000845}
846
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000847LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000848 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Chris Lattner28696be2005-01-08 19:55:00 +0000849 (float)HUGE_VAL :0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000850 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000851}