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"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
34 class LiveIntervals : public MachineFunctionPass
38 typedef std::pair<unsigned, unsigned> Range;
39 typedef std::vector<Range> Ranges;
40 unsigned reg; // the register of this interval
41 unsigned weight; // weight of this interval (number of uses)
42 Ranges ranges; // the ranges this register is valid
46 unsigned start() const {
47 assert(!ranges.empty() && "empty interval for register");
48 return ranges.front().first;
51 unsigned end() const {
52 assert(!ranges.empty() && "empty interval for register");
53 return ranges.back().second;
56 bool expiredAt(unsigned index) const {
57 return end() <= index;
60 bool liveAt(unsigned index) const;
62 bool overlaps(const Interval& other) const;
64 void addRange(unsigned start, unsigned end);
67 void mergeRangesForward(Ranges::iterator it);
69 void mergeRangesBackward(Ranges::iterator it);
72 struct StartPointComp {
73 bool operator()(const Interval& lhs, const Interval& rhs) {
74 return lhs.ranges.front().first < rhs.ranges.front().first;
79 bool operator()(const Interval& lhs, const Interval& rhs) {
80 return lhs.ranges.back().second < rhs.ranges.back().second;
84 typedef std::vector<Interval> Intervals;
85 typedef std::vector<MachineBasicBlock*> MachineBasicBlockPtrs;
89 const TargetMachine* tm_;
90 const MRegisterInfo* mri_;
91 MachineBasicBlock* currentMbb_;
92 MachineBasicBlock::iterator currentInstr_;
95 std::vector<bool> allocatableRegisters_;
97 typedef std::map<unsigned, MachineBasicBlock*> MbbIndex2MbbMap;
98 MbbIndex2MbbMap mbbi2mbbMap_;
100 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
101 Mi2IndexMap mi2iMap_;
103 typedef std::map<unsigned, unsigned> Reg2IntervalMap;
104 Reg2IntervalMap r2iMap_;
106 Intervals intervals_;
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();
116 result.push_back(it->second);
122 /// runOnMachineFunction - pass entry point
123 bool runOnMachineFunction(MachineFunction&);
125 /// computeIntervals - compute live intervals
126 void computeIntervals();
129 /// handleRegisterDef - update intervals for a register def
130 /// (calls handlePhysicalRegisterDef and
131 /// handleVirtualRegisterDef)
132 void handleRegisterDef(MachineBasicBlock* mbb,
133 MachineBasicBlock::iterator mi,
136 /// handleVirtualRegisterDef - update intervals for a virtual
138 void handleVirtualRegisterDef(MachineBasicBlock* mbb,
139 MachineBasicBlock::iterator mi,
142 /// handlePhysicalRegisterDef - update intervals for a
143 /// physical register def
144 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
145 MachineBasicBlock::iterator mi,
148 unsigned getInstructionIndex(MachineInstr* instr) const;
150 void printRegName(unsigned reg) const;
153 inline bool operator==(const LiveIntervals::Interval& lhs,
154 const LiveIntervals::Interval& rhs) {
155 return lhs.reg == rhs.reg;
158 std::ostream& operator<<(std::ostream& os,
159 const LiveIntervals::Interval& li);
161 } // End llvm namespace