blob: 4d0916046a0f4d45989d2913e19f8bd2856601b7 [file] [log] [blame]
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001//===-- LiveIntervals.cpp - Live Interval Analysis ------------------------===//
2//
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"
19#include "llvm/CodeGen/LiveIntervals.h"
20#include "llvm/Function.h"
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000021#include "llvm/Analysis/LoopInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#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"
31#include "llvm/Target/TargetRegInfo.h"
32#include "llvm/Support/CFG.h"
33#include "Support/Debug.h"
34#include "Support/DepthFirstIterator.h"
35#include "Support/Statistic.h"
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037#include <iostream>
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000038#include <limits>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000039
40using namespace llvm;
41
42namespace {
43 RegisterAnalysis<LiveIntervals> X("liveintervals",
44 "Live Interval Analysis");
45
46 Statistic<> numIntervals("liveintervals", "Number of intervals");
47};
48
49void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const
50{
Alkis Evlogimenosf6f91bf2003-12-15 04:55:38 +000051 AU.addPreserved<LiveVariables>();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000052 AU.addRequired<LiveVariables>();
Alkis Evlogimenosf6f91bf2003-12-15 04:55:38 +000053 AU.addPreservedID(PHIEliminationID);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000054 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000055 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000056 AU.addRequired<LoopInfo>();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000057 MachineFunctionPass::getAnalysisUsage(AU);
58}
59
60/// runOnMachineFunction - Register allocate the whole function
61///
62bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
63 DEBUG(std::cerr << "Machine Function\n");
64 mf_ = &fn;
65 tm_ = &fn.getTarget();
66 mri_ = tm_->getRegisterInfo();
67 lv_ = &getAnalysis<LiveVariables>();
68 allocatableRegisters_.clear();
69 mbbi2mbbMap_.clear();
70 mi2iMap_.clear();
71 r2iMap_.clear();
72 r2iMap_.clear();
73 intervals_.clear();
74
75 // mark allocatable registers
76 allocatableRegisters_.resize(MRegisterInfo::FirstVirtualRegister);
77 // Loop over all of the register classes...
78 for (MRegisterInfo::regclass_iterator
79 rci = mri_->regclass_begin(), rce = mri_->regclass_end();
80 rci != rce; ++rci) {
81 // Loop over all of the allocatable registers in the function...
82 for (TargetRegisterClass::iterator
83 i = (*rci)->allocation_order_begin(*mf_),
84 e = (*rci)->allocation_order_end(*mf_); i != e; ++i) {
85 allocatableRegisters_[*i] = true; // The reg is allocatable!
86 }
87 }
88
89 // number MachineInstrs
90 unsigned miIndex = 0;
91 for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end();
92 mbb != mbbEnd; ++mbb) {
93 const std::pair<MachineBasicBlock*, unsigned>& entry =
94 lv_->getMachineBasicBlockInfo(&*mbb);
95 bool inserted = mbbi2mbbMap_.insert(std::make_pair(entry.second,
96 entry.first)).second;
97 assert(inserted && "multiple index -> MachineBasicBlock");
98
99 for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
100 mi != miEnd; ++mi) {
101 inserted = mi2iMap_.insert(std::make_pair(*mi, miIndex)).second;
102 assert(inserted && "multiple MachineInstr -> index mappings");
103 ++miIndex;
104 }
105 }
106
107 computeIntervals();
108
Alkis Evlogimenos7a40eaa2003-12-24 15:44:53 +0000109 // compute spill weights
110 const LoopInfo& loopInfo = getAnalysis<LoopInfo>();
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000111 const TargetInstrInfo& tii = tm_->getInstrInfo();
Alkis Evlogimenos7a40eaa2003-12-24 15:44:53 +0000112
113 for (MbbIndex2MbbMap::iterator
114 it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
115 it != itEnd; ++it) {
116 MachineBasicBlock* mbb = it->second;
117
118 unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
119
120 for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
121 mi != miEnd; ++mi) {
122 MachineInstr* instr = *mi;
123 for (int i = instr->getNumOperands() - 1; i >= 0; --i) {
124 MachineOperand& mop = instr->getOperand(i);
125
126 if (!mop.isVirtualRegister())
127 continue;
128
129 unsigned reg = mop.getAllocatedRegNum();
130 Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg);
131 assert(r2iit != r2iMap_.end());
132 intervals_[r2iit->second].weight += pow(10.0F, loopDepth);
133 }
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000134
135 // add hints for coalescing
136 unsigned src, dst;
137 if (tii.isMoveInstr(*instr, src, dst)) {
138 if (src >= MRegisterInfo::FirstVirtualRegister) {
139 Reg2IntervalMap::iterator r2iit = r2iMap_.find(src);
140 assert(r2iit != r2iMap_.end());
141 intervals_[r2iit->second].hint = dst;
142 }
143 if (dst >= MRegisterInfo::FirstVirtualRegister) {
144 Reg2IntervalMap::iterator r2iit = r2iMap_.find(dst);
145 assert(r2iit != r2iMap_.end());
146 intervals_[r2iit->second].hint = src;
147 }
148 }
Alkis Evlogimenos7a40eaa2003-12-24 15:44:53 +0000149 }
150 }
151
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000152 return true;
153}
154
155void LiveIntervals::printRegName(unsigned reg) const
156{
157 if (reg < MRegisterInfo::FirstVirtualRegister)
158 std::cerr << mri_->getName(reg);
159 else
160 std::cerr << '%' << reg;
161}
162
163void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
164 MachineBasicBlock::iterator mi,
165 unsigned reg)
166{
167 DEBUG(std::cerr << "\t\t\tregister: ";printRegName(reg); std::cerr << '\n');
168
169 unsigned instrIndex = getInstructionIndex(*mi);
170
171 LiveVariables::VarInfo& vi = lv_->getVarInfo(reg);
172
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000173 Interval* interval = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000174 Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg);
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000175 if (r2iit == r2iMap_.end()) {
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000176 // add new interval
177 intervals_.push_back(Interval(reg));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000178 // update interval index for this register
179 r2iMap_[reg] = intervals_.size() - 1;
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000180 interval = &intervals_.back();
181 }
182 else {
183 interval = &intervals_[r2iit->second];
184 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000185
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000186 for (MbbIndex2MbbMap::iterator
187 it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
188 it != itEnd; ++it) {
189 unsigned liveBlockIndex = it->first;
190 MachineBasicBlock* liveBlock = it->second;
191 if (liveBlockIndex < vi.AliveBlocks.size() &&
Alkis Evlogimenos056063e2003-12-22 13:54:29 +0000192 vi.AliveBlocks[liveBlockIndex] &&
193 !liveBlock->empty()) {
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000194 unsigned start = getInstructionIndex(liveBlock->front());
195 unsigned end = getInstructionIndex(liveBlock->back()) + 1;
196 interval->addRange(start, end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000197 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000198 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000199
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000200 bool killedInDefiningBasicBlock = false;
201 for (int i = 0, e = vi.Kills.size(); i != e; ++i) {
202 MachineBasicBlock* killerBlock = vi.Kills[i].first;
203 MachineInstr* killerInstr = vi.Kills[i].second;
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000204 unsigned start = (mbb == killerBlock ?
205 instrIndex :
206 getInstructionIndex(killerBlock->front()));
207 unsigned end = getInstructionIndex(killerInstr) + 1;
Alkis Evlogimenos43f692f2003-12-18 08:53:43 +0000208 if (start < end) {
209 killedInDefiningBasicBlock |= mbb == killerBlock;
210 interval->addRange(start, end);
211 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000212 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000213
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000214 if (!killedInDefiningBasicBlock) {
215 unsigned end = getInstructionIndex(mbb->back()) + 1;
216 interval->addRange(instrIndex, end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000217 }
218}
219
220void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock* mbb,
221 MachineBasicBlock::iterator mi,
222 unsigned reg)
223{
224 DEBUG(std::cerr << "\t\t\tregister: ";printRegName(reg); std::cerr << '\n');
Alkis Evlogimenos4c214d22003-12-13 11:11:02 +0000225 if (!lv_->getAllocatablePhysicalRegisters()[reg]) {
226 DEBUG(std::cerr << "\t\t\t\tnon allocatable register: ignoring\n");
227 return;
228 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000229
230 unsigned start = getInstructionIndex(*mi);
231 unsigned end = start;
232
233 for (MachineBasicBlock::iterator e = mbb->end(); mi != e; ++mi) {
234 for (LiveVariables::killed_iterator
235 ki = lv_->dead_begin(*mi),
236 ke = lv_->dead_end(*mi);
237 ki != ke; ++ki) {
238 if (reg == ki->second) {
239 end = getInstructionIndex(ki->first) + 1;
240 goto exit;
241 }
242 }
243
244 for (LiveVariables::killed_iterator
245 ki = lv_->killed_begin(*mi),
246 ke = lv_->killed_end(*mi);
247 ki != ke; ++ki) {
248 if (reg == ki->second) {
249 end = getInstructionIndex(ki->first) + 1;
250 goto exit;
251 }
252 }
253 }
254exit:
255 assert(start < end && "did not find end of interval?");
256
257 Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg);
258 if (r2iit != r2iMap_.end()) {
259 unsigned ii = r2iit->second;
260 Interval& interval = intervals_[ii];
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000261 interval.addRange(start, end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000262 }
263 else {
264 intervals_.push_back(Interval(reg));
265 Interval& interval = intervals_.back();
266 // update interval index for this register
267 r2iMap_[reg] = intervals_.size() - 1;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000268 interval.addRange(start, end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000269 }
270}
271
272void LiveIntervals::handleRegisterDef(MachineBasicBlock* mbb,
273 MachineBasicBlock::iterator mi,
274 unsigned reg)
275{
276 if (reg < MRegisterInfo::FirstVirtualRegister) {
277 if (allocatableRegisters_[reg]) {
278 handlePhysicalRegisterDef(mbb, mi, reg);
279 }
280 }
281 else {
282 handleVirtualRegisterDef(mbb, mi, reg);
283 }
284}
285
286unsigned LiveIntervals::getInstructionIndex(MachineInstr* instr) const
287{
288 assert(mi2iMap_.find(instr) != mi2iMap_.end() &&
289 "instruction not assigned a number");
290 return mi2iMap_.find(instr)->second;
291}
292
293/// computeIntervals - computes the live intervals for virtual
294/// registers. for some ordering of the machine instructions [1,N] a
295/// live interval is an interval [i, j] where 1 <= i <= j <= N for
296/// which a variable is live
297void LiveIntervals::computeIntervals()
298{
299 DEBUG(std::cerr << "computing live intervals:\n");
300
301 for (MbbIndex2MbbMap::iterator
302 it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
303 it != itEnd; ++it) {
304 MachineBasicBlock* mbb = it->second;
305 DEBUG(std::cerr << "machine basic block: "
306 << mbb->getBasicBlock()->getName() << "\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000307
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000308 for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
309 mi != miEnd; ++mi) {
310 MachineInstr* instr = *mi;
311 const TargetInstrDescriptor& tid =
312 tm_->getInstrInfo().get(instr->getOpcode());
313 DEBUG(std::cerr << "\t\tinstruction["
314 << getInstructionIndex(instr) << "]: ";
315 instr->print(std::cerr, *tm_););
316
317 // handle implicit defs
318 for (const unsigned* id = tid.ImplicitDefs; *id; ++id) {
319 unsigned physReg = *id;
320 handlePhysicalRegisterDef(mbb, mi, physReg);
321 }
322
323 // handle explicit defs
324 for (int i = instr->getNumOperands() - 1; i >= 0; --i) {
325 MachineOperand& mop = instr->getOperand(i);
326
Alkis Evlogimenos9435eda2003-12-13 05:26:39 +0000327 if (!mop.isRegister())
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000328 continue;
329
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000330 unsigned reg = mop.getAllocatedRegNum();
331 // handle defs - build intervals
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000332 if (mop.isDef()) {
Alkis Evlogimenos9435eda2003-12-13 05:26:39 +0000333 if (reg < MRegisterInfo::FirstVirtualRegister)
334 handlePhysicalRegisterDef(mbb, mi, reg);
335 else
336 handleVirtualRegisterDef(mbb, mi, reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000337 }
338 }
339 }
340 }
341
Alkis Evlogimenos91ceae62003-12-05 10:32:01 +0000342 std::sort(intervals_.begin(), intervals_.end(), StartPointComp());
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000343 DEBUG(std::copy(intervals_.begin(), intervals_.end(),
344 std::ostream_iterator<Interval>(std::cerr, "\n")));
345}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000346
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000347LiveIntervals::Interval::Interval(unsigned r)
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000348 : reg(r), hint(0),
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000349 weight((r < MRegisterInfo::FirstVirtualRegister ?
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000350 std::numeric_limits<float>::max() : 0.0F))
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000351{
352
353}
354
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000355void LiveIntervals::Interval::addRange(unsigned start, unsigned end)
356{
357 DEBUG(std::cerr << "\t\t\t\tadding range: [" << start <<','<< end << "]\n");
358 //assert(start < end && "invalid range?");
359 Range range = std::make_pair(start, end);
360 Ranges::iterator it =
361 ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), range),
362 range);
363
364 DEBUG(std::cerr << "\t\t\t\tbefore merge forward: " << *this << '\n');
365 mergeRangesForward(it);
366 DEBUG(std::cerr << "\t\t\t\tbefore merge backward: " << *this << '\n');
367 mergeRangesBackward(it);
368 DEBUG(std::cerr << "\t\t\t\tafter merging: " << *this << '\n');
369}
370
371void LiveIntervals::Interval::mergeRangesForward(Ranges::iterator it)
372{
373 for (Ranges::iterator next = it + 1;
374 next != ranges.end() && it->second >= next->first; ) {
375 it->second = std::max(it->second, next->second);
376 next = ranges.erase(next);
377 }
378}
379
380void LiveIntervals::Interval::mergeRangesBackward(Ranges::iterator it)
381{
382 for (Ranges::iterator prev = it - 1;
383 it != ranges.begin() && it->first <= prev->second; ) {
384 it->first = std::min(it->first, prev->first);
385 it->second = std::max(it->second, prev->second);
386 it = ranges.erase(prev);
387 prev = it - 1;
388 }
389}
390
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000391bool LiveIntervals::Interval::liveAt(unsigned index) const
392{
393 Ranges::const_iterator r = ranges.begin();
394 while (r != ranges.end() && index < r->second) {
395 if (index >= r->first)
396 return true;
397 ++r;
398 }
399 return false;
400}
401
402bool LiveIntervals::Interval::overlaps(const Interval& other) const
403{
404 std::vector<bool> bitMap(end(), false);
405 for (Ranges::const_iterator r = ranges.begin(); r != ranges.end(); ++r) {
406 for (unsigned i = r->first; i < r->second; ++i)
407 bitMap[i] = true;
408 }
409 for (Ranges::const_iterator r = other.ranges.begin();
410 r != other.ranges.end(); ++r) {
411 for (unsigned i = r->first;
412 i < r->second && i < bitMap.size(); ++i)
413 if (bitMap[i])
414 return true;
415 }
416
417 return false;
418}
419
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000420std::ostream& llvm::operator<<(std::ostream& os,
421 const LiveIntervals::Interval& li)
422{
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000423 os << "%reg" << li.reg << ',' << li.weight << " = ";
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000424 for (LiveIntervals::Interval::Ranges::const_iterator
425 i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
426 os << "[" << i->first << "," << i->second << "]";
427 }
428 return os;
429}