blob: d2e32ab35c4021789af8018e5dd1fc21685920d6 [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,
710 const LoopInfo *loopInfo, std::vector<unsigned> &NewVRegs,
Evan Chengf2fbca62007-11-12 06:35:08 +0000711 std::vector<LiveInterval*> &NewLIs) {
712 RestartInstruction:
713 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
714 MachineOperand& mop = MI->getOperand(i);
715 if (!mop.isRegister())
716 continue;
717 unsigned Reg = mop.getReg();
718 unsigned RegI = Reg;
719 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
720 continue;
Evan Chengc498b022007-11-14 07:59:08 +0000721 unsigned SubIdx = mop.getSubReg();
722 bool isSubReg = SubIdx != 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000723 if (Reg != li.reg)
724 continue;
725
726 bool TryFold = !DefIsReMat;
727 bool FoldSS = true;
728 int FoldSlot = Slot;
729 if (DefIsReMat) {
730 // If this is the rematerializable definition MI itself and
731 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000732 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000733 RemoveMachineInstrFromMaps(MI);
734 MI->eraseFromParent();
735 break;
736 }
737
738 // If def for this use can't be rematerialized, then try folding.
Evan Cheng81a03822007-11-17 00:40:40 +0000739 TryFold = !ReMatOrigDefMI ||
740 (ReMatOrigDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000741 if (isLoad) {
742 // Try fold loads (from stack slot, constant pool, etc.) into uses.
743 FoldSS = isLoadSS;
744 FoldSlot = LdSlot;
745 }
746 }
747
Evan Cheng81a03822007-11-17 00:40:40 +0000748 // If we are splitting live intervals, only fold if it's 1) the first
749 // use and it's a kill or 2) there isn't another use later in this MBB.
750 TryFold &= NewVReg == 0;
751 if (TryFold && TrySplit)
752 // Do not fold store into def here if we are splitting. We'll find an
753 // optimal point to insert a store later.
754 if (HasDef || mop.isDef() ||
755 (!mop.isKill() && hasALaterUse(MI->getParent(), MI, li.reg)))
756 TryFold = false;
757
Evan Chengf2fbca62007-11-12 06:35:08 +0000758 // FIXME: fold subreg use
759 if (!isSubReg && TryFold &&
Evan Cheng81a03822007-11-17 00:40:40 +0000760 tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index, i, FoldSS, FoldSlot,
761 Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000762 // Folding the load/store can completely change the instruction in
763 // unpredictable ways, rescan it from the beginning.
764 goto RestartInstruction;
765
766 // Create a new virtual register for the spill interval.
Evan Cheng81a03822007-11-17 00:40:40 +0000767 bool CreatedNewVReg = false;
768 if (NewVReg == 0) {
769 NewVReg = RegMap->createVirtualRegister(rc);
770 vrm.grow();
771 CreatedNewVReg = true;
772 }
773 mop.setReg(NewVReg);
Evan Chengf2fbca62007-11-12 06:35:08 +0000774
775 // Scan all of the operands of this instruction rewriting operands
776 // to use NewVReg instead of li.reg as appropriate. We do this for
777 // two reasons:
778 //
779 // 1. If the instr reads the same spilled vreg multiple times, we
780 // want to reuse the NewVReg.
781 // 2. If the instr is a two-addr instruction, we are required to
782 // keep the src/dst regs pinned.
783 //
784 // Keep track of whether we replace a use and/or def so that we can
785 // create the spill interval with the appropriate range.
Evan Chengf2fbca62007-11-12 06:35:08 +0000786
Evan Cheng81a03822007-11-17 00:40:40 +0000787 HasUse = mop.isUse();
788 HasDef = mop.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000789 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
790 if (!MI->getOperand(j).isRegister())
791 continue;
792 unsigned RegJ = MI->getOperand(j).getReg();
793 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
794 continue;
795 if (RegJ == RegI) {
796 MI->getOperand(j).setReg(NewVReg);
797 HasUse |= MI->getOperand(j).isUse();
798 HasDef |= MI->getOperand(j).isDef();
799 }
800 }
801
Evan Cheng81a03822007-11-17 00:40:40 +0000802 if (CreatedNewVReg) {
803 if (DefIsReMat) {
804 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
805 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
806 // Each valnum may have its own remat id.
807 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
808 } else {
809 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
810 }
811 if (!CanDelete || (HasUse && HasDef)) {
812 // If this is a two-addr instruction then its use operands are
813 // rematerializable but its def is not. It should be assigned a
814 // stack slot.
815 vrm.assignVirt2StackSlot(NewVReg, Slot);
816 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000817 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000818 vrm.assignVirt2StackSlot(NewVReg, Slot);
819 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000820 }
821
822 // create a new register interval for this spill / remat.
823 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000824 if (CreatedNewVReg) {
825 NewLIs.push_back(&nI);
826 NewVRegs[MI->getParent()->getNumber()] = NewVReg;
827 if (TrySplit)
828 vrm.setIsSplitFromReg(NewVReg, li.reg);
829 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000830
831 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +0000832 if (CreatedNewVReg) {
833 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
834 nI.getNextValue(~0U, 0, VNInfoAllocator));
835 DOUT << " +" << LR;
836 nI.addRange(LR);
837 } else {
838 // Extend the split live interval to this def / use.
839 unsigned End = getUseIndex(index)+1;
840 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
841 nI.getValNumInfo(nI.getNumValNums()-1));
842 DOUT << " +" << LR;
843 nI.addRange(LR);
844 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000845 }
846 if (HasDef) {
847 LiveRange LR(getDefIndex(index), getStoreIndex(index),
848 nI.getNextValue(~0U, 0, VNInfoAllocator));
849 DOUT << " +" << LR;
850 nI.addRange(LR);
851 }
Evan Cheng81a03822007-11-17 00:40:40 +0000852
Evan Chengf2fbca62007-11-12 06:35:08 +0000853 DOUT << "\t\t\t\tAdded new interval: ";
854 nI.print(DOUT, mri_);
855 DOUT << '\n';
856 }
857}
858
Evan Cheng81a03822007-11-17 00:40:40 +0000859bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
860 MachineBasicBlock *MBB, unsigned Idx,
861 const VNInfo *VNI) const {
862 unsigned End = getMBBEndIdx(MBB);
863 if (VNI) {
864 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
865 unsigned KillIdx = VNI->kills[j];
866 if (KillIdx > Idx && KillIdx < End)
867 return true;
868 }
869 return false;
870 }
871
872 // Look at all the VNInfo's.
873 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
874 i != e; ++i) {
875 const VNInfo *VNI = *i;
876 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
877 unsigned KillIdx = VNI->kills[j];
878 if (KillIdx > Idx && KillIdx < End)
879 return true;
880 }
881 }
882 return false;
883}
884
Evan Chengf2fbca62007-11-12 06:35:08 +0000885void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000886rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000887 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +0000888 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000889 unsigned Slot, int LdSlot,
890 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
891 VirtRegMap &vrm, SSARegMap *RegMap,
892 const TargetRegisterClass* rc,
893 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000894 const LoopInfo *loopInfo,
895 BitVector &SpillMBBs,
896 std::vector<std::pair<int, unsigned> > &SpillIdxes,
897 std::vector<unsigned> &NewVRegs,
Evan Chengf2fbca62007-11-12 06:35:08 +0000898 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng81a03822007-11-17 00:40:40 +0000899 unsigned NewVReg = 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000900 unsigned index = getBaseIndex(I->start);
901 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Cheng81a03822007-11-17 00:40:40 +0000902 bool TrySplitMI = TrySplit && vrm.getPreSplitReg(li.reg) == 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000903 for (; index != end; index += InstrSlots::NUM) {
904 // skip deleted instructions
905 while (index != end && !getInstructionFromIndex(index))
906 index += InstrSlots::NUM;
907 if (index == end) break;
908
909 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng81a03822007-11-17 00:40:40 +0000910 MachineBasicBlock *MBB = MI->getParent();
911 NewVReg = !TrySplitMI ? 0 : NewVRegs[MBB->getNumber()];
912 bool IsNew = NewVReg == 0;
913 bool HasDef = false;
914 bool HasUse = false;
915 rewriteInstructionForSpills(li, TrySplitMI, I->valno->id, index, end,
916 MI, ReMatOrigDefMI, ReMatDefMI, Slot, LdSlot,
917 isLoad, isLoadSS, DefIsReMat, CanDelete, vrm,
918 RegMap, rc, ReMatIds, NewVReg, HasDef, HasUse,
919 loopInfo, NewVRegs, NewLIs);
920 if (!HasDef && !HasUse)
921 continue;
922
923 // Update weight of spill interval.
924 LiveInterval &nI = getOrCreateInterval(NewVReg);
925 if (!TrySplitMI)
926 // The spill weight is now infinity as it cannot be spilled again.
927 nI.weight = HUGE_VALF;
928 else {
929 // Keep track of the last def in each MBB.
930 if (HasDef) {
931 if (MI != ReMatOrigDefMI || !CanDelete) {
932 // If this is a two-address code, then this index probably starts a
933 // VNInfo so we should examine all the VNInfo's.
934 bool HasKill = HasUse
935 ? anyKillInMBBAfterIdx(li, MBB, getDefIndex(index))
936 : anyKillInMBBAfterIdx(li, MBB, getDefIndex(index), I->valno);
937 if (!HasKill) {
938 unsigned MBBId = MBB->getNumber();
939 if ((int)index > SpillIdxes[MBBId].first)
940 // High bit specify whether this spill ought to be folded if
941 // possible.
942 SpillIdxes[MBBId] = std::make_pair(index, NewVReg | (1 << 31));
943 SpillMBBs.set(MBBId);
944 }
945 }
946 if (!IsNew) {
947 // It this interval hasn't been assigned a stack slot
948 // (because earlier def is remat), do it now.
949 int SS = vrm.getStackSlot(NewVReg);
950 if (SS != (int)Slot) {
951 assert(SS == VirtRegMap::NO_STACK_SLOT);
952 vrm.assignVirt2StackSlot(NewVReg, Slot);
953 }
954 }
955 } else if (HasUse) {
956 // Use(s) following the last def, it's not safe to fold the spill.
957 unsigned MBBId = MBB->getNumber();
958 if ((SpillIdxes[MBBId].second & ((1<<31)-1)) == NewVReg &&
959 (int)getUseIndex(index) > SpillIdxes[MBBId].first)
960 SpillIdxes[MBBId].second &= (1<<31)-1;
961 }
962
963 // Update spill weight.
964 unsigned loopDepth = loopInfo->getLoopDepth(MBB->getBasicBlock());
965 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
966 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000967 }
968}
969
Evan Cheng81a03822007-11-17 00:40:40 +0000970
971
Evan Chengf2fbca62007-11-12 06:35:08 +0000972std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000973addIntervalsForSpills(const LiveInterval &li,
974 const LoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000975 // Since this is called after the analysis is done we don't know if
976 // LiveVariables is available
977 lv_ = getAnalysisToUpdate<LiveVariables>();
978
979 assert(li.weight != HUGE_VALF &&
980 "attempt to spill already spilled interval!");
981
982 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
983 li.print(DOUT, mri_);
984 DOUT << '\n';
985
Evan Cheng81a03822007-11-17 00:40:40 +0000986 // Each bit specify whether it a spill is required in the MBB.
987 BitVector SpillMBBs(mf_->getNumBlockIDs());
988 std::vector<std::pair<int, unsigned> > SpillIdxes(mf_->getNumBlockIDs(),
989 std::make_pair(-1,0));
990 std::vector<unsigned> NewVRegs(mf_->getNumBlockIDs(), 0);
Evan Chengf2fbca62007-11-12 06:35:08 +0000991 std::vector<LiveInterval*> NewLIs;
992 SSARegMap *RegMap = mf_->getSSARegMap();
993 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
994
995 unsigned NumValNums = li.getNumValNums();
996 SmallVector<MachineInstr*, 4> ReMatDefs;
997 ReMatDefs.resize(NumValNums, NULL);
998 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
999 ReMatOrigDefs.resize(NumValNums, NULL);
1000 SmallVector<int, 4> ReMatIds;
1001 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1002 BitVector ReMatDelete(NumValNums);
1003 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1004
Evan Cheng81a03822007-11-17 00:40:40 +00001005 // Spilling a split live interval. It cannot be split any further. Also,
1006 // it's also guaranteed to be a single val# / range interval.
1007 if (vrm.getPreSplitReg(li.reg)) {
1008 vrm.setIsSplitFromReg(li.reg, 0);
1009 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1010 Slot = vrm.getStackSlot(li.reg);
1011 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1012 MachineInstr *ReMatDefMI = DefIsReMat ?
1013 vrm.getReMaterializedMI(li.reg) : NULL;
1014 int LdSlot = 0;
1015 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1016 bool isLoad = isLoadSS ||
1017 (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
1018 vrm.removeAllSpillPtsForReg(li.reg);
1019 bool IsFirstRange = true;
1020 for (LiveInterval::Ranges::const_iterator
1021 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1022 // If this is a split live interval with multiple ranges, it means there
1023 // are two-address instructions that re-defined the value. Only the
1024 // first def can be rematerialized!
1025 if (IsFirstRange) {
1026 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1027 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1028 false, vrm, RegMap, rc, ReMatIds,
1029 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
1030 } else {
1031 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1032 Slot, 0, false, false, false,
1033 false, vrm, RegMap, rc, ReMatIds,
1034 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
1035 }
1036 IsFirstRange = false;
1037 }
1038 return NewLIs;
1039 }
1040
1041 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Chengf2fbca62007-11-12 06:35:08 +00001042 bool NeedStackSlot = false;
1043 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1044 i != e; ++i) {
1045 const VNInfo *VNI = *i;
1046 unsigned VN = VNI->id;
1047 unsigned DefIdx = VNI->def;
1048 if (DefIdx == ~1U)
1049 continue; // Dead val#.
1050 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001051 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1052 ? 0 : getInstructionFromIndex(DefIdx);
1053 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001054 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001055 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001056 // Original def may be modified so we have to make a copy here. vrm must
1057 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001058 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
1059 vrm.setVirtIsReMaterialized(li.reg, ReMatDefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001060
1061 bool CanDelete = true;
1062 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1063 unsigned KillIdx = VNI->kills[j];
1064 MachineInstr *KillMI = (KillIdx & 1)
1065 ? NULL : getInstructionFromIndex(KillIdx);
1066 // Kill is a phi node, not all of its uses can be rematerialized.
1067 // It must not be deleted.
1068 if (!KillMI) {
1069 CanDelete = false;
1070 // Need a stack slot if there is any live range where uses cannot be
1071 // rematerialized.
1072 NeedStackSlot = true;
1073 break;
1074 }
1075 }
1076
1077 if (CanDelete)
1078 ReMatDelete.set(VN);
1079 } else {
1080 // Need a stack slot if there is any live range where uses cannot be
1081 // rematerialized.
1082 NeedStackSlot = true;
1083 }
1084 }
1085
1086 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001087 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001088 Slot = vrm.assignVirt2StackSlot(li.reg);
1089
Evan Cheng81a03822007-11-17 00:40:40 +00001090
Evan Chengf2fbca62007-11-12 06:35:08 +00001091 // Create new intervals and rewrite defs and uses.
1092 for (LiveInterval::Ranges::const_iterator
1093 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001094 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1095 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1096 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001097 bool CanDelete = ReMatDelete[I->valno->id];
1098 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001099 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001100 bool isLoad = isLoadSS ||
Evan Cheng81a03822007-11-17 00:40:40 +00001101 (DefIsReMat && (ReMatDefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
1102 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
1103 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1104 CanDelete, vrm, RegMap, rc, ReMatIds,
1105 loopInfo, SpillMBBs, SpillIdxes, NewVRegs, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001106 }
1107
Evan Cheng81a03822007-11-17 00:40:40 +00001108 // Insert spills if we are splitting.
1109 if (TrySplit && NeedStackSlot) {
1110 int Id = SpillMBBs.find_first();
1111 while (Id != -1) {
1112 unsigned index = SpillIdxes[Id].first;
1113 unsigned VReg = SpillIdxes[Id].second & ((1 << 31)-1);
1114 bool TryFold = SpillIdxes[Id].second & (1 << 31);
1115 MachineInstr *MI = getInstructionFromIndex(index);
1116 int OpIdx = -1;
1117 if (TryFold) {
1118 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1119 MachineOperand &MO = MI->getOperand(j);
1120 if (!MO.isRegister() || MO.getReg() != VReg)
1121 continue;
1122 if (MO.isUse()) {
1123 // Can't fold if it's two-address code.
1124 OpIdx = -1;
1125 break;
1126 }
1127 OpIdx = (int)j;
1128 }
1129 }
1130 // Fold the store into the def if possible.
1131 if (OpIdx == -1 ||
1132 !tryFoldMemoryOperand(MI, vrm, NULL, index, OpIdx, true, Slot, VReg))
1133 // Else tell the spiller to issue a store for us.
1134 vrm.addSpillPoint(VReg, MI);
1135 Id = SpillMBBs.find_next(Id);
1136 }
1137 }
1138
1139 // Finalize spill weights.
1140 if (TrySplit)
1141 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i)
1142 NewLIs[i]->weight /= NewLIs[i]->getSize();
1143
Evan Chengf2fbca62007-11-12 06:35:08 +00001144 return NewLIs;
1145}