blob: a78aed186f48cf5a5b92ba7b34c885f12cc1ea78 [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 Lattner418da552004-06-21 13:10:56 +000032 struct LiveInterval {
Alkis Evlogimenos69240632004-05-30 07:46:27 +000033 typedef std::pair<unsigned, unsigned> Range;
34 typedef std::vector<Range> Ranges;
35 unsigned reg; // the register of this interval
36 float weight; // weight of this interval:
37 // (number of uses *10^loopDepth)
38 Ranges ranges; // the ranges in which this register is live
Chris Lattnerfe1630b2004-07-23 05:26:05 +000039 bool isDefinedOnce; // True if there is one def of this register
Alkis Evlogimenos69240632004-05-30 07:46:27 +000040
Chris Lattner418da552004-06-21 13:10:56 +000041 explicit LiveInterval(unsigned r);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000042
43 bool empty() const { return ranges.empty(); }
44
45 bool spilled() const;
46
47 unsigned start() const {
48 assert(!empty() && "empty interval for register");
49 return ranges.front().first;
50 }
51
52 unsigned end() const {
53 assert(!empty() && "empty interval for register");
54 return ranges.back().second;
55 }
56
57 bool expiredAt(unsigned index) const {
58 return end() <= (index + 1);
59 }
60
61 bool liveAt(unsigned index) const;
62
Chris Lattner418da552004-06-21 13:10:56 +000063 bool overlaps(const LiveInterval& other) const;
Alkis Evlogimenos69240632004-05-30 07:46:27 +000064
65 void addRange(unsigned start, unsigned end);
66
Chris Lattner418da552004-06-21 13:10:56 +000067 void join(const LiveInterval& other);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000068
Chris Lattner418da552004-06-21 13:10:56 +000069 bool operator<(const LiveInterval& other) const {
Alkis Evlogimenos69240632004-05-30 07:46:27 +000070 return start() < other.start();
71 }
72
Chris Lattner418da552004-06-21 13:10:56 +000073 bool operator==(const LiveInterval& other) const {
Alkis Evlogimenos69240632004-05-30 07:46:27 +000074 return reg == other.reg;
75 }
76
77 private:
78 Ranges::iterator mergeRangesForward(Ranges::iterator it);
79 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
80 };
81
Chris Lattner418da552004-06-21 13:10:56 +000082 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
Alkis Evlogimenos69240632004-05-30 07:46:27 +000083
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000084 class LiveIntervals : public MachineFunctionPass
85 {
86 public:
Chris Lattner418da552004-06-21 13:10:56 +000087 typedef std::list<LiveInterval> 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<MachineInstr*, unsigned> Mi2IndexMap;
98 Mi2IndexMap mi2iMap_;
99
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000100 typedef std::vector<MachineInstr*> Index2MiMap;
101 Index2MiMap i2miMap_;
102
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000103 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000104 Reg2IntervalMap r2iMap_;
105
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000106 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000107 Reg2RegMap r2rMap_;
108
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000109 Intervals intervals_;
110
111 public:
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000112 struct InstrSlots
113 {
114 enum {
115 LOAD = 0,
116 USE = 1,
117 DEF = 2,
118 STORE = 3,
119 NUM = 4,
120 };
121 };
122
123 static unsigned getBaseIndex(unsigned index) {
Alkis Evlogimenos7200c6b2004-02-22 04:05:13 +0000124 return index - (index % InstrSlots::NUM);
125 }
126 static unsigned getBoundaryIndex(unsigned index) {
127 return getBaseIndex(index + InstrSlots::NUM - 1);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000128 }
129 static unsigned getLoadIndex(unsigned index) {
130 return getBaseIndex(index) + InstrSlots::LOAD;
131 }
132 static unsigned getUseIndex(unsigned index) {
133 return getBaseIndex(index) + InstrSlots::USE;
134 }
135 static unsigned getDefIndex(unsigned index) {
136 return getBaseIndex(index) + InstrSlots::DEF;
137 }
138 static unsigned getStoreIndex(unsigned index) {
139 return getBaseIndex(index) + InstrSlots::STORE;
140 }
141
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000142 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000143 virtual void releaseMemory();
144
145 /// runOnMachineFunction - pass entry point
146 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000147
Chris Lattner418da552004-06-21 13:10:56 +0000148 LiveInterval& getInterval(unsigned reg) {
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000149 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
150 return *r2iMap_.find(reg)->second;
151 }
152
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000153 /// getInstructionIndex - returns the base index of instr
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000154 unsigned getInstructionIndex(MachineInstr* instr) const;
155
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000156 /// getInstructionFromIndex - given an index in any slot of an
157 /// instruction return a pointer the instruction
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000158 MachineInstr* getInstructionFromIndex(unsigned index) const;
159
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000160 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000161
Chris Lattner418da552004-06-21 13:10:56 +0000162 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
163 VirtRegMap& vrm,
164 int slot);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000165
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000166 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000167 /// computeIntervals - compute live intervals
168 void computeIntervals();
169
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000170 /// joinIntervals - join compatible live intervals
171 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000172
Chris Lattner1c5c0442004-07-19 14:08:10 +0000173 /// joinIntervalsInMachineBB - Join intervals based on move
174 /// instructions in the specified basic block.
175 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
176
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000177 /// 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,
Chris Lattner418da552004-06-21 13:10:56 +0000188 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000189
190 /// handlePhysicalRegisterDef - update intervals for a
191 /// physical register def
192 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
193 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000194 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000195
Chris Lattner418da552004-06-21 13:10:56 +0000196 bool overlapsAliases(const LiveInterval& lhs,
197 const LiveInterval& rhs) const;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000198
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000199
Chris Lattner418da552004-06-21 13:10:56 +0000200 LiveInterval& getOrCreateInterval(unsigned reg);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000201
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000202 /// rep - returns the representative of this register
203 unsigned rep(unsigned reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000204
205 void printRegName(unsigned reg) const;
206 };
207
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000208} // End llvm namespace
209
210#endif