blob: 825cf396ab127aff026059ad30744bd1153f1bb0 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/CodeGen/Passes.h"
Dan Gohman6d69ba82008-07-25 00:02:30 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000031#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000034#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000039#include "llvm/ADT/DepthFirstIterator.h"
40#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/ADT/Statistic.h"
42#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000043#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000044#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000046using namespace llvm;
47
Dan Gohman844731a2008-05-13 00:00:25 +000048// Hidden options for help debugging.
49static cl::opt<bool> DisableReMat("disable-rematerialization",
50 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000051
Dan Gohman4c8f8702008-07-25 15:08:37 +000052static cl::opt<bool> EnableAggressiveRemat("aggressive-remat", cl::Hidden);
53
Owen Andersonae339ba2008-08-19 00:17:30 +000054static cl::opt<bool> EnableFastSpilling("fast-spill",
55 cl::init(false), cl::Hidden);
56
Evan Cheng752195e2009-09-14 21:33:42 +000057static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
58
59static cl::opt<int> CoalescingLimit("early-coalescing-limit",
60 cl::init(-1), cl::Hidden);
61
62STATISTIC(numIntervals , "Number of original intervals");
63STATISTIC(numFolds , "Number of loads/stores folded into instructions");
64STATISTIC(numSplits , "Number of intervals split");
65STATISTIC(numCoalescing, "Number of early coalescing performed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000066
Devang Patel19974732007-05-03 01:11:54 +000067char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000068static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069
Chris Lattnerf7da2c72006-08-24 22:43:55 +000070void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000071 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000072 AU.addRequired<AliasAnalysis>();
73 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000074 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000075 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000076 AU.addPreservedID(MachineLoopInfoID);
77 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000078
79 if (!StrongPHIElim) {
80 AU.addPreservedID(PHIEliminationID);
81 AU.addRequiredID(PHIEliminationID);
82 }
83
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000086}
87
Chris Lattnerf7da2c72006-08-24 22:43:55 +000088void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000089 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000090 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000091 E = r2iMap_.end(); I != E; ++I)
92 delete I->second;
93
Evan Cheng3f32d652008-06-04 09:18:41 +000094 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000095 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 mi2iMap_.clear();
97 i2miMap_.clear();
98 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000099 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000100 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000101
Evan Chengdd199d22007-09-06 01:07:24 +0000102 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
103 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000104 while (!CloneMIs.empty()) {
105 MachineInstr *MI = CloneMIs.back();
106 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000107 mf_->DeleteMachineInstr(MI);
108 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000109}
110
Evan Cheng6ade93b2009-08-05 03:53:14 +0000111static bool CanTurnIntoImplicitDef(MachineInstr *MI, unsigned Reg,
Evan Chengb0f59732009-09-21 04:32:32 +0000112 unsigned OpIdx, const TargetInstrInfo *tii_){
Evan Cheng6ade93b2009-08-05 03:53:14 +0000113 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
114 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
115 Reg == SrcReg)
116 return true;
117
Evan Chengb0f59732009-09-21 04:32:32 +0000118 if (OpIdx == 2 && MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000119 return true;
Evan Chengb0f59732009-09-21 04:32:32 +0000120 if (OpIdx == 1 && MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng6ade93b2009-08-05 03:53:14 +0000121 return true;
122 return false;
123}
124
Evan Cheng2578ba22009-07-01 01:59:31 +0000125/// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
126/// there is one implicit_def for each use. Add isUndef marker to
127/// implicit_def defs and their uses.
128void LiveIntervals::processImplicitDefs() {
129 SmallSet<unsigned, 8> ImpDefRegs;
130 SmallVector<MachineInstr*, 8> ImpDefMIs;
131 MachineBasicBlock *Entry = mf_->begin();
132 SmallPtrSet<MachineBasicBlock*,16> Visited;
133 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
134 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
135 DFI != E; ++DFI) {
136 MachineBasicBlock *MBB = *DFI;
137 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
138 I != E; ) {
139 MachineInstr *MI = &*I;
140 ++I;
141 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
142 unsigned Reg = MI->getOperand(0).getReg();
Evan Cheng2578ba22009-07-01 01:59:31 +0000143 ImpDefRegs.insert(Reg);
Evan Cheng296925d2009-09-23 06:28:31 +0000144 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
145 for (const unsigned *SS = tri_->getSubRegisters(Reg); *SS; ++SS)
146 ImpDefRegs.insert(*SS);
147 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000148 ImpDefMIs.push_back(MI);
149 continue;
150 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000151
Evan Chengb0f59732009-09-21 04:32:32 +0000152 if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
153 MachineOperand &MO = MI->getOperand(2);
154 if (ImpDefRegs.count(MO.getReg())) {
155 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
156 // This is an identity copy, eliminate it now.
157 if (MO.isKill()) {
158 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
159 vi.removeKill(MI);
160 }
161 MI->eraseFromParent();
162 continue;
163 }
164 }
165
Evan Cheng459a7c62009-07-01 08:19:36 +0000166 bool ChangedToImpDef = false;
167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000168 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000169 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000170 continue;
171 unsigned Reg = MO.getReg();
172 if (!Reg)
173 continue;
174 if (!ImpDefRegs.count(Reg))
175 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000176 // Use is a copy, just turn it into an implicit_def.
Evan Chengb0f59732009-09-21 04:32:32 +0000177 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000178 bool isKill = MO.isKill();
179 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
180 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
181 MI->RemoveOperand(j);
Evan Chengb0f59732009-09-21 04:32:32 +0000182 if (isKill) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000183 ImpDefRegs.erase(Reg);
Evan Chengb0f59732009-09-21 04:32:32 +0000184 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
185 vi.removeKill(MI);
186 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000187 ChangedToImpDef = true;
188 break;
189 }
190
Evan Cheng2578ba22009-07-01 01:59:31 +0000191 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000192 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
193 // Make sure other uses of
194 for (unsigned j = i+1; j != e; ++j) {
195 MachineOperand &MOJ = MI->getOperand(j);
196 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
197 MOJ.setIsUndef();
198 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000199 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000200 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000201 }
202
Evan Cheng459a7c62009-07-01 08:19:36 +0000203 if (ChangedToImpDef) {
204 // Backtrack to process this new implicit_def.
205 --I;
206 } else {
207 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
208 MachineOperand& MO = MI->getOperand(i);
209 if (!MO.isReg() || !MO.isDef())
210 continue;
211 ImpDefRegs.erase(MO.getReg());
212 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000213 }
214 }
215
216 // Any outstanding liveout implicit_def's?
217 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
218 MachineInstr *MI = ImpDefMIs[i];
219 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000220 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
221 !ImpDefRegs.count(Reg)) {
222 // Delete all "local" implicit_def's. That include those which define
223 // physical registers since they cannot be liveout.
224 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000225 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000226 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000227
228 // If there are multiple defs of the same register and at least one
229 // is not an implicit_def, do not insert implicit_def's before the
230 // uses.
231 bool Skip = false;
232 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
233 DE = mri_->def_end(); DI != DE; ++DI) {
234 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
235 Skip = true;
236 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000237 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000238 }
239 if (Skip)
240 continue;
241
Evan Chengd129d732009-07-17 19:43:40 +0000242 // The only implicit_def which we want to keep are those that are live
243 // out of its block.
244 MI->eraseFromParent();
245
Evan Cheng459a7c62009-07-01 08:19:36 +0000246 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
247 UE = mri_->use_end(); UI != UE; ) {
248 MachineOperand &RMO = UI.getOperand();
249 MachineInstr *RMI = &*UI;
250 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000251 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000252 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000253 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000254
255 // Turn a copy use into an implicit_def.
256 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
257 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
258 Reg == SrcReg) {
259 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
260 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
261 RMI->RemoveOperand(j);
262 continue;
263 }
264
Evan Cheng2578ba22009-07-01 01:59:31 +0000265 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
266 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000267 RMO.setReg(NewVReg);
268 RMO.setIsUndef();
269 RMO.setIsKill();
270 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000271 }
272 ImpDefRegs.clear();
273 ImpDefMIs.clear();
274 }
275}
276
Lang Hames86511252009-09-04 20:41:11 +0000277
Owen Anderson80b3ce62008-05-28 20:54:50 +0000278void LiveIntervals::computeNumbering() {
279 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000280 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000281
282 Idx2MBBMap.clear();
283 MBB2IdxMap.clear();
284 mi2iMap_.clear();
285 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000286 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000287 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000288
Owen Andersona1566f22008-07-22 22:46:49 +0000289 FunctionSize = 0;
290
Chris Lattner428b92e2006-09-15 03:57:23 +0000291 // Number MachineInstrs and MachineBasicBlocks.
292 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000293 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
294 std::make_pair(MachineInstrIndex(),MachineInstrIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000295
Lang Hames86511252009-09-04 20:41:11 +0000296 MachineInstrIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000297 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
298 MBB != E; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000299 MachineInstrIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000300
Owen Anderson7fbad272008-07-23 21:37:49 +0000301 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000302 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000303 i2miMap_.push_back(0);
304
Chris Lattner428b92e2006-09-15 03:57:23 +0000305 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
306 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000307
308 if (I == MBB->getFirstTerminator()) {
309 // Leave a gap for before terminators, this is where we will point
310 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000311 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000312 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000313 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000314 assert(inserted &&
315 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000316 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000317 i2miMap_.push_back(0);
318
Lang Hames35f291d2009-09-12 03:34:03 +0000319 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000320 }
321
Chris Lattner428b92e2006-09-15 03:57:23 +0000322 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000323 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000324 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000325 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000326 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000327 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000328
Evan Cheng4ed43292008-10-18 05:21:37 +0000329 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000330 unsigned Slots = I->getDesc().getNumDefs();
331 if (Slots == 0)
332 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000333 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000334 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000335 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000336 }
337
Owen Anderson35578012008-06-16 07:10:49 +0000338 }
Lang Hamesffd13262009-07-09 03:57:02 +0000339
340 if (MBB->getFirstTerminator() == MBB->end()) {
341 // Leave a gap for before terminators, this is where we will point
342 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000343 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000344 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000345 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000346 assert(inserted &&
347 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000348 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000349 i2miMap_.push_back(0);
350
Lang Hames35f291d2009-09-12 03:34:03 +0000351 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000352 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000353
Owen Anderson1fbb4542008-06-16 16:58:24 +0000354 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000355 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000356 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000357 }
Lang Hamesffd13262009-07-09 03:57:02 +0000358
Evan Cheng4ca980e2007-10-17 02:10:22 +0000359 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000360
361 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000362 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000363 for (LiveInterval::iterator LI = OI->second->begin(),
364 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000365
Owen Anderson7eec0c22008-05-29 23:01:22 +0000366 // Remap the start index of the live range to the corresponding new
367 // number, or our best guess at what it _should_ correspond to if the
368 // original instruction has been erased. This is either the following
369 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000370 unsigned index = LI->start.getVecIndex();
371 MachineInstrIndex::Slot offset = LI->start.getSlot();
372 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000373 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000374 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000375 // Take the pair containing the index
376 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000377 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000378
Owen Anderson7fbad272008-07-23 21:37:49 +0000379 LI->start = getMBBStartIdx(J->second);
380 } else {
Lang Hames86511252009-09-04 20:41:11 +0000381 LI->start = MachineInstrIndex(
382 MachineInstrIndex(mi2iMap_[OldI2MI[index]]),
383 (MachineInstrIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000384 }
385
386 // Remap the ending index in the same way that we remapped the start,
387 // except for the final step where we always map to the immediately
388 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000389 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000390 offset = LI->end.getSlot();
391 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000392 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000393 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000394 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000395 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000396
Lang Hames35f291d2009-09-12 03:34:03 +0000397 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000398 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000399 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000400 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
401
402 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000403 LI->end =
404 MachineInstrIndex(mi2iMap_[OldI2MI[index]],
405 (idx == index ? offset : MachineInstrIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000406 else
Lang Hames86511252009-09-04 20:41:11 +0000407 LI->end =
408 MachineInstrIndex(MachineInstrIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000409 }
Owen Anderson788d0412008-08-06 18:35:45 +0000410 }
411
Owen Anderson03857b22008-08-13 21:49:13 +0000412 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
413 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000414 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000415
Owen Anderson7eec0c22008-05-29 23:01:22 +0000416 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000417 // start indices above. VN's with special sentinel defs
418 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000419 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000420 unsigned index = vni->def.getVecIndex();
421 MachineInstrIndex::Slot offset = vni->def.getSlot();
422 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000423 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000424 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000425 // Take the pair containing the index
426 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000427 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000428
Owen Anderson91292392008-07-30 17:42:47 +0000429 vni->def = getMBBStartIdx(J->second);
430 } else {
Lang Hames86511252009-09-04 20:41:11 +0000431 vni->def = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000432 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000433 }
Owen Anderson745825f42008-05-28 22:40:08 +0000434
Owen Anderson7eec0c22008-05-29 23:01:22 +0000435 // Remap the VNInfo kill indices, which works the same as
436 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000437 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000438 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000439 MachineInstrIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000440
Lang Hames86511252009-09-04 20:41:11 +0000441 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000442 assert("Value killed at a load slot.");
443 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000444 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000445 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000446
Lang Hamesffd13262009-07-09 03:57:02 +0000447 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000448 } else {
Lang Hames86511252009-09-04 20:41:11 +0000449 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000450 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000451 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000452 --I;
Lang Hames86511252009-09-04 20:41:11 +0000453 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000454 } else {
455 assert(OldI2MI[index] != 0 &&
456 "Kill refers to instruction not present in index maps.");
Lang Hames86511252009-09-04 20:41:11 +0000457 vni->kills[i] = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000458 }
459
460 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000461 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000462 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
463
464 if (index != OldI2MI.size())
465 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
466 (idx == index ? offset : 0);
467 else
468 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000469 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000470 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000471 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000472 }
Owen Anderson788d0412008-08-06 18:35:45 +0000473 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000474}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000475
Lang Hamesf41538d2009-06-02 16:53:25 +0000476void LiveIntervals::scaleNumbering(int factor) {
477 // Need to
478 // * scale MBB begin and end points
479 // * scale all ranges.
480 // * Update VNI structures.
481 // * Scale instruction numberings
482
483 // Scale the MBB indices.
484 Idx2MBBMap.clear();
485 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
486 MBB != MBBE; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000487 std::pair<MachineInstrIndex, MachineInstrIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
488 mbbIndices.first = mbbIndices.first.scale(factor);
489 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000490 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
491 }
492 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
493
Lang Hamesffd13262009-07-09 03:57:02 +0000494 // Scale terminator gaps.
Lang Hames86511252009-09-04 20:41:11 +0000495 for (DenseMap<MachineBasicBlock*, MachineInstrIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000496 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
497 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000498 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000499 }
500
Lang Hamesf41538d2009-06-02 16:53:25 +0000501 // Scale the intervals.
502 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
503 LI->second->scaleNumbering(factor);
504 }
505
506 // Scale MachineInstrs.
507 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hames86511252009-09-04 20:41:11 +0000508 MachineInstrIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000509 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
510 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000511 MachineInstrIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000512 mi2iMap_[MI->first] = newSlot;
513 highestSlot = std::max(highestSlot, newSlot);
514 }
515
Lang Hames86511252009-09-04 20:41:11 +0000516 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000517 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000518 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000519 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
520 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000521 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000522 }
523
524}
525
526
Owen Anderson80b3ce62008-05-28 20:54:50 +0000527/// runOnMachineFunction - Register allocate the whole function
528///
529bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
530 mf_ = &fn;
531 mri_ = &mf_->getRegInfo();
532 tm_ = &fn.getTarget();
533 tri_ = tm_->getRegisterInfo();
534 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000535 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000536 lv_ = &getAnalysis<LiveVariables>();
537 allocatableRegs_ = tri_->getAllocatableSet(fn);
538
Evan Cheng2578ba22009-07-01 01:59:31 +0000539 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000540 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000542 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000543
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 numIntervals += getNumIntervals();
545
Chris Lattner70ca3582004-09-30 15:59:17 +0000546 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000548}
549
Chris Lattner70ca3582004-09-30 15:59:17 +0000550/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000551void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000552 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000553 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000554 I->second->print(OS, tri_);
555 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000556 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000557
Evan Cheng752195e2009-09-14 21:33:42 +0000558 printInstrs(OS);
559}
560
561void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000562 OS << "********** MACHINEINSTRS **********\n";
563
Chris Lattner3380d5c2009-07-21 21:12:58 +0000564 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
565 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000566 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000567 for (MachineBasicBlock::iterator mii = mbbi->begin(),
568 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000569 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000570 }
571 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000572}
573
Evan Cheng752195e2009-09-14 21:33:42 +0000574void LiveIntervals::dumpInstrs() const {
575 printInstrs(errs());
576}
577
Evan Chengc92da382007-11-03 07:20:12 +0000578/// conflictsWithPhysRegDef - Returns true if the specified register
579/// is defined during the duration of the specified interval.
580bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
581 VirtRegMap &vrm, unsigned reg) {
582 for (LiveInterval::Ranges::const_iterator
583 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000584 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000585 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
586 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000587 // skip deleted instructions
588 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000589 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000590 if (index == end) break;
591
592 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000593 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
594 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000595 if (SrcReg == li.reg || DstReg == li.reg)
596 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000597 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
598 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000599 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000600 continue;
601 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000602 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000603 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000604 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000605 if (!vrm.hasPhys(PhysReg))
606 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000607 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000608 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000609 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000610 return true;
611 }
612 }
613 }
614
615 return false;
616}
617
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000618/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
619/// it can check use as well.
620bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
621 unsigned Reg, bool CheckUse,
622 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
623 for (LiveInterval::Ranges::const_iterator
624 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000625 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000626 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
627 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000628 // Skip deleted instructions.
629 MachineInstr *MI = 0;
630 while (index != end) {
631 MI = getInstructionFromIndex(index);
632 if (MI)
633 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000634 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000635 }
636 if (index == end) break;
637
638 if (JoinedCopies.count(MI))
639 continue;
640 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
641 MachineOperand& MO = MI->getOperand(i);
642 if (!MO.isReg())
643 continue;
644 if (MO.isUse() && !CheckUse)
645 continue;
646 unsigned PhysReg = MO.getReg();
647 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
648 continue;
649 if (tri_->isSubRegister(Reg, PhysReg))
650 return true;
651 }
652 }
653 }
654
655 return false;
656}
657
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000658#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000659static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000660 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000661 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000662 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000663 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000664}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000665#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000666
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000667void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000668 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000669 MachineInstrIndex MIIdx,
670 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000671 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000672 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000673 DEBUG({
674 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000675 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000676 });
Evan Cheng419852c2008-04-03 16:39:43 +0000677
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000678 // Virtual registers may be defined multiple times (due to phi
679 // elimination and 2-addr elimination). Much of what we do only has to be
680 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000681 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000682 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 if (interval.empty()) {
684 // Get the Idx of the defining instructions.
Lang Hames86511252009-09-04 20:41:11 +0000685 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000686 // Earlyclobbers move back one, so that they overlap the live range
687 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000688 if (MO.isEarlyClobber())
689 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000690 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000691 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000692 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000693 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000694 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000695 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000696 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000697 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000698 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000699 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000700
701 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000702
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 // Loop over all of the blocks that the vreg is defined in. There are
704 // two cases we have to handle here. The most common case is a vreg
705 // whose lifetime is contained within a basic block. In this case there
706 // will be a single kill, in MBB, which comes after the definition.
707 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
708 // FIXME: what about dead vars?
Lang Hames86511252009-09-04 20:41:11 +0000709 MachineInstrIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000710 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000711 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000712 else if (MO.isEarlyClobber())
713 // Earlyclobbers that die in this instruction move up one extra, to
714 // compensate for having the starting point moved back one. This
715 // gets them to overlap the live range of other outputs.
716 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000717 else
Lang Hames35f291d2009-09-12 03:34:03 +0000718 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000719
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000720 // If the kill happens after the definition, we have an intra-block
721 // live range.
722 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000723 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000724 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000725 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000726 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000727 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000728 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000729 return;
730 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000731 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000732
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000733 // The other case we handle is when a virtual register lives to the end
734 // of the defining block, potentially live across some blocks, then is
735 // live into some number of blocks, but gets killed. Start by adding a
736 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000737 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000738 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000739 interval.addRange(NewLR);
740
741 // Iterate over all of the blocks that the variable is completely
742 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
743 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000744 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
745 E = vi.AliveBlocks.end(); I != E; ++I) {
746 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000747 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000748 ValNo);
749 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000750 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000751 }
752
753 // Finally, this virtual register is live from the start of any killing
754 // block to the 'use' slot of the killing instruction.
755 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
756 MachineInstr *Kill = vi.Kills[i];
Evan Cheng21731112009-09-12 02:01:07 +0000757 MachineInstrIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000758 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Evan Chengb0f59732009-09-21 04:32:32 +0000759 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000760 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000761 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000762 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000763 }
764
765 } else {
766 // If this is the second time we see a virtual register definition, it
767 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000768 // the result of two address elimination, then the vreg is one of the
769 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000770 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000771 // If this is a two-address definition, then we have already processed
772 // the live range. The only problem is that we didn't realize there
773 // are actually two values in the live interval. Because of this we
774 // need to take the LiveRegion that defines this register and split it
775 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000776 assert(interval.containsOneValue());
Lang Hames86511252009-09-04 20:41:11 +0000777 MachineInstrIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
778 MachineInstrIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000779 if (MO.isEarlyClobber())
780 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000781
Lang Hames35f291d2009-09-12 03:34:03 +0000782 const LiveRange *OldLR =
783 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000784 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000785
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000786 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000787 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000788 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000789
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000790 // Two-address vregs should always only be redefined once. This means
791 // that at this point, there should be exactly one value number in it.
792 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
793
Chris Lattner91725b72006-08-31 05:54:43 +0000794 // The new value number (#1) is defined by the instruction we claimed
795 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000796 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000797 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000798 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000799 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
800
Chris Lattner91725b72006-08-31 05:54:43 +0000801 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000802 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000803 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000804 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000805 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000806
807 // Add the new live interval which replaces the range for the input copy.
808 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000809 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000810 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000811 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000812
813 // If this redefinition is dead, we need to add a dummy unit live
814 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000815 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000816 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000817 LiveRange(RedefIndex, MO.isEarlyClobber() ?
818 getNextSlot(getNextSlot(RedefIndex)) :
819 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000820
Bill Wendling8e6179f2009-08-22 20:18:03 +0000821 DEBUG({
822 errs() << " RESULT: ";
823 interval.print(errs(), tri_);
824 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000825 } else {
826 // Otherwise, this must be because of phi elimination. If this is the
827 // first redefinition of the vreg that we have seen, go back and change
828 // the live range in the PHI block to be a different value number.
829 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000830 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000831 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000832 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000833 phiJoinCopies.push_back(Killer);
Lang Hames86511252009-09-04 20:41:11 +0000834 MachineInstrIndex Start = getMBBStartIdx(Killer->getParent());
Evan Cheng21731112009-09-12 02:01:07 +0000835 MachineInstrIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000836 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000837 DEBUG({
838 errs() << " Removing [" << Start << "," << End << "] from: ";
839 interval.print(errs(), tri_);
840 errs() << "\n";
841 });
Lang Hamesffd13262009-07-09 03:57:02 +0000842 interval.removeRange(Start, End);
843 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000844 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000845 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
846 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000847 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000848 DEBUG({
849 errs() << " RESULT: ";
850 interval.print(errs(), tri_);
851 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000852
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000853 // Replace the interval with one of a NEW value number. Note that this
854 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000855 LiveRange LR(Start, End,
Lang Hames86511252009-09-04 20:41:11 +0000856 interval.getNextValue(MachineInstrIndex(mbb->getNumber()),
857 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000858 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000859 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000860 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000861 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000862 DEBUG({
863 errs() << " RESULT: ";
864 interval.print(errs(), tri_);
865 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000866 }
867
868 // In the case of PHI elimination, each variable definition is only
869 // live until the end of the block. We've already taken care of the
870 // rest of the live range.
Lang Hames86511252009-09-04 20:41:11 +0000871 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000872 if (MO.isEarlyClobber())
873 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000874
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000875 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000876 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000877 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000878 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000879 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000880 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000881 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000882 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000883 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000884
Lang Hames35f291d2009-09-12 03:34:03 +0000885 MachineInstrIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000886 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000887 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000888 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000889 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000890 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000891 }
892 }
893
Bill Wendling8e6179f2009-08-22 20:18:03 +0000894 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000895}
896
Chris Lattnerf35fef72004-07-23 21:24:19 +0000897void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000898 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000899 MachineInstrIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000900 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000901 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000902 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000903 // A physical register cannot be live across basic block, so its
904 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000905 DEBUG({
906 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000907 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000908 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000909
Lang Hames86511252009-09-04 20:41:11 +0000910 MachineInstrIndex baseIndex = MIIdx;
911 MachineInstrIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000912 // Earlyclobbers move back one.
913 if (MO.isEarlyClobber())
914 start = getUseIndex(MIIdx);
Lang Hames86511252009-09-04 20:41:11 +0000915 MachineInstrIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000916
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000917 // If it is not used after definition, it is considered dead at
918 // the instruction defining it. Hence its interval is:
919 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000920 // For earlyclobbers, the defSlot was pushed back one; the extra
921 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000922 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000923 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000924 if (MO.isEarlyClobber())
925 end = getNextSlot(getNextSlot(start));
926 else
927 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000928 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000929 }
930
931 // If it is not dead on definition, it must be killed by a
932 // subsequent instruction. Hence its interval is:
933 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000934 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000935 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000936 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000937 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000938 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000939 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000940 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000941 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000942 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000943 } else {
944 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
945 if (DefIdx != -1) {
946 if (mi->isRegTiedToUseOperand(DefIdx)) {
947 // Two-address instruction.
948 end = getDefIndex(baseIndex);
949 if (mi->getOperand(DefIdx).isEarlyClobber())
950 end = getUseIndex(baseIndex);
951 } else {
952 // Another instruction redefines the register before it is ever read.
953 // Then the register is essentially dead at the instruction that defines
954 // it. Hence its interval is:
955 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000956 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000957 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000958 }
959 goto exit;
960 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000961 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000962
Lang Hames35f291d2009-09-12 03:34:03 +0000963 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000964 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000965
966 // The only case we should have a dead physreg here without a killing or
967 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000968 // and never used. Another possible case is the implicit use of the
969 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000970 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000971
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000972exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000973 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000974
Evan Cheng24a3cc42007-04-25 07:30:23 +0000975 // Already exists? Extend old live interval.
976 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000977 bool Extend = OldLR != interval.end();
978 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000979 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000980 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000981 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000982 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000983 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000984 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000985 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000986}
987
Chris Lattnerf35fef72004-07-23 21:24:19 +0000988void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
989 MachineBasicBlock::iterator MI,
Lang Hames86511252009-09-04 20:41:11 +0000990 MachineInstrIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000991 MachineOperand& MO,
992 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000993 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000994 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000995 getOrCreateInterval(MO.getReg()));
996 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000997 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000998 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000999 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +00001000 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +00001001 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +00001002 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001003 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +00001004 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001005 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +00001006 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +00001007 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +00001008 // If MI also modifies the sub-register explicitly, avoid processing it
1009 // more than once. Do not pass in TRI here so it checks for exact match.
1010 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +00001011 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001012 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +00001013 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001014}
1015
Evan Chengb371f452007-02-19 21:49:54 +00001016void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames86511252009-09-04 20:41:11 +00001017 MachineInstrIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001018 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001019 DEBUG({
1020 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001021 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001022 });
Evan Chengb371f452007-02-19 21:49:54 +00001023
1024 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1025 // be considered a livein.
1026 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames86511252009-09-04 20:41:11 +00001027 MachineInstrIndex baseIndex = MIIdx;
1028 MachineInstrIndex start = baseIndex;
1029 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001030 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001031 baseIndex = getNextIndex(baseIndex);
Lang Hames86511252009-09-04 20:41:11 +00001032 MachineInstrIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001033 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001034
Evan Chengb371f452007-02-19 21:49:54 +00001035 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001036 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001037 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001038 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001039 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001040 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001041 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001042 // Another instruction redefines the register before it is ever read.
1043 // Then the register is essentially dead at the instruction that defines
1044 // it. Hence its interval is:
1045 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001046 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001047 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001048 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001049 break;
Evan Chengb371f452007-02-19 21:49:54 +00001050 }
1051
Lang Hames35f291d2009-09-12 03:34:03 +00001052 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001053 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001054 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001055 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001056 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001057 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001058 }
Evan Chengb371f452007-02-19 21:49:54 +00001059 }
1060
Evan Cheng75611fb2007-06-27 01:16:36 +00001061 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001062 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001063 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001064 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001065 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001066 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001067 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001068 end = baseIndex;
1069 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001070 }
1071
Lang Hames10382fb2009-06-19 02:17:53 +00001072 VNInfo *vni =
Lang Hames86511252009-09-04 20:41:11 +00001073 interval.getNextValue(MachineInstrIndex(MBB->getNumber()),
1074 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001075 vni->setIsPHIDef(true);
1076 LiveRange LR(start, end, vni);
1077
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001078 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001079 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001080 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001081}
1082
Evan Cheng752195e2009-09-14 21:33:42 +00001083bool
1084LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1085 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001086 SmallVector<MachineInstr*,16> &OtherCopies) {
1087 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001088 unsigned NumIdent = 0;
Evan Cheng752195e2009-09-14 21:33:42 +00001089 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(SrcInt.reg),
1090 re = mri_->reg_end(); ri != re; ++ri) {
1091 MachineOperand &O = ri.getOperand();
1092 if (!O.isDef())
1093 continue;
1094
Evan Cheng752195e2009-09-14 21:33:42 +00001095 MachineInstr *MI = &*ri;
1096 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1097 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001098 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001099 if (SrcReg != DstInt.reg) {
1100 OtherCopies.push_back(MI);
1101 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1102 } else {
1103 IdentCopies.push_back(MI);
1104 ++NumIdent;
1105 }
1106 }
1107
Evan Cheng3f855492009-09-15 06:45:16 +00001108 if (!HaveConflict)
1109 return false; // Let coalescer handle it
1110 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001111}
1112
1113void LiveIntervals::performEarlyCoalescing() {
1114 if (!EarlyCoalescing)
1115 return;
1116
1117 /// Perform early coalescing: eliminate copies which feed into phi joins
1118 /// and whose sources are defined by the phi joins.
1119 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1120 MachineInstr *Join = phiJoinCopies[i];
1121 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1122 break;
1123
1124 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1125 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1126#ifndef NDEBUG
1127 assert(isMove && "PHI join instruction must be a move!");
1128#else
1129 isMove = isMove;
1130#endif
1131
1132 LiveInterval &DstInt = getInterval(PHIDst);
1133 LiveInterval &SrcInt = getInterval(PHISrc);
1134 SmallVector<MachineInstr*, 16> IdentCopies;
1135 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001136 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001137 continue;
1138
1139 DEBUG(errs() << "PHI Join: " << *Join);
1140 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1141 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001142
Evan Cheng3f855492009-09-15 06:45:16 +00001143 // Change the non-identity copies to directly target the phi destination.
1144 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1145 MachineInstr *PHICopy = OtherCopies[i];
1146 DEBUG(errs() << "Moving: " << *PHICopy);
1147
Evan Cheng752195e2009-09-14 21:33:42 +00001148 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1149 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1150 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001151 MachineInstrIndex StartIndex = SLR->start;
Evan Cheng752195e2009-09-14 21:33:42 +00001152 MachineInstrIndex EndIndex = SLR->end;
1153
1154 // Delete val# defined by the now identity copy and add the range from
1155 // beginning of the mbb to the end of the range.
1156 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001157 DEBUG(errs() << " added range [" << StartIndex << ','
1158 << EndIndex << "] to reg" << DstInt.reg << '\n');
1159 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001160 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001161 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1162 VNInfoAllocator);
1163 NewVNI->setHasPHIKill(true);
1164 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1165 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1166 MachineOperand &MO = PHICopy->getOperand(j);
1167 if (!MO.isReg() || MO.getReg() != PHISrc)
1168 continue;
1169 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001170 }
Evan Cheng3f855492009-09-15 06:45:16 +00001171 }
1172
1173 // Now let's eliminate all the would-be identity copies.
1174 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1175 MachineInstr *PHICopy = IdentCopies[i];
1176 DEBUG(errs() << "Coalescing: " << *PHICopy);
1177
1178 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1179 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1180 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
1181 MachineInstrIndex StartIndex = SLR->start;
1182 MachineInstrIndex EndIndex = SLR->end;
1183
1184 // Delete val# defined by the now identity copy and add the range from
1185 // beginning of the mbb to the end of the range.
1186 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001187 RemoveMachineInstrFromMaps(PHICopy);
1188 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001189 DEBUG(errs() << " added range [" << StartIndex << ','
1190 << EndIndex << "] to reg" << DstInt.reg << '\n');
1191 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001192 }
Evan Cheng752195e2009-09-14 21:33:42 +00001193
Evan Cheng3f855492009-09-15 06:45:16 +00001194 // Remove the phi join and update the phi block liveness.
1195 MachineInstrIndex MIIndex = getInstructionIndex(Join);
1196 MachineInstrIndex UseIndex = getUseIndex(MIIndex);
1197 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1198 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1199 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1200 DLR->valno->setCopy(0);
1201 DLR->valno->setIsDefAccurate(false);
1202 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1203 SrcInt.removeRange(SLR->start, SLR->end);
1204 assert(SrcInt.empty());
1205 removeInterval(PHISrc);
1206 RemoveMachineInstrFromMaps(Join);
1207 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001208
1209 ++numCoalescing;
1210 }
1211}
1212
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001213/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001214/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001215/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001216/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001217void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001218 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001219 << "********** Function: "
1220 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001221
1222 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001223 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1224 MBBI != E; ++MBBI) {
1225 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001226 // Track the index of the current machine instr.
Lang Hames86511252009-09-04 20:41:11 +00001227 MachineInstrIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001228 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001229
Chris Lattner428b92e2006-09-15 03:57:23 +00001230 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001231
Dan Gohmancb406c22007-10-03 19:26:29 +00001232 // Create intervals for live-ins to this BB first.
1233 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1234 LE = MBB->livein_end(); LI != LE; ++LI) {
1235 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1236 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001237 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001238 if (!hasInterval(*AS))
1239 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1240 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001241 }
1242
Owen Anderson99500ae2008-09-15 22:00:38 +00001243 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001244 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001245 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001246 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001247
Chris Lattner428b92e2006-09-15 03:57:23 +00001248 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001249 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001250
Evan Cheng438f7bc2006-11-10 08:43:01 +00001251 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001252 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1253 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001254 if (!MO.isReg() || !MO.getReg())
1255 continue;
1256
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001257 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001258 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001259 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001260 else if (MO.isUndef())
1261 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001262 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001263
1264 // Skip over the empty slots after each instruction.
1265 unsigned Slots = MI->getDesc().getNumDefs();
1266 if (Slots == 0)
1267 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001268
1269 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001270 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001271
1272 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001273 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001274 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001275 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001276 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001277 }
Evan Chengd129d732009-07-17 19:43:40 +00001278
1279 // Create empty intervals for registers defined by implicit_def's (except
1280 // for those implicit_def that define values which are liveout of their
1281 // blocks.
1282 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1283 unsigned UndefReg = UndefUses[i];
1284 (void)getOrCreateInterval(UndefReg);
1285 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001286}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001287
Lang Hames86511252009-09-04 20:41:11 +00001288bool LiveIntervals::findLiveInMBBs(
1289 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001290 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001291 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001292 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001293
1294 bool ResVal = false;
1295 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001296 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001297 break;
1298 MBBs.push_back(I->second);
1299 ResVal = true;
1300 ++I;
1301 }
1302 return ResVal;
1303}
1304
Lang Hames86511252009-09-04 20:41:11 +00001305bool LiveIntervals::findReachableMBBs(
1306 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001307 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1308 std::vector<IdxMBBPair>::const_iterator I =
1309 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1310
1311 bool ResVal = false;
1312 while (I != Idx2MBBMap.end()) {
1313 if (I->first > End)
1314 break;
1315 MachineBasicBlock *MBB = I->second;
1316 if (getMBBEndIdx(MBB) > End)
1317 break;
1318 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1319 SE = MBB->succ_end(); SI != SE; ++SI)
1320 MBBs.push_back(*SI);
1321 ResVal = true;
1322 ++I;
1323 }
1324 return ResVal;
1325}
1326
Owen Anderson03857b22008-08-13 21:49:13 +00001327LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001328 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001329 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001330}
Evan Chengf2fbca62007-11-12 06:35:08 +00001331
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001332/// dupInterval - Duplicate a live interval. The caller is responsible for
1333/// managing the allocated memory.
1334LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1335 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001336 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001337 return NewLI;
1338}
1339
Evan Chengc8d044e2008-02-15 18:24:29 +00001340/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1341/// copy field and returns the source register that defines it.
1342unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001343 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001344 return 0;
1345
Lang Hames52c1afc2009-08-10 23:43:28 +00001346 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001347 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001348 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001349 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001350 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001351 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001352 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1353 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1354 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001355
Evan Cheng04ee5a12009-01-20 19:12:24 +00001356 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001357 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001358 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001359 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001360 return 0;
1361}
Evan Chengf2fbca62007-11-12 06:35:08 +00001362
1363//===----------------------------------------------------------------------===//
1364// Register allocator hooks.
1365//
1366
Evan Chengd70dbb52008-02-22 09:24:50 +00001367/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1368/// allow one) virtual register operand, then its uses are implicitly using
1369/// the register. Returns the virtual register.
1370unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1371 MachineInstr *MI) const {
1372 unsigned RegOp = 0;
1373 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1374 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001375 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001376 continue;
1377 unsigned Reg = MO.getReg();
1378 if (Reg == 0 || Reg == li.reg)
1379 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001380
1381 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1382 !allocatableRegs_[Reg])
1383 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001384 // FIXME: For now, only remat MI with at most one register operand.
1385 assert(!RegOp &&
1386 "Can't rematerialize instruction with multiple register operand!");
1387 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001388#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001389 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001390#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001391 }
1392 return RegOp;
1393}
1394
1395/// isValNoAvailableAt - Return true if the val# of the specified interval
1396/// which reaches the given instruction also reaches the specified use index.
1397bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +00001398 MachineInstrIndex UseIdx) const {
1399 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001400 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1401 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1402 return UI != li.end() && UI->valno == ValNo;
1403}
1404
Evan Chengf2fbca62007-11-12 06:35:08 +00001405/// isReMaterializable - Returns true if the definition MI of the specified
1406/// val# of the specified interval is re-materializable.
1407bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001408 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001409 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001410 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001411 if (DisableReMat)
1412 return false;
1413
Evan Cheng20ccded2008-03-15 00:19:36 +00001414 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001415 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001416
1417 int FrameIdx = 0;
1418 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001419 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001420 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1421 // this but remember this is not safe to fold into a two-address
1422 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001423 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001424 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001425
Dan Gohman6d69ba82008-07-25 00:02:30 +00001426 // If the target-specific rules don't identify an instruction as
1427 // being trivially rematerializable, use some target-independent
1428 // rules.
1429 if (!MI->getDesc().isRematerializable() ||
1430 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001431 if (!EnableAggressiveRemat)
1432 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001433
Dan Gohman0471a792008-07-28 18:43:51 +00001434 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001435 // we can't analyze it.
1436 const TargetInstrDesc &TID = MI->getDesc();
1437 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1438 return false;
1439
1440 // Avoid instructions obviously unsafe for remat.
1441 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1442 return false;
1443
1444 // If the instruction accesses memory and the memory could be non-constant,
1445 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001446 for (std::list<MachineMemOperand>::const_iterator
1447 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001448 const MachineMemOperand &MMO = *I;
1449 if (MMO.isVolatile() || MMO.isStore())
1450 return false;
1451 const Value *V = MMO.getValue();
1452 if (!V)
1453 return false;
1454 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1455 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001456 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001457 } else if (!aa_->pointsToConstantMemory(V))
1458 return false;
1459 }
1460
1461 // If any of the registers accessed are non-constant, conservatively assume
1462 // the instruction is not rematerializable.
1463 unsigned ImpUse = 0;
1464 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1465 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001466 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001467 unsigned Reg = MO.getReg();
1468 if (Reg == 0)
1469 continue;
1470 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1471 return false;
1472
1473 // Only allow one def, and that in the first operand.
1474 if (MO.isDef() != (i == 0))
1475 return false;
1476
1477 // Only allow constant-valued registers.
1478 bool IsLiveIn = mri_->isLiveIn(Reg);
1479 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1480 E = mri_->def_end();
1481
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001482 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001483 if (MO.isDef() && (next(I) != E || IsLiveIn))
1484 return false;
1485
1486 if (MO.isUse()) {
1487 // Only allow one use other register use, as that's all the
1488 // remat mechanisms support currently.
1489 if (Reg != li.reg) {
1490 if (ImpUse == 0)
1491 ImpUse = Reg;
1492 else if (Reg != ImpUse)
1493 return false;
1494 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001495 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001496 if (I != E && (next(I) != E || IsLiveIn))
1497 return false;
1498 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001499 }
1500 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001501 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001502
Dan Gohman6d69ba82008-07-25 00:02:30 +00001503 unsigned ImpUse = getReMatImplicitUse(li, MI);
1504 if (ImpUse) {
1505 const LiveInterval &ImpLi = getInterval(ImpUse);
1506 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1507 re = mri_->use_end(); ri != re; ++ri) {
1508 MachineInstr *UseMI = &*ri;
Lang Hames86511252009-09-04 20:41:11 +00001509 MachineInstrIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001510 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1511 continue;
1512 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1513 return false;
1514 }
Evan Chengdc377862008-09-30 15:44:16 +00001515
1516 // If a register operand of the re-materialized instruction is going to
1517 // be spilled next, then it's not legal to re-materialize this instruction.
1518 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1519 if (ImpUse == SpillIs[i]->reg)
1520 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001521 }
1522 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001523}
1524
Evan Cheng06587492008-10-24 02:05:00 +00001525/// isReMaterializable - Returns true if the definition MI of the specified
1526/// val# of the specified interval is re-materializable.
1527bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1528 const VNInfo *ValNo, MachineInstr *MI) {
1529 SmallVector<LiveInterval*, 4> Dummy1;
1530 bool Dummy2;
1531 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1532}
1533
Evan Cheng5ef3a042007-12-06 00:01:56 +00001534/// isReMaterializable - Returns true if every definition of MI of every
1535/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001536bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1537 SmallVectorImpl<LiveInterval*> &SpillIs,
1538 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001539 isLoad = false;
1540 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1541 i != e; ++i) {
1542 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001543 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001544 continue; // Dead val#.
1545 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001546 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001547 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001548 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001549 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001550 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001551 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001552 return false;
1553 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001554 }
1555 return true;
1556}
1557
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001558/// FilterFoldedOps - Filter out two-address use operands. Return
1559/// true if it finds any issue with the operands that ought to prevent
1560/// folding.
1561static bool FilterFoldedOps(MachineInstr *MI,
1562 SmallVector<unsigned, 2> &Ops,
1563 unsigned &MRInfo,
1564 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001565 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001566 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1567 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001568 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001569 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001570 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001571 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001572 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001573 MRInfo |= (unsigned)VirtRegMap::isMod;
1574 else {
1575 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001576 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001577 MRInfo = VirtRegMap::isModRef;
1578 continue;
1579 }
1580 MRInfo |= (unsigned)VirtRegMap::isRef;
1581 }
1582 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001583 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001584 return false;
1585}
1586
1587
1588/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1589/// slot / to reg or any rematerialized load into ith operand of specified
1590/// MI. If it is successul, MI is updated with the newly created MI and
1591/// returns true.
1592bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1593 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames86511252009-09-04 20:41:11 +00001594 MachineInstrIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001595 SmallVector<unsigned, 2> &Ops,
1596 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001597 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001598 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001599 RemoveMachineInstrFromMaps(MI);
1600 vrm.RemoveMachineInstrFromMaps(MI);
1601 MI->eraseFromParent();
1602 ++numFolds;
1603 return true;
1604 }
1605
1606 // Filter the list of operand indexes that are to be folded. Abort if
1607 // any operand will prevent folding.
1608 unsigned MRInfo = 0;
1609 SmallVector<unsigned, 2> FoldOps;
1610 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1611 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001612
Evan Cheng427f4c12008-03-31 23:19:51 +00001613 // The only time it's safe to fold into a two address instruction is when
1614 // it's folding reload and spill from / into a spill stack slot.
1615 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001616 return false;
1617
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001618 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1619 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001620 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001621 // Remember this instruction uses the spill slot.
1622 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1623
Evan Chengf2fbca62007-11-12 06:35:08 +00001624 // Attempt to fold the memory reference into the instruction. If
1625 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001626 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001627 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001628 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001629 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001630 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001631 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001632 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001633 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001634 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001635 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001636 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001637 return true;
1638 }
1639 return false;
1640}
1641
Evan Cheng018f9b02007-12-05 03:22:34 +00001642/// canFoldMemoryOperand - Returns true if the specified load / store
1643/// folding is possible.
1644bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001645 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001646 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001647 // Filter the list of operand indexes that are to be folded. Abort if
1648 // any operand will prevent folding.
1649 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001650 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001651 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1652 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001653
Evan Cheng3c75ba82008-04-01 21:37:32 +00001654 // It's only legal to remat for a use, not a def.
1655 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001656 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001657
Evan Chengd70dbb52008-02-22 09:24:50 +00001658 return tii_->canFoldMemoryOperand(MI, FoldOps);
1659}
1660
Evan Cheng81a03822007-11-17 00:40:40 +00001661bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1662 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1663 for (LiveInterval::Ranges::const_iterator
1664 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1665 std::vector<IdxMBBPair>::const_iterator II =
1666 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1667 if (II == Idx2MBBMap.end())
1668 continue;
1669 if (I->end > II->first) // crossing a MBB.
1670 return false;
1671 MBBs.insert(II->second);
1672 if (MBBs.size() > 1)
1673 return false;
1674 }
1675 return true;
1676}
1677
Evan Chengd70dbb52008-02-22 09:24:50 +00001678/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1679/// interval on to-be re-materialized operands of MI) with new register.
1680void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1681 MachineInstr *MI, unsigned NewVReg,
1682 VirtRegMap &vrm) {
1683 // There is an implicit use. That means one of the other operand is
1684 // being remat'ed and the remat'ed instruction has li.reg as an
1685 // use operand. Make sure we rewrite that as well.
1686 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1687 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001688 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001689 continue;
1690 unsigned Reg = MO.getReg();
1691 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1692 continue;
1693 if (!vrm.isReMaterialized(Reg))
1694 continue;
1695 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001696 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1697 if (UseMO)
1698 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001699 }
1700}
1701
Evan Chengf2fbca62007-11-12 06:35:08 +00001702/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1703/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001704bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001705rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001706 bool TrySplit, MachineInstrIndex index, MachineInstrIndex end,
1707 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001708 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001709 unsigned Slot, int LdSlot,
1710 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001711 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001712 const TargetRegisterClass* rc,
1713 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001714 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001715 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001716 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001717 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001718 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001719 RestartInstruction:
1720 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1721 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001722 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001723 continue;
1724 unsigned Reg = mop.getReg();
1725 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001726 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001727 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001728 if (Reg != li.reg)
1729 continue;
1730
1731 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001732 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001733 int FoldSlot = Slot;
1734 if (DefIsReMat) {
1735 // If this is the rematerializable definition MI itself and
1736 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001737 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001738 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1739 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001740 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001741 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001742 MI->eraseFromParent();
1743 break;
1744 }
1745
1746 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001747 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001748 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001749 if (isLoad) {
1750 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1751 FoldSS = isLoadSS;
1752 FoldSlot = LdSlot;
1753 }
1754 }
1755
Evan Chengf2fbca62007-11-12 06:35:08 +00001756 // Scan all of the operands of this instruction rewriting operands
1757 // to use NewVReg instead of li.reg as appropriate. We do this for
1758 // two reasons:
1759 //
1760 // 1. If the instr reads the same spilled vreg multiple times, we
1761 // want to reuse the NewVReg.
1762 // 2. If the instr is a two-addr instruction, we are required to
1763 // keep the src/dst regs pinned.
1764 //
1765 // Keep track of whether we replace a use and/or def so that we can
1766 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001767
Evan Cheng81a03822007-11-17 00:40:40 +00001768 HasUse = mop.isUse();
1769 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001770 SmallVector<unsigned, 2> Ops;
1771 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001772 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001773 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001774 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001775 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001776 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001777 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001778 continue;
1779 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001780 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001781 if (!MOj.isUndef()) {
1782 HasUse |= MOj.isUse();
1783 HasDef |= MOj.isDef();
1784 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001785 }
1786 }
1787
David Greene26b86a02008-10-27 17:38:59 +00001788 // Create a new virtual register for the spill interval.
1789 // Create the new register now so we can map the fold instruction
1790 // to the new register so when it is unfolded we get the correct
1791 // answer.
1792 bool CreatedNewVReg = false;
1793 if (NewVReg == 0) {
1794 NewVReg = mri_->createVirtualRegister(rc);
1795 vrm.grow();
1796 CreatedNewVReg = true;
1797 }
1798
Evan Cheng9c3c2212008-06-06 07:54:39 +00001799 if (!TryFold)
1800 CanFold = false;
1801 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001802 // Do not fold load / store here if we are splitting. We'll find an
1803 // optimal point to insert a load / store later.
1804 if (!TrySplit) {
1805 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001806 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001807 // Folding the load/store can completely change the instruction in
1808 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001809
1810 if (FoldSS) {
1811 // We need to give the new vreg the same stack slot as the
1812 // spilled interval.
1813 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1814 }
1815
Evan Cheng018f9b02007-12-05 03:22:34 +00001816 HasUse = false;
1817 HasDef = false;
1818 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001819 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001820 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001821 goto RestartInstruction;
1822 }
1823 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001824 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001825 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001826 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001827 }
Evan Chengcddbb832007-11-30 21:23:43 +00001828
Evan Chengcddbb832007-11-30 21:23:43 +00001829 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001830 if (mop.isImplicit())
1831 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001832
1833 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001834 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1835 MachineOperand &mopj = MI->getOperand(Ops[j]);
1836 mopj.setReg(NewVReg);
1837 if (mopj.isImplicit())
1838 rewriteImplicitOps(li, MI, NewVReg, vrm);
1839 }
Evan Chengcddbb832007-11-30 21:23:43 +00001840
Evan Cheng81a03822007-11-17 00:40:40 +00001841 if (CreatedNewVReg) {
1842 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001843 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001844 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001845 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001846 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001847 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001848 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001849 }
1850 if (!CanDelete || (HasUse && HasDef)) {
1851 // If this is a two-addr instruction then its use operands are
1852 // rematerializable but its def is not. It should be assigned a
1853 // stack slot.
1854 vrm.assignVirt2StackSlot(NewVReg, Slot);
1855 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001856 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001857 vrm.assignVirt2StackSlot(NewVReg, Slot);
1858 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001859 } else if (HasUse && HasDef &&
1860 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1861 // If this interval hasn't been assigned a stack slot (because earlier
1862 // def is a deleted remat def), do it now.
1863 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1864 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001865 }
1866
Evan Cheng313d4b82008-02-23 00:33:04 +00001867 // Re-matting an instruction with virtual register use. Add the
1868 // register as an implicit use on the use MI.
1869 if (DefIsReMat && ImpUse)
1870 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1871
Evan Cheng5b69eba2009-04-21 22:46:52 +00001872 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001873 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001874 if (CreatedNewVReg) {
1875 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001876 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001877 if (TrySplit)
1878 vrm.setIsSplitFromReg(NewVReg, li.reg);
1879 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001880
1881 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001882 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001883 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hames86511252009-09-04 20:41:11 +00001884 nI.getNextValue(MachineInstrIndex(), 0, false,
1885 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001886 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001887 nI.addRange(LR);
1888 } else {
1889 // Extend the split live interval to this def / use.
Lang Hames35f291d2009-09-12 03:34:03 +00001890 MachineInstrIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001891 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1892 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001893 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001894 nI.addRange(LR);
1895 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001896 }
1897 if (HasDef) {
1898 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00001899 nI.getNextValue(MachineInstrIndex(), 0, false,
1900 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001901 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001902 nI.addRange(LR);
1903 }
Evan Cheng81a03822007-11-17 00:40:40 +00001904
Bill Wendling8e6179f2009-08-22 20:18:03 +00001905 DEBUG({
1906 errs() << "\t\t\t\tAdded new interval: ";
1907 nI.print(errs(), tri_);
1908 errs() << '\n';
1909 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001910 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001911 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001912}
Evan Cheng81a03822007-11-17 00:40:40 +00001913bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001914 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001915 MachineBasicBlock *MBB,
1916 MachineInstrIndex Idx) const {
1917 MachineInstrIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001918 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001919 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001920 continue;
1921
Lang Hames86511252009-09-04 20:41:11 +00001922 MachineInstrIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001923 if (KillIdx > Idx && KillIdx < End)
1924 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001925 }
1926 return false;
1927}
1928
Evan Cheng063284c2008-02-21 00:34:19 +00001929/// RewriteInfo - Keep track of machine instrs that will be rewritten
1930/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001931namespace {
1932 struct RewriteInfo {
Lang Hames86511252009-09-04 20:41:11 +00001933 MachineInstrIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001934 MachineInstr *MI;
1935 bool HasUse;
1936 bool HasDef;
Lang Hames86511252009-09-04 20:41:11 +00001937 RewriteInfo(MachineInstrIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001938 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1939 };
Evan Cheng063284c2008-02-21 00:34:19 +00001940
Dan Gohman844731a2008-05-13 00:00:25 +00001941 struct RewriteInfoCompare {
1942 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1943 return LHS.Index < RHS.Index;
1944 }
1945 };
1946}
Evan Cheng063284c2008-02-21 00:34:19 +00001947
Evan Chengf2fbca62007-11-12 06:35:08 +00001948void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001949rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001950 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001951 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001952 unsigned Slot, int LdSlot,
1953 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001954 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001955 const TargetRegisterClass* rc,
1956 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001957 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001958 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001959 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001960 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001961 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1962 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001963 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001964 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001965 unsigned NewVReg = 0;
Lang Hames86511252009-09-04 20:41:11 +00001966 MachineInstrIndex start = getBaseIndex(I->start);
Lang Hames35f291d2009-09-12 03:34:03 +00001967 MachineInstrIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001968
Evan Cheng063284c2008-02-21 00:34:19 +00001969 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001970 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001971 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001972 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1973 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001974 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001975 MachineOperand &O = ri.getOperand();
1976 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001977 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames86511252009-09-04 20:41:11 +00001978 MachineInstrIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001979 if (index < start || index >= end)
1980 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001981
1982 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001983 // Must be defined by an implicit def. It should not be spilled. Note,
1984 // this is for correctness reason. e.g.
1985 // 8 %reg1024<def> = IMPLICIT_DEF
1986 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1987 // The live range [12, 14) are not part of the r1024 live interval since
1988 // it's defined by an implicit def. It will not conflicts with live
1989 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001990 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001991 // the INSERT_SUBREG and both target registers that would overlap.
1992 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001993 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1994 }
1995 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1996
Evan Cheng313d4b82008-02-23 00:33:04 +00001997 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001998 // Now rewrite the defs and uses.
1999 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
2000 RewriteInfo &rwi = RewriteMIs[i];
2001 ++i;
Lang Hames86511252009-09-04 20:41:11 +00002002 MachineInstrIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00002003 bool MIHasUse = rwi.HasUse;
2004 bool MIHasDef = rwi.HasDef;
2005 MachineInstr *MI = rwi.MI;
2006 // If MI def and/or use the same register multiple times, then there
2007 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00002008 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002009 while (i != e && RewriteMIs[i].MI == MI) {
2010 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00002011 bool isUse = RewriteMIs[i].HasUse;
2012 if (isUse) ++NumUses;
2013 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002014 MIHasDef |= RewriteMIs[i].HasDef;
2015 ++i;
2016 }
Evan Cheng81a03822007-11-17 00:40:40 +00002017 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00002018
Evan Cheng0a891ed2008-05-23 23:00:04 +00002019 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00002020 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002021 // register interval's spill weight to HUGE_VALF to prevent it from
2022 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00002023 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002024 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00002025 }
2026
Evan Cheng063284c2008-02-21 00:34:19 +00002027 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00002028 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00002029 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00002030 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002031 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00002032 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002033 // One common case:
2034 // x = use
2035 // ...
2036 // ...
2037 // def = ...
2038 // = use
2039 // It's better to start a new interval to avoid artifically
2040 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002041 if (MIHasDef && !MIHasUse) {
2042 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00002043 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002044 }
2045 }
Evan Chengcada2452007-11-28 01:28:46 +00002046 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002047
2048 bool IsNew = ThisVReg == 0;
2049 if (IsNew) {
2050 // This ends the previous live interval. If all of its def / use
2051 // can be folded, give it a low spill weight.
2052 if (NewVReg && TrySplit && AllCanFold) {
2053 LiveInterval &nI = getOrCreateInterval(NewVReg);
2054 nI.weight /= 10.0F;
2055 }
2056 AllCanFold = true;
2057 }
2058 NewVReg = ThisVReg;
2059
Evan Cheng81a03822007-11-17 00:40:40 +00002060 bool HasDef = false;
2061 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00002062 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00002063 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
2064 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
2065 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00002066 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002067 if (!HasDef && !HasUse)
2068 continue;
2069
Evan Cheng018f9b02007-12-05 03:22:34 +00002070 AllCanFold &= CanFold;
2071
Evan Cheng81a03822007-11-17 00:40:40 +00002072 // Update weight of spill interval.
2073 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00002074 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00002075 // The spill weight is now infinity as it cannot be spilled again.
2076 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002077 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00002078 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002079
2080 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00002081 if (HasDef) {
2082 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002083 bool HasKill = false;
2084 if (!HasUse)
2085 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
2086 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002087 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00002088 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002089 if (VNI)
2090 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2091 }
Owen Anderson28998312008-08-13 22:28:50 +00002092 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002093 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002094 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002095 if (SII == SpillIdxes.end()) {
2096 std::vector<SRInfo> S;
2097 S.push_back(SRInfo(index, NewVReg, true));
2098 SpillIdxes.insert(std::make_pair(MBBId, S));
2099 } else if (SII->second.back().vreg != NewVReg) {
2100 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002101 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002102 // If there is an earlier def and this is a two-address
2103 // instruction, then it's not possible to fold the store (which
2104 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002105 SRInfo &Info = SII->second.back();
2106 Info.index = index;
2107 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002108 }
2109 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002110 } else if (SII != SpillIdxes.end() &&
2111 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002112 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002113 // There is an earlier def that's not killed (must be two-address).
2114 // The spill is no longer needed.
2115 SII->second.pop_back();
2116 if (SII->second.empty()) {
2117 SpillIdxes.erase(MBBId);
2118 SpillMBBs.reset(MBBId);
2119 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002120 }
2121 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002122 }
2123
2124 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002125 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002126 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002127 if (SII != SpillIdxes.end() &&
2128 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002129 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002130 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002131 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002132 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002133 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002134 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002135 // If we are splitting live intervals, only fold if it's the first
2136 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002137 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002138 else if (IsNew) {
2139 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002140 if (RII == RestoreIdxes.end()) {
2141 std::vector<SRInfo> Infos;
2142 Infos.push_back(SRInfo(index, NewVReg, true));
2143 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2144 } else {
2145 RII->second.push_back(SRInfo(index, NewVReg, true));
2146 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002147 RestoreMBBs.set(MBBId);
2148 }
2149 }
2150
2151 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002152 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002153 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002154 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002155
2156 if (NewVReg && TrySplit && AllCanFold) {
2157 // If all of its def / use can be folded, give it a low spill weight.
2158 LiveInterval &nI = getOrCreateInterval(NewVReg);
2159 nI.weight /= 10.0F;
2160 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002161}
2162
Lang Hames86511252009-09-04 20:41:11 +00002163bool LiveIntervals::alsoFoldARestore(int Id, MachineInstrIndex index,
2164 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002165 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002166 if (!RestoreMBBs[Id])
2167 return false;
2168 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2169 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2170 if (Restores[i].index == index &&
2171 Restores[i].vreg == vr &&
2172 Restores[i].canFold)
2173 return true;
2174 return false;
2175}
2176
Lang Hames86511252009-09-04 20:41:11 +00002177void LiveIntervals::eraseRestoreInfo(int Id, MachineInstrIndex index,
2178 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002179 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002180 if (!RestoreMBBs[Id])
2181 return;
2182 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2183 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2184 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames86511252009-09-04 20:41:11 +00002185 Restores[i].index = MachineInstrIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002186}
Evan Cheng81a03822007-11-17 00:40:40 +00002187
Evan Cheng4cce6b42008-04-11 17:53:36 +00002188/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2189/// spilled and create empty intervals for their uses.
2190void
2191LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2192 const TargetRegisterClass* rc,
2193 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002194 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2195 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002196 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002197 MachineInstr *MI = &*ri;
2198 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002199 if (O.isDef()) {
2200 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2201 "Register def was not rewritten?");
2202 RemoveMachineInstrFromMaps(MI);
2203 vrm.RemoveMachineInstrFromMaps(MI);
2204 MI->eraseFromParent();
2205 } else {
2206 // This must be an use of an implicit_def so it's not part of the live
2207 // interval. Create a new empty live interval for it.
2208 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2209 unsigned NewVReg = mri_->createVirtualRegister(rc);
2210 vrm.grow();
2211 vrm.setIsImplicitlyDefined(NewVReg);
2212 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2213 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2214 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002215 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002216 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002217 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002218 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002219 }
2220 }
Evan Cheng419852c2008-04-03 16:39:43 +00002221 }
2222}
2223
Evan Chengf2fbca62007-11-12 06:35:08 +00002224std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002225addIntervalsForSpillsFast(const LiveInterval &li,
2226 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002227 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002228 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002229
2230 std::vector<LiveInterval*> added;
2231
2232 assert(li.weight != HUGE_VALF &&
2233 "attempt to spill already spilled interval!");
2234
Bill Wendling8e6179f2009-08-22 20:18:03 +00002235 DEBUG({
2236 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2237 li.dump();
2238 errs() << '\n';
2239 });
Owen Andersond6664312008-08-18 18:05:32 +00002240
2241 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2242
Owen Andersona41e47a2008-08-19 22:12:11 +00002243 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2244 while (RI != mri_->reg_end()) {
2245 MachineInstr* MI = &*RI;
2246
2247 SmallVector<unsigned, 2> Indices;
2248 bool HasUse = false;
2249 bool HasDef = false;
2250
2251 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2252 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002253 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002254
2255 HasUse |= MI->getOperand(i).isUse();
2256 HasDef |= MI->getOperand(i).isDef();
2257
2258 Indices.push_back(i);
2259 }
2260
2261 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2262 Indices, true, slot, li.reg)) {
2263 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002264 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002265 vrm.assignVirt2StackSlot(NewVReg, slot);
2266
Owen Andersona41e47a2008-08-19 22:12:11 +00002267 // create a new register for this spill
2268 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002269
Owen Andersona41e47a2008-08-19 22:12:11 +00002270 // the spill weight is now infinity as it
2271 // cannot be spilled again
2272 nI.weight = HUGE_VALF;
2273
2274 // Rewrite register operands to use the new vreg.
2275 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2276 E = Indices.end(); I != E; ++I) {
2277 MI->getOperand(*I).setReg(NewVReg);
2278
2279 if (MI->getOperand(*I).isUse())
2280 MI->getOperand(*I).setIsKill(true);
2281 }
2282
2283 // Fill in the new live interval.
Lang Hames86511252009-09-04 20:41:11 +00002284 MachineInstrIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002285 if (HasUse) {
2286 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002287 nI.getNextValue(MachineInstrIndex(), 0, false,
2288 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002289 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002290 nI.addRange(LR);
2291 vrm.addRestorePoint(NewVReg, MI);
2292 }
2293 if (HasDef) {
2294 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002295 nI.getNextValue(MachineInstrIndex(), 0, false,
2296 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002297 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002298 nI.addRange(LR);
2299 vrm.addSpillPoint(NewVReg, true, MI);
2300 }
2301
Owen Anderson17197312008-08-18 23:41:04 +00002302 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002303
Bill Wendling8e6179f2009-08-22 20:18:03 +00002304 DEBUG({
2305 errs() << "\t\t\t\tadded new interval: ";
2306 nI.dump();
2307 errs() << '\n';
2308 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002309 }
Owen Anderson9a032932008-08-18 21:20:32 +00002310
Owen Anderson9a032932008-08-18 21:20:32 +00002311
Owen Andersona41e47a2008-08-19 22:12:11 +00002312 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002313 }
Owen Andersond6664312008-08-18 18:05:32 +00002314
2315 return added;
2316}
2317
2318std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002319addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002320 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002321 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002322
2323 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002324 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002325
Evan Chengf2fbca62007-11-12 06:35:08 +00002326 assert(li.weight != HUGE_VALF &&
2327 "attempt to spill already spilled interval!");
2328
Bill Wendling8e6179f2009-08-22 20:18:03 +00002329 DEBUG({
2330 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2331 li.print(errs(), tri_);
2332 errs() << '\n';
2333 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002334
Evan Cheng72eeb942008-12-05 17:00:16 +00002335 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002336 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002337 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002338 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002339 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2340 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002341 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002342 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002343
2344 unsigned NumValNums = li.getNumValNums();
2345 SmallVector<MachineInstr*, 4> ReMatDefs;
2346 ReMatDefs.resize(NumValNums, NULL);
2347 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2348 ReMatOrigDefs.resize(NumValNums, NULL);
2349 SmallVector<int, 4> ReMatIds;
2350 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2351 BitVector ReMatDelete(NumValNums);
2352 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2353
Evan Cheng81a03822007-11-17 00:40:40 +00002354 // Spilling a split live interval. It cannot be split any further. Also,
2355 // it's also guaranteed to be a single val# / range interval.
2356 if (vrm.getPreSplitReg(li.reg)) {
2357 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002358 // Unset the split kill marker on the last use.
Lang Hames86511252009-09-04 20:41:11 +00002359 MachineInstrIndex KillIdx = vrm.getKillPoint(li.reg);
2360 if (KillIdx != MachineInstrIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002361 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2362 assert(KillMI && "Last use disappeared?");
2363 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2364 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002365 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002366 }
Evan Chengadf85902007-12-05 09:51:10 +00002367 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002368 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2369 Slot = vrm.getStackSlot(li.reg);
2370 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2371 MachineInstr *ReMatDefMI = DefIsReMat ?
2372 vrm.getReMaterializedMI(li.reg) : NULL;
2373 int LdSlot = 0;
2374 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2375 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002376 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002377 bool IsFirstRange = true;
2378 for (LiveInterval::Ranges::const_iterator
2379 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2380 // If this is a split live interval with multiple ranges, it means there
2381 // are two-address instructions that re-defined the value. Only the
2382 // first def can be rematerialized!
2383 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002384 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002385 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2386 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002387 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002388 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002389 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002390 } else {
2391 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2392 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002393 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002394 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002395 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002396 }
2397 IsFirstRange = false;
2398 }
Evan Cheng419852c2008-04-03 16:39:43 +00002399
Evan Cheng4cce6b42008-04-11 17:53:36 +00002400 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002401 return NewLIs;
2402 }
2403
Evan Cheng752195e2009-09-14 21:33:42 +00002404 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002405 if (TrySplit)
2406 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002407 bool NeedStackSlot = false;
2408 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2409 i != e; ++i) {
2410 const VNInfo *VNI = *i;
2411 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002412 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002413 continue; // Dead val#.
2414 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002415 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2416 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002417 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002418 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002419 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002420 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002421 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002422 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002423 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002424 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002425
2426 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002427 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002428 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002429 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002430 CanDelete = false;
2431 // Need a stack slot if there is any live range where uses cannot be
2432 // rematerialized.
2433 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002434 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002435 if (CanDelete)
2436 ReMatDelete.set(VN);
2437 } else {
2438 // Need a stack slot if there is any live range where uses cannot be
2439 // rematerialized.
2440 NeedStackSlot = true;
2441 }
2442 }
2443
2444 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002445 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2446 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2447 Slot = vrm.assignVirt2StackSlot(li.reg);
2448
2449 // This case only occurs when the prealloc splitter has already assigned
2450 // a stack slot to this vreg.
2451 else
2452 Slot = vrm.getStackSlot(li.reg);
2453 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002454
2455 // Create new intervals and rewrite defs and uses.
2456 for (LiveInterval::Ranges::const_iterator
2457 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002458 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2459 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2460 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002461 bool CanDelete = ReMatDelete[I->valno->id];
2462 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002463 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002464 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002465 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002466 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002467 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002468 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002469 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002470 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002471 }
2472
Evan Cheng0cbb1162007-11-29 01:06:25 +00002473 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002474 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002475 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002476 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002477 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002478
Evan Chengb50bb8c2007-12-05 08:16:32 +00002479 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002480 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002481 if (NeedStackSlot) {
2482 int Id = SpillMBBs.find_first();
2483 while (Id != -1) {
2484 std::vector<SRInfo> &spills = SpillIdxes[Id];
2485 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002486 MachineInstrIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002487 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002488 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002489 bool isReMat = vrm.isReMaterialized(VReg);
2490 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002491 bool CanFold = false;
2492 bool FoundUse = false;
2493 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002494 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002495 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002496 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2497 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002498 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002499 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002500
2501 Ops.push_back(j);
2502 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002503 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002504 if (isReMat ||
2505 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2506 RestoreMBBs, RestoreIdxes))) {
2507 // MI has two-address uses of the same register. If the use
2508 // isn't the first and only use in the BB, then we can't fold
2509 // it. FIXME: Move this to rewriteInstructionsForSpills.
2510 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002511 break;
2512 }
Evan Chengaee4af62007-12-02 08:30:39 +00002513 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002514 }
2515 }
2516 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002517 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002518 if (CanFold && !Ops.empty()) {
2519 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002520 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002521 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002522 // Also folded uses, do not issue a load.
2523 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002524 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002525 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002526 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002527 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002528 }
2529
Evan Cheng7e073ba2008-04-09 20:57:25 +00002530 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002531 if (!Folded) {
2532 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2533 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002534 if (!MI->registerDefIsDead(nI.reg))
2535 // No need to spill a dead def.
2536 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002537 if (isKill)
2538 AddedKill.insert(&nI);
2539 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002540 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002541 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002542 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002543 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002544
Evan Cheng1953d0c2007-11-29 10:12:14 +00002545 int Id = RestoreMBBs.find_first();
2546 while (Id != -1) {
2547 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2548 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002549 MachineInstrIndex index = restores[i].index;
2550 if (index == MachineInstrIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002551 continue;
2552 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002553 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002554 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002555 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002556 bool CanFold = false;
2557 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002558 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002559 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002560 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2561 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002562 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002563 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002564
Evan Cheng0cbb1162007-11-29 01:06:25 +00002565 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002566 // If this restore were to be folded, it would have been folded
2567 // already.
2568 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002569 break;
2570 }
Evan Chengaee4af62007-12-02 08:30:39 +00002571 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002572 }
2573 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002574
2575 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002576 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002577 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002578 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002579 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2580 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002581 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2582 int LdSlot = 0;
2583 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2584 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002585 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002586 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2587 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002588 if (!Folded) {
2589 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2590 if (ImpUse) {
2591 // Re-matting an instruction with virtual register use. Add the
2592 // register as an implicit use on the use MI and update the register
2593 // interval's spill weight to HUGE_VALF to prevent it from being
2594 // spilled.
2595 LiveInterval &ImpLi = getInterval(ImpUse);
2596 ImpLi.weight = HUGE_VALF;
2597 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2598 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002599 }
Evan Chengaee4af62007-12-02 08:30:39 +00002600 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002601 }
2602 // If folding is not possible / failed, then tell the spiller to issue a
2603 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002604 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002605 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002606 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002607 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002608 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002609 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002610 }
2611
Evan Chengb50bb8c2007-12-05 08:16:32 +00002612 // Finalize intervals: add kills, finalize spill weights, and filter out
2613 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002614 std::vector<LiveInterval*> RetNewLIs;
2615 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2616 LiveInterval *LI = NewLIs[i];
2617 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002618 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002619 if (!AddedKill.count(LI)) {
2620 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames86511252009-09-04 20:41:11 +00002621 MachineInstrIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002622 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002623 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002624 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002625 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002626 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002627 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002628 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002629 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002630 RetNewLIs.push_back(LI);
2631 }
2632 }
Evan Cheng81a03822007-11-17 00:40:40 +00002633
Evan Cheng4cce6b42008-04-11 17:53:36 +00002634 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002635 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002636}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002637
2638/// hasAllocatableSuperReg - Return true if the specified physical register has
2639/// any super register that's allocatable.
2640bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2641 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2642 if (allocatableRegs_[*AS] && hasInterval(*AS))
2643 return true;
2644 return false;
2645}
2646
2647/// getRepresentativeReg - Find the largest super register of the specified
2648/// physical register.
2649unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2650 // Find the largest super-register that is allocatable.
2651 unsigned BestReg = Reg;
2652 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2653 unsigned SuperReg = *AS;
2654 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2655 BestReg = SuperReg;
2656 break;
2657 }
2658 }
2659 return BestReg;
2660}
2661
2662/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2663/// specified interval that conflicts with the specified physical register.
2664unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2665 unsigned PhysReg) const {
2666 unsigned NumConflicts = 0;
2667 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2668 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2669 E = mri_->reg_end(); I != E; ++I) {
2670 MachineOperand &O = I.getOperand();
2671 MachineInstr *MI = O.getParent();
Lang Hames86511252009-09-04 20:41:11 +00002672 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002673 if (pli.liveAt(Index))
2674 ++NumConflicts;
2675 }
2676 return NumConflicts;
2677}
2678
2679/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002680/// around all defs and uses of the specified interval. Return true if it
2681/// was able to cut its interval.
2682bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002683 unsigned PhysReg, VirtRegMap &vrm) {
2684 unsigned SpillReg = getRepresentativeReg(PhysReg);
2685
2686 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2687 // If there are registers which alias PhysReg, but which are not a
2688 // sub-register of the chosen representative super register. Assert
2689 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002690 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002691 tri_->isSuperRegister(*AS, SpillReg));
2692
Evan Cheng2824a652009-03-23 18:24:37 +00002693 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002694 LiveInterval &pli = getInterval(SpillReg);
2695 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2696 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2697 E = mri_->reg_end(); I != E; ++I) {
2698 MachineOperand &O = I.getOperand();
2699 MachineInstr *MI = O.getParent();
2700 if (SeenMIs.count(MI))
2701 continue;
2702 SeenMIs.insert(MI);
Lang Hames86511252009-09-04 20:41:11 +00002703 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002704 if (pli.liveAt(Index)) {
2705 vrm.addEmergencySpill(SpillReg, MI);
Lang Hames86511252009-09-04 20:41:11 +00002706 MachineInstrIndex StartIdx = getLoadIndex(Index);
Lang Hames35f291d2009-09-12 03:34:03 +00002707 MachineInstrIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002708 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002709 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002710 Cut = true;
2711 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002712 std::string msg;
2713 raw_string_ostream Msg(msg);
2714 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002715 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002716 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002717 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002718 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002719 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002720 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002721 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002722 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2723 if (!hasInterval(*AS))
2724 continue;
2725 LiveInterval &spli = getInterval(*AS);
2726 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002727 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002728 }
2729 }
2730 }
Evan Cheng2824a652009-03-23 18:24:37 +00002731 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002732}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002733
2734LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002735 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002736 LiveInterval& Interval = getOrCreateInterval(reg);
2737 VNInfo* VN = Interval.getNextValue(
Lang Hames86511252009-09-04 20:41:11 +00002738 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
2739 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002740 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002741 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2742 LiveRange LR(
2743 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002744 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002745 Interval.addRange(LR);
2746
2747 return LR;
2748}
David Greeneb5257662009-08-03 21:55:09 +00002749