blob: b4f50e19291825407aeb16adf18eb4ff781f24e2 [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"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000028#include "llvm/Target/MRegisterInfo.h"
29#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
Evan Chengbc165e42007-08-16 07:24:22 +000039namespace {
40 // Hidden options for help debugging.
41 cl::opt<bool> DisableReMat("disable-rematerialization",
42 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000043
44 cl::opt<bool> SplitAtBB("split-intervals-at-bb",
Evan Cheng33faddc2007-12-06 08:54:31 +000045 cl::init(true), cl::Hidden);
Evan Cheng0cbb1162007-11-29 01:06:25 +000046 cl::opt<int> SplitLimit("split-limit",
47 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000048}
49
Chris Lattnercd3245a2006-12-19 22:41:21 +000050STATISTIC(numIntervals, "Number of original intervals");
51STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000052STATISTIC(numFolds , "Number of loads/stores folded into instructions");
53STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000054
Devang Patel19974732007-05-03 01:11:54 +000055char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000056namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000057 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000059
Chris Lattnerf7da2c72006-08-24 22:43:55 +000060void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000061 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000063 AU.addPreservedID(MachineLoopInfoID);
64 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 AU.addPreservedID(PHIEliminationID);
66 AU.addRequiredID(PHIEliminationID);
67 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069}
70
Chris Lattnerf7da2c72006-08-24 22:43:55 +000071void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000072 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 mi2iMap_.clear();
74 i2miMap_.clear();
75 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000076 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
77 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000078 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
79 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000080}
81
Evan Cheng4ca980e2007-10-17 02:10:22 +000082namespace llvm {
83 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
84 return V < IM.first;
85 }
86
87 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
88 return IM.first < V;
89 }
90
91 struct Idx2MBBCompare {
92 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
93 return LHS.first < RHS.first;
94 }
95 };
96}
97
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000098/// runOnMachineFunction - Register allocate the whole function
99///
100bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000101 mf_ = &fn;
102 tm_ = &fn.getTarget();
103 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +0000104 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +0000106 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000107
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 // Number MachineInstrs and MachineBasicBlocks.
109 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000110 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000111
112 unsigned MIIndex = 0;
113 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
114 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000115 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000116
Chris Lattner428b92e2006-09-15 03:57:23 +0000117 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
118 I != E; ++I) {
119 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000121 i2miMap_.push_back(I);
122 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000123 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000124
125 // Set the MBB2IdxMap entry for this MBB.
126 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000127 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000128 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000129 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000130
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000132
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000133 numIntervals += getNumIntervals();
134
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000135 DOUT << "********** INTERVALS **********\n";
136 for (iterator I = begin(), E = end(); I != E; ++I) {
137 I->second.print(DOUT, mri_);
138 DOUT << "\n";
139 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000142 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000143 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144}
145
Chris Lattner70ca3582004-09-30 15:59:17 +0000146/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000147void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000148 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000149 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000150 I->second.print(DOUT, mri_);
151 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000152 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000153
154 O << "********** MACHINEINSTRS **********\n";
155 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
156 mbbi != mbbe; ++mbbi) {
157 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
158 for (MachineBasicBlock::iterator mii = mbbi->begin(),
159 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000160 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000161 }
162 }
163}
164
Evan Chengc92da382007-11-03 07:20:12 +0000165/// conflictsWithPhysRegDef - Returns true if the specified register
166/// is defined during the duration of the specified interval.
167bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
168 VirtRegMap &vrm, unsigned reg) {
169 for (LiveInterval::Ranges::const_iterator
170 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
171 for (unsigned index = getBaseIndex(I->start),
172 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
173 index += InstrSlots::NUM) {
174 // skip deleted instructions
175 while (index != end && !getInstructionFromIndex(index))
176 index += InstrSlots::NUM;
177 if (index == end) break;
178
179 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000180 unsigned SrcReg, DstReg;
181 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
182 if (SrcReg == li.reg || DstReg == li.reg)
183 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000184 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
185 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000186 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000187 continue;
188 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000189 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000190 continue;
Evan Cheng5d446262007-11-15 08:13:29 +0000191 if (MRegisterInfo::isVirtualRegister(PhysReg)) {
192 if (!vrm.hasPhys(PhysReg))
193 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000194 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000195 }
Evan Cheng5f5f3b62007-11-05 00:59:10 +0000196 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000197 return true;
198 }
199 }
200 }
201
202 return false;
203}
204
Evan Cheng549f27d32007-08-13 23:45:17 +0000205void LiveIntervals::printRegName(unsigned reg) const {
206 if (MRegisterInfo::isPhysicalRegister(reg))
207 cerr << mri_->getName(reg);
208 else
209 cerr << "%reg" << reg;
210}
211
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000212void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000213 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000214 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000215 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000216 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000217 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000218
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000219 // Virtual registers may be defined multiple times (due to phi
220 // elimination and 2-addr elimination). Much of what we do only has to be
221 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222 // time we see a vreg.
223 if (interval.empty()) {
224 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000225 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000226 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000227 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000228 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000229 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng48ff2822007-10-12 17:16:50 +0000230 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000231 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
232 VNInfoAllocator);
233 else
234 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000235
236 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000237
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 // Loop over all of the blocks that the vreg is defined in. There are
239 // two cases we have to handle here. The most common case is a vreg
240 // whose lifetime is contained within a basic block. In this case there
241 // will be a single kill, in MBB, which comes after the definition.
242 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
243 // FIXME: what about dead vars?
244 unsigned killIdx;
245 if (vi.Kills[0] != mi)
246 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
247 else
248 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000249
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 // If the kill happens after the definition, we have an intra-block
251 // live range.
252 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000253 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000255 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000256 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000257 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000258 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259 return;
260 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000261 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000262
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000263 // The other case we handle is when a virtual register lives to the end
264 // of the defining block, potentially live across some blocks, then is
265 // live into some number of blocks, but gets killed. Start by adding a
266 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000267 LiveRange NewLR(defIndex,
268 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000269 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000270 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271 interval.addRange(NewLR);
272
273 // Iterate over all of the blocks that the variable is completely
274 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
275 // live interval.
276 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
277 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000278 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
279 if (!MBB->empty()) {
280 LiveRange LR(getMBBStartIdx(i),
281 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000282 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000284 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 }
286 }
287 }
288
289 // Finally, this virtual register is live from the start of any killing
290 // block to the 'use' slot of the killing instruction.
291 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
292 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000293 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000294 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000295 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000297 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000298 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 }
300
301 } else {
302 // If this is the second time we see a virtual register definition, it
303 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000304 // the result of two address elimination, then the vreg is one of the
305 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000306 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 // If this is a two-address definition, then we have already processed
308 // the live range. The only problem is that we didn't realize there
309 // are actually two values in the live interval. Because of this we
310 // need to take the LiveRegion that defines this register and split it
311 // into two values.
Owen Andersonc95f0752008-01-10 03:12:54 +0000312 MachineRegisterInfo& MRI = mbb->getParent()->getRegInfo();
313 unsigned lowIndex = ~0U;
314 for (MachineRegisterInfo::def_iterator DI = MRI.def_begin(interval.reg),
315 DE = MRI.def_end(); DI != DE; ++DI)
316 if (getInstructionIndex(&*DI) < lowIndex)
317 lowIndex = getInstructionIndex(&*DI);
318
319 unsigned DefIndex = getDefIndex(lowIndex);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000320 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000321
Evan Cheng4f8ff162007-08-11 00:59:19 +0000322 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000323 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000324 unsigned OldEnd = OldLR->end;
325
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000327 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000329
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000330 // Two-address vregs should always only be redefined once. This means
331 // that at this point, there should be exactly one value number in it.
332 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
333
Chris Lattner91725b72006-08-31 05:54:43 +0000334 // The new value number (#1) is defined by the instruction we claimed
335 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000336 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
337 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000338
Chris Lattner91725b72006-08-31 05:54:43 +0000339 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000340 OldValNo->def = RedefIndex;
341 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000342
343 // Add the new live interval which replaces the range for the input copy.
344 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000345 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000347 interval.addKill(ValNo, RedefIndex);
348 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349
350 // If this redefinition is dead, we need to add a dummy unit live
351 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000352 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000353 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000355 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000356 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357
358 } else {
359 // Otherwise, this must be because of phi elimination. If this is the
360 // first redefinition of the vreg that we have seen, go back and change
361 // the live range in the PHI block to be a different value number.
362 if (interval.containsOneValue()) {
363 assert(vi.Kills.size() == 1 &&
364 "PHI elimination vreg should have one kill, the PHI itself!");
365
366 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000367 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000368 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000369 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000370 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000371 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000372 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000374 interval.addKill(VNI, Start);
375 VNI->hasPHIKill = true;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000376 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000377
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000378 // Replace the interval with one of a NEW value number. Note that this
379 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000380 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000381 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000382 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000383 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000384 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 }
386
387 // In the case of PHI elimination, each variable definition is only
388 // live until the end of the block. We've already taken care of the
389 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000390 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000391
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000392 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000393 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000394 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000395 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000396 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
397 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
398 VNInfoAllocator);
399 else
400 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000401
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000402 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000403 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000404 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000405 interval.addKill(ValNo, killIndex);
406 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000407 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000408 }
409 }
410
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000411 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000412}
413
Chris Lattnerf35fef72004-07-23 21:24:19 +0000414void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000415 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000416 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000417 LiveInterval &interval,
418 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000419 // A physical register cannot be live across basic block, so its
420 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000421 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000422
Chris Lattner6b128bd2006-09-03 08:07:11 +0000423 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000424 unsigned start = getDefIndex(baseIndex);
425 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000426
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 // If it is not used after definition, it is considered dead at
428 // the instruction defining it. Hence its interval is:
429 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000430 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000431 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000432 end = getDefIndex(start) + 1;
433 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000434 }
435
436 // If it is not dead on definition, it must be killed by a
437 // subsequent instruction. Hence its interval is:
438 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000439 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000441 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000442 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000443 end = getUseIndex(baseIndex) + 1;
444 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000445 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
446 // Another instruction redefines the register before it is ever read.
447 // Then the register is essentially dead at the instruction that defines
448 // it. Hence its interval is:
449 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000450 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000451 end = getDefIndex(start) + 1;
452 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000453 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000454 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000455
456 // The only case we should have a dead physreg here without a killing or
457 // instruction where we know it's dead is if it is live-in to the function
458 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000459 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000460 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000461
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000462exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000463 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000464
Evan Cheng24a3cc42007-04-25 07:30:23 +0000465 // Already exists? Extend old live interval.
466 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000467 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000468 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000469 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000470 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000471 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000472 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000473}
474
Chris Lattnerf35fef72004-07-23 21:24:19 +0000475void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
476 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000477 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000478 unsigned reg) {
479 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000480 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000481 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000482 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000483 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
484 SrcReg = MI->getOperand(1).getReg();
485 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000486 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000487 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000488 // Def of a register also defines its sub-registers.
489 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
490 // Avoid processing some defs more than once.
491 if (!MI->findRegisterDefOperand(*AS))
492 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000493 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000494}
495
Evan Chengb371f452007-02-19 21:49:54 +0000496void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000497 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000498 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000499 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
500
501 // Look for kills, if it reaches a def before it's killed, then it shouldn't
502 // be considered a livein.
503 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000504 unsigned baseIndex = MIIdx;
505 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000506 unsigned end = start;
507 while (mi != MBB->end()) {
508 if (lv_->KillsRegister(mi, interval.reg)) {
509 DOUT << " killed";
510 end = getUseIndex(baseIndex) + 1;
511 goto exit;
512 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
513 // Another instruction redefines the register before it is ever read.
514 // Then the register is essentially dead at the instruction that defines
515 // it. Hence its interval is:
516 // [defSlot(def), defSlot(def)+1)
517 DOUT << " dead";
518 end = getDefIndex(start) + 1;
519 goto exit;
520 }
521
522 baseIndex += InstrSlots::NUM;
523 ++mi;
524 }
525
526exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000527 // Live-in register might not be used at all.
528 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000529 if (isAlias) {
530 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000531 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000532 } else {
533 DOUT << " live through";
534 end = baseIndex;
535 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000536 }
537
Evan Chengf3bb2e62007-09-05 21:46:51 +0000538 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000539 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000540 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000541 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000542}
543
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000544/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000545/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000546/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000547/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000548void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000549 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
550 << "********** Function: "
551 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000552 // Track the index of the current machine instr.
553 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000554 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
555 MBBI != E; ++MBBI) {
556 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000557 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000558
Chris Lattner428b92e2006-09-15 03:57:23 +0000559 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000560
Dan Gohmancb406c22007-10-03 19:26:29 +0000561 // Create intervals for live-ins to this BB first.
562 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
563 LE = MBB->livein_end(); LI != LE; ++LI) {
564 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
565 // Multiple live-ins can alias the same register.
566 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
567 if (!hasInterval(*AS))
568 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
569 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000570 }
571
Chris Lattner428b92e2006-09-15 03:57:23 +0000572 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000573 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000574
Evan Cheng438f7bc2006-11-10 08:43:01 +0000575 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000576 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
577 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000578 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000579 if (MO.isRegister() && MO.getReg() && MO.isDef())
580 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000581 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000582
583 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000584 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000585 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000586}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000587
Evan Cheng4ca980e2007-10-17 02:10:22 +0000588bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000589 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000590 std::vector<IdxMBBPair>::const_iterator I =
591 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
592
593 bool ResVal = false;
594 while (I != Idx2MBBMap.end()) {
595 if (LR.end <= I->first)
596 break;
597 MBBs.push_back(I->second);
598 ResVal = true;
599 ++I;
600 }
601 return ResVal;
602}
603
604
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000605LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000606 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000607 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000608 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000609}
Evan Chengf2fbca62007-11-12 06:35:08 +0000610
611
612//===----------------------------------------------------------------------===//
613// Register allocator hooks.
614//
615
616/// isReMaterializable - Returns true if the definition MI of the specified
617/// val# of the specified interval is re-materializable.
618bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000619 const VNInfo *ValNo, MachineInstr *MI,
620 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000621 if (DisableReMat)
622 return false;
623
Evan Cheng5ef3a042007-12-06 00:01:56 +0000624 isLoad = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000625 const TargetInstrDesc &TID = MI->getDesc();
626 if (TID.isImplicitDef() || tii_->isTriviallyReMaterializable(MI)) {
627 isLoad = TID.isSimpleLoad();
Evan Chengf2fbca62007-11-12 06:35:08 +0000628 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000629 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000630
631 int FrameIdx = 0;
632 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
633 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
634 return false;
635
636 // This is a load from fixed stack slot. It can be rematerialized unless it's
637 // re-defined by a two-address instruction.
Evan Cheng5ef3a042007-12-06 00:01:56 +0000638 isLoad = true;
Evan Chengf2fbca62007-11-12 06:35:08 +0000639 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
640 i != e; ++i) {
641 const VNInfo *VNI = *i;
642 if (VNI == ValNo)
643 continue;
644 unsigned DefIdx = VNI->def;
645 if (DefIdx == ~1U)
646 continue; // Dead val#.
647 MachineInstr *DefMI = (DefIdx == ~0u)
648 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000649 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg)) {
650 isLoad = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000651 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000652 }
653 }
654 return true;
655}
656
657/// isReMaterializable - Returns true if every definition of MI of every
658/// val# of the specified interval is re-materializable.
659bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
660 isLoad = false;
661 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
662 i != e; ++i) {
663 const VNInfo *VNI = *i;
664 unsigned DefIdx = VNI->def;
665 if (DefIdx == ~1U)
666 continue; // Dead val#.
667 // Is the def for the val# rematerializable?
668 if (DefIdx == ~0u)
669 return false;
670 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
671 bool DefIsLoad = false;
672 if (!ReMatDefMI || !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
673 return false;
674 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000675 }
676 return true;
677}
678
679/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
680/// slot / to reg or any rematerialized load into ith operand of specified
681/// MI. If it is successul, MI is updated with the newly created MI and
682/// returns true.
Evan Cheng81a03822007-11-17 00:40:40 +0000683bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
Evan Chengcddbb832007-11-30 21:23:43 +0000684 VirtRegMap &vrm, MachineInstr *DefMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000685 unsigned InstrIdx,
686 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000687 bool isSS, int Slot, unsigned Reg) {
Evan Chengaee4af62007-12-02 08:30:39 +0000688 unsigned MRInfo = 0;
Chris Lattner749c6f62008-01-07 07:27:27 +0000689 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000690 // If it is an implicit def instruction, just delete it.
Chris Lattner749c6f62008-01-07 07:27:27 +0000691 if (TID.isImplicitDef()) {
Evan Cheng6e141fd2007-12-12 23:12:09 +0000692 RemoveMachineInstrFromMaps(MI);
693 vrm.RemoveMachineInstrFromMaps(MI);
694 MI->eraseFromParent();
695 ++numFolds;
696 return true;
697 }
698
Evan Chengaee4af62007-12-02 08:30:39 +0000699 SmallVector<unsigned, 2> FoldOps;
700 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
701 unsigned OpIdx = Ops[i];
702 // FIXME: fold subreg use.
703 if (MI->getOperand(OpIdx).getSubReg())
Evan Chenge62f97c2007-12-01 02:07:52 +0000704 return false;
Evan Chengaee4af62007-12-02 08:30:39 +0000705 if (MI->getOperand(OpIdx).isDef())
706 MRInfo |= (unsigned)VirtRegMap::isMod;
707 else {
708 // Filter out two-address use operand(s).
Chris Lattner749c6f62008-01-07 07:27:27 +0000709 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000710 MRInfo = VirtRegMap::isModRef;
711 continue;
712 }
713 MRInfo |= (unsigned)VirtRegMap::isRef;
714 }
715 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000716 }
717
Owen Anderson6425f8b2008-01-07 01:35:56 +0000718 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(MI, FoldOps, Slot)
719 : tii_->foldMemoryOperand(MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000720 if (fmi) {
721 // Attempt to fold the memory reference into the instruction. If
722 // we can do this, we don't need to insert spill code.
723 if (lv_)
724 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000725 else
726 LiveVariables::transferKillDeadInfo(MI, fmi, mri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000727 MachineBasicBlock &MBB = *MI->getParent();
Evan Chengcddbb832007-11-30 21:23:43 +0000728 if (isSS && !mf_->getFrameInfo()->isFixedObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000729 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000730 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000731 vrm.transferRestorePts(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000732 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000733 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
734 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000735 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000736 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000737 return true;
738 }
739 return false;
740}
741
Evan Cheng018f9b02007-12-05 03:22:34 +0000742/// canFoldMemoryOperand - Returns true if the specified load / store
743/// folding is possible.
744bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
745 SmallVector<unsigned, 2> &Ops) const {
746 SmallVector<unsigned, 2> FoldOps;
747 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
748 unsigned OpIdx = Ops[i];
749 // FIXME: fold subreg use.
750 if (MI->getOperand(OpIdx).getSubReg())
751 return false;
752 FoldOps.push_back(OpIdx);
753 }
754
Owen Anderson6425f8b2008-01-07 01:35:56 +0000755 return tii_->canFoldMemoryOperand(MI, FoldOps);
Evan Cheng018f9b02007-12-05 03:22:34 +0000756}
757
Evan Cheng81a03822007-11-17 00:40:40 +0000758bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
759 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
760 for (LiveInterval::Ranges::const_iterator
761 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
762 std::vector<IdxMBBPair>::const_iterator II =
763 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
764 if (II == Idx2MBBMap.end())
765 continue;
766 if (I->end > II->first) // crossing a MBB.
767 return false;
768 MBBs.insert(II->second);
769 if (MBBs.size() > 1)
770 return false;
771 }
772 return true;
773}
774
Evan Chengf2fbca62007-11-12 06:35:08 +0000775/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
776/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000777bool LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000778rewriteInstructionForSpills(const LiveInterval &li, bool TrySplit,
779 unsigned id, unsigned index, unsigned end, MachineInstr *MI,
780 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000781 unsigned Slot, int LdSlot,
782 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000783 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000784 const TargetRegisterClass* rc,
785 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000786 unsigned &NewVReg, bool &HasDef, bool &HasUse,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000787 const MachineLoopInfo *loopInfo,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000788 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000789 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000790 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000791 RestartInstruction:
792 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
793 MachineOperand& mop = MI->getOperand(i);
794 if (!mop.isRegister())
795 continue;
796 unsigned Reg = mop.getReg();
797 unsigned RegI = Reg;
798 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
799 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000800 if (Reg != li.reg)
801 continue;
802
803 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000804 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000805 int FoldSlot = Slot;
806 if (DefIsReMat) {
807 // If this is the rematerializable definition MI itself and
808 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000809 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000810 DOUT << "\t\t\t\tErasing re-materlizable def: ";
811 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000812 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000813 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000814 MI->eraseFromParent();
815 break;
816 }
817
818 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000819 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000820 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000821 if (isLoad) {
822 // Try fold loads (from stack slot, constant pool, etc.) into uses.
823 FoldSS = isLoadSS;
824 FoldSlot = LdSlot;
825 }
826 }
827
Evan Chengf2fbca62007-11-12 06:35:08 +0000828 // Scan all of the operands of this instruction rewriting operands
829 // to use NewVReg instead of li.reg as appropriate. We do this for
830 // two reasons:
831 //
832 // 1. If the instr reads the same spilled vreg multiple times, we
833 // want to reuse the NewVReg.
834 // 2. If the instr is a two-addr instruction, we are required to
835 // keep the src/dst regs pinned.
836 //
837 // Keep track of whether we replace a use and/or def so that we can
838 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000839
Evan Cheng81a03822007-11-17 00:40:40 +0000840 HasUse = mop.isUse();
841 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000842 SmallVector<unsigned, 2> Ops;
843 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000844 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000845 const MachineOperand &MOj = MI->getOperand(j);
846 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000847 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000848 unsigned RegJ = MOj.getReg();
Evan Chengf2fbca62007-11-12 06:35:08 +0000849 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
850 continue;
851 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000852 Ops.push_back(j);
853 HasUse |= MOj.isUse();
854 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000855 }
856 }
857
Evan Cheng018f9b02007-12-05 03:22:34 +0000858 if (TryFold) {
859 // Do not fold load / store here if we are splitting. We'll find an
860 // optimal point to insert a load / store later.
861 if (!TrySplit) {
862 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
863 Ops, FoldSS, FoldSlot, Reg)) {
864 // Folding the load/store can completely change the instruction in
865 // unpredictable ways, rescan it from the beginning.
866 HasUse = false;
867 HasDef = false;
868 CanFold = false;
869 goto RestartInstruction;
870 }
871 } else {
872 CanFold = canFoldMemoryOperand(MI, Ops);
873 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000874 } else
875 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000876
877 // Create a new virtual register for the spill interval.
878 bool CreatedNewVReg = false;
879 if (NewVReg == 0) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000880 NewVReg = RegInfo.createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000881 vrm.grow();
882 CreatedNewVReg = true;
883 }
884 mop.setReg(NewVReg);
885
886 // Reuse NewVReg for other reads.
Evan Chengaee4af62007-12-02 08:30:39 +0000887 for (unsigned j = 0, e = Ops.size(); j != e; ++j)
888 MI->getOperand(Ops[j]).setReg(NewVReg);
Evan Chengcddbb832007-11-30 21:23:43 +0000889
Evan Cheng81a03822007-11-17 00:40:40 +0000890 if (CreatedNewVReg) {
891 if (DefIsReMat) {
892 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
893 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
894 // Each valnum may have its own remat id.
895 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
896 } else {
897 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
898 }
899 if (!CanDelete || (HasUse && HasDef)) {
900 // If this is a two-addr instruction then its use operands are
901 // rematerializable but its def is not. It should be assigned a
902 // stack slot.
903 vrm.assignVirt2StackSlot(NewVReg, Slot);
904 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000905 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000906 vrm.assignVirt2StackSlot(NewVReg, Slot);
907 }
Evan Chengcb3c3302007-11-29 23:02:50 +0000908 } else if (HasUse && HasDef &&
909 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
910 // If this interval hasn't been assigned a stack slot (because earlier
911 // def is a deleted remat def), do it now.
912 assert(Slot != VirtRegMap::NO_STACK_SLOT);
913 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +0000914 }
915
916 // create a new register interval for this spill / remat.
917 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000918 if (CreatedNewVReg) {
919 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000920 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +0000921 if (TrySplit)
922 vrm.setIsSplitFromReg(NewVReg, li.reg);
923 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000924
925 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +0000926 if (CreatedNewVReg) {
927 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
928 nI.getNextValue(~0U, 0, VNInfoAllocator));
929 DOUT << " +" << LR;
930 nI.addRange(LR);
931 } else {
932 // Extend the split live interval to this def / use.
933 unsigned End = getUseIndex(index)+1;
934 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
935 nI.getValNumInfo(nI.getNumValNums()-1));
936 DOUT << " +" << LR;
937 nI.addRange(LR);
938 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000939 }
940 if (HasDef) {
941 LiveRange LR(getDefIndex(index), getStoreIndex(index),
942 nI.getNextValue(~0U, 0, VNInfoAllocator));
943 DOUT << " +" << LR;
944 nI.addRange(LR);
945 }
Evan Cheng81a03822007-11-17 00:40:40 +0000946
Evan Chengf2fbca62007-11-12 06:35:08 +0000947 DOUT << "\t\t\t\tAdded new interval: ";
948 nI.print(DOUT, mri_);
949 DOUT << '\n';
950 }
Evan Cheng018f9b02007-12-05 03:22:34 +0000951 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +0000952}
Evan Cheng81a03822007-11-17 00:40:40 +0000953bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000954 const VNInfo *VNI,
955 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000956 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000957 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
958 unsigned KillIdx = VNI->kills[j];
959 if (KillIdx > Idx && KillIdx < End)
960 return true;
Evan Cheng81a03822007-11-17 00:40:40 +0000961 }
962 return false;
963}
964
Evan Cheng1953d0c2007-11-29 10:12:14 +0000965static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
966 const VNInfo *VNI = NULL;
967 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
968 e = li.vni_end(); i != e; ++i)
969 if ((*i)->def == DefIdx) {
970 VNI = *i;
971 break;
972 }
973 return VNI;
974}
975
Evan Chengf2fbca62007-11-12 06:35:08 +0000976void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000977rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000978 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +0000979 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000980 unsigned Slot, int LdSlot,
981 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000982 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000983 const TargetRegisterClass* rc,
984 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000985 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000986 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000987 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000988 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000989 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
990 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000991 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000992 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +0000993 unsigned NewVReg = 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000994 unsigned index = getBaseIndex(I->start);
995 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
996 for (; index != end; index += InstrSlots::NUM) {
997 // skip deleted instructions
998 while (index != end && !getInstructionFromIndex(index))
999 index += InstrSlots::NUM;
1000 if (index == end) break;
1001
1002 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng81a03822007-11-17 00:40:40 +00001003 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng018f9b02007-12-05 03:22:34 +00001004 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001005 if (TrySplit) {
Evan Chengcada2452007-11-28 01:28:46 +00001006 std::map<unsigned,unsigned>::const_iterator NVI =
Evan Cheng1953d0c2007-11-29 10:12:14 +00001007 MBBVRegsMap.find(MBB->getNumber());
1008 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001009 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001010 // One common case:
1011 // x = use
1012 // ...
1013 // ...
1014 // def = ...
1015 // = use
1016 // It's better to start a new interval to avoid artifically
1017 // extend the new interval.
1018 // FIXME: Too slow? Can we fix it after rewriteInstructionsForSpills?
1019 bool MIHasUse = false;
1020 bool MIHasDef = false;
1021 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1022 MachineOperand& mop = MI->getOperand(i);
1023 if (!mop.isRegister() || mop.getReg() != li.reg)
1024 continue;
1025 if (mop.isUse())
1026 MIHasUse = true;
1027 else
1028 MIHasDef = true;
1029 }
1030 if (MIHasDef && !MIHasUse) {
1031 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001032 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001033 }
1034 }
Evan Chengcada2452007-11-28 01:28:46 +00001035 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001036
1037 bool IsNew = ThisVReg == 0;
1038 if (IsNew) {
1039 // This ends the previous live interval. If all of its def / use
1040 // can be folded, give it a low spill weight.
1041 if (NewVReg && TrySplit && AllCanFold) {
1042 LiveInterval &nI = getOrCreateInterval(NewVReg);
1043 nI.weight /= 10.0F;
1044 }
1045 AllCanFold = true;
1046 }
1047 NewVReg = ThisVReg;
1048
Evan Cheng81a03822007-11-17 00:40:40 +00001049 bool HasDef = false;
1050 bool HasUse = false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001051 bool CanFold = rewriteInstructionForSpills(li, TrySplit, I->valno->id,
1052 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1053 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001054 CanDelete, vrm, RegInfo, rc, ReMatIds, NewVReg,
Evan Cheng018f9b02007-12-05 03:22:34 +00001055 HasDef, HasUse, loopInfo, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001056 if (!HasDef && !HasUse)
1057 continue;
1058
Evan Cheng018f9b02007-12-05 03:22:34 +00001059 AllCanFold &= CanFold;
1060
Evan Cheng81a03822007-11-17 00:40:40 +00001061 // Update weight of spill interval.
1062 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001063 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001064 // The spill weight is now infinity as it cannot be spilled again.
1065 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001066 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001067 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001068
1069 // Keep track of the last def and first use in each MBB.
1070 unsigned MBBId = MBB->getNumber();
1071 if (HasDef) {
1072 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001073 bool HasKill = false;
1074 if (!HasUse)
1075 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1076 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001077 // If this is a two-address code, then this index starts a new VNInfo.
1078 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001079 if (VNI)
1080 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1081 }
Evan Chenge3110d02007-12-01 04:42:39 +00001082 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1083 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001084 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001085 if (SII == SpillIdxes.end()) {
1086 std::vector<SRInfo> S;
1087 S.push_back(SRInfo(index, NewVReg, true));
1088 SpillIdxes.insert(std::make_pair(MBBId, S));
1089 } else if (SII->second.back().vreg != NewVReg) {
1090 SII->second.push_back(SRInfo(index, NewVReg, true));
1091 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001092 // If there is an earlier def and this is a two-address
1093 // instruction, then it's not possible to fold the store (which
1094 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001095 SRInfo &Info = SII->second.back();
1096 Info.index = index;
1097 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001098 }
1099 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001100 } else if (SII != SpillIdxes.end() &&
1101 SII->second.back().vreg == NewVReg &&
1102 (int)index > SII->second.back().index) {
1103 // There is an earlier def that's not killed (must be two-address).
1104 // The spill is no longer needed.
1105 SII->second.pop_back();
1106 if (SII->second.empty()) {
1107 SpillIdxes.erase(MBBId);
1108 SpillMBBs.reset(MBBId);
1109 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001110 }
1111 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001112 }
1113
1114 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001115 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001116 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001117 if (SII != SpillIdxes.end() &&
1118 SII->second.back().vreg == NewVReg &&
1119 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001120 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001121 SII->second.back().canFold = false;
1122 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001123 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001124 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001125 // If we are splitting live intervals, only fold if it's the first
1126 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001127 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001128 else if (IsNew) {
1129 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001130 if (RII == RestoreIdxes.end()) {
1131 std::vector<SRInfo> Infos;
1132 Infos.push_back(SRInfo(index, NewVReg, true));
1133 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1134 } else {
1135 RII->second.push_back(SRInfo(index, NewVReg, true));
1136 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001137 RestoreMBBs.set(MBBId);
1138 }
1139 }
1140
1141 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001142 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001143 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001144 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001145
1146 if (NewVReg && TrySplit && AllCanFold) {
1147 // If all of its def / use can be folded, give it a low spill weight.
1148 LiveInterval &nI = getOrCreateInterval(NewVReg);
1149 nI.weight /= 10.0F;
1150 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001151}
1152
Evan Cheng1953d0c2007-11-29 10:12:14 +00001153bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1154 BitVector &RestoreMBBs,
1155 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1156 if (!RestoreMBBs[Id])
1157 return false;
1158 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1159 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1160 if (Restores[i].index == index &&
1161 Restores[i].vreg == vr &&
1162 Restores[i].canFold)
1163 return true;
1164 return false;
1165}
1166
1167void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1168 BitVector &RestoreMBBs,
1169 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1170 if (!RestoreMBBs[Id])
1171 return;
1172 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1173 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1174 if (Restores[i].index == index && Restores[i].vreg)
1175 Restores[i].index = -1;
1176}
Evan Cheng81a03822007-11-17 00:40:40 +00001177
1178
Evan Chengf2fbca62007-11-12 06:35:08 +00001179std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001180addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001181 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001182 // Since this is called after the analysis is done we don't know if
1183 // LiveVariables is available
1184 lv_ = getAnalysisToUpdate<LiveVariables>();
1185
1186 assert(li.weight != HUGE_VALF &&
1187 "attempt to spill already spilled interval!");
1188
1189 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1190 li.print(DOUT, mri_);
1191 DOUT << '\n';
1192
Evan Cheng81a03822007-11-17 00:40:40 +00001193 // Each bit specify whether it a spill is required in the MBB.
1194 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001195 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001196 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001197 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1198 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001199 std::vector<LiveInterval*> NewLIs;
Chris Lattner84bc5422007-12-31 04:13:23 +00001200 MachineRegisterInfo &RegInfo = mf_->getRegInfo();
1201 const TargetRegisterClass* rc = RegInfo.getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001202
1203 unsigned NumValNums = li.getNumValNums();
1204 SmallVector<MachineInstr*, 4> ReMatDefs;
1205 ReMatDefs.resize(NumValNums, NULL);
1206 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1207 ReMatOrigDefs.resize(NumValNums, NULL);
1208 SmallVector<int, 4> ReMatIds;
1209 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1210 BitVector ReMatDelete(NumValNums);
1211 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1212
Evan Cheng81a03822007-11-17 00:40:40 +00001213 // Spilling a split live interval. It cannot be split any further. Also,
1214 // it's also guaranteed to be a single val# / range interval.
1215 if (vrm.getPreSplitReg(li.reg)) {
1216 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001217 // Unset the split kill marker on the last use.
1218 unsigned KillIdx = vrm.getKillPoint(li.reg);
1219 if (KillIdx) {
1220 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1221 assert(KillMI && "Last use disappeared?");
1222 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1223 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001224 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001225 }
Evan Chengadf85902007-12-05 09:51:10 +00001226 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001227 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1228 Slot = vrm.getStackSlot(li.reg);
1229 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1230 MachineInstr *ReMatDefMI = DefIsReMat ?
1231 vrm.getReMaterializedMI(li.reg) : NULL;
1232 int LdSlot = 0;
1233 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1234 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001235 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001236 bool IsFirstRange = true;
1237 for (LiveInterval::Ranges::const_iterator
1238 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1239 // If this is a split live interval with multiple ranges, it means there
1240 // are two-address instructions that re-defined the value. Only the
1241 // first def can be rematerialized!
1242 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001243 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001244 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1245 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001246 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001247 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001248 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001249 } else {
1250 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1251 Slot, 0, false, false, false,
Chris Lattner84bc5422007-12-31 04:13:23 +00001252 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001253 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001254 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001255 }
1256 IsFirstRange = false;
1257 }
1258 return NewLIs;
1259 }
1260
1261 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001262 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1263 TrySplit = false;
1264 if (TrySplit)
1265 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001266 bool NeedStackSlot = false;
1267 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1268 i != e; ++i) {
1269 const VNInfo *VNI = *i;
1270 unsigned VN = VNI->id;
1271 unsigned DefIdx = VNI->def;
1272 if (DefIdx == ~1U)
1273 continue; // Dead val#.
1274 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001275 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1276 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001277 bool dummy;
1278 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001279 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001280 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001281 // Original def may be modified so we have to make a copy here. vrm must
1282 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001283 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001284
1285 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001286 if (VNI->hasPHIKill) {
1287 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001288 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001289 CanDelete = false;
1290 // Need a stack slot if there is any live range where uses cannot be
1291 // rematerialized.
1292 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001293 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001294 if (CanDelete)
1295 ReMatDelete.set(VN);
1296 } else {
1297 // Need a stack slot if there is any live range where uses cannot be
1298 // rematerialized.
1299 NeedStackSlot = true;
1300 }
1301 }
1302
1303 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001304 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001305 Slot = vrm.assignVirt2StackSlot(li.reg);
1306
1307 // Create new intervals and rewrite defs and uses.
1308 for (LiveInterval::Ranges::const_iterator
1309 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001310 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1311 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1312 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001313 bool CanDelete = ReMatDelete[I->valno->id];
1314 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001315 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001316 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001317 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001318 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001319 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001320 CanDelete, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001321 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001322 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001323 }
1324
Evan Cheng0cbb1162007-11-29 01:06:25 +00001325 // Insert spills / restores if we are splitting.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001326 if (!TrySplit)
1327 return NewLIs;
1328
Evan Chengb50bb8c2007-12-05 08:16:32 +00001329 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001330 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001331 if (NeedStackSlot) {
1332 int Id = SpillMBBs.find_first();
1333 while (Id != -1) {
1334 std::vector<SRInfo> &spills = SpillIdxes[Id];
1335 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1336 int index = spills[i].index;
1337 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001338 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001339 bool isReMat = vrm.isReMaterialized(VReg);
1340 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001341 bool CanFold = false;
1342 bool FoundUse = false;
1343 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001344 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001345 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001346 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1347 MachineOperand &MO = MI->getOperand(j);
1348 if (!MO.isRegister() || MO.getReg() != VReg)
1349 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001350
1351 Ops.push_back(j);
1352 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001353 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001354 if (isReMat ||
1355 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1356 RestoreMBBs, RestoreIdxes))) {
1357 // MI has two-address uses of the same register. If the use
1358 // isn't the first and only use in the BB, then we can't fold
1359 // it. FIXME: Move this to rewriteInstructionsForSpills.
1360 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001361 break;
1362 }
Evan Chengaee4af62007-12-02 08:30:39 +00001363 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001364 }
1365 }
1366 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001367 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001368 if (CanFold && !Ops.empty()) {
1369 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001370 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001371 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001372 // Also folded uses, do not issue a load.
1373 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001374 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1375 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001376 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001377 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001378 }
1379
Evan Chengaee4af62007-12-02 08:30:39 +00001380 // Else tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001381 if (!Folded) {
1382 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1383 bool isKill = LR->end == getStoreIndex(index);
1384 vrm.addSpillPoint(VReg, isKill, MI);
1385 if (isKill)
1386 AddedKill.insert(&nI);
1387 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001388 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001389 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001390 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001391 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001392
Evan Cheng1953d0c2007-11-29 10:12:14 +00001393 int Id = RestoreMBBs.find_first();
1394 while (Id != -1) {
1395 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1396 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1397 int index = restores[i].index;
1398 if (index == -1)
1399 continue;
1400 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001401 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001402 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001403 bool CanFold = false;
1404 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001405 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001406 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001407 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1408 MachineOperand &MO = MI->getOperand(j);
1409 if (!MO.isRegister() || MO.getReg() != VReg)
1410 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001411
Evan Cheng0cbb1162007-11-29 01:06:25 +00001412 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001413 // If this restore were to be folded, it would have been folded
1414 // already.
1415 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001416 break;
1417 }
Evan Chengaee4af62007-12-02 08:30:39 +00001418 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001419 }
1420 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001421
1422 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001423 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001424 if (CanFold && !Ops.empty()) {
1425 if (!vrm.isReMaterialized(VReg))
1426 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1427 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001428 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1429 int LdSlot = 0;
1430 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1431 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001432 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001433 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1434 Ops, isLoadSS, LdSlot, VReg);
1435 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001436 }
1437 // If folding is not possible / failed, then tell the spiller to issue a
1438 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001439 if (Folded)
1440 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001441 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001442 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001443 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001444 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001445 }
1446
Evan Chengb50bb8c2007-12-05 08:16:32 +00001447 // Finalize intervals: add kills, finalize spill weights, and filter out
1448 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001449 std::vector<LiveInterval*> RetNewLIs;
1450 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1451 LiveInterval *LI = NewLIs[i];
1452 if (!LI->empty()) {
1453 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001454 if (!AddedKill.count(LI)) {
1455 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001456 unsigned LastUseIdx = getBaseIndex(LR->end);
1457 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001458 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg);
1459 assert(UseIdx != -1);
Chris Lattner749c6f62008-01-07 07:27:27 +00001460 if (LastUse->getDesc().getOperandConstraint(UseIdx, TOI::TIED_TO) ==
Chris Lattner69244302008-01-07 01:56:04 +00001461 -1) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001462 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001463 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001464 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001465 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001466 RetNewLIs.push_back(LI);
1467 }
1468 }
Evan Cheng81a03822007-11-17 00:40:40 +00001469
Evan Cheng597d10d2007-12-04 00:32:23 +00001470 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001471}