1 //===-- llvm/CodeGen/LiveInterval.h - Live Interval Analysis ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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),
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CODEGEN_LIVEINTERVALS_H
22 #define LLVM_CODEGEN_LIVEINTERVALS_H
24 #include "llvm/CodeGen/MachineFunctionPass.h"
33 class LiveIntervals : public MachineFunctionPass
37 typedef std::pair<unsigned, unsigned> Range;
38 typedef std::vector<Range> Ranges;
39 unsigned reg; // the register of this interval
40 float weight; // weight of this interval (number of uses
42 Ranges ranges; // the ranges in which this register is live
46 bool empty() const { return ranges.empty(); }
50 unsigned start() const {
51 assert(!empty() && "empty interval for register");
52 return ranges.front().first;
55 unsigned end() const {
56 assert(!empty() && "empty interval for register");
57 return ranges.back().second;
60 bool expiredAt(unsigned index) const {
61 return end() <= (index + 1);
64 bool liveAt(unsigned index) const;
66 bool overlaps(const Interval& other) const;
68 void addRange(unsigned start, unsigned end);
70 void join(const Interval& other);
73 Ranges::iterator mergeRangesForward(Ranges::iterator it);
75 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
78 struct StartPointComp {
79 bool operator()(const Interval& lhs, const Interval& rhs) {
80 return lhs.ranges.front().first < rhs.ranges.front().first;
85 bool operator()(const Interval& lhs, const Interval& rhs) {
86 return lhs.ranges.back().second < rhs.ranges.back().second;
90 typedef std::list<Interval> Intervals;
94 const TargetMachine* tm_;
95 const MRegisterInfo* mri_;
96 MachineBasicBlock* currentMbb_;
97 MachineBasicBlock::iterator currentInstr_;
100 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
101 MbbIndex2MbbMap mbbi2mbbMap_;
103 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
104 Mi2IndexMap mi2iMap_;
106 typedef std::vector<MachineInstr*> Index2MiMap;
107 Index2MiMap i2miMap_;
109 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
110 Reg2IntervalMap r2iMap_;
112 typedef std::map<unsigned, unsigned> Reg2RegMap;
115 Intervals intervals_;
129 static unsigned getBaseIndex(unsigned index) {
130 return index - (index % InstrSlots::NUM);
132 static unsigned getBoundaryIndex(unsigned index) {
133 return getBaseIndex(index + InstrSlots::NUM - 1);
135 static unsigned getLoadIndex(unsigned index) {
136 return getBaseIndex(index) + InstrSlots::LOAD;
138 static unsigned getUseIndex(unsigned index) {
139 return getBaseIndex(index) + InstrSlots::USE;
141 static unsigned getDefIndex(unsigned index) {
142 return getBaseIndex(index) + InstrSlots::DEF;
144 static unsigned getStoreIndex(unsigned index) {
145 return getBaseIndex(index) + InstrSlots::STORE;
148 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
149 virtual void releaseMemory();
151 /// runOnMachineFunction - pass entry point
152 virtual bool runOnMachineFunction(MachineFunction&);
154 Interval& getInterval(unsigned reg) {
155 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
156 return *r2iMap_.find(reg)->second;
159 /// getInstructionIndex - returns the base index of instr
160 unsigned getInstructionIndex(MachineInstr* instr) const;
162 /// getInstructionFromIndex - given an index in any slot of an
163 /// instruction return a pointer the instruction
164 MachineInstr* getInstructionFromIndex(unsigned index) const;
166 Intervals& getIntervals() { return intervals_; }
168 void updateSpilledInterval(Interval& i, VirtRegMap& vrm, int slot);
171 /// computeIntervals - compute live intervals
172 void computeIntervals();
174 /// joinIntervals - join compatible live intervals
175 void joinIntervals();
177 /// handleRegisterDef - update intervals for a register def
178 /// (calls handlePhysicalRegisterDef and
179 /// handleVirtualRegisterDef)
180 void handleRegisterDef(MachineBasicBlock* mbb,
181 MachineBasicBlock::iterator mi,
184 /// handleVirtualRegisterDef - update intervals for a virtual
186 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
187 MachineBasicBlock::iterator mi,
190 /// handlePhysicalRegisterDef - update intervals for a
191 /// physical register def
192 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
193 MachineBasicBlock::iterator mi,
196 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
198 /// rep - returns the representative of this register
199 unsigned rep(unsigned reg);
201 void printRegName(unsigned reg) const;
204 inline bool operator==(const LiveIntervals::Interval& lhs,
205 const LiveIntervals::Interval& rhs) {
206 return lhs.reg == rhs.reg;
209 std::ostream& operator<<(std::ostream& os,
210 const LiveIntervals::Interval& li);
212 } // End llvm namespace