blob: 036ca132d22c0502082376cd650be6b1b964fb31 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
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
Chris Lattnera3b8b5c2004-07-23 17:56:30 +000020#ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
21#define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +000023#include "llvm/ADT/DenseMap.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerfb449b92004-07-23 17:49:16 +000025#include "LiveInterval.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000026
27namespace llvm {
28
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000029 class LiveVariables;
30 class MRegisterInfo;
31 class VirtRegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000033 class LiveIntervals : public MachineFunctionPass {
34 MachineFunction* mf_;
35 const TargetMachine* tm_;
36 const MRegisterInfo* mri_;
37 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000038
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000039 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
40 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000041
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 typedef std::vector<MachineInstr*> Index2MiMap;
43 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000044
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000045 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
46 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +000048 typedef DenseMap<unsigned> Reg2RegMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000049 Reg2RegMap r2rMap_;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000050
Alkis Evlogimenos53278012004-08-26 22:22:38 +000051 std::vector<bool> allocatableRegs_;
52
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000053 public:
54 struct InstrSlots
55 {
56 enum {
57 LOAD = 0,
58 USE = 1,
59 DEF = 2,
60 STORE = 3,
61 NUM = 4,
62 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000063 };
64
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 static unsigned getBaseIndex(unsigned index) {
66 return index - (index % InstrSlots::NUM);
67 }
68 static unsigned getBoundaryIndex(unsigned index) {
69 return getBaseIndex(index + InstrSlots::NUM - 1);
70 }
71 static unsigned getLoadIndex(unsigned index) {
72 return getBaseIndex(index) + InstrSlots::LOAD;
73 }
74 static unsigned getUseIndex(unsigned index) {
75 return getBaseIndex(index) + InstrSlots::USE;
76 }
77 static unsigned getDefIndex(unsigned index) {
78 return getBaseIndex(index) + InstrSlots::DEF;
79 }
80 static unsigned getStoreIndex(unsigned index) {
81 return getBaseIndex(index) + InstrSlots::STORE;
82 }
83
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +000085 typedef Reg2IntervalMap::const_iterator const_iterator;
86 const_iterator begin() const { return r2iMap_.begin(); }
87 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000088 iterator begin() { return r2iMap_.begin(); }
89 iterator end() { return r2iMap_.end(); }
90 unsigned getNumIntervals() const { return r2iMap_.size(); }
91
92 LiveInterval &getInterval(unsigned reg) {
93 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
94 assert(I != r2iMap_.end() && "Interval does not exist for register");
95 return I->second;
96 }
97
98 const LiveInterval &getInterval(unsigned reg) const {
99 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
100 assert(I != r2iMap_.end() && "Interval does not exist for register");
101 return I->second;
102 }
103
104 /// getInstructionIndex - returns the base index of instr
105 unsigned getInstructionIndex(MachineInstr* instr) const {
106 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
107 assert(it != mi2iMap_.end() && "Invalid instruction!");
108 return it->second;
109 }
110
111 /// getInstructionFromIndex - given an index in any slot of an
112 /// instruction return a pointer the instruction
113 MachineInstr* getInstructionFromIndex(unsigned index) const {
114 index /= InstrSlots::NUM; // convert index to vector index
115 assert(index < i2miMap_.size() &&
116 "index does not correspond to an instruction");
117 return i2miMap_[index];
118 }
119
120 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
121 VirtRegMap& vrm,
122 int slot);
123
124 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
125 virtual void releaseMemory();
126
127 /// runOnMachineFunction - pass entry point
128 virtual bool runOnMachineFunction(MachineFunction&);
129
Chris Lattner70ca3582004-09-30 15:59:17 +0000130 /// print - Implement the dump method.
131 virtual void print(std::ostream &O) const;
132
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000133 private:
134 /// computeIntervals - compute live intervals
135 void computeIntervals();
136
137 /// joinIntervals - join compatible live intervals
138 void joinIntervals();
139
140 /// joinIntervalsInMachineBB - Join intervals based on move
141 /// instructions in the specified basic block.
142 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
143
144 /// handleRegisterDef - update intervals for a register def
145 /// (calls handlePhysicalRegisterDef and
146 /// handleVirtualRegisterDef)
147 void handleRegisterDef(MachineBasicBlock* mbb,
148 MachineBasicBlock::iterator mi,
149 unsigned reg);
150
151 /// handleVirtualRegisterDef - update intervals for a virtual
152 /// register def
153 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
154 MachineBasicBlock::iterator mi,
155 LiveInterval& interval);
156
157 /// handlePhysicalRegisterDef - update intervals for a
158 /// physical register def
159 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
160 MachineBasicBlock::iterator mi,
161 LiveInterval& interval);
162
163 /// Return true if the two specified registers belong to different
164 /// register classes. The registers may be either phys or virt regs.
165 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
166
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000167 bool overlapsAliases(const LiveInterval *lhs,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000168 const LiveInterval *rhs) const;
169
170 static LiveInterval createInterval(unsigned Reg);
171
172 LiveInterval &getOrCreateInterval(unsigned reg) {
173 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
174 if (I == r2iMap_.end())
175 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
176 return I->second;
177 }
178
179 /// rep - returns the representative of this register
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000180 unsigned rep(unsigned Reg) {
181 unsigned Rep = r2rMap_[Reg];
182 if (Rep)
183 return r2rMap_[Reg] = rep(Rep);
184 return Reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000185 }
186
187 void printRegName(unsigned reg) const;
188 };
189
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000190} // End llvm namespace
191
192#endif