Fix spilled interval update. It was too conservative.
[oota-llvm.git] / lib / CodeGen / LiveIntervalAnalysis.cpp
1 //===-- 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/Analysis/LoopInfo.h"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/SSARegMap.h"
26 #include "llvm/Target/MRegisterInfo.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Support/CFG.h"
30 #include "Support/CommandLine.h"
31 #include "Support/Debug.h"
32 #include "Support/Statistic.h"
33 #include "Support/STLExtras.h"
34 #include <cmath>
35 #include <iostream>
36 #include <limits>
37
38 using namespace llvm;
39
40 namespace {
41     RegisterAnalysis<LiveIntervals> X("liveintervals",
42                                       "Live Interval Analysis");
43
44     Statistic<> numIntervals("liveintervals", "Number of intervals");
45     Statistic<> numJoined   ("liveintervals", "Number of joined intervals");
46     Statistic<> numPeep     ("liveintervals", "Number of identity moves "
47                              "eliminated after coalescing");
48
49     cl::opt<bool>
50     join("join-liveintervals",
51          cl::desc("Join compatible live intervals"),
52          cl::init(true));
53 };
54
55 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const
56 {
57     AU.addPreserved<LiveVariables>();
58     AU.addRequired<LiveVariables>();
59     AU.addPreservedID(PHIEliminationID);
60     AU.addRequiredID(PHIEliminationID);
61     AU.addRequiredID(TwoAddressInstructionPassID);
62     AU.addRequired<LoopInfo>();
63     MachineFunctionPass::getAnalysisUsage(AU);
64 }
65
66 void LiveIntervals::releaseMemory()
67 {
68     mbbi2mbbMap_.clear();
69     mi2iMap_.clear();
70     i2miMap_.clear();
71     r2iMap_.clear();
72     r2rMap_.clear();
73     intervals_.clear();
74 }
75
76
77 /// runOnMachineFunction - Register allocate the whole function
78 ///
79 bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
80     DEBUG(std::cerr << "MACHINE FUNCTION: "; fn.print(std::cerr));
81     mf_ = &fn;
82     tm_ = &fn.getTarget();
83     mri_ = tm_->getRegisterInfo();
84     lv_ = &getAnalysis<LiveVariables>();
85
86     // number MachineInstrs
87     unsigned miIndex = 0;
88     for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end();
89          mbb != mbbEnd; ++mbb) {
90         const std::pair<MachineBasicBlock*, unsigned>& entry =
91             lv_->getMachineBasicBlockInfo(mbb);
92         bool inserted = mbbi2mbbMap_.insert(std::make_pair(entry.second,
93                                                            entry.first)).second;
94         assert(inserted && "multiple index -> MachineBasicBlock");
95
96         for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
97              mi != miEnd; ++mi) {
98             inserted = mi2iMap_.insert(std::make_pair(mi, miIndex)).second;
99             assert(inserted && "multiple MachineInstr -> index mappings");
100             i2miMap_.push_back(mi);
101             miIndex += 2;
102         }
103     }
104
105     computeIntervals();
106
107     numIntervals += intervals_.size();
108
109     // join intervals if requested
110     if (join) joinIntervals();
111
112     // perform a final pass over the instructions and compute spill
113     // weights, coalesce virtual registers and remove identity moves
114     const LoopInfo& loopInfo = getAnalysis<LoopInfo>();
115     const TargetInstrInfo& tii = tm_->getInstrInfo();
116
117     for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
118          mbbi != mbbe; ++mbbi) {
119         MachineBasicBlock* mbb = mbbi;
120         unsigned loopDepth = loopInfo.getLoopDepth(mbb->getBasicBlock());
121
122         for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
123              mii != mie; ) {
124             for (unsigned i = 0; i < mii->getNumOperands(); ++i) {
125                 const MachineOperand& mop = mii->getOperand(i);
126                 if (mop.isRegister()) {
127                     // replace register with representative register
128                     unsigned reg = rep(mop.getReg());
129                     mii->SetMachineOperandReg(i, reg);
130
131                     if (MRegisterInfo::isVirtualRegister(reg)) {
132                         Reg2IntervalMap::iterator r2iit = r2iMap_.find(reg);
133                         assert(r2iit != r2iMap_.end());
134                         r2iit->second->weight += pow(10.0F, loopDepth);
135                     }
136                 }
137             }
138
139             // if the move is now an identity move delete it
140             unsigned srcReg, dstReg;
141             if (tii.isMoveInstr(*mii, srcReg, dstReg) && srcReg == dstReg) {
142                 // remove index -> MachineInstr and
143                 // MachineInstr -> index mappings
144                 Mi2IndexMap::iterator mi2i = mi2iMap_.find(mii);
145                 if (mi2i != mi2iMap_.end()) {
146                     i2miMap_[mi2i->second/2] = 0;
147                     mi2iMap_.erase(mi2i);
148                 }
149                 mii = mbbi->erase(mii);
150                 ++numPeep;
151             }
152             else
153                 ++mii;
154         }
155     }
156
157     intervals_.sort(StartPointComp());
158     DEBUG(std::cerr << "*** INTERVALS ***\n");
159     DEBUG(std::copy(intervals_.begin(), intervals_.end(),
160                     std::ostream_iterator<Interval>(std::cerr, "\n")));
161     DEBUG(std::cerr << "*** MACHINEINSTRS ***\n");
162     DEBUG(
163         for (unsigned i = 0; i != i2miMap_.size(); ++i) {
164             if (const MachineInstr* mi = i2miMap_[i]) {
165                 std:: cerr << i*2 << '\t';
166                 mi->print(std::cerr, *tm_);
167             }
168         });
169
170     return true;
171 }
172
173 void LiveIntervals::updateSpilledInterval(Interval& li)
174 {
175     assert(li.weight != std::numeric_limits<float>::infinity() &&
176            "attempt to spill already spilled interval!");
177     Interval::Ranges oldRanges;
178     swap(oldRanges, li.ranges);
179
180     for (Interval::Ranges::iterator i = oldRanges.begin(), e = oldRanges.end();
181          i != e; ++i) {
182         unsigned index = i->first & ~1;
183         unsigned end = i->second;
184
185         for (; index < end; index += 2) {
186             // skip deleted instructions
187             while (!getInstructionFromIndex(index)) index += 2;
188             MachineInstr* mi = getInstructionFromIndex(index);
189             for (unsigned i = 0; i < mi->getNumOperands(); ++i) {
190                 MachineOperand& mop = mi->getOperand(i);
191                 if (mop.isRegister()) {
192                     unsigned reg = mop.getReg();
193                     if (rep(reg) == li.reg) {
194                         if (mop.isUse())
195                             li.addRange(index, index+2);
196                         else
197                             li.addRange(index+1, index+2);
198                     }
199                 }
200             }
201         }
202     }
203     // the new spill weight is now infinity as it cannot be spilled again
204     li.weight = std::numeric_limits<float>::infinity();
205 }
206
207 void LiveIntervals::printRegName(unsigned reg) const
208 {
209     if (MRegisterInfo::isPhysicalRegister(reg))
210         std::cerr << mri_->getName(reg);
211     else
212         std::cerr << '%' << reg;
213 }
214
215 void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock* mbb,
216                                              MachineBasicBlock::iterator mi,
217                                              unsigned reg)
218 {
219     DEBUG(std::cerr << "\t\tregister: ";printRegName(reg); std::cerr << '\n');
220
221     LiveVariables::VarInfo& vi = lv_->getVarInfo(reg);
222
223     Interval* interval = 0;
224     Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg);
225     if (r2iit == r2iMap_.end() || r2iit->first != reg) {
226         // add new interval
227         intervals_.push_back(Interval(reg));
228         // update interval index for this register
229         r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end()));
230         interval = &intervals_.back();
231
232         // iterate over all of the blocks that the variable is
233         // completely live in, adding them to the live
234         // interval. obviously we only need to do this once.
235         for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
236             if (vi.AliveBlocks[i]) {
237                 MachineBasicBlock* mbb = lv_->getIndexMachineBasicBlock(i);
238                 if (!mbb->empty()) {
239                     interval->addRange(getInstructionIndex(&mbb->front()),
240                                        getInstructionIndex(&mbb->back()) + 1);
241                 }
242             }
243         }
244     }
245     else {
246         interval = &*r2iit->second;
247     }
248
249     // we consider defs to happen at the second time slot of the
250     // instruction
251     unsigned instrIndex = getInstructionIndex(mi) + 1;
252
253     bool killedInDefiningBasicBlock = false;
254     for (int i = 0, e = vi.Kills.size(); i != e; ++i) {
255         MachineBasicBlock* killerBlock = vi.Kills[i].first;
256         MachineInstr* killerInstr = vi.Kills[i].second;
257         unsigned start = (mbb == killerBlock ?
258                           instrIndex :
259                           getInstructionIndex(&killerBlock->front()));
260         unsigned end = (killerInstr == mi ?
261                         instrIndex + 1 : // dead
262                         getInstructionIndex(killerInstr) + 1); // killed
263         // we do not want to add invalid ranges. these can happen when
264         // a variable has its latest use and is redefined later on in
265         // the same basic block (common with variables introduced by
266         // PHI elimination)
267         if (start < end) {
268             killedInDefiningBasicBlock |= mbb == killerBlock;
269             interval->addRange(start, end);
270         }
271     }
272
273     if (!killedInDefiningBasicBlock) {
274         unsigned end = getInstructionIndex(&mbb->back()) + 1;
275         interval->addRange(instrIndex, end);
276     }
277 }
278
279 void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock* mbb,
280                                               MachineBasicBlock::iterator mi,
281                                               unsigned reg)
282 {
283     typedef LiveVariables::killed_iterator KillIter;
284
285     DEBUG(std::cerr << "\t\tregister: "; printRegName(reg));
286
287     MachineBasicBlock::iterator e = mbb->end();
288     // we consider defs to happen at the second time slot of the
289     // instruction
290     unsigned start, end;
291     start = end = getInstructionIndex(mi) + 1;
292
293     // a variable can be dead by the instruction defining it
294     for (KillIter ki = lv_->dead_begin(mi), ke = lv_->dead_end(mi);
295          ki != ke; ++ki) {
296         if (reg == ki->second) {
297             DEBUG(std::cerr << " dead\n");
298             ++end;
299             goto exit;
300         }
301     }
302
303     // a variable can only be killed by subsequent instructions
304     do {
305         ++mi;
306         end += 2;
307         for (KillIter ki = lv_->killed_begin(mi), ke = lv_->killed_end(mi);
308              ki != ke; ++ki) {
309             if (reg == ki->second) {
310                 DEBUG(std::cerr << " killed\n");
311                 goto exit;
312             }
313         }
314     } while (mi != e);
315
316 exit:
317     assert(start < end && "did not find end of interval?");
318
319     Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg);
320     if (r2iit != r2iMap_.end() && r2iit->first == reg) {
321         r2iit->second->addRange(start, end);
322     }
323     else {
324         intervals_.push_back(Interval(reg));
325         // update interval index for this register
326         r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end()));
327         intervals_.back().addRange(start, end);
328     }
329 }
330
331 void LiveIntervals::handleRegisterDef(MachineBasicBlock* mbb,
332                                       MachineBasicBlock::iterator mi,
333                                       unsigned reg)
334 {
335     if (MRegisterInfo::isPhysicalRegister(reg)) {
336         if (lv_->getAllocatablePhysicalRegisters()[reg]) {
337             handlePhysicalRegisterDef(mbb, mi, reg);
338             for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as)
339                 handlePhysicalRegisterDef(mbb, mi, *as);
340         }
341     }
342     else {
343         handleVirtualRegisterDef(mbb, mi, reg);
344     }
345 }
346
347 unsigned LiveIntervals::getInstructionIndex(MachineInstr* instr) const
348 {
349     Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
350     return it == mi2iMap_.end() ? std::numeric_limits<unsigned>::max() : it->second;
351 }
352
353 MachineInstr* LiveIntervals::getInstructionFromIndex(unsigned index) const
354 {
355     index /= 2; // convert index to vector index
356     assert(index < i2miMap_.size() &&
357            "index does not correspond to an instruction");
358     return i2miMap_[index];
359 }
360
361 /// computeIntervals - computes the live intervals for virtual
362 /// registers. for some ordering of the machine instructions [1,N] a
363 /// live interval is an interval [i, j) where 1 <= i <= j < N for
364 /// which a variable is live
365 void LiveIntervals::computeIntervals()
366 {
367     DEBUG(std::cerr << "*** COMPUTING LIVE INTERVALS ***\n");
368
369     for (MbbIndex2MbbMap::iterator
370              it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
371          it != itEnd; ++it) {
372         MachineBasicBlock* mbb = it->second;
373         DEBUG(std::cerr << mbb->getBasicBlock()->getName() << ":\n");
374
375         for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end();
376              mi != miEnd; ++mi) {
377             const TargetInstrDescriptor& tid =
378                 tm_->getInstrInfo().get(mi->getOpcode());
379             DEBUG(std::cerr << "[" << getInstructionIndex(mi) << "]\t";
380                   mi->print(std::cerr, *tm_););
381
382             // handle implicit defs
383             for (const unsigned* id = tid.ImplicitDefs; *id; ++id)
384                 handleRegisterDef(mbb, mi, *id);
385
386             // handle explicit defs
387             for (int i = mi->getNumOperands() - 1; i >= 0; --i) {
388                 MachineOperand& mop = mi->getOperand(i);
389                 // handle register defs - build intervals
390                 if (mop.isRegister() && mop.isDef())
391                     handleRegisterDef(mbb, mi, mop.getReg());
392             }
393         }
394     }
395 }
396
397 unsigned LiveIntervals::rep(unsigned reg)
398 {
399     Reg2RegMap::iterator it = r2rMap_.find(reg);
400     if (it != r2rMap_.end())
401         return it->second = rep(it->second);
402     return reg;
403 }
404
405 void LiveIntervals::joinIntervals()
406 {
407     DEBUG(std::cerr << "** JOINING INTERVALS ***\n");
408
409     const TargetInstrInfo& tii = tm_->getInstrInfo();
410
411     for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
412          mbbi != mbbe; ++mbbi) {
413         MachineBasicBlock* mbb = mbbi;
414         DEBUG(std::cerr << mbb->getBasicBlock()->getName() << ":\n");
415
416         for (MachineBasicBlock::iterator mi = mbb->begin(), mie = mbb->end();
417              mi != mie; ++mi) {
418             const TargetInstrDescriptor& tid =
419                 tm_->getInstrInfo().get(mi->getOpcode());
420             DEBUG(std::cerr << "[" << getInstructionIndex(mi) << "]\t";
421                   mi->print(std::cerr, *tm_););
422
423             // we only join virtual registers with allocatable
424             // physical registers since we do not have liveness information
425             // on not allocatable physical registers
426             unsigned regA, regB;
427             if (tii.isMoveInstr(*mi, regA, regB) &&
428                 (MRegisterInfo::isVirtualRegister(regA) ||
429                  lv_->getAllocatablePhysicalRegisters()[regA]) &&
430                 (MRegisterInfo::isVirtualRegister(regB) ||
431                  lv_->getAllocatablePhysicalRegisters()[regB])) {
432
433                 // get representative registers
434                 regA = rep(regA);
435                 regB = rep(regB);
436
437                 // if they are already joined we continue
438                 if (regA == regB)
439                     continue;
440
441                 Reg2IntervalMap::iterator r2iA = r2iMap_.find(regA);
442                 assert(r2iA != r2iMap_.end());
443                 Reg2IntervalMap::iterator r2iB = r2iMap_.find(regB);
444                 assert(r2iB != r2iMap_.end());
445
446                 Intervals::iterator intA = r2iA->second;
447                 Intervals::iterator intB = r2iB->second;
448
449                 // both A and B are virtual registers
450                 if (MRegisterInfo::isVirtualRegister(intA->reg) &&
451                     MRegisterInfo::isVirtualRegister(intB->reg)) {
452
453                     const TargetRegisterClass *rcA, *rcB;
454                     rcA = mf_->getSSARegMap()->getRegClass(intA->reg);
455                     rcB = mf_->getSSARegMap()->getRegClass(intB->reg);
456                     assert(rcA == rcB && "registers must be of the same class");
457
458                     // if their intervals do not overlap we join them
459                     if (!intB->overlaps(*intA)) {
460                         intA->join(*intB);
461                         r2iB->second = r2iA->second;
462                         r2rMap_.insert(std::make_pair(intB->reg, intA->reg));
463                         intervals_.erase(intB);
464                         ++numJoined;
465                     }
466                 }
467                 else if (MRegisterInfo::isPhysicalRegister(intA->reg) ^
468                          MRegisterInfo::isPhysicalRegister(intB->reg)) {
469                     if (MRegisterInfo::isPhysicalRegister(intB->reg)) {
470                         std::swap(regA, regB);
471                         std::swap(intA, intB);
472                         std::swap(r2iA, r2iB);
473                     }
474
475                     assert(MRegisterInfo::isPhysicalRegister(intA->reg) &&
476                            MRegisterInfo::isVirtualRegister(intB->reg) &&
477                            "A must be physical and B must be virtual");
478
479                     if (!intA->overlaps(*intB) &&
480                          !overlapsAliases(*intA, *intB)) {
481                         intA->join(*intB);
482                         r2iB->second = r2iA->second;
483                         r2rMap_.insert(std::make_pair(intB->reg, intA->reg));
484                         intervals_.erase(intB);
485                         ++numJoined;
486                     }
487                 }
488             }
489         }
490     }
491 }
492
493 bool LiveIntervals::overlapsAliases(const Interval& lhs,
494                                     const Interval& rhs) const
495 {
496     assert(MRegisterInfo::isPhysicalRegister(lhs.reg) &&
497            "first interval must describe a physical register");
498
499     for (const unsigned* as = mri_->getAliasSet(lhs.reg); *as; ++as) {
500         Reg2IntervalMap::const_iterator r2i = r2iMap_.find(*as);
501         assert(r2i != r2iMap_.end() && "alias does not have interval?");
502         if (rhs.overlaps(*r2i->second))
503             return true;
504     }
505
506     return false;
507 }
508
509 LiveIntervals::Interval::Interval(unsigned r)
510     : reg(r),
511       weight((MRegisterInfo::isPhysicalRegister(r) ?
512               std::numeric_limits<float>::infinity() : 0.0F))
513 {
514
515 }
516
517 // An example for liveAt():
518 //
519 // this = [1,2), liveAt(0) will return false. The instruction defining
520 // this spans slots [0,1]. Since it is a definition we say that it is
521 // live in the second slot onwards. By ending the lifetime of this
522 // interval at 2 it means that it is not used at all. liveAt(1)
523 // returns true which means that this clobbers a register at
524 // instruction at 0.
525 //
526 // this = [1,4), liveAt(0) will return false and liveAt(2) will return
527 // true.  The variable is defined at instruction 0 and last used at 2.
528 bool LiveIntervals::Interval::liveAt(unsigned index) const
529 {
530     Range dummy(index, index+1);
531     Ranges::const_iterator r = std::upper_bound(ranges.begin(),
532                                                 ranges.end(),
533                                                 dummy);
534     if (r == ranges.begin())
535         return false;
536
537     --r;
538     return index >= r->first && index < r->second;
539 }
540
541 // An example for overlaps():
542 //
543 // 0: A = ...
544 // 2: B = ...
545 // 4: C = A + B ;; last use of A
546 //
547 // The live intervals should look like:
548 //
549 // A = [1, 5)
550 // B = [3, x)
551 // C = [5, y)
552 //
553 // A->overlaps(C) should return false since we want to be able to join
554 // A and C.
555 bool LiveIntervals::Interval::overlaps(const Interval& other) const
556 {
557     Ranges::const_iterator i = ranges.begin();
558     Ranges::const_iterator ie = ranges.end();
559     Ranges::const_iterator j = other.ranges.begin();
560     Ranges::const_iterator je = other.ranges.end();
561     if (i->first < j->first) {
562         i = std::upper_bound(i, ie, *j);
563         if (i != ranges.begin()) --i;
564     }
565     else if (j->first < i->first) {
566         j = std::upper_bound(j, je, *i);
567         if (j != other.ranges.begin()) --j;
568     }
569
570     while (i != ie && j != je) {
571         if (i->first == j->first) {
572             return true;
573         }
574         else {
575             if (i->first > j->first) {
576                 swap(i, j);
577                 swap(ie, je);
578             }
579             assert(i->first < j->first);
580
581             if (i->second > j->first) {
582                 return true;
583             }
584             else {
585                 ++i;
586             }
587         }
588     }
589
590     return false;
591 }
592
593 void LiveIntervals::Interval::addRange(unsigned start, unsigned end)
594 {
595     assert(start < end && "Invalid range to add!");
596     DEBUG(std::cerr << "\t\t\tadding range: [" << start <<','<< end << ") -> ");
597     //assert(start < end && "invalid range?");
598     Range range = std::make_pair(start, end);
599     Ranges::iterator it =
600         ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), range),
601                       range);
602
603     it = mergeRangesForward(it);
604     it = mergeRangesBackward(it);
605     DEBUG(std::cerr << "\t\t\t\tafter merging: " << *this << '\n');
606 }
607
608 void LiveIntervals::Interval::join(const LiveIntervals::Interval& other)
609 {
610     DEBUG(std::cerr << "\t\t\t\tjoining intervals: "
611           << other << " and " << *this << '\n');
612     Ranges::iterator cur = ranges.begin();
613
614     for (Ranges::const_iterator i = other.ranges.begin(),
615              e = other.ranges.end(); i != e; ++i) {
616         cur = ranges.insert(std::upper_bound(cur, ranges.end(), *i), *i);
617         cur = mergeRangesForward(cur);
618         cur = mergeRangesBackward(cur);
619     }
620     if (MRegisterInfo::isVirtualRegister(reg))
621         weight += other.weight;
622
623     DEBUG(std::cerr << "\t\t\t\tafter merging: " << *this << '\n');
624 }
625
626 LiveIntervals::Interval::Ranges::iterator
627 LiveIntervals::Interval::mergeRangesForward(Ranges::iterator it)
628 {
629     for (Ranges::iterator next = it + 1;
630          next != ranges.end() && it->second >= next->first; ) {
631         it->second = std::max(it->second, next->second);
632         next = ranges.erase(next);
633     }
634     return it;
635 }
636
637 LiveIntervals::Interval::Ranges::iterator
638 LiveIntervals::Interval::mergeRangesBackward(Ranges::iterator it)
639 {
640     while (it != ranges.begin()) {
641         Ranges::iterator prev = it - 1;
642         if (it->first > prev->second) break;
643
644         it->first = std::min(it->first, prev->first);
645         it->second = std::max(it->second, prev->second);
646         it = ranges.erase(prev);
647     }
648
649     return it;
650 }
651
652 std::ostream& llvm::operator<<(std::ostream& os,
653                                const LiveIntervals::Interval& li)
654 {
655     os << "%reg" << li.reg << ',' << li.weight << " = ";
656     for (LiveIntervals::Interval::Ranges::const_iterator
657              i = li.ranges.begin(), e = li.ranges.end(); i != e; ++i) {
658         os << "[" << i->first << "," << i->second << ")";
659     }
660     return os;
661 }