blob: 63a391a319ac9203d6966b577c64eda74a0c7fb2 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Dan Gohman844731a2008-05-13 00:00:25 +000052static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
53 cl::init(true), cl::Hidden);
54static cl::opt<int> SplitLimit("split-limit",
55 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000056
Dan Gohman4c8f8702008-07-25 15:08:37 +000057static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
58
Owen Andersonae339ba2008-08-19 00:17:30 +000059static cl::opt<bool> EnableFastSpilling("fast-spill",
60 cl::init(false), cl::Hidden);
61
Chris Lattnercd3245a2006-12-19 22:41:21 +000062STATISTIC(numIntervals, "Number of original intervals");
Evan Cheng0cbb1162007-11-29 01:06:25 +000063STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000065
Devang Patel19974732007-05-03 01:11:54 +000066char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000067static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Chris Lattnerf7da2c72006-08-24 22:43:55 +000069void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000070 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000071 AU.addRequired<AliasAnalysis>();
72 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000073 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000074 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000075 AU.addPreservedID(MachineLoopInfoID);
76 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000077
78 if (!StrongPHIElim) {
79 AU.addPreservedID(PHIEliminationID);
80 AU.addRequiredID(PHIEliminationID);
81 }
82
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085}
86
Chris Lattnerf7da2c72006-08-24 22:43:55 +000087void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000088 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000089 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000090 E = r2iMap_.end(); I != E; ++I)
91 delete I->second;
92
Evan Cheng3f32d652008-06-04 09:18:41 +000093 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000094 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 mi2iMap_.clear();
96 i2miMap_.clear();
97 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000098 terminatorGaps.clear();
99
Evan Chengdd199d22007-09-06 01:07:24 +0000100 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
101 VNInfoAllocator.Reset();
Evan Cheng1ed99222008-07-19 00:37:25 +0000102 while (!ClonedMIs.empty()) {
103 MachineInstr *MI = ClonedMIs.back();
104 ClonedMIs.pop_back();
105 mf_->DeleteMachineInstr(MI);
106 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000107}
108
Evan Cheng2578ba22009-07-01 01:59:31 +0000109/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
110/// there is one implicit_def for each use. Add isUndef marker to
111/// implicit_def defs and their uses.
112void LiveIntervals::processImplicitDefs() {
113 SmallSet<unsigned, 8> ImpDefRegs;
114 SmallVector<MachineInstr*, 8> ImpDefMIs;
115 MachineBasicBlock *Entry = mf_->begin();
116 SmallPtrSet<MachineBasicBlock*,16> Visited;
117 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
118 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
119 DFI != E; ++DFI) {
120 MachineBasicBlock *MBB = *DFI;
121 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
122 I != E; ) {
123 MachineInstr *MI = &*I;
124 ++I;
125 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
126 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000127 ImpDefRegs.insert(Reg);
128 ImpDefMIs.push_back(MI);
129 continue;
130 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000131
132 bool ChangedToImpDef = false;
133 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000134 MachineOperand& MO = MI->getOperand(i);
135 if (!MO.isReg() || !MO.isUse())
136 continue;
137 unsigned Reg = MO.getReg();
138 if (!Reg)
139 continue;
140 if (!ImpDefRegs.count(Reg))
141 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000142 // Use is a copy, just turn it into an implicit_def.
143 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
144 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
145 Reg == SrcReg) {
146 bool isKill = MO.isKill();
147 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
148 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
149 MI->RemoveOperand(j);
150 if (isKill)
151 ImpDefRegs.erase(Reg);
152 ChangedToImpDef = true;
153 break;
154 }
155
Evan Cheng2578ba22009-07-01 01:59:31 +0000156 MO.setIsUndef();
157 if (MO.isKill() || MI->isRegTiedToDefOperand(i))
158 ImpDefRegs.erase(Reg);
159 }
160
Evan Cheng459a7c62009-07-01 08:19:36 +0000161 if (ChangedToImpDef) {
162 // Backtrack to process this new implicit_def.
163 --I;
164 } else {
165 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
166 MachineOperand& MO = MI->getOperand(i);
167 if (!MO.isReg() || !MO.isDef())
168 continue;
169 ImpDefRegs.erase(MO.getReg());
170 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000171 }
172 }
173
174 // Any outstanding liveout implicit_def's?
175 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
176 MachineInstr *MI = ImpDefMIs[i];
177 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000178 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
179 !ImpDefRegs.count(Reg)) {
180 // Delete all "local" implicit_def's. That include those which define
181 // physical registers since they cannot be liveout.
182 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000183 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000184 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000185
186 // If there are multiple defs of the same register and at least one
187 // is not an implicit_def, do not insert implicit_def's before the
188 // uses.
189 bool Skip = false;
190 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
191 DE = mri_->def_end(); DI != DE; ++DI) {
192 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
193 Skip = true;
194 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000195 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000196 }
197 if (Skip)
198 continue;
199
Evan Chengd129d732009-07-17 19:43:40 +0000200 // The only implicit_def which we want to keep are those that are live
201 // out of its block.
202 MI->eraseFromParent();
203
Evan Cheng459a7c62009-07-01 08:19:36 +0000204 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
205 UE = mri_->use_end(); UI != UE; ) {
206 MachineOperand &RMO = UI.getOperand();
207 MachineInstr *RMI = &*UI;
208 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000209 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000210 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000211 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000212
213 // Turn a copy use into an implicit_def.
214 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
215 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
216 Reg == SrcReg) {
217 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
218 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
219 RMI->RemoveOperand(j);
220 continue;
221 }
222
Evan Cheng2578ba22009-07-01 01:59:31 +0000223 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
224 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000225 RMO.setReg(NewVReg);
226 RMO.setIsUndef();
227 RMO.setIsKill();
228 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000229 }
230 ImpDefRegs.clear();
231 ImpDefMIs.clear();
232 }
233}
234
Owen Anderson80b3ce62008-05-28 20:54:50 +0000235void LiveIntervals::computeNumbering() {
236 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000237 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000238
239 Idx2MBBMap.clear();
240 MBB2IdxMap.clear();
241 mi2iMap_.clear();
242 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000243 terminatorGaps.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000244
Owen Andersona1566f22008-07-22 22:46:49 +0000245 FunctionSize = 0;
246
Chris Lattner428b92e2006-09-15 03:57:23 +0000247 // Number MachineInstrs and MachineBasicBlocks.
248 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000249 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000250
251 unsigned MIIndex = 0;
252 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
253 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000254 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000255
Owen Anderson7fbad272008-07-23 21:37:49 +0000256 // Insert an empty slot at the beginning of each block.
257 MIIndex += InstrSlots::NUM;
258 i2miMap_.push_back(0);
259
Chris Lattner428b92e2006-09-15 03:57:23 +0000260 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
261 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000262
263 if (I == MBB->getFirstTerminator()) {
264 // Leave a gap for before terminators, this is where we will point
265 // PHI kills.
266 bool inserted =
267 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
268 assert(inserted &&
269 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000270 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000271 i2miMap_.push_back(0);
272
273 MIIndex += InstrSlots::NUM;
274 }
275
Chris Lattner428b92e2006-09-15 03:57:23 +0000276 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000277 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000278 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000279 i2miMap_.push_back(I);
280 MIIndex += InstrSlots::NUM;
Owen Andersona1566f22008-07-22 22:46:49 +0000281 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000282
Evan Cheng4ed43292008-10-18 05:21:37 +0000283 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000284 unsigned Slots = I->getDesc().getNumDefs();
285 if (Slots == 0)
286 Slots = 1;
287 MIIndex += InstrSlots::NUM * Slots;
288 while (Slots--)
289 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000290 }
Lang Hamesffd13262009-07-09 03:57:02 +0000291
292 if (MBB->getFirstTerminator() == MBB->end()) {
293 // Leave a gap for before terminators, this is where we will point
294 // PHI kills.
295 bool inserted =
296 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
297 assert(inserted &&
298 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000299 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000300 i2miMap_.push_back(0);
301
302 MIIndex += InstrSlots::NUM;
303 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000304
Owen Anderson1fbb4542008-06-16 16:58:24 +0000305 // Set the MBB2IdxMap entry for this MBB.
306 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
307 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000308 }
Lang Hamesffd13262009-07-09 03:57:02 +0000309
Evan Cheng4ca980e2007-10-17 02:10:22 +0000310 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000311
312 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000313 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000314 for (LiveInterval::iterator LI = OI->second->begin(),
315 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000316
Owen Anderson7eec0c22008-05-29 23:01:22 +0000317 // Remap the start index of the live range to the corresponding new
318 // number, or our best guess at what it _should_ correspond to if the
319 // original instruction has been erased. This is either the following
320 // instruction or its predecessor.
Owen Anderson7fbad272008-07-23 21:37:49 +0000321 unsigned index = LI->start / InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000322 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson0a7615a2008-07-25 23:06:59 +0000323 if (offset == InstrSlots::LOAD) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000324 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000325 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000326 // Take the pair containing the index
327 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000328 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000329
Owen Anderson7fbad272008-07-23 21:37:49 +0000330 LI->start = getMBBStartIdx(J->second);
331 } else {
332 LI->start = mi2iMap_[OldI2MI[index]] + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000333 }
334
335 // Remap the ending index in the same way that we remapped the start,
336 // except for the final step where we always map to the immediately
337 // following instruction.
Owen Andersond7dcbec2008-07-25 19:50:48 +0000338 index = (LI->end - 1) / InstrSlots::NUM;
Owen Anderson7fbad272008-07-23 21:37:49 +0000339 offset = LI->end % InstrSlots::NUM;
Owen Anderson9382b932008-07-30 00:22:56 +0000340 if (offset == InstrSlots::LOAD) {
341 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000342 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000343 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000344 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000345
Owen Anderson9382b932008-07-30 00:22:56 +0000346 LI->end = getMBBEndIdx(I->second) + 1;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000347 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000348 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000349 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
350
351 if (index != OldI2MI.size())
352 LI->end = mi2iMap_[OldI2MI[index]] + (idx == index ? offset : 0);
353 else
354 LI->end = InstrSlots::NUM * i2miMap_.size();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000355 }
Owen Anderson788d0412008-08-06 18:35:45 +0000356 }
357
Owen Anderson03857b22008-08-13 21:49:13 +0000358 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
359 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000360 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000361
Owen Anderson7eec0c22008-05-29 23:01:22 +0000362 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000363 // start indices above. VN's with special sentinel defs
364 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000365 if (vni->isDefAccurate() && !vni->isUnused()) {
Owen Anderson788d0412008-08-06 18:35:45 +0000366 unsigned index = vni->def / InstrSlots::NUM;
367 unsigned offset = vni->def % InstrSlots::NUM;
Owen Anderson91292392008-07-30 17:42:47 +0000368 if (offset == InstrSlots::LOAD) {
369 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000370 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000371 // Take the pair containing the index
372 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000373 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000374
Owen Anderson91292392008-07-30 17:42:47 +0000375 vni->def = getMBBStartIdx(J->second);
376 } else {
377 vni->def = mi2iMap_[OldI2MI[index]] + offset;
378 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000379 }
Owen Anderson745825f42008-05-28 22:40:08 +0000380
Owen Anderson7eec0c22008-05-29 23:01:22 +0000381 // Remap the VNInfo kill indices, which works the same as
382 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000383 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000384 unsigned killIdx = vni->kills[i].killIdx;
385
386 unsigned index = (killIdx - 1) / InstrSlots::NUM;
387 unsigned offset = killIdx % InstrSlots::NUM;
388
Owen Anderson309c6162008-09-30 22:51:54 +0000389 if (offset == InstrSlots::LOAD) {
Lang Hamesffd13262009-07-09 03:57:02 +0000390 assert("Value killed at a load slot.");
391 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000392 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000393 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000394
Lang Hamesffd13262009-07-09 03:57:02 +0000395 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000396 } else {
Lang Hamesffd13262009-07-09 03:57:02 +0000397 if (vni->kills[i].isPHIKill) {
398 std::vector<IdxMBBPair>::const_iterator I =
399 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
400 --I;
401 vni->kills[i].killIdx = terminatorGaps[I->second];
402 } else {
403 assert(OldI2MI[index] != 0 &&
404 "Kill refers to instruction not present in index maps.");
405 vni->kills[i].killIdx = mi2iMap_[OldI2MI[index]] + offset;
406 }
407
408 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000409 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000410 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
411
412 if (index != OldI2MI.size())
413 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
414 (idx == index ? offset : 0);
415 else
416 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000417 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000418 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000419 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000420 }
Owen Anderson788d0412008-08-06 18:35:45 +0000421 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000422}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000423
Lang Hamesf41538d2009-06-02 16:53:25 +0000424void LiveIntervals::scaleNumbering(int factor) {
425 // Need to
426 // * scale MBB begin and end points
427 // * scale all ranges.
428 // * Update VNI structures.
429 // * Scale instruction numberings
430
431 // Scale the MBB indices.
432 Idx2MBBMap.clear();
433 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
434 MBB != MBBE; ++MBB) {
435 std::pair<unsigned, unsigned> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
436 mbbIndices.first = InstrSlots::scale(mbbIndices.first, factor);
437 mbbIndices.second = InstrSlots::scale(mbbIndices.second, factor);
438 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
439 }
440 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
441
Lang Hamesffd13262009-07-09 03:57:02 +0000442 // Scale terminator gaps.
443 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
444 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
445 TGI != TGE; ++TGI) {
446 terminatorGaps[TGI->first] = InstrSlots::scale(TGI->second, factor);
447 }
448
Lang Hamesf41538d2009-06-02 16:53:25 +0000449 // Scale the intervals.
450 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
451 LI->second->scaleNumbering(factor);
452 }
453
454 // Scale MachineInstrs.
455 Mi2IndexMap oldmi2iMap = mi2iMap_;
456 unsigned highestSlot = 0;
457 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
458 MI != ME; ++MI) {
459 unsigned newSlot = InstrSlots::scale(MI->second, factor);
460 mi2iMap_[MI->first] = newSlot;
461 highestSlot = std::max(highestSlot, newSlot);
462 }
463
464 i2miMap_.clear();
465 i2miMap_.resize(highestSlot + 1);
466 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
467 MI != ME; ++MI) {
David Greene340482d2009-07-22 21:56:14 +0000468 i2miMap_[MI->second] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000469 }
470
471}
472
473
Owen Anderson80b3ce62008-05-28 20:54:50 +0000474/// runOnMachineFunction - Register allocate the whole function
475///
476bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
477 mf_ = &fn;
478 mri_ = &mf_->getRegInfo();
479 tm_ = &fn.getTarget();
480 tri_ = tm_->getRegisterInfo();
481 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000482 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000483 lv_ = &getAnalysis<LiveVariables>();
484 allocatableRegs_ = tri_->getAllocatableSet(fn);
485
Evan Cheng2578ba22009-07-01 01:59:31 +0000486 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000487 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000488 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000489
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000490 numIntervals += getNumIntervals();
491
Chris Lattner70ca3582004-09-30 15:59:17 +0000492 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000493 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000494}
495
Chris Lattner70ca3582004-09-30 15:59:17 +0000496/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000497void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000498 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000499 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Owen Anderson03857b22008-08-13 21:49:13 +0000500 I->second->print(O, tri_);
Evan Cheng3f32d652008-06-04 09:18:41 +0000501 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000502 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000503
504 O << "********** MACHINEINSTRS **********\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000505 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
506 mbbi != mbbe; ++mbbi) {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000507 O << ((Value*)mbbi->getBasicBlock())->getNameStr() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000508 for (MachineBasicBlock::iterator mii = mbbi->begin(),
509 mie = mbbi->end(); mii != mie; ++mii) {
510 O << getInstructionIndex(mii) << '\t' << *mii;
511 }
512 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000513}
514
Evan Chengc92da382007-11-03 07:20:12 +0000515/// conflictsWithPhysRegDef - Returns true if the specified register
516/// is defined during the duration of the specified interval.
517bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
518 VirtRegMap &vrm, unsigned reg) {
519 for (LiveInterval::Ranges::const_iterator
520 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
521 for (unsigned index = getBaseIndex(I->start),
522 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
523 index += InstrSlots::NUM) {
524 // skip deleted instructions
525 while (index != end && !getInstructionFromIndex(index))
526 index += InstrSlots::NUM;
527 if (index == end) break;
528
529 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000530 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
531 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000532 if (SrcReg == li.reg || DstReg == li.reg)
533 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000534 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
535 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000536 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000537 continue;
538 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000539 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000540 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000541 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000542 if (!vrm.hasPhys(PhysReg))
543 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000544 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000545 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000546 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000547 return true;
548 }
549 }
550 }
551
552 return false;
553}
554
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000555/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
556/// it can check use as well.
557bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
558 unsigned Reg, bool CheckUse,
559 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
560 for (LiveInterval::Ranges::const_iterator
561 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
562 for (unsigned index = getBaseIndex(I->start),
563 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
564 index += InstrSlots::NUM) {
565 // Skip deleted instructions.
566 MachineInstr *MI = 0;
567 while (index != end) {
568 MI = getInstructionFromIndex(index);
569 if (MI)
570 break;
571 index += InstrSlots::NUM;
572 }
573 if (index == end) break;
574
575 if (JoinedCopies.count(MI))
576 continue;
577 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
578 MachineOperand& MO = MI->getOperand(i);
579 if (!MO.isReg())
580 continue;
581 if (MO.isUse() && !CheckUse)
582 continue;
583 unsigned PhysReg = MO.getReg();
584 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
585 continue;
586 if (tri_->isSubRegister(Reg, PhysReg))
587 return true;
588 }
589 }
590 }
591
592 return false;
593}
594
595
Evan Cheng549f27d32007-08-13 23:45:17 +0000596void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000597 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000598 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000599 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000600 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000601}
602
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000603void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000604 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000605 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000606 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000607 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000608 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Evan Cheng419852c2008-04-03 16:39:43 +0000609
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000610 // Virtual registers may be defined multiple times (due to phi
611 // elimination and 2-addr elimination). Much of what we do only has to be
612 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000613 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000614 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615 if (interval.empty()) {
616 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000617 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000618 // Earlyclobbers move back one.
619 if (MO.isEarlyClobber())
620 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000621 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000622 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000623 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000624 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000625 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000626 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000627 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000628 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000629 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000630 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000631
632 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000633
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000634 // Loop over all of the blocks that the vreg is defined in. There are
635 // two cases we have to handle here. The most common case is a vreg
636 // whose lifetime is contained within a basic block. In this case there
637 // will be a single kill, in MBB, which comes after the definition.
638 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
639 // FIXME: what about dead vars?
640 unsigned killIdx;
641 if (vi.Kills[0] != mi)
642 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
643 else
644 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000645
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000646 // If the kill happens after the definition, we have an intra-block
647 // live range.
648 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000649 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000650 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000651 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000652 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000653 DOUT << " +" << LR << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000654 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000655 return;
656 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000657 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000658
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 // The other case we handle is when a virtual register lives to the end
660 // of the defining block, potentially live across some blocks, then is
661 // live into some number of blocks, but gets killed. Start by adding a
662 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000663 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000664 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 interval.addRange(NewLR);
666
667 // Iterate over all of the blocks that the variable is completely
668 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
669 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000670 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
671 E = vi.AliveBlocks.end(); I != E; ++I) {
672 LiveRange LR(getMBBStartIdx(*I),
673 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000674 ValNo);
675 interval.addRange(LR);
676 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 }
678
679 // Finally, this virtual register is live from the start of any killing
680 // block to the 'use' slot of the killing instruction.
681 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
682 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000683 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000684 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000685 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000686 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000687 interval.addKill(ValNo, killIdx, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000688 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000689 }
690
691 } else {
692 // If this is the second time we see a virtual register definition, it
693 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000694 // the result of two address elimination, then the vreg is one of the
695 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000696 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000697 // If this is a two-address definition, then we have already processed
698 // the live range. The only problem is that we didn't realize there
699 // are actually two values in the live interval. Because of this we
700 // need to take the LiveRegion that defines this register and split it
701 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000702 assert(interval.containsOneValue());
703 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000704 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000705 if (MO.isEarlyClobber())
706 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000707
Evan Cheng4f8ff162007-08-11 00:59:19 +0000708 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000709 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000710
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000711 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000712 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000713 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000714
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000715 // Two-address vregs should always only be redefined once. This means
716 // that at this point, there should be exactly one value number in it.
717 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
718
Chris Lattner91725b72006-08-31 05:54:43 +0000719 // The new value number (#1) is defined by the instruction we claimed
720 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000721 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
Lang Hames857c4e02009-06-17 21:01:20 +0000722 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000723 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000724 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
725
Chris Lattner91725b72006-08-31 05:54:43 +0000726 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000727 OldValNo->def = RedefIndex;
728 OldValNo->copy = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000729 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000730 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000731
732 // Add the new live interval which replaces the range for the input copy.
733 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000734 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000735 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000736 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000737
738 // If this redefinition is dead, we need to add a dummy unit live
739 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000740 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000741 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000742
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000743 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000744 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000745
746 } else {
747 // Otherwise, this must be because of phi elimination. If this is the
748 // first redefinition of the vreg that we have seen, go back and change
749 // the live range in the PHI block to be a different value number.
750 if (interval.containsOneValue()) {
751 assert(vi.Kills.size() == 1 &&
752 "PHI elimination vreg should have one kill, the PHI itself!");
753
754 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000755 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000756 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000757 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000758 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000759 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000760 interval.print(DOUT, tri_); DOUT << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000761 interval.removeRange(Start, End);
762 assert(interval.ranges.size() == 1 &&
763 "newly discovered PHI interval has >1 ranges.");
764 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
765 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000766 VNI->setHasPHIKill(true);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000767 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000768
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000769 // Replace the interval with one of a NEW value number. Note that this
770 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000771 LiveRange LR(Start, End,
772 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000773 LR.valno->setIsPHIDef(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000774 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000775 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000776 interval.addKill(LR.valno, End, false);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000777 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000778 }
779
780 // In the case of PHI elimination, each variable definition is only
781 // live until the end of the block. We've already taken care of the
782 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000783 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000784 if (MO.isEarlyClobber())
785 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000786
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000787 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000788 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000789 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000790 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000791 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000792 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000793 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000794 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000795 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000796
Owen Anderson7fbad272008-07-23 21:37:49 +0000797 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000798 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000799 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000800 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000801 ValNo->setHasPHIKill(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000802 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000803 }
804 }
805
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000806 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000807}
808
Chris Lattnerf35fef72004-07-23 21:24:19 +0000809void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000810 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000811 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000812 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000813 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000814 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000815 // A physical register cannot be live across basic block, so its
816 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000817 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000818
Chris Lattner6b128bd2006-09-03 08:07:11 +0000819 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000820 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000821 // Earlyclobbers move back one.
822 if (MO.isEarlyClobber())
823 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000824 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000825
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000826 // If it is not used after definition, it is considered dead at
827 // the instruction defining it. Hence its interval is:
828 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000829 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000830 DOUT << " dead";
Dale Johannesen86b49f82008-09-24 01:07:17 +0000831 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000832 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000833 }
834
835 // If it is not dead on definition, it must be killed by a
836 // subsequent instruction. Hence its interval is:
837 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000838 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000839 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000840 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
841 getInstructionFromIndex(baseIndex) == 0)
842 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000843 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000844 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000845 end = getUseIndex(baseIndex) + 1;
846 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000847 } else {
848 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
849 if (DefIdx != -1) {
850 if (mi->isRegTiedToUseOperand(DefIdx)) {
851 // Two-address instruction.
852 end = getDefIndex(baseIndex);
853 if (mi->getOperand(DefIdx).isEarlyClobber())
854 end = getUseIndex(baseIndex);
855 } else {
856 // Another instruction redefines the register before it is ever read.
857 // Then the register is essentially dead at the instruction that defines
858 // it. Hence its interval is:
859 // [defSlot(def), defSlot(def)+1)
860 DOUT << " dead";
861 end = start + 1;
862 }
863 goto exit;
864 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000865 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000866
867 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000868 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000869
870 // The only case we should have a dead physreg here without a killing or
871 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000872 // and never used. Another possible case is the implicit use of the
873 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000874 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000875
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000876exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000877 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000878
Evan Cheng24a3cc42007-04-25 07:30:23 +0000879 // Already exists? Extend old live interval.
880 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000881 bool Extend = OldLR != interval.end();
882 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000883 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000884 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000885 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000886 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000887 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000888 interval.addKill(LR.valno, end, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000889 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000890}
891
Chris Lattnerf35fef72004-07-23 21:24:19 +0000892void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
893 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000894 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000895 MachineOperand& MO,
896 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000897 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000898 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000899 getOrCreateInterval(MO.getReg()));
900 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000901 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000902 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000903 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000904 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000905 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000906 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000907 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000908 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000909 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000910 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000911 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000912 // If MI also modifies the sub-register explicitly, avoid processing it
913 // more than once. Do not pass in TRI here so it checks for exact match.
914 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000915 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000916 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000917 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000918}
919
Evan Chengb371f452007-02-19 21:49:54 +0000920void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000921 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000922 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000923 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
924
925 // Look for kills, if it reaches a def before it's killed, then it shouldn't
926 // be considered a livein.
927 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000928 unsigned baseIndex = MIIdx;
929 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000930 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
931 getInstructionFromIndex(baseIndex) == 0)
932 baseIndex += InstrSlots::NUM;
933 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000934 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000935
Evan Chengb371f452007-02-19 21:49:54 +0000936 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000937 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000938 DOUT << " killed";
939 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000940 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000941 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000942 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000943 // Another instruction redefines the register before it is ever read.
944 // Then the register is essentially dead at the instruction that defines
945 // it. Hence its interval is:
946 // [defSlot(def), defSlot(def)+1)
947 DOUT << " dead";
948 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000949 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000950 break;
Evan Chengb371f452007-02-19 21:49:54 +0000951 }
952
953 baseIndex += InstrSlots::NUM;
954 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000955 if (mi != MBB->end()) {
956 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
957 getInstructionFromIndex(baseIndex) == 0)
958 baseIndex += InstrSlots::NUM;
959 }
Evan Chengb371f452007-02-19 21:49:54 +0000960 }
961
Evan Cheng75611fb2007-06-27 01:16:36 +0000962 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000963 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000964 if (isAlias) {
965 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000966 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000967 } else {
968 DOUT << " live through";
969 end = baseIndex;
970 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000971 }
972
Lang Hames10382fb2009-06-19 02:17:53 +0000973 VNInfo *vni =
974 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000975 vni->setIsPHIDef(true);
976 LiveRange LR(start, end, vni);
977
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000978 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000979 interval.addKill(LR.valno, end, false);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000980 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000981}
982
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000983/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000984/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000985/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000986/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000987void LiveIntervals::computeIntervals() {
Dale Johannesen91aac102008-09-17 21:13:11 +0000988
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000989 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
990 << "********** Function: "
991 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +0000992
993 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000994 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
995 MBBI != E; ++MBBI) {
996 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000997 // Track the index of the current machine instr.
998 unsigned MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000999 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001000
Chris Lattner428b92e2006-09-15 03:57:23 +00001001 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001002
Dan Gohmancb406c22007-10-03 19:26:29 +00001003 // Create intervals for live-ins to this BB first.
1004 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1005 LE = MBB->livein_end(); LI != LE; ++LI) {
1006 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1007 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001008 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001009 if (!hasInterval(*AS))
1010 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1011 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001012 }
1013
Owen Anderson99500ae2008-09-15 22:00:38 +00001014 // Skip over empty initial indices.
1015 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1016 getInstructionFromIndex(MIIndex) == 0)
1017 MIIndex += InstrSlots::NUM;
1018
Chris Lattner428b92e2006-09-15 03:57:23 +00001019 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001020 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001021
Evan Cheng438f7bc2006-11-10 08:43:01 +00001022 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001023 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1024 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001025 if (!MO.isReg() || !MO.getReg())
1026 continue;
1027
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001028 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001029 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001030 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001031 else if (MO.isUndef())
1032 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001033 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001034
1035 // Skip over the empty slots after each instruction.
1036 unsigned Slots = MI->getDesc().getNumDefs();
1037 if (Slots == 0)
1038 Slots = 1;
1039 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001040
1041 // Skip over empty indices.
1042 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1043 getInstructionFromIndex(MIIndex) == 0)
1044 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001045 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001046 }
Evan Chengd129d732009-07-17 19:43:40 +00001047
1048 // Create empty intervals for registers defined by implicit_def's (except
1049 // for those implicit_def that define values which are liveout of their
1050 // blocks.
1051 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1052 unsigned UndefReg = UndefUses[i];
1053 (void)getOrCreateInterval(UndefReg);
1054 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001055}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001056
Evan Chengd0e32c52008-10-29 05:06:14 +00001057bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001058 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001059 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001060 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001061
1062 bool ResVal = false;
1063 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001064 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001065 break;
1066 MBBs.push_back(I->second);
1067 ResVal = true;
1068 ++I;
1069 }
1070 return ResVal;
1071}
1072
Evan Chengd0e32c52008-10-29 05:06:14 +00001073bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1074 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1075 std::vector<IdxMBBPair>::const_iterator I =
1076 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1077
1078 bool ResVal = false;
1079 while (I != Idx2MBBMap.end()) {
1080 if (I->first > End)
1081 break;
1082 MachineBasicBlock *MBB = I->second;
1083 if (getMBBEndIdx(MBB) > End)
1084 break;
1085 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1086 SE = MBB->succ_end(); SI != SE; ++SI)
1087 MBBs.push_back(*SI);
1088 ResVal = true;
1089 ++I;
1090 }
1091 return ResVal;
1092}
1093
Owen Anderson03857b22008-08-13 21:49:13 +00001094LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001095 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001096 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001097}
Evan Chengf2fbca62007-11-12 06:35:08 +00001098
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001099/// dupInterval - Duplicate a live interval. The caller is responsible for
1100/// managing the allocated memory.
1101LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1102 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001103 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001104 return NewLI;
1105}
1106
Evan Chengc8d044e2008-02-15 18:24:29 +00001107/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1108/// copy field and returns the source register that defines it.
1109unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
1110 if (!VNI->copy)
1111 return 0;
1112
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001113 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1114 // If it's extracting out of a physical register, return the sub-register.
1115 unsigned Reg = VNI->copy->getOperand(1).getReg();
1116 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1117 Reg = tri_->getSubReg(Reg, VNI->copy->getOperand(2).getImm());
1118 return Reg;
Dan Gohman97121ba2009-04-08 00:15:30 +00001119 } else if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1120 VNI->copy->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng7e073ba2008-04-09 20:57:25 +00001121 return VNI->copy->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001122
Evan Cheng04ee5a12009-01-20 19:12:24 +00001123 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1124 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001125 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001126 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001127 return 0;
1128}
Evan Chengf2fbca62007-11-12 06:35:08 +00001129
1130//===----------------------------------------------------------------------===//
1131// Register allocator hooks.
1132//
1133
Evan Chengd70dbb52008-02-22 09:24:50 +00001134/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1135/// allow one) virtual register operand, then its uses are implicitly using
1136/// the register. Returns the virtual register.
1137unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1138 MachineInstr *MI) const {
1139 unsigned RegOp = 0;
1140 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1141 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001142 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001143 continue;
1144 unsigned Reg = MO.getReg();
1145 if (Reg == 0 || Reg == li.reg)
1146 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001147
1148 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1149 !allocatableRegs_[Reg])
1150 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001151 // FIXME: For now, only remat MI with at most one register operand.
1152 assert(!RegOp &&
1153 "Can't rematerialize instruction with multiple register operand!");
1154 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001155#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001156 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001157#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001158 }
1159 return RegOp;
1160}
1161
1162/// isValNoAvailableAt - Return true if the val# of the specified interval
1163/// which reaches the given instruction also reaches the specified use index.
1164bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1165 unsigned UseIdx) const {
1166 unsigned Index = getInstructionIndex(MI);
1167 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1168 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1169 return UI != li.end() && UI->valno == ValNo;
1170}
1171
Evan Chengf2fbca62007-11-12 06:35:08 +00001172/// isReMaterializable - Returns true if the definition MI of the specified
1173/// val# of the specified interval is re-materializable.
1174bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001175 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001176 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001177 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001178 if (DisableReMat)
1179 return false;
1180
Evan Cheng20ccded2008-03-15 00:19:36 +00001181 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001182 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001183
1184 int FrameIdx = 0;
1185 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001186 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001187 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1188 // this but remember this is not safe to fold into a two-address
1189 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001190 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001191 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001192
Dan Gohman6d69ba82008-07-25 00:02:30 +00001193 // If the target-specific rules don't identify an instruction as
1194 // being trivially rematerializable, use some target-independent
1195 // rules.
1196 if (!MI->getDesc().isRematerializable() ||
1197 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001198 if (!EnableAggressiveRemat)
1199 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001200
Dan Gohman0471a792008-07-28 18:43:51 +00001201 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001202 // we can't analyze it.
1203 const TargetInstrDesc &TID = MI->getDesc();
1204 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1205 return false;
1206
1207 // Avoid instructions obviously unsafe for remat.
1208 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1209 return false;
1210
1211 // If the instruction accesses memory and the memory could be non-constant,
1212 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001213 for (std::list<MachineMemOperand>::const_iterator
1214 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001215 const MachineMemOperand &MMO = *I;
1216 if (MMO.isVolatile() || MMO.isStore())
1217 return false;
1218 const Value *V = MMO.getValue();
1219 if (!V)
1220 return false;
1221 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1222 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001223 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001224 } else if (!aa_->pointsToConstantMemory(V))
1225 return false;
1226 }
1227
1228 // If any of the registers accessed are non-constant, conservatively assume
1229 // the instruction is not rematerializable.
1230 unsigned ImpUse = 0;
1231 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1232 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001233 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001234 unsigned Reg = MO.getReg();
1235 if (Reg == 0)
1236 continue;
1237 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1238 return false;
1239
1240 // Only allow one def, and that in the first operand.
1241 if (MO.isDef() != (i == 0))
1242 return false;
1243
1244 // Only allow constant-valued registers.
1245 bool IsLiveIn = mri_->isLiveIn(Reg);
1246 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1247 E = mri_->def_end();
1248
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001249 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001250 if (MO.isDef() && (next(I) != E || IsLiveIn))
1251 return false;
1252
1253 if (MO.isUse()) {
1254 // Only allow one use other register use, as that's all the
1255 // remat mechanisms support currently.
1256 if (Reg != li.reg) {
1257 if (ImpUse == 0)
1258 ImpUse = Reg;
1259 else if (Reg != ImpUse)
1260 return false;
1261 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001262 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001263 if (I != E && (next(I) != E || IsLiveIn))
1264 return false;
1265 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001266 }
1267 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001268 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001269
Dan Gohman6d69ba82008-07-25 00:02:30 +00001270 unsigned ImpUse = getReMatImplicitUse(li, MI);
1271 if (ImpUse) {
1272 const LiveInterval &ImpLi = getInterval(ImpUse);
1273 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1274 re = mri_->use_end(); ri != re; ++ri) {
1275 MachineInstr *UseMI = &*ri;
1276 unsigned UseIdx = getInstructionIndex(UseMI);
1277 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1278 continue;
1279 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1280 return false;
1281 }
Evan Chengdc377862008-09-30 15:44:16 +00001282
1283 // If a register operand of the re-materialized instruction is going to
1284 // be spilled next, then it's not legal to re-materialize this instruction.
1285 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1286 if (ImpUse == SpillIs[i]->reg)
1287 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001288 }
1289 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001290}
1291
Evan Cheng06587492008-10-24 02:05:00 +00001292/// isReMaterializable - Returns true if the definition MI of the specified
1293/// val# of the specified interval is re-materializable.
1294bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1295 const VNInfo *ValNo, MachineInstr *MI) {
1296 SmallVector<LiveInterval*, 4> Dummy1;
1297 bool Dummy2;
1298 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1299}
1300
Evan Cheng5ef3a042007-12-06 00:01:56 +00001301/// isReMaterializable - Returns true if every definition of MI of every
1302/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001303bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1304 SmallVectorImpl<LiveInterval*> &SpillIs,
1305 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001306 isLoad = false;
1307 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1308 i != e; ++i) {
1309 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001310 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001311 continue; // Dead val#.
1312 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001313 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001314 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001315 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001316 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001317 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001318 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001319 return false;
1320 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001321 }
1322 return true;
1323}
1324
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001325/// FilterFoldedOps - Filter out two-address use operands. Return
1326/// true if it finds any issue with the operands that ought to prevent
1327/// folding.
1328static bool FilterFoldedOps(MachineInstr *MI,
1329 SmallVector<unsigned, 2> &Ops,
1330 unsigned &MRInfo,
1331 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001332 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001333 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1334 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001335 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001336 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001337 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001338 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001339 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001340 MRInfo |= (unsigned)VirtRegMap::isMod;
1341 else {
1342 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001343 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001344 MRInfo = VirtRegMap::isModRef;
1345 continue;
1346 }
1347 MRInfo |= (unsigned)VirtRegMap::isRef;
1348 }
1349 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001350 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001351 return false;
1352}
1353
1354
1355/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1356/// slot / to reg or any rematerialized load into ith operand of specified
1357/// MI. If it is successul, MI is updated with the newly created MI and
1358/// returns true.
1359bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1360 VirtRegMap &vrm, MachineInstr *DefMI,
1361 unsigned InstrIdx,
1362 SmallVector<unsigned, 2> &Ops,
1363 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001364 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001365 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001366 RemoveMachineInstrFromMaps(MI);
1367 vrm.RemoveMachineInstrFromMaps(MI);
1368 MI->eraseFromParent();
1369 ++numFolds;
1370 return true;
1371 }
1372
1373 // Filter the list of operand indexes that are to be folded. Abort if
1374 // any operand will prevent folding.
1375 unsigned MRInfo = 0;
1376 SmallVector<unsigned, 2> FoldOps;
1377 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1378 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001379
Evan Cheng427f4c12008-03-31 23:19:51 +00001380 // The only time it's safe to fold into a two address instruction is when
1381 // it's folding reload and spill from / into a spill stack slot.
1382 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001383 return false;
1384
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001385 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1386 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001387 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001388 // Remember this instruction uses the spill slot.
1389 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1390
Evan Chengf2fbca62007-11-12 06:35:08 +00001391 // Attempt to fold the memory reference into the instruction. If
1392 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001393 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001394 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001395 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001396 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001397 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001398 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001399 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001400 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1401 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001402 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001403 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001404 return true;
1405 }
1406 return false;
1407}
1408
Evan Cheng018f9b02007-12-05 03:22:34 +00001409/// canFoldMemoryOperand - Returns true if the specified load / store
1410/// folding is possible.
1411bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001412 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001413 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001414 // Filter the list of operand indexes that are to be folded. Abort if
1415 // any operand will prevent folding.
1416 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001417 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001418 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1419 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001420
Evan Cheng3c75ba82008-04-01 21:37:32 +00001421 // It's only legal to remat for a use, not a def.
1422 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001423 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001424
Evan Chengd70dbb52008-02-22 09:24:50 +00001425 return tii_->canFoldMemoryOperand(MI, FoldOps);
1426}
1427
Evan Cheng81a03822007-11-17 00:40:40 +00001428bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1429 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1430 for (LiveInterval::Ranges::const_iterator
1431 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1432 std::vector<IdxMBBPair>::const_iterator II =
1433 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1434 if (II == Idx2MBBMap.end())
1435 continue;
1436 if (I->end > II->first) // crossing a MBB.
1437 return false;
1438 MBBs.insert(II->second);
1439 if (MBBs.size() > 1)
1440 return false;
1441 }
1442 return true;
1443}
1444
Evan Chengd70dbb52008-02-22 09:24:50 +00001445/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1446/// interval on to-be re-materialized operands of MI) with new register.
1447void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1448 MachineInstr *MI, unsigned NewVReg,
1449 VirtRegMap &vrm) {
1450 // There is an implicit use. That means one of the other operand is
1451 // being remat'ed and the remat'ed instruction has li.reg as an
1452 // use operand. Make sure we rewrite that as well.
1453 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1454 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001455 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001456 continue;
1457 unsigned Reg = MO.getReg();
1458 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1459 continue;
1460 if (!vrm.isReMaterialized(Reg))
1461 continue;
1462 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001463 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1464 if (UseMO)
1465 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001466 }
1467}
1468
Evan Chengf2fbca62007-11-12 06:35:08 +00001469/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1470/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001471bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001472rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1473 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001474 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001475 unsigned Slot, int LdSlot,
1476 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001477 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001478 const TargetRegisterClass* rc,
1479 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001480 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001481 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001482 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001483 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001484 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001485 RestartInstruction:
1486 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1487 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001488 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001489 continue;
1490 unsigned Reg = mop.getReg();
1491 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001492 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001493 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001494 if (Reg != li.reg)
1495 continue;
1496
1497 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001498 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001499 int FoldSlot = Slot;
1500 if (DefIsReMat) {
1501 // If this is the rematerializable definition MI itself and
1502 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001503 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001504 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1505 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001506 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001507 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001508 MI->eraseFromParent();
1509 break;
1510 }
1511
1512 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001513 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001514 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001515 if (isLoad) {
1516 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1517 FoldSS = isLoadSS;
1518 FoldSlot = LdSlot;
1519 }
1520 }
1521
Evan Chengf2fbca62007-11-12 06:35:08 +00001522 // Scan all of the operands of this instruction rewriting operands
1523 // to use NewVReg instead of li.reg as appropriate. We do this for
1524 // two reasons:
1525 //
1526 // 1. If the instr reads the same spilled vreg multiple times, we
1527 // want to reuse the NewVReg.
1528 // 2. If the instr is a two-addr instruction, we are required to
1529 // keep the src/dst regs pinned.
1530 //
1531 // Keep track of whether we replace a use and/or def so that we can
1532 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001533
Evan Cheng81a03822007-11-17 00:40:40 +00001534 HasUse = mop.isUse();
1535 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001536 SmallVector<unsigned, 2> Ops;
1537 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001538 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001539 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001540 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001541 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001542 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001543 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001544 continue;
1545 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001546 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001547 if (!MOj.isUndef()) {
1548 HasUse |= MOj.isUse();
1549 HasDef |= MOj.isDef();
1550 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001551 }
1552 }
1553
David Greene26b86a02008-10-27 17:38:59 +00001554 // Create a new virtual register for the spill interval.
1555 // Create the new register now so we can map the fold instruction
1556 // to the new register so when it is unfolded we get the correct
1557 // answer.
1558 bool CreatedNewVReg = false;
1559 if (NewVReg == 0) {
1560 NewVReg = mri_->createVirtualRegister(rc);
1561 vrm.grow();
1562 CreatedNewVReg = true;
1563 }
1564
Evan Cheng9c3c2212008-06-06 07:54:39 +00001565 if (!TryFold)
1566 CanFold = false;
1567 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001568 // Do not fold load / store here if we are splitting. We'll find an
1569 // optimal point to insert a load / store later.
1570 if (!TrySplit) {
1571 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001572 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001573 // Folding the load/store can completely change the instruction in
1574 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001575
1576 if (FoldSS) {
1577 // We need to give the new vreg the same stack slot as the
1578 // spilled interval.
1579 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1580 }
1581
Evan Cheng018f9b02007-12-05 03:22:34 +00001582 HasUse = false;
1583 HasDef = false;
1584 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001585 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001586 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001587 goto RestartInstruction;
1588 }
1589 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001590 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001591 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001592 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001593 }
Evan Chengcddbb832007-11-30 21:23:43 +00001594
Evan Chengcddbb832007-11-30 21:23:43 +00001595 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001596 if (mop.isImplicit())
1597 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001598
1599 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001600 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1601 MachineOperand &mopj = MI->getOperand(Ops[j]);
1602 mopj.setReg(NewVReg);
1603 if (mopj.isImplicit())
1604 rewriteImplicitOps(li, MI, NewVReg, vrm);
1605 }
Evan Chengcddbb832007-11-30 21:23:43 +00001606
Evan Cheng81a03822007-11-17 00:40:40 +00001607 if (CreatedNewVReg) {
1608 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001609 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001610 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001611 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001612 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001613 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001614 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001615 }
1616 if (!CanDelete || (HasUse && HasDef)) {
1617 // If this is a two-addr instruction then its use operands are
1618 // rematerializable but its def is not. It should be assigned a
1619 // stack slot.
1620 vrm.assignVirt2StackSlot(NewVReg, Slot);
1621 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001622 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001623 vrm.assignVirt2StackSlot(NewVReg, Slot);
1624 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001625 } else if (HasUse && HasDef &&
1626 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1627 // If this interval hasn't been assigned a stack slot (because earlier
1628 // def is a deleted remat def), do it now.
1629 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1630 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001631 }
1632
Evan Cheng313d4b82008-02-23 00:33:04 +00001633 // Re-matting an instruction with virtual register use. Add the
1634 // register as an implicit use on the use MI.
1635 if (DefIsReMat && ImpUse)
1636 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1637
Evan Cheng5b69eba2009-04-21 22:46:52 +00001638 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001639 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001640 if (CreatedNewVReg) {
1641 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001642 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001643 if (TrySplit)
1644 vrm.setIsSplitFromReg(NewVReg, li.reg);
1645 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001646
1647 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001648 if (CreatedNewVReg) {
1649 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001650 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Cheng81a03822007-11-17 00:40:40 +00001651 DOUT << " +" << LR;
1652 nI.addRange(LR);
1653 } else {
1654 // Extend the split live interval to this def / use.
1655 unsigned End = getUseIndex(index)+1;
1656 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1657 nI.getValNumInfo(nI.getNumValNums()-1));
1658 DOUT << " +" << LR;
1659 nI.addRange(LR);
1660 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001661 }
1662 if (HasDef) {
1663 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001664 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Chengf2fbca62007-11-12 06:35:08 +00001665 DOUT << " +" << LR;
1666 nI.addRange(LR);
1667 }
Evan Cheng81a03822007-11-17 00:40:40 +00001668
Evan Chengf2fbca62007-11-12 06:35:08 +00001669 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001670 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001671 DOUT << '\n';
1672 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001673 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001674}
Evan Cheng81a03822007-11-17 00:40:40 +00001675bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001676 const VNInfo *VNI,
1677 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001678 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001679 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001680 if (VNI->kills[j].isPHIKill)
1681 continue;
1682
1683 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001684 if (KillIdx > Idx && KillIdx < End)
1685 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001686 }
1687 return false;
1688}
1689
Evan Cheng063284c2008-02-21 00:34:19 +00001690/// RewriteInfo - Keep track of machine instrs that will be rewritten
1691/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001692namespace {
1693 struct RewriteInfo {
1694 unsigned Index;
1695 MachineInstr *MI;
1696 bool HasUse;
1697 bool HasDef;
1698 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1699 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1700 };
Evan Cheng063284c2008-02-21 00:34:19 +00001701
Dan Gohman844731a2008-05-13 00:00:25 +00001702 struct RewriteInfoCompare {
1703 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1704 return LHS.Index < RHS.Index;
1705 }
1706 };
1707}
Evan Cheng063284c2008-02-21 00:34:19 +00001708
Evan Chengf2fbca62007-11-12 06:35:08 +00001709void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001710rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001711 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001712 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001713 unsigned Slot, int LdSlot,
1714 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001715 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001716 const TargetRegisterClass* rc,
1717 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001718 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001719 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001720 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001721 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001722 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1723 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001724 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001725 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001726 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001727 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001728 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001729
Evan Cheng063284c2008-02-21 00:34:19 +00001730 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001731 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001732 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001733 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1734 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001735 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001736 MachineOperand &O = ri.getOperand();
1737 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001738 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001739 unsigned index = getInstructionIndex(MI);
1740 if (index < start || index >= end)
1741 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001742
1743 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001744 // Must be defined by an implicit def. It should not be spilled. Note,
1745 // this is for correctness reason. e.g.
1746 // 8 %reg1024<def> = IMPLICIT_DEF
1747 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1748 // The live range [12, 14) are not part of the r1024 live interval since
1749 // it's defined by an implicit def. It will not conflicts with live
1750 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001751 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001752 // the INSERT_SUBREG and both target registers that would overlap.
1753 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001754 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1755 }
1756 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1757
Evan Cheng313d4b82008-02-23 00:33:04 +00001758 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001759 // Now rewrite the defs and uses.
1760 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1761 RewriteInfo &rwi = RewriteMIs[i];
1762 ++i;
1763 unsigned index = rwi.Index;
1764 bool MIHasUse = rwi.HasUse;
1765 bool MIHasDef = rwi.HasDef;
1766 MachineInstr *MI = rwi.MI;
1767 // If MI def and/or use the same register multiple times, then there
1768 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001769 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001770 while (i != e && RewriteMIs[i].MI == MI) {
1771 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001772 bool isUse = RewriteMIs[i].HasUse;
1773 if (isUse) ++NumUses;
1774 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001775 MIHasDef |= RewriteMIs[i].HasDef;
1776 ++i;
1777 }
Evan Cheng81a03822007-11-17 00:40:40 +00001778 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001779
Evan Cheng0a891ed2008-05-23 23:00:04 +00001780 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001781 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001782 // register interval's spill weight to HUGE_VALF to prevent it from
1783 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001784 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001785 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001786 }
1787
Evan Cheng063284c2008-02-21 00:34:19 +00001788 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001789 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001790 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001791 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001792 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001793 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001794 // One common case:
1795 // x = use
1796 // ...
1797 // ...
1798 // def = ...
1799 // = use
1800 // It's better to start a new interval to avoid artifically
1801 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001802 if (MIHasDef && !MIHasUse) {
1803 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001804 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001805 }
1806 }
Evan Chengcada2452007-11-28 01:28:46 +00001807 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001808
1809 bool IsNew = ThisVReg == 0;
1810 if (IsNew) {
1811 // This ends the previous live interval. If all of its def / use
1812 // can be folded, give it a low spill weight.
1813 if (NewVReg && TrySplit && AllCanFold) {
1814 LiveInterval &nI = getOrCreateInterval(NewVReg);
1815 nI.weight /= 10.0F;
1816 }
1817 AllCanFold = true;
1818 }
1819 NewVReg = ThisVReg;
1820
Evan Cheng81a03822007-11-17 00:40:40 +00001821 bool HasDef = false;
1822 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001823 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001824 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1825 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1826 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001827 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001828 if (!HasDef && !HasUse)
1829 continue;
1830
Evan Cheng018f9b02007-12-05 03:22:34 +00001831 AllCanFold &= CanFold;
1832
Evan Cheng81a03822007-11-17 00:40:40 +00001833 // Update weight of spill interval.
1834 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001835 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001836 // The spill weight is now infinity as it cannot be spilled again.
1837 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001838 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001839 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001840
1841 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001842 if (HasDef) {
1843 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001844 bool HasKill = false;
1845 if (!HasUse)
1846 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1847 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001848 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001849 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001850 if (VNI)
1851 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1852 }
Owen Anderson28998312008-08-13 22:28:50 +00001853 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001854 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001855 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001856 if (SII == SpillIdxes.end()) {
1857 std::vector<SRInfo> S;
1858 S.push_back(SRInfo(index, NewVReg, true));
1859 SpillIdxes.insert(std::make_pair(MBBId, S));
1860 } else if (SII->second.back().vreg != NewVReg) {
1861 SII->second.push_back(SRInfo(index, NewVReg, true));
1862 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001863 // If there is an earlier def and this is a two-address
1864 // instruction, then it's not possible to fold the store (which
1865 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001866 SRInfo &Info = SII->second.back();
1867 Info.index = index;
1868 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001869 }
1870 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001871 } else if (SII != SpillIdxes.end() &&
1872 SII->second.back().vreg == NewVReg &&
1873 (int)index > SII->second.back().index) {
1874 // There is an earlier def that's not killed (must be two-address).
1875 // The spill is no longer needed.
1876 SII->second.pop_back();
1877 if (SII->second.empty()) {
1878 SpillIdxes.erase(MBBId);
1879 SpillMBBs.reset(MBBId);
1880 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001881 }
1882 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001883 }
1884
1885 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001886 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001887 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001888 if (SII != SpillIdxes.end() &&
1889 SII->second.back().vreg == NewVReg &&
1890 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001891 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001892 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001893 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001894 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001895 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001896 // If we are splitting live intervals, only fold if it's the first
1897 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001898 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001899 else if (IsNew) {
1900 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001901 if (RII == RestoreIdxes.end()) {
1902 std::vector<SRInfo> Infos;
1903 Infos.push_back(SRInfo(index, NewVReg, true));
1904 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1905 } else {
1906 RII->second.push_back(SRInfo(index, NewVReg, true));
1907 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001908 RestoreMBBs.set(MBBId);
1909 }
1910 }
1911
1912 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001913 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001914 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001915 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001916
1917 if (NewVReg && TrySplit && AllCanFold) {
1918 // If all of its def / use can be folded, give it a low spill weight.
1919 LiveInterval &nI = getOrCreateInterval(NewVReg);
1920 nI.weight /= 10.0F;
1921 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001922}
1923
Evan Cheng1953d0c2007-11-29 10:12:14 +00001924bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1925 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001926 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001927 if (!RestoreMBBs[Id])
1928 return false;
1929 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1930 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1931 if (Restores[i].index == index &&
1932 Restores[i].vreg == vr &&
1933 Restores[i].canFold)
1934 return true;
1935 return false;
1936}
1937
1938void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1939 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001940 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001941 if (!RestoreMBBs[Id])
1942 return;
1943 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1944 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1945 if (Restores[i].index == index && Restores[i].vreg)
1946 Restores[i].index = -1;
1947}
Evan Cheng81a03822007-11-17 00:40:40 +00001948
Evan Cheng4cce6b42008-04-11 17:53:36 +00001949/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1950/// spilled and create empty intervals for their uses.
1951void
1952LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1953 const TargetRegisterClass* rc,
1954 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001955 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1956 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001957 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001958 MachineInstr *MI = &*ri;
1959 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001960 if (O.isDef()) {
1961 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1962 "Register def was not rewritten?");
1963 RemoveMachineInstrFromMaps(MI);
1964 vrm.RemoveMachineInstrFromMaps(MI);
1965 MI->eraseFromParent();
1966 } else {
1967 // This must be an use of an implicit_def so it's not part of the live
1968 // interval. Create a new empty live interval for it.
1969 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1970 unsigned NewVReg = mri_->createVirtualRegister(rc);
1971 vrm.grow();
1972 vrm.setIsImplicitlyDefined(NewVReg);
1973 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1974 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1975 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001976 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001977 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001978 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001979 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001980 }
1981 }
Evan Cheng419852c2008-04-03 16:39:43 +00001982 }
1983}
1984
Evan Chengf2fbca62007-11-12 06:35:08 +00001985std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001986addIntervalsForSpillsFast(const LiveInterval &li,
1987 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001988 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001989 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001990
1991 std::vector<LiveInterval*> added;
1992
1993 assert(li.weight != HUGE_VALF &&
1994 "attempt to spill already spilled interval!");
1995
1996 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1997 DEBUG(li.dump());
1998 DOUT << '\n';
1999
2000 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2001
Owen Andersona41e47a2008-08-19 22:12:11 +00002002 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2003 while (RI != mri_->reg_end()) {
2004 MachineInstr* MI = &*RI;
2005
2006 SmallVector<unsigned, 2> Indices;
2007 bool HasUse = false;
2008 bool HasDef = false;
2009
2010 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2011 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002012 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002013
2014 HasUse |= MI->getOperand(i).isUse();
2015 HasDef |= MI->getOperand(i).isDef();
2016
2017 Indices.push_back(i);
2018 }
2019
2020 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2021 Indices, true, slot, li.reg)) {
2022 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002023 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002024 vrm.assignVirt2StackSlot(NewVReg, slot);
2025
Owen Andersona41e47a2008-08-19 22:12:11 +00002026 // create a new register for this spill
2027 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002028
Owen Andersona41e47a2008-08-19 22:12:11 +00002029 // the spill weight is now infinity as it
2030 // cannot be spilled again
2031 nI.weight = HUGE_VALF;
2032
2033 // Rewrite register operands to use the new vreg.
2034 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2035 E = Indices.end(); I != E; ++I) {
2036 MI->getOperand(*I).setReg(NewVReg);
2037
2038 if (MI->getOperand(*I).isUse())
2039 MI->getOperand(*I).setIsKill(true);
2040 }
2041
2042 // Fill in the new live interval.
2043 unsigned index = getInstructionIndex(MI);
2044 if (HasUse) {
2045 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002046 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002047 DOUT << " +" << LR;
2048 nI.addRange(LR);
2049 vrm.addRestorePoint(NewVReg, MI);
2050 }
2051 if (HasDef) {
2052 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002053 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002054 DOUT << " +" << LR;
2055 nI.addRange(LR);
2056 vrm.addSpillPoint(NewVReg, true, MI);
2057 }
2058
Owen Anderson17197312008-08-18 23:41:04 +00002059 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002060
Owen Andersona41e47a2008-08-19 22:12:11 +00002061 DOUT << "\t\t\t\tadded new interval: ";
2062 DEBUG(nI.dump());
2063 DOUT << '\n';
Owen Andersona41e47a2008-08-19 22:12:11 +00002064 }
Owen Anderson9a032932008-08-18 21:20:32 +00002065
Owen Anderson9a032932008-08-18 21:20:32 +00002066
Owen Andersona41e47a2008-08-19 22:12:11 +00002067 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002068 }
Owen Andersond6664312008-08-18 18:05:32 +00002069
2070 return added;
2071}
2072
2073std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002074addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002075 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002076 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002077
2078 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002079 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002080
Evan Chengf2fbca62007-11-12 06:35:08 +00002081 assert(li.weight != HUGE_VALF &&
2082 "attempt to spill already spilled interval!");
2083
2084 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00002085 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00002086 DOUT << '\n';
2087
Evan Cheng72eeb942008-12-05 17:00:16 +00002088 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002089 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002090 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002091 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002092 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2093 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002094 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002095 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002096
2097 unsigned NumValNums = li.getNumValNums();
2098 SmallVector<MachineInstr*, 4> ReMatDefs;
2099 ReMatDefs.resize(NumValNums, NULL);
2100 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2101 ReMatOrigDefs.resize(NumValNums, NULL);
2102 SmallVector<int, 4> ReMatIds;
2103 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2104 BitVector ReMatDelete(NumValNums);
2105 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2106
Evan Cheng81a03822007-11-17 00:40:40 +00002107 // Spilling a split live interval. It cannot be split any further. Also,
2108 // it's also guaranteed to be a single val# / range interval.
2109 if (vrm.getPreSplitReg(li.reg)) {
2110 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002111 // Unset the split kill marker on the last use.
2112 unsigned KillIdx = vrm.getKillPoint(li.reg);
2113 if (KillIdx) {
2114 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2115 assert(KillMI && "Last use disappeared?");
2116 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2117 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002118 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002119 }
Evan Chengadf85902007-12-05 09:51:10 +00002120 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002121 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2122 Slot = vrm.getStackSlot(li.reg);
2123 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2124 MachineInstr *ReMatDefMI = DefIsReMat ?
2125 vrm.getReMaterializedMI(li.reg) : NULL;
2126 int LdSlot = 0;
2127 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2128 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002129 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002130 bool IsFirstRange = true;
2131 for (LiveInterval::Ranges::const_iterator
2132 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2133 // If this is a split live interval with multiple ranges, it means there
2134 // are two-address instructions that re-defined the value. Only the
2135 // first def can be rematerialized!
2136 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002137 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002138 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2139 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002140 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002141 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002142 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002143 } else {
2144 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2145 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002146 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002147 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002148 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002149 }
2150 IsFirstRange = false;
2151 }
Evan Cheng419852c2008-04-03 16:39:43 +00002152
Evan Cheng4cce6b42008-04-11 17:53:36 +00002153 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002154 return NewLIs;
2155 }
2156
2157 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002158 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2159 TrySplit = false;
2160 if (TrySplit)
2161 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002162 bool NeedStackSlot = false;
2163 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2164 i != e; ++i) {
2165 const VNInfo *VNI = *i;
2166 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002167 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002168 continue; // Dead val#.
2169 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002170 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2171 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002172 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002173 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002174 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002175 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002176 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002177 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2178 ClonedMIs.push_back(Clone);
2179 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002180
2181 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002182 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002183 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002184 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002185 CanDelete = false;
2186 // Need a stack slot if there is any live range where uses cannot be
2187 // rematerialized.
2188 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002189 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002190 if (CanDelete)
2191 ReMatDelete.set(VN);
2192 } else {
2193 // Need a stack slot if there is any live range where uses cannot be
2194 // rematerialized.
2195 NeedStackSlot = true;
2196 }
2197 }
2198
2199 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002200 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2201 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2202 Slot = vrm.assignVirt2StackSlot(li.reg);
2203
2204 // This case only occurs when the prealloc splitter has already assigned
2205 // a stack slot to this vreg.
2206 else
2207 Slot = vrm.getStackSlot(li.reg);
2208 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002209
2210 // Create new intervals and rewrite defs and uses.
2211 for (LiveInterval::Ranges::const_iterator
2212 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002213 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2214 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2215 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002216 bool CanDelete = ReMatDelete[I->valno->id];
2217 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002218 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002219 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002220 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002221 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002222 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002223 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002224 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002225 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002226 }
2227
Evan Cheng0cbb1162007-11-29 01:06:25 +00002228 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002229 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002230 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002231 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002232 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002233
Evan Chengb50bb8c2007-12-05 08:16:32 +00002234 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002235 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002236 if (NeedStackSlot) {
2237 int Id = SpillMBBs.find_first();
2238 while (Id != -1) {
2239 std::vector<SRInfo> &spills = SpillIdxes[Id];
2240 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2241 int index = spills[i].index;
2242 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002243 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002244 bool isReMat = vrm.isReMaterialized(VReg);
2245 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002246 bool CanFold = false;
2247 bool FoundUse = false;
2248 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002249 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002250 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002251 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2252 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002253 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002254 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002255
2256 Ops.push_back(j);
2257 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002258 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002259 if (isReMat ||
2260 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2261 RestoreMBBs, RestoreIdxes))) {
2262 // MI has two-address uses of the same register. If the use
2263 // isn't the first and only use in the BB, then we can't fold
2264 // it. FIXME: Move this to rewriteInstructionsForSpills.
2265 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002266 break;
2267 }
Evan Chengaee4af62007-12-02 08:30:39 +00002268 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002269 }
2270 }
2271 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002272 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002273 if (CanFold && !Ops.empty()) {
2274 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002275 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002276 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002277 // Also folded uses, do not issue a load.
2278 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002279 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2280 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002281 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002282 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002283 }
2284
Evan Cheng7e073ba2008-04-09 20:57:25 +00002285 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002286 if (!Folded) {
2287 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2288 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002289 if (!MI->registerDefIsDead(nI.reg))
2290 // No need to spill a dead def.
2291 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002292 if (isKill)
2293 AddedKill.insert(&nI);
2294 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002295 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002296 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002297 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002298 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002299
Evan Cheng1953d0c2007-11-29 10:12:14 +00002300 int Id = RestoreMBBs.find_first();
2301 while (Id != -1) {
2302 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2303 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2304 int index = restores[i].index;
2305 if (index == -1)
2306 continue;
2307 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002308 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002309 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002310 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002311 bool CanFold = false;
2312 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002313 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002314 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002315 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2316 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002317 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002318 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002319
Evan Cheng0cbb1162007-11-29 01:06:25 +00002320 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002321 // If this restore were to be folded, it would have been folded
2322 // already.
2323 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002324 break;
2325 }
Evan Chengaee4af62007-12-02 08:30:39 +00002326 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002327 }
2328 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002329
2330 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002331 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002332 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002333 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002334 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2335 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002336 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2337 int LdSlot = 0;
2338 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2339 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002340 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002341 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2342 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002343 if (!Folded) {
2344 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2345 if (ImpUse) {
2346 // Re-matting an instruction with virtual register use. Add the
2347 // register as an implicit use on the use MI and update the register
2348 // interval's spill weight to HUGE_VALF to prevent it from being
2349 // spilled.
2350 LiveInterval &ImpLi = getInterval(ImpUse);
2351 ImpLi.weight = HUGE_VALF;
2352 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2353 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002354 }
Evan Chengaee4af62007-12-02 08:30:39 +00002355 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002356 }
2357 // If folding is not possible / failed, then tell the spiller to issue a
2358 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002359 if (Folded)
2360 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002361 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002362 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002363 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002364 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002365 }
2366
Evan Chengb50bb8c2007-12-05 08:16:32 +00002367 // Finalize intervals: add kills, finalize spill weights, and filter out
2368 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002369 std::vector<LiveInterval*> RetNewLIs;
2370 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2371 LiveInterval *LI = NewLIs[i];
2372 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002373 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002374 if (!AddedKill.count(LI)) {
2375 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002376 unsigned LastUseIdx = getBaseIndex(LR->end);
2377 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002378 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002379 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002380 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002381 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002382 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002383 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002384 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002385 RetNewLIs.push_back(LI);
2386 }
2387 }
Evan Cheng81a03822007-11-17 00:40:40 +00002388
Evan Cheng4cce6b42008-04-11 17:53:36 +00002389 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002390 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002391}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002392
2393/// hasAllocatableSuperReg - Return true if the specified physical register has
2394/// any super register that's allocatable.
2395bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2396 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2397 if (allocatableRegs_[*AS] && hasInterval(*AS))
2398 return true;
2399 return false;
2400}
2401
2402/// getRepresentativeReg - Find the largest super register of the specified
2403/// physical register.
2404unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2405 // Find the largest super-register that is allocatable.
2406 unsigned BestReg = Reg;
2407 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2408 unsigned SuperReg = *AS;
2409 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2410 BestReg = SuperReg;
2411 break;
2412 }
2413 }
2414 return BestReg;
2415}
2416
2417/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2418/// specified interval that conflicts with the specified physical register.
2419unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2420 unsigned PhysReg) const {
2421 unsigned NumConflicts = 0;
2422 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2423 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2424 E = mri_->reg_end(); I != E; ++I) {
2425 MachineOperand &O = I.getOperand();
2426 MachineInstr *MI = O.getParent();
2427 unsigned Index = getInstructionIndex(MI);
2428 if (pli.liveAt(Index))
2429 ++NumConflicts;
2430 }
2431 return NumConflicts;
2432}
2433
2434/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002435/// around all defs and uses of the specified interval. Return true if it
2436/// was able to cut its interval.
2437bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002438 unsigned PhysReg, VirtRegMap &vrm) {
2439 unsigned SpillReg = getRepresentativeReg(PhysReg);
2440
2441 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2442 // If there are registers which alias PhysReg, but which are not a
2443 // sub-register of the chosen representative super register. Assert
2444 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002445 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002446 tri_->isSuperRegister(*AS, SpillReg));
2447
Evan Cheng2824a652009-03-23 18:24:37 +00002448 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002449 LiveInterval &pli = getInterval(SpillReg);
2450 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2451 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2452 E = mri_->reg_end(); I != E; ++I) {
2453 MachineOperand &O = I.getOperand();
2454 MachineInstr *MI = O.getParent();
2455 if (SeenMIs.count(MI))
2456 continue;
2457 SeenMIs.insert(MI);
2458 unsigned Index = getInstructionIndex(MI);
2459 if (pli.liveAt(Index)) {
2460 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002461 unsigned StartIdx = getLoadIndex(Index);
2462 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002463 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002464 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002465 Cut = true;
2466 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002467 std::string msg;
2468 raw_string_ostream Msg(msg);
2469 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002470 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002471 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002472 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002473 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002474 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002475 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002476 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002477 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2478 if (!hasInterval(*AS))
2479 continue;
2480 LiveInterval &spli = getInterval(*AS);
2481 if (spli.liveAt(Index))
2482 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2483 }
2484 }
2485 }
Evan Cheng2824a652009-03-23 18:24:37 +00002486 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002487}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002488
2489LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002490 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002491 LiveInterval& Interval = getOrCreateInterval(reg);
2492 VNInfo* VN = Interval.getNextValue(
2493 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002494 startInst, true, getVNInfoAllocator());
2495 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002496 VN->kills.push_back(
2497 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002498 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2499 getMBBEndIdx(startInst->getParent()) + 1, VN);
2500 Interval.addRange(LR);
2501
2502 return LR;
2503}
David Greeneb5257662009-08-03 21:55:09 +00002504
2505raw_ostream &
2506IntervalPrefixPrinter::operator()(raw_ostream &out,
2507 const MachineInstr &instr) const {
2508 return out << liinfo.getInstructionIndex(&instr);
2509}