blob: d49dfd058c539552dd9f023c32993a5c81c6102c [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
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
63 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000068 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mi2iMap_.clear();
70 i2miMap_.clear();
71 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000072 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
73 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000074 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
75 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000076}
77
Owen Anderson80b3ce62008-05-28 20:54:50 +000078void LiveIntervals::computeNumbering() {
79 Index2MiMap OldI2MI = i2miMap_;
80
81 Idx2MBBMap.clear();
82 MBB2IdxMap.clear();
83 mi2iMap_.clear();
84 i2miMap_.clear();
85
Chris Lattner428b92e2006-09-15 03:57:23 +000086 // Number MachineInstrs and MachineBasicBlocks.
87 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000088 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000089
90 unsigned MIIndex = 0;
91 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
92 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000093 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000094
Chris Lattner428b92e2006-09-15 03:57:23 +000095 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
96 I != E; ++I) {
97 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000099 i2miMap_.push_back(I);
100 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000101 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000102
103 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000104 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
105 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
106 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000107 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000109 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000110
111 if (!OldI2MI.empty())
112 for (iterator I = begin(), E = end(); I != E; ++I)
113 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
114 LI != LE; ++LI) {
115 LI->start = mi2iMap_[OldI2MI[LI->start]];
116 LI->end = mi2iMap_[OldI2MI[LI->end]];
117 }
118}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000119
Owen Anderson80b3ce62008-05-28 20:54:50 +0000120/// runOnMachineFunction - Register allocate the whole function
121///
122bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
123 mf_ = &fn;
124 mri_ = &mf_->getRegInfo();
125 tm_ = &fn.getTarget();
126 tri_ = tm_->getRegisterInfo();
127 tii_ = tm_->getInstrInfo();
128 lv_ = &getAnalysis<LiveVariables>();
129 allocatableRegs_ = tri_->getAllocatableSet(fn);
130
131 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000133
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134 numIntervals += getNumIntervals();
135
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000136 DOUT << "********** INTERVALS **********\n";
137 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000138 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000139 DOUT << "\n";
140 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000142 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000143 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000144 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000145}
146
Chris Lattner70ca3582004-09-30 15:59:17 +0000147/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000148void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000149 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000150 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000151 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000152 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000153 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000154
155 O << "********** MACHINEINSTRS **********\n";
156 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
157 mbbi != mbbe; ++mbbi) {
158 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
159 for (MachineBasicBlock::iterator mii = mbbi->begin(),
160 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000161 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000162 }
163 }
164}
165
Evan Chengc92da382007-11-03 07:20:12 +0000166/// conflictsWithPhysRegDef - Returns true if the specified register
167/// is defined during the duration of the specified interval.
168bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
169 VirtRegMap &vrm, unsigned reg) {
170 for (LiveInterval::Ranges::const_iterator
171 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
172 for (unsigned index = getBaseIndex(I->start),
173 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
174 index += InstrSlots::NUM) {
175 // skip deleted instructions
176 while (index != end && !getInstructionFromIndex(index))
177 index += InstrSlots::NUM;
178 if (index == end) break;
179
180 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000181 unsigned SrcReg, DstReg;
182 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
183 if (SrcReg == li.reg || DstReg == li.reg)
184 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000185 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
186 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000187 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000188 continue;
189 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000190 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000191 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000192 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000193 if (!vrm.hasPhys(PhysReg))
194 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000195 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000196 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000197 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000198 return true;
199 }
200 }
201 }
202
203 return false;
204}
205
Evan Cheng549f27d32007-08-13 23:45:17 +0000206void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000207 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000208 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000209 else
210 cerr << "%reg" << reg;
211}
212
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000213void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000214 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000215 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000216 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000217 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000218 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000219
Evan Cheng419852c2008-04-03 16:39:43 +0000220 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
221 DOUT << "is a implicit_def\n";
222 return;
223 }
224
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000225 // Virtual registers may be defined multiple times (due to phi
226 // elimination and 2-addr elimination). Much of what we do only has to be
227 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000228 // time we see a vreg.
229 if (interval.empty()) {
230 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000231 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000232 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000233 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000234 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000235 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000236 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000237 tii_->isMoveInstr(*mi, SrcReg, DstReg))
238 CopyMI = mi;
239 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000240
241 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000242
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000243 // Loop over all of the blocks that the vreg is defined in. There are
244 // two cases we have to handle here. The most common case is a vreg
245 // whose lifetime is contained within a basic block. In this case there
246 // will be a single kill, in MBB, which comes after the definition.
247 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
248 // FIXME: what about dead vars?
249 unsigned killIdx;
250 if (vi.Kills[0] != mi)
251 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
252 else
253 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000254
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000255 // If the kill happens after the definition, we have an intra-block
256 // live range.
257 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000258 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000261 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000262 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000263 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000264 return;
265 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000266 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000267
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000268 // The other case we handle is when a virtual register lives to the end
269 // of the defining block, potentially live across some blocks, then is
270 // live into some number of blocks, but gets killed. Start by adding a
271 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000272 LiveRange NewLR(defIndex,
273 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000274 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000275 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 interval.addRange(NewLR);
277
278 // Iterate over all of the blocks that the variable is completely
279 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
280 // live interval.
281 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
282 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000283 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
284 if (!MBB->empty()) {
285 LiveRange LR(getMBBStartIdx(i),
286 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000287 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000288 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000289 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 }
291 }
292 }
293
294 // Finally, this virtual register is live from the start of any killing
295 // block to the 'use' slot of the killing instruction.
296 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
297 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000298 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000299 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000300 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000301 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000302 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000303 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000304 }
305
306 } else {
307 // If this is the second time we see a virtual register definition, it
308 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000309 // the result of two address elimination, then the vreg is one of the
310 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000311 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000312 // If this is a two-address definition, then we have already processed
313 // the live range. The only problem is that we didn't realize there
314 // are actually two values in the live interval. Because of this we
315 // need to take the LiveRegion that defines this register and split it
316 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000317 assert(interval.containsOneValue());
318 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000319 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320
Evan Cheng4f8ff162007-08-11 00:59:19 +0000321 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000322 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000323
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000325 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000327
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000328 // Two-address vregs should always only be redefined once. This means
329 // that at this point, there should be exactly one value number in it.
330 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
331
Chris Lattner91725b72006-08-31 05:54:43 +0000332 // The new value number (#1) is defined by the instruction we claimed
333 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000334 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
335 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000336
Chris Lattner91725b72006-08-31 05:54:43 +0000337 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000338 OldValNo->def = RedefIndex;
339 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000340
341 // Add the new live interval which replaces the range for the input copy.
342 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000343 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000345 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000346
347 // If this redefinition is dead, we need to add a dummy unit live
348 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000349 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000350 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000351
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000352 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000353 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354
355 } else {
356 // Otherwise, this must be because of phi elimination. If this is the
357 // first redefinition of the vreg that we have seen, go back and change
358 // the live range in the PHI block to be a different value number.
359 if (interval.containsOneValue()) {
360 assert(vi.Kills.size() == 1 &&
361 "PHI elimination vreg should have one kill, the PHI itself!");
362
363 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000364 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000366 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000367 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000368 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000369 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000370 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000371 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000372 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000374 // Replace the interval with one of a NEW value number. Note that this
375 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000376 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000377 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000378 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000379 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000380 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 }
382
383 // In the case of PHI elimination, each variable definition is only
384 // live until the end of the block. We've already taken care of the
385 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000386 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000387
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000388 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000389 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000390 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000391 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000392 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000393 tii_->isMoveInstr(*mi, SrcReg, DstReg))
394 CopyMI = mi;
395 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000396
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000397 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000398 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000399 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000400 interval.addKill(ValNo, killIndex);
401 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000402 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000403 }
404 }
405
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000406 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000407}
408
Chris Lattnerf35fef72004-07-23 21:24:19 +0000409void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000410 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000411 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000412 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000413 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000414 // A physical register cannot be live across basic block, so its
415 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000416 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000417
Chris Lattner6b128bd2006-09-03 08:07:11 +0000418 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000419 unsigned start = getDefIndex(baseIndex);
420 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000421
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 // If it is not used after definition, it is considered dead at
423 // the instruction defining it. Hence its interval is:
424 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000425 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000426 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000427 end = getDefIndex(start) + 1;
428 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429 }
430
431 // If it is not dead on definition, it must be killed by a
432 // subsequent instruction. Hence its interval is:
433 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000434 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000436 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000437 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000438 end = getUseIndex(baseIndex) + 1;
439 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000440 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000441 // Another instruction redefines the register before it is ever read.
442 // Then the register is essentially dead at the instruction that defines
443 // it. Hence its interval is:
444 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000445 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000446 end = getDefIndex(start) + 1;
447 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000448 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000449 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000450
451 // The only case we should have a dead physreg here without a killing or
452 // instruction where we know it's dead is if it is live-in to the function
453 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000454 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000455 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000456
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000457exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000458 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000459
Evan Cheng24a3cc42007-04-25 07:30:23 +0000460 // Already exists? Extend old live interval.
461 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000462 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000463 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000464 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000465 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000466 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000467 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000468}
469
Chris Lattnerf35fef72004-07-23 21:24:19 +0000470void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
471 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000472 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000473 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000474 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000475 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000476 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000477 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000478 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000479 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000480 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000481 tii_->isMoveInstr(*MI, SrcReg, DstReg))
482 CopyMI = MI;
483 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000484 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000485 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000486 // If MI also modifies the sub-register explicitly, avoid processing it
487 // more than once. Do not pass in TRI here so it checks for exact match.
488 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000489 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000490 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000491}
492
Evan Chengb371f452007-02-19 21:49:54 +0000493void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000494 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000495 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000496 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
497
498 // Look for kills, if it reaches a def before it's killed, then it shouldn't
499 // be considered a livein.
500 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000501 unsigned baseIndex = MIIdx;
502 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000503 unsigned end = start;
504 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000505 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000506 DOUT << " killed";
507 end = getUseIndex(baseIndex) + 1;
508 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000509 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000510 // Another instruction redefines the register before it is ever read.
511 // Then the register is essentially dead at the instruction that defines
512 // it. Hence its interval is:
513 // [defSlot(def), defSlot(def)+1)
514 DOUT << " dead";
515 end = getDefIndex(start) + 1;
516 goto exit;
517 }
518
519 baseIndex += InstrSlots::NUM;
520 ++mi;
521 }
522
523exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000524 // Live-in register might not be used at all.
525 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000526 if (isAlias) {
527 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000528 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000529 } else {
530 DOUT << " live through";
531 end = baseIndex;
532 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000533 }
534
Evan Chengf3bb2e62007-09-05 21:46:51 +0000535 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000536 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000537 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000538 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000539}
540
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000541/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000542/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000543/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000544/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000545void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000546 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
547 << "********** Function: "
548 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000549 // Track the index of the current machine instr.
550 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000551 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
552 MBBI != E; ++MBBI) {
553 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000554 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000555
Chris Lattner428b92e2006-09-15 03:57:23 +0000556 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000557
Dan Gohmancb406c22007-10-03 19:26:29 +0000558 // Create intervals for live-ins to this BB first.
559 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
560 LE = MBB->livein_end(); LI != LE; ++LI) {
561 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
562 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000563 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000564 if (!hasInterval(*AS))
565 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
566 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000567 }
568
Chris Lattner428b92e2006-09-15 03:57:23 +0000569 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000570 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000571
Evan Cheng438f7bc2006-11-10 08:43:01 +0000572 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000573 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
574 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000575 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000576 if (MO.isRegister() && MO.getReg() && MO.isDef())
577 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000578 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000579
580 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000581 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000582 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000583}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000584
Evan Cheng4ca980e2007-10-17 02:10:22 +0000585bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000586 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000587 std::vector<IdxMBBPair>::const_iterator I =
588 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
589
590 bool ResVal = false;
591 while (I != Idx2MBBMap.end()) {
592 if (LR.end <= I->first)
593 break;
594 MBBs.push_back(I->second);
595 ResVal = true;
596 ++I;
597 }
598 return ResVal;
599}
600
601
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000602LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000603 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000604 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000605 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000606}
Evan Chengf2fbca62007-11-12 06:35:08 +0000607
Evan Chengc8d044e2008-02-15 18:24:29 +0000608/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
609/// copy field and returns the source register that defines it.
610unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
611 if (!VNI->copy)
612 return 0;
613
614 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
615 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000616 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
617 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000618 unsigned SrcReg, DstReg;
619 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
620 return SrcReg;
621 assert(0 && "Unrecognized copy instruction!");
622 return 0;
623}
Evan Chengf2fbca62007-11-12 06:35:08 +0000624
625//===----------------------------------------------------------------------===//
626// Register allocator hooks.
627//
628
Evan Chengd70dbb52008-02-22 09:24:50 +0000629/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
630/// allow one) virtual register operand, then its uses are implicitly using
631/// the register. Returns the virtual register.
632unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
633 MachineInstr *MI) const {
634 unsigned RegOp = 0;
635 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
636 MachineOperand &MO = MI->getOperand(i);
637 if (!MO.isRegister() || !MO.isUse())
638 continue;
639 unsigned Reg = MO.getReg();
640 if (Reg == 0 || Reg == li.reg)
641 continue;
642 // FIXME: For now, only remat MI with at most one register operand.
643 assert(!RegOp &&
644 "Can't rematerialize instruction with multiple register operand!");
645 RegOp = MO.getReg();
646 break;
647 }
648 return RegOp;
649}
650
651/// isValNoAvailableAt - Return true if the val# of the specified interval
652/// which reaches the given instruction also reaches the specified use index.
653bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
654 unsigned UseIdx) const {
655 unsigned Index = getInstructionIndex(MI);
656 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
657 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
658 return UI != li.end() && UI->valno == ValNo;
659}
660
Evan Chengf2fbca62007-11-12 06:35:08 +0000661/// isReMaterializable - Returns true if the definition MI of the specified
662/// val# of the specified interval is re-materializable.
663bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000664 const VNInfo *ValNo, MachineInstr *MI,
665 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000666 if (DisableReMat)
667 return false;
668
Evan Cheng5ef3a042007-12-06 00:01:56 +0000669 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000670 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000671 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000672
673 int FrameIdx = 0;
674 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000675 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000676 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
677 // this but remember this is not safe to fold into a two-address
678 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000679 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000680 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000681
Evan Chengd70dbb52008-02-22 09:24:50 +0000682 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000683 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000684 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000685
686 unsigned ImpUse = getReMatImplicitUse(li, MI);
687 if (ImpUse) {
688 const LiveInterval &ImpLi = getInterval(ImpUse);
689 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
690 re = mri_->use_end(); ri != re; ++ri) {
691 MachineInstr *UseMI = &*ri;
692 unsigned UseIdx = getInstructionIndex(UseMI);
693 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
694 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000695 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000696 return false;
697 }
698 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000699 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000700 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000701
Evan Chengdd3465e2008-02-23 01:44:27 +0000702 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000703}
704
705/// isReMaterializable - Returns true if every definition of MI of every
706/// val# of the specified interval is re-materializable.
707bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
708 isLoad = false;
709 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
710 i != e; ++i) {
711 const VNInfo *VNI = *i;
712 unsigned DefIdx = VNI->def;
713 if (DefIdx == ~1U)
714 continue; // Dead val#.
715 // Is the def for the val# rematerializable?
716 if (DefIdx == ~0u)
717 return false;
718 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
719 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000720 if (!ReMatDefMI ||
721 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000722 return false;
723 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000724 }
725 return true;
726}
727
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000728/// FilterFoldedOps - Filter out two-address use operands. Return
729/// true if it finds any issue with the operands that ought to prevent
730/// folding.
731static bool FilterFoldedOps(MachineInstr *MI,
732 SmallVector<unsigned, 2> &Ops,
733 unsigned &MRInfo,
734 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000735 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000736
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000737 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000738 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
739 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000740 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000741 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000742 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000743 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000744 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000745 MRInfo |= (unsigned)VirtRegMap::isMod;
746 else {
747 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000748 if (!MO.isImplicit() &&
749 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000750 MRInfo = VirtRegMap::isModRef;
751 continue;
752 }
753 MRInfo |= (unsigned)VirtRegMap::isRef;
754 }
755 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000756 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000757 return false;
758}
759
760
761/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
762/// slot / to reg or any rematerialized load into ith operand of specified
763/// MI. If it is successul, MI is updated with the newly created MI and
764/// returns true.
765bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
766 VirtRegMap &vrm, MachineInstr *DefMI,
767 unsigned InstrIdx,
768 SmallVector<unsigned, 2> &Ops,
769 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000770 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000771 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000772 RemoveMachineInstrFromMaps(MI);
773 vrm.RemoveMachineInstrFromMaps(MI);
774 MI->eraseFromParent();
775 ++numFolds;
776 return true;
777 }
778
779 // Filter the list of operand indexes that are to be folded. Abort if
780 // any operand will prevent folding.
781 unsigned MRInfo = 0;
782 SmallVector<unsigned, 2> FoldOps;
783 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
784 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000785
Evan Cheng427f4c12008-03-31 23:19:51 +0000786 // The only time it's safe to fold into a two address instruction is when
787 // it's folding reload and spill from / into a spill stack slot.
788 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000789 return false;
790
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000791 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
792 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000793 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000794 // Remember this instruction uses the spill slot.
795 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
796
Evan Chengf2fbca62007-11-12 06:35:08 +0000797 // Attempt to fold the memory reference into the instruction. If
798 // we can do this, we don't need to insert spill code.
799 if (lv_)
800 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000801 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000802 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000803 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000804 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000805 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000806 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000807 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000808 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000809 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000810 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
811 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000812 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000813 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000814 return true;
815 }
816 return false;
817}
818
Evan Cheng018f9b02007-12-05 03:22:34 +0000819/// canFoldMemoryOperand - Returns true if the specified load / store
820/// folding is possible.
821bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000822 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000823 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000824 // Filter the list of operand indexes that are to be folded. Abort if
825 // any operand will prevent folding.
826 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000827 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000828 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
829 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000830
Evan Cheng3c75ba82008-04-01 21:37:32 +0000831 // It's only legal to remat for a use, not a def.
832 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000833 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000834
Evan Chengd70dbb52008-02-22 09:24:50 +0000835 return tii_->canFoldMemoryOperand(MI, FoldOps);
836}
837
Evan Cheng81a03822007-11-17 00:40:40 +0000838bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
839 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
840 for (LiveInterval::Ranges::const_iterator
841 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
842 std::vector<IdxMBBPair>::const_iterator II =
843 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
844 if (II == Idx2MBBMap.end())
845 continue;
846 if (I->end > II->first) // crossing a MBB.
847 return false;
848 MBBs.insert(II->second);
849 if (MBBs.size() > 1)
850 return false;
851 }
852 return true;
853}
854
Evan Chengd70dbb52008-02-22 09:24:50 +0000855/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
856/// interval on to-be re-materialized operands of MI) with new register.
857void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
858 MachineInstr *MI, unsigned NewVReg,
859 VirtRegMap &vrm) {
860 // There is an implicit use. That means one of the other operand is
861 // being remat'ed and the remat'ed instruction has li.reg as an
862 // use operand. Make sure we rewrite that as well.
863 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
864 MachineOperand &MO = MI->getOperand(i);
865 if (!MO.isRegister())
866 continue;
867 unsigned Reg = MO.getReg();
868 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
869 continue;
870 if (!vrm.isReMaterialized(Reg))
871 continue;
872 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000873 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
874 if (UseMO)
875 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000876 }
877}
878
Evan Chengf2fbca62007-11-12 06:35:08 +0000879/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
880/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000881bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000882rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
883 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000884 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000885 unsigned Slot, int LdSlot,
886 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000887 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000888 const TargetRegisterClass* rc,
889 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000890 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000891 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000892 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000893 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000894 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000895 RestartInstruction:
896 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
897 MachineOperand& mop = MI->getOperand(i);
898 if (!mop.isRegister())
899 continue;
900 unsigned Reg = mop.getReg();
901 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000902 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000903 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000904 if (Reg != li.reg)
905 continue;
906
907 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000908 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000909 int FoldSlot = Slot;
910 if (DefIsReMat) {
911 // If this is the rematerializable definition MI itself and
912 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000913 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000914 DOUT << "\t\t\t\tErasing re-materlizable def: ";
915 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000916 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000917 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000918 MI->eraseFromParent();
919 break;
920 }
921
922 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000923 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000924 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000925 if (isLoad) {
926 // Try fold loads (from stack slot, constant pool, etc.) into uses.
927 FoldSS = isLoadSS;
928 FoldSlot = LdSlot;
929 }
930 }
931
Evan Chengf2fbca62007-11-12 06:35:08 +0000932 // Scan all of the operands of this instruction rewriting operands
933 // to use NewVReg instead of li.reg as appropriate. We do this for
934 // two reasons:
935 //
936 // 1. If the instr reads the same spilled vreg multiple times, we
937 // want to reuse the NewVReg.
938 // 2. If the instr is a two-addr instruction, we are required to
939 // keep the src/dst regs pinned.
940 //
941 // Keep track of whether we replace a use and/or def so that we can
942 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000943
Evan Cheng81a03822007-11-17 00:40:40 +0000944 HasUse = mop.isUse();
945 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000946 SmallVector<unsigned, 2> Ops;
947 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000948 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000949 const MachineOperand &MOj = MI->getOperand(j);
950 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000951 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000952 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000953 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000954 continue;
955 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000956 Ops.push_back(j);
957 HasUse |= MOj.isUse();
958 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000959 }
960 }
961
Evan Cheng018f9b02007-12-05 03:22:34 +0000962 if (TryFold) {
963 // Do not fold load / store here if we are splitting. We'll find an
964 // optimal point to insert a load / store later.
965 if (!TrySplit) {
966 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
967 Ops, FoldSS, FoldSlot, Reg)) {
968 // Folding the load/store can completely change the instruction in
969 // unpredictable ways, rescan it from the beginning.
970 HasUse = false;
971 HasDef = false;
972 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000973 if (isRemoved(MI))
974 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000975 goto RestartInstruction;
976 }
977 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000978 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000979 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000980 } else
981 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000982
983 // Create a new virtual register for the spill interval.
984 bool CreatedNewVReg = false;
985 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +0000986 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000987 vrm.grow();
988 CreatedNewVReg = true;
989 }
990 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000991 if (mop.isImplicit())
992 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +0000993
994 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +0000995 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
996 MachineOperand &mopj = MI->getOperand(Ops[j]);
997 mopj.setReg(NewVReg);
998 if (mopj.isImplicit())
999 rewriteImplicitOps(li, MI, NewVReg, vrm);
1000 }
Evan Chengcddbb832007-11-30 21:23:43 +00001001
Evan Cheng81a03822007-11-17 00:40:40 +00001002 if (CreatedNewVReg) {
1003 if (DefIsReMat) {
1004 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001005 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001006 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001007 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001008 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001009 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001010 }
1011 if (!CanDelete || (HasUse && HasDef)) {
1012 // If this is a two-addr instruction then its use operands are
1013 // rematerializable but its def is not. It should be assigned a
1014 // stack slot.
1015 vrm.assignVirt2StackSlot(NewVReg, Slot);
1016 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001017 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001018 vrm.assignVirt2StackSlot(NewVReg, Slot);
1019 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001020 } else if (HasUse && HasDef &&
1021 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1022 // If this interval hasn't been assigned a stack slot (because earlier
1023 // def is a deleted remat def), do it now.
1024 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1025 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001026 }
1027
Evan Cheng313d4b82008-02-23 00:33:04 +00001028 // Re-matting an instruction with virtual register use. Add the
1029 // register as an implicit use on the use MI.
1030 if (DefIsReMat && ImpUse)
1031 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1032
Evan Chengf2fbca62007-11-12 06:35:08 +00001033 // create a new register interval for this spill / remat.
1034 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001035 if (CreatedNewVReg) {
1036 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001037 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001038 if (TrySplit)
1039 vrm.setIsSplitFromReg(NewVReg, li.reg);
1040 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001041
1042 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001043 if (CreatedNewVReg) {
1044 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1045 nI.getNextValue(~0U, 0, VNInfoAllocator));
1046 DOUT << " +" << LR;
1047 nI.addRange(LR);
1048 } else {
1049 // Extend the split live interval to this def / use.
1050 unsigned End = getUseIndex(index)+1;
1051 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1052 nI.getValNumInfo(nI.getNumValNums()-1));
1053 DOUT << " +" << LR;
1054 nI.addRange(LR);
1055 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001056 }
1057 if (HasDef) {
1058 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1059 nI.getNextValue(~0U, 0, VNInfoAllocator));
1060 DOUT << " +" << LR;
1061 nI.addRange(LR);
1062 }
Evan Cheng81a03822007-11-17 00:40:40 +00001063
Evan Chengf2fbca62007-11-12 06:35:08 +00001064 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001065 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001066 DOUT << '\n';
1067 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001068 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001069}
Evan Cheng81a03822007-11-17 00:40:40 +00001070bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001071 const VNInfo *VNI,
1072 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001073 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001074 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1075 unsigned KillIdx = VNI->kills[j];
1076 if (KillIdx > Idx && KillIdx < End)
1077 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001078 }
1079 return false;
1080}
1081
Evan Cheng1953d0c2007-11-29 10:12:14 +00001082static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1083 const VNInfo *VNI = NULL;
1084 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1085 e = li.vni_end(); i != e; ++i)
1086 if ((*i)->def == DefIdx) {
1087 VNI = *i;
1088 break;
1089 }
1090 return VNI;
1091}
1092
Evan Cheng063284c2008-02-21 00:34:19 +00001093/// RewriteInfo - Keep track of machine instrs that will be rewritten
1094/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001095namespace {
1096 struct RewriteInfo {
1097 unsigned Index;
1098 MachineInstr *MI;
1099 bool HasUse;
1100 bool HasDef;
1101 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1102 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1103 };
Evan Cheng063284c2008-02-21 00:34:19 +00001104
Dan Gohman844731a2008-05-13 00:00:25 +00001105 struct RewriteInfoCompare {
1106 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1107 return LHS.Index < RHS.Index;
1108 }
1109 };
1110}
Evan Cheng063284c2008-02-21 00:34:19 +00001111
Evan Chengf2fbca62007-11-12 06:35:08 +00001112void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001113rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001114 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001115 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001116 unsigned Slot, int LdSlot,
1117 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001118 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001119 const TargetRegisterClass* rc,
1120 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001121 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001122 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001123 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001124 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001125 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1126 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001127 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001128 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001129 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001130 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001131 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001132
Evan Cheng063284c2008-02-21 00:34:19 +00001133 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001134 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001135 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001136 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1137 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001138 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001139 MachineOperand &O = ri.getOperand();
1140 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001141 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001142 unsigned index = getInstructionIndex(MI);
1143 if (index < start || index >= end)
1144 continue;
1145 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1146 }
1147 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1148
Evan Cheng313d4b82008-02-23 00:33:04 +00001149 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001150 // Now rewrite the defs and uses.
1151 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1152 RewriteInfo &rwi = RewriteMIs[i];
1153 ++i;
1154 unsigned index = rwi.Index;
1155 bool MIHasUse = rwi.HasUse;
1156 bool MIHasDef = rwi.HasDef;
1157 MachineInstr *MI = rwi.MI;
1158 // If MI def and/or use the same register multiple times, then there
1159 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001160 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001161 while (i != e && RewriteMIs[i].MI == MI) {
1162 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001163 bool isUse = RewriteMIs[i].HasUse;
1164 if (isUse) ++NumUses;
1165 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001166 MIHasDef |= RewriteMIs[i].HasDef;
1167 ++i;
1168 }
Evan Cheng81a03822007-11-17 00:40:40 +00001169 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001170
Evan Cheng0a891ed2008-05-23 23:00:04 +00001171 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001172 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001173 // register interval's spill weight to HUGE_VALF to prevent it from
1174 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001175 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001176 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001177 }
1178
Evan Cheng063284c2008-02-21 00:34:19 +00001179 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001180 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001181 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001182 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001183 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001184 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001185 // One common case:
1186 // x = use
1187 // ...
1188 // ...
1189 // def = ...
1190 // = use
1191 // It's better to start a new interval to avoid artifically
1192 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001193 if (MIHasDef && !MIHasUse) {
1194 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001195 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001196 }
1197 }
Evan Chengcada2452007-11-28 01:28:46 +00001198 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001199
1200 bool IsNew = ThisVReg == 0;
1201 if (IsNew) {
1202 // This ends the previous live interval. If all of its def / use
1203 // can be folded, give it a low spill weight.
1204 if (NewVReg && TrySplit && AllCanFold) {
1205 LiveInterval &nI = getOrCreateInterval(NewVReg);
1206 nI.weight /= 10.0F;
1207 }
1208 AllCanFold = true;
1209 }
1210 NewVReg = ThisVReg;
1211
Evan Cheng81a03822007-11-17 00:40:40 +00001212 bool HasDef = false;
1213 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001214 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001215 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1216 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001217 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001218 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001219 if (!HasDef && !HasUse)
1220 continue;
1221
Evan Cheng018f9b02007-12-05 03:22:34 +00001222 AllCanFold &= CanFold;
1223
Evan Cheng81a03822007-11-17 00:40:40 +00001224 // Update weight of spill interval.
1225 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001226 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001227 // The spill weight is now infinity as it cannot be spilled again.
1228 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001229 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001230 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001231
1232 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001233 if (HasDef) {
1234 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001235 bool HasKill = false;
1236 if (!HasUse)
1237 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1238 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001239 // If this is a two-address code, then this index starts a new VNInfo.
1240 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001241 if (VNI)
1242 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1243 }
Evan Chenge3110d02007-12-01 04:42:39 +00001244 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1245 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001246 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001247 if (SII == SpillIdxes.end()) {
1248 std::vector<SRInfo> S;
1249 S.push_back(SRInfo(index, NewVReg, true));
1250 SpillIdxes.insert(std::make_pair(MBBId, S));
1251 } else if (SII->second.back().vreg != NewVReg) {
1252 SII->second.push_back(SRInfo(index, NewVReg, true));
1253 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001254 // If there is an earlier def and this is a two-address
1255 // instruction, then it's not possible to fold the store (which
1256 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001257 SRInfo &Info = SII->second.back();
1258 Info.index = index;
1259 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001260 }
1261 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001262 } else if (SII != SpillIdxes.end() &&
1263 SII->second.back().vreg == NewVReg &&
1264 (int)index > SII->second.back().index) {
1265 // There is an earlier def that's not killed (must be two-address).
1266 // The spill is no longer needed.
1267 SII->second.pop_back();
1268 if (SII->second.empty()) {
1269 SpillIdxes.erase(MBBId);
1270 SpillMBBs.reset(MBBId);
1271 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001272 }
1273 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001274 }
1275
1276 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001277 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001278 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001279 if (SII != SpillIdxes.end() &&
1280 SII->second.back().vreg == NewVReg &&
1281 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001282 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001283 SII->second.back().canFold = false;
1284 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001285 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001286 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001287 // If we are splitting live intervals, only fold if it's the first
1288 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001289 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001290 else if (IsNew) {
1291 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001292 if (RII == RestoreIdxes.end()) {
1293 std::vector<SRInfo> Infos;
1294 Infos.push_back(SRInfo(index, NewVReg, true));
1295 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1296 } else {
1297 RII->second.push_back(SRInfo(index, NewVReg, true));
1298 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001299 RestoreMBBs.set(MBBId);
1300 }
1301 }
1302
1303 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001304 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001305 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001306 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001307
1308 if (NewVReg && TrySplit && AllCanFold) {
1309 // If all of its def / use can be folded, give it a low spill weight.
1310 LiveInterval &nI = getOrCreateInterval(NewVReg);
1311 nI.weight /= 10.0F;
1312 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001313}
1314
Evan Cheng1953d0c2007-11-29 10:12:14 +00001315bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1316 BitVector &RestoreMBBs,
1317 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1318 if (!RestoreMBBs[Id])
1319 return false;
1320 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1321 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1322 if (Restores[i].index == index &&
1323 Restores[i].vreg == vr &&
1324 Restores[i].canFold)
1325 return true;
1326 return false;
1327}
1328
1329void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1330 BitVector &RestoreMBBs,
1331 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1332 if (!RestoreMBBs[Id])
1333 return;
1334 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1335 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1336 if (Restores[i].index == index && Restores[i].vreg)
1337 Restores[i].index = -1;
1338}
Evan Cheng81a03822007-11-17 00:40:40 +00001339
Evan Cheng4cce6b42008-04-11 17:53:36 +00001340/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1341/// spilled and create empty intervals for their uses.
1342void
1343LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1344 const TargetRegisterClass* rc,
1345 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001346 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1347 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001348 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001349 MachineInstr *MI = &*ri;
1350 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001351 if (O.isDef()) {
1352 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1353 "Register def was not rewritten?");
1354 RemoveMachineInstrFromMaps(MI);
1355 vrm.RemoveMachineInstrFromMaps(MI);
1356 MI->eraseFromParent();
1357 } else {
1358 // This must be an use of an implicit_def so it's not part of the live
1359 // interval. Create a new empty live interval for it.
1360 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1361 unsigned NewVReg = mri_->createVirtualRegister(rc);
1362 vrm.grow();
1363 vrm.setIsImplicitlyDefined(NewVReg);
1364 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1365 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1366 MachineOperand &MO = MI->getOperand(i);
1367 if (MO.isReg() && MO.getReg() == li.reg)
1368 MO.setReg(NewVReg);
1369 }
1370 }
Evan Cheng419852c2008-04-03 16:39:43 +00001371 }
1372}
1373
Evan Cheng81a03822007-11-17 00:40:40 +00001374
Evan Chengf2fbca62007-11-12 06:35:08 +00001375std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001376addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001377 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001378 // Since this is called after the analysis is done we don't know if
1379 // LiveVariables is available
1380 lv_ = getAnalysisToUpdate<LiveVariables>();
1381
1382 assert(li.weight != HUGE_VALF &&
1383 "attempt to spill already spilled interval!");
1384
1385 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001386 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001387 DOUT << '\n';
1388
Evan Cheng81a03822007-11-17 00:40:40 +00001389 // Each bit specify whether it a spill is required in the MBB.
1390 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001391 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001392 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001393 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1394 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001395 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001396 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001397
1398 unsigned NumValNums = li.getNumValNums();
1399 SmallVector<MachineInstr*, 4> ReMatDefs;
1400 ReMatDefs.resize(NumValNums, NULL);
1401 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1402 ReMatOrigDefs.resize(NumValNums, NULL);
1403 SmallVector<int, 4> ReMatIds;
1404 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1405 BitVector ReMatDelete(NumValNums);
1406 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1407
Evan Cheng81a03822007-11-17 00:40:40 +00001408 // Spilling a split live interval. It cannot be split any further. Also,
1409 // it's also guaranteed to be a single val# / range interval.
1410 if (vrm.getPreSplitReg(li.reg)) {
1411 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001412 // Unset the split kill marker on the last use.
1413 unsigned KillIdx = vrm.getKillPoint(li.reg);
1414 if (KillIdx) {
1415 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1416 assert(KillMI && "Last use disappeared?");
1417 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1418 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001419 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001420 }
Evan Chengadf85902007-12-05 09:51:10 +00001421 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001422 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1423 Slot = vrm.getStackSlot(li.reg);
1424 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1425 MachineInstr *ReMatDefMI = DefIsReMat ?
1426 vrm.getReMaterializedMI(li.reg) : NULL;
1427 int LdSlot = 0;
1428 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1429 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001430 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001431 bool IsFirstRange = true;
1432 for (LiveInterval::Ranges::const_iterator
1433 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1434 // If this is a split live interval with multiple ranges, it means there
1435 // are two-address instructions that re-defined the value. Only the
1436 // first def can be rematerialized!
1437 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001438 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001439 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1440 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001441 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001442 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001443 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001444 } else {
1445 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1446 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001447 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001448 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001449 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001450 }
1451 IsFirstRange = false;
1452 }
Evan Cheng419852c2008-04-03 16:39:43 +00001453
Evan Cheng4cce6b42008-04-11 17:53:36 +00001454 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001455 return NewLIs;
1456 }
1457
1458 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001459 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1460 TrySplit = false;
1461 if (TrySplit)
1462 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001463 bool NeedStackSlot = false;
1464 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1465 i != e; ++i) {
1466 const VNInfo *VNI = *i;
1467 unsigned VN = VNI->id;
1468 unsigned DefIdx = VNI->def;
1469 if (DefIdx == ~1U)
1470 continue; // Dead val#.
1471 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001472 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1473 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001474 bool dummy;
1475 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001476 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001477 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001478 // Original def may be modified so we have to make a copy here. vrm must
1479 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001480 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001481
1482 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001483 if (VNI->hasPHIKill) {
1484 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001485 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001486 CanDelete = false;
1487 // Need a stack slot if there is any live range where uses cannot be
1488 // rematerialized.
1489 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001490 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001491 if (CanDelete)
1492 ReMatDelete.set(VN);
1493 } else {
1494 // Need a stack slot if there is any live range where uses cannot be
1495 // rematerialized.
1496 NeedStackSlot = true;
1497 }
1498 }
1499
1500 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001501 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001502 Slot = vrm.assignVirt2StackSlot(li.reg);
1503
1504 // Create new intervals and rewrite defs and uses.
1505 for (LiveInterval::Ranges::const_iterator
1506 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001507 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1508 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1509 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001510 bool CanDelete = ReMatDelete[I->valno->id];
1511 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001512 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001513 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001514 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001515 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001516 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001517 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001518 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001519 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001520 }
1521
Evan Cheng0cbb1162007-11-29 01:06:25 +00001522 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001523 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001524 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001525 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001526 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001527
Evan Chengb50bb8c2007-12-05 08:16:32 +00001528 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001529 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001530 if (NeedStackSlot) {
1531 int Id = SpillMBBs.find_first();
1532 while (Id != -1) {
1533 std::vector<SRInfo> &spills = SpillIdxes[Id];
1534 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1535 int index = spills[i].index;
1536 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001537 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001538 bool isReMat = vrm.isReMaterialized(VReg);
1539 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001540 bool CanFold = false;
1541 bool FoundUse = false;
1542 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001543 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001544 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001545 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1546 MachineOperand &MO = MI->getOperand(j);
1547 if (!MO.isRegister() || MO.getReg() != VReg)
1548 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001549
1550 Ops.push_back(j);
1551 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001552 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001553 if (isReMat ||
1554 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1555 RestoreMBBs, RestoreIdxes))) {
1556 // MI has two-address uses of the same register. If the use
1557 // isn't the first and only use in the BB, then we can't fold
1558 // it. FIXME: Move this to rewriteInstructionsForSpills.
1559 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001560 break;
1561 }
Evan Chengaee4af62007-12-02 08:30:39 +00001562 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001563 }
1564 }
1565 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001566 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001567 if (CanFold && !Ops.empty()) {
1568 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001569 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001570 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001571 // Also folded uses, do not issue a load.
1572 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001573 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1574 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001575 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001576 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001577 }
1578
Evan Cheng7e073ba2008-04-09 20:57:25 +00001579 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001580 if (!Folded) {
1581 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1582 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001583 if (!MI->registerDefIsDead(nI.reg))
1584 // No need to spill a dead def.
1585 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001586 if (isKill)
1587 AddedKill.insert(&nI);
1588 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001589 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001590 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001591 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001592 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001593
Evan Cheng1953d0c2007-11-29 10:12:14 +00001594 int Id = RestoreMBBs.find_first();
1595 while (Id != -1) {
1596 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1597 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1598 int index = restores[i].index;
1599 if (index == -1)
1600 continue;
1601 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001602 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001603 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001604 bool CanFold = false;
1605 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001606 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001607 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001608 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1609 MachineOperand &MO = MI->getOperand(j);
1610 if (!MO.isRegister() || MO.getReg() != VReg)
1611 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001612
Evan Cheng0cbb1162007-11-29 01:06:25 +00001613 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001614 // If this restore were to be folded, it would have been folded
1615 // already.
1616 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001617 break;
1618 }
Evan Chengaee4af62007-12-02 08:30:39 +00001619 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001620 }
1621 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001622
1623 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001624 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001625 if (CanFold && !Ops.empty()) {
1626 if (!vrm.isReMaterialized(VReg))
1627 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1628 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001629 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1630 int LdSlot = 0;
1631 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1632 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001633 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001634 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1635 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001636 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1637 if (ImpUse) {
1638 // Re-matting an instruction with virtual register use. Add the
1639 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001640 // interval's spill weight to HUGE_VALF to prevent it from being
1641 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001642 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001643 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001644 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1645 }
Evan Chengaee4af62007-12-02 08:30:39 +00001646 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001647 }
1648 // If folding is not possible / failed, then tell the spiller to issue a
1649 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001650 if (Folded)
1651 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001652 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001653 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001654 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001655 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001656 }
1657
Evan Chengb50bb8c2007-12-05 08:16:32 +00001658 // Finalize intervals: add kills, finalize spill weights, and filter out
1659 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001660 std::vector<LiveInterval*> RetNewLIs;
1661 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1662 LiveInterval *LI = NewLIs[i];
1663 if (!LI->empty()) {
1664 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001665 if (!AddedKill.count(LI)) {
1666 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001667 unsigned LastUseIdx = getBaseIndex(LR->end);
1668 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001669 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001670 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001671 if (LastUse->getOperand(UseIdx).isImplicit() ||
1672 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001673 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001674 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001675 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001676 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001677 RetNewLIs.push_back(LI);
1678 }
1679 }
Evan Cheng81a03822007-11-17 00:40:40 +00001680
Evan Cheng4cce6b42008-04-11 17:53:36 +00001681 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001682 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001683}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001684
1685/// hasAllocatableSuperReg - Return true if the specified physical register has
1686/// any super register that's allocatable.
1687bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1688 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1689 if (allocatableRegs_[*AS] && hasInterval(*AS))
1690 return true;
1691 return false;
1692}
1693
1694/// getRepresentativeReg - Find the largest super register of the specified
1695/// physical register.
1696unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1697 // Find the largest super-register that is allocatable.
1698 unsigned BestReg = Reg;
1699 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1700 unsigned SuperReg = *AS;
1701 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1702 BestReg = SuperReg;
1703 break;
1704 }
1705 }
1706 return BestReg;
1707}
1708
1709/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1710/// specified interval that conflicts with the specified physical register.
1711unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1712 unsigned PhysReg) const {
1713 unsigned NumConflicts = 0;
1714 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1715 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1716 E = mri_->reg_end(); I != E; ++I) {
1717 MachineOperand &O = I.getOperand();
1718 MachineInstr *MI = O.getParent();
1719 unsigned Index = getInstructionIndex(MI);
1720 if (pli.liveAt(Index))
1721 ++NumConflicts;
1722 }
1723 return NumConflicts;
1724}
1725
1726/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1727/// around all defs and uses of the specified interval.
1728void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1729 unsigned PhysReg, VirtRegMap &vrm) {
1730 unsigned SpillReg = getRepresentativeReg(PhysReg);
1731
1732 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1733 // If there are registers which alias PhysReg, but which are not a
1734 // sub-register of the chosen representative super register. Assert
1735 // since we can't handle it yet.
1736 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1737 tri_->isSuperRegister(*AS, SpillReg));
1738
1739 LiveInterval &pli = getInterval(SpillReg);
1740 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1741 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1742 E = mri_->reg_end(); I != E; ++I) {
1743 MachineOperand &O = I.getOperand();
1744 MachineInstr *MI = O.getParent();
1745 if (SeenMIs.count(MI))
1746 continue;
1747 SeenMIs.insert(MI);
1748 unsigned Index = getInstructionIndex(MI);
1749 if (pli.liveAt(Index)) {
1750 vrm.addEmergencySpill(SpillReg, MI);
1751 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1752 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1753 if (!hasInterval(*AS))
1754 continue;
1755 LiveInterval &spli = getInterval(*AS);
1756 if (spli.liveAt(Index))
1757 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1758 }
1759 }
1760 }
1761}