blob: b4228ce9b6c8cb45301138bf61548dbddef7b305 [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 Evlogimenosf5f16892004-01-16 16:06:59 +000025#include <list>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000026
27namespace llvm {
28
29 class LiveVariables;
30 class MRegisterInfo;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000031 class VirtRegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032
33 class LiveIntervals : public MachineFunctionPass
34 {
35 public:
Chris Lattner418da552004-06-21 13:10:56 +000036 typedef std::list<LiveInterval> Intervals;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037
38 private:
39 MachineFunction* mf_;
40 const TargetMachine* tm_;
41 const MRegisterInfo* mri_;
42 MachineBasicBlock* currentMbb_;
43 MachineBasicBlock::iterator currentInstr_;
44 LiveVariables* lv_;
45
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
47 Mi2IndexMap mi2iMap_;
48
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000049 typedef std::vector<MachineInstr*> Index2MiMap;
50 Index2MiMap i2miMap_;
51
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000052 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000053 Reg2IntervalMap r2iMap_;
54
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +000055 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000056 Reg2RegMap r2rMap_;
57
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000058 Intervals intervals_;
59
60 public:
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000061 struct InstrSlots
62 {
63 enum {
64 LOAD = 0,
65 USE = 1,
66 DEF = 2,
67 STORE = 3,
68 NUM = 4,
69 };
70 };
71
72 static unsigned getBaseIndex(unsigned index) {
Alkis Evlogimenos7200c6b2004-02-22 04:05:13 +000073 return index - (index % InstrSlots::NUM);
74 }
75 static unsigned getBoundaryIndex(unsigned index) {
76 return getBaseIndex(index + InstrSlots::NUM - 1);
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +000077 }
78 static unsigned getLoadIndex(unsigned index) {
79 return getBaseIndex(index) + InstrSlots::LOAD;
80 }
81 static unsigned getUseIndex(unsigned index) {
82 return getBaseIndex(index) + InstrSlots::USE;
83 }
84 static unsigned getDefIndex(unsigned index) {
85 return getBaseIndex(index) + InstrSlots::DEF;
86 }
87 static unsigned getStoreIndex(unsigned index) {
88 return getBaseIndex(index) + InstrSlots::STORE;
89 }
90
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000092 virtual void releaseMemory();
93
94 /// runOnMachineFunction - pass entry point
95 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000096
Chris Lattner418da552004-06-21 13:10:56 +000097 LiveInterval& getInterval(unsigned reg) {
Chris Lattner4dc54ae2004-07-23 18:38:52 +000098 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
99 assert(I != r2iMap_.end()&& "Interval does not exist for register");
100 return *I->second;
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +0000101 }
102
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000103 /// getInstructionIndex - returns the base index of instr
Chris Lattnerf35fef72004-07-23 21:24:19 +0000104 unsigned getInstructionIndex(MachineInstr* instr) const {
105 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
106 assert(it != mi2iMap_.end() && "Invalid instruction!");
107 return it->second;
108 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000109
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000110 /// getInstructionFromIndex - given an index in any slot of an
111 /// instruction return a pointer the instruction
Chris Lattnerf35fef72004-07-23 21:24:19 +0000112 MachineInstr* getInstructionFromIndex(unsigned index) const {
113 index /= InstrSlots::NUM; // convert index to vector index
114 assert(index < i2miMap_.size() &&
115 "index does not correspond to an instruction");
116 return i2miMap_[index];
117 }
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000118
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000119 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000120
Chris Lattner418da552004-06-21 13:10:56 +0000121 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
122 VirtRegMap& vrm,
123 int slot);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000124
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000125 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000126 /// computeIntervals - compute live intervals
127 void computeIntervals();
128
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000129 /// joinIntervals - join compatible live intervals
130 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000131
Chris Lattner1c5c0442004-07-19 14:08:10 +0000132 /// joinIntervalsInMachineBB - Join intervals based on move
133 /// instructions in the specified basic block.
134 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
135
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000136 /// handleRegisterDef - update intervals for a register def
137 /// (calls handlePhysicalRegisterDef and
138 /// handleVirtualRegisterDef)
139 void handleRegisterDef(MachineBasicBlock* mbb,
140 MachineBasicBlock::iterator mi,
141 unsigned reg);
142
143 /// handleVirtualRegisterDef - update intervals for a virtual
144 /// register def
145 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
146 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000147 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000148
149 /// handlePhysicalRegisterDef - update intervals for a
150 /// physical register def
151 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
152 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000153 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000154
Chris Lattner418da552004-06-21 13:10:56 +0000155 bool overlapsAliases(const LiveInterval& lhs,
156 const LiveInterval& rhs) const;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000157
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000158
Chris Lattner418da552004-06-21 13:10:56 +0000159 LiveInterval& getOrCreateInterval(unsigned reg);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000160
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000161 /// rep - returns the representative of this register
Chris Lattnerf35fef72004-07-23 21:24:19 +0000162 unsigned rep(unsigned reg) {
163 Reg2RegMap::iterator it = r2rMap_.find(reg);
164 if (it != r2rMap_.end())
165 return it->second = rep(it->second);
166 return reg;
167 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000168
169 void printRegName(unsigned reg) const;
170 };
171
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000172} // End llvm namespace
173
174#endif