blob: 93d3d4c83896c116fe2d170bab7ebbb5238f7f2b [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"
Dan Gohmanc76909a2009-09-25 20:36:54 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000030#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000031#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000035#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000040#include "llvm/ADT/DepthFirstIterator.h"
41#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000044#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000045#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000046#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047using namespace llvm;
48
Dan Gohman844731a2008-05-13 00:00:25 +000049// Hidden options for help debugging.
50static cl::opt<bool> DisableReMat("disable-rematerialization",
51 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000052
Owen Andersonae339ba2008-08-19 00:17:30 +000053static cl::opt<bool> EnableFastSpilling("fast-spill",
54 cl::init(false), cl::Hidden);
55
Evan Cheng752195e2009-09-14 21:33:42 +000056static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
57
58static cl::opt<int> CoalescingLimit("early-coalescing-limit",
59 cl::init(-1), cl::Hidden);
60
61STATISTIC(numIntervals , "Number of original intervals");
62STATISTIC(numFolds , "Number of loads/stores folded into instructions");
63STATISTIC(numSplits , "Number of intervals split");
64STATISTIC(numCoalescing, "Number of early coalescing performed");
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();
Evan Cheng752195e2009-09-14 21:33:42 +000099 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000100
Evan Chengdd199d22007-09-06 01:07:24 +0000101 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
102 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000103 while (!CloneMIs.empty()) {
104 MachineInstr *MI = CloneMIs.back();
105 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000106 mf_->DeleteMachineInstr(MI);
107 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000108}
109
Evan Cheng6ade93b2009-08-05 03:53:14 +0000110static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
Evan Chengb0f59732009-09-21 04:32:32 +0000111 unsigned OpIdx, const TargetInstrInfo *tii_){
Evan Cheng6ade93b2009-08-05 03:53:14 +0000112 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
113 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
114 Reg == SrcReg)
115 return true;
116
Evan Chengb0f59732009-09-21 04:32:32 +0000117 if (OpIdx == 2 && MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000118 return true;
Evan Chengb0f59732009-09-21 04:32:32 +0000119 if (OpIdx == 1 && MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000120 return true;
121 return false;
122}
123
Evan Cheng2578ba22009-07-01 01:59:31 +0000124/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
125/// there is one implicit_def for each use. Add isUndef marker to
126/// implicit_def defs and their uses.
127void LiveIntervals::processImplicitDefs() {
128 SmallSet<unsigned, 8> ImpDefRegs;
129 SmallVector<MachineInstr*, 8> ImpDefMIs;
130 MachineBasicBlock *Entry = mf_->begin();
131 SmallPtrSet<MachineBasicBlock*,16> Visited;
132 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
133 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
134 DFI != E; ++DFI) {
135 MachineBasicBlock *MBB = *DFI;
136 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
137 I != E; ) {
138 MachineInstr *MI = &*I;
139 ++I;
140 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
141 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000142 ImpDefRegs.insert(Reg);
Evan Cheng296925d2009-09-23 06:28:31 +0000143 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
144 for (const unsigned *SS = tri_->getSubRegisters(Reg); *SS; ++SS)
145 ImpDefRegs.insert(*SS);
146 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000147 ImpDefMIs.push_back(MI);
148 continue;
149 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000150
Evan Chengb0f59732009-09-21 04:32:32 +0000151 if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
152 MachineOperand &MO = MI->getOperand(2);
153 if (ImpDefRegs.count(MO.getReg())) {
154 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
155 // This is an identity copy, eliminate it now.
156 if (MO.isKill()) {
157 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
158 vi.removeKill(MI);
159 }
160 MI->eraseFromParent();
161 continue;
162 }
163 }
164
Evan Cheng459a7c62009-07-01 08:19:36 +0000165 bool ChangedToImpDef = false;
166 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000167 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000168 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000169 continue;
170 unsigned Reg = MO.getReg();
171 if (!Reg)
172 continue;
173 if (!ImpDefRegs.count(Reg))
174 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000175 // Use is a copy, just turn it into an implicit_def.
Evan Chengb0f59732009-09-21 04:32:32 +0000176 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000177 bool isKill = MO.isKill();
178 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
179 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
180 MI->RemoveOperand(j);
Evan Chengb0f59732009-09-21 04:32:32 +0000181 if (isKill) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000182 ImpDefRegs.erase(Reg);
Evan Chengb0f59732009-09-21 04:32:32 +0000183 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
184 vi.removeKill(MI);
185 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000186 ChangedToImpDef = true;
187 break;
188 }
189
Evan Cheng2578ba22009-07-01 01:59:31 +0000190 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000191 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
192 // Make sure other uses of
193 for (unsigned j = i+1; j != e; ++j) {
194 MachineOperand &MOJ = MI->getOperand(j);
195 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
196 MOJ.setIsUndef();
197 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000198 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000199 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000200 }
201
Evan Cheng459a7c62009-07-01 08:19:36 +0000202 if (ChangedToImpDef) {
203 // Backtrack to process this new implicit_def.
204 --I;
205 } else {
206 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
207 MachineOperand& MO = MI->getOperand(i);
208 if (!MO.isReg() || !MO.isDef())
209 continue;
210 ImpDefRegs.erase(MO.getReg());
211 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000212 }
213 }
214
215 // Any outstanding liveout implicit_def's?
216 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
217 MachineInstr *MI = ImpDefMIs[i];
218 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000219 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
220 !ImpDefRegs.count(Reg)) {
221 // Delete all "local" implicit_def's. That include those which define
222 // physical registers since they cannot be liveout.
223 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000224 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000225 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000226
227 // If there are multiple defs of the same register and at least one
228 // is not an implicit_def, do not insert implicit_def's before the
229 // uses.
230 bool Skip = false;
231 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
232 DE = mri_->def_end(); DI != DE; ++DI) {
233 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
234 Skip = true;
235 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000236 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000237 }
238 if (Skip)
239 continue;
240
Evan Chengd129d732009-07-17 19:43:40 +0000241 // The only implicit_def which we want to keep are those that are live
242 // out of its block.
243 MI->eraseFromParent();
244
Evan Cheng459a7c62009-07-01 08:19:36 +0000245 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
246 UE = mri_->use_end(); UI != UE; ) {
247 MachineOperand &RMO = UI.getOperand();
248 MachineInstr *RMI = &*UI;
249 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000250 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000251 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000252 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000253
254 // Turn a copy use into an implicit_def.
255 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
256 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
257 Reg == SrcReg) {
258 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
259 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
260 RMI->RemoveOperand(j);
261 continue;
262 }
263
Evan Cheng2578ba22009-07-01 01:59:31 +0000264 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
265 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000266 RMO.setReg(NewVReg);
267 RMO.setIsUndef();
268 RMO.setIsKill();
269 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000270 }
271 ImpDefRegs.clear();
272 ImpDefMIs.clear();
273 }
274}
275
Lang Hames86511252009-09-04 20:41:11 +0000276
Owen Anderson80b3ce62008-05-28 20:54:50 +0000277void LiveIntervals::computeNumbering() {
278 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000279 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000280
281 Idx2MBBMap.clear();
282 MBB2IdxMap.clear();
283 mi2iMap_.clear();
284 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000285 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000286 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000287
Owen Andersona1566f22008-07-22 22:46:49 +0000288 FunctionSize = 0;
289
Chris Lattner428b92e2006-09-15 03:57:23 +0000290 // Number MachineInstrs and MachineBasicBlocks.
291 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000292 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
Lang Hames6cc91e32009-10-03 04:31:31 +0000293 std::make_pair(LiveIndex(),LiveIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000294
Lang Hamescc3b0652009-10-03 04:21:37 +0000295 LiveIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000296 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
297 MBB != E; ++MBB) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000298 LiveIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000299
Owen Anderson7fbad272008-07-23 21:37:49 +0000300 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000301 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000302 i2miMap_.push_back(0);
303
Chris Lattner428b92e2006-09-15 03:57:23 +0000304 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
305 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000306
307 if (I == MBB->getFirstTerminator()) {
308 // Leave a gap for before terminators, this is where we will point
309 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000310 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000311 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000312 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000313 assert(inserted &&
314 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000315 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000316 i2miMap_.push_back(0);
317
Lang Hames35f291d2009-09-12 03:34:03 +0000318 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000319 }
320
Chris Lattner428b92e2006-09-15 03:57:23 +0000321 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000322 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000323 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000324 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000325 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000326 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000327
Evan Cheng4ed43292008-10-18 05:21:37 +0000328 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000329 unsigned Slots = I->getDesc().getNumDefs();
330 if (Slots == 0)
331 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000332 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000333 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000334 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000335 }
336
Owen Anderson35578012008-06-16 07:10:49 +0000337 }
Lang Hamesffd13262009-07-09 03:57:02 +0000338
339 if (MBB->getFirstTerminator() == MBB->end()) {
340 // Leave a gap for before terminators, this is where we will point
341 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000342 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000343 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000344 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000345 assert(inserted &&
346 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000347 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000348 i2miMap_.push_back(0);
349
Lang Hames35f291d2009-09-12 03:34:03 +0000350 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000351 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000352
Owen Anderson1fbb4542008-06-16 16:58:24 +0000353 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000354 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000355 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000356 }
Lang Hamesffd13262009-07-09 03:57:02 +0000357
Evan Cheng4ca980e2007-10-17 02:10:22 +0000358 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000359
360 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000361 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000362 for (LiveInterval::iterator LI = OI->second->begin(),
363 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000364
Owen Anderson7eec0c22008-05-29 23:01:22 +0000365 // Remap the start index of the live range to the corresponding new
366 // number, or our best guess at what it _should_ correspond to if the
367 // original instruction has been erased. This is either the following
368 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000369 unsigned index = LI->start.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000370 LiveIndex::Slot offset = LI->start.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000371 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000372 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000373 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000374 // Take the pair containing the index
375 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000376 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000377
Owen Anderson7fbad272008-07-23 21:37:49 +0000378 LI->start = getMBBStartIdx(J->second);
379 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000380 LI->start = LiveIndex(
381 LiveIndex(mi2iMap_[OldI2MI[index]]),
382 (LiveIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000383 }
384
385 // Remap the ending index in the same way that we remapped the start,
386 // except for the final step where we always map to the immediately
387 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000388 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000389 offset = LI->end.getSlot();
390 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000391 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000392 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000393 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000394 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000395
Lang Hames35f291d2009-09-12 03:34:03 +0000396 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000397 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000398 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000399 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
400
401 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000402 LI->end =
Lang Hamescc3b0652009-10-03 04:21:37 +0000403 LiveIndex(mi2iMap_[OldI2MI[index]],
404 (idx == index ? offset : LiveIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000405 else
Lang Hames86511252009-09-04 20:41:11 +0000406 LI->end =
Lang Hames6cc91e32009-10-03 04:31:31 +0000407 LiveIndex(LiveIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000408 }
Owen Anderson788d0412008-08-06 18:35:45 +0000409 }
410
Owen Anderson03857b22008-08-13 21:49:13 +0000411 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
412 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000413 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000414
Owen Anderson7eec0c22008-05-29 23:01:22 +0000415 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000416 // start indices above. VN's with special sentinel defs
417 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000418 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000419 unsigned index = vni->def.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000420 LiveIndex::Slot offset = vni->def.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000421 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000422 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000423 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000424 // Take the pair containing the index
425 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000426 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000427
Owen Anderson91292392008-07-30 17:42:47 +0000428 vni->def = getMBBStartIdx(J->second);
429 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000430 vni->def = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000431 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000432 }
Owen Anderson745825f42008-05-28 22:40:08 +0000433
Owen Anderson7eec0c22008-05-29 23:01:22 +0000434 // Remap the VNInfo kill indices, which works the same as
435 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000436 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000437 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000438 LiveIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000439
Lang Hames86511252009-09-04 20:41:11 +0000440 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000441 assert("Value killed at a load slot.");
442 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000443 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000444 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000445
Lang Hamesffd13262009-07-09 03:57:02 +0000446 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000447 } else {
Lang Hames86511252009-09-04 20:41:11 +0000448 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000449 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000450 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000451 --I;
Lang Hames86511252009-09-04 20:41:11 +0000452 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000453 } else {
454 assert(OldI2MI[index] != 0 &&
455 "Kill refers to instruction not present in index maps.");
Lang Hamescc3b0652009-10-03 04:21:37 +0000456 vni->kills[i] = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000457 }
458
459 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000460 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000461 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
462
463 if (index != OldI2MI.size())
464 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
465 (idx == index ? offset : 0);
466 else
467 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000468 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000469 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000470 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000471 }
Owen Anderson788d0412008-08-06 18:35:45 +0000472 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000473}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000474
Lang Hamesf41538d2009-06-02 16:53:25 +0000475void LiveIntervals::scaleNumbering(int factor) {
476 // Need to
477 // * scale MBB begin and end points
478 // * scale all ranges.
479 // * Update VNI structures.
480 // * Scale instruction numberings
481
482 // Scale the MBB indices.
483 Idx2MBBMap.clear();
484 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
485 MBB != MBBE; ++MBB) {
Lang Hames6cc91e32009-10-03 04:31:31 +0000486 std::pair<LiveIndex, LiveIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
Lang Hames86511252009-09-04 20:41:11 +0000487 mbbIndices.first = mbbIndices.first.scale(factor);
488 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000489 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
490 }
491 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
492
Lang Hamesffd13262009-07-09 03:57:02 +0000493 // Scale terminator gaps.
Lang Hamescc3b0652009-10-03 04:21:37 +0000494 for (DenseMap<MachineBasicBlock*, LiveIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000495 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
496 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000497 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000498 }
499
Lang Hamesf41538d2009-06-02 16:53:25 +0000500 // Scale the intervals.
501 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
502 LI->second->scaleNumbering(factor);
503 }
504
505 // Scale MachineInstrs.
506 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hamescc3b0652009-10-03 04:21:37 +0000507 LiveIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000508 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
509 MI != ME; ++MI) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000510 LiveIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000511 mi2iMap_[MI->first] = newSlot;
512 highestSlot = std::max(highestSlot, newSlot);
513 }
514
Lang Hames86511252009-09-04 20:41:11 +0000515 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000516 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000517 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000518 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
519 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000520 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000521 }
522
523}
524
525
Owen Anderson80b3ce62008-05-28 20:54:50 +0000526/// runOnMachineFunction - Register allocate the whole function
527///
528bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
529 mf_ = &fn;
530 mri_ = &mf_->getRegInfo();
531 tm_ = &fn.getTarget();
532 tri_ = tm_->getRegisterInfo();
533 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000534 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000535 lv_ = &getAnalysis<LiveVariables>();
536 allocatableRegs_ = tri_->getAllocatableSet(fn);
537
Evan Cheng2578ba22009-07-01 01:59:31 +0000538 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000539 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000540 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000541 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000542
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 numIntervals += getNumIntervals();
544
Chris Lattner70ca3582004-09-30 15:59:17 +0000545 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000547}
548
Chris Lattner70ca3582004-09-30 15:59:17 +0000549/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000550void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000551 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000552 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000553 I->second->print(OS, tri_);
554 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000555 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000556
Evan Cheng752195e2009-09-14 21:33:42 +0000557 printInstrs(OS);
558}
559
560void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000561 OS << "********** MACHINEINSTRS **********\n";
562
Chris Lattner3380d5c2009-07-21 21:12:58 +0000563 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
564 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000565 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000566 for (MachineBasicBlock::iterator mii = mbbi->begin(),
567 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000568 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000569 }
570 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000571}
572
Evan Cheng752195e2009-09-14 21:33:42 +0000573void LiveIntervals::dumpInstrs() const {
574 printInstrs(errs());
575}
576
Evan Chengc92da382007-11-03 07:20:12 +0000577/// conflictsWithPhysRegDef - Returns true if the specified register
578/// is defined during the duration of the specified interval.
579bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
580 VirtRegMap &vrm, unsigned reg) {
581 for (LiveInterval::Ranges::const_iterator
582 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000583 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000584 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
585 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000586 // skip deleted instructions
587 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000588 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000589 if (index == end) break;
590
591 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000592 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
593 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000594 if (SrcReg == li.reg || DstReg == li.reg)
595 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000596 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
597 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000598 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000599 continue;
600 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000601 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000602 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000603 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000604 if (!vrm.hasPhys(PhysReg))
605 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000606 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000607 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000608 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000609 return true;
610 }
611 }
612 }
613
614 return false;
615}
616
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000617/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
618/// it can check use as well.
619bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
620 unsigned Reg, bool CheckUse,
621 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
622 for (LiveInterval::Ranges::const_iterator
623 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000624 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000625 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
626 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000627 // Skip deleted instructions.
628 MachineInstr *MI = 0;
629 while (index != end) {
630 MI = getInstructionFromIndex(index);
631 if (MI)
632 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000633 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000634 }
635 if (index == end) break;
636
637 if (JoinedCopies.count(MI))
638 continue;
639 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
640 MachineOperand& MO = MI->getOperand(i);
641 if (!MO.isReg())
642 continue;
643 if (MO.isUse() && !CheckUse)
644 continue;
645 unsigned PhysReg = MO.getReg();
646 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
647 continue;
648 if (tri_->isSubRegister(Reg, PhysReg))
649 return true;
650 }
651 }
652 }
653
654 return false;
655}
656
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000657#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000658static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000659 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000660 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000661 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000662 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000663}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000664#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000665
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000666void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000667 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000668 LiveIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000669 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000670 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000671 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000672 DEBUG({
673 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000674 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000675 });
Evan Cheng419852c2008-04-03 16:39:43 +0000676
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000677 // Virtual registers may be defined multiple times (due to phi
678 // elimination and 2-addr elimination). Much of what we do only has to be
679 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000681 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000682 if (interval.empty()) {
683 // Get the Idx of the defining instructions.
Lang Hamescc3b0652009-10-03 04:21:37 +0000684 LiveIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000685 // Earlyclobbers move back one, so that they overlap the live range
686 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000687 if (MO.isEarlyClobber())
688 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000689 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000690 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000691 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000692 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000693 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000694 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000695 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000696 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000697 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000698 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000699
700 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000701
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 // Loop over all of the blocks that the vreg is defined in. There are
703 // two cases we have to handle here. The most common case is a vreg
704 // whose lifetime is contained within a basic block. In this case there
705 // will be a single kill, in MBB, which comes after the definition.
706 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
707 // FIXME: what about dead vars?
Lang Hamescc3b0652009-10-03 04:21:37 +0000708 LiveIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000709 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000710 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000711 else if (MO.isEarlyClobber())
712 // Earlyclobbers that die in this instruction move up one extra, to
713 // compensate for having the starting point moved back one. This
714 // gets them to overlap the live range of other outputs.
715 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000716 else
Lang Hames35f291d2009-09-12 03:34:03 +0000717 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000718
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000719 // If the kill happens after the definition, we have an intra-block
720 // live range.
721 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000722 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000723 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000724 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000725 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000726 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000727 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000728 return;
729 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000730 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000731
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000732 // The other case we handle is when a virtual register lives to the end
733 // of the defining block, potentially live across some blocks, then is
734 // live into some number of blocks, but gets killed. Start by adding a
735 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000736 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000737 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000738 interval.addRange(NewLR);
739
740 // Iterate over all of the blocks that the variable is completely
741 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
742 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000743 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
744 E = vi.AliveBlocks.end(); I != E; ++I) {
745 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000746 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000747 ValNo);
748 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000749 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000750 }
751
752 // Finally, this virtual register is live from the start of any killing
753 // block to the 'use' slot of the killing instruction.
754 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
755 MachineInstr *Kill = vi.Kills[i];
Lang Hamescc3b0652009-10-03 04:21:37 +0000756 LiveIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000757 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Evan Chengb0f59732009-09-21 04:32:32 +0000758 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000759 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000760 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000761 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000762 }
763
764 } else {
765 // If this is the second time we see a virtual register definition, it
766 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000767 // the result of two address elimination, then the vreg is one of the
768 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000769 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000770 // If this is a two-address definition, then we have already processed
771 // the live range. The only problem is that we didn't realize there
772 // are actually two values in the live interval. Because of this we
773 // need to take the LiveRegion that defines this register and split it
774 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000775 assert(interval.containsOneValue());
Lang Hamescc3b0652009-10-03 04:21:37 +0000776 LiveIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
777 LiveIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000778 if (MO.isEarlyClobber())
779 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000780
Lang Hames35f291d2009-09-12 03:34:03 +0000781 const LiveRange *OldLR =
782 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000783 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000784
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000785 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000786 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000787 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000788
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000789 // Two-address vregs should always only be redefined once. This means
790 // that at this point, there should be exactly one value number in it.
791 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
792
Chris Lattner91725b72006-08-31 05:54:43 +0000793 // The new value number (#1) is defined by the instruction we claimed
794 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000795 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000796 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000797 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000798 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
799
Chris Lattner91725b72006-08-31 05:54:43 +0000800 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000801 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000802 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000803 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000804 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000805
806 // Add the new live interval which replaces the range for the input copy.
807 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000808 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000809 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000810 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000811
812 // If this redefinition is dead, we need to add a dummy unit live
813 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000814 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000815 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000816 LiveRange(RedefIndex, MO.isEarlyClobber() ?
817 getNextSlot(getNextSlot(RedefIndex)) :
818 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000819
Bill Wendling8e6179f2009-08-22 20:18:03 +0000820 DEBUG({
821 errs() << " RESULT: ";
822 interval.print(errs(), tri_);
823 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000824 } else {
825 // Otherwise, this must be because of phi elimination. If this is the
826 // first redefinition of the vreg that we have seen, go back and change
827 // the live range in the PHI block to be a different value number.
828 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000829 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000830 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000831 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000832 phiJoinCopies.push_back(Killer);
Lang Hamescc3b0652009-10-03 04:21:37 +0000833 LiveIndex Start = getMBBStartIdx(Killer->getParent());
834 LiveIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000835 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000836 DEBUG({
837 errs() << " Removing [" << Start << "," << End << "] from: ";
838 interval.print(errs(), tri_);
839 errs() << "\n";
840 });
Lang Hamesffd13262009-07-09 03:57:02 +0000841 interval.removeRange(Start, End);
842 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000843 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000844 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
845 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000846 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000847 DEBUG({
848 errs() << " RESULT: ";
849 interval.print(errs(), tri_);
850 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000851
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000852 // Replace the interval with one of a NEW value number. Note that this
853 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000854 LiveRange LR(Start, End,
Lang Hamescc3b0652009-10-03 04:21:37 +0000855 interval.getNextValue(LiveIndex(mbb->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +0000856 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000857 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000858 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000859 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000860 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000861 DEBUG({
862 errs() << " RESULT: ";
863 interval.print(errs(), tri_);
864 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000865 }
866
867 // In the case of PHI elimination, each variable definition is only
868 // live until the end of the block. We've already taken care of the
869 // rest of the live range.
Lang Hamescc3b0652009-10-03 04:21:37 +0000870 LiveIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000871 if (MO.isEarlyClobber())
872 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000873
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000874 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000875 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000876 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000877 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000878 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000879 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000880 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000881 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000882 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000883
Lang Hamescc3b0652009-10-03 04:21:37 +0000884 LiveIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000885 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000886 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000887 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000888 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000889 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000890 }
891 }
892
Bill Wendling8e6179f2009-08-22 20:18:03 +0000893 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000894}
895
Chris Lattnerf35fef72004-07-23 21:24:19 +0000896void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000897 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000898 LiveIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000899 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000900 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000901 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000902 // A physical register cannot be live across basic block, so its
903 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000904 DEBUG({
905 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000906 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000907 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000908
Lang Hamescc3b0652009-10-03 04:21:37 +0000909 LiveIndex baseIndex = MIIdx;
910 LiveIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000911 // Earlyclobbers move back one.
912 if (MO.isEarlyClobber())
913 start = getUseIndex(MIIdx);
Lang Hamescc3b0652009-10-03 04:21:37 +0000914 LiveIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000915
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000916 // If it is not used after definition, it is considered dead at
917 // the instruction defining it. Hence its interval is:
918 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000919 // For earlyclobbers, the defSlot was pushed back one; the extra
920 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000921 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000922 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000923 if (MO.isEarlyClobber())
924 end = getNextSlot(getNextSlot(start));
925 else
926 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000927 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000928 }
929
930 // If it is not dead on definition, it must be killed by a
931 // subsequent instruction. Hence its interval is:
932 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000933 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000934 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000935 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000936 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000937 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000938 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000939 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000940 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000941 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000942 } else {
943 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
944 if (DefIdx != -1) {
945 if (mi->isRegTiedToUseOperand(DefIdx)) {
946 // Two-address instruction.
947 end = getDefIndex(baseIndex);
948 if (mi->getOperand(DefIdx).isEarlyClobber())
949 end = getUseIndex(baseIndex);
950 } else {
951 // Another instruction redefines the register before it is ever read.
952 // Then the register is essentially dead at the instruction that defines
953 // it. Hence its interval is:
954 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000955 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000956 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000957 }
958 goto exit;
959 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000960 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000961
Lang Hames35f291d2009-09-12 03:34:03 +0000962 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000963 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000964
965 // The only case we should have a dead physreg here without a killing or
966 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000967 // and never used. Another possible case is the implicit use of the
968 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000969 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000970
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000971exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000972 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000973
Evan Cheng24a3cc42007-04-25 07:30:23 +0000974 // Already exists? Extend old live interval.
975 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000976 bool Extend = OldLR != interval.end();
977 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000978 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000979 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000980 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000981 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000982 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000983 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000984 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000985}
986
Chris Lattnerf35fef72004-07-23 21:24:19 +0000987void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
988 MachineBasicBlock::iterator MI,
Lang Hamescc3b0652009-10-03 04:21:37 +0000989 LiveIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000990 MachineOperand& MO,
991 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000992 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000993 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000994 getOrCreateInterval(MO.getReg()));
995 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000996 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000997 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000998 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000999 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +00001000 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +00001001 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001002 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +00001003 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001004 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +00001005 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +00001006 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +00001007 // If MI also modifies the sub-register explicitly, avoid processing it
1008 // more than once. Do not pass in TRI here so it checks for exact match.
1009 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +00001010 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001011 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +00001012 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001013}
1014
Evan Chengb371f452007-02-19 21:49:54 +00001015void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001016 LiveIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001017 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001018 DEBUG({
1019 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001020 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001021 });
Evan Chengb371f452007-02-19 21:49:54 +00001022
1023 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1024 // be considered a livein.
1025 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hamescc3b0652009-10-03 04:21:37 +00001026 LiveIndex baseIndex = MIIdx;
1027 LiveIndex start = baseIndex;
Lang Hames86511252009-09-04 20:41:11 +00001028 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001029 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001030 baseIndex = getNextIndex(baseIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001031 LiveIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001032 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001033
Evan Chengb371f452007-02-19 21:49:54 +00001034 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001035 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001036 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001037 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001038 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001039 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001040 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001041 // Another instruction redefines the register before it is ever read.
1042 // Then the register is essentially dead at the instruction that defines
1043 // it. Hence its interval is:
1044 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001045 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001046 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001047 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001048 break;
Evan Chengb371f452007-02-19 21:49:54 +00001049 }
1050
Lang Hames35f291d2009-09-12 03:34:03 +00001051 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001052 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001053 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001054 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001055 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001056 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001057 }
Evan Chengb371f452007-02-19 21:49:54 +00001058 }
1059
Evan Cheng75611fb2007-06-27 01:16:36 +00001060 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001061 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001062 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001063 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001064 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001065 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001066 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001067 end = baseIndex;
1068 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001069 }
1070
Lang Hames10382fb2009-06-19 02:17:53 +00001071 VNInfo *vni =
Lang Hamescc3b0652009-10-03 04:21:37 +00001072 interval.getNextValue(LiveIndex(MBB->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +00001073 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001074 vni->setIsPHIDef(true);
1075 LiveRange LR(start, end, vni);
1076
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001077 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001078 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001079 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001080}
1081
Evan Cheng752195e2009-09-14 21:33:42 +00001082bool
1083LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1084 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001085 SmallVector<MachineInstr*,16> &OtherCopies) {
1086 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001087 unsigned NumIdent = 0;
Dan Gohman2bf06492009-09-25 22:26:13 +00001088 for (MachineRegisterInfo::def_iterator ri = mri_->def_begin(SrcInt.reg),
1089 re = mri_->def_end(); ri != re; ++ri) {
Evan Cheng752195e2009-09-14 21:33:42 +00001090 MachineInstr *MI = &*ri;
1091 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1092 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001093 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001094 if (SrcReg != DstInt.reg) {
1095 OtherCopies.push_back(MI);
1096 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1097 } else {
1098 IdentCopies.push_back(MI);
1099 ++NumIdent;
1100 }
1101 }
1102
Evan Cheng3f855492009-09-15 06:45:16 +00001103 if (!HaveConflict)
1104 return false; // Let coalescer handle it
1105 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001106}
1107
1108void LiveIntervals::performEarlyCoalescing() {
1109 if (!EarlyCoalescing)
1110 return;
1111
1112 /// Perform early coalescing: eliminate copies which feed into phi joins
1113 /// and whose sources are defined by the phi joins.
1114 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1115 MachineInstr *Join = phiJoinCopies[i];
1116 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1117 break;
1118
1119 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1120 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1121#ifndef NDEBUG
1122 assert(isMove && "PHI join instruction must be a move!");
1123#else
1124 isMove = isMove;
1125#endif
1126
1127 LiveInterval &DstInt = getInterval(PHIDst);
1128 LiveInterval &SrcInt = getInterval(PHISrc);
1129 SmallVector<MachineInstr*, 16> IdentCopies;
1130 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001131 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001132 continue;
1133
1134 DEBUG(errs() << "PHI Join: " << *Join);
1135 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1136 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001137
Evan Cheng3f855492009-09-15 06:45:16 +00001138 // Change the non-identity copies to directly target the phi destination.
1139 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1140 MachineInstr *PHICopy = OtherCopies[i];
1141 DEBUG(errs() << "Moving: " << *PHICopy);
1142
Lang Hamescc3b0652009-10-03 04:21:37 +00001143 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1144 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng752195e2009-09-14 21:33:42 +00001145 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001146 LiveIndex StartIndex = SLR->start;
1147 LiveIndex EndIndex = SLR->end;
Evan Cheng752195e2009-09-14 21:33:42 +00001148
1149 // Delete val# defined by the now identity copy and add the range from
1150 // beginning of the mbb to the end of the range.
1151 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001152 DEBUG(errs() << " added range [" << StartIndex << ','
1153 << EndIndex << "] to reg" << DstInt.reg << '\n');
1154 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001155 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001156 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1157 VNInfoAllocator);
1158 NewVNI->setHasPHIKill(true);
1159 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1160 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1161 MachineOperand &MO = PHICopy->getOperand(j);
1162 if (!MO.isReg() || MO.getReg() != PHISrc)
1163 continue;
1164 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001165 }
Evan Cheng3f855492009-09-15 06:45:16 +00001166 }
1167
1168 // Now let's eliminate all the would-be identity copies.
1169 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1170 MachineInstr *PHICopy = IdentCopies[i];
1171 DEBUG(errs() << "Coalescing: " << *PHICopy);
1172
Lang Hamescc3b0652009-10-03 04:21:37 +00001173 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1174 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001175 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001176 LiveIndex StartIndex = SLR->start;
1177 LiveIndex EndIndex = SLR->end;
Evan Cheng3f855492009-09-15 06:45:16 +00001178
1179 // Delete val# defined by the now identity copy and add the range from
1180 // beginning of the mbb to the end of the range.
1181 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001182 RemoveMachineInstrFromMaps(PHICopy);
1183 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001184 DEBUG(errs() << " added range [" << StartIndex << ','
1185 << EndIndex << "] to reg" << DstInt.reg << '\n');
1186 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001187 }
Evan Cheng752195e2009-09-14 21:33:42 +00001188
Evan Cheng3f855492009-09-15 06:45:16 +00001189 // Remove the phi join and update the phi block liveness.
Lang Hamescc3b0652009-10-03 04:21:37 +00001190 LiveIndex MIIndex = getInstructionIndex(Join);
1191 LiveIndex UseIndex = getUseIndex(MIIndex);
1192 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001193 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1194 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1195 DLR->valno->setCopy(0);
1196 DLR->valno->setIsDefAccurate(false);
1197 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1198 SrcInt.removeRange(SLR->start, SLR->end);
1199 assert(SrcInt.empty());
1200 removeInterval(PHISrc);
1201 RemoveMachineInstrFromMaps(Join);
1202 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001203
1204 ++numCoalescing;
1205 }
1206}
1207
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001208/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001209/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001210/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001211/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001212void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001213 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001214 << "********** Function: "
1215 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001216
1217 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001218 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1219 MBBI != E; ++MBBI) {
1220 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001221 // Track the index of the current machine instr.
Lang Hamescc3b0652009-10-03 04:21:37 +00001222 LiveIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001223 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001224
Chris Lattner428b92e2006-09-15 03:57:23 +00001225 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001226
Dan Gohmancb406c22007-10-03 19:26:29 +00001227 // Create intervals for live-ins to this BB first.
1228 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1229 LE = MBB->livein_end(); LI != LE; ++LI) {
1230 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1231 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001232 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001233 if (!hasInterval(*AS))
1234 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1235 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001236 }
1237
Owen Anderson99500ae2008-09-15 22:00:38 +00001238 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001239 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001240 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001241 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001242
Chris Lattner428b92e2006-09-15 03:57:23 +00001243 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001244 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001245
Evan Cheng438f7bc2006-11-10 08:43:01 +00001246 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001247 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1248 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001249 if (!MO.isReg() || !MO.getReg())
1250 continue;
1251
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001252 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001253 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001254 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001255 else if (MO.isUndef())
1256 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001257 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001258
1259 // Skip over the empty slots after each instruction.
1260 unsigned Slots = MI->getDesc().getNumDefs();
1261 if (Slots == 0)
1262 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001263
1264 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001265 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001266
1267 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001268 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001269 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001270 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001271 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001272 }
Evan Chengd129d732009-07-17 19:43:40 +00001273
1274 // Create empty intervals for registers defined by implicit_def's (except
1275 // for those implicit_def that define values which are liveout of their
1276 // blocks.
1277 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1278 unsigned UndefReg = UndefUses[i];
1279 (void)getOrCreateInterval(UndefReg);
1280 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001281}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001282
Lang Hames86511252009-09-04 20:41:11 +00001283bool LiveIntervals::findLiveInMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001284 LiveIndex Start, LiveIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001285 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001286 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001287 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001288
1289 bool ResVal = false;
1290 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001291 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001292 break;
1293 MBBs.push_back(I->second);
1294 ResVal = true;
1295 ++I;
1296 }
1297 return ResVal;
1298}
1299
Lang Hames86511252009-09-04 20:41:11 +00001300bool LiveIntervals::findReachableMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001301 LiveIndex Start, LiveIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001302 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1303 std::vector<IdxMBBPair>::const_iterator I =
1304 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1305
1306 bool ResVal = false;
1307 while (I != Idx2MBBMap.end()) {
1308 if (I->first > End)
1309 break;
1310 MachineBasicBlock *MBB = I->second;
1311 if (getMBBEndIdx(MBB) > End)
1312 break;
1313 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1314 SE = MBB->succ_end(); SI != SE; ++SI)
1315 MBBs.push_back(*SI);
1316 ResVal = true;
1317 ++I;
1318 }
1319 return ResVal;
1320}
1321
Owen Anderson03857b22008-08-13 21:49:13 +00001322LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001323 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001324 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001325}
Evan Chengf2fbca62007-11-12 06:35:08 +00001326
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001327/// dupInterval - Duplicate a live interval. The caller is responsible for
1328/// managing the allocated memory.
1329LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1330 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001331 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001332 return NewLI;
1333}
1334
Evan Chengc8d044e2008-02-15 18:24:29 +00001335/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1336/// copy field and returns the source register that defines it.
1337unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001338 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001339 return 0;
1340
Lang Hames52c1afc2009-08-10 23:43:28 +00001341 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001342 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001343 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001344 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001345 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001346 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001347 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1348 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1349 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001350
Evan Cheng04ee5a12009-01-20 19:12:24 +00001351 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001352 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001353 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001354 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001355 return 0;
1356}
Evan Chengf2fbca62007-11-12 06:35:08 +00001357
1358//===----------------------------------------------------------------------===//
1359// Register allocator hooks.
1360//
1361
Evan Chengd70dbb52008-02-22 09:24:50 +00001362/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1363/// allow one) virtual register operand, then its uses are implicitly using
1364/// the register. Returns the virtual register.
1365unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1366 MachineInstr *MI) const {
1367 unsigned RegOp = 0;
1368 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1369 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001370 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001371 continue;
1372 unsigned Reg = MO.getReg();
1373 if (Reg == 0 || Reg == li.reg)
1374 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001375
1376 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1377 !allocatableRegs_[Reg])
1378 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001379 // FIXME: For now, only remat MI with at most one register operand.
1380 assert(!RegOp &&
1381 "Can't rematerialize instruction with multiple register operand!");
1382 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001383#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001384 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001385#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001386 }
1387 return RegOp;
1388}
1389
1390/// isValNoAvailableAt - Return true if the val# of the specified interval
1391/// which reaches the given instruction also reaches the specified use index.
1392bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001393 LiveIndex UseIdx) const {
1394 LiveIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001395 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1396 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1397 return UI != li.end() && UI->valno == ValNo;
1398}
1399
Evan Chengf2fbca62007-11-12 06:35:08 +00001400/// isReMaterializable - Returns true if the definition MI of the specified
1401/// val# of the specified interval is re-materializable.
1402bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001403 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001404 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001405 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001406 if (DisableReMat)
1407 return false;
1408
Dan Gohmana70dca12009-10-09 23:27:56 +00001409 if (!tii_->isTriviallyReMaterializable(MI, aa_))
1410 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +00001411
Dan Gohmana70dca12009-10-09 23:27:56 +00001412 // Target-specific code can mark an instruction as being rematerializable
1413 // if it has one virtual reg use, though it had better be something like
1414 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001415 unsigned ImpUse = getReMatImplicitUse(li, MI);
1416 if (ImpUse) {
1417 const LiveInterval &ImpLi = getInterval(ImpUse);
1418 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1419 re = mri_->use_end(); ri != re; ++ri) {
1420 MachineInstr *UseMI = &*ri;
Lang Hamescc3b0652009-10-03 04:21:37 +00001421 LiveIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001422 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1423 continue;
1424 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1425 return false;
1426 }
Evan Chengdc377862008-09-30 15:44:16 +00001427
1428 // If a register operand of the re-materialized instruction is going to
1429 // be spilled next, then it's not legal to re-materialize this instruction.
1430 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1431 if (ImpUse == SpillIs[i]->reg)
1432 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001433 }
1434 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001435}
1436
Evan Cheng06587492008-10-24 02:05:00 +00001437/// isReMaterializable - Returns true if the definition MI of the specified
1438/// val# of the specified interval is re-materializable.
1439bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1440 const VNInfo *ValNo, MachineInstr *MI) {
1441 SmallVector<LiveInterval*, 4> Dummy1;
1442 bool Dummy2;
1443 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1444}
1445
Evan Cheng5ef3a042007-12-06 00:01:56 +00001446/// isReMaterializable - Returns true if every definition of MI of every
1447/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001448bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1449 SmallVectorImpl<LiveInterval*> &SpillIs,
1450 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001451 isLoad = false;
1452 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1453 i != e; ++i) {
1454 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001455 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001456 continue; // Dead val#.
1457 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001458 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001459 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001460 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001461 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001462 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001463 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001464 return false;
1465 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001466 }
1467 return true;
1468}
1469
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001470/// FilterFoldedOps - Filter out two-address use operands. Return
1471/// true if it finds any issue with the operands that ought to prevent
1472/// folding.
1473static bool FilterFoldedOps(MachineInstr *MI,
1474 SmallVector<unsigned, 2> &Ops,
1475 unsigned &MRInfo,
1476 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001477 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001478 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1479 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001480 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001481 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001482 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001483 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001484 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001485 MRInfo |= (unsigned)VirtRegMap::isMod;
1486 else {
1487 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001488 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001489 MRInfo = VirtRegMap::isModRef;
1490 continue;
1491 }
1492 MRInfo |= (unsigned)VirtRegMap::isRef;
1493 }
1494 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001495 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001496 return false;
1497}
1498
1499
1500/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1501/// slot / to reg or any rematerialized load into ith operand of specified
1502/// MI. If it is successul, MI is updated with the newly created MI and
1503/// returns true.
1504bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1505 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001506 LiveIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001507 SmallVector<unsigned, 2> &Ops,
1508 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001509 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001510 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001511 RemoveMachineInstrFromMaps(MI);
1512 vrm.RemoveMachineInstrFromMaps(MI);
1513 MI->eraseFromParent();
1514 ++numFolds;
1515 return true;
1516 }
1517
1518 // Filter the list of operand indexes that are to be folded. Abort if
1519 // any operand will prevent folding.
1520 unsigned MRInfo = 0;
1521 SmallVector<unsigned, 2> FoldOps;
1522 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1523 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001524
Evan Cheng427f4c12008-03-31 23:19:51 +00001525 // The only time it's safe to fold into a two address instruction is when
1526 // it's folding reload and spill from / into a spill stack slot.
1527 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001528 return false;
1529
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001530 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1531 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001532 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001533 // Remember this instruction uses the spill slot.
1534 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1535
Evan Chengf2fbca62007-11-12 06:35:08 +00001536 // Attempt to fold the memory reference into the instruction. If
1537 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001538 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001539 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001540 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001541 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001542 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001543 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001544 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001545 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001546 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001547 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001548 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001549 return true;
1550 }
1551 return false;
1552}
1553
Evan Cheng018f9b02007-12-05 03:22:34 +00001554/// canFoldMemoryOperand - Returns true if the specified load / store
1555/// folding is possible.
1556bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001557 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001558 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001559 // Filter the list of operand indexes that are to be folded. Abort if
1560 // any operand will prevent folding.
1561 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001562 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001563 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1564 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001565
Evan Cheng3c75ba82008-04-01 21:37:32 +00001566 // It's only legal to remat for a use, not a def.
1567 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001568 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001569
Evan Chengd70dbb52008-02-22 09:24:50 +00001570 return tii_->canFoldMemoryOperand(MI, FoldOps);
1571}
1572
Evan Cheng81a03822007-11-17 00:40:40 +00001573bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1574 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1575 for (LiveInterval::Ranges::const_iterator
1576 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1577 std::vector<IdxMBBPair>::const_iterator II =
1578 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1579 if (II == Idx2MBBMap.end())
1580 continue;
1581 if (I->end > II->first) // crossing a MBB.
1582 return false;
1583 MBBs.insert(II->second);
1584 if (MBBs.size() > 1)
1585 return false;
1586 }
1587 return true;
1588}
1589
Evan Chengd70dbb52008-02-22 09:24:50 +00001590/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1591/// interval on to-be re-materialized operands of MI) with new register.
1592void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1593 MachineInstr *MI, unsigned NewVReg,
1594 VirtRegMap &vrm) {
1595 // There is an implicit use. That means one of the other operand is
1596 // being remat'ed and the remat'ed instruction has li.reg as an
1597 // use operand. Make sure we rewrite that as well.
1598 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1599 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001600 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001601 continue;
1602 unsigned Reg = MO.getReg();
1603 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1604 continue;
1605 if (!vrm.isReMaterialized(Reg))
1606 continue;
1607 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001608 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1609 if (UseMO)
1610 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001611 }
1612}
1613
Evan Chengf2fbca62007-11-12 06:35:08 +00001614/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1615/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001616bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001617rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames6cc91e32009-10-03 04:31:31 +00001618 bool TrySplit, LiveIndex index, LiveIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001619 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001620 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001621 unsigned Slot, int LdSlot,
1622 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001623 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001624 const TargetRegisterClass* rc,
1625 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001626 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001627 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001628 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001629 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001630 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001631 RestartInstruction:
1632 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1633 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001634 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001635 continue;
1636 unsigned Reg = mop.getReg();
1637 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001638 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001639 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001640 if (Reg != li.reg)
1641 continue;
1642
1643 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001644 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001645 int FoldSlot = Slot;
1646 if (DefIsReMat) {
1647 // If this is the rematerializable definition MI itself and
1648 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001649 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001650 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1651 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001652 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001653 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001654 MI->eraseFromParent();
1655 break;
1656 }
1657
1658 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001659 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001660 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001661 if (isLoad) {
1662 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1663 FoldSS = isLoadSS;
1664 FoldSlot = LdSlot;
1665 }
1666 }
1667
Evan Chengf2fbca62007-11-12 06:35:08 +00001668 // Scan all of the operands of this instruction rewriting operands
1669 // to use NewVReg instead of li.reg as appropriate. We do this for
1670 // two reasons:
1671 //
1672 // 1. If the instr reads the same spilled vreg multiple times, we
1673 // want to reuse the NewVReg.
1674 // 2. If the instr is a two-addr instruction, we are required to
1675 // keep the src/dst regs pinned.
1676 //
1677 // Keep track of whether we replace a use and/or def so that we can
1678 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001679
Evan Cheng81a03822007-11-17 00:40:40 +00001680 HasUse = mop.isUse();
1681 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001682 SmallVector<unsigned, 2> Ops;
1683 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001684 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001685 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001686 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001687 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001688 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001689 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001690 continue;
1691 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001692 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001693 if (!MOj.isUndef()) {
1694 HasUse |= MOj.isUse();
1695 HasDef |= MOj.isDef();
1696 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001697 }
1698 }
1699
David Greene26b86a02008-10-27 17:38:59 +00001700 // Create a new virtual register for the spill interval.
1701 // Create the new register now so we can map the fold instruction
1702 // to the new register so when it is unfolded we get the correct
1703 // answer.
1704 bool CreatedNewVReg = false;
1705 if (NewVReg == 0) {
1706 NewVReg = mri_->createVirtualRegister(rc);
1707 vrm.grow();
1708 CreatedNewVReg = true;
1709 }
1710
Evan Cheng9c3c2212008-06-06 07:54:39 +00001711 if (!TryFold)
1712 CanFold = false;
1713 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001714 // Do not fold load / store here if we are splitting. We'll find an
1715 // optimal point to insert a load / store later.
1716 if (!TrySplit) {
1717 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001718 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001719 // Folding the load/store can completely change the instruction in
1720 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001721
1722 if (FoldSS) {
1723 // We need to give the new vreg the same stack slot as the
1724 // spilled interval.
1725 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1726 }
1727
Evan Cheng018f9b02007-12-05 03:22:34 +00001728 HasUse = false;
1729 HasDef = false;
1730 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001731 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001732 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001733 goto RestartInstruction;
1734 }
1735 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001736 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001737 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001738 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001739 }
Evan Chengcddbb832007-11-30 21:23:43 +00001740
Evan Chengcddbb832007-11-30 21:23:43 +00001741 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001742 if (mop.isImplicit())
1743 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001744
1745 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001746 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1747 MachineOperand &mopj = MI->getOperand(Ops[j]);
1748 mopj.setReg(NewVReg);
1749 if (mopj.isImplicit())
1750 rewriteImplicitOps(li, MI, NewVReg, vrm);
1751 }
Evan Chengcddbb832007-11-30 21:23:43 +00001752
Evan Cheng81a03822007-11-17 00:40:40 +00001753 if (CreatedNewVReg) {
1754 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001755 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001756 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001757 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001758 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001759 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001760 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001761 }
1762 if (!CanDelete || (HasUse && HasDef)) {
1763 // If this is a two-addr instruction then its use operands are
1764 // rematerializable but its def is not. It should be assigned a
1765 // stack slot.
1766 vrm.assignVirt2StackSlot(NewVReg, Slot);
1767 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001768 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001769 vrm.assignVirt2StackSlot(NewVReg, Slot);
1770 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001771 } else if (HasUse && HasDef &&
1772 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1773 // If this interval hasn't been assigned a stack slot (because earlier
1774 // def is a deleted remat def), do it now.
1775 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1776 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001777 }
1778
Evan Cheng313d4b82008-02-23 00:33:04 +00001779 // Re-matting an instruction with virtual register use. Add the
1780 // register as an implicit use on the use MI.
1781 if (DefIsReMat && ImpUse)
1782 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1783
Evan Cheng5b69eba2009-04-21 22:46:52 +00001784 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001785 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001786 if (CreatedNewVReg) {
1787 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001788 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001789 if (TrySplit)
1790 vrm.setIsSplitFromReg(NewVReg, li.reg);
1791 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001792
1793 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001794 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001795 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hamescc3b0652009-10-03 04:21:37 +00001796 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001797 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001798 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001799 nI.addRange(LR);
1800 } else {
1801 // Extend the split live interval to this def / use.
Lang Hamescc3b0652009-10-03 04:21:37 +00001802 LiveIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001803 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1804 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001805 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001806 nI.addRange(LR);
1807 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001808 }
1809 if (HasDef) {
1810 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00001811 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001812 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001813 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001814 nI.addRange(LR);
1815 }
Evan Cheng81a03822007-11-17 00:40:40 +00001816
Bill Wendling8e6179f2009-08-22 20:18:03 +00001817 DEBUG({
1818 errs() << "\t\t\t\tAdded new interval: ";
1819 nI.print(errs(), tri_);
1820 errs() << '\n';
1821 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001822 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001823 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001824}
Evan Cheng81a03822007-11-17 00:40:40 +00001825bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001826 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001827 MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001828 LiveIndex Idx) const {
1829 LiveIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001830 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001831 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001832 continue;
1833
Lang Hamescc3b0652009-10-03 04:21:37 +00001834 LiveIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001835 if (KillIdx > Idx && KillIdx < End)
1836 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001837 }
1838 return false;
1839}
1840
Evan Cheng063284c2008-02-21 00:34:19 +00001841/// RewriteInfo - Keep track of machine instrs that will be rewritten
1842/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001843namespace {
1844 struct RewriteInfo {
Lang Hamescc3b0652009-10-03 04:21:37 +00001845 LiveIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001846 MachineInstr *MI;
1847 bool HasUse;
1848 bool HasDef;
Lang Hamescc3b0652009-10-03 04:21:37 +00001849 RewriteInfo(LiveIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001850 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1851 };
Evan Cheng063284c2008-02-21 00:34:19 +00001852
Dan Gohman844731a2008-05-13 00:00:25 +00001853 struct RewriteInfoCompare {
1854 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1855 return LHS.Index < RHS.Index;
1856 }
1857 };
1858}
Evan Cheng063284c2008-02-21 00:34:19 +00001859
Evan Chengf2fbca62007-11-12 06:35:08 +00001860void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001861rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001862 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001863 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001864 unsigned Slot, int LdSlot,
1865 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001866 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001867 const TargetRegisterClass* rc,
1868 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001869 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001870 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001871 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001872 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001873 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1874 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001875 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001876 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001877 unsigned NewVReg = 0;
Lang Hamescc3b0652009-10-03 04:21:37 +00001878 LiveIndex start = getBaseIndex(I->start);
1879 LiveIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001880
Evan Cheng063284c2008-02-21 00:34:19 +00001881 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001882 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001883 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001884 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1885 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001886 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001887 MachineOperand &O = ri.getOperand();
1888 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001889 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hamescc3b0652009-10-03 04:21:37 +00001890 LiveIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001891 if (index < start || index >= end)
1892 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001893
1894 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001895 // Must be defined by an implicit def. It should not be spilled. Note,
1896 // this is for correctness reason. e.g.
1897 // 8 %reg1024<def> = IMPLICIT_DEF
1898 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1899 // The live range [12, 14) are not part of the r1024 live interval since
1900 // it's defined by an implicit def. It will not conflicts with live
1901 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001902 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001903 // the INSERT_SUBREG and both target registers that would overlap.
1904 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001905 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1906 }
1907 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1908
Evan Cheng313d4b82008-02-23 00:33:04 +00001909 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001910 // Now rewrite the defs and uses.
1911 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1912 RewriteInfo &rwi = RewriteMIs[i];
1913 ++i;
Lang Hamescc3b0652009-10-03 04:21:37 +00001914 LiveIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001915 bool MIHasUse = rwi.HasUse;
1916 bool MIHasDef = rwi.HasDef;
1917 MachineInstr *MI = rwi.MI;
1918 // If MI def and/or use the same register multiple times, then there
1919 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001920 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001921 while (i != e && RewriteMIs[i].MI == MI) {
1922 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001923 bool isUse = RewriteMIs[i].HasUse;
1924 if (isUse) ++NumUses;
1925 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001926 MIHasDef |= RewriteMIs[i].HasDef;
1927 ++i;
1928 }
Evan Cheng81a03822007-11-17 00:40:40 +00001929 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001930
Evan Cheng0a891ed2008-05-23 23:00:04 +00001931 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001932 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001933 // register interval's spill weight to HUGE_VALF to prevent it from
1934 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001935 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001936 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001937 }
1938
Evan Cheng063284c2008-02-21 00:34:19 +00001939 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001940 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001941 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001942 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001943 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001944 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001945 // One common case:
1946 // x = use
1947 // ...
1948 // ...
1949 // def = ...
1950 // = use
1951 // It's better to start a new interval to avoid artifically
1952 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001953 if (MIHasDef && !MIHasUse) {
1954 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001955 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001956 }
1957 }
Evan Chengcada2452007-11-28 01:28:46 +00001958 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001959
1960 bool IsNew = ThisVReg == 0;
1961 if (IsNew) {
1962 // This ends the previous live interval. If all of its def / use
1963 // can be folded, give it a low spill weight.
1964 if (NewVReg && TrySplit && AllCanFold) {
1965 LiveInterval &nI = getOrCreateInterval(NewVReg);
1966 nI.weight /= 10.0F;
1967 }
1968 AllCanFold = true;
1969 }
1970 NewVReg = ThisVReg;
1971
Evan Cheng81a03822007-11-17 00:40:40 +00001972 bool HasDef = false;
1973 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001974 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001975 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1976 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1977 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001978 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001979 if (!HasDef && !HasUse)
1980 continue;
1981
Evan Cheng018f9b02007-12-05 03:22:34 +00001982 AllCanFold &= CanFold;
1983
Evan Cheng81a03822007-11-17 00:40:40 +00001984 // Update weight of spill interval.
1985 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001986 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001987 // The spill weight is now infinity as it cannot be spilled again.
1988 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001989 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001990 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001991
1992 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001993 if (HasDef) {
1994 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001995 bool HasKill = false;
1996 if (!HasUse)
1997 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1998 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001999 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00002000 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002001 if (VNI)
2002 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2003 }
Owen Anderson28998312008-08-13 22:28:50 +00002004 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002005 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002006 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002007 if (SII == SpillIdxes.end()) {
2008 std::vector<SRInfo> S;
2009 S.push_back(SRInfo(index, NewVReg, true));
2010 SpillIdxes.insert(std::make_pair(MBBId, S));
2011 } else if (SII->second.back().vreg != NewVReg) {
2012 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002013 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002014 // If there is an earlier def and this is a two-address
2015 // instruction, then it's not possible to fold the store (which
2016 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002017 SRInfo &Info = SII->second.back();
2018 Info.index = index;
2019 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002020 }
2021 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002022 } else if (SII != SpillIdxes.end() &&
2023 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002024 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002025 // There is an earlier def that's not killed (must be two-address).
2026 // The spill is no longer needed.
2027 SII->second.pop_back();
2028 if (SII->second.empty()) {
2029 SpillIdxes.erase(MBBId);
2030 SpillMBBs.reset(MBBId);
2031 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002032 }
2033 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002034 }
2035
2036 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002037 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002038 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002039 if (SII != SpillIdxes.end() &&
2040 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002041 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002042 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002043 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002044 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002045 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002046 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002047 // If we are splitting live intervals, only fold if it's the first
2048 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002049 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002050 else if (IsNew) {
2051 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002052 if (RII == RestoreIdxes.end()) {
2053 std::vector<SRInfo> Infos;
2054 Infos.push_back(SRInfo(index, NewVReg, true));
2055 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2056 } else {
2057 RII->second.push_back(SRInfo(index, NewVReg, true));
2058 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002059 RestoreMBBs.set(MBBId);
2060 }
2061 }
2062
2063 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002064 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002065 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002066 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002067
2068 if (NewVReg && TrySplit && AllCanFold) {
2069 // If all of its def / use can be folded, give it a low spill weight.
2070 LiveInterval &nI = getOrCreateInterval(NewVReg);
2071 nI.weight /= 10.0F;
2072 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002073}
2074
Lang Hamescc3b0652009-10-03 04:21:37 +00002075bool LiveIntervals::alsoFoldARestore(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002076 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002077 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002078 if (!RestoreMBBs[Id])
2079 return false;
2080 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2081 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2082 if (Restores[i].index == index &&
2083 Restores[i].vreg == vr &&
2084 Restores[i].canFold)
2085 return true;
2086 return false;
2087}
2088
Lang Hamescc3b0652009-10-03 04:21:37 +00002089void LiveIntervals::eraseRestoreInfo(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002090 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002091 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002092 if (!RestoreMBBs[Id])
2093 return;
2094 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2095 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2096 if (Restores[i].index == index && Restores[i].vreg)
Lang Hamescc3b0652009-10-03 04:21:37 +00002097 Restores[i].index = LiveIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002098}
Evan Cheng81a03822007-11-17 00:40:40 +00002099
Evan Cheng4cce6b42008-04-11 17:53:36 +00002100/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2101/// spilled and create empty intervals for their uses.
2102void
2103LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2104 const TargetRegisterClass* rc,
2105 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002106 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2107 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002108 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002109 MachineInstr *MI = &*ri;
2110 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002111 if (O.isDef()) {
2112 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2113 "Register def was not rewritten?");
2114 RemoveMachineInstrFromMaps(MI);
2115 vrm.RemoveMachineInstrFromMaps(MI);
2116 MI->eraseFromParent();
2117 } else {
2118 // This must be an use of an implicit_def so it's not part of the live
2119 // interval. Create a new empty live interval for it.
2120 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2121 unsigned NewVReg = mri_->createVirtualRegister(rc);
2122 vrm.grow();
2123 vrm.setIsImplicitlyDefined(NewVReg);
2124 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2125 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2126 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002127 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002128 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002129 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002130 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002131 }
2132 }
Evan Cheng419852c2008-04-03 16:39:43 +00002133 }
2134}
2135
Evan Chengf2fbca62007-11-12 06:35:08 +00002136std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002137addIntervalsForSpillsFast(const LiveInterval &li,
2138 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002139 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002140 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002141
2142 std::vector<LiveInterval*> added;
2143
2144 assert(li.weight != HUGE_VALF &&
2145 "attempt to spill already spilled interval!");
2146
Bill Wendling8e6179f2009-08-22 20:18:03 +00002147 DEBUG({
2148 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2149 li.dump();
2150 errs() << '\n';
2151 });
Owen Andersond6664312008-08-18 18:05:32 +00002152
2153 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2154
Owen Andersona41e47a2008-08-19 22:12:11 +00002155 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2156 while (RI != mri_->reg_end()) {
2157 MachineInstr* MI = &*RI;
2158
2159 SmallVector<unsigned, 2> Indices;
2160 bool HasUse = false;
2161 bool HasDef = false;
2162
2163 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2164 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002165 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002166
2167 HasUse |= MI->getOperand(i).isUse();
2168 HasDef |= MI->getOperand(i).isDef();
2169
2170 Indices.push_back(i);
2171 }
2172
2173 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2174 Indices, true, slot, li.reg)) {
2175 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002176 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002177 vrm.assignVirt2StackSlot(NewVReg, slot);
2178
Owen Andersona41e47a2008-08-19 22:12:11 +00002179 // create a new register for this spill
2180 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002181
Owen Andersona41e47a2008-08-19 22:12:11 +00002182 // the spill weight is now infinity as it
2183 // cannot be spilled again
2184 nI.weight = HUGE_VALF;
2185
2186 // Rewrite register operands to use the new vreg.
2187 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2188 E = Indices.end(); I != E; ++I) {
2189 MI->getOperand(*I).setReg(NewVReg);
2190
2191 if (MI->getOperand(*I).isUse())
2192 MI->getOperand(*I).setIsKill(true);
2193 }
2194
2195 // Fill in the new live interval.
Lang Hamescc3b0652009-10-03 04:21:37 +00002196 LiveIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002197 if (HasUse) {
2198 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002199 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002200 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002201 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002202 nI.addRange(LR);
2203 vrm.addRestorePoint(NewVReg, MI);
2204 }
2205 if (HasDef) {
2206 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002207 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002208 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002209 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002210 nI.addRange(LR);
2211 vrm.addSpillPoint(NewVReg, true, MI);
2212 }
2213
Owen Anderson17197312008-08-18 23:41:04 +00002214 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002215
Bill Wendling8e6179f2009-08-22 20:18:03 +00002216 DEBUG({
2217 errs() << "\t\t\t\tadded new interval: ";
2218 nI.dump();
2219 errs() << '\n';
2220 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002221 }
Owen Anderson9a032932008-08-18 21:20:32 +00002222
Owen Anderson9a032932008-08-18 21:20:32 +00002223
Owen Andersona41e47a2008-08-19 22:12:11 +00002224 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002225 }
Owen Andersond6664312008-08-18 18:05:32 +00002226
2227 return added;
2228}
2229
2230std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002231addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002232 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002233 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002234
2235 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002236 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002237
Evan Chengf2fbca62007-11-12 06:35:08 +00002238 assert(li.weight != HUGE_VALF &&
2239 "attempt to spill already spilled interval!");
2240
Bill Wendling8e6179f2009-08-22 20:18:03 +00002241 DEBUG({
2242 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2243 li.print(errs(), tri_);
2244 errs() << '\n';
2245 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002246
Evan Cheng72eeb942008-12-05 17:00:16 +00002247 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002248 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002249 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002250 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002251 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2252 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002253 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002254 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002255
2256 unsigned NumValNums = li.getNumValNums();
2257 SmallVector<MachineInstr*, 4> ReMatDefs;
2258 ReMatDefs.resize(NumValNums, NULL);
2259 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2260 ReMatOrigDefs.resize(NumValNums, NULL);
2261 SmallVector<int, 4> ReMatIds;
2262 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2263 BitVector ReMatDelete(NumValNums);
2264 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2265
Evan Cheng81a03822007-11-17 00:40:40 +00002266 // Spilling a split live interval. It cannot be split any further. Also,
2267 // it's also guaranteed to be a single val# / range interval.
2268 if (vrm.getPreSplitReg(li.reg)) {
2269 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002270 // Unset the split kill marker on the last use.
Lang Hamescc3b0652009-10-03 04:21:37 +00002271 LiveIndex KillIdx = vrm.getKillPoint(li.reg);
2272 if (KillIdx != LiveIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002273 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2274 assert(KillMI && "Last use disappeared?");
2275 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2276 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002277 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002278 }
Evan Chengadf85902007-12-05 09:51:10 +00002279 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002280 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2281 Slot = vrm.getStackSlot(li.reg);
2282 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2283 MachineInstr *ReMatDefMI = DefIsReMat ?
2284 vrm.getReMaterializedMI(li.reg) : NULL;
2285 int LdSlot = 0;
2286 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2287 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002288 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002289 bool IsFirstRange = true;
2290 for (LiveInterval::Ranges::const_iterator
2291 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2292 // If this is a split live interval with multiple ranges, it means there
2293 // are two-address instructions that re-defined the value. Only the
2294 // first def can be rematerialized!
2295 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002296 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002297 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2298 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002299 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002300 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002301 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002302 } else {
2303 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2304 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002305 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002306 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002307 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002308 }
2309 IsFirstRange = false;
2310 }
Evan Cheng419852c2008-04-03 16:39:43 +00002311
Evan Cheng4cce6b42008-04-11 17:53:36 +00002312 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002313 return NewLIs;
2314 }
2315
Evan Cheng752195e2009-09-14 21:33:42 +00002316 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002317 if (TrySplit)
2318 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002319 bool NeedStackSlot = false;
2320 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2321 i != e; ++i) {
2322 const VNInfo *VNI = *i;
2323 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002324 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002325 continue; // Dead val#.
2326 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002327 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2328 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002329 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002330 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002331 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002332 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002333 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002334 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002335 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002336 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002337
2338 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002339 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002340 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002341 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002342 CanDelete = false;
2343 // Need a stack slot if there is any live range where uses cannot be
2344 // rematerialized.
2345 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002346 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002347 if (CanDelete)
2348 ReMatDelete.set(VN);
2349 } else {
2350 // Need a stack slot if there is any live range where uses cannot be
2351 // rematerialized.
2352 NeedStackSlot = true;
2353 }
2354 }
2355
2356 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002357 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2358 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2359 Slot = vrm.assignVirt2StackSlot(li.reg);
2360
2361 // This case only occurs when the prealloc splitter has already assigned
2362 // a stack slot to this vreg.
2363 else
2364 Slot = vrm.getStackSlot(li.reg);
2365 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002366
2367 // Create new intervals and rewrite defs and uses.
2368 for (LiveInterval::Ranges::const_iterator
2369 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002370 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2371 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2372 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002373 bool CanDelete = ReMatDelete[I->valno->id];
2374 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002375 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002376 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002377 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002378 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002379 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002380 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002381 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002382 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002383 }
2384
Evan Cheng0cbb1162007-11-29 01:06:25 +00002385 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002386 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002387 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002388 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002389 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002390
Evan Chengb50bb8c2007-12-05 08:16:32 +00002391 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002392 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002393 if (NeedStackSlot) {
2394 int Id = SpillMBBs.find_first();
2395 while (Id != -1) {
2396 std::vector<SRInfo> &spills = SpillIdxes[Id];
2397 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002398 LiveIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002399 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002400 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002401 bool isReMat = vrm.isReMaterialized(VReg);
2402 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002403 bool CanFold = false;
2404 bool FoundUse = false;
2405 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002406 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002407 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002408 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2409 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002410 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002411 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002412
2413 Ops.push_back(j);
2414 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002415 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002416 if (isReMat ||
2417 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2418 RestoreMBBs, RestoreIdxes))) {
2419 // MI has two-address uses of the same register. If the use
2420 // isn't the first and only use in the BB, then we can't fold
2421 // it. FIXME: Move this to rewriteInstructionsForSpills.
2422 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002423 break;
2424 }
Evan Chengaee4af62007-12-02 08:30:39 +00002425 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002426 }
2427 }
2428 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002429 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002430 if (CanFold && !Ops.empty()) {
2431 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002432 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002433 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002434 // Also folded uses, do not issue a load.
2435 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002436 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002437 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002438 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002439 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002440 }
2441
Evan Cheng7e073ba2008-04-09 20:57:25 +00002442 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002443 if (!Folded) {
2444 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2445 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002446 if (!MI->registerDefIsDead(nI.reg))
2447 // No need to spill a dead def.
2448 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002449 if (isKill)
2450 AddedKill.insert(&nI);
2451 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002452 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002453 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002454 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002455 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002456
Evan Cheng1953d0c2007-11-29 10:12:14 +00002457 int Id = RestoreMBBs.find_first();
2458 while (Id != -1) {
2459 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2460 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002461 LiveIndex index = restores[i].index;
2462 if (index == LiveIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002463 continue;
2464 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002465 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002466 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002467 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002468 bool CanFold = false;
2469 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002470 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002471 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002472 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2473 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002474 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002475 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002476
Evan Cheng0cbb1162007-11-29 01:06:25 +00002477 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002478 // If this restore were to be folded, it would have been folded
2479 // already.
2480 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002481 break;
2482 }
Evan Chengaee4af62007-12-02 08:30:39 +00002483 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002484 }
2485 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002486
2487 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002488 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002489 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002490 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002491 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2492 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002493 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2494 int LdSlot = 0;
2495 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2496 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002497 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002498 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2499 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002500 if (!Folded) {
2501 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2502 if (ImpUse) {
2503 // Re-matting an instruction with virtual register use. Add the
2504 // register as an implicit use on the use MI and update the register
2505 // interval's spill weight to HUGE_VALF to prevent it from being
2506 // spilled.
2507 LiveInterval &ImpLi = getInterval(ImpUse);
2508 ImpLi.weight = HUGE_VALF;
2509 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2510 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002511 }
Evan Chengaee4af62007-12-02 08:30:39 +00002512 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002513 }
2514 // If folding is not possible / failed, then tell the spiller to issue a
2515 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002516 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002517 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002518 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002519 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002520 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002521 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002522 }
2523
Evan Chengb50bb8c2007-12-05 08:16:32 +00002524 // Finalize intervals: add kills, finalize spill weights, and filter out
2525 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002526 std::vector<LiveInterval*> RetNewLIs;
2527 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2528 LiveInterval *LI = NewLIs[i];
2529 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002530 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002531 if (!AddedKill.count(LI)) {
2532 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hamescc3b0652009-10-03 04:21:37 +00002533 LiveIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002534 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002535 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002536 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002537 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002538 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002539 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002540 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002541 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002542 RetNewLIs.push_back(LI);
2543 }
2544 }
Evan Cheng81a03822007-11-17 00:40:40 +00002545
Evan Cheng4cce6b42008-04-11 17:53:36 +00002546 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002547 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002548}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002549
2550/// hasAllocatableSuperReg - Return true if the specified physical register has
2551/// any super register that's allocatable.
2552bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2553 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2554 if (allocatableRegs_[*AS] && hasInterval(*AS))
2555 return true;
2556 return false;
2557}
2558
2559/// getRepresentativeReg - Find the largest super register of the specified
2560/// physical register.
2561unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2562 // Find the largest super-register that is allocatable.
2563 unsigned BestReg = Reg;
2564 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2565 unsigned SuperReg = *AS;
2566 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2567 BestReg = SuperReg;
2568 break;
2569 }
2570 }
2571 return BestReg;
2572}
2573
2574/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2575/// specified interval that conflicts with the specified physical register.
2576unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2577 unsigned PhysReg) const {
2578 unsigned NumConflicts = 0;
2579 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2580 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2581 E = mri_->reg_end(); I != E; ++I) {
2582 MachineOperand &O = I.getOperand();
2583 MachineInstr *MI = O.getParent();
Lang Hamescc3b0652009-10-03 04:21:37 +00002584 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002585 if (pli.liveAt(Index))
2586 ++NumConflicts;
2587 }
2588 return NumConflicts;
2589}
2590
2591/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002592/// around all defs and uses of the specified interval. Return true if it
2593/// was able to cut its interval.
2594bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002595 unsigned PhysReg, VirtRegMap &vrm) {
2596 unsigned SpillReg = getRepresentativeReg(PhysReg);
2597
2598 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2599 // If there are registers which alias PhysReg, but which are not a
2600 // sub-register of the chosen representative super register. Assert
2601 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002602 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002603 tri_->isSuperRegister(*AS, SpillReg));
2604
Evan Cheng2824a652009-03-23 18:24:37 +00002605 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002606 LiveInterval &pli = getInterval(SpillReg);
2607 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2608 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2609 E = mri_->reg_end(); I != E; ++I) {
2610 MachineOperand &O = I.getOperand();
2611 MachineInstr *MI = O.getParent();
2612 if (SeenMIs.count(MI))
2613 continue;
2614 SeenMIs.insert(MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002615 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002616 if (pli.liveAt(Index)) {
2617 vrm.addEmergencySpill(SpillReg, MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002618 LiveIndex StartIdx = getLoadIndex(Index);
2619 LiveIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002620 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002621 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002622 Cut = true;
2623 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002624 std::string msg;
2625 raw_string_ostream Msg(msg);
2626 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002627 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002628 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002629 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002630 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002631 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002632 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002633 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002634 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2635 if (!hasInterval(*AS))
2636 continue;
2637 LiveInterval &spli = getInterval(*AS);
2638 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002639 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002640 }
2641 }
2642 }
Evan Cheng2824a652009-03-23 18:24:37 +00002643 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002644}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002645
2646LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002647 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002648 LiveInterval& Interval = getOrCreateInterval(reg);
2649 VNInfo* VN = Interval.getNextValue(
Lang Hames6cc91e32009-10-03 04:31:31 +00002650 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames86511252009-09-04 20:41:11 +00002651 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002652 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002653 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2654 LiveRange LR(
Lang Hames6cc91e32009-10-03 04:31:31 +00002655 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002656 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002657 Interval.addRange(LR);
2658
2659 return LR;
2660}
David Greeneb5257662009-08-03 21:55:09 +00002661