blob: b3e6aa72920b778918357e794b76b8c7f7f0b279 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Dan Gohman844731a2008-05-13 00:00:25 +000052static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
53 cl::init(true), cl::Hidden);
54static cl::opt<int> SplitLimit("split-limit",
55 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000056
Dan Gohman4c8f8702008-07-25 15:08:37 +000057static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
58
Owen Andersonae339ba2008-08-19 00:17:30 +000059static cl::opt<bool> EnableFastSpilling("fast-spill",
60 cl::init(false), cl::Hidden);
61
Chris Lattnercd3245a2006-12-19 22:41:21 +000062STATISTIC(numIntervals, "Number of original intervals");
Evan Cheng0cbb1162007-11-29 01:06:25 +000063STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000065
Devang Patel19974732007-05-03 01:11:54 +000066char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000067static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Chris Lattnerf7da2c72006-08-24 22:43:55 +000069void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000070 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000071 AU.addRequired<AliasAnalysis>();
72 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000073 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000074 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000075 AU.addPreservedID(MachineLoopInfoID);
76 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000077
78 if (!StrongPHIElim) {
79 AU.addPreservedID(PHIEliminationID);
80 AU.addRequiredID(PHIEliminationID);
81 }
82
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085}
86
Chris Lattnerf7da2c72006-08-24 22:43:55 +000087void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000088 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000089 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000090 E = r2iMap_.end(); I != E; ++I)
91 delete I->second;
92
Evan Cheng3f32d652008-06-04 09:18:41 +000093 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000094 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 mi2iMap_.clear();
96 i2miMap_.clear();
97 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000098 terminatorGaps.clear();
99
Evan Chengdd199d22007-09-06 01:07:24 +0000100 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
101 VNInfoAllocator.Reset();
Evan Cheng1ed99222008-07-19 00:37:25 +0000102 while (!ClonedMIs.empty()) {
103 MachineInstr *MI = ClonedMIs.back();
104 ClonedMIs.pop_back();
105 mf_->DeleteMachineInstr(MI);
106 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000107}
108
Evan Cheng6ade93b2009-08-05 03:53:14 +0000109static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
110 const TargetInstrInfo *tii_) {
111 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
112 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
113 Reg == SrcReg)
114 return true;
115
116 if ((MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
117 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) &&
118 MI->getOperand(2).getReg() == Reg)
119 return true;
120 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG &&
121 MI->getOperand(1).getReg() == Reg)
122 return true;
123 return false;
124}
125
Evan Cheng2578ba22009-07-01 01:59:31 +0000126/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
127/// there is one implicit_def for each use. Add isUndef marker to
128/// implicit_def defs and their uses.
129void LiveIntervals::processImplicitDefs() {
130 SmallSet<unsigned, 8> ImpDefRegs;
131 SmallVector<MachineInstr*, 8> ImpDefMIs;
132 MachineBasicBlock *Entry = mf_->begin();
133 SmallPtrSet<MachineBasicBlock*,16> Visited;
134 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
135 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
136 DFI != E; ++DFI) {
137 MachineBasicBlock *MBB = *DFI;
138 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
139 I != E; ) {
140 MachineInstr *MI = &*I;
141 ++I;
142 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
143 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000144 ImpDefRegs.insert(Reg);
145 ImpDefMIs.push_back(MI);
146 continue;
147 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000148
149 bool ChangedToImpDef = false;
150 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000151 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000152 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000153 continue;
154 unsigned Reg = MO.getReg();
155 if (!Reg)
156 continue;
157 if (!ImpDefRegs.count(Reg))
158 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000159 // Use is a copy, just turn it into an implicit_def.
Evan Cheng6ade93b2009-08-05 03:53:14 +0000160 if (CanTurnIntoImplicitDef(MI, Reg, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000161 bool isKill = MO.isKill();
162 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
163 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
164 MI->RemoveOperand(j);
165 if (isKill)
166 ImpDefRegs.erase(Reg);
167 ChangedToImpDef = true;
168 break;
169 }
170
Evan Cheng2578ba22009-07-01 01:59:31 +0000171 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000172 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
173 // Make sure other uses of
174 for (unsigned j = i+1; j != e; ++j) {
175 MachineOperand &MOJ = MI->getOperand(j);
176 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
177 MOJ.setIsUndef();
178 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000179 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000180 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000181 }
182
Evan Cheng459a7c62009-07-01 08:19:36 +0000183 if (ChangedToImpDef) {
184 // Backtrack to process this new implicit_def.
185 --I;
186 } else {
187 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
188 MachineOperand& MO = MI->getOperand(i);
189 if (!MO.isReg() || !MO.isDef())
190 continue;
191 ImpDefRegs.erase(MO.getReg());
192 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000193 }
194 }
195
196 // Any outstanding liveout implicit_def's?
197 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
198 MachineInstr *MI = ImpDefMIs[i];
199 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000200 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
201 !ImpDefRegs.count(Reg)) {
202 // Delete all "local" implicit_def's. That include those which define
203 // physical registers since they cannot be liveout.
204 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000205 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000206 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000207
208 // If there are multiple defs of the same register and at least one
209 // is not an implicit_def, do not insert implicit_def's before the
210 // uses.
211 bool Skip = false;
212 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
213 DE = mri_->def_end(); DI != DE; ++DI) {
214 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
215 Skip = true;
216 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000217 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000218 }
219 if (Skip)
220 continue;
221
Evan Chengd129d732009-07-17 19:43:40 +0000222 // The only implicit_def which we want to keep are those that are live
223 // out of its block.
224 MI->eraseFromParent();
225
Evan Cheng459a7c62009-07-01 08:19:36 +0000226 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
227 UE = mri_->use_end(); UI != UE; ) {
228 MachineOperand &RMO = UI.getOperand();
229 MachineInstr *RMI = &*UI;
230 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000231 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000232 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000233 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000234
235 // Turn a copy use into an implicit_def.
236 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
237 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
238 Reg == SrcReg) {
239 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
240 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
241 RMI->RemoveOperand(j);
242 continue;
243 }
244
Evan Cheng2578ba22009-07-01 01:59:31 +0000245 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
246 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000247 RMO.setReg(NewVReg);
248 RMO.setIsUndef();
249 RMO.setIsKill();
250 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000251 }
252 ImpDefRegs.clear();
253 ImpDefMIs.clear();
254 }
255}
256
Lang Hames86511252009-09-04 20:41:11 +0000257
Owen Anderson80b3ce62008-05-28 20:54:50 +0000258void LiveIntervals::computeNumbering() {
259 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000260 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000261
262 Idx2MBBMap.clear();
263 MBB2IdxMap.clear();
264 mi2iMap_.clear();
265 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000266 terminatorGaps.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000267
Owen Andersona1566f22008-07-22 22:46:49 +0000268 FunctionSize = 0;
269
Chris Lattner428b92e2006-09-15 03:57:23 +0000270 // Number MachineInstrs and MachineBasicBlocks.
271 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000272 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
273 std::make_pair(MachineInstrIndex(),MachineInstrIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000274
Lang Hames86511252009-09-04 20:41:11 +0000275 MachineInstrIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000276 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
277 MBB != E; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000278 MachineInstrIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000279
Owen Anderson7fbad272008-07-23 21:37:49 +0000280 // Insert an empty slot at the beginning of each block.
Lang Hames86511252009-09-04 20:41:11 +0000281 MIIndex = MIIndex.nextIndex();
Owen Anderson7fbad272008-07-23 21:37:49 +0000282 i2miMap_.push_back(0);
283
Chris Lattner428b92e2006-09-15 03:57:23 +0000284 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
285 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000286
287 if (I == MBB->getFirstTerminator()) {
288 // Leave a gap for before terminators, this is where we will point
289 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000290 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000291 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000292 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000293 assert(inserted &&
294 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000295 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000296 i2miMap_.push_back(0);
297
Lang Hames86511252009-09-04 20:41:11 +0000298 MIIndex = MIIndex.nextIndex();
Lang Hamesffd13262009-07-09 03:57:02 +0000299 }
300
Chris Lattner428b92e2006-09-15 03:57:23 +0000301 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000303 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000304 i2miMap_.push_back(I);
Lang Hames86511252009-09-04 20:41:11 +0000305 MIIndex = MIIndex.nextIndex();
Owen Andersona1566f22008-07-22 22:46:49 +0000306 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000307
Evan Cheng4ed43292008-10-18 05:21:37 +0000308 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000309 unsigned Slots = I->getDesc().getNumDefs();
310 if (Slots == 0)
311 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000312 while (Slots--) {
313 MIIndex = MIIndex.nextIndex();
Evan Cheng99fe34b2008-10-18 05:18:55 +0000314 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000315 }
316
Owen Anderson35578012008-06-16 07:10:49 +0000317 }
Lang Hamesffd13262009-07-09 03:57:02 +0000318
319 if (MBB->getFirstTerminator() == MBB->end()) {
320 // Leave a gap for before terminators, this is where we will point
321 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000322 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000323 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000324 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000325 assert(inserted &&
326 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000327 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000328 i2miMap_.push_back(0);
329
Lang Hames86511252009-09-04 20:41:11 +0000330 MIIndex = MIIndex.nextIndex();
Lang Hamesffd13262009-07-09 03:57:02 +0000331 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000332
Owen Anderson1fbb4542008-06-16 16:58:24 +0000333 // Set the MBB2IdxMap entry for this MBB.
Lang Hames86511252009-09-04 20:41:11 +0000334 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex.prevSlot());
Owen Anderson1fbb4542008-06-16 16:58:24 +0000335 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000336 }
Lang Hamesffd13262009-07-09 03:57:02 +0000337
Evan Cheng4ca980e2007-10-17 02:10:22 +0000338 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000339
340 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000341 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000342 for (LiveInterval::iterator LI = OI->second->begin(),
343 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000344
Owen Anderson7eec0c22008-05-29 23:01:22 +0000345 // Remap the start index of the live range to the corresponding new
346 // number, or our best guess at what it _should_ correspond to if the
347 // original instruction has been erased. This is either the following
348 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000349 unsigned index = LI->start.getVecIndex();
350 MachineInstrIndex::Slot offset = LI->start.getSlot();
351 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000352 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000353 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000354 // Take the pair containing the index
355 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000356 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000357
Owen Anderson7fbad272008-07-23 21:37:49 +0000358 LI->start = getMBBStartIdx(J->second);
359 } else {
Lang Hames86511252009-09-04 20:41:11 +0000360 LI->start = MachineInstrIndex(
361 MachineInstrIndex(mi2iMap_[OldI2MI[index]]),
362 (MachineInstrIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000363 }
364
365 // Remap the ending index in the same way that we remapped the start,
366 // except for the final step where we always map to the immediately
367 // following instruction.
Lang Hames86511252009-09-04 20:41:11 +0000368 index = (LI->end.prevSlot()).getVecIndex();
369 offset = LI->end.getSlot();
370 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000371 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000372 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000373 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000374 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000375
Lang Hames86511252009-09-04 20:41:11 +0000376 LI->end = getMBBEndIdx(I->second).nextSlot();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000377 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000378 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000379 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
380
381 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000382 LI->end =
383 MachineInstrIndex(mi2iMap_[OldI2MI[index]],
384 (idx == index ? offset : MachineInstrIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000385 else
Lang Hames86511252009-09-04 20:41:11 +0000386 LI->end =
387 MachineInstrIndex(MachineInstrIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000388 }
Owen Anderson788d0412008-08-06 18:35:45 +0000389 }
390
Owen Anderson03857b22008-08-13 21:49:13 +0000391 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
392 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000393 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000394
Owen Anderson7eec0c22008-05-29 23:01:22 +0000395 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000396 // start indices above. VN's with special sentinel defs
397 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000398 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000399 unsigned index = vni->def.getVecIndex();
400 MachineInstrIndex::Slot offset = vni->def.getSlot();
401 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000402 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000403 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000404 // Take the pair containing the index
405 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000406 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000407
Owen Anderson91292392008-07-30 17:42:47 +0000408 vni->def = getMBBStartIdx(J->second);
409 } else {
Lang Hames86511252009-09-04 20:41:11 +0000410 vni->def = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000411 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000412 }
Owen Anderson745825f42008-05-28 22:40:08 +0000413
Owen Anderson7eec0c22008-05-29 23:01:22 +0000414 // Remap the VNInfo kill indices, which works the same as
415 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000416 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames86511252009-09-04 20:41:11 +0000417 unsigned index = vni->kills[i].prevSlot().getVecIndex();
418 MachineInstrIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000419
Lang Hames86511252009-09-04 20:41:11 +0000420 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000421 assert("Value killed at a load slot.");
422 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000423 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000424 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000425
Lang Hamesffd13262009-07-09 03:57:02 +0000426 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000427 } else {
Lang Hames86511252009-09-04 20:41:11 +0000428 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000429 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000430 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000431 --I;
Lang Hames86511252009-09-04 20:41:11 +0000432 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000433 } else {
434 assert(OldI2MI[index] != 0 &&
435 "Kill refers to instruction not present in index maps.");
Lang Hames86511252009-09-04 20:41:11 +0000436 vni->kills[i] = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000437 }
438
439 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000440 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000441 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
442
443 if (index != OldI2MI.size())
444 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
445 (idx == index ? offset : 0);
446 else
447 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000448 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000449 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000450 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000451 }
Owen Anderson788d0412008-08-06 18:35:45 +0000452 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000453}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000454
Lang Hamesf41538d2009-06-02 16:53:25 +0000455void LiveIntervals::scaleNumbering(int factor) {
456 // Need to
457 // * scale MBB begin and end points
458 // * scale all ranges.
459 // * Update VNI structures.
460 // * Scale instruction numberings
461
462 // Scale the MBB indices.
463 Idx2MBBMap.clear();
464 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
465 MBB != MBBE; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000466 std::pair<MachineInstrIndex, MachineInstrIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
467 mbbIndices.first = mbbIndices.first.scale(factor);
468 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000469 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
470 }
471 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
472
Lang Hamesffd13262009-07-09 03:57:02 +0000473 // Scale terminator gaps.
Lang Hames86511252009-09-04 20:41:11 +0000474 for (DenseMap<MachineBasicBlock*, MachineInstrIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000475 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
476 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000477 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000478 }
479
Lang Hamesf41538d2009-06-02 16:53:25 +0000480 // Scale the intervals.
481 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
482 LI->second->scaleNumbering(factor);
483 }
484
485 // Scale MachineInstrs.
486 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hames86511252009-09-04 20:41:11 +0000487 MachineInstrIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000488 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
489 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000490 MachineInstrIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000491 mi2iMap_[MI->first] = newSlot;
492 highestSlot = std::max(highestSlot, newSlot);
493 }
494
Lang Hames86511252009-09-04 20:41:11 +0000495 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000496 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000497 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000498 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
499 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000500 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000501 }
502
503}
504
505
Owen Anderson80b3ce62008-05-28 20:54:50 +0000506/// runOnMachineFunction - Register allocate the whole function
507///
508bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
509 mf_ = &fn;
510 mri_ = &mf_->getRegInfo();
511 tm_ = &fn.getTarget();
512 tri_ = tm_->getRegisterInfo();
513 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000514 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000515 lv_ = &getAnalysis<LiveVariables>();
516 allocatableRegs_ = tri_->getAllocatableSet(fn);
517
Evan Cheng2578ba22009-07-01 01:59:31 +0000518 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000519 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000520 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000521
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000522 numIntervals += getNumIntervals();
523
Chris Lattner70ca3582004-09-30 15:59:17 +0000524 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000525 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000526}
527
Chris Lattner70ca3582004-09-30 15:59:17 +0000528/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000529void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000530 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000531 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000532 I->second->print(OS, tri_);
533 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000534 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000535
Chris Lattner705e07f2009-08-23 03:41:05 +0000536 OS << "********** MACHINEINSTRS **********\n";
537
Chris Lattner3380d5c2009-07-21 21:12:58 +0000538 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
539 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000540 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000541 for (MachineBasicBlock::iterator mii = mbbi->begin(),
542 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000543 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000544 }
545 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000546}
547
Evan Chengc92da382007-11-03 07:20:12 +0000548/// conflictsWithPhysRegDef - Returns true if the specified register
549/// is defined during the duration of the specified interval.
550bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
551 VirtRegMap &vrm, unsigned reg) {
552 for (LiveInterval::Ranges::const_iterator
553 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000554 for (MachineInstrIndex index = getBaseIndex(I->start),
555 end = getBaseIndex(I->end.prevSlot()).nextIndex(); index != end;
556 index = index.nextIndex()) {
Evan Chengc92da382007-11-03 07:20:12 +0000557 // skip deleted instructions
558 while (index != end && !getInstructionFromIndex(index))
Lang Hames86511252009-09-04 20:41:11 +0000559 index = index.nextIndex();
Evan Chengc92da382007-11-03 07:20:12 +0000560 if (index == end) break;
561
562 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000563 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
564 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000565 if (SrcReg == li.reg || DstReg == li.reg)
566 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000567 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
568 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000569 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000570 continue;
571 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000572 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000573 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000574 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000575 if (!vrm.hasPhys(PhysReg))
576 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000577 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000578 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000579 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000580 return true;
581 }
582 }
583 }
584
585 return false;
586}
587
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000588/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
589/// it can check use as well.
590bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
591 unsigned Reg, bool CheckUse,
592 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
593 for (LiveInterval::Ranges::const_iterator
594 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000595 for (MachineInstrIndex index = getBaseIndex(I->start),
596 end = getBaseIndex(I->end.prevSlot()).nextIndex(); index != end;
597 index = index.nextIndex()) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000598 // Skip deleted instructions.
599 MachineInstr *MI = 0;
600 while (index != end) {
601 MI = getInstructionFromIndex(index);
602 if (MI)
603 break;
Lang Hames86511252009-09-04 20:41:11 +0000604 index = index.nextIndex();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000605 }
606 if (index == end) break;
607
608 if (JoinedCopies.count(MI))
609 continue;
610 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
611 MachineOperand& MO = MI->getOperand(i);
612 if (!MO.isReg())
613 continue;
614 if (MO.isUse() && !CheckUse)
615 continue;
616 unsigned PhysReg = MO.getReg();
617 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
618 continue;
619 if (tri_->isSubRegister(Reg, PhysReg))
620 return true;
621 }
622 }
623 }
624
625 return false;
626}
627
628
Evan Cheng549f27d32007-08-13 23:45:17 +0000629void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000630 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000631 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000632 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000633 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000634}
635
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000636void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000637 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000638 MachineInstrIndex MIIdx,
639 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000640 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000641 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000642 DEBUG({
643 errs() << "\t\tregister: ";
644 printRegName(interval.reg);
645 });
Evan Cheng419852c2008-04-03 16:39:43 +0000646
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000647 // Virtual registers may be defined multiple times (due to phi
648 // elimination and 2-addr elimination). Much of what we do only has to be
649 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000651 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000652 if (interval.empty()) {
653 // Get the Idx of the defining instructions.
Lang Hames86511252009-09-04 20:41:11 +0000654 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000655 // Earlyclobbers move back one.
656 if (MO.isEarlyClobber())
657 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000658 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000659 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000660 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000661 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000662 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000663 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000664 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000665 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000666 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000667 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000668
669 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000670
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000671 // Loop over all of the blocks that the vreg is defined in. There are
672 // two cases we have to handle here. The most common case is a vreg
673 // whose lifetime is contained within a basic block. In this case there
674 // will be a single kill, in MBB, which comes after the definition.
675 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
676 // FIXME: what about dead vars?
Lang Hames86511252009-09-04 20:41:11 +0000677 MachineInstrIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000678 if (vi.Kills[0] != mi)
Lang Hames86511252009-09-04 20:41:11 +0000679 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0])).nextSlot();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 else
Lang Hames86511252009-09-04 20:41:11 +0000681 killIdx = defIndex.nextSlot();
Chris Lattner6097d132004-07-19 02:15:56 +0000682
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 // If the kill happens after the definition, we have an intra-block
684 // live range.
685 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000686 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000688 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000689 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000690 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000691 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000692 return;
693 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000694 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000695
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000696 // The other case we handle is when a virtual register lives to the end
697 // of the defining block, potentially live across some blocks, then is
698 // live into some number of blocks, but gets killed. Start by adding a
699 // range that goes from this definition to the end of the defining block.
Lang Hames86511252009-09-04 20:41:11 +0000700 LiveRange NewLR(defIndex, getMBBEndIdx(mbb).nextSlot(), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000701 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 interval.addRange(NewLR);
703
704 // Iterate over all of the blocks that the variable is completely
705 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
706 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000707 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
708 E = vi.AliveBlocks.end(); I != E; ++I) {
709 LiveRange LR(getMBBStartIdx(*I),
Lang Hames86511252009-09-04 20:41:11 +0000710 getMBBEndIdx(*I).nextSlot(), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000711 ValNo);
712 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000713 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000714 }
715
716 // Finally, this virtual register is live from the start of any killing
717 // block to the 'use' slot of the killing instruction.
718 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
719 MachineInstr *Kill = vi.Kills[i];
Lang Hames86511252009-09-04 20:41:11 +0000720 MachineInstrIndex killIdx = getUseIndex(getInstructionIndex(Kill)).nextSlot();
Chris Lattner428b92e2006-09-15 03:57:23 +0000721 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000722 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000723 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000724 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000725 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000726 }
727
728 } else {
729 // If this is the second time we see a virtual register definition, it
730 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000731 // the result of two address elimination, then the vreg is one of the
732 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000733 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734 // If this is a two-address definition, then we have already processed
735 // the live range. The only problem is that we didn't realize there
736 // are actually two values in the live interval. Because of this we
737 // need to take the LiveRegion that defines this register and split it
738 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000739 assert(interval.containsOneValue());
Lang Hames86511252009-09-04 20:41:11 +0000740 MachineInstrIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
741 MachineInstrIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000742 if (MO.isEarlyClobber())
743 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000744
Lang Hames86511252009-09-04 20:41:11 +0000745 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex.prevSlot());
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000746 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000747
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000748 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000749 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000750 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000751
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000752 // Two-address vregs should always only be redefined once. This means
753 // that at this point, there should be exactly one value number in it.
754 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
755
Chris Lattner91725b72006-08-31 05:54:43 +0000756 // The new value number (#1) is defined by the instruction we claimed
757 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000758 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000759 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000760 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000761 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
762
Chris Lattner91725b72006-08-31 05:54:43 +0000763 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000764 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000765 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000766 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000767 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000768
769 // Add the new live interval which replaces the range for the input copy.
770 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000771 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000772 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000773 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000774
775 // If this redefinition is dead, we need to add a dummy unit live
776 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000777 if (MO.isDead())
Lang Hames86511252009-09-04 20:41:11 +0000778 interval.addRange(LiveRange(RedefIndex, RedefIndex.nextSlot(), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000779
Bill Wendling8e6179f2009-08-22 20:18:03 +0000780 DEBUG({
781 errs() << " RESULT: ";
782 interval.print(errs(), tri_);
783 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000784 } else {
785 // Otherwise, this must be because of phi elimination. If this is the
786 // first redefinition of the vreg that we have seen, go back and change
787 // the live range in the PHI block to be a different value number.
788 if (interval.containsOneValue()) {
789 assert(vi.Kills.size() == 1 &&
790 "PHI elimination vreg should have one kill, the PHI itself!");
791
792 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000793 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000794 MachineInstr *Killer = vi.Kills[0];
Lang Hames86511252009-09-04 20:41:11 +0000795 MachineInstrIndex Start = getMBBStartIdx(Killer->getParent());
796 MachineInstrIndex End = getUseIndex(getInstructionIndex(Killer)).nextSlot();
Bill Wendling8e6179f2009-08-22 20:18:03 +0000797 DEBUG({
798 errs() << " Removing [" << Start << "," << End << "] from: ";
799 interval.print(errs(), tri_);
800 errs() << "\n";
801 });
Lang Hamesffd13262009-07-09 03:57:02 +0000802 interval.removeRange(Start, End);
803 assert(interval.ranges.size() == 1 &&
804 "newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000805 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
806 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000807 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000808 DEBUG({
809 errs() << " RESULT: ";
810 interval.print(errs(), tri_);
811 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000812
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000813 // Replace the interval with one of a NEW value number. Note that this
814 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000815 LiveRange LR(Start, End,
Lang Hames86511252009-09-04 20:41:11 +0000816 interval.getNextValue(MachineInstrIndex(mbb->getNumber()),
817 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000818 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000819 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000820 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000821 LR.valno->addKill(End);
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 }
827
828 // In the case of PHI elimination, each variable definition is only
829 // live until the end of the block. We've already taken care of the
830 // rest of the live range.
Lang Hames86511252009-09-04 20:41:11 +0000831 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000832 if (MO.isEarlyClobber())
833 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000834
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000835 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000836 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000837 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000838 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000839 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000840 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000841 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000842 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000843 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000844
Lang Hames86511252009-09-04 20:41:11 +0000845 MachineInstrIndex killIndex = getMBBEndIdx(mbb).nextSlot();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000846 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000847 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000848 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000849 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000850 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000851 }
852 }
853
Bill Wendling8e6179f2009-08-22 20:18:03 +0000854 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000855}
856
Chris Lattnerf35fef72004-07-23 21:24:19 +0000857void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000858 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000859 MachineInstrIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000860 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000861 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000862 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000863 // A physical register cannot be live across basic block, so its
864 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000865 DEBUG({
866 errs() << "\t\tregister: ";
867 printRegName(interval.reg);
868 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000869
Lang Hames86511252009-09-04 20:41:11 +0000870 MachineInstrIndex baseIndex = MIIdx;
871 MachineInstrIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000872 // Earlyclobbers move back one.
873 if (MO.isEarlyClobber())
874 start = getUseIndex(MIIdx);
Lang Hames86511252009-09-04 20:41:11 +0000875 MachineInstrIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000876
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000877 // If it is not used after definition, it is considered dead at
878 // the instruction defining it. Hence its interval is:
879 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000880 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000881 DEBUG(errs() << " dead");
Lang Hames86511252009-09-04 20:41:11 +0000882 end = start.nextSlot();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000883 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000884 }
885
886 // If it is not dead on definition, it must be killed by a
887 // subsequent instruction. Hence its interval is:
888 // [defSlot(def), useSlot(kill)+1)
Lang Hames86511252009-09-04 20:41:11 +0000889 baseIndex = baseIndex.nextIndex();
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000890 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000891 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000892 getInstructionFromIndex(baseIndex) == 0)
Lang Hames86511252009-09-04 20:41:11 +0000893 baseIndex = baseIndex.nextIndex();
Evan Cheng6130f662008-03-05 00:59:57 +0000894 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000895 DEBUG(errs() << " killed");
Lang Hames86511252009-09-04 20:41:11 +0000896 end = getUseIndex(baseIndex).nextSlot();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000897 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000898 } else {
899 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
900 if (DefIdx != -1) {
901 if (mi->isRegTiedToUseOperand(DefIdx)) {
902 // Two-address instruction.
903 end = getDefIndex(baseIndex);
904 if (mi->getOperand(DefIdx).isEarlyClobber())
905 end = getUseIndex(baseIndex);
906 } else {
907 // Another instruction redefines the register before it is ever read.
908 // Then the register is essentially dead at the instruction that defines
909 // it. Hence its interval is:
910 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000911 DEBUG(errs() << " dead");
Lang Hames86511252009-09-04 20:41:11 +0000912 end = start.nextSlot();
Evan Chengc45288e2009-04-27 20:42:46 +0000913 }
914 goto exit;
915 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000916 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000917
Lang Hames86511252009-09-04 20:41:11 +0000918 baseIndex = baseIndex.nextIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000919 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000920
921 // The only case we should have a dead physreg here without a killing or
922 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000923 // and never used. Another possible case is the implicit use of the
924 // physical register has been deleted by two-address pass.
Lang Hames86511252009-09-04 20:41:11 +0000925 end = start.nextSlot();
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000926
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000927exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000928 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000929
Evan Cheng24a3cc42007-04-25 07:30:23 +0000930 // Already exists? Extend old live interval.
931 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000932 bool Extend = OldLR != interval.end();
933 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000934 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000935 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000936 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000937 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000938 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000939 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000940 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000941}
942
Chris Lattnerf35fef72004-07-23 21:24:19 +0000943void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
944 MachineBasicBlock::iterator MI,
Lang Hames86511252009-09-04 20:41:11 +0000945 MachineInstrIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000946 MachineOperand& MO,
947 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000948 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000949 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000950 getOrCreateInterval(MO.getReg()));
951 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000952 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000953 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000954 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000955 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000956 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000957 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000958 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000959 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000960 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000961 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000962 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000963 // If MI also modifies the sub-register explicitly, avoid processing it
964 // more than once. Do not pass in TRI here so it checks for exact match.
965 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000966 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000967 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000968 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000969}
970
Evan Chengb371f452007-02-19 21:49:54 +0000971void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames86511252009-09-04 20:41:11 +0000972 MachineInstrIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000973 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000974 DEBUG({
975 errs() << "\t\tlivein register: ";
976 printRegName(interval.reg);
977 });
Evan Chengb371f452007-02-19 21:49:54 +0000978
979 // Look for kills, if it reaches a def before it's killed, then it shouldn't
980 // be considered a livein.
981 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames86511252009-09-04 20:41:11 +0000982 MachineInstrIndex baseIndex = MIIdx;
983 MachineInstrIndex start = baseIndex;
984 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +0000985 getInstructionFromIndex(baseIndex) == 0)
Lang Hames86511252009-09-04 20:41:11 +0000986 baseIndex = baseIndex.nextIndex();
987 MachineInstrIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000988 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000989
Evan Chengb371f452007-02-19 21:49:54 +0000990 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000991 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000992 DEBUG(errs() << " killed");
Lang Hames86511252009-09-04 20:41:11 +0000993 end = getUseIndex(baseIndex).nextSlot();
Evan Cheng0076c612009-03-05 03:34:26 +0000994 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000995 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000996 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000997 // Another instruction redefines the register before it is ever read.
998 // Then the register is essentially dead at the instruction that defines
999 // it. Hence its interval is:
1000 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001001 DEBUG(errs() << " dead");
Lang Hames86511252009-09-04 20:41:11 +00001002 end = getDefIndex(start).nextSlot();
Evan Cheng0076c612009-03-05 03:34:26 +00001003 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001004 break;
Evan Chengb371f452007-02-19 21:49:54 +00001005 }
1006
Lang Hames86511252009-09-04 20:41:11 +00001007 baseIndex = baseIndex.nextIndex();
Evan Chengb371f452007-02-19 21:49:54 +00001008 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001009 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001010 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001011 getInstructionFromIndex(baseIndex) == 0)
Lang Hames86511252009-09-04 20:41:11 +00001012 baseIndex = baseIndex.nextIndex();
Evan Cheng0076c612009-03-05 03:34:26 +00001013 }
Evan Chengb371f452007-02-19 21:49:54 +00001014 }
1015
Evan Cheng75611fb2007-06-27 01:16:36 +00001016 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001017 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001018 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001019 DEBUG(errs() << " dead");
Lang Hames86511252009-09-04 20:41:11 +00001020 end = getDefIndex(MIIdx).nextSlot();
Evan Cheng292da942007-06-27 18:47:28 +00001021 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001022 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001023 end = baseIndex;
1024 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001025 }
1026
Lang Hames10382fb2009-06-19 02:17:53 +00001027 VNInfo *vni =
Lang Hames86511252009-09-04 20:41:11 +00001028 interval.getNextValue(MachineInstrIndex(MBB->getNumber()),
1029 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001030 vni->setIsPHIDef(true);
1031 LiveRange LR(start, end, vni);
1032
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001033 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001034 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001035 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001036}
1037
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001038/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001039/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001040/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001041/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001042void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001043 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001044 << "********** Function: "
1045 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001046
1047 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001048 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1049 MBBI != E; ++MBBI) {
1050 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001051 // Track the index of the current machine instr.
Lang Hames86511252009-09-04 20:41:11 +00001052 MachineInstrIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001053 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001054
Chris Lattner428b92e2006-09-15 03:57:23 +00001055 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001056
Dan Gohmancb406c22007-10-03 19:26:29 +00001057 // Create intervals for live-ins to this BB first.
1058 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1059 LE = MBB->livein_end(); LI != LE; ++LI) {
1060 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1061 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001062 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001063 if (!hasInterval(*AS))
1064 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1065 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001066 }
1067
Owen Anderson99500ae2008-09-15 22:00:38 +00001068 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001069 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001070 getInstructionFromIndex(MIIndex) == 0)
Lang Hames86511252009-09-04 20:41:11 +00001071 MIIndex = MIIndex.nextIndex();
Owen Anderson99500ae2008-09-15 22:00:38 +00001072
Chris Lattner428b92e2006-09-15 03:57:23 +00001073 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001074 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001075
Evan Cheng438f7bc2006-11-10 08:43:01 +00001076 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001077 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1078 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001079 if (!MO.isReg() || !MO.getReg())
1080 continue;
1081
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001082 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001083 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001084 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001085 else if (MO.isUndef())
1086 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001087 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001088
1089 // Skip over the empty slots after each instruction.
1090 unsigned Slots = MI->getDesc().getNumDefs();
1091 if (Slots == 0)
1092 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001093
1094 while (Slots--)
1095 MIIndex = MIIndex.nextIndex();
Owen Anderson7fbad272008-07-23 21:37:49 +00001096
1097 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001098 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001099 getInstructionFromIndex(MIIndex) == 0)
Lang Hames86511252009-09-04 20:41:11 +00001100 MIIndex = MIIndex.nextIndex();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001101 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001102 }
Evan Chengd129d732009-07-17 19:43:40 +00001103
1104 // Create empty intervals for registers defined by implicit_def's (except
1105 // for those implicit_def that define values which are liveout of their
1106 // blocks.
1107 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1108 unsigned UndefReg = UndefUses[i];
1109 (void)getOrCreateInterval(UndefReg);
1110 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001111}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001112
Lang Hames86511252009-09-04 20:41:11 +00001113bool LiveIntervals::findLiveInMBBs(
1114 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001115 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001116 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001117 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001118
1119 bool ResVal = false;
1120 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001121 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001122 break;
1123 MBBs.push_back(I->second);
1124 ResVal = true;
1125 ++I;
1126 }
1127 return ResVal;
1128}
1129
Lang Hames86511252009-09-04 20:41:11 +00001130bool LiveIntervals::findReachableMBBs(
1131 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001132 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1133 std::vector<IdxMBBPair>::const_iterator I =
1134 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1135
1136 bool ResVal = false;
1137 while (I != Idx2MBBMap.end()) {
1138 if (I->first > End)
1139 break;
1140 MachineBasicBlock *MBB = I->second;
1141 if (getMBBEndIdx(MBB) > End)
1142 break;
1143 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1144 SE = MBB->succ_end(); SI != SE; ++SI)
1145 MBBs.push_back(*SI);
1146 ResVal = true;
1147 ++I;
1148 }
1149 return ResVal;
1150}
1151
Owen Anderson03857b22008-08-13 21:49:13 +00001152LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001153 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001154 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001155}
Evan Chengf2fbca62007-11-12 06:35:08 +00001156
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001157/// dupInterval - Duplicate a live interval. The caller is responsible for
1158/// managing the allocated memory.
1159LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1160 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001161 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001162 return NewLI;
1163}
1164
Evan Chengc8d044e2008-02-15 18:24:29 +00001165/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1166/// copy field and returns the source register that defines it.
1167unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001168 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001169 return 0;
1170
Lang Hames52c1afc2009-08-10 23:43:28 +00001171 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001172 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001173 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001174 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001175 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001176 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001177 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1178 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1179 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001180
Evan Cheng04ee5a12009-01-20 19:12:24 +00001181 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001182 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001183 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001184 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001185 return 0;
1186}
Evan Chengf2fbca62007-11-12 06:35:08 +00001187
1188//===----------------------------------------------------------------------===//
1189// Register allocator hooks.
1190//
1191
Evan Chengd70dbb52008-02-22 09:24:50 +00001192/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1193/// allow one) virtual register operand, then its uses are implicitly using
1194/// the register. Returns the virtual register.
1195unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1196 MachineInstr *MI) const {
1197 unsigned RegOp = 0;
1198 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1199 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001200 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001201 continue;
1202 unsigned Reg = MO.getReg();
1203 if (Reg == 0 || Reg == li.reg)
1204 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001205
1206 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1207 !allocatableRegs_[Reg])
1208 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001209 // FIXME: For now, only remat MI with at most one register operand.
1210 assert(!RegOp &&
1211 "Can't rematerialize instruction with multiple register operand!");
1212 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001213#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001214 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001215#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001216 }
1217 return RegOp;
1218}
1219
1220/// isValNoAvailableAt - Return true if the val# of the specified interval
1221/// which reaches the given instruction also reaches the specified use index.
1222bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +00001223 MachineInstrIndex UseIdx) const {
1224 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001225 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1226 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1227 return UI != li.end() && UI->valno == ValNo;
1228}
1229
Evan Chengf2fbca62007-11-12 06:35:08 +00001230/// isReMaterializable - Returns true if the definition MI of the specified
1231/// val# of the specified interval is re-materializable.
1232bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001233 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001234 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001235 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001236 if (DisableReMat)
1237 return false;
1238
Evan Cheng20ccded2008-03-15 00:19:36 +00001239 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001240 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001241
1242 int FrameIdx = 0;
1243 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001244 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001245 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1246 // this but remember this is not safe to fold into a two-address
1247 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001248 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001249 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001250
Dan Gohman6d69ba82008-07-25 00:02:30 +00001251 // If the target-specific rules don't identify an instruction as
1252 // being trivially rematerializable, use some target-independent
1253 // rules.
1254 if (!MI->getDesc().isRematerializable() ||
1255 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001256 if (!EnableAggressiveRemat)
1257 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001258
Dan Gohman0471a792008-07-28 18:43:51 +00001259 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001260 // we can't analyze it.
1261 const TargetInstrDesc &TID = MI->getDesc();
1262 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1263 return false;
1264
1265 // Avoid instructions obviously unsafe for remat.
1266 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1267 return false;
1268
1269 // If the instruction accesses memory and the memory could be non-constant,
1270 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001271 for (std::list<MachineMemOperand>::const_iterator
1272 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001273 const MachineMemOperand &MMO = *I;
1274 if (MMO.isVolatile() || MMO.isStore())
1275 return false;
1276 const Value *V = MMO.getValue();
1277 if (!V)
1278 return false;
1279 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1280 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001281 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001282 } else if (!aa_->pointsToConstantMemory(V))
1283 return false;
1284 }
1285
1286 // If any of the registers accessed are non-constant, conservatively assume
1287 // the instruction is not rematerializable.
1288 unsigned ImpUse = 0;
1289 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1290 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001291 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001292 unsigned Reg = MO.getReg();
1293 if (Reg == 0)
1294 continue;
1295 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1296 return false;
1297
1298 // Only allow one def, and that in the first operand.
1299 if (MO.isDef() != (i == 0))
1300 return false;
1301
1302 // Only allow constant-valued registers.
1303 bool IsLiveIn = mri_->isLiveIn(Reg);
1304 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1305 E = mri_->def_end();
1306
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001307 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001308 if (MO.isDef() && (next(I) != E || IsLiveIn))
1309 return false;
1310
1311 if (MO.isUse()) {
1312 // Only allow one use other register use, as that's all the
1313 // remat mechanisms support currently.
1314 if (Reg != li.reg) {
1315 if (ImpUse == 0)
1316 ImpUse = Reg;
1317 else if (Reg != ImpUse)
1318 return false;
1319 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001320 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001321 if (I != E && (next(I) != E || IsLiveIn))
1322 return false;
1323 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001324 }
1325 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001326 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001327
Dan Gohman6d69ba82008-07-25 00:02:30 +00001328 unsigned ImpUse = getReMatImplicitUse(li, MI);
1329 if (ImpUse) {
1330 const LiveInterval &ImpLi = getInterval(ImpUse);
1331 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1332 re = mri_->use_end(); ri != re; ++ri) {
1333 MachineInstr *UseMI = &*ri;
Lang Hames86511252009-09-04 20:41:11 +00001334 MachineInstrIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001335 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1336 continue;
1337 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1338 return false;
1339 }
Evan Chengdc377862008-09-30 15:44:16 +00001340
1341 // If a register operand of the re-materialized instruction is going to
1342 // be spilled next, then it's not legal to re-materialize this instruction.
1343 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1344 if (ImpUse == SpillIs[i]->reg)
1345 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001346 }
1347 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001348}
1349
Evan Cheng06587492008-10-24 02:05:00 +00001350/// isReMaterializable - Returns true if the definition MI of the specified
1351/// val# of the specified interval is re-materializable.
1352bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1353 const VNInfo *ValNo, MachineInstr *MI) {
1354 SmallVector<LiveInterval*, 4> Dummy1;
1355 bool Dummy2;
1356 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1357}
1358
Evan Cheng5ef3a042007-12-06 00:01:56 +00001359/// isReMaterializable - Returns true if every definition of MI of every
1360/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001361bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1362 SmallVectorImpl<LiveInterval*> &SpillIs,
1363 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001364 isLoad = false;
1365 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1366 i != e; ++i) {
1367 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001368 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001369 continue; // Dead val#.
1370 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001371 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001372 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001373 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001374 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001375 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001376 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001377 return false;
1378 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001379 }
1380 return true;
1381}
1382
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001383/// FilterFoldedOps - Filter out two-address use operands. Return
1384/// true if it finds any issue with the operands that ought to prevent
1385/// folding.
1386static bool FilterFoldedOps(MachineInstr *MI,
1387 SmallVector<unsigned, 2> &Ops,
1388 unsigned &MRInfo,
1389 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001390 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001391 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1392 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001393 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001394 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001395 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001396 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001397 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001398 MRInfo |= (unsigned)VirtRegMap::isMod;
1399 else {
1400 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001401 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001402 MRInfo = VirtRegMap::isModRef;
1403 continue;
1404 }
1405 MRInfo |= (unsigned)VirtRegMap::isRef;
1406 }
1407 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001408 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001409 return false;
1410}
1411
1412
1413/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1414/// slot / to reg or any rematerialized load into ith operand of specified
1415/// MI. If it is successul, MI is updated with the newly created MI and
1416/// returns true.
1417bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1418 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames86511252009-09-04 20:41:11 +00001419 MachineInstrIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001420 SmallVector<unsigned, 2> &Ops,
1421 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001422 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001423 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001424 RemoveMachineInstrFromMaps(MI);
1425 vrm.RemoveMachineInstrFromMaps(MI);
1426 MI->eraseFromParent();
1427 ++numFolds;
1428 return true;
1429 }
1430
1431 // Filter the list of operand indexes that are to be folded. Abort if
1432 // any operand will prevent folding.
1433 unsigned MRInfo = 0;
1434 SmallVector<unsigned, 2> FoldOps;
1435 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1436 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001437
Evan Cheng427f4c12008-03-31 23:19:51 +00001438 // The only time it's safe to fold into a two address instruction is when
1439 // it's folding reload and spill from / into a spill stack slot.
1440 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001441 return false;
1442
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001443 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1444 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001445 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001446 // Remember this instruction uses the spill slot.
1447 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1448
Evan Chengf2fbca62007-11-12 06:35:08 +00001449 // Attempt to fold the memory reference into the instruction. If
1450 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001451 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001452 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001453 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001454 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001455 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001456 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001457 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001458 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001459 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001460 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001461 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001462 return true;
1463 }
1464 return false;
1465}
1466
Evan Cheng018f9b02007-12-05 03:22:34 +00001467/// canFoldMemoryOperand - Returns true if the specified load / store
1468/// folding is possible.
1469bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001470 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001471 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001472 // Filter the list of operand indexes that are to be folded. Abort if
1473 // any operand will prevent folding.
1474 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001475 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001476 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1477 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001478
Evan Cheng3c75ba82008-04-01 21:37:32 +00001479 // It's only legal to remat for a use, not a def.
1480 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001481 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001482
Evan Chengd70dbb52008-02-22 09:24:50 +00001483 return tii_->canFoldMemoryOperand(MI, FoldOps);
1484}
1485
Evan Cheng81a03822007-11-17 00:40:40 +00001486bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1487 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1488 for (LiveInterval::Ranges::const_iterator
1489 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1490 std::vector<IdxMBBPair>::const_iterator II =
1491 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1492 if (II == Idx2MBBMap.end())
1493 continue;
1494 if (I->end > II->first) // crossing a MBB.
1495 return false;
1496 MBBs.insert(II->second);
1497 if (MBBs.size() > 1)
1498 return false;
1499 }
1500 return true;
1501}
1502
Evan Chengd70dbb52008-02-22 09:24:50 +00001503/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1504/// interval on to-be re-materialized operands of MI) with new register.
1505void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1506 MachineInstr *MI, unsigned NewVReg,
1507 VirtRegMap &vrm) {
1508 // There is an implicit use. That means one of the other operand is
1509 // being remat'ed and the remat'ed instruction has li.reg as an
1510 // use operand. Make sure we rewrite that as well.
1511 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1512 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001513 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001514 continue;
1515 unsigned Reg = MO.getReg();
1516 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1517 continue;
1518 if (!vrm.isReMaterialized(Reg))
1519 continue;
1520 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001521 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1522 if (UseMO)
1523 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001524 }
1525}
1526
Evan Chengf2fbca62007-11-12 06:35:08 +00001527/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1528/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001529bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001530rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001531 bool TrySplit, MachineInstrIndex index, MachineInstrIndex end,
1532 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001533 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001534 unsigned Slot, int LdSlot,
1535 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001536 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001537 const TargetRegisterClass* rc,
1538 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001539 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001540 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001541 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001542 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001543 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001544 RestartInstruction:
1545 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1546 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001547 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001548 continue;
1549 unsigned Reg = mop.getReg();
1550 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001551 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001552 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001553 if (Reg != li.reg)
1554 continue;
1555
1556 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001557 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001558 int FoldSlot = Slot;
1559 if (DefIsReMat) {
1560 // If this is the rematerializable definition MI itself and
1561 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001562 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001563 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1564 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001565 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001566 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001567 MI->eraseFromParent();
1568 break;
1569 }
1570
1571 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001572 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001573 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001574 if (isLoad) {
1575 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1576 FoldSS = isLoadSS;
1577 FoldSlot = LdSlot;
1578 }
1579 }
1580
Evan Chengf2fbca62007-11-12 06:35:08 +00001581 // Scan all of the operands of this instruction rewriting operands
1582 // to use NewVReg instead of li.reg as appropriate. We do this for
1583 // two reasons:
1584 //
1585 // 1. If the instr reads the same spilled vreg multiple times, we
1586 // want to reuse the NewVReg.
1587 // 2. If the instr is a two-addr instruction, we are required to
1588 // keep the src/dst regs pinned.
1589 //
1590 // Keep track of whether we replace a use and/or def so that we can
1591 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001592
Evan Cheng81a03822007-11-17 00:40:40 +00001593 HasUse = mop.isUse();
1594 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001595 SmallVector<unsigned, 2> Ops;
1596 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001597 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001598 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001599 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001600 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001601 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001602 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001603 continue;
1604 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001605 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001606 if (!MOj.isUndef()) {
1607 HasUse |= MOj.isUse();
1608 HasDef |= MOj.isDef();
1609 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001610 }
1611 }
1612
David Greene26b86a02008-10-27 17:38:59 +00001613 // Create a new virtual register for the spill interval.
1614 // Create the new register now so we can map the fold instruction
1615 // to the new register so when it is unfolded we get the correct
1616 // answer.
1617 bool CreatedNewVReg = false;
1618 if (NewVReg == 0) {
1619 NewVReg = mri_->createVirtualRegister(rc);
1620 vrm.grow();
1621 CreatedNewVReg = true;
1622 }
1623
Evan Cheng9c3c2212008-06-06 07:54:39 +00001624 if (!TryFold)
1625 CanFold = false;
1626 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001627 // Do not fold load / store here if we are splitting. We'll find an
1628 // optimal point to insert a load / store later.
1629 if (!TrySplit) {
1630 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001631 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001632 // Folding the load/store can completely change the instruction in
1633 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001634
1635 if (FoldSS) {
1636 // We need to give the new vreg the same stack slot as the
1637 // spilled interval.
1638 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1639 }
1640
Evan Cheng018f9b02007-12-05 03:22:34 +00001641 HasUse = false;
1642 HasDef = false;
1643 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001644 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001645 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001646 goto RestartInstruction;
1647 }
1648 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001649 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001650 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001651 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001652 }
Evan Chengcddbb832007-11-30 21:23:43 +00001653
Evan Chengcddbb832007-11-30 21:23:43 +00001654 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001655 if (mop.isImplicit())
1656 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001657
1658 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001659 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1660 MachineOperand &mopj = MI->getOperand(Ops[j]);
1661 mopj.setReg(NewVReg);
1662 if (mopj.isImplicit())
1663 rewriteImplicitOps(li, MI, NewVReg, vrm);
1664 }
Evan Chengcddbb832007-11-30 21:23:43 +00001665
Evan Cheng81a03822007-11-17 00:40:40 +00001666 if (CreatedNewVReg) {
1667 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001668 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001669 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001670 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001671 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001672 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001673 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001674 }
1675 if (!CanDelete || (HasUse && HasDef)) {
1676 // If this is a two-addr instruction then its use operands are
1677 // rematerializable but its def is not. It should be assigned a
1678 // stack slot.
1679 vrm.assignVirt2StackSlot(NewVReg, Slot);
1680 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001681 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001682 vrm.assignVirt2StackSlot(NewVReg, Slot);
1683 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001684 } else if (HasUse && HasDef &&
1685 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1686 // If this interval hasn't been assigned a stack slot (because earlier
1687 // def is a deleted remat def), do it now.
1688 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1689 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001690 }
1691
Evan Cheng313d4b82008-02-23 00:33:04 +00001692 // Re-matting an instruction with virtual register use. Add the
1693 // register as an implicit use on the use MI.
1694 if (DefIsReMat && ImpUse)
1695 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1696
Evan Cheng5b69eba2009-04-21 22:46:52 +00001697 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001698 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001699 if (CreatedNewVReg) {
1700 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001701 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001702 if (TrySplit)
1703 vrm.setIsSplitFromReg(NewVReg, li.reg);
1704 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001705
1706 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001707 if (CreatedNewVReg) {
Lang Hames86511252009-09-04 20:41:11 +00001708 LiveRange LR(getLoadIndex(index), getUseIndex(index).nextSlot(),
1709 nI.getNextValue(MachineInstrIndex(), 0, false,
1710 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001711 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001712 nI.addRange(LR);
1713 } else {
1714 // Extend the split live interval to this def / use.
Lang Hames86511252009-09-04 20:41:11 +00001715 MachineInstrIndex End = getUseIndex(index).nextSlot();
Evan Cheng81a03822007-11-17 00:40:40 +00001716 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1717 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001718 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001719 nI.addRange(LR);
1720 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001721 }
1722 if (HasDef) {
1723 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00001724 nI.getNextValue(MachineInstrIndex(), 0, false,
1725 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001726 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001727 nI.addRange(LR);
1728 }
Evan Cheng81a03822007-11-17 00:40:40 +00001729
Bill Wendling8e6179f2009-08-22 20:18:03 +00001730 DEBUG({
1731 errs() << "\t\t\t\tAdded new interval: ";
1732 nI.print(errs(), tri_);
1733 errs() << '\n';
1734 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001735 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001736 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001737}
Evan Cheng81a03822007-11-17 00:40:40 +00001738bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001739 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001740 MachineBasicBlock *MBB,
1741 MachineInstrIndex Idx) const {
1742 MachineInstrIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001743 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001744 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001745 continue;
1746
Lang Hames86511252009-09-04 20:41:11 +00001747 MachineInstrIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001748 if (KillIdx > Idx && KillIdx < End)
1749 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001750 }
1751 return false;
1752}
1753
Evan Cheng063284c2008-02-21 00:34:19 +00001754/// RewriteInfo - Keep track of machine instrs that will be rewritten
1755/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001756namespace {
1757 struct RewriteInfo {
Lang Hames86511252009-09-04 20:41:11 +00001758 MachineInstrIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001759 MachineInstr *MI;
1760 bool HasUse;
1761 bool HasDef;
Lang Hames86511252009-09-04 20:41:11 +00001762 RewriteInfo(MachineInstrIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001763 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1764 };
Evan Cheng063284c2008-02-21 00:34:19 +00001765
Dan Gohman844731a2008-05-13 00:00:25 +00001766 struct RewriteInfoCompare {
1767 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1768 return LHS.Index < RHS.Index;
1769 }
1770 };
1771}
Evan Cheng063284c2008-02-21 00:34:19 +00001772
Evan Chengf2fbca62007-11-12 06:35:08 +00001773void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001774rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001775 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001776 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001777 unsigned Slot, int LdSlot,
1778 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001779 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001780 const TargetRegisterClass* rc,
1781 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001782 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001783 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001784 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001785 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001786 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1787 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001788 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001789 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001790 unsigned NewVReg = 0;
Lang Hames86511252009-09-04 20:41:11 +00001791 MachineInstrIndex start = getBaseIndex(I->start);
1792 MachineInstrIndex end = getBaseIndex(I->end.prevSlot()).nextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001793
Evan Cheng063284c2008-02-21 00:34:19 +00001794 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001795 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001796 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001797 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1798 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001799 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001800 MachineOperand &O = ri.getOperand();
1801 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001802 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames86511252009-09-04 20:41:11 +00001803 MachineInstrIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001804 if (index < start || index >= end)
1805 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001806
1807 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001808 // Must be defined by an implicit def. It should not be spilled. Note,
1809 // this is for correctness reason. e.g.
1810 // 8 %reg1024<def> = IMPLICIT_DEF
1811 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1812 // The live range [12, 14) are not part of the r1024 live interval since
1813 // it's defined by an implicit def. It will not conflicts with live
1814 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001815 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001816 // the INSERT_SUBREG and both target registers that would overlap.
1817 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001818 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1819 }
1820 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1821
Evan Cheng313d4b82008-02-23 00:33:04 +00001822 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001823 // Now rewrite the defs and uses.
1824 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1825 RewriteInfo &rwi = RewriteMIs[i];
1826 ++i;
Lang Hames86511252009-09-04 20:41:11 +00001827 MachineInstrIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001828 bool MIHasUse = rwi.HasUse;
1829 bool MIHasDef = rwi.HasDef;
1830 MachineInstr *MI = rwi.MI;
1831 // If MI def and/or use the same register multiple times, then there
1832 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001833 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001834 while (i != e && RewriteMIs[i].MI == MI) {
1835 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001836 bool isUse = RewriteMIs[i].HasUse;
1837 if (isUse) ++NumUses;
1838 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001839 MIHasDef |= RewriteMIs[i].HasDef;
1840 ++i;
1841 }
Evan Cheng81a03822007-11-17 00:40:40 +00001842 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001843
Evan Cheng0a891ed2008-05-23 23:00:04 +00001844 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001845 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001846 // register interval's spill weight to HUGE_VALF to prevent it from
1847 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001848 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001849 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001850 }
1851
Evan Cheng063284c2008-02-21 00:34:19 +00001852 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001853 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001854 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001855 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001856 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001857 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001858 // One common case:
1859 // x = use
1860 // ...
1861 // ...
1862 // def = ...
1863 // = use
1864 // It's better to start a new interval to avoid artifically
1865 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001866 if (MIHasDef && !MIHasUse) {
1867 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001868 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001869 }
1870 }
Evan Chengcada2452007-11-28 01:28:46 +00001871 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001872
1873 bool IsNew = ThisVReg == 0;
1874 if (IsNew) {
1875 // This ends the previous live interval. If all of its def / use
1876 // can be folded, give it a low spill weight.
1877 if (NewVReg && TrySplit && AllCanFold) {
1878 LiveInterval &nI = getOrCreateInterval(NewVReg);
1879 nI.weight /= 10.0F;
1880 }
1881 AllCanFold = true;
1882 }
1883 NewVReg = ThisVReg;
1884
Evan Cheng81a03822007-11-17 00:40:40 +00001885 bool HasDef = false;
1886 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001887 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001888 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1889 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1890 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001891 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001892 if (!HasDef && !HasUse)
1893 continue;
1894
Evan Cheng018f9b02007-12-05 03:22:34 +00001895 AllCanFold &= CanFold;
1896
Evan Cheng81a03822007-11-17 00:40:40 +00001897 // Update weight of spill interval.
1898 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001899 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001900 // The spill weight is now infinity as it cannot be spilled again.
1901 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001902 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001903 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001904
1905 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001906 if (HasDef) {
1907 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001908 bool HasKill = false;
1909 if (!HasUse)
1910 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1911 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001912 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00001913 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001914 if (VNI)
1915 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1916 }
Owen Anderson28998312008-08-13 22:28:50 +00001917 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001918 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001919 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001920 if (SII == SpillIdxes.end()) {
1921 std::vector<SRInfo> S;
1922 S.push_back(SRInfo(index, NewVReg, true));
1923 SpillIdxes.insert(std::make_pair(MBBId, S));
1924 } else if (SII->second.back().vreg != NewVReg) {
1925 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001926 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001927 // If there is an earlier def and this is a two-address
1928 // instruction, then it's not possible to fold the store (which
1929 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001930 SRInfo &Info = SII->second.back();
1931 Info.index = index;
1932 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001933 }
1934 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001935 } else if (SII != SpillIdxes.end() &&
1936 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001937 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001938 // There is an earlier def that's not killed (must be two-address).
1939 // The spill is no longer needed.
1940 SII->second.pop_back();
1941 if (SII->second.empty()) {
1942 SpillIdxes.erase(MBBId);
1943 SpillMBBs.reset(MBBId);
1944 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001945 }
1946 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001947 }
1948
1949 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001950 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001951 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001952 if (SII != SpillIdxes.end() &&
1953 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001954 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001955 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001956 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001957 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001958 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001959 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001960 // If we are splitting live intervals, only fold if it's the first
1961 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001962 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001963 else if (IsNew) {
1964 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001965 if (RII == RestoreIdxes.end()) {
1966 std::vector<SRInfo> Infos;
1967 Infos.push_back(SRInfo(index, NewVReg, true));
1968 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1969 } else {
1970 RII->second.push_back(SRInfo(index, NewVReg, true));
1971 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001972 RestoreMBBs.set(MBBId);
1973 }
1974 }
1975
1976 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001977 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001978 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001979 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001980
1981 if (NewVReg && TrySplit && AllCanFold) {
1982 // If all of its def / use can be folded, give it a low spill weight.
1983 LiveInterval &nI = getOrCreateInterval(NewVReg);
1984 nI.weight /= 10.0F;
1985 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001986}
1987
Lang Hames86511252009-09-04 20:41:11 +00001988bool LiveIntervals::alsoFoldARestore(int Id, MachineInstrIndex index,
1989 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001990 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001991 if (!RestoreMBBs[Id])
1992 return false;
1993 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1994 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1995 if (Restores[i].index == index &&
1996 Restores[i].vreg == vr &&
1997 Restores[i].canFold)
1998 return true;
1999 return false;
2000}
2001
Lang Hames86511252009-09-04 20:41:11 +00002002void LiveIntervals::eraseRestoreInfo(int Id, MachineInstrIndex index,
2003 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002004 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002005 if (!RestoreMBBs[Id])
2006 return;
2007 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2008 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2009 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames86511252009-09-04 20:41:11 +00002010 Restores[i].index = MachineInstrIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002011}
Evan Cheng81a03822007-11-17 00:40:40 +00002012
Evan Cheng4cce6b42008-04-11 17:53:36 +00002013/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2014/// spilled and create empty intervals for their uses.
2015void
2016LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2017 const TargetRegisterClass* rc,
2018 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002019 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2020 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002021 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002022 MachineInstr *MI = &*ri;
2023 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002024 if (O.isDef()) {
2025 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2026 "Register def was not rewritten?");
2027 RemoveMachineInstrFromMaps(MI);
2028 vrm.RemoveMachineInstrFromMaps(MI);
2029 MI->eraseFromParent();
2030 } else {
2031 // This must be an use of an implicit_def so it's not part of the live
2032 // interval. Create a new empty live interval for it.
2033 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2034 unsigned NewVReg = mri_->createVirtualRegister(rc);
2035 vrm.grow();
2036 vrm.setIsImplicitlyDefined(NewVReg);
2037 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2038 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2039 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002040 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002041 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002042 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002043 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002044 }
2045 }
Evan Cheng419852c2008-04-03 16:39:43 +00002046 }
2047}
2048
Evan Chengf2fbca62007-11-12 06:35:08 +00002049std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002050addIntervalsForSpillsFast(const LiveInterval &li,
2051 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002052 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002053 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002054
2055 std::vector<LiveInterval*> added;
2056
2057 assert(li.weight != HUGE_VALF &&
2058 "attempt to spill already spilled interval!");
2059
Bill Wendling8e6179f2009-08-22 20:18:03 +00002060 DEBUG({
2061 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2062 li.dump();
2063 errs() << '\n';
2064 });
Owen Andersond6664312008-08-18 18:05:32 +00002065
2066 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2067
Owen Andersona41e47a2008-08-19 22:12:11 +00002068 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2069 while (RI != mri_->reg_end()) {
2070 MachineInstr* MI = &*RI;
2071
2072 SmallVector<unsigned, 2> Indices;
2073 bool HasUse = false;
2074 bool HasDef = false;
2075
2076 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2077 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002078 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002079
2080 HasUse |= MI->getOperand(i).isUse();
2081 HasDef |= MI->getOperand(i).isDef();
2082
2083 Indices.push_back(i);
2084 }
2085
2086 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2087 Indices, true, slot, li.reg)) {
2088 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002089 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002090 vrm.assignVirt2StackSlot(NewVReg, slot);
2091
Owen Andersona41e47a2008-08-19 22:12:11 +00002092 // create a new register for this spill
2093 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002094
Owen Andersona41e47a2008-08-19 22:12:11 +00002095 // the spill weight is now infinity as it
2096 // cannot be spilled again
2097 nI.weight = HUGE_VALF;
2098
2099 // Rewrite register operands to use the new vreg.
2100 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2101 E = Indices.end(); I != E; ++I) {
2102 MI->getOperand(*I).setReg(NewVReg);
2103
2104 if (MI->getOperand(*I).isUse())
2105 MI->getOperand(*I).setIsKill(true);
2106 }
2107
2108 // Fill in the new live interval.
Lang Hames86511252009-09-04 20:41:11 +00002109 MachineInstrIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002110 if (HasUse) {
2111 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002112 nI.getNextValue(MachineInstrIndex(), 0, false,
2113 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002114 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002115 nI.addRange(LR);
2116 vrm.addRestorePoint(NewVReg, MI);
2117 }
2118 if (HasDef) {
2119 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002120 nI.getNextValue(MachineInstrIndex(), 0, false,
2121 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002122 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002123 nI.addRange(LR);
2124 vrm.addSpillPoint(NewVReg, true, MI);
2125 }
2126
Owen Anderson17197312008-08-18 23:41:04 +00002127 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002128
Bill Wendling8e6179f2009-08-22 20:18:03 +00002129 DEBUG({
2130 errs() << "\t\t\t\tadded new interval: ";
2131 nI.dump();
2132 errs() << '\n';
2133 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002134 }
Owen Anderson9a032932008-08-18 21:20:32 +00002135
Owen Anderson9a032932008-08-18 21:20:32 +00002136
Owen Andersona41e47a2008-08-19 22:12:11 +00002137 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002138 }
Owen Andersond6664312008-08-18 18:05:32 +00002139
2140 return added;
2141}
2142
2143std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002144addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002145 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002146 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002147
2148 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002149 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002150
Evan Chengf2fbca62007-11-12 06:35:08 +00002151 assert(li.weight != HUGE_VALF &&
2152 "attempt to spill already spilled interval!");
2153
Bill Wendling8e6179f2009-08-22 20:18:03 +00002154 DEBUG({
2155 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2156 li.print(errs(), tri_);
2157 errs() << '\n';
2158 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002159
Evan Cheng72eeb942008-12-05 17:00:16 +00002160 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002161 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002162 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002163 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002164 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2165 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002166 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002167 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002168
2169 unsigned NumValNums = li.getNumValNums();
2170 SmallVector<MachineInstr*, 4> ReMatDefs;
2171 ReMatDefs.resize(NumValNums, NULL);
2172 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2173 ReMatOrigDefs.resize(NumValNums, NULL);
2174 SmallVector<int, 4> ReMatIds;
2175 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2176 BitVector ReMatDelete(NumValNums);
2177 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2178
Evan Cheng81a03822007-11-17 00:40:40 +00002179 // Spilling a split live interval. It cannot be split any further. Also,
2180 // it's also guaranteed to be a single val# / range interval.
2181 if (vrm.getPreSplitReg(li.reg)) {
2182 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002183 // Unset the split kill marker on the last use.
Lang Hames86511252009-09-04 20:41:11 +00002184 MachineInstrIndex KillIdx = vrm.getKillPoint(li.reg);
2185 if (KillIdx != MachineInstrIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002186 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2187 assert(KillMI && "Last use disappeared?");
2188 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2189 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002190 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002191 }
Evan Chengadf85902007-12-05 09:51:10 +00002192 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002193 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2194 Slot = vrm.getStackSlot(li.reg);
2195 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2196 MachineInstr *ReMatDefMI = DefIsReMat ?
2197 vrm.getReMaterializedMI(li.reg) : NULL;
2198 int LdSlot = 0;
2199 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2200 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002201 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002202 bool IsFirstRange = true;
2203 for (LiveInterval::Ranges::const_iterator
2204 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2205 // If this is a split live interval with multiple ranges, it means there
2206 // are two-address instructions that re-defined the value. Only the
2207 // first def can be rematerialized!
2208 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002209 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002210 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2211 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002212 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002213 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002214 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002215 } else {
2216 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2217 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002218 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002219 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002220 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002221 }
2222 IsFirstRange = false;
2223 }
Evan Cheng419852c2008-04-03 16:39:43 +00002224
Evan Cheng4cce6b42008-04-11 17:53:36 +00002225 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002226 return NewLIs;
2227 }
2228
2229 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002230 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2231 TrySplit = false;
2232 if (TrySplit)
2233 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002234 bool NeedStackSlot = false;
2235 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2236 i != e; ++i) {
2237 const VNInfo *VNI = *i;
2238 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002239 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002240 continue; // Dead val#.
2241 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002242 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2243 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002244 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002245 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002246 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002247 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002248 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002249 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2250 ClonedMIs.push_back(Clone);
2251 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002252
2253 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002254 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002255 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002256 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002257 CanDelete = false;
2258 // Need a stack slot if there is any live range where uses cannot be
2259 // rematerialized.
2260 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002261 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002262 if (CanDelete)
2263 ReMatDelete.set(VN);
2264 } else {
2265 // Need a stack slot if there is any live range where uses cannot be
2266 // rematerialized.
2267 NeedStackSlot = true;
2268 }
2269 }
2270
2271 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002272 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2273 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2274 Slot = vrm.assignVirt2StackSlot(li.reg);
2275
2276 // This case only occurs when the prealloc splitter has already assigned
2277 // a stack slot to this vreg.
2278 else
2279 Slot = vrm.getStackSlot(li.reg);
2280 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002281
2282 // Create new intervals and rewrite defs and uses.
2283 for (LiveInterval::Ranges::const_iterator
2284 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002285 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2286 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2287 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002288 bool CanDelete = ReMatDelete[I->valno->id];
2289 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002290 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002291 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002292 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002293 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002294 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002295 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002296 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002297 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002298 }
2299
Evan Cheng0cbb1162007-11-29 01:06:25 +00002300 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002301 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002302 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002303 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002304 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002305
Evan Chengb50bb8c2007-12-05 08:16:32 +00002306 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002307 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002308 if (NeedStackSlot) {
2309 int Id = SpillMBBs.find_first();
2310 while (Id != -1) {
2311 std::vector<SRInfo> &spills = SpillIdxes[Id];
2312 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002313 MachineInstrIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002314 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002315 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002316 bool isReMat = vrm.isReMaterialized(VReg);
2317 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002318 bool CanFold = false;
2319 bool FoundUse = false;
2320 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002321 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002322 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002323 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2324 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002325 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002326 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002327
2328 Ops.push_back(j);
2329 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002330 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002331 if (isReMat ||
2332 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2333 RestoreMBBs, RestoreIdxes))) {
2334 // MI has two-address uses of the same register. If the use
2335 // isn't the first and only use in the BB, then we can't fold
2336 // it. FIXME: Move this to rewriteInstructionsForSpills.
2337 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002338 break;
2339 }
Evan Chengaee4af62007-12-02 08:30:39 +00002340 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002341 }
2342 }
2343 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002344 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002345 if (CanFold && !Ops.empty()) {
2346 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002347 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002348 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002349 // Also folded uses, do not issue a load.
2350 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames86511252009-09-04 20:41:11 +00002351 nI.removeRange(getLoadIndex(index), getUseIndex(index).nextSlot());
Evan Chengf38d14f2007-12-05 09:05:34 +00002352 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002353 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002354 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002355 }
2356
Evan Cheng7e073ba2008-04-09 20:57:25 +00002357 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002358 if (!Folded) {
2359 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2360 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002361 if (!MI->registerDefIsDead(nI.reg))
2362 // No need to spill a dead def.
2363 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002364 if (isKill)
2365 AddedKill.insert(&nI);
2366 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002367 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002368 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002369 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002370 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002371
Evan Cheng1953d0c2007-11-29 10:12:14 +00002372 int Id = RestoreMBBs.find_first();
2373 while (Id != -1) {
2374 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2375 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002376 MachineInstrIndex index = restores[i].index;
2377 if (index == MachineInstrIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002378 continue;
2379 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002380 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002381 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002382 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002383 bool CanFold = false;
2384 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002385 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002386 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002387 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2388 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002389 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002390 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002391
Evan Cheng0cbb1162007-11-29 01:06:25 +00002392 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002393 // If this restore were to be folded, it would have been folded
2394 // already.
2395 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002396 break;
2397 }
Evan Chengaee4af62007-12-02 08:30:39 +00002398 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002399 }
2400 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002401
2402 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002403 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002404 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002405 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002406 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2407 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002408 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2409 int LdSlot = 0;
2410 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2411 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002412 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002413 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2414 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002415 if (!Folded) {
2416 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2417 if (ImpUse) {
2418 // Re-matting an instruction with virtual register use. Add the
2419 // register as an implicit use on the use MI and update the register
2420 // interval's spill weight to HUGE_VALF to prevent it from being
2421 // spilled.
2422 LiveInterval &ImpLi = getInterval(ImpUse);
2423 ImpLi.weight = HUGE_VALF;
2424 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2425 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002426 }
Evan Chengaee4af62007-12-02 08:30:39 +00002427 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002428 }
2429 // If folding is not possible / failed, then tell the spiller to issue a
2430 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002431 if (Folded)
Lang Hames86511252009-09-04 20:41:11 +00002432 nI.removeRange(getLoadIndex(index), getUseIndex(index).nextSlot());
Evan Chengb50bb8c2007-12-05 08:16:32 +00002433 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002434 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002435 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002436 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002437 }
2438
Evan Chengb50bb8c2007-12-05 08:16:32 +00002439 // Finalize intervals: add kills, finalize spill weights, and filter out
2440 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002441 std::vector<LiveInterval*> RetNewLIs;
2442 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2443 LiveInterval *LI = NewLIs[i];
2444 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002445 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002446 if (!AddedKill.count(LI)) {
2447 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames86511252009-09-04 20:41:11 +00002448 MachineInstrIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002449 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002450 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002451 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002452 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002453 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002454 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002455 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002456 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002457 RetNewLIs.push_back(LI);
2458 }
2459 }
Evan Cheng81a03822007-11-17 00:40:40 +00002460
Evan Cheng4cce6b42008-04-11 17:53:36 +00002461 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002462 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002463}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002464
2465/// hasAllocatableSuperReg - Return true if the specified physical register has
2466/// any super register that's allocatable.
2467bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2468 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2469 if (allocatableRegs_[*AS] && hasInterval(*AS))
2470 return true;
2471 return false;
2472}
2473
2474/// getRepresentativeReg - Find the largest super register of the specified
2475/// physical register.
2476unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2477 // Find the largest super-register that is allocatable.
2478 unsigned BestReg = Reg;
2479 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2480 unsigned SuperReg = *AS;
2481 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2482 BestReg = SuperReg;
2483 break;
2484 }
2485 }
2486 return BestReg;
2487}
2488
2489/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2490/// specified interval that conflicts with the specified physical register.
2491unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2492 unsigned PhysReg) const {
2493 unsigned NumConflicts = 0;
2494 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2495 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2496 E = mri_->reg_end(); I != E; ++I) {
2497 MachineOperand &O = I.getOperand();
2498 MachineInstr *MI = O.getParent();
Lang Hames86511252009-09-04 20:41:11 +00002499 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002500 if (pli.liveAt(Index))
2501 ++NumConflicts;
2502 }
2503 return NumConflicts;
2504}
2505
2506/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002507/// around all defs and uses of the specified interval. Return true if it
2508/// was able to cut its interval.
2509bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002510 unsigned PhysReg, VirtRegMap &vrm) {
2511 unsigned SpillReg = getRepresentativeReg(PhysReg);
2512
2513 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2514 // If there are registers which alias PhysReg, but which are not a
2515 // sub-register of the chosen representative super register. Assert
2516 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002517 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002518 tri_->isSuperRegister(*AS, SpillReg));
2519
Evan Cheng2824a652009-03-23 18:24:37 +00002520 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002521 LiveInterval &pli = getInterval(SpillReg);
2522 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2523 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2524 E = mri_->reg_end(); I != E; ++I) {
2525 MachineOperand &O = I.getOperand();
2526 MachineInstr *MI = O.getParent();
2527 if (SeenMIs.count(MI))
2528 continue;
2529 SeenMIs.insert(MI);
Lang Hames86511252009-09-04 20:41:11 +00002530 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002531 if (pli.liveAt(Index)) {
2532 vrm.addEmergencySpill(SpillReg, MI);
Lang Hames86511252009-09-04 20:41:11 +00002533 MachineInstrIndex StartIdx = getLoadIndex(Index);
2534 MachineInstrIndex EndIdx = getStoreIndex(Index).nextSlot();
Evan Cheng2824a652009-03-23 18:24:37 +00002535 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002536 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002537 Cut = true;
2538 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002539 std::string msg;
2540 raw_string_ostream Msg(msg);
2541 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002542 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002543 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002544 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002545 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002546 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002547 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002548 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002549 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2550 if (!hasInterval(*AS))
2551 continue;
2552 LiveInterval &spli = getInterval(*AS);
2553 if (spli.liveAt(Index))
Lang Hames86511252009-09-04 20:41:11 +00002554 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index).nextSlot());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002555 }
2556 }
2557 }
Evan Cheng2824a652009-03-23 18:24:37 +00002558 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002559}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002560
2561LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002562 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002563 LiveInterval& Interval = getOrCreateInterval(reg);
2564 VNInfo* VN = Interval.getNextValue(
Lang Hames86511252009-09-04 20:41:11 +00002565 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
2566 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002567 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002568 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2569 LiveRange LR(
2570 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
2571 getMBBEndIdx(startInst->getParent()).nextSlot(), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002572 Interval.addRange(LR);
2573
2574 return LR;
2575}
David Greeneb5257662009-08-03 21:55:09 +00002576