blob: 84c8e069dfea14471e732ff3867405e259df40e3 [file] [log] [blame]
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001//===-- llvm/CodeGen/LiveInterval.h - Live Interval Analysis ----*- C++ -*-===//
2//
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
20#ifndef LLVM_CODEGEN_LIVEINTERVALS_H
21#define LLVM_CODEGEN_LIVEINTERVALS_H
22
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) {
Alkis Evlogimenos52f8f562004-02-18 23:14:52 +000098 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
99 return *r2iMap_.find(reg)->second;
100 }
101
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000102 /// getInstructionIndex - returns the base index of instr
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000103 unsigned getInstructionIndex(MachineInstr* instr) const;
104
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000105 /// getInstructionFromIndex - given an index in any slot of an
106 /// instruction return a pointer the instruction
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000107 MachineInstr* getInstructionFromIndex(unsigned index) const;
108
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000109 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +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
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000115 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000116 /// computeIntervals - compute live intervals
117 void computeIntervals();
118
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000119 /// joinIntervals - join compatible live intervals
120 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000121
Chris Lattner1c5c0442004-07-19 14:08:10 +0000122 /// joinIntervalsInMachineBB - Join intervals based on move
123 /// instructions in the specified basic block.
124 void joinIntervalsInMachineBB(MachineBasicBlock *MBB);
125
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000126 /// handleRegisterDef - update intervals for a register def
127 /// (calls handlePhysicalRegisterDef and
128 /// handleVirtualRegisterDef)
129 void handleRegisterDef(MachineBasicBlock* mbb,
130 MachineBasicBlock::iterator mi,
131 unsigned reg);
132
133 /// handleVirtualRegisterDef - update intervals for a virtual
134 /// register def
135 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
136 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000137 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000138
139 /// handlePhysicalRegisterDef - update intervals for a
140 /// physical register def
141 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
142 MachineBasicBlock::iterator mi,
Chris Lattner418da552004-06-21 13:10:56 +0000143 LiveInterval& interval);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144
Chris Lattner418da552004-06-21 13:10:56 +0000145 bool overlapsAliases(const LiveInterval& lhs,
146 const LiveInterval& rhs) const;
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000147
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000148
Chris Lattner418da552004-06-21 13:10:56 +0000149 LiveInterval& getOrCreateInterval(unsigned reg);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000150
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000151 /// rep - returns the representative of this register
152 unsigned rep(unsigned reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000153
154 void printRegName(unsigned reg) const;
155 };
156
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000157} // End llvm namespace
158
159#endif