blob: 79a4762560635b9c2cce168d30fb1409767742c5 [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 Greene340482d2009-07-22 21:56:14 +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";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000504 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
505 mbbi != mbbe; ++mbbi) {
506 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
507 for (MachineBasicBlock::iterator mii = mbbi->begin(),
508 mie = mbbi->end(); mii != mie; ++mii) {
509 O << getInstructionIndex(mii) << '\t' << *mii;
510 }
511 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000512}
513
Evan Chengc92da382007-11-03 07:20:12 +0000514/// conflictsWithPhysRegDef - Returns true if the specified register
515/// is defined during the duration of the specified interval.
516bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
517 VirtRegMap &vrm, unsigned reg) {
518 for (LiveInterval::Ranges::const_iterator
519 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
520 for (unsigned index = getBaseIndex(I->start),
521 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
522 index += InstrSlots::NUM) {
523 // skip deleted instructions
524 while (index != end && !getInstructionFromIndex(index))
525 index += InstrSlots::NUM;
526 if (index == end) break;
527
528 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000529 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
530 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000531 if (SrcReg == li.reg || DstReg == li.reg)
532 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000533 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
534 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000535 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000536 continue;
537 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000538 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000539 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000540 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000541 if (!vrm.hasPhys(PhysReg))
542 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000543 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000544 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000545 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000546 return true;
547 }
548 }
549 }
550
551 return false;
552}
553
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000554/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
555/// it can check use as well.
556bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
557 unsigned Reg, bool CheckUse,
558 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
559 for (LiveInterval::Ranges::const_iterator
560 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
561 for (unsigned index = getBaseIndex(I->start),
562 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
563 index += InstrSlots::NUM) {
564 // Skip deleted instructions.
565 MachineInstr *MI = 0;
566 while (index != end) {
567 MI = getInstructionFromIndex(index);
568 if (MI)
569 break;
570 index += InstrSlots::NUM;
571 }
572 if (index == end) break;
573
574 if (JoinedCopies.count(MI))
575 continue;
576 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
577 MachineOperand& MO = MI->getOperand(i);
578 if (!MO.isReg())
579 continue;
580 if (MO.isUse() && !CheckUse)
581 continue;
582 unsigned PhysReg = MO.getReg();
583 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
584 continue;
585 if (tri_->isSubRegister(Reg, PhysReg))
586 return true;
587 }
588 }
589 }
590
591 return false;
592}
593
594
Evan Cheng549f27d32007-08-13 23:45:17 +0000595void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000596 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000597 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000598 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000599 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000600}
601
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000602void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000603 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000604 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000605 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000606 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000607 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Evan Cheng419852c2008-04-03 16:39:43 +0000608
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000609 // Virtual registers may be defined multiple times (due to phi
610 // elimination and 2-addr elimination). Much of what we do only has to be
611 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000612 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000613 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 if (interval.empty()) {
615 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000616 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000617 // Earlyclobbers move back one.
618 if (MO.isEarlyClobber())
619 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000620 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000621 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000622 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000623 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000624 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000625 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000626 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000627 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000628 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000629 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000630
631 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000632
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000633 // Loop over all of the blocks that the vreg is defined in. There are
634 // two cases we have to handle here. The most common case is a vreg
635 // whose lifetime is contained within a basic block. In this case there
636 // will be a single kill, in MBB, which comes after the definition.
637 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
638 // FIXME: what about dead vars?
639 unsigned killIdx;
640 if (vi.Kills[0] != mi)
641 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
642 else
643 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000644
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000645 // If the kill happens after the definition, we have an intra-block
646 // live range.
647 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000648 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000649 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000650 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000652 DOUT << " +" << LR << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000653 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000654 return;
655 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000656 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000657
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000658 // The other case we handle is when a virtual register lives to the end
659 // of the defining block, potentially live across some blocks, then is
660 // live into some number of blocks, but gets killed. Start by adding a
661 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000662 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000663 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000664 interval.addRange(NewLR);
665
666 // Iterate over all of the blocks that the variable is completely
667 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
668 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000669 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
670 E = vi.AliveBlocks.end(); I != E; ++I) {
671 LiveRange LR(getMBBStartIdx(*I),
672 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000673 ValNo);
674 interval.addRange(LR);
675 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000676 }
677
678 // Finally, this virtual register is live from the start of any killing
679 // block to the 'use' slot of the killing instruction.
680 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
681 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000682 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000683 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000684 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000685 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000686 interval.addKill(ValNo, killIdx, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000687 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000688 }
689
690 } else {
691 // If this is the second time we see a virtual register definition, it
692 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000693 // the result of two address elimination, then the vreg is one of the
694 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000695 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000696 // If this is a two-address definition, then we have already processed
697 // the live range. The only problem is that we didn't realize there
698 // are actually two values in the live interval. Because of this we
699 // need to take the LiveRegion that defines this register and split it
700 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000701 assert(interval.containsOneValue());
702 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000703 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000704 if (MO.isEarlyClobber())
705 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000706
Evan Cheng4f8ff162007-08-11 00:59:19 +0000707 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000708 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000709
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000710 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000711 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000712 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000713
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000714 // Two-address vregs should always only be redefined once. This means
715 // that at this point, there should be exactly one value number in it.
716 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
717
Chris Lattner91725b72006-08-31 05:54:43 +0000718 // The new value number (#1) is defined by the instruction we claimed
719 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000720 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
Lang Hames857c4e02009-06-17 21:01:20 +0000721 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000722 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000723 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
724
Chris Lattner91725b72006-08-31 05:54:43 +0000725 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000726 OldValNo->def = RedefIndex;
727 OldValNo->copy = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000728 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000729 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000730
731 // Add the new live interval which replaces the range for the input copy.
732 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000733 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000735 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000736
737 // If this redefinition is dead, we need to add a dummy unit live
738 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000739 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000740 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000741
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000742 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000743 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000744
745 } else {
746 // Otherwise, this must be because of phi elimination. If this is the
747 // first redefinition of the vreg that we have seen, go back and change
748 // the live range in the PHI block to be a different value number.
749 if (interval.containsOneValue()) {
750 assert(vi.Kills.size() == 1 &&
751 "PHI elimination vreg should have one kill, the PHI itself!");
752
753 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000754 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000755 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000756 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000757 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000758 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000759 interval.print(DOUT, tri_); DOUT << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000760 interval.removeRange(Start, End);
761 assert(interval.ranges.size() == 1 &&
762 "newly discovered PHI interval has >1 ranges.");
763 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
764 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000765 VNI->setHasPHIKill(true);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000766 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000767
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000768 // Replace the interval with one of a NEW value number. Note that this
769 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000770 LiveRange LR(Start, End,
771 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000772 LR.valno->setIsPHIDef(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000773 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000774 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000775 interval.addKill(LR.valno, End, false);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000776 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000777 }
778
779 // In the case of PHI elimination, each variable definition is only
780 // live until the end of the block. We've already taken care of the
781 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000782 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000783 if (MO.isEarlyClobber())
784 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000785
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000786 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000787 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000788 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000789 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000790 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000791 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000792 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000793 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000794 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000795
Owen Anderson7fbad272008-07-23 21:37:49 +0000796 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000797 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000798 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000799 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000800 ValNo->setHasPHIKill(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000801 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000802 }
803 }
804
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000805 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000806}
807
Chris Lattnerf35fef72004-07-23 21:24:19 +0000808void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000809 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000810 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000811 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000812 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000813 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000814 // A physical register cannot be live across basic block, so its
815 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000816 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000817
Chris Lattner6b128bd2006-09-03 08:07:11 +0000818 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000819 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000820 // Earlyclobbers move back one.
821 if (MO.isEarlyClobber())
822 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000823 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000824
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000825 // If it is not used after definition, it is considered dead at
826 // the instruction defining it. Hence its interval is:
827 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000828 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000829 DOUT << " dead";
Dale Johannesen86b49f82008-09-24 01:07:17 +0000830 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000831 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000832 }
833
834 // If it is not dead on definition, it must be killed by a
835 // subsequent instruction. Hence its interval is:
836 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000837 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000838 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000839 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
840 getInstructionFromIndex(baseIndex) == 0)
841 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000842 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000843 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000844 end = getUseIndex(baseIndex) + 1;
845 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000846 } else {
847 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
848 if (DefIdx != -1) {
849 if (mi->isRegTiedToUseOperand(DefIdx)) {
850 // Two-address instruction.
851 end = getDefIndex(baseIndex);
852 if (mi->getOperand(DefIdx).isEarlyClobber())
853 end = getUseIndex(baseIndex);
854 } else {
855 // Another instruction redefines the register before it is ever read.
856 // Then the register is essentially dead at the instruction that defines
857 // it. Hence its interval is:
858 // [defSlot(def), defSlot(def)+1)
859 DOUT << " dead";
860 end = start + 1;
861 }
862 goto exit;
863 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000864 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000865
866 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000867 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000868
869 // The only case we should have a dead physreg here without a killing or
870 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000871 // and never used. Another possible case is the implicit use of the
872 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000873 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000874
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000875exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000876 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000877
Evan Cheng24a3cc42007-04-25 07:30:23 +0000878 // Already exists? Extend old live interval.
879 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000880 bool Extend = OldLR != interval.end();
881 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000882 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000883 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000884 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000885 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000886 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000887 interval.addKill(LR.valno, end, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000888 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000889}
890
Chris Lattnerf35fef72004-07-23 21:24:19 +0000891void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
892 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000893 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000894 MachineOperand& MO,
895 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000896 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000897 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000898 getOrCreateInterval(MO.getReg()));
899 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000900 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000901 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000902 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000903 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000904 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000905 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000906 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000907 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000908 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000909 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000910 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000911 // If MI also modifies the sub-register explicitly, avoid processing it
912 // more than once. Do not pass in TRI here so it checks for exact match.
913 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000914 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000915 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000916 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000917}
918
Evan Chengb371f452007-02-19 21:49:54 +0000919void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000920 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000921 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000922 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
923
924 // Look for kills, if it reaches a def before it's killed, then it shouldn't
925 // be considered a livein.
926 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000927 unsigned baseIndex = MIIdx;
928 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000929 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
930 getInstructionFromIndex(baseIndex) == 0)
931 baseIndex += InstrSlots::NUM;
932 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000933 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000934
Evan Chengb371f452007-02-19 21:49:54 +0000935 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000936 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000937 DOUT << " killed";
938 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000939 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000940 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000941 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000942 // Another instruction redefines the register before it is ever read.
943 // Then the register is essentially dead at the instruction that defines
944 // it. Hence its interval is:
945 // [defSlot(def), defSlot(def)+1)
946 DOUT << " dead";
947 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000948 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000949 break;
Evan Chengb371f452007-02-19 21:49:54 +0000950 }
951
952 baseIndex += InstrSlots::NUM;
953 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000954 if (mi != MBB->end()) {
955 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
956 getInstructionFromIndex(baseIndex) == 0)
957 baseIndex += InstrSlots::NUM;
958 }
Evan Chengb371f452007-02-19 21:49:54 +0000959 }
960
Evan Cheng75611fb2007-06-27 01:16:36 +0000961 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000962 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000963 if (isAlias) {
964 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000965 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000966 } else {
967 DOUT << " live through";
968 end = baseIndex;
969 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000970 }
971
Lang Hames10382fb2009-06-19 02:17:53 +0000972 VNInfo *vni =
973 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000974 vni->setIsPHIDef(true);
975 LiveRange LR(start, end, vni);
976
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000977 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000978 interval.addKill(LR.valno, end, false);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000979 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000980}
981
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000982/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000983/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000984/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000985/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000986void LiveIntervals::computeIntervals() {
Dale Johannesen91aac102008-09-17 21:13:11 +0000987
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000988 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
989 << "********** Function: "
990 << ((Value*)mf_->getFunction())->getName() << '\n';
Evan Chengd129d732009-07-17 19:43:40 +0000991
992 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000993 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
994 MBBI != E; ++MBBI) {
995 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000996 // Track the index of the current machine instr.
997 unsigned MIIndex = getMBBStartIdx(MBB);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000998 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000999
Chris Lattner428b92e2006-09-15 03:57:23 +00001000 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001001
Dan Gohmancb406c22007-10-03 19:26:29 +00001002 // Create intervals for live-ins to this BB first.
1003 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1004 LE = MBB->livein_end(); LI != LE; ++LI) {
1005 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1006 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001007 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001008 if (!hasInterval(*AS))
1009 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1010 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001011 }
1012
Owen Anderson99500ae2008-09-15 22:00:38 +00001013 // Skip over empty initial indices.
1014 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1015 getInstructionFromIndex(MIIndex) == 0)
1016 MIIndex += InstrSlots::NUM;
1017
Chris Lattner428b92e2006-09-15 03:57:23 +00001018 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001019 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001020
Evan Cheng438f7bc2006-11-10 08:43:01 +00001021 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001022 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1023 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001024 if (!MO.isReg() || !MO.getReg())
1025 continue;
1026
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001027 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001028 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001029 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001030 else if (MO.isUndef())
1031 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001032 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001033
1034 // Skip over the empty slots after each instruction.
1035 unsigned Slots = MI->getDesc().getNumDefs();
1036 if (Slots == 0)
1037 Slots = 1;
1038 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001039
1040 // Skip over empty indices.
1041 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1042 getInstructionFromIndex(MIIndex) == 0)
1043 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001044 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001045 }
Evan Chengd129d732009-07-17 19:43:40 +00001046
1047 // Create empty intervals for registers defined by implicit_def's (except
1048 // for those implicit_def that define values which are liveout of their
1049 // blocks.
1050 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1051 unsigned UndefReg = UndefUses[i];
1052 (void)getOrCreateInterval(UndefReg);
1053 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001054}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001055
Evan Chengd0e32c52008-10-29 05:06:14 +00001056bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001057 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001058 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001059 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001060
1061 bool ResVal = false;
1062 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001063 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001064 break;
1065 MBBs.push_back(I->second);
1066 ResVal = true;
1067 ++I;
1068 }
1069 return ResVal;
1070}
1071
Evan Chengd0e32c52008-10-29 05:06:14 +00001072bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1073 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1074 std::vector<IdxMBBPair>::const_iterator I =
1075 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1076
1077 bool ResVal = false;
1078 while (I != Idx2MBBMap.end()) {
1079 if (I->first > End)
1080 break;
1081 MachineBasicBlock *MBB = I->second;
1082 if (getMBBEndIdx(MBB) > End)
1083 break;
1084 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1085 SE = MBB->succ_end(); SI != SE; ++SI)
1086 MBBs.push_back(*SI);
1087 ResVal = true;
1088 ++I;
1089 }
1090 return ResVal;
1091}
1092
Owen Anderson03857b22008-08-13 21:49:13 +00001093LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001094 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001095 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001096}
Evan Chengf2fbca62007-11-12 06:35:08 +00001097
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001098/// dupInterval - Duplicate a live interval. The caller is responsible for
1099/// managing the allocated memory.
1100LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1101 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001102 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001103 return NewLI;
1104}
1105
Evan Chengc8d044e2008-02-15 18:24:29 +00001106/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1107/// copy field and returns the source register that defines it.
1108unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
1109 if (!VNI->copy)
1110 return 0;
1111
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001112 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1113 // If it's extracting out of a physical register, return the sub-register.
1114 unsigned Reg = VNI->copy->getOperand(1).getReg();
1115 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1116 Reg = tri_->getSubReg(Reg, VNI->copy->getOperand(2).getImm());
1117 return Reg;
Dan Gohman97121ba2009-04-08 00:15:30 +00001118 } else if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1119 VNI->copy->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng7e073ba2008-04-09 20:57:25 +00001120 return VNI->copy->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001121
Evan Cheng04ee5a12009-01-20 19:12:24 +00001122 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1123 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001124 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001125 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001126 return 0;
1127}
Evan Chengf2fbca62007-11-12 06:35:08 +00001128
1129//===----------------------------------------------------------------------===//
1130// Register allocator hooks.
1131//
1132
Evan Chengd70dbb52008-02-22 09:24:50 +00001133/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1134/// allow one) virtual register operand, then its uses are implicitly using
1135/// the register. Returns the virtual register.
1136unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1137 MachineInstr *MI) const {
1138 unsigned RegOp = 0;
1139 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1140 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001141 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001142 continue;
1143 unsigned Reg = MO.getReg();
1144 if (Reg == 0 || Reg == li.reg)
1145 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001146
1147 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1148 !allocatableRegs_[Reg])
1149 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001150 // FIXME: For now, only remat MI with at most one register operand.
1151 assert(!RegOp &&
1152 "Can't rematerialize instruction with multiple register operand!");
1153 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001154#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001155 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001156#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001157 }
1158 return RegOp;
1159}
1160
1161/// isValNoAvailableAt - Return true if the val# of the specified interval
1162/// which reaches the given instruction also reaches the specified use index.
1163bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1164 unsigned UseIdx) const {
1165 unsigned Index = getInstructionIndex(MI);
1166 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1167 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1168 return UI != li.end() && UI->valno == ValNo;
1169}
1170
Evan Chengf2fbca62007-11-12 06:35:08 +00001171/// isReMaterializable - Returns true if the definition MI of the specified
1172/// val# of the specified interval is re-materializable.
1173bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001174 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001175 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001176 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001177 if (DisableReMat)
1178 return false;
1179
Evan Cheng20ccded2008-03-15 00:19:36 +00001180 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001181 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001182
1183 int FrameIdx = 0;
1184 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001185 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001186 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1187 // this but remember this is not safe to fold into a two-address
1188 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001189 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001190 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001191
Dan Gohman6d69ba82008-07-25 00:02:30 +00001192 // If the target-specific rules don't identify an instruction as
1193 // being trivially rematerializable, use some target-independent
1194 // rules.
1195 if (!MI->getDesc().isRematerializable() ||
1196 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001197 if (!EnableAggressiveRemat)
1198 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001199
Dan Gohman0471a792008-07-28 18:43:51 +00001200 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001201 // we can't analyze it.
1202 const TargetInstrDesc &TID = MI->getDesc();
1203 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1204 return false;
1205
1206 // Avoid instructions obviously unsafe for remat.
1207 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1208 return false;
1209
1210 // If the instruction accesses memory and the memory could be non-constant,
1211 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001212 for (std::list<MachineMemOperand>::const_iterator
1213 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001214 const MachineMemOperand &MMO = *I;
1215 if (MMO.isVolatile() || MMO.isStore())
1216 return false;
1217 const Value *V = MMO.getValue();
1218 if (!V)
1219 return false;
1220 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1221 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001222 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001223 } else if (!aa_->pointsToConstantMemory(V))
1224 return false;
1225 }
1226
1227 // If any of the registers accessed are non-constant, conservatively assume
1228 // the instruction is not rematerializable.
1229 unsigned ImpUse = 0;
1230 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1231 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001232 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001233 unsigned Reg = MO.getReg();
1234 if (Reg == 0)
1235 continue;
1236 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1237 return false;
1238
1239 // Only allow one def, and that in the first operand.
1240 if (MO.isDef() != (i == 0))
1241 return false;
1242
1243 // Only allow constant-valued registers.
1244 bool IsLiveIn = mri_->isLiveIn(Reg);
1245 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1246 E = mri_->def_end();
1247
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001248 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001249 if (MO.isDef() && (next(I) != E || IsLiveIn))
1250 return false;
1251
1252 if (MO.isUse()) {
1253 // Only allow one use other register use, as that's all the
1254 // remat mechanisms support currently.
1255 if (Reg != li.reg) {
1256 if (ImpUse == 0)
1257 ImpUse = Reg;
1258 else if (Reg != ImpUse)
1259 return false;
1260 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001261 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001262 if (I != E && (next(I) != E || IsLiveIn))
1263 return false;
1264 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001265 }
1266 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001267 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001268
Dan Gohman6d69ba82008-07-25 00:02:30 +00001269 unsigned ImpUse = getReMatImplicitUse(li, MI);
1270 if (ImpUse) {
1271 const LiveInterval &ImpLi = getInterval(ImpUse);
1272 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1273 re = mri_->use_end(); ri != re; ++ri) {
1274 MachineInstr *UseMI = &*ri;
1275 unsigned UseIdx = getInstructionIndex(UseMI);
1276 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1277 continue;
1278 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1279 return false;
1280 }
Evan Chengdc377862008-09-30 15:44:16 +00001281
1282 // If a register operand of the re-materialized instruction is going to
1283 // be spilled next, then it's not legal to re-materialize this instruction.
1284 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1285 if (ImpUse == SpillIs[i]->reg)
1286 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001287 }
1288 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001289}
1290
Evan Cheng06587492008-10-24 02:05:00 +00001291/// isReMaterializable - Returns true if the definition MI of the specified
1292/// val# of the specified interval is re-materializable.
1293bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1294 const VNInfo *ValNo, MachineInstr *MI) {
1295 SmallVector<LiveInterval*, 4> Dummy1;
1296 bool Dummy2;
1297 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1298}
1299
Evan Cheng5ef3a042007-12-06 00:01:56 +00001300/// isReMaterializable - Returns true if every definition of MI of every
1301/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001302bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1303 SmallVectorImpl<LiveInterval*> &SpillIs,
1304 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001305 isLoad = false;
1306 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1307 i != e; ++i) {
1308 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001309 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001310 continue; // Dead val#.
1311 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001312 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001313 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001314 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001315 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001316 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001317 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001318 return false;
1319 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001320 }
1321 return true;
1322}
1323
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001324/// FilterFoldedOps - Filter out two-address use operands. Return
1325/// true if it finds any issue with the operands that ought to prevent
1326/// folding.
1327static bool FilterFoldedOps(MachineInstr *MI,
1328 SmallVector<unsigned, 2> &Ops,
1329 unsigned &MRInfo,
1330 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001331 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001332 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1333 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001334 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001335 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001336 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001337 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001338 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001339 MRInfo |= (unsigned)VirtRegMap::isMod;
1340 else {
1341 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001342 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001343 MRInfo = VirtRegMap::isModRef;
1344 continue;
1345 }
1346 MRInfo |= (unsigned)VirtRegMap::isRef;
1347 }
1348 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001349 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001350 return false;
1351}
1352
1353
1354/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1355/// slot / to reg or any rematerialized load into ith operand of specified
1356/// MI. If it is successul, MI is updated with the newly created MI and
1357/// returns true.
1358bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1359 VirtRegMap &vrm, MachineInstr *DefMI,
1360 unsigned InstrIdx,
1361 SmallVector<unsigned, 2> &Ops,
1362 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001363 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001364 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001365 RemoveMachineInstrFromMaps(MI);
1366 vrm.RemoveMachineInstrFromMaps(MI);
1367 MI->eraseFromParent();
1368 ++numFolds;
1369 return true;
1370 }
1371
1372 // Filter the list of operand indexes that are to be folded. Abort if
1373 // any operand will prevent folding.
1374 unsigned MRInfo = 0;
1375 SmallVector<unsigned, 2> FoldOps;
1376 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1377 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001378
Evan Cheng427f4c12008-03-31 23:19:51 +00001379 // The only time it's safe to fold into a two address instruction is when
1380 // it's folding reload and spill from / into a spill stack slot.
1381 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001382 return false;
1383
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001384 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1385 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001386 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001387 // Remember this instruction uses the spill slot.
1388 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1389
Evan Chengf2fbca62007-11-12 06:35:08 +00001390 // Attempt to fold the memory reference into the instruction. If
1391 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001392 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001393 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001394 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001395 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001396 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001397 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001398 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001399 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1400 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001401 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001402 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001403 return true;
1404 }
1405 return false;
1406}
1407
Evan Cheng018f9b02007-12-05 03:22:34 +00001408/// canFoldMemoryOperand - Returns true if the specified load / store
1409/// folding is possible.
1410bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001411 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001412 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001413 // Filter the list of operand indexes that are to be folded. Abort if
1414 // any operand will prevent folding.
1415 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001416 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001417 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1418 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001419
Evan Cheng3c75ba82008-04-01 21:37:32 +00001420 // It's only legal to remat for a use, not a def.
1421 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001422 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001423
Evan Chengd70dbb52008-02-22 09:24:50 +00001424 return tii_->canFoldMemoryOperand(MI, FoldOps);
1425}
1426
Evan Cheng81a03822007-11-17 00:40:40 +00001427bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1428 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1429 for (LiveInterval::Ranges::const_iterator
1430 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1431 std::vector<IdxMBBPair>::const_iterator II =
1432 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1433 if (II == Idx2MBBMap.end())
1434 continue;
1435 if (I->end > II->first) // crossing a MBB.
1436 return false;
1437 MBBs.insert(II->second);
1438 if (MBBs.size() > 1)
1439 return false;
1440 }
1441 return true;
1442}
1443
Evan Chengd70dbb52008-02-22 09:24:50 +00001444/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1445/// interval on to-be re-materialized operands of MI) with new register.
1446void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1447 MachineInstr *MI, unsigned NewVReg,
1448 VirtRegMap &vrm) {
1449 // There is an implicit use. That means one of the other operand is
1450 // being remat'ed and the remat'ed instruction has li.reg as an
1451 // use operand. Make sure we rewrite that as well.
1452 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1453 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001454 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001455 continue;
1456 unsigned Reg = MO.getReg();
1457 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1458 continue;
1459 if (!vrm.isReMaterialized(Reg))
1460 continue;
1461 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001462 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1463 if (UseMO)
1464 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001465 }
1466}
1467
Evan Chengf2fbca62007-11-12 06:35:08 +00001468/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1469/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001470bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001471rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1472 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001473 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001474 unsigned Slot, int LdSlot,
1475 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001476 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001477 const TargetRegisterClass* rc,
1478 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001479 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001480 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001481 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001482 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001483 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001484 RestartInstruction:
1485 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1486 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001487 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001488 continue;
1489 unsigned Reg = mop.getReg();
1490 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001491 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001492 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001493 if (Reg != li.reg)
1494 continue;
1495
1496 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001497 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001498 int FoldSlot = Slot;
1499 if (DefIsReMat) {
1500 // If this is the rematerializable definition MI itself and
1501 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001502 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001503 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1504 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001505 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001506 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001507 MI->eraseFromParent();
1508 break;
1509 }
1510
1511 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001512 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001513 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001514 if (isLoad) {
1515 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1516 FoldSS = isLoadSS;
1517 FoldSlot = LdSlot;
1518 }
1519 }
1520
Evan Chengf2fbca62007-11-12 06:35:08 +00001521 // Scan all of the operands of this instruction rewriting operands
1522 // to use NewVReg instead of li.reg as appropriate. We do this for
1523 // two reasons:
1524 //
1525 // 1. If the instr reads the same spilled vreg multiple times, we
1526 // want to reuse the NewVReg.
1527 // 2. If the instr is a two-addr instruction, we are required to
1528 // keep the src/dst regs pinned.
1529 //
1530 // Keep track of whether we replace a use and/or def so that we can
1531 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001532
Evan Cheng81a03822007-11-17 00:40:40 +00001533 HasUse = mop.isUse();
1534 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001535 SmallVector<unsigned, 2> Ops;
1536 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001537 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001538 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001539 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001540 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001541 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001542 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001543 continue;
1544 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001545 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001546 if (!MOj.isUndef()) {
1547 HasUse |= MOj.isUse();
1548 HasDef |= MOj.isDef();
1549 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001550 }
1551 }
1552
David Greene26b86a02008-10-27 17:38:59 +00001553 // Create a new virtual register for the spill interval.
1554 // Create the new register now so we can map the fold instruction
1555 // to the new register so when it is unfolded we get the correct
1556 // answer.
1557 bool CreatedNewVReg = false;
1558 if (NewVReg == 0) {
1559 NewVReg = mri_->createVirtualRegister(rc);
1560 vrm.grow();
1561 CreatedNewVReg = true;
1562 }
1563
Evan Cheng9c3c2212008-06-06 07:54:39 +00001564 if (!TryFold)
1565 CanFold = false;
1566 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001567 // Do not fold load / store here if we are splitting. We'll find an
1568 // optimal point to insert a load / store later.
1569 if (!TrySplit) {
1570 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001571 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001572 // Folding the load/store can completely change the instruction in
1573 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001574
1575 if (FoldSS) {
1576 // We need to give the new vreg the same stack slot as the
1577 // spilled interval.
1578 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1579 }
1580
Evan Cheng018f9b02007-12-05 03:22:34 +00001581 HasUse = false;
1582 HasDef = false;
1583 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001584 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001585 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001586 goto RestartInstruction;
1587 }
1588 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001589 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001590 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001591 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001592 }
Evan Chengcddbb832007-11-30 21:23:43 +00001593
Evan Chengcddbb832007-11-30 21:23:43 +00001594 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001595 if (mop.isImplicit())
1596 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001597
1598 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001599 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1600 MachineOperand &mopj = MI->getOperand(Ops[j]);
1601 mopj.setReg(NewVReg);
1602 if (mopj.isImplicit())
1603 rewriteImplicitOps(li, MI, NewVReg, vrm);
1604 }
Evan Chengcddbb832007-11-30 21:23:43 +00001605
Evan Cheng81a03822007-11-17 00:40:40 +00001606 if (CreatedNewVReg) {
1607 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001608 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001609 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001610 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001611 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001612 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001613 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001614 }
1615 if (!CanDelete || (HasUse && HasDef)) {
1616 // If this is a two-addr instruction then its use operands are
1617 // rematerializable but its def is not. It should be assigned a
1618 // stack slot.
1619 vrm.assignVirt2StackSlot(NewVReg, Slot);
1620 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001621 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001622 vrm.assignVirt2StackSlot(NewVReg, Slot);
1623 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001624 } else if (HasUse && HasDef &&
1625 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1626 // If this interval hasn't been assigned a stack slot (because earlier
1627 // def is a deleted remat def), do it now.
1628 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1629 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001630 }
1631
Evan Cheng313d4b82008-02-23 00:33:04 +00001632 // Re-matting an instruction with virtual register use. Add the
1633 // register as an implicit use on the use MI.
1634 if (DefIsReMat && ImpUse)
1635 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1636
Evan Cheng5b69eba2009-04-21 22:46:52 +00001637 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001638 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001639 if (CreatedNewVReg) {
1640 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001641 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001642 if (TrySplit)
1643 vrm.setIsSplitFromReg(NewVReg, li.reg);
1644 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001645
1646 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001647 if (CreatedNewVReg) {
1648 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001649 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Cheng81a03822007-11-17 00:40:40 +00001650 DOUT << " +" << LR;
1651 nI.addRange(LR);
1652 } else {
1653 // Extend the split live interval to this def / use.
1654 unsigned End = getUseIndex(index)+1;
1655 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1656 nI.getValNumInfo(nI.getNumValNums()-1));
1657 DOUT << " +" << LR;
1658 nI.addRange(LR);
1659 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001660 }
1661 if (HasDef) {
1662 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001663 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Chengf2fbca62007-11-12 06:35:08 +00001664 DOUT << " +" << LR;
1665 nI.addRange(LR);
1666 }
Evan Cheng81a03822007-11-17 00:40:40 +00001667
Evan Chengf2fbca62007-11-12 06:35:08 +00001668 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001669 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001670 DOUT << '\n';
1671 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001672 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001673}
Evan Cheng81a03822007-11-17 00:40:40 +00001674bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001675 const VNInfo *VNI,
1676 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001677 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001678 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001679 if (VNI->kills[j].isPHIKill)
1680 continue;
1681
1682 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001683 if (KillIdx > Idx && KillIdx < End)
1684 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001685 }
1686 return false;
1687}
1688
Evan Cheng063284c2008-02-21 00:34:19 +00001689/// RewriteInfo - Keep track of machine instrs that will be rewritten
1690/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001691namespace {
1692 struct RewriteInfo {
1693 unsigned Index;
1694 MachineInstr *MI;
1695 bool HasUse;
1696 bool HasDef;
1697 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1698 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1699 };
Evan Cheng063284c2008-02-21 00:34:19 +00001700
Dan Gohman844731a2008-05-13 00:00:25 +00001701 struct RewriteInfoCompare {
1702 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1703 return LHS.Index < RHS.Index;
1704 }
1705 };
1706}
Evan Cheng063284c2008-02-21 00:34:19 +00001707
Evan Chengf2fbca62007-11-12 06:35:08 +00001708void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001709rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001710 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001711 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001712 unsigned Slot, int LdSlot,
1713 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001714 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001715 const TargetRegisterClass* rc,
1716 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001717 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001718 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001719 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001720 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001721 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1722 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001723 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001724 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001725 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001726 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001727 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001728
Evan Cheng063284c2008-02-21 00:34:19 +00001729 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001730 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001731 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001732 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1733 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001734 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001735 MachineOperand &O = ri.getOperand();
1736 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001737 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001738 unsigned index = getInstructionIndex(MI);
1739 if (index < start || index >= end)
1740 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001741
1742 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001743 // Must be defined by an implicit def. It should not be spilled. Note,
1744 // this is for correctness reason. e.g.
1745 // 8 %reg1024<def> = IMPLICIT_DEF
1746 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1747 // The live range [12, 14) are not part of the r1024 live interval since
1748 // it's defined by an implicit def. It will not conflicts with live
1749 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001750 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001751 // the INSERT_SUBREG and both target registers that would overlap.
1752 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001753 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1754 }
1755 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1756
Evan Cheng313d4b82008-02-23 00:33:04 +00001757 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001758 // Now rewrite the defs and uses.
1759 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1760 RewriteInfo &rwi = RewriteMIs[i];
1761 ++i;
1762 unsigned index = rwi.Index;
1763 bool MIHasUse = rwi.HasUse;
1764 bool MIHasDef = rwi.HasDef;
1765 MachineInstr *MI = rwi.MI;
1766 // If MI def and/or use the same register multiple times, then there
1767 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001768 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001769 while (i != e && RewriteMIs[i].MI == MI) {
1770 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001771 bool isUse = RewriteMIs[i].HasUse;
1772 if (isUse) ++NumUses;
1773 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001774 MIHasDef |= RewriteMIs[i].HasDef;
1775 ++i;
1776 }
Evan Cheng81a03822007-11-17 00:40:40 +00001777 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001778
Evan Cheng0a891ed2008-05-23 23:00:04 +00001779 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001780 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001781 // register interval's spill weight to HUGE_VALF to prevent it from
1782 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001783 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001784 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001785 }
1786
Evan Cheng063284c2008-02-21 00:34:19 +00001787 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001788 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001789 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001790 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001791 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001792 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001793 // One common case:
1794 // x = use
1795 // ...
1796 // ...
1797 // def = ...
1798 // = use
1799 // It's better to start a new interval to avoid artifically
1800 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001801 if (MIHasDef && !MIHasUse) {
1802 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001803 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001804 }
1805 }
Evan Chengcada2452007-11-28 01:28:46 +00001806 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001807
1808 bool IsNew = ThisVReg == 0;
1809 if (IsNew) {
1810 // This ends the previous live interval. If all of its def / use
1811 // can be folded, give it a low spill weight.
1812 if (NewVReg && TrySplit && AllCanFold) {
1813 LiveInterval &nI = getOrCreateInterval(NewVReg);
1814 nI.weight /= 10.0F;
1815 }
1816 AllCanFold = true;
1817 }
1818 NewVReg = ThisVReg;
1819
Evan Cheng81a03822007-11-17 00:40:40 +00001820 bool HasDef = false;
1821 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001822 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001823 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1824 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1825 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001826 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001827 if (!HasDef && !HasUse)
1828 continue;
1829
Evan Cheng018f9b02007-12-05 03:22:34 +00001830 AllCanFold &= CanFold;
1831
Evan Cheng81a03822007-11-17 00:40:40 +00001832 // Update weight of spill interval.
1833 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001834 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001835 // The spill weight is now infinity as it cannot be spilled again.
1836 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001837 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001838 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001839
1840 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001841 if (HasDef) {
1842 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001843 bool HasKill = false;
1844 if (!HasUse)
1845 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1846 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001847 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001848 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001849 if (VNI)
1850 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1851 }
Owen Anderson28998312008-08-13 22:28:50 +00001852 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001853 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001854 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001855 if (SII == SpillIdxes.end()) {
1856 std::vector<SRInfo> S;
1857 S.push_back(SRInfo(index, NewVReg, true));
1858 SpillIdxes.insert(std::make_pair(MBBId, S));
1859 } else if (SII->second.back().vreg != NewVReg) {
1860 SII->second.push_back(SRInfo(index, NewVReg, true));
1861 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001862 // If there is an earlier def and this is a two-address
1863 // instruction, then it's not possible to fold the store (which
1864 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001865 SRInfo &Info = SII->second.back();
1866 Info.index = index;
1867 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001868 }
1869 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001870 } else if (SII != SpillIdxes.end() &&
1871 SII->second.back().vreg == NewVReg &&
1872 (int)index > SII->second.back().index) {
1873 // There is an earlier def that's not killed (must be two-address).
1874 // The spill is no longer needed.
1875 SII->second.pop_back();
1876 if (SII->second.empty()) {
1877 SpillIdxes.erase(MBBId);
1878 SpillMBBs.reset(MBBId);
1879 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001880 }
1881 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001882 }
1883
1884 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001885 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001886 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001887 if (SII != SpillIdxes.end() &&
1888 SII->second.back().vreg == NewVReg &&
1889 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001890 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001891 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001892 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001893 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001894 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001895 // If we are splitting live intervals, only fold if it's the first
1896 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001897 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001898 else if (IsNew) {
1899 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001900 if (RII == RestoreIdxes.end()) {
1901 std::vector<SRInfo> Infos;
1902 Infos.push_back(SRInfo(index, NewVReg, true));
1903 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1904 } else {
1905 RII->second.push_back(SRInfo(index, NewVReg, true));
1906 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001907 RestoreMBBs.set(MBBId);
1908 }
1909 }
1910
1911 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001912 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001913 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001914 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001915
1916 if (NewVReg && TrySplit && AllCanFold) {
1917 // If all of its def / use can be folded, give it a low spill weight.
1918 LiveInterval &nI = getOrCreateInterval(NewVReg);
1919 nI.weight /= 10.0F;
1920 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001921}
1922
Evan Cheng1953d0c2007-11-29 10:12:14 +00001923bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1924 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001925 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001926 if (!RestoreMBBs[Id])
1927 return false;
1928 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1929 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1930 if (Restores[i].index == index &&
1931 Restores[i].vreg == vr &&
1932 Restores[i].canFold)
1933 return true;
1934 return false;
1935}
1936
1937void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1938 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001939 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001940 if (!RestoreMBBs[Id])
1941 return;
1942 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1943 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1944 if (Restores[i].index == index && Restores[i].vreg)
1945 Restores[i].index = -1;
1946}
Evan Cheng81a03822007-11-17 00:40:40 +00001947
Evan Cheng4cce6b42008-04-11 17:53:36 +00001948/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1949/// spilled and create empty intervals for their uses.
1950void
1951LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1952 const TargetRegisterClass* rc,
1953 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001954 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1955 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001956 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001957 MachineInstr *MI = &*ri;
1958 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001959 if (O.isDef()) {
1960 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1961 "Register def was not rewritten?");
1962 RemoveMachineInstrFromMaps(MI);
1963 vrm.RemoveMachineInstrFromMaps(MI);
1964 MI->eraseFromParent();
1965 } else {
1966 // This must be an use of an implicit_def so it's not part of the live
1967 // interval. Create a new empty live interval for it.
1968 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1969 unsigned NewVReg = mri_->createVirtualRegister(rc);
1970 vrm.grow();
1971 vrm.setIsImplicitlyDefined(NewVReg);
1972 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1973 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1974 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001975 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001976 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001977 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001978 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001979 }
1980 }
Evan Cheng419852c2008-04-03 16:39:43 +00001981 }
1982}
1983
Evan Chengf2fbca62007-11-12 06:35:08 +00001984std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001985addIntervalsForSpillsFast(const LiveInterval &li,
1986 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001987 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001988 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001989
1990 std::vector<LiveInterval*> added;
1991
1992 assert(li.weight != HUGE_VALF &&
1993 "attempt to spill already spilled interval!");
1994
1995 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1996 DEBUG(li.dump());
1997 DOUT << '\n';
1998
1999 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2000
Owen Andersona41e47a2008-08-19 22:12:11 +00002001 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2002 while (RI != mri_->reg_end()) {
2003 MachineInstr* MI = &*RI;
2004
2005 SmallVector<unsigned, 2> Indices;
2006 bool HasUse = false;
2007 bool HasDef = false;
2008
2009 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2010 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002011 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002012
2013 HasUse |= MI->getOperand(i).isUse();
2014 HasDef |= MI->getOperand(i).isDef();
2015
2016 Indices.push_back(i);
2017 }
2018
2019 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2020 Indices, true, slot, li.reg)) {
2021 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002022 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002023 vrm.assignVirt2StackSlot(NewVReg, slot);
2024
Owen Andersona41e47a2008-08-19 22:12:11 +00002025 // create a new register for this spill
2026 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002027
Owen Andersona41e47a2008-08-19 22:12:11 +00002028 // the spill weight is now infinity as it
2029 // cannot be spilled again
2030 nI.weight = HUGE_VALF;
2031
2032 // Rewrite register operands to use the new vreg.
2033 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2034 E = Indices.end(); I != E; ++I) {
2035 MI->getOperand(*I).setReg(NewVReg);
2036
2037 if (MI->getOperand(*I).isUse())
2038 MI->getOperand(*I).setIsKill(true);
2039 }
2040
2041 // Fill in the new live interval.
2042 unsigned index = getInstructionIndex(MI);
2043 if (HasUse) {
2044 LiveRange LR(getLoadIndex(index), getUseIndex(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.addRestorePoint(NewVReg, MI);
2049 }
2050 if (HasDef) {
2051 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002052 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002053 DOUT << " +" << LR;
2054 nI.addRange(LR);
2055 vrm.addSpillPoint(NewVReg, true, MI);
2056 }
2057
Owen Anderson17197312008-08-18 23:41:04 +00002058 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002059
Owen Andersona41e47a2008-08-19 22:12:11 +00002060 DOUT << "\t\t\t\tadded new interval: ";
2061 DEBUG(nI.dump());
2062 DOUT << '\n';
Owen Andersona41e47a2008-08-19 22:12:11 +00002063 }
Owen Anderson9a032932008-08-18 21:20:32 +00002064
Owen Anderson9a032932008-08-18 21:20:32 +00002065
Owen Andersona41e47a2008-08-19 22:12:11 +00002066 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002067 }
Owen Andersond6664312008-08-18 18:05:32 +00002068
2069 return added;
2070}
2071
2072std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002073addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002074 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002075 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002076
2077 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002078 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002079
Evan Chengf2fbca62007-11-12 06:35:08 +00002080 assert(li.weight != HUGE_VALF &&
2081 "attempt to spill already spilled interval!");
2082
2083 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00002084 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00002085 DOUT << '\n';
2086
Evan Cheng72eeb942008-12-05 17:00:16 +00002087 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002088 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002089 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002090 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002091 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2092 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002093 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002094 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002095
2096 unsigned NumValNums = li.getNumValNums();
2097 SmallVector<MachineInstr*, 4> ReMatDefs;
2098 ReMatDefs.resize(NumValNums, NULL);
2099 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2100 ReMatOrigDefs.resize(NumValNums, NULL);
2101 SmallVector<int, 4> ReMatIds;
2102 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2103 BitVector ReMatDelete(NumValNums);
2104 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2105
Evan Cheng81a03822007-11-17 00:40:40 +00002106 // Spilling a split live interval. It cannot be split any further. Also,
2107 // it's also guaranteed to be a single val# / range interval.
2108 if (vrm.getPreSplitReg(li.reg)) {
2109 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002110 // Unset the split kill marker on the last use.
2111 unsigned KillIdx = vrm.getKillPoint(li.reg);
2112 if (KillIdx) {
2113 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2114 assert(KillMI && "Last use disappeared?");
2115 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2116 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002117 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002118 }
Evan Chengadf85902007-12-05 09:51:10 +00002119 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002120 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2121 Slot = vrm.getStackSlot(li.reg);
2122 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2123 MachineInstr *ReMatDefMI = DefIsReMat ?
2124 vrm.getReMaterializedMI(li.reg) : NULL;
2125 int LdSlot = 0;
2126 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2127 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002128 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002129 bool IsFirstRange = true;
2130 for (LiveInterval::Ranges::const_iterator
2131 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2132 // If this is a split live interval with multiple ranges, it means there
2133 // are two-address instructions that re-defined the value. Only the
2134 // first def can be rematerialized!
2135 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002136 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002137 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2138 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002139 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002140 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002141 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002142 } else {
2143 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2144 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002145 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002146 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002147 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002148 }
2149 IsFirstRange = false;
2150 }
Evan Cheng419852c2008-04-03 16:39:43 +00002151
Evan Cheng4cce6b42008-04-11 17:53:36 +00002152 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002153 return NewLIs;
2154 }
2155
2156 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002157 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2158 TrySplit = false;
2159 if (TrySplit)
2160 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002161 bool NeedStackSlot = false;
2162 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2163 i != e; ++i) {
2164 const VNInfo *VNI = *i;
2165 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002166 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002167 continue; // Dead val#.
2168 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002169 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2170 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002171 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002172 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002173 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002174 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002175 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002176 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2177 ClonedMIs.push_back(Clone);
2178 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002179
2180 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002181 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002182 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002183 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002184 CanDelete = false;
2185 // Need a stack slot if there is any live range where uses cannot be
2186 // rematerialized.
2187 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002188 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002189 if (CanDelete)
2190 ReMatDelete.set(VN);
2191 } else {
2192 // Need a stack slot if there is any live range where uses cannot be
2193 // rematerialized.
2194 NeedStackSlot = true;
2195 }
2196 }
2197
2198 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002199 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2200 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2201 Slot = vrm.assignVirt2StackSlot(li.reg);
2202
2203 // This case only occurs when the prealloc splitter has already assigned
2204 // a stack slot to this vreg.
2205 else
2206 Slot = vrm.getStackSlot(li.reg);
2207 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002208
2209 // Create new intervals and rewrite defs and uses.
2210 for (LiveInterval::Ranges::const_iterator
2211 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002212 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2213 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2214 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002215 bool CanDelete = ReMatDelete[I->valno->id];
2216 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002217 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002218 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002219 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002220 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002221 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002222 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002223 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002224 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002225 }
2226
Evan Cheng0cbb1162007-11-29 01:06:25 +00002227 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002228 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002229 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002230 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002231 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002232
Evan Chengb50bb8c2007-12-05 08:16:32 +00002233 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002234 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002235 if (NeedStackSlot) {
2236 int Id = SpillMBBs.find_first();
2237 while (Id != -1) {
2238 std::vector<SRInfo> &spills = SpillIdxes[Id];
2239 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2240 int index = spills[i].index;
2241 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002242 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002243 bool isReMat = vrm.isReMaterialized(VReg);
2244 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002245 bool CanFold = false;
2246 bool FoundUse = false;
2247 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002248 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002249 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002250 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2251 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002252 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002253 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002254
2255 Ops.push_back(j);
2256 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002257 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002258 if (isReMat ||
2259 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2260 RestoreMBBs, RestoreIdxes))) {
2261 // MI has two-address uses of the same register. If the use
2262 // isn't the first and only use in the BB, then we can't fold
2263 // it. FIXME: Move this to rewriteInstructionsForSpills.
2264 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002265 break;
2266 }
Evan Chengaee4af62007-12-02 08:30:39 +00002267 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002268 }
2269 }
2270 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002271 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002272 if (CanFold && !Ops.empty()) {
2273 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002274 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002275 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002276 // Also folded uses, do not issue a load.
2277 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002278 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2279 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002280 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002281 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002282 }
2283
Evan Cheng7e073ba2008-04-09 20:57:25 +00002284 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002285 if (!Folded) {
2286 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2287 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002288 if (!MI->registerDefIsDead(nI.reg))
2289 // No need to spill a dead def.
2290 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002291 if (isKill)
2292 AddedKill.insert(&nI);
2293 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002294 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002295 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002296 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002297 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002298
Evan Cheng1953d0c2007-11-29 10:12:14 +00002299 int Id = RestoreMBBs.find_first();
2300 while (Id != -1) {
2301 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2302 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2303 int index = restores[i].index;
2304 if (index == -1)
2305 continue;
2306 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002307 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002308 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002309 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002310 bool CanFold = false;
2311 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002312 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002313 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002314 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2315 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002316 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002317 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002318
Evan Cheng0cbb1162007-11-29 01:06:25 +00002319 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002320 // If this restore were to be folded, it would have been folded
2321 // already.
2322 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002323 break;
2324 }
Evan Chengaee4af62007-12-02 08:30:39 +00002325 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002326 }
2327 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002328
2329 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002330 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002331 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002332 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002333 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2334 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002335 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2336 int LdSlot = 0;
2337 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2338 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002339 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002340 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2341 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002342 if (!Folded) {
2343 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2344 if (ImpUse) {
2345 // Re-matting an instruction with virtual register use. Add the
2346 // register as an implicit use on the use MI and update the register
2347 // interval's spill weight to HUGE_VALF to prevent it from being
2348 // spilled.
2349 LiveInterval &ImpLi = getInterval(ImpUse);
2350 ImpLi.weight = HUGE_VALF;
2351 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2352 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002353 }
Evan Chengaee4af62007-12-02 08:30:39 +00002354 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002355 }
2356 // If folding is not possible / failed, then tell the spiller to issue a
2357 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002358 if (Folded)
2359 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002360 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002361 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002362 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002363 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002364 }
2365
Evan Chengb50bb8c2007-12-05 08:16:32 +00002366 // Finalize intervals: add kills, finalize spill weights, and filter out
2367 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002368 std::vector<LiveInterval*> RetNewLIs;
2369 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2370 LiveInterval *LI = NewLIs[i];
2371 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002372 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002373 if (!AddedKill.count(LI)) {
2374 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002375 unsigned LastUseIdx = getBaseIndex(LR->end);
2376 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002377 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002378 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002379 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002380 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002381 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002382 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002383 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002384 RetNewLIs.push_back(LI);
2385 }
2386 }
Evan Cheng81a03822007-11-17 00:40:40 +00002387
Evan Cheng4cce6b42008-04-11 17:53:36 +00002388 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002389 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002390}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002391
2392/// hasAllocatableSuperReg - Return true if the specified physical register has
2393/// any super register that's allocatable.
2394bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2395 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2396 if (allocatableRegs_[*AS] && hasInterval(*AS))
2397 return true;
2398 return false;
2399}
2400
2401/// getRepresentativeReg - Find the largest super register of the specified
2402/// physical register.
2403unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2404 // Find the largest super-register that is allocatable.
2405 unsigned BestReg = Reg;
2406 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2407 unsigned SuperReg = *AS;
2408 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2409 BestReg = SuperReg;
2410 break;
2411 }
2412 }
2413 return BestReg;
2414}
2415
2416/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2417/// specified interval that conflicts with the specified physical register.
2418unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2419 unsigned PhysReg) const {
2420 unsigned NumConflicts = 0;
2421 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2422 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2423 E = mri_->reg_end(); I != E; ++I) {
2424 MachineOperand &O = I.getOperand();
2425 MachineInstr *MI = O.getParent();
2426 unsigned Index = getInstructionIndex(MI);
2427 if (pli.liveAt(Index))
2428 ++NumConflicts;
2429 }
2430 return NumConflicts;
2431}
2432
2433/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002434/// around all defs and uses of the specified interval. Return true if it
2435/// was able to cut its interval.
2436bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002437 unsigned PhysReg, VirtRegMap &vrm) {
2438 unsigned SpillReg = getRepresentativeReg(PhysReg);
2439
2440 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2441 // If there are registers which alias PhysReg, but which are not a
2442 // sub-register of the chosen representative super register. Assert
2443 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002444 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002445 tri_->isSuperRegister(*AS, SpillReg));
2446
Evan Cheng2824a652009-03-23 18:24:37 +00002447 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002448 LiveInterval &pli = getInterval(SpillReg);
2449 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2450 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2451 E = mri_->reg_end(); I != E; ++I) {
2452 MachineOperand &O = I.getOperand();
2453 MachineInstr *MI = O.getParent();
2454 if (SeenMIs.count(MI))
2455 continue;
2456 SeenMIs.insert(MI);
2457 unsigned Index = getInstructionIndex(MI);
2458 if (pli.liveAt(Index)) {
2459 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002460 unsigned StartIdx = getLoadIndex(Index);
2461 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002462 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002463 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002464 Cut = true;
2465 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002466 std::string msg;
2467 raw_string_ostream Msg(msg);
2468 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002469 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002470 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002471 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002472 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002473 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002474 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002475 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002476 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2477 if (!hasInterval(*AS))
2478 continue;
2479 LiveInterval &spli = getInterval(*AS);
2480 if (spli.liveAt(Index))
2481 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2482 }
2483 }
2484 }
Evan Cheng2824a652009-03-23 18:24:37 +00002485 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002486}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002487
2488LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002489 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002490 LiveInterval& Interval = getOrCreateInterval(reg);
2491 VNInfo* VN = Interval.getNextValue(
2492 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002493 startInst, true, getVNInfoAllocator());
2494 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002495 VN->kills.push_back(
2496 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002497 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2498 getMBBEndIdx(startInst->getParent()) + 1, VN);
2499 Interval.addRange(LR);
2500
2501 return LR;
2502}