blob: b0df51d3c014378e87a175d9517aa541171e8b00 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000030#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000031#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000035#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000040#include "llvm/ADT/DepthFirstIterator.h"
41#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000044#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000045#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000046#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047using namespace llvm;
48
Dan Gohman844731a2008-05-13 00:00:25 +000049// Hidden options for help debugging.
50static cl::opt<bool> DisableReMat("disable-rematerialization",
51 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000052
Dan Gohman4c8f8702008-07-25 15:08:37 +000053static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
54
Owen Andersonae339ba2008-08-19 00:17:30 +000055static cl::opt<bool> EnableFastSpilling("fast-spill",
56 cl::init(false), cl::Hidden);
57
Evan Cheng752195e2009-09-14 21:33:42 +000058static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
59
60static cl::opt<int> CoalescingLimit("early-coalescing-limit",
61 cl::init(-1), cl::Hidden);
62
63STATISTIC(numIntervals , "Number of original intervals");
64STATISTIC(numFolds , "Number of loads/stores folded into instructions");
65STATISTIC(numSplits , "Number of intervals split");
66STATISTIC(numCoalescing, "Number of early coalescing performed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000067
Devang Patel19974732007-05-03 01:11:54 +000068char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000069static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000070
Chris Lattnerf7da2c72006-08-24 22:43:55 +000071void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000072 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000073 AU.addRequired<AliasAnalysis>();
74 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000075 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000076 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000077 AU.addPreservedID(MachineLoopInfoID);
78 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000079
80 if (!StrongPHIElim) {
81 AU.addPreservedID(PHIEliminationID);
82 AU.addRequiredID(PHIEliminationID);
83 }
84
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000087}
88
Chris Lattnerf7da2c72006-08-24 22:43:55 +000089void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000090 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000091 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000092 E = r2iMap_.end(); I != E; ++I)
93 delete I->second;
94
Evan Cheng3f32d652008-06-04 09:18:41 +000095 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000096 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000097 mi2iMap_.clear();
98 i2miMap_.clear();
99 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000100 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000101 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000102
Evan Chengdd199d22007-09-06 01:07:24 +0000103 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
104 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000105 while (!CloneMIs.empty()) {
106 MachineInstr *MI = CloneMIs.back();
107 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000108 mf_->DeleteMachineInstr(MI);
109 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000110}
111
Evan Cheng6ade93b2009-08-05 03:53:14 +0000112static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
Evan Chengb0f59732009-09-21 04:32:32 +0000113 unsigned OpIdx, const TargetInstrInfo *tii_){
Evan Cheng6ade93b2009-08-05 03:53:14 +0000114 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
115 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
116 Reg == SrcReg)
117 return true;
118
Evan Chengb0f59732009-09-21 04:32:32 +0000119 if (OpIdx == 2 && MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000120 return true;
Evan Chengb0f59732009-09-21 04:32:32 +0000121 if (OpIdx == 1 && MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000122 return true;
123 return false;
124}
125
Evan Cheng2578ba22009-07-01 01:59:31 +0000126/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
127/// there is one implicit_def for each use. Add isUndef marker to
128/// implicit_def defs and their uses.
129void LiveIntervals::processImplicitDefs() {
130 SmallSet<unsigned, 8> ImpDefRegs;
131 SmallVector<MachineInstr*, 8> ImpDefMIs;
132 MachineBasicBlock *Entry = mf_->begin();
133 SmallPtrSet<MachineBasicBlock*,16> Visited;
134 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
135 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
136 DFI != E; ++DFI) {
137 MachineBasicBlock *MBB = *DFI;
138 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
139 I != E; ) {
140 MachineInstr *MI = &*I;
141 ++I;
142 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
143 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000144 ImpDefRegs.insert(Reg);
Evan Cheng296925d2009-09-23 06:28:31 +0000145 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
146 for (const unsigned *SS = tri_->getSubRegisters(Reg); *SS; ++SS)
147 ImpDefRegs.insert(*SS);
148 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000149 ImpDefMIs.push_back(MI);
150 continue;
151 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000152
Evan Chengb0f59732009-09-21 04:32:32 +0000153 if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
154 MachineOperand &MO = MI->getOperand(2);
155 if (ImpDefRegs.count(MO.getReg())) {
156 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
157 // This is an identity copy, eliminate it now.
158 if (MO.isKill()) {
159 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
160 vi.removeKill(MI);
161 }
162 MI->eraseFromParent();
163 continue;
164 }
165 }
166
Evan Cheng459a7c62009-07-01 08:19:36 +0000167 bool ChangedToImpDef = false;
168 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000169 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000170 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000171 continue;
172 unsigned Reg = MO.getReg();
173 if (!Reg)
174 continue;
175 if (!ImpDefRegs.count(Reg))
176 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000177 // Use is a copy, just turn it into an implicit_def.
Evan Chengb0f59732009-09-21 04:32:32 +0000178 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000179 bool isKill = MO.isKill();
180 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
181 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
182 MI->RemoveOperand(j);
Evan Chengb0f59732009-09-21 04:32:32 +0000183 if (isKill) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000184 ImpDefRegs.erase(Reg);
Evan Chengb0f59732009-09-21 04:32:32 +0000185 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
186 vi.removeKill(MI);
187 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000188 ChangedToImpDef = true;
189 break;
190 }
191
Evan Cheng2578ba22009-07-01 01:59:31 +0000192 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000193 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
194 // Make sure other uses of
195 for (unsigned j = i+1; j != e; ++j) {
196 MachineOperand &MOJ = MI->getOperand(j);
197 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
198 MOJ.setIsUndef();
199 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000200 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000201 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000202 }
203
Evan Cheng459a7c62009-07-01 08:19:36 +0000204 if (ChangedToImpDef) {
205 // Backtrack to process this new implicit_def.
206 --I;
207 } else {
208 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
209 MachineOperand& MO = MI->getOperand(i);
210 if (!MO.isReg() || !MO.isDef())
211 continue;
212 ImpDefRegs.erase(MO.getReg());
213 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000214 }
215 }
216
217 // Any outstanding liveout implicit_def's?
218 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
219 MachineInstr *MI = ImpDefMIs[i];
220 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000221 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
222 !ImpDefRegs.count(Reg)) {
223 // Delete all "local" implicit_def's. That include those which define
224 // physical registers since they cannot be liveout.
225 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000226 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000227 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000228
229 // If there are multiple defs of the same register and at least one
230 // is not an implicit_def, do not insert implicit_def's before the
231 // uses.
232 bool Skip = false;
233 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
234 DE = mri_->def_end(); DI != DE; ++DI) {
235 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
236 Skip = true;
237 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000238 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000239 }
240 if (Skip)
241 continue;
242
Evan Chengd129d732009-07-17 19:43:40 +0000243 // The only implicit_def which we want to keep are those that are live
244 // out of its block.
245 MI->eraseFromParent();
246
Evan Cheng459a7c62009-07-01 08:19:36 +0000247 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
248 UE = mri_->use_end(); UI != UE; ) {
249 MachineOperand &RMO = UI.getOperand();
250 MachineInstr *RMI = &*UI;
251 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000252 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000253 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000254 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000255
256 // Turn a copy use into an implicit_def.
257 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
258 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
259 Reg == SrcReg) {
260 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
261 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
262 RMI->RemoveOperand(j);
263 continue;
264 }
265
Evan Cheng2578ba22009-07-01 01:59:31 +0000266 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
267 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000268 RMO.setReg(NewVReg);
269 RMO.setIsUndef();
270 RMO.setIsKill();
271 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000272 }
273 ImpDefRegs.clear();
274 ImpDefMIs.clear();
275 }
276}
277
Lang Hames86511252009-09-04 20:41:11 +0000278
Owen Anderson80b3ce62008-05-28 20:54:50 +0000279void LiveIntervals::computeNumbering() {
280 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000281 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000282
283 Idx2MBBMap.clear();
284 MBB2IdxMap.clear();
285 mi2iMap_.clear();
286 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000287 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000288 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000289
Owen Andersona1566f22008-07-22 22:46:49 +0000290 FunctionSize = 0;
291
Chris Lattner428b92e2006-09-15 03:57:23 +0000292 // Number MachineInstrs and MachineBasicBlocks.
293 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000294 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
Lang Hames6cc91e32009-10-03 04:31:31 +0000295 std::make_pair(LiveIndex(),LiveIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000296
Lang Hamescc3b0652009-10-03 04:21:37 +0000297 LiveIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000298 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
299 MBB != E; ++MBB) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000300 LiveIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000301
Owen Anderson7fbad272008-07-23 21:37:49 +0000302 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000303 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000304 i2miMap_.push_back(0);
305
Chris Lattner428b92e2006-09-15 03:57:23 +0000306 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
307 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000308
309 if (I == MBB->getFirstTerminator()) {
310 // Leave a gap for before terminators, this is where we will point
311 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000312 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000313 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000314 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000315 assert(inserted &&
316 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000317 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000318 i2miMap_.push_back(0);
319
Lang Hames35f291d2009-09-12 03:34:03 +0000320 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000321 }
322
Chris Lattner428b92e2006-09-15 03:57:23 +0000323 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000325 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000326 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000327 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000328 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000329
Evan Cheng4ed43292008-10-18 05:21:37 +0000330 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000331 unsigned Slots = I->getDesc().getNumDefs();
332 if (Slots == 0)
333 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000334 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000335 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000336 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000337 }
338
Owen Anderson35578012008-06-16 07:10:49 +0000339 }
Lang Hamesffd13262009-07-09 03:57:02 +0000340
341 if (MBB->getFirstTerminator() == MBB->end()) {
342 // Leave a gap for before terminators, this is where we will point
343 // PHI kills.
Lang Hamescc3b0652009-10-03 04:21:37 +0000344 LiveIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000345 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000346 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000347 assert(inserted &&
348 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000349 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000350 i2miMap_.push_back(0);
351
Lang Hames35f291d2009-09-12 03:34:03 +0000352 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000353 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000354
Owen Anderson1fbb4542008-06-16 16:58:24 +0000355 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000356 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000357 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000358 }
Lang Hamesffd13262009-07-09 03:57:02 +0000359
Evan Cheng4ca980e2007-10-17 02:10:22 +0000360 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000361
362 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000363 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000364 for (LiveInterval::iterator LI = OI->second->begin(),
365 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000366
Owen Anderson7eec0c22008-05-29 23:01:22 +0000367 // Remap the start index of the live range to the corresponding new
368 // number, or our best guess at what it _should_ correspond to if the
369 // original instruction has been erased. This is either the following
370 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000371 unsigned index = LI->start.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000372 LiveIndex::Slot offset = LI->start.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000373 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000374 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000375 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000376 // Take the pair containing the index
377 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000378 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000379
Owen Anderson7fbad272008-07-23 21:37:49 +0000380 LI->start = getMBBStartIdx(J->second);
381 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000382 LI->start = LiveIndex(
383 LiveIndex(mi2iMap_[OldI2MI[index]]),
384 (LiveIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000385 }
386
387 // Remap the ending index in the same way that we remapped the start,
388 // except for the final step where we always map to the immediately
389 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000390 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000391 offset = LI->end.getSlot();
392 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000393 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000394 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000395 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000396 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000397
Lang Hames35f291d2009-09-12 03:34:03 +0000398 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000399 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000400 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000401 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
402
403 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000404 LI->end =
Lang Hamescc3b0652009-10-03 04:21:37 +0000405 LiveIndex(mi2iMap_[OldI2MI[index]],
406 (idx == index ? offset : LiveIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000407 else
Lang Hames86511252009-09-04 20:41:11 +0000408 LI->end =
Lang Hames6cc91e32009-10-03 04:31:31 +0000409 LiveIndex(LiveIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000410 }
Owen Anderson788d0412008-08-06 18:35:45 +0000411 }
412
Owen Anderson03857b22008-08-13 21:49:13 +0000413 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
414 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000415 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000416
Owen Anderson7eec0c22008-05-29 23:01:22 +0000417 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000418 // start indices above. VN's with special sentinel defs
419 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000420 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000421 unsigned index = vni->def.getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000422 LiveIndex::Slot offset = vni->def.getSlot();
Lang Hames86511252009-09-04 20:41:11 +0000423 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000424 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000425 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000426 // Take the pair containing the index
427 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000428 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000429
Owen Anderson91292392008-07-30 17:42:47 +0000430 vni->def = getMBBStartIdx(J->second);
431 } else {
Lang Hamescc3b0652009-10-03 04:21:37 +0000432 vni->def = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000433 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000434 }
Owen Anderson745825f42008-05-28 22:40:08 +0000435
Owen Anderson7eec0c22008-05-29 23:01:22 +0000436 // Remap the VNInfo kill indices, which works the same as
437 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000438 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000439 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hamescc3b0652009-10-03 04:21:37 +0000440 LiveIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000441
Lang Hames86511252009-09-04 20:41:11 +0000442 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000443 assert("Value killed at a load slot.");
444 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000445 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000446 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000447
Lang Hamesffd13262009-07-09 03:57:02 +0000448 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000449 } else {
Lang Hames86511252009-09-04 20:41:11 +0000450 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000451 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000452 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000453 --I;
Lang Hames86511252009-09-04 20:41:11 +0000454 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000455 } else {
456 assert(OldI2MI[index] != 0 &&
457 "Kill refers to instruction not present in index maps.");
Lang Hamescc3b0652009-10-03 04:21:37 +0000458 vni->kills[i] = LiveIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000459 }
460
461 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000462 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000463 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
464
465 if (index != OldI2MI.size())
466 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
467 (idx == index ? offset : 0);
468 else
469 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000470 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000471 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000472 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000473 }
Owen Anderson788d0412008-08-06 18:35:45 +0000474 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000475}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000476
Lang Hamesf41538d2009-06-02 16:53:25 +0000477void LiveIntervals::scaleNumbering(int factor) {
478 // Need to
479 // * scale MBB begin and end points
480 // * scale all ranges.
481 // * Update VNI structures.
482 // * Scale instruction numberings
483
484 // Scale the MBB indices.
485 Idx2MBBMap.clear();
486 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
487 MBB != MBBE; ++MBB) {
Lang Hames6cc91e32009-10-03 04:31:31 +0000488 std::pair<LiveIndex, LiveIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
Lang Hames86511252009-09-04 20:41:11 +0000489 mbbIndices.first = mbbIndices.first.scale(factor);
490 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000491 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
492 }
493 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
494
Lang Hamesffd13262009-07-09 03:57:02 +0000495 // Scale terminator gaps.
Lang Hamescc3b0652009-10-03 04:21:37 +0000496 for (DenseMap<MachineBasicBlock*, LiveIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000497 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
498 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000499 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000500 }
501
Lang Hamesf41538d2009-06-02 16:53:25 +0000502 // Scale the intervals.
503 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
504 LI->second->scaleNumbering(factor);
505 }
506
507 // Scale MachineInstrs.
508 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hamescc3b0652009-10-03 04:21:37 +0000509 LiveIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000510 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
511 MI != ME; ++MI) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000512 LiveIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000513 mi2iMap_[MI->first] = newSlot;
514 highestSlot = std::max(highestSlot, newSlot);
515 }
516
Lang Hames86511252009-09-04 20:41:11 +0000517 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000518 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000519 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000520 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
521 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000522 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000523 }
524
525}
526
527
Owen Anderson80b3ce62008-05-28 20:54:50 +0000528/// runOnMachineFunction - Register allocate the whole function
529///
530bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
531 mf_ = &fn;
532 mri_ = &mf_->getRegInfo();
533 tm_ = &fn.getTarget();
534 tri_ = tm_->getRegisterInfo();
535 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000536 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000537 lv_ = &getAnalysis<LiveVariables>();
538 allocatableRegs_ = tri_->getAllocatableSet(fn);
539
Evan Cheng2578ba22009-07-01 01:59:31 +0000540 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000541 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000542 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000543 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000544
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000545 numIntervals += getNumIntervals();
546
Chris Lattner70ca3582004-09-30 15:59:17 +0000547 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000548 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000549}
550
Chris Lattner70ca3582004-09-30 15:59:17 +0000551/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000552void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000553 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000554 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000555 I->second->print(OS, tri_);
556 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000557 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000558
Evan Cheng752195e2009-09-14 21:33:42 +0000559 printInstrs(OS);
560}
561
562void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000563 OS << "********** MACHINEINSTRS **********\n";
564
Chris Lattner3380d5c2009-07-21 21:12:58 +0000565 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
566 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000567 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000568 for (MachineBasicBlock::iterator mii = mbbi->begin(),
569 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000570 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000571 }
572 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000573}
574
Evan Cheng752195e2009-09-14 21:33:42 +0000575void LiveIntervals::dumpInstrs() const {
576 printInstrs(errs());
577}
578
Evan Chengc92da382007-11-03 07:20:12 +0000579/// conflictsWithPhysRegDef - Returns true if the specified register
580/// is defined during the duration of the specified interval.
581bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
582 VirtRegMap &vrm, unsigned reg) {
583 for (LiveInterval::Ranges::const_iterator
584 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000585 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000586 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
587 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000588 // skip deleted instructions
589 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000590 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000591 if (index == end) break;
592
593 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000594 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
595 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000596 if (SrcReg == li.reg || DstReg == li.reg)
597 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000598 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
599 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000600 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000601 continue;
602 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000603 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000604 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000605 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000606 if (!vrm.hasPhys(PhysReg))
607 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000608 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000609 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000610 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000611 return true;
612 }
613 }
614 }
615
616 return false;
617}
618
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000619/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
620/// it can check use as well.
621bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
622 unsigned Reg, bool CheckUse,
623 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
624 for (LiveInterval::Ranges::const_iterator
625 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hamescc3b0652009-10-03 04:21:37 +0000626 for (LiveIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000627 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
628 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000629 // Skip deleted instructions.
630 MachineInstr *MI = 0;
631 while (index != end) {
632 MI = getInstructionFromIndex(index);
633 if (MI)
634 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000635 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000636 }
637 if (index == end) break;
638
639 if (JoinedCopies.count(MI))
640 continue;
641 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
642 MachineOperand& MO = MI->getOperand(i);
643 if (!MO.isReg())
644 continue;
645 if (MO.isUse() && !CheckUse)
646 continue;
647 unsigned PhysReg = MO.getReg();
648 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
649 continue;
650 if (tri_->isSubRegister(Reg, PhysReg))
651 return true;
652 }
653 }
654 }
655
656 return false;
657}
658
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000659#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000660static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000661 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000662 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000663 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000664 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000665}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000666#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000667
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000668void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000669 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000670 LiveIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000671 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000672 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000673 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000674 DEBUG({
675 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000676 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000677 });
Evan Cheng419852c2008-04-03 16:39:43 +0000678
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000679 // Virtual registers may be defined multiple times (due to phi
680 // elimination and 2-addr elimination). Much of what we do only has to be
681 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000682 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000683 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000684 if (interval.empty()) {
685 // Get the Idx of the defining instructions.
Lang Hamescc3b0652009-10-03 04:21:37 +0000686 LiveIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000687 // Earlyclobbers move back one, so that they overlap the live range
688 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000689 if (MO.isEarlyClobber())
690 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000691 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000692 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000693 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000694 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000695 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000696 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000697 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000698 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000699 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000700 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000701
702 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000703
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 // Loop over all of the blocks that the vreg is defined in. There are
705 // two cases we have to handle here. The most common case is a vreg
706 // whose lifetime is contained within a basic block. In this case there
707 // will be a single kill, in MBB, which comes after the definition.
708 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
709 // FIXME: what about dead vars?
Lang Hamescc3b0652009-10-03 04:21:37 +0000710 LiveIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000711 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000712 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000713 else if (MO.isEarlyClobber())
714 // Earlyclobbers that die in this instruction move up one extra, to
715 // compensate for having the starting point moved back one. This
716 // gets them to overlap the live range of other outputs.
717 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000718 else
Lang Hames35f291d2009-09-12 03:34:03 +0000719 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000720
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000721 // If the kill happens after the definition, we have an intra-block
722 // live range.
723 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000724 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000725 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000726 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000728 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000729 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000730 return;
731 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000732 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000733
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734 // The other case we handle is when a virtual register lives to the end
735 // of the defining block, potentially live across some blocks, then is
736 // live into some number of blocks, but gets killed. Start by adding a
737 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000738 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000739 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000740 interval.addRange(NewLR);
741
742 // Iterate over all of the blocks that the variable is completely
743 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
744 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000745 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
746 E = vi.AliveBlocks.end(); I != E; ++I) {
747 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000748 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000749 ValNo);
750 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000751 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000752 }
753
754 // Finally, this virtual register is live from the start of any killing
755 // block to the 'use' slot of the killing instruction.
756 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
757 MachineInstr *Kill = vi.Kills[i];
Lang Hamescc3b0652009-10-03 04:21:37 +0000758 LiveIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000759 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Evan Chengb0f59732009-09-21 04:32:32 +0000760 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000761 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000762 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000763 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000764 }
765
766 } else {
767 // If this is the second time we see a virtual register definition, it
768 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000769 // the result of two address elimination, then the vreg is one of the
770 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000771 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000772 // If this is a two-address definition, then we have already processed
773 // the live range. The only problem is that we didn't realize there
774 // are actually two values in the live interval. Because of this we
775 // need to take the LiveRegion that defines this register and split it
776 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000777 assert(interval.containsOneValue());
Lang Hamescc3b0652009-10-03 04:21:37 +0000778 LiveIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
779 LiveIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000780 if (MO.isEarlyClobber())
781 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000782
Lang Hames35f291d2009-09-12 03:34:03 +0000783 const LiveRange *OldLR =
784 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000785 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000786
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000787 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000788 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000789 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000790
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000791 // Two-address vregs should always only be redefined once. This means
792 // that at this point, there should be exactly one value number in it.
793 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
794
Chris Lattner91725b72006-08-31 05:54:43 +0000795 // The new value number (#1) is defined by the instruction we claimed
796 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000797 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000798 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000799 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000800 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
801
Chris Lattner91725b72006-08-31 05:54:43 +0000802 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000803 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000804 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000805 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000806 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000807
808 // Add the new live interval which replaces the range for the input copy.
809 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000810 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000811 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000812 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000813
814 // If this redefinition is dead, we need to add a dummy unit live
815 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000816 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000817 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000818 LiveRange(RedefIndex, MO.isEarlyClobber() ?
819 getNextSlot(getNextSlot(RedefIndex)) :
820 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000821
Bill Wendling8e6179f2009-08-22 20:18:03 +0000822 DEBUG({
823 errs() << " RESULT: ";
824 interval.print(errs(), tri_);
825 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000826 } else {
827 // Otherwise, this must be because of phi elimination. If this is the
828 // first redefinition of the vreg that we have seen, go back and change
829 // the live range in the PHI block to be a different value number.
830 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000831 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000832 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000833 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000834 phiJoinCopies.push_back(Killer);
Lang Hamescc3b0652009-10-03 04:21:37 +0000835 LiveIndex Start = getMBBStartIdx(Killer->getParent());
836 LiveIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000837 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000838 DEBUG({
839 errs() << " Removing [" << Start << "," << End << "] from: ";
840 interval.print(errs(), tri_);
841 errs() << "\n";
842 });
Lang Hamesffd13262009-07-09 03:57:02 +0000843 interval.removeRange(Start, End);
844 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000845 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000846 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
847 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000848 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000849 DEBUG({
850 errs() << " RESULT: ";
851 interval.print(errs(), tri_);
852 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000853
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000854 // Replace the interval with one of a NEW value number. Note that this
855 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000856 LiveRange LR(Start, End,
Lang Hamescc3b0652009-10-03 04:21:37 +0000857 interval.getNextValue(LiveIndex(mbb->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +0000858 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000859 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000860 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000861 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000862 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000863 DEBUG({
864 errs() << " RESULT: ";
865 interval.print(errs(), tri_);
866 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000867 }
868
869 // In the case of PHI elimination, each variable definition is only
870 // live until the end of the block. We've already taken care of the
871 // rest of the live range.
Lang Hamescc3b0652009-10-03 04:21:37 +0000872 LiveIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000873 if (MO.isEarlyClobber())
874 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000875
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000876 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000877 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000878 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000879 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000880 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000881 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000882 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000883 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000884 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000885
Lang Hamescc3b0652009-10-03 04:21:37 +0000886 LiveIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000887 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000888 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000889 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000890 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000891 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000892 }
893 }
894
Bill Wendling8e6179f2009-08-22 20:18:03 +0000895 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000896}
897
Chris Lattnerf35fef72004-07-23 21:24:19 +0000898void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000899 MachineBasicBlock::iterator mi,
Lang Hamescc3b0652009-10-03 04:21:37 +0000900 LiveIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000901 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000902 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000903 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000904 // A physical register cannot be live across basic block, so its
905 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000906 DEBUG({
907 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000908 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000909 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000910
Lang Hamescc3b0652009-10-03 04:21:37 +0000911 LiveIndex baseIndex = MIIdx;
912 LiveIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000913 // Earlyclobbers move back one.
914 if (MO.isEarlyClobber())
915 start = getUseIndex(MIIdx);
Lang Hamescc3b0652009-10-03 04:21:37 +0000916 LiveIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000917
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000918 // If it is not used after definition, it is considered dead at
919 // the instruction defining it. Hence its interval is:
920 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000921 // For earlyclobbers, the defSlot was pushed back one; the extra
922 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000923 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000924 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000925 if (MO.isEarlyClobber())
926 end = getNextSlot(getNextSlot(start));
927 else
928 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000929 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000930 }
931
932 // If it is not dead on definition, it must be killed by a
933 // subsequent instruction. Hence its interval is:
934 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000935 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000936 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000937 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000938 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000939 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000940 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000941 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000942 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000943 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000944 } else {
945 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
946 if (DefIdx != -1) {
947 if (mi->isRegTiedToUseOperand(DefIdx)) {
948 // Two-address instruction.
949 end = getDefIndex(baseIndex);
950 if (mi->getOperand(DefIdx).isEarlyClobber())
951 end = getUseIndex(baseIndex);
952 } else {
953 // Another instruction redefines the register before it is ever read.
954 // Then the register is essentially dead at the instruction that defines
955 // it. Hence its interval is:
956 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000957 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000958 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000959 }
960 goto exit;
961 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000962 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000963
Lang Hames35f291d2009-09-12 03:34:03 +0000964 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000965 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000966
967 // The only case we should have a dead physreg here without a killing or
968 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000969 // and never used. Another possible case is the implicit use of the
970 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000971 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000972
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000973exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000974 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000975
Evan Cheng24a3cc42007-04-25 07:30:23 +0000976 // Already exists? Extend old live interval.
977 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000978 bool Extend = OldLR != interval.end();
979 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000980 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000981 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000982 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000983 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000984 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000985 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000986 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000987}
988
Chris Lattnerf35fef72004-07-23 21:24:19 +0000989void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
990 MachineBasicBlock::iterator MI,
Lang Hamescc3b0652009-10-03 04:21:37 +0000991 LiveIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000992 MachineOperand& MO,
993 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000994 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000995 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000996 getOrCreateInterval(MO.getReg()));
997 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000998 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000999 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +00001000 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +00001001 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +00001002 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +00001003 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001004 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +00001005 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001006 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +00001007 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +00001008 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +00001009 // If MI also modifies the sub-register explicitly, avoid processing it
1010 // more than once. Do not pass in TRI here so it checks for exact match.
1011 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +00001012 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001013 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +00001014 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001015}
1016
Evan Chengb371f452007-02-19 21:49:54 +00001017void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001018 LiveIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001019 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001020 DEBUG({
1021 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001022 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001023 });
Evan Chengb371f452007-02-19 21:49:54 +00001024
1025 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1026 // be considered a livein.
1027 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hamescc3b0652009-10-03 04:21:37 +00001028 LiveIndex baseIndex = MIIdx;
1029 LiveIndex start = baseIndex;
Lang Hames86511252009-09-04 20:41:11 +00001030 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001031 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001032 baseIndex = getNextIndex(baseIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001033 LiveIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001034 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001035
Evan Chengb371f452007-02-19 21:49:54 +00001036 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001037 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001038 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001039 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001040 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001041 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001042 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001043 // Another instruction redefines the register before it is ever read.
1044 // Then the register is essentially dead at the instruction that defines
1045 // it. Hence its interval is:
1046 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001047 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001048 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001049 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001050 break;
Evan Chengb371f452007-02-19 21:49:54 +00001051 }
1052
Lang Hames35f291d2009-09-12 03:34:03 +00001053 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001054 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001055 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001056 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001057 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001058 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001059 }
Evan Chengb371f452007-02-19 21:49:54 +00001060 }
1061
Evan Cheng75611fb2007-06-27 01:16:36 +00001062 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001063 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001064 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001065 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001066 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001067 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001068 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001069 end = baseIndex;
1070 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001071 }
1072
Lang Hames10382fb2009-06-19 02:17:53 +00001073 VNInfo *vni =
Lang Hamescc3b0652009-10-03 04:21:37 +00001074 interval.getNextValue(LiveIndex(MBB->getNumber()),
Lang Hames86511252009-09-04 20:41:11 +00001075 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001076 vni->setIsPHIDef(true);
1077 LiveRange LR(start, end, vni);
1078
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001079 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001080 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001081 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001082}
1083
Evan Cheng752195e2009-09-14 21:33:42 +00001084bool
1085LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1086 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001087 SmallVector<MachineInstr*,16> &OtherCopies) {
1088 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001089 unsigned NumIdent = 0;
Dan Gohman2bf06492009-09-25 22:26:13 +00001090 for (MachineRegisterInfo::def_iterator ri = mri_->def_begin(SrcInt.reg),
1091 re = mri_->def_end(); ri != re; ++ri) {
Evan Cheng752195e2009-09-14 21:33:42 +00001092 MachineInstr *MI = &*ri;
1093 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1094 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001095 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001096 if (SrcReg != DstInt.reg) {
1097 OtherCopies.push_back(MI);
1098 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1099 } else {
1100 IdentCopies.push_back(MI);
1101 ++NumIdent;
1102 }
1103 }
1104
Evan Cheng3f855492009-09-15 06:45:16 +00001105 if (!HaveConflict)
1106 return false; // Let coalescer handle it
1107 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001108}
1109
1110void LiveIntervals::performEarlyCoalescing() {
1111 if (!EarlyCoalescing)
1112 return;
1113
1114 /// Perform early coalescing: eliminate copies which feed into phi joins
1115 /// and whose sources are defined by the phi joins.
1116 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1117 MachineInstr *Join = phiJoinCopies[i];
1118 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1119 break;
1120
1121 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1122 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1123#ifndef NDEBUG
1124 assert(isMove && "PHI join instruction must be a move!");
1125#else
1126 isMove = isMove;
1127#endif
1128
1129 LiveInterval &DstInt = getInterval(PHIDst);
1130 LiveInterval &SrcInt = getInterval(PHISrc);
1131 SmallVector<MachineInstr*, 16> IdentCopies;
1132 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001133 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001134 continue;
1135
1136 DEBUG(errs() << "PHI Join: " << *Join);
1137 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1138 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001139
Evan Cheng3f855492009-09-15 06:45:16 +00001140 // Change the non-identity copies to directly target the phi destination.
1141 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1142 MachineInstr *PHICopy = OtherCopies[i];
1143 DEBUG(errs() << "Moving: " << *PHICopy);
1144
Lang Hamescc3b0652009-10-03 04:21:37 +00001145 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1146 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng752195e2009-09-14 21:33:42 +00001147 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001148 LiveIndex StartIndex = SLR->start;
1149 LiveIndex EndIndex = SLR->end;
Evan Cheng752195e2009-09-14 21:33:42 +00001150
1151 // Delete val# defined by the now identity copy and add the range from
1152 // beginning of the mbb to the end of the range.
1153 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001154 DEBUG(errs() << " added range [" << StartIndex << ','
1155 << EndIndex << "] to reg" << DstInt.reg << '\n');
1156 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001157 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001158 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1159 VNInfoAllocator);
1160 NewVNI->setHasPHIKill(true);
1161 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1162 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1163 MachineOperand &MO = PHICopy->getOperand(j);
1164 if (!MO.isReg() || MO.getReg() != PHISrc)
1165 continue;
1166 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001167 }
Evan Cheng3f855492009-09-15 06:45:16 +00001168 }
1169
1170 // Now let's eliminate all the would-be identity copies.
1171 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1172 MachineInstr *PHICopy = IdentCopies[i];
1173 DEBUG(errs() << "Coalescing: " << *PHICopy);
1174
Lang Hamescc3b0652009-10-03 04:21:37 +00001175 LiveIndex MIIndex = getInstructionIndex(PHICopy);
1176 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001177 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hamescc3b0652009-10-03 04:21:37 +00001178 LiveIndex StartIndex = SLR->start;
1179 LiveIndex EndIndex = SLR->end;
Evan Cheng3f855492009-09-15 06:45:16 +00001180
1181 // Delete val# defined by the now identity copy and add the range from
1182 // beginning of the mbb to the end of the range.
1183 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001184 RemoveMachineInstrFromMaps(PHICopy);
1185 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001186 DEBUG(errs() << " added range [" << StartIndex << ','
1187 << EndIndex << "] to reg" << DstInt.reg << '\n');
1188 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001189 }
Evan Cheng752195e2009-09-14 21:33:42 +00001190
Evan Cheng3f855492009-09-15 06:45:16 +00001191 // Remove the phi join and update the phi block liveness.
Lang Hamescc3b0652009-10-03 04:21:37 +00001192 LiveIndex MIIndex = getInstructionIndex(Join);
1193 LiveIndex UseIndex = getUseIndex(MIIndex);
1194 LiveIndex DefIndex = getDefIndex(MIIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001195 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1196 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1197 DLR->valno->setCopy(0);
1198 DLR->valno->setIsDefAccurate(false);
1199 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1200 SrcInt.removeRange(SLR->start, SLR->end);
1201 assert(SrcInt.empty());
1202 removeInterval(PHISrc);
1203 RemoveMachineInstrFromMaps(Join);
1204 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001205
1206 ++numCoalescing;
1207 }
1208}
1209
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001210/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001211/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001212/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001213/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001214void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001215 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001216 << "********** Function: "
1217 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001218
1219 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001220 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1221 MBBI != E; ++MBBI) {
1222 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001223 // Track the index of the current machine instr.
Lang Hamescc3b0652009-10-03 04:21:37 +00001224 LiveIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001225 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001226
Chris Lattner428b92e2006-09-15 03:57:23 +00001227 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001228
Dan Gohmancb406c22007-10-03 19:26:29 +00001229 // Create intervals for live-ins to this BB first.
1230 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1231 LE = MBB->livein_end(); LI != LE; ++LI) {
1232 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1233 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001234 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001235 if (!hasInterval(*AS))
1236 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1237 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001238 }
1239
Owen Anderson99500ae2008-09-15 22:00:38 +00001240 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001241 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001242 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001243 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001244
Chris Lattner428b92e2006-09-15 03:57:23 +00001245 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001246 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001247
Evan Cheng438f7bc2006-11-10 08:43:01 +00001248 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001249 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1250 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001251 if (!MO.isReg() || !MO.getReg())
1252 continue;
1253
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001254 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001255 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001256 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001257 else if (MO.isUndef())
1258 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001259 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001260
1261 // Skip over the empty slots after each instruction.
1262 unsigned Slots = MI->getDesc().getNumDefs();
1263 if (Slots == 0)
1264 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001265
1266 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001267 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001268
1269 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001270 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001271 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001272 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001273 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001274 }
Evan Chengd129d732009-07-17 19:43:40 +00001275
1276 // Create empty intervals for registers defined by implicit_def's (except
1277 // for those implicit_def that define values which are liveout of their
1278 // blocks.
1279 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1280 unsigned UndefReg = UndefUses[i];
1281 (void)getOrCreateInterval(UndefReg);
1282 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001283}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001284
Lang Hames86511252009-09-04 20:41:11 +00001285bool LiveIntervals::findLiveInMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001286 LiveIndex Start, LiveIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001287 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001288 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001289 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001290
1291 bool ResVal = false;
1292 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001293 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001294 break;
1295 MBBs.push_back(I->second);
1296 ResVal = true;
1297 ++I;
1298 }
1299 return ResVal;
1300}
1301
Lang Hames86511252009-09-04 20:41:11 +00001302bool LiveIntervals::findReachableMBBs(
Lang Hames6cc91e32009-10-03 04:31:31 +00001303 LiveIndex Start, LiveIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001304 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1305 std::vector<IdxMBBPair>::const_iterator I =
1306 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1307
1308 bool ResVal = false;
1309 while (I != Idx2MBBMap.end()) {
1310 if (I->first > End)
1311 break;
1312 MachineBasicBlock *MBB = I->second;
1313 if (getMBBEndIdx(MBB) > End)
1314 break;
1315 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1316 SE = MBB->succ_end(); SI != SE; ++SI)
1317 MBBs.push_back(*SI);
1318 ResVal = true;
1319 ++I;
1320 }
1321 return ResVal;
1322}
1323
Owen Anderson03857b22008-08-13 21:49:13 +00001324LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001325 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001326 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001327}
Evan Chengf2fbca62007-11-12 06:35:08 +00001328
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001329/// dupInterval - Duplicate a live interval. The caller is responsible for
1330/// managing the allocated memory.
1331LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1332 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001333 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001334 return NewLI;
1335}
1336
Evan Chengc8d044e2008-02-15 18:24:29 +00001337/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1338/// copy field and returns the source register that defines it.
1339unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001340 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001341 return 0;
1342
Lang Hames52c1afc2009-08-10 23:43:28 +00001343 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001344 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001345 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001346 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001347 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001348 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001349 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1350 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1351 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001352
Evan Cheng04ee5a12009-01-20 19:12:24 +00001353 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001354 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001355 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001356 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001357 return 0;
1358}
Evan Chengf2fbca62007-11-12 06:35:08 +00001359
1360//===----------------------------------------------------------------------===//
1361// Register allocator hooks.
1362//
1363
Evan Chengd70dbb52008-02-22 09:24:50 +00001364/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1365/// allow one) virtual register operand, then its uses are implicitly using
1366/// the register. Returns the virtual register.
1367unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1368 MachineInstr *MI) const {
1369 unsigned RegOp = 0;
1370 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1371 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001372 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001373 continue;
1374 unsigned Reg = MO.getReg();
1375 if (Reg == 0 || Reg == li.reg)
1376 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001377
1378 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1379 !allocatableRegs_[Reg])
1380 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001381 // FIXME: For now, only remat MI with at most one register operand.
1382 assert(!RegOp &&
1383 "Can't rematerialize instruction with multiple register operand!");
1384 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001385#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001386 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001387#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001388 }
1389 return RegOp;
1390}
1391
1392/// isValNoAvailableAt - Return true if the val# of the specified interval
1393/// which reaches the given instruction also reaches the specified use index.
1394bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001395 LiveIndex UseIdx) const {
1396 LiveIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001397 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1398 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1399 return UI != li.end() && UI->valno == ValNo;
1400}
1401
Evan Chengf2fbca62007-11-12 06:35:08 +00001402/// isReMaterializable - Returns true if the definition MI of the specified
1403/// val# of the specified interval is re-materializable.
1404bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001405 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001406 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001407 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001408 if (DisableReMat)
1409 return false;
1410
Evan Cheng20ccded2008-03-15 00:19:36 +00001411 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001412 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001413
1414 int FrameIdx = 0;
1415 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001416 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001417 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1418 // this but remember this is not safe to fold into a two-address
1419 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001420 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001421 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001422
Dan Gohman6d69ba82008-07-25 00:02:30 +00001423 // If the target-specific rules don't identify an instruction as
1424 // being trivially rematerializable, use some target-independent
1425 // rules.
1426 if (!MI->getDesc().isRematerializable() ||
1427 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001428 if (!EnableAggressiveRemat)
1429 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001430
Dan Gohman0471a792008-07-28 18:43:51 +00001431 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001432 // we can't analyze it.
1433 const TargetInstrDesc &TID = MI->getDesc();
1434 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1435 return false;
1436
1437 // Avoid instructions obviously unsafe for remat.
1438 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1439 return false;
1440
1441 // If the instruction accesses memory and the memory could be non-constant,
1442 // assume the instruction is not rematerializable.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001443 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
1444 E = MI->memoperands_end(); I != E; ++I){
1445 const MachineMemOperand *MMO = *I;
1446 if (MMO->isVolatile() || MMO->isStore())
Dan Gohman6d69ba82008-07-25 00:02:30 +00001447 return false;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001448 const Value *V = MMO->getValue();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001449 if (!V)
1450 return false;
1451 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1452 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001453 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001454 } else if (!aa_->pointsToConstantMemory(V))
1455 return false;
1456 }
1457
1458 // If any of the registers accessed are non-constant, conservatively assume
1459 // the instruction is not rematerializable.
1460 unsigned ImpUse = 0;
1461 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1462 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001463 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001464 unsigned Reg = MO.getReg();
1465 if (Reg == 0)
1466 continue;
1467 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1468 return false;
1469
1470 // Only allow one def, and that in the first operand.
1471 if (MO.isDef() != (i == 0))
1472 return false;
1473
1474 // Only allow constant-valued registers.
1475 bool IsLiveIn = mri_->isLiveIn(Reg);
1476 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1477 E = mri_->def_end();
1478
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001479 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001480 if (MO.isDef() && (next(I) != E || IsLiveIn))
1481 return false;
1482
1483 if (MO.isUse()) {
1484 // Only allow one use other register use, as that's all the
1485 // remat mechanisms support currently.
1486 if (Reg != li.reg) {
1487 if (ImpUse == 0)
1488 ImpUse = Reg;
1489 else if (Reg != ImpUse)
1490 return false;
1491 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001492 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001493 if (I != E && (next(I) != E || IsLiveIn))
1494 return false;
1495 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001496 }
1497 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001498 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001499
Dan Gohman6d69ba82008-07-25 00:02:30 +00001500 unsigned ImpUse = getReMatImplicitUse(li, MI);
1501 if (ImpUse) {
1502 const LiveInterval &ImpLi = getInterval(ImpUse);
1503 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1504 re = mri_->use_end(); ri != re; ++ri) {
1505 MachineInstr *UseMI = &*ri;
Lang Hamescc3b0652009-10-03 04:21:37 +00001506 LiveIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001507 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1508 continue;
1509 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1510 return false;
1511 }
Evan Chengdc377862008-09-30 15:44:16 +00001512
1513 // If a register operand of the re-materialized instruction is going to
1514 // be spilled next, then it's not legal to re-materialize this instruction.
1515 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1516 if (ImpUse == SpillIs[i]->reg)
1517 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001518 }
1519 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001520}
1521
Evan Cheng06587492008-10-24 02:05:00 +00001522/// isReMaterializable - Returns true if the definition MI of the specified
1523/// val# of the specified interval is re-materializable.
1524bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1525 const VNInfo *ValNo, MachineInstr *MI) {
1526 SmallVector<LiveInterval*, 4> Dummy1;
1527 bool Dummy2;
1528 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1529}
1530
Evan Cheng5ef3a042007-12-06 00:01:56 +00001531/// isReMaterializable - Returns true if every definition of MI of every
1532/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001533bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1534 SmallVectorImpl<LiveInterval*> &SpillIs,
1535 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001536 isLoad = false;
1537 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1538 i != e; ++i) {
1539 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001540 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001541 continue; // Dead val#.
1542 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001543 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001544 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001545 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001546 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001547 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001548 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001549 return false;
1550 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001551 }
1552 return true;
1553}
1554
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001555/// FilterFoldedOps - Filter out two-address use operands. Return
1556/// true if it finds any issue with the operands that ought to prevent
1557/// folding.
1558static bool FilterFoldedOps(MachineInstr *MI,
1559 SmallVector<unsigned, 2> &Ops,
1560 unsigned &MRInfo,
1561 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001562 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001563 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1564 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001565 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001566 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001567 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001568 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001569 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001570 MRInfo |= (unsigned)VirtRegMap::isMod;
1571 else {
1572 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001573 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001574 MRInfo = VirtRegMap::isModRef;
1575 continue;
1576 }
1577 MRInfo |= (unsigned)VirtRegMap::isRef;
1578 }
1579 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001580 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001581 return false;
1582}
1583
1584
1585/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1586/// slot / to reg or any rematerialized load into ith operand of specified
1587/// MI. If it is successul, MI is updated with the newly created MI and
1588/// returns true.
1589bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1590 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hamescc3b0652009-10-03 04:21:37 +00001591 LiveIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001592 SmallVector<unsigned, 2> &Ops,
1593 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001594 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001595 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001596 RemoveMachineInstrFromMaps(MI);
1597 vrm.RemoveMachineInstrFromMaps(MI);
1598 MI->eraseFromParent();
1599 ++numFolds;
1600 return true;
1601 }
1602
1603 // Filter the list of operand indexes that are to be folded. Abort if
1604 // any operand will prevent folding.
1605 unsigned MRInfo = 0;
1606 SmallVector<unsigned, 2> FoldOps;
1607 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1608 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001609
Evan Cheng427f4c12008-03-31 23:19:51 +00001610 // The only time it's safe to fold into a two address instruction is when
1611 // it's folding reload and spill from / into a spill stack slot.
1612 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001613 return false;
1614
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001615 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1616 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001617 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001618 // Remember this instruction uses the spill slot.
1619 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1620
Evan Chengf2fbca62007-11-12 06:35:08 +00001621 // Attempt to fold the memory reference into the instruction. If
1622 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001623 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001624 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001625 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001626 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001627 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001628 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001629 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001630 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001631 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001632 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001633 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001634 return true;
1635 }
1636 return false;
1637}
1638
Evan Cheng018f9b02007-12-05 03:22:34 +00001639/// canFoldMemoryOperand - Returns true if the specified load / store
1640/// folding is possible.
1641bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001642 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001643 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001644 // Filter the list of operand indexes that are to be folded. Abort if
1645 // any operand will prevent folding.
1646 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001647 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001648 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1649 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001650
Evan Cheng3c75ba82008-04-01 21:37:32 +00001651 // It's only legal to remat for a use, not a def.
1652 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001653 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001654
Evan Chengd70dbb52008-02-22 09:24:50 +00001655 return tii_->canFoldMemoryOperand(MI, FoldOps);
1656}
1657
Evan Cheng81a03822007-11-17 00:40:40 +00001658bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1659 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1660 for (LiveInterval::Ranges::const_iterator
1661 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1662 std::vector<IdxMBBPair>::const_iterator II =
1663 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1664 if (II == Idx2MBBMap.end())
1665 continue;
1666 if (I->end > II->first) // crossing a MBB.
1667 return false;
1668 MBBs.insert(II->second);
1669 if (MBBs.size() > 1)
1670 return false;
1671 }
1672 return true;
1673}
1674
Evan Chengd70dbb52008-02-22 09:24:50 +00001675/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1676/// interval on to-be re-materialized operands of MI) with new register.
1677void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1678 MachineInstr *MI, unsigned NewVReg,
1679 VirtRegMap &vrm) {
1680 // There is an implicit use. That means one of the other operand is
1681 // being remat'ed and the remat'ed instruction has li.reg as an
1682 // use operand. Make sure we rewrite that as well.
1683 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1684 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001685 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001686 continue;
1687 unsigned Reg = MO.getReg();
1688 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1689 continue;
1690 if (!vrm.isReMaterialized(Reg))
1691 continue;
1692 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001693 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1694 if (UseMO)
1695 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001696 }
1697}
1698
Evan Chengf2fbca62007-11-12 06:35:08 +00001699/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1700/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001701bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001702rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames6cc91e32009-10-03 04:31:31 +00001703 bool TrySplit, LiveIndex index, LiveIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001704 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001705 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001706 unsigned Slot, int LdSlot,
1707 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001708 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001709 const TargetRegisterClass* rc,
1710 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001711 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001712 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001713 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001714 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001715 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001716 RestartInstruction:
1717 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1718 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001719 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001720 continue;
1721 unsigned Reg = mop.getReg();
1722 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001723 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001724 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001725 if (Reg != li.reg)
1726 continue;
1727
1728 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001729 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001730 int FoldSlot = Slot;
1731 if (DefIsReMat) {
1732 // If this is the rematerializable definition MI itself and
1733 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001734 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001735 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1736 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001737 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001738 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001739 MI->eraseFromParent();
1740 break;
1741 }
1742
1743 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001744 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001745 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001746 if (isLoad) {
1747 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1748 FoldSS = isLoadSS;
1749 FoldSlot = LdSlot;
1750 }
1751 }
1752
Evan Chengf2fbca62007-11-12 06:35:08 +00001753 // Scan all of the operands of this instruction rewriting operands
1754 // to use NewVReg instead of li.reg as appropriate. We do this for
1755 // two reasons:
1756 //
1757 // 1. If the instr reads the same spilled vreg multiple times, we
1758 // want to reuse the NewVReg.
1759 // 2. If the instr is a two-addr instruction, we are required to
1760 // keep the src/dst regs pinned.
1761 //
1762 // Keep track of whether we replace a use and/or def so that we can
1763 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001764
Evan Cheng81a03822007-11-17 00:40:40 +00001765 HasUse = mop.isUse();
1766 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001767 SmallVector<unsigned, 2> Ops;
1768 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001769 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001770 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001771 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001772 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001773 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001774 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001775 continue;
1776 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001777 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001778 if (!MOj.isUndef()) {
1779 HasUse |= MOj.isUse();
1780 HasDef |= MOj.isDef();
1781 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001782 }
1783 }
1784
David Greene26b86a02008-10-27 17:38:59 +00001785 // Create a new virtual register for the spill interval.
1786 // Create the new register now so we can map the fold instruction
1787 // to the new register so when it is unfolded we get the correct
1788 // answer.
1789 bool CreatedNewVReg = false;
1790 if (NewVReg == 0) {
1791 NewVReg = mri_->createVirtualRegister(rc);
1792 vrm.grow();
1793 CreatedNewVReg = true;
1794 }
1795
Evan Cheng9c3c2212008-06-06 07:54:39 +00001796 if (!TryFold)
1797 CanFold = false;
1798 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001799 // Do not fold load / store here if we are splitting. We'll find an
1800 // optimal point to insert a load / store later.
1801 if (!TrySplit) {
1802 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001803 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001804 // Folding the load/store can completely change the instruction in
1805 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001806
1807 if (FoldSS) {
1808 // We need to give the new vreg the same stack slot as the
1809 // spilled interval.
1810 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1811 }
1812
Evan Cheng018f9b02007-12-05 03:22:34 +00001813 HasUse = false;
1814 HasDef = false;
1815 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001816 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001817 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001818 goto RestartInstruction;
1819 }
1820 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001821 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001822 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001823 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001824 }
Evan Chengcddbb832007-11-30 21:23:43 +00001825
Evan Chengcddbb832007-11-30 21:23:43 +00001826 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001827 if (mop.isImplicit())
1828 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001829
1830 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001831 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1832 MachineOperand &mopj = MI->getOperand(Ops[j]);
1833 mopj.setReg(NewVReg);
1834 if (mopj.isImplicit())
1835 rewriteImplicitOps(li, MI, NewVReg, vrm);
1836 }
Evan Chengcddbb832007-11-30 21:23:43 +00001837
Evan Cheng81a03822007-11-17 00:40:40 +00001838 if (CreatedNewVReg) {
1839 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001840 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001841 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001842 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001843 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001844 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001845 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001846 }
1847 if (!CanDelete || (HasUse && HasDef)) {
1848 // If this is a two-addr instruction then its use operands are
1849 // rematerializable but its def is not. It should be assigned a
1850 // stack slot.
1851 vrm.assignVirt2StackSlot(NewVReg, Slot);
1852 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001853 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001854 vrm.assignVirt2StackSlot(NewVReg, Slot);
1855 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001856 } else if (HasUse && HasDef &&
1857 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1858 // If this interval hasn't been assigned a stack slot (because earlier
1859 // def is a deleted remat def), do it now.
1860 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1861 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001862 }
1863
Evan Cheng313d4b82008-02-23 00:33:04 +00001864 // Re-matting an instruction with virtual register use. Add the
1865 // register as an implicit use on the use MI.
1866 if (DefIsReMat && ImpUse)
1867 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1868
Evan Cheng5b69eba2009-04-21 22:46:52 +00001869 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001870 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001871 if (CreatedNewVReg) {
1872 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001873 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001874 if (TrySplit)
1875 vrm.setIsSplitFromReg(NewVReg, li.reg);
1876 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001877
1878 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001879 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001880 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hamescc3b0652009-10-03 04:21:37 +00001881 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001882 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001883 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001884 nI.addRange(LR);
1885 } else {
1886 // Extend the split live interval to this def / use.
Lang Hamescc3b0652009-10-03 04:21:37 +00001887 LiveIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001888 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1889 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001890 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001891 nI.addRange(LR);
1892 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001893 }
1894 if (HasDef) {
1895 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00001896 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001897 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001898 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001899 nI.addRange(LR);
1900 }
Evan Cheng81a03822007-11-17 00:40:40 +00001901
Bill Wendling8e6179f2009-08-22 20:18:03 +00001902 DEBUG({
1903 errs() << "\t\t\t\tAdded new interval: ";
1904 nI.print(errs(), tri_);
1905 errs() << '\n';
1906 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001907 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001908 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001909}
Evan Cheng81a03822007-11-17 00:40:40 +00001910bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001911 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001912 MachineBasicBlock *MBB,
Lang Hamescc3b0652009-10-03 04:21:37 +00001913 LiveIndex Idx) const {
1914 LiveIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001915 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001916 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001917 continue;
1918
Lang Hamescc3b0652009-10-03 04:21:37 +00001919 LiveIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001920 if (KillIdx > Idx && KillIdx < End)
1921 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001922 }
1923 return false;
1924}
1925
Evan Cheng063284c2008-02-21 00:34:19 +00001926/// RewriteInfo - Keep track of machine instrs that will be rewritten
1927/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001928namespace {
1929 struct RewriteInfo {
Lang Hamescc3b0652009-10-03 04:21:37 +00001930 LiveIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001931 MachineInstr *MI;
1932 bool HasUse;
1933 bool HasDef;
Lang Hamescc3b0652009-10-03 04:21:37 +00001934 RewriteInfo(LiveIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001935 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1936 };
Evan Cheng063284c2008-02-21 00:34:19 +00001937
Dan Gohman844731a2008-05-13 00:00:25 +00001938 struct RewriteInfoCompare {
1939 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1940 return LHS.Index < RHS.Index;
1941 }
1942 };
1943}
Evan Cheng063284c2008-02-21 00:34:19 +00001944
Evan Chengf2fbca62007-11-12 06:35:08 +00001945void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001946rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001947 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001948 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001949 unsigned Slot, int LdSlot,
1950 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001951 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001952 const TargetRegisterClass* rc,
1953 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001954 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001955 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001956 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001957 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001958 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1959 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001960 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001961 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001962 unsigned NewVReg = 0;
Lang Hamescc3b0652009-10-03 04:21:37 +00001963 LiveIndex start = getBaseIndex(I->start);
1964 LiveIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001965
Evan Cheng063284c2008-02-21 00:34:19 +00001966 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001967 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001968 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001969 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1970 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001971 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001972 MachineOperand &O = ri.getOperand();
1973 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001974 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hamescc3b0652009-10-03 04:21:37 +00001975 LiveIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001976 if (index < start || index >= end)
1977 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001978
1979 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001980 // Must be defined by an implicit def. It should not be spilled. Note,
1981 // this is for correctness reason. e.g.
1982 // 8 %reg1024<def> = IMPLICIT_DEF
1983 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1984 // The live range [12, 14) are not part of the r1024 live interval since
1985 // it's defined by an implicit def. It will not conflicts with live
1986 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001987 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001988 // the INSERT_SUBREG and both target registers that would overlap.
1989 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001990 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1991 }
1992 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1993
Evan Cheng313d4b82008-02-23 00:33:04 +00001994 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001995 // Now rewrite the defs and uses.
1996 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1997 RewriteInfo &rwi = RewriteMIs[i];
1998 ++i;
Lang Hamescc3b0652009-10-03 04:21:37 +00001999 LiveIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00002000 bool MIHasUse = rwi.HasUse;
2001 bool MIHasDef = rwi.HasDef;
2002 MachineInstr *MI = rwi.MI;
2003 // If MI def and/or use the same register multiple times, then there
2004 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00002005 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002006 while (i != e && RewriteMIs[i].MI == MI) {
2007 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00002008 bool isUse = RewriteMIs[i].HasUse;
2009 if (isUse) ++NumUses;
2010 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002011 MIHasDef |= RewriteMIs[i].HasDef;
2012 ++i;
2013 }
Evan Cheng81a03822007-11-17 00:40:40 +00002014 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00002015
Evan Cheng0a891ed2008-05-23 23:00:04 +00002016 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00002017 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002018 // register interval's spill weight to HUGE_VALF to prevent it from
2019 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00002020 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002021 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00002022 }
2023
Evan Cheng063284c2008-02-21 00:34:19 +00002024 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00002025 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00002026 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00002027 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002028 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00002029 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002030 // One common case:
2031 // x = use
2032 // ...
2033 // ...
2034 // def = ...
2035 // = use
2036 // It's better to start a new interval to avoid artifically
2037 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002038 if (MIHasDef && !MIHasUse) {
2039 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00002040 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002041 }
2042 }
Evan Chengcada2452007-11-28 01:28:46 +00002043 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002044
2045 bool IsNew = ThisVReg == 0;
2046 if (IsNew) {
2047 // This ends the previous live interval. If all of its def / use
2048 // can be folded, give it a low spill weight.
2049 if (NewVReg && TrySplit && AllCanFold) {
2050 LiveInterval &nI = getOrCreateInterval(NewVReg);
2051 nI.weight /= 10.0F;
2052 }
2053 AllCanFold = true;
2054 }
2055 NewVReg = ThisVReg;
2056
Evan Cheng81a03822007-11-17 00:40:40 +00002057 bool HasDef = false;
2058 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00002059 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00002060 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
2061 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
2062 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00002063 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002064 if (!HasDef && !HasUse)
2065 continue;
2066
Evan Cheng018f9b02007-12-05 03:22:34 +00002067 AllCanFold &= CanFold;
2068
Evan Cheng81a03822007-11-17 00:40:40 +00002069 // Update weight of spill interval.
2070 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00002071 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00002072 // The spill weight is now infinity as it cannot be spilled again.
2073 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002074 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00002075 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002076
2077 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00002078 if (HasDef) {
2079 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002080 bool HasKill = false;
2081 if (!HasUse)
2082 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
2083 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002084 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00002085 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002086 if (VNI)
2087 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2088 }
Owen Anderson28998312008-08-13 22:28:50 +00002089 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002090 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002091 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002092 if (SII == SpillIdxes.end()) {
2093 std::vector<SRInfo> S;
2094 S.push_back(SRInfo(index, NewVReg, true));
2095 SpillIdxes.insert(std::make_pair(MBBId, S));
2096 } else if (SII->second.back().vreg != NewVReg) {
2097 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002098 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002099 // If there is an earlier def and this is a two-address
2100 // instruction, then it's not possible to fold the store (which
2101 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002102 SRInfo &Info = SII->second.back();
2103 Info.index = index;
2104 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002105 }
2106 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002107 } else if (SII != SpillIdxes.end() &&
2108 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002109 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002110 // There is an earlier def that's not killed (must be two-address).
2111 // The spill is no longer needed.
2112 SII->second.pop_back();
2113 if (SII->second.empty()) {
2114 SpillIdxes.erase(MBBId);
2115 SpillMBBs.reset(MBBId);
2116 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002117 }
2118 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002119 }
2120
2121 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002122 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002123 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002124 if (SII != SpillIdxes.end() &&
2125 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002126 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002127 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002128 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002129 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002130 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002131 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002132 // If we are splitting live intervals, only fold if it's the first
2133 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002134 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002135 else if (IsNew) {
2136 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002137 if (RII == RestoreIdxes.end()) {
2138 std::vector<SRInfo> Infos;
2139 Infos.push_back(SRInfo(index, NewVReg, true));
2140 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2141 } else {
2142 RII->second.push_back(SRInfo(index, NewVReg, true));
2143 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002144 RestoreMBBs.set(MBBId);
2145 }
2146 }
2147
2148 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002149 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002150 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002151 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002152
2153 if (NewVReg && TrySplit && AllCanFold) {
2154 // If all of its def / use can be folded, give it a low spill weight.
2155 LiveInterval &nI = getOrCreateInterval(NewVReg);
2156 nI.weight /= 10.0F;
2157 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002158}
2159
Lang Hamescc3b0652009-10-03 04:21:37 +00002160bool LiveIntervals::alsoFoldARestore(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002161 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002162 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002163 if (!RestoreMBBs[Id])
2164 return false;
2165 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2166 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2167 if (Restores[i].index == index &&
2168 Restores[i].vreg == vr &&
2169 Restores[i].canFold)
2170 return true;
2171 return false;
2172}
2173
Lang Hamescc3b0652009-10-03 04:21:37 +00002174void LiveIntervals::eraseRestoreInfo(int Id, LiveIndex index,
Lang Hames86511252009-09-04 20:41:11 +00002175 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002176 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002177 if (!RestoreMBBs[Id])
2178 return;
2179 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2180 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2181 if (Restores[i].index == index && Restores[i].vreg)
Lang Hamescc3b0652009-10-03 04:21:37 +00002182 Restores[i].index = LiveIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002183}
Evan Cheng81a03822007-11-17 00:40:40 +00002184
Evan Cheng4cce6b42008-04-11 17:53:36 +00002185/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2186/// spilled and create empty intervals for their uses.
2187void
2188LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2189 const TargetRegisterClass* rc,
2190 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002191 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2192 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002193 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002194 MachineInstr *MI = &*ri;
2195 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002196 if (O.isDef()) {
2197 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2198 "Register def was not rewritten?");
2199 RemoveMachineInstrFromMaps(MI);
2200 vrm.RemoveMachineInstrFromMaps(MI);
2201 MI->eraseFromParent();
2202 } else {
2203 // This must be an use of an implicit_def so it's not part of the live
2204 // interval. Create a new empty live interval for it.
2205 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2206 unsigned NewVReg = mri_->createVirtualRegister(rc);
2207 vrm.grow();
2208 vrm.setIsImplicitlyDefined(NewVReg);
2209 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2210 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2211 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002212 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002213 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002214 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002215 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002216 }
2217 }
Evan Cheng419852c2008-04-03 16:39:43 +00002218 }
2219}
2220
Evan Chengf2fbca62007-11-12 06:35:08 +00002221std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002222addIntervalsForSpillsFast(const LiveInterval &li,
2223 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002224 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002225 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002226
2227 std::vector<LiveInterval*> added;
2228
2229 assert(li.weight != HUGE_VALF &&
2230 "attempt to spill already spilled interval!");
2231
Bill Wendling8e6179f2009-08-22 20:18:03 +00002232 DEBUG({
2233 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2234 li.dump();
2235 errs() << '\n';
2236 });
Owen Andersond6664312008-08-18 18:05:32 +00002237
2238 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2239
Owen Andersona41e47a2008-08-19 22:12:11 +00002240 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2241 while (RI != mri_->reg_end()) {
2242 MachineInstr* MI = &*RI;
2243
2244 SmallVector<unsigned, 2> Indices;
2245 bool HasUse = false;
2246 bool HasDef = false;
2247
2248 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2249 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002250 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002251
2252 HasUse |= MI->getOperand(i).isUse();
2253 HasDef |= MI->getOperand(i).isDef();
2254
2255 Indices.push_back(i);
2256 }
2257
2258 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2259 Indices, true, slot, li.reg)) {
2260 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002261 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002262 vrm.assignVirt2StackSlot(NewVReg, slot);
2263
Owen Andersona41e47a2008-08-19 22:12:11 +00002264 // create a new register for this spill
2265 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002266
Owen Andersona41e47a2008-08-19 22:12:11 +00002267 // the spill weight is now infinity as it
2268 // cannot be spilled again
2269 nI.weight = HUGE_VALF;
2270
2271 // Rewrite register operands to use the new vreg.
2272 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2273 E = Indices.end(); I != E; ++I) {
2274 MI->getOperand(*I).setReg(NewVReg);
2275
2276 if (MI->getOperand(*I).isUse())
2277 MI->getOperand(*I).setIsKill(true);
2278 }
2279
2280 // Fill in the new live interval.
Lang Hamescc3b0652009-10-03 04:21:37 +00002281 LiveIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002282 if (HasUse) {
2283 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002284 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002285 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002286 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002287 nI.addRange(LR);
2288 vrm.addRestorePoint(NewVReg, MI);
2289 }
2290 if (HasDef) {
2291 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hamescc3b0652009-10-03 04:21:37 +00002292 nI.getNextValue(LiveIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00002293 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002294 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002295 nI.addRange(LR);
2296 vrm.addSpillPoint(NewVReg, true, MI);
2297 }
2298
Owen Anderson17197312008-08-18 23:41:04 +00002299 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002300
Bill Wendling8e6179f2009-08-22 20:18:03 +00002301 DEBUG({
2302 errs() << "\t\t\t\tadded new interval: ";
2303 nI.dump();
2304 errs() << '\n';
2305 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002306 }
Owen Anderson9a032932008-08-18 21:20:32 +00002307
Owen Anderson9a032932008-08-18 21:20:32 +00002308
Owen Andersona41e47a2008-08-19 22:12:11 +00002309 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002310 }
Owen Andersond6664312008-08-18 18:05:32 +00002311
2312 return added;
2313}
2314
2315std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002316addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002317 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002318 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002319
2320 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002321 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002322
Evan Chengf2fbca62007-11-12 06:35:08 +00002323 assert(li.weight != HUGE_VALF &&
2324 "attempt to spill already spilled interval!");
2325
Bill Wendling8e6179f2009-08-22 20:18:03 +00002326 DEBUG({
2327 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2328 li.print(errs(), tri_);
2329 errs() << '\n';
2330 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002331
Evan Cheng72eeb942008-12-05 17:00:16 +00002332 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002333 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002334 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002335 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002336 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2337 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002338 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002339 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002340
2341 unsigned NumValNums = li.getNumValNums();
2342 SmallVector<MachineInstr*, 4> ReMatDefs;
2343 ReMatDefs.resize(NumValNums, NULL);
2344 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2345 ReMatOrigDefs.resize(NumValNums, NULL);
2346 SmallVector<int, 4> ReMatIds;
2347 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2348 BitVector ReMatDelete(NumValNums);
2349 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2350
Evan Cheng81a03822007-11-17 00:40:40 +00002351 // Spilling a split live interval. It cannot be split any further. Also,
2352 // it's also guaranteed to be a single val# / range interval.
2353 if (vrm.getPreSplitReg(li.reg)) {
2354 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002355 // Unset the split kill marker on the last use.
Lang Hamescc3b0652009-10-03 04:21:37 +00002356 LiveIndex KillIdx = vrm.getKillPoint(li.reg);
2357 if (KillIdx != LiveIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002358 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2359 assert(KillMI && "Last use disappeared?");
2360 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2361 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002362 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002363 }
Evan Chengadf85902007-12-05 09:51:10 +00002364 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002365 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2366 Slot = vrm.getStackSlot(li.reg);
2367 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2368 MachineInstr *ReMatDefMI = DefIsReMat ?
2369 vrm.getReMaterializedMI(li.reg) : NULL;
2370 int LdSlot = 0;
2371 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2372 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002373 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002374 bool IsFirstRange = true;
2375 for (LiveInterval::Ranges::const_iterator
2376 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2377 // If this is a split live interval with multiple ranges, it means there
2378 // are two-address instructions that re-defined the value. Only the
2379 // first def can be rematerialized!
2380 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002381 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002382 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2383 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002384 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002385 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002386 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002387 } else {
2388 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2389 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002390 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002391 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002392 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002393 }
2394 IsFirstRange = false;
2395 }
Evan Cheng419852c2008-04-03 16:39:43 +00002396
Evan Cheng4cce6b42008-04-11 17:53:36 +00002397 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002398 return NewLIs;
2399 }
2400
Evan Cheng752195e2009-09-14 21:33:42 +00002401 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002402 if (TrySplit)
2403 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002404 bool NeedStackSlot = false;
2405 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2406 i != e; ++i) {
2407 const VNInfo *VNI = *i;
2408 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002409 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002410 continue; // Dead val#.
2411 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002412 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2413 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002414 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002415 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002416 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002417 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002418 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002419 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002420 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002421 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002422
2423 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002424 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002425 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002426 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002427 CanDelete = false;
2428 // Need a stack slot if there is any live range where uses cannot be
2429 // rematerialized.
2430 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002431 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002432 if (CanDelete)
2433 ReMatDelete.set(VN);
2434 } else {
2435 // Need a stack slot if there is any live range where uses cannot be
2436 // rematerialized.
2437 NeedStackSlot = true;
2438 }
2439 }
2440
2441 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002442 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2443 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2444 Slot = vrm.assignVirt2StackSlot(li.reg);
2445
2446 // This case only occurs when the prealloc splitter has already assigned
2447 // a stack slot to this vreg.
2448 else
2449 Slot = vrm.getStackSlot(li.reg);
2450 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002451
2452 // Create new intervals and rewrite defs and uses.
2453 for (LiveInterval::Ranges::const_iterator
2454 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002455 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2456 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2457 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002458 bool CanDelete = ReMatDelete[I->valno->id];
2459 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002460 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002461 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002462 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002463 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002464 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002465 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002466 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002467 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002468 }
2469
Evan Cheng0cbb1162007-11-29 01:06:25 +00002470 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002471 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002472 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002473 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002474 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002475
Evan Chengb50bb8c2007-12-05 08:16:32 +00002476 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002477 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002478 if (NeedStackSlot) {
2479 int Id = SpillMBBs.find_first();
2480 while (Id != -1) {
2481 std::vector<SRInfo> &spills = SpillIdxes[Id];
2482 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002483 LiveIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002484 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002485 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002486 bool isReMat = vrm.isReMaterialized(VReg);
2487 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002488 bool CanFold = false;
2489 bool FoundUse = false;
2490 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002491 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002492 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002493 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2494 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002495 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002496 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002497
2498 Ops.push_back(j);
2499 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002500 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002501 if (isReMat ||
2502 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2503 RestoreMBBs, RestoreIdxes))) {
2504 // MI has two-address uses of the same register. If the use
2505 // isn't the first and only use in the BB, then we can't fold
2506 // it. FIXME: Move this to rewriteInstructionsForSpills.
2507 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002508 break;
2509 }
Evan Chengaee4af62007-12-02 08:30:39 +00002510 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002511 }
2512 }
2513 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002514 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002515 if (CanFold && !Ops.empty()) {
2516 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002517 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002518 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002519 // Also folded uses, do not issue a load.
2520 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002521 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002522 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002523 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002524 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002525 }
2526
Evan Cheng7e073ba2008-04-09 20:57:25 +00002527 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002528 if (!Folded) {
2529 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2530 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002531 if (!MI->registerDefIsDead(nI.reg))
2532 // No need to spill a dead def.
2533 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002534 if (isKill)
2535 AddedKill.insert(&nI);
2536 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002537 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002538 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002539 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002540 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002541
Evan Cheng1953d0c2007-11-29 10:12:14 +00002542 int Id = RestoreMBBs.find_first();
2543 while (Id != -1) {
2544 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2545 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hamescc3b0652009-10-03 04:21:37 +00002546 LiveIndex index = restores[i].index;
2547 if (index == LiveIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002548 continue;
2549 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002550 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002551 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002552 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002553 bool CanFold = false;
2554 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002555 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002556 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002557 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2558 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002559 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002560 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002561
Evan Cheng0cbb1162007-11-29 01:06:25 +00002562 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002563 // If this restore were to be folded, it would have been folded
2564 // already.
2565 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002566 break;
2567 }
Evan Chengaee4af62007-12-02 08:30:39 +00002568 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002569 }
2570 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002571
2572 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002573 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002574 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002575 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002576 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2577 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002578 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2579 int LdSlot = 0;
2580 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2581 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002582 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002583 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2584 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002585 if (!Folded) {
2586 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2587 if (ImpUse) {
2588 // Re-matting an instruction with virtual register use. Add the
2589 // register as an implicit use on the use MI and update the register
2590 // interval's spill weight to HUGE_VALF to prevent it from being
2591 // spilled.
2592 LiveInterval &ImpLi = getInterval(ImpUse);
2593 ImpLi.weight = HUGE_VALF;
2594 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2595 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002596 }
Evan Chengaee4af62007-12-02 08:30:39 +00002597 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002598 }
2599 // If folding is not possible / failed, then tell the spiller to issue a
2600 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002601 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002602 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002603 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002604 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002605 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002606 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002607 }
2608
Evan Chengb50bb8c2007-12-05 08:16:32 +00002609 // Finalize intervals: add kills, finalize spill weights, and filter out
2610 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002611 std::vector<LiveInterval*> RetNewLIs;
2612 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2613 LiveInterval *LI = NewLIs[i];
2614 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002615 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002616 if (!AddedKill.count(LI)) {
2617 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hamescc3b0652009-10-03 04:21:37 +00002618 LiveIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002619 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002620 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002621 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002622 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002623 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002624 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002625 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002626 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002627 RetNewLIs.push_back(LI);
2628 }
2629 }
Evan Cheng81a03822007-11-17 00:40:40 +00002630
Evan Cheng4cce6b42008-04-11 17:53:36 +00002631 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002632 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002633}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002634
2635/// hasAllocatableSuperReg - Return true if the specified physical register has
2636/// any super register that's allocatable.
2637bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2638 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2639 if (allocatableRegs_[*AS] && hasInterval(*AS))
2640 return true;
2641 return false;
2642}
2643
2644/// getRepresentativeReg - Find the largest super register of the specified
2645/// physical register.
2646unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2647 // Find the largest super-register that is allocatable.
2648 unsigned BestReg = Reg;
2649 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2650 unsigned SuperReg = *AS;
2651 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2652 BestReg = SuperReg;
2653 break;
2654 }
2655 }
2656 return BestReg;
2657}
2658
2659/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2660/// specified interval that conflicts with the specified physical register.
2661unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2662 unsigned PhysReg) const {
2663 unsigned NumConflicts = 0;
2664 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2665 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2666 E = mri_->reg_end(); I != E; ++I) {
2667 MachineOperand &O = I.getOperand();
2668 MachineInstr *MI = O.getParent();
Lang Hamescc3b0652009-10-03 04:21:37 +00002669 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002670 if (pli.liveAt(Index))
2671 ++NumConflicts;
2672 }
2673 return NumConflicts;
2674}
2675
2676/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002677/// around all defs and uses of the specified interval. Return true if it
2678/// was able to cut its interval.
2679bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002680 unsigned PhysReg, VirtRegMap &vrm) {
2681 unsigned SpillReg = getRepresentativeReg(PhysReg);
2682
2683 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2684 // If there are registers which alias PhysReg, but which are not a
2685 // sub-register of the chosen representative super register. Assert
2686 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002687 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002688 tri_->isSuperRegister(*AS, SpillReg));
2689
Evan Cheng2824a652009-03-23 18:24:37 +00002690 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002691 LiveInterval &pli = getInterval(SpillReg);
2692 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2693 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2694 E = mri_->reg_end(); I != E; ++I) {
2695 MachineOperand &O = I.getOperand();
2696 MachineInstr *MI = O.getParent();
2697 if (SeenMIs.count(MI))
2698 continue;
2699 SeenMIs.insert(MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002700 LiveIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002701 if (pli.liveAt(Index)) {
2702 vrm.addEmergencySpill(SpillReg, MI);
Lang Hamescc3b0652009-10-03 04:21:37 +00002703 LiveIndex StartIdx = getLoadIndex(Index);
2704 LiveIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002705 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002706 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002707 Cut = true;
2708 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002709 std::string msg;
2710 raw_string_ostream Msg(msg);
2711 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002712 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002713 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002714 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002715 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002716 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002717 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002718 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002719 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2720 if (!hasInterval(*AS))
2721 continue;
2722 LiveInterval &spli = getInterval(*AS);
2723 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002724 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002725 }
2726 }
2727 }
Evan Cheng2824a652009-03-23 18:24:37 +00002728 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002729}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002730
2731LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002732 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002733 LiveInterval& Interval = getOrCreateInterval(reg);
2734 VNInfo* VN = Interval.getNextValue(
Lang Hames6cc91e32009-10-03 04:31:31 +00002735 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames86511252009-09-04 20:41:11 +00002736 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002737 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002738 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2739 LiveRange LR(
Lang Hames6cc91e32009-10-03 04:31:31 +00002740 LiveIndex(getInstructionIndex(startInst), LiveIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002741 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002742 Interval.addRange(LR);
2743
2744 return LR;
2745}
David Greeneb5257662009-08-03 21:55:09 +00002746