blob: aba6ff11b42fcb6817782e2fe412010056b1f637 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Dan Gohman844731a2008-05-13 00:00:25 +000052static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
53 cl::init(true), cl::Hidden);
54static cl::opt<int> SplitLimit("split-limit",
55 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000056
Dan Gohman4c8f8702008-07-25 15:08:37 +000057static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
58
Owen Andersonae339ba2008-08-19 00:17:30 +000059static cl::opt<bool> EnableFastSpilling("fast-spill",
60 cl::init(false), cl::Hidden);
61
Chris Lattnercd3245a2006-12-19 22:41:21 +000062STATISTIC(numIntervals, "Number of original intervals");
Evan Cheng0cbb1162007-11-29 01:06:25 +000063STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000065
Devang Patel19974732007-05-03 01:11:54 +000066char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000067static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Chris Lattnerf7da2c72006-08-24 22:43:55 +000069void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman6d69ba82008-07-25 00:02:30 +000070 AU.addRequired<AliasAnalysis>();
71 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000072 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000074 AU.addPreservedID(MachineLoopInfoID);
75 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000076
77 if (!StrongPHIElim) {
78 AU.addPreservedID(PHIEliminationID);
79 AU.addRequiredID(PHIEliminationID);
80 }
81
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000082 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000084}
85
Chris Lattnerf7da2c72006-08-24 22:43:55 +000086void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000087 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000088 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000089 E = r2iMap_.end(); I != E; ++I)
90 delete I->second;
91
Evan Cheng3f32d652008-06-04 09:18:41 +000092 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000093 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000094 mi2iMap_.clear();
95 i2miMap_.clear();
96 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000097 terminatorGaps.clear();
98
Evan Chengdd199d22007-09-06 01:07:24 +000099 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
100 VNInfoAllocator.Reset();
Evan Cheng1ed99222008-07-19 00:37:25 +0000101 while (!ClonedMIs.empty()) {
102 MachineInstr *MI = ClonedMIs.back();
103 ClonedMIs.pop_back();
104 mf_->DeleteMachineInstr(MI);
105 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000106}
107
Evan Cheng2578ba22009-07-01 01:59:31 +0000108/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
109/// there is one implicit_def for each use. Add isUndef marker to
110/// implicit_def defs and their uses.
111void LiveIntervals::processImplicitDefs() {
112 SmallSet<unsigned, 8> ImpDefRegs;
113 SmallVector<MachineInstr*, 8> ImpDefMIs;
114 MachineBasicBlock *Entry = mf_->begin();
115 SmallPtrSet<MachineBasicBlock*,16> Visited;
116 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
117 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
118 DFI != E; ++DFI) {
119 MachineBasicBlock *MBB = *DFI;
120 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
121 I != E; ) {
122 MachineInstr *MI = &*I;
123 ++I;
124 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
125 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000126 ImpDefRegs.insert(Reg);
127 ImpDefMIs.push_back(MI);
128 continue;
129 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000130
131 bool ChangedToImpDef = false;
132 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000133 MachineOperand& MO = MI->getOperand(i);
134 if (!MO.isReg() || !MO.isUse())
135 continue;
136 unsigned Reg = MO.getReg();
137 if (!Reg)
138 continue;
139 if (!ImpDefRegs.count(Reg))
140 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000141 // Use is a copy, just turn it into an implicit_def.
142 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
143 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
144 Reg == SrcReg) {
145 bool isKill = MO.isKill();
146 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
147 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
148 MI->RemoveOperand(j);
149 if (isKill)
150 ImpDefRegs.erase(Reg);
151 ChangedToImpDef = true;
152 break;
153 }
154
Evan Cheng2578ba22009-07-01 01:59:31 +0000155 MO.setIsUndef();
156 if (MO.isKill() || MI->isRegTiedToDefOperand(i))
157 ImpDefRegs.erase(Reg);
158 }
159
Evan Cheng459a7c62009-07-01 08:19:36 +0000160 if (ChangedToImpDef) {
161 // Backtrack to process this new implicit_def.
162 --I;
163 } else {
164 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
165 MachineOperand& MO = MI->getOperand(i);
166 if (!MO.isReg() || !MO.isDef())
167 continue;
168 ImpDefRegs.erase(MO.getReg());
169 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000170 }
171 }
172
173 // Any outstanding liveout implicit_def's?
174 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
175 MachineInstr *MI = ImpDefMIs[i];
176 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000177 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
178 !ImpDefRegs.count(Reg)) {
179 // Delete all "local" implicit_def's. That include those which define
180 // physical registers since they cannot be liveout.
181 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000182 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000183 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000184
185 // If there are multiple defs of the same register and at least one
186 // is not an implicit_def, do not insert implicit_def's before the
187 // uses.
188 bool Skip = false;
189 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
190 DE = mri_->def_end(); DI != DE; ++DI) {
191 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
192 Skip = true;
193 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000194 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000195 }
196 if (Skip)
197 continue;
198
Evan Chengd129d732009-07-17 19:43:40 +0000199 // The only implicit_def which we want to keep are those that are live
200 // out of its block.
201 MI->eraseFromParent();
202
Evan Cheng459a7c62009-07-01 08:19:36 +0000203 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
204 UE = mri_->use_end(); UI != UE; ) {
205 MachineOperand &RMO = UI.getOperand();
206 MachineInstr *RMI = &*UI;
207 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000208 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000209 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000210 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000211
212 // Turn a copy use into an implicit_def.
213 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
214 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
215 Reg == SrcReg) {
216 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
217 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
218 RMI->RemoveOperand(j);
219 continue;
220 }
221
Evan Cheng2578ba22009-07-01 01:59:31 +0000222 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
223 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000224 RMO.setReg(NewVReg);
225 RMO.setIsUndef();
226 RMO.setIsKill();
227 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000228 }
229 ImpDefRegs.clear();
230 ImpDefMIs.clear();
231 }
232}
233
Owen Anderson80b3ce62008-05-28 20:54:50 +0000234void LiveIntervals::computeNumbering() {
235 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000236 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000237
238 Idx2MBBMap.clear();
239 MBB2IdxMap.clear();
240 mi2iMap_.clear();
241 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000242 terminatorGaps.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000243
Owen Andersona1566f22008-07-22 22:46:49 +0000244 FunctionSize = 0;
245
Chris Lattner428b92e2006-09-15 03:57:23 +0000246 // Number MachineInstrs and MachineBasicBlocks.
247 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000248 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000249
250 unsigned MIIndex = 0;
251 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
252 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000253 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000254
Owen Anderson7fbad272008-07-23 21:37:49 +0000255 // Insert an empty slot at the beginning of each block.
256 MIIndex += InstrSlots::NUM;
257 i2miMap_.push_back(0);
258
Chris Lattner428b92e2006-09-15 03:57:23 +0000259 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
260 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000261
262 if (I == MBB->getFirstTerminator()) {
263 // Leave a gap for before terminators, this is where we will point
264 // PHI kills.
265 bool inserted =
266 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
267 assert(inserted &&
268 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000269 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000270 i2miMap_.push_back(0);
271
272 MIIndex += InstrSlots::NUM;
273 }
274
Chris Lattner428b92e2006-09-15 03:57:23 +0000275 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000277 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000278 i2miMap_.push_back(I);
279 MIIndex += InstrSlots::NUM;
Owen Andersona1566f22008-07-22 22:46:49 +0000280 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000281
Evan Cheng4ed43292008-10-18 05:21:37 +0000282 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000283 unsigned Slots = I->getDesc().getNumDefs();
284 if (Slots == 0)
285 Slots = 1;
286 MIIndex += InstrSlots::NUM * Slots;
287 while (Slots--)
288 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000289 }
Lang Hamesffd13262009-07-09 03:57:02 +0000290
291 if (MBB->getFirstTerminator() == MBB->end()) {
292 // Leave a gap for before terminators, this is where we will point
293 // PHI kills.
294 bool inserted =
295 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
296 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
301 MIIndex += InstrSlots::NUM;
302 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000303
Owen Anderson1fbb4542008-06-16 16:58:24 +0000304 // Set the MBB2IdxMap entry for this MBB.
305 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
306 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000307 }
Lang Hamesffd13262009-07-09 03:57:02 +0000308
Evan Cheng4ca980e2007-10-17 02:10:22 +0000309 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000310
311 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000312 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000313 for (LiveInterval::iterator LI = OI->second->begin(),
314 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000315
Owen Anderson7eec0c22008-05-29 23:01:22 +0000316 // Remap the start index of the live range to the corresponding new
317 // number, or our best guess at what it _should_ correspond to if the
318 // original instruction has been erased. This is either the following
319 // instruction or its predecessor.
Owen Anderson7fbad272008-07-23 21:37:49 +0000320 unsigned index = LI->start / InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000321 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson0a7615a2008-07-25 23:06:59 +0000322 if (offset == InstrSlots::LOAD) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000323 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000324 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000325 // Take the pair containing the index
326 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000327 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000328
Owen Anderson7fbad272008-07-23 21:37:49 +0000329 LI->start = getMBBStartIdx(J->second);
330 } else {
331 LI->start = mi2iMap_[OldI2MI[index]] + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000332 }
333
334 // Remap the ending index in the same way that we remapped the start,
335 // except for the final step where we always map to the immediately
336 // following instruction.
Owen Andersond7dcbec2008-07-25 19:50:48 +0000337 index = (LI->end - 1) / InstrSlots::NUM;
Owen Anderson7fbad272008-07-23 21:37:49 +0000338 offset = LI->end % InstrSlots::NUM;
Owen Anderson9382b932008-07-30 00:22:56 +0000339 if (offset == InstrSlots::LOAD) {
340 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000341 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000342 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000343 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000344
Owen Anderson9382b932008-07-30 00:22:56 +0000345 LI->end = getMBBEndIdx(I->second) + 1;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000346 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000347 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000348 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
349
350 if (index != OldI2MI.size())
351 LI->end = mi2iMap_[OldI2MI[index]] + (idx == index ? offset : 0);
352 else
353 LI->end = InstrSlots::NUM * i2miMap_.size();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000354 }
Owen Anderson788d0412008-08-06 18:35:45 +0000355 }
356
Owen Anderson03857b22008-08-13 21:49:13 +0000357 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
358 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000359 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000360
Owen Anderson7eec0c22008-05-29 23:01:22 +0000361 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000362 // start indices above. VN's with special sentinel defs
363 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000364 if (vni->isDefAccurate() && !vni->isUnused()) {
Owen Anderson788d0412008-08-06 18:35:45 +0000365 unsigned index = vni->def / InstrSlots::NUM;
366 unsigned offset = vni->def % InstrSlots::NUM;
Owen Anderson91292392008-07-30 17:42:47 +0000367 if (offset == InstrSlots::LOAD) {
368 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000369 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000370 // Take the pair containing the index
371 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000372 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000373
Owen Anderson91292392008-07-30 17:42:47 +0000374 vni->def = getMBBStartIdx(J->second);
375 } else {
376 vni->def = mi2iMap_[OldI2MI[index]] + offset;
377 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000378 }
Owen Anderson745825f42008-05-28 22:40:08 +0000379
Owen Anderson7eec0c22008-05-29 23:01:22 +0000380 // Remap the VNInfo kill indices, which works the same as
381 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000382 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000383 unsigned killIdx = vni->kills[i].killIdx;
384
385 unsigned index = (killIdx - 1) / InstrSlots::NUM;
386 unsigned offset = killIdx % InstrSlots::NUM;
387
Owen Anderson309c6162008-09-30 22:51:54 +0000388 if (offset == InstrSlots::LOAD) {
Lang Hamesffd13262009-07-09 03:57:02 +0000389 assert("Value killed at a load slot.");
390 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000391 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000392 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000393
Lang Hamesffd13262009-07-09 03:57:02 +0000394 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000395 } else {
Lang Hamesffd13262009-07-09 03:57:02 +0000396 if (vni->kills[i].isPHIKill) {
397 std::vector<IdxMBBPair>::const_iterator I =
398 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
399 --I;
400 vni->kills[i].killIdx = terminatorGaps[I->second];
401 } else {
402 assert(OldI2MI[index] != 0 &&
403 "Kill refers to instruction not present in index maps.");
404 vni->kills[i].killIdx = mi2iMap_[OldI2MI[index]] + offset;
405 }
406
407 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000408 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000409 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
410
411 if (index != OldI2MI.size())
412 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
413 (idx == index ? offset : 0);
414 else
415 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000416 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000417 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000418 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000419 }
Owen Anderson788d0412008-08-06 18:35:45 +0000420 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000421}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000422
Lang Hamesf41538d2009-06-02 16:53:25 +0000423void LiveIntervals::scaleNumbering(int factor) {
424 // Need to
425 // * scale MBB begin and end points
426 // * scale all ranges.
427 // * Update VNI structures.
428 // * Scale instruction numberings
429
430 // Scale the MBB indices.
431 Idx2MBBMap.clear();
432 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
433 MBB != MBBE; ++MBB) {
434 std::pair<unsigned, unsigned> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
435 mbbIndices.first = InstrSlots::scale(mbbIndices.first, factor);
436 mbbIndices.second = InstrSlots::scale(mbbIndices.second, factor);
437 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
438 }
439 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
440
Lang Hamesffd13262009-07-09 03:57:02 +0000441 // Scale terminator gaps.
442 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
443 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
444 TGI != TGE; ++TGI) {
445 terminatorGaps[TGI->first] = InstrSlots::scale(TGI->second, factor);
446 }
447
Lang Hamesf41538d2009-06-02 16:53:25 +0000448 // Scale the intervals.
449 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
450 LI->second->scaleNumbering(factor);
451 }
452
453 // Scale MachineInstrs.
454 Mi2IndexMap oldmi2iMap = mi2iMap_;
455 unsigned highestSlot = 0;
456 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
457 MI != ME; ++MI) {
458 unsigned newSlot = InstrSlots::scale(MI->second, factor);
459 mi2iMap_[MI->first] = newSlot;
460 highestSlot = std::max(highestSlot, newSlot);
461 }
462
463 i2miMap_.clear();
464 i2miMap_.resize(highestSlot + 1);
465 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
466 MI != ME; ++MI) {
David Greenea358c1d2009-07-21 18:56:32 +0000467 i2miMap_[MI->second] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000468 }
469
470}
471
472
Owen Anderson80b3ce62008-05-28 20:54:50 +0000473/// runOnMachineFunction - Register allocate the whole function
474///
475bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
476 mf_ = &fn;
477 mri_ = &mf_->getRegInfo();
478 tm_ = &fn.getTarget();
479 tri_ = tm_->getRegisterInfo();
480 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000481 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000482 lv_ = &getAnalysis<LiveVariables>();
483 allocatableRegs_ = tri_->getAllocatableSet(fn);
484
Evan Cheng2578ba22009-07-01 01:59:31 +0000485 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000486 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000487 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000488
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000489 numIntervals += getNumIntervals();
490
Chris Lattner70ca3582004-09-30 15:59:17 +0000491 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000492 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493}
494
Chris Lattner70ca3582004-09-30 15:59:17 +0000495/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000496void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000497 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000498 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Owen Anderson03857b22008-08-13 21:49:13 +0000499 I->second->print(O, tri_);
Evan Cheng3f32d652008-06-04 09:18:41 +0000500 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000501 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000502
503 O << "********** MACHINEINSTRS **********\n";
David Greenea358c1d2009-07-21 18:56:32 +0000504 mf_->print(O, IntervalPrefixPrinter(*this));
Chris Lattner70ca3582004-09-30 15:59:17 +0000505}
506
Evan Chengc92da382007-11-03 07:20:12 +0000507/// conflictsWithPhysRegDef - Returns true if the specified register
508/// is defined during the duration of the specified interval.
509bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
510 VirtRegMap &vrm, unsigned reg) {
511 for (LiveInterval::Ranges::const_iterator
512 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
513 for (unsigned index = getBaseIndex(I->start),
514 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
515 index += InstrSlots::NUM) {
516 // skip deleted instructions
517 while (index != end && !getInstructionFromIndex(index))
518 index += InstrSlots::NUM;
519 if (index == end) break;
520
521 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000522 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
523 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000524 if (SrcReg == li.reg || DstReg == li.reg)
525 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000526 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
527 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000528 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000529 continue;
530 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000531 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000532 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000533 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000534 if (!vrm.hasPhys(PhysReg))
535 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000536 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000537 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000538 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000539 return true;
540 }
541 }
542 }
543
544 return false;
545}
546
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000547/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
548/// it can check use as well.
549bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
550 unsigned Reg, bool CheckUse,
551 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
552 for (LiveInterval::Ranges::const_iterator
553 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
554 for (unsigned index = getBaseIndex(I->start),
555 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
556 index += InstrSlots::NUM) {
557 // Skip deleted instructions.
558 MachineInstr *MI = 0;
559 while (index != end) {
560 MI = getInstructionFromIndex(index);
561 if (MI)
562 break;
563 index += InstrSlots::NUM;
564 }
565 if (index == end) break;
566
567 if (JoinedCopies.count(MI))
568 continue;
569 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
570 MachineOperand& MO = MI->getOperand(i);
571 if (!MO.isReg())
572 continue;
573 if (MO.isUse() && !CheckUse)
574 continue;
575 unsigned PhysReg = MO.getReg();
576 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
577 continue;
578 if (tri_->isSubRegister(Reg, PhysReg))
579 return true;
580 }
581 }
582 }
583
584 return false;
585}
586
587
Evan Cheng549f27d32007-08-13 23:45:17 +0000588void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000589 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000590 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000591 else
592 cerr << "%reg" << reg;
593}
594
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000595void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000596 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000597 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000598 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000599 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000600 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Evan Cheng419852c2008-04-03 16:39:43 +0000601
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000602 // Virtual registers may be defined multiple times (due to phi
603 // elimination and 2-addr elimination). Much of what we do only has to be
604 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000605 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000606 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000607 if (interval.empty()) {
608 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000609 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000610 // Earlyclobbers move back one.
611 if (MO.isEarlyClobber())
612 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000613 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000614 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000615 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000616 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000617 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000618 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000619 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000620 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000621 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000622 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000623
624 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000625
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000626 // Loop over all of the blocks that the vreg is defined in. There are
627 // two cases we have to handle here. The most common case is a vreg
628 // whose lifetime is contained within a basic block. In this case there
629 // will be a single kill, in MBB, which comes after the definition.
630 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
631 // FIXME: what about dead vars?
632 unsigned killIdx;
633 if (vi.Kills[0] != mi)
634 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
635 else
636 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000637
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 // If the kill happens after the definition, we have an intra-block
639 // live range.
640 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000641 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000643 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000644 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000645 DOUT << " +" << LR << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000646 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 return;
648 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000649 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000650
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 // The other case we handle is when a virtual register lives to the end
652 // of the defining block, potentially live across some blocks, then is
653 // live into some number of blocks, but gets killed. Start by adding a
654 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000655 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000656 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000657 interval.addRange(NewLR);
658
659 // Iterate over all of the blocks that the variable is completely
660 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
661 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000662 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
663 E = vi.AliveBlocks.end(); I != E; ++I) {
664 LiveRange LR(getMBBStartIdx(*I),
665 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000666 ValNo);
667 interval.addRange(LR);
668 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 }
670
671 // Finally, this virtual register is live from the start of any killing
672 // block to the 'use' slot of the killing instruction.
673 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
674 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000675 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000676 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000677 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000678 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000679 interval.addKill(ValNo, killIdx, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000680 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000681 }
682
683 } else {
684 // If this is the second time we see a virtual register definition, it
685 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000686 // the result of two address elimination, then the vreg is one of the
687 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000688 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000689 // If this is a two-address definition, then we have already processed
690 // the live range. The only problem is that we didn't realize there
691 // are actually two values in the live interval. Because of this we
692 // need to take the LiveRegion that defines this register and split it
693 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000694 assert(interval.containsOneValue());
695 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000696 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000697 if (MO.isEarlyClobber())
698 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699
Evan Cheng4f8ff162007-08-11 00:59:19 +0000700 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000701 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000702
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000704 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000705 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000706
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000707 // Two-address vregs should always only be redefined once. This means
708 // that at this point, there should be exactly one value number in it.
709 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
710
Chris Lattner91725b72006-08-31 05:54:43 +0000711 // The new value number (#1) is defined by the instruction we claimed
712 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000713 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
Lang Hames857c4e02009-06-17 21:01:20 +0000714 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000715 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000716 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
717
Chris Lattner91725b72006-08-31 05:54:43 +0000718 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000719 OldValNo->def = RedefIndex;
720 OldValNo->copy = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000721 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000722 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000723
724 // Add the new live interval which replaces the range for the input copy.
725 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000726 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000728 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000729
730 // If this redefinition is dead, we need to add a dummy unit live
731 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000732 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000733 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000735 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000736 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000737
738 } else {
739 // Otherwise, this must be because of phi elimination. If this is the
740 // first redefinition of the vreg that we have seen, go back and change
741 // the live range in the PHI block to be a different value number.
742 if (interval.containsOneValue()) {
743 assert(vi.Kills.size() == 1 &&
744 "PHI elimination vreg should have one kill, the PHI itself!");
745
746 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000747 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000748 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000749 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000750 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000751 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000752 interval.print(DOUT, tri_); DOUT << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000753 interval.removeRange(Start, End);
754 assert(interval.ranges.size() == 1 &&
755 "newly discovered PHI interval has >1 ranges.");
756 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
757 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000758 VNI->setHasPHIKill(true);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000759 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000760
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000761 // Replace the interval with one of a NEW value number. Note that this
762 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000763 LiveRange LR(Start, End,
764 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000765 LR.valno->setIsPHIDef(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000766 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000767 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000768 interval.addKill(LR.valno, End, false);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000769 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000770 }
771
772 // In the case of PHI elimination, each variable definition is only
773 // live until the end of the block. We've already taken care of the
774 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000775 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000776 if (MO.isEarlyClobber())
777 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000778
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000779 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000780 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000781 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000782 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000783 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000784 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000785 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000786 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000787 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000788
Owen Anderson7fbad272008-07-23 21:37:49 +0000789 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000790 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000791 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000792 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000793 ValNo->setHasPHIKill(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000794 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000795 }
796 }
797
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000798 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000799}
800
Chris Lattnerf35fef72004-07-23 21:24:19 +0000801void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000802 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000803 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000804 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000805 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000806 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000807 // A physical register cannot be live across basic block, so its
808 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000809 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000810
Chris Lattner6b128bd2006-09-03 08:07:11 +0000811 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000812 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000813 // Earlyclobbers move back one.
814 if (MO.isEarlyClobber())
815 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000816 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000817
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000818 // If it is not used after definition, it is considered dead at
819 // the instruction defining it. Hence its interval is:
820 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000821 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000822 DOUT << " dead";
Dale Johannesen86b49f82008-09-24 01:07:17 +0000823 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000824 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000825 }
826
827 // If it is not dead on definition, it must be killed by a
828 // subsequent instruction. Hence its interval is:
829 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000830 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000831 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000832 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
833 getInstructionFromIndex(baseIndex) == 0)
834 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000835 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000836 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000837 end = getUseIndex(baseIndex) + 1;
838 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000839 } else {
840 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
841 if (DefIdx != -1) {
842 if (mi->isRegTiedToUseOperand(DefIdx)) {
843 // Two-address instruction.
844 end = getDefIndex(baseIndex);
845 if (mi->getOperand(DefIdx).isEarlyClobber())
846 end = getUseIndex(baseIndex);
847 } else {
848 // Another instruction redefines the register before it is ever read.
849 // Then the register is essentially dead at the instruction that defines
850 // it. Hence its interval is:
851 // [defSlot(def), defSlot(def)+1)
852 DOUT << " dead";
853 end = start + 1;
854 }
855 goto exit;
856 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000857 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000858
859 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000860 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000861
862 // The only case we should have a dead physreg here without a killing or
863 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000864 // and never used. Another possible case is the implicit use of the
865 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000866 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000867
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000868exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000869 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000870
Evan Cheng24a3cc42007-04-25 07:30:23 +0000871 // Already exists? Extend old live interval.
872 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000873 bool Extend = OldLR != interval.end();
874 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000875 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000876 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000877 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000878 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000879 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000880 interval.addKill(LR.valno, end, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000881 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000882}
883
Chris Lattnerf35fef72004-07-23 21:24:19 +0000884void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
885 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000886 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000887 MachineOperand& MO,
888 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000889 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000890 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000891 getOrCreateInterval(MO.getReg()));
892 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000893 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000894 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000895 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000896 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000897 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000898 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000899 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000900 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000901 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000902 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000903 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000904 // If MI also modifies the sub-register explicitly, avoid processing it
905 // more than once. Do not pass in TRI here so it checks for exact match.
906 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000907 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000908 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000909 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000910}
911
Evan Chengb371f452007-02-19 21:49:54 +0000912void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000913 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000914 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000915 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
916
917 // Look for kills, if it reaches a def before it's killed, then it shouldn't
918 // be considered a livein.
919 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000920 unsigned baseIndex = MIIdx;
921 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000922 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
923 getInstructionFromIndex(baseIndex) == 0)
924 baseIndex += InstrSlots::NUM;
925 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000926 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000927
Evan Chengb371f452007-02-19 21:49:54 +0000928 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000929 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000930 DOUT << " killed";
931 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000932 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000933 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000934 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000935 // 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)
939 DOUT << " dead";
940 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000941 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000942 break;
Evan Chengb371f452007-02-19 21:49:54 +0000943 }
944
945 baseIndex += InstrSlots::NUM;
946 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000947 if (mi != MBB->end()) {
948 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
949 getInstructionFromIndex(baseIndex) == 0)
950 baseIndex += InstrSlots::NUM;
951 }
Evan Chengb371f452007-02-19 21:49:54 +0000952 }
953
Evan Cheng75611fb2007-06-27 01:16:36 +0000954 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000955 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000956 if (isAlias) {
957 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000958 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000959 } else {
960 DOUT << " live through";
961 end = baseIndex;
962 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000963 }
964
Lang Hames10382fb2009-06-19 02:17:53 +0000965 VNInfo *vni =
966 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000967 vni->setIsPHIDef(true);
968 LiveRange LR(start, end, vni);
969
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000970 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000971 interval.addKill(LR.valno, end, false);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000972 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000973}
974
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000975/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000976/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000977/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000978/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000979void LiveIntervals::computeIntervals() {
Dale Johannesen91aac102008-09-17 21:13:11 +0000980
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000981 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
982 << "********** Function: "
983 << ((Value*)mf_->getFunction())->getName() << '\n';
Evan Chengd129d732009-07-17 19:43:40 +0000984
985 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000986 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
987 MBBI != E; ++MBBI) {
988 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000989 // Track the index of the current machine instr.
990 unsigned MIIndex = getMBBStartIdx(MBB);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000991 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000992
Chris Lattner428b92e2006-09-15 03:57:23 +0000993 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000994
Dan Gohmancb406c22007-10-03 19:26:29 +0000995 // Create intervals for live-ins to this BB first.
996 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
997 LE = MBB->livein_end(); LI != LE; ++LI) {
998 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
999 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001000 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001001 if (!hasInterval(*AS))
1002 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1003 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001004 }
1005
Owen Anderson99500ae2008-09-15 22:00:38 +00001006 // Skip over empty initial indices.
1007 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1008 getInstructionFromIndex(MIIndex) == 0)
1009 MIIndex += InstrSlots::NUM;
1010
Chris Lattner428b92e2006-09-15 03:57:23 +00001011 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001012 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001013
Evan Cheng438f7bc2006-11-10 08:43:01 +00001014 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001015 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1016 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001017 if (!MO.isReg() || !MO.getReg())
1018 continue;
1019
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001020 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001021 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001022 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001023 else if (MO.isUndef())
1024 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001025 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001026
1027 // Skip over the empty slots after each instruction.
1028 unsigned Slots = MI->getDesc().getNumDefs();
1029 if (Slots == 0)
1030 Slots = 1;
1031 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001032
1033 // Skip over empty indices.
1034 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1035 getInstructionFromIndex(MIIndex) == 0)
1036 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001037 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001038 }
Evan Chengd129d732009-07-17 19:43:40 +00001039
1040 // Create empty intervals for registers defined by implicit_def's (except
1041 // for those implicit_def that define values which are liveout of their
1042 // blocks.
1043 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1044 unsigned UndefReg = UndefUses[i];
1045 (void)getOrCreateInterval(UndefReg);
1046 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001047}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001048
Evan Chengd0e32c52008-10-29 05:06:14 +00001049bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001050 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001051 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001052 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001053
1054 bool ResVal = false;
1055 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001056 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001057 break;
1058 MBBs.push_back(I->second);
1059 ResVal = true;
1060 ++I;
1061 }
1062 return ResVal;
1063}
1064
Evan Chengd0e32c52008-10-29 05:06:14 +00001065bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1066 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1067 std::vector<IdxMBBPair>::const_iterator I =
1068 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1069
1070 bool ResVal = false;
1071 while (I != Idx2MBBMap.end()) {
1072 if (I->first > End)
1073 break;
1074 MachineBasicBlock *MBB = I->second;
1075 if (getMBBEndIdx(MBB) > End)
1076 break;
1077 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1078 SE = MBB->succ_end(); SI != SE; ++SI)
1079 MBBs.push_back(*SI);
1080 ResVal = true;
1081 ++I;
1082 }
1083 return ResVal;
1084}
1085
Owen Anderson03857b22008-08-13 21:49:13 +00001086LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001087 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001088 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001089}
Evan Chengf2fbca62007-11-12 06:35:08 +00001090
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001091/// dupInterval - Duplicate a live interval. The caller is responsible for
1092/// managing the allocated memory.
1093LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1094 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001095 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001096 return NewLI;
1097}
1098
Evan Chengc8d044e2008-02-15 18:24:29 +00001099/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1100/// copy field and returns the source register that defines it.
1101unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
1102 if (!VNI->copy)
1103 return 0;
1104
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001105 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1106 // If it's extracting out of a physical register, return the sub-register.
1107 unsigned Reg = VNI->copy->getOperand(1).getReg();
1108 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1109 Reg = tri_->getSubReg(Reg, VNI->copy->getOperand(2).getImm());
1110 return Reg;
Dan Gohman97121ba2009-04-08 00:15:30 +00001111 } else if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1112 VNI->copy->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng7e073ba2008-04-09 20:57:25 +00001113 return VNI->copy->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001114
Evan Cheng04ee5a12009-01-20 19:12:24 +00001115 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1116 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001117 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001118 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001119 return 0;
1120}
Evan Chengf2fbca62007-11-12 06:35:08 +00001121
1122//===----------------------------------------------------------------------===//
1123// Register allocator hooks.
1124//
1125
Evan Chengd70dbb52008-02-22 09:24:50 +00001126/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1127/// allow one) virtual register operand, then its uses are implicitly using
1128/// the register. Returns the virtual register.
1129unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1130 MachineInstr *MI) const {
1131 unsigned RegOp = 0;
1132 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1133 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001134 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001135 continue;
1136 unsigned Reg = MO.getReg();
1137 if (Reg == 0 || Reg == li.reg)
1138 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001139
1140 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1141 !allocatableRegs_[Reg])
1142 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001143 // FIXME: For now, only remat MI with at most one register operand.
1144 assert(!RegOp &&
1145 "Can't rematerialize instruction with multiple register operand!");
1146 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001147#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001148 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001149#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001150 }
1151 return RegOp;
1152}
1153
1154/// isValNoAvailableAt - Return true if the val# of the specified interval
1155/// which reaches the given instruction also reaches the specified use index.
1156bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1157 unsigned UseIdx) const {
1158 unsigned Index = getInstructionIndex(MI);
1159 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1160 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1161 return UI != li.end() && UI->valno == ValNo;
1162}
1163
Evan Chengf2fbca62007-11-12 06:35:08 +00001164/// isReMaterializable - Returns true if the definition MI of the specified
1165/// val# of the specified interval is re-materializable.
1166bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001167 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001168 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001169 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001170 if (DisableReMat)
1171 return false;
1172
Evan Cheng20ccded2008-03-15 00:19:36 +00001173 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001174 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001175
1176 int FrameIdx = 0;
1177 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001178 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001179 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1180 // this but remember this is not safe to fold into a two-address
1181 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001182 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001183 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001184
Dan Gohman6d69ba82008-07-25 00:02:30 +00001185 // If the target-specific rules don't identify an instruction as
1186 // being trivially rematerializable, use some target-independent
1187 // rules.
1188 if (!MI->getDesc().isRematerializable() ||
1189 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001190 if (!EnableAggressiveRemat)
1191 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001192
Dan Gohman0471a792008-07-28 18:43:51 +00001193 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001194 // we can't analyze it.
1195 const TargetInstrDesc &TID = MI->getDesc();
1196 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1197 return false;
1198
1199 // Avoid instructions obviously unsafe for remat.
1200 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1201 return false;
1202
1203 // If the instruction accesses memory and the memory could be non-constant,
1204 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001205 for (std::list<MachineMemOperand>::const_iterator
1206 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001207 const MachineMemOperand &MMO = *I;
1208 if (MMO.isVolatile() || MMO.isStore())
1209 return false;
1210 const Value *V = MMO.getValue();
1211 if (!V)
1212 return false;
1213 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1214 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001215 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001216 } else if (!aa_->pointsToConstantMemory(V))
1217 return false;
1218 }
1219
1220 // If any of the registers accessed are non-constant, conservatively assume
1221 // the instruction is not rematerializable.
1222 unsigned ImpUse = 0;
1223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1224 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001225 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001226 unsigned Reg = MO.getReg();
1227 if (Reg == 0)
1228 continue;
1229 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1230 return false;
1231
1232 // Only allow one def, and that in the first operand.
1233 if (MO.isDef() != (i == 0))
1234 return false;
1235
1236 // Only allow constant-valued registers.
1237 bool IsLiveIn = mri_->isLiveIn(Reg);
1238 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1239 E = mri_->def_end();
1240
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001241 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001242 if (MO.isDef() && (next(I) != E || IsLiveIn))
1243 return false;
1244
1245 if (MO.isUse()) {
1246 // Only allow one use other register use, as that's all the
1247 // remat mechanisms support currently.
1248 if (Reg != li.reg) {
1249 if (ImpUse == 0)
1250 ImpUse = Reg;
1251 else if (Reg != ImpUse)
1252 return false;
1253 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001254 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001255 if (I != E && (next(I) != E || IsLiveIn))
1256 return false;
1257 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001258 }
1259 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001260 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001261
Dan Gohman6d69ba82008-07-25 00:02:30 +00001262 unsigned ImpUse = getReMatImplicitUse(li, MI);
1263 if (ImpUse) {
1264 const LiveInterval &ImpLi = getInterval(ImpUse);
1265 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1266 re = mri_->use_end(); ri != re; ++ri) {
1267 MachineInstr *UseMI = &*ri;
1268 unsigned UseIdx = getInstructionIndex(UseMI);
1269 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1270 continue;
1271 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1272 return false;
1273 }
Evan Chengdc377862008-09-30 15:44:16 +00001274
1275 // If a register operand of the re-materialized instruction is going to
1276 // be spilled next, then it's not legal to re-materialize this instruction.
1277 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1278 if (ImpUse == SpillIs[i]->reg)
1279 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001280 }
1281 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001282}
1283
Evan Cheng06587492008-10-24 02:05:00 +00001284/// isReMaterializable - Returns true if the definition MI of the specified
1285/// val# of the specified interval is re-materializable.
1286bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1287 const VNInfo *ValNo, MachineInstr *MI) {
1288 SmallVector<LiveInterval*, 4> Dummy1;
1289 bool Dummy2;
1290 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1291}
1292
Evan Cheng5ef3a042007-12-06 00:01:56 +00001293/// isReMaterializable - Returns true if every definition of MI of every
1294/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001295bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1296 SmallVectorImpl<LiveInterval*> &SpillIs,
1297 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001298 isLoad = false;
1299 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1300 i != e; ++i) {
1301 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001302 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001303 continue; // Dead val#.
1304 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001305 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001306 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001307 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001308 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001309 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001310 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001311 return false;
1312 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001313 }
1314 return true;
1315}
1316
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001317/// FilterFoldedOps - Filter out two-address use operands. Return
1318/// true if it finds any issue with the operands that ought to prevent
1319/// folding.
1320static bool FilterFoldedOps(MachineInstr *MI,
1321 SmallVector<unsigned, 2> &Ops,
1322 unsigned &MRInfo,
1323 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001324 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001325 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1326 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001327 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001328 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001329 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001330 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001331 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001332 MRInfo |= (unsigned)VirtRegMap::isMod;
1333 else {
1334 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001335 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001336 MRInfo = VirtRegMap::isModRef;
1337 continue;
1338 }
1339 MRInfo |= (unsigned)VirtRegMap::isRef;
1340 }
1341 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001342 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001343 return false;
1344}
1345
1346
1347/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1348/// slot / to reg or any rematerialized load into ith operand of specified
1349/// MI. If it is successul, MI is updated with the newly created MI and
1350/// returns true.
1351bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1352 VirtRegMap &vrm, MachineInstr *DefMI,
1353 unsigned InstrIdx,
1354 SmallVector<unsigned, 2> &Ops,
1355 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001356 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001357 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001358 RemoveMachineInstrFromMaps(MI);
1359 vrm.RemoveMachineInstrFromMaps(MI);
1360 MI->eraseFromParent();
1361 ++numFolds;
1362 return true;
1363 }
1364
1365 // Filter the list of operand indexes that are to be folded. Abort if
1366 // any operand will prevent folding.
1367 unsigned MRInfo = 0;
1368 SmallVector<unsigned, 2> FoldOps;
1369 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1370 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001371
Evan Cheng427f4c12008-03-31 23:19:51 +00001372 // The only time it's safe to fold into a two address instruction is when
1373 // it's folding reload and spill from / into a spill stack slot.
1374 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001375 return false;
1376
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001377 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1378 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001379 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001380 // Remember this instruction uses the spill slot.
1381 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1382
Evan Chengf2fbca62007-11-12 06:35:08 +00001383 // Attempt to fold the memory reference into the instruction. If
1384 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001385 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001386 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001387 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001388 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001389 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001390 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001391 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001392 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1393 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001394 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001395 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001396 return true;
1397 }
1398 return false;
1399}
1400
Evan Cheng018f9b02007-12-05 03:22:34 +00001401/// canFoldMemoryOperand - Returns true if the specified load / store
1402/// folding is possible.
1403bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001404 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001405 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001406 // Filter the list of operand indexes that are to be folded. Abort if
1407 // any operand will prevent folding.
1408 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001409 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001410 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1411 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001412
Evan Cheng3c75ba82008-04-01 21:37:32 +00001413 // It's only legal to remat for a use, not a def.
1414 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001415 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001416
Evan Chengd70dbb52008-02-22 09:24:50 +00001417 return tii_->canFoldMemoryOperand(MI, FoldOps);
1418}
1419
Evan Cheng81a03822007-11-17 00:40:40 +00001420bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1421 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1422 for (LiveInterval::Ranges::const_iterator
1423 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1424 std::vector<IdxMBBPair>::const_iterator II =
1425 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1426 if (II == Idx2MBBMap.end())
1427 continue;
1428 if (I->end > II->first) // crossing a MBB.
1429 return false;
1430 MBBs.insert(II->second);
1431 if (MBBs.size() > 1)
1432 return false;
1433 }
1434 return true;
1435}
1436
Evan Chengd70dbb52008-02-22 09:24:50 +00001437/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1438/// interval on to-be re-materialized operands of MI) with new register.
1439void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1440 MachineInstr *MI, unsigned NewVReg,
1441 VirtRegMap &vrm) {
1442 // There is an implicit use. That means one of the other operand is
1443 // being remat'ed and the remat'ed instruction has li.reg as an
1444 // use operand. Make sure we rewrite that as well.
1445 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1446 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001447 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001448 continue;
1449 unsigned Reg = MO.getReg();
1450 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1451 continue;
1452 if (!vrm.isReMaterialized(Reg))
1453 continue;
1454 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001455 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1456 if (UseMO)
1457 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001458 }
1459}
1460
Evan Chengf2fbca62007-11-12 06:35:08 +00001461/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1462/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001463bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001464rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1465 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001466 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001467 unsigned Slot, int LdSlot,
1468 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001469 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001470 const TargetRegisterClass* rc,
1471 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001472 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001473 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001474 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001475 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001476 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001477 RestartInstruction:
1478 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1479 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001480 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001481 continue;
1482 unsigned Reg = mop.getReg();
1483 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001484 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001485 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001486 if (Reg != li.reg)
1487 continue;
1488
1489 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001490 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001491 int FoldSlot = Slot;
1492 if (DefIsReMat) {
1493 // If this is the rematerializable definition MI itself and
1494 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001495 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001496 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1497 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001498 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001499 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001500 MI->eraseFromParent();
1501 break;
1502 }
1503
1504 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001505 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001506 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001507 if (isLoad) {
1508 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1509 FoldSS = isLoadSS;
1510 FoldSlot = LdSlot;
1511 }
1512 }
1513
Evan Chengf2fbca62007-11-12 06:35:08 +00001514 // Scan all of the operands of this instruction rewriting operands
1515 // to use NewVReg instead of li.reg as appropriate. We do this for
1516 // two reasons:
1517 //
1518 // 1. If the instr reads the same spilled vreg multiple times, we
1519 // want to reuse the NewVReg.
1520 // 2. If the instr is a two-addr instruction, we are required to
1521 // keep the src/dst regs pinned.
1522 //
1523 // Keep track of whether we replace a use and/or def so that we can
1524 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001525
Evan Cheng81a03822007-11-17 00:40:40 +00001526 HasUse = mop.isUse();
1527 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001528 SmallVector<unsigned, 2> Ops;
1529 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001530 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001531 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001532 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001533 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001534 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001535 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001536 continue;
1537 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001538 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001539 if (!MOj.isUndef()) {
1540 HasUse |= MOj.isUse();
1541 HasDef |= MOj.isDef();
1542 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001543 }
1544 }
1545
David Greene26b86a02008-10-27 17:38:59 +00001546 // Create a new virtual register for the spill interval.
1547 // Create the new register now so we can map the fold instruction
1548 // to the new register so when it is unfolded we get the correct
1549 // answer.
1550 bool CreatedNewVReg = false;
1551 if (NewVReg == 0) {
1552 NewVReg = mri_->createVirtualRegister(rc);
1553 vrm.grow();
1554 CreatedNewVReg = true;
1555 }
1556
Evan Cheng9c3c2212008-06-06 07:54:39 +00001557 if (!TryFold)
1558 CanFold = false;
1559 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001560 // Do not fold load / store here if we are splitting. We'll find an
1561 // optimal point to insert a load / store later.
1562 if (!TrySplit) {
1563 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001564 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001565 // Folding the load/store can completely change the instruction in
1566 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001567
1568 if (FoldSS) {
1569 // We need to give the new vreg the same stack slot as the
1570 // spilled interval.
1571 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1572 }
1573
Evan Cheng018f9b02007-12-05 03:22:34 +00001574 HasUse = false;
1575 HasDef = false;
1576 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001577 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001578 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001579 goto RestartInstruction;
1580 }
1581 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001582 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001583 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001584 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001585 }
Evan Chengcddbb832007-11-30 21:23:43 +00001586
Evan Chengcddbb832007-11-30 21:23:43 +00001587 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001588 if (mop.isImplicit())
1589 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001590
1591 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001592 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1593 MachineOperand &mopj = MI->getOperand(Ops[j]);
1594 mopj.setReg(NewVReg);
1595 if (mopj.isImplicit())
1596 rewriteImplicitOps(li, MI, NewVReg, vrm);
1597 }
Evan Chengcddbb832007-11-30 21:23:43 +00001598
Evan Cheng81a03822007-11-17 00:40:40 +00001599 if (CreatedNewVReg) {
1600 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001601 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001602 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001603 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001604 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001605 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001606 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001607 }
1608 if (!CanDelete || (HasUse && HasDef)) {
1609 // If this is a two-addr instruction then its use operands are
1610 // rematerializable but its def is not. It should be assigned a
1611 // stack slot.
1612 vrm.assignVirt2StackSlot(NewVReg, Slot);
1613 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001614 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001615 vrm.assignVirt2StackSlot(NewVReg, Slot);
1616 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001617 } else if (HasUse && HasDef &&
1618 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1619 // If this interval hasn't been assigned a stack slot (because earlier
1620 // def is a deleted remat def), do it now.
1621 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1622 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001623 }
1624
Evan Cheng313d4b82008-02-23 00:33:04 +00001625 // Re-matting an instruction with virtual register use. Add the
1626 // register as an implicit use on the use MI.
1627 if (DefIsReMat && ImpUse)
1628 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1629
Evan Cheng5b69eba2009-04-21 22:46:52 +00001630 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001631 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001632 if (CreatedNewVReg) {
1633 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001634 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001635 if (TrySplit)
1636 vrm.setIsSplitFromReg(NewVReg, li.reg);
1637 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001638
1639 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001640 if (CreatedNewVReg) {
1641 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001642 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Cheng81a03822007-11-17 00:40:40 +00001643 DOUT << " +" << LR;
1644 nI.addRange(LR);
1645 } else {
1646 // Extend the split live interval to this def / use.
1647 unsigned End = getUseIndex(index)+1;
1648 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1649 nI.getValNumInfo(nI.getNumValNums()-1));
1650 DOUT << " +" << LR;
1651 nI.addRange(LR);
1652 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001653 }
1654 if (HasDef) {
1655 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001656 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Chengf2fbca62007-11-12 06:35:08 +00001657 DOUT << " +" << LR;
1658 nI.addRange(LR);
1659 }
Evan Cheng81a03822007-11-17 00:40:40 +00001660
Evan Chengf2fbca62007-11-12 06:35:08 +00001661 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001662 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001663 DOUT << '\n';
1664 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001665 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001666}
Evan Cheng81a03822007-11-17 00:40:40 +00001667bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001668 const VNInfo *VNI,
1669 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001670 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001671 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001672 if (VNI->kills[j].isPHIKill)
1673 continue;
1674
1675 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001676 if (KillIdx > Idx && KillIdx < End)
1677 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001678 }
1679 return false;
1680}
1681
Evan Cheng063284c2008-02-21 00:34:19 +00001682/// RewriteInfo - Keep track of machine instrs that will be rewritten
1683/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001684namespace {
1685 struct RewriteInfo {
1686 unsigned Index;
1687 MachineInstr *MI;
1688 bool HasUse;
1689 bool HasDef;
1690 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1691 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1692 };
Evan Cheng063284c2008-02-21 00:34:19 +00001693
Dan Gohman844731a2008-05-13 00:00:25 +00001694 struct RewriteInfoCompare {
1695 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1696 return LHS.Index < RHS.Index;
1697 }
1698 };
1699}
Evan Cheng063284c2008-02-21 00:34:19 +00001700
Evan Chengf2fbca62007-11-12 06:35:08 +00001701void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001702rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001703 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001704 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001705 unsigned Slot, int LdSlot,
1706 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001707 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001708 const TargetRegisterClass* rc,
1709 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001710 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001711 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001712 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001713 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001714 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1715 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001716 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001717 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001718 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001719 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001720 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001721
Evan Cheng063284c2008-02-21 00:34:19 +00001722 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001723 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001724 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001725 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1726 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001727 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001728 MachineOperand &O = ri.getOperand();
1729 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001730 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001731 unsigned index = getInstructionIndex(MI);
1732 if (index < start || index >= end)
1733 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001734
1735 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001736 // Must be defined by an implicit def. It should not be spilled. Note,
1737 // this is for correctness reason. e.g.
1738 // 8 %reg1024<def> = IMPLICIT_DEF
1739 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1740 // The live range [12, 14) are not part of the r1024 live interval since
1741 // it's defined by an implicit def. It will not conflicts with live
1742 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001743 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001744 // the INSERT_SUBREG and both target registers that would overlap.
1745 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001746 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1747 }
1748 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1749
Evan Cheng313d4b82008-02-23 00:33:04 +00001750 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001751 // Now rewrite the defs and uses.
1752 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1753 RewriteInfo &rwi = RewriteMIs[i];
1754 ++i;
1755 unsigned index = rwi.Index;
1756 bool MIHasUse = rwi.HasUse;
1757 bool MIHasDef = rwi.HasDef;
1758 MachineInstr *MI = rwi.MI;
1759 // If MI def and/or use the same register multiple times, then there
1760 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001761 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001762 while (i != e && RewriteMIs[i].MI == MI) {
1763 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001764 bool isUse = RewriteMIs[i].HasUse;
1765 if (isUse) ++NumUses;
1766 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001767 MIHasDef |= RewriteMIs[i].HasDef;
1768 ++i;
1769 }
Evan Cheng81a03822007-11-17 00:40:40 +00001770 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001771
Evan Cheng0a891ed2008-05-23 23:00:04 +00001772 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001773 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001774 // register interval's spill weight to HUGE_VALF to prevent it from
1775 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001776 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001777 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001778 }
1779
Evan Cheng063284c2008-02-21 00:34:19 +00001780 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001781 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001782 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001783 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001784 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001785 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001786 // One common case:
1787 // x = use
1788 // ...
1789 // ...
1790 // def = ...
1791 // = use
1792 // It's better to start a new interval to avoid artifically
1793 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001794 if (MIHasDef && !MIHasUse) {
1795 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001796 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001797 }
1798 }
Evan Chengcada2452007-11-28 01:28:46 +00001799 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001800
1801 bool IsNew = ThisVReg == 0;
1802 if (IsNew) {
1803 // This ends the previous live interval. If all of its def / use
1804 // can be folded, give it a low spill weight.
1805 if (NewVReg && TrySplit && AllCanFold) {
1806 LiveInterval &nI = getOrCreateInterval(NewVReg);
1807 nI.weight /= 10.0F;
1808 }
1809 AllCanFold = true;
1810 }
1811 NewVReg = ThisVReg;
1812
Evan Cheng81a03822007-11-17 00:40:40 +00001813 bool HasDef = false;
1814 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001815 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001816 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1817 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1818 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001819 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001820 if (!HasDef && !HasUse)
1821 continue;
1822
Evan Cheng018f9b02007-12-05 03:22:34 +00001823 AllCanFold &= CanFold;
1824
Evan Cheng81a03822007-11-17 00:40:40 +00001825 // Update weight of spill interval.
1826 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001827 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001828 // The spill weight is now infinity as it cannot be spilled again.
1829 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001830 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001831 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001832
1833 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001834 if (HasDef) {
1835 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001836 bool HasKill = false;
1837 if (!HasUse)
1838 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1839 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001840 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001841 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001842 if (VNI)
1843 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1844 }
Owen Anderson28998312008-08-13 22:28:50 +00001845 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001846 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001847 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001848 if (SII == SpillIdxes.end()) {
1849 std::vector<SRInfo> S;
1850 S.push_back(SRInfo(index, NewVReg, true));
1851 SpillIdxes.insert(std::make_pair(MBBId, S));
1852 } else if (SII->second.back().vreg != NewVReg) {
1853 SII->second.push_back(SRInfo(index, NewVReg, true));
1854 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001855 // If there is an earlier def and this is a two-address
1856 // instruction, then it's not possible to fold the store (which
1857 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001858 SRInfo &Info = SII->second.back();
1859 Info.index = index;
1860 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001861 }
1862 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001863 } else if (SII != SpillIdxes.end() &&
1864 SII->second.back().vreg == NewVReg &&
1865 (int)index > SII->second.back().index) {
1866 // There is an earlier def that's not killed (must be two-address).
1867 // The spill is no longer needed.
1868 SII->second.pop_back();
1869 if (SII->second.empty()) {
1870 SpillIdxes.erase(MBBId);
1871 SpillMBBs.reset(MBBId);
1872 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001873 }
1874 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001875 }
1876
1877 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001878 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001879 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001880 if (SII != SpillIdxes.end() &&
1881 SII->second.back().vreg == NewVReg &&
1882 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001883 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001884 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001885 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001886 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001887 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001888 // If we are splitting live intervals, only fold if it's the first
1889 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001890 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001891 else if (IsNew) {
1892 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001893 if (RII == RestoreIdxes.end()) {
1894 std::vector<SRInfo> Infos;
1895 Infos.push_back(SRInfo(index, NewVReg, true));
1896 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1897 } else {
1898 RII->second.push_back(SRInfo(index, NewVReg, true));
1899 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001900 RestoreMBBs.set(MBBId);
1901 }
1902 }
1903
1904 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001905 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001906 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001907 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001908
1909 if (NewVReg && TrySplit && AllCanFold) {
1910 // If all of its def / use can be folded, give it a low spill weight.
1911 LiveInterval &nI = getOrCreateInterval(NewVReg);
1912 nI.weight /= 10.0F;
1913 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001914}
1915
Evan Cheng1953d0c2007-11-29 10:12:14 +00001916bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1917 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001918 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001919 if (!RestoreMBBs[Id])
1920 return false;
1921 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1922 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1923 if (Restores[i].index == index &&
1924 Restores[i].vreg == vr &&
1925 Restores[i].canFold)
1926 return true;
1927 return false;
1928}
1929
1930void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1931 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001932 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001933 if (!RestoreMBBs[Id])
1934 return;
1935 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1936 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1937 if (Restores[i].index == index && Restores[i].vreg)
1938 Restores[i].index = -1;
1939}
Evan Cheng81a03822007-11-17 00:40:40 +00001940
Evan Cheng4cce6b42008-04-11 17:53:36 +00001941/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1942/// spilled and create empty intervals for their uses.
1943void
1944LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1945 const TargetRegisterClass* rc,
1946 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001947 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1948 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001949 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001950 MachineInstr *MI = &*ri;
1951 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001952 if (O.isDef()) {
1953 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1954 "Register def was not rewritten?");
1955 RemoveMachineInstrFromMaps(MI);
1956 vrm.RemoveMachineInstrFromMaps(MI);
1957 MI->eraseFromParent();
1958 } else {
1959 // This must be an use of an implicit_def so it's not part of the live
1960 // interval. Create a new empty live interval for it.
1961 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1962 unsigned NewVReg = mri_->createVirtualRegister(rc);
1963 vrm.grow();
1964 vrm.setIsImplicitlyDefined(NewVReg);
1965 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1966 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1967 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001968 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001969 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001970 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001971 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001972 }
1973 }
Evan Cheng419852c2008-04-03 16:39:43 +00001974 }
1975}
1976
Evan Chengf2fbca62007-11-12 06:35:08 +00001977std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001978addIntervalsForSpillsFast(const LiveInterval &li,
1979 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001980 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001981 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001982
1983 std::vector<LiveInterval*> added;
1984
1985 assert(li.weight != HUGE_VALF &&
1986 "attempt to spill already spilled interval!");
1987
1988 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1989 DEBUG(li.dump());
1990 DOUT << '\n';
1991
1992 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1993
Owen Andersona41e47a2008-08-19 22:12:11 +00001994 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1995 while (RI != mri_->reg_end()) {
1996 MachineInstr* MI = &*RI;
1997
1998 SmallVector<unsigned, 2> Indices;
1999 bool HasUse = false;
2000 bool HasDef = false;
2001
2002 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2003 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002004 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002005
2006 HasUse |= MI->getOperand(i).isUse();
2007 HasDef |= MI->getOperand(i).isDef();
2008
2009 Indices.push_back(i);
2010 }
2011
2012 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2013 Indices, true, slot, li.reg)) {
2014 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002015 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002016 vrm.assignVirt2StackSlot(NewVReg, slot);
2017
Owen Andersona41e47a2008-08-19 22:12:11 +00002018 // create a new register for this spill
2019 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002020
Owen Andersona41e47a2008-08-19 22:12:11 +00002021 // the spill weight is now infinity as it
2022 // cannot be spilled again
2023 nI.weight = HUGE_VALF;
2024
2025 // Rewrite register operands to use the new vreg.
2026 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2027 E = Indices.end(); I != E; ++I) {
2028 MI->getOperand(*I).setReg(NewVReg);
2029
2030 if (MI->getOperand(*I).isUse())
2031 MI->getOperand(*I).setIsKill(true);
2032 }
2033
2034 // Fill in the new live interval.
2035 unsigned index = getInstructionIndex(MI);
2036 if (HasUse) {
2037 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002038 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002039 DOUT << " +" << LR;
2040 nI.addRange(LR);
2041 vrm.addRestorePoint(NewVReg, MI);
2042 }
2043 if (HasDef) {
2044 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002045 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002046 DOUT << " +" << LR;
2047 nI.addRange(LR);
2048 vrm.addSpillPoint(NewVReg, true, MI);
2049 }
2050
Owen Anderson17197312008-08-18 23:41:04 +00002051 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002052
Owen Andersona41e47a2008-08-19 22:12:11 +00002053 DOUT << "\t\t\t\tadded new interval: ";
2054 DEBUG(nI.dump());
2055 DOUT << '\n';
Owen Andersona41e47a2008-08-19 22:12:11 +00002056 }
Owen Anderson9a032932008-08-18 21:20:32 +00002057
Owen Anderson9a032932008-08-18 21:20:32 +00002058
Owen Andersona41e47a2008-08-19 22:12:11 +00002059 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002060 }
Owen Andersond6664312008-08-18 18:05:32 +00002061
2062 return added;
2063}
2064
2065std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002066addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002067 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002068 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002069
2070 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002071 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002072
Evan Chengf2fbca62007-11-12 06:35:08 +00002073 assert(li.weight != HUGE_VALF &&
2074 "attempt to spill already spilled interval!");
2075
2076 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00002077 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00002078 DOUT << '\n';
2079
Evan Cheng72eeb942008-12-05 17:00:16 +00002080 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002081 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002082 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002083 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002084 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2085 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002086 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002087 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002088
2089 unsigned NumValNums = li.getNumValNums();
2090 SmallVector<MachineInstr*, 4> ReMatDefs;
2091 ReMatDefs.resize(NumValNums, NULL);
2092 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2093 ReMatOrigDefs.resize(NumValNums, NULL);
2094 SmallVector<int, 4> ReMatIds;
2095 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2096 BitVector ReMatDelete(NumValNums);
2097 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2098
Evan Cheng81a03822007-11-17 00:40:40 +00002099 // Spilling a split live interval. It cannot be split any further. Also,
2100 // it's also guaranteed to be a single val# / range interval.
2101 if (vrm.getPreSplitReg(li.reg)) {
2102 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002103 // Unset the split kill marker on the last use.
2104 unsigned KillIdx = vrm.getKillPoint(li.reg);
2105 if (KillIdx) {
2106 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2107 assert(KillMI && "Last use disappeared?");
2108 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2109 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002110 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002111 }
Evan Chengadf85902007-12-05 09:51:10 +00002112 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002113 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2114 Slot = vrm.getStackSlot(li.reg);
2115 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2116 MachineInstr *ReMatDefMI = DefIsReMat ?
2117 vrm.getReMaterializedMI(li.reg) : NULL;
2118 int LdSlot = 0;
2119 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2120 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002121 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002122 bool IsFirstRange = true;
2123 for (LiveInterval::Ranges::const_iterator
2124 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2125 // If this is a split live interval with multiple ranges, it means there
2126 // are two-address instructions that re-defined the value. Only the
2127 // first def can be rematerialized!
2128 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002129 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002130 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2131 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002132 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002133 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002134 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002135 } else {
2136 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2137 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002138 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002139 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002140 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002141 }
2142 IsFirstRange = false;
2143 }
Evan Cheng419852c2008-04-03 16:39:43 +00002144
Evan Cheng4cce6b42008-04-11 17:53:36 +00002145 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002146 return NewLIs;
2147 }
2148
2149 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002150 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2151 TrySplit = false;
2152 if (TrySplit)
2153 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002154 bool NeedStackSlot = false;
2155 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2156 i != e; ++i) {
2157 const VNInfo *VNI = *i;
2158 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002159 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002160 continue; // Dead val#.
2161 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002162 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2163 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002164 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002165 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002166 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002167 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002168 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002169 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2170 ClonedMIs.push_back(Clone);
2171 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002172
2173 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002174 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002175 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002176 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002177 CanDelete = false;
2178 // Need a stack slot if there is any live range where uses cannot be
2179 // rematerialized.
2180 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002181 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002182 if (CanDelete)
2183 ReMatDelete.set(VN);
2184 } else {
2185 // Need a stack slot if there is any live range where uses cannot be
2186 // rematerialized.
2187 NeedStackSlot = true;
2188 }
2189 }
2190
2191 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002192 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2193 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2194 Slot = vrm.assignVirt2StackSlot(li.reg);
2195
2196 // This case only occurs when the prealloc splitter has already assigned
2197 // a stack slot to this vreg.
2198 else
2199 Slot = vrm.getStackSlot(li.reg);
2200 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002201
2202 // Create new intervals and rewrite defs and uses.
2203 for (LiveInterval::Ranges::const_iterator
2204 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002205 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2206 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2207 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002208 bool CanDelete = ReMatDelete[I->valno->id];
2209 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002210 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002211 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002212 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002213 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002214 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002215 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002216 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002217 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002218 }
2219
Evan Cheng0cbb1162007-11-29 01:06:25 +00002220 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002221 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002222 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002223 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002224 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002225
Evan Chengb50bb8c2007-12-05 08:16:32 +00002226 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002227 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002228 if (NeedStackSlot) {
2229 int Id = SpillMBBs.find_first();
2230 while (Id != -1) {
2231 std::vector<SRInfo> &spills = SpillIdxes[Id];
2232 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2233 int index = spills[i].index;
2234 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002235 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002236 bool isReMat = vrm.isReMaterialized(VReg);
2237 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002238 bool CanFold = false;
2239 bool FoundUse = false;
2240 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002241 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002242 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002243 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2244 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002245 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002246 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002247
2248 Ops.push_back(j);
2249 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002250 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002251 if (isReMat ||
2252 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2253 RestoreMBBs, RestoreIdxes))) {
2254 // MI has two-address uses of the same register. If the use
2255 // isn't the first and only use in the BB, then we can't fold
2256 // it. FIXME: Move this to rewriteInstructionsForSpills.
2257 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002258 break;
2259 }
Evan Chengaee4af62007-12-02 08:30:39 +00002260 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002261 }
2262 }
2263 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002264 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002265 if (CanFold && !Ops.empty()) {
2266 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002267 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002268 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002269 // Also folded uses, do not issue a load.
2270 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002271 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2272 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002273 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002274 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002275 }
2276
Evan Cheng7e073ba2008-04-09 20:57:25 +00002277 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002278 if (!Folded) {
2279 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2280 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002281 if (!MI->registerDefIsDead(nI.reg))
2282 // No need to spill a dead def.
2283 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002284 if (isKill)
2285 AddedKill.insert(&nI);
2286 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002287 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002288 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002289 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002290 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002291
Evan Cheng1953d0c2007-11-29 10:12:14 +00002292 int Id = RestoreMBBs.find_first();
2293 while (Id != -1) {
2294 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2295 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2296 int index = restores[i].index;
2297 if (index == -1)
2298 continue;
2299 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002300 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002301 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002302 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002303 bool CanFold = false;
2304 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002305 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002306 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002307 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2308 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002309 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002310 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002311
Evan Cheng0cbb1162007-11-29 01:06:25 +00002312 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002313 // If this restore were to be folded, it would have been folded
2314 // already.
2315 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002316 break;
2317 }
Evan Chengaee4af62007-12-02 08:30:39 +00002318 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002319 }
2320 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002321
2322 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002323 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002324 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002325 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002326 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2327 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002328 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2329 int LdSlot = 0;
2330 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2331 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002332 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002333 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2334 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002335 if (!Folded) {
2336 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2337 if (ImpUse) {
2338 // Re-matting an instruction with virtual register use. Add the
2339 // register as an implicit use on the use MI and update the register
2340 // interval's spill weight to HUGE_VALF to prevent it from being
2341 // spilled.
2342 LiveInterval &ImpLi = getInterval(ImpUse);
2343 ImpLi.weight = HUGE_VALF;
2344 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2345 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002346 }
Evan Chengaee4af62007-12-02 08:30:39 +00002347 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002348 }
2349 // If folding is not possible / failed, then tell the spiller to issue a
2350 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002351 if (Folded)
2352 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002353 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002354 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002355 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002356 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002357 }
2358
Evan Chengb50bb8c2007-12-05 08:16:32 +00002359 // Finalize intervals: add kills, finalize spill weights, and filter out
2360 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002361 std::vector<LiveInterval*> RetNewLIs;
2362 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2363 LiveInterval *LI = NewLIs[i];
2364 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002365 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002366 if (!AddedKill.count(LI)) {
2367 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002368 unsigned LastUseIdx = getBaseIndex(LR->end);
2369 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002370 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002371 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002372 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002373 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002374 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002375 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002376 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002377 RetNewLIs.push_back(LI);
2378 }
2379 }
Evan Cheng81a03822007-11-17 00:40:40 +00002380
Evan Cheng4cce6b42008-04-11 17:53:36 +00002381 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002382 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002383}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002384
2385/// hasAllocatableSuperReg - Return true if the specified physical register has
2386/// any super register that's allocatable.
2387bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2388 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2389 if (allocatableRegs_[*AS] && hasInterval(*AS))
2390 return true;
2391 return false;
2392}
2393
2394/// getRepresentativeReg - Find the largest super register of the specified
2395/// physical register.
2396unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2397 // Find the largest super-register that is allocatable.
2398 unsigned BestReg = Reg;
2399 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2400 unsigned SuperReg = *AS;
2401 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2402 BestReg = SuperReg;
2403 break;
2404 }
2405 }
2406 return BestReg;
2407}
2408
2409/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2410/// specified interval that conflicts with the specified physical register.
2411unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2412 unsigned PhysReg) const {
2413 unsigned NumConflicts = 0;
2414 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2415 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2416 E = mri_->reg_end(); I != E; ++I) {
2417 MachineOperand &O = I.getOperand();
2418 MachineInstr *MI = O.getParent();
2419 unsigned Index = getInstructionIndex(MI);
2420 if (pli.liveAt(Index))
2421 ++NumConflicts;
2422 }
2423 return NumConflicts;
2424}
2425
2426/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002427/// around all defs and uses of the specified interval. Return true if it
2428/// was able to cut its interval.
2429bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002430 unsigned PhysReg, VirtRegMap &vrm) {
2431 unsigned SpillReg = getRepresentativeReg(PhysReg);
2432
2433 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2434 // If there are registers which alias PhysReg, but which are not a
2435 // sub-register of the chosen representative super register. Assert
2436 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002437 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002438 tri_->isSuperRegister(*AS, SpillReg));
2439
Evan Cheng2824a652009-03-23 18:24:37 +00002440 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002441 LiveInterval &pli = getInterval(SpillReg);
2442 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2443 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2444 E = mri_->reg_end(); I != E; ++I) {
2445 MachineOperand &O = I.getOperand();
2446 MachineInstr *MI = O.getParent();
2447 if (SeenMIs.count(MI))
2448 continue;
2449 SeenMIs.insert(MI);
2450 unsigned Index = getInstructionIndex(MI);
2451 if (pli.liveAt(Index)) {
2452 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002453 unsigned StartIdx = getLoadIndex(Index);
2454 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002455 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002456 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002457 Cut = true;
2458 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002459 std::string msg;
2460 raw_string_ostream Msg(msg);
2461 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002462 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002463 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002464 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002465 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002466 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002467 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002468 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002469 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2470 if (!hasInterval(*AS))
2471 continue;
2472 LiveInterval &spli = getInterval(*AS);
2473 if (spli.liveAt(Index))
2474 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2475 }
2476 }
2477 }
Evan Cheng2824a652009-03-23 18:24:37 +00002478 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002479}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002480
2481LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002482 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002483 LiveInterval& Interval = getOrCreateInterval(reg);
2484 VNInfo* VN = Interval.getNextValue(
2485 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002486 startInst, true, getVNInfoAllocator());
2487 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002488 VN->kills.push_back(
2489 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002490 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2491 getMBBEndIdx(startInst->getParent()) + 1, VN);
2492 Interval.addRange(LR);
2493
2494 return LR;
2495}