blob: abab1eef6866ac20840e569fa59b869a3f81e804 [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);
144 ImpDefMIs.push_back(MI);
145 continue;
146 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000147
Evan Chengb0f59732009-09-21 04:32:32 +0000148 if (MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) {
149 MachineOperand &MO = MI->getOperand(2);
150 if (ImpDefRegs.count(MO.getReg())) {
151 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
152 // This is an identity copy, eliminate it now.
153 if (MO.isKill()) {
154 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
155 vi.removeKill(MI);
156 }
157 MI->eraseFromParent();
158 continue;
159 }
160 }
161
Evan Cheng459a7c62009-07-01 08:19:36 +0000162 bool ChangedToImpDef = false;
163 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng2578ba22009-07-01 01:59:31 +0000164 MachineOperand& MO = MI->getOperand(i);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000165 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng2578ba22009-07-01 01:59:31 +0000166 continue;
167 unsigned Reg = MO.getReg();
168 if (!Reg)
169 continue;
170 if (!ImpDefRegs.count(Reg))
171 continue;
Evan Cheng459a7c62009-07-01 08:19:36 +0000172 // Use is a copy, just turn it into an implicit_def.
Evan Chengb0f59732009-09-21 04:32:32 +0000173 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000174 bool isKill = MO.isKill();
175 MI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
176 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
177 MI->RemoveOperand(j);
Evan Chengb0f59732009-09-21 04:32:32 +0000178 if (isKill) {
Evan Cheng459a7c62009-07-01 08:19:36 +0000179 ImpDefRegs.erase(Reg);
Evan Chengb0f59732009-09-21 04:32:32 +0000180 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
181 vi.removeKill(MI);
182 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000183 ChangedToImpDef = true;
184 break;
185 }
186
Evan Cheng2578ba22009-07-01 01:59:31 +0000187 MO.setIsUndef();
Evan Cheng6ade93b2009-08-05 03:53:14 +0000188 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
189 // Make sure other uses of
190 for (unsigned j = i+1; j != e; ++j) {
191 MachineOperand &MOJ = MI->getOperand(j);
192 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
193 MOJ.setIsUndef();
194 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000195 ImpDefRegs.erase(Reg);
Evan Cheng6ade93b2009-08-05 03:53:14 +0000196 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000197 }
198
Evan Cheng459a7c62009-07-01 08:19:36 +0000199 if (ChangedToImpDef) {
200 // Backtrack to process this new implicit_def.
201 --I;
202 } else {
203 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
204 MachineOperand& MO = MI->getOperand(i);
205 if (!MO.isReg() || !MO.isDef())
206 continue;
207 ImpDefRegs.erase(MO.getReg());
208 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000209 }
210 }
211
212 // Any outstanding liveout implicit_def's?
213 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
214 MachineInstr *MI = ImpDefMIs[i];
215 unsigned Reg = MI->getOperand(0).getReg();
Evan Chengd129d732009-07-17 19:43:40 +0000216 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
217 !ImpDefRegs.count(Reg)) {
218 // Delete all "local" implicit_def's. That include those which define
219 // physical registers since they cannot be liveout.
220 MI->eraseFromParent();
Evan Cheng2578ba22009-07-01 01:59:31 +0000221 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000222 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000223
224 // If there are multiple defs of the same register and at least one
225 // is not an implicit_def, do not insert implicit_def's before the
226 // uses.
227 bool Skip = false;
228 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
229 DE = mri_->def_end(); DI != DE; ++DI) {
230 if (DI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) {
231 Skip = true;
232 break;
Evan Cheng2578ba22009-07-01 01:59:31 +0000233 }
Evan Cheng459a7c62009-07-01 08:19:36 +0000234 }
235 if (Skip)
236 continue;
237
Evan Chengd129d732009-07-17 19:43:40 +0000238 // The only implicit_def which we want to keep are those that are live
239 // out of its block.
240 MI->eraseFromParent();
241
Evan Cheng459a7c62009-07-01 08:19:36 +0000242 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
243 UE = mri_->use_end(); UI != UE; ) {
244 MachineOperand &RMO = UI.getOperand();
245 MachineInstr *RMI = &*UI;
246 ++UI;
Evan Cheng2578ba22009-07-01 01:59:31 +0000247 MachineBasicBlock *RMBB = RMI->getParent();
Evan Cheng459a7c62009-07-01 08:19:36 +0000248 if (RMBB == MBB)
Evan Cheng2578ba22009-07-01 01:59:31 +0000249 continue;
Evan Chengd129d732009-07-17 19:43:40 +0000250
251 // Turn a copy use into an implicit_def.
252 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
253 if (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
254 Reg == SrcReg) {
255 RMI->setDesc(tii_->get(TargetInstrInfo::IMPLICIT_DEF));
256 for (int j = RMI->getNumOperands() - 1, ee = 0; j > ee; --j)
257 RMI->RemoveOperand(j);
258 continue;
259 }
260
Evan Cheng2578ba22009-07-01 01:59:31 +0000261 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
262 unsigned NewVReg = mri_->createVirtualRegister(RC);
Evan Cheng2578ba22009-07-01 01:59:31 +0000263 RMO.setReg(NewVReg);
264 RMO.setIsUndef();
265 RMO.setIsKill();
266 }
Evan Cheng2578ba22009-07-01 01:59:31 +0000267 }
268 ImpDefRegs.clear();
269 ImpDefMIs.clear();
270 }
271}
272
Lang Hames86511252009-09-04 20:41:11 +0000273
Owen Anderson80b3ce62008-05-28 20:54:50 +0000274void LiveIntervals::computeNumbering() {
275 Index2MiMap OldI2MI = i2miMap_;
Owen Anderson7fbad272008-07-23 21:37:49 +0000276 std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
Owen Anderson80b3ce62008-05-28 20:54:50 +0000277
278 Idx2MBBMap.clear();
279 MBB2IdxMap.clear();
280 mi2iMap_.clear();
281 i2miMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +0000282 terminatorGaps.clear();
Evan Cheng752195e2009-09-14 21:33:42 +0000283 phiJoinCopies.clear();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000284
Owen Andersona1566f22008-07-22 22:46:49 +0000285 FunctionSize = 0;
286
Chris Lattner428b92e2006-09-15 03:57:23 +0000287 // Number MachineInstrs and MachineBasicBlocks.
288 // Initialize MBB indexes to a sentinal.
Lang Hames86511252009-09-04 20:41:11 +0000289 MBB2IdxMap.resize(mf_->getNumBlockIDs(),
290 std::make_pair(MachineInstrIndex(),MachineInstrIndex()));
Chris Lattner428b92e2006-09-15 03:57:23 +0000291
Lang Hames86511252009-09-04 20:41:11 +0000292 MachineInstrIndex MIIndex;
Chris Lattner428b92e2006-09-15 03:57:23 +0000293 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
294 MBB != E; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000295 MachineInstrIndex StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000296
Owen Anderson7fbad272008-07-23 21:37:49 +0000297 // Insert an empty slot at the beginning of each block.
Lang Hames35f291d2009-09-12 03:34:03 +0000298 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +0000299 i2miMap_.push_back(0);
300
Chris Lattner428b92e2006-09-15 03:57:23 +0000301 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
302 I != E; ++I) {
Lang Hamesffd13262009-07-09 03:57:02 +0000303
304 if (I == MBB->getFirstTerminator()) {
305 // Leave a gap for before terminators, this is where we will point
306 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000307 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000308 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000309 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000310 assert(inserted &&
311 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000312 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000313 i2miMap_.push_back(0);
314
Lang Hames35f291d2009-09-12 03:34:03 +0000315 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000316 }
317
Chris Lattner428b92e2006-09-15 03:57:23 +0000318 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000319 assert(inserted && "multiple MachineInstr -> index mappings");
Devang Patel59500c82008-11-21 20:00:59 +0000320 inserted = true;
Chris Lattner428b92e2006-09-15 03:57:23 +0000321 i2miMap_.push_back(I);
Lang Hames35f291d2009-09-12 03:34:03 +0000322 MIIndex = getNextIndex(MIIndex);
Owen Andersona1566f22008-07-22 22:46:49 +0000323 FunctionSize++;
Owen Anderson7fbad272008-07-23 21:37:49 +0000324
Evan Cheng4ed43292008-10-18 05:21:37 +0000325 // Insert max(1, numdefs) empty slots after every instruction.
Evan Cheng99fe34b2008-10-18 05:18:55 +0000326 unsigned Slots = I->getDesc().getNumDefs();
327 if (Slots == 0)
328 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +0000329 while (Slots--) {
Lang Hames35f291d2009-09-12 03:34:03 +0000330 MIIndex = getNextIndex(MIIndex);
Evan Cheng99fe34b2008-10-18 05:18:55 +0000331 i2miMap_.push_back(0);
Lang Hames86511252009-09-04 20:41:11 +0000332 }
333
Owen Anderson35578012008-06-16 07:10:49 +0000334 }
Lang Hamesffd13262009-07-09 03:57:02 +0000335
336 if (MBB->getFirstTerminator() == MBB->end()) {
337 // Leave a gap for before terminators, this is where we will point
338 // PHI kills.
Lang Hames86511252009-09-04 20:41:11 +0000339 MachineInstrIndex tGap(true, MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000340 bool inserted =
Lang Hames86511252009-09-04 20:41:11 +0000341 terminatorGaps.insert(std::make_pair(&*MBB, tGap)).second;
Lang Hamesffd13262009-07-09 03:57:02 +0000342 assert(inserted &&
343 "Multiple 'first' terminators encountered during numbering.");
Duncan Sands413a15e2009-07-10 20:07:07 +0000344 inserted = inserted; // Avoid compiler warning if assertions turned off.
Lang Hamesffd13262009-07-09 03:57:02 +0000345 i2miMap_.push_back(0);
346
Lang Hames35f291d2009-09-12 03:34:03 +0000347 MIIndex = getNextIndex(MIIndex);
Lang Hamesffd13262009-07-09 03:57:02 +0000348 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000349
Owen Anderson1fbb4542008-06-16 16:58:24 +0000350 // Set the MBB2IdxMap entry for this MBB.
Lang Hames35f291d2009-09-12 03:34:03 +0000351 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, getPrevSlot(MIIndex));
Owen Anderson1fbb4542008-06-16 16:58:24 +0000352 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000353 }
Lang Hamesffd13262009-07-09 03:57:02 +0000354
Evan Cheng4ca980e2007-10-17 02:10:22 +0000355 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000356
357 if (!OldI2MI.empty())
Owen Anderson788d0412008-08-06 18:35:45 +0000358 for (iterator OI = begin(), OE = end(); OI != OE; ++OI) {
Owen Anderson03857b22008-08-13 21:49:13 +0000359 for (LiveInterval::iterator LI = OI->second->begin(),
360 LE = OI->second->end(); LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000361
Owen Anderson7eec0c22008-05-29 23:01:22 +0000362 // Remap the start index of the live range to the corresponding new
363 // number, or our best guess at what it _should_ correspond to if the
364 // original instruction has been erased. This is either the following
365 // instruction or its predecessor.
Lang Hames86511252009-09-04 20:41:11 +0000366 unsigned index = LI->start.getVecIndex();
367 MachineInstrIndex::Slot offset = LI->start.getSlot();
368 if (LI->start.isLoad()) {
Owen Anderson7fbad272008-07-23 21:37:49 +0000369 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000370 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->start);
Owen Anderson7fbad272008-07-23 21:37:49 +0000371 // Take the pair containing the index
372 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000373 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000374
Owen Anderson7fbad272008-07-23 21:37:49 +0000375 LI->start = getMBBStartIdx(J->second);
376 } else {
Lang Hames86511252009-09-04 20:41:11 +0000377 LI->start = MachineInstrIndex(
378 MachineInstrIndex(mi2iMap_[OldI2MI[index]]),
379 (MachineInstrIndex::Slot)offset);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000380 }
381
382 // Remap the ending index in the same way that we remapped the start,
383 // except for the final step where we always map to the immediately
384 // following instruction.
Lang Hames35f291d2009-09-12 03:34:03 +0000385 index = (getPrevSlot(LI->end)).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000386 offset = LI->end.getSlot();
387 if (LI->end.isLoad()) {
Owen Anderson9382b932008-07-30 00:22:56 +0000388 // VReg dies at end of block.
Owen Anderson7fbad272008-07-23 21:37:49 +0000389 std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000390 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), LI->end);
Owen Anderson9382b932008-07-30 00:22:56 +0000391 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000392
Lang Hames35f291d2009-09-12 03:34:03 +0000393 LI->end = getNextSlot(getMBBEndIdx(I->second));
Owen Anderson4b5b2092008-05-29 18:15:49 +0000394 } else {
Owen Andersond7dcbec2008-07-25 19:50:48 +0000395 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000396 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
397
398 if (index != OldI2MI.size())
Lang Hames86511252009-09-04 20:41:11 +0000399 LI->end =
400 MachineInstrIndex(mi2iMap_[OldI2MI[index]],
401 (idx == index ? offset : MachineInstrIndex::LOAD));
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000402 else
Lang Hames86511252009-09-04 20:41:11 +0000403 LI->end =
404 MachineInstrIndex(MachineInstrIndex::NUM * i2miMap_.size());
Owen Anderson4b5b2092008-05-29 18:15:49 +0000405 }
Owen Anderson788d0412008-08-06 18:35:45 +0000406 }
407
Owen Anderson03857b22008-08-13 21:49:13 +0000408 for (LiveInterval::vni_iterator VNI = OI->second->vni_begin(),
409 VNE = OI->second->vni_end(); VNI != VNE; ++VNI) {
Owen Anderson788d0412008-08-06 18:35:45 +0000410 VNInfo* vni = *VNI;
Owen Anderson745825f42008-05-28 22:40:08 +0000411
Owen Anderson7eec0c22008-05-29 23:01:22 +0000412 // Remap the VNInfo def index, which works the same as the
Owen Anderson788d0412008-08-06 18:35:45 +0000413 // start indices above. VN's with special sentinel defs
414 // don't need to be remapped.
Lang Hames857c4e02009-06-17 21:01:20 +0000415 if (vni->isDefAccurate() && !vni->isUnused()) {
Lang Hames86511252009-09-04 20:41:11 +0000416 unsigned index = vni->def.getVecIndex();
417 MachineInstrIndex::Slot offset = vni->def.getSlot();
418 if (vni->def.isLoad()) {
Owen Anderson91292392008-07-30 17:42:47 +0000419 std::vector<IdxMBBPair>::const_iterator I =
Owen Anderson0a7615a2008-07-25 23:06:59 +0000420 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->def);
Owen Anderson91292392008-07-30 17:42:47 +0000421 // Take the pair containing the index
422 std::vector<IdxMBBPair>::const_iterator J =
Owen Andersona0c032f2008-07-29 21:15:44 +0000423 (I == OldI2MBB.end() && OldI2MBB.size()>0) ? (I-1): I;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000424
Owen Anderson91292392008-07-30 17:42:47 +0000425 vni->def = getMBBStartIdx(J->second);
426 } else {
Lang Hames86511252009-09-04 20:41:11 +0000427 vni->def = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Owen Anderson91292392008-07-30 17:42:47 +0000428 }
Owen Anderson7eec0c22008-05-29 23:01:22 +0000429 }
Owen Anderson745825f42008-05-28 22:40:08 +0000430
Owen Anderson7eec0c22008-05-29 23:01:22 +0000431 // Remap the VNInfo kill indices, which works the same as
432 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000433 for (size_t i = 0; i < vni->kills.size(); ++i) {
Lang Hames35f291d2009-09-12 03:34:03 +0000434 unsigned index = getPrevSlot(vni->kills[i]).getVecIndex();
Lang Hames86511252009-09-04 20:41:11 +0000435 MachineInstrIndex::Slot offset = vni->kills[i].getSlot();
Lang Hamesffd13262009-07-09 03:57:02 +0000436
Lang Hames86511252009-09-04 20:41:11 +0000437 if (vni->kills[i].isLoad()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000438 assert("Value killed at a load slot.");
439 /*std::vector<IdxMBBPair>::const_iterator I =
Owen Andersond7dcbec2008-07-25 19:50:48 +0000440 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Owen Anderson9382b932008-07-30 00:22:56 +0000441 --I;
Owen Anderson7fbad272008-07-23 21:37:49 +0000442
Lang Hamesffd13262009-07-09 03:57:02 +0000443 vni->kills[i] = getMBBEndIdx(I->second);*/
Owen Anderson7fbad272008-07-23 21:37:49 +0000444 } else {
Lang Hames86511252009-09-04 20:41:11 +0000445 if (vni->kills[i].isPHIIndex()) {
Lang Hamesffd13262009-07-09 03:57:02 +0000446 std::vector<IdxMBBPair>::const_iterator I =
Lang Hames86511252009-09-04 20:41:11 +0000447 std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), vni->kills[i]);
Lang Hamesffd13262009-07-09 03:57:02 +0000448 --I;
Lang Hames86511252009-09-04 20:41:11 +0000449 vni->kills[i] = terminatorGaps[I->second];
Lang Hamesffd13262009-07-09 03:57:02 +0000450 } else {
451 assert(OldI2MI[index] != 0 &&
452 "Kill refers to instruction not present in index maps.");
Lang Hames86511252009-09-04 20:41:11 +0000453 vni->kills[i] = MachineInstrIndex(mi2iMap_[OldI2MI[index]], offset);
Lang Hamesffd13262009-07-09 03:57:02 +0000454 }
455
456 /*
Owen Andersond7dcbec2008-07-25 19:50:48 +0000457 unsigned idx = index;
Owen Anderson8d0cc0a2008-07-25 21:07:13 +0000458 while (index < OldI2MI.size() && !OldI2MI[index]) ++index;
459
460 if (index != OldI2MI.size())
461 vni->kills[i] = mi2iMap_[OldI2MI[index]] +
462 (idx == index ? offset : 0);
463 else
464 vni->kills[i] = InstrSlots::NUM * i2miMap_.size();
Lang Hamesffd13262009-07-09 03:57:02 +0000465 */
Owen Anderson7eec0c22008-05-29 23:01:22 +0000466 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000467 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000468 }
Owen Anderson788d0412008-08-06 18:35:45 +0000469 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000470}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000471
Lang Hamesf41538d2009-06-02 16:53:25 +0000472void LiveIntervals::scaleNumbering(int factor) {
473 // Need to
474 // * scale MBB begin and end points
475 // * scale all ranges.
476 // * Update VNI structures.
477 // * Scale instruction numberings
478
479 // Scale the MBB indices.
480 Idx2MBBMap.clear();
481 for (MachineFunction::iterator MBB = mf_->begin(), MBBE = mf_->end();
482 MBB != MBBE; ++MBB) {
Lang Hames86511252009-09-04 20:41:11 +0000483 std::pair<MachineInstrIndex, MachineInstrIndex> &mbbIndices = MBB2IdxMap[MBB->getNumber()];
484 mbbIndices.first = mbbIndices.first.scale(factor);
485 mbbIndices.second = mbbIndices.second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000486 Idx2MBBMap.push_back(std::make_pair(mbbIndices.first, MBB));
487 }
488 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
489
Lang Hamesffd13262009-07-09 03:57:02 +0000490 // Scale terminator gaps.
Lang Hames86511252009-09-04 20:41:11 +0000491 for (DenseMap<MachineBasicBlock*, MachineInstrIndex>::iterator
Lang Hamesffd13262009-07-09 03:57:02 +0000492 TGI = terminatorGaps.begin(), TGE = terminatorGaps.end();
493 TGI != TGE; ++TGI) {
Lang Hames86511252009-09-04 20:41:11 +0000494 terminatorGaps[TGI->first] = TGI->second.scale(factor);
Lang Hamesffd13262009-07-09 03:57:02 +0000495 }
496
Lang Hamesf41538d2009-06-02 16:53:25 +0000497 // Scale the intervals.
498 for (iterator LI = begin(), LE = end(); LI != LE; ++LI) {
499 LI->second->scaleNumbering(factor);
500 }
501
502 // Scale MachineInstrs.
503 Mi2IndexMap oldmi2iMap = mi2iMap_;
Lang Hames86511252009-09-04 20:41:11 +0000504 MachineInstrIndex highestSlot;
Lang Hamesf41538d2009-06-02 16:53:25 +0000505 for (Mi2IndexMap::iterator MI = oldmi2iMap.begin(), ME = oldmi2iMap.end();
506 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000507 MachineInstrIndex newSlot = MI->second.scale(factor);
Lang Hamesf41538d2009-06-02 16:53:25 +0000508 mi2iMap_[MI->first] = newSlot;
509 highestSlot = std::max(highestSlot, newSlot);
510 }
511
Lang Hames86511252009-09-04 20:41:11 +0000512 unsigned highestVIndex = highestSlot.getVecIndex();
Lang Hamesf41538d2009-06-02 16:53:25 +0000513 i2miMap_.clear();
Lang Hames86511252009-09-04 20:41:11 +0000514 i2miMap_.resize(highestVIndex + 1);
Lang Hamesf41538d2009-06-02 16:53:25 +0000515 for (Mi2IndexMap::iterator MI = mi2iMap_.begin(), ME = mi2iMap_.end();
516 MI != ME; ++MI) {
Lang Hames86511252009-09-04 20:41:11 +0000517 i2miMap_[MI->second.getVecIndex()] = const_cast<MachineInstr *>(MI->first);
Lang Hamesf41538d2009-06-02 16:53:25 +0000518 }
519
520}
521
522
Owen Anderson80b3ce62008-05-28 20:54:50 +0000523/// runOnMachineFunction - Register allocate the whole function
524///
525bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
526 mf_ = &fn;
527 mri_ = &mf_->getRegInfo();
528 tm_ = &fn.getTarget();
529 tri_ = tm_->getRegisterInfo();
530 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000531 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000532 lv_ = &getAnalysis<LiveVariables>();
533 allocatableRegs_ = tri_->getAllocatableSet(fn);
534
Evan Cheng2578ba22009-07-01 01:59:31 +0000535 processImplicitDefs();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000536 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000537 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000538 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000539
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000540 numIntervals += getNumIntervals();
541
Chris Lattner70ca3582004-09-30 15:59:17 +0000542 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000544}
545
Chris Lattner70ca3582004-09-30 15:59:17 +0000546/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000547void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000548 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000549 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000550 I->second->print(OS, tri_);
551 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000552 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000553
Evan Cheng752195e2009-09-14 21:33:42 +0000554 printInstrs(OS);
555}
556
557void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000558 OS << "********** MACHINEINSTRS **********\n";
559
Chris Lattner3380d5c2009-07-21 21:12:58 +0000560 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
561 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000562 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000563 for (MachineBasicBlock::iterator mii = mbbi->begin(),
564 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000565 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000566 }
567 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000568}
569
Evan Cheng752195e2009-09-14 21:33:42 +0000570void LiveIntervals::dumpInstrs() const {
571 printInstrs(errs());
572}
573
Evan Chengc92da382007-11-03 07:20:12 +0000574/// conflictsWithPhysRegDef - Returns true if the specified register
575/// is defined during the duration of the specified interval.
576bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
577 VirtRegMap &vrm, unsigned reg) {
578 for (LiveInterval::Ranges::const_iterator
579 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000580 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000581 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
582 index = getNextIndex(index)) {
Evan Chengc92da382007-11-03 07:20:12 +0000583 // skip deleted instructions
584 while (index != end && !getInstructionFromIndex(index))
Lang Hames35f291d2009-09-12 03:34:03 +0000585 index = getNextIndex(index);
Evan Chengc92da382007-11-03 07:20:12 +0000586 if (index == end) break;
587
588 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000589 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
590 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000591 if (SrcReg == li.reg || DstReg == li.reg)
592 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000593 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
594 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000595 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000596 continue;
597 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000598 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000599 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000600 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000601 if (!vrm.hasPhys(PhysReg))
602 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000603 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000604 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000605 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000606 return true;
607 }
608 }
609 }
610
611 return false;
612}
613
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000614/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
615/// it can check use as well.
616bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
617 unsigned Reg, bool CheckUse,
618 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
619 for (LiveInterval::Ranges::const_iterator
620 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames86511252009-09-04 20:41:11 +0000621 for (MachineInstrIndex index = getBaseIndex(I->start),
Lang Hames35f291d2009-09-12 03:34:03 +0000622 end = getNextIndex(getBaseIndex(getPrevSlot(I->end))); index != end;
623 index = getNextIndex(index)) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000624 // Skip deleted instructions.
625 MachineInstr *MI = 0;
626 while (index != end) {
627 MI = getInstructionFromIndex(index);
628 if (MI)
629 break;
Lang Hames35f291d2009-09-12 03:34:03 +0000630 index = getNextIndex(index);
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000631 }
632 if (index == end) break;
633
634 if (JoinedCopies.count(MI))
635 continue;
636 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
637 MachineOperand& MO = MI->getOperand(i);
638 if (!MO.isReg())
639 continue;
640 if (MO.isUse() && !CheckUse)
641 continue;
642 unsigned PhysReg = MO.getReg();
643 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
644 continue;
645 if (tri_->isSubRegister(Reg, PhysReg))
646 return true;
647 }
648 }
649 }
650
651 return false;
652}
653
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000654#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000655static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000656 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000657 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000658 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000659 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000660}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000661#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000662
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000663void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000664 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000665 MachineInstrIndex MIIdx,
666 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000667 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000668 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000669 DEBUG({
670 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000671 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000672 });
Evan Cheng419852c2008-04-03 16:39:43 +0000673
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000674 // Virtual registers may be defined multiple times (due to phi
675 // elimination and 2-addr elimination). Much of what we do only has to be
676 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000678 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 if (interval.empty()) {
680 // Get the Idx of the defining instructions.
Lang Hames86511252009-09-04 20:41:11 +0000681 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Dale Johannesen39faac22009-09-20 00:36:41 +0000682 // Earlyclobbers move back one, so that they overlap the live range
683 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000684 if (MO.isEarlyClobber())
685 defIndex = getUseIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000686 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000687 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000688 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000689 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000690 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000691 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000692 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000693 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000694 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000695 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000696
697 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000698
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 // Loop over all of the blocks that the vreg is defined in. There are
700 // two cases we have to handle here. The most common case is a vreg
701 // whose lifetime is contained within a basic block. In this case there
702 // will be a single kill, in MBB, which comes after the definition.
703 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
704 // FIXME: what about dead vars?
Lang Hames86511252009-09-04 20:41:11 +0000705 MachineInstrIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000706 if (vi.Kills[0] != mi)
Lang Hames35f291d2009-09-12 03:34:03 +0000707 killIdx = getNextSlot(getUseIndex(getInstructionIndex(vi.Kills[0])));
Dale Johannesen39faac22009-09-20 00:36:41 +0000708 else if (MO.isEarlyClobber())
709 // Earlyclobbers that die in this instruction move up one extra, to
710 // compensate for having the starting point moved back one. This
711 // gets them to overlap the live range of other outputs.
712 killIdx = getNextSlot(getNextSlot(defIndex));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000713 else
Lang Hames35f291d2009-09-12 03:34:03 +0000714 killIdx = getNextSlot(defIndex);
Chris Lattner6097d132004-07-19 02:15:56 +0000715
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000716 // If the kill happens after the definition, we have an intra-block
717 // live range.
718 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000719 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000720 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000721 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000722 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000723 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000724 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000725 return;
726 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000727 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000728
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000729 // The other case we handle is when a virtual register lives to the end
730 // of the defining block, potentially live across some blocks, then is
731 // live into some number of blocks, but gets killed. Start by adding a
732 // range that goes from this definition to the end of the defining block.
Lang Hames35f291d2009-09-12 03:34:03 +0000733 LiveRange NewLR(defIndex, getNextSlot(getMBBEndIdx(mbb)), ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000734 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000735 interval.addRange(NewLR);
736
737 // Iterate over all of the blocks that the variable is completely
738 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
739 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000740 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
741 E = vi.AliveBlocks.end(); I != E; ++I) {
742 LiveRange LR(getMBBStartIdx(*I),
Lang Hames35f291d2009-09-12 03:34:03 +0000743 getNextSlot(getMBBEndIdx(*I)), // MBB ends at -1.
Dan Gohman4a829ec2008-11-13 16:31:27 +0000744 ValNo);
745 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000746 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000747 }
748
749 // Finally, this virtual register is live from the start of any killing
750 // block to the 'use' slot of the killing instruction.
751 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
752 MachineInstr *Kill = vi.Kills[i];
Evan Cheng21731112009-09-12 02:01:07 +0000753 MachineInstrIndex killIdx =
Lang Hames35f291d2009-09-12 03:34:03 +0000754 getNextSlot(getUseIndex(getInstructionIndex(Kill)));
Evan Chengb0f59732009-09-21 04:32:32 +0000755 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000756 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000757 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000758 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000759 }
760
761 } else {
762 // If this is the second time we see a virtual register definition, it
763 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000764 // the result of two address elimination, then the vreg is one of the
765 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000766 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000767 // If this is a two-address definition, then we have already processed
768 // the live range. The only problem is that we didn't realize there
769 // are actually two values in the live interval. Because of this we
770 // need to take the LiveRegion that defines this register and split it
771 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000772 assert(interval.containsOneValue());
Lang Hames86511252009-09-04 20:41:11 +0000773 MachineInstrIndex DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
774 MachineInstrIndex RedefIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000775 if (MO.isEarlyClobber())
776 RedefIndex = getUseIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000777
Lang Hames35f291d2009-09-12 03:34:03 +0000778 const LiveRange *OldLR =
779 interval.getLiveRangeContaining(getPrevSlot(RedefIndex));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000780 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000781
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000782 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000783 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000784 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000785
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000786 // Two-address vregs should always only be redefined once. This means
787 // that at this point, there should be exactly one value number in it.
788 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
789
Chris Lattner91725b72006-08-31 05:54:43 +0000790 // The new value number (#1) is defined by the instruction we claimed
791 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000792 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000793 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000794 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000795 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
796
Chris Lattner91725b72006-08-31 05:54:43 +0000797 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000798 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000799 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000800 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000801 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000802
803 // Add the new live interval which replaces the range for the input copy.
804 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000805 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000806 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000807 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000808
809 // If this redefinition is dead, we need to add a dummy unit live
810 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000811 if (MO.isDead())
Lang Hames35f291d2009-09-12 03:34:03 +0000812 interval.addRange(
Dale Johannesen39faac22009-09-20 00:36:41 +0000813 LiveRange(RedefIndex, MO.isEarlyClobber() ?
814 getNextSlot(getNextSlot(RedefIndex)) :
815 getNextSlot(RedefIndex), OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000816
Bill Wendling8e6179f2009-08-22 20:18:03 +0000817 DEBUG({
818 errs() << " RESULT: ";
819 interval.print(errs(), tri_);
820 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000821 } else {
822 // Otherwise, this must be because of phi elimination. If this is the
823 // first redefinition of the vreg that we have seen, go back and change
824 // the live range in the PHI block to be a different value number.
825 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000826 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000827 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000828 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000829 phiJoinCopies.push_back(Killer);
Lang Hames86511252009-09-04 20:41:11 +0000830 MachineInstrIndex Start = getMBBStartIdx(Killer->getParent());
Evan Cheng21731112009-09-12 02:01:07 +0000831 MachineInstrIndex End =
Lang Hames35f291d2009-09-12 03:34:03 +0000832 getNextSlot(getUseIndex(getInstructionIndex(Killer)));
Bill Wendling8e6179f2009-08-22 20:18:03 +0000833 DEBUG({
834 errs() << " Removing [" << Start << "," << End << "] from: ";
835 interval.print(errs(), tri_);
836 errs() << "\n";
837 });
Lang Hamesffd13262009-07-09 03:57:02 +0000838 interval.removeRange(Start, End);
839 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000840 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000841 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
842 VNI->addKill(terminatorGaps[killMBB]);
Lang Hames857c4e02009-06-17 21:01:20 +0000843 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000844 DEBUG({
845 errs() << " RESULT: ";
846 interval.print(errs(), tri_);
847 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000848
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000849 // Replace the interval with one of a NEW value number. Note that this
850 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000851 LiveRange LR(Start, End,
Lang Hames86511252009-09-04 20:41:11 +0000852 interval.getNextValue(MachineInstrIndex(mbb->getNumber()),
853 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000854 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000855 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000856 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000857 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000858 DEBUG({
859 errs() << " RESULT: ";
860 interval.print(errs(), tri_);
861 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000862 }
863
864 // In the case of PHI elimination, each variable definition is only
865 // live until the end of the block. We've already taken care of the
866 // rest of the live range.
Lang Hames86511252009-09-04 20:41:11 +0000867 MachineInstrIndex defIndex = getDefIndex(MIIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000868 if (MO.isEarlyClobber())
869 defIndex = getUseIndex(MIIdx);
Evan Cheng752195e2009-09-14 21:33:42 +0000870
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000871 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000872 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000873 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000874 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000875 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000876 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000877 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000878 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000879 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000880
Lang Hames35f291d2009-09-12 03:34:03 +0000881 MachineInstrIndex killIndex = getNextSlot(getMBBEndIdx(mbb));
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000882 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000883 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000884 ValNo->addKill(terminatorGaps[mbb]);
Lang Hames857c4e02009-06-17 21:01:20 +0000885 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000886 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000887 }
888 }
889
Bill Wendling8e6179f2009-08-22 20:18:03 +0000890 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000891}
892
Chris Lattnerf35fef72004-07-23 21:24:19 +0000893void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000894 MachineBasicBlock::iterator mi,
Lang Hames86511252009-09-04 20:41:11 +0000895 MachineInstrIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000896 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000897 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000898 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000899 // A physical register cannot be live across basic block, so its
900 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000901 DEBUG({
902 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000903 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000904 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000905
Lang Hames86511252009-09-04 20:41:11 +0000906 MachineInstrIndex baseIndex = MIIdx;
907 MachineInstrIndex start = getDefIndex(baseIndex);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000908 // Earlyclobbers move back one.
909 if (MO.isEarlyClobber())
910 start = getUseIndex(MIIdx);
Lang Hames86511252009-09-04 20:41:11 +0000911 MachineInstrIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000912
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000913 // If it is not used after definition, it is considered dead at
914 // the instruction defining it. Hence its interval is:
915 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000916 // For earlyclobbers, the defSlot was pushed back one; the extra
917 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000918 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000919 DEBUG(errs() << " dead");
Dale Johannesen39faac22009-09-20 00:36:41 +0000920 if (MO.isEarlyClobber())
921 end = getNextSlot(getNextSlot(start));
922 else
923 end = getNextSlot(start);
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000924 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000925 }
926
927 // If it is not dead on definition, it must be killed by a
928 // subsequent instruction. Hence its interval is:
929 // [defSlot(def), useSlot(kill)+1)
Lang Hames35f291d2009-09-12 03:34:03 +0000930 baseIndex = getNextIndex(baseIndex);
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000931 while (++mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +0000932 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +0000933 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +0000934 baseIndex = getNextIndex(baseIndex);
Evan Cheng6130f662008-03-05 00:59:57 +0000935 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000936 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +0000937 end = getNextSlot(getUseIndex(baseIndex));
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000938 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000939 } else {
940 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
941 if (DefIdx != -1) {
942 if (mi->isRegTiedToUseOperand(DefIdx)) {
943 // Two-address instruction.
944 end = getDefIndex(baseIndex);
945 if (mi->getOperand(DefIdx).isEarlyClobber())
946 end = getUseIndex(baseIndex);
947 } else {
948 // Another instruction redefines the register before it is ever read.
949 // Then the register is essentially dead at the instruction that defines
950 // it. Hence its interval is:
951 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000952 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +0000953 end = getNextSlot(start);
Evan Chengc45288e2009-04-27 20:42:46 +0000954 }
955 goto exit;
956 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000957 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000958
Lang Hames35f291d2009-09-12 03:34:03 +0000959 baseIndex = getNextIndex(baseIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000960 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000961
962 // The only case we should have a dead physreg here without a killing or
963 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000964 // and never used. Another possible case is the implicit use of the
965 // physical register has been deleted by two-address pass.
Lang Hames35f291d2009-09-12 03:34:03 +0000966 end = getNextSlot(start);
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000967
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000968exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000969 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000970
Evan Cheng24a3cc42007-04-25 07:30:23 +0000971 // Already exists? Extend old live interval.
972 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000973 bool Extend = OldLR != interval.end();
974 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000975 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000976 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000977 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000978 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000979 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000980 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000981 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000982}
983
Chris Lattnerf35fef72004-07-23 21:24:19 +0000984void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
985 MachineBasicBlock::iterator MI,
Lang Hames86511252009-09-04 20:41:11 +0000986 MachineInstrIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000987 MachineOperand& MO,
988 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000989 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000990 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000991 getOrCreateInterval(MO.getReg()));
992 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000993 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000994 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000995 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000996 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000997 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000998 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000999 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +00001000 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001001 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +00001002 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +00001003 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +00001004 // If MI also modifies the sub-register explicitly, avoid processing it
1005 // more than once. Do not pass in TRI here so it checks for exact match.
1006 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +00001007 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +00001008 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +00001009 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001010}
1011
Evan Chengb371f452007-02-19 21:49:54 +00001012void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames86511252009-09-04 20:41:11 +00001013 MachineInstrIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +00001014 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001015 DEBUG({
1016 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +00001017 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001018 });
Evan Chengb371f452007-02-19 21:49:54 +00001019
1020 // Look for kills, if it reaches a def before it's killed, then it shouldn't
1021 // be considered a livein.
1022 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames86511252009-09-04 20:41:11 +00001023 MachineInstrIndex baseIndex = MIIdx;
1024 MachineInstrIndex start = baseIndex;
1025 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001026 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001027 baseIndex = getNextIndex(baseIndex);
Lang Hames86511252009-09-04 20:41:11 +00001028 MachineInstrIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +00001029 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +00001030
Evan Chengb371f452007-02-19 21:49:54 +00001031 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +00001032 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001033 DEBUG(errs() << " killed");
Lang Hames35f291d2009-09-12 03:34:03 +00001034 end = getNextSlot(getUseIndex(baseIndex));
Evan Cheng0076c612009-03-05 03:34:26 +00001035 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001036 break;
Evan Cheng6130f662008-03-05 00:59:57 +00001037 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +00001038 // Another instruction redefines the register before it is ever read.
1039 // Then the register is essentially dead at the instruction that defines
1040 // it. Hence its interval is:
1041 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +00001042 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001043 end = getNextSlot(getDefIndex(start));
Evan Cheng0076c612009-03-05 03:34:26 +00001044 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +00001045 break;
Evan Chengb371f452007-02-19 21:49:54 +00001046 }
1047
Lang Hames35f291d2009-09-12 03:34:03 +00001048 baseIndex = getNextIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +00001049 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +00001050 if (mi != MBB->end()) {
Lang Hames86511252009-09-04 20:41:11 +00001051 while (baseIndex.getVecIndex() < i2miMap_.size() &&
Evan Cheng0076c612009-03-05 03:34:26 +00001052 getInstructionFromIndex(baseIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001053 baseIndex = getNextIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +00001054 }
Evan Chengb371f452007-02-19 21:49:54 +00001055 }
1056
Evan Cheng75611fb2007-06-27 01:16:36 +00001057 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +00001058 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +00001059 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001060 DEBUG(errs() << " dead");
Lang Hames35f291d2009-09-12 03:34:03 +00001061 end = getNextSlot(getDefIndex(MIIdx));
Evan Cheng292da942007-06-27 18:47:28 +00001062 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001063 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +00001064 end = baseIndex;
1065 }
Evan Cheng24a3cc42007-04-25 07:30:23 +00001066 }
1067
Lang Hames10382fb2009-06-19 02:17:53 +00001068 VNInfo *vni =
Lang Hames86511252009-09-04 20:41:11 +00001069 interval.getNextValue(MachineInstrIndex(MBB->getNumber()),
1070 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +00001071 vni->setIsPHIDef(true);
1072 LiveRange LR(start, end, vni);
1073
Jim Laskey9b25b8c2007-02-21 22:41:17 +00001074 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +00001075 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +00001076 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +00001077}
1078
Evan Cheng752195e2009-09-14 21:33:42 +00001079bool
1080LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
1081 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +00001082 SmallVector<MachineInstr*,16> &OtherCopies) {
1083 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +00001084 unsigned NumIdent = 0;
Evan Cheng752195e2009-09-14 21:33:42 +00001085 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(SrcInt.reg),
1086 re = mri_->reg_end(); ri != re; ++ri) {
1087 MachineOperand &O = ri.getOperand();
1088 if (!O.isDef())
1089 continue;
1090
Evan Cheng752195e2009-09-14 21:33:42 +00001091 MachineInstr *MI = &*ri;
1092 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
1093 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +00001094 return false;
Evan Cheng752195e2009-09-14 21:33:42 +00001095 if (SrcReg != DstInt.reg) {
1096 OtherCopies.push_back(MI);
1097 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
1098 } else {
1099 IdentCopies.push_back(MI);
1100 ++NumIdent;
1101 }
1102 }
1103
Evan Cheng3f855492009-09-15 06:45:16 +00001104 if (!HaveConflict)
1105 return false; // Let coalescer handle it
1106 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +00001107}
1108
1109void LiveIntervals::performEarlyCoalescing() {
1110 if (!EarlyCoalescing)
1111 return;
1112
1113 /// Perform early coalescing: eliminate copies which feed into phi joins
1114 /// and whose sources are defined by the phi joins.
1115 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
1116 MachineInstr *Join = phiJoinCopies[i];
1117 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
1118 break;
1119
1120 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
1121 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
1122#ifndef NDEBUG
1123 assert(isMove && "PHI join instruction must be a move!");
1124#else
1125 isMove = isMove;
1126#endif
1127
1128 LiveInterval &DstInt = getInterval(PHIDst);
1129 LiveInterval &SrcInt = getInterval(PHISrc);
1130 SmallVector<MachineInstr*, 16> IdentCopies;
1131 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +00001132 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +00001133 continue;
1134
1135 DEBUG(errs() << "PHI Join: " << *Join);
1136 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
1137 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +00001138
Evan Cheng3f855492009-09-15 06:45:16 +00001139 // Change the non-identity copies to directly target the phi destination.
1140 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
1141 MachineInstr *PHICopy = OtherCopies[i];
1142 DEBUG(errs() << "Moving: " << *PHICopy);
1143
Evan Cheng752195e2009-09-14 21:33:42 +00001144 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1145 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1146 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001147 MachineInstrIndex StartIndex = SLR->start;
Evan Cheng752195e2009-09-14 21:33:42 +00001148 MachineInstrIndex EndIndex = SLR->end;
1149
1150 // Delete val# defined by the now identity copy and add the range from
1151 // beginning of the mbb to the end of the range.
1152 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +00001153 DEBUG(errs() << " added range [" << StartIndex << ','
1154 << EndIndex << "] to reg" << DstInt.reg << '\n');
1155 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +00001156 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +00001157 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
1158 VNInfoAllocator);
1159 NewVNI->setHasPHIKill(true);
1160 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
1161 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
1162 MachineOperand &MO = PHICopy->getOperand(j);
1163 if (!MO.isReg() || MO.getReg() != PHISrc)
1164 continue;
1165 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +00001166 }
Evan Cheng3f855492009-09-15 06:45:16 +00001167 }
1168
1169 // Now let's eliminate all the would-be identity copies.
1170 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
1171 MachineInstr *PHICopy = IdentCopies[i];
1172 DEBUG(errs() << "Coalescing: " << *PHICopy);
1173
1174 MachineInstrIndex MIIndex = getInstructionIndex(PHICopy);
1175 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1176 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
1177 MachineInstrIndex StartIndex = SLR->start;
1178 MachineInstrIndex EndIndex = SLR->end;
1179
1180 // Delete val# defined by the now identity copy and add the range from
1181 // beginning of the mbb to the end of the range.
1182 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +00001183 RemoveMachineInstrFromMaps(PHICopy);
1184 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +00001185 DEBUG(errs() << " added range [" << StartIndex << ','
1186 << EndIndex << "] to reg" << DstInt.reg << '\n');
1187 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +00001188 }
Evan Cheng752195e2009-09-14 21:33:42 +00001189
Evan Cheng3f855492009-09-15 06:45:16 +00001190 // Remove the phi join and update the phi block liveness.
1191 MachineInstrIndex MIIndex = getInstructionIndex(Join);
1192 MachineInstrIndex UseIndex = getUseIndex(MIIndex);
1193 MachineInstrIndex DefIndex = getDefIndex(MIIndex);
1194 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
1195 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
1196 DLR->valno->setCopy(0);
1197 DLR->valno->setIsDefAccurate(false);
1198 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
1199 SrcInt.removeRange(SLR->start, SLR->end);
1200 assert(SrcInt.empty());
1201 removeInterval(PHISrc);
1202 RemoveMachineInstrFromMaps(Join);
1203 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +00001204
1205 ++numCoalescing;
1206 }
1207}
1208
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001209/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +00001210/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +00001211/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001212/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +00001213void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001214 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +00001215 << "********** Function: "
1216 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +00001217
1218 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +00001219 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
1220 MBBI != E; ++MBBI) {
1221 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +00001222 // Track the index of the current machine instr.
Lang Hames86511252009-09-04 20:41:11 +00001223 MachineInstrIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +00001224 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +00001225
Chris Lattner428b92e2006-09-15 03:57:23 +00001226 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +00001227
Dan Gohmancb406c22007-10-03 19:26:29 +00001228 // Create intervals for live-ins to this BB first.
1229 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
1230 LE = MBB->livein_end(); LI != LE; ++LI) {
1231 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
1232 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001233 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +00001234 if (!hasInterval(*AS))
1235 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
1236 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +00001237 }
1238
Owen Anderson99500ae2008-09-15 22:00:38 +00001239 // Skip over empty initial indices.
Lang Hames86511252009-09-04 20:41:11 +00001240 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson99500ae2008-09-15 22:00:38 +00001241 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001242 MIIndex = getNextIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +00001243
Chris Lattner428b92e2006-09-15 03:57:23 +00001244 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001245 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001246
Evan Cheng438f7bc2006-11-10 08:43:01 +00001247 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +00001248 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
1249 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +00001250 if (!MO.isReg() || !MO.getReg())
1251 continue;
1252
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001253 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +00001254 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +00001255 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +00001256 else if (MO.isUndef())
1257 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001258 }
Evan Cheng99fe34b2008-10-18 05:18:55 +00001259
1260 // Skip over the empty slots after each instruction.
1261 unsigned Slots = MI->getDesc().getNumDefs();
1262 if (Slots == 0)
1263 Slots = 1;
Lang Hames86511252009-09-04 20:41:11 +00001264
1265 while (Slots--)
Lang Hames35f291d2009-09-12 03:34:03 +00001266 MIIndex = getNextIndex(MIIndex);
Owen Anderson7fbad272008-07-23 21:37:49 +00001267
1268 // Skip over empty indices.
Lang Hames86511252009-09-04 20:41:11 +00001269 while (MIIndex.getVecIndex() < i2miMap_.size() &&
Owen Anderson7fbad272008-07-23 21:37:49 +00001270 getInstructionFromIndex(MIIndex) == 0)
Lang Hames35f291d2009-09-12 03:34:03 +00001271 MIIndex = getNextIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001272 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +00001273 }
Evan Chengd129d732009-07-17 19:43:40 +00001274
1275 // Create empty intervals for registers defined by implicit_def's (except
1276 // for those implicit_def that define values which are liveout of their
1277 // blocks.
1278 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
1279 unsigned UndefReg = UndefUses[i];
1280 (void)getOrCreateInterval(UndefReg);
1281 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001282}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +00001283
Lang Hames86511252009-09-04 20:41:11 +00001284bool LiveIntervals::findLiveInMBBs(
1285 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chenga5bfc972007-10-17 06:53:44 +00001286 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +00001287 std::vector<IdxMBBPair>::const_iterator I =
Evan Chengd0e32c52008-10-29 05:06:14 +00001288 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
Evan Cheng4ca980e2007-10-17 02:10:22 +00001289
1290 bool ResVal = false;
1291 while (I != Idx2MBBMap.end()) {
Dan Gohman2ad82452008-11-26 05:50:31 +00001292 if (I->first >= End)
Evan Cheng4ca980e2007-10-17 02:10:22 +00001293 break;
1294 MBBs.push_back(I->second);
1295 ResVal = true;
1296 ++I;
1297 }
1298 return ResVal;
1299}
1300
Lang Hames86511252009-09-04 20:41:11 +00001301bool LiveIntervals::findReachableMBBs(
1302 MachineInstrIndex Start, MachineInstrIndex End,
Evan Chengd0e32c52008-10-29 05:06:14 +00001303 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
1304 std::vector<IdxMBBPair>::const_iterator I =
1305 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), Start);
1306
1307 bool ResVal = false;
1308 while (I != Idx2MBBMap.end()) {
1309 if (I->first > End)
1310 break;
1311 MachineBasicBlock *MBB = I->second;
1312 if (getMBBEndIdx(MBB) > End)
1313 break;
1314 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1315 SE = MBB->succ_end(); SI != SE; ++SI)
1316 MBBs.push_back(*SI);
1317 ResVal = true;
1318 ++I;
1319 }
1320 return ResVal;
1321}
1322
Owen Anderson03857b22008-08-13 21:49:13 +00001323LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001324 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +00001325 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +00001326}
Evan Chengf2fbca62007-11-12 06:35:08 +00001327
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001328/// dupInterval - Duplicate a live interval. The caller is responsible for
1329/// managing the allocated memory.
1330LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
1331 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +00001332 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +00001333 return NewLI;
1334}
1335
Evan Chengc8d044e2008-02-15 18:24:29 +00001336/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
1337/// copy field and returns the source register that defines it.
1338unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +00001339 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +00001340 return 0;
1341
Lang Hames52c1afc2009-08-10 23:43:28 +00001342 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001343 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +00001344 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001345 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +00001346 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001347 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001348 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
1349 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
1350 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +00001351
Evan Cheng04ee5a12009-01-20 19:12:24 +00001352 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +00001353 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +00001354 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +00001355 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +00001356 return 0;
1357}
Evan Chengf2fbca62007-11-12 06:35:08 +00001358
1359//===----------------------------------------------------------------------===//
1360// Register allocator hooks.
1361//
1362
Evan Chengd70dbb52008-02-22 09:24:50 +00001363/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
1364/// allow one) virtual register operand, then its uses are implicitly using
1365/// the register. Returns the virtual register.
1366unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
1367 MachineInstr *MI) const {
1368 unsigned RegOp = 0;
1369 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1370 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001371 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +00001372 continue;
1373 unsigned Reg = MO.getReg();
1374 if (Reg == 0 || Reg == li.reg)
1375 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +00001376
1377 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
1378 !allocatableRegs_[Reg])
1379 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +00001380 // FIXME: For now, only remat MI with at most one register operand.
1381 assert(!RegOp &&
1382 "Can't rematerialize instruction with multiple register operand!");
1383 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +00001384#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +00001385 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001386#endif
Evan Chengd70dbb52008-02-22 09:24:50 +00001387 }
1388 return RegOp;
1389}
1390
1391/// isValNoAvailableAt - Return true if the val# of the specified interval
1392/// which reaches the given instruction also reaches the specified use index.
1393bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames86511252009-09-04 20:41:11 +00001394 MachineInstrIndex UseIdx) const {
1395 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001396 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
1397 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
1398 return UI != li.end() && UI->valno == ValNo;
1399}
1400
Evan Chengf2fbca62007-11-12 06:35:08 +00001401/// isReMaterializable - Returns true if the definition MI of the specified
1402/// val# of the specified interval is re-materializable.
1403bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001404 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +00001405 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +00001406 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001407 if (DisableReMat)
1408 return false;
1409
Evan Cheng20ccded2008-03-15 00:19:36 +00001410 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +00001411 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001412
1413 int FrameIdx = 0;
1414 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +00001415 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001416 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
1417 // this but remember this is not safe to fold into a two-address
1418 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +00001419 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +00001420 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +00001421
Dan Gohman6d69ba82008-07-25 00:02:30 +00001422 // If the target-specific rules don't identify an instruction as
1423 // being trivially rematerializable, use some target-independent
1424 // rules.
1425 if (!MI->getDesc().isRematerializable() ||
1426 !tii_->isTriviallyReMaterializable(MI)) {
Dan Gohman4c8f8702008-07-25 15:08:37 +00001427 if (!EnableAggressiveRemat)
1428 return false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001429
Dan Gohman0471a792008-07-28 18:43:51 +00001430 // If the instruction accesses memory but the memoperands have been lost,
Dan Gohman6d69ba82008-07-25 00:02:30 +00001431 // we can't analyze it.
1432 const TargetInstrDesc &TID = MI->getDesc();
1433 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
1434 return false;
1435
1436 // Avoid instructions obviously unsafe for remat.
1437 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
1438 return false;
1439
1440 // If the instruction accesses memory and the memory could be non-constant,
1441 // assume the instruction is not rematerializable.
Evan Chengdc377862008-09-30 15:44:16 +00001442 for (std::list<MachineMemOperand>::const_iterator
1443 I = MI->memoperands_begin(), E = MI->memoperands_end(); I != E; ++I){
Dan Gohman6d69ba82008-07-25 00:02:30 +00001444 const MachineMemOperand &MMO = *I;
1445 if (MMO.isVolatile() || MMO.isStore())
1446 return false;
1447 const Value *V = MMO.getValue();
1448 if (!V)
1449 return false;
1450 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
1451 if (!PSV->isConstant(mf_->getFrameInfo()))
Evan Chengd70dbb52008-02-22 09:24:50 +00001452 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001453 } else if (!aa_->pointsToConstantMemory(V))
1454 return false;
1455 }
1456
1457 // If any of the registers accessed are non-constant, conservatively assume
1458 // the instruction is not rematerializable.
1459 unsigned ImpUse = 0;
1460 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1461 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001462 if (MO.isReg()) {
Dan Gohman6d69ba82008-07-25 00:02:30 +00001463 unsigned Reg = MO.getReg();
1464 if (Reg == 0)
1465 continue;
1466 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1467 return false;
1468
1469 // Only allow one def, and that in the first operand.
1470 if (MO.isDef() != (i == 0))
1471 return false;
1472
1473 // Only allow constant-valued registers.
1474 bool IsLiveIn = mri_->isLiveIn(Reg);
1475 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
1476 E = mri_->def_end();
1477
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001478 // For the def, it should be the only def of that register.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001479 if (MO.isDef() && (next(I) != E || IsLiveIn))
1480 return false;
1481
1482 if (MO.isUse()) {
1483 // Only allow one use other register use, as that's all the
1484 // remat mechanisms support currently.
1485 if (Reg != li.reg) {
1486 if (ImpUse == 0)
1487 ImpUse = Reg;
1488 else if (Reg != ImpUse)
1489 return false;
1490 }
Dan Gohmanc93ced5b2008-12-08 04:53:23 +00001491 // For the use, there should be only one associated def.
Dan Gohman6d69ba82008-07-25 00:02:30 +00001492 if (I != E && (next(I) != E || IsLiveIn))
1493 return false;
1494 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001495 }
1496 }
Evan Cheng5ef3a042007-12-06 00:01:56 +00001497 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001498
Dan Gohman6d69ba82008-07-25 00:02:30 +00001499 unsigned ImpUse = getReMatImplicitUse(li, MI);
1500 if (ImpUse) {
1501 const LiveInterval &ImpLi = getInterval(ImpUse);
1502 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
1503 re = mri_->use_end(); ri != re; ++ri) {
1504 MachineInstr *UseMI = &*ri;
Lang Hames86511252009-09-04 20:41:11 +00001505 MachineInstrIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +00001506 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
1507 continue;
1508 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
1509 return false;
1510 }
Evan Chengdc377862008-09-30 15:44:16 +00001511
1512 // If a register operand of the re-materialized instruction is going to
1513 // be spilled next, then it's not legal to re-materialize this instruction.
1514 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
1515 if (ImpUse == SpillIs[i]->reg)
1516 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +00001517 }
1518 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001519}
1520
Evan Cheng06587492008-10-24 02:05:00 +00001521/// isReMaterializable - Returns true if the definition MI of the specified
1522/// val# of the specified interval is re-materializable.
1523bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1524 const VNInfo *ValNo, MachineInstr *MI) {
1525 SmallVector<LiveInterval*, 4> Dummy1;
1526 bool Dummy2;
1527 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
1528}
1529
Evan Cheng5ef3a042007-12-06 00:01:56 +00001530/// isReMaterializable - Returns true if every definition of MI of every
1531/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +00001532bool LiveIntervals::isReMaterializable(const LiveInterval &li,
1533 SmallVectorImpl<LiveInterval*> &SpillIs,
1534 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +00001535 isLoad = false;
1536 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1537 i != e; ++i) {
1538 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +00001539 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001540 continue; // Dead val#.
1541 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001542 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +00001543 return false;
Lang Hames857c4e02009-06-17 21:01:20 +00001544 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001545 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001546 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +00001547 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +00001548 return false;
1549 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +00001550 }
1551 return true;
1552}
1553
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001554/// FilterFoldedOps - Filter out two-address use operands. Return
1555/// true if it finds any issue with the operands that ought to prevent
1556/// folding.
1557static bool FilterFoldedOps(MachineInstr *MI,
1558 SmallVector<unsigned, 2> &Ops,
1559 unsigned &MRInfo,
1560 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001561 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001562 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1563 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001564 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001565 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001566 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001567 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001568 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001569 MRInfo |= (unsigned)VirtRegMap::isMod;
1570 else {
1571 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001572 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001573 MRInfo = VirtRegMap::isModRef;
1574 continue;
1575 }
1576 MRInfo |= (unsigned)VirtRegMap::isRef;
1577 }
1578 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001579 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001580 return false;
1581}
1582
1583
1584/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1585/// slot / to reg or any rematerialized load into ith operand of specified
1586/// MI. If it is successul, MI is updated with the newly created MI and
1587/// returns true.
1588bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1589 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames86511252009-09-04 20:41:11 +00001590 MachineInstrIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001591 SmallVector<unsigned, 2> &Ops,
1592 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001593 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001594 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001595 RemoveMachineInstrFromMaps(MI);
1596 vrm.RemoveMachineInstrFromMaps(MI);
1597 MI->eraseFromParent();
1598 ++numFolds;
1599 return true;
1600 }
1601
1602 // Filter the list of operand indexes that are to be folded. Abort if
1603 // any operand will prevent folding.
1604 unsigned MRInfo = 0;
1605 SmallVector<unsigned, 2> FoldOps;
1606 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1607 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001608
Evan Cheng427f4c12008-03-31 23:19:51 +00001609 // The only time it's safe to fold into a two address instruction is when
1610 // it's folding reload and spill from / into a spill stack slot.
1611 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001612 return false;
1613
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001614 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1615 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001616 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001617 // Remember this instruction uses the spill slot.
1618 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1619
Evan Chengf2fbca62007-11-12 06:35:08 +00001620 // Attempt to fold the memory reference into the instruction. If
1621 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001622 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001623 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001624 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001625 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001626 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001627 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001628 mi2iMap_.erase(MI);
Lang Hames86511252009-09-04 20:41:11 +00001629 i2miMap_[InstrIdx.getVecIndex()] = fmi;
Evan Chengcddbb832007-11-30 21:23:43 +00001630 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +00001631 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001632 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001633 return true;
1634 }
1635 return false;
1636}
1637
Evan Cheng018f9b02007-12-05 03:22:34 +00001638/// canFoldMemoryOperand - Returns true if the specified load / store
1639/// folding is possible.
1640bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001641 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001642 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001643 // Filter the list of operand indexes that are to be folded. Abort if
1644 // any operand will prevent folding.
1645 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001646 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001647 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1648 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001649
Evan Cheng3c75ba82008-04-01 21:37:32 +00001650 // It's only legal to remat for a use, not a def.
1651 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001652 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001653
Evan Chengd70dbb52008-02-22 09:24:50 +00001654 return tii_->canFoldMemoryOperand(MI, FoldOps);
1655}
1656
Evan Cheng81a03822007-11-17 00:40:40 +00001657bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
1658 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
1659 for (LiveInterval::Ranges::const_iterator
1660 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1661 std::vector<IdxMBBPair>::const_iterator II =
1662 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
1663 if (II == Idx2MBBMap.end())
1664 continue;
1665 if (I->end > II->first) // crossing a MBB.
1666 return false;
1667 MBBs.insert(II->second);
1668 if (MBBs.size() > 1)
1669 return false;
1670 }
1671 return true;
1672}
1673
Evan Chengd70dbb52008-02-22 09:24:50 +00001674/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1675/// interval on to-be re-materialized operands of MI) with new register.
1676void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1677 MachineInstr *MI, unsigned NewVReg,
1678 VirtRegMap &vrm) {
1679 // There is an implicit use. That means one of the other operand is
1680 // being remat'ed and the remat'ed instruction has li.reg as an
1681 // use operand. Make sure we rewrite that as well.
1682 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1683 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001684 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001685 continue;
1686 unsigned Reg = MO.getReg();
1687 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1688 continue;
1689 if (!vrm.isReMaterialized(Reg))
1690 continue;
1691 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001692 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1693 if (UseMO)
1694 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001695 }
1696}
1697
Evan Chengf2fbca62007-11-12 06:35:08 +00001698/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1699/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001700bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001701rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001702 bool TrySplit, MachineInstrIndex index, MachineInstrIndex end,
1703 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001704 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001705 unsigned Slot, int LdSlot,
1706 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001707 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001708 const TargetRegisterClass* rc,
1709 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001710 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001711 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001712 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001713 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001714 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001715 RestartInstruction:
1716 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1717 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001718 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001719 continue;
1720 unsigned Reg = mop.getReg();
1721 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001722 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001723 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001724 if (Reg != li.reg)
1725 continue;
1726
1727 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001728 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001729 int FoldSlot = Slot;
1730 if (DefIsReMat) {
1731 // If this is the rematerializable definition MI itself and
1732 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001733 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001734 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1735 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001736 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001737 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001738 MI->eraseFromParent();
1739 break;
1740 }
1741
1742 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001743 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001744 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001745 if (isLoad) {
1746 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1747 FoldSS = isLoadSS;
1748 FoldSlot = LdSlot;
1749 }
1750 }
1751
Evan Chengf2fbca62007-11-12 06:35:08 +00001752 // Scan all of the operands of this instruction rewriting operands
1753 // to use NewVReg instead of li.reg as appropriate. We do this for
1754 // two reasons:
1755 //
1756 // 1. If the instr reads the same spilled vreg multiple times, we
1757 // want to reuse the NewVReg.
1758 // 2. If the instr is a two-addr instruction, we are required to
1759 // keep the src/dst regs pinned.
1760 //
1761 // Keep track of whether we replace a use and/or def so that we can
1762 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001763
Evan Cheng81a03822007-11-17 00:40:40 +00001764 HasUse = mop.isUse();
1765 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001766 SmallVector<unsigned, 2> Ops;
1767 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001768 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001769 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001770 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001771 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001772 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001773 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001774 continue;
1775 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001776 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001777 if (!MOj.isUndef()) {
1778 HasUse |= MOj.isUse();
1779 HasDef |= MOj.isDef();
1780 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001781 }
1782 }
1783
David Greene26b86a02008-10-27 17:38:59 +00001784 // Create a new virtual register for the spill interval.
1785 // Create the new register now so we can map the fold instruction
1786 // to the new register so when it is unfolded we get the correct
1787 // answer.
1788 bool CreatedNewVReg = false;
1789 if (NewVReg == 0) {
1790 NewVReg = mri_->createVirtualRegister(rc);
1791 vrm.grow();
1792 CreatedNewVReg = true;
1793 }
1794
Evan Cheng9c3c2212008-06-06 07:54:39 +00001795 if (!TryFold)
1796 CanFold = false;
1797 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001798 // Do not fold load / store here if we are splitting. We'll find an
1799 // optimal point to insert a load / store later.
1800 if (!TrySplit) {
1801 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001802 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001803 // Folding the load/store can completely change the instruction in
1804 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001805
1806 if (FoldSS) {
1807 // We need to give the new vreg the same stack slot as the
1808 // spilled interval.
1809 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1810 }
1811
Evan Cheng018f9b02007-12-05 03:22:34 +00001812 HasUse = false;
1813 HasDef = false;
1814 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001815 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001816 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001817 goto RestartInstruction;
1818 }
1819 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001820 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001821 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001822 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001823 }
Evan Chengcddbb832007-11-30 21:23:43 +00001824
Evan Chengcddbb832007-11-30 21:23:43 +00001825 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001826 if (mop.isImplicit())
1827 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001828
1829 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001830 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1831 MachineOperand &mopj = MI->getOperand(Ops[j]);
1832 mopj.setReg(NewVReg);
1833 if (mopj.isImplicit())
1834 rewriteImplicitOps(li, MI, NewVReg, vrm);
1835 }
Evan Chengcddbb832007-11-30 21:23:43 +00001836
Evan Cheng81a03822007-11-17 00:40:40 +00001837 if (CreatedNewVReg) {
1838 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001839 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001840 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001841 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001842 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001843 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001844 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001845 }
1846 if (!CanDelete || (HasUse && HasDef)) {
1847 // If this is a two-addr instruction then its use operands are
1848 // rematerializable but its def is not. It should be assigned a
1849 // stack slot.
1850 vrm.assignVirt2StackSlot(NewVReg, Slot);
1851 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001852 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001853 vrm.assignVirt2StackSlot(NewVReg, Slot);
1854 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001855 } else if (HasUse && HasDef &&
1856 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1857 // If this interval hasn't been assigned a stack slot (because earlier
1858 // def is a deleted remat def), do it now.
1859 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1860 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001861 }
1862
Evan Cheng313d4b82008-02-23 00:33:04 +00001863 // Re-matting an instruction with virtual register use. Add the
1864 // register as an implicit use on the use MI.
1865 if (DefIsReMat && ImpUse)
1866 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1867
Evan Cheng5b69eba2009-04-21 22:46:52 +00001868 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001869 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001870 if (CreatedNewVReg) {
1871 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001872 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001873 if (TrySplit)
1874 vrm.setIsSplitFromReg(NewVReg, li.reg);
1875 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001876
1877 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001878 if (CreatedNewVReg) {
Lang Hames35f291d2009-09-12 03:34:03 +00001879 LiveRange LR(getLoadIndex(index), getNextSlot(getUseIndex(index)),
Lang Hames86511252009-09-04 20:41:11 +00001880 nI.getNextValue(MachineInstrIndex(), 0, false,
1881 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001882 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001883 nI.addRange(LR);
1884 } else {
1885 // Extend the split live interval to this def / use.
Lang Hames35f291d2009-09-12 03:34:03 +00001886 MachineInstrIndex End = getNextSlot(getUseIndex(index));
Evan Cheng81a03822007-11-17 00:40:40 +00001887 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1888 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001889 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001890 nI.addRange(LR);
1891 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001892 }
1893 if (HasDef) {
1894 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00001895 nI.getNextValue(MachineInstrIndex(), 0, false,
1896 VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001897 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001898 nI.addRange(LR);
1899 }
Evan Cheng81a03822007-11-17 00:40:40 +00001900
Bill Wendling8e6179f2009-08-22 20:18:03 +00001901 DEBUG({
1902 errs() << "\t\t\t\tAdded new interval: ";
1903 nI.print(errs(), tri_);
1904 errs() << '\n';
1905 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001906 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001907 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001908}
Evan Cheng81a03822007-11-17 00:40:40 +00001909bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001910 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001911 MachineBasicBlock *MBB,
1912 MachineInstrIndex Idx) const {
1913 MachineInstrIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001914 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames86511252009-09-04 20:41:11 +00001915 if (VNI->kills[j].isPHIIndex())
Lang Hamesffd13262009-07-09 03:57:02 +00001916 continue;
1917
Lang Hames86511252009-09-04 20:41:11 +00001918 MachineInstrIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001919 if (KillIdx > Idx && KillIdx < End)
1920 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001921 }
1922 return false;
1923}
1924
Evan Cheng063284c2008-02-21 00:34:19 +00001925/// RewriteInfo - Keep track of machine instrs that will be rewritten
1926/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001927namespace {
1928 struct RewriteInfo {
Lang Hames86511252009-09-04 20:41:11 +00001929 MachineInstrIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001930 MachineInstr *MI;
1931 bool HasUse;
1932 bool HasDef;
Lang Hames86511252009-09-04 20:41:11 +00001933 RewriteInfo(MachineInstrIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001934 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1935 };
Evan Cheng063284c2008-02-21 00:34:19 +00001936
Dan Gohman844731a2008-05-13 00:00:25 +00001937 struct RewriteInfoCompare {
1938 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1939 return LHS.Index < RHS.Index;
1940 }
1941 };
1942}
Evan Cheng063284c2008-02-21 00:34:19 +00001943
Evan Chengf2fbca62007-11-12 06:35:08 +00001944void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001945rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001946 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001947 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001948 unsigned Slot, int LdSlot,
1949 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001950 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001951 const TargetRegisterClass* rc,
1952 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001953 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001954 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001955 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001956 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001957 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1958 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001959 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001960 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001961 unsigned NewVReg = 0;
Lang Hames86511252009-09-04 20:41:11 +00001962 MachineInstrIndex start = getBaseIndex(I->start);
Lang Hames35f291d2009-09-12 03:34:03 +00001963 MachineInstrIndex end = getNextIndex(getBaseIndex(getPrevSlot(I->end)));
Evan Chengf2fbca62007-11-12 06:35:08 +00001964
Evan Cheng063284c2008-02-21 00:34:19 +00001965 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001966 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001967 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001968 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1969 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001970 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001971 MachineOperand &O = ri.getOperand();
1972 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001973 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames86511252009-09-04 20:41:11 +00001974 MachineInstrIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001975 if (index < start || index >= end)
1976 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001977
1978 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001979 // Must be defined by an implicit def. It should not be spilled. Note,
1980 // this is for correctness reason. e.g.
1981 // 8 %reg1024<def> = IMPLICIT_DEF
1982 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1983 // The live range [12, 14) are not part of the r1024 live interval since
1984 // it's defined by an implicit def. It will not conflicts with live
1985 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001986 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001987 // the INSERT_SUBREG and both target registers that would overlap.
1988 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001989 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1990 }
1991 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1992
Evan Cheng313d4b82008-02-23 00:33:04 +00001993 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001994 // Now rewrite the defs and uses.
1995 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1996 RewriteInfo &rwi = RewriteMIs[i];
1997 ++i;
Lang Hames86511252009-09-04 20:41:11 +00001998 MachineInstrIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001999 bool MIHasUse = rwi.HasUse;
2000 bool MIHasDef = rwi.HasDef;
2001 MachineInstr *MI = rwi.MI;
2002 // If MI def and/or use the same register multiple times, then there
2003 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00002004 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002005 while (i != e && RewriteMIs[i].MI == MI) {
2006 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00002007 bool isUse = RewriteMIs[i].HasUse;
2008 if (isUse) ++NumUses;
2009 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00002010 MIHasDef |= RewriteMIs[i].HasDef;
2011 ++i;
2012 }
Evan Cheng81a03822007-11-17 00:40:40 +00002013 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00002014
Evan Cheng0a891ed2008-05-23 23:00:04 +00002015 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00002016 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002017 // register interval's spill weight to HUGE_VALF to prevent it from
2018 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00002019 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00002020 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00002021 }
2022
Evan Cheng063284c2008-02-21 00:34:19 +00002023 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00002024 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00002025 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00002026 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002027 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00002028 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002029 // One common case:
2030 // x = use
2031 // ...
2032 // ...
2033 // def = ...
2034 // = use
2035 // It's better to start a new interval to avoid artifically
2036 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002037 if (MIHasDef && !MIHasUse) {
2038 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00002039 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002040 }
2041 }
Evan Chengcada2452007-11-28 01:28:46 +00002042 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002043
2044 bool IsNew = ThisVReg == 0;
2045 if (IsNew) {
2046 // This ends the previous live interval. If all of its def / use
2047 // can be folded, give it a low spill weight.
2048 if (NewVReg && TrySplit && AllCanFold) {
2049 LiveInterval &nI = getOrCreateInterval(NewVReg);
2050 nI.weight /= 10.0F;
2051 }
2052 AllCanFold = true;
2053 }
2054 NewVReg = ThisVReg;
2055
Evan Cheng81a03822007-11-17 00:40:40 +00002056 bool HasDef = false;
2057 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00002058 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00002059 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
2060 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
2061 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00002062 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002063 if (!HasDef && !HasUse)
2064 continue;
2065
Evan Cheng018f9b02007-12-05 03:22:34 +00002066 AllCanFold &= CanFold;
2067
Evan Cheng81a03822007-11-17 00:40:40 +00002068 // Update weight of spill interval.
2069 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00002070 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00002071 // The spill weight is now infinity as it cannot be spilled again.
2072 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002073 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00002074 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002075
2076 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00002077 if (HasDef) {
2078 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002079 bool HasKill = false;
2080 if (!HasUse)
2081 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
2082 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002083 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames86511252009-09-04 20:41:11 +00002084 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00002085 if (VNI)
2086 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
2087 }
Owen Anderson28998312008-08-13 22:28:50 +00002088 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00002089 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002090 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002091 if (SII == SpillIdxes.end()) {
2092 std::vector<SRInfo> S;
2093 S.push_back(SRInfo(index, NewVReg, true));
2094 SpillIdxes.insert(std::make_pair(MBBId, S));
2095 } else if (SII->second.back().vreg != NewVReg) {
2096 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00002097 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002098 // If there is an earlier def and this is a two-address
2099 // instruction, then it's not possible to fold the store (which
2100 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00002101 SRInfo &Info = SII->second.back();
2102 Info.index = index;
2103 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002104 }
2105 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00002106 } else if (SII != SpillIdxes.end() &&
2107 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002108 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00002109 // There is an earlier def that's not killed (must be two-address).
2110 // The spill is no longer needed.
2111 SII->second.pop_back();
2112 if (SII->second.empty()) {
2113 SpillIdxes.erase(MBBId);
2114 SpillMBBs.reset(MBBId);
2115 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002116 }
2117 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002118 }
2119
2120 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00002121 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002122 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002123 if (SII != SpillIdxes.end() &&
2124 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00002125 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002126 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002127 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00002128 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00002129 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002130 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002131 // If we are splitting live intervals, only fold if it's the first
2132 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002133 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002134 else if (IsNew) {
2135 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00002136 if (RII == RestoreIdxes.end()) {
2137 std::vector<SRInfo> Infos;
2138 Infos.push_back(SRInfo(index, NewVReg, true));
2139 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
2140 } else {
2141 RII->second.push_back(SRInfo(index, NewVReg, true));
2142 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002143 RestoreMBBs.set(MBBId);
2144 }
2145 }
2146
2147 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00002148 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00002149 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00002150 }
Evan Cheng018f9b02007-12-05 03:22:34 +00002151
2152 if (NewVReg && TrySplit && AllCanFold) {
2153 // If all of its def / use can be folded, give it a low spill weight.
2154 LiveInterval &nI = getOrCreateInterval(NewVReg);
2155 nI.weight /= 10.0F;
2156 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002157}
2158
Lang Hames86511252009-09-04 20:41:11 +00002159bool LiveIntervals::alsoFoldARestore(int Id, MachineInstrIndex index,
2160 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002161 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002162 if (!RestoreMBBs[Id])
2163 return false;
2164 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2165 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2166 if (Restores[i].index == index &&
2167 Restores[i].vreg == vr &&
2168 Restores[i].canFold)
2169 return true;
2170 return false;
2171}
2172
Lang Hames86511252009-09-04 20:41:11 +00002173void LiveIntervals::eraseRestoreInfo(int Id, MachineInstrIndex index,
2174 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00002175 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00002176 if (!RestoreMBBs[Id])
2177 return;
2178 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
2179 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
2180 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames86511252009-09-04 20:41:11 +00002181 Restores[i].index = MachineInstrIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00002182}
Evan Cheng81a03822007-11-17 00:40:40 +00002183
Evan Cheng4cce6b42008-04-11 17:53:36 +00002184/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
2185/// spilled and create empty intervals for their uses.
2186void
2187LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
2188 const TargetRegisterClass* rc,
2189 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00002190 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
2191 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002192 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00002193 MachineInstr *MI = &*ri;
2194 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00002195 if (O.isDef()) {
2196 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
2197 "Register def was not rewritten?");
2198 RemoveMachineInstrFromMaps(MI);
2199 vrm.RemoveMachineInstrFromMaps(MI);
2200 MI->eraseFromParent();
2201 } else {
2202 // This must be an use of an implicit_def so it's not part of the live
2203 // interval. Create a new empty live interval for it.
2204 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
2205 unsigned NewVReg = mri_->createVirtualRegister(rc);
2206 vrm.grow();
2207 vrm.setIsImplicitlyDefined(NewVReg);
2208 NewLIs.push_back(&getOrCreateInterval(NewVReg));
2209 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2210 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002211 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002212 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00002213 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00002214 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00002215 }
2216 }
Evan Cheng419852c2008-04-03 16:39:43 +00002217 }
2218}
2219
Evan Chengf2fbca62007-11-12 06:35:08 +00002220std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00002221addIntervalsForSpillsFast(const LiveInterval &li,
2222 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00002223 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00002224 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002225
2226 std::vector<LiveInterval*> added;
2227
2228 assert(li.weight != HUGE_VALF &&
2229 "attempt to spill already spilled interval!");
2230
Bill Wendling8e6179f2009-08-22 20:18:03 +00002231 DEBUG({
2232 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2233 li.dump();
2234 errs() << '\n';
2235 });
Owen Andersond6664312008-08-18 18:05:32 +00002236
2237 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
2238
Owen Andersona41e47a2008-08-19 22:12:11 +00002239 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
2240 while (RI != mri_->reg_end()) {
2241 MachineInstr* MI = &*RI;
2242
2243 SmallVector<unsigned, 2> Indices;
2244 bool HasUse = false;
2245 bool HasDef = false;
2246
2247 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
2248 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002249 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00002250
2251 HasUse |= MI->getOperand(i).isUse();
2252 HasDef |= MI->getOperand(i).isDef();
2253
2254 Indices.push_back(i);
2255 }
2256
2257 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
2258 Indices, true, slot, li.reg)) {
2259 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00002260 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00002261 vrm.assignVirt2StackSlot(NewVReg, slot);
2262
Owen Andersona41e47a2008-08-19 22:12:11 +00002263 // create a new register for this spill
2264 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00002265
Owen Andersona41e47a2008-08-19 22:12:11 +00002266 // the spill weight is now infinity as it
2267 // cannot be spilled again
2268 nI.weight = HUGE_VALF;
2269
2270 // Rewrite register operands to use the new vreg.
2271 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
2272 E = Indices.end(); I != E; ++I) {
2273 MI->getOperand(*I).setReg(NewVReg);
2274
2275 if (MI->getOperand(*I).isUse())
2276 MI->getOperand(*I).setIsKill(true);
2277 }
2278
2279 // Fill in the new live interval.
Lang Hames86511252009-09-04 20:41:11 +00002280 MachineInstrIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00002281 if (HasUse) {
2282 LiveRange LR(getLoadIndex(index), getUseIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002283 nI.getNextValue(MachineInstrIndex(), 0, false,
2284 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002285 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002286 nI.addRange(LR);
2287 vrm.addRestorePoint(NewVReg, MI);
2288 }
2289 if (HasDef) {
2290 LiveRange LR(getDefIndex(index), getStoreIndex(index),
Lang Hames86511252009-09-04 20:41:11 +00002291 nI.getNextValue(MachineInstrIndex(), 0, false,
2292 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00002293 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00002294 nI.addRange(LR);
2295 vrm.addSpillPoint(NewVReg, true, MI);
2296 }
2297
Owen Anderson17197312008-08-18 23:41:04 +00002298 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00002299
Bill Wendling8e6179f2009-08-22 20:18:03 +00002300 DEBUG({
2301 errs() << "\t\t\t\tadded new interval: ";
2302 nI.dump();
2303 errs() << '\n';
2304 });
Owen Andersona41e47a2008-08-19 22:12:11 +00002305 }
Owen Anderson9a032932008-08-18 21:20:32 +00002306
Owen Anderson9a032932008-08-18 21:20:32 +00002307
Owen Andersona41e47a2008-08-19 22:12:11 +00002308 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00002309 }
Owen Andersond6664312008-08-18 18:05:32 +00002310
2311 return added;
2312}
2313
2314std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00002315addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00002316 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00002317 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00002318
2319 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00002320 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00002321
Evan Chengf2fbca62007-11-12 06:35:08 +00002322 assert(li.weight != HUGE_VALF &&
2323 "attempt to spill already spilled interval!");
2324
Bill Wendling8e6179f2009-08-22 20:18:03 +00002325 DEBUG({
2326 errs() << "\t\t\t\tadding intervals for spills for interval: ";
2327 li.print(errs(), tri_);
2328 errs() << '\n';
2329 });
Evan Chengf2fbca62007-11-12 06:35:08 +00002330
Evan Cheng72eeb942008-12-05 17:00:16 +00002331 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00002332 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002333 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002334 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00002335 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
2336 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00002337 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00002338 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00002339
2340 unsigned NumValNums = li.getNumValNums();
2341 SmallVector<MachineInstr*, 4> ReMatDefs;
2342 ReMatDefs.resize(NumValNums, NULL);
2343 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
2344 ReMatOrigDefs.resize(NumValNums, NULL);
2345 SmallVector<int, 4> ReMatIds;
2346 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
2347 BitVector ReMatDelete(NumValNums);
2348 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
2349
Evan Cheng81a03822007-11-17 00:40:40 +00002350 // Spilling a split live interval. It cannot be split any further. Also,
2351 // it's also guaranteed to be a single val# / range interval.
2352 if (vrm.getPreSplitReg(li.reg)) {
2353 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00002354 // Unset the split kill marker on the last use.
Lang Hames86511252009-09-04 20:41:11 +00002355 MachineInstrIndex KillIdx = vrm.getKillPoint(li.reg);
2356 if (KillIdx != MachineInstrIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00002357 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
2358 assert(KillMI && "Last use disappeared?");
2359 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
2360 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00002361 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00002362 }
Evan Chengadf85902007-12-05 09:51:10 +00002363 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00002364 bool DefIsReMat = vrm.isReMaterialized(li.reg);
2365 Slot = vrm.getStackSlot(li.reg);
2366 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
2367 MachineInstr *ReMatDefMI = DefIsReMat ?
2368 vrm.getReMaterializedMI(li.reg) : NULL;
2369 int LdSlot = 0;
2370 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2371 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002372 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00002373 bool IsFirstRange = true;
2374 for (LiveInterval::Ranges::const_iterator
2375 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
2376 // If this is a split live interval with multiple ranges, it means there
2377 // are two-address instructions that re-defined the value. Only the
2378 // first def can be rematerialized!
2379 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00002380 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00002381 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
2382 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002383 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002384 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002385 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002386 } else {
2387 rewriteInstructionsForSpills(li, false, I, NULL, 0,
2388 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00002389 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002390 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002391 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002392 }
2393 IsFirstRange = false;
2394 }
Evan Cheng419852c2008-04-03 16:39:43 +00002395
Evan Cheng4cce6b42008-04-11 17:53:36 +00002396 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00002397 return NewLIs;
2398 }
2399
Evan Cheng752195e2009-09-14 21:33:42 +00002400 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002401 if (TrySplit)
2402 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00002403 bool NeedStackSlot = false;
2404 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
2405 i != e; ++i) {
2406 const VNInfo *VNI = *i;
2407 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00002408 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00002409 continue; // Dead val#.
2410 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00002411 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
2412 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00002413 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00002414 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00002415 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00002416 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00002417 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00002418 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00002419 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00002420 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00002421
2422 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00002423 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00002424 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00002425 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00002426 CanDelete = false;
2427 // Need a stack slot if there is any live range where uses cannot be
2428 // rematerialized.
2429 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00002430 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002431 if (CanDelete)
2432 ReMatDelete.set(VN);
2433 } else {
2434 // Need a stack slot if there is any live range where uses cannot be
2435 // rematerialized.
2436 NeedStackSlot = true;
2437 }
2438 }
2439
2440 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00002441 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
2442 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
2443 Slot = vrm.assignVirt2StackSlot(li.reg);
2444
2445 // This case only occurs when the prealloc splitter has already assigned
2446 // a stack slot to this vreg.
2447 else
2448 Slot = vrm.getStackSlot(li.reg);
2449 }
Evan Chengf2fbca62007-11-12 06:35:08 +00002450
2451 // Create new intervals and rewrite defs and uses.
2452 for (LiveInterval::Ranges::const_iterator
2453 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00002454 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
2455 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
2456 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00002457 bool CanDelete = ReMatDelete[I->valno->id];
2458 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00002459 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00002460 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00002461 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00002462 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002463 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00002464 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00002465 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00002466 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00002467 }
2468
Evan Cheng0cbb1162007-11-29 01:06:25 +00002469 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00002470 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00002471 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00002472 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00002473 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002474
Evan Chengb50bb8c2007-12-05 08:16:32 +00002475 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00002476 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002477 if (NeedStackSlot) {
2478 int Id = SpillMBBs.find_first();
2479 while (Id != -1) {
2480 std::vector<SRInfo> &spills = SpillIdxes[Id];
2481 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002482 MachineInstrIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00002483 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002484 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002485 bool isReMat = vrm.isReMaterialized(VReg);
2486 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002487 bool CanFold = false;
2488 bool FoundUse = false;
2489 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002490 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002491 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002492 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2493 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002494 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00002495 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002496
2497 Ops.push_back(j);
2498 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00002499 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002500 if (isReMat ||
2501 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
2502 RestoreMBBs, RestoreIdxes))) {
2503 // MI has two-address uses of the same register. If the use
2504 // isn't the first and only use in the BB, then we can't fold
2505 // it. FIXME: Move this to rewriteInstructionsForSpills.
2506 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00002507 break;
2508 }
Evan Chengaee4af62007-12-02 08:30:39 +00002509 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00002510 }
2511 }
2512 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002513 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002514 if (CanFold && !Ops.empty()) {
2515 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00002516 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00002517 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00002518 // Also folded uses, do not issue a load.
2519 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames35f291d2009-09-12 03:34:03 +00002520 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengf38d14f2007-12-05 09:05:34 +00002521 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002522 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00002523 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002524 }
2525
Evan Cheng7e073ba2008-04-09 20:57:25 +00002526 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00002527 if (!Folded) {
2528 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
2529 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00002530 if (!MI->registerDefIsDead(nI.reg))
2531 // No need to spill a dead def.
2532 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002533 if (isKill)
2534 AddedKill.insert(&nI);
2535 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002536 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002537 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00002538 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002539 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002540
Evan Cheng1953d0c2007-11-29 10:12:14 +00002541 int Id = RestoreMBBs.find_first();
2542 while (Id != -1) {
2543 std::vector<SRInfo> &restores = RestoreIdxes[Id];
2544 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames86511252009-09-04 20:41:11 +00002545 MachineInstrIndex index = restores[i].index;
2546 if (index == MachineInstrIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00002547 continue;
2548 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00002549 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00002550 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00002551 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00002552 bool CanFold = false;
2553 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00002554 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00002555 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00002556 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
2557 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00002558 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00002559 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00002560
Evan Cheng0cbb1162007-11-29 01:06:25 +00002561 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00002562 // If this restore were to be folded, it would have been folded
2563 // already.
2564 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002565 break;
2566 }
Evan Chengaee4af62007-12-02 08:30:39 +00002567 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002568 }
2569 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002570
2571 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002572 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002573 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002574 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002575 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2576 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002577 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2578 int LdSlot = 0;
2579 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2580 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002581 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002582 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2583 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002584 if (!Folded) {
2585 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2586 if (ImpUse) {
2587 // Re-matting an instruction with virtual register use. Add the
2588 // register as an implicit use on the use MI and update the register
2589 // interval's spill weight to HUGE_VALF to prevent it from being
2590 // spilled.
2591 LiveInterval &ImpLi = getInterval(ImpUse);
2592 ImpLi.weight = HUGE_VALF;
2593 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2594 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002595 }
Evan Chengaee4af62007-12-02 08:30:39 +00002596 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002597 }
2598 // If folding is not possible / failed, then tell the spiller to issue a
2599 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002600 if (Folded)
Lang Hames35f291d2009-09-12 03:34:03 +00002601 nI.removeRange(getLoadIndex(index), getNextSlot(getUseIndex(index)));
Evan Chengb50bb8c2007-12-05 08:16:32 +00002602 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002603 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002604 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002605 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002606 }
2607
Evan Chengb50bb8c2007-12-05 08:16:32 +00002608 // Finalize intervals: add kills, finalize spill weights, and filter out
2609 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002610 std::vector<LiveInterval*> RetNewLIs;
2611 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2612 LiveInterval *LI = NewLIs[i];
2613 if (!LI->empty()) {
Owen Anderson496bac52008-07-23 19:47:27 +00002614 LI->weight /= InstrSlots::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002615 if (!AddedKill.count(LI)) {
2616 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames86511252009-09-04 20:41:11 +00002617 MachineInstrIndex LastUseIdx = getBaseIndex(LR->end);
Evan Chengd120ffd2007-12-05 10:24:35 +00002618 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002619 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002620 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002621 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002622 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002623 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002624 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002625 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002626 RetNewLIs.push_back(LI);
2627 }
2628 }
Evan Cheng81a03822007-11-17 00:40:40 +00002629
Evan Cheng4cce6b42008-04-11 17:53:36 +00002630 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002631 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002632}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002633
2634/// hasAllocatableSuperReg - Return true if the specified physical register has
2635/// any super register that's allocatable.
2636bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2637 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2638 if (allocatableRegs_[*AS] && hasInterval(*AS))
2639 return true;
2640 return false;
2641}
2642
2643/// getRepresentativeReg - Find the largest super register of the specified
2644/// physical register.
2645unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2646 // Find the largest super-register that is allocatable.
2647 unsigned BestReg = Reg;
2648 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2649 unsigned SuperReg = *AS;
2650 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2651 BestReg = SuperReg;
2652 break;
2653 }
2654 }
2655 return BestReg;
2656}
2657
2658/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2659/// specified interval that conflicts with the specified physical register.
2660unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2661 unsigned PhysReg) const {
2662 unsigned NumConflicts = 0;
2663 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2664 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2665 E = mri_->reg_end(); I != E; ++I) {
2666 MachineOperand &O = I.getOperand();
2667 MachineInstr *MI = O.getParent();
Lang Hames86511252009-09-04 20:41:11 +00002668 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002669 if (pli.liveAt(Index))
2670 ++NumConflicts;
2671 }
2672 return NumConflicts;
2673}
2674
2675/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002676/// around all defs and uses of the specified interval. Return true if it
2677/// was able to cut its interval.
2678bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002679 unsigned PhysReg, VirtRegMap &vrm) {
2680 unsigned SpillReg = getRepresentativeReg(PhysReg);
2681
2682 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2683 // If there are registers which alias PhysReg, but which are not a
2684 // sub-register of the chosen representative super register. Assert
2685 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002686 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002687 tri_->isSuperRegister(*AS, SpillReg));
2688
Evan Cheng2824a652009-03-23 18:24:37 +00002689 bool Cut = false;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002690 LiveInterval &pli = getInterval(SpillReg);
2691 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2692 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2693 E = mri_->reg_end(); I != E; ++I) {
2694 MachineOperand &O = I.getOperand();
2695 MachineInstr *MI = O.getParent();
2696 if (SeenMIs.count(MI))
2697 continue;
2698 SeenMIs.insert(MI);
Lang Hames86511252009-09-04 20:41:11 +00002699 MachineInstrIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002700 if (pli.liveAt(Index)) {
2701 vrm.addEmergencySpill(SpillReg, MI);
Lang Hames86511252009-09-04 20:41:11 +00002702 MachineInstrIndex StartIdx = getLoadIndex(Index);
Lang Hames35f291d2009-09-12 03:34:03 +00002703 MachineInstrIndex EndIdx = getNextSlot(getStoreIndex(Index));
Evan Cheng2824a652009-03-23 18:24:37 +00002704 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002705 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002706 Cut = true;
2707 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002708 std::string msg;
2709 raw_string_ostream Msg(msg);
2710 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002711 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002712 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002713 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002714 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002715 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002716 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002717 }
Evan Cheng676dd7c2008-03-11 07:19:34 +00002718 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
2719 if (!hasInterval(*AS))
2720 continue;
2721 LiveInterval &spli = getInterval(*AS);
2722 if (spli.liveAt(Index))
Lang Hames35f291d2009-09-12 03:34:03 +00002723 spli.removeRange(getLoadIndex(Index), getNextSlot(getStoreIndex(Index)));
Evan Cheng676dd7c2008-03-11 07:19:34 +00002724 }
2725 }
2726 }
Evan Cheng2824a652009-03-23 18:24:37 +00002727 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002728}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002729
2730LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002731 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002732 LiveInterval& Interval = getOrCreateInterval(reg);
2733 VNInfo* VN = Interval.getNextValue(
Lang Hames86511252009-09-04 20:41:11 +00002734 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
2735 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002736 VN->setHasPHIKill(true);
Lang Hames86511252009-09-04 20:41:11 +00002737 VN->kills.push_back(terminatorGaps[startInst->getParent()]);
2738 LiveRange LR(
2739 MachineInstrIndex(getInstructionIndex(startInst), MachineInstrIndex::DEF),
Lang Hames35f291d2009-09-12 03:34:03 +00002740 getNextSlot(getMBBEndIdx(startInst->getParent())), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002741 Interval.addRange(LR);
2742
2743 return LR;
2744}
David Greeneb5257662009-08-03 21:55:09 +00002745