blob: 238d8aa2c3ce8e7c2c115b0b1b6eb0a46141b544 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Evan Cheng81a03822007-11-17 00:40:40 +000022#include "llvm/Analysis/LoopInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#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",
45 cl::init(false), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000046}
47
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Chris Lattnercd3245a2006-12-19 22:41:21 +000050STATISTIC(numFolded , "Number of loads/stores folded into instructions");
51
Devang Patel19974732007-05-03 01:11:54 +000052char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000053namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000054 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000055}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000056
Chris Lattnerf7da2c72006-08-24 22:43:55 +000057void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000058 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 AU.addRequired<LiveVariables>();
60 AU.addPreservedID(PHIEliminationID);
61 AU.addRequiredID(PHIEliminationID);
62 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000064}
65
Chris Lattnerf7da2c72006-08-24 22:43:55 +000066void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000067 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 mi2iMap_.clear();
69 i2miMap_.clear();
70 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000071 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
72 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000073 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
74 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000075}
76
Evan Cheng4ca980e2007-10-17 02:10:22 +000077namespace llvm {
78 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
79 return V < IM.first;
80 }
81
82 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
83 return IM.first < V;
84 }
85
86 struct Idx2MBBCompare {
87 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
88 return LHS.first < RHS.first;
89 }
90 };
91}
92
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000093/// runOnMachineFunction - Register allocate the whole function
94///
95bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 mf_ = &fn;
97 tm_ = &fn.getTarget();
98 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000099 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000100 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +0000101 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000102
Chris Lattner428b92e2006-09-15 03:57:23 +0000103 // Number MachineInstrs and MachineBasicBlocks.
104 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000105 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000106
107 unsigned MIIndex = 0;
108 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
109 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000110 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000111
Chris Lattner428b92e2006-09-15 03:57:23 +0000112 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
113 I != E; ++I) {
114 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000116 i2miMap_.push_back(I);
117 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000118 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000119
120 // Set the MBB2IdxMap entry for this MBB.
121 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000122 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000123 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000124 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000125
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000126 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000127
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 numIntervals += getNumIntervals();
129
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000130 DOUT << "********** INTERVALS **********\n";
131 for (iterator I = begin(), E = end(); I != E; ++I) {
132 I->second.print(DOUT, mri_);
133 DOUT << "\n";
134 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000135
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000136 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000137 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000138 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000139}
140
Chris Lattner70ca3582004-09-30 15:59:17 +0000141/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000142void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000143 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000144 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000145 I->second.print(DOUT, mri_);
146 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000147 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000148
149 O << "********** MACHINEINSTRS **********\n";
150 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
151 mbbi != mbbe; ++mbbi) {
152 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
153 for (MachineBasicBlock::iterator mii = mbbi->begin(),
154 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000155 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000156 }
157 }
158}
159
Evan Chengc92da382007-11-03 07:20:12 +0000160/// conflictsWithPhysRegDef - Returns true if the specified register
161/// is defined during the duration of the specified interval.
162bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
163 VirtRegMap &vrm, unsigned reg) {
164 for (LiveInterval::Ranges::const_iterator
165 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
166 for (unsigned index = getBaseIndex(I->start),
167 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
168 index += InstrSlots::NUM) {
169 // skip deleted instructions
170 while (index != end && !getInstructionFromIndex(index))
171 index += InstrSlots::NUM;
172 if (index == end) break;
173
174 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000175 unsigned SrcReg, DstReg;
176 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
177 if (SrcReg == li.reg || DstReg == li.reg)
178 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000179 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
180 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000181 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000182 continue;
183 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000184 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000185 continue;
Evan Cheng5d446262007-11-15 08:13:29 +0000186 if (MRegisterInfo::isVirtualRegister(PhysReg)) {
187 if (!vrm.hasPhys(PhysReg))
188 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000189 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000190 }
Evan Cheng5f5f3b62007-11-05 00:59:10 +0000191 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000192 return true;
193 }
194 }
195 }
196
197 return false;
198}
199
Evan Cheng549f27d32007-08-13 23:45:17 +0000200void LiveIntervals::printRegName(unsigned reg) const {
201 if (MRegisterInfo::isPhysicalRegister(reg))
202 cerr << mri_->getName(reg);
203 else
204 cerr << "%reg" << reg;
205}
206
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000207void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000208 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000209 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000210 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000211 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000212 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000213
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000214 // Virtual registers may be defined multiple times (due to phi
215 // elimination and 2-addr elimination). Much of what we do only has to be
216 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000217 // time we see a vreg.
218 if (interval.empty()) {
219 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000220 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000221 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000222 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000223 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000224 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng48ff2822007-10-12 17:16:50 +0000225 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000226 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
227 VNInfoAllocator);
228 else
229 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000230
231 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000232
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000233 // Loop over all of the blocks that the vreg is defined in. There are
234 // two cases we have to handle here. The most common case is a vreg
235 // whose lifetime is contained within a basic block. In this case there
236 // will be a single kill, in MBB, which comes after the definition.
237 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
238 // FIXME: what about dead vars?
239 unsigned killIdx;
240 if (vi.Kills[0] != mi)
241 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
242 else
243 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000244
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000245 // If the kill happens after the definition, we have an intra-block
246 // live range.
247 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000248 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000250 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000251 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000252 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000253 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 return;
255 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000256 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000257
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000258 // The other case we handle is when a virtual register lives to the end
259 // of the defining block, potentially live across some blocks, then is
260 // live into some number of blocks, but gets killed. Start by adding a
261 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000262 LiveRange NewLR(defIndex,
263 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000264 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000265 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000266 interval.addRange(NewLR);
267
268 // Iterate over all of the blocks that the variable is completely
269 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
270 // live interval.
271 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
272 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000273 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
274 if (!MBB->empty()) {
275 LiveRange LR(getMBBStartIdx(i),
276 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000277 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000278 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000279 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 }
281 }
282 }
283
284 // Finally, this virtual register is live from the start of any killing
285 // block to the 'use' slot of the killing instruction.
286 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
287 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000288 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000289 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000290 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000292 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000293 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 }
295
296 } else {
297 // If this is the second time we see a virtual register definition, it
298 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000299 // the result of two address elimination, then the vreg is one of the
300 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000301 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 // If this is a two-address definition, then we have already processed
303 // the live range. The only problem is that we didn't realize there
304 // are actually two values in the live interval. Because of this we
305 // need to take the LiveRegion that defines this register and split it
306 // into two values.
307 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000308 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309
Evan Cheng4f8ff162007-08-11 00:59:19 +0000310 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000311 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000312 unsigned OldEnd = OldLR->end;
313
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000314 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000315 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000316 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000317
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000318 // Two-address vregs should always only be redefined once. This means
319 // that at this point, there should be exactly one value number in it.
320 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
321
Chris Lattner91725b72006-08-31 05:54:43 +0000322 // The new value number (#1) is defined by the instruction we claimed
323 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000324 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
325 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000326
Chris Lattner91725b72006-08-31 05:54:43 +0000327 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000328 OldValNo->def = RedefIndex;
329 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000330
331 // Add the new live interval which replaces the range for the input copy.
332 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000333 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000334 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000335 interval.addKill(ValNo, RedefIndex);
336 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337
338 // If this redefinition is dead, we need to add a dummy unit live
339 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000340 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000341 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000342
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000343 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000344 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345
346 } else {
347 // Otherwise, this must be because of phi elimination. If this is the
348 // first redefinition of the vreg that we have seen, go back and change
349 // the live range in the PHI block to be a different value number.
350 if (interval.containsOneValue()) {
351 assert(vi.Kills.size() == 1 &&
352 "PHI elimination vreg should have one kill, the PHI itself!");
353
354 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000355 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000356 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000357 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000358 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000359 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000360 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000362 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000363 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000364
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000365 // Replace the interval with one of a NEW value number. Note that this
366 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000367 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000368 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000369 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000370 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000371 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000372 }
373
374 // In the case of PHI elimination, each variable definition is only
375 // live until the end of the block. We've already taken care of the
376 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000377 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000378
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000379 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000380 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000381 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000382 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000383 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
384 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
385 VNInfoAllocator);
386 else
387 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000388
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000389 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000390 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000391 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000392 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000393 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000394 }
395 }
396
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000397 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000398}
399
Chris Lattnerf35fef72004-07-23 21:24:19 +0000400void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000401 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000402 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000403 LiveInterval &interval,
404 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000405 // A physical register cannot be live across basic block, so its
406 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000407 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000408
Chris Lattner6b128bd2006-09-03 08:07:11 +0000409 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000410 unsigned start = getDefIndex(baseIndex);
411 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000412
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000413 // If it is not used after definition, it is considered dead at
414 // the instruction defining it. Hence its interval is:
415 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000416 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000417 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000418 end = getDefIndex(start) + 1;
419 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 }
421
422 // If it is not dead on definition, it must be killed by a
423 // subsequent instruction. Hence its interval is:
424 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000425 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000427 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000428 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000429 end = getUseIndex(baseIndex) + 1;
430 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000431 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
432 // Another instruction redefines the register before it is ever read.
433 // Then the register is essentially dead at the instruction that defines
434 // it. Hence its interval is:
435 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000436 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000437 end = getDefIndex(start) + 1;
438 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000439 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000441
442 // The only case we should have a dead physreg here without a killing or
443 // instruction where we know it's dead is if it is live-in to the function
444 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000445 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000446 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000447
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000448exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000449 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000450
Evan Cheng24a3cc42007-04-25 07:30:23 +0000451 // Already exists? Extend old live interval.
452 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000453 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000454 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000455 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000457 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000458 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000459}
460
Chris Lattnerf35fef72004-07-23 21:24:19 +0000461void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
462 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000463 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000464 unsigned reg) {
465 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000466 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000467 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000468 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000469 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
470 SrcReg = MI->getOperand(1).getReg();
471 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000472 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000473 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000474 // Def of a register also defines its sub-registers.
475 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
476 // Avoid processing some defs more than once.
477 if (!MI->findRegisterDefOperand(*AS))
478 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000479 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000480}
481
Evan Chengb371f452007-02-19 21:49:54 +0000482void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000483 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000484 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000485 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
486
487 // Look for kills, if it reaches a def before it's killed, then it shouldn't
488 // be considered a livein.
489 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000490 unsigned baseIndex = MIIdx;
491 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000492 unsigned end = start;
493 while (mi != MBB->end()) {
494 if (lv_->KillsRegister(mi, interval.reg)) {
495 DOUT << " killed";
496 end = getUseIndex(baseIndex) + 1;
497 goto exit;
498 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
499 // Another instruction redefines the register before it is ever read.
500 // Then the register is essentially dead at the instruction that defines
501 // it. Hence its interval is:
502 // [defSlot(def), defSlot(def)+1)
503 DOUT << " dead";
504 end = getDefIndex(start) + 1;
505 goto exit;
506 }
507
508 baseIndex += InstrSlots::NUM;
509 ++mi;
510 }
511
512exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000513 // Live-in register might not be used at all.
514 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000515 if (isAlias) {
516 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000517 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000518 } else {
519 DOUT << " live through";
520 end = baseIndex;
521 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000522 }
523
Evan Chengf3bb2e62007-09-05 21:46:51 +0000524 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000525 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000526 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000527 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000528}
529
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000530/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000531/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000532/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000533/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000534void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000535 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
536 << "********** Function: "
537 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000538 // Track the index of the current machine instr.
539 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000540 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
541 MBBI != E; ++MBBI) {
542 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000543 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000544
Chris Lattner428b92e2006-09-15 03:57:23 +0000545 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000546
Dan Gohmancb406c22007-10-03 19:26:29 +0000547 // Create intervals for live-ins to this BB first.
548 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
549 LE = MBB->livein_end(); LI != LE; ++LI) {
550 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
551 // Multiple live-ins can alias the same register.
552 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
553 if (!hasInterval(*AS))
554 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
555 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000556 }
557
Chris Lattner428b92e2006-09-15 03:57:23 +0000558 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000559 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000560
Evan Cheng438f7bc2006-11-10 08:43:01 +0000561 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000562 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
563 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000565 if (MO.isRegister() && MO.getReg() && MO.isDef())
566 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000567 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000568
569 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000570 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000571 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000572}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000573
Evan Cheng4ca980e2007-10-17 02:10:22 +0000574bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000575 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000576 std::vector<IdxMBBPair>::const_iterator I =
577 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
578
579 bool ResVal = false;
580 while (I != Idx2MBBMap.end()) {
581 if (LR.end <= I->first)
582 break;
583 MBBs.push_back(I->second);
584 ResVal = true;
585 ++I;
586 }
587 return ResVal;
588}
589
590
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000591LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000592 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000593 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000594 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000595}
Evan Chengf2fbca62007-11-12 06:35:08 +0000596
597
598//===----------------------------------------------------------------------===//
599// Register allocator hooks.
600//
601
602/// isReMaterializable - Returns true if the definition MI of the specified
603/// val# of the specified interval is re-materializable.
604bool LiveIntervals::isReMaterializable(const LiveInterval &li,
605 const VNInfo *ValNo, MachineInstr *MI) {
606 if (DisableReMat)
607 return false;
608
609 if (tii_->isTriviallyReMaterializable(MI))
610 return true;
611
612 int FrameIdx = 0;
613 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
614 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
615 return false;
616
617 // This is a load from fixed stack slot. It can be rematerialized unless it's
618 // re-defined by a two-address instruction.
619 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
620 i != e; ++i) {
621 const VNInfo *VNI = *i;
622 if (VNI == ValNo)
623 continue;
624 unsigned DefIdx = VNI->def;
625 if (DefIdx == ~1U)
626 continue; // Dead val#.
627 MachineInstr *DefMI = (DefIdx == ~0u)
628 ? NULL : getInstructionFromIndex(DefIdx);
629 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
630 return false;
631 }
632 return true;
633}
634
635/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
636/// slot / to reg or any rematerialized load into ith operand of specified
637/// MI. If it is successul, MI is updated with the newly created MI and
638/// returns true.
Evan Cheng81a03822007-11-17 00:40:40 +0000639bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
640 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000641 MachineInstr *DefMI,
642 unsigned index, unsigned i,
643 bool isSS, int slot, unsigned reg) {
644 MachineInstr *fmi = isSS
645 ? mri_->foldMemoryOperand(MI, i, slot)
646 : mri_->foldMemoryOperand(MI, i, DefMI);
647 if (fmi) {
648 // Attempt to fold the memory reference into the instruction. If
649 // we can do this, we don't need to insert spill code.
650 if (lv_)
651 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000652 else
653 LiveVariables::transferKillDeadInfo(MI, fmi, mri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000654 MachineBasicBlock &MBB = *MI->getParent();
655 vrm.virtFolded(reg, MI, i, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000656 vrm.transferSpillPts(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000657 mi2iMap_.erase(MI);
658 i2miMap_[index/InstrSlots::NUM] = fmi;
659 mi2iMap_[fmi] = index;
660 MI = MBB.insert(MBB.erase(MI), fmi);
661 ++numFolded;
662 return true;
663 }
664 return false;
665}
666
Evan Cheng81a03822007-11-17 00:40:40 +0000667bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
668 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
669 for (LiveInterval::Ranges::const_iterator
670 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
671 std::vector<IdxMBBPair>::const_iterator II =
672 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
673 if (II == Idx2MBBMap.end())
674 continue;
675 if (I->end > II->first) // crossing a MBB.
676 return false;
677 MBBs.insert(II->second);
678 if (MBBs.size() > 1)
679 return false;
680 }
681 return true;
682}
683
684static
685bool hasALaterUse(MachineBasicBlock *MBB, MachineInstr *MI, unsigned Reg) {
686 MachineBasicBlock::iterator I = MI;
687 if (I == MBB->end())
688 return false;
689 ++I;
690 while (I != MBB->end()) {
691 if (I->findRegisterUseOperandIdx(Reg) != -1)
692 return true;
693 ++I;
694 }
695 return false;
696}
697
Evan Chengf2fbca62007-11-12 06:35:08 +0000698/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
699/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
700void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000701rewriteInstructionForSpills(const LiveInterval &li, bool TrySplit,
702 unsigned id, unsigned index, unsigned end, MachineInstr *MI,
703 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000704 unsigned Slot, int LdSlot,
705 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
706 VirtRegMap &vrm, SSARegMap *RegMap,
707 const TargetRegisterClass* rc,
708 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000709 unsigned &NewVReg, bool &HasDef, bool &HasUse,
Evan Chengcada2452007-11-28 01:28:46 +0000710 const LoopInfo *loopInfo,
711 std::map<unsigned,unsigned> &NewVRegs,
Evan Chengf2fbca62007-11-12 06:35:08 +0000712 std::vector<LiveInterval*> &NewLIs) {
713 RestartInstruction:
714 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
715 MachineOperand& mop = MI->getOperand(i);
716 if (!mop.isRegister())
717 continue;
718 unsigned Reg = mop.getReg();
719 unsigned RegI = Reg;
720 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
721 continue;
Evan Chengc498b022007-11-14 07:59:08 +0000722 unsigned SubIdx = mop.getSubReg();
723 bool isSubReg = SubIdx != 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000724 if (Reg != li.reg)
725 continue;
726
727 bool TryFold = !DefIsReMat;
728 bool FoldSS = true;
729 int FoldSlot = Slot;
730 if (DefIsReMat) {
731 // If this is the rematerializable definition MI itself and
732 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000733 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000734 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000735 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000736 MI->eraseFromParent();
737 break;
738 }
739
740 // If def for this use can't be rematerialized, then try folding.
Evan Cheng81a03822007-11-17 00:40:40 +0000741 TryFold = !ReMatOrigDefMI ||
742 (ReMatOrigDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000743 if (isLoad) {
744 // Try fold loads (from stack slot, constant pool, etc.) into uses.
745 FoldSS = isLoadSS;
746 FoldSlot = LdSlot;
747 }
748 }
749
Evan Cheng81a03822007-11-17 00:40:40 +0000750 // If we are splitting live intervals, only fold if it's 1) the first
751 // use and it's a kill or 2) there isn't another use later in this MBB.
752 TryFold &= NewVReg == 0;
753 if (TryFold && TrySplit)
754 // Do not fold store into def here if we are splitting. We'll find an
755 // optimal point to insert a store later.
756 if (HasDef || mop.isDef() ||
757 (!mop.isKill() && hasALaterUse(MI->getParent(), MI, li.reg)))
758 TryFold = false;
759
Evan Chengf2fbca62007-11-12 06:35:08 +0000760 // FIXME: fold subreg use
761 if (!isSubReg && TryFold &&
Evan Cheng81a03822007-11-17 00:40:40 +0000762 tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index, i, FoldSS, FoldSlot,
763 Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000764 // Folding the load/store can completely change the instruction in
765 // unpredictable ways, rescan it from the beginning.
766 goto RestartInstruction;
767
768 // Create a new virtual register for the spill interval.
Evan Cheng81a03822007-11-17 00:40:40 +0000769 bool CreatedNewVReg = false;
770 if (NewVReg == 0) {
771 NewVReg = RegMap->createVirtualRegister(rc);
772 vrm.grow();
773 CreatedNewVReg = true;
774 }
775 mop.setReg(NewVReg);
Evan Chengf2fbca62007-11-12 06:35:08 +0000776
777 // Scan all of the operands of this instruction rewriting operands
778 // to use NewVReg instead of li.reg as appropriate. We do this for
779 // two reasons:
780 //
781 // 1. If the instr reads the same spilled vreg multiple times, we
782 // want to reuse the NewVReg.
783 // 2. If the instr is a two-addr instruction, we are required to
784 // keep the src/dst regs pinned.
785 //
786 // Keep track of whether we replace a use and/or def so that we can
787 // create the spill interval with the appropriate range.
Evan Chengf2fbca62007-11-12 06:35:08 +0000788
Evan Cheng81a03822007-11-17 00:40:40 +0000789 HasUse = mop.isUse();
790 HasDef = mop.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000791 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
792 if (!MI->getOperand(j).isRegister())
793 continue;
794 unsigned RegJ = MI->getOperand(j).getReg();
795 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
796 continue;
797 if (RegJ == RegI) {
798 MI->getOperand(j).setReg(NewVReg);
799 HasUse |= MI->getOperand(j).isUse();
800 HasDef |= MI->getOperand(j).isDef();
801 }
802 }
803
Evan Cheng81a03822007-11-17 00:40:40 +0000804 if (CreatedNewVReg) {
805 if (DefIsReMat) {
806 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
807 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
808 // Each valnum may have its own remat id.
809 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
810 } else {
811 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
812 }
813 if (!CanDelete || (HasUse && HasDef)) {
814 // If this is a two-addr instruction then its use operands are
815 // rematerializable but its def is not. It should be assigned a
816 // stack slot.
817 vrm.assignVirt2StackSlot(NewVReg, Slot);
818 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000819 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000820 vrm.assignVirt2StackSlot(NewVReg, Slot);
821 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000822 }
823
824 // create a new register interval for this spill / remat.
825 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000826 if (CreatedNewVReg) {
827 NewLIs.push_back(&nI);
Evan Chengcada2452007-11-28 01:28:46 +0000828 NewVRegs.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +0000829 if (TrySplit)
830 vrm.setIsSplitFromReg(NewVReg, li.reg);
831 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000832
833 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +0000834 if (CreatedNewVReg) {
835 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
836 nI.getNextValue(~0U, 0, VNInfoAllocator));
837 DOUT << " +" << LR;
838 nI.addRange(LR);
839 } else {
840 // Extend the split live interval to this def / use.
841 unsigned End = getUseIndex(index)+1;
842 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
843 nI.getValNumInfo(nI.getNumValNums()-1));
844 DOUT << " +" << LR;
845 nI.addRange(LR);
846 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000847 }
848 if (HasDef) {
849 LiveRange LR(getDefIndex(index), getStoreIndex(index),
850 nI.getNextValue(~0U, 0, VNInfoAllocator));
851 DOUT << " +" << LR;
852 nI.addRange(LR);
853 }
Evan Cheng81a03822007-11-17 00:40:40 +0000854
Evan Chengf2fbca62007-11-12 06:35:08 +0000855 DOUT << "\t\t\t\tAdded new interval: ";
856 nI.print(DOUT, mri_);
857 DOUT << '\n';
858 }
859}
860
Evan Cheng81a03822007-11-17 00:40:40 +0000861bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
862 MachineBasicBlock *MBB, unsigned Idx,
863 const VNInfo *VNI) const {
864 unsigned End = getMBBEndIdx(MBB);
865 if (VNI) {
866 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
867 unsigned KillIdx = VNI->kills[j];
868 if (KillIdx > Idx && KillIdx < End)
869 return true;
870 }
871 return false;
872 }
873
874 // Look at all the VNInfo's.
875 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
876 i != e; ++i) {
877 const VNInfo *VNI = *i;
878 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
879 unsigned KillIdx = VNI->kills[j];
880 if (KillIdx > Idx && KillIdx < End)
881 return true;
882 }
883 }
884 return false;
885}
886
Evan Chengf2fbca62007-11-12 06:35:08 +0000887void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000888rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000889 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +0000890 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000891 unsigned Slot, int LdSlot,
892 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
893 VirtRegMap &vrm, SSARegMap *RegMap,
894 const TargetRegisterClass* rc,
895 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000896 const LoopInfo *loopInfo,
897 BitVector &SpillMBBs,
Evan Chengcada2452007-11-28 01:28:46 +0000898 std::map<unsigned, std::pair<int, unsigned> > &SpillIdxes,
899 std::map<unsigned,unsigned> &NewVRegs,
Evan Chengf2fbca62007-11-12 06:35:08 +0000900 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng81a03822007-11-17 00:40:40 +0000901 unsigned NewVReg = 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000902 unsigned index = getBaseIndex(I->start);
903 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Cheng81a03822007-11-17 00:40:40 +0000904 bool TrySplitMI = TrySplit && vrm.getPreSplitReg(li.reg) == 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000905 for (; index != end; index += InstrSlots::NUM) {
906 // skip deleted instructions
907 while (index != end && !getInstructionFromIndex(index))
908 index += InstrSlots::NUM;
909 if (index == end) break;
910
911 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng81a03822007-11-17 00:40:40 +0000912 MachineBasicBlock *MBB = MI->getParent();
Evan Chengcada2452007-11-28 01:28:46 +0000913 NewVReg = 0;
914 if (TrySplitMI) {
915 std::map<unsigned,unsigned>::const_iterator NVI =
916 NewVRegs.find(MBB->getNumber());
917 if (NVI != NewVRegs.end())
918 NewVReg = NVI->second;
919 }
Evan Cheng81a03822007-11-17 00:40:40 +0000920 bool IsNew = NewVReg == 0;
921 bool HasDef = false;
922 bool HasUse = false;
923 rewriteInstructionForSpills(li, TrySplitMI, I->valno->id, index, end,
924 MI, ReMatOrigDefMI, ReMatDefMI, Slot, LdSlot,
925 isLoad, isLoadSS, DefIsReMat, CanDelete, vrm,
926 RegMap, rc, ReMatIds, NewVReg, HasDef, HasUse,
927 loopInfo, NewVRegs, NewLIs);
928 if (!HasDef && !HasUse)
929 continue;
930
931 // Update weight of spill interval.
932 LiveInterval &nI = getOrCreateInterval(NewVReg);
933 if (!TrySplitMI)
934 // The spill weight is now infinity as it cannot be spilled again.
935 nI.weight = HUGE_VALF;
936 else {
937 // Keep track of the last def in each MBB.
938 if (HasDef) {
939 if (MI != ReMatOrigDefMI || !CanDelete) {
940 // If this is a two-address code, then this index probably starts a
941 // VNInfo so we should examine all the VNInfo's.
942 bool HasKill = HasUse
943 ? anyKillInMBBAfterIdx(li, MBB, getDefIndex(index))
944 : anyKillInMBBAfterIdx(li, MBB, getDefIndex(index), I->valno);
945 if (!HasKill) {
946 unsigned MBBId = MBB->getNumber();
Evan Chengcada2452007-11-28 01:28:46 +0000947 // High bit specify whether this spill ought to be folded if
948 // possible.
949 std::map<unsigned, std::pair<int,unsigned> >::iterator SII =
950 SpillIdxes.find(MBBId);
951 if (SII == SpillIdxes.end() || (int)index > SII->second.first)
Evan Cheng81a03822007-11-17 00:40:40 +0000952 SpillIdxes[MBBId] = std::make_pair(index, NewVReg | (1 << 31));
953 SpillMBBs.set(MBBId);
954 }
955 }
956 if (!IsNew) {
957 // It this interval hasn't been assigned a stack slot
958 // (because earlier def is remat), do it now.
959 int SS = vrm.getStackSlot(NewVReg);
960 if (SS != (int)Slot) {
961 assert(SS == VirtRegMap::NO_STACK_SLOT);
962 vrm.assignVirt2StackSlot(NewVReg, Slot);
963 }
964 }
965 } else if (HasUse) {
966 // Use(s) following the last def, it's not safe to fold the spill.
967 unsigned MBBId = MBB->getNumber();
Evan Chengcada2452007-11-28 01:28:46 +0000968 std::map<unsigned, std::pair<int,unsigned> >::iterator SII =
969 SpillIdxes.find(MBBId);
970 if (SII != SpillIdxes.end() &&
971 (SII->second.second & ((1<<31)-1)) == NewVReg &&
972 (int)getUseIndex(index) > SII->second.first)
Evan Cheng81a03822007-11-17 00:40:40 +0000973 SpillIdxes[MBBId].second &= (1<<31)-1;
974 }
975
976 // Update spill weight.
977 unsigned loopDepth = loopInfo->getLoopDepth(MBB->getBasicBlock());
978 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
979 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000980 }
981}
982
Evan Cheng81a03822007-11-17 00:40:40 +0000983
984
Evan Chengf2fbca62007-11-12 06:35:08 +0000985std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000986addIntervalsForSpills(const LiveInterval &li,
987 const LoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000988 // Since this is called after the analysis is done we don't know if
989 // LiveVariables is available
990 lv_ = getAnalysisToUpdate<LiveVariables>();
991
992 assert(li.weight != HUGE_VALF &&
993 "attempt to spill already spilled interval!");
994
995 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
996 li.print(DOUT, mri_);
997 DOUT << '\n';
998
Evan Cheng81a03822007-11-17 00:40:40 +0000999 // Each bit specify whether it a spill is required in the MBB.
1000 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Chengcada2452007-11-28 01:28:46 +00001001 std::map<unsigned, std::pair<int, unsigned> > SpillIdxes;
1002 std::map<unsigned,unsigned> NewVRegs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001003 std::vector<LiveInterval*> NewLIs;
1004 SSARegMap *RegMap = mf_->getSSARegMap();
1005 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
1006
1007 unsigned NumValNums = li.getNumValNums();
1008 SmallVector<MachineInstr*, 4> ReMatDefs;
1009 ReMatDefs.resize(NumValNums, NULL);
1010 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1011 ReMatOrigDefs.resize(NumValNums, NULL);
1012 SmallVector<int, 4> ReMatIds;
1013 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1014 BitVector ReMatDelete(NumValNums);
1015 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1016
Evan Cheng81a03822007-11-17 00:40:40 +00001017 // Spilling a split live interval. It cannot be split any further. Also,
1018 // it's also guaranteed to be a single val# / range interval.
1019 if (vrm.getPreSplitReg(li.reg)) {
1020 vrm.setIsSplitFromReg(li.reg, 0);
1021 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1022 Slot = vrm.getStackSlot(li.reg);
1023 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1024 MachineInstr *ReMatDefMI = DefIsReMat ?
1025 vrm.getReMaterializedMI(li.reg) : NULL;
1026 int LdSlot = 0;
1027 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1028 bool isLoad = isLoadSS ||
1029 (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
Evan Cheng81a03822007-11-17 00:40:40 +00001030 bool IsFirstRange = true;
1031 for (LiveInterval::Ranges::const_iterator
1032 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1033 // If this is a split live interval with multiple ranges, it means there
1034 // are two-address instructions that re-defined the value. Only the
1035 // first def can be rematerialized!
1036 if (IsFirstRange) {
1037 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1038 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1039 false, vrm, RegMap, rc, ReMatIds,
1040 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
1041 } else {
1042 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1043 Slot, 0, false, false, false,
1044 false, vrm, RegMap, rc, ReMatIds,
1045 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
1046 }
1047 IsFirstRange = false;
1048 }
1049 return NewLIs;
1050 }
1051
1052 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Chengf2fbca62007-11-12 06:35:08 +00001053 bool NeedStackSlot = false;
1054 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1055 i != e; ++i) {
1056 const VNInfo *VNI = *i;
1057 unsigned VN = VNI->id;
1058 unsigned DefIdx = VNI->def;
1059 if (DefIdx == ~1U)
1060 continue; // Dead val#.
1061 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001062 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1063 ? 0 : getInstructionFromIndex(DefIdx);
1064 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001065 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001066 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001067 // Original def may be modified so we have to make a copy here. vrm must
1068 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001069 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
1070 vrm.setVirtIsReMaterialized(li.reg, ReMatDefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001071
1072 bool CanDelete = true;
1073 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1074 unsigned KillIdx = VNI->kills[j];
1075 MachineInstr *KillMI = (KillIdx & 1)
1076 ? NULL : getInstructionFromIndex(KillIdx);
1077 // Kill is a phi node, not all of its uses can be rematerialized.
1078 // It must not be deleted.
1079 if (!KillMI) {
1080 CanDelete = false;
1081 // Need a stack slot if there is any live range where uses cannot be
1082 // rematerialized.
1083 NeedStackSlot = true;
1084 break;
1085 }
1086 }
1087
1088 if (CanDelete)
1089 ReMatDelete.set(VN);
1090 } else {
1091 // Need a stack slot if there is any live range where uses cannot be
1092 // rematerialized.
1093 NeedStackSlot = true;
1094 }
1095 }
1096
1097 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001098 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001099 Slot = vrm.assignVirt2StackSlot(li.reg);
1100
1101 // Create new intervals and rewrite defs and uses.
1102 for (LiveInterval::Ranges::const_iterator
1103 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001104 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1105 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1106 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001107 bool CanDelete = ReMatDelete[I->valno->id];
1108 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001109 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001110 bool isLoad = isLoadSS ||
Evan Cheng81a03822007-11-17 00:40:40 +00001111 (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
1112 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
1113 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1114 CanDelete, vrm, RegMap, rc, ReMatIds,
1115 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001116 }
1117
Evan Cheng81a03822007-11-17 00:40:40 +00001118 // Insert spills if we are splitting.
1119 if (TrySplit && NeedStackSlot) {
1120 int Id = SpillMBBs.find_first();
1121 while (Id != -1) {
1122 unsigned index = SpillIdxes[Id].first;
1123 unsigned VReg = SpillIdxes[Id].second & ((1 << 31)-1);
1124 bool TryFold = SpillIdxes[Id].second & (1 << 31);
1125 MachineInstr *MI = getInstructionFromIndex(index);
1126 int OpIdx = -1;
1127 if (TryFold) {
1128 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1129 MachineOperand &MO = MI->getOperand(j);
1130 if (!MO.isRegister() || MO.getReg() != VReg)
1131 continue;
1132 if (MO.isUse()) {
1133 // Can't fold if it's two-address code.
1134 OpIdx = -1;
1135 break;
1136 }
1137 OpIdx = (int)j;
1138 }
1139 }
1140 // Fold the store into the def if possible.
1141 if (OpIdx == -1 ||
1142 !tryFoldMemoryOperand(MI, vrm, NULL, index, OpIdx, true, Slot, VReg))
1143 // Else tell the spiller to issue a store for us.
1144 vrm.addSpillPoint(VReg, MI);
1145 Id = SpillMBBs.find_next(Id);
1146 }
1147 }
1148
1149 // Finalize spill weights.
1150 if (TrySplit)
1151 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i)
1152 NewLIs[i]->weight /= NewLIs[i]->getSize();
1153
Evan Chengf2fbca62007-11-12 06:35:08 +00001154 return NewLIs;
1155}