blob: 27ab5d535758bc0832dda919895eb9d4aa4d94f0 [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"
Evan Cheng2578ba22009-07-01 01:59:31 +000037#include "llvm/ADT/DepthFirstIterator.h"
38#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000039#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000041#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000042#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000043#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000044using namespace llvm;
45
Dan Gohman844731a2008-05-13 00:00:25 +000046// Hidden options for help debugging.
47static cl::opt<bool> DisableReMat("disable-rematerialization",
48 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000049
Dan Gohman844731a2008-05-13 00:00:25 +000050static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
51 cl::init(true), cl::Hidden);
52static cl::opt<int> SplitLimit("split-limit",
53 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000054
Dan Gohman4c8f8702008-07-25 15:08:37 +000055static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
56
Owen Andersonae339ba2008-08-19 00:17:30 +000057static cl::opt<bool> EnableFastSpilling("fast-spill",
58 cl::init(false), cl::Hidden);
59
Chris Lattnercd3245a2006-12-19 22:41:21 +000060STATISTIC(numIntervals, "Number of original intervals");
Evan Cheng0cbb1162007-11-29 01:06:25 +000061STATISTIC(numFolds , "Number of loads/stores folded into instructions");
62STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000063
Devang Patel19974732007-05-03 01:11:54 +000064char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000065static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000066
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman6d69ba82008-07-25 00:02:30 +000068 AU.addRequired<AliasAnalysis>();
69 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000070 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000071 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000072 AU.addPreservedID(MachineLoopInfoID);
73 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000074
75 if (!StrongPHIElim) {
76 AU.addPreservedID(PHIEliminationID);
77 AU.addRequiredID(PHIEliminationID);
78 }
79
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000080 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000081 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000082}
83
Chris Lattnerf7da2c72006-08-24 22:43:55 +000084void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000085 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000086 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000087 E = r2iMap_.end(); I != E; ++I)
88 delete I->second;
89
Evan Cheng3f32d652008-06-04 09:18:41 +000090 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000091 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000092 mi2iMap_.clear();
93 i2miMap_.clear();
94 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000095 terminatorGaps.clear();
96
Evan Chengdd199d22007-09-06 01:07:24 +000097 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
98 VNInfoAllocator.Reset();
Evan Cheng1ed99222008-07-19 00:37:25 +000099 while (!ClonedMIs.empty()) {
100 MachineInstr *MI = ClonedMIs.back();
101 ClonedMIs.pop_back();
102 mf_->DeleteMachineInstr(MI);
103 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000104}
105
Evan Cheng2578ba22009-07-01 01:59:31 +0000106/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
107/// there is one implicit_def for each use. Add isUndef marker to
108/// implicit_def defs and their uses.
109void LiveIntervals::processImplicitDefs() {
110 SmallSet<unsigned, 8> ImpDefRegs;
111 SmallVector<MachineInstr*, 8> ImpDefMIs;
112 MachineBasicBlock *Entry = mf_->begin();
113 SmallPtrSet<MachineBasicBlock*,16> Visited;
114 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
115 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
116 DFI != E; ++DFI) {
117 MachineBasicBlock *MBB = *DFI;
118 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
119 I != E; ) {
120 MachineInstr *MI = &*I;
121 ++I;
122 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
123 unsigned Reg = MI->getOperand(0).getReg();
124 MI->getOperand(0).setIsUndef();
125 ImpDefRegs.insert(Reg);
126 ImpDefMIs.push_back(MI);
127 continue;
128 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000129
130 bool ChangedToImpDef = false;
131 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000132 MachineOperand& MO = MI->getOperand(i);
133 if (!MO.isReg() || !MO.isUse())
134 continue;
135 unsigned Reg = MO.getReg();
136 if (!Reg)
137 continue;
138 if (!ImpDefRegs.count(Reg))
139 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000140 // Use is a copy, just turn it into an implicit_def.
141 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
142 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
143 Reg == SrcReg) {
144 bool isKill = MO.isKill();
145 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
146 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
147 MI->RemoveOperand(j);
148 if (isKill)
149 ImpDefRegs.erase(Reg);
150 ChangedToImpDef = true;
151 break;
152 }
153
Evan Cheng2578ba22009-07-01 01:59:31 +0000154 MO.setIsUndef();
155 if (MO.isKill() || MI->isRegTiedToDefOperand(i))
156 ImpDefRegs.erase(Reg);
157 }
158
Evan Cheng459a7c62009-07-01 08:19:36 +0000159 if (ChangedToImpDef) {
160 // Backtrack to process this new implicit_def.
161 --I;
162 } else {
163 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
164 MachineOperand& MO = MI->getOperand(i);
165 if (!MO.isReg() || !MO.isDef())
166 continue;
167 ImpDefRegs.erase(MO.getReg());
168 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000169 }
170 }
171
172 // Any outstanding liveout implicit_def's?
173 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
174 MachineInstr *MI = ImpDefMIs[i];
175 unsigned Reg = MI->getOperand(0).getReg();
176 if (TargetRegisterInfo::isPhysicalRegister(Reg))
177 // Physical registers are not liveout (yet).
178 continue;
179 if (!ImpDefRegs.count(Reg))
180 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000181
182 // If there are multiple defs of the same register and at least one
183 // is not an implicit_def, do not insert implicit_def's before the
184 // uses.
185 bool Skip = false;
186 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
187 DE = mri_->def_end(); DI != DE; ++DI) {
188 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
189 Skip = true;
190 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000191 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000192 }
193 if (Skip)
194 continue;
195
196 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
197 UE = mri_->use_end(); UI != UE; ) {
198 MachineOperand &RMO = UI.getOperand();
199 MachineInstr *RMI = &*UI;
200 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000201 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000202 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000203 continue;
Evan Cheng2578ba22009-07-01 01:59:31 +0000204 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
205 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng459a7c62009-07-01 08:19:36 +0000206 MachineInstrBuilder MIB =
207 BuildMI(*RMBB, RMI, RMI->getDebugLoc(),
208 tii_->get(TargetInstrInfo::IMPLICIT_DEF), NewVReg);
209 (*MIB).getOperand(0).setIsUndef();
Evan Cheng2578ba22009-07-01 01:59:31 +0000210 RMO.setReg(NewVReg);
211 RMO.setIsUndef();
212 RMO.setIsKill();
213 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000214 }
215 ImpDefRegs.clear();
216 ImpDefMIs.clear();
217 }
218}
219
Owen Anderson80b3ce62008-05-28 20:54:50 +0000220void LiveIntervals::computeNumbering() {
221 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000222 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000223
224 Idx2MBBMap.clear();
225 MBB2IdxMap.clear();
226 mi2iMap_.clear();
227 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000228 terminatorGaps.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000229
Owen Andersona1566f22008-07-22 22:46:49 +0000230 FunctionSize = 0;
231
Chris Lattner428b92e2006-09-15 03:57:23 +0000232 // Number MachineInstrs and MachineBasicBlocks.
233 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000234 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000235
236 unsigned MIIndex = 0;
237 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
238 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000239 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000240
Owen Anderson7fbad272008-07-23 21:37:49 +0000241 // Insert an empty slot at the beginning of each block.
242 MIIndex += InstrSlots::NUM;
243 i2miMap_.push_back(0);
244
Chris Lattner428b92e2006-09-15 03:57:23 +0000245 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
246 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000247
248 if (I == MBB->getFirstTerminator()) {
249 // Leave a gap for before terminators, this is where we will point
250 // PHI kills.
251 bool inserted =
252 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
253 assert(inserted &&
254 "Multiple 'first' terminators encountered during numbering.");
255 i2miMap_.push_back(0);
256
257 MIIndex += InstrSlots::NUM;
258 }
259
Chris Lattner428b92e2006-09-15 03:57:23 +0000260 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000261 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000262 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000263 i2miMap_.push_back(I);
264 MIIndex += InstrSlots::NUM;
Owen Andersona1566f22008-07-22 22:46:49 +0000265 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000266
Evan Cheng4ed43292008-10-18 05:21:37 +0000267 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000268 unsigned Slots = I->getDesc().getNumDefs();
269 if (Slots == 0)
270 Slots = 1;
271 MIIndex += InstrSlots::NUM * Slots;
272 while (Slots--)
273 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000274 }
Lang Hamesffd13262009-07-09 03:57:02 +0000275
276 if (MBB->getFirstTerminator() == MBB->end()) {
277 // Leave a gap for before terminators, this is where we will point
278 // PHI kills.
279 bool inserted =
280 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
281 assert(inserted &&
282 "Multiple 'first' terminators encountered during numbering.");
283 i2miMap_.push_back(0);
284
285 MIIndex += InstrSlots::NUM;
286 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000287
Owen Anderson1fbb4542008-06-16 16:58:24 +0000288 // Set the MBB2IdxMap entry for this MBB.
289 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
290 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000291 }
Lang Hamesffd13262009-07-09 03:57:02 +0000292
Evan Cheng4ca980e2007-10-17 02:10:22 +0000293 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000294
295 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000296 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000297 for (LiveInterval::iterator LI = OI->second->begin(),
298 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000299
Owen Anderson7eec0c22008-05-29 23:01:22 +0000300 // Remap the start index of the live range to the corresponding new
301 // number, or our best guess at what it _should_ correspond to if the
302 // original instruction has been erased. This is either the following
303 // instruction or its predecessor.
Owen Anderson7fbad272008-07-23 21:37:49 +0000304 unsigned index = LI->start / InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000305 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson0a7615a2008-07-25 23:06:59 +0000306 if (offset == InstrSlots::LOAD) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000307 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000308 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000309 // Take the pair containing the index
310 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000311 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000312
Owen Anderson7fbad272008-07-23 21:37:49 +0000313 LI->start = getMBBStartIdx(J->second);
314 } else {
315 LI->start = mi2iMap_[OldI2MI[index]] + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000316 }
317
318 // Remap the ending index in the same way that we remapped the start,
319 // except for the final step where we always map to the immediately
320 // following instruction.
Owen Andersond7dcbec2008-07-25 19:50:48 +0000321 index = (LI->end - 1) / InstrSlots::NUM;
Owen Anderson7fbad272008-07-23 21:37:49 +0000322 offset = LI->end % InstrSlots::NUM;
Owen Anderson9382b932008-07-30 00:22:56 +0000323 if (offset == InstrSlots::LOAD) {
324 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000325 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000326 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000327 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000328
Owen Anderson9382b932008-07-30 00:22:56 +0000329 LI->end = getMBBEndIdx(I->second) + 1;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000330 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000331 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000332 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
333
334 if (index != OldI2MI.size())
335 LI->end = mi2iMap_[OldI2MI[index]] + (idx == index ? offset : 0);
336 else
337 LI->end = InstrSlots::NUM * i2miMap_.size();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000338 }
Owen Anderson788d0412008-08-06 18:35:45 +0000339 }
340
Owen Anderson03857b22008-08-13 21:49:13 +0000341 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
342 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000343 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000344
Owen Anderson7eec0c22008-05-29 23:01:22 +0000345 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000346 // start indices above. VN's with special sentinel defs
347 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000348 if (vni->isDefAccurate() && !vni->isUnused()) {
Owen Anderson788d0412008-08-06 18:35:45 +0000349 unsigned index = vni->def / InstrSlots::NUM;
350 unsigned offset = vni->def % InstrSlots::NUM;
Owen Anderson91292392008-07-30 17:42:47 +0000351 if (offset == InstrSlots::LOAD) {
352 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000353 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000354 // Take the pair containing the index
355 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000356 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000357
Owen Anderson91292392008-07-30 17:42:47 +0000358 vni->def = getMBBStartIdx(J->second);
359 } else {
360 vni->def = mi2iMap_[OldI2MI[index]] + offset;
361 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000362 }
Owen Anderson745825f42008-05-28 22:40:08 +0000363
Owen Anderson7eec0c22008-05-29 23:01:22 +0000364 // Remap the VNInfo kill indices, which works the same as
365 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000366 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000367 unsigned killIdx = vni->kills[i].killIdx;
368
369 unsigned index = (killIdx - 1) / InstrSlots::NUM;
370 unsigned offset = killIdx % InstrSlots::NUM;
371
Owen Anderson309c6162008-09-30 22:51:54 +0000372 if (offset == InstrSlots::LOAD) {
Lang Hamesffd13262009-07-09 03:57:02 +0000373 assert("Value killed at a load slot.");
374 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000375 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000376 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000377
Lang Hamesffd13262009-07-09 03:57:02 +0000378 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000379 } else {
Lang Hamesffd13262009-07-09 03:57:02 +0000380 if (vni->kills[i].isPHIKill) {
381 std::vector<IdxMBBPair>::const_iterator I =
382 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
383 --I;
384 vni->kills[i].killIdx = terminatorGaps[I->second];
385 } else {
386 assert(OldI2MI[index] != 0 &&
387 "Kill refers to instruction not present in index maps.");
388 vni->kills[i].killIdx = mi2iMap_[OldI2MI[index]] + offset;
389 }
390
391 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000392 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000393 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
394
395 if (index != OldI2MI.size())
396 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
397 (idx == index ? offset : 0);
398 else
399 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000400 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000401 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000402 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000403 }
Owen Anderson788d0412008-08-06 18:35:45 +0000404 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000405}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000406
Lang Hamesf41538d2009-06-02 16:53:25 +0000407void LiveIntervals::scaleNumbering(int factor) {
408 // Need to
409 // * scale MBB begin and end points
410 // * scale all ranges.
411 // * Update VNI structures.
412 // * Scale instruction numberings
413
414 // Scale the MBB indices.
415 Idx2MBBMap.clear();
416 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
417 MBB != MBBE; ++MBB) {
418 std::pair<unsigned, unsigned> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
419 mbbIndices.first = InstrSlots::scale(mbbIndices.first, factor);
420 mbbIndices.second = InstrSlots::scale(mbbIndices.second, factor);
421 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
422 }
423 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
424
Lang Hamesffd13262009-07-09 03:57:02 +0000425 // Scale terminator gaps.
426 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
427 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
428 TGI != TGE; ++TGI) {
429 terminatorGaps[TGI->first] = InstrSlots::scale(TGI->second, factor);
430 }
431
Lang Hamesf41538d2009-06-02 16:53:25 +0000432 // Scale the intervals.
433 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
434 LI->second->scaleNumbering(factor);
435 }
436
437 // Scale MachineInstrs.
438 Mi2IndexMap oldmi2iMap = mi2iMap_;
439 unsigned highestSlot = 0;
440 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
441 MI != ME; ++MI) {
442 unsigned newSlot = InstrSlots::scale(MI->second, factor);
443 mi2iMap_[MI->first] = newSlot;
444 highestSlot = std::max(highestSlot, newSlot);
445 }
446
447 i2miMap_.clear();
448 i2miMap_.resize(highestSlot + 1);
449 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
450 MI != ME; ++MI) {
451 i2miMap_[MI->second] = MI->first;
452 }
453
454}
455
456
Owen Anderson80b3ce62008-05-28 20:54:50 +0000457/// runOnMachineFunction - Register allocate the whole function
458///
459bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
460 mf_ = &fn;
461 mri_ = &mf_->getRegInfo();
462 tm_ = &fn.getTarget();
463 tri_ = tm_->getRegisterInfo();
464 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000465 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000466 lv_ = &getAnalysis<LiveVariables>();
467 allocatableRegs_ = tri_->getAllocatableSet(fn);
468
Evan Cheng2578ba22009-07-01 01:59:31 +0000469 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000470 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000471 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000472
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000473 numIntervals += getNumIntervals();
474
Chris Lattner70ca3582004-09-30 15:59:17 +0000475 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000476 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000477}
478
Chris Lattner70ca3582004-09-30 15:59:17 +0000479/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000480void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000481 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000482 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Owen Anderson03857b22008-08-13 21:49:13 +0000483 I->second->print(O, tri_);
Evan Cheng3f32d652008-06-04 09:18:41 +0000484 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000485 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000486
487 O << "********** MACHINEINSTRS **********\n";
488 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
489 mbbi != mbbe; ++mbbi) {
490 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
491 for (MachineBasicBlock::iterator mii = mbbi->begin(),
492 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000493 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000494 }
495 }
496}
497
Evan Chengc92da382007-11-03 07:20:12 +0000498/// conflictsWithPhysRegDef - Returns true if the specified register
499/// is defined during the duration of the specified interval.
500bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
501 VirtRegMap &vrm, unsigned reg) {
502 for (LiveInterval::Ranges::const_iterator
503 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
504 for (unsigned index = getBaseIndex(I->start),
505 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
506 index += InstrSlots::NUM) {
507 // skip deleted instructions
508 while (index != end && !getInstructionFromIndex(index))
509 index += InstrSlots::NUM;
510 if (index == end) break;
511
512 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000513 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
514 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000515 if (SrcReg == li.reg || DstReg == li.reg)
516 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000517 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
518 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000519 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000520 continue;
521 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000522 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000523 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000524 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000525 if (!vrm.hasPhys(PhysReg))
526 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000527 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000528 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000529 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000530 return true;
531 }
532 }
533 }
534
535 return false;
536}
537
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000538/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
539/// it can check use as well.
540bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
541 unsigned Reg, bool CheckUse,
542 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
543 for (LiveInterval::Ranges::const_iterator
544 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
545 for (unsigned index = getBaseIndex(I->start),
546 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
547 index += InstrSlots::NUM) {
548 // Skip deleted instructions.
549 MachineInstr *MI = 0;
550 while (index != end) {
551 MI = getInstructionFromIndex(index);
552 if (MI)
553 break;
554 index += InstrSlots::NUM;
555 }
556 if (index == end) break;
557
558 if (JoinedCopies.count(MI))
559 continue;
560 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
561 MachineOperand& MO = MI->getOperand(i);
562 if (!MO.isReg())
563 continue;
564 if (MO.isUse() && !CheckUse)
565 continue;
566 unsigned PhysReg = MO.getReg();
567 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
568 continue;
569 if (tri_->isSubRegister(Reg, PhysReg))
570 return true;
571 }
572 }
573 }
574
575 return false;
576}
577
578
Evan Cheng549f27d32007-08-13 23:45:17 +0000579void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000580 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000581 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000582 else
583 cerr << "%reg" << reg;
584}
585
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000586void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000587 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000588 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000589 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000590 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000591 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000592 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000593
Evan Cheng419852c2008-04-03 16:39:43 +0000594 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
595 DOUT << "is a implicit_def\n";
596 return;
597 }
598
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000599 // Virtual registers may be defined multiple times (due to phi
600 // elimination and 2-addr elimination). Much of what we do only has to be
601 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000602 // time we see a vreg.
603 if (interval.empty()) {
604 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000605 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000606 // Earlyclobbers move back one.
607 if (MO.isEarlyClobber())
608 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000609 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000610 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000611 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000612 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000613 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000614 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000615 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000616 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000617 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000618 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000619
620 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000621
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000622 // Loop over all of the blocks that the vreg is defined in. There are
623 // two cases we have to handle here. The most common case is a vreg
624 // whose lifetime is contained within a basic block. In this case there
625 // will be a single kill, in MBB, which comes after the definition.
626 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
627 // FIXME: what about dead vars?
628 unsigned killIdx;
629 if (vi.Kills[0] != mi)
630 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
631 else
632 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000633
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000634 // If the kill happens after the definition, we have an intra-block
635 // live range.
636 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000637 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000639 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000641 DOUT << " +" << LR << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000642 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000643 return;
644 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000645 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000646
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 // The other case we handle is when a virtual register lives to the end
648 // of the defining block, potentially live across some blocks, then is
649 // live into some number of blocks, but gets killed. Start by adding a
650 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000651 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000652 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000653 interval.addRange(NewLR);
654
655 // Iterate over all of the blocks that the variable is completely
656 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
657 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000658 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
659 E = vi.AliveBlocks.end(); I != E; ++I) {
660 LiveRange LR(getMBBStartIdx(*I),
661 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000662 ValNo);
663 interval.addRange(LR);
664 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 }
666
667 // Finally, this virtual register is live from the start of any killing
668 // block to the 'use' slot of the killing instruction.
669 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
670 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000671 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000672 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000673 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000674 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000675 interval.addKill(ValNo, killIdx, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000676 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 }
678
679 } else {
680 // If this is the second time we see a virtual register definition, it
681 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000682 // the result of two address elimination, then the vreg is one of the
683 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000684 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000685 // If this is a two-address definition, then we have already processed
686 // the live range. The only problem is that we didn't realize there
687 // are actually two values in the live interval. Because of this we
688 // need to take the LiveRegion that defines this register and split it
689 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000690 assert(interval.containsOneValue());
691 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000692 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000693 if (MO.isEarlyClobber())
694 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000695
Evan Cheng4f8ff162007-08-11 00:59:19 +0000696 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000697 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000698
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000700 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000702
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000703 // Two-address vregs should always only be redefined once. This means
704 // that at this point, there should be exactly one value number in it.
705 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
706
Chris Lattner91725b72006-08-31 05:54:43 +0000707 // The new value number (#1) is defined by the instruction we claimed
708 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000709 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
Lang Hames857c4e02009-06-17 21:01:20 +0000710 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000711 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000712 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
713
Chris Lattner91725b72006-08-31 05:54:43 +0000714 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000715 OldValNo->def = RedefIndex;
716 OldValNo->copy = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000717 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000718 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000719
720 // Add the new live interval which replaces the range for the input copy.
721 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000722 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000723 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000724 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000725
726 // If this redefinition is dead, we need to add a dummy unit live
727 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000728 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000729 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000730
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000731 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000732 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000733
734 } else {
735 // Otherwise, this must be because of phi elimination. If this is the
736 // first redefinition of the vreg that we have seen, go back and change
737 // the live range in the PHI block to be a different value number.
738 if (interval.containsOneValue()) {
739 assert(vi.Kills.size() == 1 &&
740 "PHI elimination vreg should have one kill, the PHI itself!");
741
742 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000743 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000744 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000745 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000746 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000747 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000748 interval.print(DOUT, tri_); DOUT << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000749 interval.removeRange(Start, End);
750 assert(interval.ranges.size() == 1 &&
751 "newly discovered PHI interval has >1 ranges.");
752 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
753 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000754 VNI->setHasPHIKill(true);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000755 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000756
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000757 // Replace the interval with one of a NEW value number. Note that this
758 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000759 LiveRange LR(Start, End,
760 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000761 LR.valno->setIsPHIDef(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000762 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000763 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000764 interval.addKill(LR.valno, End, false);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000765 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000766 }
767
768 // In the case of PHI elimination, each variable definition is only
769 // live until the end of the block. We've already taken care of the
770 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000771 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000772 if (MO.isEarlyClobber())
773 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000774
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000775 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000776 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000777 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000778 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000779 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000780 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000781 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000782 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000783 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000784
Owen Anderson7fbad272008-07-23 21:37:49 +0000785 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000786 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000787 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000788 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000789 ValNo->setHasPHIKill(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000790 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000791 }
792 }
793
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000794 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000795}
796
Chris Lattnerf35fef72004-07-23 21:24:19 +0000797void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000798 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000799 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000800 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000801 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000802 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000803 // A physical register cannot be live across basic block, so its
804 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000805 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000806
Chris Lattner6b128bd2006-09-03 08:07:11 +0000807 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000808 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000809 // Earlyclobbers move back one.
810 if (MO.isEarlyClobber())
811 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000812 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000813
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000814 // If it is not used after definition, it is considered dead at
815 // the instruction defining it. Hence its interval is:
816 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000817 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000818 DOUT << " dead";
Dale Johannesen86b49f82008-09-24 01:07:17 +0000819 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000820 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000821 }
822
823 // If it is not dead on definition, it must be killed by a
824 // subsequent instruction. Hence its interval is:
825 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000826 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000827 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000828 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
829 getInstructionFromIndex(baseIndex) == 0)
830 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000831 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000832 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000833 end = getUseIndex(baseIndex) + 1;
834 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000835 } else {
836 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
837 if (DefIdx != -1) {
838 if (mi->isRegTiedToUseOperand(DefIdx)) {
839 // Two-address instruction.
840 end = getDefIndex(baseIndex);
841 if (mi->getOperand(DefIdx).isEarlyClobber())
842 end = getUseIndex(baseIndex);
843 } else {
844 // Another instruction redefines the register before it is ever read.
845 // Then the register is essentially dead at the instruction that defines
846 // it. Hence its interval is:
847 // [defSlot(def), defSlot(def)+1)
848 DOUT << " dead";
849 end = start + 1;
850 }
851 goto exit;
852 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000853 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000854
855 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000856 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000857
858 // The only case we should have a dead physreg here without a killing or
859 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000860 // and never used. Another possible case is the implicit use of the
861 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000862 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000863
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000864exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000865 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000866
Evan Cheng24a3cc42007-04-25 07:30:23 +0000867 // Already exists? Extend old live interval.
868 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000869 bool Extend = OldLR != interval.end();
870 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000871 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000872 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000873 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000874 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000875 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000876 interval.addKill(LR.valno, end, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000877 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000878}
879
Chris Lattnerf35fef72004-07-23 21:24:19 +0000880void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
881 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000882 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000883 MachineOperand& MO,
884 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000885 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000886 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000887 getOrCreateInterval(MO.getReg()));
888 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000889 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000890 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000891 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000892 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000893 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000894 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000895 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000896 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000897 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000898 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000899 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000900 // If MI also modifies the sub-register explicitly, avoid processing it
901 // more than once. Do not pass in TRI here so it checks for exact match.
902 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000903 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000904 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000905 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000906}
907
Evan Chengb371f452007-02-19 21:49:54 +0000908void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000909 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000910 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000911 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
912
913 // Look for kills, if it reaches a def before it's killed, then it shouldn't
914 // be considered a livein.
915 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000916 unsigned baseIndex = MIIdx;
917 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000918 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
919 getInstructionFromIndex(baseIndex) == 0)
920 baseIndex += InstrSlots::NUM;
921 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000922 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000923
Evan Chengb371f452007-02-19 21:49:54 +0000924 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000925 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000926 DOUT << " killed";
927 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000928 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000929 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000930 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000931 // Another instruction redefines the register before it is ever read.
932 // Then the register is essentially dead at the instruction that defines
933 // it. Hence its interval is:
934 // [defSlot(def), defSlot(def)+1)
935 DOUT << " dead";
936 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000937 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000938 break;
Evan Chengb371f452007-02-19 21:49:54 +0000939 }
940
941 baseIndex += InstrSlots::NUM;
942 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000943 if (mi != MBB->end()) {
944 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
945 getInstructionFromIndex(baseIndex) == 0)
946 baseIndex += InstrSlots::NUM;
947 }
Evan Chengb371f452007-02-19 21:49:54 +0000948 }
949
Evan Cheng75611fb2007-06-27 01:16:36 +0000950 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000951 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000952 if (isAlias) {
953 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000954 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000955 } else {
956 DOUT << " live through";
957 end = baseIndex;
958 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000959 }
960
Lang Hames10382fb2009-06-19 02:17:53 +0000961 VNInfo *vni =
962 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000963 vni->setIsPHIDef(true);
964 LiveRange LR(start, end, vni);
965
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000966 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000967 interval.addKill(LR.valno, end, false);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000968 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000969}
970
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000971/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000972/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000973/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000974/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000975void LiveIntervals::computeIntervals() {
Dale Johannesen91aac102008-09-17 21:13:11 +0000976
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000977 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
978 << "********** Function: "
979 << ((Value*)mf_->getFunction())->getName() << '\n';
Owen Anderson7fbad272008-07-23 21:37:49 +0000980
Chris Lattner428b92e2006-09-15 03:57:23 +0000981 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
982 MBBI != E; ++MBBI) {
983 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000984 // Track the index of the current machine instr.
985 unsigned MIIndex = getMBBStartIdx(MBB);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000986 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000987
Chris Lattner428b92e2006-09-15 03:57:23 +0000988 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000989
Dan Gohmancb406c22007-10-03 19:26:29 +0000990 // Create intervals for live-ins to this BB first.
991 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
992 LE = MBB->livein_end(); LI != LE; ++LI) {
993 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
994 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000995 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000996 if (!hasInterval(*AS))
997 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
998 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000999 }
1000
Owen Anderson99500ae2008-09-15 22:00:38 +00001001 // Skip over empty initial indices.
1002 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1003 getInstructionFromIndex(MIIndex) == 0)
1004 MIIndex += InstrSlots::NUM;
1005
Chris Lattner428b92e2006-09-15 03:57:23 +00001006 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001007 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001008
Evan Cheng438f7bc2006-11-10 08:43:01 +00001009 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001010 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1011 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001012 // handle register defs - build intervals
Dan Gohmand735b802008-10-03 15:45:36 +00001013 if (MO.isReg() && MO.getReg() && MO.isDef()) {
Evan Chengef0732d2008-07-10 07:35:43 +00001014 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Dale Johannesen91aac102008-09-17 21:13:11 +00001015 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001016 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001017
1018 // Skip over the empty slots after each instruction.
1019 unsigned Slots = MI->getDesc().getNumDefs();
1020 if (Slots == 0)
1021 Slots = 1;
1022 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001023
1024 // Skip over empty indices.
1025 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1026 getInstructionFromIndex(MIIndex) == 0)
1027 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001028 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001029 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001030}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001031
Evan Chengd0e32c52008-10-29 05:06:14 +00001032bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001033 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001034 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001035 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001036
1037 bool ResVal = false;
1038 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001039 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001040 break;
1041 MBBs.push_back(I->second);
1042 ResVal = true;
1043 ++I;
1044 }
1045 return ResVal;
1046}
1047
Evan Chengd0e32c52008-10-29 05:06:14 +00001048bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1049 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1050 std::vector<IdxMBBPair>::const_iterator I =
1051 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1052
1053 bool ResVal = false;
1054 while (I != Idx2MBBMap.end()) {
1055 if (I->first > End)
1056 break;
1057 MachineBasicBlock *MBB = I->second;
1058 if (getMBBEndIdx(MBB) > End)
1059 break;
1060 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1061 SE = MBB->succ_end(); SI != SE; ++SI)
1062 MBBs.push_back(*SI);
1063 ResVal = true;
1064 ++I;
1065 }
1066 return ResVal;
1067}
1068
Owen Anderson03857b22008-08-13 21:49:13 +00001069LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001070 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001071 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001072}
Evan Chengf2fbca62007-11-12 06:35:08 +00001073
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001074/// dupInterval - Duplicate a live interval. The caller is responsible for
1075/// managing the allocated memory.
1076LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1077 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001078 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001079 return NewLI;
1080}
1081
Evan Chengc8d044e2008-02-15 18:24:29 +00001082/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1083/// copy field and returns the source register that defines it.
1084unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
1085 if (!VNI->copy)
1086 return 0;
1087
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001088 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1089 // If it's extracting out of a physical register, return the sub-register.
1090 unsigned Reg = VNI->copy->getOperand(1).getReg();
1091 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1092 Reg = tri_->getSubReg(Reg, VNI->copy->getOperand(2).getImm());
1093 return Reg;
Dan Gohman97121ba2009-04-08 00:15:30 +00001094 } else if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1095 VNI->copy->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng7e073ba2008-04-09 20:57:25 +00001096 return VNI->copy->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001097
Evan Cheng04ee5a12009-01-20 19:12:24 +00001098 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1099 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001100 return SrcReg;
1101 assert(0 && "Unrecognized copy instruction!");
1102 return 0;
1103}
Evan Chengf2fbca62007-11-12 06:35:08 +00001104
1105//===----------------------------------------------------------------------===//
1106// Register allocator hooks.
1107//
1108
Evan Chengd70dbb52008-02-22 09:24:50 +00001109/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1110/// allow one) virtual register operand, then its uses are implicitly using
1111/// the register. Returns the virtual register.
1112unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1113 MachineInstr *MI) const {
1114 unsigned RegOp = 0;
1115 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1116 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001117 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001118 continue;
1119 unsigned Reg = MO.getReg();
1120 if (Reg == 0 || Reg == li.reg)
1121 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001122
1123 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1124 !allocatableRegs_[Reg])
1125 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001126 // FIXME: For now, only remat MI with at most one register operand.
1127 assert(!RegOp &&
1128 "Can't rematerialize instruction with multiple register operand!");
1129 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001130#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001131 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001132#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001133 }
1134 return RegOp;
1135}
1136
1137/// isValNoAvailableAt - Return true if the val# of the specified interval
1138/// which reaches the given instruction also reaches the specified use index.
1139bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1140 unsigned UseIdx) const {
1141 unsigned Index = getInstructionIndex(MI);
1142 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1143 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1144 return UI != li.end() && UI->valno == ValNo;
1145}
1146
Evan Chengf2fbca62007-11-12 06:35:08 +00001147/// isReMaterializable - Returns true if the definition MI of the specified
1148/// val# of the specified interval is re-materializable.
1149bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001150 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001151 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001152 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001153 if (DisableReMat)
1154 return false;
1155
Evan Cheng20ccded2008-03-15 00:19:36 +00001156 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001157 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001158
1159 int FrameIdx = 0;
1160 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001161 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001162 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1163 // this but remember this is not safe to fold into a two-address
1164 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001165 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001166 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001167
Dan Gohman6d69ba82008-07-25 00:02:30 +00001168 // If the target-specific rules don't identify an instruction as
1169 // being trivially rematerializable, use some target-independent
1170 // rules.
1171 if (!MI->getDesc().isRematerializable() ||
1172 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001173 if (!EnableAggressiveRemat)
1174 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001175
Dan Gohman0471a792008-07-28 18:43:51 +00001176 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001177 // we can't analyze it.
1178 const TargetInstrDesc &TID = MI->getDesc();
1179 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1180 return false;
1181
1182 // Avoid instructions obviously unsafe for remat.
1183 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1184 return false;
1185
1186 // If the instruction accesses memory and the memory could be non-constant,
1187 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001188 for (std::list<MachineMemOperand>::const_iterator
1189 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001190 const MachineMemOperand &MMO = *I;
1191 if (MMO.isVolatile() || MMO.isStore())
1192 return false;
1193 const Value *V = MMO.getValue();
1194 if (!V)
1195 return false;
1196 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1197 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001198 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001199 } else if (!aa_->pointsToConstantMemory(V))
1200 return false;
1201 }
1202
1203 // If any of the registers accessed are non-constant, conservatively assume
1204 // the instruction is not rematerializable.
1205 unsigned ImpUse = 0;
1206 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1207 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001208 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001209 unsigned Reg = MO.getReg();
1210 if (Reg == 0)
1211 continue;
1212 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1213 return false;
1214
1215 // Only allow one def, and that in the first operand.
1216 if (MO.isDef() != (i == 0))
1217 return false;
1218
1219 // Only allow constant-valued registers.
1220 bool IsLiveIn = mri_->isLiveIn(Reg);
1221 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1222 E = mri_->def_end();
1223
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001224 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001225 if (MO.isDef() && (next(I) != E || IsLiveIn))
1226 return false;
1227
1228 if (MO.isUse()) {
1229 // Only allow one use other register use, as that's all the
1230 // remat mechanisms support currently.
1231 if (Reg != li.reg) {
1232 if (ImpUse == 0)
1233 ImpUse = Reg;
1234 else if (Reg != ImpUse)
1235 return false;
1236 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001237 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001238 if (I != E && (next(I) != E || IsLiveIn))
1239 return false;
1240 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001241 }
1242 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001243 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001244
Dan Gohman6d69ba82008-07-25 00:02:30 +00001245 unsigned ImpUse = getReMatImplicitUse(li, MI);
1246 if (ImpUse) {
1247 const LiveInterval &ImpLi = getInterval(ImpUse);
1248 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1249 re = mri_->use_end(); ri != re; ++ri) {
1250 MachineInstr *UseMI = &*ri;
1251 unsigned UseIdx = getInstructionIndex(UseMI);
1252 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1253 continue;
1254 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1255 return false;
1256 }
Evan Chengdc377862008-09-30 15:44:16 +00001257
1258 // If a register operand of the re-materialized instruction is going to
1259 // be spilled next, then it's not legal to re-materialize this instruction.
1260 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1261 if (ImpUse == SpillIs[i]->reg)
1262 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001263 }
1264 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001265}
1266
Evan Cheng06587492008-10-24 02:05:00 +00001267/// isReMaterializable - Returns true if the definition MI of the specified
1268/// val# of the specified interval is re-materializable.
1269bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1270 const VNInfo *ValNo, MachineInstr *MI) {
1271 SmallVector<LiveInterval*, 4> Dummy1;
1272 bool Dummy2;
1273 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1274}
1275
Evan Cheng5ef3a042007-12-06 00:01:56 +00001276/// isReMaterializable - Returns true if every definition of MI of every
1277/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001278bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1279 SmallVectorImpl<LiveInterval*> &SpillIs,
1280 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001281 isLoad = false;
1282 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1283 i != e; ++i) {
1284 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001285 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001286 continue; // Dead val#.
1287 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001288 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001289 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001290 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001291 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001292 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001293 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001294 return false;
1295 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001296 }
1297 return true;
1298}
1299
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001300/// FilterFoldedOps - Filter out two-address use operands. Return
1301/// true if it finds any issue with the operands that ought to prevent
1302/// folding.
1303static bool FilterFoldedOps(MachineInstr *MI,
1304 SmallVector<unsigned, 2> &Ops,
1305 unsigned &MRInfo,
1306 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001307 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001308 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1309 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001310 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001311 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001312 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001313 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001314 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001315 MRInfo |= (unsigned)VirtRegMap::isMod;
1316 else {
1317 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001318 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001319 MRInfo = VirtRegMap::isModRef;
1320 continue;
1321 }
1322 MRInfo |= (unsigned)VirtRegMap::isRef;
1323 }
1324 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001325 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001326 return false;
1327}
1328
1329
1330/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1331/// slot / to reg or any rematerialized load into ith operand of specified
1332/// MI. If it is successul, MI is updated with the newly created MI and
1333/// returns true.
1334bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1335 VirtRegMap &vrm, MachineInstr *DefMI,
1336 unsigned InstrIdx,
1337 SmallVector<unsigned, 2> &Ops,
1338 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001339 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001340 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001341 RemoveMachineInstrFromMaps(MI);
1342 vrm.RemoveMachineInstrFromMaps(MI);
1343 MI->eraseFromParent();
1344 ++numFolds;
1345 return true;
1346 }
1347
1348 // Filter the list of operand indexes that are to be folded. Abort if
1349 // any operand will prevent folding.
1350 unsigned MRInfo = 0;
1351 SmallVector<unsigned, 2> FoldOps;
1352 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1353 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001354
Evan Cheng427f4c12008-03-31 23:19:51 +00001355 // The only time it's safe to fold into a two address instruction is when
1356 // it's folding reload and spill from / into a spill stack slot.
1357 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001358 return false;
1359
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001360 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1361 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001362 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001363 // Remember this instruction uses the spill slot.
1364 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1365
Evan Chengf2fbca62007-11-12 06:35:08 +00001366 // Attempt to fold the memory reference into the instruction. If
1367 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001368 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001369 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001370 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001371 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001372 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001373 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001374 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001375 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1376 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001377 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001378 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001379 return true;
1380 }
1381 return false;
1382}
1383
Evan Cheng018f9b02007-12-05 03:22:34 +00001384/// canFoldMemoryOperand - Returns true if the specified load / store
1385/// folding is possible.
1386bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001387 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001388 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001389 // Filter the list of operand indexes that are to be folded. Abort if
1390 // any operand will prevent folding.
1391 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001392 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001393 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1394 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001395
Evan Cheng3c75ba82008-04-01 21:37:32 +00001396 // It's only legal to remat for a use, not a def.
1397 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001398 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001399
Evan Chengd70dbb52008-02-22 09:24:50 +00001400 return tii_->canFoldMemoryOperand(MI, FoldOps);
1401}
1402
Evan Cheng81a03822007-11-17 00:40:40 +00001403bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1404 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1405 for (LiveInterval::Ranges::const_iterator
1406 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1407 std::vector<IdxMBBPair>::const_iterator II =
1408 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1409 if (II == Idx2MBBMap.end())
1410 continue;
1411 if (I->end > II->first) // crossing a MBB.
1412 return false;
1413 MBBs.insert(II->second);
1414 if (MBBs.size() > 1)
1415 return false;
1416 }
1417 return true;
1418}
1419
Evan Chengd70dbb52008-02-22 09:24:50 +00001420/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1421/// interval on to-be re-materialized operands of MI) with new register.
1422void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1423 MachineInstr *MI, unsigned NewVReg,
1424 VirtRegMap &vrm) {
1425 // There is an implicit use. That means one of the other operand is
1426 // being remat'ed and the remat'ed instruction has li.reg as an
1427 // use operand. Make sure we rewrite that as well.
1428 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1429 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001430 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001431 continue;
1432 unsigned Reg = MO.getReg();
1433 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1434 continue;
1435 if (!vrm.isReMaterialized(Reg))
1436 continue;
1437 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001438 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1439 if (UseMO)
1440 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001441 }
1442}
1443
Evan Chengf2fbca62007-11-12 06:35:08 +00001444/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1445/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001446bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001447rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1448 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001449 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001450 unsigned Slot, int LdSlot,
1451 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001452 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001453 const TargetRegisterClass* rc,
1454 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001455 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001456 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001457 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001458 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001459 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001460 RestartInstruction:
1461 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1462 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001463 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001464 continue;
1465 unsigned Reg = mop.getReg();
1466 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001467 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001468 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001469 if (Reg != li.reg)
1470 continue;
1471
1472 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001473 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001474 int FoldSlot = Slot;
1475 if (DefIsReMat) {
1476 // If this is the rematerializable definition MI itself and
1477 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001478 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001479 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1480 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001481 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001482 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001483 MI->eraseFromParent();
1484 break;
1485 }
1486
1487 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001488 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001489 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001490 if (isLoad) {
1491 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1492 FoldSS = isLoadSS;
1493 FoldSlot = LdSlot;
1494 }
1495 }
1496
Evan Chengf2fbca62007-11-12 06:35:08 +00001497 // Scan all of the operands of this instruction rewriting operands
1498 // to use NewVReg instead of li.reg as appropriate. We do this for
1499 // two reasons:
1500 //
1501 // 1. If the instr reads the same spilled vreg multiple times, we
1502 // want to reuse the NewVReg.
1503 // 2. If the instr is a two-addr instruction, we are required to
1504 // keep the src/dst regs pinned.
1505 //
1506 // Keep track of whether we replace a use and/or def so that we can
1507 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001508
Evan Cheng81a03822007-11-17 00:40:40 +00001509 HasUse = mop.isUse();
1510 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001511 SmallVector<unsigned, 2> Ops;
1512 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001513 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001514 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001515 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001516 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001517 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001518 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001519 continue;
1520 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001521 Ops.push_back(j);
1522 HasUse |= MOj.isUse();
1523 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001524 }
1525 }
1526
Evan Cheng79a796c2008-07-12 01:56:02 +00001527 if (HasUse && !li.liveAt(getUseIndex(index)))
1528 // Must be defined by an implicit def. It should not be spilled. Note,
1529 // this is for correctness reason. e.g.
1530 // 8 %reg1024<def> = IMPLICIT_DEF
1531 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1532 // The live range [12, 14) are not part of the r1024 live interval since
1533 // it's defined by an implicit def. It will not conflicts with live
1534 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001535 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001536 // the INSERT_SUBREG and both target registers that would overlap.
1537 HasUse = false;
1538
David Greene26b86a02008-10-27 17:38:59 +00001539 // Create a new virtual register for the spill interval.
1540 // Create the new register now so we can map the fold instruction
1541 // to the new register so when it is unfolded we get the correct
1542 // answer.
1543 bool CreatedNewVReg = false;
1544 if (NewVReg == 0) {
1545 NewVReg = mri_->createVirtualRegister(rc);
1546 vrm.grow();
1547 CreatedNewVReg = true;
1548 }
1549
Evan Cheng9c3c2212008-06-06 07:54:39 +00001550 if (!TryFold)
1551 CanFold = false;
1552 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001553 // Do not fold load / store here if we are splitting. We'll find an
1554 // optimal point to insert a load / store later.
1555 if (!TrySplit) {
1556 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001557 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001558 // Folding the load/store can completely change the instruction in
1559 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001560
1561 if (FoldSS) {
1562 // We need to give the new vreg the same stack slot as the
1563 // spilled interval.
1564 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1565 }
1566
Evan Cheng018f9b02007-12-05 03:22:34 +00001567 HasUse = false;
1568 HasDef = false;
1569 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001570 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001571 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001572 goto RestartInstruction;
1573 }
1574 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001575 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001576 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001577 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001578 }
Evan Chengcddbb832007-11-30 21:23:43 +00001579
Evan Chengcddbb832007-11-30 21:23:43 +00001580 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001581 if (mop.isImplicit())
1582 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001583
1584 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001585 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1586 MachineOperand &mopj = MI->getOperand(Ops[j]);
1587 mopj.setReg(NewVReg);
1588 if (mopj.isImplicit())
1589 rewriteImplicitOps(li, MI, NewVReg, vrm);
1590 }
Evan Chengcddbb832007-11-30 21:23:43 +00001591
Evan Cheng81a03822007-11-17 00:40:40 +00001592 if (CreatedNewVReg) {
1593 if (DefIsReMat) {
1594 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001595 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001596 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001597 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001598 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001599 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001600 }
1601 if (!CanDelete || (HasUse && HasDef)) {
1602 // If this is a two-addr instruction then its use operands are
1603 // rematerializable but its def is not. It should be assigned a
1604 // stack slot.
1605 vrm.assignVirt2StackSlot(NewVReg, Slot);
1606 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001607 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001608 vrm.assignVirt2StackSlot(NewVReg, Slot);
1609 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001610 } else if (HasUse && HasDef &&
1611 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1612 // If this interval hasn't been assigned a stack slot (because earlier
1613 // def is a deleted remat def), do it now.
1614 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1615 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001616 }
1617
Evan Cheng313d4b82008-02-23 00:33:04 +00001618 // Re-matting an instruction with virtual register use. Add the
1619 // register as an implicit use on the use MI.
1620 if (DefIsReMat && ImpUse)
1621 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1622
Evan Cheng5b69eba2009-04-21 22:46:52 +00001623 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001624 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001625 if (CreatedNewVReg) {
1626 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001627 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001628 if (TrySplit)
1629 vrm.setIsSplitFromReg(NewVReg, li.reg);
1630 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001631
1632 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001633 if (CreatedNewVReg) {
1634 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001635 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Cheng81a03822007-11-17 00:40:40 +00001636 DOUT << " +" << LR;
1637 nI.addRange(LR);
1638 } else {
1639 // Extend the split live interval to this def / use.
1640 unsigned End = getUseIndex(index)+1;
1641 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1642 nI.getValNumInfo(nI.getNumValNums()-1));
1643 DOUT << " +" << LR;
1644 nI.addRange(LR);
1645 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001646 }
1647 if (HasDef) {
1648 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001649 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Chengf2fbca62007-11-12 06:35:08 +00001650 DOUT << " +" << LR;
1651 nI.addRange(LR);
1652 }
Evan Cheng81a03822007-11-17 00:40:40 +00001653
Evan Chengf2fbca62007-11-12 06:35:08 +00001654 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001655 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001656 DOUT << '\n';
1657 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001658 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001659}
Evan Cheng81a03822007-11-17 00:40:40 +00001660bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001661 const VNInfo *VNI,
1662 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001663 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001664 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001665 if (VNI->kills[j].isPHIKill)
1666 continue;
1667
1668 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001669 if (KillIdx > Idx && KillIdx < End)
1670 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001671 }
1672 return false;
1673}
1674
Evan Cheng063284c2008-02-21 00:34:19 +00001675/// RewriteInfo - Keep track of machine instrs that will be rewritten
1676/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001677namespace {
1678 struct RewriteInfo {
1679 unsigned Index;
1680 MachineInstr *MI;
1681 bool HasUse;
1682 bool HasDef;
1683 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1684 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1685 };
Evan Cheng063284c2008-02-21 00:34:19 +00001686
Dan Gohman844731a2008-05-13 00:00:25 +00001687 struct RewriteInfoCompare {
1688 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1689 return LHS.Index < RHS.Index;
1690 }
1691 };
1692}
Evan Cheng063284c2008-02-21 00:34:19 +00001693
Evan Chengf2fbca62007-11-12 06:35:08 +00001694void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001695rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001696 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001697 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001698 unsigned Slot, int LdSlot,
1699 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001700 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001701 const TargetRegisterClass* rc,
1702 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001703 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001704 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001705 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001706 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001707 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1708 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001709 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001710 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001711 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001712 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001713 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001714
Evan Cheng063284c2008-02-21 00:34:19 +00001715 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001716 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001717 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001718 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1719 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001720 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001721 MachineOperand &O = ri.getOperand();
1722 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001723 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001724 unsigned index = getInstructionIndex(MI);
1725 if (index < start || index >= end)
1726 continue;
Evan Cheng79a796c2008-07-12 01:56:02 +00001727 if (O.isUse() && !li.liveAt(getUseIndex(index)))
1728 // Must be defined by an implicit def. It should not be spilled. Note,
1729 // this is for correctness reason. e.g.
1730 // 8 %reg1024<def> = IMPLICIT_DEF
1731 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1732 // The live range [12, 14) are not part of the r1024 live interval since
1733 // it's defined by an implicit def. It will not conflicts with live
1734 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001735 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001736 // the INSERT_SUBREG and both target registers that would overlap.
1737 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001738 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1739 }
1740 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1741
Evan Cheng313d4b82008-02-23 00:33:04 +00001742 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001743 // Now rewrite the defs and uses.
1744 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1745 RewriteInfo &rwi = RewriteMIs[i];
1746 ++i;
1747 unsigned index = rwi.Index;
1748 bool MIHasUse = rwi.HasUse;
1749 bool MIHasDef = rwi.HasDef;
1750 MachineInstr *MI = rwi.MI;
1751 // If MI def and/or use the same register multiple times, then there
1752 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001753 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001754 while (i != e && RewriteMIs[i].MI == MI) {
1755 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001756 bool isUse = RewriteMIs[i].HasUse;
1757 if (isUse) ++NumUses;
1758 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001759 MIHasDef |= RewriteMIs[i].HasDef;
1760 ++i;
1761 }
Evan Cheng81a03822007-11-17 00:40:40 +00001762 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001763
Evan Cheng0a891ed2008-05-23 23:00:04 +00001764 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001765 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001766 // register interval's spill weight to HUGE_VALF to prevent it from
1767 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001768 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001769 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001770 }
1771
Evan Cheng063284c2008-02-21 00:34:19 +00001772 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001773 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001774 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001775 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001776 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001777 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001778 // One common case:
1779 // x = use
1780 // ...
1781 // ...
1782 // def = ...
1783 // = use
1784 // It's better to start a new interval to avoid artifically
1785 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001786 if (MIHasDef && !MIHasUse) {
1787 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001788 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001789 }
1790 }
Evan Chengcada2452007-11-28 01:28:46 +00001791 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001792
1793 bool IsNew = ThisVReg == 0;
1794 if (IsNew) {
1795 // This ends the previous live interval. If all of its def / use
1796 // can be folded, give it a low spill weight.
1797 if (NewVReg && TrySplit && AllCanFold) {
1798 LiveInterval &nI = getOrCreateInterval(NewVReg);
1799 nI.weight /= 10.0F;
1800 }
1801 AllCanFold = true;
1802 }
1803 NewVReg = ThisVReg;
1804
Evan Cheng81a03822007-11-17 00:40:40 +00001805 bool HasDef = false;
1806 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001807 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001808 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1809 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1810 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001811 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001812 if (!HasDef && !HasUse)
1813 continue;
1814
Evan Cheng018f9b02007-12-05 03:22:34 +00001815 AllCanFold &= CanFold;
1816
Evan Cheng81a03822007-11-17 00:40:40 +00001817 // Update weight of spill interval.
1818 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001819 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001820 // The spill weight is now infinity as it cannot be spilled again.
1821 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001822 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001823 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001824
1825 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001826 if (HasDef) {
1827 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001828 bool HasKill = false;
1829 if (!HasUse)
1830 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1831 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001832 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001833 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001834 if (VNI)
1835 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1836 }
Owen Anderson28998312008-08-13 22:28:50 +00001837 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001838 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001839 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001840 if (SII == SpillIdxes.end()) {
1841 std::vector<SRInfo> S;
1842 S.push_back(SRInfo(index, NewVReg, true));
1843 SpillIdxes.insert(std::make_pair(MBBId, S));
1844 } else if (SII->second.back().vreg != NewVReg) {
1845 SII->second.push_back(SRInfo(index, NewVReg, true));
1846 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001847 // If there is an earlier def and this is a two-address
1848 // instruction, then it's not possible to fold the store (which
1849 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001850 SRInfo &Info = SII->second.back();
1851 Info.index = index;
1852 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001853 }
1854 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001855 } else if (SII != SpillIdxes.end() &&
1856 SII->second.back().vreg == NewVReg &&
1857 (int)index > SII->second.back().index) {
1858 // There is an earlier def that's not killed (must be two-address).
1859 // The spill is no longer needed.
1860 SII->second.pop_back();
1861 if (SII->second.empty()) {
1862 SpillIdxes.erase(MBBId);
1863 SpillMBBs.reset(MBBId);
1864 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001865 }
1866 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001867 }
1868
1869 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001870 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001871 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001872 if (SII != SpillIdxes.end() &&
1873 SII->second.back().vreg == NewVReg &&
1874 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001875 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001876 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001877 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001878 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001879 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001880 // If we are splitting live intervals, only fold if it's the first
1881 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001882 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001883 else if (IsNew) {
1884 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001885 if (RII == RestoreIdxes.end()) {
1886 std::vector<SRInfo> Infos;
1887 Infos.push_back(SRInfo(index, NewVReg, true));
1888 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1889 } else {
1890 RII->second.push_back(SRInfo(index, NewVReg, true));
1891 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001892 RestoreMBBs.set(MBBId);
1893 }
1894 }
1895
1896 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001897 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001898 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001899 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001900
1901 if (NewVReg && TrySplit && AllCanFold) {
1902 // If all of its def / use can be folded, give it a low spill weight.
1903 LiveInterval &nI = getOrCreateInterval(NewVReg);
1904 nI.weight /= 10.0F;
1905 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001906}
1907
Evan Cheng1953d0c2007-11-29 10:12:14 +00001908bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1909 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001910 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001911 if (!RestoreMBBs[Id])
1912 return false;
1913 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1914 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1915 if (Restores[i].index == index &&
1916 Restores[i].vreg == vr &&
1917 Restores[i].canFold)
1918 return true;
1919 return false;
1920}
1921
1922void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1923 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001924 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001925 if (!RestoreMBBs[Id])
1926 return;
1927 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1928 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1929 if (Restores[i].index == index && Restores[i].vreg)
1930 Restores[i].index = -1;
1931}
Evan Cheng81a03822007-11-17 00:40:40 +00001932
Evan Cheng4cce6b42008-04-11 17:53:36 +00001933/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1934/// spilled and create empty intervals for their uses.
1935void
1936LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1937 const TargetRegisterClass* rc,
1938 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001939 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1940 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001941 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001942 MachineInstr *MI = &*ri;
1943 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001944 if (O.isDef()) {
1945 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1946 "Register def was not rewritten?");
1947 RemoveMachineInstrFromMaps(MI);
1948 vrm.RemoveMachineInstrFromMaps(MI);
1949 MI->eraseFromParent();
1950 } else {
1951 // This must be an use of an implicit_def so it's not part of the live
1952 // interval. Create a new empty live interval for it.
1953 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1954 unsigned NewVReg = mri_->createVirtualRegister(rc);
1955 vrm.grow();
1956 vrm.setIsImplicitlyDefined(NewVReg);
1957 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1958 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1959 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001960 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001961 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001962 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001963 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001964 }
1965 }
Evan Cheng419852c2008-04-03 16:39:43 +00001966 }
1967}
1968
Evan Chengf2fbca62007-11-12 06:35:08 +00001969std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001970addIntervalsForSpillsFast(const LiveInterval &li,
1971 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001972 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001973 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001974
1975 std::vector<LiveInterval*> added;
1976
1977 assert(li.weight != HUGE_VALF &&
1978 "attempt to spill already spilled interval!");
1979
1980 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1981 DEBUG(li.dump());
1982 DOUT << '\n';
1983
1984 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1985
Owen Andersona41e47a2008-08-19 22:12:11 +00001986 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1987 while (RI != mri_->reg_end()) {
1988 MachineInstr* MI = &*RI;
1989
1990 SmallVector<unsigned, 2> Indices;
1991 bool HasUse = false;
1992 bool HasDef = false;
1993
1994 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1995 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001996 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001997
1998 HasUse |= MI->getOperand(i).isUse();
1999 HasDef |= MI->getOperand(i).isDef();
2000
2001 Indices.push_back(i);
2002 }
2003
2004 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2005 Indices, true, slot, li.reg)) {
2006 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002007 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002008 vrm.assignVirt2StackSlot(NewVReg, slot);
2009
Owen Andersona41e47a2008-08-19 22:12:11 +00002010 // create a new register for this spill
2011 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002012
Owen Andersona41e47a2008-08-19 22:12:11 +00002013 // the spill weight is now infinity as it
2014 // cannot be spilled again
2015 nI.weight = HUGE_VALF;
2016
2017 // Rewrite register operands to use the new vreg.
2018 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2019 E = Indices.end(); I != E; ++I) {
2020 MI->getOperand(*I).setReg(NewVReg);
2021
2022 if (MI->getOperand(*I).isUse())
2023 MI->getOperand(*I).setIsKill(true);
2024 }
2025
2026 // Fill in the new live interval.
2027 unsigned index = getInstructionIndex(MI);
2028 if (HasUse) {
2029 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002030 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002031 DOUT << " +" << LR;
2032 nI.addRange(LR);
2033 vrm.addRestorePoint(NewVReg, MI);
2034 }
2035 if (HasDef) {
2036 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002037 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002038 DOUT << " +" << LR;
2039 nI.addRange(LR);
2040 vrm.addSpillPoint(NewVReg, true, MI);
2041 }
2042
Owen Anderson17197312008-08-18 23:41:04 +00002043 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002044
Owen Andersona41e47a2008-08-19 22:12:11 +00002045 DOUT << "\t\t\t\tadded new interval: ";
2046 DEBUG(nI.dump());
2047 DOUT << '\n';
Owen Andersona41e47a2008-08-19 22:12:11 +00002048 }
Owen Anderson9a032932008-08-18 21:20:32 +00002049
Owen Anderson9a032932008-08-18 21:20:32 +00002050
Owen Andersona41e47a2008-08-19 22:12:11 +00002051 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002052 }
Owen Andersond6664312008-08-18 18:05:32 +00002053
2054 return added;
2055}
2056
2057std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002058addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002059 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002060 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002061
2062 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002063 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002064
Evan Chengf2fbca62007-11-12 06:35:08 +00002065 assert(li.weight != HUGE_VALF &&
2066 "attempt to spill already spilled interval!");
2067
2068 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00002069 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00002070 DOUT << '\n';
2071
Evan Cheng72eeb942008-12-05 17:00:16 +00002072 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002073 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002074 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002075 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002076 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2077 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002078 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002079 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002080
2081 unsigned NumValNums = li.getNumValNums();
2082 SmallVector<MachineInstr*, 4> ReMatDefs;
2083 ReMatDefs.resize(NumValNums, NULL);
2084 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2085 ReMatOrigDefs.resize(NumValNums, NULL);
2086 SmallVector<int, 4> ReMatIds;
2087 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2088 BitVector ReMatDelete(NumValNums);
2089 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2090
Evan Cheng81a03822007-11-17 00:40:40 +00002091 // Spilling a split live interval. It cannot be split any further. Also,
2092 // it's also guaranteed to be a single val# / range interval.
2093 if (vrm.getPreSplitReg(li.reg)) {
2094 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002095 // Unset the split kill marker on the last use.
2096 unsigned KillIdx = vrm.getKillPoint(li.reg);
2097 if (KillIdx) {
2098 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2099 assert(KillMI && "Last use disappeared?");
2100 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2101 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002102 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002103 }
Evan Chengadf85902007-12-05 09:51:10 +00002104 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002105 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2106 Slot = vrm.getStackSlot(li.reg);
2107 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2108 MachineInstr *ReMatDefMI = DefIsReMat ?
2109 vrm.getReMaterializedMI(li.reg) : NULL;
2110 int LdSlot = 0;
2111 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2112 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002113 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002114 bool IsFirstRange = true;
2115 for (LiveInterval::Ranges::const_iterator
2116 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2117 // If this is a split live interval with multiple ranges, it means there
2118 // are two-address instructions that re-defined the value. Only the
2119 // first def can be rematerialized!
2120 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002121 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002122 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2123 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002124 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002125 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002126 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002127 } else {
2128 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2129 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002130 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002131 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002132 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002133 }
2134 IsFirstRange = false;
2135 }
Evan Cheng419852c2008-04-03 16:39:43 +00002136
Evan Cheng4cce6b42008-04-11 17:53:36 +00002137 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002138 return NewLIs;
2139 }
2140
2141 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002142 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2143 TrySplit = false;
2144 if (TrySplit)
2145 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002146 bool NeedStackSlot = false;
2147 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2148 i != e; ++i) {
2149 const VNInfo *VNI = *i;
2150 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002151 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002152 continue; // Dead val#.
2153 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002154 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2155 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002156 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002157 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002158 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002159 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002160 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002161 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2162 ClonedMIs.push_back(Clone);
2163 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002164
2165 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002166 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002167 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002168 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002169 CanDelete = false;
2170 // Need a stack slot if there is any live range where uses cannot be
2171 // rematerialized.
2172 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002173 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002174 if (CanDelete)
2175 ReMatDelete.set(VN);
2176 } else {
2177 // Need a stack slot if there is any live range where uses cannot be
2178 // rematerialized.
2179 NeedStackSlot = true;
2180 }
2181 }
2182
2183 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002184 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2185 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2186 Slot = vrm.assignVirt2StackSlot(li.reg);
2187
2188 // This case only occurs when the prealloc splitter has already assigned
2189 // a stack slot to this vreg.
2190 else
2191 Slot = vrm.getStackSlot(li.reg);
2192 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002193
2194 // Create new intervals and rewrite defs and uses.
2195 for (LiveInterval::Ranges::const_iterator
2196 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002197 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2198 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2199 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002200 bool CanDelete = ReMatDelete[I->valno->id];
2201 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002202 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002203 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002204 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002205 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002206 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002207 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002208 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002209 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002210 }
2211
Evan Cheng0cbb1162007-11-29 01:06:25 +00002212 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002213 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002214 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002215 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002216 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002217
Evan Chengb50bb8c2007-12-05 08:16:32 +00002218 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002219 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002220 if (NeedStackSlot) {
2221 int Id = SpillMBBs.find_first();
2222 while (Id != -1) {
2223 std::vector<SRInfo> &spills = SpillIdxes[Id];
2224 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2225 int index = spills[i].index;
2226 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002227 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002228 bool isReMat = vrm.isReMaterialized(VReg);
2229 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002230 bool CanFold = false;
2231 bool FoundUse = false;
2232 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002233 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002234 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002235 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2236 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002237 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002238 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002239
2240 Ops.push_back(j);
2241 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002242 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002243 if (isReMat ||
2244 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2245 RestoreMBBs, RestoreIdxes))) {
2246 // MI has two-address uses of the same register. If the use
2247 // isn't the first and only use in the BB, then we can't fold
2248 // it. FIXME: Move this to rewriteInstructionsForSpills.
2249 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002250 break;
2251 }
Evan Chengaee4af62007-12-02 08:30:39 +00002252 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002253 }
2254 }
2255 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002256 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002257 if (CanFold && !Ops.empty()) {
2258 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002259 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002260 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002261 // Also folded uses, do not issue a load.
2262 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002263 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2264 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002265 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002266 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002267 }
2268
Evan Cheng7e073ba2008-04-09 20:57:25 +00002269 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002270 if (!Folded) {
2271 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2272 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002273 if (!MI->registerDefIsDead(nI.reg))
2274 // No need to spill a dead def.
2275 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002276 if (isKill)
2277 AddedKill.insert(&nI);
2278 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002279 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002280 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002281 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002282 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002283
Evan Cheng1953d0c2007-11-29 10:12:14 +00002284 int Id = RestoreMBBs.find_first();
2285 while (Id != -1) {
2286 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2287 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2288 int index = restores[i].index;
2289 if (index == -1)
2290 continue;
2291 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002292 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002293 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002294 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002295 bool CanFold = false;
2296 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002297 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002298 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002299 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2300 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002301 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002302 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002303
Evan Cheng0cbb1162007-11-29 01:06:25 +00002304 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002305 // If this restore were to be folded, it would have been folded
2306 // already.
2307 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002308 break;
2309 }
Evan Chengaee4af62007-12-02 08:30:39 +00002310 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002311 }
2312 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002313
2314 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002315 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002316 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002317 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002318 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2319 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002320 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2321 int LdSlot = 0;
2322 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2323 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002324 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002325 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2326 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002327 if (!Folded) {
2328 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2329 if (ImpUse) {
2330 // Re-matting an instruction with virtual register use. Add the
2331 // register as an implicit use on the use MI and update the register
2332 // interval's spill weight to HUGE_VALF to prevent it from being
2333 // spilled.
2334 LiveInterval &ImpLi = getInterval(ImpUse);
2335 ImpLi.weight = HUGE_VALF;
2336 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2337 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002338 }
Evan Chengaee4af62007-12-02 08:30:39 +00002339 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002340 }
2341 // If folding is not possible / failed, then tell the spiller to issue a
2342 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002343 if (Folded)
2344 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002345 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002346 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002347 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002348 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002349 }
2350
Evan Chengb50bb8c2007-12-05 08:16:32 +00002351 // Finalize intervals: add kills, finalize spill weights, and filter out
2352 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002353 std::vector<LiveInterval*> RetNewLIs;
2354 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2355 LiveInterval *LI = NewLIs[i];
2356 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002357 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002358 if (!AddedKill.count(LI)) {
2359 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002360 unsigned LastUseIdx = getBaseIndex(LR->end);
2361 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002362 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002363 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002364 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002365 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002366 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002367 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002368 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002369 RetNewLIs.push_back(LI);
2370 }
2371 }
Evan Cheng81a03822007-11-17 00:40:40 +00002372
Evan Cheng4cce6b42008-04-11 17:53:36 +00002373 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002374 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002375}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002376
2377/// hasAllocatableSuperReg - Return true if the specified physical register has
2378/// any super register that's allocatable.
2379bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2380 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2381 if (allocatableRegs_[*AS] && hasInterval(*AS))
2382 return true;
2383 return false;
2384}
2385
2386/// getRepresentativeReg - Find the largest super register of the specified
2387/// physical register.
2388unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2389 // Find the largest super-register that is allocatable.
2390 unsigned BestReg = Reg;
2391 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2392 unsigned SuperReg = *AS;
2393 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2394 BestReg = SuperReg;
2395 break;
2396 }
2397 }
2398 return BestReg;
2399}
2400
2401/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2402/// specified interval that conflicts with the specified physical register.
2403unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2404 unsigned PhysReg) const {
2405 unsigned NumConflicts = 0;
2406 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2407 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2408 E = mri_->reg_end(); I != E; ++I) {
2409 MachineOperand &O = I.getOperand();
2410 MachineInstr *MI = O.getParent();
2411 unsigned Index = getInstructionIndex(MI);
2412 if (pli.liveAt(Index))
2413 ++NumConflicts;
2414 }
2415 return NumConflicts;
2416}
2417
2418/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002419/// around all defs and uses of the specified interval. Return true if it
2420/// was able to cut its interval.
2421bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002422 unsigned PhysReg, VirtRegMap &vrm) {
2423 unsigned SpillReg = getRepresentativeReg(PhysReg);
2424
2425 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2426 // If there are registers which alias PhysReg, but which are not a
2427 // sub-register of the chosen representative super register. Assert
2428 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002429 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002430 tri_->isSuperRegister(*AS, SpillReg));
2431
Evan Cheng2824a652009-03-23 18:24:37 +00002432 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002433 LiveInterval &pli = getInterval(SpillReg);
2434 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2435 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2436 E = mri_->reg_end(); I != E; ++I) {
2437 MachineOperand &O = I.getOperand();
2438 MachineInstr *MI = O.getParent();
2439 if (SeenMIs.count(MI))
2440 continue;
2441 SeenMIs.insert(MI);
2442 unsigned Index = getInstructionIndex(MI);
2443 if (pli.liveAt(Index)) {
2444 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002445 unsigned StartIdx = getLoadIndex(Index);
2446 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002447 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002448 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002449 Cut = true;
2450 } else {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002451 cerr << "Ran out of registers during register allocation!\n";
2452 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
2453 cerr << "Please check your inline asm statement for invalid "
2454 << "constraints:\n";
2455 MI->print(cerr.stream(), tm_);
2456 }
2457 exit(1);
2458 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002459 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2460 if (!hasInterval(*AS))
2461 continue;
2462 LiveInterval &spli = getInterval(*AS);
2463 if (spli.liveAt(Index))
2464 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2465 }
2466 }
2467 }
Evan Cheng2824a652009-03-23 18:24:37 +00002468 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002469}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002470
2471LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002472 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002473 LiveInterval& Interval = getOrCreateInterval(reg);
2474 VNInfo* VN = Interval.getNextValue(
2475 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002476 startInst, true, getVNInfoAllocator());
2477 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002478 VN->kills.push_back(
2479 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002480 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2481 getMBBEndIdx(startInst->getParent()) + 1, VN);
2482 Interval.addRange(LR);
2483
2484 return LR;
2485}