blob: 16dff5583670a8d87f52a6e2e88a0ebe6c5e9727 [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"
Evan Cheng549f27d32007-08-13 23:45:17 +000027#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/SmallVector.h"
Evan Chengf3bb2e62007-09-05 21:46:51 +000029#include "llvm/Support/Allocator.h"
Hartmut Kaiserffb15de2007-11-13 23:04:28 +000030#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000031
32namespace llvm {
33
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000034 class LiveVariables;
35 class MRegisterInfo;
Evan Chengf2fbca62007-11-12 06:35:08 +000036 class SSARegMap;
Chris Lattnerf768bba2005-03-09 23:05:19 +000037 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000038 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000039 class VirtRegMap;
Evan Cheng4ca980e2007-10-17 02:10:22 +000040 typedef std::pair<unsigned, MachineBasicBlock*> IdxMBBPair;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000041
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 class LiveIntervals : public MachineFunctionPass {
43 MachineFunction* mf_;
44 const TargetMachine* tm_;
45 const MRegisterInfo* mri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000046 const TargetInstrInfo* tii_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000047 LiveVariables* lv_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000048
Evan Chengf3bb2e62007-09-05 21:46:51 +000049 /// Special pool allocator for VNInfo's (LiveInterval val#).
50 ///
51 BumpPtrAllocator VNInfoAllocator;
52
Evan Cheng549f27d32007-08-13 23:45:17 +000053 /// MBB2IdxMap - The indexes of the first and last instructions in the
54 /// specified basic block.
55 std::vector<std::pair<unsigned, unsigned> > MBB2IdxMap;
David Greene25133302007-06-08 17:18:56 +000056
Evan Cheng4ca980e2007-10-17 02:10:22 +000057 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
58 /// and MBB id.
59 std::vector<IdxMBBPair> Idx2MBBMap;
60
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
62 Mi2IndexMap mi2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000063
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 typedef std::vector<MachineInstr*> Index2MiMap;
65 Index2MiMap i2miMap_;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000066
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000067 typedef std::map<unsigned, LiveInterval> Reg2IntervalMap;
68 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069
Evan Cheng61de82d2007-02-15 05:59:24 +000070 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000071
Evan Cheng549f27d32007-08-13 23:45:17 +000072 std::vector<MachineInstr*> ClonedMIs;
73
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000074 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000075 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000076 LiveIntervals() : MachineFunctionPass((intptr_t)&ID) {}
77
Chris Lattnerf7da2c72006-08-24 22:43:55 +000078 struct InstrSlots {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000079 enum {
80 LOAD = 0,
81 USE = 1,
82 DEF = 2,
83 STORE = 3,
Chris Lattner410354f2006-02-22 16:23:43 +000084 NUM = 4
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000086 };
87
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000088 static unsigned getBaseIndex(unsigned index) {
89 return index - (index % InstrSlots::NUM);
90 }
91 static unsigned getBoundaryIndex(unsigned index) {
92 return getBaseIndex(index + InstrSlots::NUM - 1);
93 }
94 static unsigned getLoadIndex(unsigned index) {
95 return getBaseIndex(index) + InstrSlots::LOAD;
96 }
97 static unsigned getUseIndex(unsigned index) {
98 return getBaseIndex(index) + InstrSlots::USE;
99 }
100 static unsigned getDefIndex(unsigned index) {
101 return getBaseIndex(index) + InstrSlots::DEF;
102 }
103 static unsigned getStoreIndex(unsigned index) {
104 return getBaseIndex(index) + InstrSlots::STORE;
105 }
106
Evan Chengf2fbca62007-11-12 06:35:08 +0000107 static float getSpillWeight(const MachineOperand &mop, unsigned loopDepth) {
108 return (mop.isUse()+mop.isDef()) * powf(10.0F, (float)loopDepth);
109 }
110
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000111 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +0000112 typedef Reg2IntervalMap::const_iterator const_iterator;
113 const_iterator begin() const { return r2iMap_.begin(); }
114 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 iterator begin() { return r2iMap_.begin(); }
116 iterator end() { return r2iMap_.end(); }
117 unsigned getNumIntervals() const { return r2iMap_.size(); }
118
119 LiveInterval &getInterval(unsigned reg) {
120 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
121 assert(I != r2iMap_.end() && "Interval does not exist for register");
122 return I->second;
123 }
124
125 const LiveInterval &getInterval(unsigned reg) const {
126 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
127 assert(I != r2iMap_.end() && "Interval does not exist for register");
128 return I->second;
129 }
130
Evan Chengb371f452007-02-19 21:49:54 +0000131 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000132 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000133 }
134
Chris Lattner428b92e2006-09-15 03:57:23 +0000135 /// getMBBStartIdx - Return the base index of the first instruction in the
136 /// specified MachineBasicBlock.
137 unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
138 return getMBBStartIdx(MBB->getNumber());
139 }
Chris Lattner428b92e2006-09-15 03:57:23 +0000140 unsigned getMBBStartIdx(unsigned MBBNo) const {
141 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
Evan Cheng549f27d32007-08-13 23:45:17 +0000142 return MBB2IdxMap[MBBNo].first;
143 }
144
145 /// getMBBEndIdx - Return the store index of the last instruction in the
146 /// specified MachineBasicBlock.
147 unsigned getMBBEndIdx(MachineBasicBlock *MBB) const {
148 return getMBBEndIdx(MBB->getNumber());
149 }
150 unsigned getMBBEndIdx(unsigned MBBNo) const {
151 assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
152 return MBB2IdxMap[MBBNo].second;
Chris Lattner428b92e2006-09-15 03:57:23 +0000153 }
154
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000155 /// getInstructionIndex - returns the base index of instr
156 unsigned getInstructionIndex(MachineInstr* instr) const {
157 Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
158 assert(it != mi2iMap_.end() && "Invalid instruction!");
159 return it->second;
160 }
161
162 /// getInstructionFromIndex - given an index in any slot of an
163 /// instruction return a pointer the instruction
164 MachineInstr* getInstructionFromIndex(unsigned index) const {
165 index /= InstrSlots::NUM; // convert index to vector index
166 assert(index < i2miMap_.size() &&
167 "index does not correspond to an instruction");
168 return i2miMap_[index];
169 }
David Greene25133302007-06-08 17:18:56 +0000170
Evan Chengc92da382007-11-03 07:20:12 +0000171 /// conflictsWithPhysRegDef - Returns true if the specified register
172 /// is defined during the duration of the specified interval.
173 bool conflictsWithPhysRegDef(const LiveInterval &li, VirtRegMap &vrm,
174 unsigned reg);
175
Evan Cheng4ca980e2007-10-17 02:10:22 +0000176 /// findLiveInMBBs - Given a live range, if the value of the range
177 /// is live in any MBB returns true as well as the list of basic blocks
178 /// where the value is live in.
179 bool findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000180 SmallVectorImpl<MachineBasicBlock*> &MBBs) const;
Evan Cheng4ca980e2007-10-17 02:10:22 +0000181
David Greene25133302007-06-08 17:18:56 +0000182 // Interval creation
183
184 LiveInterval &getOrCreateInterval(unsigned reg) {
185 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
186 if (I == r2iMap_.end())
187 I = r2iMap_.insert(I, std::make_pair(reg, createInterval(reg)));
188 return I->second;
189 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000190
David Greene25133302007-06-08 17:18:56 +0000191 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000192
David Greene25133302007-06-08 17:18:56 +0000193 void removeInterval(unsigned Reg) {
194 r2iMap_.erase(Reg);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000195 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000196
Evan Cheng30cac022007-02-22 23:03:39 +0000197 /// isRemoved - returns true if the specified machine instr has been
198 /// removed.
199 bool isRemoved(MachineInstr* instr) const {
Evan Cheng7d35c0e2007-02-22 23:52:23 +0000200 return !mi2iMap_.count(instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000201 }
202
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000203 /// RemoveMachineInstrFromMaps - This marks the specified machine instr as
204 /// deleted.
205 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
206 // remove index -> MachineInstr and
207 // MachineInstr -> index mappings
208 Mi2IndexMap::iterator mi2i = mi2iMap_.find(MI);
209 if (mi2i != mi2iMap_.end()) {
210 i2miMap_[mi2i->second/InstrSlots::NUM] = 0;
211 mi2iMap_.erase(mi2i);
212 }
213 }
David Greene25133302007-06-08 17:18:56 +0000214
Evan Chengf3bb2e62007-09-05 21:46:51 +0000215 BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
216
David Greene25133302007-06-08 17:18:56 +0000217 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
218 virtual void releaseMemory();
219
220 /// runOnMachineFunction - pass entry point
221 virtual bool runOnMachineFunction(MachineFunction&);
222
223 /// print - Implement the dump method.
224 virtual void print(std::ostream &O, const Module* = 0) const;
225 void print(std::ostream *O, const Module* M = 0) const {
226 if (O) print(*O, M);
227 }
228
Evan Chengf2fbca62007-11-12 06:35:08 +0000229 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
230 /// the given interval.
231 std::vector<LiveInterval*>
232 addIntervalsForSpills(const LiveInterval& i, VirtRegMap& vrm);
233
David Greene25133302007-06-08 17:18:56 +0000234 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000235 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000236 void computeIntervals();
Chris Lattner6bda49f2006-09-02 05:26:01 +0000237
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 /// handleRegisterDef - update intervals for a register def
239 /// (calls handlePhysicalRegisterDef and
240 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000241 void handleRegisterDef(MachineBasicBlock *MBB,
242 MachineBasicBlock::iterator MI, unsigned MIIdx,
Chris Lattnerf38a05d2006-01-29 07:59:37 +0000243 unsigned reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000244
245 /// handleVirtualRegisterDef - update intervals for a virtual
246 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000247 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
248 MachineBasicBlock::iterator MI,
249 unsigned MIIdx,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 LiveInterval& interval);
251
Chris Lattnerf768bba2005-03-09 23:05:19 +0000252 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000253 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
255 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000256 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000257 LiveInterval &interval,
258 unsigned SrcReg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259
Evan Chengb371f452007-02-19 21:49:54 +0000260 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000261 void handleLiveInRegister(MachineBasicBlock* mbb,
262 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000263 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000264
Evan Cheng549f27d32007-08-13 23:45:17 +0000265 /// isReMaterializable - Returns true if the definition MI of the specified
266 /// val# of the specified interval is re-materializable.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000267 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Cheng549f27d32007-08-13 23:45:17 +0000268 MachineInstr *MI);
269
Evan Cheng35b35c52007-08-30 05:52:20 +0000270 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
271 /// slot / to reg or any rematerialized load into ith operand of specified
272 /// MI. If it is successul, MI is updated with the newly created MI and
273 /// returns true.
274 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Evan Cheng32dfbea2007-10-12 08:50:34 +0000275 MachineInstr *DefMI, unsigned index, unsigned i,
276 bool isSS, int slot, unsigned reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000277
Evan Chengf2fbca62007-11-12 06:35:08 +0000278 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
279 /// for addIntervalsForSpills to rewrite uses / defs for the given live range.
280 void rewriteInstructionForSpills(const LiveInterval &li,
281 unsigned id, unsigned index, unsigned end, MachineInstr *MI,
282 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
283 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
284 VirtRegMap &vrm, SSARegMap *RegMap, const TargetRegisterClass* rc,
285 SmallVector<int, 4> &ReMatIds,
286 std::vector<LiveInterval*> &NewLIs);
287 void rewriteInstructionsForSpills(const LiveInterval &li,
288 LiveInterval::Ranges::const_iterator &I,
289 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
290 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
291 VirtRegMap &vrm, SSARegMap *RegMap, const TargetRegisterClass* rc,
292 SmallVector<int, 4> &ReMatIds,
293 std::vector<LiveInterval*> &NewLIs);
294
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 static LiveInterval createInterval(unsigned Reg);
296
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 void printRegName(unsigned reg) const;
298 };
299
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000300} // End llvm namespace
301
302#endif