blob: c9f9d2dcc28ee81e76effc23c7f66dea343a4c25 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
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//
Chris Lattner6b929062004-07-19 02:13:59 +000010// This file implements the LiveInterval analysis pass. Given some numbering of
11// each the machine instructions (in this implemention depth-first order) an
12// interval [i, j) is said to be a live interval for register v if there is no
13// instruction with number j' > j such that v is live at j' abd there is no
14// instruction with number i' < i such that v is live at i'. In this
15// implementation intervals can have holes, i.e. an interval might look like
16// [1,20), [50,65), [1000,1001).
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattnera3b8b5c2004-07-23 17:56:30 +000020#ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
21#define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022
23#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner779a6512005-09-21 04:18:25 +000024#include "llvm/CodeGen/LiveInterval.h"
Evan Cheng61de82d2007-02-15 05:59:24 +000025#include "llvm/ADT/BitVector.h"
Evan Cheng20b0abc2007-04-17 20:32:26 +000026#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000027#include "llvm/ADT/IndexedMap.h"
Evan Cheng549f27d32007-08-13 23:45:17 +000028#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/SmallVector.h"
Evan Chengf3bb2e62007-09-05 21:46:51 +000030#include "llvm/Support/Allocator.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000031
32namespace llvm {
33
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000034 class LiveVariables;
35 class MRegisterInfo;
Chris Lattnerf768bba2005-03-09 23:05:19 +000036 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000037 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000038 class VirtRegMap;
Evan Cheng4ca980e2007-10-17 02:10:22 +000039 typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000040
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000041 class LiveIntervals : public MachineFunctionPass {
42 MachineFunction* mf_;
43 const TargetMachine* tm_;
44 const MRegisterInfo* mri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000045 const TargetInstrInfo* tii_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000046 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047
Evan Chengf3bb2e62007-09-05 21:46:51 +000048 /// Special pool allocator for VNInfo's (LiveInterval val#).
49 ///
50 BumpPtrAllocator VNInfoAllocator;
51
Evan Cheng549f27d32007-08-13 23:45:17 +000052 /// MBB2IdxMap - The indexes of the first and last instructions in the
53 /// specified basic block.
54 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000055
Evan Cheng4ca980e2007-10-17 02:10:22 +000056 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
57 /// and MBB id.
58 std::vector<IdxMBBPair> Idx2MBBMap;
59
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000060 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
61 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 typedef std::vector<MachineInstr*> Index2MiMap;
64 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000065
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000066 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
67 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Evan Cheng61de82d2007-02-15 05:59:24 +000069 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000070
Evan Cheng549f27d32007-08-13 23:45:17 +000071 std::vector<MachineInstr*> ClonedMIs;
72
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000074 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000075 LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {}
76
Chris Lattnerf7da2c72006-08-24 22:43:55 +000077 struct InstrSlots {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000078 enum {
79 LOAD = 0,
80 USE = 1,
81 DEF = 2,
82 STORE = 3,
Chris Lattner410354f2006-02-22 16:23:43 +000083 NUM = 4
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085 };
86
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000087 static unsigned getBaseIndex(unsigned index) {
88 return index - (index % InstrSlots::NUM);
89 }
90 static unsigned getBoundaryIndex(unsigned index) {
91 return getBaseIndex(index + InstrSlots::NUM - 1);
92 }
93 static unsigned getLoadIndex(unsigned index) {
94 return getBaseIndex(index) + InstrSlots::LOAD;
95 }
96 static unsigned getUseIndex(unsigned index) {
97 return getBaseIndex(index) + InstrSlots::USE;
98 }
99 static unsigned getDefIndex(unsigned index) {
100 return getBaseIndex(index) + InstrSlots::DEF;
101 }
102 static unsigned getStoreIndex(unsigned index) {
103 return getBaseIndex(index) + InstrSlots::STORE;
104 }
105
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +0000107 typedef Reg2IntervalMap::const_iterator const_iterator;
108 const_iterator begin() const { return r2iMap_.begin(); }
109 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000110 iterator begin() { return r2iMap_.begin(); }
111 iterator end() { return r2iMap_.end(); }
112 unsigned getNumIntervals() const { return r2iMap_.size(); }
113
114 LiveInterval &getInterval(unsigned reg) {
115 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
116 assert(I != r2iMap_.end() && "Interval does not exist for register");
117 return I->second;
118 }
119
120 const LiveInterval &getInterval(unsigned reg) const {
121 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
122 assert(I != r2iMap_.end() && "Interval does not exist for register");
123 return I->second;
124 }
125
Evan Chengb371f452007-02-19 21:49:54 +0000126 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000127 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000128 }
129
Chris Lattner428b92e2006-09-15 03:57:23 +0000130 /// getMBBStartIdx - Return the base index of the first instruction in the
131 /// specified MachineBasicBlock.
132 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
133 return getMBBStartIdx(MBB->getNumber());
134 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000135 unsigned getMBBStartIdx(unsigned MBBNo) const {
136 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000137 return MBB2IdxMap[MBBNo].first;
138 }
139
140 /// getMBBEndIdx - Return the store index of the last instruction in the
141 /// specified MachineBasicBlock.
142 unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
143 return getMBBEndIdx(MBB->getNumber());
144 }
145 unsigned getMBBEndIdx(unsigned MBBNo) const {
146 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
147 return MBB2IdxMap[MBBNo].second;
Chris Lattner428b92e2006-09-15 03:57:23 +0000148 }
149
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000150 /// getInstructionIndex - returns the base index of instr
151 unsigned getInstructionIndex(MachineInstr* instr) const {
152 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
153 assert(it != mi2iMap_.end() && "Invalid instruction!");
154 return it->second;
155 }
156
157 /// getInstructionFromIndex - given an index in any slot of an
158 /// instruction return a pointer the instruction
159 MachineInstr* getInstructionFromIndex(unsigned index) const {
160 index /= InstrSlots::NUM; // convert index to vector index
161 assert(index < i2miMap_.size() &&
162 "index does not correspond to an instruction");
163 return i2miMap_[index];
164 }
David Greene25133302007-06-08 17:18:56 +0000165
Evan Cheng4ca980e2007-10-17 02:10:22 +0000166 /// findLiveInMBBs - Given a live range, if the value of the range
167 /// is live in any MBB returns true as well as the list of basic blocks
168 /// where the value is live in.
169 bool findLiveInMBBs(const LiveRange &LR,
170 SmallVector<MachineBasicBlock*, 4> &MBBs) const;
171
David Greene25133302007-06-08 17:18:56 +0000172 // Interval creation
173
174 LiveInterval &getOrCreateInterval(unsigned reg) {
175 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
176 if (I == r2iMap_.end())
177 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
178 return I->second;
179 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000180
David Greene25133302007-06-08 17:18:56 +0000181 std::vector<LiveInterval*> addIntervalsForSpills(const LiveInterval& i,
Evan Cheng549f27d32007-08-13 23:45:17 +0000182 VirtRegMap& vrm, unsigned reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000183
David Greene25133302007-06-08 17:18:56 +0000184 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000185
David Greene25133302007-06-08 17:18:56 +0000186 void removeInterval(unsigned Reg) {
187 r2iMap_.erase(Reg);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000188 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000189
Evan Cheng30cac022007-02-22 23:03:39 +0000190 /// isRemoved - returns true if the specified machine instr has been
191 /// removed.
192 bool isRemoved(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000193 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000194 }
195
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000196 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
197 /// deleted.
198 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
199 // remove index -> MachineInstr and
200 // MachineInstr -> index mappings
201 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
202 if (mi2i != mi2iMap_.end()) {
203 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
204 mi2iMap_.erase(mi2i);
205 }
206 }
David Greene25133302007-06-08 17:18:56 +0000207
Evan Chengf3bb2e62007-09-05 21:46:51 +0000208 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
209
David Greene25133302007-06-08 17:18:56 +0000210 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
211 virtual void releaseMemory();
212
213 /// runOnMachineFunction - pass entry point
214 virtual bool runOnMachineFunction(MachineFunction&);
215
216 /// print - Implement the dump method.
217 virtual void print(std::ostream &O, const Module* = 0) const;
218 void print(std::ostream *O, const Module* M = 0) const {
219 if (O) print(*O, M);
220 }
221
222 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000223 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000224 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000225
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000226 /// handleRegisterDef - update intervals for a register def
227 /// (calls handlePhysicalRegisterDef and
228 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000229 void handleRegisterDef(MachineBasicBlock *MBB,
230 MachineBasicBlock::iterator MI, unsigned MIIdx,
Chris Lattnerf38a05d2006-01-29 07:59:37 +0000231 unsigned reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000232
233 /// handleVirtualRegisterDef - update intervals for a virtual
234 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000235 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
236 MachineBasicBlock::iterator MI,
237 unsigned MIIdx,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 LiveInterval& interval);
239
Chris Lattnerf768bba2005-03-09 23:05:19 +0000240 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000241 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
243 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000244 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000245 LiveInterval &interval,
246 unsigned SrcReg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000247
Evan Chengb371f452007-02-19 21:49:54 +0000248 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000249 void handleLiveInRegister(MachineBasicBlock* mbb,
250 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000251 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000252
Evan Cheng549f27d32007-08-13 23:45:17 +0000253 /// isReMaterializable - Returns true if the definition MI of the specified
254 /// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000255 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Cheng549f27d32007-08-13 23:45:17 +0000256 MachineInstr *MI);
257
Evan Cheng35b35c52007-08-30 05:52:20 +0000258 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
259 /// slot / to reg or any rematerialized load into ith operand of specified
260 /// MI. If it is successul, MI is updated with the newly created MI and
261 /// returns true.
262 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000263 MachineInstr *DefMI, unsigned index, unsigned i,
264 bool isSS, int slot, unsigned reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000265
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000266 static LiveInterval createInterval(unsigned Reg);
267
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000268 void printRegName(unsigned reg) const;
269 };
270
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000271} // End llvm namespace
272
273#endif