blob: d8ca141c6693da14588111428013878e16f10381 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Evan Chengbc165e42007-08-16 07:24:22 +000039namespace {
40 // Hidden options for help debugging.
41 cl::opt<bool> DisableReMat("disable-rematerialization",
42 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000043
44 cl::opt<bool> SplitAtBB("split-intervals-at-bb",
Evan Cheng33faddc2007-12-06 08:54:31 +000045 cl::init(true), cl::Hidden);
Evan Cheng0cbb1162007-11-29 01:06:25 +000046 cl::opt<int> SplitLimit("split-limit",
47 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000048}
49
Chris Lattnercd3245a2006-12-19 22:41:21 +000050STATISTIC(numIntervals, "Number of original intervals");
51STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000052STATISTIC(numFolds , "Number of loads/stores folded into instructions");
53STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000054
Devang Patel19974732007-05-03 01:11:54 +000055char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000056namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000057 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000059
Chris Lattnerf7da2c72006-08-24 22:43:55 +000060void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000061 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000063 AU.addPreservedID(MachineLoopInfoID);
64 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000065 AU.addPreservedID(PHIEliminationID);
66 AU.addRequiredID(PHIEliminationID);
67 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000069}
70
Chris Lattnerf7da2c72006-08-24 22:43:55 +000071void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000072 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000073 mi2iMap_.clear();
74 i2miMap_.clear();
75 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000076 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
77 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000078 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
79 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000080}
81
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000082/// runOnMachineFunction - Register allocate the whole function
83///
84bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 mf_ = &fn;
Evan Chengd70dbb52008-02-22 09:24:50 +000086 mri_ = &mf_->getRegInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000087 tm_ = &fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +000088 tri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000089 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 lv_ = &getAnalysis<LiveVariables>();
Dan Gohman6f0d0242008-02-10 18:45:23 +000091 allocatableRegs_ = tri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000092
Chris Lattner428b92e2006-09-15 03:57:23 +000093 // Number MachineInstrs and MachineBasicBlocks.
94 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000095 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000096
97 unsigned MIIndex = 0;
98 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
99 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000100 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000101
Chris Lattner428b92e2006-09-15 03:57:23 +0000102 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
103 I != E; ++I) {
104 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000106 i2miMap_.push_back(I);
107 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000108 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000109
110 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000111 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
112 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
113 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000114 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000115 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000116 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000117
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000118 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000119
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120 numIntervals += getNumIntervals();
121
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000122 DOUT << "********** INTERVALS **********\n";
123 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000124 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000125 DOUT << "\n";
126 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000127
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000129 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000130 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000131}
132
Chris Lattner70ca3582004-09-30 15:59:17 +0000133/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000134void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000135 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000136 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000137 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000138 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000139 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000140
141 O << "********** MACHINEINSTRS **********\n";
142 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
143 mbbi != mbbe; ++mbbi) {
144 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
145 for (MachineBasicBlock::iterator mii = mbbi->begin(),
146 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000147 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000148 }
149 }
150}
151
Evan Chengc92da382007-11-03 07:20:12 +0000152/// conflictsWithPhysRegDef - Returns true if the specified register
153/// is defined during the duration of the specified interval.
154bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
155 VirtRegMap &vrm, unsigned reg) {
156 for (LiveInterval::Ranges::const_iterator
157 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
158 for (unsigned index = getBaseIndex(I->start),
159 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
160 index += InstrSlots::NUM) {
161 // skip deleted instructions
162 while (index != end && !getInstructionFromIndex(index))
163 index += InstrSlots::NUM;
164 if (index == end) break;
165
166 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000167 unsigned SrcReg, DstReg;
168 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
169 if (SrcReg == li.reg || DstReg == li.reg)
170 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000171 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
172 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000173 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000174 continue;
175 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000176 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000177 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000178 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000179 if (!vrm.hasPhys(PhysReg))
180 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000181 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000182 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000183 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000184 return true;
185 }
186 }
187 }
188
189 return false;
190}
191
Evan Cheng549f27d32007-08-13 23:45:17 +0000192void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000193 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000194 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000195 else
196 cerr << "%reg" << reg;
197}
198
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000199void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000200 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000201 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000202 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000203 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000204 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000205
Evan Cheng419852c2008-04-03 16:39:43 +0000206 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
207 DOUT << "is a implicit_def\n";
208 return;
209 }
210
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000211 // Virtual registers may be defined multiple times (due to phi
212 // elimination and 2-addr elimination). Much of what we do only has to be
213 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000214 // time we see a vreg.
215 if (interval.empty()) {
216 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000217 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000218 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000219 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000220 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000221 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000222 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000223 tii_->isMoveInstr(*mi, SrcReg, DstReg))
224 CopyMI = mi;
225 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000226
227 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000228
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000229 // Loop over all of the blocks that the vreg is defined in. There are
230 // two cases we have to handle here. The most common case is a vreg
231 // whose lifetime is contained within a basic block. In this case there
232 // will be a single kill, in MBB, which comes after the definition.
233 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
234 // FIXME: what about dead vars?
235 unsigned killIdx;
236 if (vi.Kills[0] != mi)
237 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
238 else
239 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000240
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000241 // If the kill happens after the definition, we have an intra-block
242 // live range.
243 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000244 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000245 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000246 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000247 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000248 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000249 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 return;
251 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000252 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000253
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 // The other case we handle is when a virtual register lives to the end
255 // of the defining block, potentially live across some blocks, then is
256 // live into some number of blocks, but gets killed. Start by adding a
257 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000258 LiveRange NewLR(defIndex,
259 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000261 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 interval.addRange(NewLR);
263
264 // Iterate over all of the blocks that the variable is completely
265 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
266 // live interval.
267 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
268 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000269 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
270 if (!MBB->empty()) {
271 LiveRange LR(getMBBStartIdx(i),
272 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000273 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000274 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000275 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 }
277 }
278 }
279
280 // Finally, this virtual register is live from the start of any killing
281 // block to the 'use' slot of the killing instruction.
282 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
283 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000284 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000285 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000286 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000288 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000289 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 }
291
292 } else {
293 // If this is the second time we see a virtual register definition, it
294 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000295 // the result of two address elimination, then the vreg is one of the
296 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000297 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298 // If this is a two-address definition, then we have already processed
299 // the live range. The only problem is that we didn't realize there
300 // are actually two values in the live interval. Because of this we
301 // need to take the LiveRegion that defines this register and split it
302 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000303 assert(interval.containsOneValue());
304 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000305 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000306
Evan Cheng4f8ff162007-08-11 00:59:19 +0000307 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000308 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000309
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000311 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000312 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000313
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000314 // Two-address vregs should always only be redefined once. This means
315 // that at this point, there should be exactly one value number in it.
316 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
317
Chris Lattner91725b72006-08-31 05:54:43 +0000318 // The new value number (#1) is defined by the instruction we claimed
319 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000320 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
321 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000322
Chris Lattner91725b72006-08-31 05:54:43 +0000323 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000324 OldValNo->def = RedefIndex;
325 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000326
327 // Add the new live interval which replaces the range for the input copy.
328 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000329 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000331 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332
333 // If this redefinition is dead, we need to add a dummy unit live
334 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000335 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000336 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000338 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000339 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000340
341 } else {
342 // Otherwise, this must be because of phi elimination. If this is the
343 // first redefinition of the vreg that we have seen, go back and change
344 // the live range in the PHI block to be a different value number.
345 if (interval.containsOneValue()) {
346 assert(vi.Kills.size() == 1 &&
347 "PHI elimination vreg should have one kill, the PHI itself!");
348
349 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000350 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000351 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000352 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000353 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000354 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000355 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000356 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000357 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000358 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000359
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000360 // Replace the interval with one of a NEW value number. Note that this
361 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000362 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000363 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000364 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000365 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000366 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000367 }
368
369 // In the case of PHI elimination, each variable definition is only
370 // live until the end of the block. We've already taken care of the
371 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000372 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000373
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000374 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000375 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000376 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000377 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000378 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000379 tii_->isMoveInstr(*mi, SrcReg, DstReg))
380 CopyMI = mi;
381 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000382
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000383 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000384 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000386 interval.addKill(ValNo, killIndex);
387 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000388 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389 }
390 }
391
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000392 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000393}
394
Chris Lattnerf35fef72004-07-23 21:24:19 +0000395void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000396 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000397 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000398 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000399 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000400 // A physical register cannot be live across basic block, so its
401 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000402 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000403
Chris Lattner6b128bd2006-09-03 08:07:11 +0000404 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000405 unsigned start = getDefIndex(baseIndex);
406 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000407
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000408 // If it is not used after definition, it is considered dead at
409 // the instruction defining it. Hence its interval is:
410 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000411 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000412 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000413 end = getDefIndex(start) + 1;
414 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000415 }
416
417 // If it is not dead on definition, it must be killed by a
418 // subsequent instruction. Hence its interval is:
419 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000420 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000422 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000423 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000424 end = getUseIndex(baseIndex) + 1;
425 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000426 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000427 // Another instruction redefines the register before it is ever read.
428 // Then the register is essentially dead at the instruction that defines
429 // it. Hence its interval is:
430 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000431 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000432 end = getDefIndex(start) + 1;
433 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000434 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000436
437 // The only case we should have a dead physreg here without a killing or
438 // instruction where we know it's dead is if it is live-in to the function
439 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000440 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000441 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000442
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000443exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000444 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000445
Evan Cheng24a3cc42007-04-25 07:30:23 +0000446 // Already exists? Extend old live interval.
447 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000448 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000449 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000450 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000451 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000452 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000453 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000454}
455
Chris Lattnerf35fef72004-07-23 21:24:19 +0000456void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
457 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000458 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000459 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000460 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000461 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000462 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000463 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000464 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000465 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000466 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000467 tii_->isMoveInstr(*MI, SrcReg, DstReg))
468 CopyMI = MI;
469 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000470 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000471 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000472 // If MI also modifies the sub-register explicitly, avoid processing it
473 // more than once. Do not pass in TRI here so it checks for exact match.
474 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000475 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000476 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000477}
478
Evan Chengb371f452007-02-19 21:49:54 +0000479void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000480 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000481 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000482 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
483
484 // Look for kills, if it reaches a def before it's killed, then it shouldn't
485 // be considered a livein.
486 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000487 unsigned baseIndex = MIIdx;
488 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000489 unsigned end = start;
490 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000491 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000492 DOUT << " killed";
493 end = getUseIndex(baseIndex) + 1;
494 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000495 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000496 // Another instruction redefines the register before it is ever read.
497 // Then the register is essentially dead at the instruction that defines
498 // it. Hence its interval is:
499 // [defSlot(def), defSlot(def)+1)
500 DOUT << " dead";
501 end = getDefIndex(start) + 1;
502 goto exit;
503 }
504
505 baseIndex += InstrSlots::NUM;
506 ++mi;
507 }
508
509exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000510 // Live-in register might not be used at all.
511 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000512 if (isAlias) {
513 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000514 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000515 } else {
516 DOUT << " live through";
517 end = baseIndex;
518 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000519 }
520
Evan Chengf3bb2e62007-09-05 21:46:51 +0000521 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000522 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000523 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000524 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000525}
526
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000527/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000528/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000529/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000530/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000531void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000532 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
533 << "********** Function: "
534 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000535 // Track the index of the current machine instr.
536 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000537 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
538 MBBI != E; ++MBBI) {
539 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000540 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000541
Chris Lattner428b92e2006-09-15 03:57:23 +0000542 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000543
Dan Gohmancb406c22007-10-03 19:26:29 +0000544 // Create intervals for live-ins to this BB first.
545 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
546 LE = MBB->livein_end(); LI != LE; ++LI) {
547 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
548 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000549 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000550 if (!hasInterval(*AS))
551 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
552 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000553 }
554
Chris Lattner428b92e2006-09-15 03:57:23 +0000555 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000556 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000557
Evan Cheng438f7bc2006-11-10 08:43:01 +0000558 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000559 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
560 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000561 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000562 if (MO.isRegister() && MO.getReg() && MO.isDef())
563 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000565
566 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000567 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000568 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000569}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000570
Evan Cheng4ca980e2007-10-17 02:10:22 +0000571bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000572 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000573 std::vector<IdxMBBPair>::const_iterator I =
574 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
575
576 bool ResVal = false;
577 while (I != Idx2MBBMap.end()) {
578 if (LR.end <= I->first)
579 break;
580 MBBs.push_back(I->second);
581 ResVal = true;
582 ++I;
583 }
584 return ResVal;
585}
586
587
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000588LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000589 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000590 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000591 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000592}
Evan Chengf2fbca62007-11-12 06:35:08 +0000593
Evan Chengc8d044e2008-02-15 18:24:29 +0000594/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
595/// copy field and returns the source register that defines it.
596unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
597 if (!VNI->copy)
598 return 0;
599
600 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
601 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000602 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
603 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000604 unsigned SrcReg, DstReg;
605 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
606 return SrcReg;
607 assert(0 && "Unrecognized copy instruction!");
608 return 0;
609}
Evan Chengf2fbca62007-11-12 06:35:08 +0000610
611//===----------------------------------------------------------------------===//
612// Register allocator hooks.
613//
614
Evan Chengd70dbb52008-02-22 09:24:50 +0000615/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
616/// allow one) virtual register operand, then its uses are implicitly using
617/// the register. Returns the virtual register.
618unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
619 MachineInstr *MI) const {
620 unsigned RegOp = 0;
621 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
622 MachineOperand &MO = MI->getOperand(i);
623 if (!MO.isRegister() || !MO.isUse())
624 continue;
625 unsigned Reg = MO.getReg();
626 if (Reg == 0 || Reg == li.reg)
627 continue;
628 // FIXME: For now, only remat MI with at most one register operand.
629 assert(!RegOp &&
630 "Can't rematerialize instruction with multiple register operand!");
631 RegOp = MO.getReg();
632 break;
633 }
634 return RegOp;
635}
636
637/// isValNoAvailableAt - Return true if the val# of the specified interval
638/// which reaches the given instruction also reaches the specified use index.
639bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
640 unsigned UseIdx) const {
641 unsigned Index = getInstructionIndex(MI);
642 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
643 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
644 return UI != li.end() && UI->valno == ValNo;
645}
646
Evan Chengf2fbca62007-11-12 06:35:08 +0000647/// isReMaterializable - Returns true if the definition MI of the specified
648/// val# of the specified interval is re-materializable.
649bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000650 const VNInfo *ValNo, MachineInstr *MI,
651 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000652 if (DisableReMat)
653 return false;
654
Evan Cheng5ef3a042007-12-06 00:01:56 +0000655 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000656 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000657 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000658
659 int FrameIdx = 0;
660 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000661 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000662 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
663 // this but remember this is not safe to fold into a two-address
664 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000665 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000666 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000667
Evan Chengd70dbb52008-02-22 09:24:50 +0000668 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000669 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000670 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000671
672 unsigned ImpUse = getReMatImplicitUse(li, MI);
673 if (ImpUse) {
674 const LiveInterval &ImpLi = getInterval(ImpUse);
675 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
676 re = mri_->use_end(); ri != re; ++ri) {
677 MachineInstr *UseMI = &*ri;
678 unsigned UseIdx = getInstructionIndex(UseMI);
679 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
680 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000681 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000682 return false;
683 }
684 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000685 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000686 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000687
Evan Chengdd3465e2008-02-23 01:44:27 +0000688 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000689}
690
691/// isReMaterializable - Returns true if every definition of MI of every
692/// val# of the specified interval is re-materializable.
693bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
694 isLoad = false;
695 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
696 i != e; ++i) {
697 const VNInfo *VNI = *i;
698 unsigned DefIdx = VNI->def;
699 if (DefIdx == ~1U)
700 continue; // Dead val#.
701 // Is the def for the val# rematerializable?
702 if (DefIdx == ~0u)
703 return false;
704 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
705 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000706 if (!ReMatDefMI ||
707 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000708 return false;
709 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000710 }
711 return true;
712}
713
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000714/// FilterFoldedOps - Filter out two-address use operands. Return
715/// true if it finds any issue with the operands that ought to prevent
716/// folding.
717static bool FilterFoldedOps(MachineInstr *MI,
718 SmallVector<unsigned, 2> &Ops,
719 unsigned &MRInfo,
720 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000721 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000722
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000723 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000724 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
725 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000726 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000727 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000728 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000729 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000730 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000731 MRInfo |= (unsigned)VirtRegMap::isMod;
732 else {
733 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000734 if (!MO.isImplicit() &&
735 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000736 MRInfo = VirtRegMap::isModRef;
737 continue;
738 }
739 MRInfo |= (unsigned)VirtRegMap::isRef;
740 }
741 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000742 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000743 return false;
744}
745
746
747/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
748/// slot / to reg or any rematerialized load into ith operand of specified
749/// MI. If it is successul, MI is updated with the newly created MI and
750/// returns true.
751bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
752 VirtRegMap &vrm, MachineInstr *DefMI,
753 unsigned InstrIdx,
754 SmallVector<unsigned, 2> &Ops,
755 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000756 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000757 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000758 RemoveMachineInstrFromMaps(MI);
759 vrm.RemoveMachineInstrFromMaps(MI);
760 MI->eraseFromParent();
761 ++numFolds;
762 return true;
763 }
764
765 // Filter the list of operand indexes that are to be folded. Abort if
766 // any operand will prevent folding.
767 unsigned MRInfo = 0;
768 SmallVector<unsigned, 2> FoldOps;
769 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
770 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000771
Evan Cheng427f4c12008-03-31 23:19:51 +0000772 // The only time it's safe to fold into a two address instruction is when
773 // it's folding reload and spill from / into a spill stack slot.
774 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000775 return false;
776
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000777 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
778 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000779 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000780 // Remember this instruction uses the spill slot.
781 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
782
Evan Chengf2fbca62007-11-12 06:35:08 +0000783 // Attempt to fold the memory reference into the instruction. If
784 // we can do this, we don't need to insert spill code.
785 if (lv_)
786 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000787 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000788 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000789 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000790 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000791 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000792 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000793 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000794 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000795 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000796 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
797 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000798 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000799 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000800 return true;
801 }
802 return false;
803}
804
Evan Cheng018f9b02007-12-05 03:22:34 +0000805/// canFoldMemoryOperand - Returns true if the specified load / store
806/// folding is possible.
807bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000808 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000809 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000810 // Filter the list of operand indexes that are to be folded. Abort if
811 // any operand will prevent folding.
812 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000813 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000814 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
815 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000816
Evan Cheng3c75ba82008-04-01 21:37:32 +0000817 // It's only legal to remat for a use, not a def.
818 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000819 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000820
Evan Chengd70dbb52008-02-22 09:24:50 +0000821 return tii_->canFoldMemoryOperand(MI, FoldOps);
822}
823
Evan Cheng81a03822007-11-17 00:40:40 +0000824bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
825 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
826 for (LiveInterval::Ranges::const_iterator
827 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
828 std::vector<IdxMBBPair>::const_iterator II =
829 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
830 if (II == Idx2MBBMap.end())
831 continue;
832 if (I->end > II->first) // crossing a MBB.
833 return false;
834 MBBs.insert(II->second);
835 if (MBBs.size() > 1)
836 return false;
837 }
838 return true;
839}
840
Evan Chengd70dbb52008-02-22 09:24:50 +0000841/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
842/// interval on to-be re-materialized operands of MI) with new register.
843void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
844 MachineInstr *MI, unsigned NewVReg,
845 VirtRegMap &vrm) {
846 // There is an implicit use. That means one of the other operand is
847 // being remat'ed and the remat'ed instruction has li.reg as an
848 // use operand. Make sure we rewrite that as well.
849 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
850 MachineOperand &MO = MI->getOperand(i);
851 if (!MO.isRegister())
852 continue;
853 unsigned Reg = MO.getReg();
854 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
855 continue;
856 if (!vrm.isReMaterialized(Reg))
857 continue;
858 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000859 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
860 if (UseMO)
861 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000862 }
863}
864
Evan Chengf2fbca62007-11-12 06:35:08 +0000865/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
866/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000867bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000868rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
869 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000870 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000871 unsigned Slot, int LdSlot,
872 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000873 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000874 const TargetRegisterClass* rc,
875 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000876 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000877 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000878 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000879 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000880 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000881 RestartInstruction:
882 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
883 MachineOperand& mop = MI->getOperand(i);
884 if (!mop.isRegister())
885 continue;
886 unsigned Reg = mop.getReg();
887 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000888 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000889 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000890 if (Reg != li.reg)
891 continue;
892
893 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000894 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000895 int FoldSlot = Slot;
896 if (DefIsReMat) {
897 // If this is the rematerializable definition MI itself and
898 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000899 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000900 DOUT << "\t\t\t\tErasing re-materlizable def: ";
901 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000902 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000903 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000904 MI->eraseFromParent();
905 break;
906 }
907
908 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000909 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000910 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000911 if (isLoad) {
912 // Try fold loads (from stack slot, constant pool, etc.) into uses.
913 FoldSS = isLoadSS;
914 FoldSlot = LdSlot;
915 }
916 }
917
Evan Chengf2fbca62007-11-12 06:35:08 +0000918 // Scan all of the operands of this instruction rewriting operands
919 // to use NewVReg instead of li.reg as appropriate. We do this for
920 // two reasons:
921 //
922 // 1. If the instr reads the same spilled vreg multiple times, we
923 // want to reuse the NewVReg.
924 // 2. If the instr is a two-addr instruction, we are required to
925 // keep the src/dst regs pinned.
926 //
927 // Keep track of whether we replace a use and/or def so that we can
928 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000929
Evan Cheng81a03822007-11-17 00:40:40 +0000930 HasUse = mop.isUse();
931 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000932 SmallVector<unsigned, 2> Ops;
933 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000934 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000935 const MachineOperand &MOj = MI->getOperand(j);
936 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000937 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000938 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000939 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000940 continue;
941 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000942 Ops.push_back(j);
943 HasUse |= MOj.isUse();
944 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000945 }
946 }
947
Evan Cheng018f9b02007-12-05 03:22:34 +0000948 if (TryFold) {
949 // Do not fold load / store here if we are splitting. We'll find an
950 // optimal point to insert a load / store later.
951 if (!TrySplit) {
952 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
953 Ops, FoldSS, FoldSlot, Reg)) {
954 // Folding the load/store can completely change the instruction in
955 // unpredictable ways, rescan it from the beginning.
956 HasUse = false;
957 HasDef = false;
958 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000959 if (isRemoved(MI))
960 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000961 goto RestartInstruction;
962 }
963 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000964 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000965 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000966 } else
967 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000968
969 // Create a new virtual register for the spill interval.
970 bool CreatedNewVReg = false;
971 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +0000972 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000973 vrm.grow();
974 CreatedNewVReg = true;
975 }
976 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000977 if (mop.isImplicit())
978 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +0000979
980 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +0000981 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
982 MachineOperand &mopj = MI->getOperand(Ops[j]);
983 mopj.setReg(NewVReg);
984 if (mopj.isImplicit())
985 rewriteImplicitOps(li, MI, NewVReg, vrm);
986 }
Evan Chengcddbb832007-11-30 21:23:43 +0000987
Evan Cheng81a03822007-11-17 00:40:40 +0000988 if (CreatedNewVReg) {
989 if (DefIsReMat) {
990 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +0000991 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +0000992 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +0000993 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000994 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +0000995 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +0000996 }
997 if (!CanDelete || (HasUse && HasDef)) {
998 // If this is a two-addr instruction then its use operands are
999 // rematerializable but its def is not. It should be assigned a
1000 // stack slot.
1001 vrm.assignVirt2StackSlot(NewVReg, Slot);
1002 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001003 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001004 vrm.assignVirt2StackSlot(NewVReg, Slot);
1005 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001006 } else if (HasUse && HasDef &&
1007 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1008 // If this interval hasn't been assigned a stack slot (because earlier
1009 // def is a deleted remat def), do it now.
1010 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1011 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001012 }
1013
Evan Cheng313d4b82008-02-23 00:33:04 +00001014 // Re-matting an instruction with virtual register use. Add the
1015 // register as an implicit use on the use MI.
1016 if (DefIsReMat && ImpUse)
1017 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1018
Evan Chengf2fbca62007-11-12 06:35:08 +00001019 // create a new register interval for this spill / remat.
1020 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001021 if (CreatedNewVReg) {
1022 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001023 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001024 if (TrySplit)
1025 vrm.setIsSplitFromReg(NewVReg, li.reg);
1026 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001027
1028 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001029 if (CreatedNewVReg) {
1030 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1031 nI.getNextValue(~0U, 0, VNInfoAllocator));
1032 DOUT << " +" << LR;
1033 nI.addRange(LR);
1034 } else {
1035 // Extend the split live interval to this def / use.
1036 unsigned End = getUseIndex(index)+1;
1037 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1038 nI.getValNumInfo(nI.getNumValNums()-1));
1039 DOUT << " +" << LR;
1040 nI.addRange(LR);
1041 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001042 }
1043 if (HasDef) {
1044 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1045 nI.getNextValue(~0U, 0, VNInfoAllocator));
1046 DOUT << " +" << LR;
1047 nI.addRange(LR);
1048 }
Evan Cheng81a03822007-11-17 00:40:40 +00001049
Evan Chengf2fbca62007-11-12 06:35:08 +00001050 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001051 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001052 DOUT << '\n';
1053 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001054 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001055}
Evan Cheng81a03822007-11-17 00:40:40 +00001056bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001057 const VNInfo *VNI,
1058 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001059 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001060 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1061 unsigned KillIdx = VNI->kills[j];
1062 if (KillIdx > Idx && KillIdx < End)
1063 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001064 }
1065 return false;
1066}
1067
Evan Cheng1953d0c2007-11-29 10:12:14 +00001068static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1069 const VNInfo *VNI = NULL;
1070 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1071 e = li.vni_end(); i != e; ++i)
1072 if ((*i)->def == DefIdx) {
1073 VNI = *i;
1074 break;
1075 }
1076 return VNI;
1077}
1078
Evan Cheng063284c2008-02-21 00:34:19 +00001079/// RewriteInfo - Keep track of machine instrs that will be rewritten
1080/// during spilling.
1081struct RewriteInfo {
1082 unsigned Index;
1083 MachineInstr *MI;
1084 bool HasUse;
1085 bool HasDef;
1086 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1087 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1088};
1089
1090struct RewriteInfoCompare {
1091 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1092 return LHS.Index < RHS.Index;
1093 }
1094};
1095
Evan Chengf2fbca62007-11-12 06:35:08 +00001096void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001097rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001098 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001099 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001100 unsigned Slot, int LdSlot,
1101 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001102 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001103 const TargetRegisterClass* rc,
1104 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001105 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001106 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001107 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001108 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001109 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1110 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001111 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001112 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001113 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001114 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001115 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001116
Evan Cheng063284c2008-02-21 00:34:19 +00001117 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001118 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001119 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001120 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1121 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001122 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001123 MachineOperand &O = ri.getOperand();
1124 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001125 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001126 unsigned index = getInstructionIndex(MI);
1127 if (index < start || index >= end)
1128 continue;
1129 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1130 }
1131 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1132
Evan Cheng313d4b82008-02-23 00:33:04 +00001133 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001134 // Now rewrite the defs and uses.
1135 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1136 RewriteInfo &rwi = RewriteMIs[i];
1137 ++i;
1138 unsigned index = rwi.Index;
1139 bool MIHasUse = rwi.HasUse;
1140 bool MIHasDef = rwi.HasDef;
1141 MachineInstr *MI = rwi.MI;
1142 // If MI def and/or use the same register multiple times, then there
1143 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001144 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001145 while (i != e && RewriteMIs[i].MI == MI) {
1146 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001147 bool isUse = RewriteMIs[i].HasUse;
1148 if (isUse) ++NumUses;
1149 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001150 MIHasDef |= RewriteMIs[i].HasDef;
1151 ++i;
1152 }
Evan Cheng81a03822007-11-17 00:40:40 +00001153 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001154
1155 if (ImpUse && MI != ReMatDefMI) {
1156 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001157 // register interval's spill weight to HUGE_VALF to prevent it from
1158 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001159 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001160 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001161 }
1162
Evan Cheng063284c2008-02-21 00:34:19 +00001163 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001164 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001165 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001166 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001167 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001168 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001169 // One common case:
1170 // x = use
1171 // ...
1172 // ...
1173 // def = ...
1174 // = use
1175 // It's better to start a new interval to avoid artifically
1176 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001177 if (MIHasDef && !MIHasUse) {
1178 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001179 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001180 }
1181 }
Evan Chengcada2452007-11-28 01:28:46 +00001182 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001183
1184 bool IsNew = ThisVReg == 0;
1185 if (IsNew) {
1186 // This ends the previous live interval. If all of its def / use
1187 // can be folded, give it a low spill weight.
1188 if (NewVReg && TrySplit && AllCanFold) {
1189 LiveInterval &nI = getOrCreateInterval(NewVReg);
1190 nI.weight /= 10.0F;
1191 }
1192 AllCanFold = true;
1193 }
1194 NewVReg = ThisVReg;
1195
Evan Cheng81a03822007-11-17 00:40:40 +00001196 bool HasDef = false;
1197 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001198 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001199 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1200 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001201 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001202 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001203 if (!HasDef && !HasUse)
1204 continue;
1205
Evan Cheng018f9b02007-12-05 03:22:34 +00001206 AllCanFold &= CanFold;
1207
Evan Cheng81a03822007-11-17 00:40:40 +00001208 // Update weight of spill interval.
1209 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001210 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001211 // The spill weight is now infinity as it cannot be spilled again.
1212 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001213 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001214 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001215
1216 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001217 if (HasDef) {
1218 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001219 bool HasKill = false;
1220 if (!HasUse)
1221 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1222 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001223 // If this is a two-address code, then this index starts a new VNInfo.
1224 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001225 if (VNI)
1226 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1227 }
Evan Chenge3110d02007-12-01 04:42:39 +00001228 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1229 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001230 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001231 if (SII == SpillIdxes.end()) {
1232 std::vector<SRInfo> S;
1233 S.push_back(SRInfo(index, NewVReg, true));
1234 SpillIdxes.insert(std::make_pair(MBBId, S));
1235 } else if (SII->second.back().vreg != NewVReg) {
1236 SII->second.push_back(SRInfo(index, NewVReg, true));
1237 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001238 // If there is an earlier def and this is a two-address
1239 // instruction, then it's not possible to fold the store (which
1240 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001241 SRInfo &Info = SII->second.back();
1242 Info.index = index;
1243 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001244 }
1245 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001246 } else if (SII != SpillIdxes.end() &&
1247 SII->second.back().vreg == NewVReg &&
1248 (int)index > SII->second.back().index) {
1249 // There is an earlier def that's not killed (must be two-address).
1250 // The spill is no longer needed.
1251 SII->second.pop_back();
1252 if (SII->second.empty()) {
1253 SpillIdxes.erase(MBBId);
1254 SpillMBBs.reset(MBBId);
1255 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001256 }
1257 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001258 }
1259
1260 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001261 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001262 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001263 if (SII != SpillIdxes.end() &&
1264 SII->second.back().vreg == NewVReg &&
1265 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001266 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001267 SII->second.back().canFold = false;
1268 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001269 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001270 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001271 // If we are splitting live intervals, only fold if it's the first
1272 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001273 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001274 else if (IsNew) {
1275 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001276 if (RII == RestoreIdxes.end()) {
1277 std::vector<SRInfo> Infos;
1278 Infos.push_back(SRInfo(index, NewVReg, true));
1279 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1280 } else {
1281 RII->second.push_back(SRInfo(index, NewVReg, true));
1282 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001283 RestoreMBBs.set(MBBId);
1284 }
1285 }
1286
1287 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001288 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001289 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001290 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001291
1292 if (NewVReg && TrySplit && AllCanFold) {
1293 // If all of its def / use can be folded, give it a low spill weight.
1294 LiveInterval &nI = getOrCreateInterval(NewVReg);
1295 nI.weight /= 10.0F;
1296 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001297}
1298
Evan Cheng1953d0c2007-11-29 10:12:14 +00001299bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1300 BitVector &RestoreMBBs,
1301 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1302 if (!RestoreMBBs[Id])
1303 return false;
1304 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1305 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1306 if (Restores[i].index == index &&
1307 Restores[i].vreg == vr &&
1308 Restores[i].canFold)
1309 return true;
1310 return false;
1311}
1312
1313void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1314 BitVector &RestoreMBBs,
1315 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1316 if (!RestoreMBBs[Id])
1317 return;
1318 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1319 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1320 if (Restores[i].index == index && Restores[i].vreg)
1321 Restores[i].index = -1;
1322}
Evan Cheng81a03822007-11-17 00:40:40 +00001323
Evan Cheng4cce6b42008-04-11 17:53:36 +00001324/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1325/// spilled and create empty intervals for their uses.
1326void
1327LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1328 const TargetRegisterClass* rc,
1329 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001330 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1331 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001332 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001333 MachineInstr *MI = &*ri;
1334 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001335 if (O.isDef()) {
1336 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1337 "Register def was not rewritten?");
1338 RemoveMachineInstrFromMaps(MI);
1339 vrm.RemoveMachineInstrFromMaps(MI);
1340 MI->eraseFromParent();
1341 } else {
1342 // This must be an use of an implicit_def so it's not part of the live
1343 // interval. Create a new empty live interval for it.
1344 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1345 unsigned NewVReg = mri_->createVirtualRegister(rc);
1346 vrm.grow();
1347 vrm.setIsImplicitlyDefined(NewVReg);
1348 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1349 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1350 MachineOperand &MO = MI->getOperand(i);
1351 if (MO.isReg() && MO.getReg() == li.reg)
1352 MO.setReg(NewVReg);
1353 }
1354 }
Evan Cheng419852c2008-04-03 16:39:43 +00001355 }
1356}
1357
Evan Cheng81a03822007-11-17 00:40:40 +00001358
Evan Chengf2fbca62007-11-12 06:35:08 +00001359std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001360addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001361 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001362 // Since this is called after the analysis is done we don't know if
1363 // LiveVariables is available
1364 lv_ = getAnalysisToUpdate<LiveVariables>();
1365
1366 assert(li.weight != HUGE_VALF &&
1367 "attempt to spill already spilled interval!");
1368
1369 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001370 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001371 DOUT << '\n';
1372
Evan Cheng81a03822007-11-17 00:40:40 +00001373 // Each bit specify whether it a spill is required in the MBB.
1374 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001375 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001376 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001377 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1378 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001379 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001380 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001381
1382 unsigned NumValNums = li.getNumValNums();
1383 SmallVector<MachineInstr*, 4> ReMatDefs;
1384 ReMatDefs.resize(NumValNums, NULL);
1385 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1386 ReMatOrigDefs.resize(NumValNums, NULL);
1387 SmallVector<int, 4> ReMatIds;
1388 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1389 BitVector ReMatDelete(NumValNums);
1390 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1391
Evan Cheng81a03822007-11-17 00:40:40 +00001392 // Spilling a split live interval. It cannot be split any further. Also,
1393 // it's also guaranteed to be a single val# / range interval.
1394 if (vrm.getPreSplitReg(li.reg)) {
1395 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001396 // Unset the split kill marker on the last use.
1397 unsigned KillIdx = vrm.getKillPoint(li.reg);
1398 if (KillIdx) {
1399 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1400 assert(KillMI && "Last use disappeared?");
1401 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1402 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001403 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001404 }
Evan Chengadf85902007-12-05 09:51:10 +00001405 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001406 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1407 Slot = vrm.getStackSlot(li.reg);
1408 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1409 MachineInstr *ReMatDefMI = DefIsReMat ?
1410 vrm.getReMaterializedMI(li.reg) : NULL;
1411 int LdSlot = 0;
1412 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1413 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001414 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001415 bool IsFirstRange = true;
1416 for (LiveInterval::Ranges::const_iterator
1417 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1418 // If this is a split live interval with multiple ranges, it means there
1419 // are two-address instructions that re-defined the value. Only the
1420 // first def can be rematerialized!
1421 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001422 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001423 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1424 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001425 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001426 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001427 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001428 } else {
1429 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1430 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001431 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001432 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001433 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001434 }
1435 IsFirstRange = false;
1436 }
Evan Cheng419852c2008-04-03 16:39:43 +00001437
Evan Cheng4cce6b42008-04-11 17:53:36 +00001438 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001439 return NewLIs;
1440 }
1441
1442 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001443 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1444 TrySplit = false;
1445 if (TrySplit)
1446 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001447 bool NeedStackSlot = false;
1448 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1449 i != e; ++i) {
1450 const VNInfo *VNI = *i;
1451 unsigned VN = VNI->id;
1452 unsigned DefIdx = VNI->def;
1453 if (DefIdx == ~1U)
1454 continue; // Dead val#.
1455 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001456 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1457 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001458 bool dummy;
1459 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001460 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001461 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001462 // Original def may be modified so we have to make a copy here. vrm must
1463 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001464 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001465
1466 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001467 if (VNI->hasPHIKill) {
1468 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001469 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001470 CanDelete = false;
1471 // Need a stack slot if there is any live range where uses cannot be
1472 // rematerialized.
1473 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001474 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001475 if (CanDelete)
1476 ReMatDelete.set(VN);
1477 } else {
1478 // Need a stack slot if there is any live range where uses cannot be
1479 // rematerialized.
1480 NeedStackSlot = true;
1481 }
1482 }
1483
1484 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001485 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001486 Slot = vrm.assignVirt2StackSlot(li.reg);
1487
1488 // Create new intervals and rewrite defs and uses.
1489 for (LiveInterval::Ranges::const_iterator
1490 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001491 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1492 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1493 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001494 bool CanDelete = ReMatDelete[I->valno->id];
1495 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001496 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001497 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001498 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001499 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001500 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001501 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001502 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001503 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001504 }
1505
Evan Cheng0cbb1162007-11-29 01:06:25 +00001506 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001507 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001508 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001509 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001510 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001511
Evan Chengb50bb8c2007-12-05 08:16:32 +00001512 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001513 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001514 if (NeedStackSlot) {
1515 int Id = SpillMBBs.find_first();
1516 while (Id != -1) {
1517 std::vector<SRInfo> &spills = SpillIdxes[Id];
1518 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1519 int index = spills[i].index;
1520 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001521 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001522 bool isReMat = vrm.isReMaterialized(VReg);
1523 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001524 bool CanFold = false;
1525 bool FoundUse = false;
1526 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001527 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001528 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001529 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1530 MachineOperand &MO = MI->getOperand(j);
1531 if (!MO.isRegister() || MO.getReg() != VReg)
1532 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001533
1534 Ops.push_back(j);
1535 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001536 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001537 if (isReMat ||
1538 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1539 RestoreMBBs, RestoreIdxes))) {
1540 // MI has two-address uses of the same register. If the use
1541 // isn't the first and only use in the BB, then we can't fold
1542 // it. FIXME: Move this to rewriteInstructionsForSpills.
1543 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001544 break;
1545 }
Evan Chengaee4af62007-12-02 08:30:39 +00001546 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001547 }
1548 }
1549 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001550 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001551 if (CanFold && !Ops.empty()) {
1552 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001553 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001554 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001555 // Also folded uses, do not issue a load.
1556 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001557 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1558 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001559 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001560 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001561 }
1562
Evan Cheng7e073ba2008-04-09 20:57:25 +00001563 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001564 if (!Folded) {
1565 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1566 bool isKill = LR->end == getStoreIndex(index);
1567 vrm.addSpillPoint(VReg, isKill, MI);
1568 if (isKill)
1569 AddedKill.insert(&nI);
1570 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001571 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001572 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001573 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001574 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001575
Evan Cheng1953d0c2007-11-29 10:12:14 +00001576 int Id = RestoreMBBs.find_first();
1577 while (Id != -1) {
1578 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1579 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1580 int index = restores[i].index;
1581 if (index == -1)
1582 continue;
1583 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001584 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001585 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001586 bool CanFold = false;
1587 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001588 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001589 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001590 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1591 MachineOperand &MO = MI->getOperand(j);
1592 if (!MO.isRegister() || MO.getReg() != VReg)
1593 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001594
Evan Cheng0cbb1162007-11-29 01:06:25 +00001595 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001596 // If this restore were to be folded, it would have been folded
1597 // already.
1598 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001599 break;
1600 }
Evan Chengaee4af62007-12-02 08:30:39 +00001601 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001602 }
1603 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001604
1605 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001606 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001607 if (CanFold && !Ops.empty()) {
1608 if (!vrm.isReMaterialized(VReg))
1609 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1610 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001611 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1612 int LdSlot = 0;
1613 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1614 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001615 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001616 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1617 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001618 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1619 if (ImpUse) {
1620 // Re-matting an instruction with virtual register use. Add the
1621 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001622 // interval's spill weight to HUGE_VALF to prevent it from being
1623 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001624 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001625 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001626 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1627 }
Evan Chengaee4af62007-12-02 08:30:39 +00001628 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001629 }
1630 // If folding is not possible / failed, then tell the spiller to issue a
1631 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001632 if (Folded)
1633 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001634 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001635 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001636 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001637 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001638 }
1639
Evan Chengb50bb8c2007-12-05 08:16:32 +00001640 // Finalize intervals: add kills, finalize spill weights, and filter out
1641 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001642 std::vector<LiveInterval*> RetNewLIs;
1643 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1644 LiveInterval *LI = NewLIs[i];
1645 if (!LI->empty()) {
1646 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001647 if (!AddedKill.count(LI)) {
1648 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001649 unsigned LastUseIdx = getBaseIndex(LR->end);
1650 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001651 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001652 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001653 if (LastUse->getOperand(UseIdx).isImplicit() ||
1654 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001655 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001656 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001657 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001658 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001659 RetNewLIs.push_back(LI);
1660 }
1661 }
Evan Cheng81a03822007-11-17 00:40:40 +00001662
Evan Cheng4cce6b42008-04-11 17:53:36 +00001663 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001664 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001665}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001666
1667/// hasAllocatableSuperReg - Return true if the specified physical register has
1668/// any super register that's allocatable.
1669bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1670 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1671 if (allocatableRegs_[*AS] && hasInterval(*AS))
1672 return true;
1673 return false;
1674}
1675
1676/// getRepresentativeReg - Find the largest super register of the specified
1677/// physical register.
1678unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1679 // Find the largest super-register that is allocatable.
1680 unsigned BestReg = Reg;
1681 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1682 unsigned SuperReg = *AS;
1683 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1684 BestReg = SuperReg;
1685 break;
1686 }
1687 }
1688 return BestReg;
1689}
1690
1691/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1692/// specified interval that conflicts with the specified physical register.
1693unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1694 unsigned PhysReg) const {
1695 unsigned NumConflicts = 0;
1696 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1697 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1698 E = mri_->reg_end(); I != E; ++I) {
1699 MachineOperand &O = I.getOperand();
1700 MachineInstr *MI = O.getParent();
1701 unsigned Index = getInstructionIndex(MI);
1702 if (pli.liveAt(Index))
1703 ++NumConflicts;
1704 }
1705 return NumConflicts;
1706}
1707
1708/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1709/// around all defs and uses of the specified interval.
1710void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1711 unsigned PhysReg, VirtRegMap &vrm) {
1712 unsigned SpillReg = getRepresentativeReg(PhysReg);
1713
1714 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1715 // If there are registers which alias PhysReg, but which are not a
1716 // sub-register of the chosen representative super register. Assert
1717 // since we can't handle it yet.
1718 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1719 tri_->isSuperRegister(*AS, SpillReg));
1720
1721 LiveInterval &pli = getInterval(SpillReg);
1722 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1723 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1724 E = mri_->reg_end(); I != E; ++I) {
1725 MachineOperand &O = I.getOperand();
1726 MachineInstr *MI = O.getParent();
1727 if (SeenMIs.count(MI))
1728 continue;
1729 SeenMIs.insert(MI);
1730 unsigned Index = getInstructionIndex(MI);
1731 if (pli.liveAt(Index)) {
1732 vrm.addEmergencySpill(SpillReg, MI);
1733 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1734 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1735 if (!hasInterval(*AS))
1736 continue;
1737 LiveInterval &spli = getInterval(*AS);
1738 if (spli.liveAt(Index))
1739 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1740 }
1741 }
1742 }
1743}