blob: 7cf627b3558e1066a1a915bb665eb59d7d98e0fc [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
12// depth-first order) an interval [i, j] is said to be a live interval
13// 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
16// have holes, i.e. an interval might look like [1,20], [50,65],
17// [1000,1001]
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_LIVEINTERVALS_H
22#define LLVM_CODEGEN_LIVEINTERVALS_H
23
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include <iostream>
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000027#include <list>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000028#include <map>
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000029#include <vector>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000030
31namespace llvm {
32
33 class LiveVariables;
34 class MRegisterInfo;
35
36 class LiveIntervals : public MachineFunctionPass
37 {
38 public:
39 struct Interval {
40 typedef std::pair<unsigned, unsigned> Range;
41 typedef std::vector<Range> Ranges;
42 unsigned reg; // the register of this interval
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000043 float weight; // weight of this interval (number of uses
44 // * 10^loopDepth)
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000045 Ranges ranges; // the ranges in which this register is live
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000047 Interval(unsigned r);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000048
49 unsigned start() const {
50 assert(!ranges.empty() && "empty interval for register");
51 return ranges.front().first;
52 }
53
54 unsigned end() const {
55 assert(!ranges.empty() && "empty interval for register");
56 return ranges.back().second;
57 }
58
Alkis Evlogimenos485ec3c2003-12-18 08:56:11 +000059 bool expiredAt(unsigned index) const {
Alkis Evlogimenos3b02cbe2004-01-16 20:17:05 +000060 return end() <= (index + 1);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000061 }
62
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000063 bool liveAt(unsigned index) const;
64
65 bool overlaps(const Interval& other) const;
66
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000067 void addRange(unsigned start, unsigned end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000069 void join(const Interval& other);
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000070
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000071 private:
72 Ranges::iterator mergeRangesForward(Ranges::iterator it);
73
74 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000075 };
76
77 struct StartPointComp {
78 bool operator()(const Interval& lhs, const Interval& rhs) {
79 return lhs.ranges.front().first < rhs.ranges.front().first;
80 }
81 };
82
83 struct EndPointComp {
84 bool operator()(const Interval& lhs, const Interval& rhs) {
85 return lhs.ranges.back().second < rhs.ranges.back().second;
86 }
87 };
88
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +000089 typedef std::list<Interval> Intervals;
Alkis Evlogimenose88280a2004-01-22 23:08:45 +000090 typedef std::map<unsigned, unsigned> Reg2RegMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091 typedef std::vector<MachineBasicBlock*> MachineBasicBlockPtrs;
92
93 private:
94 MachineFunction* mf_;
95 const TargetMachine* tm_;
96 const MRegisterInfo* mri_;
97 MachineBasicBlock* currentMbb_;
98 MachineBasicBlock::iterator currentInstr_;
99 LiveVariables* lv_;
100
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000101 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
102 MbbIndex2MbbMap mbbi2mbbMap_;
103
104 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
105 Mi2IndexMap mi2iMap_;
106
Alkis Evlogimenosf5f16892004-01-16 16:06:59 +0000107 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000108 Reg2IntervalMap r2iMap_;
109
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000110 Reg2RegMap r2rMap_;
111
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000112 Intervals intervals_;
113
114 public:
115 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
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 Evlogimenosff0cbe12003-11-20 03:32:25 +0000119 MachineBasicBlockPtrs getOrderedMachineBasicBlockPtrs() const {
120 MachineBasicBlockPtrs result;
121 for (MbbIndex2MbbMap::const_iterator
122 it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
123 it != itEnd; ++it) {
124 result.push_back(it->second);
125 }
126 return result;
127 }
128
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000129 const Reg2RegMap& getJoinedRegMap() const {
130 return r2rMap_;
131 }
132
133 /// rep - returns the representative of this register
134 unsigned rep(unsigned reg);
135
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000136 private:
137 /// runOnMachineFunction - pass entry point
138 bool runOnMachineFunction(MachineFunction&);
139
140 /// computeIntervals - compute live intervals
141 void computeIntervals();
142
Alkis Evlogimenose88280a2004-01-22 23:08:45 +0000143 /// joinIntervals - join compatible live intervals
144 void joinIntervals();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000145
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 unsigned reg);
158
159 /// handlePhysicalRegisterDef - update intervals for a
160 /// physical register def
161 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
162 MachineBasicBlock::iterator mi,
163 unsigned reg);
164
Alkis Evlogimenos79b0c3f2004-01-23 13:37:51 +0000165 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
166
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000167 unsigned getInstructionIndex(MachineInstr* instr) const;
168
169 void printRegName(unsigned reg) const;
170 };
171
172 inline bool operator==(const LiveIntervals::Interval& lhs,
173 const LiveIntervals::Interval& rhs) {
174 return lhs.reg == rhs.reg;
175 }
176
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000177 std::ostream& operator<<(std::ostream& os,
178 const LiveIntervals::Interval& li);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000179
180} // End llvm namespace
181
182#endif