blob: c690ac32412cf492c8af2cff56d843928559d1f0 [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 Gohman4c8f8702008-07-25 15:08:37 +000052static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
53
Owen Andersonae339ba2008-08-19 00:17:30 +000054static cl::opt<bool> EnableFastSpilling("fast-spill",
55 cl::init(false), cl::Hidden);
56
Evan Cheng752195e2009-09-14 21:33:42 +000057static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
58
59static cl::opt<int> CoalescingLimit("early-coalescing-limit",
60 cl::init(-1), cl::Hidden);
61
62STATISTIC(numIntervals , "Number of original intervals");
63STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
65STATISTIC(numCoalescing, "Number of early coalescing performed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000066
Devang Patel19974732007-05-03 01:11:54 +000067char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000068static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069
Chris Lattnerf7da2c72006-08-24 22:43:55 +000070void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000071 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000072 AU.addRequired<AliasAnalysis>();
73 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000074 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000075 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000076 AU.addPreservedID(MachineLoopInfoID);
77 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000078
79 if (!StrongPHIElim) {
80 AU.addPreservedID(PHIEliminationID);
81 AU.addRequiredID(PHIEliminationID);
82 }
83
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000086}
87
Chris Lattnerf7da2c72006-08-24 22:43:55 +000088void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000089 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000090 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000091 E = r2iMap_.end(); I != E; ++I)
92 delete I->second;
93
Evan Cheng3f32d652008-06-04 09:18:41 +000094 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000095 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 mi2iMap_.clear();
97 i2miMap_.clear();
98 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000099 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000100 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000101
Evan Chengdd199d22007-09-06 01:07:24 +0000102 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
103 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000104 while (!CloneMIs.empty()) {
105 MachineInstr *MI = CloneMIs.back();
106 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000107 mf_->DeleteMachineInstr(MI);
108 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000109}
110
Evan Cheng6ade93b2009-08-05 03:53:14 +0000111static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
112 const TargetInstrInfo *tii_) {
113 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
114 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
115 Reg == SrcReg)
116 return true;
117
118 if ((MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
119 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG) &&
120 MI->getOperand(2).getReg() == Reg)
121 return true;
122 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG &&
123 MI->getOperand(1).getReg() == Reg)
124 return true;
125 return false;
126}
127
Evan Cheng2578ba22009-07-01 01:59:31 +0000128/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
129/// there is one implicit_def for each use. Add isUndef marker to
130/// implicit_def defs and their uses.
131void LiveIntervals::processImplicitDefs() {
132 SmallSet<unsigned, 8> ImpDefRegs;
133 SmallVector<MachineInstr*, 8> ImpDefMIs;
134 MachineBasicBlock *Entry = mf_->begin();
135 SmallPtrSet<MachineBasicBlock*,16> Visited;
136 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
137 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
138 DFI != E; ++DFI) {
139 MachineBasicBlock *MBB = *DFI;
140 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
141 I != E; ) {
142 MachineInstr *MI = &*I;
143 ++I;
144 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
145 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000146 ImpDefRegs.insert(Reg);
147 ImpDefMIs.push_back(MI);
148 continue;
149 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000150
151 bool ChangedToImpDef = false;
152 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000153 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000154 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000155 continue;
156 unsigned Reg = MO.getReg();
157 if (!Reg)
158 continue;
159 if (!ImpDefRegs.count(Reg))
160 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000161 // Use is a copy, just turn it into an implicit_def.
Evan Cheng6ade93b2009-08-05 03:53:14 +0000162 if (CanTurnIntoImplicitDef(MI, Reg, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000163 bool isKill = MO.isKill();
164 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
165 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
166 MI->RemoveOperand(j);
167 if (isKill)
168 ImpDefRegs.erase(Reg);
169 ChangedToImpDef = true;
170 break;
171 }
172
Evan Cheng2578ba22009-07-01 01:59:31 +0000173 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000174 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
175 // Make sure other uses of
176 for (unsigned j = i+1; j != e; ++j) {
177 MachineOperand &MOJ = MI->getOperand(j);
178 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
179 MOJ.setIsUndef();
180 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000181 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000182 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000183 }
184
Evan Cheng459a7c62009-07-01 08:19:36 +0000185 if (ChangedToImpDef) {
186 // Backtrack to process this new implicit_def.
187 --I;
188 } else {
189 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
190 MachineOperand& MO = MI->getOperand(i);
191 if (!MO.isReg() || !MO.isDef())
192 continue;
193 ImpDefRegs.erase(MO.getReg());
194 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000195 }
196 }
197
198 // Any outstanding liveout implicit_def's?
199 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
200 MachineInstr *MI = ImpDefMIs[i];
201 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000202 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
203 !ImpDefRegs.count(Reg)) {
204 // Delete all "local" implicit_def's. That include those which define
205 // physical registers since they cannot be liveout.
206 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000207 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000208 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000209
210 // If there are multiple defs of the same register and at least one
211 // is not an implicit_def, do not insert implicit_def's before the
212 // uses.
213 bool Skip = false;
214 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
215 DE = mri_->def_end(); DI != DE; ++DI) {
216 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
217 Skip = true;
218 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000219 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000220 }
221 if (Skip)
222 continue;
223
Evan Chengd129d732009-07-17 19:43:40 +0000224 // The only implicit_def which we want to keep are those that are live
225 // out of its block.
226 MI->eraseFromParent();
227
Evan Cheng459a7c62009-07-01 08:19:36 +0000228 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
229 UE = mri_->use_end(); UI != UE; ) {
230 MachineOperand &RMO = UI.getOperand();
231 MachineInstr *RMI = &*UI;
232 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000233 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000234 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000235 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000236
237 // Turn a copy use into an implicit_def.
238 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
239 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
240 Reg == SrcReg) {
241 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
242 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
243 RMI->RemoveOperand(j);
244 continue;
245 }
246
Evan Cheng2578ba22009-07-01 01:59:31 +0000247 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
248 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000249 RMO.setReg(NewVReg);
250 RMO.setIsUndef();
251 RMO.setIsKill();
252 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000253 }
254 ImpDefRegs.clear();
255 ImpDefMIs.clear();
256 }
257}
258
Lang Hames86511252009-09-04 20:41:11 +0000259
Owen Anderson80b3ce62008-05-28 20:54:50 +0000260void LiveIntervals::computeNumbering() {
261 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000262 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000263
264 Idx2MBBMap.clear();
265 MBB2IdxMap.clear();
266 mi2iMap_.clear();
267 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000268 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000269 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000270
Owen Andersona1566f22008-07-22 22:46:49 +0000271 FunctionSize = 0;
272
Chris Lattner428b92e2006-09-15 03:57:23 +0000273 // Number MachineInstrs and MachineBasicBlocks.
274 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000275 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
276 std::make_pair(MachineInstrIndex(),MachineInstrIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000277
Lang Hames86511252009-09-04 20:41:11 +0000278 MachineInstrIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000279 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
280 MBB != E; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000281 MachineInstrIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000282
Owen Anderson7fbad272008-07-23 21:37:49 +0000283 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000284 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000285 i2miMap_.push_back(0);
286
Chris Lattner428b92e2006-09-15 03:57:23 +0000287 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
288 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000289
290 if (I == MBB->getFirstTerminator()) {
291 // Leave a gap for before terminators, this is where we will point
292 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000293 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000294 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000295 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000296 assert(inserted &&
297 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000298 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000299 i2miMap_.push_back(0);
300
Lang Hames35f291d2009-09-12 03:34:03 +0000301 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000302 }
303
Chris Lattner428b92e2006-09-15 03:57:23 +0000304 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000305 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000306 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000307 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000308 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000309 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000310
Evan Cheng4ed43292008-10-18 05:21:37 +0000311 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000312 unsigned Slots = I->getDesc().getNumDefs();
313 if (Slots == 0)
314 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000315 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000316 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000317 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000318 }
319
Owen Anderson35578012008-06-16 07:10:49 +0000320 }
Lang Hamesffd13262009-07-09 03:57:02 +0000321
322 if (MBB->getFirstTerminator() == MBB->end()) {
323 // Leave a gap for before terminators, this is where we will point
324 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000325 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000326 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000327 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000328 assert(inserted &&
329 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000330 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000331 i2miMap_.push_back(0);
332
Lang Hames35f291d2009-09-12 03:34:03 +0000333 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000334 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000335
Owen Anderson1fbb4542008-06-16 16:58:24 +0000336 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000337 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000338 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000339 }
Lang Hamesffd13262009-07-09 03:57:02 +0000340
Evan Cheng4ca980e2007-10-17 02:10:22 +0000341 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000342
343 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000344 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000345 for (LiveInterval::iterator LI = OI->second->begin(),
346 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000347
Owen Anderson7eec0c22008-05-29 23:01:22 +0000348 // Remap the start index of the live range to the corresponding new
349 // number, or our best guess at what it _should_ correspond to if the
350 // original instruction has been erased. This is either the following
351 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000352 unsigned index = LI->start.getVecIndex();
353 MachineInstrIndex::Slot offset = LI->start.getSlot();
354 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000355 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000356 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000357 // Take the pair containing the index
358 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000359 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000360
Owen Anderson7fbad272008-07-23 21:37:49 +0000361 LI->start = getMBBStartIdx(J->second);
362 } else {
Lang Hames86511252009-09-04 20:41:11 +0000363 LI->start = MachineInstrIndex(
364 MachineInstrIndex(mi2iMap_[OldI2MI[index]]),
365 (MachineInstrIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000366 }
367
368 // Remap the ending index in the same way that we remapped the start,
369 // except for the final step where we always map to the immediately
370 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000371 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000372 offset = LI->end.getSlot();
373 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000374 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000375 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000376 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000377 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000378
Lang Hames35f291d2009-09-12 03:34:03 +0000379 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000380 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000381 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000382 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
383
384 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000385 LI->end =
386 MachineInstrIndex(mi2iMap_[OldI2MI[index]],
387 (idx == index ? offset : MachineInstrIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000388 else
Lang Hames86511252009-09-04 20:41:11 +0000389 LI->end =
390 MachineInstrIndex(MachineInstrIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000391 }
Owen Anderson788d0412008-08-06 18:35:45 +0000392 }
393
Owen Anderson03857b22008-08-13 21:49:13 +0000394 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
395 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000396 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000397
Owen Anderson7eec0c22008-05-29 23:01:22 +0000398 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000399 // start indices above. VN's with special sentinel defs
400 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000401 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000402 unsigned index = vni->def.getVecIndex();
403 MachineInstrIndex::Slot offset = vni->def.getSlot();
404 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000405 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000406 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000407 // Take the pair containing the index
408 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000409 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000410
Owen Anderson91292392008-07-30 17:42:47 +0000411 vni->def = getMBBStartIdx(J->second);
412 } else {
Lang Hames86511252009-09-04 20:41:11 +0000413 vni->def = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000414 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000415 }
Owen Anderson745825f42008-05-28 22:40:08 +0000416
Owen Anderson7eec0c22008-05-29 23:01:22 +0000417 // Remap the VNInfo kill indices, which works the same as
418 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000419 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000420 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000421 MachineInstrIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000422
Lang Hames86511252009-09-04 20:41:11 +0000423 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000424 assert("Value killed at a load slot.");
425 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000426 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000427 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000428
Lang Hamesffd13262009-07-09 03:57:02 +0000429 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000430 } else {
Lang Hames86511252009-09-04 20:41:11 +0000431 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000432 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000433 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000434 --I;
Lang Hames86511252009-09-04 20:41:11 +0000435 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000436 } else {
437 assert(OldI2MI[index] != 0 &&
438 "Kill refers to instruction not present in index maps.");
Lang Hames86511252009-09-04 20:41:11 +0000439 vni->kills[i] = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000440 }
441
442 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000443 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000444 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
445
446 if (index != OldI2MI.size())
447 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
448 (idx == index ? offset : 0);
449 else
450 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000451 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000452 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000453 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000454 }
Owen Anderson788d0412008-08-06 18:35:45 +0000455 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000456}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000457
Lang Hamesf41538d2009-06-02 16:53:25 +0000458void LiveIntervals::scaleNumbering(int factor) {
459 // Need to
460 // * scale MBB begin and end points
461 // * scale all ranges.
462 // * Update VNI structures.
463 // * Scale instruction numberings
464
465 // Scale the MBB indices.
466 Idx2MBBMap.clear();
467 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
468 MBB != MBBE; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000469 std::pair<MachineInstrIndex, MachineInstrIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
470 mbbIndices.first = mbbIndices.first.scale(factor);
471 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000472 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
473 }
474 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
475
Lang Hamesffd13262009-07-09 03:57:02 +0000476 // Scale terminator gaps.
Lang Hames86511252009-09-04 20:41:11 +0000477 for (DenseMap<MachineBasicBlock*, MachineInstrIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000478 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
479 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000480 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000481 }
482
Lang Hamesf41538d2009-06-02 16:53:25 +0000483 // Scale the intervals.
484 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
485 LI->second->scaleNumbering(factor);
486 }
487
488 // Scale MachineInstrs.
489 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hames86511252009-09-04 20:41:11 +0000490 MachineInstrIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000491 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
492 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000493 MachineInstrIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000494 mi2iMap_[MI->first] = newSlot;
495 highestSlot = std::max(highestSlot, newSlot);
496 }
497
Lang Hames86511252009-09-04 20:41:11 +0000498 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000499 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000500 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000501 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
502 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000503 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000504 }
505
506}
507
508
Owen Anderson80b3ce62008-05-28 20:54:50 +0000509/// runOnMachineFunction - Register allocate the whole function
510///
511bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
512 mf_ = &fn;
513 mri_ = &mf_->getRegInfo();
514 tm_ = &fn.getTarget();
515 tri_ = tm_->getRegisterInfo();
516 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000517 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000518 lv_ = &getAnalysis<LiveVariables>();
519 allocatableRegs_ = tri_->getAllocatableSet(fn);
520
Evan Cheng2578ba22009-07-01 01:59:31 +0000521 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000522 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000523 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000524 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000525
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000526 numIntervals += getNumIntervals();
527
Chris Lattner70ca3582004-09-30 15:59:17 +0000528 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000530}
531
Chris Lattner70ca3582004-09-30 15:59:17 +0000532/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000533void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000534 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000535 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000536 I->second->print(OS, tri_);
537 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000538 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000539
Evan Cheng752195e2009-09-14 21:33:42 +0000540 printInstrs(OS);
541}
542
543void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000544 OS << "********** MACHINEINSTRS **********\n";
545
Chris Lattner3380d5c2009-07-21 21:12:58 +0000546 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
547 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000548 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000549 for (MachineBasicBlock::iterator mii = mbbi->begin(),
550 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000551 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000552 }
553 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000554}
555
Evan Cheng752195e2009-09-14 21:33:42 +0000556void LiveIntervals::dumpInstrs() const {
557 printInstrs(errs());
558}
559
Evan Chengc92da382007-11-03 07:20:12 +0000560/// conflictsWithPhysRegDef - Returns true if the specified register
561/// is defined during the duration of the specified interval.
562bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
563 VirtRegMap &vrm, unsigned reg) {
564 for (LiveInterval::Ranges::const_iterator
565 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000566 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000567 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
568 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000569 // skip deleted instructions
570 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000571 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000572 if (index == end) break;
573
574 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000575 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
576 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000577 if (SrcReg == li.reg || DstReg == li.reg)
578 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000579 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
580 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000581 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000582 continue;
583 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000584 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000585 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000586 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000587 if (!vrm.hasPhys(PhysReg))
588 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000589 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000590 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000591 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000592 return true;
593 }
594 }
595 }
596
597 return false;
598}
599
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000600/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
601/// it can check use as well.
602bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
603 unsigned Reg, bool CheckUse,
604 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
605 for (LiveInterval::Ranges::const_iterator
606 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000607 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000608 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
609 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000610 // Skip deleted instructions.
611 MachineInstr *MI = 0;
612 while (index != end) {
613 MI = getInstructionFromIndex(index);
614 if (MI)
615 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000616 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000617 }
618 if (index == end) break;
619
620 if (JoinedCopies.count(MI))
621 continue;
622 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
623 MachineOperand& MO = MI->getOperand(i);
624 if (!MO.isReg())
625 continue;
626 if (MO.isUse() && !CheckUse)
627 continue;
628 unsigned PhysReg = MO.getReg();
629 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
630 continue;
631 if (tri_->isSubRegister(Reg, PhysReg))
632 return true;
633 }
634 }
635 }
636
637 return false;
638}
639
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000640#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000641static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000642 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000643 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000644 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000645 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000646}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000647#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000648
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000649void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000650 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000651 MachineInstrIndex MIIdx,
652 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000653 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000654 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000655 DEBUG({
656 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000657 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000658 });
Evan Cheng419852c2008-04-03 16:39:43 +0000659
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000660 // Virtual registers may be defined multiple times (due to phi
661 // elimination and 2-addr elimination). Much of what we do only has to be
662 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000663 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000664 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 if (interval.empty()) {
666 // Get the Idx of the defining instructions.
Lang Hames86511252009-09-04 20:41:11 +0000667 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000668 // Earlyclobbers move back one, so that they overlap the live range
669 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000670 if (MO.isEarlyClobber())
671 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000672 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000673 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000674 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000675 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000676 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000677 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000678 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000679 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000680 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000681 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000682
683 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000684
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000685 // Loop over all of the blocks that the vreg is defined in. There are
686 // two cases we have to handle here. The most common case is a vreg
687 // whose lifetime is contained within a basic block. In this case there
688 // will be a single kill, in MBB, which comes after the definition.
689 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
690 // FIXME: what about dead vars?
Lang Hames86511252009-09-04 20:41:11 +0000691 MachineInstrIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000692 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000693 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000694 else if (MO.isEarlyClobber())
695 // Earlyclobbers that die in this instruction move up one extra, to
696 // compensate for having the starting point moved back one. This
697 // gets them to overlap the live range of other outputs.
698 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 else
Lang Hames35f291d2009-09-12 03:34:03 +0000700 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000701
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 // If the kill happens after the definition, we have an intra-block
703 // live range.
704 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000705 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000706 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000707 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000708 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000709 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000710 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000711 return;
712 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000713 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000714
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000715 // The other case we handle is when a virtual register lives to the end
716 // of the defining block, potentially live across some blocks, then is
717 // live into some number of blocks, but gets killed. Start by adding a
718 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000719 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000720 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000721 interval.addRange(NewLR);
722
723 // Iterate over all of the blocks that the variable is completely
724 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
725 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000726 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
727 E = vi.AliveBlocks.end(); I != E; ++I) {
728 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000729 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000730 ValNo);
731 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000732 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000733 }
734
735 // Finally, this virtual register is live from the start of any killing
736 // block to the 'use' slot of the killing instruction.
737 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
738 MachineInstr *Kill = vi.Kills[i];
Evan Cheng21731112009-09-12 02:01:07 +0000739 MachineInstrIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000740 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Chris Lattner428b92e2006-09-15 03:57:23 +0000741 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000742 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000743 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000744 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000745 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000746 }
747
748 } else {
749 // If this is the second time we see a virtual register definition, it
750 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000751 // the result of two address elimination, then the vreg is one of the
752 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000753 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000754 // If this is a two-address definition, then we have already processed
755 // the live range. The only problem is that we didn't realize there
756 // are actually two values in the live interval. Because of this we
757 // need to take the LiveRegion that defines this register and split it
758 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000759 assert(interval.containsOneValue());
Lang Hames86511252009-09-04 20:41:11 +0000760 MachineInstrIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
761 MachineInstrIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000762 if (MO.isEarlyClobber())
763 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000764
Lang Hames35f291d2009-09-12 03:34:03 +0000765 const LiveRange *OldLR =
766 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000767 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000768
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000769 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000770 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000771 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000772
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000773 // Two-address vregs should always only be redefined once. This means
774 // that at this point, there should be exactly one value number in it.
775 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
776
Chris Lattner91725b72006-08-31 05:54:43 +0000777 // The new value number (#1) is defined by the instruction we claimed
778 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000779 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000780 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000781 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000782 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
783
Chris Lattner91725b72006-08-31 05:54:43 +0000784 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000785 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000786 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000787 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000788 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000789
790 // Add the new live interval which replaces the range for the input copy.
791 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000792 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000793 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000794 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000795
796 // If this redefinition is dead, we need to add a dummy unit live
797 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000798 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000799 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000800 LiveRange(RedefIndex, MO.isEarlyClobber() ?
801 getNextSlot(getNextSlot(RedefIndex)) :
802 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000803
Bill Wendling8e6179f2009-08-22 20:18:03 +0000804 DEBUG({
805 errs() << " RESULT: ";
806 interval.print(errs(), tri_);
807 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000808 } else {
809 // Otherwise, this must be because of phi elimination. If this is the
810 // first redefinition of the vreg that we have seen, go back and change
811 // the live range in the PHI block to be a different value number.
812 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000813 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000814 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000815 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000816 phiJoinCopies.push_back(Killer);
Lang Hames86511252009-09-04 20:41:11 +0000817 MachineInstrIndex Start = getMBBStartIdx(Killer->getParent());
Evan Cheng21731112009-09-12 02:01:07 +0000818 MachineInstrIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000819 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000820 DEBUG({
821 errs() << " Removing [" << Start << "," << End << "] from: ";
822 interval.print(errs(), tri_);
823 errs() << "\n";
824 });
Lang Hamesffd13262009-07-09 03:57:02 +0000825 interval.removeRange(Start, End);
826 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000827 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000828 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
829 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000830 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000831 DEBUG({
832 errs() << " RESULT: ";
833 interval.print(errs(), tri_);
834 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000835
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000836 // Replace the interval with one of a NEW value number. Note that this
837 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000838 LiveRange LR(Start, End,
Lang Hames86511252009-09-04 20:41:11 +0000839 interval.getNextValue(MachineInstrIndex(mbb->getNumber()),
840 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000841 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000842 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000843 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000844 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000845 DEBUG({
846 errs() << " RESULT: ";
847 interval.print(errs(), tri_);
848 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000849 }
850
851 // In the case of PHI elimination, each variable definition is only
852 // live until the end of the block. We've already taken care of the
853 // rest of the live range.
Lang Hames86511252009-09-04 20:41:11 +0000854 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000855 if (MO.isEarlyClobber())
856 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000857
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000858 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000859 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000860 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000861 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000862 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000863 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000864 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000865 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000866 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000867
Lang Hames35f291d2009-09-12 03:34:03 +0000868 MachineInstrIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000869 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000870 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000871 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000872 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000873 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000874 }
875 }
876
Bill Wendling8e6179f2009-08-22 20:18:03 +0000877 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000878}
879
Chris Lattnerf35fef72004-07-23 21:24:19 +0000880void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000881 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000882 MachineInstrIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000883 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000884 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000885 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000886 // A physical register cannot be live across basic block, so its
887 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000888 DEBUG({
889 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000890 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000891 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000892
Lang Hames86511252009-09-04 20:41:11 +0000893 MachineInstrIndex baseIndex = MIIdx;
894 MachineInstrIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000895 // Earlyclobbers move back one.
896 if (MO.isEarlyClobber())
897 start = getUseIndex(MIIdx);
Lang Hames86511252009-09-04 20:41:11 +0000898 MachineInstrIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000899
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000900 // If it is not used after definition, it is considered dead at
901 // the instruction defining it. Hence its interval is:
902 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000903 // For earlyclobbers, the defSlot was pushed back one; the extra
904 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000905 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000906 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000907 if (MO.isEarlyClobber())
908 end = getNextSlot(getNextSlot(start));
909 else
910 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000911 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000912 }
913
914 // If it is not dead on definition, it must be killed by a
915 // subsequent instruction. Hence its interval is:
916 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000917 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000918 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000919 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000920 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000921 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000922 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000923 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000924 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000925 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000926 } else {
927 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
928 if (DefIdx != -1) {
929 if (mi->isRegTiedToUseOperand(DefIdx)) {
930 // Two-address instruction.
931 end = getDefIndex(baseIndex);
932 if (mi->getOperand(DefIdx).isEarlyClobber())
933 end = getUseIndex(baseIndex);
934 } else {
935 // Another instruction redefines the register before it is ever read.
936 // Then the register is essentially dead at the instruction that defines
937 // it. Hence its interval is:
938 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000939 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000940 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000941 }
942 goto exit;
943 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000944 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000945
Lang Hames35f291d2009-09-12 03:34:03 +0000946 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000947 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000948
949 // The only case we should have a dead physreg here without a killing or
950 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000951 // and never used. Another possible case is the implicit use of the
952 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000953 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000954
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000955exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000956 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000957
Evan Cheng24a3cc42007-04-25 07:30:23 +0000958 // Already exists? Extend old live interval.
959 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000960 bool Extend = OldLR != interval.end();
961 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000962 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000963 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000964 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000965 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000966 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000967 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000968 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000969}
970
Chris Lattnerf35fef72004-07-23 21:24:19 +0000971void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
972 MachineBasicBlock::iterator MI,
Lang Hames86511252009-09-04 20:41:11 +0000973 MachineInstrIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000974 MachineOperand& MO,
975 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000976 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000977 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000978 getOrCreateInterval(MO.getReg()));
979 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000980 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000981 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000982 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000983 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000984 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000985 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000986 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000987 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000988 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000989 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000990 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000991 // If MI also modifies the sub-register explicitly, avoid processing it
992 // more than once. Do not pass in TRI here so it checks for exact match.
993 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000994 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000995 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000996 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000997}
998
Evan Chengb371f452007-02-19 21:49:54 +0000999void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames86511252009-09-04 20:41:11 +00001000 MachineInstrIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001001 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001002 DEBUG({
1003 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001004 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001005 });
Evan Chengb371f452007-02-19 21:49:54 +00001006
1007 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1008 // be considered a livein.
1009 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames86511252009-09-04 20:41:11 +00001010 MachineInstrIndex baseIndex = MIIdx;
1011 MachineInstrIndex start = baseIndex;
1012 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001013 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001014 baseIndex = getNextIndex(baseIndex);
Lang Hames86511252009-09-04 20:41:11 +00001015 MachineInstrIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001016 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001017
Evan Chengb371f452007-02-19 21:49:54 +00001018 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001019 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001020 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001021 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001022 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001023 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001024 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001025 // Another instruction redefines the register before it is ever read.
1026 // Then the register is essentially dead at the instruction that defines
1027 // it. Hence its interval is:
1028 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001029 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001030 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001031 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001032 break;
Evan Chengb371f452007-02-19 21:49:54 +00001033 }
1034
Lang Hames35f291d2009-09-12 03:34:03 +00001035 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001036 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001037 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001038 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001039 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001040 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001041 }
Evan Chengb371f452007-02-19 21:49:54 +00001042 }
1043
Evan Cheng75611fb2007-06-27 01:16:36 +00001044 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001045 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001046 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001047 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001048 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001049 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001050 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001051 end = baseIndex;
1052 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001053 }
1054
Lang Hames10382fb2009-06-19 02:17:53 +00001055 VNInfo *vni =
Lang Hames86511252009-09-04 20:41:11 +00001056 interval.getNextValue(MachineInstrIndex(MBB->getNumber()),
1057 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001058 vni->setIsPHIDef(true);
1059 LiveRange LR(start, end, vni);
1060
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001061 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001062 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001063 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001064}
1065
Evan Cheng752195e2009-09-14 21:33:42 +00001066bool
1067LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1068 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001069 SmallVector<MachineInstr*,16> &OtherCopies) {
1070 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001071 unsigned NumIdent = 0;
Evan Cheng752195e2009-09-14 21:33:42 +00001072 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(SrcInt.reg),
1073 re = mri_->reg_end(); ri != re; ++ri) {
1074 MachineOperand &O = ri.getOperand();
1075 if (!O.isDef())
1076 continue;
1077
Evan Cheng752195e2009-09-14 21:33:42 +00001078 MachineInstr *MI = &*ri;
1079 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1080 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001081 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001082 if (SrcReg != DstInt.reg) {
1083 OtherCopies.push_back(MI);
1084 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1085 } else {
1086 IdentCopies.push_back(MI);
1087 ++NumIdent;
1088 }
1089 }
1090
Evan Cheng3f855492009-09-15 06:45:16 +00001091 if (!HaveConflict)
1092 return false; // Let coalescer handle it
1093 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001094}
1095
1096void LiveIntervals::performEarlyCoalescing() {
1097 if (!EarlyCoalescing)
1098 return;
1099
1100 /// Perform early coalescing: eliminate copies which feed into phi joins
1101 /// and whose sources are defined by the phi joins.
1102 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1103 MachineInstr *Join = phiJoinCopies[i];
1104 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1105 break;
1106
1107 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1108 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1109#ifndef NDEBUG
1110 assert(isMove && "PHI join instruction must be a move!");
1111#else
1112 isMove = isMove;
1113#endif
1114
1115 LiveInterval &DstInt = getInterval(PHIDst);
1116 LiveInterval &SrcInt = getInterval(PHISrc);
1117 SmallVector<MachineInstr*, 16> IdentCopies;
1118 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001119 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001120 continue;
1121
1122 DEBUG(errs() << "PHI Join: " << *Join);
1123 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1124 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001125
Evan Cheng3f855492009-09-15 06:45:16 +00001126 // Change the non-identity copies to directly target the phi destination.
1127 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1128 MachineInstr *PHICopy = OtherCopies[i];
1129 DEBUG(errs() << "Moving: " << *PHICopy);
1130
Evan Cheng752195e2009-09-14 21:33:42 +00001131 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1132 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1133 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001134 MachineInstrIndex StartIndex = SLR->start;
Evan Cheng752195e2009-09-14 21:33:42 +00001135 MachineInstrIndex EndIndex = SLR->end;
1136
1137 // Delete val# defined by the now identity copy and add the range from
1138 // beginning of the mbb to the end of the range.
1139 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001140 DEBUG(errs() << " added range [" << StartIndex << ','
1141 << EndIndex << "] to reg" << DstInt.reg << '\n');
1142 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001143 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001144 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1145 VNInfoAllocator);
1146 NewVNI->setHasPHIKill(true);
1147 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1148 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1149 MachineOperand &MO = PHICopy->getOperand(j);
1150 if (!MO.isReg() || MO.getReg() != PHISrc)
1151 continue;
1152 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001153 }
Evan Cheng3f855492009-09-15 06:45:16 +00001154 }
1155
1156 // Now let's eliminate all the would-be identity copies.
1157 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1158 MachineInstr *PHICopy = IdentCopies[i];
1159 DEBUG(errs() << "Coalescing: " << *PHICopy);
1160
1161 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1162 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1163 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
1164 MachineInstrIndex StartIndex = SLR->start;
1165 MachineInstrIndex EndIndex = SLR->end;
1166
1167 // Delete val# defined by the now identity copy and add the range from
1168 // beginning of the mbb to the end of the range.
1169 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001170 RemoveMachineInstrFromMaps(PHICopy);
1171 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001172 DEBUG(errs() << " added range [" << StartIndex << ','
1173 << EndIndex << "] to reg" << DstInt.reg << '\n');
1174 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001175 }
Evan Cheng752195e2009-09-14 21:33:42 +00001176
Evan Cheng3f855492009-09-15 06:45:16 +00001177 // Remove the phi join and update the phi block liveness.
1178 MachineInstrIndex MIIndex = getInstructionIndex(Join);
1179 MachineInstrIndex UseIndex = getUseIndex(MIIndex);
1180 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1181 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1182 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1183 DLR->valno->setCopy(0);
1184 DLR->valno->setIsDefAccurate(false);
1185 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1186 SrcInt.removeRange(SLR->start, SLR->end);
1187 assert(SrcInt.empty());
1188 removeInterval(PHISrc);
1189 RemoveMachineInstrFromMaps(Join);
1190 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001191
1192 ++numCoalescing;
1193 }
1194}
1195
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001196/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001197/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001198/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001199/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001200void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001201 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001202 << "********** Function: "
1203 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001204
1205 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001206 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1207 MBBI != E; ++MBBI) {
1208 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001209 // Track the index of the current machine instr.
Lang Hames86511252009-09-04 20:41:11 +00001210 MachineInstrIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001211 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001212
Chris Lattner428b92e2006-09-15 03:57:23 +00001213 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001214
Dan Gohmancb406c22007-10-03 19:26:29 +00001215 // Create intervals for live-ins to this BB first.
1216 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1217 LE = MBB->livein_end(); LI != LE; ++LI) {
1218 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1219 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001220 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001221 if (!hasInterval(*AS))
1222 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1223 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001224 }
1225
Owen Anderson99500ae2008-09-15 22:00:38 +00001226 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001227 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001228 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001229 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001230
Chris Lattner428b92e2006-09-15 03:57:23 +00001231 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001232 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001233
Evan Cheng438f7bc2006-11-10 08:43:01 +00001234 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001235 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1236 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001237 if (!MO.isReg() || !MO.getReg())
1238 continue;
1239
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001240 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001241 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001242 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001243 else if (MO.isUndef())
1244 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001245 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001246
1247 // Skip over the empty slots after each instruction.
1248 unsigned Slots = MI->getDesc().getNumDefs();
1249 if (Slots == 0)
1250 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001251
1252 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001253 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001254
1255 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001256 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001257 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001258 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001259 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001260 }
Evan Chengd129d732009-07-17 19:43:40 +00001261
1262 // Create empty intervals for registers defined by implicit_def's (except
1263 // for those implicit_def that define values which are liveout of their
1264 // blocks.
1265 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1266 unsigned UndefReg = UndefUses[i];
1267 (void)getOrCreateInterval(UndefReg);
1268 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001269}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001270
Lang Hames86511252009-09-04 20:41:11 +00001271bool LiveIntervals::findLiveInMBBs(
1272 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001273 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001274 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001275 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001276
1277 bool ResVal = false;
1278 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001279 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001280 break;
1281 MBBs.push_back(I->second);
1282 ResVal = true;
1283 ++I;
1284 }
1285 return ResVal;
1286}
1287
Lang Hames86511252009-09-04 20:41:11 +00001288bool LiveIntervals::findReachableMBBs(
1289 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001290 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1291 std::vector<IdxMBBPair>::const_iterator I =
1292 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1293
1294 bool ResVal = false;
1295 while (I != Idx2MBBMap.end()) {
1296 if (I->first > End)
1297 break;
1298 MachineBasicBlock *MBB = I->second;
1299 if (getMBBEndIdx(MBB) > End)
1300 break;
1301 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1302 SE = MBB->succ_end(); SI != SE; ++SI)
1303 MBBs.push_back(*SI);
1304 ResVal = true;
1305 ++I;
1306 }
1307 return ResVal;
1308}
1309
Owen Anderson03857b22008-08-13 21:49:13 +00001310LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001311 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001312 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001313}
Evan Chengf2fbca62007-11-12 06:35:08 +00001314
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001315/// dupInterval - Duplicate a live interval. The caller is responsible for
1316/// managing the allocated memory.
1317LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1318 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001319 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001320 return NewLI;
1321}
1322
Evan Chengc8d044e2008-02-15 18:24:29 +00001323/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1324/// copy field and returns the source register that defines it.
1325unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001326 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001327 return 0;
1328
Lang Hames52c1afc2009-08-10 23:43:28 +00001329 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001330 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001331 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001332 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001333 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001334 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001335 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1336 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1337 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001338
Evan Cheng04ee5a12009-01-20 19:12:24 +00001339 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001340 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001341 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001342 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001343 return 0;
1344}
Evan Chengf2fbca62007-11-12 06:35:08 +00001345
1346//===----------------------------------------------------------------------===//
1347// Register allocator hooks.
1348//
1349
Evan Chengd70dbb52008-02-22 09:24:50 +00001350/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1351/// allow one) virtual register operand, then its uses are implicitly using
1352/// the register. Returns the virtual register.
1353unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1354 MachineInstr *MI) const {
1355 unsigned RegOp = 0;
1356 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1357 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001358 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001359 continue;
1360 unsigned Reg = MO.getReg();
1361 if (Reg == 0 || Reg == li.reg)
1362 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001363
1364 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1365 !allocatableRegs_[Reg])
1366 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001367 // FIXME: For now, only remat MI with at most one register operand.
1368 assert(!RegOp &&
1369 "Can't rematerialize instruction with multiple register operand!");
1370 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001371#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001372 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001373#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001374 }
1375 return RegOp;
1376}
1377
1378/// isValNoAvailableAt - Return true if the val# of the specified interval
1379/// which reaches the given instruction also reaches the specified use index.
1380bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +00001381 MachineInstrIndex UseIdx) const {
1382 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001383 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1384 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1385 return UI != li.end() && UI->valno == ValNo;
1386}
1387
Evan Chengf2fbca62007-11-12 06:35:08 +00001388/// isReMaterializable - Returns true if the definition MI of the specified
1389/// val# of the specified interval is re-materializable.
1390bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001391 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001392 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001393 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001394 if (DisableReMat)
1395 return false;
1396
Evan Cheng20ccded2008-03-15 00:19:36 +00001397 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001398 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001399
1400 int FrameIdx = 0;
1401 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001402 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001403 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1404 // this but remember this is not safe to fold into a two-address
1405 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001406 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001407 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001408
Dan Gohman6d69ba82008-07-25 00:02:30 +00001409 // If the target-specific rules don't identify an instruction as
1410 // being trivially rematerializable, use some target-independent
1411 // rules.
1412 if (!MI->getDesc().isRematerializable() ||
1413 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001414 if (!EnableAggressiveRemat)
1415 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001416
Dan Gohman0471a792008-07-28 18:43:51 +00001417 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001418 // we can't analyze it.
1419 const TargetInstrDesc &TID = MI->getDesc();
1420 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1421 return false;
1422
1423 // Avoid instructions obviously unsafe for remat.
1424 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1425 return false;
1426
1427 // If the instruction accesses memory and the memory could be non-constant,
1428 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001429 for (std::list<MachineMemOperand>::const_iterator
1430 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001431 const MachineMemOperand &MMO = *I;
1432 if (MMO.isVolatile() || MMO.isStore())
1433 return false;
1434 const Value *V = MMO.getValue();
1435 if (!V)
1436 return false;
1437 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1438 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001439 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001440 } else if (!aa_->pointsToConstantMemory(V))
1441 return false;
1442 }
1443
1444 // If any of the registers accessed are non-constant, conservatively assume
1445 // the instruction is not rematerializable.
1446 unsigned ImpUse = 0;
1447 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1448 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001449 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001450 unsigned Reg = MO.getReg();
1451 if (Reg == 0)
1452 continue;
1453 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1454 return false;
1455
1456 // Only allow one def, and that in the first operand.
1457 if (MO.isDef() != (i == 0))
1458 return false;
1459
1460 // Only allow constant-valued registers.
1461 bool IsLiveIn = mri_->isLiveIn(Reg);
1462 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1463 E = mri_->def_end();
1464
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001465 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001466 if (MO.isDef() && (next(I) != E || IsLiveIn))
1467 return false;
1468
1469 if (MO.isUse()) {
1470 // Only allow one use other register use, as that's all the
1471 // remat mechanisms support currently.
1472 if (Reg != li.reg) {
1473 if (ImpUse == 0)
1474 ImpUse = Reg;
1475 else if (Reg != ImpUse)
1476 return false;
1477 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001478 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001479 if (I != E && (next(I) != E || IsLiveIn))
1480 return false;
1481 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001482 }
1483 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001484 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001485
Dan Gohman6d69ba82008-07-25 00:02:30 +00001486 unsigned ImpUse = getReMatImplicitUse(li, MI);
1487 if (ImpUse) {
1488 const LiveInterval &ImpLi = getInterval(ImpUse);
1489 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1490 re = mri_->use_end(); ri != re; ++ri) {
1491 MachineInstr *UseMI = &*ri;
Lang Hames86511252009-09-04 20:41:11 +00001492 MachineInstrIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001493 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1494 continue;
1495 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1496 return false;
1497 }
Evan Chengdc377862008-09-30 15:44:16 +00001498
1499 // If a register operand of the re-materialized instruction is going to
1500 // be spilled next, then it's not legal to re-materialize this instruction.
1501 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1502 if (ImpUse == SpillIs[i]->reg)
1503 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001504 }
1505 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001506}
1507
Evan Cheng06587492008-10-24 02:05:00 +00001508/// isReMaterializable - Returns true if the definition MI of the specified
1509/// val# of the specified interval is re-materializable.
1510bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1511 const VNInfo *ValNo, MachineInstr *MI) {
1512 SmallVector<LiveInterval*, 4> Dummy1;
1513 bool Dummy2;
1514 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1515}
1516
Evan Cheng5ef3a042007-12-06 00:01:56 +00001517/// isReMaterializable - Returns true if every definition of MI of every
1518/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001519bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1520 SmallVectorImpl<LiveInterval*> &SpillIs,
1521 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001522 isLoad = false;
1523 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1524 i != e; ++i) {
1525 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001526 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001527 continue; // Dead val#.
1528 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001529 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001530 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001531 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001532 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001533 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001534 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001535 return false;
1536 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001537 }
1538 return true;
1539}
1540
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001541/// FilterFoldedOps - Filter out two-address use operands. Return
1542/// true if it finds any issue with the operands that ought to prevent
1543/// folding.
1544static bool FilterFoldedOps(MachineInstr *MI,
1545 SmallVector<unsigned, 2> &Ops,
1546 unsigned &MRInfo,
1547 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001548 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001549 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1550 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001551 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001552 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001553 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001554 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001555 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001556 MRInfo |= (unsigned)VirtRegMap::isMod;
1557 else {
1558 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001559 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001560 MRInfo = VirtRegMap::isModRef;
1561 continue;
1562 }
1563 MRInfo |= (unsigned)VirtRegMap::isRef;
1564 }
1565 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001566 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001567 return false;
1568}
1569
1570
1571/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1572/// slot / to reg or any rematerialized load into ith operand of specified
1573/// MI. If it is successul, MI is updated with the newly created MI and
1574/// returns true.
1575bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1576 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames86511252009-09-04 20:41:11 +00001577 MachineInstrIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001578 SmallVector<unsigned, 2> &Ops,
1579 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001580 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001581 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001582 RemoveMachineInstrFromMaps(MI);
1583 vrm.RemoveMachineInstrFromMaps(MI);
1584 MI->eraseFromParent();
1585 ++numFolds;
1586 return true;
1587 }
1588
1589 // Filter the list of operand indexes that are to be folded. Abort if
1590 // any operand will prevent folding.
1591 unsigned MRInfo = 0;
1592 SmallVector<unsigned, 2> FoldOps;
1593 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1594 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001595
Evan Cheng427f4c12008-03-31 23:19:51 +00001596 // The only time it's safe to fold into a two address instruction is when
1597 // it's folding reload and spill from / into a spill stack slot.
1598 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001599 return false;
1600
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001601 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1602 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001603 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001604 // Remember this instruction uses the spill slot.
1605 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1606
Evan Chengf2fbca62007-11-12 06:35:08 +00001607 // Attempt to fold the memory reference into the instruction. If
1608 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001609 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001610 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001611 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001612 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001613 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001614 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001615 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001616 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001617 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001618 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001619 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001620 return true;
1621 }
1622 return false;
1623}
1624
Evan Cheng018f9b02007-12-05 03:22:34 +00001625/// canFoldMemoryOperand - Returns true if the specified load / store
1626/// folding is possible.
1627bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001628 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001629 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001630 // Filter the list of operand indexes that are to be folded. Abort if
1631 // any operand will prevent folding.
1632 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001633 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001634 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1635 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001636
Evan Cheng3c75ba82008-04-01 21:37:32 +00001637 // It's only legal to remat for a use, not a def.
1638 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001639 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001640
Evan Chengd70dbb52008-02-22 09:24:50 +00001641 return tii_->canFoldMemoryOperand(MI, FoldOps);
1642}
1643
Evan Cheng81a03822007-11-17 00:40:40 +00001644bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1645 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1646 for (LiveInterval::Ranges::const_iterator
1647 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1648 std::vector<IdxMBBPair>::const_iterator II =
1649 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1650 if (II == Idx2MBBMap.end())
1651 continue;
1652 if (I->end > II->first) // crossing a MBB.
1653 return false;
1654 MBBs.insert(II->second);
1655 if (MBBs.size() > 1)
1656 return false;
1657 }
1658 return true;
1659}
1660
Evan Chengd70dbb52008-02-22 09:24:50 +00001661/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1662/// interval on to-be re-materialized operands of MI) with new register.
1663void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1664 MachineInstr *MI, unsigned NewVReg,
1665 VirtRegMap &vrm) {
1666 // There is an implicit use. That means one of the other operand is
1667 // being remat'ed and the remat'ed instruction has li.reg as an
1668 // use operand. Make sure we rewrite that as well.
1669 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1670 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001671 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001672 continue;
1673 unsigned Reg = MO.getReg();
1674 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1675 continue;
1676 if (!vrm.isReMaterialized(Reg))
1677 continue;
1678 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001679 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1680 if (UseMO)
1681 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001682 }
1683}
1684
Evan Chengf2fbca62007-11-12 06:35:08 +00001685/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1686/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001687bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001688rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001689 bool TrySplit, MachineInstrIndex index, MachineInstrIndex end,
1690 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001691 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001692 unsigned Slot, int LdSlot,
1693 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001694 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001695 const TargetRegisterClass* rc,
1696 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001697 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001698 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001699 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001700 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001701 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001702 RestartInstruction:
1703 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1704 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001705 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001706 continue;
1707 unsigned Reg = mop.getReg();
1708 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001709 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001710 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001711 if (Reg != li.reg)
1712 continue;
1713
1714 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001715 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001716 int FoldSlot = Slot;
1717 if (DefIsReMat) {
1718 // If this is the rematerializable definition MI itself and
1719 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001720 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001721 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1722 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001723 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001724 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001725 MI->eraseFromParent();
1726 break;
1727 }
1728
1729 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001730 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001731 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001732 if (isLoad) {
1733 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1734 FoldSS = isLoadSS;
1735 FoldSlot = LdSlot;
1736 }
1737 }
1738
Evan Chengf2fbca62007-11-12 06:35:08 +00001739 // Scan all of the operands of this instruction rewriting operands
1740 // to use NewVReg instead of li.reg as appropriate. We do this for
1741 // two reasons:
1742 //
1743 // 1. If the instr reads the same spilled vreg multiple times, we
1744 // want to reuse the NewVReg.
1745 // 2. If the instr is a two-addr instruction, we are required to
1746 // keep the src/dst regs pinned.
1747 //
1748 // Keep track of whether we replace a use and/or def so that we can
1749 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001750
Evan Cheng81a03822007-11-17 00:40:40 +00001751 HasUse = mop.isUse();
1752 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001753 SmallVector<unsigned, 2> Ops;
1754 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001755 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001756 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001757 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001758 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001759 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001760 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001761 continue;
1762 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001763 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001764 if (!MOj.isUndef()) {
1765 HasUse |= MOj.isUse();
1766 HasDef |= MOj.isDef();
1767 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001768 }
1769 }
1770
David Greene26b86a02008-10-27 17:38:59 +00001771 // Create a new virtual register for the spill interval.
1772 // Create the new register now so we can map the fold instruction
1773 // to the new register so when it is unfolded we get the correct
1774 // answer.
1775 bool CreatedNewVReg = false;
1776 if (NewVReg == 0) {
1777 NewVReg = mri_->createVirtualRegister(rc);
1778 vrm.grow();
1779 CreatedNewVReg = true;
1780 }
1781
Evan Cheng9c3c2212008-06-06 07:54:39 +00001782 if (!TryFold)
1783 CanFold = false;
1784 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001785 // Do not fold load / store here if we are splitting. We'll find an
1786 // optimal point to insert a load / store later.
1787 if (!TrySplit) {
1788 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001789 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001790 // Folding the load/store can completely change the instruction in
1791 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001792
1793 if (FoldSS) {
1794 // We need to give the new vreg the same stack slot as the
1795 // spilled interval.
1796 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1797 }
1798
Evan Cheng018f9b02007-12-05 03:22:34 +00001799 HasUse = false;
1800 HasDef = false;
1801 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001802 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001803 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001804 goto RestartInstruction;
1805 }
1806 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001807 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001808 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001809 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001810 }
Evan Chengcddbb832007-11-30 21:23:43 +00001811
Evan Chengcddbb832007-11-30 21:23:43 +00001812 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001813 if (mop.isImplicit())
1814 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001815
1816 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001817 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1818 MachineOperand &mopj = MI->getOperand(Ops[j]);
1819 mopj.setReg(NewVReg);
1820 if (mopj.isImplicit())
1821 rewriteImplicitOps(li, MI, NewVReg, vrm);
1822 }
Evan Chengcddbb832007-11-30 21:23:43 +00001823
Evan Cheng81a03822007-11-17 00:40:40 +00001824 if (CreatedNewVReg) {
1825 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001826 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001827 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001828 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001829 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001830 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001831 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001832 }
1833 if (!CanDelete || (HasUse && HasDef)) {
1834 // If this is a two-addr instruction then its use operands are
1835 // rematerializable but its def is not. It should be assigned a
1836 // stack slot.
1837 vrm.assignVirt2StackSlot(NewVReg, Slot);
1838 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001839 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001840 vrm.assignVirt2StackSlot(NewVReg, Slot);
1841 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001842 } else if (HasUse && HasDef &&
1843 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1844 // If this interval hasn't been assigned a stack slot (because earlier
1845 // def is a deleted remat def), do it now.
1846 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1847 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001848 }
1849
Evan Cheng313d4b82008-02-23 00:33:04 +00001850 // Re-matting an instruction with virtual register use. Add the
1851 // register as an implicit use on the use MI.
1852 if (DefIsReMat && ImpUse)
1853 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1854
Evan Cheng5b69eba2009-04-21 22:46:52 +00001855 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001856 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001857 if (CreatedNewVReg) {
1858 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001859 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001860 if (TrySplit)
1861 vrm.setIsSplitFromReg(NewVReg, li.reg);
1862 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001863
1864 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001865 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001866 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hames86511252009-09-04 20:41:11 +00001867 nI.getNextValue(MachineInstrIndex(), 0, false,
1868 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001869 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001870 nI.addRange(LR);
1871 } else {
1872 // Extend the split live interval to this def / use.
Lang Hames35f291d2009-09-12 03:34:03 +00001873 MachineInstrIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001874 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1875 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001876 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001877 nI.addRange(LR);
1878 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001879 }
1880 if (HasDef) {
1881 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00001882 nI.getNextValue(MachineInstrIndex(), 0, false,
1883 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001884 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001885 nI.addRange(LR);
1886 }
Evan Cheng81a03822007-11-17 00:40:40 +00001887
Bill Wendling8e6179f2009-08-22 20:18:03 +00001888 DEBUG({
1889 errs() << "\t\t\t\tAdded new interval: ";
1890 nI.print(errs(), tri_);
1891 errs() << '\n';
1892 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001893 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001894 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001895}
Evan Cheng81a03822007-11-17 00:40:40 +00001896bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001897 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001898 MachineBasicBlock *MBB,
1899 MachineInstrIndex Idx) const {
1900 MachineInstrIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001901 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001902 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001903 continue;
1904
Lang Hames86511252009-09-04 20:41:11 +00001905 MachineInstrIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001906 if (KillIdx > Idx && KillIdx < End)
1907 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001908 }
1909 return false;
1910}
1911
Evan Cheng063284c2008-02-21 00:34:19 +00001912/// RewriteInfo - Keep track of machine instrs that will be rewritten
1913/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001914namespace {
1915 struct RewriteInfo {
Lang Hames86511252009-09-04 20:41:11 +00001916 MachineInstrIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001917 MachineInstr *MI;
1918 bool HasUse;
1919 bool HasDef;
Lang Hames86511252009-09-04 20:41:11 +00001920 RewriteInfo(MachineInstrIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001921 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1922 };
Evan Cheng063284c2008-02-21 00:34:19 +00001923
Dan Gohman844731a2008-05-13 00:00:25 +00001924 struct RewriteInfoCompare {
1925 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1926 return LHS.Index < RHS.Index;
1927 }
1928 };
1929}
Evan Cheng063284c2008-02-21 00:34:19 +00001930
Evan Chengf2fbca62007-11-12 06:35:08 +00001931void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001932rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001933 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001934 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001935 unsigned Slot, int LdSlot,
1936 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001937 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001938 const TargetRegisterClass* rc,
1939 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001940 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001941 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001942 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001943 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001944 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1945 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001946 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001947 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001948 unsigned NewVReg = 0;
Lang Hames86511252009-09-04 20:41:11 +00001949 MachineInstrIndex start = getBaseIndex(I->start);
Lang Hames35f291d2009-09-12 03:34:03 +00001950 MachineInstrIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001951
Evan Cheng063284c2008-02-21 00:34:19 +00001952 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001953 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001954 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001955 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1956 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001957 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001958 MachineOperand &O = ri.getOperand();
1959 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001960 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames86511252009-09-04 20:41:11 +00001961 MachineInstrIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001962 if (index < start || index >= end)
1963 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001964
1965 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001966 // Must be defined by an implicit def. It should not be spilled. Note,
1967 // this is for correctness reason. e.g.
1968 // 8 %reg1024<def> = IMPLICIT_DEF
1969 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1970 // The live range [12, 14) are not part of the r1024 live interval since
1971 // it's defined by an implicit def. It will not conflicts with live
1972 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001973 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001974 // the INSERT_SUBREG and both target registers that would overlap.
1975 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001976 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1977 }
1978 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1979
Evan Cheng313d4b82008-02-23 00:33:04 +00001980 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001981 // Now rewrite the defs and uses.
1982 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1983 RewriteInfo &rwi = RewriteMIs[i];
1984 ++i;
Lang Hames86511252009-09-04 20:41:11 +00001985 MachineInstrIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001986 bool MIHasUse = rwi.HasUse;
1987 bool MIHasDef = rwi.HasDef;
1988 MachineInstr *MI = rwi.MI;
1989 // If MI def and/or use the same register multiple times, then there
1990 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001991 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001992 while (i != e && RewriteMIs[i].MI == MI) {
1993 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001994 bool isUse = RewriteMIs[i].HasUse;
1995 if (isUse) ++NumUses;
1996 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001997 MIHasDef |= RewriteMIs[i].HasDef;
1998 ++i;
1999 }
Evan Cheng81a03822007-11-17 00:40:40 +00002000 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00002001
Evan Cheng0a891ed2008-05-23 23:00:04 +00002002 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00002003 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002004 // register interval's spill weight to HUGE_VALF to prevent it from
2005 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00002006 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002007 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00002008 }
2009
Evan Cheng063284c2008-02-21 00:34:19 +00002010 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00002011 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00002012 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00002013 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002014 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00002015 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002016 // One common case:
2017 // x = use
2018 // ...
2019 // ...
2020 // def = ...
2021 // = use
2022 // It's better to start a new interval to avoid artifically
2023 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002024 if (MIHasDef && !MIHasUse) {
2025 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00002026 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002027 }
2028 }
Evan Chengcada2452007-11-28 01:28:46 +00002029 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002030
2031 bool IsNew = ThisVReg == 0;
2032 if (IsNew) {
2033 // This ends the previous live interval. If all of its def / use
2034 // can be folded, give it a low spill weight.
2035 if (NewVReg && TrySplit && AllCanFold) {
2036 LiveInterval &nI = getOrCreateInterval(NewVReg);
2037 nI.weight /= 10.0F;
2038 }
2039 AllCanFold = true;
2040 }
2041 NewVReg = ThisVReg;
2042
Evan Cheng81a03822007-11-17 00:40:40 +00002043 bool HasDef = false;
2044 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00002045 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00002046 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
2047 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
2048 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00002049 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002050 if (!HasDef && !HasUse)
2051 continue;
2052
Evan Cheng018f9b02007-12-05 03:22:34 +00002053 AllCanFold &= CanFold;
2054
Evan Cheng81a03822007-11-17 00:40:40 +00002055 // Update weight of spill interval.
2056 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00002057 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00002058 // The spill weight is now infinity as it cannot be spilled again.
2059 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002060 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00002061 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002062
2063 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00002064 if (HasDef) {
2065 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002066 bool HasKill = false;
2067 if (!HasUse)
2068 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
2069 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002070 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00002071 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002072 if (VNI)
2073 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2074 }
Owen Anderson28998312008-08-13 22:28:50 +00002075 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002076 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002077 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002078 if (SII == SpillIdxes.end()) {
2079 std::vector<SRInfo> S;
2080 S.push_back(SRInfo(index, NewVReg, true));
2081 SpillIdxes.insert(std::make_pair(MBBId, S));
2082 } else if (SII->second.back().vreg != NewVReg) {
2083 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002084 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002085 // If there is an earlier def and this is a two-address
2086 // instruction, then it's not possible to fold the store (which
2087 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002088 SRInfo &Info = SII->second.back();
2089 Info.index = index;
2090 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002091 }
2092 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002093 } else if (SII != SpillIdxes.end() &&
2094 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002095 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002096 // There is an earlier def that's not killed (must be two-address).
2097 // The spill is no longer needed.
2098 SII->second.pop_back();
2099 if (SII->second.empty()) {
2100 SpillIdxes.erase(MBBId);
2101 SpillMBBs.reset(MBBId);
2102 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002103 }
2104 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002105 }
2106
2107 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002108 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002109 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002110 if (SII != SpillIdxes.end() &&
2111 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002112 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002113 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002114 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002115 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002116 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002117 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002118 // If we are splitting live intervals, only fold if it's the first
2119 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002120 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002121 else if (IsNew) {
2122 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002123 if (RII == RestoreIdxes.end()) {
2124 std::vector<SRInfo> Infos;
2125 Infos.push_back(SRInfo(index, NewVReg, true));
2126 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2127 } else {
2128 RII->second.push_back(SRInfo(index, NewVReg, true));
2129 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002130 RestoreMBBs.set(MBBId);
2131 }
2132 }
2133
2134 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002135 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002136 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002137 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002138
2139 if (NewVReg && TrySplit && AllCanFold) {
2140 // If all of its def / use can be folded, give it a low spill weight.
2141 LiveInterval &nI = getOrCreateInterval(NewVReg);
2142 nI.weight /= 10.0F;
2143 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002144}
2145
Lang Hames86511252009-09-04 20:41:11 +00002146bool LiveIntervals::alsoFoldARestore(int Id, MachineInstrIndex index,
2147 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002148 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002149 if (!RestoreMBBs[Id])
2150 return false;
2151 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2152 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2153 if (Restores[i].index == index &&
2154 Restores[i].vreg == vr &&
2155 Restores[i].canFold)
2156 return true;
2157 return false;
2158}
2159
Lang Hames86511252009-09-04 20:41:11 +00002160void LiveIntervals::eraseRestoreInfo(int Id, MachineInstrIndex index,
2161 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002162 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002163 if (!RestoreMBBs[Id])
2164 return;
2165 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2166 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2167 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames86511252009-09-04 20:41:11 +00002168 Restores[i].index = MachineInstrIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002169}
Evan Cheng81a03822007-11-17 00:40:40 +00002170
Evan Cheng4cce6b42008-04-11 17:53:36 +00002171/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2172/// spilled and create empty intervals for their uses.
2173void
2174LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2175 const TargetRegisterClass* rc,
2176 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002177 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2178 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002179 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002180 MachineInstr *MI = &*ri;
2181 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002182 if (O.isDef()) {
2183 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2184 "Register def was not rewritten?");
2185 RemoveMachineInstrFromMaps(MI);
2186 vrm.RemoveMachineInstrFromMaps(MI);
2187 MI->eraseFromParent();
2188 } else {
2189 // This must be an use of an implicit_def so it's not part of the live
2190 // interval. Create a new empty live interval for it.
2191 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2192 unsigned NewVReg = mri_->createVirtualRegister(rc);
2193 vrm.grow();
2194 vrm.setIsImplicitlyDefined(NewVReg);
2195 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2196 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2197 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002198 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002199 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002200 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002201 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002202 }
2203 }
Evan Cheng419852c2008-04-03 16:39:43 +00002204 }
2205}
2206
Evan Chengf2fbca62007-11-12 06:35:08 +00002207std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002208addIntervalsForSpillsFast(const LiveInterval &li,
2209 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002210 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002211 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002212
2213 std::vector<LiveInterval*> added;
2214
2215 assert(li.weight != HUGE_VALF &&
2216 "attempt to spill already spilled interval!");
2217
Bill Wendling8e6179f2009-08-22 20:18:03 +00002218 DEBUG({
2219 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2220 li.dump();
2221 errs() << '\n';
2222 });
Owen Andersond6664312008-08-18 18:05:32 +00002223
2224 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2225
Owen Andersona41e47a2008-08-19 22:12:11 +00002226 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2227 while (RI != mri_->reg_end()) {
2228 MachineInstr* MI = &*RI;
2229
2230 SmallVector<unsigned, 2> Indices;
2231 bool HasUse = false;
2232 bool HasDef = false;
2233
2234 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2235 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002236 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002237
2238 HasUse |= MI->getOperand(i).isUse();
2239 HasDef |= MI->getOperand(i).isDef();
2240
2241 Indices.push_back(i);
2242 }
2243
2244 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2245 Indices, true, slot, li.reg)) {
2246 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002247 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002248 vrm.assignVirt2StackSlot(NewVReg, slot);
2249
Owen Andersona41e47a2008-08-19 22:12:11 +00002250 // create a new register for this spill
2251 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002252
Owen Andersona41e47a2008-08-19 22:12:11 +00002253 // the spill weight is now infinity as it
2254 // cannot be spilled again
2255 nI.weight = HUGE_VALF;
2256
2257 // Rewrite register operands to use the new vreg.
2258 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2259 E = Indices.end(); I != E; ++I) {
2260 MI->getOperand(*I).setReg(NewVReg);
2261
2262 if (MI->getOperand(*I).isUse())
2263 MI->getOperand(*I).setIsKill(true);
2264 }
2265
2266 // Fill in the new live interval.
Lang Hames86511252009-09-04 20:41:11 +00002267 MachineInstrIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002268 if (HasUse) {
2269 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002270 nI.getNextValue(MachineInstrIndex(), 0, false,
2271 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002272 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002273 nI.addRange(LR);
2274 vrm.addRestorePoint(NewVReg, MI);
2275 }
2276 if (HasDef) {
2277 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002278 nI.getNextValue(MachineInstrIndex(), 0, false,
2279 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002280 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002281 nI.addRange(LR);
2282 vrm.addSpillPoint(NewVReg, true, MI);
2283 }
2284
Owen Anderson17197312008-08-18 23:41:04 +00002285 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002286
Bill Wendling8e6179f2009-08-22 20:18:03 +00002287 DEBUG({
2288 errs() << "\t\t\t\tadded new interval: ";
2289 nI.dump();
2290 errs() << '\n';
2291 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002292 }
Owen Anderson9a032932008-08-18 21:20:32 +00002293
Owen Anderson9a032932008-08-18 21:20:32 +00002294
Owen Andersona41e47a2008-08-19 22:12:11 +00002295 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002296 }
Owen Andersond6664312008-08-18 18:05:32 +00002297
2298 return added;
2299}
2300
2301std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002302addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002303 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002304 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002305
2306 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002307 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002308
Evan Chengf2fbca62007-11-12 06:35:08 +00002309 assert(li.weight != HUGE_VALF &&
2310 "attempt to spill already spilled interval!");
2311
Bill Wendling8e6179f2009-08-22 20:18:03 +00002312 DEBUG({
2313 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2314 li.print(errs(), tri_);
2315 errs() << '\n';
2316 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002317
Evan Cheng72eeb942008-12-05 17:00:16 +00002318 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002319 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002320 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002321 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002322 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2323 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002324 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002325 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002326
2327 unsigned NumValNums = li.getNumValNums();
2328 SmallVector<MachineInstr*, 4> ReMatDefs;
2329 ReMatDefs.resize(NumValNums, NULL);
2330 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2331 ReMatOrigDefs.resize(NumValNums, NULL);
2332 SmallVector<int, 4> ReMatIds;
2333 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2334 BitVector ReMatDelete(NumValNums);
2335 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2336
Evan Cheng81a03822007-11-17 00:40:40 +00002337 // Spilling a split live interval. It cannot be split any further. Also,
2338 // it's also guaranteed to be a single val# / range interval.
2339 if (vrm.getPreSplitReg(li.reg)) {
2340 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002341 // Unset the split kill marker on the last use.
Lang Hames86511252009-09-04 20:41:11 +00002342 MachineInstrIndex KillIdx = vrm.getKillPoint(li.reg);
2343 if (KillIdx != MachineInstrIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002344 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2345 assert(KillMI && "Last use disappeared?");
2346 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2347 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002348 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002349 }
Evan Chengadf85902007-12-05 09:51:10 +00002350 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002351 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2352 Slot = vrm.getStackSlot(li.reg);
2353 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2354 MachineInstr *ReMatDefMI = DefIsReMat ?
2355 vrm.getReMaterializedMI(li.reg) : NULL;
2356 int LdSlot = 0;
2357 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2358 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002359 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002360 bool IsFirstRange = true;
2361 for (LiveInterval::Ranges::const_iterator
2362 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2363 // If this is a split live interval with multiple ranges, it means there
2364 // are two-address instructions that re-defined the value. Only the
2365 // first def can be rematerialized!
2366 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002367 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002368 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2369 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002370 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002371 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002372 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002373 } else {
2374 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2375 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002376 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002377 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002378 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002379 }
2380 IsFirstRange = false;
2381 }
Evan Cheng419852c2008-04-03 16:39:43 +00002382
Evan Cheng4cce6b42008-04-11 17:53:36 +00002383 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002384 return NewLIs;
2385 }
2386
Evan Cheng752195e2009-09-14 21:33:42 +00002387 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002388 if (TrySplit)
2389 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002390 bool NeedStackSlot = false;
2391 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2392 i != e; ++i) {
2393 const VNInfo *VNI = *i;
2394 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002395 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002396 continue; // Dead val#.
2397 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002398 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2399 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002400 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002401 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002402 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002403 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002404 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002405 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002406 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002407 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002408
2409 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002410 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002411 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002412 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002413 CanDelete = false;
2414 // Need a stack slot if there is any live range where uses cannot be
2415 // rematerialized.
2416 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002417 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002418 if (CanDelete)
2419 ReMatDelete.set(VN);
2420 } else {
2421 // Need a stack slot if there is any live range where uses cannot be
2422 // rematerialized.
2423 NeedStackSlot = true;
2424 }
2425 }
2426
2427 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002428 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2429 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2430 Slot = vrm.assignVirt2StackSlot(li.reg);
2431
2432 // This case only occurs when the prealloc splitter has already assigned
2433 // a stack slot to this vreg.
2434 else
2435 Slot = vrm.getStackSlot(li.reg);
2436 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002437
2438 // Create new intervals and rewrite defs and uses.
2439 for (LiveInterval::Ranges::const_iterator
2440 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002441 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2442 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2443 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002444 bool CanDelete = ReMatDelete[I->valno->id];
2445 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002446 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002447 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002448 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002449 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002450 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002451 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002452 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002453 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002454 }
2455
Evan Cheng0cbb1162007-11-29 01:06:25 +00002456 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002457 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002458 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002459 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002460 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002461
Evan Chengb50bb8c2007-12-05 08:16:32 +00002462 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002463 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002464 if (NeedStackSlot) {
2465 int Id = SpillMBBs.find_first();
2466 while (Id != -1) {
2467 std::vector<SRInfo> &spills = SpillIdxes[Id];
2468 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002469 MachineInstrIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002470 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002471 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002472 bool isReMat = vrm.isReMaterialized(VReg);
2473 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002474 bool CanFold = false;
2475 bool FoundUse = false;
2476 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002477 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002478 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002479 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2480 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002481 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002482 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002483
2484 Ops.push_back(j);
2485 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002486 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002487 if (isReMat ||
2488 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2489 RestoreMBBs, RestoreIdxes))) {
2490 // MI has two-address uses of the same register. If the use
2491 // isn't the first and only use in the BB, then we can't fold
2492 // it. FIXME: Move this to rewriteInstructionsForSpills.
2493 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002494 break;
2495 }
Evan Chengaee4af62007-12-02 08:30:39 +00002496 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002497 }
2498 }
2499 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002500 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002501 if (CanFold && !Ops.empty()) {
2502 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002503 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002504 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002505 // Also folded uses, do not issue a load.
2506 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002507 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002508 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002509 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002510 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002511 }
2512
Evan Cheng7e073ba2008-04-09 20:57:25 +00002513 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002514 if (!Folded) {
2515 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2516 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002517 if (!MI->registerDefIsDead(nI.reg))
2518 // No need to spill a dead def.
2519 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002520 if (isKill)
2521 AddedKill.insert(&nI);
2522 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002523 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002524 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002525 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002526 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002527
Evan Cheng1953d0c2007-11-29 10:12:14 +00002528 int Id = RestoreMBBs.find_first();
2529 while (Id != -1) {
2530 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2531 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002532 MachineInstrIndex index = restores[i].index;
2533 if (index == MachineInstrIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002534 continue;
2535 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002536 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002537 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002538 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002539 bool CanFold = false;
2540 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002541 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002542 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002543 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2544 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002545 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002546 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002547
Evan Cheng0cbb1162007-11-29 01:06:25 +00002548 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002549 // If this restore were to be folded, it would have been folded
2550 // already.
2551 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002552 break;
2553 }
Evan Chengaee4af62007-12-02 08:30:39 +00002554 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002555 }
2556 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002557
2558 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002559 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002560 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002561 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002562 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2563 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002564 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2565 int LdSlot = 0;
2566 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2567 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002568 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002569 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2570 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002571 if (!Folded) {
2572 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2573 if (ImpUse) {
2574 // Re-matting an instruction with virtual register use. Add the
2575 // register as an implicit use on the use MI and update the register
2576 // interval's spill weight to HUGE_VALF to prevent it from being
2577 // spilled.
2578 LiveInterval &ImpLi = getInterval(ImpUse);
2579 ImpLi.weight = HUGE_VALF;
2580 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2581 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002582 }
Evan Chengaee4af62007-12-02 08:30:39 +00002583 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002584 }
2585 // If folding is not possible / failed, then tell the spiller to issue a
2586 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002587 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002588 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002589 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002590 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002591 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002592 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002593 }
2594
Evan Chengb50bb8c2007-12-05 08:16:32 +00002595 // Finalize intervals: add kills, finalize spill weights, and filter out
2596 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002597 std::vector<LiveInterval*> RetNewLIs;
2598 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2599 LiveInterval *LI = NewLIs[i];
2600 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002601 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002602 if (!AddedKill.count(LI)) {
2603 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames86511252009-09-04 20:41:11 +00002604 MachineInstrIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002605 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002606 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002607 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002608 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002609 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002610 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002611 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002612 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002613 RetNewLIs.push_back(LI);
2614 }
2615 }
Evan Cheng81a03822007-11-17 00:40:40 +00002616
Evan Cheng4cce6b42008-04-11 17:53:36 +00002617 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002618 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002619}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002620
2621/// hasAllocatableSuperReg - Return true if the specified physical register has
2622/// any super register that's allocatable.
2623bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2624 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2625 if (allocatableRegs_[*AS] && hasInterval(*AS))
2626 return true;
2627 return false;
2628}
2629
2630/// getRepresentativeReg - Find the largest super register of the specified
2631/// physical register.
2632unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2633 // Find the largest super-register that is allocatable.
2634 unsigned BestReg = Reg;
2635 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2636 unsigned SuperReg = *AS;
2637 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2638 BestReg = SuperReg;
2639 break;
2640 }
2641 }
2642 return BestReg;
2643}
2644
2645/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2646/// specified interval that conflicts with the specified physical register.
2647unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2648 unsigned PhysReg) const {
2649 unsigned NumConflicts = 0;
2650 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2651 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2652 E = mri_->reg_end(); I != E; ++I) {
2653 MachineOperand &O = I.getOperand();
2654 MachineInstr *MI = O.getParent();
Lang Hames86511252009-09-04 20:41:11 +00002655 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002656 if (pli.liveAt(Index))
2657 ++NumConflicts;
2658 }
2659 return NumConflicts;
2660}
2661
2662/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002663/// around all defs and uses of the specified interval. Return true if it
2664/// was able to cut its interval.
2665bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002666 unsigned PhysReg, VirtRegMap &vrm) {
2667 unsigned SpillReg = getRepresentativeReg(PhysReg);
2668
2669 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2670 // If there are registers which alias PhysReg, but which are not a
2671 // sub-register of the chosen representative super register. Assert
2672 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002673 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002674 tri_->isSuperRegister(*AS, SpillReg));
2675
Evan Cheng2824a652009-03-23 18:24:37 +00002676 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002677 LiveInterval &pli = getInterval(SpillReg);
2678 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2679 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2680 E = mri_->reg_end(); I != E; ++I) {
2681 MachineOperand &O = I.getOperand();
2682 MachineInstr *MI = O.getParent();
2683 if (SeenMIs.count(MI))
2684 continue;
2685 SeenMIs.insert(MI);
Lang Hames86511252009-09-04 20:41:11 +00002686 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002687 if (pli.liveAt(Index)) {
2688 vrm.addEmergencySpill(SpillReg, MI);
Lang Hames86511252009-09-04 20:41:11 +00002689 MachineInstrIndex StartIdx = getLoadIndex(Index);
Lang Hames35f291d2009-09-12 03:34:03 +00002690 MachineInstrIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002691 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002692 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002693 Cut = true;
2694 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002695 std::string msg;
2696 raw_string_ostream Msg(msg);
2697 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002698 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002699 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002700 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002701 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002702 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002703 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002704 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002705 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2706 if (!hasInterval(*AS))
2707 continue;
2708 LiveInterval &spli = getInterval(*AS);
2709 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002710 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002711 }
2712 }
2713 }
Evan Cheng2824a652009-03-23 18:24:37 +00002714 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002715}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002716
2717LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002718 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002719 LiveInterval& Interval = getOrCreateInterval(reg);
2720 VNInfo* VN = Interval.getNextValue(
Lang Hames86511252009-09-04 20:41:11 +00002721 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
2722 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002723 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002724 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2725 LiveRange LR(
2726 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002727 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002728 Interval.addRange(LR);
2729
2730 return LR;
2731}
David Greeneb5257662009-08-03 21:55:09 +00002732