blob: 974571944a87b7a58811acf4622139295c863a09 [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"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Owen Andersonfcc63502008-05-29 18:35:21 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng3f32d652008-06-04 09:18:41 +000068 MBB2IdxMap.clear();
Evan Cheng4ca980e2007-10-17 02:10:22 +000069 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000070 mi2iMap_.clear();
71 i2miMap_.clear();
72 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000073 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
74 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000075 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
Dan Gohman8e5f2c62008-07-07 23:14:23 +000076 mf_->DeleteMachineInstr(ClonedMIs[i]);
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000077}
78
Owen Anderson80b3ce62008-05-28 20:54:50 +000079void LiveIntervals::computeNumbering() {
80 Index2MiMap OldI2MI = i2miMap_;
81
82 Idx2MBBMap.clear();
83 MBB2IdxMap.clear();
84 mi2iMap_.clear();
85 i2miMap_.clear();
86
Chris Lattner428b92e2006-09-15 03:57:23 +000087 // Number MachineInstrs and MachineBasicBlocks.
88 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000089 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000090
91 unsigned MIIndex = 0;
92 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
93 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000094 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000095
Chris Lattner428b92e2006-09-15 03:57:23 +000096 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
97 I != E; ++I) {
98 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000099 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000100 i2miMap_.push_back(I);
101 MIIndex += InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000102 }
103
104 if (StartIdx == MIIndex) {
105 // Empty MBB
Owen Anderson1fbb4542008-06-16 16:58:24 +0000106 MIIndex += InstrSlots::NUM;
107 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000108 }
Owen Anderson1fbb4542008-06-16 16:58:24 +0000109 // Set the MBB2IdxMap entry for this MBB.
110 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
111 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000112 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000113 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000114
115 if (!OldI2MI.empty())
Owen Anderson29b03992008-06-19 05:29:34 +0000116 for (iterator I = begin(), E = end(); I != E; ++I)
117 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
118 LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000119
Owen Anderson7eec0c22008-05-29 23:01:22 +0000120 // Remap the start index of the live range to the corresponding new
121 // number, or our best guess at what it _should_ correspond to if the
122 // original instruction has been erased. This is either the following
123 // instruction or its predecessor.
124 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000125 if (OldI2MI[LI->start / InstrSlots::NUM])
126 LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
127 else {
128 unsigned i = 0;
129 MachineInstr* newInstr = 0;
130 do {
131 newInstr = OldI2MI[LI->start / InstrSlots::NUM + i];
132 i++;
133 } while (!newInstr);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000134
Owen Anderson29b03992008-06-19 05:29:34 +0000135 if (mi2iMap_[newInstr] ==
136 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
137 LI->start = mi2iMap_[newInstr];
138 else
139 LI->start = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000140 }
141
142 // Remap the ending index in the same way that we remapped the start,
143 // except for the final step where we always map to the immediately
144 // following instruction.
Owen Anderson29b03992008-06-19 05:29:34 +0000145 if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
146 offset = LI->end % InstrSlots::NUM;
147 if (OldI2MI[LI->end / InstrSlots::NUM])
148 LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
149 else {
150 unsigned i = 0;
151 MachineInstr* newInstr = 0;
152 do {
153 newInstr = OldI2MI[LI->end / InstrSlots::NUM + i];
154 i++;
155 } while (!newInstr);
156
157 LI->end = mi2iMap_[newInstr];
158 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000159 } else {
Owen Anderson29b03992008-06-19 05:29:34 +0000160 LI->end = i2miMap_.size() * InstrSlots::NUM;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000161 }
Owen Anderson745825f42008-05-28 22:40:08 +0000162
Owen Anderson7eec0c22008-05-29 23:01:22 +0000163 // Remap the VNInfo def index, which works the same as the
164 // start indices above.
Owen Anderson745825f42008-05-28 22:40:08 +0000165 VNInfo* vni = LI->valno;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000166 offset = vni->def % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000167 if (OldI2MI[vni->def / InstrSlots::NUM])
168 vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
169 else {
170 unsigned i = 0;
171 MachineInstr* newInstr = 0;
172 do {
173 newInstr = OldI2MI[vni->def / InstrSlots::NUM + i];
174 i++;
175 } while (!newInstr);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000176
Owen Anderson29b03992008-06-19 05:29:34 +0000177 if (mi2iMap_[newInstr] ==
178 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
179 vni->def = mi2iMap_[newInstr];
180 else
181 vni->def = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000182 }
Owen Anderson745825f42008-05-28 22:40:08 +0000183
Owen Anderson7eec0c22008-05-29 23:01:22 +0000184 // Remap the VNInfo kill indices, which works the same as
185 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000186 for (size_t i = 0; i < vni->kills.size(); ++i) {
187 offset = vni->kills[i] % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000188 if (OldI2MI[vni->kills[i] / InstrSlots::NUM])
189 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
190 offset;
191 else {
192 unsigned e = 0;
193 MachineInstr* newInstr = 0;
194 do {
195 newInstr = OldI2MI[vni->kills[i] / InstrSlots::NUM + e];
196 e++;
197 } while (!newInstr);
198
199 vni->kills[i] = mi2iMap_[newInstr];
Owen Anderson7eec0c22008-05-29 23:01:22 +0000200 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000201 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000202 }
203}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000204
Owen Anderson80b3ce62008-05-28 20:54:50 +0000205/// runOnMachineFunction - Register allocate the whole function
206///
207bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
208 mf_ = &fn;
209 mri_ = &mf_->getRegInfo();
210 tm_ = &fn.getTarget();
211 tri_ = tm_->getRegisterInfo();
212 tii_ = tm_->getInstrInfo();
213 lv_ = &getAnalysis<LiveVariables>();
214 allocatableRegs_ = tri_->getAllocatableSet(fn);
215
216 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000217 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000218
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000219 numIntervals += getNumIntervals();
220
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000221 DOUT << "********** INTERVALS **********\n";
222 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000223 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000224 DOUT << "\n";
225 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000226
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000227 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000228 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000229 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000230}
231
Chris Lattner70ca3582004-09-30 15:59:17 +0000232/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000233void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000234 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000235 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Evan Cheng3f32d652008-06-04 09:18:41 +0000236 I->second.print(O, tri_);
237 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000238 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000239
240 O << "********** MACHINEINSTRS **********\n";
241 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
242 mbbi != mbbe; ++mbbi) {
243 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
244 for (MachineBasicBlock::iterator mii = mbbi->begin(),
245 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000246 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000247 }
248 }
249}
250
Evan Chengc92da382007-11-03 07:20:12 +0000251/// conflictsWithPhysRegDef - Returns true if the specified register
252/// is defined during the duration of the specified interval.
253bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
254 VirtRegMap &vrm, unsigned reg) {
255 for (LiveInterval::Ranges::const_iterator
256 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
257 for (unsigned index = getBaseIndex(I->start),
258 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
259 index += InstrSlots::NUM) {
260 // skip deleted instructions
261 while (index != end && !getInstructionFromIndex(index))
262 index += InstrSlots::NUM;
263 if (index == end) break;
264
265 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000266 unsigned SrcReg, DstReg;
267 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
268 if (SrcReg == li.reg || DstReg == li.reg)
269 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000270 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
271 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000272 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000273 continue;
274 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000275 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000276 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000277 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000278 if (!vrm.hasPhys(PhysReg))
279 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000280 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000281 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000282 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000283 return true;
284 }
285 }
286 }
287
288 return false;
289}
290
Evan Cheng549f27d32007-08-13 23:45:17 +0000291void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000292 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000293 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000294 else
295 cerr << "%reg" << reg;
296}
297
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000298void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000299 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000300 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000301 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000302 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000303 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000304 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000305
Evan Cheng419852c2008-04-03 16:39:43 +0000306 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
307 DOUT << "is a implicit_def\n";
308 return;
309 }
310
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000311 // Virtual registers may be defined multiple times (due to phi
312 // elimination and 2-addr elimination). Much of what we do only has to be
313 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314 // time we see a vreg.
315 if (interval.empty()) {
316 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000317 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000318 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000319 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000320 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000321 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000322 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000323 tii_->isMoveInstr(*mi, SrcReg, DstReg))
324 CopyMI = mi;
325 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000326
327 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000328
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000329 // Loop over all of the blocks that the vreg is defined in. There are
330 // two cases we have to handle here. The most common case is a vreg
331 // whose lifetime is contained within a basic block. In this case there
332 // will be a single kill, in MBB, which comes after the definition.
333 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
334 // FIXME: what about dead vars?
335 unsigned killIdx;
336 if (vi.Kills[0] != mi)
337 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
338 else
339 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000340
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341 // If the kill happens after the definition, we have an intra-block
342 // live range.
343 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000344 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000346 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000348 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000349 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350 return;
351 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000352 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000353
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354 // The other case we handle is when a virtual register lives to the end
355 // of the defining block, potentially live across some blocks, then is
356 // live into some number of blocks, but gets killed. Start by adding a
357 // range that goes from this definition to the end of the defining block.
Owen Anderson29b03992008-06-19 05:29:34 +0000358 LiveRange NewLR(defIndex,
359 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
360 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000361 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000362 interval.addRange(NewLR);
363
364 // Iterate over all of the blocks that the variable is completely
365 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
366 // live interval.
367 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
368 if (vi.AliveBlocks[i]) {
Owen Anderson31ec8412008-06-16 19:32:40 +0000369 LiveRange LR(getMBBStartIdx(i),
Evan Chengf26e8552008-06-17 20:13:36 +0000370 getMBBEndIdx(i)+1, // MBB ends at -1.
Owen Anderson31ec8412008-06-16 19:32:40 +0000371 ValNo);
372 interval.addRange(LR);
373 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 }
375 }
376
377 // Finally, this virtual register is live from the start of any killing
378 // block to the 'use' slot of the killing instruction.
379 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
380 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000381 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000382 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000383 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000384 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000385 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000386 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000387 }
388
389 } else {
390 // If this is the second time we see a virtual register definition, it
391 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000392 // the result of two address elimination, then the vreg is one of the
393 // def-and-use register operand.
Evan Chengef0732d2008-07-10 07:35:43 +0000394 if (mi->isRegReDefinedByTwoAddr(interval.reg, MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000395 // If this is a two-address definition, then we have already processed
396 // the live range. The only problem is that we didn't realize there
397 // are actually two values in the live interval. Because of this we
398 // need to take the LiveRegion that defines this register and split it
399 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000400 assert(interval.containsOneValue());
401 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000402 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000403
Evan Cheng4f8ff162007-08-11 00:59:19 +0000404 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000405 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000406
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000407 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000408 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000409 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000410
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000411 // Two-address vregs should always only be redefined once. This means
412 // that at this point, there should be exactly one value number in it.
413 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
414
Chris Lattner91725b72006-08-31 05:54:43 +0000415 // The new value number (#1) is defined by the instruction we claimed
416 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000417 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
418 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000419
Chris Lattner91725b72006-08-31 05:54:43 +0000420 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000421 OldValNo->def = RedefIndex;
422 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000423
424 // Add the new live interval which replaces the range for the input copy.
425 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000426 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000428 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429
430 // If this redefinition is dead, we need to add a dummy unit live
431 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000432 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000433 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000434
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000435 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000436 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437
438 } else {
439 // Otherwise, this must be because of phi elimination. If this is the
440 // first redefinition of the vreg that we have seen, go back and change
441 // the live range in the PHI block to be a different value number.
442 if (interval.containsOneValue()) {
443 assert(vi.Kills.size() == 1 &&
444 "PHI elimination vreg should have one kill, the PHI itself!");
445
446 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000447 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000448 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000449 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000450 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000451 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000452 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000453 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000454 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000455 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000457 // Replace the interval with one of a NEW value number. Note that this
458 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000459 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000460 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000461 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000462 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000463 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000464 }
465
466 // In the case of PHI elimination, each variable definition is only
467 // live until the end of the block. We've already taken care of the
468 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000469 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000470
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000471 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000472 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000473 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000474 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000475 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000476 tii_->isMoveInstr(*mi, SrcReg, DstReg))
477 CopyMI = mi;
478 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000479
Owen Anderson29b03992008-06-19 05:29:34 +0000480 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000482 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000483 interval.addKill(ValNo, killIndex);
484 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000485 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000486 }
487 }
488
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000489 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000490}
491
Chris Lattnerf35fef72004-07-23 21:24:19 +0000492void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000494 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000495 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000496 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000497 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000498 // A physical register cannot be live across basic block, so its
499 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000500 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000501
Chris Lattner6b128bd2006-09-03 08:07:11 +0000502 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000503 unsigned start = getDefIndex(baseIndex);
504 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000505
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000506 // If it is not used after definition, it is considered dead at
507 // the instruction defining it. Hence its interval is:
508 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000509 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000510 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000511 end = getDefIndex(start) + 1;
512 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000513 }
514
515 // If it is not dead on definition, it must be killed by a
516 // subsequent instruction. Hence its interval is:
517 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000518 while (++mi != MBB->end()) {
Owen Anderson29b03992008-06-19 05:29:34 +0000519 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000520 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000521 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000522 end = getUseIndex(baseIndex) + 1;
523 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000524 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000525 // Another instruction redefines the register before it is ever read.
526 // Then the register is essentially dead at the instruction that defines
527 // it. Hence its interval is:
528 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000529 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000530 end = getDefIndex(start) + 1;
531 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000532 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000533 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000534
535 // The only case we should have a dead physreg here without a killing or
536 // instruction where we know it's dead is if it is live-in to the function
537 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000538 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000539 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000540
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000541exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000542 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000543
Evan Cheng24a3cc42007-04-25 07:30:23 +0000544 // Already exists? Extend old live interval.
545 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000546 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000547 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000548 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000549 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000550 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000551 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000552}
553
Chris Lattnerf35fef72004-07-23 21:24:19 +0000554void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
555 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000556 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000557 MachineOperand& MO,
558 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000559 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000560 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000561 getOrCreateInterval(MO.getReg()));
562 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000563 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000564 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000565 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000566 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000567 tii_->isMoveInstr(*MI, SrcReg, DstReg))
568 CopyMI = MI;
Owen Anderson6b098de2008-06-25 23:39:39 +0000569 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
570 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000571 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000572 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000573 // If MI also modifies the sub-register explicitly, avoid processing it
574 // more than once. Do not pass in TRI here so it checks for exact match.
575 if (!MI->modifiesRegister(*AS))
Owen Anderson6b098de2008-06-25 23:39:39 +0000576 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
577 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000578 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000579}
580
Evan Chengb371f452007-02-19 21:49:54 +0000581void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000582 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000583 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000584 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
585
586 // Look for kills, if it reaches a def before it's killed, then it shouldn't
587 // be considered a livein.
588 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000589 unsigned baseIndex = MIIdx;
590 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000591 unsigned end = start;
592 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000593 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000594 DOUT << " killed";
595 end = getUseIndex(baseIndex) + 1;
596 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000597 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000598 // Another instruction redefines the register before it is ever read.
599 // Then the register is essentially dead at the instruction that defines
600 // it. Hence its interval is:
601 // [defSlot(def), defSlot(def)+1)
602 DOUT << " dead";
603 end = getDefIndex(start) + 1;
604 goto exit;
605 }
606
607 baseIndex += InstrSlots::NUM;
608 ++mi;
609 }
610
611exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000612 // Live-in register might not be used at all.
613 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000614 if (isAlias) {
615 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000616 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000617 } else {
618 DOUT << " live through";
619 end = baseIndex;
620 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000621 }
622
Evan Chengf3bb2e62007-09-05 21:46:51 +0000623 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000624 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000625 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000626 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000627}
628
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000629/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000630/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000631/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000632/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000633void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000634 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
635 << "********** Function: "
636 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000637 // Track the index of the current machine instr.
638 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000639 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
640 MBBI != E; ++MBBI) {
641 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000642 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000643
Chris Lattner428b92e2006-09-15 03:57:23 +0000644 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000645
Dan Gohmancb406c22007-10-03 19:26:29 +0000646 // Create intervals for live-ins to this BB first.
647 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
648 LE = MBB->livein_end(); LI != LE; ++LI) {
649 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
650 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000651 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000652 if (!hasInterval(*AS))
653 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
654 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000655 }
656
Chris Lattner428b92e2006-09-15 03:57:23 +0000657 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000658 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000659
Evan Cheng438f7bc2006-11-10 08:43:01 +0000660 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000661 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
662 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000663 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000664 if (MO.isRegister() && MO.getReg() && MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000665 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000666 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000667
668 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000669 }
Owen Anderson29b03992008-06-19 05:29:34 +0000670
671 if (MBB->begin() == miEnd) MIIndex += InstrSlots::NUM; // Empty MBB
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000672 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000673}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000674
Evan Cheng4ca980e2007-10-17 02:10:22 +0000675bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000676 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000677 std::vector<IdxMBBPair>::const_iterator I =
678 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
679
680 bool ResVal = false;
681 while (I != Idx2MBBMap.end()) {
682 if (LR.end <= I->first)
683 break;
684 MBBs.push_back(I->second);
685 ResVal = true;
686 ++I;
687 }
688 return ResVal;
689}
690
691
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000692LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000693 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000694 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000695 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000696}
Evan Chengf2fbca62007-11-12 06:35:08 +0000697
Evan Chengc8d044e2008-02-15 18:24:29 +0000698/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
699/// copy field and returns the source register that defines it.
700unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
701 if (!VNI->copy)
702 return 0;
703
704 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
705 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000706 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
707 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000708 unsigned SrcReg, DstReg;
709 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
710 return SrcReg;
711 assert(0 && "Unrecognized copy instruction!");
712 return 0;
713}
Evan Chengf2fbca62007-11-12 06:35:08 +0000714
715//===----------------------------------------------------------------------===//
716// Register allocator hooks.
717//
718
Evan Chengd70dbb52008-02-22 09:24:50 +0000719/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
720/// allow one) virtual register operand, then its uses are implicitly using
721/// the register. Returns the virtual register.
722unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
723 MachineInstr *MI) const {
724 unsigned RegOp = 0;
725 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
726 MachineOperand &MO = MI->getOperand(i);
727 if (!MO.isRegister() || !MO.isUse())
728 continue;
729 unsigned Reg = MO.getReg();
730 if (Reg == 0 || Reg == li.reg)
731 continue;
732 // FIXME: For now, only remat MI with at most one register operand.
733 assert(!RegOp &&
734 "Can't rematerialize instruction with multiple register operand!");
735 RegOp = MO.getReg();
736 break;
737 }
738 return RegOp;
739}
740
741/// isValNoAvailableAt - Return true if the val# of the specified interval
742/// which reaches the given instruction also reaches the specified use index.
743bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
744 unsigned UseIdx) const {
745 unsigned Index = getInstructionIndex(MI);
746 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
747 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
748 return UI != li.end() && UI->valno == ValNo;
749}
750
Evan Chengf2fbca62007-11-12 06:35:08 +0000751/// isReMaterializable - Returns true if the definition MI of the specified
752/// val# of the specified interval is re-materializable.
753bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000754 const VNInfo *ValNo, MachineInstr *MI,
755 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000756 if (DisableReMat)
757 return false;
758
Evan Cheng5ef3a042007-12-06 00:01:56 +0000759 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000760 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000761 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000762
763 int FrameIdx = 0;
764 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000765 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000766 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
767 // this but remember this is not safe to fold into a two-address
768 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000769 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000770 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000771
Evan Chengd70dbb52008-02-22 09:24:50 +0000772 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000773 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000774 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000775
776 unsigned ImpUse = getReMatImplicitUse(li, MI);
777 if (ImpUse) {
778 const LiveInterval &ImpLi = getInterval(ImpUse);
779 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
780 re = mri_->use_end(); ri != re; ++ri) {
781 MachineInstr *UseMI = &*ri;
782 unsigned UseIdx = getInstructionIndex(UseMI);
783 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
784 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000785 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000786 return false;
787 }
788 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000789 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000790 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000791
Evan Chengdd3465e2008-02-23 01:44:27 +0000792 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000793}
794
795/// isReMaterializable - Returns true if every definition of MI of every
796/// val# of the specified interval is re-materializable.
797bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
798 isLoad = false;
799 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
800 i != e; ++i) {
801 const VNInfo *VNI = *i;
802 unsigned DefIdx = VNI->def;
803 if (DefIdx == ~1U)
804 continue; // Dead val#.
805 // Is the def for the val# rematerializable?
806 if (DefIdx == ~0u)
807 return false;
808 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
809 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000810 if (!ReMatDefMI ||
811 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000812 return false;
813 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000814 }
815 return true;
816}
817
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000818/// FilterFoldedOps - Filter out two-address use operands. Return
819/// true if it finds any issue with the operands that ought to prevent
820/// folding.
821static bool FilterFoldedOps(MachineInstr *MI,
822 SmallVector<unsigned, 2> &Ops,
823 unsigned &MRInfo,
824 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000825 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000826
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000827 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000828 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
829 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000830 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000831 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000832 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000833 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000834 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000835 MRInfo |= (unsigned)VirtRegMap::isMod;
836 else {
837 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000838 if (!MO.isImplicit() &&
839 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000840 MRInfo = VirtRegMap::isModRef;
841 continue;
842 }
843 MRInfo |= (unsigned)VirtRegMap::isRef;
844 }
845 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000846 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000847 return false;
848}
849
850
851/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
852/// slot / to reg or any rematerialized load into ith operand of specified
853/// MI. If it is successul, MI is updated with the newly created MI and
854/// returns true.
855bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
856 VirtRegMap &vrm, MachineInstr *DefMI,
857 unsigned InstrIdx,
858 SmallVector<unsigned, 2> &Ops,
859 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000860 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000861 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000862 RemoveMachineInstrFromMaps(MI);
863 vrm.RemoveMachineInstrFromMaps(MI);
864 MI->eraseFromParent();
865 ++numFolds;
866 return true;
867 }
868
869 // Filter the list of operand indexes that are to be folded. Abort if
870 // any operand will prevent folding.
871 unsigned MRInfo = 0;
872 SmallVector<unsigned, 2> FoldOps;
873 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
874 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000875
Evan Cheng427f4c12008-03-31 23:19:51 +0000876 // The only time it's safe to fold into a two address instruction is when
877 // it's folding reload and spill from / into a spill stack slot.
878 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000879 return false;
880
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000881 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
882 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000883 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000884 // Remember this instruction uses the spill slot.
885 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
886
Evan Chengf2fbca62007-11-12 06:35:08 +0000887 // Attempt to fold the memory reference into the instruction. If
888 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +0000889 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000890 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000891 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000892 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000893 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000894 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000895 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000896 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
897 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000898 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000899 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000900 return true;
901 }
902 return false;
903}
904
Evan Cheng018f9b02007-12-05 03:22:34 +0000905/// canFoldMemoryOperand - Returns true if the specified load / store
906/// folding is possible.
907bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000908 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000909 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000910 // Filter the list of operand indexes that are to be folded. Abort if
911 // any operand will prevent folding.
912 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000913 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000914 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
915 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000916
Evan Cheng3c75ba82008-04-01 21:37:32 +0000917 // It's only legal to remat for a use, not a def.
918 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000919 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000920
Evan Chengd70dbb52008-02-22 09:24:50 +0000921 return tii_->canFoldMemoryOperand(MI, FoldOps);
922}
923
Evan Cheng81a03822007-11-17 00:40:40 +0000924bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
925 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
926 for (LiveInterval::Ranges::const_iterator
927 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
928 std::vector<IdxMBBPair>::const_iterator II =
929 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
930 if (II == Idx2MBBMap.end())
931 continue;
932 if (I->end > II->first) // crossing a MBB.
933 return false;
934 MBBs.insert(II->second);
935 if (MBBs.size() > 1)
936 return false;
937 }
938 return true;
939}
940
Evan Chengd70dbb52008-02-22 09:24:50 +0000941/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
942/// interval on to-be re-materialized operands of MI) with new register.
943void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
944 MachineInstr *MI, unsigned NewVReg,
945 VirtRegMap &vrm) {
946 // There is an implicit use. That means one of the other operand is
947 // being remat'ed and the remat'ed instruction has li.reg as an
948 // use operand. Make sure we rewrite that as well.
949 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
950 MachineOperand &MO = MI->getOperand(i);
951 if (!MO.isRegister())
952 continue;
953 unsigned Reg = MO.getReg();
954 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
955 continue;
956 if (!vrm.isReMaterialized(Reg))
957 continue;
958 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000959 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
960 if (UseMO)
961 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000962 }
963}
964
Evan Chengf2fbca62007-11-12 06:35:08 +0000965/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
966/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000967bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000968rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
969 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000970 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000971 unsigned Slot, int LdSlot,
972 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000973 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000974 const TargetRegisterClass* rc,
975 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000976 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000977 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000978 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000979 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
980 MachineBasicBlock *MBB = MI->getParent();
981 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng018f9b02007-12-05 03:22:34 +0000982 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000983 RestartInstruction:
984 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
985 MachineOperand& mop = MI->getOperand(i);
986 if (!mop.isRegister())
987 continue;
988 unsigned Reg = mop.getReg();
989 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000990 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000991 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000992 if (Reg != li.reg)
993 continue;
994
995 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000996 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000997 int FoldSlot = Slot;
998 if (DefIsReMat) {
999 // If this is the rematerializable definition MI itself and
1000 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001001 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001002 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1003 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001004 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001005 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001006 MI->eraseFromParent();
1007 break;
1008 }
1009
1010 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001011 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001012 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001013 if (isLoad) {
1014 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1015 FoldSS = isLoadSS;
1016 FoldSlot = LdSlot;
1017 }
1018 }
1019
Evan Chengf2fbca62007-11-12 06:35:08 +00001020 // Scan all of the operands of this instruction rewriting operands
1021 // to use NewVReg instead of li.reg as appropriate. We do this for
1022 // two reasons:
1023 //
1024 // 1. If the instr reads the same spilled vreg multiple times, we
1025 // want to reuse the NewVReg.
1026 // 2. If the instr is a two-addr instruction, we are required to
1027 // keep the src/dst regs pinned.
1028 //
1029 // Keep track of whether we replace a use and/or def so that we can
1030 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001031
Evan Cheng81a03822007-11-17 00:40:40 +00001032 HasUse = mop.isUse();
1033 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001034 SmallVector<unsigned, 2> Ops;
1035 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001036 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001037 const MachineOperand &MOj = MI->getOperand(j);
1038 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +00001039 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001040 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001041 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001042 continue;
1043 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001044 Ops.push_back(j);
1045 HasUse |= MOj.isUse();
1046 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001047 }
1048 }
1049
Evan Cheng9c3c2212008-06-06 07:54:39 +00001050 // Update stack slot spill weight if we are splitting.
Evan Chengc3417602008-06-21 06:45:54 +00001051 float Weight = getSpillWeight(HasDef, HasUse, loopDepth);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001052 if (!TrySplit)
1053 SSWeight += Weight;
1054
1055 if (!TryFold)
1056 CanFold = false;
1057 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001058 // Do not fold load / store here if we are splitting. We'll find an
1059 // optimal point to insert a load / store later.
1060 if (!TrySplit) {
1061 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1062 Ops, FoldSS, FoldSlot, Reg)) {
1063 // Folding the load/store can completely change the instruction in
1064 // unpredictable ways, rescan it from the beginning.
1065 HasUse = false;
1066 HasDef = false;
1067 CanFold = false;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001068 if (isRemoved(MI)) {
1069 SSWeight -= Weight;
Evan Cheng7e073ba2008-04-09 20:57:25 +00001070 break;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001071 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001072 goto RestartInstruction;
1073 }
1074 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001075 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001076 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001077 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001078 }
Evan Chengcddbb832007-11-30 21:23:43 +00001079
1080 // Create a new virtual register for the spill interval.
1081 bool CreatedNewVReg = false;
1082 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001083 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001084 vrm.grow();
1085 CreatedNewVReg = true;
1086 }
1087 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001088 if (mop.isImplicit())
1089 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001090
1091 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001092 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1093 MachineOperand &mopj = MI->getOperand(Ops[j]);
1094 mopj.setReg(NewVReg);
1095 if (mopj.isImplicit())
1096 rewriteImplicitOps(li, MI, NewVReg, vrm);
1097 }
Evan Chengcddbb832007-11-30 21:23:43 +00001098
Evan Cheng81a03822007-11-17 00:40:40 +00001099 if (CreatedNewVReg) {
1100 if (DefIsReMat) {
1101 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001102 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001103 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001104 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001105 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001106 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001107 }
1108 if (!CanDelete || (HasUse && HasDef)) {
1109 // If this is a two-addr instruction then its use operands are
1110 // rematerializable but its def is not. It should be assigned a
1111 // stack slot.
1112 vrm.assignVirt2StackSlot(NewVReg, Slot);
1113 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001114 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001115 vrm.assignVirt2StackSlot(NewVReg, Slot);
1116 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001117 } else if (HasUse && HasDef &&
1118 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1119 // If this interval hasn't been assigned a stack slot (because earlier
1120 // def is a deleted remat def), do it now.
1121 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1122 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001123 }
1124
Evan Cheng313d4b82008-02-23 00:33:04 +00001125 // Re-matting an instruction with virtual register use. Add the
1126 // register as an implicit use on the use MI.
1127 if (DefIsReMat && ImpUse)
1128 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1129
Evan Chengf2fbca62007-11-12 06:35:08 +00001130 // create a new register interval for this spill / remat.
1131 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001132 if (CreatedNewVReg) {
1133 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001134 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001135 if (TrySplit)
1136 vrm.setIsSplitFromReg(NewVReg, li.reg);
1137 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001138
1139 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001140 if (CreatedNewVReg) {
1141 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1142 nI.getNextValue(~0U, 0, VNInfoAllocator));
1143 DOUT << " +" << LR;
1144 nI.addRange(LR);
1145 } else {
1146 // Extend the split live interval to this def / use.
1147 unsigned End = getUseIndex(index)+1;
1148 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1149 nI.getValNumInfo(nI.getNumValNums()-1));
1150 DOUT << " +" << LR;
1151 nI.addRange(LR);
1152 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001153 }
1154 if (HasDef) {
1155 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1156 nI.getNextValue(~0U, 0, VNInfoAllocator));
1157 DOUT << " +" << LR;
1158 nI.addRange(LR);
1159 }
Evan Cheng81a03822007-11-17 00:40:40 +00001160
Evan Chengf2fbca62007-11-12 06:35:08 +00001161 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001162 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001163 DOUT << '\n';
1164 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001165 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001166}
Evan Cheng81a03822007-11-17 00:40:40 +00001167bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001168 const VNInfo *VNI,
1169 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001170 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001171 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1172 unsigned KillIdx = VNI->kills[j];
1173 if (KillIdx > Idx && KillIdx < End)
1174 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001175 }
1176 return false;
1177}
1178
Evan Cheng063284c2008-02-21 00:34:19 +00001179/// RewriteInfo - Keep track of machine instrs that will be rewritten
1180/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001181namespace {
1182 struct RewriteInfo {
1183 unsigned Index;
1184 MachineInstr *MI;
1185 bool HasUse;
1186 bool HasDef;
1187 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1188 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1189 };
Evan Cheng063284c2008-02-21 00:34:19 +00001190
Dan Gohman844731a2008-05-13 00:00:25 +00001191 struct RewriteInfoCompare {
1192 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1193 return LHS.Index < RHS.Index;
1194 }
1195 };
1196}
Evan Cheng063284c2008-02-21 00:34:19 +00001197
Evan Chengf2fbca62007-11-12 06:35:08 +00001198void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001199rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001200 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001201 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001202 unsigned Slot, int LdSlot,
1203 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001204 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001205 const TargetRegisterClass* rc,
1206 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001207 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001208 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001209 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001210 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001211 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1212 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001213 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001214 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001215 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001216 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001217 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001218
Evan Cheng063284c2008-02-21 00:34:19 +00001219 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001220 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001221 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001222 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1223 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001224 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001225 MachineOperand &O = ri.getOperand();
1226 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001227 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001228 unsigned index = getInstructionIndex(MI);
1229 if (index < start || index >= end)
1230 continue;
1231 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1232 }
1233 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1234
Evan Cheng313d4b82008-02-23 00:33:04 +00001235 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001236 // Now rewrite the defs and uses.
1237 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1238 RewriteInfo &rwi = RewriteMIs[i];
1239 ++i;
1240 unsigned index = rwi.Index;
1241 bool MIHasUse = rwi.HasUse;
1242 bool MIHasDef = rwi.HasDef;
1243 MachineInstr *MI = rwi.MI;
1244 // If MI def and/or use the same register multiple times, then there
1245 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001246 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001247 while (i != e && RewriteMIs[i].MI == MI) {
1248 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001249 bool isUse = RewriteMIs[i].HasUse;
1250 if (isUse) ++NumUses;
1251 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001252 MIHasDef |= RewriteMIs[i].HasDef;
1253 ++i;
1254 }
Evan Cheng81a03822007-11-17 00:40:40 +00001255 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001256
Evan Cheng0a891ed2008-05-23 23:00:04 +00001257 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001258 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001259 // register interval's spill weight to HUGE_VALF to prevent it from
1260 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001261 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001262 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001263 }
1264
Evan Cheng063284c2008-02-21 00:34:19 +00001265 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001266 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001267 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001268 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001269 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001270 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001271 // One common case:
1272 // x = use
1273 // ...
1274 // ...
1275 // def = ...
1276 // = use
1277 // It's better to start a new interval to avoid artifically
1278 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001279 if (MIHasDef && !MIHasUse) {
1280 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001281 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001282 }
1283 }
Evan Chengcada2452007-11-28 01:28:46 +00001284 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001285
1286 bool IsNew = ThisVReg == 0;
1287 if (IsNew) {
1288 // This ends the previous live interval. If all of its def / use
1289 // can be folded, give it a low spill weight.
1290 if (NewVReg && TrySplit && AllCanFold) {
1291 LiveInterval &nI = getOrCreateInterval(NewVReg);
1292 nI.weight /= 10.0F;
1293 }
1294 AllCanFold = true;
1295 }
1296 NewVReg = ThisVReg;
1297
Evan Cheng81a03822007-11-17 00:40:40 +00001298 bool HasDef = false;
1299 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001300 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001301 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1302 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1303 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
1304 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001305 if (!HasDef && !HasUse)
1306 continue;
1307
Evan Cheng018f9b02007-12-05 03:22:34 +00001308 AllCanFold &= CanFold;
1309
Evan Cheng81a03822007-11-17 00:40:40 +00001310 // Update weight of spill interval.
1311 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001312 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001313 // The spill weight is now infinity as it cannot be spilled again.
1314 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001315 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001316 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001317
1318 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001319 if (HasDef) {
1320 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001321 bool HasKill = false;
1322 if (!HasUse)
1323 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1324 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001325 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001326 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001327 if (VNI)
1328 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1329 }
Evan Chenge3110d02007-12-01 04:42:39 +00001330 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1331 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001332 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001333 if (SII == SpillIdxes.end()) {
1334 std::vector<SRInfo> S;
1335 S.push_back(SRInfo(index, NewVReg, true));
1336 SpillIdxes.insert(std::make_pair(MBBId, S));
1337 } else if (SII->second.back().vreg != NewVReg) {
1338 SII->second.push_back(SRInfo(index, NewVReg, true));
1339 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001340 // If there is an earlier def and this is a two-address
1341 // instruction, then it's not possible to fold the store (which
1342 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001343 SRInfo &Info = SII->second.back();
1344 Info.index = index;
1345 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001346 }
1347 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001348 } else if (SII != SpillIdxes.end() &&
1349 SII->second.back().vreg == NewVReg &&
1350 (int)index > SII->second.back().index) {
1351 // There is an earlier def that's not killed (must be two-address).
1352 // The spill is no longer needed.
1353 SII->second.pop_back();
1354 if (SII->second.empty()) {
1355 SpillIdxes.erase(MBBId);
1356 SpillMBBs.reset(MBBId);
1357 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001358 }
1359 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001360 }
1361
1362 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001363 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001364 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001365 if (SII != SpillIdxes.end() &&
1366 SII->second.back().vreg == NewVReg &&
1367 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001368 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001369 SII->second.back().canFold = false;
1370 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001371 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001372 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001373 // If we are splitting live intervals, only fold if it's the first
1374 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001375 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001376 else if (IsNew) {
1377 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001378 if (RII == RestoreIdxes.end()) {
1379 std::vector<SRInfo> Infos;
1380 Infos.push_back(SRInfo(index, NewVReg, true));
1381 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1382 } else {
1383 RII->second.push_back(SRInfo(index, NewVReg, true));
1384 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001385 RestoreMBBs.set(MBBId);
1386 }
1387 }
1388
1389 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001390 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001391 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001392 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001393
1394 if (NewVReg && TrySplit && AllCanFold) {
1395 // If all of its def / use can be folded, give it a low spill weight.
1396 LiveInterval &nI = getOrCreateInterval(NewVReg);
1397 nI.weight /= 10.0F;
1398 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001399}
1400
Evan Cheng1953d0c2007-11-29 10:12:14 +00001401bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1402 BitVector &RestoreMBBs,
1403 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1404 if (!RestoreMBBs[Id])
1405 return false;
1406 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1407 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1408 if (Restores[i].index == index &&
1409 Restores[i].vreg == vr &&
1410 Restores[i].canFold)
1411 return true;
1412 return false;
1413}
1414
1415void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1416 BitVector &RestoreMBBs,
1417 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1418 if (!RestoreMBBs[Id])
1419 return;
1420 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1421 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1422 if (Restores[i].index == index && Restores[i].vreg)
1423 Restores[i].index = -1;
1424}
Evan Cheng81a03822007-11-17 00:40:40 +00001425
Evan Cheng4cce6b42008-04-11 17:53:36 +00001426/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1427/// spilled and create empty intervals for their uses.
1428void
1429LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1430 const TargetRegisterClass* rc,
1431 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001432 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1433 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001434 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001435 MachineInstr *MI = &*ri;
1436 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001437 if (O.isDef()) {
1438 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1439 "Register def was not rewritten?");
1440 RemoveMachineInstrFromMaps(MI);
1441 vrm.RemoveMachineInstrFromMaps(MI);
1442 MI->eraseFromParent();
1443 } else {
1444 // This must be an use of an implicit_def so it's not part of the live
1445 // interval. Create a new empty live interval for it.
1446 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1447 unsigned NewVReg = mri_->createVirtualRegister(rc);
1448 vrm.grow();
1449 vrm.setIsImplicitlyDefined(NewVReg);
1450 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1451 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1452 MachineOperand &MO = MI->getOperand(i);
1453 if (MO.isReg() && MO.getReg() == li.reg)
1454 MO.setReg(NewVReg);
1455 }
1456 }
Evan Cheng419852c2008-04-03 16:39:43 +00001457 }
1458}
1459
Evan Cheng81a03822007-11-17 00:40:40 +00001460
Evan Chengf2fbca62007-11-12 06:35:08 +00001461std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001462addIntervalsForSpills(const LiveInterval &li,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001463 const MachineLoopInfo *loopInfo, VirtRegMap &vrm,
1464 float &SSWeight) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001465 assert(li.weight != HUGE_VALF &&
1466 "attempt to spill already spilled interval!");
1467
1468 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001469 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001470 DOUT << '\n';
1471
Evan Cheng9c3c2212008-06-06 07:54:39 +00001472 // Spill slot weight.
1473 SSWeight = 0.0f;
1474
Evan Cheng81a03822007-11-17 00:40:40 +00001475 // Each bit specify whether it a spill is required in the MBB.
1476 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001477 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001478 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001479 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1480 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001481 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001482 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001483
1484 unsigned NumValNums = li.getNumValNums();
1485 SmallVector<MachineInstr*, 4> ReMatDefs;
1486 ReMatDefs.resize(NumValNums, NULL);
1487 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1488 ReMatOrigDefs.resize(NumValNums, NULL);
1489 SmallVector<int, 4> ReMatIds;
1490 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1491 BitVector ReMatDelete(NumValNums);
1492 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1493
Evan Cheng81a03822007-11-17 00:40:40 +00001494 // Spilling a split live interval. It cannot be split any further. Also,
1495 // it's also guaranteed to be a single val# / range interval.
1496 if (vrm.getPreSplitReg(li.reg)) {
1497 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001498 // Unset the split kill marker on the last use.
1499 unsigned KillIdx = vrm.getKillPoint(li.reg);
1500 if (KillIdx) {
1501 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1502 assert(KillMI && "Last use disappeared?");
1503 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1504 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001505 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001506 }
Evan Chengadf85902007-12-05 09:51:10 +00001507 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001508 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1509 Slot = vrm.getStackSlot(li.reg);
1510 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1511 MachineInstr *ReMatDefMI = DefIsReMat ?
1512 vrm.getReMaterializedMI(li.reg) : NULL;
1513 int LdSlot = 0;
1514 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1515 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001516 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001517 bool IsFirstRange = true;
1518 for (LiveInterval::Ranges::const_iterator
1519 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1520 // If this is a split live interval with multiple ranges, it means there
1521 // are two-address instructions that re-defined the value. Only the
1522 // first def can be rematerialized!
1523 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001524 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001525 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1526 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001527 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001528 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001529 MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001530 } else {
1531 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1532 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001533 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001534 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001535 MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001536 }
1537 IsFirstRange = false;
1538 }
Evan Cheng419852c2008-04-03 16:39:43 +00001539
Evan Cheng9c3c2212008-06-06 07:54:39 +00001540 SSWeight = 0.0f; // Already accounted for when split.
Evan Cheng4cce6b42008-04-11 17:53:36 +00001541 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001542 return NewLIs;
1543 }
1544
1545 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001546 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1547 TrySplit = false;
1548 if (TrySplit)
1549 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001550 bool NeedStackSlot = false;
1551 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1552 i != e; ++i) {
1553 const VNInfo *VNI = *i;
1554 unsigned VN = VNI->id;
1555 unsigned DefIdx = VNI->def;
1556 if (DefIdx == ~1U)
1557 continue; // Dead val#.
1558 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001559 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1560 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001561 bool dummy;
1562 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001563 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001564 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001565 // Original def may be modified so we have to make a copy here. vrm must
1566 // delete these!
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001567 ReMatDefs[VN] = ReMatDefMI = mf_->CloneMachineInstr(ReMatDefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001568
1569 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001570 if (VNI->hasPHIKill) {
1571 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001572 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001573 CanDelete = false;
1574 // Need a stack slot if there is any live range where uses cannot be
1575 // rematerialized.
1576 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001577 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001578 if (CanDelete)
1579 ReMatDelete.set(VN);
1580 } else {
1581 // Need a stack slot if there is any live range where uses cannot be
1582 // rematerialized.
1583 NeedStackSlot = true;
1584 }
1585 }
1586
1587 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001588 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001589 Slot = vrm.assignVirt2StackSlot(li.reg);
1590
1591 // Create new intervals and rewrite defs and uses.
1592 for (LiveInterval::Ranges::const_iterator
1593 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001594 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1595 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1596 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001597 bool CanDelete = ReMatDelete[I->valno->id];
1598 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001599 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001600 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001601 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001602 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001603 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001604 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001605 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001606 MBBVRegsMap, NewLIs, SSWeight);
Evan Chengf2fbca62007-11-12 06:35:08 +00001607 }
1608
Evan Cheng0cbb1162007-11-29 01:06:25 +00001609 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001610 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001611 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001612 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001613 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001614
Evan Chengb50bb8c2007-12-05 08:16:32 +00001615 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001616 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001617 if (NeedStackSlot) {
1618 int Id = SpillMBBs.find_first();
1619 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001620 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1621 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001622 std::vector<SRInfo> &spills = SpillIdxes[Id];
1623 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1624 int index = spills[i].index;
1625 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001626 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001627 bool isReMat = vrm.isReMaterialized(VReg);
1628 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001629 bool CanFold = false;
1630 bool FoundUse = false;
1631 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001632 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001633 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001634 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1635 MachineOperand &MO = MI->getOperand(j);
1636 if (!MO.isRegister() || MO.getReg() != VReg)
1637 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001638
1639 Ops.push_back(j);
1640 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001641 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001642 if (isReMat ||
1643 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1644 RestoreMBBs, RestoreIdxes))) {
1645 // MI has two-address uses of the same register. If the use
1646 // isn't the first and only use in the BB, then we can't fold
1647 // it. FIXME: Move this to rewriteInstructionsForSpills.
1648 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001649 break;
1650 }
Evan Chengaee4af62007-12-02 08:30:39 +00001651 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001652 }
1653 }
1654 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001655 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001656 if (CanFold && !Ops.empty()) {
1657 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001658 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001659 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001660 // Also folded uses, do not issue a load.
1661 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001662 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1663 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001664 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001665 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001666 }
1667
Evan Cheng7e073ba2008-04-09 20:57:25 +00001668 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001669 if (!Folded) {
1670 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1671 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001672 if (!MI->registerDefIsDead(nI.reg))
1673 // No need to spill a dead def.
1674 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001675 if (isKill)
1676 AddedKill.insert(&nI);
1677 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001678
1679 // Update spill slot weight.
1680 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001681 SSWeight += getSpillWeight(true, false, loopDepth);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001682 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001683 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001684 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001685 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001686
Evan Cheng1953d0c2007-11-29 10:12:14 +00001687 int Id = RestoreMBBs.find_first();
1688 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001689 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1690 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
1691
Evan Cheng1953d0c2007-11-29 10:12:14 +00001692 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1693 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1694 int index = restores[i].index;
1695 if (index == -1)
1696 continue;
1697 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001698 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001699 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001700 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001701 bool CanFold = false;
1702 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001703 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001704 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001705 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1706 MachineOperand &MO = MI->getOperand(j);
1707 if (!MO.isRegister() || MO.getReg() != VReg)
1708 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001709
Evan Cheng0cbb1162007-11-29 01:06:25 +00001710 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001711 // If this restore were to be folded, it would have been folded
1712 // already.
1713 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001714 break;
1715 }
Evan Chengaee4af62007-12-02 08:30:39 +00001716 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001717 }
1718 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001719
1720 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001721 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001722 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001723 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001724 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1725 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001726 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1727 int LdSlot = 0;
1728 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1729 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001730 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001731 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1732 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001733 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1734 if (ImpUse) {
1735 // Re-matting an instruction with virtual register use. Add the
1736 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001737 // interval's spill weight to HUGE_VALF to prevent it from being
1738 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001739 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001740 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001741 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1742 }
Evan Chengaee4af62007-12-02 08:30:39 +00001743 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001744 }
1745 // If folding is not possible / failed, then tell the spiller to issue a
1746 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001747 if (Folded)
1748 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001749 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001750 vrm.addRestorePoint(VReg, MI);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001751
1752 // Update spill slot weight.
1753 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001754 SSWeight += getSpillWeight(false, true, loopDepth);
Evan Cheng81a03822007-11-17 00:40:40 +00001755 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001756 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001757 }
1758
Evan Chengb50bb8c2007-12-05 08:16:32 +00001759 // Finalize intervals: add kills, finalize spill weights, and filter out
1760 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001761 std::vector<LiveInterval*> RetNewLIs;
1762 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1763 LiveInterval *LI = NewLIs[i];
1764 if (!LI->empty()) {
1765 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001766 if (!AddedKill.count(LI)) {
1767 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001768 unsigned LastUseIdx = getBaseIndex(LR->end);
1769 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001770 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001771 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001772 if (LastUse->getOperand(UseIdx).isImplicit() ||
1773 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001774 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001775 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001776 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001777 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001778 RetNewLIs.push_back(LI);
1779 }
1780 }
Evan Cheng81a03822007-11-17 00:40:40 +00001781
Evan Cheng4cce6b42008-04-11 17:53:36 +00001782 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001783 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001784}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001785
1786/// hasAllocatableSuperReg - Return true if the specified physical register has
1787/// any super register that's allocatable.
1788bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1789 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1790 if (allocatableRegs_[*AS] && hasInterval(*AS))
1791 return true;
1792 return false;
1793}
1794
1795/// getRepresentativeReg - Find the largest super register of the specified
1796/// physical register.
1797unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1798 // Find the largest super-register that is allocatable.
1799 unsigned BestReg = Reg;
1800 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1801 unsigned SuperReg = *AS;
1802 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1803 BestReg = SuperReg;
1804 break;
1805 }
1806 }
1807 return BestReg;
1808}
1809
1810/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1811/// specified interval that conflicts with the specified physical register.
1812unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1813 unsigned PhysReg) const {
1814 unsigned NumConflicts = 0;
1815 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1816 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1817 E = mri_->reg_end(); I != E; ++I) {
1818 MachineOperand &O = I.getOperand();
1819 MachineInstr *MI = O.getParent();
1820 unsigned Index = getInstructionIndex(MI);
1821 if (pli.liveAt(Index))
1822 ++NumConflicts;
1823 }
1824 return NumConflicts;
1825}
1826
1827/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1828/// around all defs and uses of the specified interval.
1829void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1830 unsigned PhysReg, VirtRegMap &vrm) {
1831 unsigned SpillReg = getRepresentativeReg(PhysReg);
1832
1833 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1834 // If there are registers which alias PhysReg, but which are not a
1835 // sub-register of the chosen representative super register. Assert
1836 // since we can't handle it yet.
1837 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1838 tri_->isSuperRegister(*AS, SpillReg));
1839
1840 LiveInterval &pli = getInterval(SpillReg);
1841 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1842 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1843 E = mri_->reg_end(); I != E; ++I) {
1844 MachineOperand &O = I.getOperand();
1845 MachineInstr *MI = O.getParent();
1846 if (SeenMIs.count(MI))
1847 continue;
1848 SeenMIs.insert(MI);
1849 unsigned Index = getInstructionIndex(MI);
1850 if (pli.liveAt(Index)) {
1851 vrm.addEmergencySpill(SpillReg, MI);
1852 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1853 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1854 if (!hasInterval(*AS))
1855 continue;
1856 LiveInterval &spli = getInterval(*AS);
1857 if (spli.liveAt(Index))
1858 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1859 }
1860 }
1861 }
1862}
Owen Andersonc4dc1322008-06-05 17:15:43 +00001863
1864LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
1865 MachineInstr* startInst) {
1866 LiveInterval& Interval = getOrCreateInterval(reg);
1867 VNInfo* VN = Interval.getNextValue(
1868 getInstructionIndex(startInst) + InstrSlots::DEF,
1869 startInst, getVNInfoAllocator());
1870 VN->hasPHIKill = true;
1871 VN->kills.push_back(getMBBEndIdx(startInst->getParent()));
1872 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
1873 getMBBEndIdx(startInst->getParent()) + 1, VN);
1874 Interval.addRange(LR);
1875
1876 return LR;
1877}