blob: 5aea679b5712a85782b29413c310ade961a3174c [file] [log] [blame]
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001//===-- llvm/CodeGen/LiveInterval.h - Live Interval Analysis ----*- C++ -*-===//
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. Given some
11// numbering of each the machine instructions (in this implemention
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000012// depth-first order) an interval [i, j) is said to be a live interval
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000013// for register v if there is no instruction with number j' > j such
14// that v is live at j' abd there is no instruction with number i' < i
15// such that v is live at i'. In this implementation intervals can
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000016// have holes, i.e. an interval might look like [1,20), [50,65),
17// [1000,1001)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000018//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_LIVEINTERVALS_H
22#define LLVM_CODEGEN_LIVEINTERVALS_H
23
24#include "llvm/CodeGen/MachineFunctionPass.h"
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000025#include <list>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000026
27namespace llvm {
28
29 class LiveVariables;
30 class MRegisterInfo;
31
32 class LiveIntervals : public MachineFunctionPass
33 {
34 public:
35 struct Interval {
36 typedef std::pair<unsigned, unsigned> Range;
37 typedef std::vector<Range> Ranges;
38 unsigned reg; // the register of this interval
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000039 float weight; // weight of this interval (number of uses
40 // * 10^loopDepth)
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000041 Ranges ranges; // the ranges in which this register is live
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000042
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000043 Interval(unsigned r);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000044
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000045 bool empty() const { return ranges.empty(); }
46
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047 unsigned start() const {
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000048 assert(!empty() && "empty interval for register");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000049 return ranges.front().first;
50 }
51
52 unsigned end() const {
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000053 assert(!empty() && "empty interval for register");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000054 return ranges.back().second;
55 }
56
Alkis Evlogimenos485ec3c2003-12-18 08:56:11 +000057 bool expiredAt(unsigned index) const {
Alkis Evlogimenos3b02cbe2004-01-16 20:17:05 +000058 return end() <= (index + 1);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000059 }
60
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000061 bool liveAt(unsigned index) const;
62
63 bool overlaps(const Interval& other) const;
64
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000065 void addRange(unsigned start, unsigned end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000066
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000067 void join(const Interval& other);
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000068
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000069 private:
70 Ranges::iterator mergeRangesForward(Ranges::iterator it);
71
72 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000073 };
74
75 struct StartPointComp {
76 bool operator()(const Interval& lhs, const Interval& rhs) {
77 return lhs.ranges.front().first < rhs.ranges.front().first;
78 }
79 };
80
81 struct EndPointComp {
82 bool operator()(const Interval& lhs, const Interval& rhs) {
83 return lhs.ranges.back().second < rhs.ranges.back().second;
84 }
85 };
86
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000087 typedef std::list<Interval> Intervals;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000088
89 private:
90 MachineFunction* mf_;
91 const TargetMachine* tm_;
92 const MRegisterInfo* mri_;
93 MachineBasicBlock* currentMbb_;
94 MachineBasicBlock::iterator currentInstr_;
95 LiveVariables* lv_;
96
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000097 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
98 MbbIndex2MbbMap mbbi2mbbMap_;
99
100 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
101 Mi2IndexMap mi2iMap_;
102
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000103 typedef std::vector<MachineInstr*> Index2MiMap;
104 Index2MiMap i2miMap_;
105
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000106 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000107 Reg2IntervalMap r2iMap_;
108
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000109 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000110 Reg2RegMap r2rMap_;
111
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000112 Intervals intervals_;
113
114 public:
115 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000116 virtual void releaseMemory();
117
118 /// runOnMachineFunction - pass entry point
119 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000120
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000121 Interval& getInterval(unsigned reg) {
122 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
123 return *r2iMap_.find(reg)->second;
124 }
125
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000126 unsigned getInstructionIndex(MachineInstr* instr) const;
127
128 MachineInstr* getInstructionFromIndex(unsigned index) const;
129
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000130 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000131
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000132 void updateSpilledInterval(Interval& i);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000133
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000134 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000135 /// computeIntervals - compute live intervals
136 void computeIntervals();
137
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000138 /// joinIntervals - join compatible live intervals
139 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000140
141 /// handleRegisterDef - update intervals for a register def
142 /// (calls handlePhysicalRegisterDef and
143 /// handleVirtualRegisterDef)
144 void handleRegisterDef(MachineBasicBlock* mbb,
145 MachineBasicBlock::iterator mi,
146 unsigned reg);
147
148 /// handleVirtualRegisterDef - update intervals for a virtual
149 /// register def
150 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
151 MachineBasicBlock::iterator mi,
152 unsigned reg);
153
154 /// handlePhysicalRegisterDef - update intervals for a
155 /// physical register def
156 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
157 MachineBasicBlock::iterator mi,
158 unsigned reg);
159
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000160 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
161
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000162 /// rep - returns the representative of this register
163 unsigned rep(unsigned reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000164
165 void printRegName(unsigned reg) const;
166 };
167
168 inline bool operator==(const LiveIntervals::Interval& lhs,
169 const LiveIntervals::Interval& rhs) {
170 return lhs.reg == rhs.reg;
171 }
172
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000173 std::ostream& operator<<(std::ostream& os,
174 const LiveIntervals::Interval& li);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000175
176} // End llvm namespace
177
178#endif