llvm.org GIT mirror llvm / fb449b9
Pull the LiveRange and LiveInterval classes out of LiveIntervals.h (which will soon be renamed) into their own file. The new file should not emit DEBUG output or have other side effects. The LiveInterval class also now doesn't know whether its working on registers or some other thing. In the future we will want to use the LiveInterval class and friends to do stack packing. In addition to a code simplification, this will allow us to do it more easily. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@15134 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
6 changed file(s) with 396 addition(s) and 314 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LiveRange and LiveInterval classes. Given some
10 // numbering of each the machine instructions an interval [i, j) is said to be a
11 // live interval for register v if there is no instruction with number j' > j
12 // such that v is live at j' abd there is no instruction with number i' < i such
13 // that v is live at i'. In this implementation intervals can have holes,
14 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
15 // individual range is represented as an instance of LiveRange, and the whole
16 // interval is represented as an instance of LiveInterval.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
21 #define LLVM_CODEGEN_LIVEINTERVAL_H
22
23 #include
24 #include
25 #include
26
27 namespace llvm {
28 /// LiveRange structure - This represents a simple register range in the
29 /// program, with an inclusive start point and an exclusive end point.
30 /// These ranges are rendered as [start,end).
31 struct LiveRange {
32 unsigned start; // Start point of the interval (inclusive)
33 unsigned end; // End point of the interval (exclusive)
34 LiveRange(unsigned S, unsigned E) : start(S), end(E) {
35 assert(S < E && "Cannot create empty or backwards range");
36 }
37
38 bool operator<(const LiveRange &LR) const {
39 return start < LR.start || (start == LR.start && end < LR.end);
40 }
41 bool operator==(const LiveRange &LR) const {
42 return start == LR.start && end == LR.end;
43 }
44 private:
45 LiveRange(); // DO NOT IMPLEMENT
46 };
47 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
48
49 /// LiveInterval - This class represents some number of live ranges for a
50 /// register or value. This class also contains a bit of register allocator
51 /// state.
52 struct LiveInterval {
53 typedef std::vector Ranges;
54 unsigned reg; // the register of this interval
55 float weight; // weight of this interval
56 Ranges ranges; // the ranges in which this register is live
57 bool isDefinedOnce; // True if this interval contains one value.
58
59 LiveInterval(unsigned Reg, float Weight)
60 : reg(Reg), weight(Weight), isDefinedOnce(false) {
61 }
62
63 bool containsOneValue() const { return isDefinedOnce; }
64
65 bool empty() const { return ranges.empty(); }
66
67 /// start - Return the lowest numbered slot covered by interval.
68 unsigned start() const {
69 assert(!empty() && "empty interval for register");
70 return ranges.front().start;
71 }
72
73 /// end - return the maximum point of the interval of the whole,
74 /// exclusive.
75 unsigned end() const {
76 assert(!empty() && "empty interval for register");
77 return ranges.back().end;
78 }
79
80 bool expiredAt(unsigned index) const {
81 return end() <= (index + 1);
82 }
83
84 bool liveAt(unsigned index) const;
85
86 bool overlaps(const LiveInterval& other) const;
87
88 void addRange(LiveRange R);
89
90 void join(const LiveInterval& other);
91
92 bool operator<(const LiveInterval& other) const {
93 return start() < other.start();
94 }
95
96 bool operator==(const LiveInterval& other) const {
97 return reg == other.reg;
98 }
99
100 private:
101 Ranges::iterator mergeRangesForward(Ranges::iterator it);
102 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
103 };
104
105 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
106 }
107
108 #endif
2020 #define LLVM_CODEGEN_LIVEINTERVALS_H
2121
2222 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "LiveInterval.h"
2324 #include
2425
2526 namespace llvm {
2728 class LiveVariables;
2829 class MRegisterInfo;
2930 class VirtRegMap;
30
31 /// LiveRange structure - This represents a simple register range in the
32 /// program, with an inclusive start point and an exclusive end point.
33 /// These ranges are rendered as [start,end).
34 struct LiveRange {
35 unsigned start; // Start point of the interval (inclusive)
36 unsigned end; // End point of the interval (exclusive)
37 LiveRange(unsigned S, unsigned E) : start(S), end(E) {
38 assert(S < E && "Cannot create empty or backwards range");
39 }
40
41 bool operator<(const LiveRange &LR) const {
42 return start < LR.start || (start == LR.start && end < LR.end);
43 }
44 bool operator==(const LiveRange &LR) const {
45 return start == LR.start && end == LR.end;
46 }
47 private:
48 LiveRange(); // DO NOT IMPLEMENT
49 };
50 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
51
52 struct LiveInterval {
53 typedef std::vector Ranges;
54 unsigned reg; // the register of this interval
55 float weight; // weight of this interval:
56 // (number of uses *10^loopDepth)
57 Ranges ranges; // the ranges in which this register is live
58 bool isDefinedOnce; // True if there is one def of this register
59
60 explicit LiveInterval(unsigned r);
61
62 bool empty() const { return ranges.empty(); }
63
64 bool spilled() const;
65
66 /// start - Return the lowest numbered slot covered by interval.
67 unsigned start() const {
68 assert(!empty() && "empty interval for register");
69 return ranges.front().start;
70 }
71
72 /// end - return the maximum point of the interval of the whole,
73 /// exclusive.
74 unsigned end() const {
75 assert(!empty() && "empty interval for register");
76 return ranges.back().end;
77 }
78
79 bool expiredAt(unsigned index) const {
80 return end() <= (index + 1);
81 }
82
83 bool liveAt(unsigned index) const;
84
85 bool overlaps(const LiveInterval& other) const;
86
87 void addRange(LiveRange R);
88
89 void join(const LiveInterval& other);
90
91 bool operator<(const LiveInterval& other) const {
92 return start() < other.start();
93 }
94
95 bool operator==(const LiveInterval& other) const {
96 return reg == other.reg;
97 }
98
99 private:
100 Ranges::iterator mergeRangesForward(Ranges::iterator it);
101 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
102 };
103
104 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
10531
10632 class LiveIntervals : public MachineFunctionPass
10733 {
0 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LiveRange and LiveInterval classes. Given some
10 // numbering of each the machine instructions an interval [i, j) is said to be a
11 // live interval for register v if there is no instruction with number j' > j
12 // such that v is live at j' abd there is no instruction with number i' < i such
13 // that v is live at i'. In this implementation intervals can have holes,
14 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
15 // individual range is represented as an instance of LiveRange, and the whole
16 // interval is represented as an instance of LiveInterval.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "LiveInterval.h"
21 #include "Support/STLExtras.h"
22 #include
23 using namespace llvm;
24
25 // An example for liveAt():
26 //
27 // this = [1,4), liveAt(0) will return false. The instruction defining
28 // this spans slots [0,3]. The interval belongs to an spilled
29 // definition of the variable it represents. This is because slot 1 is
30 // used (def slot) and spans up to slot 3 (store slot).
31 //
32 bool LiveInterval::liveAt(unsigned index) const {
33 LiveRange dummy(index, index+1);
34 Ranges::const_iterator r = std::upper_bound(ranges.begin(),
35 ranges.end(),
36 dummy);
37 if (r == ranges.begin())
38 return false;
39
40 --r;
41 return index >= r->start && index < r->end;
42 }
43
44 // An example for overlaps():
45 //
46 // 0: A = ...
47 // 4: B = ...
48 // 8: C = A + B ;; last use of A
49 //
50 // The live intervals should look like:
51 //
52 // A = [3, 11)
53 // B = [7, x)
54 // C = [11, y)
55 //
56 // A->overlaps(C) should return false since we want to be able to join
57 // A and C.
58 bool LiveInterval::overlaps(const LiveInterval& other) const {
59 Ranges::const_iterator i = ranges.begin();
60 Ranges::const_iterator ie = ranges.end();
61 Ranges::const_iterator j = other.ranges.begin();
62 Ranges::const_iterator je = other.ranges.end();
63 if (i->start < j->start) {
64 i = std::upper_bound(i, ie, *j);
65 if (i != ranges.begin()) --i;
66 }
67 else if (j->start < i->start) {
68 j = std::upper_bound(j, je, *i);
69 if (j != other.ranges.begin()) --j;
70 }
71
72 while (i != ie && j != je) {
73 if (i->start == j->start)
74 return true;
75
76 if (i->start > j->start) {
77 swap(i, j);
78 swap(ie, je);
79 }
80 assert(i->start < j->start);
81
82 if (i->end > j->start)
83 return true;
84 ++i;
85 }
86
87 return false;
88 }
89
90 void LiveInterval::addRange(LiveRange LR) {
91 Ranges::iterator it =
92 ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), LR), LR);
93
94 mergeRangesBackward(mergeRangesForward(it));
95 }
96
97 void LiveInterval::join(const LiveInterval& other) {
98 Ranges::iterator cur = ranges.begin();
99 isDefinedOnce &= other.isDefinedOnce;
100
101 for (Ranges::const_iterator i = other.ranges.begin(),
102 e = other.ranges.end(); i != e; ++i) {
103 cur = ranges.insert(std::upper_bound(cur, ranges.end(), *i), *i);
104 cur = mergeRangesBackward(mergeRangesForward(cur));
105 }
106 weight += other.weight;
107 }
108
109 LiveInterval::Ranges::iterator
110 LiveInterval::mergeRangesForward(Ranges::iterator it) {
111 Ranges::iterator n;
112 while ((n = next(it)) != ranges.end()) {
113 if (n->start > it->end)
114 break;
115 it->end = std::max(it->end, n->end);
116 n = ranges.erase(n);
117 }
118 return it;
119 }
120
121 LiveInterval::Ranges::iterator
122 LiveInterval::mergeRangesBackward(Ranges::iterator it) {
123 while (it != ranges.begin()) {
124 Ranges::iterator p = prior(it);
125 if (it->start > p->end)
126 break;
127
128 it->start = std::min(it->start, p->start);
129 it->end = std::max(it->end, p->end);
130 it = ranges.erase(p);
131 }
132
133 return it;
134 }
135
136 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
137 return os << "[" << LR.start << "," << LR.end << ")";
138 }
139
140 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
141 os << "%reg" << li.reg << ',' << li.weight;
142 if (li.empty())
143 return os << "EMPTY";
144
145 os << " = ";
146 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
147 e = li.ranges.end(); i != e; ++i)
148 os << *i;
149 return os;
150 }
0 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LiveRange and LiveInterval classes. Given some
10 // numbering of each the machine instructions an interval [i, j) is said to be a
11 // live interval for register v if there is no instruction with number j' > j
12 // such that v is live at j' abd there is no instruction with number i' < i such
13 // that v is live at i'. In this implementation intervals can have holes,
14 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
15 // individual range is represented as an instance of LiveRange, and the whole
16 // interval is represented as an instance of LiveInterval.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
21 #define LLVM_CODEGEN_LIVEINTERVAL_H
22
23 #include
24 #include
25 #include
26
27 namespace llvm {
28 /// LiveRange structure - This represents a simple register range in the
29 /// program, with an inclusive start point and an exclusive end point.
30 /// These ranges are rendered as [start,end).
31 struct LiveRange {
32 unsigned start; // Start point of the interval (inclusive)
33 unsigned end; // End point of the interval (exclusive)
34 LiveRange(unsigned S, unsigned E) : start(S), end(E) {
35 assert(S < E && "Cannot create empty or backwards range");
36 }
37
38 bool operator<(const LiveRange &LR) const {
39 return start < LR.start || (start == LR.start && end < LR.end);
40 }
41 bool operator==(const LiveRange &LR) const {
42 return start == LR.start && end == LR.end;
43 }
44 private:
45 LiveRange(); // DO NOT IMPLEMENT
46 };
47 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
48
49 /// LiveInterval - This class represents some number of live ranges for a
50 /// register or value. This class also contains a bit of register allocator
51 /// state.
52 struct LiveInterval {
53 typedef std::vector Ranges;
54 unsigned reg; // the register of this interval
55 float weight; // weight of this interval
56 Ranges ranges; // the ranges in which this register is live
57 bool isDefinedOnce; // True if this interval contains one value.
58
59 LiveInterval(unsigned Reg, float Weight)
60 : reg(Reg), weight(Weight), isDefinedOnce(false) {
61 }
62
63 bool containsOneValue() const { return isDefinedOnce; }
64
65 bool empty() const { return ranges.empty(); }
66
67 /// start - Return the lowest numbered slot covered by interval.
68 unsigned start() const {
69 assert(!empty() && "empty interval for register");
70 return ranges.front().start;
71 }
72
73 /// end - return the maximum point of the interval of the whole,
74 /// exclusive.
75 unsigned end() const {
76 assert(!empty() && "empty interval for register");
77 return ranges.back().end;
78 }
79
80 bool expiredAt(unsigned index) const {
81 return end() <= (index + 1);
82 }
83
84 bool liveAt(unsigned index) const;
85
86 bool overlaps(const LiveInterval& other) const;
87
88 void addRange(LiveRange R);
89
90 void join(const LiveInterval& other);
91
92 bool operator<(const LiveInterval& other) const {
93 return start() < other.start();
94 }
95
96 bool operator==(const LiveInterval& other) const {
97 return reg == other.reg;
98 }
99
100 private:
101 Ranges::iterator mergeRangesForward(Ranges::iterator it);
102 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
103 };
104
105 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
106 }
107
108 #endif
249249 // the spill weight is now infinity as it
250250 // cannot be spilled again
251251 nI.weight = HUGE_VAL;
252 DEBUG(std::cerr << " +" << LiveRange(start, end));
252253 nI.addRange(LiveRange(start, end));
253254 added.push_back(&nI);
254255 // update live variables
308309 assert(vi.AliveBlocks.empty() &&
309310 "Shouldn't be alive across any blocks!");
310311 interval.addRange(LiveRange(defIndex, killIdx));
311 DEBUG(std::cerr << "\n");
312 DEBUG(std::cerr << " +" << LiveRange(defIndex, killIdx) << "\n");
312313 return;
313314 }
314315 }
317318 // of the defining block, potentially live across some blocks, then is
318319 // live into some number of blocks, but gets killed. Start by adding a
319320 // range that goes from this definition to the end of the defining block.
320 interval.addRange(LiveRange(defIndex,
321 getInstructionIndex(&mbb->back()) +
322 InstrSlots::NUM));
321 LiveRange NewLR(defIndex, getInstructionIndex(&mbb->back()) +
322 InstrSlots::NUM);
323 DEBUG(std::cerr << " +" << NewLR);
324 interval.addRange(NewLR);
323325
324326 // Iterate over all of the blocks that the variable is completely
325327 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
328330 if (vi.AliveBlocks[i]) {
329331 MachineBasicBlock* mbb = mf_->getBlockNumbered(i);
330332 if (!mbb->empty()) {
331 interval.addRange(LiveRange(
332 getInstructionIndex(&mbb->front()),
333 getInstructionIndex(&mbb->back()) + InstrSlots::NUM));
333 LiveRange LR(getInstructionIndex(&mbb->front()),
334 getInstructionIndex(&mbb->back())+InstrSlots::NUM);
335 interval.addRange(LR);
336 DEBUG(std::cerr << " +" << LR);
334337 }
335338 }
336339 }
339342 // block to the 'use' slot of the killing instruction.
340343 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
341344 MachineInstr *Kill = vi.Kills[i];
342 interval.addRange(LiveRange(
343 getInstructionIndex(Kill->getParent()->begin()),
344 getUseIndex(getInstructionIndex(Kill))+1));
345 LiveRange LR(getInstructionIndex(Kill->getParent()->begin()),
346 getUseIndex(getInstructionIndex(Kill))+1);
347 interval.addRange(LR);
348 DEBUG(std::cerr << " +" << LR);
345349 }
346350
347351 } else {
358362 // the defined value will be live until the end of the basic block it
359363 // is defined in.
360364 unsigned defIndex = getDefIndex(getInstructionIndex(mi));
361 interval.addRange(LiveRange(defIndex,
362 getInstructionIndex(&mbb->back()) +InstrSlots::NUM));
365 LiveRange LR(defIndex,
366 getInstructionIndex(&mbb->back()) +InstrSlots::NUM);
367 interval.addRange(LR);
368 DEBUG(std::cerr << " +" << LR);
363369 }
364370 interval.isDefinedOnce = false;
365371 }
412418 exit:
413419 assert(start < end && "did not find end of interval?");
414420 interval.addRange(LiveRange(start, end));
415 DEBUG(std::cerr << '\n');
421 DEBUG(std::cerr << " +" << LiveRange(start, end) << '\n');
416422 }
417423
418424 void LiveIntervals::handleRegisterDef(MachineBasicBlock* mbb,
546552 continue;
547553 }
548554
549 // if their intervals do not overlap we join them
550 if ((intA->isDefinedOnce && intB->isDefinedOnce) ||
555 // if their intervals do not overlap we join them.
556 if ((intA->containsOneValue() && intB->containsOneValue()) ||
551557 !intB->overlaps(*intA)) {
552558 intA->join(*intB);
559 ++numJoins;
553560 DEBUG(std::cerr << "Joined. Result = " << *intA << "\n");
554561 r2iB->second = r2iA->second;
555562 r2rMap_.insert(std::make_pair(intB->reg, intA->reg));
581588 if (!intA->overlaps(*intB) &&
582589 !overlapsAliases(*intA, *intB)) {
583590 intA->join(*intB);
591 ++numJoins;
584592 DEBUG(std::cerr << "Joined. Result = " << *intA << "\n");
585593 r2iB->second = r2iA->second;
586594 r2rMap_.insert(std::make_pair(intB->reg, intA->reg));
655663 {
656664 Reg2IntervalMap::iterator r2iit = r2iMap_.lower_bound(reg);
657665 if (r2iit == r2iMap_.end() || r2iit->first != reg) {
658 intervals_.push_back(LiveInterval(reg));
666 float Weight = MRegisterInfo::isPhysicalRegister(reg) ? HUGE_VAL :0.0F;
667 intervals_.push_back(LiveInterval(reg, Weight));
659668 r2iit = r2iMap_.insert(r2iit, std::make_pair(reg, --intervals_.end()));
660669 }
661670
662671 return *r2iit->second;
663672 }
664673
665 LiveInterval::LiveInterval(unsigned r)
666 : reg(r),
667 weight((MRegisterInfo::isPhysicalRegister(r) ? HUGE_VAL : 0.0F)),
668 isDefinedOnce(false) {
669 }
670
671 bool LiveInterval::spilled() const
672 {
673 return (weight == HUGE_VAL &&
674 MRegisterInfo::isVirtualRegister(reg));
675 }
676
677 // An example for liveAt():
678 //
679 // this = [1,4), liveAt(0) will return false. The instruction defining
680 // this spans slots [0,3]. The interval belongs to an spilled
681 // definition of the variable it represents. This is because slot 1 is
682 // used (def slot) and spans up to slot 3 (store slot).
683 //
684 bool LiveInterval::liveAt(unsigned index) const
685 {
686 LiveRange dummy(index, index+1);
687 Ranges::const_iterator r = std::upper_bound(ranges.begin(),
688 ranges.end(),
689 dummy);
690 if (r == ranges.begin())
691 return false;
692
693 --r;
694 return index >= r->start && index < r->end;
695 }
696
697 // An example for overlaps():
698 //
699 // 0: A = ...
700 // 4: B = ...
701 // 8: C = A + B ;; last use of A
702 //
703 // The live intervals should look like:
704 //
705 // A = [3, 11)
706 // B = [7, x)
707 // C = [11, y)
708 //
709 // A->overlaps(C) should return false since we want to be able to join
710 // A and C.
711 bool LiveInterval::overlaps(const LiveInterval& other) const
712 {
713 Ranges::const_iterator i = ranges.begin();
714 Ranges::const_iterator ie = ranges.end();
715 Ranges::const_iterator j = other.ranges.begin();
716 Ranges::const_iterator je = other.ranges.end();
717 if (i->start < j->start) {
718 i = std::upper_bound(i, ie, *j);
719 if (i != ranges.begin()) --i;
720 }
721 else if (j->start < i->start) {
722 j = std::upper_bound(j, je, *i);
723 if (j != other.ranges.begin()) --j;
724 }
725
726 while (i != ie && j != je) {
727 if (i->start == j->start)
728 return true;
729
730 if (i->start > j->start) {
731 swap(i, j);
732 swap(ie, je);
733 }
734 assert(i->start < j->start);
735
736 if (i->end > j->start)
737 return true;
738 ++i;
739 }
740
741 return false;
742 }
743
744 void LiveInterval::addRange(LiveRange LR) {
745 DEBUG(std::cerr << " +" << LR);
746 Ranges::iterator it =
747 ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), LR), LR);
748
749 mergeRangesBackward(mergeRangesForward(it));
750 }
751
752 void LiveInterval::join(const LiveInterval& other)
753 {
754 Ranges::iterator cur = ranges.begin();
755 isDefinedOnce &= other.isDefinedOnce;
756
757 for (Ranges::const_iterator i = other.ranges.begin(),
758 e = other.ranges.end(); i != e; ++i) {
759 cur = ranges.insert(std::upper_bound(cur, ranges.end(), *i), *i);
760 cur = mergeRangesForward(cur);
761 cur = mergeRangesBackward(cur);
762 }
763 weight += other.weight;
764 ++numJoins;
765 }
766
767 LiveInterval::Ranges::iterator LiveInterval::
768 mergeRangesForward(Ranges::iterator it)
769 {
770 Ranges::iterator n;
771 while ((n = next(it)) != ranges.end()) {
772 if (n->start > it->end)
773 break;
774 it->end = std::max(it->end, n->end);
775 n = ranges.erase(n);
776 }
777 return it;
778 }
779
780 LiveInterval::Ranges::iterator LiveInterval::
781 mergeRangesBackward(Ranges::iterator it)
782 {
783 while (it != ranges.begin()) {
784 Ranges::iterator p = prior(it);
785 if (it->start > p->end)
786 break;
787
788 it->start = std::min(it->start, p->start);
789 it->end = std::max(it->end, p->end);
790 it = ranges.erase(p);
791 }
792
793 return it;
794 }
795
796 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
797 return os << "[" << LR.start << "," << LR.end << ")";
798 }
799
800
801 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li)
802 {
803 os << "%reg" << li.reg << ',' << li.weight;
804 if (li.empty())
805 return os << "EMPTY";
806
807 os << " = ";
808 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
809 e = li.ranges.end(); i != e; ++i)
810 os << *i;
811 return os;
812 }
2020 #define LLVM_CODEGEN_LIVEINTERVALS_H
2121
2222 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "LiveInterval.h"
2324 #include
2425
2526 namespace llvm {
2728 class LiveVariables;
2829 class MRegisterInfo;
2930 class VirtRegMap;
30
31 /// LiveRange structure - This represents a simple register range in the
32 /// program, with an inclusive start point and an exclusive end point.
33 /// These ranges are rendered as [start,end).
34 struct LiveRange {
35 unsigned start; // Start point of the interval (inclusive)
36 unsigned end; // End point of the interval (exclusive)
37 LiveRange(unsigned S, unsigned E) : start(S), end(E) {
38 assert(S < E && "Cannot create empty or backwards range");
39 }
40
41 bool operator<(const LiveRange &LR) const {
42 return start < LR.start || (start == LR.start && end < LR.end);
43 }
44 bool operator==(const LiveRange &LR) const {
45 return start == LR.start && end == LR.end;
46 }
47 private:
48 LiveRange(); // DO NOT IMPLEMENT
49 };
50 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
51
52 struct LiveInterval {
53 typedef std::vector Ranges;
54 unsigned reg; // the register of this interval
55 float weight; // weight of this interval:
56 // (number of uses *10^loopDepth)
57 Ranges ranges; // the ranges in which this register is live
58 bool isDefinedOnce; // True if there is one def of this register
59
60 explicit LiveInterval(unsigned r);
61
62 bool empty() const { return ranges.empty(); }
63
64 bool spilled() const;
65
66 /// start - Return the lowest numbered slot covered by interval.
67 unsigned start() const {
68 assert(!empty() && "empty interval for register");
69 return ranges.front().start;
70 }
71
72 /// end - return the maximum point of the interval of the whole,
73 /// exclusive.
74 unsigned end() const {
75 assert(!empty() && "empty interval for register");
76 return ranges.back().end;
77 }
78
79 bool expiredAt(unsigned index) const {
80 return end() <= (index + 1);
81 }
82
83 bool liveAt(unsigned index) const;
84
85 bool overlaps(const LiveInterval& other) const;
86
87 void addRange(LiveRange R);
88
89 void join(const LiveInterval& other);
90
91 bool operator<(const LiveInterval& other) const {
92 return start() < other.start();
93 }
94
95 bool operator==(const LiveInterval& other) const {
96 return reg == other.reg;
97 }
98
99 private:
100 Ranges::iterator mergeRangesForward(Ranges::iterator it);
101 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
102 };
103
104 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
10531
10632 class LiveIntervals : public MachineFunctionPass
10733 {