blob: 80d3547e4b41798b68e8d3d19bbdad5d7d0a44a0 [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.
312 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000313 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314
Evan Cheng4f8ff162007-08-11 00:59:19 +0000315 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000316 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000317 unsigned OldEnd = OldLR->end;
318
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000319 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000320 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000321 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000322
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000323 // Two-address vregs should always only be redefined once. This means
324 // that at this point, there should be exactly one value number in it.
325 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
326
Chris Lattner91725b72006-08-31 05:54:43 +0000327 // The new value number (#1) is defined by the instruction we claimed
328 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000329 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
330 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000331
Chris Lattner91725b72006-08-31 05:54:43 +0000332 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000333 OldValNo->def = RedefIndex;
334 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000335
336 // Add the new live interval which replaces the range for the input copy.
337 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000338 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000340 interval.addKill(ValNo, RedefIndex);
341 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000342
343 // If this redefinition is dead, we need to add a dummy unit live
344 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000345 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000346 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000348 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000349 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350
351 } else {
352 // Otherwise, this must be because of phi elimination. If this is the
353 // first redefinition of the vreg that we have seen, go back and change
354 // the live range in the PHI block to be a different value number.
355 if (interval.containsOneValue()) {
356 assert(vi.Kills.size() == 1 &&
357 "PHI elimination vreg should have one kill, the PHI itself!");
358
359 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000360 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000362 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000363 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000364 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000365 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000367 interval.addKill(VNI, Start);
368 VNI->hasPHIKill = true;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000369 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000370
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000371 // Replace the interval with one of a NEW value number. Note that this
372 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000373 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000374 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000375 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000376 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000377 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000378 }
379
380 // In the case of PHI elimination, each variable definition is only
381 // live until the end of the block. We've already taken care of the
382 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000383 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000384
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000385 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000386 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000387 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000388 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000389 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
390 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
391 VNInfoAllocator);
392 else
393 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000394
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000395 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000396 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000397 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000398 interval.addKill(ValNo, killIndex);
399 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000400 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 }
402 }
403
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000404 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000405}
406
Chris Lattnerf35fef72004-07-23 21:24:19 +0000407void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000408 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000409 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000410 LiveInterval &interval,
411 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000412 // A physical register cannot be live across basic block, so its
413 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000414 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000415
Chris Lattner6b128bd2006-09-03 08:07:11 +0000416 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000417 unsigned start = getDefIndex(baseIndex);
418 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000419
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 // If it is not used after definition, it is considered dead at
421 // the instruction defining it. Hence its interval is:
422 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000423 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000424 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000425 end = getDefIndex(start) + 1;
426 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 }
428
429 // If it is not dead on definition, it must be killed by a
430 // subsequent instruction. Hence its interval is:
431 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000432 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000433 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000434 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000435 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000436 end = getUseIndex(baseIndex) + 1;
437 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000438 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
439 // Another instruction redefines the register before it is ever read.
440 // Then the register is essentially dead at the instruction that defines
441 // it. Hence its interval is:
442 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000443 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000444 end = getDefIndex(start) + 1;
445 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000446 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000447 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000448
449 // The only case we should have a dead physreg here without a killing or
450 // instruction where we know it's dead is if it is live-in to the function
451 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000452 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000453 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000454
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000455exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000457
Evan Cheng24a3cc42007-04-25 07:30:23 +0000458 // Already exists? Extend old live interval.
459 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000460 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000461 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000462 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000463 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000464 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000465 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000466}
467
Chris Lattnerf35fef72004-07-23 21:24:19 +0000468void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
469 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000470 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000471 unsigned reg) {
472 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000473 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000474 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000475 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000476 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
477 SrcReg = MI->getOperand(1).getReg();
478 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000479 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000480 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000481 // Def of a register also defines its sub-registers.
482 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
483 // Avoid processing some defs more than once.
484 if (!MI->findRegisterDefOperand(*AS))
485 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000486 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000487}
488
Evan Chengb371f452007-02-19 21:49:54 +0000489void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000490 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000491 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000492 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
493
494 // Look for kills, if it reaches a def before it's killed, then it shouldn't
495 // be considered a livein.
496 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000497 unsigned baseIndex = MIIdx;
498 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000499 unsigned end = start;
500 while (mi != MBB->end()) {
501 if (lv_->KillsRegister(mi, interval.reg)) {
502 DOUT << " killed";
503 end = getUseIndex(baseIndex) + 1;
504 goto exit;
505 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
506 // Another instruction redefines the register before it is ever read.
507 // Then the register is essentially dead at the instruction that defines
508 // it. Hence its interval is:
509 // [defSlot(def), defSlot(def)+1)
510 DOUT << " dead";
511 end = getDefIndex(start) + 1;
512 goto exit;
513 }
514
515 baseIndex += InstrSlots::NUM;
516 ++mi;
517 }
518
519exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000520 // Live-in register might not be used at all.
521 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000522 if (isAlias) {
523 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000524 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000525 } else {
526 DOUT << " live through";
527 end = baseIndex;
528 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000529 }
530
Evan Chengf3bb2e62007-09-05 21:46:51 +0000531 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000532 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000533 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000534 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000535}
536
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000537/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000538/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000539/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000540/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000541void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000542 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
543 << "********** Function: "
544 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000545 // Track the index of the current machine instr.
546 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000547 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
548 MBBI != E; ++MBBI) {
549 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000550 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000551
Chris Lattner428b92e2006-09-15 03:57:23 +0000552 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000553
Dan Gohmancb406c22007-10-03 19:26:29 +0000554 // Create intervals for live-ins to this BB first.
555 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
556 LE = MBB->livein_end(); LI != LE; ++LI) {
557 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
558 // Multiple live-ins can alias the same register.
559 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
560 if (!hasInterval(*AS))
561 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
562 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000563 }
564
Chris Lattner428b92e2006-09-15 03:57:23 +0000565 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000566 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000567
Evan Cheng438f7bc2006-11-10 08:43:01 +0000568 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000569 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
570 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000571 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000572 if (MO.isRegister() && MO.getReg() && MO.isDef())
573 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000574 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000575
576 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000577 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000578 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000579}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000580
Evan Cheng4ca980e2007-10-17 02:10:22 +0000581bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000582 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000583 std::vector<IdxMBBPair>::const_iterator I =
584 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
585
586 bool ResVal = false;
587 while (I != Idx2MBBMap.end()) {
588 if (LR.end <= I->first)
589 break;
590 MBBs.push_back(I->second);
591 ResVal = true;
592 ++I;
593 }
594 return ResVal;
595}
596
597
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000598LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000599 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000600 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000601 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000602}
Evan Chengf2fbca62007-11-12 06:35:08 +0000603
604
605//===----------------------------------------------------------------------===//
606// Register allocator hooks.
607//
608
609/// isReMaterializable - Returns true if the definition MI of the specified
610/// val# of the specified interval is re-materializable.
611bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000612 const VNInfo *ValNo, MachineInstr *MI,
613 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000614 if (DisableReMat)
615 return false;
616
Evan Cheng5ef3a042007-12-06 00:01:56 +0000617 isLoad = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000618 const TargetInstrDesc &TID = MI->getDesc();
619 if (TID.isImplicitDef() || tii_->isTriviallyReMaterializable(MI)) {
620 isLoad = TID.isSimpleLoad();
Evan Chengf2fbca62007-11-12 06:35:08 +0000621 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000622 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000623
624 int FrameIdx = 0;
625 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
626 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
627 return false;
628
629 // This is a load from fixed stack slot. It can be rematerialized unless it's
630 // re-defined by a two-address instruction.
Evan Cheng5ef3a042007-12-06 00:01:56 +0000631 isLoad = true;
Evan Chengf2fbca62007-11-12 06:35:08 +0000632 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
633 i != e; ++i) {
634 const VNInfo *VNI = *i;
635 if (VNI == ValNo)
636 continue;
637 unsigned DefIdx = VNI->def;
638 if (DefIdx == ~1U)
639 continue; // Dead val#.
640 MachineInstr *DefMI = (DefIdx == ~0u)
641 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000642 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg)) {
643 isLoad = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000644 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000645 }
646 }
647 return true;
648}
649
650/// isReMaterializable - Returns true if every definition of MI of every
651/// val# of the specified interval is re-materializable.
652bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
653 isLoad = false;
654 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
655 i != e; ++i) {
656 const VNInfo *VNI = *i;
657 unsigned DefIdx = VNI->def;
658 if (DefIdx == ~1U)
659 continue; // Dead val#.
660 // Is the def for the val# rematerializable?
661 if (DefIdx == ~0u)
662 return false;
663 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
664 bool DefIsLoad = false;
665 if (!ReMatDefMI || !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
666 return false;
667 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000668 }
669 return true;
670}
671
672/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
673/// slot / to reg or any rematerialized load into ith operand of specified
674/// MI. If it is successul, MI is updated with the newly created MI and
675/// returns true.
Evan Cheng81a03822007-11-17 00:40:40 +0000676bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
Evan Chengcddbb832007-11-30 21:23:43 +0000677 VirtRegMap &vrm, MachineInstr *DefMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000678 unsigned InstrIdx,
679 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000680 bool isSS, int Slot, unsigned Reg) {
Evan Chengaee4af62007-12-02 08:30:39 +0000681 unsigned MRInfo = 0;
Chris Lattner749c6f62008-01-07 07:27:27 +0000682 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000683 // If it is an implicit def instruction, just delete it.
Chris Lattner749c6f62008-01-07 07:27:27 +0000684 if (TID.isImplicitDef()) {
Evan Cheng6e141fd2007-12-12 23:12:09 +0000685 RemoveMachineInstrFromMaps(MI);
686 vrm.RemoveMachineInstrFromMaps(MI);
687 MI->eraseFromParent();
688 ++numFolds;
689 return true;
690 }
691
Evan Chengaee4af62007-12-02 08:30:39 +0000692 SmallVector<unsigned, 2> FoldOps;
693 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
694 unsigned OpIdx = Ops[i];
695 // FIXME: fold subreg use.
696 if (MI->getOperand(OpIdx).getSubReg())
Evan Chenge62f97c2007-12-01 02:07:52 +0000697 return false;
Evan Chengaee4af62007-12-02 08:30:39 +0000698 if (MI->getOperand(OpIdx).isDef())
699 MRInfo |= (unsigned)VirtRegMap::isMod;
700 else {
701 // Filter out two-address use operand(s).
Chris Lattner749c6f62008-01-07 07:27:27 +0000702 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000703 MRInfo = VirtRegMap::isModRef;
704 continue;
705 }
706 MRInfo |= (unsigned)VirtRegMap::isRef;
707 }
708 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000709 }
710
Owen Anderson6425f8b2008-01-07 01:35:56 +0000711 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(MI, FoldOps, Slot)
712 : tii_->foldMemoryOperand(MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000713 if (fmi) {
714 // Attempt to fold the memory reference into the instruction. If
715 // we can do this, we don't need to insert spill code.
716 if (lv_)
717 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000718 else
719 LiveVariables::transferKillDeadInfo(MI, fmi, mri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000720 MachineBasicBlock &MBB = *MI->getParent();
Evan Chengcddbb832007-11-30 21:23:43 +0000721 if (isSS && !mf_->getFrameInfo()->isFixedObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000722 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000723 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000724 vrm.transferRestorePts(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000725 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000726 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
727 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000728 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000729 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000730 return true;
731 }
732 return false;
733}
734
Evan Cheng018f9b02007-12-05 03:22:34 +0000735/// canFoldMemoryOperand - Returns true if the specified load / store
736/// folding is possible.
737bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
738 SmallVector<unsigned, 2> &Ops) const {
739 SmallVector<unsigned, 2> FoldOps;
740 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
741 unsigned OpIdx = Ops[i];
742 // FIXME: fold subreg use.
743 if (MI->getOperand(OpIdx).getSubReg())
744 return false;
745 FoldOps.push_back(OpIdx);
746 }
747
Owen Anderson6425f8b2008-01-07 01:35:56 +0000748 return tii_->canFoldMemoryOperand(MI, FoldOps);
Evan Cheng018f9b02007-12-05 03:22:34 +0000749}
750
Evan Cheng81a03822007-11-17 00:40:40 +0000751bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
752 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
753 for (LiveInterval::Ranges::const_iterator
754 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
755 std::vector<IdxMBBPair>::const_iterator II =
756 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
757 if (II == Idx2MBBMap.end())
758 continue;
759 if (I->end > II->first) // crossing a MBB.
760 return false;
761 MBBs.insert(II->second);
762 if (MBBs.size() > 1)
763 return false;
764 }
765 return true;
766}
767
Evan Chengf2fbca62007-11-12 06:35:08 +0000768/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
769/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000770bool LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000771rewriteInstructionForSpills(const LiveInterval &li, bool TrySplit,
772 unsigned id, unsigned index, unsigned end, MachineInstr *MI,
773 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000774 unsigned Slot, int LdSlot,
775 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000776 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000777 const TargetRegisterClass* rc,
778 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000779 unsigned &NewVReg, bool &HasDef, bool &HasUse,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000780 const MachineLoopInfo *loopInfo,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000781 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000782 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000783 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000784 RestartInstruction:
785 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
786 MachineOperand& mop = MI->getOperand(i);
787 if (!mop.isRegister())
788 continue;
789 unsigned Reg = mop.getReg();
790 unsigned RegI = Reg;
791 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
792 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000793 if (Reg != li.reg)
794 continue;
795
796 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000797 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000798 int FoldSlot = Slot;
799 if (DefIsReMat) {
800 // If this is the rematerializable definition MI itself and
801 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000802 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000803 DOUT << "\t\t\t\tErasing re-materlizable def: ";
804 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000805 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000806 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000807 MI->eraseFromParent();
808 break;
809 }
810
811 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000812 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000813 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000814 if (isLoad) {
815 // Try fold loads (from stack slot, constant pool, etc.) into uses.
816 FoldSS = isLoadSS;
817 FoldSlot = LdSlot;
818 }
819 }
820
Evan Chengf2fbca62007-11-12 06:35:08 +0000821 // Scan all of the operands of this instruction rewriting operands
822 // to use NewVReg instead of li.reg as appropriate. We do this for
823 // two reasons:
824 //
825 // 1. If the instr reads the same spilled vreg multiple times, we
826 // want to reuse the NewVReg.
827 // 2. If the instr is a two-addr instruction, we are required to
828 // keep the src/dst regs pinned.
829 //
830 // Keep track of whether we replace a use and/or def so that we can
831 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000832
Evan Cheng81a03822007-11-17 00:40:40 +0000833 HasUse = mop.isUse();
834 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000835 SmallVector<unsigned, 2> Ops;
836 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000837 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000838 const MachineOperand &MOj = MI->getOperand(j);
839 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000840 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000841 unsigned RegJ = MOj.getReg();
Evan Chengf2fbca62007-11-12 06:35:08 +0000842 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
843 continue;
844 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000845 Ops.push_back(j);
846 HasUse |= MOj.isUse();
847 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000848 }
849 }
850
Evan Cheng018f9b02007-12-05 03:22:34 +0000851 if (TryFold) {
852 // Do not fold load / store here if we are splitting. We'll find an
853 // optimal point to insert a load / store later.
854 if (!TrySplit) {
855 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
856 Ops, FoldSS, FoldSlot, Reg)) {
857 // Folding the load/store can completely change the instruction in
858 // unpredictable ways, rescan it from the beginning.
859 HasUse = false;
860 HasDef = false;
861 CanFold = false;
862 goto RestartInstruction;
863 }
864 } else {
865 CanFold = canFoldMemoryOperand(MI, Ops);
866 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000867 } else
868 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000869
870 // Create a new virtual register for the spill interval.
871 bool CreatedNewVReg = false;
872 if (NewVReg == 0) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000873 NewVReg = RegInfo.createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000874 vrm.grow();
875 CreatedNewVReg = true;
876 }
877 mop.setReg(NewVReg);
878
879 // Reuse NewVReg for other reads.
Evan Chengaee4af62007-12-02 08:30:39 +0000880 for (unsigned j = 0, e = Ops.size(); j != e; ++j)
881 MI->getOperand(Ops[j]).setReg(NewVReg);
Evan Chengcddbb832007-11-30 21:23:43 +0000882
Evan Cheng81a03822007-11-17 00:40:40 +0000883 if (CreatedNewVReg) {
884 if (DefIsReMat) {
885 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
886 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
887 // Each valnum may have its own remat id.
888 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
889 } else {
890 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
891 }
892 if (!CanDelete || (HasUse && HasDef)) {
893 // If this is a two-addr instruction then its use operands are
894 // rematerializable but its def is not. It should be assigned a
895 // stack slot.
896 vrm.assignVirt2StackSlot(NewVReg, Slot);
897 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000898 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000899 vrm.assignVirt2StackSlot(NewVReg, Slot);
900 }
Evan Chengcb3c3302007-11-29 23:02:50 +0000901 } else if (HasUse && HasDef &&
902 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
903 // If this interval hasn't been assigned a stack slot (because earlier
904 // def is a deleted remat def), do it now.
905 assert(Slot != VirtRegMap::NO_STACK_SLOT);
906 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +0000907 }
908
909 // create a new register interval for this spill / remat.
910 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000911 if (CreatedNewVReg) {
912 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000913 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +0000914 if (TrySplit)
915 vrm.setIsSplitFromReg(NewVReg, li.reg);
916 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000917
918 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +0000919 if (CreatedNewVReg) {
920 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
921 nI.getNextValue(~0U, 0, VNInfoAllocator));
922 DOUT << " +" << LR;
923 nI.addRange(LR);
924 } else {
925 // Extend the split live interval to this def / use.
926 unsigned End = getUseIndex(index)+1;
927 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
928 nI.getValNumInfo(nI.getNumValNums()-1));
929 DOUT << " +" << LR;
930 nI.addRange(LR);
931 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000932 }
933 if (HasDef) {
934 LiveRange LR(getDefIndex(index), getStoreIndex(index),
935 nI.getNextValue(~0U, 0, VNInfoAllocator));
936 DOUT << " +" << LR;
937 nI.addRange(LR);
938 }
Evan Cheng81a03822007-11-17 00:40:40 +0000939
Evan Chengf2fbca62007-11-12 06:35:08 +0000940 DOUT << "\t\t\t\tAdded new interval: ";
941 nI.print(DOUT, mri_);
942 DOUT << '\n';
943 }
Evan Cheng018f9b02007-12-05 03:22:34 +0000944 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +0000945}
Evan Cheng81a03822007-11-17 00:40:40 +0000946bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000947 const VNInfo *VNI,
948 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000949 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000950 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
951 unsigned KillIdx = VNI->kills[j];
952 if (KillIdx > Idx && KillIdx < End)
953 return true;
Evan Cheng81a03822007-11-17 00:40:40 +0000954 }
955 return false;
956}
957
Evan Cheng1953d0c2007-11-29 10:12:14 +0000958static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
959 const VNInfo *VNI = NULL;
960 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
961 e = li.vni_end(); i != e; ++i)
962 if ((*i)->def == DefIdx) {
963 VNI = *i;
964 break;
965 }
966 return VNI;
967}
968
Evan Chengf2fbca62007-11-12 06:35:08 +0000969void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000970rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000971 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +0000972 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000973 unsigned Slot, int LdSlot,
974 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000975 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000976 const TargetRegisterClass* rc,
977 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000978 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000979 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000980 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000981 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000982 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
983 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000984 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000985 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +0000986 unsigned NewVReg = 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000987 unsigned index = getBaseIndex(I->start);
988 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
989 for (; index != end; index += InstrSlots::NUM) {
990 // skip deleted instructions
991 while (index != end && !getInstructionFromIndex(index))
992 index += InstrSlots::NUM;
993 if (index == end) break;
994
995 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng81a03822007-11-17 00:40:40 +0000996 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng018f9b02007-12-05 03:22:34 +0000997 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +0000998 if (TrySplit) {
Evan Chengcada2452007-11-28 01:28:46 +0000999 std::map<unsigned,unsigned>::const_iterator NVI =
Evan Cheng1953d0c2007-11-29 10:12:14 +00001000 MBBVRegsMap.find(MBB->getNumber());
1001 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001002 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001003 // One common case:
1004 // x = use
1005 // ...
1006 // ...
1007 // def = ...
1008 // = use
1009 // It's better to start a new interval to avoid artifically
1010 // extend the new interval.
1011 // FIXME: Too slow? Can we fix it after rewriteInstructionsForSpills?
1012 bool MIHasUse = false;
1013 bool MIHasDef = false;
1014 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1015 MachineOperand& mop = MI->getOperand(i);
1016 if (!mop.isRegister() || mop.getReg() != li.reg)
1017 continue;
1018 if (mop.isUse())
1019 MIHasUse = true;
1020 else
1021 MIHasDef = true;
1022 }
1023 if (MIHasDef && !MIHasUse) {
1024 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001025 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001026 }
1027 }
Evan Chengcada2452007-11-28 01:28:46 +00001028 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001029
1030 bool IsNew = ThisVReg == 0;
1031 if (IsNew) {
1032 // This ends the previous live interval. If all of its def / use
1033 // can be folded, give it a low spill weight.
1034 if (NewVReg && TrySplit && AllCanFold) {
1035 LiveInterval &nI = getOrCreateInterval(NewVReg);
1036 nI.weight /= 10.0F;
1037 }
1038 AllCanFold = true;
1039 }
1040 NewVReg = ThisVReg;
1041
Evan Cheng81a03822007-11-17 00:40:40 +00001042 bool HasDef = false;
1043 bool HasUse = false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001044 bool CanFold = rewriteInstructionForSpills(li, TrySplit, I->valno->id,
1045 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1046 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001047 CanDelete, vrm, RegInfo, rc, ReMatIds, NewVReg,
Evan Cheng018f9b02007-12-05 03:22:34 +00001048 HasDef, HasUse, loopInfo, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001049 if (!HasDef && !HasUse)
1050 continue;
1051
Evan Cheng018f9b02007-12-05 03:22:34 +00001052 AllCanFold &= CanFold;
1053
Evan Cheng81a03822007-11-17 00:40:40 +00001054 // Update weight of spill interval.
1055 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001056 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001057 // The spill weight is now infinity as it cannot be spilled again.
1058 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001059 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001060 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001061
1062 // Keep track of the last def and first use in each MBB.
1063 unsigned MBBId = MBB->getNumber();
1064 if (HasDef) {
1065 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001066 bool HasKill = false;
1067 if (!HasUse)
1068 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1069 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001070 // If this is a two-address code, then this index starts a new VNInfo.
1071 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001072 if (VNI)
1073 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1074 }
Evan Chenge3110d02007-12-01 04:42:39 +00001075 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1076 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001077 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001078 if (SII == SpillIdxes.end()) {
1079 std::vector<SRInfo> S;
1080 S.push_back(SRInfo(index, NewVReg, true));
1081 SpillIdxes.insert(std::make_pair(MBBId, S));
1082 } else if (SII->second.back().vreg != NewVReg) {
1083 SII->second.push_back(SRInfo(index, NewVReg, true));
1084 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001085 // If there is an earlier def and this is a two-address
1086 // instruction, then it's not possible to fold the store (which
1087 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001088 SRInfo &Info = SII->second.back();
1089 Info.index = index;
1090 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001091 }
1092 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001093 } else if (SII != SpillIdxes.end() &&
1094 SII->second.back().vreg == NewVReg &&
1095 (int)index > SII->second.back().index) {
1096 // There is an earlier def that's not killed (must be two-address).
1097 // The spill is no longer needed.
1098 SII->second.pop_back();
1099 if (SII->second.empty()) {
1100 SpillIdxes.erase(MBBId);
1101 SpillMBBs.reset(MBBId);
1102 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001103 }
1104 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001105 }
1106
1107 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001108 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001109 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001110 if (SII != SpillIdxes.end() &&
1111 SII->second.back().vreg == NewVReg &&
1112 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001113 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001114 SII->second.back().canFold = false;
1115 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001116 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001117 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001118 // If we are splitting live intervals, only fold if it's the first
1119 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001120 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001121 else if (IsNew) {
1122 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001123 if (RII == RestoreIdxes.end()) {
1124 std::vector<SRInfo> Infos;
1125 Infos.push_back(SRInfo(index, NewVReg, true));
1126 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1127 } else {
1128 RII->second.push_back(SRInfo(index, NewVReg, true));
1129 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001130 RestoreMBBs.set(MBBId);
1131 }
1132 }
1133
1134 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001135 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001136 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001137 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001138
1139 if (NewVReg && TrySplit && AllCanFold) {
1140 // If all of its def / use can be folded, give it a low spill weight.
1141 LiveInterval &nI = getOrCreateInterval(NewVReg);
1142 nI.weight /= 10.0F;
1143 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001144}
1145
Evan Cheng1953d0c2007-11-29 10:12:14 +00001146bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1147 BitVector &RestoreMBBs,
1148 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1149 if (!RestoreMBBs[Id])
1150 return false;
1151 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1152 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1153 if (Restores[i].index == index &&
1154 Restores[i].vreg == vr &&
1155 Restores[i].canFold)
1156 return true;
1157 return false;
1158}
1159
1160void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1161 BitVector &RestoreMBBs,
1162 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1163 if (!RestoreMBBs[Id])
1164 return;
1165 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1166 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1167 if (Restores[i].index == index && Restores[i].vreg)
1168 Restores[i].index = -1;
1169}
Evan Cheng81a03822007-11-17 00:40:40 +00001170
1171
Evan Chengf2fbca62007-11-12 06:35:08 +00001172std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001173addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001174 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001175 // Since this is called after the analysis is done we don't know if
1176 // LiveVariables is available
1177 lv_ = getAnalysisToUpdate<LiveVariables>();
1178
1179 assert(li.weight != HUGE_VALF &&
1180 "attempt to spill already spilled interval!");
1181
1182 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
1183 li.print(DOUT, mri_);
1184 DOUT << '\n';
1185
Evan Cheng81a03822007-11-17 00:40:40 +00001186 // Each bit specify whether it a spill is required in the MBB.
1187 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001188 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001189 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001190 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1191 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001192 std::vector<LiveInterval*> NewLIs;
Chris Lattner84bc5422007-12-31 04:13:23 +00001193 MachineRegisterInfo &RegInfo = mf_->getRegInfo();
1194 const TargetRegisterClass* rc = RegInfo.getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001195
1196 unsigned NumValNums = li.getNumValNums();
1197 SmallVector<MachineInstr*, 4> ReMatDefs;
1198 ReMatDefs.resize(NumValNums, NULL);
1199 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1200 ReMatOrigDefs.resize(NumValNums, NULL);
1201 SmallVector<int, 4> ReMatIds;
1202 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1203 BitVector ReMatDelete(NumValNums);
1204 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1205
Evan Cheng81a03822007-11-17 00:40:40 +00001206 // Spilling a split live interval. It cannot be split any further. Also,
1207 // it's also guaranteed to be a single val# / range interval.
1208 if (vrm.getPreSplitReg(li.reg)) {
1209 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001210 // Unset the split kill marker on the last use.
1211 unsigned KillIdx = vrm.getKillPoint(li.reg);
1212 if (KillIdx) {
1213 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1214 assert(KillMI && "Last use disappeared?");
1215 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1216 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001217 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001218 }
Evan Chengadf85902007-12-05 09:51:10 +00001219 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001220 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1221 Slot = vrm.getStackSlot(li.reg);
1222 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1223 MachineInstr *ReMatDefMI = DefIsReMat ?
1224 vrm.getReMaterializedMI(li.reg) : NULL;
1225 int LdSlot = 0;
1226 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1227 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001228 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001229 bool IsFirstRange = true;
1230 for (LiveInterval::Ranges::const_iterator
1231 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1232 // If this is a split live interval with multiple ranges, it means there
1233 // are two-address instructions that re-defined the value. Only the
1234 // first def can be rematerialized!
1235 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001236 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001237 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1238 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001239 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001240 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001241 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001242 } else {
1243 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1244 Slot, 0, false, false, false,
Chris Lattner84bc5422007-12-31 04:13:23 +00001245 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001246 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001247 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001248 }
1249 IsFirstRange = false;
1250 }
1251 return NewLIs;
1252 }
1253
1254 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001255 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1256 TrySplit = false;
1257 if (TrySplit)
1258 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001259 bool NeedStackSlot = false;
1260 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1261 i != e; ++i) {
1262 const VNInfo *VNI = *i;
1263 unsigned VN = VNI->id;
1264 unsigned DefIdx = VNI->def;
1265 if (DefIdx == ~1U)
1266 continue; // Dead val#.
1267 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001268 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1269 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001270 bool dummy;
1271 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001272 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001273 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001274 // Original def may be modified so we have to make a copy here. vrm must
1275 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001276 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001277
1278 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001279 if (VNI->hasPHIKill) {
1280 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001281 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001282 CanDelete = false;
1283 // Need a stack slot if there is any live range where uses cannot be
1284 // rematerialized.
1285 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001286 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001287 if (CanDelete)
1288 ReMatDelete.set(VN);
1289 } else {
1290 // Need a stack slot if there is any live range where uses cannot be
1291 // rematerialized.
1292 NeedStackSlot = true;
1293 }
1294 }
1295
1296 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001297 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001298 Slot = vrm.assignVirt2StackSlot(li.reg);
1299
1300 // Create new intervals and rewrite defs and uses.
1301 for (LiveInterval::Ranges::const_iterator
1302 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001303 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1304 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1305 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001306 bool CanDelete = ReMatDelete[I->valno->id];
1307 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001308 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001309 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001310 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001311 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001312 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001313 CanDelete, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001314 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001315 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001316 }
1317
Evan Cheng0cbb1162007-11-29 01:06:25 +00001318 // Insert spills / restores if we are splitting.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001319 if (!TrySplit)
1320 return NewLIs;
1321
Evan Chengb50bb8c2007-12-05 08:16:32 +00001322 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001323 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001324 if (NeedStackSlot) {
1325 int Id = SpillMBBs.find_first();
1326 while (Id != -1) {
1327 std::vector<SRInfo> &spills = SpillIdxes[Id];
1328 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1329 int index = spills[i].index;
1330 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001331 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001332 bool isReMat = vrm.isReMaterialized(VReg);
1333 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001334 bool CanFold = false;
1335 bool FoundUse = false;
1336 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001337 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001338 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001339 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1340 MachineOperand &MO = MI->getOperand(j);
1341 if (!MO.isRegister() || MO.getReg() != VReg)
1342 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001343
1344 Ops.push_back(j);
1345 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001346 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001347 if (isReMat ||
1348 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1349 RestoreMBBs, RestoreIdxes))) {
1350 // MI has two-address uses of the same register. If the use
1351 // isn't the first and only use in the BB, then we can't fold
1352 // it. FIXME: Move this to rewriteInstructionsForSpills.
1353 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001354 break;
1355 }
Evan Chengaee4af62007-12-02 08:30:39 +00001356 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001357 }
1358 }
1359 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001360 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001361 if (CanFold && !Ops.empty()) {
1362 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001363 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001364 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001365 // Also folded uses, do not issue a load.
1366 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001367 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1368 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001369 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001370 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001371 }
1372
Evan Chengaee4af62007-12-02 08:30:39 +00001373 // Else tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001374 if (!Folded) {
1375 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1376 bool isKill = LR->end == getStoreIndex(index);
1377 vrm.addSpillPoint(VReg, isKill, MI);
1378 if (isKill)
1379 AddedKill.insert(&nI);
1380 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001381 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001382 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001383 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001384 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001385
Evan Cheng1953d0c2007-11-29 10:12:14 +00001386 int Id = RestoreMBBs.find_first();
1387 while (Id != -1) {
1388 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1389 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1390 int index = restores[i].index;
1391 if (index == -1)
1392 continue;
1393 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001394 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001395 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001396 bool CanFold = false;
1397 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001398 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001399 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001400 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1401 MachineOperand &MO = MI->getOperand(j);
1402 if (!MO.isRegister() || MO.getReg() != VReg)
1403 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001404
Evan Cheng0cbb1162007-11-29 01:06:25 +00001405 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001406 // If this restore were to be folded, it would have been folded
1407 // already.
1408 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001409 break;
1410 }
Evan Chengaee4af62007-12-02 08:30:39 +00001411 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001412 }
1413 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001414
1415 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001416 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001417 if (CanFold && !Ops.empty()) {
1418 if (!vrm.isReMaterialized(VReg))
1419 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1420 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001421 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1422 int LdSlot = 0;
1423 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1424 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001425 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001426 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1427 Ops, isLoadSS, LdSlot, VReg);
1428 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001429 }
1430 // If folding is not possible / failed, then tell the spiller to issue a
1431 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001432 if (Folded)
1433 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001434 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001435 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001436 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001437 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001438 }
1439
Evan Chengb50bb8c2007-12-05 08:16:32 +00001440 // Finalize intervals: add kills, finalize spill weights, and filter out
1441 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001442 std::vector<LiveInterval*> RetNewLIs;
1443 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1444 LiveInterval *LI = NewLIs[i];
1445 if (!LI->empty()) {
1446 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001447 if (!AddedKill.count(LI)) {
1448 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001449 unsigned LastUseIdx = getBaseIndex(LR->end);
1450 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001451 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg);
1452 assert(UseIdx != -1);
Chris Lattner749c6f62008-01-07 07:27:27 +00001453 if (LastUse->getDesc().getOperandConstraint(UseIdx, TOI::TIED_TO) ==
Chris Lattner69244302008-01-07 01:56:04 +00001454 -1) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001455 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001456 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001457 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001458 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001459 RetNewLIs.push_back(LI);
1460 }
1461 }
Evan Cheng81a03822007-11-17 00:40:40 +00001462
Evan Cheng597d10d2007-12-04 00:32:23 +00001463 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001464}