blob: e4fc062dc7bc3f9e0631dfb3fced573b335b849e [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//
Chris Lattner7ed47a12007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
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
Dan Gohman8131a502008-03-13 23:04:27 +000013// instruction with number j' > j such that v is live at j' and there is no
Chris Lattner6b929062004-07-19 02:13:59 +000014// 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
David Greenede0cc5a2009-08-19 20:52:54 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner779a6512005-09-21 04:18:25 +000025#include "llvm/CodeGen/LiveInterval.h"
Lang Hames233a60e2009-11-03 23:52:08 +000026#include "llvm/CodeGen/SlotIndexes.h"
Evan Cheng61de82d2007-02-15 05:59:24 +000027#include "llvm/ADT/BitVector.h"
Evan Cheng20b0abc2007-04-17 20:32:26 +000028#include "llvm/ADT/DenseMap.h"
Evan Cheng8f90b6e2009-01-07 02:08:57 +000029#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng549f27d32007-08-13 23:45:17 +000030#include "llvm/ADT/SmallVector.h"
Evan Chengf3bb2e62007-09-05 21:46:51 +000031#include "llvm/Support/Allocator.h"
Hartmut Kaiserffb15de2007-11-13 23:04:28 +000032#include <cmath>
Lang Hames233a60e2009-11-03 23:52:08 +000033#include <iterator>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000034
35namespace llvm {
36
Dan Gohman6d69ba82008-07-25 00:02:30 +000037 class AliasAnalysis;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000038 class LiveVariables;
Evan Cheng22f07ff2007-12-11 02:09:15 +000039 class MachineLoopInfo;
Dan Gohman6f0d0242008-02-10 18:45:23 +000040 class TargetRegisterInfo;
Chris Lattner84bc5422007-12-31 04:13:23 +000041 class MachineRegisterInfo;
Chris Lattnerf768bba2005-03-09 23:05:19 +000042 class TargetInstrInfo;
Evan Cheng20b0abc2007-04-17 20:32:26 +000043 class TargetRegisterClass;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000044 class VirtRegMap;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +000045
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000046 class LiveIntervals : public MachineFunctionPass {
47 MachineFunction* mf_;
Evan Chengd70dbb52008-02-22 09:24:50 +000048 MachineRegisterInfo* mri_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000049 const TargetMachine* tm_;
Dan Gohman6f0d0242008-02-10 18:45:23 +000050 const TargetRegisterInfo* tri_;
Chris Lattnerf768bba2005-03-09 23:05:19 +000051 const TargetInstrInfo* tii_;
Dan Gohman6d69ba82008-07-25 00:02:30 +000052 AliasAnalysis *aa_;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000053 LiveVariables* lv_;
Lang Hames233a60e2009-11-03 23:52:08 +000054 SlotIndexes* indexes_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Evan Chengf3bb2e62007-09-05 21:46:51 +000056 /// Special pool allocator for VNInfo's (LiveInterval val#).
57 ///
Benjamin Kramer991de142010-03-30 20:16:45 +000058 VNInfo::Allocator VNInfoAllocator;
Evan Chengf3bb2e62007-09-05 21:46:51 +000059
Owen Anderson20e28392008-08-13 22:08:30 +000060 typedef DenseMap<unsigned, LiveInterval*> Reg2IntervalMap;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 Reg2IntervalMap r2iMap_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062
Evan Cheng752195e2009-09-14 21:33:42 +000063 /// allocatableRegs_ - A bit vector of allocatable registers.
Evan Cheng61de82d2007-02-15 05:59:24 +000064 BitVector allocatableRegs_;
Evan Cheng88d1f582007-03-01 02:03:03 +000065
Evan Cheng752195e2009-09-14 21:33:42 +000066 /// CloneMIs - A list of clones as result of re-materialization.
67 std::vector<MachineInstr*> CloneMIs;
Evan Cheng549f27d32007-08-13 23:45:17 +000068
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000070 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000071 LiveIntervals() : MachineFunctionPass(ID) {
72 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
73 }
Devang Patel794fd752007-05-01 21:15:47 +000074
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +000075 // Calculate the spill weight to assign to a single instruction.
76 static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +000077
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +000078 // After summing the spill weights of all defs and uses, the final weight
79 // should be normalized, dividing the weight of the interval by its size.
80 // This encourages spilling of intervals that are large and have few uses,
81 // and discourages spilling of small intervals with many uses.
82 void normalizeSpillWeight(LiveInterval &li) {
83 li.weight /= getApproximateInstructionCount(li) + 25;
84 }
85
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 typedef Reg2IntervalMap::iterator iterator;
Chris Lattner70ca3582004-09-30 15:59:17 +000087 typedef Reg2IntervalMap::const_iterator const_iterator;
88 const_iterator begin() const { return r2iMap_.begin(); }
89 const_iterator end() const { return r2iMap_.end(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 iterator begin() { return r2iMap_.begin(); }
91 iterator end() { return r2iMap_.end(); }
Evan Cheng34cd4a42008-05-05 18:30:58 +000092 unsigned getNumIntervals() const { return (unsigned)r2iMap_.size(); }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000093
94 LiveInterval &getInterval(unsigned reg) {
95 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
96 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +000097 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 }
99
100 const LiveInterval &getInterval(unsigned reg) const {
101 Reg2IntervalMap::const_iterator I = r2iMap_.find(reg);
102 assert(I != r2iMap_.end() && "Interval does not exist for register");
Owen Anderson03857b22008-08-13 21:49:13 +0000103 return *I->second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000104 }
105
Evan Chengb371f452007-02-19 21:49:54 +0000106 bool hasInterval(unsigned reg) const {
Evan Cheng88d1f582007-03-01 02:03:03 +0000107 return r2iMap_.count(reg);
Evan Chengb371f452007-02-19 21:49:54 +0000108 }
109
Jakob Stoklund Olesendf30cf92010-08-10 00:02:26 +0000110 /// isAllocatable - is the physical register reg allocatable in the current
111 /// function?
112 bool isAllocatable(unsigned reg) const {
113 return allocatableRegs_.test(reg);
114 }
115
Owen Andersona1566f22008-07-22 22:46:49 +0000116 /// getScaledIntervalSize - get the size of an interval in "units,"
Owen Anderson72e04092008-06-23 23:25:37 +0000117 /// where every function is composed of one thousand units. This
118 /// measure scales properly with empty index slots in the function.
Owen Andersona1566f22008-07-22 22:46:49 +0000119 double getScaledIntervalSize(LiveInterval& I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000120 return (1000.0 * I.getSize()) / indexes_->getIndexesLength();
Owen Andersona1566f22008-07-22 22:46:49 +0000121 }
Evan Cheng30fdb5c2010-04-21 00:44:22 +0000122
123 /// getFuncInstructionCount - Return the number of instructions in the
124 /// current function.
125 unsigned getFuncInstructionCount() {
126 return indexes_->getFunctionSize();
127 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000128
Owen Andersona1566f22008-07-22 22:46:49 +0000129 /// getApproximateInstructionCount - computes an estimate of the number
130 /// of instructions in a given LiveInterval.
131 unsigned getApproximateInstructionCount(LiveInterval& I) {
132 double IntervalPercentage = getScaledIntervalSize(I) / 1000.0;
Lang Hames233a60e2009-11-03 23:52:08 +0000133 return (unsigned)(IntervalPercentage * indexes_->getFunctionSize());
Evan Chengf5cd4f02008-10-23 20:43:13 +0000134 }
135
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000136 /// conflictsWithPhysReg - Returns true if the specified register is used or
137 /// defined during the duration of the specified interval. Copies to and
138 /// from li.reg are allowed. This method is only able to analyze simple
139 /// ranges that stay within a single basic block. Anything else is
140 /// considered a conflict.
141 bool conflictsWithPhysReg(const LiveInterval &li, VirtRegMap &vrm,
142 unsigned reg);
Evan Chengc92da382007-11-03 07:20:12 +0000143
Jakob Stoklund Olesena24986d2010-06-24 18:15:01 +0000144 /// conflictsWithAliasRef - Similar to conflictsWithPhysRegRef except
145 /// it checks for alias uses and defs.
146 bool conflictsWithAliasRef(LiveInterval &li, unsigned Reg,
Evan Cheng826cbac2010-03-11 08:20:21 +0000147 SmallPtrSet<MachineInstr*,32> &JoinedCopies);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000148
David Greene25133302007-06-08 17:18:56 +0000149 // Interval creation
David Greene25133302007-06-08 17:18:56 +0000150 LiveInterval &getOrCreateInterval(unsigned reg) {
151 Reg2IntervalMap::iterator I = r2iMap_.find(reg);
152 if (I == r2iMap_.end())
Owen Anderson20e28392008-08-13 22:08:30 +0000153 I = r2iMap_.insert(std::make_pair(reg, createInterval(reg))).first;
Owen Anderson03857b22008-08-13 21:49:13 +0000154 return *I->second;
David Greene25133302007-06-08 17:18:56 +0000155 }
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000156
157 /// dupInterval - Duplicate a live interval. The caller is responsible for
158 /// managing the allocated memory.
159 LiveInterval *dupInterval(LiveInterval *li);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000160
Owen Andersonc4dc1322008-06-05 17:15:43 +0000161 /// addLiveRangeToEndOfBlock - Given a register and an instruction,
162 /// adds a live range from that instruction to the end of its MBB.
163 LiveRange addLiveRangeToEndOfBlock(unsigned reg,
Lang Hames86511252009-09-04 20:41:11 +0000164 MachineInstr* startInst);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000165
David Greene25133302007-06-08 17:18:56 +0000166 // Interval removal
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000167
David Greene25133302007-06-08 17:18:56 +0000168 void removeInterval(unsigned Reg) {
Owen Anderson20e28392008-08-13 22:08:30 +0000169 DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.find(Reg);
Owen Anderson03857b22008-08-13 21:49:13 +0000170 delete I->second;
171 r2iMap_.erase(I);
Bill Wendling5c7e3262006-12-17 05:15:13 +0000172 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000173
Jakob Stoklund Olesen7fd747b2011-01-12 22:28:48 +0000174 SlotIndexes *getSlotIndexes() const {
175 return indexes_;
176 }
177
Lang Hames233a60e2009-11-03 23:52:08 +0000178 SlotIndex getZeroIndex() const {
179 return indexes_->getZeroIndex();
180 }
181
182 SlotIndex getInvalidIndex() const {
183 return indexes_->getInvalidIndex();
184 }
185
Evan Cheng5b69eba2009-04-21 22:46:52 +0000186 /// isNotInMIMap - returns true if the specified machine instr has been
187 /// removed or was never entered in the map.
Lang Hames233a60e2009-11-03 23:52:08 +0000188 bool isNotInMIMap(const MachineInstr* Instr) const {
189 return !indexes_->hasIndex(Instr);
Evan Cheng30cac022007-02-22 23:03:39 +0000190 }
191
Lang Hames233a60e2009-11-03 23:52:08 +0000192 /// Returns the base index of the given instruction.
193 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
194 return indexes_->getInstructionIndex(instr);
195 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000196
Lang Hames233a60e2009-11-03 23:52:08 +0000197 /// Returns the instruction associated with the given index.
198 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
199 return indexes_->getInstructionFromIndex(index);
200 }
201
202 /// Return the first index in the given basic block.
203 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
204 return indexes_->getMBBStartIdx(mbb);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000205 }
Lang Hames233a60e2009-11-03 23:52:08 +0000206
207 /// Return the last index in the given basic block.
208 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
209 return indexes_->getMBBEndIdx(mbb);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000210 }
Lang Hames233a60e2009-11-03 23:52:08 +0000211
Lang Hames60f422f2010-07-17 07:34:01 +0000212 bool isLiveInToMBB(const LiveInterval &li,
213 const MachineBasicBlock *mbb) const {
214 return li.liveAt(getMBBStartIdx(mbb));
215 }
216
217 LiveRange* findEnteringRange(LiveInterval &li,
218 const MachineBasicBlock *mbb) {
219 return li.getLiveRangeContaining(getMBBStartIdx(mbb));
220 }
221
222 bool isLiveOutOfMBB(const LiveInterval &li,
223 const MachineBasicBlock *mbb) const {
224 return li.liveAt(getMBBEndIdx(mbb).getPrevSlot());
225 }
226
227 LiveRange* findExitingRange(LiveInterval &li,
228 const MachineBasicBlock *mbb) {
229 return li.getLiveRangeContaining(getMBBEndIdx(mbb).getPrevSlot());
230 }
231
Lang Hames233a60e2009-11-03 23:52:08 +0000232 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
233 return indexes_->getMBBFromIndex(index);
234 }
235
Lang Hamesb3661582009-11-14 00:02:51 +0000236 SlotIndex InsertMachineInstrInMaps(MachineInstr *MI) {
237 return indexes_->insertMachineInstrInMaps(MI);
Lang Hames233a60e2009-11-03 23:52:08 +0000238 }
239
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000240 void RemoveMachineInstrFromMaps(MachineInstr *MI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000241 indexes_->removeMachineInstrFromMaps(MI);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000242 }
David Greene25133302007-06-08 17:18:56 +0000243
Evan Cheng70071432008-02-13 03:01:43 +0000244 void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
Lang Hames233a60e2009-11-03 23:52:08 +0000245 indexes_->replaceMachineInstrInMaps(MI, NewMI);
246 }
247
Lang Hames60f422f2010-07-17 07:34:01 +0000248 void InsertMBBInMaps(MachineBasicBlock *MBB) {
249 indexes_->insertMBBInMaps(MBB);
250 }
251
Lang Hames233a60e2009-11-03 23:52:08 +0000252 bool findLiveInMBBs(SlotIndex Start, SlotIndex End,
253 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
254 return indexes_->findLiveInMBBs(Start, End, MBBs);
255 }
256
257 void renumber() {
Lang Hamesb3661582009-11-14 00:02:51 +0000258 indexes_->renumberIndexes();
Evan Cheng70071432008-02-13 03:01:43 +0000259 }
260
Benjamin Kramer991de142010-03-30 20:16:45 +0000261 VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; }
Evan Chengf3bb2e62007-09-05 21:46:51 +0000262
David Greene25133302007-06-08 17:18:56 +0000263 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
264 virtual void releaseMemory();
265
266 /// runOnMachineFunction - pass entry point
267 virtual bool runOnMachineFunction(MachineFunction&);
268
269 /// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000270 virtual void print(raw_ostream &O, const Module* = 0) const;
David Greene25133302007-06-08 17:18:56 +0000271
Evan Chengf2fbca62007-11-12 06:35:08 +0000272 /// addIntervalsForSpills - Create new intervals for spilled defs / uses of
Evan Cheng9c3c2212008-06-06 07:54:39 +0000273 /// the given interval. FIXME: It also returns the weight of the spill slot
274 /// (if any is created) by reference. This is temporary.
Evan Chengf2fbca62007-11-12 06:35:08 +0000275 std::vector<LiveInterval*>
Evan Cheng81a03822007-11-17 00:40:40 +0000276 addIntervalsForSpills(const LiveInterval& i,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000277 const SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +0000278 const MachineLoopInfo *loopInfo, VirtRegMap& vrm);
Evan Chengf2fbca62007-11-12 06:35:08 +0000279
Evan Cheng676dd7c2008-03-11 07:19:34 +0000280 /// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +0000281 /// around all defs and uses of the specified interval. Return true if it
282 /// was able to cut its interval.
283 bool spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +0000284 unsigned PhysReg, VirtRegMap &vrm);
285
Evan Cheng5ef3a042007-12-06 00:01:56 +0000286 /// isReMaterializable - Returns true if every definition of MI of every
287 /// val# of the specified interval is re-materializable. Also returns true
288 /// by reference if all of the defs are load instructions.
Evan Chengdc377862008-09-30 15:44:16 +0000289 bool isReMaterializable(const LiveInterval &li,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000290 const SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengdc377862008-09-30 15:44:16 +0000291 bool &isLoad);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000292
Evan Cheng06587492008-10-24 02:05:00 +0000293 /// isReMaterializable - Returns true if the definition MI of the specified
294 /// val# of the specified interval is re-materializable.
295 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
296 MachineInstr *MI);
297
Evan Cheng676dd7c2008-03-11 07:19:34 +0000298 /// getRepresentativeReg - Find the largest super register of the specified
299 /// physical register.
300 unsigned getRepresentativeReg(unsigned Reg) const;
301
302 /// getNumConflictsWithPhysReg - Return the number of uses and defs of the
303 /// specified interval that conflicts with the specified physical register.
304 unsigned getNumConflictsWithPhysReg(const LiveInterval &li,
305 unsigned PhysReg) const;
306
Owen Anderson0c2e7b92009-01-13 06:05:10 +0000307 /// intervalIsInOneMBB - Returns true if the specified interval is entirely
308 /// within a single basic block.
309 bool intervalIsInOneMBB(const LiveInterval &li) const;
310
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000311 private:
Chris Lattner428b92e2006-09-15 03:57:23 +0000312 /// computeIntervals - Compute live intervals.
Chris Lattnerc7695eb2006-09-14 06:42:17 +0000313 void computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000314
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000315 /// handleRegisterDef - update intervals for a register def
316 /// (calls handlePhysicalRegisterDef and
317 /// handleVirtualRegisterDef)
Chris Lattner6b128bd2006-09-03 08:07:11 +0000318 void handleRegisterDef(MachineBasicBlock *MBB,
Lang Hames86511252009-09-04 20:41:11 +0000319 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000320 SlotIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000321 MachineOperand& MO, unsigned MOIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000322
Evan Cheng37499432010-05-05 18:27:40 +0000323 /// isPartialRedef - Return true if the specified def at the specific index
324 /// is partially re-defining the specified live interval. A common case of
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000325 /// this is a definition of the sub-register.
Evan Cheng37499432010-05-05 18:27:40 +0000326 bool isPartialRedef(SlotIndex MIIdx, MachineOperand &MO,
327 LiveInterval &interval);
328
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000329 /// handleVirtualRegisterDef - update intervals for a virtual
330 /// register def
Chris Lattner6b128bd2006-09-03 08:07:11 +0000331 void handleVirtualRegisterDef(MachineBasicBlock *MBB,
332 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000333 SlotIndex MIIdx, MachineOperand& MO,
Lang Hames86511252009-09-04 20:41:11 +0000334 unsigned MOIdx,
335 LiveInterval& interval);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336
Chris Lattnerf768bba2005-03-09 23:05:19 +0000337 /// handlePhysicalRegisterDef - update intervals for a physical register
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000338 /// def.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339 void handlePhysicalRegisterDef(MachineBasicBlock* mbb,
340 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000341 SlotIndex MIIdx, MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000342 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000343 MachineInstr *CopyMI);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344
Evan Chengb371f452007-02-19 21:49:54 +0000345 /// handleLiveInRegister - Create interval for a livein register.
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000346 void handleLiveInRegister(MachineBasicBlock* mbb,
Lang Hames233a60e2009-11-03 23:52:08 +0000347 SlotIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000348 LiveInterval &interval, bool isAlias = false);
Evan Chengb371f452007-02-19 21:49:54 +0000349
Evan Chengd70dbb52008-02-22 09:24:50 +0000350 /// getReMatImplicitUse - If the remat definition MI has one (for now, we
351 /// only allow one) virtual register operand, then its uses are implicitly
352 /// using the register. Returns the virtual register.
353 unsigned getReMatImplicitUse(const LiveInterval &li,
354 MachineInstr *MI) const;
355
356 /// isValNoAvailableAt - Return true if the val# of the specified interval
357 /// which reaches the given instruction also reaches the specified use
358 /// index.
359 bool isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000360 SlotIndex UseIdx) const;
Evan Chengd70dbb52008-02-22 09:24:50 +0000361
Evan Cheng549f27d32007-08-13 23:45:17 +0000362 /// isReMaterializable - Returns true if the definition MI of the specified
Evan Cheng5ef3a042007-12-06 00:01:56 +0000363 /// val# of the specified interval is re-materializable. Also returns true
364 /// by reference if the def is a load.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000365 bool isReMaterializable(const LiveInterval &li, const VNInfo *ValNo,
Evan Chengdc377862008-09-30 15:44:16 +0000366 MachineInstr *MI,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000367 const SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengdc377862008-09-30 15:44:16 +0000368 bool &isLoad);
Evan Cheng549f27d32007-08-13 23:45:17 +0000369
Evan Cheng35b35c52007-08-30 05:52:20 +0000370 /// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
371 /// slot / to reg or any rematerialized load into ith operand of specified
372 /// MI. If it is successul, MI is updated with the newly created MI and
373 /// returns true.
374 bool tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
Lang Hames233a60e2009-11-03 23:52:08 +0000375 MachineInstr *DefMI, SlotIndex InstrIdx,
Evan Chengaee4af62007-12-02 08:30:39 +0000376 SmallVector<unsigned, 2> &Ops,
Lang Hames86511252009-09-04 20:41:11 +0000377 bool isSS, int FrameIndex, unsigned Reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000378
Evan Chengd70dbb52008-02-22 09:24:50 +0000379 /// canFoldMemoryOperand - Return true if the specified load / store
Evan Cheng018f9b02007-12-05 03:22:34 +0000380 /// folding is possible.
Evan Chengd64b5c82007-12-05 03:14:33 +0000381 bool canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000382 SmallVector<unsigned, 2> &Ops,
383 bool ReMatLoadSS) const;
Evan Chengd64b5c82007-12-05 03:14:33 +0000384
Evan Cheng0cbb1162007-11-29 01:06:25 +0000385 /// anyKillInMBBAfterIdx - Returns true if there is a kill of the specified
386 /// VNInfo that's after the specified index but is within the basic block.
387 bool anyKillInMBBAfterIdx(const LiveInterval &li, const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +0000388 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000389 SlotIndex Idx) const;
Evan Cheng81a03822007-11-17 00:40:40 +0000390
Evan Cheng676dd7c2008-03-11 07:19:34 +0000391 /// hasAllocatableSuperReg - Return true if the specified physical register
392 /// has any super register that's allocatable.
393 bool hasAllocatableSuperReg(unsigned Reg) const;
394
Evan Cheng1953d0c2007-11-29 10:12:14 +0000395 /// SRInfo - Spill / restore info.
396 struct SRInfo {
Lang Hames233a60e2009-11-03 23:52:08 +0000397 SlotIndex index;
Evan Cheng1953d0c2007-11-29 10:12:14 +0000398 unsigned vreg;
399 bool canFold;
Lang Hames233a60e2009-11-03 23:52:08 +0000400 SRInfo(SlotIndex i, unsigned vr, bool f)
Lang Hames86511252009-09-04 20:41:11 +0000401 : index(i), vreg(vr), canFold(f) {}
Evan Cheng1953d0c2007-11-29 10:12:14 +0000402 };
403
Lang Hames233a60e2009-11-03 23:52:08 +0000404 bool alsoFoldARestore(int Id, SlotIndex index, unsigned vr,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000405 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000406 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +0000407 void eraseRestoreInfo(int Id, SlotIndex index, unsigned vr,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000408 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000409 DenseMap<unsigned,std::vector<SRInfo> >&RestoreIdxes);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000410
Evan Cheng4cce6b42008-04-11 17:53:36 +0000411 /// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
412 /// spilled and create empty intervals for their uses.
413 void handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
414 const TargetRegisterClass* rc,
415 std::vector<LiveInterval*> &NewLIs);
Evan Cheng419852c2008-04-03 16:39:43 +0000416
Evan Chengd70dbb52008-02-22 09:24:50 +0000417 /// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
418 /// interval on to-be re-materialized operands of MI) with new register.
419 void rewriteImplicitOps(const LiveInterval &li,
420 MachineInstr *MI, unsigned NewVReg, VirtRegMap &vrm);
421
Chris Lattner84bc5422007-12-31 04:13:23 +0000422 /// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper
423 /// functions for addIntervalsForSpills to rewrite uses / defs for the given
424 /// live range.
Evan Chengd70dbb52008-02-22 09:24:50 +0000425 bool rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames233a60e2009-11-03 23:52:08 +0000426 bool TrySplit, SlotIndex index, SlotIndex end,
Lang Hames86511252009-09-04 20:41:11 +0000427 MachineInstr *MI, MachineInstr *OrigDefMI, MachineInstr *DefMI,
428 unsigned Slot, int LdSlot,
Evan Chengf2fbca62007-11-12 06:35:08 +0000429 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000430 VirtRegMap &vrm, const TargetRegisterClass* rc,
431 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng0cc83b62008-02-23 00:46:11 +0000432 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +0000433 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000434 std::vector<LiveInterval*> &NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +0000435 void rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000436 LiveInterval::Ranges::const_iterator &I,
437 MachineInstr *OrigDefMI, MachineInstr *DefMI, unsigned Slot, int LdSlot,
438 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000439 VirtRegMap &vrm, const TargetRegisterClass* rc,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000440 SmallVector<int, 4> &ReMatIds, const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000441 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000442 DenseMap<unsigned,std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000443 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +0000444 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes,
445 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +0000446 std::vector<LiveInterval*> &NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +0000447
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +0000448 // Normalize the spill weight of all the intervals in NewLIs.
449 void normalizeSpillWeights(std::vector<LiveInterval*> &NewLIs);
450
Owen Anderson03857b22008-08-13 21:49:13 +0000451 static LiveInterval* createInterval(unsigned Reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000452
Evan Cheng752195e2009-09-14 21:33:42 +0000453 void printInstrs(raw_ostream &O) const;
454 void dumpInstrs() const;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000455 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000456} // End llvm namespace
457
458#endif