blob: c1fa5c591fbdb46e9f929ecaaec09302c7370933 [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
23#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerfb449b92004-07-23 17:49:16 +000024#include "LiveInterval.h"
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 Lattner4df98e52004-07-24 03:32:06 +000032 class LiveIntervals : public MachineFunctionPass {
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033 MachineFunction* mf_;
34 const TargetMachine* tm_;
35 const MRegisterInfo* mri_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000036 LiveVariables* lv_;
37
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000038 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
39 Mi2IndexMap mi2iMap_;
40
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000041 typedef std::vector<MachineInstr*> Index2MiMap;
42 Index2MiMap i2miMap_;
43
Chris Lattner4df98e52004-07-24 03:32:06 +000044 /// r2iMap_ - This map OWNS the interval pointed to by the map. When
45 /// this map is destroyed or when entries are modified, this intervals
46 /// should be destroyed or modified as well.
47 std::map<unsigned, LiveInterval*> r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000048
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +000049 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000050 Reg2RegMap r2rMap_;
51
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000052 public:
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000053 struct InstrSlots
54 {
55 enum {
56 LOAD = 0,
57 USE = 1,
58 DEF = 2,
59 STORE = 3,
60 NUM = 4,
61 };
62 };
63
64 static unsigned getBaseIndex(unsigned index) {
Alkis Evlogimenos7200c6b2004-02-22 04:05:13 +000065 return index - (index % InstrSlots::NUM);
66 }
67 static unsigned getBoundaryIndex(unsigned index) {
68 return getBaseIndex(index + InstrSlots::NUM - 1);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000069 }
70 static unsigned getLoadIndex(unsigned index) {
71 return getBaseIndex(index) + InstrSlots::LOAD;
72 }
73 static unsigned getUseIndex(unsigned index) {
74 return getBaseIndex(index) + InstrSlots::USE;
75 }
76 static unsigned getDefIndex(unsigned index) {
77 return getBaseIndex(index) + InstrSlots::DEF;
78 }
79 static unsigned getStoreIndex(unsigned index) {
80 return getBaseIndex(index) + InstrSlots::STORE;
81 }
82
Chris Lattner4df98e52004-07-24 03:32:06 +000083 typedef std::map<unsigned, LiveInterval*>::const_iterator iterator;
84 iterator begin() const { return r2iMap_.begin(); }
85 iterator end() const { return r2iMap_.end(); }
86 unsigned getNumIntervals() const { return r2iMap_.size(); }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000087
Chris Lattner4df98e52004-07-24 03:32:06 +000088 LiveInterval &getInterval(unsigned reg) const {
89 std::map<unsigned, LiveInterval*>::const_iterator I =
90 r2iMap_.find(reg);
91 assert(I != r2iMap_.end() && "Interval does not exist for register");
Chris Lattner0f4c0762004-07-24 02:53:43 +000092 return *I->second;
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +000093 }
94
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000095 /// getInstructionIndex - returns the base index of instr
Chris Lattnerf35fef72004-07-23 21:24:19 +000096 unsigned getInstructionIndex(MachineInstr* instr) const {
97 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
98 assert(it != mi2iMap_.end() && "Invalid instruction!");
99 return it->second;
100 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000101
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000102 /// getInstructionFromIndex - given an index in any slot of an
103 /// instruction return a pointer the instruction
Chris Lattnerf35fef72004-07-23 21:24:19 +0000104 MachineInstr* getInstructionFromIndex(unsigned index) const {
105 index /= InstrSlots::NUM; // convert index to vector index
106 assert(index < i2miMap_.size() &&
107 "index does not correspond to an instruction");
108 return i2miMap_[index];
109 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000110
Chris Lattner418da552004-06-21 13:10:56 +0000111 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
112 VirtRegMap& vrm,
113 int slot);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000114
Chris Lattner4df98e52004-07-24 03:32:06 +0000115 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
116 virtual void releaseMemory();
117
118 /// runOnMachineFunction - pass entry point
119 virtual bool runOnMachineFunction(MachineFunction&);
120
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000121 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000122 /// computeIntervals - compute live intervals
123 void computeIntervals();
124
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000125 /// joinIntervals - join compatible live intervals
126 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000127
Chris Lattner1c5c0442004-07-19 14:08:10 +0000128 /// joinIntervalsInMachineBB - Join intervals based on move
129 /// instructions in the specified basic block.
130 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
131
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000132 /// handleRegisterDef - update intervals for a register def
133 /// (calls handlePhysicalRegisterDef and
134 /// handleVirtualRegisterDef)
135 void handleRegisterDef(MachineBasicBlock* mbb,
136 MachineBasicBlock::iterator mi,
137 unsigned reg);
138
139 /// handleVirtualRegisterDef - update intervals for a virtual
140 /// register def
141 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
142 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000143 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144
145 /// handlePhysicalRegisterDef - update intervals for a
146 /// physical register def
147 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
148 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000149 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000150
Chris Lattner0f4c0762004-07-24 02:53:43 +0000151 /// Return true if the two specified registers belong to different
152 /// register classes. The registers may be either phys or virt regs.
153 bool differingRegisterClasses(unsigned RegA, unsigned RegB) const;
154
155 bool overlapsAliases(const LiveInterval *lhs,
156 const LiveInterval *rhs) const;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000157
Chris Lattner4df98e52004-07-24 03:32:06 +0000158 LiveInterval *createInterval(unsigned Reg) const;
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000159
Chris Lattner4df98e52004-07-24 03:32:06 +0000160 LiveInterval &getOrCreateInterval(unsigned reg) {
161 LiveInterval *&LI = r2iMap_[reg];
162 if (LI == 0)
163 LI = createInterval(reg);
164 return *LI;
165 }
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000166
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000167 /// rep - returns the representative of this register
Chris Lattnerf35fef72004-07-23 21:24:19 +0000168 unsigned rep(unsigned reg) {
169 Reg2RegMap::iterator it = r2rMap_.find(reg);
170 if (it != r2rMap_.end())
171 return it->second = rep(it->second);
172 return reg;
173 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000174
175 void printRegName(unsigned reg) const;
176 };
177
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000178} // End llvm namespace
179
180#endif