blob: b90be6270418d5a6ba42c54f7b73966b6a35c1a9 [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
84 // FIXME: this should really be a const_iterator
85 typedef Reg2IntervalMap::iterator iterator;
86 iterator begin() { return r2iMap_.begin(); }
87 iterator end() { return r2iMap_.end(); }
88 unsigned getNumIntervals() const { return r2iMap_.size(); }
89
90 LiveInterval &getInterval(unsigned reg) {
91 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
92 assert(I != r2iMap_.end() && "Interval does not exist for register");
93 return I->second;
94 }
95
96 const LiveInterval &getInterval(unsigned reg) const {
97 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
98 assert(I != r2iMap_.end() && "Interval does not exist for register");
99 return I->second;
100 }
101
102 /// getInstructionIndex - returns the base index of instr
103 unsigned getInstructionIndex(MachineInstr* instr) const {
104 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
105 assert(it != mi2iMap_.end() && "Invalid instruction!");
106 return it->second;
107 }
108
109 /// getInstructionFromIndex - given an index in any slot of an
110 /// instruction return a pointer the instruction
111 MachineInstr* getInstructionFromIndex(unsigned index) const {
112 index /= InstrSlots::NUM; // convert index to vector index
113 assert(index < i2miMap_.size() &&
114 "index does not correspond to an instruction");
115 return i2miMap_[index];
116 }
117
118 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
119 VirtRegMap& vrm,
120 int slot);
121
122 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
123 virtual void releaseMemory();
124
125 /// runOnMachineFunction - pass entry point
126 virtual bool runOnMachineFunction(MachineFunction&);
127
128 private:
129 /// computeIntervals - compute live intervals
130 void computeIntervals();
131
132 /// joinIntervals - join compatible live intervals
133 void joinIntervals();
134
135 /// joinIntervalsInMachineBB - Join intervals based on move
136 /// instructions in the specified basic block.
137 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
138
139 /// handleRegisterDef - update intervals for a register def
140 /// (calls handlePhysicalRegisterDef and
141 /// handleVirtualRegisterDef)
142 void handleRegisterDef(MachineBasicBlock* mbb,
143 MachineBasicBlock::iterator mi,
144 unsigned reg);
145
146 /// handleVirtualRegisterDef - update intervals for a virtual
147 /// register def
148 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
149 MachineBasicBlock::iterator mi,
150 LiveInterval& interval);
151
152 /// handlePhysicalRegisterDef - update intervals for a
153 /// physical register def
154 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
155 MachineBasicBlock::iterator mi,
156 LiveInterval& interval);
157
158 /// Return true if the two specified registers belong to different
159 /// register classes. The registers may be either phys or virt regs.
160 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
161
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000162 bool overlapsAliases(const LiveInterval *lhs,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000163 const LiveInterval *rhs) const;
164
165 static LiveInterval createInterval(unsigned Reg);
166
167 LiveInterval &getOrCreateInterval(unsigned reg) {
168 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
169 if (I == r2iMap_.end())
170 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
171 return I->second;
172 }
173
174 /// rep - returns the representative of this register
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000175 unsigned rep(unsigned Reg) {
176 unsigned Rep = r2rMap_[Reg];
177 if (Rep)
178 return r2rMap_[Reg] = rep(Rep);
179 return Reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000180 }
181
182 void printRegName(unsigned reg) const;
183 };
184
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000185} // End llvm namespace
186
187#endif