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"
32 class LiveIntervals : public MachineFunctionPass
36 typedef std::pair<unsigned, unsigned> Range;
37 typedef std::vector<Range> Ranges;
38 unsigned reg; // the register of this interval
39 float weight; // weight of this interval (number of uses
41 Ranges ranges; // the ranges in which this register is live
45 bool empty() const { return ranges.empty(); }
47 unsigned start() const {
48 assert(!empty() && "empty interval for register");
49 return ranges.front().first;
52 unsigned end() const {
53 assert(!empty() && "empty interval for register");
54 return ranges.back().second;
57 bool expiredAt(unsigned index) const {
58 return end() <= (index + 1);
61 bool liveAt(unsigned index) const;
63 bool overlaps(const Interval& other) const;
65 void addRange(unsigned start, unsigned end);
67 void join(const Interval& other);
70 Ranges::iterator mergeRangesForward(Ranges::iterator it);
72 Ranges::iterator mergeRangesBackward(Ranges::iterator it);
75 struct StartPointComp {
76 bool operator()(const Interval& lhs, const Interval& rhs) {
77 return lhs.ranges.front().first < rhs.ranges.front().first;
82 bool operator()(const Interval& lhs, const Interval& rhs) {
83 return lhs.ranges.back().second < rhs.ranges.back().second;
87 typedef std::list<Interval> Intervals;
91 const TargetMachine* tm_;
92 const MRegisterInfo* mri_;
93 MachineBasicBlock* currentMbb_;
94 MachineBasicBlock::iterator currentInstr_;
97 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
98 MbbIndex2MbbMap mbbi2mbbMap_;
100 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
101 Mi2IndexMap mi2iMap_;
103 typedef std::vector<MachineInstr*> Index2MiMap;
104 Index2MiMap i2miMap_;
106 typedef std::map<unsigned, Intervals::iterator> Reg2IntervalMap;
107 Reg2IntervalMap r2iMap_;
109 typedef std::map<unsigned, unsigned> Reg2RegMap;
112 Intervals intervals_;
115 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
116 virtual void releaseMemory();
118 /// runOnMachineFunction - pass entry point
119 virtual bool runOnMachineFunction(MachineFunction&);
121 Interval& getInterval(unsigned reg) {
122 assert(r2iMap_.count(reg)&& "Interval does not exist for register");
123 return *r2iMap_.find(reg)->second;
126 unsigned getInstructionIndex(MachineInstr* instr) const;
128 MachineInstr* getInstructionFromIndex(unsigned index) const;
130 Intervals& getIntervals() { return intervals_; }
132 void updateSpilledInterval(Interval& i);
135 /// computeIntervals - compute live intervals
136 void computeIntervals();
138 /// joinIntervals - join compatible live intervals
139 void joinIntervals();
141 /// handleRegisterDef - update intervals for a register def
142 /// (calls handlePhysicalRegisterDef and
143 /// handleVirtualRegisterDef)
144 void handleRegisterDef(MachineBasicBlock* mbb,
145 MachineBasicBlock::iterator mi,
148 /// handleVirtualRegisterDef - update intervals for a virtual
150 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
151 MachineBasicBlock::iterator mi,
154 /// handlePhysicalRegisterDef - update intervals for a
155 /// physical register def
156 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
157 MachineBasicBlock::iterator mi,
160 bool overlapsAliases(const Interval& lhs, const Interval& rhs) const;
162 /// rep - returns the representative of this register
163 unsigned rep(unsigned reg);
165 void printRegName(unsigned reg) const;
168 inline bool operator==(const LiveIntervals::Interval& lhs,
169 const LiveIntervals::Interval& rhs) {
170 return lhs.reg == rhs.reg;
173 std::ostream& operator<<(std::ostream& os,
174 const LiveIntervals::Interval& li);
176 } // End llvm namespace