blob: f322ae33c91c4c59722422b5feb041c5ef14e801 [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//
10// This file implements the LiveInterval analysis pass. Given some
11// numbering of each the machine instructions (in this implemention
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000012// depth-first order) an interval [i, j) is said to be a live interval
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000013// for register v if there is no instruction with number j' > j such
14// that v is live at j' abd there is no instruction with number i' < i
15// such that v is live at i'. In this implementation intervals can
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000016// have holes, i.e. an interval might look like [1,20), [50,65),
17// [1000,1001)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000018//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_LIVEINTERVALS_H
22#define LLVM_CODEGEN_LIVEINTERVALS_H
23
24#include "llvm/CodeGen/MachineFunctionPass.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;
31
32 class LiveIntervals : public MachineFunctionPass
33 {
34 public:
35 struct Interval {
36 typedef std::pair<unsigned, unsigned> Range;
37 typedef std::vector<Range> Ranges;
38 unsigned reg; // the register of this interval
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000039 float weight; // weight of this interval (number of uses
40 // * 10^loopDepth)
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000041 Ranges ranges; // the ranges in which this register is live
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000042
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000043 Interval(unsigned r);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000044
45 unsigned start() const {
46 assert(!ranges.empty() && "empty interval for register");
47 return ranges.front().first;
48 }
49
50 unsigned end() const {
51 assert(!ranges.empty() && "empty interval for register");
52 return ranges.back().second;
53 }
54
Alkis Evlogimenos485ec3c2003-12-18 08:56:11 +000055 bool expiredAt(unsigned index) const {
Alkis Evlogimenos3b02cbe2004-01-16 20:17:05 +000056 return end() <= (index + 1);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000057 }
58
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000059 bool liveAt(unsigned index) const;
60
61 bool overlaps(const Interval& other) const;
62
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000063 void addRange(unsigned start, unsigned end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000064
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000065 void join(const Interval& other);
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000066
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000067 private:
68 Ranges::iterator mergeRangesForward(Ranges::iterator it);
69
70 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000071 };
72
73 struct StartPointComp {
74 bool operator()(const Interval& lhs, const Interval& rhs) {
75 return lhs.ranges.front().first < rhs.ranges.front().first;
76 }
77 };
78
79 struct EndPointComp {
80 bool operator()(const Interval& lhs, const Interval& rhs) {
81 return lhs.ranges.back().second < rhs.ranges.back().second;
82 }
83 };
84
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000085 typedef std::list<Interval> Intervals;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000086 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000087 typedef std::vector<MachineBasicBlock*> MachineBasicBlockPtrs;
88
89 private:
90 MachineFunction* mf_;
91 const TargetMachine* tm_;
92 const MRegisterInfo* mri_;
93 MachineBasicBlock* currentMbb_;
94 MachineBasicBlock::iterator currentInstr_;
95 LiveVariables* lv_;
96
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000097 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
98 MbbIndex2MbbMap mbbi2mbbMap_;
99
100 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
101 Mi2IndexMap mi2iMap_;
102
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000103 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000104 Reg2IntervalMap r2iMap_;
105
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000106 Reg2RegMap r2rMap_;
107
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000108 Intervals intervals_;
109
110 public:
111 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000112 virtual void releaseMemory();
113
114 /// runOnMachineFunction - pass entry point
115 virtual bool runOnMachineFunction(MachineFunction&);
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000116
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000117 Intervals& getIntervals() { return intervals_; }
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000118
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000119 const Reg2RegMap& getJoinedRegMap() const {
120 return r2rMap_;
121 }
122
123 /// rep - returns the representative of this register
124 unsigned rep(unsigned reg);
125
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000126 private:
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000127 /// computeIntervals - compute live intervals
128 void computeIntervals();
129
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000130 /// joinIntervals - join compatible live intervals
131 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000132
133 /// handleRegisterDef - update intervals for a register def
134 /// (calls handlePhysicalRegisterDef and
135 /// handleVirtualRegisterDef)
136 void handleRegisterDef(MachineBasicBlock* mbb,
137 MachineBasicBlock::iterator mi,
138 unsigned reg);
139
140 /// handleVirtualRegisterDef - update intervals for a virtual
141 /// register def
142 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
143 MachineBasicBlock::iterator mi,
144 unsigned reg);
145
146 /// handlePhysicalRegisterDef - update intervals for a
147 /// physical register def
148 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
149 MachineBasicBlock::iterator mi,
150 unsigned reg);
151
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000152 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
153
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000154 unsigned getInstructionIndex(MachineInstr* instr) const;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000155
156 void printRegName(unsigned reg) const;
157 };
158
159 inline bool operator==(const LiveIntervals::Interval& lhs,
160 const LiveIntervals::Interval& rhs) {
161 return lhs.reg == rhs.reg;
162 }
163
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000164 std::ostream& operator<<(std::ostream& os,
165 const LiveIntervals::Interval& li);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000166
167} // End llvm namespace
168
169#endif