blob: f57cd2b7d189aa92f7636eb244bb4e85981174a3 [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 Cheng1ed99222008-07-19 00:37:25 +000075 while (!ClonedMIs.empty()) {
76 MachineInstr *MI = ClonedMIs.back();
77 ClonedMIs.pop_back();
78 mf_->DeleteMachineInstr(MI);
79 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000080}
81
Owen Anderson80b3ce62008-05-28 20:54:50 +000082void LiveIntervals::computeNumbering() {
83 Index2MiMap OldI2MI = i2miMap_;
84
85 Idx2MBBMap.clear();
86 MBB2IdxMap.clear();
87 mi2iMap_.clear();
88 i2miMap_.clear();
89
Chris Lattner428b92e2006-09-15 03:57:23 +000090 // Number MachineInstrs and MachineBasicBlocks.
91 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000092 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000093
94 unsigned MIIndex = 0;
95 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
96 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000097 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000098
Chris Lattner428b92e2006-09-15 03:57:23 +000099 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
100 I != E; ++I) {
101 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000102 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000103 i2miMap_.push_back(I);
104 MIIndex += InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000105 }
106
107 if (StartIdx == MIIndex) {
108 // Empty MBB
Owen Anderson1fbb4542008-06-16 16:58:24 +0000109 MIIndex += InstrSlots::NUM;
110 i2miMap_.push_back(0);
Owen Anderson35578012008-06-16 07:10:49 +0000111 }
Owen Anderson1fbb4542008-06-16 16:58:24 +0000112 // Set the MBB2IdxMap entry for this MBB.
113 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
114 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000115 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000116 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000117
118 if (!OldI2MI.empty())
Owen Anderson29b03992008-06-19 05:29:34 +0000119 for (iterator I = begin(), E = end(); I != E; ++I)
120 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
121 LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000122
Owen Anderson7eec0c22008-05-29 23:01:22 +0000123 // Remap the start index of the live range to the corresponding new
124 // number, or our best guess at what it _should_ correspond to if the
125 // original instruction has been erased. This is either the following
126 // instruction or its predecessor.
127 unsigned offset = LI->start % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000128 if (OldI2MI[LI->start / InstrSlots::NUM])
129 LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
130 else {
131 unsigned i = 0;
132 MachineInstr* newInstr = 0;
133 do {
134 newInstr = OldI2MI[LI->start / InstrSlots::NUM + i];
135 i++;
136 } while (!newInstr);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000137
Owen Anderson29b03992008-06-19 05:29:34 +0000138 if (mi2iMap_[newInstr] ==
139 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
140 LI->start = mi2iMap_[newInstr];
141 else
142 LI->start = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000143 }
144
145 // Remap the ending index in the same way that we remapped the start,
146 // except for the final step where we always map to the immediately
147 // following instruction.
Owen Anderson29b03992008-06-19 05:29:34 +0000148 if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
149 offset = LI->end % InstrSlots::NUM;
150 if (OldI2MI[LI->end / InstrSlots::NUM])
151 LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
152 else {
153 unsigned i = 0;
154 MachineInstr* newInstr = 0;
155 do {
156 newInstr = OldI2MI[LI->end / InstrSlots::NUM + i];
157 i++;
158 } while (!newInstr);
159
160 LI->end = mi2iMap_[newInstr];
161 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000162 } else {
Owen Anderson29b03992008-06-19 05:29:34 +0000163 LI->end = i2miMap_.size() * InstrSlots::NUM;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000164 }
Owen Anderson745825f42008-05-28 22:40:08 +0000165
Owen Anderson7eec0c22008-05-29 23:01:22 +0000166 // Remap the VNInfo def index, which works the same as the
167 // start indices above.
Owen Anderson745825f42008-05-28 22:40:08 +0000168 VNInfo* vni = LI->valno;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000169 offset = vni->def % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000170 if (OldI2MI[vni->def / InstrSlots::NUM])
171 vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
172 else {
173 unsigned i = 0;
174 MachineInstr* newInstr = 0;
175 do {
176 newInstr = OldI2MI[vni->def / InstrSlots::NUM + i];
177 i++;
178 } while (!newInstr);
Owen Anderson7eec0c22008-05-29 23:01:22 +0000179
Owen Anderson29b03992008-06-19 05:29:34 +0000180 if (mi2iMap_[newInstr] ==
181 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
182 vni->def = mi2iMap_[newInstr];
183 else
184 vni->def = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000185 }
Owen Anderson745825f42008-05-28 22:40:08 +0000186
Owen Anderson7eec0c22008-05-29 23:01:22 +0000187 // Remap the VNInfo kill indices, which works the same as
188 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000189 for (size_t i = 0; i < vni->kills.size(); ++i) {
190 offset = vni->kills[i] % InstrSlots::NUM;
Owen Anderson29b03992008-06-19 05:29:34 +0000191 if (OldI2MI[vni->kills[i] / InstrSlots::NUM])
192 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
193 offset;
194 else {
195 unsigned e = 0;
196 MachineInstr* newInstr = 0;
197 do {
198 newInstr = OldI2MI[vni->kills[i] / InstrSlots::NUM + e];
199 e++;
200 } while (!newInstr);
201
202 vni->kills[i] = mi2iMap_[newInstr];
Owen Anderson7eec0c22008-05-29 23:01:22 +0000203 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000204 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000205 }
206}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000207
Owen Anderson80b3ce62008-05-28 20:54:50 +0000208/// runOnMachineFunction - Register allocate the whole function
209///
210bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
211 mf_ = &fn;
212 mri_ = &mf_->getRegInfo();
213 tm_ = &fn.getTarget();
214 tri_ = tm_->getRegisterInfo();
215 tii_ = tm_->getInstrInfo();
216 lv_ = &getAnalysis<LiveVariables>();
217 allocatableRegs_ = tri_->getAllocatableSet(fn);
218
219 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000220 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000221
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222 numIntervals += getNumIntervals();
223
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000224 DOUT << "********** INTERVALS **********\n";
225 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000226 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000227 DOUT << "\n";
228 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000229
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000230 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000231 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000232 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000233}
234
Chris Lattner70ca3582004-09-30 15:59:17 +0000235/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000236void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000237 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000238 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Evan Cheng3f32d652008-06-04 09:18:41 +0000239 I->second.print(O, tri_);
240 O << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000241 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000242
243 O << "********** MACHINEINSTRS **********\n";
244 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
245 mbbi != mbbe; ++mbbi) {
246 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
247 for (MachineBasicBlock::iterator mii = mbbi->begin(),
248 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000249 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000250 }
251 }
252}
253
Evan Chengc92da382007-11-03 07:20:12 +0000254/// conflictsWithPhysRegDef - Returns true if the specified register
255/// is defined during the duration of the specified interval.
256bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
257 VirtRegMap &vrm, unsigned reg) {
258 for (LiveInterval::Ranges::const_iterator
259 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
260 for (unsigned index = getBaseIndex(I->start),
261 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
262 index += InstrSlots::NUM) {
263 // skip deleted instructions
264 while (index != end && !getInstructionFromIndex(index))
265 index += InstrSlots::NUM;
266 if (index == end) break;
267
268 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000269 unsigned SrcReg, DstReg;
270 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
271 if (SrcReg == li.reg || DstReg == li.reg)
272 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000273 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
274 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000275 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000276 continue;
277 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000278 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000279 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000280 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000281 if (!vrm.hasPhys(PhysReg))
282 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000283 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000284 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000285 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000286 return true;
287 }
288 }
289 }
290
291 return false;
292}
293
Evan Cheng549f27d32007-08-13 23:45:17 +0000294void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000295 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000296 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000297 else
298 cerr << "%reg" << reg;
299}
300
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000301void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000302 MachineBasicBlock::iterator mi,
Owen Anderson6b098de2008-06-25 23:39:39 +0000303 unsigned MIIdx, MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000304 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000305 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000306 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000308
Evan Cheng419852c2008-04-03 16:39:43 +0000309 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
310 DOUT << "is a implicit_def\n";
311 return;
312 }
313
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000314 // Virtual registers may be defined multiple times (due to phi
315 // elimination and 2-addr elimination). Much of what we do only has to be
316 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 // time we see a vreg.
318 if (interval.empty()) {
319 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000320 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000321 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000322 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000323 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000324 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000325 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000326 tii_->isMoveInstr(*mi, SrcReg, DstReg))
327 CopyMI = mi;
328 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000329
330 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000331
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332 // Loop over all of the blocks that the vreg is defined in. There are
333 // two cases we have to handle here. The most common case is a vreg
334 // whose lifetime is contained within a basic block. In this case there
335 // will be a single kill, in MBB, which comes after the definition.
336 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
337 // FIXME: what about dead vars?
338 unsigned killIdx;
339 if (vi.Kills[0] != mi)
340 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
341 else
342 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000343
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 // If the kill happens after the definition, we have an intra-block
345 // live range.
346 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000347 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000348 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000349 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000351 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000352 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000353 return;
354 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000355 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000356
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357 // The other case we handle is when a virtual register lives to the end
358 // of the defining block, potentially live across some blocks, then is
359 // live into some number of blocks, but gets killed. Start by adding a
360 // range that goes from this definition to the end of the defining block.
Owen Anderson29b03992008-06-19 05:29:34 +0000361 LiveRange NewLR(defIndex,
362 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
363 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000364 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365 interval.addRange(NewLR);
366
367 // Iterate over all of the blocks that the variable is completely
368 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
369 // live interval.
370 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
371 if (vi.AliveBlocks[i]) {
Owen Anderson31ec8412008-06-16 19:32:40 +0000372 LiveRange LR(getMBBStartIdx(i),
Evan Chengf26e8552008-06-17 20:13:36 +0000373 getMBBEndIdx(i)+1, // MBB ends at -1.
Owen Anderson31ec8412008-06-16 19:32:40 +0000374 ValNo);
375 interval.addRange(LR);
376 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000377 }
378 }
379
380 // Finally, this virtual register is live from the start of any killing
381 // block to the 'use' slot of the killing instruction.
382 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
383 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000384 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000385 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000386 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000387 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000388 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000389 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000390 }
391
392 } else {
393 // If this is the second time we see a virtual register definition, it
394 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000395 // the result of two address elimination, then the vreg is one of the
396 // def-and-use register operand.
Evan Chengef0732d2008-07-10 07:35:43 +0000397 if (mi->isRegReDefinedByTwoAddr(interval.reg, MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000398 // If this is a two-address definition, then we have already processed
399 // the live range. The only problem is that we didn't realize there
400 // are actually two values in the live interval. Because of this we
401 // need to take the LiveRegion that defines this register and split it
402 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000403 assert(interval.containsOneValue());
404 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000405 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000406
Evan Cheng4f8ff162007-08-11 00:59:19 +0000407 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000408 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000409
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000410 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000411 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000412 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000413
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000414 // Two-address vregs should always only be redefined once. This means
415 // that at this point, there should be exactly one value number in it.
416 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
417
Chris Lattner91725b72006-08-31 05:54:43 +0000418 // The new value number (#1) is defined by the instruction we claimed
419 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000420 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
421 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000422
Chris Lattner91725b72006-08-31 05:54:43 +0000423 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000424 OldValNo->def = RedefIndex;
425 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000426
427 // Add the new live interval which replaces the range for the input copy.
428 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000429 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000430 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000431 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000432
433 // If this redefinition is dead, we need to add a dummy unit live
434 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000435 if (MO.isDead())
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000436 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000438 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000439 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440
441 } else {
442 // Otherwise, this must be because of phi elimination. If this is the
443 // first redefinition of the vreg that we have seen, go back and change
444 // the live range in the PHI block to be a different value number.
445 if (interval.containsOneValue()) {
446 assert(vi.Kills.size() == 1 &&
447 "PHI elimination vreg should have one kill, the PHI itself!");
448
449 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000450 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000451 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000452 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000453 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000454 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000455 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000457 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000458 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000459
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000460 // Replace the interval with one of a NEW value number. Note that this
461 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000462 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000463 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000464 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000465 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000466 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000467 }
468
469 // In the case of PHI elimination, each variable definition is only
470 // live until the end of the block. We've already taken care of the
471 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000472 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000473
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000474 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000475 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000476 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000477 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000478 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000479 tii_->isMoveInstr(*mi, SrcReg, DstReg))
480 CopyMI = mi;
481 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000482
Owen Anderson29b03992008-06-19 05:29:34 +0000483 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000484 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000485 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000486 interval.addKill(ValNo, killIndex);
487 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000488 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000489 }
490 }
491
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000492 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493}
494
Chris Lattnerf35fef72004-07-23 21:24:19 +0000495void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000496 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000497 unsigned MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000498 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000499 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000500 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000501 // A physical register cannot be live across basic block, so its
502 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000503 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000504
Chris Lattner6b128bd2006-09-03 08:07:11 +0000505 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000506 unsigned start = getDefIndex(baseIndex);
507 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000508
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000509 // If it is not used after definition, it is considered dead at
510 // the instruction defining it. Hence its interval is:
511 // [defSlot(def), defSlot(def)+1)
Owen Anderson6b098de2008-06-25 23:39:39 +0000512 if (MO.isDead()) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000513 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000514 end = getDefIndex(start) + 1;
515 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000516 }
517
518 // If it is not dead on definition, it must be killed by a
519 // subsequent instruction. Hence its interval is:
520 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000521 while (++mi != MBB->end()) {
Owen Anderson29b03992008-06-19 05:29:34 +0000522 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000523 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000524 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000525 end = getUseIndex(baseIndex) + 1;
526 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000527 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000528 // Another instruction redefines the register before it is ever read.
529 // Then the register is essentially dead at the instruction that defines
530 // it. Hence its interval is:
531 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000532 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000533 end = getDefIndex(start) + 1;
534 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000535 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000537
538 // The only case we should have a dead physreg here without a killing or
539 // instruction where we know it's dead is if it is live-in to the function
540 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000541 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000542 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000543
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000544exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000545 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000546
Evan Cheng24a3cc42007-04-25 07:30:23 +0000547 // Already exists? Extend old live interval.
548 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000549 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000550 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000551 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000552 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000553 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000554 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000555}
556
Chris Lattnerf35fef72004-07-23 21:24:19 +0000557void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
558 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000559 unsigned MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000560 MachineOperand& MO,
561 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000562 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000563 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000564 getOrCreateInterval(MO.getReg()));
565 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000566 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000567 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000568 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000569 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000570 tii_->isMoveInstr(*MI, SrcReg, DstReg))
571 CopyMI = MI;
Owen Anderson6b098de2008-06-25 23:39:39 +0000572 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
573 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000574 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000575 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000576 // If MI also modifies the sub-register explicitly, avoid processing it
577 // more than once. Do not pass in TRI here so it checks for exact match.
578 if (!MI->modifiesRegister(*AS))
Owen Anderson6b098de2008-06-25 23:39:39 +0000579 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
580 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000581 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000582}
583
Evan Chengb371f452007-02-19 21:49:54 +0000584void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000585 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000586 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000587 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
588
589 // Look for kills, if it reaches a def before it's killed, then it shouldn't
590 // be considered a livein.
591 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000592 unsigned baseIndex = MIIdx;
593 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000594 unsigned end = start;
595 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000596 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000597 DOUT << " killed";
598 end = getUseIndex(baseIndex) + 1;
599 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000600 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000601 // Another instruction redefines the register before it is ever read.
602 // Then the register is essentially dead at the instruction that defines
603 // it. Hence its interval is:
604 // [defSlot(def), defSlot(def)+1)
605 DOUT << " dead";
606 end = getDefIndex(start) + 1;
607 goto exit;
608 }
609
610 baseIndex += InstrSlots::NUM;
611 ++mi;
612 }
613
614exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000615 // Live-in register might not be used at all.
616 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000617 if (isAlias) {
618 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000619 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000620 } else {
621 DOUT << " live through";
622 end = baseIndex;
623 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000624 }
625
Evan Chengf3bb2e62007-09-05 21:46:51 +0000626 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000627 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000628 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000629 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000630}
631
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000632/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000633/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000634/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000635/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000636void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000637 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
638 << "********** Function: "
639 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000640 // Track the index of the current machine instr.
641 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000642 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
643 MBBI != E; ++MBBI) {
644 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000645 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000646
Chris Lattner428b92e2006-09-15 03:57:23 +0000647 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000648
Dan Gohmancb406c22007-10-03 19:26:29 +0000649 // Create intervals for live-ins to this BB first.
650 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
651 LE = MBB->livein_end(); LI != LE; ++LI) {
652 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
653 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000654 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000655 if (!hasInterval(*AS))
656 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
657 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000658 }
659
Chris Lattner428b92e2006-09-15 03:57:23 +0000660 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000661 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000662
Evan Cheng438f7bc2006-11-10 08:43:01 +0000663 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000664 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
665 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000666 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000667 if (MO.isRegister() && MO.getReg() && MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000668 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000670
671 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000672 }
Owen Anderson29b03992008-06-19 05:29:34 +0000673
674 if (MBB->begin() == miEnd) MIIndex += InstrSlots::NUM; // Empty MBB
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000675 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000676}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000677
Evan Cheng4ca980e2007-10-17 02:10:22 +0000678bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000679 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000680 std::vector<IdxMBBPair>::const_iterator I =
681 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
682
683 bool ResVal = false;
684 while (I != Idx2MBBMap.end()) {
685 if (LR.end <= I->first)
686 break;
687 MBBs.push_back(I->second);
688 ResVal = true;
689 ++I;
690 }
691 return ResVal;
692}
693
694
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000695LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000696 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000697 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000698 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000699}
Evan Chengf2fbca62007-11-12 06:35:08 +0000700
Evan Chengc8d044e2008-02-15 18:24:29 +0000701/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
702/// copy field and returns the source register that defines it.
703unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
704 if (!VNI->copy)
705 return 0;
706
707 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
708 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000709 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
710 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000711 unsigned SrcReg, DstReg;
712 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
713 return SrcReg;
714 assert(0 && "Unrecognized copy instruction!");
715 return 0;
716}
Evan Chengf2fbca62007-11-12 06:35:08 +0000717
718//===----------------------------------------------------------------------===//
719// Register allocator hooks.
720//
721
Evan Chengd70dbb52008-02-22 09:24:50 +0000722/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
723/// allow one) virtual register operand, then its uses are implicitly using
724/// the register. Returns the virtual register.
725unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
726 MachineInstr *MI) const {
727 unsigned RegOp = 0;
728 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
729 MachineOperand &MO = MI->getOperand(i);
730 if (!MO.isRegister() || !MO.isUse())
731 continue;
732 unsigned Reg = MO.getReg();
733 if (Reg == 0 || Reg == li.reg)
734 continue;
735 // FIXME: For now, only remat MI with at most one register operand.
736 assert(!RegOp &&
737 "Can't rematerialize instruction with multiple register operand!");
738 RegOp = MO.getReg();
739 break;
740 }
741 return RegOp;
742}
743
744/// isValNoAvailableAt - Return true if the val# of the specified interval
745/// which reaches the given instruction also reaches the specified use index.
746bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
747 unsigned UseIdx) const {
748 unsigned Index = getInstructionIndex(MI);
749 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
750 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
751 return UI != li.end() && UI->valno == ValNo;
752}
753
Evan Chengf2fbca62007-11-12 06:35:08 +0000754/// isReMaterializable - Returns true if the definition MI of the specified
755/// val# of the specified interval is re-materializable.
756bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000757 const VNInfo *ValNo, MachineInstr *MI,
758 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000759 if (DisableReMat)
760 return false;
761
Evan Cheng5ef3a042007-12-06 00:01:56 +0000762 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000763 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000764 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000765
766 int FrameIdx = 0;
767 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000768 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000769 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
770 // this but remember this is not safe to fold into a two-address
771 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000772 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000773 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000774
Evan Chengd70dbb52008-02-22 09:24:50 +0000775 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000776 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000777 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000778
779 unsigned ImpUse = getReMatImplicitUse(li, MI);
780 if (ImpUse) {
781 const LiveInterval &ImpLi = getInterval(ImpUse);
782 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
783 re = mri_->use_end(); ri != re; ++ri) {
784 MachineInstr *UseMI = &*ri;
785 unsigned UseIdx = getInstructionIndex(UseMI);
786 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
787 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000788 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000789 return false;
790 }
791 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000792 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000793 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000794
Evan Chengdd3465e2008-02-23 01:44:27 +0000795 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000796}
797
798/// isReMaterializable - Returns true if every definition of MI of every
799/// val# of the specified interval is re-materializable.
800bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
801 isLoad = false;
802 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
803 i != e; ++i) {
804 const VNInfo *VNI = *i;
805 unsigned DefIdx = VNI->def;
806 if (DefIdx == ~1U)
807 continue; // Dead val#.
808 // Is the def for the val# rematerializable?
809 if (DefIdx == ~0u)
810 return false;
811 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
812 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000813 if (!ReMatDefMI ||
814 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000815 return false;
816 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000817 }
818 return true;
819}
820
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000821/// FilterFoldedOps - Filter out two-address use operands. Return
822/// true if it finds any issue with the operands that ought to prevent
823/// folding.
824static bool FilterFoldedOps(MachineInstr *MI,
825 SmallVector<unsigned, 2> &Ops,
826 unsigned &MRInfo,
827 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000828 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000829
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000830 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000831 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
832 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000833 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000834 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000835 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000836 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000837 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000838 MRInfo |= (unsigned)VirtRegMap::isMod;
839 else {
840 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000841 if (!MO.isImplicit() &&
842 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000843 MRInfo = VirtRegMap::isModRef;
844 continue;
845 }
846 MRInfo |= (unsigned)VirtRegMap::isRef;
847 }
848 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000849 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000850 return false;
851}
852
853
854/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
855/// slot / to reg or any rematerialized load into ith operand of specified
856/// MI. If it is successul, MI is updated with the newly created MI and
857/// returns true.
858bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
859 VirtRegMap &vrm, MachineInstr *DefMI,
860 unsigned InstrIdx,
861 SmallVector<unsigned, 2> &Ops,
862 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000863 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000864 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000865 RemoveMachineInstrFromMaps(MI);
866 vrm.RemoveMachineInstrFromMaps(MI);
867 MI->eraseFromParent();
868 ++numFolds;
869 return true;
870 }
871
872 // Filter the list of operand indexes that are to be folded. Abort if
873 // any operand will prevent folding.
874 unsigned MRInfo = 0;
875 SmallVector<unsigned, 2> FoldOps;
876 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
877 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000878
Evan Cheng427f4c12008-03-31 23:19:51 +0000879 // The only time it's safe to fold into a two address instruction is when
880 // it's folding reload and spill from / into a spill stack slot.
881 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000882 return false;
883
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000884 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
885 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000886 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000887 // Remember this instruction uses the spill slot.
888 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
889
Evan Chengf2fbca62007-11-12 06:35:08 +0000890 // Attempt to fold the memory reference into the instruction. If
891 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +0000892 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000893 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000894 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000895 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000896 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000897 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000898 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000899 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
900 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000901 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000902 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000903 return true;
904 }
905 return false;
906}
907
Evan Cheng018f9b02007-12-05 03:22:34 +0000908/// canFoldMemoryOperand - Returns true if the specified load / store
909/// folding is possible.
910bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000911 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000912 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000913 // Filter the list of operand indexes that are to be folded. Abort if
914 // any operand will prevent folding.
915 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000916 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000917 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
918 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000919
Evan Cheng3c75ba82008-04-01 21:37:32 +0000920 // It's only legal to remat for a use, not a def.
921 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000922 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000923
Evan Chengd70dbb52008-02-22 09:24:50 +0000924 return tii_->canFoldMemoryOperand(MI, FoldOps);
925}
926
Evan Cheng81a03822007-11-17 00:40:40 +0000927bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
928 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
929 for (LiveInterval::Ranges::const_iterator
930 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
931 std::vector<IdxMBBPair>::const_iterator II =
932 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
933 if (II == Idx2MBBMap.end())
934 continue;
935 if (I->end > II->first) // crossing a MBB.
936 return false;
937 MBBs.insert(II->second);
938 if (MBBs.size() > 1)
939 return false;
940 }
941 return true;
942}
943
Evan Chengd70dbb52008-02-22 09:24:50 +0000944/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
945/// interval on to-be re-materialized operands of MI) with new register.
946void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
947 MachineInstr *MI, unsigned NewVReg,
948 VirtRegMap &vrm) {
949 // There is an implicit use. That means one of the other operand is
950 // being remat'ed and the remat'ed instruction has li.reg as an
951 // use operand. Make sure we rewrite that as well.
952 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
953 MachineOperand &MO = MI->getOperand(i);
954 if (!MO.isRegister())
955 continue;
956 unsigned Reg = MO.getReg();
957 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
958 continue;
959 if (!vrm.isReMaterialized(Reg))
960 continue;
961 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000962 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
963 if (UseMO)
964 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000965 }
966}
967
Evan Chengf2fbca62007-11-12 06:35:08 +0000968/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
969/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000970bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000971rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
972 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000973 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000974 unsigned Slot, int LdSlot,
975 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000976 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000977 const TargetRegisterClass* rc,
978 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000979 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000980 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000981 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +0000982 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
983 MachineBasicBlock *MBB = MI->getParent();
984 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng018f9b02007-12-05 03:22:34 +0000985 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000986 RestartInstruction:
987 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
988 MachineOperand& mop = MI->getOperand(i);
989 if (!mop.isRegister())
990 continue;
991 unsigned Reg = mop.getReg();
992 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000993 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000994 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000995 if (Reg != li.reg)
996 continue;
997
998 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000999 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001000 int FoldSlot = Slot;
1001 if (DefIsReMat) {
1002 // If this is the rematerializable definition MI itself and
1003 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001004 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +00001005 DOUT << "\t\t\t\tErasing re-materlizable def: ";
1006 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +00001007 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001008 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001009 MI->eraseFromParent();
1010 break;
1011 }
1012
1013 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001014 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001015 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001016 if (isLoad) {
1017 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1018 FoldSS = isLoadSS;
1019 FoldSlot = LdSlot;
1020 }
1021 }
1022
Evan Chengf2fbca62007-11-12 06:35:08 +00001023 // Scan all of the operands of this instruction rewriting operands
1024 // to use NewVReg instead of li.reg as appropriate. We do this for
1025 // two reasons:
1026 //
1027 // 1. If the instr reads the same spilled vreg multiple times, we
1028 // want to reuse the NewVReg.
1029 // 2. If the instr is a two-addr instruction, we are required to
1030 // keep the src/dst regs pinned.
1031 //
1032 // Keep track of whether we replace a use and/or def so that we can
1033 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001034
Evan Cheng81a03822007-11-17 00:40:40 +00001035 HasUse = mop.isUse();
1036 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001037 SmallVector<unsigned, 2> Ops;
1038 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001039 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001040 const MachineOperand &MOj = MI->getOperand(j);
1041 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +00001042 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001043 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001044 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001045 continue;
1046 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001047 Ops.push_back(j);
1048 HasUse |= MOj.isUse();
1049 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001050 }
1051 }
1052
Evan Cheng79a796c2008-07-12 01:56:02 +00001053 if (HasUse && !li.liveAt(getUseIndex(index)))
1054 // Must be defined by an implicit def. It should not be spilled. Note,
1055 // this is for correctness reason. e.g.
1056 // 8 %reg1024<def> = IMPLICIT_DEF
1057 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1058 // The live range [12, 14) are not part of the r1024 live interval since
1059 // it's defined by an implicit def. It will not conflicts with live
1060 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001061 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001062 // the INSERT_SUBREG and both target registers that would overlap.
1063 HasUse = false;
1064
Evan Cheng9c3c2212008-06-06 07:54:39 +00001065 // Update stack slot spill weight if we are splitting.
Evan Chengc3417602008-06-21 06:45:54 +00001066 float Weight = getSpillWeight(HasDef, HasUse, loopDepth);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001067 if (!TrySplit)
1068 SSWeight += Weight;
1069
1070 if (!TryFold)
1071 CanFold = false;
1072 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001073 // Do not fold load / store here if we are splitting. We'll find an
1074 // optimal point to insert a load / store later.
1075 if (!TrySplit) {
1076 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1077 Ops, FoldSS, FoldSlot, Reg)) {
1078 // Folding the load/store can completely change the instruction in
1079 // unpredictable ways, rescan it from the beginning.
1080 HasUse = false;
1081 HasDef = false;
1082 CanFold = false;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001083 if (isRemoved(MI)) {
1084 SSWeight -= Weight;
Evan Cheng7e073ba2008-04-09 20:57:25 +00001085 break;
Evan Cheng9c3c2212008-06-06 07:54:39 +00001086 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001087 goto RestartInstruction;
1088 }
1089 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001090 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001091 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001092 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001093 }
Evan Chengcddbb832007-11-30 21:23:43 +00001094
1095 // Create a new virtual register for the spill interval.
1096 bool CreatedNewVReg = false;
1097 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001098 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001099 vrm.grow();
1100 CreatedNewVReg = true;
1101 }
1102 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001103 if (mop.isImplicit())
1104 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001105
1106 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001107 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1108 MachineOperand &mopj = MI->getOperand(Ops[j]);
1109 mopj.setReg(NewVReg);
1110 if (mopj.isImplicit())
1111 rewriteImplicitOps(li, MI, NewVReg, vrm);
1112 }
Evan Chengcddbb832007-11-30 21:23:43 +00001113
Evan Cheng81a03822007-11-17 00:40:40 +00001114 if (CreatedNewVReg) {
1115 if (DefIsReMat) {
1116 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001117 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001118 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001119 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001120 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001121 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001122 }
1123 if (!CanDelete || (HasUse && HasDef)) {
1124 // If this is a two-addr instruction then its use operands are
1125 // rematerializable but its def is not. It should be assigned a
1126 // stack slot.
1127 vrm.assignVirt2StackSlot(NewVReg, Slot);
1128 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001129 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001130 vrm.assignVirt2StackSlot(NewVReg, Slot);
1131 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001132 } else if (HasUse && HasDef &&
1133 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1134 // If this interval hasn't been assigned a stack slot (because earlier
1135 // def is a deleted remat def), do it now.
1136 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1137 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001138 }
1139
Evan Cheng313d4b82008-02-23 00:33:04 +00001140 // Re-matting an instruction with virtual register use. Add the
1141 // register as an implicit use on the use MI.
1142 if (DefIsReMat && ImpUse)
1143 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1144
Evan Chengf2fbca62007-11-12 06:35:08 +00001145 // create a new register interval for this spill / remat.
1146 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001147 if (CreatedNewVReg) {
1148 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001149 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001150 if (TrySplit)
1151 vrm.setIsSplitFromReg(NewVReg, li.reg);
1152 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001153
1154 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001155 if (CreatedNewVReg) {
1156 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1157 nI.getNextValue(~0U, 0, VNInfoAllocator));
1158 DOUT << " +" << LR;
1159 nI.addRange(LR);
1160 } else {
1161 // Extend the split live interval to this def / use.
1162 unsigned End = getUseIndex(index)+1;
1163 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1164 nI.getValNumInfo(nI.getNumValNums()-1));
1165 DOUT << " +" << LR;
1166 nI.addRange(LR);
1167 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001168 }
1169 if (HasDef) {
1170 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1171 nI.getNextValue(~0U, 0, VNInfoAllocator));
1172 DOUT << " +" << LR;
1173 nI.addRange(LR);
1174 }
Evan Cheng81a03822007-11-17 00:40:40 +00001175
Evan Chengf2fbca62007-11-12 06:35:08 +00001176 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001177 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001178 DOUT << '\n';
1179 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001180 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001181}
Evan Cheng81a03822007-11-17 00:40:40 +00001182bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001183 const VNInfo *VNI,
1184 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001185 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001186 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1187 unsigned KillIdx = VNI->kills[j];
1188 if (KillIdx > Idx && KillIdx < End)
1189 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001190 }
1191 return false;
1192}
1193
Evan Cheng063284c2008-02-21 00:34:19 +00001194/// RewriteInfo - Keep track of machine instrs that will be rewritten
1195/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001196namespace {
1197 struct RewriteInfo {
1198 unsigned Index;
1199 MachineInstr *MI;
1200 bool HasUse;
1201 bool HasDef;
1202 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1203 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1204 };
Evan Cheng063284c2008-02-21 00:34:19 +00001205
Dan Gohman844731a2008-05-13 00:00:25 +00001206 struct RewriteInfoCompare {
1207 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1208 return LHS.Index < RHS.Index;
1209 }
1210 };
1211}
Evan Cheng063284c2008-02-21 00:34:19 +00001212
Evan Chengf2fbca62007-11-12 06:35:08 +00001213void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001214rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001215 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001216 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001217 unsigned Slot, int LdSlot,
1218 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001219 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001220 const TargetRegisterClass* rc,
1221 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001222 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001223 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001224 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001225 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001226 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1227 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001228 std::vector<LiveInterval*> &NewLIs, float &SSWeight) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001229 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001230 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001231 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001232 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001233
Evan Cheng063284c2008-02-21 00:34:19 +00001234 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001235 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001236 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001237 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1238 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001239 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001240 MachineOperand &O = ri.getOperand();
1241 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001242 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001243 unsigned index = getInstructionIndex(MI);
1244 if (index < start || index >= end)
1245 continue;
Evan Cheng79a796c2008-07-12 01:56:02 +00001246 if (O.isUse() && !li.liveAt(getUseIndex(index)))
1247 // Must be defined by an implicit def. It should not be spilled. Note,
1248 // this is for correctness reason. e.g.
1249 // 8 %reg1024<def> = IMPLICIT_DEF
1250 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1251 // The live range [12, 14) are not part of the r1024 live interval since
1252 // it's defined by an implicit def. It will not conflicts with live
1253 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001254 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001255 // the INSERT_SUBREG and both target registers that would overlap.
1256 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001257 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1258 }
1259 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1260
Evan Cheng313d4b82008-02-23 00:33:04 +00001261 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001262 // Now rewrite the defs and uses.
1263 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1264 RewriteInfo &rwi = RewriteMIs[i];
1265 ++i;
1266 unsigned index = rwi.Index;
1267 bool MIHasUse = rwi.HasUse;
1268 bool MIHasDef = rwi.HasDef;
1269 MachineInstr *MI = rwi.MI;
1270 // If MI def and/or use the same register multiple times, then there
1271 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001272 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001273 while (i != e && RewriteMIs[i].MI == MI) {
1274 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001275 bool isUse = RewriteMIs[i].HasUse;
1276 if (isUse) ++NumUses;
1277 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001278 MIHasDef |= RewriteMIs[i].HasDef;
1279 ++i;
1280 }
Evan Cheng81a03822007-11-17 00:40:40 +00001281 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001282
Evan Cheng0a891ed2008-05-23 23:00:04 +00001283 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001284 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001285 // register interval's spill weight to HUGE_VALF to prevent it from
1286 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001287 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001288 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001289 }
1290
Evan Cheng063284c2008-02-21 00:34:19 +00001291 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001292 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001293 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001294 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001295 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001296 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001297 // One common case:
1298 // x = use
1299 // ...
1300 // ...
1301 // def = ...
1302 // = use
1303 // It's better to start a new interval to avoid artifically
1304 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001305 if (MIHasDef && !MIHasUse) {
1306 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001307 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001308 }
1309 }
Evan Chengcada2452007-11-28 01:28:46 +00001310 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001311
1312 bool IsNew = ThisVReg == 0;
1313 if (IsNew) {
1314 // This ends the previous live interval. If all of its def / use
1315 // can be folded, give it a low spill weight.
1316 if (NewVReg && TrySplit && AllCanFold) {
1317 LiveInterval &nI = getOrCreateInterval(NewVReg);
1318 nI.weight /= 10.0F;
1319 }
1320 AllCanFold = true;
1321 }
1322 NewVReg = ThisVReg;
1323
Evan Cheng81a03822007-11-17 00:40:40 +00001324 bool HasDef = false;
1325 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001326 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001327 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1328 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1329 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
1330 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001331 if (!HasDef && !HasUse)
1332 continue;
1333
Evan Cheng018f9b02007-12-05 03:22:34 +00001334 AllCanFold &= CanFold;
1335
Evan Cheng81a03822007-11-17 00:40:40 +00001336 // Update weight of spill interval.
1337 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001338 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001339 // The spill weight is now infinity as it cannot be spilled again.
1340 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001341 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001342 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001343
1344 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001345 if (HasDef) {
1346 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001347 bool HasKill = false;
1348 if (!HasUse)
1349 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1350 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001351 // If this is a two-address code, then this index starts a new VNInfo.
Evan Cheng3f32d652008-06-04 09:18:41 +00001352 const VNInfo *VNI = li.findDefinedVNInfo(getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001353 if (VNI)
1354 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1355 }
Evan Chenge3110d02007-12-01 04:42:39 +00001356 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1357 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001358 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001359 if (SII == SpillIdxes.end()) {
1360 std::vector<SRInfo> S;
1361 S.push_back(SRInfo(index, NewVReg, true));
1362 SpillIdxes.insert(std::make_pair(MBBId, S));
1363 } else if (SII->second.back().vreg != NewVReg) {
1364 SII->second.push_back(SRInfo(index, NewVReg, true));
1365 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001366 // If there is an earlier def and this is a two-address
1367 // instruction, then it's not possible to fold the store (which
1368 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001369 SRInfo &Info = SII->second.back();
1370 Info.index = index;
1371 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001372 }
1373 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001374 } else if (SII != SpillIdxes.end() &&
1375 SII->second.back().vreg == NewVReg &&
1376 (int)index > SII->second.back().index) {
1377 // There is an earlier def that's not killed (must be two-address).
1378 // The spill is no longer needed.
1379 SII->second.pop_back();
1380 if (SII->second.empty()) {
1381 SpillIdxes.erase(MBBId);
1382 SpillMBBs.reset(MBBId);
1383 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001384 }
1385 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001386 }
1387
1388 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001389 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001390 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001391 if (SII != SpillIdxes.end() &&
1392 SII->second.back().vreg == NewVReg &&
1393 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001394 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001395 SII->second.back().canFold = false;
1396 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001397 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001398 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001399 // If we are splitting live intervals, only fold if it's the first
1400 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001401 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001402 else if (IsNew) {
1403 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001404 if (RII == RestoreIdxes.end()) {
1405 std::vector<SRInfo> Infos;
1406 Infos.push_back(SRInfo(index, NewVReg, true));
1407 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1408 } else {
1409 RII->second.push_back(SRInfo(index, NewVReg, true));
1410 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001411 RestoreMBBs.set(MBBId);
1412 }
1413 }
1414
1415 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001416 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001417 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001418 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001419
1420 if (NewVReg && TrySplit && AllCanFold) {
1421 // If all of its def / use can be folded, give it a low spill weight.
1422 LiveInterval &nI = getOrCreateInterval(NewVReg);
1423 nI.weight /= 10.0F;
1424 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001425}
1426
Evan Cheng1953d0c2007-11-29 10:12:14 +00001427bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1428 BitVector &RestoreMBBs,
1429 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1430 if (!RestoreMBBs[Id])
1431 return false;
1432 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1433 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1434 if (Restores[i].index == index &&
1435 Restores[i].vreg == vr &&
1436 Restores[i].canFold)
1437 return true;
1438 return false;
1439}
1440
1441void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1442 BitVector &RestoreMBBs,
1443 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1444 if (!RestoreMBBs[Id])
1445 return;
1446 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1447 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1448 if (Restores[i].index == index && Restores[i].vreg)
1449 Restores[i].index = -1;
1450}
Evan Cheng81a03822007-11-17 00:40:40 +00001451
Evan Cheng4cce6b42008-04-11 17:53:36 +00001452/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1453/// spilled and create empty intervals for their uses.
1454void
1455LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1456 const TargetRegisterClass* rc,
1457 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001458 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1459 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001460 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001461 MachineInstr *MI = &*ri;
1462 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001463 if (O.isDef()) {
1464 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1465 "Register def was not rewritten?");
1466 RemoveMachineInstrFromMaps(MI);
1467 vrm.RemoveMachineInstrFromMaps(MI);
1468 MI->eraseFromParent();
1469 } else {
1470 // This must be an use of an implicit_def so it's not part of the live
1471 // interval. Create a new empty live interval for it.
1472 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1473 unsigned NewVReg = mri_->createVirtualRegister(rc);
1474 vrm.grow();
1475 vrm.setIsImplicitlyDefined(NewVReg);
1476 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1477 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1478 MachineOperand &MO = MI->getOperand(i);
1479 if (MO.isReg() && MO.getReg() == li.reg)
1480 MO.setReg(NewVReg);
1481 }
1482 }
Evan Cheng419852c2008-04-03 16:39:43 +00001483 }
1484}
1485
Evan Cheng81a03822007-11-17 00:40:40 +00001486
Evan Chengf2fbca62007-11-12 06:35:08 +00001487std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001488addIntervalsForSpills(const LiveInterval &li,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001489 const MachineLoopInfo *loopInfo, VirtRegMap &vrm,
1490 float &SSWeight) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001491 assert(li.weight != HUGE_VALF &&
1492 "attempt to spill already spilled interval!");
1493
1494 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001495 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001496 DOUT << '\n';
1497
Evan Cheng9c3c2212008-06-06 07:54:39 +00001498 // Spill slot weight.
1499 SSWeight = 0.0f;
1500
Evan Cheng81a03822007-11-17 00:40:40 +00001501 // Each bit specify whether it a spill is required in the MBB.
1502 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001503 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001504 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001505 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1506 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001507 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001508 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001509
1510 unsigned NumValNums = li.getNumValNums();
1511 SmallVector<MachineInstr*, 4> ReMatDefs;
1512 ReMatDefs.resize(NumValNums, NULL);
1513 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1514 ReMatOrigDefs.resize(NumValNums, NULL);
1515 SmallVector<int, 4> ReMatIds;
1516 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1517 BitVector ReMatDelete(NumValNums);
1518 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1519
Evan Cheng81a03822007-11-17 00:40:40 +00001520 // Spilling a split live interval. It cannot be split any further. Also,
1521 // it's also guaranteed to be a single val# / range interval.
1522 if (vrm.getPreSplitReg(li.reg)) {
1523 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001524 // Unset the split kill marker on the last use.
1525 unsigned KillIdx = vrm.getKillPoint(li.reg);
1526 if (KillIdx) {
1527 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1528 assert(KillMI && "Last use disappeared?");
1529 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1530 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001531 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001532 }
Evan Chengadf85902007-12-05 09:51:10 +00001533 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001534 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1535 Slot = vrm.getStackSlot(li.reg);
1536 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1537 MachineInstr *ReMatDefMI = DefIsReMat ?
1538 vrm.getReMaterializedMI(li.reg) : NULL;
1539 int LdSlot = 0;
1540 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1541 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001542 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001543 bool IsFirstRange = true;
1544 for (LiveInterval::Ranges::const_iterator
1545 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1546 // If this is a split live interval with multiple ranges, it means there
1547 // are two-address instructions that re-defined the value. Only the
1548 // first def can be rematerialized!
1549 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001550 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001551 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1552 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001553 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001554 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001555 MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001556 } else {
1557 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1558 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001559 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001560 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001561 MBBVRegsMap, NewLIs, SSWeight);
Evan Cheng81a03822007-11-17 00:40:40 +00001562 }
1563 IsFirstRange = false;
1564 }
Evan Cheng419852c2008-04-03 16:39:43 +00001565
Evan Cheng9c3c2212008-06-06 07:54:39 +00001566 SSWeight = 0.0f; // Already accounted for when split.
Evan Cheng4cce6b42008-04-11 17:53:36 +00001567 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001568 return NewLIs;
1569 }
1570
1571 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001572 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1573 TrySplit = false;
1574 if (TrySplit)
1575 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001576 bool NeedStackSlot = false;
1577 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1578 i != e; ++i) {
1579 const VNInfo *VNI = *i;
1580 unsigned VN = VNI->id;
1581 unsigned DefIdx = VNI->def;
1582 if (DefIdx == ~1U)
1583 continue; // Dead val#.
1584 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001585 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1586 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001587 bool dummy;
1588 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001589 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001590 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001591 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001592 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
1593 ClonedMIs.push_back(Clone);
1594 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001595
1596 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001597 if (VNI->hasPHIKill) {
1598 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001599 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001600 CanDelete = false;
1601 // Need a stack slot if there is any live range where uses cannot be
1602 // rematerialized.
1603 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001604 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001605 if (CanDelete)
1606 ReMatDelete.set(VN);
1607 } else {
1608 // Need a stack slot if there is any live range where uses cannot be
1609 // rematerialized.
1610 NeedStackSlot = true;
1611 }
1612 }
1613
1614 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001615 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001616 Slot = vrm.assignVirt2StackSlot(li.reg);
1617
1618 // Create new intervals and rewrite defs and uses.
1619 for (LiveInterval::Ranges::const_iterator
1620 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001621 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1622 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1623 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001624 bool CanDelete = ReMatDelete[I->valno->id];
1625 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001626 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001627 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001628 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001629 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001630 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001631 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001632 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001633 MBBVRegsMap, NewLIs, SSWeight);
Evan Chengf2fbca62007-11-12 06:35:08 +00001634 }
1635
Evan Cheng0cbb1162007-11-29 01:06:25 +00001636 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001637 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001638 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001639 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001640 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001641
Evan Chengb50bb8c2007-12-05 08:16:32 +00001642 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001643 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001644 if (NeedStackSlot) {
1645 int Id = SpillMBBs.find_first();
1646 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001647 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1648 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001649 std::vector<SRInfo> &spills = SpillIdxes[Id];
1650 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1651 int index = spills[i].index;
1652 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001653 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001654 bool isReMat = vrm.isReMaterialized(VReg);
1655 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001656 bool CanFold = false;
1657 bool FoundUse = false;
1658 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001659 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001660 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001661 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1662 MachineOperand &MO = MI->getOperand(j);
1663 if (!MO.isRegister() || MO.getReg() != VReg)
1664 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001665
1666 Ops.push_back(j);
1667 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001668 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001669 if (isReMat ||
1670 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1671 RestoreMBBs, RestoreIdxes))) {
1672 // MI has two-address uses of the same register. If the use
1673 // isn't the first and only use in the BB, then we can't fold
1674 // it. FIXME: Move this to rewriteInstructionsForSpills.
1675 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001676 break;
1677 }
Evan Chengaee4af62007-12-02 08:30:39 +00001678 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001679 }
1680 }
1681 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001682 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001683 if (CanFold && !Ops.empty()) {
1684 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001685 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001686 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001687 // Also folded uses, do not issue a load.
1688 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001689 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1690 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001691 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001692 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001693 }
1694
Evan Cheng7e073ba2008-04-09 20:57:25 +00001695 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001696 if (!Folded) {
1697 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1698 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001699 if (!MI->registerDefIsDead(nI.reg))
1700 // No need to spill a dead def.
1701 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001702 if (isKill)
1703 AddedKill.insert(&nI);
1704 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001705
1706 // Update spill slot weight.
1707 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001708 SSWeight += getSpillWeight(true, false, loopDepth);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001709 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001710 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001711 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001712 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001713
Evan Cheng1953d0c2007-11-29 10:12:14 +00001714 int Id = RestoreMBBs.find_first();
1715 while (Id != -1) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001716 MachineBasicBlock *MBB = mf_->getBlockNumbered(Id);
1717 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
1718
Evan Cheng1953d0c2007-11-29 10:12:14 +00001719 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1720 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1721 int index = restores[i].index;
1722 if (index == -1)
1723 continue;
1724 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001725 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001726 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001727 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001728 bool CanFold = false;
1729 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001730 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001731 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001732 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1733 MachineOperand &MO = MI->getOperand(j);
1734 if (!MO.isRegister() || MO.getReg() != VReg)
1735 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001736
Evan Cheng0cbb1162007-11-29 01:06:25 +00001737 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001738 // If this restore were to be folded, it would have been folded
1739 // already.
1740 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001741 break;
1742 }
Evan Chengaee4af62007-12-02 08:30:39 +00001743 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001744 }
1745 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001746
1747 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001748 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001749 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001750 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001751 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1752 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001753 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1754 int LdSlot = 0;
1755 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1756 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001757 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001758 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1759 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001760 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1761 if (ImpUse) {
1762 // Re-matting an instruction with virtual register use. Add the
1763 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001764 // interval's spill weight to HUGE_VALF to prevent it from being
1765 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001766 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001767 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001768 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1769 }
Evan Chengaee4af62007-12-02 08:30:39 +00001770 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001771 }
1772 // If folding is not possible / failed, then tell the spiller to issue a
1773 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001774 if (Folded)
1775 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001776 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001777 vrm.addRestorePoint(VReg, MI);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001778
1779 // Update spill slot weight.
1780 if (!isReMat)
Evan Chengc3417602008-06-21 06:45:54 +00001781 SSWeight += getSpillWeight(false, true, loopDepth);
Evan Cheng81a03822007-11-17 00:40:40 +00001782 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001783 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001784 }
1785
Evan Chengb50bb8c2007-12-05 08:16:32 +00001786 // Finalize intervals: add kills, finalize spill weights, and filter out
1787 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001788 std::vector<LiveInterval*> RetNewLIs;
1789 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1790 LiveInterval *LI = NewLIs[i];
1791 if (!LI->empty()) {
1792 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001793 if (!AddedKill.count(LI)) {
1794 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001795 unsigned LastUseIdx = getBaseIndex(LR->end);
1796 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001797 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001798 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001799 if (LastUse->getOperand(UseIdx).isImplicit() ||
1800 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001801 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001802 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001803 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001804 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001805 RetNewLIs.push_back(LI);
1806 }
1807 }
Evan Cheng81a03822007-11-17 00:40:40 +00001808
Evan Cheng4cce6b42008-04-11 17:53:36 +00001809 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001810 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001811}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001812
1813/// hasAllocatableSuperReg - Return true if the specified physical register has
1814/// any super register that's allocatable.
1815bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1816 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1817 if (allocatableRegs_[*AS] && hasInterval(*AS))
1818 return true;
1819 return false;
1820}
1821
1822/// getRepresentativeReg - Find the largest super register of the specified
1823/// physical register.
1824unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1825 // Find the largest super-register that is allocatable.
1826 unsigned BestReg = Reg;
1827 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1828 unsigned SuperReg = *AS;
1829 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1830 BestReg = SuperReg;
1831 break;
1832 }
1833 }
1834 return BestReg;
1835}
1836
1837/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1838/// specified interval that conflicts with the specified physical register.
1839unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1840 unsigned PhysReg) const {
1841 unsigned NumConflicts = 0;
1842 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1843 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1844 E = mri_->reg_end(); I != E; ++I) {
1845 MachineOperand &O = I.getOperand();
1846 MachineInstr *MI = O.getParent();
1847 unsigned Index = getInstructionIndex(MI);
1848 if (pli.liveAt(Index))
1849 ++NumConflicts;
1850 }
1851 return NumConflicts;
1852}
1853
1854/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1855/// around all defs and uses of the specified interval.
1856void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1857 unsigned PhysReg, VirtRegMap &vrm) {
1858 unsigned SpillReg = getRepresentativeReg(PhysReg);
1859
1860 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1861 // If there are registers which alias PhysReg, but which are not a
1862 // sub-register of the chosen representative super register. Assert
1863 // since we can't handle it yet.
1864 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1865 tri_->isSuperRegister(*AS, SpillReg));
1866
1867 LiveInterval &pli = getInterval(SpillReg);
1868 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1869 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1870 E = mri_->reg_end(); I != E; ++I) {
1871 MachineOperand &O = I.getOperand();
1872 MachineInstr *MI = O.getParent();
1873 if (SeenMIs.count(MI))
1874 continue;
1875 SeenMIs.insert(MI);
1876 unsigned Index = getInstructionIndex(MI);
1877 if (pli.liveAt(Index)) {
1878 vrm.addEmergencySpill(SpillReg, MI);
1879 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1880 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1881 if (!hasInterval(*AS))
1882 continue;
1883 LiveInterval &spli = getInterval(*AS);
1884 if (spli.liveAt(Index))
1885 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1886 }
1887 }
1888 }
1889}
Owen Andersonc4dc1322008-06-05 17:15:43 +00001890
1891LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
1892 MachineInstr* startInst) {
1893 LiveInterval& Interval = getOrCreateInterval(reg);
1894 VNInfo* VN = Interval.getNextValue(
1895 getInstructionIndex(startInst) + InstrSlots::DEF,
1896 startInst, getVNInfoAllocator());
1897 VN->hasPHIKill = true;
1898 VN->kills.push_back(getMBBEndIdx(startInst->getParent()));
1899 LiveRange LR(getInstructionIndex(startInst) + InstrSlots::DEF,
1900 getMBBEndIdx(startInst->getParent()) + 1, VN);
1901 Interval.addRange(LR);
1902
1903 return LR;
1904}