blob: a64bf60f3b243c9427e70f8fc4faf24ad04ee403 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Evan Chengbc165e42007-08-16 07:24:22 +000039namespace {
40 // Hidden options for help debugging.
41 cl::opt<bool> DisableReMat("disable-rematerialization",
42 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000043
44 cl::opt<bool> SplitAtBB("split-intervals-at-bb",
Evan Cheng33faddc2007-12-06 08:54:31 +000045 cl::init(true), cl::Hidden);
Evan Cheng0cbb1162007-11-29 01:06:25 +000046 cl::opt<int> SplitLimit("split-limit",
47 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000048}
49
Chris Lattnercd3245a2006-12-19 22:41:21 +000050STATISTIC(numIntervals, "Number of original intervals");
51STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000052STATISTIC(numFolds , "Number of loads/stores folded into instructions");
53STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000054
Devang Patel19974732007-05-03 01:11:54 +000055char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000056namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000057 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000059
Chris Lattnerf7da2c72006-08-24 22:43:55 +000060void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000061 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000063 AU.addPreservedID(MachineLoopInfoID);
64 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 AU.addPreservedID(PHIEliminationID);
66 AU.addRequiredID(PHIEliminationID);
67 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069}
70
Chris Lattnerf7da2c72006-08-24 22:43:55 +000071void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000072 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 mi2iMap_.clear();
74 i2miMap_.clear();
75 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000076 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
77 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000078 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
79 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000080}
81
Evan Cheng4ca980e2007-10-17 02:10:22 +000082namespace llvm {
83 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
84 return V < IM.first;
85 }
86
87 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
88 return IM.first < V;
89 }
90
91 struct Idx2MBBCompare {
92 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
93 return LHS.first < RHS.first;
94 }
95 };
96}
97
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000098/// runOnMachineFunction - Register allocate the whole function
99///
100bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000101 mf_ = &fn;
102 tm_ = &fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000103 tri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +0000104 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 lv_ = &getAnalysis<LiveVariables>();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000106 allocatableRegs_ = tri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000107
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 // Number MachineInstrs and MachineBasicBlocks.
109 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000110 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000111
112 unsigned MIIndex = 0;
113 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
114 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000115 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000116
Chris Lattner428b92e2006-09-15 03:57:23 +0000117 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
118 I != E; ++I) {
119 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000121 i2miMap_.push_back(I);
122 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000123 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000124
125 // Set the MBB2IdxMap entry for this MBB.
126 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000127 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000128 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000129 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000130
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000132
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000133 numIntervals += getNumIntervals();
134
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000135 DOUT << "********** INTERVALS **********\n";
136 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000137 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000138 DOUT << "\n";
139 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000142 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000143 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144}
145
Chris Lattner70ca3582004-09-30 15:59:17 +0000146/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000147void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000148 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000149 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000150 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000151 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000152 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000153
154 O << "********** MACHINEINSTRS **********\n";
155 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
156 mbbi != mbbe; ++mbbi) {
157 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
158 for (MachineBasicBlock::iterator mii = mbbi->begin(),
159 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000160 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000161 }
162 }
163}
164
Evan Chengc92da382007-11-03 07:20:12 +0000165/// conflictsWithPhysRegDef - Returns true if the specified register
166/// is defined during the duration of the specified interval.
167bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
168 VirtRegMap &vrm, unsigned reg) {
169 for (LiveInterval::Ranges::const_iterator
170 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
171 for (unsigned index = getBaseIndex(I->start),
172 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
173 index += InstrSlots::NUM) {
174 // skip deleted instructions
175 while (index != end && !getInstructionFromIndex(index))
176 index += InstrSlots::NUM;
177 if (index == end) break;
178
179 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000180 unsigned SrcReg, DstReg;
181 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
182 if (SrcReg == li.reg || DstReg == li.reg)
183 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000184 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
185 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000186 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000187 continue;
188 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000189 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000190 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000191 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000192 if (!vrm.hasPhys(PhysReg))
193 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000194 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000195 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000196 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000197 return true;
198 }
199 }
200 }
201
202 return false;
203}
204
Evan Cheng549f27d32007-08-13 23:45:17 +0000205void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000206 if (TargetRegisterInfo::isPhysicalRegister(reg))
207 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000208 else
209 cerr << "%reg" << reg;
210}
211
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000212void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000213 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000214 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000215 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000216 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000217 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000218
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000219 // Virtual registers may be defined multiple times (due to phi
220 // elimination and 2-addr elimination). Much of what we do only has to be
221 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222 // time we see a vreg.
223 if (interval.empty()) {
224 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000225 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000226 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000227 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000228 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000229 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
230 tii_->isMoveInstr(*mi, SrcReg, DstReg))
231 CopyMI = mi;
232 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000233
234 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000235
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000236 // Loop over all of the blocks that the vreg is defined in. There are
237 // two cases we have to handle here. The most common case is a vreg
238 // whose lifetime is contained within a basic block. In this case there
239 // will be a single kill, in MBB, which comes after the definition.
240 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
241 // FIXME: what about dead vars?
242 unsigned killIdx;
243 if (vi.Kills[0] != mi)
244 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
245 else
246 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000247
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000248 // If the kill happens after the definition, we have an intra-block
249 // live range.
250 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000251 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000252 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000253 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000255 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000256 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000257 return;
258 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000259 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000260
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000261 // The other case we handle is when a virtual register lives to the end
262 // of the defining block, potentially live across some blocks, then is
263 // live into some number of blocks, but gets killed. Start by adding a
264 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000265 LiveRange NewLR(defIndex,
266 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000267 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000268 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000269 interval.addRange(NewLR);
270
271 // Iterate over all of the blocks that the variable is completely
272 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
273 // live interval.
274 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
275 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000276 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
277 if (!MBB->empty()) {
278 LiveRange LR(getMBBStartIdx(i),
279 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000280 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000281 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000282 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 }
284 }
285 }
286
287 // Finally, this virtual register is live from the start of any killing
288 // block to the 'use' slot of the killing instruction.
289 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
290 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000291 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000292 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000295 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000296 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 }
298
299 } else {
300 // If this is the second time we see a virtual register definition, it
301 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000302 // the result of two address elimination, then the vreg is one of the
303 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000304 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000305 // If this is a two-address definition, then we have already processed
306 // the live range. The only problem is that we didn't realize there
307 // are actually two values in the live interval. Because of this we
308 // need to take the LiveRegion that defines this register and split it
309 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000310 assert(interval.containsOneValue());
311 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000312 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313
Evan Cheng4f8ff162007-08-11 00:59:19 +0000314 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000315 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000316
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000318 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000319 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000320
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000321 // Two-address vregs should always only be redefined once. This means
322 // that at this point, there should be exactly one value number in it.
323 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
324
Chris Lattner91725b72006-08-31 05:54:43 +0000325 // The new value number (#1) is defined by the instruction we claimed
326 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000327 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
328 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000329
Chris Lattner91725b72006-08-31 05:54:43 +0000330 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000331 OldValNo->def = RedefIndex;
332 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000333
334 // Add the new live interval which replaces the range for the input copy.
335 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000336 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000338 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339
340 // If this redefinition is dead, we need to add a dummy unit live
341 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000342 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000343 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000345 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000346 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347
348 } else {
349 // Otherwise, this must be because of phi elimination. If this is the
350 // first redefinition of the vreg that we have seen, go back and change
351 // the live range in the PHI block to be a different value number.
352 if (interval.containsOneValue()) {
353 assert(vi.Kills.size() == 1 &&
354 "PHI elimination vreg should have one kill, the PHI itself!");
355
356 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000357 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000358 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000359 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000360 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000361 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000362 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000363 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000364 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000365 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000367 // Replace the interval with one of a NEW value number. Note that this
368 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000369 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000370 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000372 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000373 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 }
375
376 // In the case of PHI elimination, each variable definition is only
377 // live until the end of the block. We've already taken care of the
378 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000379 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000380
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000381 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000382 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000383 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000384 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
385 tii_->isMoveInstr(*mi, SrcReg, DstReg))
386 CopyMI = mi;
387 ValNo = interval.getNextValue(defIndex, CopyMI, 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 Chengc3fc7d92007-11-29 09:49:23 +0000392 interval.addKill(ValNo, killIndex);
393 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000394 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000395 }
396 }
397
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000398 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000399}
400
Chris Lattnerf35fef72004-07-23 21:24:19 +0000401void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000402 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000403 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000404 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000405 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000406 // A physical register cannot be live across basic block, so its
407 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000408 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000409
Chris Lattner6b128bd2006-09-03 08:07:11 +0000410 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000411 unsigned start = getDefIndex(baseIndex);
412 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000413
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000414 // If it is not used after definition, it is considered dead at
415 // the instruction defining it. Hence its interval is:
416 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000417 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000418 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000419 end = getDefIndex(start) + 1;
420 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421 }
422
423 // If it is not dead on definition, it must be killed by a
424 // subsequent instruction. Hence its interval is:
425 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000426 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000428 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000429 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000430 end = getUseIndex(baseIndex) + 1;
431 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000432 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
433 // Another instruction redefines the register before it is ever read.
434 // Then the register is essentially dead at the instruction that defines
435 // it. Hence its interval is:
436 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000437 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000438 end = getDefIndex(start) + 1;
439 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000440 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000441 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000442
443 // The only case we should have a dead physreg here without a killing or
444 // instruction where we know it's dead is if it is live-in to the function
445 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000446 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000447 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000448
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000449exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000450 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000451
Evan Cheng24a3cc42007-04-25 07:30:23 +0000452 // Already exists? Extend old live interval.
453 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000454 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000455 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000456 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000457 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000458 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000459 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000460}
461
Chris Lattnerf35fef72004-07-23 21:24:19 +0000462void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
463 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000464 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000465 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000466 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000467 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000468 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000469 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000470 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000471 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
472 tii_->isMoveInstr(*MI, SrcReg, DstReg))
473 CopyMI = MI;
474 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000475 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000476 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng24a3cc42007-04-25 07:30:23 +0000477 // Avoid processing some defs more than once.
478 if (!MI->findRegisterDefOperand(*AS))
479 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000480 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000481}
482
Evan Chengb371f452007-02-19 21:49:54 +0000483void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000484 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000485 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000486 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
487
488 // Look for kills, if it reaches a def before it's killed, then it shouldn't
489 // be considered a livein.
490 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000491 unsigned baseIndex = MIIdx;
492 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000493 unsigned end = start;
494 while (mi != MBB->end()) {
495 if (lv_->KillsRegister(mi, interval.reg)) {
496 DOUT << " killed";
497 end = getUseIndex(baseIndex) + 1;
498 goto exit;
499 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
500 // Another instruction redefines the register before it is ever read.
501 // Then the register is essentially dead at the instruction that defines
502 // it. Hence its interval is:
503 // [defSlot(def), defSlot(def)+1)
504 DOUT << " dead";
505 end = getDefIndex(start) + 1;
506 goto exit;
507 }
508
509 baseIndex += InstrSlots::NUM;
510 ++mi;
511 }
512
513exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000514 // Live-in register might not be used at all.
515 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000516 if (isAlias) {
517 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000518 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000519 } else {
520 DOUT << " live through";
521 end = baseIndex;
522 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000523 }
524
Evan Chengf3bb2e62007-09-05 21:46:51 +0000525 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000526 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000527 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000528 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000529}
530
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000531/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000532/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000533/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000534/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000535void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000536 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
537 << "********** Function: "
538 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000539 // Track the index of the current machine instr.
540 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000541 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
542 MBBI != E; ++MBBI) {
543 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000544 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000545
Chris Lattner428b92e2006-09-15 03:57:23 +0000546 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000547
Dan Gohmancb406c22007-10-03 19:26:29 +0000548 // Create intervals for live-ins to this BB first.
549 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
550 LE = MBB->livein_end(); LI != LE; ++LI) {
551 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
552 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000553 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000554 if (!hasInterval(*AS))
555 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
556 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000557 }
558
Chris Lattner428b92e2006-09-15 03:57:23 +0000559 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000560 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000561
Evan Cheng438f7bc2006-11-10 08:43:01 +0000562 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000563 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
564 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000565 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000566 if (MO.isRegister() && MO.getReg() && MO.isDef())
567 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000569
570 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000571 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000572 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000573}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000574
Evan Cheng4ca980e2007-10-17 02:10:22 +0000575bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000576 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000577 std::vector<IdxMBBPair>::const_iterator I =
578 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
579
580 bool ResVal = false;
581 while (I != Idx2MBBMap.end()) {
582 if (LR.end <= I->first)
583 break;
584 MBBs.push_back(I->second);
585 ResVal = true;
586 ++I;
587 }
588 return ResVal;
589}
590
591
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000592LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000593 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000594 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000595 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000596}
Evan Chengf2fbca62007-11-12 06:35:08 +0000597
Evan Chengc8d044e2008-02-15 18:24:29 +0000598/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
599/// copy field and returns the source register that defines it.
600unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
601 if (!VNI->copy)
602 return 0;
603
604 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
605 return VNI->copy->getOperand(1).getReg();
606 unsigned SrcReg, DstReg;
607 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
608 return SrcReg;
609 assert(0 && "Unrecognized copy instruction!");
610 return 0;
611}
Evan Chengf2fbca62007-11-12 06:35:08 +0000612
613//===----------------------------------------------------------------------===//
614// Register allocator hooks.
615//
616
617/// isReMaterializable - Returns true if the definition MI of the specified
618/// val# of the specified interval is re-materializable.
619bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000620 const VNInfo *ValNo, MachineInstr *MI,
621 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000622 if (DisableReMat)
623 return false;
624
Evan Cheng5ef3a042007-12-06 00:01:56 +0000625 isLoad = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000626 const TargetInstrDesc &TID = MI->getDesc();
627 if (TID.isImplicitDef() || tii_->isTriviallyReMaterializable(MI)) {
628 isLoad = TID.isSimpleLoad();
Evan Chengf2fbca62007-11-12 06:35:08 +0000629 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000630 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000631
632 int FrameIdx = 0;
633 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
Evan Cheng84802932008-01-10 08:24:38 +0000634 !mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Chengf2fbca62007-11-12 06:35:08 +0000635 return false;
636
637 // This is a load from fixed stack slot. It can be rematerialized unless it's
638 // re-defined by a two-address instruction.
Evan Cheng5ef3a042007-12-06 00:01:56 +0000639 isLoad = true;
Evan Chengf2fbca62007-11-12 06:35:08 +0000640 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
641 i != e; ++i) {
642 const VNInfo *VNI = *i;
643 if (VNI == ValNo)
644 continue;
645 unsigned DefIdx = VNI->def;
646 if (DefIdx == ~1U)
647 continue; // Dead val#.
648 MachineInstr *DefMI = (DefIdx == ~0u)
649 ? NULL : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000650 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg)) {
651 isLoad = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000652 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000653 }
654 }
655 return true;
656}
657
658/// isReMaterializable - Returns true if every definition of MI of every
659/// val# of the specified interval is re-materializable.
660bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
661 isLoad = false;
662 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
663 i != e; ++i) {
664 const VNInfo *VNI = *i;
665 unsigned DefIdx = VNI->def;
666 if (DefIdx == ~1U)
667 continue; // Dead val#.
668 // Is the def for the val# rematerializable?
669 if (DefIdx == ~0u)
670 return false;
671 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
672 bool DefIsLoad = false;
673 if (!ReMatDefMI || !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
674 return false;
675 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000676 }
677 return true;
678}
679
680/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
681/// slot / to reg or any rematerialized load into ith operand of specified
682/// MI. If it is successul, MI is updated with the newly created MI and
683/// returns true.
Evan Cheng81a03822007-11-17 00:40:40 +0000684bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
Evan Chengcddbb832007-11-30 21:23:43 +0000685 VirtRegMap &vrm, MachineInstr *DefMI,
Evan Chengaee4af62007-12-02 08:30:39 +0000686 unsigned InstrIdx,
687 SmallVector<unsigned, 2> &Ops,
Evan Chengcddbb832007-11-30 21:23:43 +0000688 bool isSS, int Slot, unsigned Reg) {
Evan Chengaee4af62007-12-02 08:30:39 +0000689 unsigned MRInfo = 0;
Chris Lattner749c6f62008-01-07 07:27:27 +0000690 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000691 // If it is an implicit def instruction, just delete it.
Chris Lattner749c6f62008-01-07 07:27:27 +0000692 if (TID.isImplicitDef()) {
Evan Cheng6e141fd2007-12-12 23:12:09 +0000693 RemoveMachineInstrFromMaps(MI);
694 vrm.RemoveMachineInstrFromMaps(MI);
695 MI->eraseFromParent();
696 ++numFolds;
697 return true;
698 }
699
Evan Chengaee4af62007-12-02 08:30:39 +0000700 SmallVector<unsigned, 2> FoldOps;
701 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
702 unsigned OpIdx = Ops[i];
703 // FIXME: fold subreg use.
704 if (MI->getOperand(OpIdx).getSubReg())
Evan Chenge62f97c2007-12-01 02:07:52 +0000705 return false;
Evan Chengaee4af62007-12-02 08:30:39 +0000706 if (MI->getOperand(OpIdx).isDef())
707 MRInfo |= (unsigned)VirtRegMap::isMod;
708 else {
709 // Filter out two-address use operand(s).
Chris Lattner749c6f62008-01-07 07:27:27 +0000710 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000711 MRInfo = VirtRegMap::isModRef;
712 continue;
713 }
714 MRInfo |= (unsigned)VirtRegMap::isRef;
715 }
716 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000717 }
718
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000719 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
720 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000721 if (fmi) {
722 // Attempt to fold the memory reference into the instruction. If
723 // we can do this, we don't need to insert spill code.
724 if (lv_)
725 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000726 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000727 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000728 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000729 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000730 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000731 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000732 vrm.transferRestorePts(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000733 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000734 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
735 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000736 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000737 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000738 return true;
739 }
740 return false;
741}
742
Evan Cheng018f9b02007-12-05 03:22:34 +0000743/// canFoldMemoryOperand - Returns true if the specified load / store
744/// folding is possible.
745bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
746 SmallVector<unsigned, 2> &Ops) const {
747 SmallVector<unsigned, 2> FoldOps;
748 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
749 unsigned OpIdx = Ops[i];
750 // FIXME: fold subreg use.
751 if (MI->getOperand(OpIdx).getSubReg())
752 return false;
753 FoldOps.push_back(OpIdx);
754 }
755
Owen Anderson6425f8b2008-01-07 01:35:56 +0000756 return tii_->canFoldMemoryOperand(MI, FoldOps);
Evan Cheng018f9b02007-12-05 03:22:34 +0000757}
758
Evan Cheng81a03822007-11-17 00:40:40 +0000759bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
760 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
761 for (LiveInterval::Ranges::const_iterator
762 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
763 std::vector<IdxMBBPair>::const_iterator II =
764 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
765 if (II == Idx2MBBMap.end())
766 continue;
767 if (I->end > II->first) // crossing a MBB.
768 return false;
769 MBBs.insert(II->second);
770 if (MBBs.size() > 1)
771 return false;
772 }
773 return true;
774}
775
Evan Chengf2fbca62007-11-12 06:35:08 +0000776/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
777/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000778bool LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000779rewriteInstructionForSpills(const LiveInterval &li, bool TrySplit,
780 unsigned id, unsigned index, unsigned end, MachineInstr *MI,
781 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000782 unsigned Slot, int LdSlot,
783 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000784 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000785 const TargetRegisterClass* rc,
786 SmallVector<int, 4> &ReMatIds,
Evan Cheng81a03822007-11-17 00:40:40 +0000787 unsigned &NewVReg, bool &HasDef, bool &HasUse,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000788 const MachineLoopInfo *loopInfo,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000789 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000790 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000791 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000792 RestartInstruction:
793 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
794 MachineOperand& mop = MI->getOperand(i);
795 if (!mop.isRegister())
796 continue;
797 unsigned Reg = mop.getReg();
798 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000799 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000800 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000801 if (Reg != li.reg)
802 continue;
803
804 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000805 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000806 int FoldSlot = Slot;
807 if (DefIsReMat) {
808 // If this is the rematerializable definition MI itself and
809 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000810 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000811 DOUT << "\t\t\t\tErasing re-materlizable def: ";
812 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000813 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000814 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000815 MI->eraseFromParent();
816 break;
817 }
818
819 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000820 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000821 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000822 if (isLoad) {
823 // Try fold loads (from stack slot, constant pool, etc.) into uses.
824 FoldSS = isLoadSS;
825 FoldSlot = LdSlot;
826 }
827 }
828
Evan Chengf2fbca62007-11-12 06:35:08 +0000829 // Scan all of the operands of this instruction rewriting operands
830 // to use NewVReg instead of li.reg as appropriate. We do this for
831 // two reasons:
832 //
833 // 1. If the instr reads the same spilled vreg multiple times, we
834 // want to reuse the NewVReg.
835 // 2. If the instr is a two-addr instruction, we are required to
836 // keep the src/dst regs pinned.
837 //
838 // Keep track of whether we replace a use and/or def so that we can
839 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000840
Evan Cheng81a03822007-11-17 00:40:40 +0000841 HasUse = mop.isUse();
842 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000843 SmallVector<unsigned, 2> Ops;
844 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000845 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000846 const MachineOperand &MOj = MI->getOperand(j);
847 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000848 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000849 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000850 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000851 continue;
852 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000853 Ops.push_back(j);
854 HasUse |= MOj.isUse();
855 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000856 }
857 }
858
Evan Cheng018f9b02007-12-05 03:22:34 +0000859 if (TryFold) {
860 // Do not fold load / store here if we are splitting. We'll find an
861 // optimal point to insert a load / store later.
862 if (!TrySplit) {
863 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
864 Ops, FoldSS, FoldSlot, Reg)) {
865 // Folding the load/store can completely change the instruction in
866 // unpredictable ways, rescan it from the beginning.
867 HasUse = false;
868 HasDef = false;
869 CanFold = false;
870 goto RestartInstruction;
871 }
872 } else {
873 CanFold = canFoldMemoryOperand(MI, Ops);
874 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000875 } else
876 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000877
878 // Create a new virtual register for the spill interval.
879 bool CreatedNewVReg = false;
880 if (NewVReg == 0) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000881 NewVReg = RegInfo.createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000882 vrm.grow();
883 CreatedNewVReg = true;
884 }
885 mop.setReg(NewVReg);
886
887 // Reuse NewVReg for other reads.
Evan Chengaee4af62007-12-02 08:30:39 +0000888 for (unsigned j = 0, e = Ops.size(); j != e; ++j)
889 MI->getOperand(Ops[j]).setReg(NewVReg);
Evan Chengcddbb832007-11-30 21:23:43 +0000890
Evan Cheng81a03822007-11-17 00:40:40 +0000891 if (CreatedNewVReg) {
892 if (DefIsReMat) {
893 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
894 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
895 // Each valnum may have its own remat id.
896 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
897 } else {
898 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
899 }
900 if (!CanDelete || (HasUse && HasDef)) {
901 // If this is a two-addr instruction then its use operands are
902 // rematerializable but its def is not. It should be assigned a
903 // stack slot.
904 vrm.assignVirt2StackSlot(NewVReg, Slot);
905 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000906 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000907 vrm.assignVirt2StackSlot(NewVReg, Slot);
908 }
Evan Chengcb3c3302007-11-29 23:02:50 +0000909 } else if (HasUse && HasDef &&
910 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
911 // If this interval hasn't been assigned a stack slot (because earlier
912 // def is a deleted remat def), do it now.
913 assert(Slot != VirtRegMap::NO_STACK_SLOT);
914 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +0000915 }
916
917 // create a new register interval for this spill / remat.
918 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000919 if (CreatedNewVReg) {
920 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +0000921 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +0000922 if (TrySplit)
923 vrm.setIsSplitFromReg(NewVReg, li.reg);
924 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000925
926 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +0000927 if (CreatedNewVReg) {
928 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
929 nI.getNextValue(~0U, 0, VNInfoAllocator));
930 DOUT << " +" << LR;
931 nI.addRange(LR);
932 } else {
933 // Extend the split live interval to this def / use.
934 unsigned End = getUseIndex(index)+1;
935 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
936 nI.getValNumInfo(nI.getNumValNums()-1));
937 DOUT << " +" << LR;
938 nI.addRange(LR);
939 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000940 }
941 if (HasDef) {
942 LiveRange LR(getDefIndex(index), getStoreIndex(index),
943 nI.getNextValue(~0U, 0, VNInfoAllocator));
944 DOUT << " +" << LR;
945 nI.addRange(LR);
946 }
Evan Cheng81a03822007-11-17 00:40:40 +0000947
Evan Chengf2fbca62007-11-12 06:35:08 +0000948 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000949 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000950 DOUT << '\n';
951 }
Evan Cheng018f9b02007-12-05 03:22:34 +0000952 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +0000953}
Evan Cheng81a03822007-11-17 00:40:40 +0000954bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000955 const VNInfo *VNI,
956 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000957 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000958 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
959 unsigned KillIdx = VNI->kills[j];
960 if (KillIdx > Idx && KillIdx < End)
961 return true;
Evan Cheng81a03822007-11-17 00:40:40 +0000962 }
963 return false;
964}
965
Evan Cheng1953d0c2007-11-29 10:12:14 +0000966static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
967 const VNInfo *VNI = NULL;
968 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
969 e = li.vni_end(); i != e; ++i)
970 if ((*i)->def == DefIdx) {
971 VNI = *i;
972 break;
973 }
974 return VNI;
975}
976
Evan Chengf2fbca62007-11-12 06:35:08 +0000977void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +0000978rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +0000979 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +0000980 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000981 unsigned Slot, int LdSlot,
982 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Chris Lattner84bc5422007-12-31 04:13:23 +0000983 VirtRegMap &vrm, MachineRegisterInfo &RegInfo,
Evan Chengf2fbca62007-11-12 06:35:08 +0000984 const TargetRegisterClass* rc,
985 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000986 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +0000987 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000988 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +0000989 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000990 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
991 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000992 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000993 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +0000994 unsigned NewVReg = 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000995 unsigned index = getBaseIndex(I->start);
996 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
997 for (; index != end; index += InstrSlots::NUM) {
998 // skip deleted instructions
999 while (index != end && !getInstructionFromIndex(index))
1000 index += InstrSlots::NUM;
1001 if (index == end) break;
1002
1003 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng81a03822007-11-17 00:40:40 +00001004 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng018f9b02007-12-05 03:22:34 +00001005 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001006 if (TrySplit) {
Evan Chengcada2452007-11-28 01:28:46 +00001007 std::map<unsigned,unsigned>::const_iterator NVI =
Evan Cheng1953d0c2007-11-29 10:12:14 +00001008 MBBVRegsMap.find(MBB->getNumber());
1009 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001010 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001011 // One common case:
1012 // x = use
1013 // ...
1014 // ...
1015 // def = ...
1016 // = use
1017 // It's better to start a new interval to avoid artifically
1018 // extend the new interval.
1019 // FIXME: Too slow? Can we fix it after rewriteInstructionsForSpills?
1020 bool MIHasUse = false;
1021 bool MIHasDef = false;
1022 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1023 MachineOperand& mop = MI->getOperand(i);
1024 if (!mop.isRegister() || mop.getReg() != li.reg)
1025 continue;
1026 if (mop.isUse())
1027 MIHasUse = true;
1028 else
1029 MIHasDef = true;
1030 }
1031 if (MIHasDef && !MIHasUse) {
1032 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001033 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001034 }
1035 }
Evan Chengcada2452007-11-28 01:28:46 +00001036 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001037
1038 bool IsNew = ThisVReg == 0;
1039 if (IsNew) {
1040 // This ends the previous live interval. If all of its def / use
1041 // can be folded, give it a low spill weight.
1042 if (NewVReg && TrySplit && AllCanFold) {
1043 LiveInterval &nI = getOrCreateInterval(NewVReg);
1044 nI.weight /= 10.0F;
1045 }
1046 AllCanFold = true;
1047 }
1048 NewVReg = ThisVReg;
1049
Evan Cheng81a03822007-11-17 00:40:40 +00001050 bool HasDef = false;
1051 bool HasUse = false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001052 bool CanFold = rewriteInstructionForSpills(li, TrySplit, I->valno->id,
1053 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1054 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001055 CanDelete, vrm, RegInfo, rc, ReMatIds, NewVReg,
Evan Cheng018f9b02007-12-05 03:22:34 +00001056 HasDef, HasUse, loopInfo, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001057 if (!HasDef && !HasUse)
1058 continue;
1059
Evan Cheng018f9b02007-12-05 03:22:34 +00001060 AllCanFold &= CanFold;
1061
Evan Cheng81a03822007-11-17 00:40:40 +00001062 // Update weight of spill interval.
1063 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001064 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001065 // The spill weight is now infinity as it cannot be spilled again.
1066 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001067 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001068 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001069
1070 // Keep track of the last def and first use in each MBB.
1071 unsigned MBBId = MBB->getNumber();
1072 if (HasDef) {
1073 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001074 bool HasKill = false;
1075 if (!HasUse)
1076 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1077 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001078 // If this is a two-address code, then this index starts a new VNInfo.
1079 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001080 if (VNI)
1081 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1082 }
Evan Chenge3110d02007-12-01 04:42:39 +00001083 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1084 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001085 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001086 if (SII == SpillIdxes.end()) {
1087 std::vector<SRInfo> S;
1088 S.push_back(SRInfo(index, NewVReg, true));
1089 SpillIdxes.insert(std::make_pair(MBBId, S));
1090 } else if (SII->second.back().vreg != NewVReg) {
1091 SII->second.push_back(SRInfo(index, NewVReg, true));
1092 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001093 // If there is an earlier def and this is a two-address
1094 // instruction, then it's not possible to fold the store (which
1095 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001096 SRInfo &Info = SII->second.back();
1097 Info.index = index;
1098 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001099 }
1100 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001101 } else if (SII != SpillIdxes.end() &&
1102 SII->second.back().vreg == NewVReg &&
1103 (int)index > SII->second.back().index) {
1104 // There is an earlier def that's not killed (must be two-address).
1105 // The spill is no longer needed.
1106 SII->second.pop_back();
1107 if (SII->second.empty()) {
1108 SpillIdxes.erase(MBBId);
1109 SpillMBBs.reset(MBBId);
1110 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001111 }
1112 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001113 }
1114
1115 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001116 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001117 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001118 if (SII != SpillIdxes.end() &&
1119 SII->second.back().vreg == NewVReg &&
1120 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001121 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001122 SII->second.back().canFold = false;
1123 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001124 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001125 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001126 // If we are splitting live intervals, only fold if it's the first
1127 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001128 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001129 else if (IsNew) {
1130 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001131 if (RII == RestoreIdxes.end()) {
1132 std::vector<SRInfo> Infos;
1133 Infos.push_back(SRInfo(index, NewVReg, true));
1134 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1135 } else {
1136 RII->second.push_back(SRInfo(index, NewVReg, true));
1137 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001138 RestoreMBBs.set(MBBId);
1139 }
1140 }
1141
1142 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001143 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001144 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001145 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001146
1147 if (NewVReg && TrySplit && AllCanFold) {
1148 // If all of its def / use can be folded, give it a low spill weight.
1149 LiveInterval &nI = getOrCreateInterval(NewVReg);
1150 nI.weight /= 10.0F;
1151 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001152}
1153
Evan Cheng1953d0c2007-11-29 10:12:14 +00001154bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1155 BitVector &RestoreMBBs,
1156 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1157 if (!RestoreMBBs[Id])
1158 return false;
1159 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1160 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1161 if (Restores[i].index == index &&
1162 Restores[i].vreg == vr &&
1163 Restores[i].canFold)
1164 return true;
1165 return false;
1166}
1167
1168void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1169 BitVector &RestoreMBBs,
1170 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1171 if (!RestoreMBBs[Id])
1172 return;
1173 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1174 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1175 if (Restores[i].index == index && Restores[i].vreg)
1176 Restores[i].index = -1;
1177}
Evan Cheng81a03822007-11-17 00:40:40 +00001178
1179
Evan Chengf2fbca62007-11-12 06:35:08 +00001180std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001181addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001182 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001183 // Since this is called after the analysis is done we don't know if
1184 // LiveVariables is available
1185 lv_ = getAnalysisToUpdate<LiveVariables>();
1186
1187 assert(li.weight != HUGE_VALF &&
1188 "attempt to spill already spilled interval!");
1189
1190 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001191 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001192 DOUT << '\n';
1193
Evan Cheng81a03822007-11-17 00:40:40 +00001194 // Each bit specify whether it a spill is required in the MBB.
1195 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001196 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001197 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001198 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1199 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001200 std::vector<LiveInterval*> NewLIs;
Chris Lattner84bc5422007-12-31 04:13:23 +00001201 MachineRegisterInfo &RegInfo = mf_->getRegInfo();
1202 const TargetRegisterClass* rc = RegInfo.getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001203
1204 unsigned NumValNums = li.getNumValNums();
1205 SmallVector<MachineInstr*, 4> ReMatDefs;
1206 ReMatDefs.resize(NumValNums, NULL);
1207 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1208 ReMatOrigDefs.resize(NumValNums, NULL);
1209 SmallVector<int, 4> ReMatIds;
1210 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1211 BitVector ReMatDelete(NumValNums);
1212 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1213
Evan Cheng81a03822007-11-17 00:40:40 +00001214 // Spilling a split live interval. It cannot be split any further. Also,
1215 // it's also guaranteed to be a single val# / range interval.
1216 if (vrm.getPreSplitReg(li.reg)) {
1217 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001218 // Unset the split kill marker on the last use.
1219 unsigned KillIdx = vrm.getKillPoint(li.reg);
1220 if (KillIdx) {
1221 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1222 assert(KillMI && "Last use disappeared?");
1223 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1224 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001225 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001226 }
Evan Chengadf85902007-12-05 09:51:10 +00001227 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001228 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1229 Slot = vrm.getStackSlot(li.reg);
1230 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1231 MachineInstr *ReMatDefMI = DefIsReMat ?
1232 vrm.getReMaterializedMI(li.reg) : NULL;
1233 int LdSlot = 0;
1234 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1235 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001236 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001237 bool IsFirstRange = true;
1238 for (LiveInterval::Ranges::const_iterator
1239 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1240 // If this is a split live interval with multiple ranges, it means there
1241 // are two-address instructions that re-defined the value. Only the
1242 // first def can be rematerialized!
1243 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001244 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001245 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1246 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001247 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001248 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001249 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001250 } else {
1251 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1252 Slot, 0, false, false, false,
Chris Lattner84bc5422007-12-31 04:13:23 +00001253 false, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001254 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001255 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001256 }
1257 IsFirstRange = false;
1258 }
1259 return NewLIs;
1260 }
1261
1262 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001263 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1264 TrySplit = false;
1265 if (TrySplit)
1266 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001267 bool NeedStackSlot = false;
1268 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1269 i != e; ++i) {
1270 const VNInfo *VNI = *i;
1271 unsigned VN = VNI->id;
1272 unsigned DefIdx = VNI->def;
1273 if (DefIdx == ~1U)
1274 continue; // Dead val#.
1275 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001276 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1277 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001278 bool dummy;
1279 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001280 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001281 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001282 // Original def may be modified so we have to make a copy here. vrm must
1283 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001284 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001285
1286 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001287 if (VNI->hasPHIKill) {
1288 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001289 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001290 CanDelete = false;
1291 // Need a stack slot if there is any live range where uses cannot be
1292 // rematerialized.
1293 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001294 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001295 if (CanDelete)
1296 ReMatDelete.set(VN);
1297 } else {
1298 // Need a stack slot if there is any live range where uses cannot be
1299 // rematerialized.
1300 NeedStackSlot = true;
1301 }
1302 }
1303
1304 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001305 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001306 Slot = vrm.assignVirt2StackSlot(li.reg);
1307
1308 // Create new intervals and rewrite defs and uses.
1309 for (LiveInterval::Ranges::const_iterator
1310 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001311 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1312 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1313 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001314 bool CanDelete = ReMatDelete[I->valno->id];
1315 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001316 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001317 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001318 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001319 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001320 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Chris Lattner84bc5422007-12-31 04:13:23 +00001321 CanDelete, vrm, RegInfo, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001322 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001323 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001324 }
1325
Evan Cheng0cbb1162007-11-29 01:06:25 +00001326 // Insert spills / restores if we are splitting.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001327 if (!TrySplit)
1328 return NewLIs;
1329
Evan Chengb50bb8c2007-12-05 08:16:32 +00001330 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001331 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001332 if (NeedStackSlot) {
1333 int Id = SpillMBBs.find_first();
1334 while (Id != -1) {
1335 std::vector<SRInfo> &spills = SpillIdxes[Id];
1336 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1337 int index = spills[i].index;
1338 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001339 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001340 bool isReMat = vrm.isReMaterialized(VReg);
1341 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001342 bool CanFold = false;
1343 bool FoundUse = false;
1344 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001345 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001346 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001347 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1348 MachineOperand &MO = MI->getOperand(j);
1349 if (!MO.isRegister() || MO.getReg() != VReg)
1350 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001351
1352 Ops.push_back(j);
1353 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001354 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001355 if (isReMat ||
1356 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1357 RestoreMBBs, RestoreIdxes))) {
1358 // MI has two-address uses of the same register. If the use
1359 // isn't the first and only use in the BB, then we can't fold
1360 // it. FIXME: Move this to rewriteInstructionsForSpills.
1361 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001362 break;
1363 }
Evan Chengaee4af62007-12-02 08:30:39 +00001364 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001365 }
1366 }
1367 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001368 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001369 if (CanFold && !Ops.empty()) {
1370 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001371 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001372 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001373 // Also folded uses, do not issue a load.
1374 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001375 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1376 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001377 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001378 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001379 }
1380
Evan Chengaee4af62007-12-02 08:30:39 +00001381 // Else tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001382 if (!Folded) {
1383 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1384 bool isKill = LR->end == getStoreIndex(index);
1385 vrm.addSpillPoint(VReg, isKill, MI);
1386 if (isKill)
1387 AddedKill.insert(&nI);
1388 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001389 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001390 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001391 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001392 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001393
Evan Cheng1953d0c2007-11-29 10:12:14 +00001394 int Id = RestoreMBBs.find_first();
1395 while (Id != -1) {
1396 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1397 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1398 int index = restores[i].index;
1399 if (index == -1)
1400 continue;
1401 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001402 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001403 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001404 bool CanFold = false;
1405 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001406 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001407 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001408 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1409 MachineOperand &MO = MI->getOperand(j);
1410 if (!MO.isRegister() || MO.getReg() != VReg)
1411 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001412
Evan Cheng0cbb1162007-11-29 01:06:25 +00001413 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001414 // If this restore were to be folded, it would have been folded
1415 // already.
1416 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001417 break;
1418 }
Evan Chengaee4af62007-12-02 08:30:39 +00001419 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001420 }
1421 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001422
1423 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001424 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001425 if (CanFold && !Ops.empty()) {
1426 if (!vrm.isReMaterialized(VReg))
1427 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1428 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001429 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1430 int LdSlot = 0;
1431 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1432 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001433 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001434 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1435 Ops, isLoadSS, LdSlot, VReg);
1436 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001437 }
1438 // If folding is not possible / failed, then tell the spiller to issue a
1439 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001440 if (Folded)
1441 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001442 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001443 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001444 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001445 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001446 }
1447
Evan Chengb50bb8c2007-12-05 08:16:32 +00001448 // Finalize intervals: add kills, finalize spill weights, and filter out
1449 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001450 std::vector<LiveInterval*> RetNewLIs;
1451 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1452 LiveInterval *LI = NewLIs[i];
1453 if (!LI->empty()) {
1454 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001455 if (!AddedKill.count(LI)) {
1456 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001457 unsigned LastUseIdx = getBaseIndex(LR->end);
1458 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001459 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg);
1460 assert(UseIdx != -1);
Chris Lattner749c6f62008-01-07 07:27:27 +00001461 if (LastUse->getDesc().getOperandConstraint(UseIdx, TOI::TIED_TO) ==
Chris Lattner69244302008-01-07 01:56:04 +00001462 -1) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001463 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001464 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001465 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001466 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001467 RetNewLIs.push_back(LI);
1468 }
1469 }
Evan Cheng81a03822007-11-17 00:40:40 +00001470
Evan Cheng597d10d2007-12-04 00:32:23 +00001471 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001472}