blob: f7f4569cb3518b05f08268bd8311d03642bf8014 [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;
Chris Lattnerf768bba2005-03-09 23:05:19 +000031 class TargetInstrInfo;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000032 class VirtRegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000034 class LiveIntervals : public MachineFunctionPass {
35 MachineFunction* mf_;
36 const TargetMachine* tm_;
37 const MRegisterInfo* mri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000038 const TargetInstrInfo* tii_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000039 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000040
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000041 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
42 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000043
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000044 typedef std::vector<MachineInstr*> Index2MiMap;
45 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000046
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000047 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
48 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000049
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +000050 typedef DenseMap<unsigned> Reg2RegMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000051 Reg2RegMap r2rMap_;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000052
Alkis Evlogimenos53278012004-08-26 22:22:38 +000053 std::vector<bool> allocatableRegs_;
54
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 public:
56 struct InstrSlots
57 {
58 enum {
59 LOAD = 0,
60 USE = 1,
61 DEF = 2,
62 STORE = 3,
63 NUM = 4,
64 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065 };
66
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000067 static unsigned getBaseIndex(unsigned index) {
68 return index - (index % InstrSlots::NUM);
69 }
70 static unsigned getBoundaryIndex(unsigned index) {
71 return getBaseIndex(index + InstrSlots::NUM - 1);
72 }
73 static unsigned getLoadIndex(unsigned index) {
74 return getBaseIndex(index) + InstrSlots::LOAD;
75 }
76 static unsigned getUseIndex(unsigned index) {
77 return getBaseIndex(index) + InstrSlots::USE;
78 }
79 static unsigned getDefIndex(unsigned index) {
80 return getBaseIndex(index) + InstrSlots::DEF;
81 }
82 static unsigned getStoreIndex(unsigned index) {
83 return getBaseIndex(index) + InstrSlots::STORE;
84 }
85
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +000087 typedef Reg2IntervalMap::const_iterator const_iterator;
88 const_iterator begin() const { return r2iMap_.begin(); }
89 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 iterator begin() { return r2iMap_.begin(); }
91 iterator end() { return r2iMap_.end(); }
92 unsigned getNumIntervals() const { return r2iMap_.size(); }
93
94 LiveInterval &getInterval(unsigned reg) {
95 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
96 assert(I != r2iMap_.end() && "Interval does not exist for register");
97 return I->second;
98 }
99
100 const LiveInterval &getInterval(unsigned reg) const {
101 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
102 assert(I != r2iMap_.end() && "Interval does not exist for register");
103 return I->second;
104 }
105
106 /// getInstructionIndex - returns the base index of instr
107 unsigned getInstructionIndex(MachineInstr* instr) const {
108 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
109 assert(it != mi2iMap_.end() && "Invalid instruction!");
110 return it->second;
111 }
112
113 /// getInstructionFromIndex - given an index in any slot of an
114 /// instruction return a pointer the instruction
115 MachineInstr* getInstructionFromIndex(unsigned index) const {
116 index /= InstrSlots::NUM; // convert index to vector index
117 assert(index < i2miMap_.size() &&
118 "index does not correspond to an instruction");
119 return i2miMap_[index];
120 }
121
122 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
123 VirtRegMap& vrm,
124 int slot);
125
126 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
127 virtual void releaseMemory();
128
129 /// runOnMachineFunction - pass entry point
130 virtual bool runOnMachineFunction(MachineFunction&);
131
Chris Lattner70ca3582004-09-30 15:59:17 +0000132 /// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000133 virtual void print(std::ostream &O, const Module* = 0) const;
Chris Lattner70ca3582004-09-30 15:59:17 +0000134
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000135 private:
136 /// computeIntervals - compute live intervals
137 void computeIntervals();
138
139 /// joinIntervals - join compatible live intervals
140 void joinIntervals();
141
142 /// joinIntervalsInMachineBB - Join intervals based on move
143 /// instructions in the specified basic block.
144 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
145
146 /// handleRegisterDef - update intervals for a register def
147 /// (calls handlePhysicalRegisterDef and
148 /// handleVirtualRegisterDef)
149 void handleRegisterDef(MachineBasicBlock* mbb,
150 MachineBasicBlock::iterator mi,
151 unsigned reg);
152
153 /// handleVirtualRegisterDef - update intervals for a virtual
154 /// register def
155 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
156 MachineBasicBlock::iterator mi,
157 LiveInterval& interval);
158
Chris Lattnerf768bba2005-03-09 23:05:19 +0000159 /// handlePhysicalRegisterDef - update intervals for a physical register
160 /// def. If the defining instruction is a move instruction, SrcReg will be
161 /// the input register, and DestReg will be the result. Note that Interval
162 /// may not match DestReg (it might be an alias instead).
163 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000164 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
165 MachineBasicBlock::iterator mi,
Chris Lattnerf768bba2005-03-09 23:05:19 +0000166 LiveInterval& interval,
167 unsigned SrcReg, unsigned DestReg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000168
169 /// Return true if the two specified registers belong to different
170 /// register classes. The registers may be either phys or virt regs.
171 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
172
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000173 bool overlapsAliases(const LiveInterval *lhs,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000174 const LiveInterval *rhs) const;
175
176 static LiveInterval createInterval(unsigned Reg);
177
178 LiveInterval &getOrCreateInterval(unsigned reg) {
179 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
180 if (I == r2iMap_.end())
181 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
182 return I->second;
183 }
184
185 /// rep - returns the representative of this register
Alkis Evlogimenos5d0d1e32004-09-08 03:01:50 +0000186 unsigned rep(unsigned Reg) {
187 unsigned Rep = r2rMap_[Reg];
188 if (Rep)
189 return r2rMap_[Reg] = rep(Rep);
190 return Reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000191 }
192
193 void printRegName(unsigned reg) const;
194 };
195
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000196} // End llvm namespace
197
198#endif