blob: b13f49427355f284dce2eb9e16977893b9cb03a9 [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.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000255 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000256 i2miMap_.push_back(0);
257
258 MIIndex += InstrSlots::NUM;
259 }
260
Chris Lattner428b92e2006-09-15 03:57:23 +0000261 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000263 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000264 i2miMap_.push_back(I);
265 MIIndex += InstrSlots::NUM;
Owen Andersona1566f22008-07-22 22:46:49 +0000266 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000267
Evan Cheng4ed43292008-10-18 05:21:37 +0000268 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000269 unsigned Slots = I->getDesc().getNumDefs();
270 if (Slots == 0)
271 Slots = 1;
272 MIIndex += InstrSlots::NUM * Slots;
273 while (Slots--)
274 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000275 }
Lang Hamesffd13262009-07-09 03:57:02 +0000276
277 if (MBB->getFirstTerminator() == MBB->end()) {
278 // Leave a gap for before terminators, this is where we will point
279 // PHI kills.
280 bool inserted =
281 terminatorGaps.insert(std::make_pair(&*MBB, MIIndex)).second;
282 assert(inserted &&
283 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000284 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000285 i2miMap_.push_back(0);
286
287 MIIndex += InstrSlots::NUM;
288 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000289
Owen Anderson1fbb4542008-06-16 16:58:24 +0000290 // Set the MBB2IdxMap entry for this MBB.
291 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
292 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000293 }
Lang Hamesffd13262009-07-09 03:57:02 +0000294
Evan Cheng4ca980e2007-10-17 02:10:22 +0000295 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000296
297 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000298 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000299 for (LiveInterval::iterator LI = OI->second->begin(),
300 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000301
Owen Anderson7eec0c22008-05-29 23:01:22 +0000302 // Remap the start index of the live range to the corresponding new
303 // number, or our best guess at what it _should_ correspond to if the
304 // original instruction has been erased. This is either the following
305 // instruction or its predecessor.
Owen Anderson7fbad272008-07-23 21:37:49 +0000306 unsigned index = LI->start / InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000307 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson0a7615a2008-07-25 23:06:59 +0000308 if (offset == InstrSlots::LOAD) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000309 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000310 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000311 // Take the pair containing the index
312 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000313 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000314
Owen Anderson7fbad272008-07-23 21:37:49 +0000315 LI->start = getMBBStartIdx(J->second);
316 } else {
317 LI->start = mi2iMap_[OldI2MI[index]] + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000318 }
319
320 // Remap the ending index in the same way that we remapped the start,
321 // except for the final step where we always map to the immediately
322 // following instruction.
Owen Andersond7dcbec2008-07-25 19:50:48 +0000323 index = (LI->end - 1) / InstrSlots::NUM;
Owen Anderson7fbad272008-07-23 21:37:49 +0000324 offset = LI->end % InstrSlots::NUM;
Owen Anderson9382b932008-07-30 00:22:56 +0000325 if (offset == InstrSlots::LOAD) {
326 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000327 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000328 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000329 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000330
Owen Anderson9382b932008-07-30 00:22:56 +0000331 LI->end = getMBBEndIdx(I->second) + 1;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000332 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000333 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000334 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
335
336 if (index != OldI2MI.size())
337 LI->end = mi2iMap_[OldI2MI[index]] + (idx == index ? offset : 0);
338 else
339 LI->end = InstrSlots::NUM * i2miMap_.size();
Owen Anderson4b5b2092008-05-29 18:15:49 +0000340 }
Owen Anderson788d0412008-08-06 18:35:45 +0000341 }
342
Owen Anderson03857b22008-08-13 21:49:13 +0000343 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
344 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000345 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000346
Owen Anderson7eec0c22008-05-29 23:01:22 +0000347 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000348 // start indices above. VN's with special sentinel defs
349 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000350 if (vni->isDefAccurate() && !vni->isUnused()) {
Owen Anderson788d0412008-08-06 18:35:45 +0000351 unsigned index = vni->def / InstrSlots::NUM;
352 unsigned offset = vni->def % InstrSlots::NUM;
Owen Anderson91292392008-07-30 17:42:47 +0000353 if (offset == InstrSlots::LOAD) {
354 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000355 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000356 // Take the pair containing the index
357 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000358 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000359
Owen Anderson91292392008-07-30 17:42:47 +0000360 vni->def = getMBBStartIdx(J->second);
361 } else {
362 vni->def = mi2iMap_[OldI2MI[index]] + offset;
363 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000364 }
Owen Anderson745825f42008-05-28 22:40:08 +0000365
Owen Anderson7eec0c22008-05-29 23:01:22 +0000366 // Remap the VNInfo kill indices, which works the same as
367 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000368 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hamesffd13262009-07-09 03:57:02 +0000369 unsigned killIdx = vni->kills[i].killIdx;
370
371 unsigned index = (killIdx - 1) / InstrSlots::NUM;
372 unsigned offset = killIdx % InstrSlots::NUM;
373
Owen Anderson309c6162008-09-30 22:51:54 +0000374 if (offset == InstrSlots::LOAD) {
Lang Hamesffd13262009-07-09 03:57:02 +0000375 assert("Value killed at a load slot.");
376 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000377 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000378 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000379
Lang Hamesffd13262009-07-09 03:57:02 +0000380 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000381 } else {
Lang Hamesffd13262009-07-09 03:57:02 +0000382 if (vni->kills[i].isPHIKill) {
383 std::vector<IdxMBBPair>::const_iterator I =
384 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
385 --I;
386 vni->kills[i].killIdx = terminatorGaps[I->second];
387 } else {
388 assert(OldI2MI[index] != 0 &&
389 "Kill refers to instruction not present in index maps.");
390 vni->kills[i].killIdx = mi2iMap_[OldI2MI[index]] + offset;
391 }
392
393 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000394 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000395 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
396
397 if (index != OldI2MI.size())
398 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
399 (idx == index ? offset : 0);
400 else
401 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000402 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000403 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000404 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000405 }
Owen Anderson788d0412008-08-06 18:35:45 +0000406 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000407}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000408
Lang Hamesf41538d2009-06-02 16:53:25 +0000409void LiveIntervals::scaleNumbering(int factor) {
410 // Need to
411 // * scale MBB begin and end points
412 // * scale all ranges.
413 // * Update VNI structures.
414 // * Scale instruction numberings
415
416 // Scale the MBB indices.
417 Idx2MBBMap.clear();
418 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
419 MBB != MBBE; ++MBB) {
420 std::pair<unsigned, unsigned> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
421 mbbIndices.first = InstrSlots::scale(mbbIndices.first, factor);
422 mbbIndices.second = InstrSlots::scale(mbbIndices.second, factor);
423 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
424 }
425 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
426
Lang Hamesffd13262009-07-09 03:57:02 +0000427 // Scale terminator gaps.
428 for (DenseMap<MachineBasicBlock*, unsigned>::iterator
429 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
430 TGI != TGE; ++TGI) {
431 terminatorGaps[TGI->first] = InstrSlots::scale(TGI->second, factor);
432 }
433
Lang Hamesf41538d2009-06-02 16:53:25 +0000434 // Scale the intervals.
435 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
436 LI->second->scaleNumbering(factor);
437 }
438
439 // Scale MachineInstrs.
440 Mi2IndexMap oldmi2iMap = mi2iMap_;
441 unsigned highestSlot = 0;
442 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
443 MI != ME; ++MI) {
444 unsigned newSlot = InstrSlots::scale(MI->second, factor);
445 mi2iMap_[MI->first] = newSlot;
446 highestSlot = std::max(highestSlot, newSlot);
447 }
448
449 i2miMap_.clear();
450 i2miMap_.resize(highestSlot + 1);
451 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
452 MI != ME; ++MI) {
453 i2miMap_[MI->second] = MI->first;
454 }
455
456}
457
458
Owen Anderson80b3ce62008-05-28 20:54:50 +0000459/// runOnMachineFunction - Register allocate the whole function
460///
461bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
462 mf_ = &fn;
463 mri_ = &mf_->getRegInfo();
464 tm_ = &fn.getTarget();
465 tri_ = tm_->getRegisterInfo();
466 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000467 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000468 lv_ = &getAnalysis<LiveVariables>();
469 allocatableRegs_ = tri_->getAllocatableSet(fn);
470
Evan Cheng2578ba22009-07-01 01:59:31 +0000471 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000472 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000473 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000474
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000475 numIntervals += getNumIntervals();
476
Chris Lattner70ca3582004-09-30 15:59:17 +0000477 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000478 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000479}
480
Chris Lattner70ca3582004-09-30 15:59:17 +0000481/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000482void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000483 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000484 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Owen Anderson03857b22008-08-13 21:49:13 +0000485 I->second->print(O, tri_);
Evan Cheng3f32d652008-06-04 09:18:41 +0000486 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000487 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000488
489 O << "********** MACHINEINSTRS **********\n";
490 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
491 mbbi != mbbe; ++mbbi) {
492 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
493 for (MachineBasicBlock::iterator mii = mbbi->begin(),
494 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000495 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000496 }
497 }
498}
499
Evan Chengc92da382007-11-03 07:20:12 +0000500/// conflictsWithPhysRegDef - Returns true if the specified register
501/// is defined during the duration of the specified interval.
502bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
503 VirtRegMap &vrm, unsigned reg) {
504 for (LiveInterval::Ranges::const_iterator
505 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
506 for (unsigned index = getBaseIndex(I->start),
507 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
508 index += InstrSlots::NUM) {
509 // skip deleted instructions
510 while (index != end && !getInstructionFromIndex(index))
511 index += InstrSlots::NUM;
512 if (index == end) break;
513
514 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000515 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
516 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000517 if (SrcReg == li.reg || DstReg == li.reg)
518 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000519 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
520 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000521 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000522 continue;
523 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000524 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000525 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000526 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000527 if (!vrm.hasPhys(PhysReg))
528 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000529 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000530 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000531 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000532 return true;
533 }
534 }
535 }
536
537 return false;
538}
539
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000540/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
541/// it can check use as well.
542bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
543 unsigned Reg, bool CheckUse,
544 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
545 for (LiveInterval::Ranges::const_iterator
546 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
547 for (unsigned index = getBaseIndex(I->start),
548 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
549 index += InstrSlots::NUM) {
550 // Skip deleted instructions.
551 MachineInstr *MI = 0;
552 while (index != end) {
553 MI = getInstructionFromIndex(index);
554 if (MI)
555 break;
556 index += InstrSlots::NUM;
557 }
558 if (index == end) break;
559
560 if (JoinedCopies.count(MI))
561 continue;
562 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
563 MachineOperand& MO = MI->getOperand(i);
564 if (!MO.isReg())
565 continue;
566 if (MO.isUse() && !CheckUse)
567 continue;
568 unsigned PhysReg = MO.getReg();
569 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
570 continue;
571 if (tri_->isSubRegister(Reg, PhysReg))
572 return true;
573 }
574 }
575 }
576
577 return false;
578}
579
580
Evan Cheng549f27d32007-08-13 23:45:17 +0000581void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000582 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000583 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000584 else
585 cerr << "%reg" << reg;
586}
587
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000588void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000589 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000590 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000591 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000592 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000593 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000595
Evan Cheng419852c2008-04-03 16:39:43 +0000596 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
597 DOUT << "is a implicit_def\n";
598 return;
599 }
600
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000601 // Virtual registers may be defined multiple times (due to phi
602 // elimination and 2-addr elimination). Much of what we do only has to be
603 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000604 // time we see a vreg.
605 if (interval.empty()) {
606 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000607 unsigned defIndex = getDefIndex(MIIdx);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000608 // Earlyclobbers move back one.
609 if (MO.isEarlyClobber())
610 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000611 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000612 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000613 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000614 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000615 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000616 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000617 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000618 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000619 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000620 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000621
622 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000623
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000624 // Loop over all of the blocks that the vreg is defined in. There are
625 // two cases we have to handle here. The most common case is a vreg
626 // whose lifetime is contained within a basic block. In this case there
627 // will be a single kill, in MBB, which comes after the definition.
628 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
629 // FIXME: what about dead vars?
630 unsigned killIdx;
631 if (vi.Kills[0] != mi)
632 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
633 else
634 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000635
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000636 // If the kill happens after the definition, we have an intra-block
637 // live range.
638 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000639 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000641 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000643 DOUT << " +" << LR << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000644 interval.addKill(ValNo, killIdx, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000645 return;
646 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000647 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000648
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000649 // The other case we handle is when a virtual register lives to the end
650 // of the defining block, potentially live across some blocks, then is
651 // live into some number of blocks, but gets killed. Start by adding a
652 // range that goes from this definition to the end of the defining block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000653 LiveRange NewLR(defIndex, getMBBEndIdx(mbb)+1, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000654 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000655 interval.addRange(NewLR);
656
657 // Iterate over all of the blocks that the variable is completely
658 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
659 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000660 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
661 E = vi.AliveBlocks.end(); I != E; ++I) {
662 LiveRange LR(getMBBStartIdx(*I),
663 getMBBEndIdx(*I)+1, // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000664 ValNo);
665 interval.addRange(LR);
666 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000667 }
668
669 // Finally, this virtual register is live from the start of any killing
670 // block to the 'use' slot of the killing instruction.
671 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
672 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000673 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000674 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000675 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000676 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000677 interval.addKill(ValNo, killIdx, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000678 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 }
680
681 } else {
682 // If this is the second time we see a virtual register definition, it
683 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000684 // the result of two address elimination, then the vreg is one of the
685 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000686 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 // If this is a two-address definition, then we have already processed
688 // the live range. The only problem is that we didn't realize there
689 // are actually two values in the live interval. Because of this we
690 // need to take the LiveRegion that defines this register and split it
691 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000692 assert(interval.containsOneValue());
693 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000694 unsigned RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000695 if (MO.isEarlyClobber())
696 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000697
Evan Cheng4f8ff162007-08-11 00:59:19 +0000698 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000699 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000700
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000702 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000704
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000705 // Two-address vregs should always only be redefined once. This means
706 // that at this point, there should be exactly one value number in it.
707 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
708
Chris Lattner91725b72006-08-31 05:54:43 +0000709 // The new value number (#1) is defined by the instruction we claimed
710 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000711 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
Lang Hames857c4e02009-06-17 21:01:20 +0000712 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000713 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000714 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
715
Chris Lattner91725b72006-08-31 05:54:43 +0000716 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000717 OldValNo->def = RedefIndex;
718 OldValNo->copy = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000719 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000720 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000721
722 // Add the new live interval which replaces the range for the input copy.
723 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000724 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000725 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000726 interval.addKill(ValNo, RedefIndex, false);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000727
728 // If this redefinition is dead, we need to add a dummy unit live
729 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000730 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000731 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000732
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000733 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000734 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000735
736 } else {
737 // Otherwise, this must be because of phi elimination. If this is the
738 // first redefinition of the vreg that we have seen, go back and change
739 // the live range in the PHI block to be a different value number.
740 if (interval.containsOneValue()) {
741 assert(vi.Kills.size() == 1 &&
742 "PHI elimination vreg should have one kill, the PHI itself!");
743
744 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000745 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000746 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000747 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000748 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000749 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000750 interval.print(DOUT, tri_); DOUT << "\n";
Lang Hamesffd13262009-07-09 03:57:02 +0000751 interval.removeRange(Start, End);
752 assert(interval.ranges.size() == 1 &&
753 "newly discovered PHI interval has >1 ranges.");
754 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endNumber());
755 interval.addKill(VNI, terminatorGaps[killMBB], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000756 VNI->setHasPHIKill(true);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000757 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000758
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000759 // Replace the interval with one of a NEW value number. Note that this
760 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000761 LiveRange LR(Start, End,
762 interval.getNextValue(mbb->getNumber(), 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000763 LR.valno->setIsPHIDef(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000764 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000765 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000766 interval.addKill(LR.valno, End, false);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000767 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000768 }
769
770 // In the case of PHI elimination, each variable definition is only
771 // live until the end of the block. We've already taken care of the
772 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000773 unsigned defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000774 if (MO.isEarlyClobber())
775 defIndex = getUseIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000776
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000777 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000778 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000779 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000780 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000781 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000782 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000783 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000784 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000785 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000786
Owen Anderson7fbad272008-07-23 21:37:49 +0000787 unsigned killIndex = getMBBEndIdx(mbb) + 1;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000788 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000789 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000790 interval.addKill(ValNo, terminatorGaps[mbb], true);
Lang Hames857c4e02009-06-17 21:01:20 +0000791 ValNo->setHasPHIKill(true);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000792 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000793 }
794 }
795
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000796 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000797}
798
Chris Lattnerf35fef72004-07-23 21:24:19 +0000799void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000800 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000801 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000802 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000803 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000804 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000805 // A physical register cannot be live across basic block, so its
806 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000807 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000808
Chris Lattner6b128bd2006-09-03 08:07:11 +0000809 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000810 unsigned start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000811 // Earlyclobbers move back one.
812 if (MO.isEarlyClobber())
813 start = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000814 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000815
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000816 // If it is not used after definition, it is considered dead at
817 // the instruction defining it. Hence its interval is:
818 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000819 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000820 DOUT << " dead";
Dale Johannesen86b49f82008-09-24 01:07:17 +0000821 end = start + 1;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000822 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000823 }
824
825 // If it is not dead on definition, it must be killed by a
826 // subsequent instruction. Hence its interval is:
827 // [defSlot(def), useSlot(kill)+1)
Owen Anderson7fbad272008-07-23 21:37:49 +0000828 baseIndex += InstrSlots::NUM;
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000829 while (++mi != MBB->end()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000830 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
831 getInstructionFromIndex(baseIndex) == 0)
832 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000833 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000834 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000835 end = getUseIndex(baseIndex) + 1;
836 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000837 } else {
838 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
839 if (DefIdx != -1) {
840 if (mi->isRegTiedToUseOperand(DefIdx)) {
841 // Two-address instruction.
842 end = getDefIndex(baseIndex);
843 if (mi->getOperand(DefIdx).isEarlyClobber())
844 end = getUseIndex(baseIndex);
845 } else {
846 // Another instruction redefines the register before it is ever read.
847 // Then the register is essentially dead at the instruction that defines
848 // it. Hence its interval is:
849 // [defSlot(def), defSlot(def)+1)
850 DOUT << " dead";
851 end = start + 1;
852 }
853 goto exit;
854 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000855 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000856
857 baseIndex += InstrSlots::NUM;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000858 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000859
860 // The only case we should have a dead physreg here without a killing or
861 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000862 // and never used. Another possible case is the implicit use of the
863 // physical register has been deleted by two-address pass.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000864 end = start + 1;
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000865
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000866exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000867 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000868
Evan Cheng24a3cc42007-04-25 07:30:23 +0000869 // Already exists? Extend old live interval.
870 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000871 bool Extend = OldLR != interval.end();
872 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000873 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000874 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000875 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000876 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000877 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000878 interval.addKill(LR.valno, end, false);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000879 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000880}
881
Chris Lattnerf35fef72004-07-23 21:24:19 +0000882void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
883 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000884 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000885 MachineOperand& MO,
886 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000887 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000888 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000889 getOrCreateInterval(MO.getReg()));
890 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000891 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000892 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000893 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000894 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000895 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000896 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000897 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000898 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000899 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000900 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000901 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000902 // If MI also modifies the sub-register explicitly, avoid processing it
903 // more than once. Do not pass in TRI here so it checks for exact match.
904 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000905 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000906 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000907 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000908}
909
Evan Chengb371f452007-02-19 21:49:54 +0000910void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000911 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000912 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000913 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
914
915 // Look for kills, if it reaches a def before it's killed, then it shouldn't
916 // be considered a livein.
917 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000918 unsigned baseIndex = MIIdx;
919 unsigned start = baseIndex;
Owen Anderson99500ae2008-09-15 22:00:38 +0000920 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
921 getInstructionFromIndex(baseIndex) == 0)
922 baseIndex += InstrSlots::NUM;
923 unsigned end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000924 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000925
Evan Chengb371f452007-02-19 21:49:54 +0000926 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000927 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000928 DOUT << " killed";
929 end = getUseIndex(baseIndex) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000930 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000931 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000932 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000933 // Another instruction redefines the register before it is ever read.
934 // Then the register is essentially dead at the instruction that defines
935 // it. Hence its interval is:
936 // [defSlot(def), defSlot(def)+1)
937 DOUT << " dead";
938 end = getDefIndex(start) + 1;
Evan Cheng0076c612009-03-05 03:34:26 +0000939 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000940 break;
Evan Chengb371f452007-02-19 21:49:54 +0000941 }
942
943 baseIndex += InstrSlots::NUM;
944 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000945 if (mi != MBB->end()) {
946 while (baseIndex / InstrSlots::NUM < i2miMap_.size() &&
947 getInstructionFromIndex(baseIndex) == 0)
948 baseIndex += InstrSlots::NUM;
949 }
Evan Chengb371f452007-02-19 21:49:54 +0000950 }
951
Evan Cheng75611fb2007-06-27 01:16:36 +0000952 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000953 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000954 if (isAlias) {
955 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000956 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000957 } else {
958 DOUT << " live through";
959 end = baseIndex;
960 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000961 }
962
Lang Hames10382fb2009-06-19 02:17:53 +0000963 VNInfo *vni =
964 interval.getNextValue(MBB->getNumber(), 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000965 vni->setIsPHIDef(true);
966 LiveRange LR(start, end, vni);
967
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000968 interval.addRange(LR);
Lang Hamesffd13262009-07-09 03:57:02 +0000969 interval.addKill(LR.valno, end, false);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000970 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000971}
972
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000973/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000974/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000975/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000976/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000977void LiveIntervals::computeIntervals() {
Dale Johannesen91aac102008-09-17 21:13:11 +0000978
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000979 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
980 << "********** Function: "
981 << ((Value*)mf_->getFunction())->getName() << '\n';
Owen Anderson7fbad272008-07-23 21:37:49 +0000982
Chris Lattner428b92e2006-09-15 03:57:23 +0000983 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
984 MBBI != E; ++MBBI) {
985 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000986 // Track the index of the current machine instr.
987 unsigned MIIndex = getMBBStartIdx(MBB);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000988 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000989
Chris Lattner428b92e2006-09-15 03:57:23 +0000990 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000991
Dan Gohmancb406c22007-10-03 19:26:29 +0000992 // Create intervals for live-ins to this BB first.
993 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
994 LE = MBB->livein_end(); LI != LE; ++LI) {
995 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
996 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000997 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000998 if (!hasInterval(*AS))
999 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1000 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001001 }
1002
Owen Anderson99500ae2008-09-15 22:00:38 +00001003 // Skip over empty initial indices.
1004 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1005 getInstructionFromIndex(MIIndex) == 0)
1006 MIIndex += InstrSlots::NUM;
1007
Chris Lattner428b92e2006-09-15 03:57:23 +00001008 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +00001009 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001010
Evan Cheng438f7bc2006-11-10 08:43:01 +00001011 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001012 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1013 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001014 // handle register defs - build intervals
Dan Gohmand735b802008-10-03 15:45:36 +00001015 if (MO.isReg() && MO.getReg() && MO.isDef()) {
Evan Chengef0732d2008-07-10 07:35:43 +00001016 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Dale Johannesen91aac102008-09-17 21:13:11 +00001017 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001018 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001019
1020 // Skip over the empty slots after each instruction.
1021 unsigned Slots = MI->getDesc().getNumDefs();
1022 if (Slots == 0)
1023 Slots = 1;
1024 MIIndex += InstrSlots::NUM * Slots;
Owen Anderson7fbad272008-07-23 21:37:49 +00001025
1026 // Skip over empty indices.
1027 while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
1028 getInstructionFromIndex(MIIndex) == 0)
1029 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001030 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001031 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001032}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001033
Evan Chengd0e32c52008-10-29 05:06:14 +00001034bool LiveIntervals::findLiveInMBBs(unsigned Start, unsigned End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001035 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001036 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001037 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001038
1039 bool ResVal = false;
1040 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001041 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001042 break;
1043 MBBs.push_back(I->second);
1044 ResVal = true;
1045 ++I;
1046 }
1047 return ResVal;
1048}
1049
Evan Chengd0e32c52008-10-29 05:06:14 +00001050bool LiveIntervals::findReachableMBBs(unsigned Start, unsigned End,
1051 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1052 std::vector<IdxMBBPair>::const_iterator I =
1053 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1054
1055 bool ResVal = false;
1056 while (I != Idx2MBBMap.end()) {
1057 if (I->first > End)
1058 break;
1059 MachineBasicBlock *MBB = I->second;
1060 if (getMBBEndIdx(MBB) > End)
1061 break;
1062 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1063 SE = MBB->succ_end(); SI != SE; ++SI)
1064 MBBs.push_back(*SI);
1065 ResVal = true;
1066 ++I;
1067 }
1068 return ResVal;
1069}
1070
Owen Anderson03857b22008-08-13 21:49:13 +00001071LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001072 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001073 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001074}
Evan Chengf2fbca62007-11-12 06:35:08 +00001075
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001076/// dupInterval - Duplicate a live interval. The caller is responsible for
1077/// managing the allocated memory.
1078LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1079 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001080 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001081 return NewLI;
1082}
1083
Evan Chengc8d044e2008-02-15 18:24:29 +00001084/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1085/// copy field and returns the source register that defines it.
1086unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
1087 if (!VNI->copy)
1088 return 0;
1089
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001090 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
1091 // If it's extracting out of a physical register, return the sub-register.
1092 unsigned Reg = VNI->copy->getOperand(1).getReg();
1093 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1094 Reg = tri_->getSubReg(Reg, VNI->copy->getOperand(2).getImm());
1095 return Reg;
Dan Gohman97121ba2009-04-08 00:15:30 +00001096 } else if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1097 VNI->copy->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng7e073ba2008-04-09 20:57:25 +00001098 return VNI->copy->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001099
Evan Cheng04ee5a12009-01-20 19:12:24 +00001100 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1101 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001102 return SrcReg;
1103 assert(0 && "Unrecognized copy instruction!");
1104 return 0;
1105}
Evan Chengf2fbca62007-11-12 06:35:08 +00001106
1107//===----------------------------------------------------------------------===//
1108// Register allocator hooks.
1109//
1110
Evan Chengd70dbb52008-02-22 09:24:50 +00001111/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1112/// allow one) virtual register operand, then its uses are implicitly using
1113/// the register. Returns the virtual register.
1114unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1115 MachineInstr *MI) const {
1116 unsigned RegOp = 0;
1117 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1118 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001119 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001120 continue;
1121 unsigned Reg = MO.getReg();
1122 if (Reg == 0 || Reg == li.reg)
1123 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001124
1125 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1126 !allocatableRegs_[Reg])
1127 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001128 // FIXME: For now, only remat MI with at most one register operand.
1129 assert(!RegOp &&
1130 "Can't rematerialize instruction with multiple register operand!");
1131 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001132#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001133 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001134#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001135 }
1136 return RegOp;
1137}
1138
1139/// isValNoAvailableAt - Return true if the val# of the specified interval
1140/// which reaches the given instruction also reaches the specified use index.
1141bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
1142 unsigned UseIdx) const {
1143 unsigned Index = getInstructionIndex(MI);
1144 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1145 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1146 return UI != li.end() && UI->valno == ValNo;
1147}
1148
Evan Chengf2fbca62007-11-12 06:35:08 +00001149/// isReMaterializable - Returns true if the definition MI of the specified
1150/// val# of the specified interval is re-materializable.
1151bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001152 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001153 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001154 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001155 if (DisableReMat)
1156 return false;
1157
Evan Cheng20ccded2008-03-15 00:19:36 +00001158 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001159 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001160
1161 int FrameIdx = 0;
1162 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001163 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001164 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1165 // this but remember this is not safe to fold into a two-address
1166 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001167 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001168 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001169
Dan Gohman6d69ba82008-07-25 00:02:30 +00001170 // If the target-specific rules don't identify an instruction as
1171 // being trivially rematerializable, use some target-independent
1172 // rules.
1173 if (!MI->getDesc().isRematerializable() ||
1174 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001175 if (!EnableAggressiveRemat)
1176 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001177
Dan Gohman0471a792008-07-28 18:43:51 +00001178 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001179 // we can't analyze it.
1180 const TargetInstrDesc &TID = MI->getDesc();
1181 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1182 return false;
1183
1184 // Avoid instructions obviously unsafe for remat.
1185 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1186 return false;
1187
1188 // If the instruction accesses memory and the memory could be non-constant,
1189 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001190 for (std::list<MachineMemOperand>::const_iterator
1191 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001192 const MachineMemOperand &MMO = *I;
1193 if (MMO.isVolatile() || MMO.isStore())
1194 return false;
1195 const Value *V = MMO.getValue();
1196 if (!V)
1197 return false;
1198 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1199 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001200 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001201 } else if (!aa_->pointsToConstantMemory(V))
1202 return false;
1203 }
1204
1205 // If any of the registers accessed are non-constant, conservatively assume
1206 // the instruction is not rematerializable.
1207 unsigned ImpUse = 0;
1208 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1209 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001210 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001211 unsigned Reg = MO.getReg();
1212 if (Reg == 0)
1213 continue;
1214 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1215 return false;
1216
1217 // Only allow one def, and that in the first operand.
1218 if (MO.isDef() != (i == 0))
1219 return false;
1220
1221 // Only allow constant-valued registers.
1222 bool IsLiveIn = mri_->isLiveIn(Reg);
1223 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1224 E = mri_->def_end();
1225
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001226 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001227 if (MO.isDef() && (next(I) != E || IsLiveIn))
1228 return false;
1229
1230 if (MO.isUse()) {
1231 // Only allow one use other register use, as that's all the
1232 // remat mechanisms support currently.
1233 if (Reg != li.reg) {
1234 if (ImpUse == 0)
1235 ImpUse = Reg;
1236 else if (Reg != ImpUse)
1237 return false;
1238 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001239 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001240 if (I != E && (next(I) != E || IsLiveIn))
1241 return false;
1242 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001243 }
1244 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001245 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001246
Dan Gohman6d69ba82008-07-25 00:02:30 +00001247 unsigned ImpUse = getReMatImplicitUse(li, MI);
1248 if (ImpUse) {
1249 const LiveInterval &ImpLi = getInterval(ImpUse);
1250 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1251 re = mri_->use_end(); ri != re; ++ri) {
1252 MachineInstr *UseMI = &*ri;
1253 unsigned UseIdx = getInstructionIndex(UseMI);
1254 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1255 continue;
1256 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1257 return false;
1258 }
Evan Chengdc377862008-09-30 15:44:16 +00001259
1260 // If a register operand of the re-materialized instruction is going to
1261 // be spilled next, then it's not legal to re-materialize this instruction.
1262 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1263 if (ImpUse == SpillIs[i]->reg)
1264 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001265 }
1266 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001267}
1268
Evan Cheng06587492008-10-24 02:05:00 +00001269/// isReMaterializable - Returns true if the definition MI of the specified
1270/// val# of the specified interval is re-materializable.
1271bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1272 const VNInfo *ValNo, MachineInstr *MI) {
1273 SmallVector<LiveInterval*, 4> Dummy1;
1274 bool Dummy2;
1275 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1276}
1277
Evan Cheng5ef3a042007-12-06 00:01:56 +00001278/// isReMaterializable - Returns true if every definition of MI of every
1279/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001280bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1281 SmallVectorImpl<LiveInterval*> &SpillIs,
1282 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001283 isLoad = false;
1284 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1285 i != e; ++i) {
1286 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001287 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001288 continue; // Dead val#.
1289 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001290 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001291 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001292 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001293 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001294 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001295 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001296 return false;
1297 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001298 }
1299 return true;
1300}
1301
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001302/// FilterFoldedOps - Filter out two-address use operands. Return
1303/// true if it finds any issue with the operands that ought to prevent
1304/// folding.
1305static bool FilterFoldedOps(MachineInstr *MI,
1306 SmallVector<unsigned, 2> &Ops,
1307 unsigned &MRInfo,
1308 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001309 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001310 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1311 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001312 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001313 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001314 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001315 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001316 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001317 MRInfo |= (unsigned)VirtRegMap::isMod;
1318 else {
1319 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001320 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001321 MRInfo = VirtRegMap::isModRef;
1322 continue;
1323 }
1324 MRInfo |= (unsigned)VirtRegMap::isRef;
1325 }
1326 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001327 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001328 return false;
1329}
1330
1331
1332/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1333/// slot / to reg or any rematerialized load into ith operand of specified
1334/// MI. If it is successul, MI is updated with the newly created MI and
1335/// returns true.
1336bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1337 VirtRegMap &vrm, MachineInstr *DefMI,
1338 unsigned InstrIdx,
1339 SmallVector<unsigned, 2> &Ops,
1340 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001341 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001342 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001343 RemoveMachineInstrFromMaps(MI);
1344 vrm.RemoveMachineInstrFromMaps(MI);
1345 MI->eraseFromParent();
1346 ++numFolds;
1347 return true;
1348 }
1349
1350 // Filter the list of operand indexes that are to be folded. Abort if
1351 // any operand will prevent folding.
1352 unsigned MRInfo = 0;
1353 SmallVector<unsigned, 2> FoldOps;
1354 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1355 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001356
Evan Cheng427f4c12008-03-31 23:19:51 +00001357 // The only time it's safe to fold into a two address instruction is when
1358 // it's folding reload and spill from / into a spill stack slot.
1359 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001360 return false;
1361
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001362 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1363 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001364 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001365 // Remember this instruction uses the spill slot.
1366 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1367
Evan Chengf2fbca62007-11-12 06:35:08 +00001368 // Attempt to fold the memory reference into the instruction. If
1369 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001370 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001371 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001372 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001373 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001374 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001375 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001376 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +00001377 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
1378 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001379 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001380 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001381 return true;
1382 }
1383 return false;
1384}
1385
Evan Cheng018f9b02007-12-05 03:22:34 +00001386/// canFoldMemoryOperand - Returns true if the specified load / store
1387/// folding is possible.
1388bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001389 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001390 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001391 // Filter the list of operand indexes that are to be folded. Abort if
1392 // any operand will prevent folding.
1393 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001394 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001395 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1396 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001397
Evan Cheng3c75ba82008-04-01 21:37:32 +00001398 // It's only legal to remat for a use, not a def.
1399 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001400 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001401
Evan Chengd70dbb52008-02-22 09:24:50 +00001402 return tii_->canFoldMemoryOperand(MI, FoldOps);
1403}
1404
Evan Cheng81a03822007-11-17 00:40:40 +00001405bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1406 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1407 for (LiveInterval::Ranges::const_iterator
1408 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1409 std::vector<IdxMBBPair>::const_iterator II =
1410 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1411 if (II == Idx2MBBMap.end())
1412 continue;
1413 if (I->end > II->first) // crossing a MBB.
1414 return false;
1415 MBBs.insert(II->second);
1416 if (MBBs.size() > 1)
1417 return false;
1418 }
1419 return true;
1420}
1421
Evan Chengd70dbb52008-02-22 09:24:50 +00001422/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1423/// interval on to-be re-materialized operands of MI) with new register.
1424void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1425 MachineInstr *MI, unsigned NewVReg,
1426 VirtRegMap &vrm) {
1427 // There is an implicit use. That means one of the other operand is
1428 // being remat'ed and the remat'ed instruction has li.reg as an
1429 // use operand. Make sure we rewrite that as well.
1430 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1431 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001432 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001433 continue;
1434 unsigned Reg = MO.getReg();
1435 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1436 continue;
1437 if (!vrm.isReMaterialized(Reg))
1438 continue;
1439 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001440 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1441 if (UseMO)
1442 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001443 }
1444}
1445
Evan Chengf2fbca62007-11-12 06:35:08 +00001446/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1447/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001448bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001449rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
1450 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001451 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001452 unsigned Slot, int LdSlot,
1453 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001454 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001455 const TargetRegisterClass* rc,
1456 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001457 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001458 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001459 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001460 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001461 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001462 RestartInstruction:
1463 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1464 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001465 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001466 continue;
1467 unsigned Reg = mop.getReg();
1468 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001469 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001470 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001471 if (Reg != li.reg)
1472 continue;
1473
1474 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001475 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001476 int FoldSlot = Slot;
1477 if (DefIsReMat) {
1478 // If this is the rematerializable definition MI itself and
1479 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001480 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001481 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1482 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001483 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001484 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001485 MI->eraseFromParent();
1486 break;
1487 }
1488
1489 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001490 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001491 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001492 if (isLoad) {
1493 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1494 FoldSS = isLoadSS;
1495 FoldSlot = LdSlot;
1496 }
1497 }
1498
Evan Chengf2fbca62007-11-12 06:35:08 +00001499 // Scan all of the operands of this instruction rewriting operands
1500 // to use NewVReg instead of li.reg as appropriate. We do this for
1501 // two reasons:
1502 //
1503 // 1. If the instr reads the same spilled vreg multiple times, we
1504 // want to reuse the NewVReg.
1505 // 2. If the instr is a two-addr instruction, we are required to
1506 // keep the src/dst regs pinned.
1507 //
1508 // Keep track of whether we replace a use and/or def so that we can
1509 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001510
Evan Cheng81a03822007-11-17 00:40:40 +00001511 HasUse = mop.isUse();
1512 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001513 SmallVector<unsigned, 2> Ops;
1514 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001515 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001516 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001517 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001518 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001519 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001520 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001521 continue;
1522 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001523 Ops.push_back(j);
1524 HasUse |= MOj.isUse();
1525 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001526 }
1527 }
1528
Evan Cheng79a796c2008-07-12 01:56:02 +00001529 if (HasUse && !li.liveAt(getUseIndex(index)))
1530 // Must be defined by an implicit def. It should not be spilled. Note,
1531 // this is for correctness reason. e.g.
1532 // 8 %reg1024<def> = IMPLICIT_DEF
1533 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1534 // The live range [12, 14) are not part of the r1024 live interval since
1535 // it's defined by an implicit def. It will not conflicts with live
1536 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001537 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001538 // the INSERT_SUBREG and both target registers that would overlap.
1539 HasUse = false;
1540
David Greene26b86a02008-10-27 17:38:59 +00001541 // Create a new virtual register for the spill interval.
1542 // Create the new register now so we can map the fold instruction
1543 // to the new register so when it is unfolded we get the correct
1544 // answer.
1545 bool CreatedNewVReg = false;
1546 if (NewVReg == 0) {
1547 NewVReg = mri_->createVirtualRegister(rc);
1548 vrm.grow();
1549 CreatedNewVReg = true;
1550 }
1551
Evan Cheng9c3c2212008-06-06 07:54:39 +00001552 if (!TryFold)
1553 CanFold = false;
1554 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001555 // Do not fold load / store here if we are splitting. We'll find an
1556 // optimal point to insert a load / store later.
1557 if (!TrySplit) {
1558 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001559 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001560 // Folding the load/store can completely change the instruction in
1561 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001562
1563 if (FoldSS) {
1564 // We need to give the new vreg the same stack slot as the
1565 // spilled interval.
1566 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1567 }
1568
Evan Cheng018f9b02007-12-05 03:22:34 +00001569 HasUse = false;
1570 HasDef = false;
1571 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001572 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001573 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001574 goto RestartInstruction;
1575 }
1576 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001577 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001578 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001579 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001580 }
Evan Chengcddbb832007-11-30 21:23:43 +00001581
Evan Chengcddbb832007-11-30 21:23:43 +00001582 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001583 if (mop.isImplicit())
1584 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001585
1586 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001587 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1588 MachineOperand &mopj = MI->getOperand(Ops[j]);
1589 mopj.setReg(NewVReg);
1590 if (mopj.isImplicit())
1591 rewriteImplicitOps(li, MI, NewVReg, vrm);
1592 }
Evan Chengcddbb832007-11-30 21:23:43 +00001593
Evan Cheng81a03822007-11-17 00:40:40 +00001594 if (CreatedNewVReg) {
1595 if (DefIsReMat) {
1596 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001597 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001598 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001599 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001600 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001601 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001602 }
1603 if (!CanDelete || (HasUse && HasDef)) {
1604 // If this is a two-addr instruction then its use operands are
1605 // rematerializable but its def is not. It should be assigned a
1606 // stack slot.
1607 vrm.assignVirt2StackSlot(NewVReg, Slot);
1608 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001609 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001610 vrm.assignVirt2StackSlot(NewVReg, Slot);
1611 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001612 } else if (HasUse && HasDef &&
1613 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1614 // If this interval hasn't been assigned a stack slot (because earlier
1615 // def is a deleted remat def), do it now.
1616 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1617 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001618 }
1619
Evan Cheng313d4b82008-02-23 00:33:04 +00001620 // Re-matting an instruction with virtual register use. Add the
1621 // register as an implicit use on the use MI.
1622 if (DefIsReMat && ImpUse)
1623 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1624
Evan Cheng5b69eba2009-04-21 22:46:52 +00001625 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001626 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001627 if (CreatedNewVReg) {
1628 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001629 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001630 if (TrySplit)
1631 vrm.setIsSplitFromReg(NewVReg, li.reg);
1632 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001633
1634 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001635 if (CreatedNewVReg) {
1636 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
Lang Hames857c4e02009-06-17 21:01:20 +00001637 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Cheng81a03822007-11-17 00:40:40 +00001638 DOUT << " +" << LR;
1639 nI.addRange(LR);
1640 } else {
1641 // Extend the split live interval to this def / use.
1642 unsigned End = getUseIndex(index)+1;
1643 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1644 nI.getValNumInfo(nI.getNumValNums()-1));
1645 DOUT << " +" << LR;
1646 nI.addRange(LR);
1647 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001648 }
1649 if (HasDef) {
1650 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00001651 nI.getNextValue(0, 0, false, VNInfoAllocator));
Evan Chengf2fbca62007-11-12 06:35:08 +00001652 DOUT << " +" << LR;
1653 nI.addRange(LR);
1654 }
Evan Cheng81a03822007-11-17 00:40:40 +00001655
Evan Chengf2fbca62007-11-12 06:35:08 +00001656 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001657 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001658 DOUT << '\n';
1659 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001660 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001661}
Evan Cheng81a03822007-11-17 00:40:40 +00001662bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001663 const VNInfo *VNI,
1664 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001665 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001666 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hamesffd13262009-07-09 03:57:02 +00001667 if (VNI->kills[j].isPHIKill)
1668 continue;
1669
1670 unsigned KillIdx = VNI->kills[j].killIdx;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001671 if (KillIdx > Idx && KillIdx < End)
1672 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001673 }
1674 return false;
1675}
1676
Evan Cheng063284c2008-02-21 00:34:19 +00001677/// RewriteInfo - Keep track of machine instrs that will be rewritten
1678/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001679namespace {
1680 struct RewriteInfo {
1681 unsigned Index;
1682 MachineInstr *MI;
1683 bool HasUse;
1684 bool HasDef;
1685 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1686 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1687 };
Evan Cheng063284c2008-02-21 00:34:19 +00001688
Dan Gohman844731a2008-05-13 00:00:25 +00001689 struct RewriteInfoCompare {
1690 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1691 return LHS.Index < RHS.Index;
1692 }
1693 };
1694}
Evan Cheng063284c2008-02-21 00:34:19 +00001695
Evan Chengf2fbca62007-11-12 06:35:08 +00001696void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001697rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001698 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001699 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001700 unsigned Slot, int LdSlot,
1701 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001702 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001703 const TargetRegisterClass* rc,
1704 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001705 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001706 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001707 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001708 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001709 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1710 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001711 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001712 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001713 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001714 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001715 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001716
Evan Cheng063284c2008-02-21 00:34:19 +00001717 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001718 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001719 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001720 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1721 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001722 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001723 MachineOperand &O = ri.getOperand();
1724 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001725 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001726 unsigned index = getInstructionIndex(MI);
1727 if (index < start || index >= end)
1728 continue;
Evan Cheng79a796c2008-07-12 01:56:02 +00001729 if (O.isUse() && !li.liveAt(getUseIndex(index)))
1730 // Must be defined by an implicit def. It should not be spilled. Note,
1731 // this is for correctness reason. e.g.
1732 // 8 %reg1024<def> = IMPLICIT_DEF
1733 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1734 // The live range [12, 14) are not part of the r1024 live interval since
1735 // it's defined by an implicit def. It will not conflicts with live
1736 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001737 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001738 // the INSERT_SUBREG and both target registers that would overlap.
1739 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001740 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1741 }
1742 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1743
Evan Cheng313d4b82008-02-23 00:33:04 +00001744 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001745 // Now rewrite the defs and uses.
1746 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1747 RewriteInfo &rwi = RewriteMIs[i];
1748 ++i;
1749 unsigned index = rwi.Index;
1750 bool MIHasUse = rwi.HasUse;
1751 bool MIHasDef = rwi.HasDef;
1752 MachineInstr *MI = rwi.MI;
1753 // If MI def and/or use the same register multiple times, then there
1754 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001755 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001756 while (i != e && RewriteMIs[i].MI == MI) {
1757 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001758 bool isUse = RewriteMIs[i].HasUse;
1759 if (isUse) ++NumUses;
1760 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001761 MIHasDef |= RewriteMIs[i].HasDef;
1762 ++i;
1763 }
Evan Cheng81a03822007-11-17 00:40:40 +00001764 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001765
Evan Cheng0a891ed2008-05-23 23:00:04 +00001766 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001767 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001768 // register interval's spill weight to HUGE_VALF to prevent it from
1769 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001770 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001771 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001772 }
1773
Evan Cheng063284c2008-02-21 00:34:19 +00001774 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001775 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001776 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001777 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001778 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001779 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001780 // One common case:
1781 // x = use
1782 // ...
1783 // ...
1784 // def = ...
1785 // = use
1786 // It's better to start a new interval to avoid artifically
1787 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001788 if (MIHasDef && !MIHasUse) {
1789 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001790 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001791 }
1792 }
Evan Chengcada2452007-11-28 01:28:46 +00001793 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001794
1795 bool IsNew = ThisVReg == 0;
1796 if (IsNew) {
1797 // This ends the previous live interval. If all of its def / use
1798 // can be folded, give it a low spill weight.
1799 if (NewVReg && TrySplit && AllCanFold) {
1800 LiveInterval &nI = getOrCreateInterval(NewVReg);
1801 nI.weight /= 10.0F;
1802 }
1803 AllCanFold = true;
1804 }
1805 NewVReg = ThisVReg;
1806
Evan Cheng81a03822007-11-17 00:40:40 +00001807 bool HasDef = false;
1808 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001809 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001810 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1811 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1812 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001813 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001814 if (!HasDef && !HasUse)
1815 continue;
1816
Evan Cheng018f9b02007-12-05 03:22:34 +00001817 AllCanFold &= CanFold;
1818
Evan Cheng81a03822007-11-17 00:40:40 +00001819 // Update weight of spill interval.
1820 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001821 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001822 // The spill weight is now infinity as it cannot be spilled again.
1823 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001824 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001825 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001826
1827 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001828 if (HasDef) {
1829 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001830 bool HasKill = false;
1831 if (!HasUse)
1832 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1833 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001834 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001835 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001836 if (VNI)
1837 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1838 }
Owen Anderson28998312008-08-13 22:28:50 +00001839 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001840 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001841 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001842 if (SII == SpillIdxes.end()) {
1843 std::vector<SRInfo> S;
1844 S.push_back(SRInfo(index, NewVReg, true));
1845 SpillIdxes.insert(std::make_pair(MBBId, S));
1846 } else if (SII->second.back().vreg != NewVReg) {
1847 SII->second.push_back(SRInfo(index, NewVReg, true));
1848 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001849 // If there is an earlier def and this is a two-address
1850 // instruction, then it's not possible to fold the store (which
1851 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001852 SRInfo &Info = SII->second.back();
1853 Info.index = index;
1854 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001855 }
1856 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001857 } else if (SII != SpillIdxes.end() &&
1858 SII->second.back().vreg == NewVReg &&
1859 (int)index > SII->second.back().index) {
1860 // There is an earlier def that's not killed (must be two-address).
1861 // The spill is no longer needed.
1862 SII->second.pop_back();
1863 if (SII->second.empty()) {
1864 SpillIdxes.erase(MBBId);
1865 SpillMBBs.reset(MBBId);
1866 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001867 }
1868 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001869 }
1870
1871 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001872 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001873 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001874 if (SII != SpillIdxes.end() &&
1875 SII->second.back().vreg == NewVReg &&
1876 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001877 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001878 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001879 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001880 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001881 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001882 // If we are splitting live intervals, only fold if it's the first
1883 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001884 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001885 else if (IsNew) {
1886 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001887 if (RII == RestoreIdxes.end()) {
1888 std::vector<SRInfo> Infos;
1889 Infos.push_back(SRInfo(index, NewVReg, true));
1890 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1891 } else {
1892 RII->second.push_back(SRInfo(index, NewVReg, true));
1893 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001894 RestoreMBBs.set(MBBId);
1895 }
1896 }
1897
1898 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001899 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001900 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001901 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001902
1903 if (NewVReg && TrySplit && AllCanFold) {
1904 // If all of its def / use can be folded, give it a low spill weight.
1905 LiveInterval &nI = getOrCreateInterval(NewVReg);
1906 nI.weight /= 10.0F;
1907 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001908}
1909
Evan Cheng1953d0c2007-11-29 10:12:14 +00001910bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1911 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001912 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001913 if (!RestoreMBBs[Id])
1914 return false;
1915 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1916 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1917 if (Restores[i].index == index &&
1918 Restores[i].vreg == vr &&
1919 Restores[i].canFold)
1920 return true;
1921 return false;
1922}
1923
1924void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1925 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001926 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001927 if (!RestoreMBBs[Id])
1928 return;
1929 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1930 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1931 if (Restores[i].index == index && Restores[i].vreg)
1932 Restores[i].index = -1;
1933}
Evan Cheng81a03822007-11-17 00:40:40 +00001934
Evan Cheng4cce6b42008-04-11 17:53:36 +00001935/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1936/// spilled and create empty intervals for their uses.
1937void
1938LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1939 const TargetRegisterClass* rc,
1940 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001941 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1942 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001943 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001944 MachineInstr *MI = &*ri;
1945 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001946 if (O.isDef()) {
1947 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1948 "Register def was not rewritten?");
1949 RemoveMachineInstrFromMaps(MI);
1950 vrm.RemoveMachineInstrFromMaps(MI);
1951 MI->eraseFromParent();
1952 } else {
1953 // This must be an use of an implicit_def so it's not part of the live
1954 // interval. Create a new empty live interval for it.
1955 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1956 unsigned NewVReg = mri_->createVirtualRegister(rc);
1957 vrm.grow();
1958 vrm.setIsImplicitlyDefined(NewVReg);
1959 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1960 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1961 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001962 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001963 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001964 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001965 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001966 }
1967 }
Evan Cheng419852c2008-04-03 16:39:43 +00001968 }
1969}
1970
Evan Chengf2fbca62007-11-12 06:35:08 +00001971std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001972addIntervalsForSpillsFast(const LiveInterval &li,
1973 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001974 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001975 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001976
1977 std::vector<LiveInterval*> added;
1978
1979 assert(li.weight != HUGE_VALF &&
1980 "attempt to spill already spilled interval!");
1981
1982 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1983 DEBUG(li.dump());
1984 DOUT << '\n';
1985
1986 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1987
Owen Andersona41e47a2008-08-19 22:12:11 +00001988 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1989 while (RI != mri_->reg_end()) {
1990 MachineInstr* MI = &*RI;
1991
1992 SmallVector<unsigned, 2> Indices;
1993 bool HasUse = false;
1994 bool HasDef = false;
1995
1996 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1997 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001998 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001999
2000 HasUse |= MI->getOperand(i).isUse();
2001 HasDef |= MI->getOperand(i).isDef();
2002
2003 Indices.push_back(i);
2004 }
2005
2006 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2007 Indices, true, slot, li.reg)) {
2008 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002009 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002010 vrm.assignVirt2StackSlot(NewVReg, slot);
2011
Owen Andersona41e47a2008-08-19 22:12:11 +00002012 // create a new register for this spill
2013 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002014
Owen Andersona41e47a2008-08-19 22:12:11 +00002015 // the spill weight is now infinity as it
2016 // cannot be spilled again
2017 nI.weight = HUGE_VALF;
2018
2019 // Rewrite register operands to use the new vreg.
2020 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2021 E = Indices.end(); I != E; ++I) {
2022 MI->getOperand(*I).setReg(NewVReg);
2023
2024 if (MI->getOperand(*I).isUse())
2025 MI->getOperand(*I).setIsKill(true);
2026 }
2027
2028 // Fill in the new live interval.
2029 unsigned index = getInstructionIndex(MI);
2030 if (HasUse) {
2031 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002032 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002033 DOUT << " +" << LR;
2034 nI.addRange(LR);
2035 vrm.addRestorePoint(NewVReg, MI);
2036 }
2037 if (HasDef) {
2038 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames857c4e02009-06-17 21:01:20 +00002039 nI.getNextValue(0, 0, false, getVNInfoAllocator()));
Owen Andersona41e47a2008-08-19 22:12:11 +00002040 DOUT << " +" << LR;
2041 nI.addRange(LR);
2042 vrm.addSpillPoint(NewVReg, true, MI);
2043 }
2044
Owen Anderson17197312008-08-18 23:41:04 +00002045 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002046
Owen Andersona41e47a2008-08-19 22:12:11 +00002047 DOUT << "\t\t\t\tadded new interval: ";
2048 DEBUG(nI.dump());
2049 DOUT << '\n';
Owen Andersona41e47a2008-08-19 22:12:11 +00002050 }
Owen Anderson9a032932008-08-18 21:20:32 +00002051
Owen Anderson9a032932008-08-18 21:20:32 +00002052
Owen Andersona41e47a2008-08-19 22:12:11 +00002053 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002054 }
Owen Andersond6664312008-08-18 18:05:32 +00002055
2056 return added;
2057}
2058
2059std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002060addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002061 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002062 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002063
2064 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002065 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002066
Evan Chengf2fbca62007-11-12 06:35:08 +00002067 assert(li.weight != HUGE_VALF &&
2068 "attempt to spill already spilled interval!");
2069
2070 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00002071 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00002072 DOUT << '\n';
2073
Evan Cheng72eeb942008-12-05 17:00:16 +00002074 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002075 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002076 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002077 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002078 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2079 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002080 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002081 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002082
2083 unsigned NumValNums = li.getNumValNums();
2084 SmallVector<MachineInstr*, 4> ReMatDefs;
2085 ReMatDefs.resize(NumValNums, NULL);
2086 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2087 ReMatOrigDefs.resize(NumValNums, NULL);
2088 SmallVector<int, 4> ReMatIds;
2089 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2090 BitVector ReMatDelete(NumValNums);
2091 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2092
Evan Cheng81a03822007-11-17 00:40:40 +00002093 // Spilling a split live interval. It cannot be split any further. Also,
2094 // it's also guaranteed to be a single val# / range interval.
2095 if (vrm.getPreSplitReg(li.reg)) {
2096 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002097 // Unset the split kill marker on the last use.
2098 unsigned KillIdx = vrm.getKillPoint(li.reg);
2099 if (KillIdx) {
2100 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2101 assert(KillMI && "Last use disappeared?");
2102 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2103 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002104 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002105 }
Evan Chengadf85902007-12-05 09:51:10 +00002106 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002107 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2108 Slot = vrm.getStackSlot(li.reg);
2109 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2110 MachineInstr *ReMatDefMI = DefIsReMat ?
2111 vrm.getReMaterializedMI(li.reg) : NULL;
2112 int LdSlot = 0;
2113 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2114 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002115 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002116 bool IsFirstRange = true;
2117 for (LiveInterval::Ranges::const_iterator
2118 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2119 // If this is a split live interval with multiple ranges, it means there
2120 // are two-address instructions that re-defined the value. Only the
2121 // first def can be rematerialized!
2122 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002123 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002124 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2125 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002126 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002127 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002128 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002129 } else {
2130 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2131 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002132 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002133 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002134 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002135 }
2136 IsFirstRange = false;
2137 }
Evan Cheng419852c2008-04-03 16:39:43 +00002138
Evan Cheng4cce6b42008-04-11 17:53:36 +00002139 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002140 return NewLIs;
2141 }
2142
2143 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002144 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
2145 TrySplit = false;
2146 if (TrySplit)
2147 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002148 bool NeedStackSlot = false;
2149 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2150 i != e; ++i) {
2151 const VNInfo *VNI = *i;
2152 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002153 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002154 continue; // Dead val#.
2155 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002156 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2157 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002158 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002159 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002160 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002161 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002162 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002163 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
2164 ClonedMIs.push_back(Clone);
2165 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002166
2167 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002168 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002169 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002170 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002171 CanDelete = false;
2172 // Need a stack slot if there is any live range where uses cannot be
2173 // rematerialized.
2174 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002175 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002176 if (CanDelete)
2177 ReMatDelete.set(VN);
2178 } else {
2179 // Need a stack slot if there is any live range where uses cannot be
2180 // rematerialized.
2181 NeedStackSlot = true;
2182 }
2183 }
2184
2185 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002186 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2187 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2188 Slot = vrm.assignVirt2StackSlot(li.reg);
2189
2190 // This case only occurs when the prealloc splitter has already assigned
2191 // a stack slot to this vreg.
2192 else
2193 Slot = vrm.getStackSlot(li.reg);
2194 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002195
2196 // Create new intervals and rewrite defs and uses.
2197 for (LiveInterval::Ranges::const_iterator
2198 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002199 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2200 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2201 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002202 bool CanDelete = ReMatDelete[I->valno->id];
2203 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002204 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002205 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002206 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002207 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002208 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002209 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002210 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002211 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002212 }
2213
Evan Cheng0cbb1162007-11-29 01:06:25 +00002214 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002215 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002216 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002217 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002218 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002219
Evan Chengb50bb8c2007-12-05 08:16:32 +00002220 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002221 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002222 if (NeedStackSlot) {
2223 int Id = SpillMBBs.find_first();
2224 while (Id != -1) {
2225 std::vector<SRInfo> &spills = SpillIdxes[Id];
2226 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
2227 int index = spills[i].index;
2228 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002229 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002230 bool isReMat = vrm.isReMaterialized(VReg);
2231 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002232 bool CanFold = false;
2233 bool FoundUse = false;
2234 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002235 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002236 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002237 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2238 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002239 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002240 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002241
2242 Ops.push_back(j);
2243 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002244 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002245 if (isReMat ||
2246 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2247 RestoreMBBs, RestoreIdxes))) {
2248 // MI has two-address uses of the same register. If the use
2249 // isn't the first and only use in the BB, then we can't fold
2250 // it. FIXME: Move this to rewriteInstructionsForSpills.
2251 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002252 break;
2253 }
Evan Chengaee4af62007-12-02 08:30:39 +00002254 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002255 }
2256 }
2257 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002258 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002259 if (CanFold && !Ops.empty()) {
2260 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002261 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002262 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002263 // Also folded uses, do not issue a load.
2264 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00002265 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
2266 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002267 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002268 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002269 }
2270
Evan Cheng7e073ba2008-04-09 20:57:25 +00002271 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002272 if (!Folded) {
2273 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2274 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002275 if (!MI->registerDefIsDead(nI.reg))
2276 // No need to spill a dead def.
2277 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002278 if (isKill)
2279 AddedKill.insert(&nI);
2280 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002281 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002282 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002283 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002284 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002285
Evan Cheng1953d0c2007-11-29 10:12:14 +00002286 int Id = RestoreMBBs.find_first();
2287 while (Id != -1) {
2288 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2289 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
2290 int index = restores[i].index;
2291 if (index == -1)
2292 continue;
2293 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002294 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002295 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002296 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002297 bool CanFold = false;
2298 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002299 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002300 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002301 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2302 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002303 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002304 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002305
Evan Cheng0cbb1162007-11-29 01:06:25 +00002306 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002307 // If this restore were to be folded, it would have been folded
2308 // already.
2309 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002310 break;
2311 }
Evan Chengaee4af62007-12-02 08:30:39 +00002312 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002313 }
2314 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002315
2316 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002317 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002318 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002319 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002320 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2321 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002322 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2323 int LdSlot = 0;
2324 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2325 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002326 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002327 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2328 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002329 if (!Folded) {
2330 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2331 if (ImpUse) {
2332 // Re-matting an instruction with virtual register use. Add the
2333 // register as an implicit use on the use MI and update the register
2334 // interval's spill weight to HUGE_VALF to prevent it from being
2335 // spilled.
2336 LiveInterval &ImpLi = getInterval(ImpUse);
2337 ImpLi.weight = HUGE_VALF;
2338 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2339 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002340 }
Evan Chengaee4af62007-12-02 08:30:39 +00002341 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002342 }
2343 // If folding is not possible / failed, then tell the spiller to issue a
2344 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002345 if (Folded)
2346 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002347 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002348 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002349 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002350 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002351 }
2352
Evan Chengb50bb8c2007-12-05 08:16:32 +00002353 // Finalize intervals: add kills, finalize spill weights, and filter out
2354 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002355 std::vector<LiveInterval*> RetNewLIs;
2356 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2357 LiveInterval *LI = NewLIs[i];
2358 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002359 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002360 if (!AddedKill.count(LI)) {
2361 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00002362 unsigned LastUseIdx = getBaseIndex(LR->end);
2363 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002364 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002365 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002366 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002367 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002368 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002369 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002370 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002371 RetNewLIs.push_back(LI);
2372 }
2373 }
Evan Cheng81a03822007-11-17 00:40:40 +00002374
Evan Cheng4cce6b42008-04-11 17:53:36 +00002375 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002376 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002377}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002378
2379/// hasAllocatableSuperReg - Return true if the specified physical register has
2380/// any super register that's allocatable.
2381bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2382 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2383 if (allocatableRegs_[*AS] && hasInterval(*AS))
2384 return true;
2385 return false;
2386}
2387
2388/// getRepresentativeReg - Find the largest super register of the specified
2389/// physical register.
2390unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2391 // Find the largest super-register that is allocatable.
2392 unsigned BestReg = Reg;
2393 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2394 unsigned SuperReg = *AS;
2395 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2396 BestReg = SuperReg;
2397 break;
2398 }
2399 }
2400 return BestReg;
2401}
2402
2403/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2404/// specified interval that conflicts with the specified physical register.
2405unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2406 unsigned PhysReg) const {
2407 unsigned NumConflicts = 0;
2408 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2409 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2410 E = mri_->reg_end(); I != E; ++I) {
2411 MachineOperand &O = I.getOperand();
2412 MachineInstr *MI = O.getParent();
2413 unsigned Index = getInstructionIndex(MI);
2414 if (pli.liveAt(Index))
2415 ++NumConflicts;
2416 }
2417 return NumConflicts;
2418}
2419
2420/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002421/// around all defs and uses of the specified interval. Return true if it
2422/// was able to cut its interval.
2423bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002424 unsigned PhysReg, VirtRegMap &vrm) {
2425 unsigned SpillReg = getRepresentativeReg(PhysReg);
2426
2427 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2428 // If there are registers which alias PhysReg, but which are not a
2429 // sub-register of the chosen representative super register. Assert
2430 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002431 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002432 tri_->isSuperRegister(*AS, SpillReg));
2433
Evan Cheng2824a652009-03-23 18:24:37 +00002434 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002435 LiveInterval &pli = getInterval(SpillReg);
2436 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2437 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2438 E = mri_->reg_end(); I != E; ++I) {
2439 MachineOperand &O = I.getOperand();
2440 MachineInstr *MI = O.getParent();
2441 if (SeenMIs.count(MI))
2442 continue;
2443 SeenMIs.insert(MI);
2444 unsigned Index = getInstructionIndex(MI);
2445 if (pli.liveAt(Index)) {
2446 vrm.addEmergencySpill(SpillReg, MI);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002447 unsigned StartIdx = getLoadIndex(Index);
2448 unsigned EndIdx = getStoreIndex(Index)+1;
Evan Cheng2824a652009-03-23 18:24:37 +00002449 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002450 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002451 Cut = true;
2452 } else {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002453 cerr << "Ran out of registers during register allocation!\n";
2454 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
2455 cerr << "Please check your inline asm statement for invalid "
2456 << "constraints:\n";
2457 MI->print(cerr.stream(), tm_);
2458 }
2459 exit(1);
2460 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002461 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2462 if (!hasInterval(*AS))
2463 continue;
2464 LiveInterval &spli = getInterval(*AS);
2465 if (spli.liveAt(Index))
2466 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
2467 }
2468 }
2469 }
Evan Cheng2824a652009-03-23 18:24:37 +00002470 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002471}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002472
2473LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002474 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002475 LiveInterval& Interval = getOrCreateInterval(reg);
2476 VNInfo* VN = Interval.getNextValue(
2477 getInstructionIndex(startInst) + InstrSlots::DEF,
Lang Hames857c4e02009-06-17 21:01:20 +00002478 startInst, true, getVNInfoAllocator());
2479 VN->setHasPHIKill(true);
Lang Hamesffd13262009-07-09 03:57:02 +00002480 VN->kills.push_back(
2481 VNInfo::KillInfo(terminatorGaps[startInst->getParent()], true));
Owen Andersonc4dc1322008-06-05 17:15:43 +00002482 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
2483 getMBBEndIdx(startInst->getParent()) + 1, VN);
2484 Interval.addRange(LR);
2485
2486 return LR;
2487}