blob: 5b78342e281d4747e8407546da76edfcdcb4b6f7 [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;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000031 class VirtRegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032
33 class LiveIntervals : public MachineFunctionPass
34 {
35 public:
36 struct Interval {
37 typedef std::pair<unsigned, unsigned> Range;
38 typedef std::vector<Range> Ranges;
39 unsigned reg; // the register of this interval
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000040 float weight; // weight of this interval (number of uses
41 // * 10^loopDepth)
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000042 Ranges ranges; // the ranges in which this register is live
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000043
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000044 Interval(unsigned r);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000045
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000046 bool empty() const { return ranges.empty(); }
47
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000048 bool spilled() const;
49
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000050 unsigned start() const {
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000051 assert(!empty() && "empty interval for register");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000052 return ranges.front().first;
53 }
54
55 unsigned end() const {
Alkis Evlogimenos7093d372004-02-17 05:14:37 +000056 assert(!empty() && "empty interval for register");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000057 return ranges.back().second;
58 }
59
Alkis Evlogimenos485ec3c2003-12-18 08:56:11 +000060 bool expiredAt(unsigned index) const {
Alkis Evlogimenos3b02cbe2004-01-16 20:17:05 +000061 return end() <= (index + 1);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062 }
63
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000064 bool liveAt(unsigned index) const;
65
66 bool overlaps(const Interval& other) const;
67
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000068 void addRange(unsigned start, unsigned end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000070 void join(const Interval& other);
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000071
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000072 private:
73 Ranges::iterator mergeRangesForward(Ranges::iterator it);
74
75 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000076 };
77
78 struct StartPointComp {
79 bool operator()(const Interval& lhs, const Interval& rhs) {
80 return lhs.ranges.front().first < rhs.ranges.front().first;
81 }
82 };
83
84 struct EndPointComp {
85 bool operator()(const Interval& lhs, const Interval& rhs) {
86 return lhs.ranges.back().second < rhs.ranges.back().second;
87 }
88 };
89
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000090 typedef std::list<Interval> Intervals;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091
92 private:
93 MachineFunction* mf_;
94 const TargetMachine* tm_;
95 const MRegisterInfo* mri_;
96 MachineBasicBlock* currentMbb_;
97 MachineBasicBlock::iterator currentInstr_;
98 LiveVariables* lv_;
99
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000100 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
101 MbbIndex2MbbMap mbbi2mbbMap_;
102
103 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
104 Mi2IndexMap mi2iMap_;
105
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000106 typedef std::vector<MachineInstr*> Index2MiMap;
107 Index2MiMap i2miMap_;
108
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000109 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000110 Reg2IntervalMap r2iMap_;
111
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000112 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000113 Reg2RegMap r2rMap_;
114
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000115 Intervals intervals_;
116
117 public:
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000118 struct InstrSlots
119 {
120 enum {
121 LOAD = 0,
122 USE = 1,
123 DEF = 2,
124 STORE = 3,
125 NUM = 4,
126 };
127 };
128
129 static unsigned getBaseIndex(unsigned index) {
Alkis Evlogimenos7200c6b2004-02-22 04:05:13 +0000130 return index - (index % InstrSlots::NUM);
131 }
132 static unsigned getBoundaryIndex(unsigned index) {
133 return getBaseIndex(index + InstrSlots::NUM - 1);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000134 }
135 static unsigned getLoadIndex(unsigned index) {
136 return getBaseIndex(index) + InstrSlots::LOAD;
137 }
138 static unsigned getUseIndex(unsigned index) {
139 return getBaseIndex(index) + InstrSlots::USE;
140 }
141 static unsigned getDefIndex(unsigned index) {
142 return getBaseIndex(index) + InstrSlots::DEF;
143 }
144 static unsigned getStoreIndex(unsigned index) {
145 return getBaseIndex(index) + InstrSlots::STORE;
146 }
147
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000148 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000149 virtual void releaseMemory();
150
151 /// runOnMachineFunction - pass entry point
152 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000153
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000154 Interval& getInterval(unsigned reg) {
155 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
156 return *r2iMap_.find(reg)->second;
157 }
158
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000159 /// getInstructionIndex - returns the base index of instr
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000160 unsigned getInstructionIndex(MachineInstr* instr) const;
161
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000162 /// getInstructionFromIndex - given an index in any slot of an
163 /// instruction return a pointer the instruction
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000164 MachineInstr* getInstructionFromIndex(unsigned index) const;
165
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000166 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000167
Alkis Evlogimenos5f375022004-03-01 20:05:10 +0000168 void updateSpilledInterval(Interval& i, VirtRegMap& vrm, int slot);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000169
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000170 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000171 /// computeIntervals - compute live intervals
172 void computeIntervals();
173
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000174 /// joinIntervals - join compatible live intervals
175 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000176
177 /// handleRegisterDef - update intervals for a register def
178 /// (calls handlePhysicalRegisterDef and
179 /// handleVirtualRegisterDef)
180 void handleRegisterDef(MachineBasicBlock* mbb,
181 MachineBasicBlock::iterator mi,
182 unsigned reg);
183
184 /// handleVirtualRegisterDef - update intervals for a virtual
185 /// register def
186 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
187 MachineBasicBlock::iterator mi,
188 unsigned reg);
189
190 /// handlePhysicalRegisterDef - update intervals for a
191 /// physical register def
192 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
193 MachineBasicBlock::iterator mi,
194 unsigned reg);
195
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000196 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
197
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000198 /// rep - returns the representative of this register
199 unsigned rep(unsigned reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000200
201 void printRegName(unsigned reg) const;
202 };
203
204 inline bool operator==(const LiveIntervals::Interval& lhs,
205 const LiveIntervals::Interval& rhs) {
206 return lhs.reg == rhs.reg;
207 }
208
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000209 std::ostream& operator<<(std::ostream& os,
210 const LiveIntervals::Interval& li);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000211
212} // End llvm namespace
213
214#endif