blob: 91528e297d7130c95ddc7d71505f92a2cbc1bfd3 [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
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000082/// runOnMachineFunction - Register allocate the whole function
83///
84bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 mf_ = &fn;
Evan Chengd70dbb52008-02-22 09:24:50 +000086 mri_ = &mf_->getRegInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000087 tm_ = &fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +000088 tri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000089 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 lv_ = &getAnalysis<LiveVariables>();
Dan Gohman6f0d0242008-02-10 18:45:23 +000091 allocatableRegs_ = tri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000092
Chris Lattner428b92e2006-09-15 03:57:23 +000093 // Number MachineInstrs and MachineBasicBlocks.
94 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000095 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000096
97 unsigned MIIndex = 0;
98 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
99 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000100 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000101
Chris Lattner428b92e2006-09-15 03:57:23 +0000102 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
103 I != E; ++I) {
104 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000106 i2miMap_.push_back(I);
107 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000108 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000109
110 // Set the MBB2IdxMap entry for this MBB.
111 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000112 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000113 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000114 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000115
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000117
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000118 numIntervals += getNumIntervals();
119
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000120 DOUT << "********** INTERVALS **********\n";
121 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000122 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000123 DOUT << "\n";
124 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000125
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000126 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000127 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000129}
130
Chris Lattner70ca3582004-09-30 15:59:17 +0000131/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000132void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000133 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000134 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000135 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000136 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000137 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000138
139 O << "********** MACHINEINSTRS **********\n";
140 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
141 mbbi != mbbe; ++mbbi) {
142 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
143 for (MachineBasicBlock::iterator mii = mbbi->begin(),
144 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000145 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000146 }
147 }
148}
149
Evan Chengc92da382007-11-03 07:20:12 +0000150/// conflictsWithPhysRegDef - Returns true if the specified register
151/// is defined during the duration of the specified interval.
152bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
153 VirtRegMap &vrm, unsigned reg) {
154 for (LiveInterval::Ranges::const_iterator
155 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
156 for (unsigned index = getBaseIndex(I->start),
157 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
158 index += InstrSlots::NUM) {
159 // skip deleted instructions
160 while (index != end && !getInstructionFromIndex(index))
161 index += InstrSlots::NUM;
162 if (index == end) break;
163
164 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000165 unsigned SrcReg, DstReg;
166 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
167 if (SrcReg == li.reg || DstReg == li.reg)
168 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000169 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
170 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000171 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000172 continue;
173 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000174 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000175 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000176 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000177 if (!vrm.hasPhys(PhysReg))
178 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000179 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000180 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000181 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000182 return true;
183 }
184 }
185 }
186
187 return false;
188}
189
Evan Cheng549f27d32007-08-13 23:45:17 +0000190void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000191 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000192 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000193 else
194 cerr << "%reg" << reg;
195}
196
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000197void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000198 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000199 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000200 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000201 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000202 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000203
Evan Cheng419852c2008-04-03 16:39:43 +0000204 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
205 DOUT << "is a implicit_def\n";
206 return;
207 }
208
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000209 // Virtual registers may be defined multiple times (due to phi
210 // elimination and 2-addr elimination). Much of what we do only has to be
211 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000212 // time we see a vreg.
213 if (interval.empty()) {
214 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000215 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000216 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000217 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000218 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000219 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
220 tii_->isMoveInstr(*mi, SrcReg, DstReg))
221 CopyMI = mi;
222 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000223
224 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000225
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000226 // Loop over all of the blocks that the vreg is defined in. There are
227 // two cases we have to handle here. The most common case is a vreg
228 // whose lifetime is contained within a basic block. In this case there
229 // will be a single kill, in MBB, which comes after the definition.
230 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
231 // FIXME: what about dead vars?
232 unsigned killIdx;
233 if (vi.Kills[0] != mi)
234 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
235 else
236 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000237
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000238 // If the kill happens after the definition, we have an intra-block
239 // live range.
240 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000241 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000243 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000244 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000245 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000246 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000247 return;
248 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000249 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000250
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000251 // The other case we handle is when a virtual register lives to the end
252 // of the defining block, potentially live across some blocks, then is
253 // live into some number of blocks, but gets killed. Start by adding a
254 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000255 LiveRange NewLR(defIndex,
256 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000257 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000258 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259 interval.addRange(NewLR);
260
261 // Iterate over all of the blocks that the variable is completely
262 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
263 // live interval.
264 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
265 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000266 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
267 if (!MBB->empty()) {
268 LiveRange LR(getMBBStartIdx(i),
269 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000270 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000272 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000273 }
274 }
275 }
276
277 // Finally, this virtual register is live from the start of any killing
278 // block to the 'use' slot of the killing instruction.
279 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
280 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000281 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000282 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000283 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000284 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000285 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000286 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 }
288
289 } else {
290 // If this is the second time we see a virtual register definition, it
291 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000292 // the result of two address elimination, then the vreg is one of the
293 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000294 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 // If this is a two-address definition, then we have already processed
296 // the live range. The only problem is that we didn't realize there
297 // are actually two values in the live interval. Because of this we
298 // need to take the LiveRegion that defines this register and split it
299 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000300 assert(interval.containsOneValue());
301 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000302 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000303
Evan Cheng4f8ff162007-08-11 00:59:19 +0000304 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000305 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000306
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000308 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000310
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000311 // Two-address vregs should always only be redefined once. This means
312 // that at this point, there should be exactly one value number in it.
313 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
314
Chris Lattner91725b72006-08-31 05:54:43 +0000315 // The new value number (#1) is defined by the instruction we claimed
316 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000317 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
318 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000319
Chris Lattner91725b72006-08-31 05:54:43 +0000320 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000321 OldValNo->def = RedefIndex;
322 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000323
324 // Add the new live interval which replaces the range for the input copy.
325 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000326 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000327 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000328 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000329
330 // If this redefinition is dead, we need to add a dummy unit live
331 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000332 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000333 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000334
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000335 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000336 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337
338 } else {
339 // Otherwise, this must be because of phi elimination. If this is the
340 // first redefinition of the vreg that we have seen, go back and change
341 // the live range in the PHI block to be a different value number.
342 if (interval.containsOneValue()) {
343 assert(vi.Kills.size() == 1 &&
344 "PHI elimination vreg should have one kill, the PHI itself!");
345
346 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000347 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000348 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000349 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000351 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000352 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000353 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000354 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000355 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000356
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000357 // Replace the interval with one of a NEW value number. Note that this
358 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000359 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000360 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000362 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000363 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000364 }
365
366 // In the case of PHI elimination, each variable definition is only
367 // live until the end of the block. We've already taken care of the
368 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000369 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000370
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000371 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000372 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000373 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000374 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
375 tii_->isMoveInstr(*mi, SrcReg, DstReg))
376 CopyMI = mi;
377 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000378
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000379 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000380 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000382 interval.addKill(ValNo, killIndex);
383 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000384 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 }
386 }
387
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000388 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000389}
390
Chris Lattnerf35fef72004-07-23 21:24:19 +0000391void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000392 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000393 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000394 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000395 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000396 // A physical register cannot be live across basic block, so its
397 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000398 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000399
Chris Lattner6b128bd2006-09-03 08:07:11 +0000400 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 unsigned start = getDefIndex(baseIndex);
402 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000403
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000404 // If it is not used after definition, it is considered dead at
405 // the instruction defining it. Hence its interval is:
406 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000407 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000408 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000409 end = getDefIndex(start) + 1;
410 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000411 }
412
413 // If it is not dead on definition, it must be killed by a
414 // subsequent instruction. Hence its interval is:
415 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000416 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000417 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000418 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000419 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000420 end = getUseIndex(baseIndex) + 1;
421 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000422 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000423 // Another instruction redefines the register before it is ever read.
424 // Then the register is essentially dead at the instruction that defines
425 // it. Hence its interval is:
426 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000427 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000428 end = getDefIndex(start) + 1;
429 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000430 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000431 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000432
433 // The only case we should have a dead physreg here without a killing or
434 // instruction where we know it's dead is if it is live-in to the function
435 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000436 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000437 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000438
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000439exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000441
Evan Cheng24a3cc42007-04-25 07:30:23 +0000442 // Already exists? Extend old live interval.
443 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000444 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000445 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000446 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000447 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000448 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000449 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000450}
451
Chris Lattnerf35fef72004-07-23 21:24:19 +0000452void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
453 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000454 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000455 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000456 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000457 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000458 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000459 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000460 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000461 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
462 tii_->isMoveInstr(*MI, SrcReg, DstReg))
463 CopyMI = MI;
464 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000465 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000466 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000467 // If MI also modifies the sub-register explicitly, avoid processing it
468 // more than once. Do not pass in TRI here so it checks for exact match.
469 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000470 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000471 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000472}
473
Evan Chengb371f452007-02-19 21:49:54 +0000474void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000475 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000476 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000477 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
478
479 // Look for kills, if it reaches a def before it's killed, then it shouldn't
480 // be considered a livein.
481 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000482 unsigned baseIndex = MIIdx;
483 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000484 unsigned end = start;
485 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000486 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000487 DOUT << " killed";
488 end = getUseIndex(baseIndex) + 1;
489 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000490 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000491 // Another instruction redefines the register before it is ever read.
492 // Then the register is essentially dead at the instruction that defines
493 // it. Hence its interval is:
494 // [defSlot(def), defSlot(def)+1)
495 DOUT << " dead";
496 end = getDefIndex(start) + 1;
497 goto exit;
498 }
499
500 baseIndex += InstrSlots::NUM;
501 ++mi;
502 }
503
504exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000505 // Live-in register might not be used at all.
506 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000507 if (isAlias) {
508 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000509 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000510 } else {
511 DOUT << " live through";
512 end = baseIndex;
513 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000514 }
515
Evan Chengf3bb2e62007-09-05 21:46:51 +0000516 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000517 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000518 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000519 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000520}
521
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000522/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000523/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000524/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000525/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000526void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000527 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
528 << "********** Function: "
529 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000530 // Track the index of the current machine instr.
531 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000532 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
533 MBBI != E; ++MBBI) {
534 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000535 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000536
Chris Lattner428b92e2006-09-15 03:57:23 +0000537 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000538
Dan Gohmancb406c22007-10-03 19:26:29 +0000539 // Create intervals for live-ins to this BB first.
540 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
541 LE = MBB->livein_end(); LI != LE; ++LI) {
542 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
543 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000544 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000545 if (!hasInterval(*AS))
546 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
547 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000548 }
549
Chris Lattner428b92e2006-09-15 03:57:23 +0000550 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000551 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000552
Evan Cheng438f7bc2006-11-10 08:43:01 +0000553 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000554 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
555 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000556 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000557 if (MO.isRegister() && MO.getReg() && MO.isDef())
558 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000559 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000560
561 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000562 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000563 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000564}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000565
Evan Cheng4ca980e2007-10-17 02:10:22 +0000566bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000567 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000568 std::vector<IdxMBBPair>::const_iterator I =
569 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
570
571 bool ResVal = false;
572 while (I != Idx2MBBMap.end()) {
573 if (LR.end <= I->first)
574 break;
575 MBBs.push_back(I->second);
576 ResVal = true;
577 ++I;
578 }
579 return ResVal;
580}
581
582
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000583LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000584 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000585 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000586 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000587}
Evan Chengf2fbca62007-11-12 06:35:08 +0000588
Evan Chengc8d044e2008-02-15 18:24:29 +0000589/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
590/// copy field and returns the source register that defines it.
591unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
592 if (!VNI->copy)
593 return 0;
594
595 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
596 return VNI->copy->getOperand(1).getReg();
597 unsigned SrcReg, DstReg;
598 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
599 return SrcReg;
600 assert(0 && "Unrecognized copy instruction!");
601 return 0;
602}
Evan Chengf2fbca62007-11-12 06:35:08 +0000603
604//===----------------------------------------------------------------------===//
605// Register allocator hooks.
606//
607
Evan Chengd70dbb52008-02-22 09:24:50 +0000608/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
609/// allow one) virtual register operand, then its uses are implicitly using
610/// the register. Returns the virtual register.
611unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
612 MachineInstr *MI) const {
613 unsigned RegOp = 0;
614 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
615 MachineOperand &MO = MI->getOperand(i);
616 if (!MO.isRegister() || !MO.isUse())
617 continue;
618 unsigned Reg = MO.getReg();
619 if (Reg == 0 || Reg == li.reg)
620 continue;
621 // FIXME: For now, only remat MI with at most one register operand.
622 assert(!RegOp &&
623 "Can't rematerialize instruction with multiple register operand!");
624 RegOp = MO.getReg();
625 break;
626 }
627 return RegOp;
628}
629
630/// isValNoAvailableAt - Return true if the val# of the specified interval
631/// which reaches the given instruction also reaches the specified use index.
632bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
633 unsigned UseIdx) const {
634 unsigned Index = getInstructionIndex(MI);
635 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
636 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
637 return UI != li.end() && UI->valno == ValNo;
638}
639
Evan Chengf2fbca62007-11-12 06:35:08 +0000640/// isReMaterializable - Returns true if the definition MI of the specified
641/// val# of the specified interval is re-materializable.
642bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000643 const VNInfo *ValNo, MachineInstr *MI,
644 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000645 if (DisableReMat)
646 return false;
647
Evan Cheng5ef3a042007-12-06 00:01:56 +0000648 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000649 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000650 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000651
652 int FrameIdx = 0;
653 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000654 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000655 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
656 // this but remember this is not safe to fold into a two-address
657 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000658 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000659 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000660
Evan Chengd70dbb52008-02-22 09:24:50 +0000661 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000662 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000663 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000664
665 unsigned ImpUse = getReMatImplicitUse(li, MI);
666 if (ImpUse) {
667 const LiveInterval &ImpLi = getInterval(ImpUse);
668 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
669 re = mri_->use_end(); ri != re; ++ri) {
670 MachineInstr *UseMI = &*ri;
671 unsigned UseIdx = getInstructionIndex(UseMI);
672 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
673 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000674 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000675 return false;
676 }
677 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000678 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000679 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000680
Evan Chengdd3465e2008-02-23 01:44:27 +0000681 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000682}
683
684/// isReMaterializable - Returns true if every definition of MI of every
685/// val# of the specified interval is re-materializable.
686bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
687 isLoad = false;
688 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
689 i != e; ++i) {
690 const VNInfo *VNI = *i;
691 unsigned DefIdx = VNI->def;
692 if (DefIdx == ~1U)
693 continue; // Dead val#.
694 // Is the def for the val# rematerializable?
695 if (DefIdx == ~0u)
696 return false;
697 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
698 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000699 if (!ReMatDefMI ||
700 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000701 return false;
702 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000703 }
704 return true;
705}
706
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000707/// FilterFoldedOps - Filter out two-address use operands. Return
708/// true if it finds any issue with the operands that ought to prevent
709/// folding.
710static bool FilterFoldedOps(MachineInstr *MI,
711 SmallVector<unsigned, 2> &Ops,
712 unsigned &MRInfo,
713 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000714 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000715
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000716 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000717 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
718 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000719 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000720 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000721 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000722 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000723 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000724 MRInfo |= (unsigned)VirtRegMap::isMod;
725 else {
726 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000727 if (!MO.isImplicit() &&
728 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000729 MRInfo = VirtRegMap::isModRef;
730 continue;
731 }
732 MRInfo |= (unsigned)VirtRegMap::isRef;
733 }
734 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000735 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000736 return false;
737}
738
739
740/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
741/// slot / to reg or any rematerialized load into ith operand of specified
742/// MI. If it is successul, MI is updated with the newly created MI and
743/// returns true.
744bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
745 VirtRegMap &vrm, MachineInstr *DefMI,
746 unsigned InstrIdx,
747 SmallVector<unsigned, 2> &Ops,
748 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000749 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000750 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000751 RemoveMachineInstrFromMaps(MI);
752 vrm.RemoveMachineInstrFromMaps(MI);
753 MI->eraseFromParent();
754 ++numFolds;
755 return true;
756 }
757
758 // Filter the list of operand indexes that are to be folded. Abort if
759 // any operand will prevent folding.
760 unsigned MRInfo = 0;
761 SmallVector<unsigned, 2> FoldOps;
762 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
763 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000764
Evan Cheng427f4c12008-03-31 23:19:51 +0000765 // The only time it's safe to fold into a two address instruction is when
766 // it's folding reload and spill from / into a spill stack slot.
767 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000768 return false;
769
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000770 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
771 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000772 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000773 // Remember this instruction uses the spill slot.
774 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
775
Evan Chengf2fbca62007-11-12 06:35:08 +0000776 // Attempt to fold the memory reference into the instruction. If
777 // we can do this, we don't need to insert spill code.
778 if (lv_)
779 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000780 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000781 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000782 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000783 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000784 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000785 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000786 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000787 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000788 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000789 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
790 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000791 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000792 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000793 return true;
794 }
795 return false;
796}
797
Evan Cheng018f9b02007-12-05 03:22:34 +0000798/// canFoldMemoryOperand - Returns true if the specified load / store
799/// folding is possible.
800bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000801 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000802 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000803 // Filter the list of operand indexes that are to be folded. Abort if
804 // any operand will prevent folding.
805 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000806 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000807 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
808 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000809
Evan Cheng3c75ba82008-04-01 21:37:32 +0000810 // It's only legal to remat for a use, not a def.
811 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000812 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000813
Evan Chengd70dbb52008-02-22 09:24:50 +0000814 return tii_->canFoldMemoryOperand(MI, FoldOps);
815}
816
Evan Cheng81a03822007-11-17 00:40:40 +0000817bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
818 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
819 for (LiveInterval::Ranges::const_iterator
820 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
821 std::vector<IdxMBBPair>::const_iterator II =
822 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
823 if (II == Idx2MBBMap.end())
824 continue;
825 if (I->end > II->first) // crossing a MBB.
826 return false;
827 MBBs.insert(II->second);
828 if (MBBs.size() > 1)
829 return false;
830 }
831 return true;
832}
833
Evan Chengd70dbb52008-02-22 09:24:50 +0000834/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
835/// interval on to-be re-materialized operands of MI) with new register.
836void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
837 MachineInstr *MI, unsigned NewVReg,
838 VirtRegMap &vrm) {
839 // There is an implicit use. That means one of the other operand is
840 // being remat'ed and the remat'ed instruction has li.reg as an
841 // use operand. Make sure we rewrite that as well.
842 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
843 MachineOperand &MO = MI->getOperand(i);
844 if (!MO.isRegister())
845 continue;
846 unsigned Reg = MO.getReg();
847 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
848 continue;
849 if (!vrm.isReMaterialized(Reg))
850 continue;
851 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000852 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
853 if (UseMO)
854 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000855 }
856}
857
Evan Chengf2fbca62007-11-12 06:35:08 +0000858/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
859/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000860bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000861rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
862 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000863 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000864 unsigned Slot, int LdSlot,
865 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000866 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000867 const TargetRegisterClass* rc,
868 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000869 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000870 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000871 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000872 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000873 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000874 RestartInstruction:
875 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
876 MachineOperand& mop = MI->getOperand(i);
877 if (!mop.isRegister())
878 continue;
879 unsigned Reg = mop.getReg();
880 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000881 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000882 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000883 if (Reg != li.reg)
884 continue;
885
886 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000887 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000888 int FoldSlot = Slot;
889 if (DefIsReMat) {
890 // If this is the rematerializable definition MI itself and
891 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000892 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000893 DOUT << "\t\t\t\tErasing re-materlizable def: ";
894 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000895 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000896 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000897 MI->eraseFromParent();
898 break;
899 }
900
901 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000902 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000903 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000904 if (isLoad) {
905 // Try fold loads (from stack slot, constant pool, etc.) into uses.
906 FoldSS = isLoadSS;
907 FoldSlot = LdSlot;
908 }
909 }
910
Evan Chengf2fbca62007-11-12 06:35:08 +0000911 // Scan all of the operands of this instruction rewriting operands
912 // to use NewVReg instead of li.reg as appropriate. We do this for
913 // two reasons:
914 //
915 // 1. If the instr reads the same spilled vreg multiple times, we
916 // want to reuse the NewVReg.
917 // 2. If the instr is a two-addr instruction, we are required to
918 // keep the src/dst regs pinned.
919 //
920 // Keep track of whether we replace a use and/or def so that we can
921 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000922
Evan Cheng81a03822007-11-17 00:40:40 +0000923 HasUse = mop.isUse();
924 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000925 SmallVector<unsigned, 2> Ops;
926 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000927 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000928 const MachineOperand &MOj = MI->getOperand(j);
929 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000930 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000931 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000932 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000933 continue;
934 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000935 Ops.push_back(j);
936 HasUse |= MOj.isUse();
937 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000938 }
939 }
940
Evan Cheng018f9b02007-12-05 03:22:34 +0000941 if (TryFold) {
942 // Do not fold load / store here if we are splitting. We'll find an
943 // optimal point to insert a load / store later.
944 if (!TrySplit) {
945 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
946 Ops, FoldSS, FoldSlot, Reg)) {
947 // Folding the load/store can completely change the instruction in
948 // unpredictable ways, rescan it from the beginning.
949 HasUse = false;
950 HasDef = false;
951 CanFold = false;
952 goto RestartInstruction;
953 }
954 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000955 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000956 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000957 } else
958 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000959
960 // Create a new virtual register for the spill interval.
961 bool CreatedNewVReg = false;
962 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +0000963 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000964 vrm.grow();
965 CreatedNewVReg = true;
966 }
967 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000968 if (mop.isImplicit())
969 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +0000970
971 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +0000972 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
973 MachineOperand &mopj = MI->getOperand(Ops[j]);
974 mopj.setReg(NewVReg);
975 if (mopj.isImplicit())
976 rewriteImplicitOps(li, MI, NewVReg, vrm);
977 }
Evan Chengcddbb832007-11-30 21:23:43 +0000978
Evan Cheng81a03822007-11-17 00:40:40 +0000979 if (CreatedNewVReg) {
980 if (DefIsReMat) {
981 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +0000982 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +0000983 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +0000984 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000985 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +0000986 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +0000987 }
988 if (!CanDelete || (HasUse && HasDef)) {
989 // If this is a two-addr instruction then its use operands are
990 // rematerializable but its def is not. It should be assigned a
991 // stack slot.
992 vrm.assignVirt2StackSlot(NewVReg, Slot);
993 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000994 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +0000995 vrm.assignVirt2StackSlot(NewVReg, Slot);
996 }
Evan Chengcb3c3302007-11-29 23:02:50 +0000997 } else if (HasUse && HasDef &&
998 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
999 // If this interval hasn't been assigned a stack slot (because earlier
1000 // def is a deleted remat def), do it now.
1001 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1002 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001003 }
1004
Evan Cheng313d4b82008-02-23 00:33:04 +00001005 // Re-matting an instruction with virtual register use. Add the
1006 // register as an implicit use on the use MI.
1007 if (DefIsReMat && ImpUse)
1008 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1009
Evan Chengf2fbca62007-11-12 06:35:08 +00001010 // create a new register interval for this spill / remat.
1011 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001012 if (CreatedNewVReg) {
1013 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001014 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001015 if (TrySplit)
1016 vrm.setIsSplitFromReg(NewVReg, li.reg);
1017 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001018
1019 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001020 if (CreatedNewVReg) {
1021 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1022 nI.getNextValue(~0U, 0, VNInfoAllocator));
1023 DOUT << " +" << LR;
1024 nI.addRange(LR);
1025 } else {
1026 // Extend the split live interval to this def / use.
1027 unsigned End = getUseIndex(index)+1;
1028 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1029 nI.getValNumInfo(nI.getNumValNums()-1));
1030 DOUT << " +" << LR;
1031 nI.addRange(LR);
1032 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001033 }
1034 if (HasDef) {
1035 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1036 nI.getNextValue(~0U, 0, VNInfoAllocator));
1037 DOUT << " +" << LR;
1038 nI.addRange(LR);
1039 }
Evan Cheng81a03822007-11-17 00:40:40 +00001040
Evan Chengf2fbca62007-11-12 06:35:08 +00001041 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001042 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001043 DOUT << '\n';
1044 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001045 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001046}
Evan Cheng81a03822007-11-17 00:40:40 +00001047bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001048 const VNInfo *VNI,
1049 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001050 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001051 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1052 unsigned KillIdx = VNI->kills[j];
1053 if (KillIdx > Idx && KillIdx < End)
1054 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001055 }
1056 return false;
1057}
1058
Evan Cheng1953d0c2007-11-29 10:12:14 +00001059static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1060 const VNInfo *VNI = NULL;
1061 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1062 e = li.vni_end(); i != e; ++i)
1063 if ((*i)->def == DefIdx) {
1064 VNI = *i;
1065 break;
1066 }
1067 return VNI;
1068}
1069
Evan Cheng063284c2008-02-21 00:34:19 +00001070/// RewriteInfo - Keep track of machine instrs that will be rewritten
1071/// during spilling.
1072struct RewriteInfo {
1073 unsigned Index;
1074 MachineInstr *MI;
1075 bool HasUse;
1076 bool HasDef;
1077 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1078 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1079};
1080
1081struct RewriteInfoCompare {
1082 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1083 return LHS.Index < RHS.Index;
1084 }
1085};
1086
Evan Chengf2fbca62007-11-12 06:35:08 +00001087void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001088rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001089 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001090 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001091 unsigned Slot, int LdSlot,
1092 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001093 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001094 const TargetRegisterClass* rc,
1095 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001096 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001097 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001098 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001099 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001100 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1101 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001102 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001103 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001104 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001105 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001106 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001107
Evan Cheng063284c2008-02-21 00:34:19 +00001108 // First collect all the def / use in this live range that will be rewritten.
1109 // Make sure they are sorted according instruction index.
1110 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001111 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1112 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001113 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001114 MachineOperand &O = ri.getOperand();
1115 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001116 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001117 unsigned index = getInstructionIndex(MI);
1118 if (index < start || index >= end)
1119 continue;
1120 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1121 }
1122 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1123
Evan Cheng313d4b82008-02-23 00:33:04 +00001124 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001125 // Now rewrite the defs and uses.
1126 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1127 RewriteInfo &rwi = RewriteMIs[i];
1128 ++i;
1129 unsigned index = rwi.Index;
1130 bool MIHasUse = rwi.HasUse;
1131 bool MIHasDef = rwi.HasDef;
1132 MachineInstr *MI = rwi.MI;
1133 // If MI def and/or use the same register multiple times, then there
1134 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001135 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001136 while (i != e && RewriteMIs[i].MI == MI) {
1137 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001138 bool isUse = RewriteMIs[i].HasUse;
1139 if (isUse) ++NumUses;
1140 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001141 MIHasDef |= RewriteMIs[i].HasDef;
1142 ++i;
1143 }
Evan Cheng81a03822007-11-17 00:40:40 +00001144 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001145
1146 if (ImpUse && MI != ReMatDefMI) {
1147 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001148 // register interval's spill weight to HUGE_VALF to prevent it from
1149 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001150 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001151 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001152 }
1153
Evan Cheng063284c2008-02-21 00:34:19 +00001154 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001155 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001156 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001157 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001158 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001159 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001160 // One common case:
1161 // x = use
1162 // ...
1163 // ...
1164 // def = ...
1165 // = use
1166 // It's better to start a new interval to avoid artifically
1167 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001168 if (MIHasDef && !MIHasUse) {
1169 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001170 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001171 }
1172 }
Evan Chengcada2452007-11-28 01:28:46 +00001173 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001174
1175 bool IsNew = ThisVReg == 0;
1176 if (IsNew) {
1177 // This ends the previous live interval. If all of its def / use
1178 // can be folded, give it a low spill weight.
1179 if (NewVReg && TrySplit && AllCanFold) {
1180 LiveInterval &nI = getOrCreateInterval(NewVReg);
1181 nI.weight /= 10.0F;
1182 }
1183 AllCanFold = true;
1184 }
1185 NewVReg = ThisVReg;
1186
Evan Cheng81a03822007-11-17 00:40:40 +00001187 bool HasDef = false;
1188 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001189 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001190 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1191 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001192 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001193 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001194 if (!HasDef && !HasUse)
1195 continue;
1196
Evan Cheng018f9b02007-12-05 03:22:34 +00001197 AllCanFold &= CanFold;
1198
Evan Cheng81a03822007-11-17 00:40:40 +00001199 // Update weight of spill interval.
1200 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001201 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001202 // The spill weight is now infinity as it cannot be spilled again.
1203 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001204 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001205 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001206
1207 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001208 if (HasDef) {
1209 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001210 bool HasKill = false;
1211 if (!HasUse)
1212 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1213 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001214 // If this is a two-address code, then this index starts a new VNInfo.
1215 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001216 if (VNI)
1217 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1218 }
Evan Chenge3110d02007-12-01 04:42:39 +00001219 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1220 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001221 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001222 if (SII == SpillIdxes.end()) {
1223 std::vector<SRInfo> S;
1224 S.push_back(SRInfo(index, NewVReg, true));
1225 SpillIdxes.insert(std::make_pair(MBBId, S));
1226 } else if (SII->second.back().vreg != NewVReg) {
1227 SII->second.push_back(SRInfo(index, NewVReg, true));
1228 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001229 // If there is an earlier def and this is a two-address
1230 // instruction, then it's not possible to fold the store (which
1231 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001232 SRInfo &Info = SII->second.back();
1233 Info.index = index;
1234 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001235 }
1236 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001237 } else if (SII != SpillIdxes.end() &&
1238 SII->second.back().vreg == NewVReg &&
1239 (int)index > SII->second.back().index) {
1240 // There is an earlier def that's not killed (must be two-address).
1241 // The spill is no longer needed.
1242 SII->second.pop_back();
1243 if (SII->second.empty()) {
1244 SpillIdxes.erase(MBBId);
1245 SpillMBBs.reset(MBBId);
1246 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001247 }
1248 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001249 }
1250
1251 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001252 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001253 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001254 if (SII != SpillIdxes.end() &&
1255 SII->second.back().vreg == NewVReg &&
1256 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001257 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001258 SII->second.back().canFold = false;
1259 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001260 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001261 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001262 // If we are splitting live intervals, only fold if it's the first
1263 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001264 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001265 else if (IsNew) {
1266 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001267 if (RII == RestoreIdxes.end()) {
1268 std::vector<SRInfo> Infos;
1269 Infos.push_back(SRInfo(index, NewVReg, true));
1270 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1271 } else {
1272 RII->second.push_back(SRInfo(index, NewVReg, true));
1273 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001274 RestoreMBBs.set(MBBId);
1275 }
1276 }
1277
1278 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001279 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001280 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001281 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001282
1283 if (NewVReg && TrySplit && AllCanFold) {
1284 // If all of its def / use can be folded, give it a low spill weight.
1285 LiveInterval &nI = getOrCreateInterval(NewVReg);
1286 nI.weight /= 10.0F;
1287 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001288}
1289
Evan Cheng1953d0c2007-11-29 10:12:14 +00001290bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1291 BitVector &RestoreMBBs,
1292 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1293 if (!RestoreMBBs[Id])
1294 return false;
1295 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1296 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1297 if (Restores[i].index == index &&
1298 Restores[i].vreg == vr &&
1299 Restores[i].canFold)
1300 return true;
1301 return false;
1302}
1303
1304void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1305 BitVector &RestoreMBBs,
1306 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1307 if (!RestoreMBBs[Id])
1308 return;
1309 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1310 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1311 if (Restores[i].index == index && Restores[i].vreg)
1312 Restores[i].index = -1;
1313}
Evan Cheng81a03822007-11-17 00:40:40 +00001314
Evan Cheng419852c2008-04-03 16:39:43 +00001315/// removeSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1316/// spilled.
1317void LiveIntervals::removeSpilledImpDefs(const LiveInterval &li,
1318 VirtRegMap &vrm) {
1319 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1320 re = mri_->reg_end(); ri != re; ) {
1321 MachineInstr *MI = &*ri;
1322 ++ri;
1323 if (MI->getOpcode() != TargetInstrInfo::IMPLICIT_DEF)
1324 continue;
1325 RemoveMachineInstrFromMaps(MI);
1326 vrm.RemoveMachineInstrFromMaps(MI);
1327 MI->eraseFromParent();
1328 }
1329}
1330
Evan Cheng81a03822007-11-17 00:40:40 +00001331
Evan Chengf2fbca62007-11-12 06:35:08 +00001332std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001333addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001334 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001335 // Since this is called after the analysis is done we don't know if
1336 // LiveVariables is available
1337 lv_ = getAnalysisToUpdate<LiveVariables>();
1338
1339 assert(li.weight != HUGE_VALF &&
1340 "attempt to spill already spilled interval!");
1341
1342 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001343 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001344 DOUT << '\n';
1345
Evan Cheng81a03822007-11-17 00:40:40 +00001346 // Each bit specify whether it a spill is required in the MBB.
1347 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001348 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001349 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001350 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1351 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001352 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001353 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001354
1355 unsigned NumValNums = li.getNumValNums();
1356 SmallVector<MachineInstr*, 4> ReMatDefs;
1357 ReMatDefs.resize(NumValNums, NULL);
1358 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1359 ReMatOrigDefs.resize(NumValNums, NULL);
1360 SmallVector<int, 4> ReMatIds;
1361 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1362 BitVector ReMatDelete(NumValNums);
1363 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1364
Evan Cheng81a03822007-11-17 00:40:40 +00001365 // Spilling a split live interval. It cannot be split any further. Also,
1366 // it's also guaranteed to be a single val# / range interval.
1367 if (vrm.getPreSplitReg(li.reg)) {
1368 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001369 // Unset the split kill marker on the last use.
1370 unsigned KillIdx = vrm.getKillPoint(li.reg);
1371 if (KillIdx) {
1372 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1373 assert(KillMI && "Last use disappeared?");
1374 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1375 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001376 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001377 }
Evan Chengadf85902007-12-05 09:51:10 +00001378 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001379 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1380 Slot = vrm.getStackSlot(li.reg);
1381 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1382 MachineInstr *ReMatDefMI = DefIsReMat ?
1383 vrm.getReMaterializedMI(li.reg) : NULL;
1384 int LdSlot = 0;
1385 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1386 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001387 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001388 bool IsFirstRange = true;
1389 for (LiveInterval::Ranges::const_iterator
1390 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1391 // If this is a split live interval with multiple ranges, it means there
1392 // are two-address instructions that re-defined the value. Only the
1393 // first def can be rematerialized!
1394 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001395 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001396 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1397 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001398 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001399 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001400 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001401 } else {
1402 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1403 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001404 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001405 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001406 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001407 }
1408 IsFirstRange = false;
1409 }
Evan Cheng419852c2008-04-03 16:39:43 +00001410
1411 removeSpilledImpDefs(li, vrm);
Evan Cheng81a03822007-11-17 00:40:40 +00001412 return NewLIs;
1413 }
1414
1415 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001416 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1417 TrySplit = false;
1418 if (TrySplit)
1419 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001420 bool NeedStackSlot = false;
1421 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1422 i != e; ++i) {
1423 const VNInfo *VNI = *i;
1424 unsigned VN = VNI->id;
1425 unsigned DefIdx = VNI->def;
1426 if (DefIdx == ~1U)
1427 continue; // Dead val#.
1428 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001429 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1430 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001431 bool dummy;
1432 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001433 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001434 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001435 // Original def may be modified so we have to make a copy here. vrm must
1436 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001437 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001438
1439 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001440 if (VNI->hasPHIKill) {
1441 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001442 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001443 CanDelete = false;
1444 // Need a stack slot if there is any live range where uses cannot be
1445 // rematerialized.
1446 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001447 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001448 if (CanDelete)
1449 ReMatDelete.set(VN);
1450 } else {
1451 // Need a stack slot if there is any live range where uses cannot be
1452 // rematerialized.
1453 NeedStackSlot = true;
1454 }
1455 }
1456
1457 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001458 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001459 Slot = vrm.assignVirt2StackSlot(li.reg);
1460
1461 // Create new intervals and rewrite defs and uses.
1462 for (LiveInterval::Ranges::const_iterator
1463 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001464 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1465 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1466 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001467 bool CanDelete = ReMatDelete[I->valno->id];
1468 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001469 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001470 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001471 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001472 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001473 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001474 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001475 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001476 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001477 }
1478
Evan Cheng0cbb1162007-11-29 01:06:25 +00001479 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001480 if (!TrySplit) {
1481 removeSpilledImpDefs(li, vrm);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001482 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001483 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001484
Evan Chengb50bb8c2007-12-05 08:16:32 +00001485 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001486 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001487 if (NeedStackSlot) {
1488 int Id = SpillMBBs.find_first();
1489 while (Id != -1) {
1490 std::vector<SRInfo> &spills = SpillIdxes[Id];
1491 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1492 int index = spills[i].index;
1493 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001494 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001495 bool isReMat = vrm.isReMaterialized(VReg);
1496 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001497 bool CanFold = false;
1498 bool FoundUse = false;
1499 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001500 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001501 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001502 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1503 MachineOperand &MO = MI->getOperand(j);
1504 if (!MO.isRegister() || MO.getReg() != VReg)
1505 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001506
1507 Ops.push_back(j);
1508 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001509 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001510 if (isReMat ||
1511 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1512 RestoreMBBs, RestoreIdxes))) {
1513 // MI has two-address uses of the same register. If the use
1514 // isn't the first and only use in the BB, then we can't fold
1515 // it. FIXME: Move this to rewriteInstructionsForSpills.
1516 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001517 break;
1518 }
Evan Chengaee4af62007-12-02 08:30:39 +00001519 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001520 }
1521 }
1522 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001523 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001524 if (CanFold && !Ops.empty()) {
1525 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001526 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001527 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001528 // Also folded uses, do not issue a load.
1529 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001530 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1531 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001532 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001533 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001534 }
1535
Evan Chengaee4af62007-12-02 08:30:39 +00001536 // Else tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001537 if (!Folded) {
1538 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1539 bool isKill = LR->end == getStoreIndex(index);
1540 vrm.addSpillPoint(VReg, isKill, MI);
1541 if (isKill)
1542 AddedKill.insert(&nI);
1543 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001544 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001545 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001546 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001547 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001548
Evan Cheng1953d0c2007-11-29 10:12:14 +00001549 int Id = RestoreMBBs.find_first();
1550 while (Id != -1) {
1551 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1552 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1553 int index = restores[i].index;
1554 if (index == -1)
1555 continue;
1556 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001557 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001558 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001559 bool CanFold = false;
1560 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001561 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001562 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001563 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1564 MachineOperand &MO = MI->getOperand(j);
1565 if (!MO.isRegister() || MO.getReg() != VReg)
1566 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001567
Evan Cheng0cbb1162007-11-29 01:06:25 +00001568 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001569 // If this restore were to be folded, it would have been folded
1570 // already.
1571 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001572 break;
1573 }
Evan Chengaee4af62007-12-02 08:30:39 +00001574 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001575 }
1576 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001577
1578 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001579 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001580 if (CanFold && !Ops.empty()) {
1581 if (!vrm.isReMaterialized(VReg))
1582 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1583 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001584 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1585 int LdSlot = 0;
1586 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1587 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001588 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001589 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1590 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001591 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1592 if (ImpUse) {
1593 // Re-matting an instruction with virtual register use. Add the
1594 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001595 // interval's spill weight to HUGE_VALF to prevent it from being
1596 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001597 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001598 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001599 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1600 }
Evan Chengaee4af62007-12-02 08:30:39 +00001601 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001602 }
1603 // If folding is not possible / failed, then tell the spiller to issue a
1604 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001605 if (Folded)
1606 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001607 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001608 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001609 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001610 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001611 }
1612
Evan Chengb50bb8c2007-12-05 08:16:32 +00001613 // Finalize intervals: add kills, finalize spill weights, and filter out
1614 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001615 std::vector<LiveInterval*> RetNewLIs;
1616 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1617 LiveInterval *LI = NewLIs[i];
1618 if (!LI->empty()) {
1619 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001620 if (!AddedKill.count(LI)) {
1621 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001622 unsigned LastUseIdx = getBaseIndex(LR->end);
1623 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001624 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001625 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001626 if (LastUse->getOperand(UseIdx).isImplicit() ||
1627 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001628 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001629 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001630 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001631 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001632 RetNewLIs.push_back(LI);
1633 }
1634 }
Evan Cheng81a03822007-11-17 00:40:40 +00001635
Evan Cheng419852c2008-04-03 16:39:43 +00001636 removeSpilledImpDefs(li, vrm);
Evan Cheng597d10d2007-12-04 00:32:23 +00001637 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001638}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001639
1640/// hasAllocatableSuperReg - Return true if the specified physical register has
1641/// any super register that's allocatable.
1642bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1643 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1644 if (allocatableRegs_[*AS] && hasInterval(*AS))
1645 return true;
1646 return false;
1647}
1648
1649/// getRepresentativeReg - Find the largest super register of the specified
1650/// physical register.
1651unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1652 // Find the largest super-register that is allocatable.
1653 unsigned BestReg = Reg;
1654 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1655 unsigned SuperReg = *AS;
1656 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1657 BestReg = SuperReg;
1658 break;
1659 }
1660 }
1661 return BestReg;
1662}
1663
1664/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1665/// specified interval that conflicts with the specified physical register.
1666unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1667 unsigned PhysReg) const {
1668 unsigned NumConflicts = 0;
1669 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1670 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1671 E = mri_->reg_end(); I != E; ++I) {
1672 MachineOperand &O = I.getOperand();
1673 MachineInstr *MI = O.getParent();
1674 unsigned Index = getInstructionIndex(MI);
1675 if (pli.liveAt(Index))
1676 ++NumConflicts;
1677 }
1678 return NumConflicts;
1679}
1680
1681/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1682/// around all defs and uses of the specified interval.
1683void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1684 unsigned PhysReg, VirtRegMap &vrm) {
1685 unsigned SpillReg = getRepresentativeReg(PhysReg);
1686
1687 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1688 // If there are registers which alias PhysReg, but which are not a
1689 // sub-register of the chosen representative super register. Assert
1690 // since we can't handle it yet.
1691 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1692 tri_->isSuperRegister(*AS, SpillReg));
1693
1694 LiveInterval &pli = getInterval(SpillReg);
1695 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1696 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1697 E = mri_->reg_end(); I != E; ++I) {
1698 MachineOperand &O = I.getOperand();
1699 MachineInstr *MI = O.getParent();
1700 if (SeenMIs.count(MI))
1701 continue;
1702 SeenMIs.insert(MI);
1703 unsigned Index = getInstructionIndex(MI);
1704 if (pli.liveAt(Index)) {
1705 vrm.addEmergencySpill(SpillReg, MI);
1706 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1707 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1708 if (!hasInterval(*AS))
1709 continue;
1710 LiveInterval &spli = getInterval(*AS);
1711 if (spli.liveAt(Index))
1712 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1713 }
1714 }
1715 }
1716}