blob: ccf7e9d4fd4234c3c6064cfba2ae965ebfb2dfab [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)
76 delete 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,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000301 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000302 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000303 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000304
Evan Cheng419852c2008-04-03 16:39:43 +0000305 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
306 DOUT << "is a implicit_def\n";
307 return;
308 }
309
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000310 // Virtual registers may be defined multiple times (due to phi
311 // elimination and 2-addr elimination). Much of what we do only has to be
312 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313 // time we see a vreg.
314 if (interval.empty()) {
315 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000316 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000317 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000318 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000319 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000320 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000321 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000322 tii_->isMoveInstr(*mi, SrcReg, DstReg))
323 CopyMI = mi;
324 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000325
326 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000327
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328 // Loop over all of the blocks that the vreg is defined in. There are
329 // two cases we have to handle here. The most common case is a vreg
330 // whose lifetime is contained within a basic block. In this case there
331 // will be a single kill, in MBB, which comes after the definition.
332 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
333 // FIXME: what about dead vars?
334 unsigned killIdx;
335 if (vi.Kills[0] != mi)
336 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
337 else
338 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000339
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000340 // If the kill happens after the definition, we have an intra-block
341 // live range.
342 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000343 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000345 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000347 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000348 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349 return;
350 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000351 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000352
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000353 // The other case we handle is when a virtual register lives to the end
354 // of the defining block, potentially live across some blocks, then is
355 // live into some number of blocks, but gets killed. Start by adding a
356 // range that goes from this definition to the end of the defining block.
Owen Anderson29b03992008-06-19 05:29:34 +0000357 LiveRange NewLR(defIndex,
358 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
359 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000360 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 interval.addRange(NewLR);
362
363 // Iterate over all of the blocks that the variable is completely
364 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
365 // live interval.
366 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
367 if (vi.AliveBlocks[i]) {
Owen Anderson31ec8412008-06-16 19:32:40 +0000368 LiveRange LR(getMBBStartIdx(i),
Evan Chengf26e8552008-06-17 20:13:36 +0000369 getMBBEndIdx(i)+1, // MBB ends at -1.
Owen Anderson31ec8412008-06-16 19:32:40 +0000370 ValNo);
371 interval.addRange(LR);
372 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373 }
374 }
375
376 // Finally, this virtual register is live from the start of any killing
377 // block to the 'use' slot of the killing instruction.
378 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
379 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000380 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000381 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000382 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000383 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000384 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000385 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000386 }
387
388 } else {
389 // If this is the second time we see a virtual register definition, it
390 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000391 // the result of two address elimination, then the vreg is one of the
392 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000393 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000394 // If this is a two-address definition, then we have already processed
395 // the live range. The only problem is that we didn't realize there
396 // are actually two values in the live interval. Because of this we
397 // need to take the LiveRegion that defines this register and split it
398 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000399 assert(interval.containsOneValue());
400 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000401 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000402
Evan Cheng4f8ff162007-08-11 00:59:19 +0000403 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000404 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000405
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000406 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000407 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000408 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000409
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000410 // Two-address vregs should always only be redefined once. This means
411 // that at this point, there should be exactly one value number in it.
412 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
413
Chris Lattner91725b72006-08-31 05:54:43 +0000414 // The new value number (#1) is defined by the instruction we claimed
415 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000416 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
417 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000418
Chris Lattner91725b72006-08-31 05:54:43 +0000419 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000420 OldValNo->def = RedefIndex;
421 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000422
423 // Add the new live interval which replaces the range for the input copy.
424 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000425 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000427 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428
429 // If this redefinition is dead, we need to add a dummy unit live
430 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000431 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000432 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000433
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000434 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000435 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000436
437 } else {
438 // Otherwise, this must be because of phi elimination. If this is the
439 // first redefinition of the vreg that we have seen, go back and change
440 // the live range in the PHI block to be a different value number.
441 if (interval.containsOneValue()) {
442 assert(vi.Kills.size() == 1 &&
443 "PHI elimination vreg should have one kill, the PHI itself!");
444
445 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000446 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000447 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000448 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000449 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000450 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000451 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000452 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000453 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000454 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000455
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000456 // Replace the interval with one of a NEW value number. Note that this
457 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000458 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000459 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000460 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000461 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000462 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000463 }
464
465 // In the case of PHI elimination, each variable definition is only
466 // live until the end of the block. We've already taken care of the
467 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000468 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000469
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000470 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000471 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000472 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000473 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000474 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000475 tii_->isMoveInstr(*mi, SrcReg, DstReg))
476 CopyMI = mi;
477 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000478
Owen Anderson29b03992008-06-19 05:29:34 +0000479 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000480 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000481 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000482 interval.addKill(ValNo, killIndex);
483 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000484 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000485 }
486 }
487
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000488 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000489}
490
Chris Lattnerf35fef72004-07-23 21:24:19 +0000491void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000492 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000493 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000494 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000495 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000496 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000497 // A physical register cannot be live across basic block, so its
498 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000499 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000500
Chris Lattner6b128bd2006-09-03 08:07:11 +0000501 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000502 unsigned start = getDefIndex(baseIndex);
503 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000504
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000505 // If it is not used after definition, it is considered dead at
506 // the instruction defining it. Hence its interval is:
507 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000508 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000509 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000510 end = getDefIndex(start) + 1;
511 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000512 }
513
514 // If it is not dead on definition, it must be killed by a
515 // subsequent instruction. Hence its interval is:
516 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000517 while (++mi != MBB->end()) {
Owen Anderson29b03992008-06-19 05:29:34 +0000518 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000519 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000520 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000521 end = getUseIndex(baseIndex) + 1;
522 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000523 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000524 // Another instruction redefines the register before it is ever read.
525 // Then the register is essentially dead at the instruction that defines
526 // it. Hence its interval is:
527 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000528 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000529 end = getDefIndex(start) + 1;
530 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000531 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000532 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000533
534 // The only case we should have a dead physreg here without a killing or
535 // instruction where we know it's dead is if it is live-in to the function
536 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000537 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000538 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000539
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000540exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000541 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000542
Evan Cheng24a3cc42007-04-25 07:30:23 +0000543 // Already exists? Extend old live interval.
544 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000546 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000547 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000548 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000549 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000550 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000551}
552
Chris Lattnerf35fef72004-07-23 21:24:19 +0000553void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
554 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000555 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000556 MachineOperand& MO) {
557 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
558 handleVirtualRegisterDef(MBB, MI, MIIdx, MO,
559 getOrCreateInterval(MO.getReg()));
560 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000561 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000562 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000563 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000564 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000565 tii_->isMoveInstr(*MI, SrcReg, DstReg))
566 CopyMI = MI;
Owen Anderson6b098de2008-06-25 23:39:39 +0000567 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
568 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000569 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000570 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000571 // If MI also modifies the sub-register explicitly, avoid processing it
572 // more than once. Do not pass in TRI here so it checks for exact match.
573 if (!MI->modifiesRegister(*AS))
Owen Anderson6b098de2008-06-25 23:39:39 +0000574 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
575 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000576 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000577}
578
Evan Chengb371f452007-02-19 21:49:54 +0000579void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000580 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000581 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000582 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
583
584 // Look for kills, if it reaches a def before it's killed, then it shouldn't
585 // be considered a livein.
586 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000587 unsigned baseIndex = MIIdx;
588 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000589 unsigned end = start;
590 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000591 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000592 DOUT << " killed";
593 end = getUseIndex(baseIndex) + 1;
594 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000595 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000596 // Another instruction redefines the register before it is ever read.
597 // Then the register is essentially dead at the instruction that defines
598 // it. Hence its interval is:
599 // [defSlot(def), defSlot(def)+1)
600 DOUT << " dead";
601 end = getDefIndex(start) + 1;
602 goto exit;
603 }
604
605 baseIndex += InstrSlots::NUM;
606 ++mi;
607 }
608
609exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000610 // Live-in register might not be used at all.
611 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000612 if (isAlias) {
613 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000614 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000615 } else {
616 DOUT << " live through";
617 end = baseIndex;
618 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000619 }
620
Evan Chengf3bb2e62007-09-05 21:46:51 +0000621 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000622 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000623 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000624 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000625}
626
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000627/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000628/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000629/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000630/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000631void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000632 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
633 << "********** Function: "
634 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000635 // Track the index of the current machine instr.
636 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000637 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
638 MBBI != E; ++MBBI) {
639 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000640 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000641
Chris Lattner428b92e2006-09-15 03:57:23 +0000642 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000643
Dan Gohmancb406c22007-10-03 19:26:29 +0000644 // Create intervals for live-ins to this BB first.
645 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
646 LE = MBB->livein_end(); LI != LE; ++LI) {
647 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
648 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000649 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000650 if (!hasInterval(*AS))
651 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
652 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000653 }
654
Chris Lattner428b92e2006-09-15 03:57:23 +0000655 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000656 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000657
Evan Cheng438f7bc2006-11-10 08:43:01 +0000658 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000659 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
660 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000661 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000662 if (MO.isRegister() && MO.getReg() && MO.isDef())
Owen Anderson6b098de2008-06-25 23:39:39 +0000663 handleRegisterDef(MBB, MI, MIIndex, MO);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000664 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000665
666 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000667 }
Owen Anderson29b03992008-06-19 05:29:34 +0000668
669 if (MBB->begin() == miEnd) MIIndex += InstrSlots::NUM; // Empty MBB
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000670 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000671}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000672
Evan Cheng4ca980e2007-10-17 02:10:22 +0000673bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000674 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000675 std::vector<IdxMBBPair>::const_iterator I =
676 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
677
678 bool ResVal = false;
679 while (I != Idx2MBBMap.end()) {
680 if (LR.end <= I->first)
681 break;
682 MBBs.push_back(I->second);
683 ResVal = true;
684 ++I;
685 }
686 return ResVal;
687}
688
689
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000690LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000691 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000692 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000693 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000694}
Evan Chengf2fbca62007-11-12 06:35:08 +0000695
Evan Chengc8d044e2008-02-15 18:24:29 +0000696/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
697/// copy field and returns the source register that defines it.
698unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
699 if (!VNI->copy)
700 return 0;
701
702 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
703 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000704 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
705 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000706 unsigned SrcReg, DstReg;
707 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
708 return SrcReg;
709 assert(0 && "Unrecognized copy instruction!");
710 return 0;
711}
Evan Chengf2fbca62007-11-12 06:35:08 +0000712
713//===----------------------------------------------------------------------===//
714// Register allocator hooks.
715//
716
Evan Chengd70dbb52008-02-22 09:24:50 +0000717/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
718/// allow one) virtual register operand, then its uses are implicitly using
719/// the register. Returns the virtual register.
720unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
721 MachineInstr *MI) const {
722 unsigned RegOp = 0;
723 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
724 MachineOperand &MO = MI->getOperand(i);
725 if (!MO.isRegister() || !MO.isUse())
726 continue;
727 unsigned Reg = MO.getReg();
728 if (Reg == 0 || Reg == li.reg)
729 continue;
730 // FIXME: For now, only remat MI with at most one register operand.
731 assert(!RegOp &&
732 "Can't rematerialize instruction with multiple register operand!");
733 RegOp = MO.getReg();
734 break;
735 }
736 return RegOp;
737}
738
739/// isValNoAvailableAt - Return true if the val# of the specified interval
740/// which reaches the given instruction also reaches the specified use index.
741bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
742 unsigned UseIdx) const {
743 unsigned Index = getInstructionIndex(MI);
744 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
745 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
746 return UI != li.end() && UI->valno == ValNo;
747}
748
Evan Chengf2fbca62007-11-12 06:35:08 +0000749/// isReMaterializable - Returns true if the definition MI of the specified
750/// val# of the specified interval is re-materializable.
751bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000752 const VNInfo *ValNo, MachineInstr *MI,
753 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000754 if (DisableReMat)
755 return false;
756
Evan Cheng5ef3a042007-12-06 00:01:56 +0000757 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000758 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000759 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000760
761 int FrameIdx = 0;
762 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000763 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000764 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
765 // this but remember this is not safe to fold into a two-address
766 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000767 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000768 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000769
Evan Chengd70dbb52008-02-22 09:24:50 +0000770 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000771 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000772 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000773
774 unsigned ImpUse = getReMatImplicitUse(li, MI);
775 if (ImpUse) {
776 const LiveInterval &ImpLi = getInterval(ImpUse);
777 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
778 re = mri_->use_end(); ri != re; ++ri) {
779 MachineInstr *UseMI = &*ri;
780 unsigned UseIdx = getInstructionIndex(UseMI);
781 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
782 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000783 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000784 return false;
785 }
786 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000787 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000788 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000789
Evan Chengdd3465e2008-02-23 01:44:27 +0000790 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000791}
792
793/// isReMaterializable - Returns true if every definition of MI of every
794/// val# of the specified interval is re-materializable.
795bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
796 isLoad = false;
797 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
798 i != e; ++i) {
799 const VNInfo *VNI = *i;
800 unsigned DefIdx = VNI->def;
801 if (DefIdx == ~1U)
802 continue; // Dead val#.
803 // Is the def for the val# rematerializable?
804 if (DefIdx == ~0u)
805 return false;
806 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
807 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000808 if (!ReMatDefMI ||
809 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000810 return false;
811 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000812 }
813 return true;
814}
815
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000816/// FilterFoldedOps - Filter out two-address use operands. Return
817/// true if it finds any issue with the operands that ought to prevent
818/// folding.
819static bool FilterFoldedOps(MachineInstr *MI,
820 SmallVector<unsigned, 2> &Ops,
821 unsigned &MRInfo,
822 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000823 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000824
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000825 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000826 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
827 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000828 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000829 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000830 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000831 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000832 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000833 MRInfo |= (unsigned)VirtRegMap::isMod;
834 else {
835 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000836 if (!MO.isImplicit() &&
837 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000838 MRInfo = VirtRegMap::isModRef;
839 continue;
840 }
841 MRInfo |= (unsigned)VirtRegMap::isRef;
842 }
843 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000844 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000845 return false;
846}
847
848
849/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
850/// slot / to reg or any rematerialized load into ith operand of specified
851/// MI. If it is successul, MI is updated with the newly created MI and
852/// returns true.
853bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
854 VirtRegMap &vrm, MachineInstr *DefMI,
855 unsigned InstrIdx,
856 SmallVector<unsigned, 2> &Ops,
857 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000858 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000859 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000860 RemoveMachineInstrFromMaps(MI);
861 vrm.RemoveMachineInstrFromMaps(MI);
862 MI->eraseFromParent();
863 ++numFolds;
864 return true;
865 }
866
867 // Filter the list of operand indexes that are to be folded. Abort if
868 // any operand will prevent folding.
869 unsigned MRInfo = 0;
870 SmallVector<unsigned, 2> FoldOps;
871 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
872 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000873
Evan Cheng427f4c12008-03-31 23:19:51 +0000874 // The only time it's safe to fold into a two address instruction is when
875 // it's folding reload and spill from / into a spill stack slot.
876 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000877 return false;
878
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000879 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
880 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000881 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000882 // Remember this instruction uses the spill slot.
883 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
884
Evan Chengf2fbca62007-11-12 06:35:08 +0000885 // Attempt to fold the memory reference into the instruction. If
886 // we can do this, we don't need to insert spill code.
887 if (lv_)
888 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000889 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000890 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000891 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000892 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000893 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000894 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000895 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000896 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000897 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000898 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
899 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000900 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000901 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000902 return true;
903 }
904 return false;
905}
906
Evan Cheng018f9b02007-12-05 03:22:34 +0000907/// canFoldMemoryOperand - Returns true if the specified load / store
908/// folding is possible.
909bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000910 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000911 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000912 // Filter the list of operand indexes that are to be folded. Abort if
913 // any operand will prevent folding.
914 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000915 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000916 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
917 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000918
Evan Cheng3c75ba82008-04-01 21:37:32 +0000919 // It's only legal to remat for a use, not a def.
920 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000921 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000922
Evan Chengd70dbb52008-02-22 09:24:50 +0000923 return tii_->canFoldMemoryOperand(MI, FoldOps);
924}
925
Evan Cheng81a03822007-11-17 00:40:40 +0000926bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
927 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
928 for (LiveInterval::Ranges::const_iterator
929 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
930 std::vector<IdxMBBPair>::const_iterator II =
931 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
932 if (II == Idx2MBBMap.end())
933 continue;
934 if (I->end > II->first) // crossing a MBB.
935 return false;
936 MBBs.insert(II->second);
937 if (MBBs.size() > 1)
938 return false;
939 }
940 return true;
941}
942
Evan Chengd70dbb52008-02-22 09:24:50 +0000943/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
944/// interval on to-be re-materialized operands of MI) with new register.
945void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
946 MachineInstr *MI, unsigned NewVReg,
947 VirtRegMap &vrm) {
948 // There is an implicit use. That means one of the other operand is
949 // being remat'ed and the remat'ed instruction has li.reg as an
950 // use operand. Make sure we rewrite that as well.
951 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
952 MachineOperand &MO = MI->getOperand(i);
953 if (!MO.isRegister())
954 continue;
955 unsigned Reg = MO.getReg();
956 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
957 continue;
958 if (!vrm.isReMaterialized(Reg))
959 continue;
960 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000961 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
962 if (UseMO)
963 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000964 }
965}
966
Evan Chengf2fbca62007-11-12 06:35:08 +0000967/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
968/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000969bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000970rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
971 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000972 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000973 unsigned Slot, int LdSlot,
974 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000975 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000976 const TargetRegisterClass* rc,
977 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000978 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000979 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000980 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000981 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
982 MachineBasicBlock *MBB = MI->getParent();
983 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng018f9b02007-12-05 03:22:34 +0000984 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000985 RestartInstruction:
986 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
987 MachineOperand& mop = MI->getOperand(i);
988 if (!mop.isRegister())
989 continue;
990 unsigned Reg = mop.getReg();
991 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000992 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000993 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000994 if (Reg != li.reg)
995 continue;
996
997 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000998 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000999 int FoldSlot = Slot;
1000 if (DefIsReMat) {
1001 // If this is the rematerializable definition MI itself and
1002 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001003 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001004 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1005 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001006 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001007 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001008 MI->eraseFromParent();
1009 break;
1010 }
1011
1012 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001013 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001014 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001015 if (isLoad) {
1016 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1017 FoldSS = isLoadSS;
1018 FoldSlot = LdSlot;
1019 }
1020 }
1021
Evan Chengf2fbca62007-11-12 06:35:08 +00001022 // Scan all of the operands of this instruction rewriting operands
1023 // to use NewVReg instead of li.reg as appropriate. We do this for
1024 // two reasons:
1025 //
1026 // 1. If the instr reads the same spilled vreg multiple times, we
1027 // want to reuse the NewVReg.
1028 // 2. If the instr is a two-addr instruction, we are required to
1029 // keep the src/dst regs pinned.
1030 //
1031 // Keep track of whether we replace a use and/or def so that we can
1032 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001033
Evan Cheng81a03822007-11-17 00:40:40 +00001034 HasUse = mop.isUse();
1035 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001036 SmallVector<unsigned, 2> Ops;
1037 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001038 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001039 const MachineOperand &MOj = MI->getOperand(j);
1040 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +00001041 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001042 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001043 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001044 continue;
1045 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001046 Ops.push_back(j);
1047 HasUse |= MOj.isUse();
1048 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001049 }
1050 }
1051
Evan Cheng9c3c2212008-06-06 07:54:39 +00001052 // Update stack slot spill weight if we are splitting.
Evan Chengc3417602008-06-21 06:45:54 +00001053 float Weight = getSpillWeight(HasDef, HasUse, loopDepth);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001054 if (!TrySplit)
1055 SSWeight += Weight;
1056
1057 if (!TryFold)
1058 CanFold = false;
1059 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001060 // Do not fold load / store here if we are splitting. We'll find an
1061 // optimal point to insert a load / store later.
1062 if (!TrySplit) {
1063 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1064 Ops, FoldSS, FoldSlot, Reg)) {
1065 // Folding the load/store can completely change the instruction in
1066 // unpredictable ways, rescan it from the beginning.
1067 HasUse = false;
1068 HasDef = false;
1069 CanFold = false;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001070 if (isRemoved(MI)) {
1071 SSWeight -= Weight;
Evan Cheng7e073ba2008-04-09 20:57:25 +00001072 break;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001073 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001074 goto RestartInstruction;
1075 }
1076 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001077 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001078 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001079 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001080 }
Evan Chengcddbb832007-11-30 21:23:43 +00001081
1082 // Create a new virtual register for the spill interval.
1083 bool CreatedNewVReg = false;
1084 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001085 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001086 vrm.grow();
1087 CreatedNewVReg = true;
1088 }
1089 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001090 if (mop.isImplicit())
1091 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001092
1093 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001094 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1095 MachineOperand &mopj = MI->getOperand(Ops[j]);
1096 mopj.setReg(NewVReg);
1097 if (mopj.isImplicit())
1098 rewriteImplicitOps(li, MI, NewVReg, vrm);
1099 }
Evan Chengcddbb832007-11-30 21:23:43 +00001100
Evan Cheng81a03822007-11-17 00:40:40 +00001101 if (CreatedNewVReg) {
1102 if (DefIsReMat) {
1103 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001104 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001105 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001106 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001107 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001108 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001109 }
1110 if (!CanDelete || (HasUse && HasDef)) {
1111 // If this is a two-addr instruction then its use operands are
1112 // rematerializable but its def is not. It should be assigned a
1113 // stack slot.
1114 vrm.assignVirt2StackSlot(NewVReg, Slot);
1115 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001116 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001117 vrm.assignVirt2StackSlot(NewVReg, Slot);
1118 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001119 } else if (HasUse && HasDef &&
1120 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1121 // If this interval hasn't been assigned a stack slot (because earlier
1122 // def is a deleted remat def), do it now.
1123 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1124 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001125 }
1126
Evan Cheng313d4b82008-02-23 00:33:04 +00001127 // Re-matting an instruction with virtual register use. Add the
1128 // register as an implicit use on the use MI.
1129 if (DefIsReMat && ImpUse)
1130 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1131
Evan Chengf2fbca62007-11-12 06:35:08 +00001132 // create a new register interval for this spill / remat.
1133 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001134 if (CreatedNewVReg) {
1135 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001136 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001137 if (TrySplit)
1138 vrm.setIsSplitFromReg(NewVReg, li.reg);
1139 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001140
1141 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001142 if (CreatedNewVReg) {
1143 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1144 nI.getNextValue(~0U, 0, VNInfoAllocator));
1145 DOUT << " +" << LR;
1146 nI.addRange(LR);
1147 } else {
1148 // Extend the split live interval to this def / use.
1149 unsigned End = getUseIndex(index)+1;
1150 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1151 nI.getValNumInfo(nI.getNumValNums()-1));
1152 DOUT << " +" << LR;
1153 nI.addRange(LR);
1154 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001155 }
1156 if (HasDef) {
1157 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1158 nI.getNextValue(~0U, 0, VNInfoAllocator));
1159 DOUT << " +" << LR;
1160 nI.addRange(LR);
1161 }
Evan Cheng81a03822007-11-17 00:40:40 +00001162
Evan Chengf2fbca62007-11-12 06:35:08 +00001163 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001164 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001165 DOUT << '\n';
1166 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001167 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001168}
Evan Cheng81a03822007-11-17 00:40:40 +00001169bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001170 const VNInfo *VNI,
1171 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001172 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001173 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1174 unsigned KillIdx = VNI->kills[j];
1175 if (KillIdx > Idx && KillIdx < End)
1176 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001177 }
1178 return false;
1179}
1180
Evan Cheng063284c2008-02-21 00:34:19 +00001181/// RewriteInfo - Keep track of machine instrs that will be rewritten
1182/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001183namespace {
1184 struct RewriteInfo {
1185 unsigned Index;
1186 MachineInstr *MI;
1187 bool HasUse;
1188 bool HasDef;
1189 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1190 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1191 };
Evan Cheng063284c2008-02-21 00:34:19 +00001192
Dan Gohman844731a2008-05-13 00:00:25 +00001193 struct RewriteInfoCompare {
1194 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1195 return LHS.Index < RHS.Index;
1196 }
1197 };
1198}
Evan Cheng063284c2008-02-21 00:34:19 +00001199
Evan Chengf2fbca62007-11-12 06:35:08 +00001200void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001201rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001202 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001203 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001204 unsigned Slot, int LdSlot,
1205 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001206 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001207 const TargetRegisterClass* rc,
1208 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001209 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001210 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001211 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001212 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001213 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1214 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001215 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001216 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001217 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001218 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001219 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001220
Evan Cheng063284c2008-02-21 00:34:19 +00001221 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001222 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001223 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001224 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1225 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001226 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001227 MachineOperand &O = ri.getOperand();
1228 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001229 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001230 unsigned index = getInstructionIndex(MI);
1231 if (index < start || index >= end)
1232 continue;
1233 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1234 }
1235 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1236
Evan Cheng313d4b82008-02-23 00:33:04 +00001237 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001238 // Now rewrite the defs and uses.
1239 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1240 RewriteInfo &rwi = RewriteMIs[i];
1241 ++i;
1242 unsigned index = rwi.Index;
1243 bool MIHasUse = rwi.HasUse;
1244 bool MIHasDef = rwi.HasDef;
1245 MachineInstr *MI = rwi.MI;
1246 // If MI def and/or use the same register multiple times, then there
1247 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001248 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001249 while (i != e && RewriteMIs[i].MI == MI) {
1250 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001251 bool isUse = RewriteMIs[i].HasUse;
1252 if (isUse) ++NumUses;
1253 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001254 MIHasDef |= RewriteMIs[i].HasDef;
1255 ++i;
1256 }
Evan Cheng81a03822007-11-17 00:40:40 +00001257 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001258
Evan Cheng0a891ed2008-05-23 23:00:04 +00001259 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001260 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001261 // register interval's spill weight to HUGE_VALF to prevent it from
1262 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001263 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001264 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001265 }
1266
Evan Cheng063284c2008-02-21 00:34:19 +00001267 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001268 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001269 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001270 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001271 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001272 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001273 // One common case:
1274 // x = use
1275 // ...
1276 // ...
1277 // def = ...
1278 // = use
1279 // It's better to start a new interval to avoid artifically
1280 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001281 if (MIHasDef && !MIHasUse) {
1282 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001283 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001284 }
1285 }
Evan Chengcada2452007-11-28 01:28:46 +00001286 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001287
1288 bool IsNew = ThisVReg == 0;
1289 if (IsNew) {
1290 // This ends the previous live interval. If all of its def / use
1291 // can be folded, give it a low spill weight.
1292 if (NewVReg && TrySplit && AllCanFold) {
1293 LiveInterval &nI = getOrCreateInterval(NewVReg);
1294 nI.weight /= 10.0F;
1295 }
1296 AllCanFold = true;
1297 }
1298 NewVReg = ThisVReg;
1299
Evan Cheng81a03822007-11-17 00:40:40 +00001300 bool HasDef = false;
1301 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001302 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001303 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1304 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1305 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
1306 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001307 if (!HasDef && !HasUse)
1308 continue;
1309
Evan Cheng018f9b02007-12-05 03:22:34 +00001310 AllCanFold &= CanFold;
1311
Evan Cheng81a03822007-11-17 00:40:40 +00001312 // Update weight of spill interval.
1313 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001314 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001315 // The spill weight is now infinity as it cannot be spilled again.
1316 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001317 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001318 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001319
1320 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001321 if (HasDef) {
1322 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001323 bool HasKill = false;
1324 if (!HasUse)
1325 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1326 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001327 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001328 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001329 if (VNI)
1330 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1331 }
Evan Chenge3110d02007-12-01 04:42:39 +00001332 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1333 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001334 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001335 if (SII == SpillIdxes.end()) {
1336 std::vector<SRInfo> S;
1337 S.push_back(SRInfo(index, NewVReg, true));
1338 SpillIdxes.insert(std::make_pair(MBBId, S));
1339 } else if (SII->second.back().vreg != NewVReg) {
1340 SII->second.push_back(SRInfo(index, NewVReg, true));
1341 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001342 // If there is an earlier def and this is a two-address
1343 // instruction, then it's not possible to fold the store (which
1344 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001345 SRInfo &Info = SII->second.back();
1346 Info.index = index;
1347 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001348 }
1349 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001350 } else if (SII != SpillIdxes.end() &&
1351 SII->second.back().vreg == NewVReg &&
1352 (int)index > SII->second.back().index) {
1353 // There is an earlier def that's not killed (must be two-address).
1354 // The spill is no longer needed.
1355 SII->second.pop_back();
1356 if (SII->second.empty()) {
1357 SpillIdxes.erase(MBBId);
1358 SpillMBBs.reset(MBBId);
1359 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001360 }
1361 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001362 }
1363
1364 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001365 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001366 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001367 if (SII != SpillIdxes.end() &&
1368 SII->second.back().vreg == NewVReg &&
1369 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001370 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001371 SII->second.back().canFold = false;
1372 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001373 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001374 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001375 // If we are splitting live intervals, only fold if it's the first
1376 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001377 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001378 else if (IsNew) {
1379 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001380 if (RII == RestoreIdxes.end()) {
1381 std::vector<SRInfo> Infos;
1382 Infos.push_back(SRInfo(index, NewVReg, true));
1383 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1384 } else {
1385 RII->second.push_back(SRInfo(index, NewVReg, true));
1386 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001387 RestoreMBBs.set(MBBId);
1388 }
1389 }
1390
1391 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001392 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001393 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001394 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001395
1396 if (NewVReg && TrySplit && AllCanFold) {
1397 // If all of its def / use can be folded, give it a low spill weight.
1398 LiveInterval &nI = getOrCreateInterval(NewVReg);
1399 nI.weight /= 10.0F;
1400 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001401}
1402
Evan Cheng1953d0c2007-11-29 10:12:14 +00001403bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1404 BitVector &RestoreMBBs,
1405 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1406 if (!RestoreMBBs[Id])
1407 return false;
1408 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1409 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1410 if (Restores[i].index == index &&
1411 Restores[i].vreg == vr &&
1412 Restores[i].canFold)
1413 return true;
1414 return false;
1415}
1416
1417void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1418 BitVector &RestoreMBBs,
1419 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1420 if (!RestoreMBBs[Id])
1421 return;
1422 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1423 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1424 if (Restores[i].index == index && Restores[i].vreg)
1425 Restores[i].index = -1;
1426}
Evan Cheng81a03822007-11-17 00:40:40 +00001427
Evan Cheng4cce6b42008-04-11 17:53:36 +00001428/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1429/// spilled and create empty intervals for their uses.
1430void
1431LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1432 const TargetRegisterClass* rc,
1433 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001434 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1435 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001436 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001437 MachineInstr *MI = &*ri;
1438 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001439 if (O.isDef()) {
1440 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1441 "Register def was not rewritten?");
1442 RemoveMachineInstrFromMaps(MI);
1443 vrm.RemoveMachineInstrFromMaps(MI);
1444 MI->eraseFromParent();
1445 } else {
1446 // This must be an use of an implicit_def so it's not part of the live
1447 // interval. Create a new empty live interval for it.
1448 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1449 unsigned NewVReg = mri_->createVirtualRegister(rc);
1450 vrm.grow();
1451 vrm.setIsImplicitlyDefined(NewVReg);
1452 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1453 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1454 MachineOperand &MO = MI->getOperand(i);
1455 if (MO.isReg() && MO.getReg() == li.reg)
1456 MO.setReg(NewVReg);
1457 }
1458 }
Evan Cheng419852c2008-04-03 16:39:43 +00001459 }
1460}
1461
Evan Cheng81a03822007-11-17 00:40:40 +00001462
Evan Chengf2fbca62007-11-12 06:35:08 +00001463std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001464addIntervalsForSpills(const LiveInterval &li,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001465 const MachineLoopInfo *loopInfo, VirtRegMap &vrm,
1466 float &SSWeight) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001467 // Since this is called after the analysis is done we don't know if
1468 // LiveVariables is available
1469 lv_ = getAnalysisToUpdate<LiveVariables>();
1470
1471 assert(li.weight != HUGE_VALF &&
1472 "attempt to spill already spilled interval!");
1473
1474 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001475 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001476 DOUT << '\n';
1477
Evan Cheng9c3c2212008-06-06 07:54:39 +00001478 // Spill slot weight.
1479 SSWeight = 0.0f;
1480
Evan Cheng81a03822007-11-17 00:40:40 +00001481 // Each bit specify whether it a spill is required in the MBB.
1482 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001483 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001484 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001485 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1486 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001487 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001488 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001489
1490 unsigned NumValNums = li.getNumValNums();
1491 SmallVector<MachineInstr*, 4> ReMatDefs;
1492 ReMatDefs.resize(NumValNums, NULL);
1493 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1494 ReMatOrigDefs.resize(NumValNums, NULL);
1495 SmallVector<int, 4> ReMatIds;
1496 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1497 BitVector ReMatDelete(NumValNums);
1498 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1499
Evan Cheng81a03822007-11-17 00:40:40 +00001500 // Spilling a split live interval. It cannot be split any further. Also,
1501 // it's also guaranteed to be a single val# / range interval.
1502 if (vrm.getPreSplitReg(li.reg)) {
1503 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001504 // Unset the split kill marker on the last use.
1505 unsigned KillIdx = vrm.getKillPoint(li.reg);
1506 if (KillIdx) {
1507 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1508 assert(KillMI && "Last use disappeared?");
1509 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1510 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001511 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001512 }
Evan Chengadf85902007-12-05 09:51:10 +00001513 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001514 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1515 Slot = vrm.getStackSlot(li.reg);
1516 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1517 MachineInstr *ReMatDefMI = DefIsReMat ?
1518 vrm.getReMaterializedMI(li.reg) : NULL;
1519 int LdSlot = 0;
1520 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1521 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001522 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001523 bool IsFirstRange = true;
1524 for (LiveInterval::Ranges::const_iterator
1525 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1526 // If this is a split live interval with multiple ranges, it means there
1527 // are two-address instructions that re-defined the value. Only the
1528 // first def can be rematerialized!
1529 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001530 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001531 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1532 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
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 } else {
1537 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1538 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001539 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001540 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001541 MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001542 }
1543 IsFirstRange = false;
1544 }
Evan Cheng419852c2008-04-03 16:39:43 +00001545
Evan Cheng9c3c2212008-06-06 07:54:39 +00001546 SSWeight = 0.0f; // Already accounted for when split.
Evan Cheng4cce6b42008-04-11 17:53:36 +00001547 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001548 return NewLIs;
1549 }
1550
1551 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001552 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1553 TrySplit = false;
1554 if (TrySplit)
1555 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001556 bool NeedStackSlot = false;
1557 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1558 i != e; ++i) {
1559 const VNInfo *VNI = *i;
1560 unsigned VN = VNI->id;
1561 unsigned DefIdx = VNI->def;
1562 if (DefIdx == ~1U)
1563 continue; // Dead val#.
1564 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001565 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1566 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001567 bool dummy;
1568 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001569 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001570 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001571 // Original def may be modified so we have to make a copy here. vrm must
1572 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001573 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001574
1575 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001576 if (VNI->hasPHIKill) {
1577 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001578 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001579 CanDelete = false;
1580 // Need a stack slot if there is any live range where uses cannot be
1581 // rematerialized.
1582 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001583 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001584 if (CanDelete)
1585 ReMatDelete.set(VN);
1586 } else {
1587 // Need a stack slot if there is any live range where uses cannot be
1588 // rematerialized.
1589 NeedStackSlot = true;
1590 }
1591 }
1592
1593 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001594 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001595 Slot = vrm.assignVirt2StackSlot(li.reg);
1596
1597 // Create new intervals and rewrite defs and uses.
1598 for (LiveInterval::Ranges::const_iterator
1599 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001600 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1601 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1602 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001603 bool CanDelete = ReMatDelete[I->valno->id];
1604 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001605 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001606 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001607 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001608 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001609 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001610 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001611 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001612 MBBVRegsMap, NewLIs, SSWeight);
Evan Chengf2fbca62007-11-12 06:35:08 +00001613 }
1614
Evan Cheng0cbb1162007-11-29 01:06:25 +00001615 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001616 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001617 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001618 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001619 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001620
Evan Chengb50bb8c2007-12-05 08:16:32 +00001621 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001622 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001623 if (NeedStackSlot) {
1624 int Id = SpillMBBs.find_first();
1625 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001626 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1627 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001628 std::vector<SRInfo> &spills = SpillIdxes[Id];
1629 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1630 int index = spills[i].index;
1631 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001632 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001633 bool isReMat = vrm.isReMaterialized(VReg);
1634 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001635 bool CanFold = false;
1636 bool FoundUse = false;
1637 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001638 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001639 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001640 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1641 MachineOperand &MO = MI->getOperand(j);
1642 if (!MO.isRegister() || MO.getReg() != VReg)
1643 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001644
1645 Ops.push_back(j);
1646 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001647 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001648 if (isReMat ||
1649 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1650 RestoreMBBs, RestoreIdxes))) {
1651 // MI has two-address uses of the same register. If the use
1652 // isn't the first and only use in the BB, then we can't fold
1653 // it. FIXME: Move this to rewriteInstructionsForSpills.
1654 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001655 break;
1656 }
Evan Chengaee4af62007-12-02 08:30:39 +00001657 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001658 }
1659 }
1660 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001661 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001662 if (CanFold && !Ops.empty()) {
1663 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001664 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001665 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001666 // Also folded uses, do not issue a load.
1667 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001668 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1669 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001670 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001671 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001672 }
1673
Evan Cheng7e073ba2008-04-09 20:57:25 +00001674 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001675 if (!Folded) {
1676 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1677 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001678 if (!MI->registerDefIsDead(nI.reg))
1679 // No need to spill a dead def.
1680 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001681 if (isKill)
1682 AddedKill.insert(&nI);
1683 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001684
1685 // Update spill slot weight.
1686 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001687 SSWeight += getSpillWeight(true, false, loopDepth);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001688 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001689 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001690 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001691 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001692
Evan Cheng1953d0c2007-11-29 10:12:14 +00001693 int Id = RestoreMBBs.find_first();
1694 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001695 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1696 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
1697
Evan Cheng1953d0c2007-11-29 10:12:14 +00001698 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1699 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1700 int index = restores[i].index;
1701 if (index == -1)
1702 continue;
1703 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001704 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001705 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001706 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001707 bool CanFold = false;
1708 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001709 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001710 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001711 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1712 MachineOperand &MO = MI->getOperand(j);
1713 if (!MO.isRegister() || MO.getReg() != VReg)
1714 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001715
Evan Cheng0cbb1162007-11-29 01:06:25 +00001716 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001717 // If this restore were to be folded, it would have been folded
1718 // already.
1719 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001720 break;
1721 }
Evan Chengaee4af62007-12-02 08:30:39 +00001722 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001723 }
1724 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001725
1726 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001727 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001728 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001729 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001730 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1731 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001732 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1733 int LdSlot = 0;
1734 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1735 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001736 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001737 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1738 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001739 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1740 if (ImpUse) {
1741 // Re-matting an instruction with virtual register use. Add the
1742 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001743 // interval's spill weight to HUGE_VALF to prevent it from being
1744 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001745 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001746 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001747 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1748 }
Evan Chengaee4af62007-12-02 08:30:39 +00001749 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001750 }
1751 // If folding is not possible / failed, then tell the spiller to issue a
1752 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001753 if (Folded)
1754 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001755 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001756 vrm.addRestorePoint(VReg, MI);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001757
1758 // Update spill slot weight.
1759 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001760 SSWeight += getSpillWeight(false, true, loopDepth);
Evan Cheng81a03822007-11-17 00:40:40 +00001761 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001762 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001763 }
1764
Evan Chengb50bb8c2007-12-05 08:16:32 +00001765 // Finalize intervals: add kills, finalize spill weights, and filter out
1766 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001767 std::vector<LiveInterval*> RetNewLIs;
1768 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1769 LiveInterval *LI = NewLIs[i];
1770 if (!LI->empty()) {
1771 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001772 if (!AddedKill.count(LI)) {
1773 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001774 unsigned LastUseIdx = getBaseIndex(LR->end);
1775 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001776 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001777 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001778 if (LastUse->getOperand(UseIdx).isImplicit() ||
1779 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001780 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001781 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001782 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001783 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001784 RetNewLIs.push_back(LI);
1785 }
1786 }
Evan Cheng81a03822007-11-17 00:40:40 +00001787
Evan Cheng4cce6b42008-04-11 17:53:36 +00001788 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001789 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001790}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001791
1792/// hasAllocatableSuperReg - Return true if the specified physical register has
1793/// any super register that's allocatable.
1794bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1795 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1796 if (allocatableRegs_[*AS] && hasInterval(*AS))
1797 return true;
1798 return false;
1799}
1800
1801/// getRepresentativeReg - Find the largest super register of the specified
1802/// physical register.
1803unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1804 // Find the largest super-register that is allocatable.
1805 unsigned BestReg = Reg;
1806 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1807 unsigned SuperReg = *AS;
1808 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1809 BestReg = SuperReg;
1810 break;
1811 }
1812 }
1813 return BestReg;
1814}
1815
1816/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1817/// specified interval that conflicts with the specified physical register.
1818unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1819 unsigned PhysReg) const {
1820 unsigned NumConflicts = 0;
1821 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1822 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1823 E = mri_->reg_end(); I != E; ++I) {
1824 MachineOperand &O = I.getOperand();
1825 MachineInstr *MI = O.getParent();
1826 unsigned Index = getInstructionIndex(MI);
1827 if (pli.liveAt(Index))
1828 ++NumConflicts;
1829 }
1830 return NumConflicts;
1831}
1832
1833/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1834/// around all defs and uses of the specified interval.
1835void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1836 unsigned PhysReg, VirtRegMap &vrm) {
1837 unsigned SpillReg = getRepresentativeReg(PhysReg);
1838
1839 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1840 // If there are registers which alias PhysReg, but which are not a
1841 // sub-register of the chosen representative super register. Assert
1842 // since we can't handle it yet.
1843 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1844 tri_->isSuperRegister(*AS, SpillReg));
1845
1846 LiveInterval &pli = getInterval(SpillReg);
1847 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1848 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1849 E = mri_->reg_end(); I != E; ++I) {
1850 MachineOperand &O = I.getOperand();
1851 MachineInstr *MI = O.getParent();
1852 if (SeenMIs.count(MI))
1853 continue;
1854 SeenMIs.insert(MI);
1855 unsigned Index = getInstructionIndex(MI);
1856 if (pli.liveAt(Index)) {
1857 vrm.addEmergencySpill(SpillReg, MI);
1858 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1859 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1860 if (!hasInterval(*AS))
1861 continue;
1862 LiveInterval &spli = getInterval(*AS);
1863 if (spli.liveAt(Index))
1864 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1865 }
1866 }
1867 }
1868}
Owen Andersonc4dc1322008-06-05 17:15:43 +00001869
1870LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
1871 MachineInstr* startInst) {
1872 LiveInterval& Interval = getOrCreateInterval(reg);
1873 VNInfo* VN = Interval.getNextValue(
1874 getInstructionIndex(startInst) + InstrSlots::DEF,
1875 startInst, getVNInfoAllocator());
1876 VN->hasPHIKill = true;
1877 VN->kills.push_back(getMBBEndIdx(startInst->getParent()));
1878 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
1879 getMBBEndIdx(startInst->getParent()) + 1, VN);
1880 Interval.addRange(LR);
1881
1882 return LR;
1883}