blob: 79f46f3395af022a824b62dfb4ff439bc4c59db1 [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 Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Owen Andersonae339ba2008-08-19 00:17:30 +000052static cl::opt<bool> EnableFastSpilling("fast-spill",
53 cl::init(false), cl::Hidden);
54
Evan Cheng752195e2009-09-14 21:33:42 +000055static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
56
57static cl::opt<int> CoalescingLimit("early-coalescing-limit",
58 cl::init(-1), cl::Hidden);
59
60STATISTIC(numIntervals , "Number of original intervals");
61STATISTIC(numFolds , "Number of loads/stores folded into instructions");
62STATISTIC(numSplits , "Number of intervals split");
63STATISTIC(numCoalescing, "Number of early coalescing performed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000064
Devang Patel19974732007-05-03 01:11:54 +000065char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000066static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000067
Chris Lattnerf7da2c72006-08-24 22:43:55 +000068void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000069 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000070 AU.addRequired<AliasAnalysis>();
71 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000072 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000074 AU.addPreservedID(MachineLoopInfoID);
75 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000076
77 if (!StrongPHIElim) {
78 AU.addPreservedID(PHIEliminationID);
79 AU.addRequiredID(PHIEliminationID);
80 }
81
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000082 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000084}
85
Chris Lattnerf7da2c72006-08-24 22:43:55 +000086void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000087 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000088 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000089 E = r2iMap_.end(); I != E; ++I)
90 delete I->second;
91
Evan Cheng3f32d652008-06-04 09:18:41 +000092 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000093 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000094 mi2iMap_.clear();
95 i2miMap_.clear();
96 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000097 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +000098 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000099
Evan Chengdd199d22007-09-06 01:07:24 +0000100 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
101 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000102 while (!CloneMIs.empty()) {
103 MachineInstr *MI = CloneMIs.back();
104 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000105 mf_->DeleteMachineInstr(MI);
106 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000107}
108
Evan Cheng6ade93b2009-08-05 03:53:14 +0000109static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
Evan Chengb0f59732009-09-21 04:32:32 +0000110 unsigned OpIdx, const TargetInstrInfo *tii_){
Evan Cheng6ade93b2009-08-05 03:53:14 +0000111 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
112 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
113 Reg == SrcReg)
114 return true;
115
Evan Chengb0f59732009-09-21 04:32:32 +0000116 if (OpIdx == 2 && MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000117 return true;
Evan Chengb0f59732009-09-21 04:32:32 +0000118 if (OpIdx == 1 && MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000119 return true;
120 return false;
121}
122
Evan Cheng2578ba22009-07-01 01:59:31 +0000123/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
124/// there is one implicit_def for each use. Add isUndef marker to
125/// implicit_def defs and their uses.
126void LiveIntervals::processImplicitDefs() {
127 SmallSet<unsigned, 8> ImpDefRegs;
128 SmallVector<MachineInstr*, 8> ImpDefMIs;
129 MachineBasicBlock *Entry = mf_->begin();
130 SmallPtrSet<MachineBasicBlock*,16> Visited;
131 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
132 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
133 DFI != E; ++DFI) {
134 MachineBasicBlock *MBB = *DFI;
135 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
136 I != E; ) {
137 MachineInstr *MI = &*I;
138 ++I;
139 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
140 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000141 ImpDefRegs.insert(Reg);
Evan Cheng296925d2009-09-23 06:28:31 +0000142 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
143 for (const unsigned *SS = tri_->getSubRegisters(Reg); *SS; ++SS)
144 ImpDefRegs.insert(*SS);
145 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000146 ImpDefMIs.push_back(MI);
147 continue;
148 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000149
Evan Chengb0f59732009-09-21 04:32:32 +0000150 if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
151 MachineOperand &MO = MI->getOperand(2);
152 if (ImpDefRegs.count(MO.getReg())) {
153 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
154 // This is an identity copy, eliminate it now.
155 if (MO.isKill()) {
156 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
157 vi.removeKill(MI);
158 }
159 MI->eraseFromParent();
160 continue;
161 }
162 }
163
Evan Cheng459a7c62009-07-01 08:19:36 +0000164 bool ChangedToImpDef = false;
165 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000166 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000167 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000168 continue;
169 unsigned Reg = MO.getReg();
170 if (!Reg)
171 continue;
172 if (!ImpDefRegs.count(Reg))
173 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000174 // Use is a copy, just turn it into an implicit_def.
Evan Chengb0f59732009-09-21 04:32:32 +0000175 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000176 bool isKill = MO.isKill();
177 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
178 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
179 MI->RemoveOperand(j);
Evan Chengb0f59732009-09-21 04:32:32 +0000180 if (isKill) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000181 ImpDefRegs.erase(Reg);
Evan Chengb0f59732009-09-21 04:32:32 +0000182 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
183 vi.removeKill(MI);
184 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000185 ChangedToImpDef = true;
186 break;
187 }
188
Evan Cheng2578ba22009-07-01 01:59:31 +0000189 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000190 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
191 // Make sure other uses of
192 for (unsigned j = i+1; j != e; ++j) {
193 MachineOperand &MOJ = MI->getOperand(j);
194 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
195 MOJ.setIsUndef();
196 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000197 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000198 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000199 }
200
Evan Cheng459a7c62009-07-01 08:19:36 +0000201 if (ChangedToImpDef) {
202 // Backtrack to process this new implicit_def.
203 --I;
204 } else {
205 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
206 MachineOperand& MO = MI->getOperand(i);
207 if (!MO.isReg() || !MO.isDef())
208 continue;
209 ImpDefRegs.erase(MO.getReg());
210 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000211 }
212 }
213
214 // Any outstanding liveout implicit_def's?
215 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
216 MachineInstr *MI = ImpDefMIs[i];
217 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000218 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
219 !ImpDefRegs.count(Reg)) {
220 // Delete all "local" implicit_def's. That include those which define
221 // physical registers since they cannot be liveout.
222 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000223 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000224 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000225
226 // If there are multiple defs of the same register and at least one
227 // is not an implicit_def, do not insert implicit_def's before the
228 // uses.
229 bool Skip = false;
230 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
231 DE = mri_->def_end(); DI != DE; ++DI) {
232 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
233 Skip = true;
234 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000235 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000236 }
237 if (Skip)
238 continue;
239
Evan Chengd129d732009-07-17 19:43:40 +0000240 // The only implicit_def which we want to keep are those that are live
241 // out of its block.
242 MI->eraseFromParent();
243
Evan Cheng459a7c62009-07-01 08:19:36 +0000244 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
245 UE = mri_->use_end(); UI != UE; ) {
246 MachineOperand &RMO = UI.getOperand();
247 MachineInstr *RMI = &*UI;
248 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000249 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000250 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000251 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000252
253 // Turn a copy use into an implicit_def.
254 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
255 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
256 Reg == SrcReg) {
257 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
258 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
259 RMI->RemoveOperand(j);
260 continue;
261 }
262
Evan Cheng2578ba22009-07-01 01:59:31 +0000263 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
264 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000265 RMO.setReg(NewVReg);
266 RMO.setIsUndef();
267 RMO.setIsKill();
268 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000269 }
270 ImpDefRegs.clear();
271 ImpDefMIs.clear();
272 }
273}
274
Lang Hames86511252009-09-04 20:41:11 +0000275
Owen Anderson80b3ce62008-05-28 20:54:50 +0000276void LiveIntervals::computeNumbering() {
277 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000278 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000279
280 Idx2MBBMap.clear();
281 MBB2IdxMap.clear();
282 mi2iMap_.clear();
283 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000284 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000285 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000286
Owen Andersona1566f22008-07-22 22:46:49 +0000287 FunctionSize = 0;
288
Chris Lattner428b92e2006-09-15 03:57:23 +0000289 // Number MachineInstrs and MachineBasicBlocks.
290 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000291 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
Lang Hames6cc91e32009-10-03 04:31:31 +0000292 std::make_pair(LiveIndex(),LiveIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000293
Lang Hamescc3b0652009-10-03 04:21:37 +0000294 LiveIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000295 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
296 MBB != E; ++MBB) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000297 LiveIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000298
Owen Anderson7fbad272008-07-23 21:37:49 +0000299 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000300 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000301 i2miMap_.push_back(0);
302
Chris Lattner428b92e2006-09-15 03:57:23 +0000303 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
304 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000305
306 if (I == MBB->getFirstTerminator()) {
307 // Leave a gap for before terminators, this is where we will point
308 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000309 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000310 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000311 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000312 assert(inserted &&
313 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000314 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000315 i2miMap_.push_back(0);
316
Lang Hames35f291d2009-09-12 03:34:03 +0000317 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000318 }
319
Chris Lattner428b92e2006-09-15 03:57:23 +0000320 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000321 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000322 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000323 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000324 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000325 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000326
Evan Cheng4ed43292008-10-18 05:21:37 +0000327 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000328 unsigned Slots = I->getDesc().getNumDefs();
329 if (Slots == 0)
330 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000331 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000332 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000333 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000334 }
335
Owen Anderson35578012008-06-16 07:10:49 +0000336 }
Lang Hamesffd13262009-07-09 03:57:02 +0000337
338 if (MBB->getFirstTerminator() == MBB->end()) {
339 // Leave a gap for before terminators, this is where we will point
340 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000341 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000342 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000343 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000344 assert(inserted &&
345 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000346 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000347 i2miMap_.push_back(0);
348
Lang Hames35f291d2009-09-12 03:34:03 +0000349 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000350 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000351
Owen Anderson1fbb4542008-06-16 16:58:24 +0000352 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000353 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000354 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000355 }
Lang Hamesffd13262009-07-09 03:57:02 +0000356
Evan Cheng4ca980e2007-10-17 02:10:22 +0000357 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000358
359 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000360 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000361 for (LiveInterval::iterator LI = OI->second->begin(),
362 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000363
Owen Anderson7eec0c22008-05-29 23:01:22 +0000364 // Remap the start index of the live range to the corresponding new
365 // number, or our best guess at what it _should_ correspond to if the
366 // original instruction has been erased. This is either the following
367 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000368 unsigned index = LI->start.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000369 LiveIndex::Slot offset = LI->start.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000370 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000371 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000372 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000373 // Take the pair containing the index
374 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000375 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000376
Owen Anderson7fbad272008-07-23 21:37:49 +0000377 LI->start = getMBBStartIdx(J->second);
378 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000379 LI->start = LiveIndex(
380 LiveIndex(mi2iMap_[OldI2MI[index]]),
381 (LiveIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000382 }
383
384 // Remap the ending index in the same way that we remapped the start,
385 // except for the final step where we always map to the immediately
386 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000387 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000388 offset = LI->end.getSlot();
389 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000390 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000391 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000392 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000393 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000394
Lang Hames35f291d2009-09-12 03:34:03 +0000395 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000396 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000397 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000398 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
399
400 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000401 LI->end =
Lang Hamescc3b0652009-10-03 04:21:37 +0000402 LiveIndex(mi2iMap_[OldI2MI[index]],
403 (idx == index ? offset : LiveIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000404 else
Lang Hames86511252009-09-04 20:41:11 +0000405 LI->end =
Lang Hames6cc91e32009-10-03 04:31:31 +0000406 LiveIndex(LiveIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000407 }
Owen Anderson788d0412008-08-06 18:35:45 +0000408 }
409
Owen Anderson03857b22008-08-13 21:49:13 +0000410 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
411 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000412 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000413
Owen Anderson7eec0c22008-05-29 23:01:22 +0000414 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000415 // start indices above. VN's with special sentinel defs
416 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000417 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000418 unsigned index = vni->def.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000419 LiveIndex::Slot offset = vni->def.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000420 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000421 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000422 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000423 // Take the pair containing the index
424 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000425 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000426
Owen Anderson91292392008-07-30 17:42:47 +0000427 vni->def = getMBBStartIdx(J->second);
428 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000429 vni->def = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000430 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000431 }
Owen Anderson745825f42008-05-28 22:40:08 +0000432
Owen Anderson7eec0c22008-05-29 23:01:22 +0000433 // Remap the VNInfo kill indices, which works the same as
434 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000435 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000436 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000437 LiveIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000438
Lang Hames86511252009-09-04 20:41:11 +0000439 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000440 assert("Value killed at a load slot.");
441 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000442 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000443 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000444
Lang Hamesffd13262009-07-09 03:57:02 +0000445 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000446 } else {
Lang Hames86511252009-09-04 20:41:11 +0000447 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000448 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000449 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000450 --I;
Lang Hames86511252009-09-04 20:41:11 +0000451 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000452 } else {
453 assert(OldI2MI[index] != 0 &&
454 "Kill refers to instruction not present in index maps.");
Lang Hamescc3b0652009-10-03 04:21:37 +0000455 vni->kills[i] = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000456 }
457
458 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000459 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000460 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
461
462 if (index != OldI2MI.size())
463 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
464 (idx == index ? offset : 0);
465 else
466 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000467 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000468 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000469 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000470 }
Owen Anderson788d0412008-08-06 18:35:45 +0000471 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000472}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000473
Lang Hamesf41538d2009-06-02 16:53:25 +0000474void LiveIntervals::scaleNumbering(int factor) {
475 // Need to
476 // * scale MBB begin and end points
477 // * scale all ranges.
478 // * Update VNI structures.
479 // * Scale instruction numberings
480
481 // Scale the MBB indices.
482 Idx2MBBMap.clear();
483 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
484 MBB != MBBE; ++MBB) {
Lang Hames6cc91e32009-10-03 04:31:31 +0000485 std::pair<LiveIndex, LiveIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
Lang Hames86511252009-09-04 20:41:11 +0000486 mbbIndices.first = mbbIndices.first.scale(factor);
487 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000488 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
489 }
490 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
491
Lang Hamesffd13262009-07-09 03:57:02 +0000492 // Scale terminator gaps.
Lang Hamescc3b0652009-10-03 04:21:37 +0000493 for (DenseMap<MachineBasicBlock*, LiveIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000494 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
495 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000496 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000497 }
498
Lang Hamesf41538d2009-06-02 16:53:25 +0000499 // Scale the intervals.
500 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
501 LI->second->scaleNumbering(factor);
502 }
503
504 // Scale MachineInstrs.
505 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hamescc3b0652009-10-03 04:21:37 +0000506 LiveIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000507 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
508 MI != ME; ++MI) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000509 LiveIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000510 mi2iMap_[MI->first] = newSlot;
511 highestSlot = std::max(highestSlot, newSlot);
512 }
513
Lang Hames86511252009-09-04 20:41:11 +0000514 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000515 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000516 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000517 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
518 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000519 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000520 }
521
522}
523
524
Owen Anderson80b3ce62008-05-28 20:54:50 +0000525/// runOnMachineFunction - Register allocate the whole function
526///
527bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
528 mf_ = &fn;
529 mri_ = &mf_->getRegInfo();
530 tm_ = &fn.getTarget();
531 tri_ = tm_->getRegisterInfo();
532 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000533 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000534 lv_ = &getAnalysis<LiveVariables>();
535 allocatableRegs_ = tri_->getAllocatableSet(fn);
536
Evan Cheng2578ba22009-07-01 01:59:31 +0000537 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000538 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000540 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000541
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000542 numIntervals += getNumIntervals();
543
Chris Lattner70ca3582004-09-30 15:59:17 +0000544 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000545 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000546}
547
Chris Lattner70ca3582004-09-30 15:59:17 +0000548/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000549void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000550 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000551 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000552 I->second->print(OS, tri_);
553 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000554 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000555
Evan Cheng752195e2009-09-14 21:33:42 +0000556 printInstrs(OS);
557}
558
559void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000560 OS << "********** MACHINEINSTRS **********\n";
561
Chris Lattner3380d5c2009-07-21 21:12:58 +0000562 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
563 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000564 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000565 for (MachineBasicBlock::iterator mii = mbbi->begin(),
566 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000567 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000568 }
569 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000570}
571
Evan Cheng752195e2009-09-14 21:33:42 +0000572void LiveIntervals::dumpInstrs() const {
573 printInstrs(errs());
574}
575
Evan Chengc92da382007-11-03 07:20:12 +0000576/// conflictsWithPhysRegDef - Returns true if the specified register
577/// is defined during the duration of the specified interval.
578bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
579 VirtRegMap &vrm, unsigned reg) {
580 for (LiveInterval::Ranges::const_iterator
581 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000582 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000583 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
584 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000585 // skip deleted instructions
586 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000587 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000588 if (index == end) break;
589
590 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000591 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
592 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000593 if (SrcReg == li.reg || DstReg == li.reg)
594 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000595 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
596 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000597 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000598 continue;
599 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000600 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000601 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000602 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000603 if (!vrm.hasPhys(PhysReg))
604 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000605 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000606 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000607 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000608 return true;
609 }
610 }
611 }
612
613 return false;
614}
615
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000616/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
617/// it can check use as well.
618bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
619 unsigned Reg, bool CheckUse,
620 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
621 for (LiveInterval::Ranges::const_iterator
622 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000623 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000624 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
625 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000626 // Skip deleted instructions.
627 MachineInstr *MI = 0;
628 while (index != end) {
629 MI = getInstructionFromIndex(index);
630 if (MI)
631 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000632 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000633 }
634 if (index == end) break;
635
636 if (JoinedCopies.count(MI))
637 continue;
638 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
639 MachineOperand& MO = MI->getOperand(i);
640 if (!MO.isReg())
641 continue;
642 if (MO.isUse() && !CheckUse)
643 continue;
644 unsigned PhysReg = MO.getReg();
645 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
646 continue;
647 if (tri_->isSubRegister(Reg, PhysReg))
648 return true;
649 }
650 }
651 }
652
653 return false;
654}
655
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000656#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000657static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000658 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000659 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000660 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000661 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000662}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000663#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000664
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000665void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000666 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000667 LiveIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000668 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000669 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000670 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000671 DEBUG({
672 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000673 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000674 });
Evan Cheng419852c2008-04-03 16:39:43 +0000675
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000676 // Virtual registers may be defined multiple times (due to phi
677 // elimination and 2-addr elimination). Much of what we do only has to be
678 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000680 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000681 if (interval.empty()) {
682 // Get the Idx of the defining instructions.
Lang Hamescc3b0652009-10-03 04:21:37 +0000683 LiveIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000684 // Earlyclobbers move back one, so that they overlap the live range
685 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000686 if (MO.isEarlyClobber())
687 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000688 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000689 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000690 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000691 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000692 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000693 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000694 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000695 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000696 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000697 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000698
699 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000700
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 // Loop over all of the blocks that the vreg is defined in. There are
702 // two cases we have to handle here. The most common case is a vreg
703 // whose lifetime is contained within a basic block. In this case there
704 // will be a single kill, in MBB, which comes after the definition.
705 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
706 // FIXME: what about dead vars?
Lang Hamescc3b0652009-10-03 04:21:37 +0000707 LiveIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000708 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000709 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000710 else if (MO.isEarlyClobber())
711 // Earlyclobbers that die in this instruction move up one extra, to
712 // compensate for having the starting point moved back one. This
713 // gets them to overlap the live range of other outputs.
714 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000715 else
Lang Hames35f291d2009-09-12 03:34:03 +0000716 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000717
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000718 // If the kill happens after the definition, we have an intra-block
719 // live range.
720 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000721 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000722 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000723 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000724 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000725 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000726 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727 return;
728 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000729 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000730
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000731 // The other case we handle is when a virtual register lives to the end
732 // of the defining block, potentially live across some blocks, then is
733 // live into some number of blocks, but gets killed. Start by adding a
734 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000735 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000736 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000737 interval.addRange(NewLR);
738
739 // Iterate over all of the blocks that the variable is completely
740 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
741 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000742 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
743 E = vi.AliveBlocks.end(); I != E; ++I) {
744 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000745 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000746 ValNo);
747 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000748 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000749 }
750
751 // Finally, this virtual register is live from the start of any killing
752 // block to the 'use' slot of the killing instruction.
753 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
754 MachineInstr *Kill = vi.Kills[i];
Lang Hamescc3b0652009-10-03 04:21:37 +0000755 LiveIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000756 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Evan Chengb0f59732009-09-21 04:32:32 +0000757 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000758 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000759 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000760 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000761 }
762
763 } else {
764 // If this is the second time we see a virtual register definition, it
765 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000766 // the result of two address elimination, then the vreg is one of the
767 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000768 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000769 // If this is a two-address definition, then we have already processed
770 // the live range. The only problem is that we didn't realize there
771 // are actually two values in the live interval. Because of this we
772 // need to take the LiveRegion that defines this register and split it
773 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000774 assert(interval.containsOneValue());
Lang Hamescc3b0652009-10-03 04:21:37 +0000775 LiveIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
776 LiveIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000777 if (MO.isEarlyClobber())
778 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000779
Lang Hames35f291d2009-09-12 03:34:03 +0000780 const LiveRange *OldLR =
781 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000782 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000783
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000784 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000785 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000786 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000787
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000788 // Two-address vregs should always only be redefined once. This means
789 // that at this point, there should be exactly one value number in it.
790 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
791
Chris Lattner91725b72006-08-31 05:54:43 +0000792 // The new value number (#1) is defined by the instruction we claimed
793 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000794 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000795 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000796 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000797 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
798
Chris Lattner91725b72006-08-31 05:54:43 +0000799 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000800 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000801 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000802 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000803 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000804
805 // Add the new live interval which replaces the range for the input copy.
806 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000807 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000808 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000809 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000810
811 // If this redefinition is dead, we need to add a dummy unit live
812 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000813 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000814 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000815 LiveRange(RedefIndex, MO.isEarlyClobber() ?
816 getNextSlot(getNextSlot(RedefIndex)) :
817 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000818
Bill Wendling8e6179f2009-08-22 20:18:03 +0000819 DEBUG({
820 errs() << " RESULT: ";
821 interval.print(errs(), tri_);
822 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000823 } else {
824 // Otherwise, this must be because of phi elimination. If this is the
825 // first redefinition of the vreg that we have seen, go back and change
826 // the live range in the PHI block to be a different value number.
827 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000828 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000829 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000830 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000831 phiJoinCopies.push_back(Killer);
Lang Hamescc3b0652009-10-03 04:21:37 +0000832 LiveIndex Start = getMBBStartIdx(Killer->getParent());
833 LiveIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000834 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000835 DEBUG({
836 errs() << " Removing [" << Start << "," << End << "] from: ";
837 interval.print(errs(), tri_);
838 errs() << "\n";
839 });
Lang Hamesffd13262009-07-09 03:57:02 +0000840 interval.removeRange(Start, End);
841 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000842 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000843 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
844 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000845 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000846 DEBUG({
847 errs() << " RESULT: ";
848 interval.print(errs(), tri_);
849 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000850
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000851 // Replace the interval with one of a NEW value number. Note that this
852 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000853 LiveRange LR(Start, End,
Lang Hamescc3b0652009-10-03 04:21:37 +0000854 interval.getNextValue(LiveIndex(mbb->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +0000855 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000856 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000857 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000858 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000859 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000860 DEBUG({
861 errs() << " RESULT: ";
862 interval.print(errs(), tri_);
863 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000864 }
865
866 // In the case of PHI elimination, each variable definition is only
867 // live until the end of the block. We've already taken care of the
868 // rest of the live range.
Lang Hamescc3b0652009-10-03 04:21:37 +0000869 LiveIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000870 if (MO.isEarlyClobber())
871 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000872
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000873 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000874 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000875 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000876 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000877 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000878 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000879 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000880 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000881 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000882
Lang Hamescc3b0652009-10-03 04:21:37 +0000883 LiveIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000884 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000885 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000886 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000887 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000888 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000889 }
890 }
891
Bill Wendling8e6179f2009-08-22 20:18:03 +0000892 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000893}
894
Chris Lattnerf35fef72004-07-23 21:24:19 +0000895void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000896 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000897 LiveIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000898 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000899 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000900 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000901 // A physical register cannot be live across basic block, so its
902 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000903 DEBUG({
904 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000905 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000906 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000907
Lang Hamescc3b0652009-10-03 04:21:37 +0000908 LiveIndex baseIndex = MIIdx;
909 LiveIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000910 // Earlyclobbers move back one.
911 if (MO.isEarlyClobber())
912 start = getUseIndex(MIIdx);
Lang Hamescc3b0652009-10-03 04:21:37 +0000913 LiveIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000914
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000915 // If it is not used after definition, it is considered dead at
916 // the instruction defining it. Hence its interval is:
917 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000918 // For earlyclobbers, the defSlot was pushed back one; the extra
919 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000920 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000921 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000922 if (MO.isEarlyClobber())
923 end = getNextSlot(getNextSlot(start));
924 else
925 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000926 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000927 }
928
929 // If it is not dead on definition, it must be killed by a
930 // subsequent instruction. Hence its interval is:
931 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000932 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000933 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000934 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000935 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000936 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000937 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000938 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000939 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000940 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000941 } else {
942 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
943 if (DefIdx != -1) {
944 if (mi->isRegTiedToUseOperand(DefIdx)) {
945 // Two-address instruction.
946 end = getDefIndex(baseIndex);
947 if (mi->getOperand(DefIdx).isEarlyClobber())
948 end = getUseIndex(baseIndex);
949 } else {
950 // Another instruction redefines the register before it is ever read.
951 // Then the register is essentially dead at the instruction that defines
952 // it. Hence its interval is:
953 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000954 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000955 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000956 }
957 goto exit;
958 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000959 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000960
Lang Hames35f291d2009-09-12 03:34:03 +0000961 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000962 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000963
964 // The only case we should have a dead physreg here without a killing or
965 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000966 // and never used. Another possible case is the implicit use of the
967 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000968 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000969
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000970exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000971 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000972
Evan Cheng24a3cc42007-04-25 07:30:23 +0000973 // Already exists? Extend old live interval.
974 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000975 bool Extend = OldLR != interval.end();
976 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000977 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000978 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000979 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000980 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000981 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000982 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000983 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000984}
985
Chris Lattnerf35fef72004-07-23 21:24:19 +0000986void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
987 MachineBasicBlock::iterator MI,
Lang Hamescc3b0652009-10-03 04:21:37 +0000988 LiveIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000989 MachineOperand& MO,
990 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000991 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000992 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000993 getOrCreateInterval(MO.getReg()));
994 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000995 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000996 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000997 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000998 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000999 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +00001000 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001001 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +00001002 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001003 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +00001004 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +00001005 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +00001006 // If MI also modifies the sub-register explicitly, avoid processing it
1007 // more than once. Do not pass in TRI here so it checks for exact match.
1008 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +00001009 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001010 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +00001011 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001012}
1013
Evan Chengb371f452007-02-19 21:49:54 +00001014void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001015 LiveIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001016 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001017 DEBUG({
1018 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001019 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001020 });
Evan Chengb371f452007-02-19 21:49:54 +00001021
1022 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1023 // be considered a livein.
1024 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hamescc3b0652009-10-03 04:21:37 +00001025 LiveIndex baseIndex = MIIdx;
1026 LiveIndex start = baseIndex;
Lang Hames86511252009-09-04 20:41:11 +00001027 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001028 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001029 baseIndex = getNextIndex(baseIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001030 LiveIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001031 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001032
Evan Chengb371f452007-02-19 21:49:54 +00001033 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001034 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001035 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001036 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001037 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001038 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001039 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001040 // Another instruction redefines the register before it is ever read.
1041 // Then the register is essentially dead at the instruction that defines
1042 // it. Hence its interval is:
1043 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001044 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001045 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001046 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001047 break;
Evan Chengb371f452007-02-19 21:49:54 +00001048 }
1049
Lang Hames35f291d2009-09-12 03:34:03 +00001050 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001051 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001052 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001053 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001054 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001055 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001056 }
Evan Chengb371f452007-02-19 21:49:54 +00001057 }
1058
Evan Cheng75611fb2007-06-27 01:16:36 +00001059 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001060 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001061 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001062 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001063 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001064 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001065 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001066 end = baseIndex;
1067 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001068 }
1069
Lang Hames10382fb2009-06-19 02:17:53 +00001070 VNInfo *vni =
Lang Hamescc3b0652009-10-03 04:21:37 +00001071 interval.getNextValue(LiveIndex(MBB->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +00001072 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001073 vni->setIsPHIDef(true);
1074 LiveRange LR(start, end, vni);
1075
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001076 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001077 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001078 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001079}
1080
Evan Cheng752195e2009-09-14 21:33:42 +00001081bool
1082LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1083 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001084 SmallVector<MachineInstr*,16> &OtherCopies) {
1085 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001086 unsigned NumIdent = 0;
Dan Gohman2bf06492009-09-25 22:26:13 +00001087 for (MachineRegisterInfo::def_iterator ri = mri_->def_begin(SrcInt.reg),
1088 re = mri_->def_end(); ri != re; ++ri) {
Evan Cheng752195e2009-09-14 21:33:42 +00001089 MachineInstr *MI = &*ri;
1090 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1091 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001092 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001093 if (SrcReg != DstInt.reg) {
1094 OtherCopies.push_back(MI);
1095 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1096 } else {
1097 IdentCopies.push_back(MI);
1098 ++NumIdent;
1099 }
1100 }
1101
Evan Cheng3f855492009-09-15 06:45:16 +00001102 if (!HaveConflict)
1103 return false; // Let coalescer handle it
1104 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001105}
1106
1107void LiveIntervals::performEarlyCoalescing() {
1108 if (!EarlyCoalescing)
1109 return;
1110
1111 /// Perform early coalescing: eliminate copies which feed into phi joins
1112 /// and whose sources are defined by the phi joins.
1113 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1114 MachineInstr *Join = phiJoinCopies[i];
1115 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1116 break;
1117
1118 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1119 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1120#ifndef NDEBUG
1121 assert(isMove && "PHI join instruction must be a move!");
1122#else
1123 isMove = isMove;
1124#endif
1125
1126 LiveInterval &DstInt = getInterval(PHIDst);
1127 LiveInterval &SrcInt = getInterval(PHISrc);
1128 SmallVector<MachineInstr*, 16> IdentCopies;
1129 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001130 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001131 continue;
1132
1133 DEBUG(errs() << "PHI Join: " << *Join);
1134 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1135 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001136
Evan Cheng3f855492009-09-15 06:45:16 +00001137 // Change the non-identity copies to directly target the phi destination.
1138 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1139 MachineInstr *PHICopy = OtherCopies[i];
1140 DEBUG(errs() << "Moving: " << *PHICopy);
1141
Lang Hamescc3b0652009-10-03 04:21:37 +00001142 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1143 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng752195e2009-09-14 21:33:42 +00001144 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001145 LiveIndex StartIndex = SLR->start;
1146 LiveIndex EndIndex = SLR->end;
Evan Cheng752195e2009-09-14 21:33:42 +00001147
1148 // Delete val# defined by the now identity copy and add the range from
1149 // beginning of the mbb to the end of the range.
1150 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001151 DEBUG(errs() << " added range [" << StartIndex << ','
1152 << EndIndex << "] to reg" << DstInt.reg << '\n');
1153 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001154 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001155 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1156 VNInfoAllocator);
1157 NewVNI->setHasPHIKill(true);
1158 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1159 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1160 MachineOperand &MO = PHICopy->getOperand(j);
1161 if (!MO.isReg() || MO.getReg() != PHISrc)
1162 continue;
1163 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001164 }
Evan Cheng3f855492009-09-15 06:45:16 +00001165 }
1166
1167 // Now let's eliminate all the would-be identity copies.
1168 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1169 MachineInstr *PHICopy = IdentCopies[i];
1170 DEBUG(errs() << "Coalescing: " << *PHICopy);
1171
Lang Hamescc3b0652009-10-03 04:21:37 +00001172 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1173 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001174 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001175 LiveIndex StartIndex = SLR->start;
1176 LiveIndex EndIndex = SLR->end;
Evan Cheng3f855492009-09-15 06:45:16 +00001177
1178 // Delete val# defined by the now identity copy and add the range from
1179 // beginning of the mbb to the end of the range.
1180 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001181 RemoveMachineInstrFromMaps(PHICopy);
1182 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001183 DEBUG(errs() << " added range [" << StartIndex << ','
1184 << EndIndex << "] to reg" << DstInt.reg << '\n');
1185 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001186 }
Evan Cheng752195e2009-09-14 21:33:42 +00001187
Evan Cheng3f855492009-09-15 06:45:16 +00001188 // Remove the phi join and update the phi block liveness.
Lang Hamescc3b0652009-10-03 04:21:37 +00001189 LiveIndex MIIndex = getInstructionIndex(Join);
1190 LiveIndex UseIndex = getUseIndex(MIIndex);
1191 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001192 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1193 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1194 DLR->valno->setCopy(0);
1195 DLR->valno->setIsDefAccurate(false);
1196 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1197 SrcInt.removeRange(SLR->start, SLR->end);
1198 assert(SrcInt.empty());
1199 removeInterval(PHISrc);
1200 RemoveMachineInstrFromMaps(Join);
1201 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001202
1203 ++numCoalescing;
1204 }
1205}
1206
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001207/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001208/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001209/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001210/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001211void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001212 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001213 << "********** Function: "
1214 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001215
1216 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001217 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1218 MBBI != E; ++MBBI) {
1219 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001220 // Track the index of the current machine instr.
Lang Hamescc3b0652009-10-03 04:21:37 +00001221 LiveIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001222 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001223
Chris Lattner428b92e2006-09-15 03:57:23 +00001224 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001225
Dan Gohmancb406c22007-10-03 19:26:29 +00001226 // Create intervals for live-ins to this BB first.
1227 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1228 LE = MBB->livein_end(); LI != LE; ++LI) {
1229 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1230 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001231 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001232 if (!hasInterval(*AS))
1233 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1234 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001235 }
1236
Owen Anderson99500ae2008-09-15 22:00:38 +00001237 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001238 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001239 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001240 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001241
Chris Lattner428b92e2006-09-15 03:57:23 +00001242 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001243 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001244
Evan Cheng438f7bc2006-11-10 08:43:01 +00001245 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001246 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1247 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001248 if (!MO.isReg() || !MO.getReg())
1249 continue;
1250
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001251 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001252 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001253 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001254 else if (MO.isUndef())
1255 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001256 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001257
1258 // Skip over the empty slots after each instruction.
1259 unsigned Slots = MI->getDesc().getNumDefs();
1260 if (Slots == 0)
1261 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001262
1263 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001264 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001265
1266 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001267 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001268 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001269 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001270 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001271 }
Evan Chengd129d732009-07-17 19:43:40 +00001272
1273 // Create empty intervals for registers defined by implicit_def's (except
1274 // for those implicit_def that define values which are liveout of their
1275 // blocks.
1276 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1277 unsigned UndefReg = UndefUses[i];
1278 (void)getOrCreateInterval(UndefReg);
1279 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001280}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001281
Lang Hames86511252009-09-04 20:41:11 +00001282bool LiveIntervals::findLiveInMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001283 LiveIndex Start, LiveIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001284 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001285 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001286 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001287
1288 bool ResVal = false;
1289 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001290 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001291 break;
1292 MBBs.push_back(I->second);
1293 ResVal = true;
1294 ++I;
1295 }
1296 return ResVal;
1297}
1298
Lang Hames86511252009-09-04 20:41:11 +00001299bool LiveIntervals::findReachableMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001300 LiveIndex Start, LiveIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001301 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1302 std::vector<IdxMBBPair>::const_iterator I =
1303 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1304
1305 bool ResVal = false;
1306 while (I != Idx2MBBMap.end()) {
1307 if (I->first > End)
1308 break;
1309 MachineBasicBlock *MBB = I->second;
1310 if (getMBBEndIdx(MBB) > End)
1311 break;
1312 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1313 SE = MBB->succ_end(); SI != SE; ++SI)
1314 MBBs.push_back(*SI);
1315 ResVal = true;
1316 ++I;
1317 }
1318 return ResVal;
1319}
1320
Owen Anderson03857b22008-08-13 21:49:13 +00001321LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001322 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001323 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001324}
Evan Chengf2fbca62007-11-12 06:35:08 +00001325
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001326/// dupInterval - Duplicate a live interval. The caller is responsible for
1327/// managing the allocated memory.
1328LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1329 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001330 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001331 return NewLI;
1332}
1333
Evan Chengc8d044e2008-02-15 18:24:29 +00001334/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1335/// copy field and returns the source register that defines it.
1336unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001337 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001338 return 0;
1339
Lang Hames52c1afc2009-08-10 23:43:28 +00001340 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001341 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001342 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001343 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001344 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001345 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001346 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1347 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1348 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001349
Evan Cheng04ee5a12009-01-20 19:12:24 +00001350 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001351 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001352 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001353 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001354 return 0;
1355}
Evan Chengf2fbca62007-11-12 06:35:08 +00001356
1357//===----------------------------------------------------------------------===//
1358// Register allocator hooks.
1359//
1360
Evan Chengd70dbb52008-02-22 09:24:50 +00001361/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1362/// allow one) virtual register operand, then its uses are implicitly using
1363/// the register. Returns the virtual register.
1364unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1365 MachineInstr *MI) const {
1366 unsigned RegOp = 0;
1367 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1368 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001369 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001370 continue;
1371 unsigned Reg = MO.getReg();
1372 if (Reg == 0 || Reg == li.reg)
1373 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001374
1375 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1376 !allocatableRegs_[Reg])
1377 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001378 // FIXME: For now, only remat MI with at most one register operand.
1379 assert(!RegOp &&
1380 "Can't rematerialize instruction with multiple register operand!");
1381 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001382#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001383 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001384#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001385 }
1386 return RegOp;
1387}
1388
1389/// isValNoAvailableAt - Return true if the val# of the specified interval
1390/// which reaches the given instruction also reaches the specified use index.
1391bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001392 LiveIndex UseIdx) const {
1393 LiveIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001394 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1395 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1396 return UI != li.end() && UI->valno == ValNo;
1397}
1398
Evan Chengf2fbca62007-11-12 06:35:08 +00001399/// isReMaterializable - Returns true if the definition MI of the specified
1400/// val# of the specified interval is re-materializable.
1401bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001402 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001403 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001404 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001405 if (DisableReMat)
1406 return false;
1407
Dan Gohmana70dca12009-10-09 23:27:56 +00001408 if (!tii_->isTriviallyReMaterializable(MI, aa_))
1409 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +00001410
Dan Gohmana70dca12009-10-09 23:27:56 +00001411 // Target-specific code can mark an instruction as being rematerializable
1412 // if it has one virtual reg use, though it had better be something like
1413 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001414 unsigned ImpUse = getReMatImplicitUse(li, MI);
1415 if (ImpUse) {
1416 const LiveInterval &ImpLi = getInterval(ImpUse);
1417 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1418 re = mri_->use_end(); ri != re; ++ri) {
1419 MachineInstr *UseMI = &*ri;
Lang Hamescc3b0652009-10-03 04:21:37 +00001420 LiveIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001421 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1422 continue;
1423 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1424 return false;
1425 }
Evan Chengdc377862008-09-30 15:44:16 +00001426
1427 // If a register operand of the re-materialized instruction is going to
1428 // be spilled next, then it's not legal to re-materialize this instruction.
1429 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1430 if (ImpUse == SpillIs[i]->reg)
1431 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001432 }
1433 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001434}
1435
Evan Cheng06587492008-10-24 02:05:00 +00001436/// isReMaterializable - Returns true if the definition MI of the specified
1437/// val# of the specified interval is re-materializable.
1438bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1439 const VNInfo *ValNo, MachineInstr *MI) {
1440 SmallVector<LiveInterval*, 4> Dummy1;
1441 bool Dummy2;
1442 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1443}
1444
Evan Cheng5ef3a042007-12-06 00:01:56 +00001445/// isReMaterializable - Returns true if every definition of MI of every
1446/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001447bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1448 SmallVectorImpl<LiveInterval*> &SpillIs,
1449 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001450 isLoad = false;
1451 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1452 i != e; ++i) {
1453 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001454 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001455 continue; // Dead val#.
1456 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001457 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001458 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001459 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001460 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001461 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001462 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001463 return false;
1464 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001465 }
1466 return true;
1467}
1468
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001469/// FilterFoldedOps - Filter out two-address use operands. Return
1470/// true if it finds any issue with the operands that ought to prevent
1471/// folding.
1472static bool FilterFoldedOps(MachineInstr *MI,
1473 SmallVector<unsigned, 2> &Ops,
1474 unsigned &MRInfo,
1475 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001476 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001477 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1478 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001479 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001480 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001481 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001482 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001483 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001484 MRInfo |= (unsigned)VirtRegMap::isMod;
1485 else {
1486 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001487 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001488 MRInfo = VirtRegMap::isModRef;
1489 continue;
1490 }
1491 MRInfo |= (unsigned)VirtRegMap::isRef;
1492 }
1493 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001494 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001495 return false;
1496}
1497
1498
1499/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1500/// slot / to reg or any rematerialized load into ith operand of specified
1501/// MI. If it is successul, MI is updated with the newly created MI and
1502/// returns true.
1503bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1504 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001505 LiveIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001506 SmallVector<unsigned, 2> &Ops,
1507 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001508 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001509 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001510 RemoveMachineInstrFromMaps(MI);
1511 vrm.RemoveMachineInstrFromMaps(MI);
1512 MI->eraseFromParent();
1513 ++numFolds;
1514 return true;
1515 }
1516
1517 // Filter the list of operand indexes that are to be folded. Abort if
1518 // any operand will prevent folding.
1519 unsigned MRInfo = 0;
1520 SmallVector<unsigned, 2> FoldOps;
1521 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1522 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001523
Evan Cheng427f4c12008-03-31 23:19:51 +00001524 // The only time it's safe to fold into a two address instruction is when
1525 // it's folding reload and spill from / into a spill stack slot.
1526 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001527 return false;
1528
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001529 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1530 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001531 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001532 // Remember this instruction uses the spill slot.
1533 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1534
Evan Chengf2fbca62007-11-12 06:35:08 +00001535 // Attempt to fold the memory reference into the instruction. If
1536 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001537 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001538 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001539 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001540 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001541 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001542 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001543 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001544 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001545 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001546 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001547 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001548 return true;
1549 }
1550 return false;
1551}
1552
Evan Cheng018f9b02007-12-05 03:22:34 +00001553/// canFoldMemoryOperand - Returns true if the specified load / store
1554/// folding is possible.
1555bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001556 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001557 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001558 // Filter the list of operand indexes that are to be folded. Abort if
1559 // any operand will prevent folding.
1560 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001561 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001562 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1563 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001564
Evan Cheng3c75ba82008-04-01 21:37:32 +00001565 // It's only legal to remat for a use, not a def.
1566 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001567 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001568
Evan Chengd70dbb52008-02-22 09:24:50 +00001569 return tii_->canFoldMemoryOperand(MI, FoldOps);
1570}
1571
Evan Cheng81a03822007-11-17 00:40:40 +00001572bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1573 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1574 for (LiveInterval::Ranges::const_iterator
1575 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1576 std::vector<IdxMBBPair>::const_iterator II =
1577 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1578 if (II == Idx2MBBMap.end())
1579 continue;
1580 if (I->end > II->first) // crossing a MBB.
1581 return false;
1582 MBBs.insert(II->second);
1583 if (MBBs.size() > 1)
1584 return false;
1585 }
1586 return true;
1587}
1588
Evan Chengd70dbb52008-02-22 09:24:50 +00001589/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1590/// interval on to-be re-materialized operands of MI) with new register.
1591void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1592 MachineInstr *MI, unsigned NewVReg,
1593 VirtRegMap &vrm) {
1594 // There is an implicit use. That means one of the other operand is
1595 // being remat'ed and the remat'ed instruction has li.reg as an
1596 // use operand. Make sure we rewrite that as well.
1597 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1598 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001599 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001600 continue;
1601 unsigned Reg = MO.getReg();
1602 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1603 continue;
1604 if (!vrm.isReMaterialized(Reg))
1605 continue;
1606 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001607 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1608 if (UseMO)
1609 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001610 }
1611}
1612
Evan Chengf2fbca62007-11-12 06:35:08 +00001613/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1614/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001615bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001616rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames6cc91e32009-10-03 04:31:31 +00001617 bool TrySplit, LiveIndex index, LiveIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001618 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001619 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001620 unsigned Slot, int LdSlot,
1621 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001622 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001623 const TargetRegisterClass* rc,
1624 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001625 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001626 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001627 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001628 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001629 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001630 RestartInstruction:
1631 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1632 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001633 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001634 continue;
1635 unsigned Reg = mop.getReg();
1636 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001637 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001638 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001639 if (Reg != li.reg)
1640 continue;
1641
1642 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001643 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001644 int FoldSlot = Slot;
1645 if (DefIsReMat) {
1646 // If this is the rematerializable definition MI itself and
1647 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001648 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001649 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1650 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001651 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001652 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001653 MI->eraseFromParent();
1654 break;
1655 }
1656
1657 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001658 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001659 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001660 if (isLoad) {
1661 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1662 FoldSS = isLoadSS;
1663 FoldSlot = LdSlot;
1664 }
1665 }
1666
Evan Chengf2fbca62007-11-12 06:35:08 +00001667 // Scan all of the operands of this instruction rewriting operands
1668 // to use NewVReg instead of li.reg as appropriate. We do this for
1669 // two reasons:
1670 //
1671 // 1. If the instr reads the same spilled vreg multiple times, we
1672 // want to reuse the NewVReg.
1673 // 2. If the instr is a two-addr instruction, we are required to
1674 // keep the src/dst regs pinned.
1675 //
1676 // Keep track of whether we replace a use and/or def so that we can
1677 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001678
Evan Cheng81a03822007-11-17 00:40:40 +00001679 HasUse = mop.isUse();
1680 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001681 SmallVector<unsigned, 2> Ops;
1682 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001683 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001684 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001685 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001686 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001687 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001688 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001689 continue;
1690 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001691 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001692 if (!MOj.isUndef()) {
1693 HasUse |= MOj.isUse();
1694 HasDef |= MOj.isDef();
1695 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001696 }
1697 }
1698
David Greene26b86a02008-10-27 17:38:59 +00001699 // Create a new virtual register for the spill interval.
1700 // Create the new register now so we can map the fold instruction
1701 // to the new register so when it is unfolded we get the correct
1702 // answer.
1703 bool CreatedNewVReg = false;
1704 if (NewVReg == 0) {
1705 NewVReg = mri_->createVirtualRegister(rc);
1706 vrm.grow();
1707 CreatedNewVReg = true;
1708 }
1709
Evan Cheng9c3c2212008-06-06 07:54:39 +00001710 if (!TryFold)
1711 CanFold = false;
1712 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001713 // Do not fold load / store here if we are splitting. We'll find an
1714 // optimal point to insert a load / store later.
1715 if (!TrySplit) {
1716 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001717 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001718 // Folding the load/store can completely change the instruction in
1719 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001720
1721 if (FoldSS) {
1722 // We need to give the new vreg the same stack slot as the
1723 // spilled interval.
1724 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1725 }
1726
Evan Cheng018f9b02007-12-05 03:22:34 +00001727 HasUse = false;
1728 HasDef = false;
1729 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001730 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001731 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001732 goto RestartInstruction;
1733 }
1734 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001735 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001736 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001737 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001738 }
Evan Chengcddbb832007-11-30 21:23:43 +00001739
Evan Chengcddbb832007-11-30 21:23:43 +00001740 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001741 if (mop.isImplicit())
1742 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001743
1744 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001745 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1746 MachineOperand &mopj = MI->getOperand(Ops[j]);
1747 mopj.setReg(NewVReg);
1748 if (mopj.isImplicit())
1749 rewriteImplicitOps(li, MI, NewVReg, vrm);
1750 }
Evan Chengcddbb832007-11-30 21:23:43 +00001751
Evan Cheng81a03822007-11-17 00:40:40 +00001752 if (CreatedNewVReg) {
1753 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001754 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001755 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001756 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001757 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001758 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001759 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001760 }
1761 if (!CanDelete || (HasUse && HasDef)) {
1762 // If this is a two-addr instruction then its use operands are
1763 // rematerializable but its def is not. It should be assigned a
1764 // stack slot.
1765 vrm.assignVirt2StackSlot(NewVReg, Slot);
1766 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001767 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001768 vrm.assignVirt2StackSlot(NewVReg, Slot);
1769 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001770 } else if (HasUse && HasDef &&
1771 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1772 // If this interval hasn't been assigned a stack slot (because earlier
1773 // def is a deleted remat def), do it now.
1774 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1775 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001776 }
1777
Evan Cheng313d4b82008-02-23 00:33:04 +00001778 // Re-matting an instruction with virtual register use. Add the
1779 // register as an implicit use on the use MI.
1780 if (DefIsReMat && ImpUse)
1781 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1782
Evan Cheng5b69eba2009-04-21 22:46:52 +00001783 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001784 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001785 if (CreatedNewVReg) {
1786 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001787 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001788 if (TrySplit)
1789 vrm.setIsSplitFromReg(NewVReg, li.reg);
1790 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001791
1792 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001793 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001794 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hamescc3b0652009-10-03 04:21:37 +00001795 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001796 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001797 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001798 nI.addRange(LR);
1799 } else {
1800 // Extend the split live interval to this def / use.
Lang Hamescc3b0652009-10-03 04:21:37 +00001801 LiveIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001802 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1803 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001804 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001805 nI.addRange(LR);
1806 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001807 }
1808 if (HasDef) {
1809 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00001810 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001811 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001812 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001813 nI.addRange(LR);
1814 }
Evan Cheng81a03822007-11-17 00:40:40 +00001815
Bill Wendling8e6179f2009-08-22 20:18:03 +00001816 DEBUG({
1817 errs() << "\t\t\t\tAdded new interval: ";
1818 nI.print(errs(), tri_);
1819 errs() << '\n';
1820 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001821 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001822 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001823}
Evan Cheng81a03822007-11-17 00:40:40 +00001824bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001825 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001826 MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001827 LiveIndex Idx) const {
1828 LiveIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001829 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001830 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001831 continue;
1832
Lang Hamescc3b0652009-10-03 04:21:37 +00001833 LiveIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001834 if (KillIdx > Idx && KillIdx < End)
1835 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001836 }
1837 return false;
1838}
1839
Evan Cheng063284c2008-02-21 00:34:19 +00001840/// RewriteInfo - Keep track of machine instrs that will be rewritten
1841/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001842namespace {
1843 struct RewriteInfo {
Lang Hamescc3b0652009-10-03 04:21:37 +00001844 LiveIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001845 MachineInstr *MI;
1846 bool HasUse;
1847 bool HasDef;
Lang Hamescc3b0652009-10-03 04:21:37 +00001848 RewriteInfo(LiveIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001849 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1850 };
Evan Cheng063284c2008-02-21 00:34:19 +00001851
Dan Gohman844731a2008-05-13 00:00:25 +00001852 struct RewriteInfoCompare {
1853 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1854 return LHS.Index < RHS.Index;
1855 }
1856 };
1857}
Evan Cheng063284c2008-02-21 00:34:19 +00001858
Evan Chengf2fbca62007-11-12 06:35:08 +00001859void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001860rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001861 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001862 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001863 unsigned Slot, int LdSlot,
1864 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001865 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001866 const TargetRegisterClass* rc,
1867 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001868 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001869 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001870 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001871 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001872 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1873 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001874 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001875 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001876 unsigned NewVReg = 0;
Lang Hamescc3b0652009-10-03 04:21:37 +00001877 LiveIndex start = getBaseIndex(I->start);
1878 LiveIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001879
Evan Cheng063284c2008-02-21 00:34:19 +00001880 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001881 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001882 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001883 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1884 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001885 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001886 MachineOperand &O = ri.getOperand();
1887 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001888 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hamescc3b0652009-10-03 04:21:37 +00001889 LiveIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001890 if (index < start || index >= end)
1891 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001892
1893 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001894 // Must be defined by an implicit def. It should not be spilled. Note,
1895 // this is for correctness reason. e.g.
1896 // 8 %reg1024<def> = IMPLICIT_DEF
1897 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1898 // The live range [12, 14) are not part of the r1024 live interval since
1899 // it's defined by an implicit def. It will not conflicts with live
1900 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001901 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001902 // the INSERT_SUBREG and both target registers that would overlap.
1903 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001904 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1905 }
1906 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1907
Evan Cheng313d4b82008-02-23 00:33:04 +00001908 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001909 // Now rewrite the defs and uses.
1910 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1911 RewriteInfo &rwi = RewriteMIs[i];
1912 ++i;
Lang Hamescc3b0652009-10-03 04:21:37 +00001913 LiveIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001914 bool MIHasUse = rwi.HasUse;
1915 bool MIHasDef = rwi.HasDef;
1916 MachineInstr *MI = rwi.MI;
1917 // If MI def and/or use the same register multiple times, then there
1918 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001919 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001920 while (i != e && RewriteMIs[i].MI == MI) {
1921 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001922 bool isUse = RewriteMIs[i].HasUse;
1923 if (isUse) ++NumUses;
1924 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001925 MIHasDef |= RewriteMIs[i].HasDef;
1926 ++i;
1927 }
Evan Cheng81a03822007-11-17 00:40:40 +00001928 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001929
Evan Cheng0a891ed2008-05-23 23:00:04 +00001930 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001931 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001932 // register interval's spill weight to HUGE_VALF to prevent it from
1933 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001934 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001935 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001936 }
1937
Evan Cheng063284c2008-02-21 00:34:19 +00001938 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001939 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001940 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001941 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001942 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001943 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001944 // One common case:
1945 // x = use
1946 // ...
1947 // ...
1948 // def = ...
1949 // = use
1950 // It's better to start a new interval to avoid artifically
1951 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001952 if (MIHasDef && !MIHasUse) {
1953 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001954 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001955 }
1956 }
Evan Chengcada2452007-11-28 01:28:46 +00001957 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001958
1959 bool IsNew = ThisVReg == 0;
1960 if (IsNew) {
1961 // This ends the previous live interval. If all of its def / use
1962 // can be folded, give it a low spill weight.
1963 if (NewVReg && TrySplit && AllCanFold) {
1964 LiveInterval &nI = getOrCreateInterval(NewVReg);
1965 nI.weight /= 10.0F;
1966 }
1967 AllCanFold = true;
1968 }
1969 NewVReg = ThisVReg;
1970
Evan Cheng81a03822007-11-17 00:40:40 +00001971 bool HasDef = false;
1972 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001973 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001974 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1975 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1976 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001977 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001978 if (!HasDef && !HasUse)
1979 continue;
1980
Evan Cheng018f9b02007-12-05 03:22:34 +00001981 AllCanFold &= CanFold;
1982
Evan Cheng81a03822007-11-17 00:40:40 +00001983 // Update weight of spill interval.
1984 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001985 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001986 // The spill weight is now infinity as it cannot be spilled again.
1987 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001988 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001989 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001990
1991 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001992 if (HasDef) {
1993 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001994 bool HasKill = false;
1995 if (!HasUse)
1996 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1997 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001998 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00001999 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002000 if (VNI)
2001 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2002 }
Owen Anderson28998312008-08-13 22:28:50 +00002003 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002004 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002005 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002006 if (SII == SpillIdxes.end()) {
2007 std::vector<SRInfo> S;
2008 S.push_back(SRInfo(index, NewVReg, true));
2009 SpillIdxes.insert(std::make_pair(MBBId, S));
2010 } else if (SII->second.back().vreg != NewVReg) {
2011 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002012 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002013 // If there is an earlier def and this is a two-address
2014 // instruction, then it's not possible to fold the store (which
2015 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002016 SRInfo &Info = SII->second.back();
2017 Info.index = index;
2018 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002019 }
2020 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002021 } else if (SII != SpillIdxes.end() &&
2022 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002023 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002024 // There is an earlier def that's not killed (must be two-address).
2025 // The spill is no longer needed.
2026 SII->second.pop_back();
2027 if (SII->second.empty()) {
2028 SpillIdxes.erase(MBBId);
2029 SpillMBBs.reset(MBBId);
2030 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002031 }
2032 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002033 }
2034
2035 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002036 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002037 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002038 if (SII != SpillIdxes.end() &&
2039 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002040 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002041 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002042 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002043 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002044 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002045 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002046 // If we are splitting live intervals, only fold if it's the first
2047 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002048 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002049 else if (IsNew) {
2050 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002051 if (RII == RestoreIdxes.end()) {
2052 std::vector<SRInfo> Infos;
2053 Infos.push_back(SRInfo(index, NewVReg, true));
2054 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2055 } else {
2056 RII->second.push_back(SRInfo(index, NewVReg, true));
2057 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002058 RestoreMBBs.set(MBBId);
2059 }
2060 }
2061
2062 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002063 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002064 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002065 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002066
2067 if (NewVReg && TrySplit && AllCanFold) {
2068 // If all of its def / use can be folded, give it a low spill weight.
2069 LiveInterval &nI = getOrCreateInterval(NewVReg);
2070 nI.weight /= 10.0F;
2071 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002072}
2073
Lang Hamescc3b0652009-10-03 04:21:37 +00002074bool LiveIntervals::alsoFoldARestore(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002075 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002076 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002077 if (!RestoreMBBs[Id])
2078 return false;
2079 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2080 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2081 if (Restores[i].index == index &&
2082 Restores[i].vreg == vr &&
2083 Restores[i].canFold)
2084 return true;
2085 return false;
2086}
2087
Lang Hamescc3b0652009-10-03 04:21:37 +00002088void LiveIntervals::eraseRestoreInfo(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002089 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002090 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002091 if (!RestoreMBBs[Id])
2092 return;
2093 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2094 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2095 if (Restores[i].index == index && Restores[i].vreg)
Lang Hamescc3b0652009-10-03 04:21:37 +00002096 Restores[i].index = LiveIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002097}
Evan Cheng81a03822007-11-17 00:40:40 +00002098
Evan Cheng4cce6b42008-04-11 17:53:36 +00002099/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2100/// spilled and create empty intervals for their uses.
2101void
2102LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2103 const TargetRegisterClass* rc,
2104 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002105 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2106 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002107 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002108 MachineInstr *MI = &*ri;
2109 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002110 if (O.isDef()) {
2111 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2112 "Register def was not rewritten?");
2113 RemoveMachineInstrFromMaps(MI);
2114 vrm.RemoveMachineInstrFromMaps(MI);
2115 MI->eraseFromParent();
2116 } else {
2117 // This must be an use of an implicit_def so it's not part of the live
2118 // interval. Create a new empty live interval for it.
2119 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2120 unsigned NewVReg = mri_->createVirtualRegister(rc);
2121 vrm.grow();
2122 vrm.setIsImplicitlyDefined(NewVReg);
2123 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2124 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2125 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002126 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002127 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002128 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002129 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002130 }
2131 }
Evan Cheng419852c2008-04-03 16:39:43 +00002132 }
2133}
2134
Evan Chengf2fbca62007-11-12 06:35:08 +00002135std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002136addIntervalsForSpillsFast(const LiveInterval &li,
2137 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002138 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002139 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002140
2141 std::vector<LiveInterval*> added;
2142
2143 assert(li.weight != HUGE_VALF &&
2144 "attempt to spill already spilled interval!");
2145
Bill Wendling8e6179f2009-08-22 20:18:03 +00002146 DEBUG({
2147 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2148 li.dump();
2149 errs() << '\n';
2150 });
Owen Andersond6664312008-08-18 18:05:32 +00002151
2152 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2153
Owen Andersona41e47a2008-08-19 22:12:11 +00002154 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2155 while (RI != mri_->reg_end()) {
2156 MachineInstr* MI = &*RI;
2157
2158 SmallVector<unsigned, 2> Indices;
2159 bool HasUse = false;
2160 bool HasDef = false;
2161
2162 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2163 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002164 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002165
2166 HasUse |= MI->getOperand(i).isUse();
2167 HasDef |= MI->getOperand(i).isDef();
2168
2169 Indices.push_back(i);
2170 }
2171
2172 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2173 Indices, true, slot, li.reg)) {
2174 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002175 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002176 vrm.assignVirt2StackSlot(NewVReg, slot);
2177
Owen Andersona41e47a2008-08-19 22:12:11 +00002178 // create a new register for this spill
2179 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002180
Owen Andersona41e47a2008-08-19 22:12:11 +00002181 // the spill weight is now infinity as it
2182 // cannot be spilled again
2183 nI.weight = HUGE_VALF;
2184
2185 // Rewrite register operands to use the new vreg.
2186 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2187 E = Indices.end(); I != E; ++I) {
2188 MI->getOperand(*I).setReg(NewVReg);
2189
2190 if (MI->getOperand(*I).isUse())
2191 MI->getOperand(*I).setIsKill(true);
2192 }
2193
2194 // Fill in the new live interval.
Lang Hamescc3b0652009-10-03 04:21:37 +00002195 LiveIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002196 if (HasUse) {
2197 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002198 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002199 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002200 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002201 nI.addRange(LR);
2202 vrm.addRestorePoint(NewVReg, MI);
2203 }
2204 if (HasDef) {
2205 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002206 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002207 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002208 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002209 nI.addRange(LR);
2210 vrm.addSpillPoint(NewVReg, true, MI);
2211 }
2212
Owen Anderson17197312008-08-18 23:41:04 +00002213 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002214
Bill Wendling8e6179f2009-08-22 20:18:03 +00002215 DEBUG({
2216 errs() << "\t\t\t\tadded new interval: ";
2217 nI.dump();
2218 errs() << '\n';
2219 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002220 }
Owen Anderson9a032932008-08-18 21:20:32 +00002221
Owen Anderson9a032932008-08-18 21:20:32 +00002222
Owen Andersona41e47a2008-08-19 22:12:11 +00002223 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002224 }
Owen Andersond6664312008-08-18 18:05:32 +00002225
2226 return added;
2227}
2228
2229std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002230addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002231 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002232 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002233
2234 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002235 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002236
Evan Chengf2fbca62007-11-12 06:35:08 +00002237 assert(li.weight != HUGE_VALF &&
2238 "attempt to spill already spilled interval!");
2239
Bill Wendling8e6179f2009-08-22 20:18:03 +00002240 DEBUG({
2241 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2242 li.print(errs(), tri_);
2243 errs() << '\n';
2244 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002245
Evan Cheng72eeb942008-12-05 17:00:16 +00002246 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002247 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002248 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002249 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002250 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2251 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002252 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002253 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002254
2255 unsigned NumValNums = li.getNumValNums();
2256 SmallVector<MachineInstr*, 4> ReMatDefs;
2257 ReMatDefs.resize(NumValNums, NULL);
2258 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2259 ReMatOrigDefs.resize(NumValNums, NULL);
2260 SmallVector<int, 4> ReMatIds;
2261 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2262 BitVector ReMatDelete(NumValNums);
2263 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2264
Evan Cheng81a03822007-11-17 00:40:40 +00002265 // Spilling a split live interval. It cannot be split any further. Also,
2266 // it's also guaranteed to be a single val# / range interval.
2267 if (vrm.getPreSplitReg(li.reg)) {
2268 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002269 // Unset the split kill marker on the last use.
Lang Hamescc3b0652009-10-03 04:21:37 +00002270 LiveIndex KillIdx = vrm.getKillPoint(li.reg);
2271 if (KillIdx != LiveIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002272 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2273 assert(KillMI && "Last use disappeared?");
2274 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2275 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002276 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002277 }
Evan Chengadf85902007-12-05 09:51:10 +00002278 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002279 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2280 Slot = vrm.getStackSlot(li.reg);
2281 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2282 MachineInstr *ReMatDefMI = DefIsReMat ?
2283 vrm.getReMaterializedMI(li.reg) : NULL;
2284 int LdSlot = 0;
2285 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2286 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002287 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002288 bool IsFirstRange = true;
2289 for (LiveInterval::Ranges::const_iterator
2290 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2291 // If this is a split live interval with multiple ranges, it means there
2292 // are two-address instructions that re-defined the value. Only the
2293 // first def can be rematerialized!
2294 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002295 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002296 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2297 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002298 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002299 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002300 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002301 } else {
2302 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2303 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002304 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002305 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002306 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002307 }
2308 IsFirstRange = false;
2309 }
Evan Cheng419852c2008-04-03 16:39:43 +00002310
Evan Cheng4cce6b42008-04-11 17:53:36 +00002311 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002312 return NewLIs;
2313 }
2314
Evan Cheng752195e2009-09-14 21:33:42 +00002315 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002316 if (TrySplit)
2317 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002318 bool NeedStackSlot = false;
2319 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2320 i != e; ++i) {
2321 const VNInfo *VNI = *i;
2322 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002323 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002324 continue; // Dead val#.
2325 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002326 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2327 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002328 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002329 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002330 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002331 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002332 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002333 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002334 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002335 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002336
2337 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002338 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002339 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002340 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002341 CanDelete = false;
2342 // Need a stack slot if there is any live range where uses cannot be
2343 // rematerialized.
2344 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002345 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002346 if (CanDelete)
2347 ReMatDelete.set(VN);
2348 } else {
2349 // Need a stack slot if there is any live range where uses cannot be
2350 // rematerialized.
2351 NeedStackSlot = true;
2352 }
2353 }
2354
2355 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002356 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2357 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2358 Slot = vrm.assignVirt2StackSlot(li.reg);
2359
2360 // This case only occurs when the prealloc splitter has already assigned
2361 // a stack slot to this vreg.
2362 else
2363 Slot = vrm.getStackSlot(li.reg);
2364 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002365
2366 // Create new intervals and rewrite defs and uses.
2367 for (LiveInterval::Ranges::const_iterator
2368 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002369 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2370 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2371 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002372 bool CanDelete = ReMatDelete[I->valno->id];
2373 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002374 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002375 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002376 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002377 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002378 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002379 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002380 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002381 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002382 }
2383
Evan Cheng0cbb1162007-11-29 01:06:25 +00002384 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002385 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002386 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002387 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002388 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002389
Evan Chengb50bb8c2007-12-05 08:16:32 +00002390 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002391 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002392 if (NeedStackSlot) {
2393 int Id = SpillMBBs.find_first();
2394 while (Id != -1) {
2395 std::vector<SRInfo> &spills = SpillIdxes[Id];
2396 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002397 LiveIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002398 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002399 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002400 bool isReMat = vrm.isReMaterialized(VReg);
2401 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002402 bool CanFold = false;
2403 bool FoundUse = false;
2404 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002405 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002406 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002407 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2408 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002409 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002410 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002411
2412 Ops.push_back(j);
2413 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002414 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002415 if (isReMat ||
2416 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2417 RestoreMBBs, RestoreIdxes))) {
2418 // MI has two-address uses of the same register. If the use
2419 // isn't the first and only use in the BB, then we can't fold
2420 // it. FIXME: Move this to rewriteInstructionsForSpills.
2421 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002422 break;
2423 }
Evan Chengaee4af62007-12-02 08:30:39 +00002424 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002425 }
2426 }
2427 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002428 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002429 if (CanFold && !Ops.empty()) {
2430 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002431 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002432 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002433 // Also folded uses, do not issue a load.
2434 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002435 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002436 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002437 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002438 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002439 }
2440
Evan Cheng7e073ba2008-04-09 20:57:25 +00002441 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002442 if (!Folded) {
2443 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2444 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002445 if (!MI->registerDefIsDead(nI.reg))
2446 // No need to spill a dead def.
2447 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002448 if (isKill)
2449 AddedKill.insert(&nI);
2450 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002451 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002452 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002453 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002454 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002455
Evan Cheng1953d0c2007-11-29 10:12:14 +00002456 int Id = RestoreMBBs.find_first();
2457 while (Id != -1) {
2458 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2459 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002460 LiveIndex index = restores[i].index;
2461 if (index == LiveIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002462 continue;
2463 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002464 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002465 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002466 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002467 bool CanFold = false;
2468 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002469 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002470 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002471 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2472 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002473 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002474 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002475
Evan Cheng0cbb1162007-11-29 01:06:25 +00002476 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002477 // If this restore were to be folded, it would have been folded
2478 // already.
2479 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002480 break;
2481 }
Evan Chengaee4af62007-12-02 08:30:39 +00002482 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002483 }
2484 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002485
2486 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002487 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002488 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002489 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002490 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2491 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002492 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2493 int LdSlot = 0;
2494 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2495 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002496 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002497 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2498 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002499 if (!Folded) {
2500 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2501 if (ImpUse) {
2502 // Re-matting an instruction with virtual register use. Add the
2503 // register as an implicit use on the use MI and update the register
2504 // interval's spill weight to HUGE_VALF to prevent it from being
2505 // spilled.
2506 LiveInterval &ImpLi = getInterval(ImpUse);
2507 ImpLi.weight = HUGE_VALF;
2508 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2509 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002510 }
Evan Chengaee4af62007-12-02 08:30:39 +00002511 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002512 }
2513 // If folding is not possible / failed, then tell the spiller to issue a
2514 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002515 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002516 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002517 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002518 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002519 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002520 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002521 }
2522
Evan Chengb50bb8c2007-12-05 08:16:32 +00002523 // Finalize intervals: add kills, finalize spill weights, and filter out
2524 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002525 std::vector<LiveInterval*> RetNewLIs;
2526 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2527 LiveInterval *LI = NewLIs[i];
2528 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002529 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002530 if (!AddedKill.count(LI)) {
2531 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hamescc3b0652009-10-03 04:21:37 +00002532 LiveIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002533 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002534 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002535 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002536 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002537 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002538 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002539 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002540 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002541 RetNewLIs.push_back(LI);
2542 }
2543 }
Evan Cheng81a03822007-11-17 00:40:40 +00002544
Evan Cheng4cce6b42008-04-11 17:53:36 +00002545 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002546 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002547}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002548
2549/// hasAllocatableSuperReg - Return true if the specified physical register has
2550/// any super register that's allocatable.
2551bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2552 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2553 if (allocatableRegs_[*AS] && hasInterval(*AS))
2554 return true;
2555 return false;
2556}
2557
2558/// getRepresentativeReg - Find the largest super register of the specified
2559/// physical register.
2560unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2561 // Find the largest super-register that is allocatable.
2562 unsigned BestReg = Reg;
2563 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2564 unsigned SuperReg = *AS;
2565 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2566 BestReg = SuperReg;
2567 break;
2568 }
2569 }
2570 return BestReg;
2571}
2572
2573/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2574/// specified interval that conflicts with the specified physical register.
2575unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2576 unsigned PhysReg) const {
2577 unsigned NumConflicts = 0;
2578 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2579 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2580 E = mri_->reg_end(); I != E; ++I) {
2581 MachineOperand &O = I.getOperand();
2582 MachineInstr *MI = O.getParent();
Lang Hamescc3b0652009-10-03 04:21:37 +00002583 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002584 if (pli.liveAt(Index))
2585 ++NumConflicts;
2586 }
2587 return NumConflicts;
2588}
2589
2590/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002591/// around all defs and uses of the specified interval. Return true if it
2592/// was able to cut its interval.
2593bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002594 unsigned PhysReg, VirtRegMap &vrm) {
2595 unsigned SpillReg = getRepresentativeReg(PhysReg);
2596
2597 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2598 // If there are registers which alias PhysReg, but which are not a
2599 // sub-register of the chosen representative super register. Assert
2600 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002601 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002602 tri_->isSuperRegister(*AS, SpillReg));
2603
Evan Cheng2824a652009-03-23 18:24:37 +00002604 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00002605 SmallVector<unsigned, 4> PRegs;
2606 if (hasInterval(SpillReg))
2607 PRegs.push_back(SpillReg);
2608 else {
2609 SmallSet<unsigned, 4> Added;
2610 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2611 if (Added.insert(*AS) && hasInterval(*AS)) {
2612 PRegs.push_back(*AS);
2613 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2614 Added.insert(*ASS);
2615 }
2616 }
2617
Evan Cheng676dd7c2008-03-11 07:19:34 +00002618 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2619 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2620 E = mri_->reg_end(); I != E; ++I) {
2621 MachineOperand &O = I.getOperand();
2622 MachineInstr *MI = O.getParent();
2623 if (SeenMIs.count(MI))
2624 continue;
2625 SeenMIs.insert(MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002626 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002627 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2628 unsigned PReg = PRegs[i];
2629 LiveInterval &pli = getInterval(PReg);
2630 if (!pli.liveAt(Index))
2631 continue;
2632 vrm.addEmergencySpill(PReg, MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002633 LiveIndex StartIdx = getLoadIndex(Index);
2634 LiveIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002635 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002636 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002637 Cut = true;
2638 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002639 std::string msg;
2640 raw_string_ostream Msg(msg);
2641 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002642 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002643 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002644 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002645 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002646 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002647 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002648 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002649 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002650 if (!hasInterval(*AS))
2651 continue;
2652 LiveInterval &spli = getInterval(*AS);
2653 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002654 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002655 }
2656 }
2657 }
Evan Cheng2824a652009-03-23 18:24:37 +00002658 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002659}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002660
2661LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002662 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002663 LiveInterval& Interval = getOrCreateInterval(reg);
2664 VNInfo* VN = Interval.getNextValue(
Lang Hames6cc91e32009-10-03 04:31:31 +00002665 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames86511252009-09-04 20:41:11 +00002666 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002667 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002668 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2669 LiveRange LR(
Lang Hames6cc91e32009-10-03 04:31:31 +00002670 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002671 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002672 Interval.addRange(LR);
2673
2674 return LR;
2675}
David Greeneb5257662009-08-03 21:55:09 +00002676