blob: 2e238085734800cda028055210a921d44744d100 [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>
27#include <map>
28
29namespace llvm {
30
31 class LiveVariables;
32 class MRegisterInfo;
33
34 class LiveIntervals : public MachineFunctionPass
35 {
36 public:
37 struct Interval {
38 typedef std::pair<unsigned, unsigned> Range;
39 typedef std::vector<Range> Ranges;
40 unsigned reg; // the register of this interval
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +000041 unsigned hint;
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +000042 float weight; // weight of this interval (number of uses
43 // * 10^loopDepth)
44 Ranges ranges; // the ranges this register is valid
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000045
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000046 Interval(unsigned r);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047
48 unsigned start() const {
49 assert(!ranges.empty() && "empty interval for register");
50 return ranges.front().first;
51 }
52
53 unsigned end() const {
54 assert(!ranges.empty() && "empty interval for register");
55 return ranges.back().second;
56 }
57
Alkis Evlogimenos485ec3c2003-12-18 08:56:11 +000058 bool expiredAt(unsigned index) const {
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000059 return end() <= index;
60 }
61
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +000062 bool liveAt(unsigned index) const;
63
64 bool overlaps(const Interval& other) const;
65
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000066 void addRange(unsigned start, unsigned end);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000067
68 private:
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +000069 void mergeRangesForward(Ranges::iterator it);
70
71 void mergeRangesBackward(Ranges::iterator it);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000072 };
73
74 struct StartPointComp {
75 bool operator()(const Interval& lhs, const Interval& rhs) {
76 return lhs.ranges.front().first < rhs.ranges.front().first;
77 }
78 };
79
80 struct EndPointComp {
81 bool operator()(const Interval& lhs, const Interval& rhs) {
82 return lhs.ranges.back().second < rhs.ranges.back().second;
83 }
84 };
85
86 typedef std::vector<Interval> Intervals;
87 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
103 typedef std::map<unsigned, unsigned> Reg2IntervalMap;
104 Reg2IntervalMap r2iMap_;
105
106 Intervals intervals_;
107
108 public:
109 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
110 Intervals& getIntervals() { return intervals_; }
111 MachineBasicBlockPtrs getOrderedMachineBasicBlockPtrs() const {
112 MachineBasicBlockPtrs result;
113 for (MbbIndex2MbbMap::const_iterator
114 it = mbbi2mbbMap_.begin(), itEnd = mbbi2mbbMap_.end();
115 it != itEnd; ++it) {
116 result.push_back(it->second);
117 }
118 return result;
119 }
120
121 private:
122 /// runOnMachineFunction - pass entry point
123 bool runOnMachineFunction(MachineFunction&);
124
125 /// computeIntervals - compute live intervals
126 void computeIntervals();
127
128
129 /// handleRegisterDef - update intervals for a register def
130 /// (calls handlePhysicalRegisterDef and
131 /// handleVirtualRegisterDef)
132 void handleRegisterDef(MachineBasicBlock* mbb,
133 MachineBasicBlock::iterator mi,
134 unsigned reg);
135
136 /// handleVirtualRegisterDef - update intervals for a virtual
137 /// register def
138 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
139 MachineBasicBlock::iterator mi,
140 unsigned reg);
141
142 /// handlePhysicalRegisterDef - update intervals for a
143 /// physical register def
144 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
145 MachineBasicBlock::iterator mi,
146 unsigned reg);
147
148 unsigned getInstructionIndex(MachineInstr* instr) const;
149
150 void printRegName(unsigned reg) const;
151 };
152
153 inline bool operator==(const LiveIntervals::Interval& lhs,
154 const LiveIntervals::Interval& rhs) {
155 return lhs.reg == rhs.reg;
156 }
157
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000158 std::ostream& operator<<(std::ostream& os,
159 const LiveIntervals::Interval& li);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000160
161} // End llvm namespace
162
163#endif