blob: 839817cf039a9d2139de4de19a1c7ef1f37f50e2 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +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//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Dan Gohman844731a2008-05-13 00:00:25 +000052static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
53 cl::init(true), cl::Hidden);
54static cl::opt<int> SplitLimit("split-limit",
55 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000056
Dan Gohman4c8f8702008-07-25 15:08:37 +000057static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
58
Owen Andersonae339ba2008-08-19 00:17:30 +000059static cl::opt<bool> EnableFastSpilling("fast-spill",
60 cl::init(false), cl::Hidden);
61
Chris Lattnercd3245a2006-12-19 22:41:21 +000062STATISTIC(numIntervals, "Number of original intervals");
Evan Cheng0cbb1162007-11-29 01:06:25 +000063STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000065
Devang Patel19974732007-05-03 01:11:54 +000066char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000067static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Chris Lattnerf7da2c72006-08-24 22:43:55 +000069void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000070 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000071 AU.addRequired<AliasAnalysis>();
72 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000073 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000074 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000075 AU.addPreservedID(MachineLoopInfoID);
76 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000077
78 if (!StrongPHIElim) {
79 AU.addPreservedID(PHIEliminationID);
80 AU.addRequiredID(PHIEliminationID);
81 }
82
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085}
86
Chris Lattnerf7da2c72006-08-24 22:43:55 +000087void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000088 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000089 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000090 E = r2iMap_.end(); I != E; ++I)
91 delete I->second;
92
Evan Cheng3f32d652008-06-04 09:18:41 +000093 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000094 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 mi2iMap_.clear();
96 i2miMap_.clear();
97 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000098 terminatorGaps.clear();
99
Evan Chengdd199d22007-09-06 01:07:24 +0000100 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
101 VNInfoAllocator.Reset();
Evan Cheng1ed99222008-07-19 00:37:25 +0000102 while (!ClonedMIs.empty()) {
103 MachineInstr *MI = ClonedMIs.back();
104 ClonedMIs.pop_back();
105 mf_->DeleteMachineInstr(MI);
106 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000107}
108
Evan Cheng6ade93b2009-08-05 03:53:14 +0000109static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
110 const TargetInstrInfo *tii_) {
111 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
112 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
113 Reg == SrcReg)
114 return true;
115
116 if ((MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
117 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) &&
118 MI->getOperand(2).getReg() == Reg)
119 return true;
120 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG &&
121 MI->getOperand(1).getReg() == Reg)
122 return true;
123 return false;
124}
125
Evan Cheng2578ba22009-07-01 01:59:31 +0000126/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
127/// there is one implicit_def for each use. Add isUndef marker to
128/// implicit_def defs and their uses.
129void LiveIntervals::processImplicitDefs() {
130 SmallSet<unsigned, 8> ImpDefRegs;
131 SmallVector<MachineInstr*, 8> ImpDefMIs;
132 MachineBasicBlock *Entry = mf_->begin();
133 SmallPtrSet<MachineBasicBlock*,16> Visited;
134 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
135 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
136 DFI != E; ++DFI) {
137 MachineBasicBlock *MBB = *DFI;
138 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
139 I != E; ) {
140 MachineInstr *MI = &*I;
141 ++I;
142 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
143 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000144 ImpDefRegs.insert(Reg);
145 ImpDefMIs.push_back(MI);
146 continue;
147 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000148
149 bool ChangedToImpDef = false;
150 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000151 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000152 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000153 continue;
154 unsigned Reg = MO.getReg();
155 if (!Reg)
156 continue;
157 if (!ImpDefRegs.count(Reg))
158 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000159 // Use is a copy, just turn it into an implicit_def.
Evan Cheng6ade93b2009-08-05 03:53:14 +0000160 if (CanTurnIntoImplicitDef(MI, Reg, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000161 bool isKill = MO.isKill();
162 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
163 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
164 MI->RemoveOperand(j);
165 if (isKill)
166 ImpDefRegs.erase(Reg);
167 ChangedToImpDef = true;
168 break;
169 }
170
Evan Cheng2578ba22009-07-01 01:59:31 +0000171 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000172 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
173 // Make sure other uses of
174 for (unsigned j = i+1; j != e; ++j) {
175 MachineOperand &MOJ = MI->getOperand(j);
176 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
177 MOJ.setIsUndef();
178 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000179 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000180 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000181 }
182
Evan Cheng459a7c62009-07-01 08:19:36 +0000183 if (ChangedToImpDef) {
184 // Backtrack to process this new implicit_def.
185 --I;
186 } else {
187 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
188 MachineOperand& MO = MI->getOperand(i);
189 if (!MO.isReg() || !MO.isDef())
190 continue;
191 ImpDefRegs.erase(MO.getReg());
192 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000193 }
194 }
195
196 // Any outstanding liveout implicit_def's?
197 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
198 MachineInstr *MI = ImpDefMIs[i];
199 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000200 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
201 !ImpDefRegs.count(Reg)) {
202 // Delete all "local" implicit_def's. That include those which define
203 // physical registers since they cannot be liveout.
204 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000205 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000206 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000207
208 // If there are multiple defs of the same register and at least one
209 // is not an implicit_def, do not insert implicit_def's before the
210 // uses.
211 bool Skip = false;
212 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
213 DE = mri_->def_end(); DI != DE; ++DI) {
214 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
215 Skip = true;
216 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000217 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000218 }
219 if (Skip)
220 continue;
221
Evan Chengd129d732009-07-17 19:43:40 +0000222 // The only implicit_def which we want to keep are those that are live
223 // out of its block.
224 MI->eraseFromParent();
225
Evan Cheng459a7c62009-07-01 08:19:36 +0000226 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
227 UE = mri_->use_end(); UI != UE; ) {
228 MachineOperand &RMO = UI.getOperand();
229 MachineInstr *RMI = &*UI;
230 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000231 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000232 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000233 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000234
235 // Turn a copy use into an implicit_def.
236 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
237 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
238 Reg == SrcReg) {
239 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
240 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
241 RMI->RemoveOperand(j);
242 continue;
243 }
244
Evan Cheng2578ba22009-07-01 01:59:31 +0000245 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
246 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000247 RMO.setReg(NewVReg);
248 RMO.setIsUndef();
249 RMO.setIsKill();
250 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000251 }
252 ImpDefRegs.clear();
253 ImpDefMIs.clear();
254 }
255}
256
Owen Anderson80b3ce62008-05-28 20:54:50 +0000257void LiveIntervals::computeNumbering() {
258 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000259 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000260
261 Idx2MBBMap.clear();
262 MBB2IdxMap.clear();
263 mi2iMap_.clear();
264 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000265 terminatorGaps.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000266
Owen Andersona1566f22008-07-22 22:46:49 +0000267 FunctionSize = 0;
268
Chris Lattner428b92e2006-09-15 03:57:23 +0000269 // Number MachineInstrs and MachineBasicBlocks.
270 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000271 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000272
273 unsigned MIIndex = 0;
274 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
275 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000276 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000277
Owen Anderson7fbad272008-07-23 21:37:49 +0000278 // Insert an empty slot at the beginning of each block.
279 MIIndex += InstrSlots::NUM;
280 i2miMap_.push_back(0);
281
Chris Lattner428b92e2006-09-15 03:57:23 +0000282 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
283 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000284
285 if (I == MBB->getFirstTerminator()) {
286 // Leave a gap for before terminators, this is where we will point
287 // PHI kills.
288 bool inserted =
289 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
290 assert(inserted &&
291 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000292 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000293 i2miMap_.push_back(0);
294
295 MIIndex += InstrSlots::NUM;
296 }
297
Chris Lattner428b92e2006-09-15 03:57:23 +0000298 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000300 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000301 i2miMap_.push_back(I);
302 MIIndex += InstrSlots::NUM;
Owen Andersona1566f22008-07-22 22:46:49 +0000303 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000304
Evan Cheng4ed43292008-10-18 05:21:37 +0000305 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000306 unsigned Slots = I->getDesc().getNumDefs();
307 if (Slots == 0)
308 Slots = 1;
309 MIIndex += InstrSlots::NUM * Slots;
310 while (Slots--)
311 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000312 }
Lang Hamesffd13262009-07-09 03:57:02 +0000313
314 if (MBB->getFirstTerminator() == MBB->end()) {
315 // Leave a gap for before terminators, this is where we will point
316 // PHI kills.
317 bool inserted =
318 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
319 assert(inserted &&
320 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000321 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000322 i2miMap_.push_back(0);
323
324 MIIndex += InstrSlots::NUM;
325 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000326
Owen Anderson1fbb4542008-06-16 16:58:24 +0000327 // Set the MBB2IdxMap entry for this MBB.
328 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
329 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000330 }
Lang Hamesffd13262009-07-09 03:57:02 +0000331
Evan Cheng4ca980e2007-10-17 02:10:22 +0000332 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000333
334 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000335 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000336 for (LiveInterval::iterator LI = OI->second->begin(),
337 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000338
Owen Anderson7eec0c22008-05-29 23:01:22 +0000339 // Remap the start index of the live range to the corresponding new
340 // number, or our best guess at what it _should_ correspond to if the
341 // original instruction has been erased. This is either the following
342 // instruction or its predecessor.
Owen Anderson7fbad272008-07-23 21:37:49 +0000343 unsigned index = LI->start / InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000344 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson0a7615a2008-07-25 23:06:59 +0000345 if (offset == InstrSlots::LOAD) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000346 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000347 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000348 // Take the pair containing the index
349 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000350 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000351
Owen Anderson7fbad272008-07-23 21:37:49 +0000352 LI->start = getMBBStartIdx(J->second);
353 } else {
354 LI->start = mi2iMap_[OldI2MI[index]] + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000355 }
356
357 // Remap the ending index in the same way that we remapped the start,
358 // except for the final step where we always map to the immediately
359 // following instruction.
Owen Andersond7dcbec2008-07-25 19:50:48 +0000360 index = (LI->end - 1) / InstrSlots::NUM;
Owen Anderson7fbad272008-07-23 21:37:49 +0000361 offset = LI->end % InstrSlots::NUM;
Owen Anderson9382b932008-07-30 00:22:56 +0000362 if (offset == InstrSlots::LOAD) {
363 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000364 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000365 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000366 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000367
Owen Anderson9382b932008-07-30 00:22:56 +0000368 LI->end = getMBBEndIdx(I->second) + 1;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000369 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000370 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000371 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
372
373 if (index != OldI2MI.size())
374 LI->end = mi2iMap_[OldI2MI[index]] + (idx == index ? offset : 0);
375 else
376 LI->end = InstrSlots::NUM * i2miMap_.size();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000377 }
Owen Anderson788d0412008-08-06 18:35:45 +0000378 }
379
Owen Anderson03857b22008-08-13 21:49:13 +0000380 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
381 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000382 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000383
Owen Anderson7eec0c22008-05-29 23:01:22 +0000384 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000385 // start indices above. VN's with special sentinel defs
386 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000387 if (vni->isDefAccurate() && !vni->isUnused()) {
Owen Anderson788d0412008-08-06 18:35:45 +0000388 unsigned index = vni->def / InstrSlots::NUM;
389 unsigned offset = vni->def % InstrSlots::NUM;
Owen Anderson91292392008-07-30 17:42:47 +0000390 if (offset == InstrSlots::LOAD) {
391 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000392 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000393 // Take the pair containing the index
394 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000395 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000396
Owen Anderson91292392008-07-30 17:42:47 +0000397 vni->def = getMBBStartIdx(J->second);
398 } else {
399 vni->def = mi2iMap_[OldI2MI[index]] + offset;
400 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000401 }
Owen Anderson745825f42008-05-28 22:40:08 +0000402
Owen Anderson7eec0c22008-05-29 23:01:22 +0000403 // Remap the VNInfo kill indices, which works the same as
404 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000405 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000406 unsigned killIdx = vni->kills[i].killIdx;
407
408 unsigned index = (killIdx - 1) / InstrSlots::NUM;
409 unsigned offset = killIdx % InstrSlots::NUM;
410
Owen Anderson309c6162008-09-30 22:51:54 +0000411 if (offset == InstrSlots::LOAD) {
Lang Hamesffd13262009-07-09 03:57:02 +0000412 assert("Value killed at a load slot.");
413 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000414 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000415 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000416
Lang Hamesffd13262009-07-09 03:57:02 +0000417 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000418 } else {
Lang Hamesffd13262009-07-09 03:57:02 +0000419 if (vni->kills[i].isPHIKill) {
420 std::vector<IdxMBBPair>::const_iterator I =
421 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
422 --I;
423 vni->kills[i].killIdx = terminatorGaps[I->second];
424 } else {
425 assert(OldI2MI[index] != 0 &&
426 "Kill refers to instruction not present in index maps.");
427 vni->kills[i].killIdx = mi2iMap_[OldI2MI[index]] + offset;
428 }
429
430 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000431 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000432 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
433
434 if (index != OldI2MI.size())
435 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
436 (idx == index ? offset : 0);
437 else
438 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000439 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000440 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000441 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000442 }
Owen Anderson788d0412008-08-06 18:35:45 +0000443 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000444}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000445
Lang Hamesf41538d2009-06-02 16:53:25 +0000446void LiveIntervals::scaleNumbering(int factor) {
447 // Need to
448 // * scale MBB begin and end points
449 // * scale all ranges.
450 // * Update VNI structures.
451 // * Scale instruction numberings
452
453 // Scale the MBB indices.
454 Idx2MBBMap.clear();
455 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
456 MBB != MBBE; ++MBB) {
457 std::pair<unsigned, unsigned> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
458 mbbIndices.first = InstrSlots::scale(mbbIndices.first, factor);
459 mbbIndices.second = InstrSlots::scale(mbbIndices.second, factor);
460 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
461 }
462 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
463
Lang Hamesffd13262009-07-09 03:57:02 +0000464 // Scale terminator gaps.
465 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
466 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
467 TGI != TGE; ++TGI) {
468 terminatorGaps[TGI->first] = InstrSlots::scale(TGI->second, factor);
469 }
470
Lang Hamesf41538d2009-06-02 16:53:25 +0000471 // Scale the intervals.
472 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
473 LI->second->scaleNumbering(factor);
474 }
475
476 // Scale MachineInstrs.
477 Mi2IndexMap oldmi2iMap = mi2iMap_;
478 unsigned highestSlot = 0;
479 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
480 MI != ME; ++MI) {
481 unsigned newSlot = InstrSlots::scale(MI->second, factor);
482 mi2iMap_[MI->first] = newSlot;
483 highestSlot = std::max(highestSlot, newSlot);
484 }
485
486 i2miMap_.clear();
487 i2miMap_.resize(highestSlot + 1);
488 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
489 MI != ME; ++MI) {
David Greene340482d2009-07-22 21:56:14 +0000490 i2miMap_[MI->second] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000491 }
492
493}
494
495
Owen Anderson80b3ce62008-05-28 20:54:50 +0000496/// runOnMachineFunction - Register allocate the whole function
497///
498bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
499 mf_ = &fn;
500 mri_ = &mf_->getRegInfo();
501 tm_ = &fn.getTarget();
502 tri_ = tm_->getRegisterInfo();
503 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000504 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000505 lv_ = &getAnalysis<LiveVariables>();
506 allocatableRegs_ = tri_->getAllocatableSet(fn);
507
Evan Cheng2578ba22009-07-01 01:59:31 +0000508 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000509 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000511
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000512 numIntervals += getNumIntervals();
513
Chris Lattner70ca3582004-09-30 15:59:17 +0000514 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000515 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000516}
517
Chris Lattner70ca3582004-09-30 15:59:17 +0000518/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000519void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000520 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000521 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Owen Anderson03857b22008-08-13 21:49:13 +0000522 I->second->print(O, tri_);
Evan Cheng3f32d652008-06-04 09:18:41 +0000523 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000524 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000525
526 O << "********** MACHINEINSTRS **********\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000527 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
528 mbbi != mbbe; ++mbbi) {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000529 O << ((Value*)mbbi->getBasicBlock())->getNameStr() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000530 for (MachineBasicBlock::iterator mii = mbbi->begin(),
531 mie = mbbi->end(); mii != mie; ++mii) {
532 O << getInstructionIndex(mii) << '\t' << *mii;
533 }
534 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000535}
536
Evan Chengc92da382007-11-03 07:20:12 +0000537/// conflictsWithPhysRegDef - Returns true if the specified register
538/// is defined during the duration of the specified interval.
539bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
540 VirtRegMap &vrm, unsigned reg) {
541 for (LiveInterval::Ranges::const_iterator
542 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
543 for (unsigned index = getBaseIndex(I->start),
544 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
545 index += InstrSlots::NUM) {
546 // skip deleted instructions
547 while (index != end && !getInstructionFromIndex(index))
548 index += InstrSlots::NUM;
549 if (index == end) break;
550
551 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000552 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
553 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000554 if (SrcReg == li.reg || DstReg == li.reg)
555 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000556 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
557 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000558 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000559 continue;
560 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000561 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000562 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000563 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000564 if (!vrm.hasPhys(PhysReg))
565 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000566 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000567 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000568 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000569 return true;
570 }
571 }
572 }
573
574 return false;
575}
576
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000577/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
578/// it can check use as well.
579bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
580 unsigned Reg, bool CheckUse,
581 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
582 for (LiveInterval::Ranges::const_iterator
583 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
584 for (unsigned index = getBaseIndex(I->start),
585 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
586 index += InstrSlots::NUM) {
587 // Skip deleted instructions.
588 MachineInstr *MI = 0;
589 while (index != end) {
590 MI = getInstructionFromIndex(index);
591 if (MI)
592 break;
593 index += InstrSlots::NUM;
594 }
595 if (index == end) break;
596
597 if (JoinedCopies.count(MI))
598 continue;
599 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
600 MachineOperand& MO = MI->getOperand(i);
601 if (!MO.isReg())
602 continue;
603 if (MO.isUse() && !CheckUse)
604 continue;
605 unsigned PhysReg = MO.getReg();
606 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
607 continue;
608 if (tri_->isSubRegister(Reg, PhysReg))
609 return true;
610 }
611 }
612 }
613
614 return false;
615}
616
617
Evan Cheng549f27d32007-08-13 23:45:17 +0000618void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000619 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000620 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000621 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000622 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000623}
624
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000625void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000626 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000627 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000628 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000629 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000630 DEBUG({
631 errs() << "\t\tregister: ";
632 printRegName(interval.reg);
633 });
Evan Cheng419852c2008-04-03 16:39:43 +0000634
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000635 // Virtual registers may be defined multiple times (due to phi
636 // elimination and 2-addr elimination). Much of what we do only has to be
637 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000639 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 if (interval.empty()) {
641 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000642 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000643 // Earlyclobbers move back one.
644 if (MO.isEarlyClobber())
645 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000646 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000647 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000648 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000649 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000650 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000651 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000652 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000653 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000654 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000655 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000656
657 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000658
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 // Loop over all of the blocks that the vreg is defined in. There are
660 // two cases we have to handle here. The most common case is a vreg
661 // whose lifetime is contained within a basic block. In this case there
662 // will be a single kill, in MBB, which comes after the definition.
663 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
664 // FIXME: what about dead vars?
665 unsigned killIdx;
666 if (vi.Kills[0] != mi)
667 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
668 else
669 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000670
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000671 // If the kill happens after the definition, we have an intra-block
672 // live range.
673 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000674 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000675 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000676 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000678 DEBUG(errs() << " +" << LR << "\n");
Lang Hamesffd13262009-07-09 03:57:02 +0000679 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 return;
681 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000682 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000683
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000684 // The other case we handle is when a virtual register lives to the end
685 // of the defining block, potentially live across some blocks, then is
686 // live into some number of blocks, but gets killed. Start by adding a
687 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000688 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000689 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000690 interval.addRange(NewLR);
691
692 // Iterate over all of the blocks that the variable is completely
693 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
694 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000695 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
696 E = vi.AliveBlocks.end(); I != E; ++I) {
697 LiveRange LR(getMBBStartIdx(*I),
698 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000699 ValNo);
700 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000701 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 }
703
704 // Finally, this virtual register is live from the start of any killing
705 // block to the 'use' slot of the killing instruction.
706 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
707 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000708 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000709 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000710 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000711 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000712 interval.addKill(ValNo, killIdx, false);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000713 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000714 }
715
716 } else {
717 // If this is the second time we see a virtual register definition, it
718 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000719 // the result of two address elimination, then the vreg is one of the
720 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000721 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000722 // If this is a two-address definition, then we have already processed
723 // the live range. The only problem is that we didn't realize there
724 // are actually two values in the live interval. Because of this we
725 // need to take the LiveRegion that defines this register and split it
726 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000727 assert(interval.containsOneValue());
728 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000729 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000730 if (MO.isEarlyClobber())
731 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000732
Evan Cheng4f8ff162007-08-11 00:59:19 +0000733 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000734 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000735
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000736 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000737 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000738 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000739
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000740 // Two-address vregs should always only be redefined once. This means
741 // that at this point, there should be exactly one value number in it.
742 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
743
Chris Lattner91725b72006-08-31 05:54:43 +0000744 // The new value number (#1) is defined by the instruction we claimed
745 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000746 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000747 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000748 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000749 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
750
Chris Lattner91725b72006-08-31 05:54:43 +0000751 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000752 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000753 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000754 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000755 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000756
757 // Add the new live interval which replaces the range for the input copy.
758 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000759 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000760 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000761 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000762
763 // If this redefinition is dead, we need to add a dummy unit live
764 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000765 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000766 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000767
Bill Wendling8e6179f2009-08-22 20:18:03 +0000768 DEBUG({
769 errs() << " RESULT: ";
770 interval.print(errs(), tri_);
771 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000772 } else {
773 // Otherwise, this must be because of phi elimination. If this is the
774 // first redefinition of the vreg that we have seen, go back and change
775 // the live range in the PHI block to be a different value number.
776 if (interval.containsOneValue()) {
777 assert(vi.Kills.size() == 1 &&
778 "PHI elimination vreg should have one kill, the PHI itself!");
779
780 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000781 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000782 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000783 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000784 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Bill Wendling8e6179f2009-08-22 20:18:03 +0000785 DEBUG({
786 errs() << " Removing [" << Start << "," << End << "] from: ";
787 interval.print(errs(), tri_);
788 errs() << "\n";
789 });
Lang Hamesffd13262009-07-09 03:57:02 +0000790 interval.removeRange(Start, End);
791 assert(interval.ranges.size() == 1 &&
792 "newly discovered PHI interval has >1 ranges.");
793 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
794 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000795 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000796 DEBUG({
797 errs() << " RESULT: ";
798 interval.print(errs(), tri_);
799 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000800
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000801 // Replace the interval with one of a NEW value number. Note that this
802 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000803 LiveRange LR(Start, End,
804 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000805 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000806 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000807 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000808 interval.addKill(LR.valno, End, false);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000809 DEBUG({
810 errs() << " RESULT: ";
811 interval.print(errs(), tri_);
812 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000813 }
814
815 // In the case of PHI elimination, each variable definition is only
816 // live until the end of the block. We've already taken care of the
817 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000818 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000819 if (MO.isEarlyClobber())
820 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000821
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000822 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000823 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000824 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000825 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000826 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000827 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000828 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000829 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000830 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000831
Owen Anderson7fbad272008-07-23 21:37:49 +0000832 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000833 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000834 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000835 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000836 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000837 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000838 }
839 }
840
Bill Wendling8e6179f2009-08-22 20:18:03 +0000841 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000842}
843
Chris Lattnerf35fef72004-07-23 21:24:19 +0000844void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000845 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000846 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000847 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000848 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000849 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000850 // A physical register cannot be live across basic block, so its
851 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000852 DEBUG({
853 errs() << "\t\tregister: ";
854 printRegName(interval.reg);
855 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000856
Chris Lattner6b128bd2006-09-03 08:07:11 +0000857 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000858 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000859 // Earlyclobbers move back one.
860 if (MO.isEarlyClobber())
861 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000862 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000863
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000864 // If it is not used after definition, it is considered dead at
865 // the instruction defining it. Hence its interval is:
866 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000867 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000868 DEBUG(errs() << " dead");
Dale Johannesen86b49f82008-09-24 01:07:17 +0000869 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000870 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000871 }
872
873 // If it is not dead on definition, it must be killed by a
874 // subsequent instruction. Hence its interval is:
875 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000876 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000877 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000878 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
879 getInstructionFromIndex(baseIndex) == 0)
880 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000881 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000882 DEBUG(errs() << " killed");
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000883 end = getUseIndex(baseIndex) + 1;
884 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000885 } else {
886 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
887 if (DefIdx != -1) {
888 if (mi->isRegTiedToUseOperand(DefIdx)) {
889 // Two-address instruction.
890 end = getDefIndex(baseIndex);
891 if (mi->getOperand(DefIdx).isEarlyClobber())
892 end = getUseIndex(baseIndex);
893 } else {
894 // Another instruction redefines the register before it is ever read.
895 // Then the register is essentially dead at the instruction that defines
896 // it. Hence its interval is:
897 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000898 DEBUG(errs() << " dead");
Evan Chengc45288e2009-04-27 20:42:46 +0000899 end = start + 1;
900 }
901 goto exit;
902 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000903 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000904
905 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000906 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000907
908 // The only case we should have a dead physreg here without a killing or
909 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000910 // and never used. Another possible case is the implicit use of the
911 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000912 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000913
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000914exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000915 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000916
Evan Cheng24a3cc42007-04-25 07:30:23 +0000917 // Already exists? Extend old live interval.
918 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000919 bool Extend = OldLR != interval.end();
920 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000921 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000922 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000923 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000924 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000925 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000926 interval.addKill(LR.valno, end, false);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000927 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000928}
929
Chris Lattnerf35fef72004-07-23 21:24:19 +0000930void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
931 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000932 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000933 MachineOperand& MO,
934 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000935 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000936 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000937 getOrCreateInterval(MO.getReg()));
938 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000939 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000940 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000941 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000942 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000943 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000944 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000945 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000946 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000947 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000948 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000949 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000950 // If MI also modifies the sub-register explicitly, avoid processing it
951 // more than once. Do not pass in TRI here so it checks for exact match.
952 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000953 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000954 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000955 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000956}
957
Evan Chengb371f452007-02-19 21:49:54 +0000958void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000959 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000960 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000961 DEBUG({
962 errs() << "\t\tlivein register: ";
963 printRegName(interval.reg);
964 });
Evan Chengb371f452007-02-19 21:49:54 +0000965
966 // Look for kills, if it reaches a def before it's killed, then it shouldn't
967 // be considered a livein.
968 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000969 unsigned baseIndex = MIIdx;
970 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000971 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
972 getInstructionFromIndex(baseIndex) == 0)
973 baseIndex += InstrSlots::NUM;
974 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000975 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000976
Evan Chengb371f452007-02-19 21:49:54 +0000977 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000978 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000979 DEBUG(errs() << " killed");
Evan Chengb371f452007-02-19 21:49:54 +0000980 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000981 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000982 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000983 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000984 // Another instruction redefines the register before it is ever read.
985 // Then the register is essentially dead at the instruction that defines
986 // it. Hence its interval is:
987 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000988 DEBUG(errs() << " dead");
Evan Chengb371f452007-02-19 21:49:54 +0000989 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000990 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000991 break;
Evan Chengb371f452007-02-19 21:49:54 +0000992 }
993
994 baseIndex += InstrSlots::NUM;
995 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000996 if (mi != MBB->end()) {
997 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
998 getInstructionFromIndex(baseIndex) == 0)
999 baseIndex += InstrSlots::NUM;
1000 }
Evan Chengb371f452007-02-19 21:49:54 +00001001 }
1002
Evan Cheng75611fb2007-06-27 01:16:36 +00001003 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001004 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001005 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001006 DEBUG(errs() << " dead");
Evan Cheng75611fb2007-06-27 01:16:36 +00001007 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +00001008 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001009 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001010 end = baseIndex;
1011 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001012 }
1013
Lang Hames10382fb2009-06-19 02:17:53 +00001014 VNInfo *vni =
1015 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001016 vni->setIsPHIDef(true);
1017 LiveRange LR(start, end, vni);
1018
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001019 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +00001020 interval.addKill(LR.valno, end, false);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001021 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001022}
1023
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001024/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001025/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001026/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001027/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001028void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001029 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001030 << "********** Function: "
1031 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001032
1033 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001034 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1035 MBBI != E; ++MBBI) {
1036 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001037 // Track the index of the current machine instr.
1038 unsigned MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001039 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001040
Chris Lattner428b92e2006-09-15 03:57:23 +00001041 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001042
Dan Gohmancb406c22007-10-03 19:26:29 +00001043 // Create intervals for live-ins to this BB first.
1044 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1045 LE = MBB->livein_end(); LI != LE; ++LI) {
1046 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1047 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001048 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001049 if (!hasInterval(*AS))
1050 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1051 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001052 }
1053
Owen Anderson99500ae2008-09-15 22:00:38 +00001054 // Skip over empty initial indices.
1055 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1056 getInstructionFromIndex(MIIndex) == 0)
1057 MIIndex += InstrSlots::NUM;
1058
Chris Lattner428b92e2006-09-15 03:57:23 +00001059 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001060 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001061
Evan Cheng438f7bc2006-11-10 08:43:01 +00001062 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001063 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1064 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001065 if (!MO.isReg() || !MO.getReg())
1066 continue;
1067
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001068 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001069 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001070 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001071 else if (MO.isUndef())
1072 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001073 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001074
1075 // Skip over the empty slots after each instruction.
1076 unsigned Slots = MI->getDesc().getNumDefs();
1077 if (Slots == 0)
1078 Slots = 1;
1079 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001080
1081 // Skip over empty indices.
1082 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1083 getInstructionFromIndex(MIIndex) == 0)
1084 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001085 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001086 }
Evan Chengd129d732009-07-17 19:43:40 +00001087
1088 // Create empty intervals for registers defined by implicit_def's (except
1089 // for those implicit_def that define values which are liveout of their
1090 // blocks.
1091 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1092 unsigned UndefReg = UndefUses[i];
1093 (void)getOrCreateInterval(UndefReg);
1094 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001095}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001096
Evan Chengd0e32c52008-10-29 05:06:14 +00001097bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001098 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001099 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001100 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001101
1102 bool ResVal = false;
1103 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001104 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001105 break;
1106 MBBs.push_back(I->second);
1107 ResVal = true;
1108 ++I;
1109 }
1110 return ResVal;
1111}
1112
Evan Chengd0e32c52008-10-29 05:06:14 +00001113bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1114 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1115 std::vector<IdxMBBPair>::const_iterator I =
1116 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1117
1118 bool ResVal = false;
1119 while (I != Idx2MBBMap.end()) {
1120 if (I->first > End)
1121 break;
1122 MachineBasicBlock *MBB = I->second;
1123 if (getMBBEndIdx(MBB) > End)
1124 break;
1125 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1126 SE = MBB->succ_end(); SI != SE; ++SI)
1127 MBBs.push_back(*SI);
1128 ResVal = true;
1129 ++I;
1130 }
1131 return ResVal;
1132}
1133
Owen Anderson03857b22008-08-13 21:49:13 +00001134LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001135 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001136 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001137}
Evan Chengf2fbca62007-11-12 06:35:08 +00001138
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001139/// dupInterval - Duplicate a live interval. The caller is responsible for
1140/// managing the allocated memory.
1141LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1142 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001143 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001144 return NewLI;
1145}
1146
Evan Chengc8d044e2008-02-15 18:24:29 +00001147/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1148/// copy field and returns the source register that defines it.
1149unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001150 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001151 return 0;
1152
Lang Hames52c1afc2009-08-10 23:43:28 +00001153 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001154 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001155 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001156 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001157 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001158 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001159 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1160 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1161 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001162
Evan Cheng04ee5a12009-01-20 19:12:24 +00001163 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001164 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001165 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001166 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001167 return 0;
1168}
Evan Chengf2fbca62007-11-12 06:35:08 +00001169
1170//===----------------------------------------------------------------------===//
1171// Register allocator hooks.
1172//
1173
Evan Chengd70dbb52008-02-22 09:24:50 +00001174/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1175/// allow one) virtual register operand, then its uses are implicitly using
1176/// the register. Returns the virtual register.
1177unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1178 MachineInstr *MI) const {
1179 unsigned RegOp = 0;
1180 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1181 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001182 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001183 continue;
1184 unsigned Reg = MO.getReg();
1185 if (Reg == 0 || Reg == li.reg)
1186 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001187
1188 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1189 !allocatableRegs_[Reg])
1190 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001191 // FIXME: For now, only remat MI with at most one register operand.
1192 assert(!RegOp &&
1193 "Can't rematerialize instruction with multiple register operand!");
1194 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001195#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001196 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001197#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001198 }
1199 return RegOp;
1200}
1201
1202/// isValNoAvailableAt - Return true if the val# of the specified interval
1203/// which reaches the given instruction also reaches the specified use index.
1204bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1205 unsigned UseIdx) const {
1206 unsigned Index = getInstructionIndex(MI);
1207 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1208 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1209 return UI != li.end() && UI->valno == ValNo;
1210}
1211
Evan Chengf2fbca62007-11-12 06:35:08 +00001212/// isReMaterializable - Returns true if the definition MI of the specified
1213/// val# of the specified interval is re-materializable.
1214bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001215 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001216 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001217 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001218 if (DisableReMat)
1219 return false;
1220
Evan Cheng20ccded2008-03-15 00:19:36 +00001221 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001222 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001223
1224 int FrameIdx = 0;
1225 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001226 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001227 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1228 // this but remember this is not safe to fold into a two-address
1229 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001230 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001231 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001232
Dan Gohman6d69ba82008-07-25 00:02:30 +00001233 // If the target-specific rules don't identify an instruction as
1234 // being trivially rematerializable, use some target-independent
1235 // rules.
1236 if (!MI->getDesc().isRematerializable() ||
1237 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001238 if (!EnableAggressiveRemat)
1239 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001240
Dan Gohman0471a792008-07-28 18:43:51 +00001241 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001242 // we can't analyze it.
1243 const TargetInstrDesc &TID = MI->getDesc();
1244 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1245 return false;
1246
1247 // Avoid instructions obviously unsafe for remat.
1248 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1249 return false;
1250
1251 // If the instruction accesses memory and the memory could be non-constant,
1252 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001253 for (std::list<MachineMemOperand>::const_iterator
1254 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001255 const MachineMemOperand &MMO = *I;
1256 if (MMO.isVolatile() || MMO.isStore())
1257 return false;
1258 const Value *V = MMO.getValue();
1259 if (!V)
1260 return false;
1261 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1262 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001263 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001264 } else if (!aa_->pointsToConstantMemory(V))
1265 return false;
1266 }
1267
1268 // If any of the registers accessed are non-constant, conservatively assume
1269 // the instruction is not rematerializable.
1270 unsigned ImpUse = 0;
1271 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1272 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001273 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001274 unsigned Reg = MO.getReg();
1275 if (Reg == 0)
1276 continue;
1277 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1278 return false;
1279
1280 // Only allow one def, and that in the first operand.
1281 if (MO.isDef() != (i == 0))
1282 return false;
1283
1284 // Only allow constant-valued registers.
1285 bool IsLiveIn = mri_->isLiveIn(Reg);
1286 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1287 E = mri_->def_end();
1288
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001289 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001290 if (MO.isDef() && (next(I) != E || IsLiveIn))
1291 return false;
1292
1293 if (MO.isUse()) {
1294 // Only allow one use other register use, as that's all the
1295 // remat mechanisms support currently.
1296 if (Reg != li.reg) {
1297 if (ImpUse == 0)
1298 ImpUse = Reg;
1299 else if (Reg != ImpUse)
1300 return false;
1301 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001302 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001303 if (I != E && (next(I) != E || IsLiveIn))
1304 return false;
1305 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001306 }
1307 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001308 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001309
Dan Gohman6d69ba82008-07-25 00:02:30 +00001310 unsigned ImpUse = getReMatImplicitUse(li, MI);
1311 if (ImpUse) {
1312 const LiveInterval &ImpLi = getInterval(ImpUse);
1313 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1314 re = mri_->use_end(); ri != re; ++ri) {
1315 MachineInstr *UseMI = &*ri;
1316 unsigned UseIdx = getInstructionIndex(UseMI);
1317 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1318 continue;
1319 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1320 return false;
1321 }
Evan Chengdc377862008-09-30 15:44:16 +00001322
1323 // If a register operand of the re-materialized instruction is going to
1324 // be spilled next, then it's not legal to re-materialize this instruction.
1325 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1326 if (ImpUse == SpillIs[i]->reg)
1327 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001328 }
1329 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001330}
1331
Evan Cheng06587492008-10-24 02:05:00 +00001332/// isReMaterializable - Returns true if the definition MI of the specified
1333/// val# of the specified interval is re-materializable.
1334bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1335 const VNInfo *ValNo, MachineInstr *MI) {
1336 SmallVector<LiveInterval*, 4> Dummy1;
1337 bool Dummy2;
1338 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1339}
1340
Evan Cheng5ef3a042007-12-06 00:01:56 +00001341/// isReMaterializable - Returns true if every definition of MI of every
1342/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001343bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1344 SmallVectorImpl<LiveInterval*> &SpillIs,
1345 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001346 isLoad = false;
1347 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1348 i != e; ++i) {
1349 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001350 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001351 continue; // Dead val#.
1352 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001353 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001354 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001355 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001356 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001357 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001358 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001359 return false;
1360 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001361 }
1362 return true;
1363}
1364
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001365/// FilterFoldedOps - Filter out two-address use operands. Return
1366/// true if it finds any issue with the operands that ought to prevent
1367/// folding.
1368static bool FilterFoldedOps(MachineInstr *MI,
1369 SmallVector<unsigned, 2> &Ops,
1370 unsigned &MRInfo,
1371 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001372 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001373 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1374 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001375 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001376 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001377 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001378 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001379 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001380 MRInfo |= (unsigned)VirtRegMap::isMod;
1381 else {
1382 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001383 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001384 MRInfo = VirtRegMap::isModRef;
1385 continue;
1386 }
1387 MRInfo |= (unsigned)VirtRegMap::isRef;
1388 }
1389 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001390 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001391 return false;
1392}
1393
1394
1395/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1396/// slot / to reg or any rematerialized load into ith operand of specified
1397/// MI. If it is successul, MI is updated with the newly created MI and
1398/// returns true.
1399bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1400 VirtRegMap &vrm, MachineInstr *DefMI,
1401 unsigned InstrIdx,
1402 SmallVector<unsigned, 2> &Ops,
1403 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001404 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001405 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001406 RemoveMachineInstrFromMaps(MI);
1407 vrm.RemoveMachineInstrFromMaps(MI);
1408 MI->eraseFromParent();
1409 ++numFolds;
1410 return true;
1411 }
1412
1413 // Filter the list of operand indexes that are to be folded. Abort if
1414 // any operand will prevent folding.
1415 unsigned MRInfo = 0;
1416 SmallVector<unsigned, 2> FoldOps;
1417 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1418 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001419
Evan Cheng427f4c12008-03-31 23:19:51 +00001420 // The only time it's safe to fold into a two address instruction is when
1421 // it's folding reload and spill from / into a spill stack slot.
1422 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001423 return false;
1424
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001425 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1426 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001427 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001428 // Remember this instruction uses the spill slot.
1429 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1430
Evan Chengf2fbca62007-11-12 06:35:08 +00001431 // Attempt to fold the memory reference into the instruction. If
1432 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001433 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001434 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001435 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001436 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001437 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001438 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001439 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001440 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1441 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001442 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001443 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001444 return true;
1445 }
1446 return false;
1447}
1448
Evan Cheng018f9b02007-12-05 03:22:34 +00001449/// canFoldMemoryOperand - Returns true if the specified load / store
1450/// folding is possible.
1451bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001452 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001453 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001454 // Filter the list of operand indexes that are to be folded. Abort if
1455 // any operand will prevent folding.
1456 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001457 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001458 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1459 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001460
Evan Cheng3c75ba82008-04-01 21:37:32 +00001461 // It's only legal to remat for a use, not a def.
1462 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001463 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001464
Evan Chengd70dbb52008-02-22 09:24:50 +00001465 return tii_->canFoldMemoryOperand(MI, FoldOps);
1466}
1467
Evan Cheng81a03822007-11-17 00:40:40 +00001468bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1469 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1470 for (LiveInterval::Ranges::const_iterator
1471 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1472 std::vector<IdxMBBPair>::const_iterator II =
1473 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1474 if (II == Idx2MBBMap.end())
1475 continue;
1476 if (I->end > II->first) // crossing a MBB.
1477 return false;
1478 MBBs.insert(II->second);
1479 if (MBBs.size() > 1)
1480 return false;
1481 }
1482 return true;
1483}
1484
Evan Chengd70dbb52008-02-22 09:24:50 +00001485/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1486/// interval on to-be re-materialized operands of MI) with new register.
1487void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1488 MachineInstr *MI, unsigned NewVReg,
1489 VirtRegMap &vrm) {
1490 // There is an implicit use. That means one of the other operand is
1491 // being remat'ed and the remat'ed instruction has li.reg as an
1492 // use operand. Make sure we rewrite that as well.
1493 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1494 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001495 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001496 continue;
1497 unsigned Reg = MO.getReg();
1498 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1499 continue;
1500 if (!vrm.isReMaterialized(Reg))
1501 continue;
1502 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001503 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1504 if (UseMO)
1505 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001506 }
1507}
1508
Evan Chengf2fbca62007-11-12 06:35:08 +00001509/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1510/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001511bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001512rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1513 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001514 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001515 unsigned Slot, int LdSlot,
1516 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001517 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001518 const TargetRegisterClass* rc,
1519 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001520 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001521 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001522 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001523 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001524 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001525 RestartInstruction:
1526 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1527 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001528 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001529 continue;
1530 unsigned Reg = mop.getReg();
1531 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001532 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001533 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001534 if (Reg != li.reg)
1535 continue;
1536
1537 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001538 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001539 int FoldSlot = Slot;
1540 if (DefIsReMat) {
1541 // If this is the rematerializable definition MI itself and
1542 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001543 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001544 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1545 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001546 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001547 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001548 MI->eraseFromParent();
1549 break;
1550 }
1551
1552 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001553 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001554 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001555 if (isLoad) {
1556 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1557 FoldSS = isLoadSS;
1558 FoldSlot = LdSlot;
1559 }
1560 }
1561
Evan Chengf2fbca62007-11-12 06:35:08 +00001562 // Scan all of the operands of this instruction rewriting operands
1563 // to use NewVReg instead of li.reg as appropriate. We do this for
1564 // two reasons:
1565 //
1566 // 1. If the instr reads the same spilled vreg multiple times, we
1567 // want to reuse the NewVReg.
1568 // 2. If the instr is a two-addr instruction, we are required to
1569 // keep the src/dst regs pinned.
1570 //
1571 // Keep track of whether we replace a use and/or def so that we can
1572 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001573
Evan Cheng81a03822007-11-17 00:40:40 +00001574 HasUse = mop.isUse();
1575 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001576 SmallVector<unsigned, 2> Ops;
1577 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001578 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001579 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001580 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001581 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001582 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001583 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001584 continue;
1585 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001586 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001587 if (!MOj.isUndef()) {
1588 HasUse |= MOj.isUse();
1589 HasDef |= MOj.isDef();
1590 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001591 }
1592 }
1593
David Greene26b86a02008-10-27 17:38:59 +00001594 // Create a new virtual register for the spill interval.
1595 // Create the new register now so we can map the fold instruction
1596 // to the new register so when it is unfolded we get the correct
1597 // answer.
1598 bool CreatedNewVReg = false;
1599 if (NewVReg == 0) {
1600 NewVReg = mri_->createVirtualRegister(rc);
1601 vrm.grow();
1602 CreatedNewVReg = true;
1603 }
1604
Evan Cheng9c3c2212008-06-06 07:54:39 +00001605 if (!TryFold)
1606 CanFold = false;
1607 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001608 // Do not fold load / store here if we are splitting. We'll find an
1609 // optimal point to insert a load / store later.
1610 if (!TrySplit) {
1611 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001612 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001613 // Folding the load/store can completely change the instruction in
1614 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001615
1616 if (FoldSS) {
1617 // We need to give the new vreg the same stack slot as the
1618 // spilled interval.
1619 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1620 }
1621
Evan Cheng018f9b02007-12-05 03:22:34 +00001622 HasUse = false;
1623 HasDef = false;
1624 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001625 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001626 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001627 goto RestartInstruction;
1628 }
1629 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001630 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001631 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001632 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001633 }
Evan Chengcddbb832007-11-30 21:23:43 +00001634
Evan Chengcddbb832007-11-30 21:23:43 +00001635 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001636 if (mop.isImplicit())
1637 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001638
1639 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001640 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1641 MachineOperand &mopj = MI->getOperand(Ops[j]);
1642 mopj.setReg(NewVReg);
1643 if (mopj.isImplicit())
1644 rewriteImplicitOps(li, MI, NewVReg, vrm);
1645 }
Evan Chengcddbb832007-11-30 21:23:43 +00001646
Evan Cheng81a03822007-11-17 00:40:40 +00001647 if (CreatedNewVReg) {
1648 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001649 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001650 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001651 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001652 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001653 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001654 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001655 }
1656 if (!CanDelete || (HasUse && HasDef)) {
1657 // If this is a two-addr instruction then its use operands are
1658 // rematerializable but its def is not. It should be assigned a
1659 // stack slot.
1660 vrm.assignVirt2StackSlot(NewVReg, Slot);
1661 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001662 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001663 vrm.assignVirt2StackSlot(NewVReg, Slot);
1664 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001665 } else if (HasUse && HasDef &&
1666 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1667 // If this interval hasn't been assigned a stack slot (because earlier
1668 // def is a deleted remat def), do it now.
1669 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1670 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001671 }
1672
Evan Cheng313d4b82008-02-23 00:33:04 +00001673 // Re-matting an instruction with virtual register use. Add the
1674 // register as an implicit use on the use MI.
1675 if (DefIsReMat && ImpUse)
1676 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1677
Evan Cheng5b69eba2009-04-21 22:46:52 +00001678 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001679 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001680 if (CreatedNewVReg) {
1681 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001682 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001683 if (TrySplit)
1684 vrm.setIsSplitFromReg(NewVReg, li.reg);
1685 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001686
1687 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001688 if (CreatedNewVReg) {
1689 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001690 nI.getNextValue(0, 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001691 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001692 nI.addRange(LR);
1693 } else {
1694 // Extend the split live interval to this def / use.
1695 unsigned End = getUseIndex(index)+1;
1696 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1697 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001698 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001699 nI.addRange(LR);
1700 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001701 }
1702 if (HasDef) {
1703 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001704 nI.getNextValue(0, 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001705 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001706 nI.addRange(LR);
1707 }
Evan Cheng81a03822007-11-17 00:40:40 +00001708
Bill Wendling8e6179f2009-08-22 20:18:03 +00001709 DEBUG({
1710 errs() << "\t\t\t\tAdded new interval: ";
1711 nI.print(errs(), tri_);
1712 errs() << '\n';
1713 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001714 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001715 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001716}
Evan Cheng81a03822007-11-17 00:40:40 +00001717bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001718 const VNInfo *VNI,
1719 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001720 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001721 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001722 if (VNI->kills[j].isPHIKill)
1723 continue;
1724
1725 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001726 if (KillIdx > Idx && KillIdx < End)
1727 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001728 }
1729 return false;
1730}
1731
Evan Cheng063284c2008-02-21 00:34:19 +00001732/// RewriteInfo - Keep track of machine instrs that will be rewritten
1733/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001734namespace {
1735 struct RewriteInfo {
1736 unsigned Index;
1737 MachineInstr *MI;
1738 bool HasUse;
1739 bool HasDef;
1740 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1741 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1742 };
Evan Cheng063284c2008-02-21 00:34:19 +00001743
Dan Gohman844731a2008-05-13 00:00:25 +00001744 struct RewriteInfoCompare {
1745 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1746 return LHS.Index < RHS.Index;
1747 }
1748 };
1749}
Evan Cheng063284c2008-02-21 00:34:19 +00001750
Evan Chengf2fbca62007-11-12 06:35:08 +00001751void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001752rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001753 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001754 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001755 unsigned Slot, int LdSlot,
1756 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001757 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001758 const TargetRegisterClass* rc,
1759 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001760 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001761 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001762 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001763 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001764 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1765 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001766 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001767 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001768 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001769 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001770 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001771
Evan Cheng063284c2008-02-21 00:34:19 +00001772 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001773 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001774 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001775 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1776 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001777 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001778 MachineOperand &O = ri.getOperand();
1779 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001780 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001781 unsigned index = getInstructionIndex(MI);
1782 if (index < start || index >= end)
1783 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001784
1785 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001786 // Must be defined by an implicit def. It should not be spilled. Note,
1787 // this is for correctness reason. e.g.
1788 // 8 %reg1024<def> = IMPLICIT_DEF
1789 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1790 // The live range [12, 14) are not part of the r1024 live interval since
1791 // it's defined by an implicit def. It will not conflicts with live
1792 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001793 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001794 // the INSERT_SUBREG and both target registers that would overlap.
1795 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001796 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1797 }
1798 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1799
Evan Cheng313d4b82008-02-23 00:33:04 +00001800 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001801 // Now rewrite the defs and uses.
1802 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1803 RewriteInfo &rwi = RewriteMIs[i];
1804 ++i;
1805 unsigned index = rwi.Index;
1806 bool MIHasUse = rwi.HasUse;
1807 bool MIHasDef = rwi.HasDef;
1808 MachineInstr *MI = rwi.MI;
1809 // If MI def and/or use the same register multiple times, then there
1810 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001811 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001812 while (i != e && RewriteMIs[i].MI == MI) {
1813 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001814 bool isUse = RewriteMIs[i].HasUse;
1815 if (isUse) ++NumUses;
1816 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001817 MIHasDef |= RewriteMIs[i].HasDef;
1818 ++i;
1819 }
Evan Cheng81a03822007-11-17 00:40:40 +00001820 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001821
Evan Cheng0a891ed2008-05-23 23:00:04 +00001822 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001823 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001824 // register interval's spill weight to HUGE_VALF to prevent it from
1825 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001826 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001827 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001828 }
1829
Evan Cheng063284c2008-02-21 00:34:19 +00001830 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001831 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001832 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001833 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001834 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001835 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001836 // One common case:
1837 // x = use
1838 // ...
1839 // ...
1840 // def = ...
1841 // = use
1842 // It's better to start a new interval to avoid artifically
1843 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001844 if (MIHasDef && !MIHasUse) {
1845 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001846 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001847 }
1848 }
Evan Chengcada2452007-11-28 01:28:46 +00001849 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001850
1851 bool IsNew = ThisVReg == 0;
1852 if (IsNew) {
1853 // This ends the previous live interval. If all of its def / use
1854 // can be folded, give it a low spill weight.
1855 if (NewVReg && TrySplit && AllCanFold) {
1856 LiveInterval &nI = getOrCreateInterval(NewVReg);
1857 nI.weight /= 10.0F;
1858 }
1859 AllCanFold = true;
1860 }
1861 NewVReg = ThisVReg;
1862
Evan Cheng81a03822007-11-17 00:40:40 +00001863 bool HasDef = false;
1864 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001865 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001866 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1867 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1868 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001869 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001870 if (!HasDef && !HasUse)
1871 continue;
1872
Evan Cheng018f9b02007-12-05 03:22:34 +00001873 AllCanFold &= CanFold;
1874
Evan Cheng81a03822007-11-17 00:40:40 +00001875 // Update weight of spill interval.
1876 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001877 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001878 // The spill weight is now infinity as it cannot be spilled again.
1879 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001880 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001881 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001882
1883 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001884 if (HasDef) {
1885 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001886 bool HasKill = false;
1887 if (!HasUse)
1888 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1889 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001890 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001891 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001892 if (VNI)
1893 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1894 }
Owen Anderson28998312008-08-13 22:28:50 +00001895 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001896 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001897 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001898 if (SII == SpillIdxes.end()) {
1899 std::vector<SRInfo> S;
1900 S.push_back(SRInfo(index, NewVReg, true));
1901 SpillIdxes.insert(std::make_pair(MBBId, S));
1902 } else if (SII->second.back().vreg != NewVReg) {
1903 SII->second.push_back(SRInfo(index, NewVReg, true));
1904 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001905 // If there is an earlier def and this is a two-address
1906 // instruction, then it's not possible to fold the store (which
1907 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001908 SRInfo &Info = SII->second.back();
1909 Info.index = index;
1910 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001911 }
1912 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001913 } else if (SII != SpillIdxes.end() &&
1914 SII->second.back().vreg == NewVReg &&
1915 (int)index > SII->second.back().index) {
1916 // There is an earlier def that's not killed (must be two-address).
1917 // The spill is no longer needed.
1918 SII->second.pop_back();
1919 if (SII->second.empty()) {
1920 SpillIdxes.erase(MBBId);
1921 SpillMBBs.reset(MBBId);
1922 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001923 }
1924 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001925 }
1926
1927 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001928 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001929 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001930 if (SII != SpillIdxes.end() &&
1931 SII->second.back().vreg == NewVReg &&
1932 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001933 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001934 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001935 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001936 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001937 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001938 // If we are splitting live intervals, only fold if it's the first
1939 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001940 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001941 else if (IsNew) {
1942 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001943 if (RII == RestoreIdxes.end()) {
1944 std::vector<SRInfo> Infos;
1945 Infos.push_back(SRInfo(index, NewVReg, true));
1946 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1947 } else {
1948 RII->second.push_back(SRInfo(index, NewVReg, true));
1949 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001950 RestoreMBBs.set(MBBId);
1951 }
1952 }
1953
1954 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001955 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001956 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001957 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001958
1959 if (NewVReg && TrySplit && AllCanFold) {
1960 // If all of its def / use can be folded, give it a low spill weight.
1961 LiveInterval &nI = getOrCreateInterval(NewVReg);
1962 nI.weight /= 10.0F;
1963 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001964}
1965
Evan Cheng1953d0c2007-11-29 10:12:14 +00001966bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1967 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001968 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001969 if (!RestoreMBBs[Id])
1970 return false;
1971 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1972 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1973 if (Restores[i].index == index &&
1974 Restores[i].vreg == vr &&
1975 Restores[i].canFold)
1976 return true;
1977 return false;
1978}
1979
1980void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1981 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001982 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001983 if (!RestoreMBBs[Id])
1984 return;
1985 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1986 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1987 if (Restores[i].index == index && Restores[i].vreg)
1988 Restores[i].index = -1;
1989}
Evan Cheng81a03822007-11-17 00:40:40 +00001990
Evan Cheng4cce6b42008-04-11 17:53:36 +00001991/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1992/// spilled and create empty intervals for their uses.
1993void
1994LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1995 const TargetRegisterClass* rc,
1996 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001997 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1998 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001999 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002000 MachineInstr *MI = &*ri;
2001 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002002 if (O.isDef()) {
2003 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2004 "Register def was not rewritten?");
2005 RemoveMachineInstrFromMaps(MI);
2006 vrm.RemoveMachineInstrFromMaps(MI);
2007 MI->eraseFromParent();
2008 } else {
2009 // This must be an use of an implicit_def so it's not part of the live
2010 // interval. Create a new empty live interval for it.
2011 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2012 unsigned NewVReg = mri_->createVirtualRegister(rc);
2013 vrm.grow();
2014 vrm.setIsImplicitlyDefined(NewVReg);
2015 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2016 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2017 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002018 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002019 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002020 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002021 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002022 }
2023 }
Evan Cheng419852c2008-04-03 16:39:43 +00002024 }
2025}
2026
Evan Chengf2fbca62007-11-12 06:35:08 +00002027std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002028addIntervalsForSpillsFast(const LiveInterval &li,
2029 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002030 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002031 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002032
2033 std::vector<LiveInterval*> added;
2034
2035 assert(li.weight != HUGE_VALF &&
2036 "attempt to spill already spilled interval!");
2037
Bill Wendling8e6179f2009-08-22 20:18:03 +00002038 DEBUG({
2039 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2040 li.dump();
2041 errs() << '\n';
2042 });
Owen Andersond6664312008-08-18 18:05:32 +00002043
2044 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2045
Owen Andersona41e47a2008-08-19 22:12:11 +00002046 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2047 while (RI != mri_->reg_end()) {
2048 MachineInstr* MI = &*RI;
2049
2050 SmallVector<unsigned, 2> Indices;
2051 bool HasUse = false;
2052 bool HasDef = false;
2053
2054 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2055 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002056 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002057
2058 HasUse |= MI->getOperand(i).isUse();
2059 HasDef |= MI->getOperand(i).isDef();
2060
2061 Indices.push_back(i);
2062 }
2063
2064 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2065 Indices, true, slot, li.reg)) {
2066 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002067 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002068 vrm.assignVirt2StackSlot(NewVReg, slot);
2069
Owen Andersona41e47a2008-08-19 22:12:11 +00002070 // create a new register for this spill
2071 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002072
Owen Andersona41e47a2008-08-19 22:12:11 +00002073 // the spill weight is now infinity as it
2074 // cannot be spilled again
2075 nI.weight = HUGE_VALF;
2076
2077 // Rewrite register operands to use the new vreg.
2078 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2079 E = Indices.end(); I != E; ++I) {
2080 MI->getOperand(*I).setReg(NewVReg);
2081
2082 if (MI->getOperand(*I).isUse())
2083 MI->getOperand(*I).setIsKill(true);
2084 }
2085
2086 // Fill in the new live interval.
2087 unsigned index = getInstructionIndex(MI);
2088 if (HasUse) {
2089 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002090 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002091 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002092 nI.addRange(LR);
2093 vrm.addRestorePoint(NewVReg, MI);
2094 }
2095 if (HasDef) {
2096 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002097 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002098 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002099 nI.addRange(LR);
2100 vrm.addSpillPoint(NewVReg, true, MI);
2101 }
2102
Owen Anderson17197312008-08-18 23:41:04 +00002103 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002104
Bill Wendling8e6179f2009-08-22 20:18:03 +00002105 DEBUG({
2106 errs() << "\t\t\t\tadded new interval: ";
2107 nI.dump();
2108 errs() << '\n';
2109 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002110 }
Owen Anderson9a032932008-08-18 21:20:32 +00002111
Owen Anderson9a032932008-08-18 21:20:32 +00002112
Owen Andersona41e47a2008-08-19 22:12:11 +00002113 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002114 }
Owen Andersond6664312008-08-18 18:05:32 +00002115
2116 return added;
2117}
2118
2119std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002120addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002121 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002122 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002123
2124 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002125 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002126
Evan Chengf2fbca62007-11-12 06:35:08 +00002127 assert(li.weight != HUGE_VALF &&
2128 "attempt to spill already spilled interval!");
2129
Bill Wendling8e6179f2009-08-22 20:18:03 +00002130 DEBUG({
2131 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2132 li.print(errs(), tri_);
2133 errs() << '\n';
2134 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002135
Evan Cheng72eeb942008-12-05 17:00:16 +00002136 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002137 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002138 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002139 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002140 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2141 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002142 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002143 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002144
2145 unsigned NumValNums = li.getNumValNums();
2146 SmallVector<MachineInstr*, 4> ReMatDefs;
2147 ReMatDefs.resize(NumValNums, NULL);
2148 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2149 ReMatOrigDefs.resize(NumValNums, NULL);
2150 SmallVector<int, 4> ReMatIds;
2151 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2152 BitVector ReMatDelete(NumValNums);
2153 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2154
Evan Cheng81a03822007-11-17 00:40:40 +00002155 // Spilling a split live interval. It cannot be split any further. Also,
2156 // it's also guaranteed to be a single val# / range interval.
2157 if (vrm.getPreSplitReg(li.reg)) {
2158 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002159 // Unset the split kill marker on the last use.
2160 unsigned KillIdx = vrm.getKillPoint(li.reg);
2161 if (KillIdx) {
2162 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2163 assert(KillMI && "Last use disappeared?");
2164 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2165 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002166 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002167 }
Evan Chengadf85902007-12-05 09:51:10 +00002168 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002169 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2170 Slot = vrm.getStackSlot(li.reg);
2171 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2172 MachineInstr *ReMatDefMI = DefIsReMat ?
2173 vrm.getReMaterializedMI(li.reg) : NULL;
2174 int LdSlot = 0;
2175 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2176 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002177 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002178 bool IsFirstRange = true;
2179 for (LiveInterval::Ranges::const_iterator
2180 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2181 // If this is a split live interval with multiple ranges, it means there
2182 // are two-address instructions that re-defined the value. Only the
2183 // first def can be rematerialized!
2184 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002185 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002186 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2187 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002188 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002189 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002190 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002191 } else {
2192 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2193 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002194 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002195 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002196 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002197 }
2198 IsFirstRange = false;
2199 }
Evan Cheng419852c2008-04-03 16:39:43 +00002200
Evan Cheng4cce6b42008-04-11 17:53:36 +00002201 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002202 return NewLIs;
2203 }
2204
2205 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002206 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2207 TrySplit = false;
2208 if (TrySplit)
2209 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002210 bool NeedStackSlot = false;
2211 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2212 i != e; ++i) {
2213 const VNInfo *VNI = *i;
2214 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002215 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002216 continue; // Dead val#.
2217 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002218 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2219 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002220 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002221 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002222 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002223 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002224 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002225 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2226 ClonedMIs.push_back(Clone);
2227 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002228
2229 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002230 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002231 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002232 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002233 CanDelete = false;
2234 // Need a stack slot if there is any live range where uses cannot be
2235 // rematerialized.
2236 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002237 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002238 if (CanDelete)
2239 ReMatDelete.set(VN);
2240 } else {
2241 // Need a stack slot if there is any live range where uses cannot be
2242 // rematerialized.
2243 NeedStackSlot = true;
2244 }
2245 }
2246
2247 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002248 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2249 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2250 Slot = vrm.assignVirt2StackSlot(li.reg);
2251
2252 // This case only occurs when the prealloc splitter has already assigned
2253 // a stack slot to this vreg.
2254 else
2255 Slot = vrm.getStackSlot(li.reg);
2256 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002257
2258 // Create new intervals and rewrite defs and uses.
2259 for (LiveInterval::Ranges::const_iterator
2260 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002261 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2262 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2263 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002264 bool CanDelete = ReMatDelete[I->valno->id];
2265 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002266 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002267 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002268 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002269 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002270 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002271 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002272 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002273 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002274 }
2275
Evan Cheng0cbb1162007-11-29 01:06:25 +00002276 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002277 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002278 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002279 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002280 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002281
Evan Chengb50bb8c2007-12-05 08:16:32 +00002282 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002283 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002284 if (NeedStackSlot) {
2285 int Id = SpillMBBs.find_first();
2286 while (Id != -1) {
2287 std::vector<SRInfo> &spills = SpillIdxes[Id];
2288 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2289 int index = spills[i].index;
2290 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002291 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002292 bool isReMat = vrm.isReMaterialized(VReg);
2293 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002294 bool CanFold = false;
2295 bool FoundUse = false;
2296 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002297 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002298 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002299 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2300 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002301 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002302 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002303
2304 Ops.push_back(j);
2305 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002306 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002307 if (isReMat ||
2308 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2309 RestoreMBBs, RestoreIdxes))) {
2310 // MI has two-address uses of the same register. If the use
2311 // isn't the first and only use in the BB, then we can't fold
2312 // it. FIXME: Move this to rewriteInstructionsForSpills.
2313 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002314 break;
2315 }
Evan Chengaee4af62007-12-02 08:30:39 +00002316 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002317 }
2318 }
2319 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002320 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002321 if (CanFold && !Ops.empty()) {
2322 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002323 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002324 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002325 // Also folded uses, do not issue a load.
2326 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002327 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2328 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002329 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002330 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002331 }
2332
Evan Cheng7e073ba2008-04-09 20:57:25 +00002333 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002334 if (!Folded) {
2335 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2336 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002337 if (!MI->registerDefIsDead(nI.reg))
2338 // No need to spill a dead def.
2339 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002340 if (isKill)
2341 AddedKill.insert(&nI);
2342 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002343 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002344 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002345 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002346 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002347
Evan Cheng1953d0c2007-11-29 10:12:14 +00002348 int Id = RestoreMBBs.find_first();
2349 while (Id != -1) {
2350 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2351 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2352 int index = restores[i].index;
2353 if (index == -1)
2354 continue;
2355 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002356 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002357 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002358 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002359 bool CanFold = false;
2360 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002361 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002362 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002363 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2364 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002365 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002366 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002367
Evan Cheng0cbb1162007-11-29 01:06:25 +00002368 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002369 // If this restore were to be folded, it would have been folded
2370 // already.
2371 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002372 break;
2373 }
Evan Chengaee4af62007-12-02 08:30:39 +00002374 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002375 }
2376 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002377
2378 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002379 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002380 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002381 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002382 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2383 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002384 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2385 int LdSlot = 0;
2386 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2387 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002388 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002389 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2390 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002391 if (!Folded) {
2392 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2393 if (ImpUse) {
2394 // Re-matting an instruction with virtual register use. Add the
2395 // register as an implicit use on the use MI and update the register
2396 // interval's spill weight to HUGE_VALF to prevent it from being
2397 // spilled.
2398 LiveInterval &ImpLi = getInterval(ImpUse);
2399 ImpLi.weight = HUGE_VALF;
2400 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2401 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002402 }
Evan Chengaee4af62007-12-02 08:30:39 +00002403 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002404 }
2405 // If folding is not possible / failed, then tell the spiller to issue a
2406 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002407 if (Folded)
2408 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002409 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002410 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002411 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002412 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002413 }
2414
Evan Chengb50bb8c2007-12-05 08:16:32 +00002415 // Finalize intervals: add kills, finalize spill weights, and filter out
2416 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002417 std::vector<LiveInterval*> RetNewLIs;
2418 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2419 LiveInterval *LI = NewLIs[i];
2420 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002421 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002422 if (!AddedKill.count(LI)) {
2423 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002424 unsigned LastUseIdx = getBaseIndex(LR->end);
2425 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002426 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002427 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002428 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002429 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002430 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002431 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002432 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002433 RetNewLIs.push_back(LI);
2434 }
2435 }
Evan Cheng81a03822007-11-17 00:40:40 +00002436
Evan Cheng4cce6b42008-04-11 17:53:36 +00002437 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002438 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002439}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002440
2441/// hasAllocatableSuperReg - Return true if the specified physical register has
2442/// any super register that's allocatable.
2443bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2444 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2445 if (allocatableRegs_[*AS] && hasInterval(*AS))
2446 return true;
2447 return false;
2448}
2449
2450/// getRepresentativeReg - Find the largest super register of the specified
2451/// physical register.
2452unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2453 // Find the largest super-register that is allocatable.
2454 unsigned BestReg = Reg;
2455 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2456 unsigned SuperReg = *AS;
2457 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2458 BestReg = SuperReg;
2459 break;
2460 }
2461 }
2462 return BestReg;
2463}
2464
2465/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2466/// specified interval that conflicts with the specified physical register.
2467unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2468 unsigned PhysReg) const {
2469 unsigned NumConflicts = 0;
2470 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2471 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2472 E = mri_->reg_end(); I != E; ++I) {
2473 MachineOperand &O = I.getOperand();
2474 MachineInstr *MI = O.getParent();
2475 unsigned Index = getInstructionIndex(MI);
2476 if (pli.liveAt(Index))
2477 ++NumConflicts;
2478 }
2479 return NumConflicts;
2480}
2481
2482/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002483/// around all defs and uses of the specified interval. Return true if it
2484/// was able to cut its interval.
2485bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002486 unsigned PhysReg, VirtRegMap &vrm) {
2487 unsigned SpillReg = getRepresentativeReg(PhysReg);
2488
2489 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2490 // If there are registers which alias PhysReg, but which are not a
2491 // sub-register of the chosen representative super register. Assert
2492 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002493 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002494 tri_->isSuperRegister(*AS, SpillReg));
2495
Evan Cheng2824a652009-03-23 18:24:37 +00002496 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002497 LiveInterval &pli = getInterval(SpillReg);
2498 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2499 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2500 E = mri_->reg_end(); I != E; ++I) {
2501 MachineOperand &O = I.getOperand();
2502 MachineInstr *MI = O.getParent();
2503 if (SeenMIs.count(MI))
2504 continue;
2505 SeenMIs.insert(MI);
2506 unsigned Index = getInstructionIndex(MI);
2507 if (pli.liveAt(Index)) {
2508 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002509 unsigned StartIdx = getLoadIndex(Index);
2510 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002511 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002512 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002513 Cut = true;
2514 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002515 std::string msg;
2516 raw_string_ostream Msg(msg);
2517 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002518 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002519 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002520 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002521 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002522 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002523 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002524 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002525 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2526 if (!hasInterval(*AS))
2527 continue;
2528 LiveInterval &spli = getInterval(*AS);
2529 if (spli.liveAt(Index))
2530 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2531 }
2532 }
2533 }
Evan Cheng2824a652009-03-23 18:24:37 +00002534 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002535}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002536
2537LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002538 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002539 LiveInterval& Interval = getOrCreateInterval(reg);
2540 VNInfo* VN = Interval.getNextValue(
2541 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002542 startInst, true, getVNInfoAllocator());
2543 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002544 VN->kills.push_back(
2545 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002546 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2547 getMBBEndIdx(startInst->getParent()) + 1, VN);
2548 Interval.addRange(LR);
2549
2550 return LR;
2551}
David Greeneb5257662009-08-03 21:55:09 +00002552