blob: a70deffdc169a6c1f27151926c0ae6c66c58b414 [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//
Chris Lattner6b929062004-07-19 02:13:59 +000010// This file implements the LiveInterval analysis pass. Given some numbering of
11// each the machine instructions (in this implemention depth-first order) an
12// interval [i, j) is said to be a live interval for register v if there is no
13// instruction with number j' > j such that v is live at j' abd there is no
14// instruction with number i' < i such that v is live at i'. In this
15// implementation intervals can have holes, i.e. an interval might look like
16// [1,20), [50,65), [1000,1001).
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000017//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_CODEGEN_LIVEINTERVALS_H
21#define LLVM_CODEGEN_LIVEINTERVALS_H
22
23#include "llvm/CodeGen/MachineFunctionPass.h"
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000024#include <list>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025
26namespace llvm {
27
28 class LiveVariables;
29 class MRegisterInfo;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000030 class VirtRegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000031
Chris Lattnerec2bc642004-07-23 08:24:23 +000032 /// LiveRange structure - This represents a simple register range in the
33 /// program, with an inclusive start point and an exclusive end point.
34 /// These ranges are rendered as [start,end).
35 struct LiveRange {
36 unsigned start; // Start point of the interval (inclusive)
37 unsigned end; // End point of the interval (exclusive)
38 LiveRange(unsigned S, unsigned E) : start(S), end(E) {
39 assert(S < E && "Cannot create empty or backwards range");
40 }
41
42 bool operator<(const LiveRange &LR) const {
43 return start < LR.start || (start == LR.start && end < LR.end);
44 }
45 bool operator==(const LiveRange &LR) const {
46 return start == LR.start && end == LR.end;
47 }
48 private:
49 LiveRange(); // DO NOT IMPLEMENT
50 };
51 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
52
Chris Lattner418da552004-06-21 13:10:56 +000053 struct LiveInterval {
Chris Lattnerec2bc642004-07-23 08:24:23 +000054 typedef std::vector<LiveRange> Ranges;
Alkis Evlogimenos69240632004-05-30 07:46:27 +000055 unsigned reg; // the register of this interval
56 float weight; // weight of this interval:
57 // (number of uses *10^loopDepth)
58 Ranges ranges; // the ranges in which this register is live
Chris Lattnerfe1630b2004-07-23 05:26:05 +000059 bool isDefinedOnce; // True if there is one def of this register
Alkis Evlogimenos69240632004-05-30 07:46:27 +000060
Chris Lattner418da552004-06-21 13:10:56 +000061 explicit LiveInterval(unsigned r);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000062
63 bool empty() const { return ranges.empty(); }
64
65 bool spilled() const;
66
Chris Lattnerec2bc642004-07-23 08:24:23 +000067 /// start - Return the lowest numbered slot covered by interval.
Alkis Evlogimenos69240632004-05-30 07:46:27 +000068 unsigned start() const {
69 assert(!empty() && "empty interval for register");
Chris Lattnerec2bc642004-07-23 08:24:23 +000070 return ranges.front().start;
Alkis Evlogimenos69240632004-05-30 07:46:27 +000071 }
72
Chris Lattnerec2bc642004-07-23 08:24:23 +000073 /// end - return the maximum point of the interval of the whole,
74 /// exclusive.
Alkis Evlogimenos69240632004-05-30 07:46:27 +000075 unsigned end() const {
76 assert(!empty() && "empty interval for register");
Chris Lattnerec2bc642004-07-23 08:24:23 +000077 return ranges.back().end;
Alkis Evlogimenos69240632004-05-30 07:46:27 +000078 }
79
80 bool expiredAt(unsigned index) const {
81 return end() <= (index + 1);
82 }
83
84 bool liveAt(unsigned index) const;
85
Chris Lattner418da552004-06-21 13:10:56 +000086 bool overlaps(const LiveInterval& other) const;
Alkis Evlogimenos69240632004-05-30 07:46:27 +000087
Chris Lattnerec2bc642004-07-23 08:24:23 +000088 void addRange(LiveRange R);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000089
Chris Lattner418da552004-06-21 13:10:56 +000090 void join(const LiveInterval& other);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000091
Chris Lattner418da552004-06-21 13:10:56 +000092 bool operator<(const LiveInterval& other) const {
Alkis Evlogimenos69240632004-05-30 07:46:27 +000093 return start() < other.start();
94 }
95
Chris Lattner418da552004-06-21 13:10:56 +000096 bool operator==(const LiveInterval& other) const {
Alkis Evlogimenos69240632004-05-30 07:46:27 +000097 return reg == other.reg;
98 }
99
100 private:
101 Ranges::iterator mergeRangesForward(Ranges::iterator it);
102 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
103 };
104
Chris Lattner418da552004-06-21 13:10:56 +0000105 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
Alkis Evlogimenos69240632004-05-30 07:46:27 +0000106
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000107 class LiveIntervals : public MachineFunctionPass
108 {
109 public:
Chris Lattner418da552004-06-21 13:10:56 +0000110 typedef std::list<LiveInterval> Intervals;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000111
112 private:
113 MachineFunction* mf_;
114 const TargetMachine* tm_;
115 const MRegisterInfo* mri_;
116 MachineBasicBlock* currentMbb_;
117 MachineBasicBlock::iterator currentInstr_;
118 LiveVariables* lv_;
119
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000120 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
121 Mi2IndexMap mi2iMap_;
122
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000123 typedef std::vector<MachineInstr*> Index2MiMap;
124 Index2MiMap i2miMap_;
125
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000126 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000127 Reg2IntervalMap r2iMap_;
128
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000129 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000130 Reg2RegMap r2rMap_;
131
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000132 Intervals intervals_;
133
134 public:
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000135 struct InstrSlots
136 {
137 enum {
138 LOAD = 0,
139 USE = 1,
140 DEF = 2,
141 STORE = 3,
142 NUM = 4,
143 };
144 };
145
146 static unsigned getBaseIndex(unsigned index) {
Alkis Evlogimenos7200c6b2004-02-22 04:05:13 +0000147 return index - (index % InstrSlots::NUM);
148 }
149 static unsigned getBoundaryIndex(unsigned index) {
150 return getBaseIndex(index + InstrSlots::NUM - 1);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000151 }
152 static unsigned getLoadIndex(unsigned index) {
153 return getBaseIndex(index) + InstrSlots::LOAD;
154 }
155 static unsigned getUseIndex(unsigned index) {
156 return getBaseIndex(index) + InstrSlots::USE;
157 }
158 static unsigned getDefIndex(unsigned index) {
159 return getBaseIndex(index) + InstrSlots::DEF;
160 }
161 static unsigned getStoreIndex(unsigned index) {
162 return getBaseIndex(index) + InstrSlots::STORE;
163 }
164
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000165 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000166 virtual void releaseMemory();
167
168 /// runOnMachineFunction - pass entry point
169 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000170
Chris Lattner418da552004-06-21 13:10:56 +0000171 LiveInterval& getInterval(unsigned reg) {
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000172 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
173 return *r2iMap_.find(reg)->second;
174 }
175
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000176 /// getInstructionIndex - returns the base index of instr
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000177 unsigned getInstructionIndex(MachineInstr* instr) const;
178
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000179 /// getInstructionFromIndex - given an index in any slot of an
180 /// instruction return a pointer the instruction
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000181 MachineInstr* getInstructionFromIndex(unsigned index) const;
182
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000183 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000184
Chris Lattner418da552004-06-21 13:10:56 +0000185 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
186 VirtRegMap& vrm,
187 int slot);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000188
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000189 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000190 /// computeIntervals - compute live intervals
191 void computeIntervals();
192
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000193 /// joinIntervals - join compatible live intervals
194 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000195
Chris Lattner1c5c0442004-07-19 14:08:10 +0000196 /// joinIntervalsInMachineBB - Join intervals based on move
197 /// instructions in the specified basic block.
198 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
199
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000200 /// handleRegisterDef - update intervals for a register def
201 /// (calls handlePhysicalRegisterDef and
202 /// handleVirtualRegisterDef)
203 void handleRegisterDef(MachineBasicBlock* mbb,
204 MachineBasicBlock::iterator mi,
205 unsigned reg);
206
207 /// handleVirtualRegisterDef - update intervals for a virtual
208 /// register def
209 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
210 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000211 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000212
213 /// handlePhysicalRegisterDef - update intervals for a
214 /// physical register def
215 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
216 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000217 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000218
Chris Lattner418da552004-06-21 13:10:56 +0000219 bool overlapsAliases(const LiveInterval& lhs,
220 const LiveInterval& rhs) const;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000221
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000222
Chris Lattner418da552004-06-21 13:10:56 +0000223 LiveInterval& getOrCreateInterval(unsigned reg);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000224
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000225 /// rep - returns the representative of this register
226 unsigned rep(unsigned reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000227
228 void printRegName(unsigned reg) const;
229 };
230
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000231} // End llvm namespace
232
233#endif