blob: 2163bae81c537c460e5601af92abc0bdbdd665c1 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Owen Andersonfcc63502008-05-29 18:35:21 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000068 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mi2iMap_.clear();
70 i2miMap_.clear();
71 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000072 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
73 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000074 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
75 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000076}
77
Owen Anderson4b5b2092008-05-29 18:15:49 +000078#include <iostream>
79
Owen Anderson80b3ce62008-05-28 20:54:50 +000080void LiveIntervals::computeNumbering() {
81 Index2MiMap OldI2MI = i2miMap_;
82
83 Idx2MBBMap.clear();
84 MBB2IdxMap.clear();
85 mi2iMap_.clear();
86 i2miMap_.clear();
87
Chris Lattner428b92e2006-09-15 03:57:23 +000088 // Number MachineInstrs and MachineBasicBlocks.
89 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000090 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000091
92 unsigned MIIndex = 0;
93 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
94 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000095 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000096
Chris Lattner428b92e2006-09-15 03:57:23 +000097 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
98 I != E; ++I) {
99 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000100 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000101 i2miMap_.push_back(I);
102 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000103 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000104
105 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000106 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
107 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
108 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000109 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000110 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000111 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000112
113 if (!OldI2MI.empty())
114 for (iterator I = begin(), E = end(); I != E; ++I)
115 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
116 LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000117 unsigned offset = LI->start % InstrSlots::NUM;
118 LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
119
120 if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
121 // FIXME: Not correct when the instruction at LI->end has
122 // been removed
123 offset = LI->end % InstrSlots::NUM;
124 LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
125 } else {
126 LI->end = i2miMap_.size() * InstrSlots::NUM;
127 }
Owen Anderson745825f42008-05-28 22:40:08 +0000128
129 VNInfo* vni = LI->valno;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000130 offset = vni->def % InstrSlots::NUM;
131 vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
Owen Anderson745825f42008-05-28 22:40:08 +0000132
Owen Anderson4b5b2092008-05-29 18:15:49 +0000133 for (size_t i = 0; i < vni->kills.size(); ++i) {
134 offset = vni->kills[i] % InstrSlots::NUM;
135 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
136 offset;
137 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000138 }
139}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000140
Owen Anderson80b3ce62008-05-28 20:54:50 +0000141/// runOnMachineFunction - Register allocate the whole function
142///
143bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
144 mf_ = &fn;
145 mri_ = &mf_->getRegInfo();
146 tm_ = &fn.getTarget();
147 tri_ = tm_->getRegisterInfo();
148 tii_ = tm_->getInstrInfo();
149 lv_ = &getAnalysis<LiveVariables>();
150 allocatableRegs_ = tri_->getAllocatableSet(fn);
151
152 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000153 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000154
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000155 numIntervals += getNumIntervals();
156
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000157 DOUT << "********** INTERVALS **********\n";
158 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000159 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000160 DOUT << "\n";
161 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000162
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000163 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000164 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000165 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000166}
167
Chris Lattner70ca3582004-09-30 15:59:17 +0000168/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000169void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000170 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000171 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000172 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000173 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000174 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000175
176 O << "********** MACHINEINSTRS **********\n";
177 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
178 mbbi != mbbe; ++mbbi) {
179 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
180 for (MachineBasicBlock::iterator mii = mbbi->begin(),
181 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000182 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000183 }
184 }
185}
186
Evan Chengc92da382007-11-03 07:20:12 +0000187/// conflictsWithPhysRegDef - Returns true if the specified register
188/// is defined during the duration of the specified interval.
189bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
190 VirtRegMap &vrm, unsigned reg) {
191 for (LiveInterval::Ranges::const_iterator
192 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
193 for (unsigned index = getBaseIndex(I->start),
194 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
195 index += InstrSlots::NUM) {
196 // skip deleted instructions
197 while (index != end && !getInstructionFromIndex(index))
198 index += InstrSlots::NUM;
199 if (index == end) break;
200
201 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000202 unsigned SrcReg, DstReg;
203 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
204 if (SrcReg == li.reg || DstReg == li.reg)
205 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000206 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
207 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000208 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000209 continue;
210 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000211 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000212 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000213 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000214 if (!vrm.hasPhys(PhysReg))
215 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000216 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000217 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000218 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000219 return true;
220 }
221 }
222 }
223
224 return false;
225}
226
Evan Cheng549f27d32007-08-13 23:45:17 +0000227void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000228 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000229 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000230 else
231 cerr << "%reg" << reg;
232}
233
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000234void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000235 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000236 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000237 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000238 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000240
Evan Cheng419852c2008-04-03 16:39:43 +0000241 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
242 DOUT << "is a implicit_def\n";
243 return;
244 }
245
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000246 // Virtual registers may be defined multiple times (due to phi
247 // elimination and 2-addr elimination). Much of what we do only has to be
248 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 // time we see a vreg.
250 if (interval.empty()) {
251 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000252 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000253 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000254 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000255 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000256 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000257 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000258 tii_->isMoveInstr(*mi, SrcReg, DstReg))
259 CopyMI = mi;
260 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000261
262 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000263
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000264 // Loop over all of the blocks that the vreg is defined in. There are
265 // two cases we have to handle here. The most common case is a vreg
266 // whose lifetime is contained within a basic block. In this case there
267 // will be a single kill, in MBB, which comes after the definition.
268 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
269 // FIXME: what about dead vars?
270 unsigned killIdx;
271 if (vi.Kills[0] != mi)
272 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
273 else
274 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000275
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 // If the kill happens after the definition, we have an intra-block
277 // live range.
278 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000279 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000281 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000282 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000283 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000284 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 return;
286 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000287 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000288
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000289 // The other case we handle is when a virtual register lives to the end
290 // of the defining block, potentially live across some blocks, then is
291 // live into some number of blocks, but gets killed. Start by adding a
292 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000293 LiveRange NewLR(defIndex,
294 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000295 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000296 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 interval.addRange(NewLR);
298
299 // Iterate over all of the blocks that the variable is completely
300 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
301 // live interval.
302 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
303 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000304 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
305 if (!MBB->empty()) {
306 LiveRange LR(getMBBStartIdx(i),
307 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000308 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000310 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 }
312 }
313 }
314
315 // Finally, this virtual register is live from the start of any killing
316 // block to the 'use' slot of the killing instruction.
317 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
318 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000319 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000320 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000321 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000322 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000323 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000324 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000325 }
326
327 } else {
328 // If this is the second time we see a virtual register definition, it
329 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000330 // the result of two address elimination, then the vreg is one of the
331 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000332 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000333 // If this is a two-address definition, then we have already processed
334 // the live range. The only problem is that we didn't realize there
335 // are actually two values in the live interval. Because of this we
336 // need to take the LiveRegion that defines this register and split it
337 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000338 assert(interval.containsOneValue());
339 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000340 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341
Evan Cheng4f8ff162007-08-11 00:59:19 +0000342 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000343 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000344
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000346 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000348
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000349 // Two-address vregs should always only be redefined once. This means
350 // that at this point, there should be exactly one value number in it.
351 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
352
Chris Lattner91725b72006-08-31 05:54:43 +0000353 // The new value number (#1) is defined by the instruction we claimed
354 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000355 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
356 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000357
Chris Lattner91725b72006-08-31 05:54:43 +0000358 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000359 OldValNo->def = RedefIndex;
360 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000361
362 // Add the new live interval which replaces the range for the input copy.
363 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000364 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000366 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000367
368 // If this redefinition is dead, we need to add a dummy unit live
369 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000370 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000371 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000372
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000373 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000374 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000375
376 } else {
377 // Otherwise, this must be because of phi elimination. If this is the
378 // first redefinition of the vreg that we have seen, go back and change
379 // the live range in the PHI block to be a different value number.
380 if (interval.containsOneValue()) {
381 assert(vi.Kills.size() == 1 &&
382 "PHI elimination vreg should have one kill, the PHI itself!");
383
384 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000385 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000386 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000387 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000388 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000389 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000390 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000391 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000392 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000393 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000394
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000395 // Replace the interval with one of a NEW value number. Note that this
396 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000397 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000398 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000399 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000400 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000401 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000402 }
403
404 // In the case of PHI elimination, each variable definition is only
405 // live until the end of the block. We've already taken care of the
406 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000407 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000408
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000409 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000410 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000411 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000412 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000413 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000414 tii_->isMoveInstr(*mi, SrcReg, DstReg))
415 CopyMI = mi;
416 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000417
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000418 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000419 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000421 interval.addKill(ValNo, killIndex);
422 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000423 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000424 }
425 }
426
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000427 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000428}
429
Chris Lattnerf35fef72004-07-23 21:24:19 +0000430void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000431 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000432 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000433 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000434 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435 // A physical register cannot be live across basic block, so its
436 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000437 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000438
Chris Lattner6b128bd2006-09-03 08:07:11 +0000439 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440 unsigned start = getDefIndex(baseIndex);
441 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000442
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 // If it is not used after definition, it is considered dead at
444 // the instruction defining it. Hence its interval is:
445 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000446 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000447 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000448 end = getDefIndex(start) + 1;
449 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000450 }
451
452 // If it is not dead on definition, it must be killed by a
453 // subsequent instruction. Hence its interval is:
454 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000455 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000457 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000458 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000459 end = getUseIndex(baseIndex) + 1;
460 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000461 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000462 // Another instruction redefines the register before it is ever read.
463 // Then the register is essentially dead at the instruction that defines
464 // it. Hence its interval is:
465 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000466 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000467 end = getDefIndex(start) + 1;
468 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000469 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000470 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000471
472 // The only case we should have a dead physreg here without a killing or
473 // instruction where we know it's dead is if it is live-in to the function
474 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000475 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000476 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000477
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000478exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000479 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000480
Evan Cheng24a3cc42007-04-25 07:30:23 +0000481 // Already exists? Extend old live interval.
482 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000483 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000484 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000485 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000486 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000487 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000488 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000489}
490
Chris Lattnerf35fef72004-07-23 21:24:19 +0000491void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
492 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000493 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000494 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000495 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000496 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000497 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000498 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000499 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000500 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000501 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000502 tii_->isMoveInstr(*MI, SrcReg, DstReg))
503 CopyMI = MI;
504 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000505 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000506 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000507 // If MI also modifies the sub-register explicitly, avoid processing it
508 // more than once. Do not pass in TRI here so it checks for exact match.
509 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000510 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000511 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000512}
513
Evan Chengb371f452007-02-19 21:49:54 +0000514void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000515 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000516 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000517 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
518
519 // Look for kills, if it reaches a def before it's killed, then it shouldn't
520 // be considered a livein.
521 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000522 unsigned baseIndex = MIIdx;
523 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000524 unsigned end = start;
525 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000526 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000527 DOUT << " killed";
528 end = getUseIndex(baseIndex) + 1;
529 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000530 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000531 // Another instruction redefines the register before it is ever read.
532 // Then the register is essentially dead at the instruction that defines
533 // it. Hence its interval is:
534 // [defSlot(def), defSlot(def)+1)
535 DOUT << " dead";
536 end = getDefIndex(start) + 1;
537 goto exit;
538 }
539
540 baseIndex += InstrSlots::NUM;
541 ++mi;
542 }
543
544exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000545 // Live-in register might not be used at all.
546 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000547 if (isAlias) {
548 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000549 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000550 } else {
551 DOUT << " live through";
552 end = baseIndex;
553 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000554 }
555
Evan Chengf3bb2e62007-09-05 21:46:51 +0000556 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000557 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000558 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000559 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000560}
561
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000562/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000563/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000564/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000565/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000566void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000567 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
568 << "********** Function: "
569 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000570 // Track the index of the current machine instr.
571 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000572 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
573 MBBI != E; ++MBBI) {
574 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000575 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000576
Chris Lattner428b92e2006-09-15 03:57:23 +0000577 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000578
Dan Gohmancb406c22007-10-03 19:26:29 +0000579 // Create intervals for live-ins to this BB first.
580 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
581 LE = MBB->livein_end(); LI != LE; ++LI) {
582 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
583 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000584 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000585 if (!hasInterval(*AS))
586 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
587 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000588 }
589
Chris Lattner428b92e2006-09-15 03:57:23 +0000590 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000591 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000592
Evan Cheng438f7bc2006-11-10 08:43:01 +0000593 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000594 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
595 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000596 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000597 if (MO.isRegister() && MO.getReg() && MO.isDef())
598 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000599 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000600
601 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000602 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000603 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000604}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000605
Evan Cheng4ca980e2007-10-17 02:10:22 +0000606bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000607 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000608 std::vector<IdxMBBPair>::const_iterator I =
609 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
610
611 bool ResVal = false;
612 while (I != Idx2MBBMap.end()) {
613 if (LR.end <= I->first)
614 break;
615 MBBs.push_back(I->second);
616 ResVal = true;
617 ++I;
618 }
619 return ResVal;
620}
621
622
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000623LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000624 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000625 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000626 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000627}
Evan Chengf2fbca62007-11-12 06:35:08 +0000628
Evan Chengc8d044e2008-02-15 18:24:29 +0000629/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
630/// copy field and returns the source register that defines it.
631unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
632 if (!VNI->copy)
633 return 0;
634
635 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
636 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000637 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
638 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000639 unsigned SrcReg, DstReg;
640 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
641 return SrcReg;
642 assert(0 && "Unrecognized copy instruction!");
643 return 0;
644}
Evan Chengf2fbca62007-11-12 06:35:08 +0000645
646//===----------------------------------------------------------------------===//
647// Register allocator hooks.
648//
649
Evan Chengd70dbb52008-02-22 09:24:50 +0000650/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
651/// allow one) virtual register operand, then its uses are implicitly using
652/// the register. Returns the virtual register.
653unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
654 MachineInstr *MI) const {
655 unsigned RegOp = 0;
656 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
657 MachineOperand &MO = MI->getOperand(i);
658 if (!MO.isRegister() || !MO.isUse())
659 continue;
660 unsigned Reg = MO.getReg();
661 if (Reg == 0 || Reg == li.reg)
662 continue;
663 // FIXME: For now, only remat MI with at most one register operand.
664 assert(!RegOp &&
665 "Can't rematerialize instruction with multiple register operand!");
666 RegOp = MO.getReg();
667 break;
668 }
669 return RegOp;
670}
671
672/// isValNoAvailableAt - Return true if the val# of the specified interval
673/// which reaches the given instruction also reaches the specified use index.
674bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
675 unsigned UseIdx) const {
676 unsigned Index = getInstructionIndex(MI);
677 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
678 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
679 return UI != li.end() && UI->valno == ValNo;
680}
681
Evan Chengf2fbca62007-11-12 06:35:08 +0000682/// isReMaterializable - Returns true if the definition MI of the specified
683/// val# of the specified interval is re-materializable.
684bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000685 const VNInfo *ValNo, MachineInstr *MI,
686 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000687 if (DisableReMat)
688 return false;
689
Evan Cheng5ef3a042007-12-06 00:01:56 +0000690 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000691 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000692 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000693
694 int FrameIdx = 0;
695 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000696 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000697 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
698 // this but remember this is not safe to fold into a two-address
699 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000700 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000701 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000702
Evan Chengd70dbb52008-02-22 09:24:50 +0000703 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000704 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000705 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000706
707 unsigned ImpUse = getReMatImplicitUse(li, MI);
708 if (ImpUse) {
709 const LiveInterval &ImpLi = getInterval(ImpUse);
710 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
711 re = mri_->use_end(); ri != re; ++ri) {
712 MachineInstr *UseMI = &*ri;
713 unsigned UseIdx = getInstructionIndex(UseMI);
714 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
715 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000716 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000717 return false;
718 }
719 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000720 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000721 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000722
Evan Chengdd3465e2008-02-23 01:44:27 +0000723 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000724}
725
726/// isReMaterializable - Returns true if every definition of MI of every
727/// val# of the specified interval is re-materializable.
728bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
729 isLoad = false;
730 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
731 i != e; ++i) {
732 const VNInfo *VNI = *i;
733 unsigned DefIdx = VNI->def;
734 if (DefIdx == ~1U)
735 continue; // Dead val#.
736 // Is the def for the val# rematerializable?
737 if (DefIdx == ~0u)
738 return false;
739 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
740 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000741 if (!ReMatDefMI ||
742 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000743 return false;
744 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000745 }
746 return true;
747}
748
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000749/// FilterFoldedOps - Filter out two-address use operands. Return
750/// true if it finds any issue with the operands that ought to prevent
751/// folding.
752static bool FilterFoldedOps(MachineInstr *MI,
753 SmallVector<unsigned, 2> &Ops,
754 unsigned &MRInfo,
755 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000756 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000757
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000758 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000759 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
760 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000761 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000762 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000763 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000764 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000765 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000766 MRInfo |= (unsigned)VirtRegMap::isMod;
767 else {
768 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000769 if (!MO.isImplicit() &&
770 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000771 MRInfo = VirtRegMap::isModRef;
772 continue;
773 }
774 MRInfo |= (unsigned)VirtRegMap::isRef;
775 }
776 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000777 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000778 return false;
779}
780
781
782/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
783/// slot / to reg or any rematerialized load into ith operand of specified
784/// MI. If it is successul, MI is updated with the newly created MI and
785/// returns true.
786bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
787 VirtRegMap &vrm, MachineInstr *DefMI,
788 unsigned InstrIdx,
789 SmallVector<unsigned, 2> &Ops,
790 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000791 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000792 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000793 RemoveMachineInstrFromMaps(MI);
794 vrm.RemoveMachineInstrFromMaps(MI);
795 MI->eraseFromParent();
796 ++numFolds;
797 return true;
798 }
799
800 // Filter the list of operand indexes that are to be folded. Abort if
801 // any operand will prevent folding.
802 unsigned MRInfo = 0;
803 SmallVector<unsigned, 2> FoldOps;
804 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
805 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000806
Evan Cheng427f4c12008-03-31 23:19:51 +0000807 // The only time it's safe to fold into a two address instruction is when
808 // it's folding reload and spill from / into a spill stack slot.
809 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000810 return false;
811
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000812 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
813 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000814 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000815 // Remember this instruction uses the spill slot.
816 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
817
Evan Chengf2fbca62007-11-12 06:35:08 +0000818 // Attempt to fold the memory reference into the instruction. If
819 // we can do this, we don't need to insert spill code.
820 if (lv_)
821 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000822 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000823 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000824 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000825 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000826 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000827 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000828 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000829 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000830 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000831 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
832 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000833 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000834 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000835 return true;
836 }
837 return false;
838}
839
Evan Cheng018f9b02007-12-05 03:22:34 +0000840/// canFoldMemoryOperand - Returns true if the specified load / store
841/// folding is possible.
842bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000843 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000844 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000845 // Filter the list of operand indexes that are to be folded. Abort if
846 // any operand will prevent folding.
847 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000848 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000849 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
850 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000851
Evan Cheng3c75ba82008-04-01 21:37:32 +0000852 // It's only legal to remat for a use, not a def.
853 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000854 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000855
Evan Chengd70dbb52008-02-22 09:24:50 +0000856 return tii_->canFoldMemoryOperand(MI, FoldOps);
857}
858
Evan Cheng81a03822007-11-17 00:40:40 +0000859bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
860 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
861 for (LiveInterval::Ranges::const_iterator
862 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
863 std::vector<IdxMBBPair>::const_iterator II =
864 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
865 if (II == Idx2MBBMap.end())
866 continue;
867 if (I->end > II->first) // crossing a MBB.
868 return false;
869 MBBs.insert(II->second);
870 if (MBBs.size() > 1)
871 return false;
872 }
873 return true;
874}
875
Evan Chengd70dbb52008-02-22 09:24:50 +0000876/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
877/// interval on to-be re-materialized operands of MI) with new register.
878void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
879 MachineInstr *MI, unsigned NewVReg,
880 VirtRegMap &vrm) {
881 // There is an implicit use. That means one of the other operand is
882 // being remat'ed and the remat'ed instruction has li.reg as an
883 // use operand. Make sure we rewrite that as well.
884 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
885 MachineOperand &MO = MI->getOperand(i);
886 if (!MO.isRegister())
887 continue;
888 unsigned Reg = MO.getReg();
889 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
890 continue;
891 if (!vrm.isReMaterialized(Reg))
892 continue;
893 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000894 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
895 if (UseMO)
896 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000897 }
898}
899
Evan Chengf2fbca62007-11-12 06:35:08 +0000900/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
901/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000902bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000903rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
904 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000905 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000906 unsigned Slot, int LdSlot,
907 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000908 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000909 const TargetRegisterClass* rc,
910 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000911 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000912 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000913 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000914 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000915 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000916 RestartInstruction:
917 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
918 MachineOperand& mop = MI->getOperand(i);
919 if (!mop.isRegister())
920 continue;
921 unsigned Reg = mop.getReg();
922 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000923 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000924 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000925 if (Reg != li.reg)
926 continue;
927
928 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000929 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000930 int FoldSlot = Slot;
931 if (DefIsReMat) {
932 // If this is the rematerializable definition MI itself and
933 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000934 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000935 DOUT << "\t\t\t\tErasing re-materlizable def: ";
936 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000937 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000938 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000939 MI->eraseFromParent();
940 break;
941 }
942
943 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000944 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000945 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000946 if (isLoad) {
947 // Try fold loads (from stack slot, constant pool, etc.) into uses.
948 FoldSS = isLoadSS;
949 FoldSlot = LdSlot;
950 }
951 }
952
Evan Chengf2fbca62007-11-12 06:35:08 +0000953 // Scan all of the operands of this instruction rewriting operands
954 // to use NewVReg instead of li.reg as appropriate. We do this for
955 // two reasons:
956 //
957 // 1. If the instr reads the same spilled vreg multiple times, we
958 // want to reuse the NewVReg.
959 // 2. If the instr is a two-addr instruction, we are required to
960 // keep the src/dst regs pinned.
961 //
962 // Keep track of whether we replace a use and/or def so that we can
963 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000964
Evan Cheng81a03822007-11-17 00:40:40 +0000965 HasUse = mop.isUse();
966 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000967 SmallVector<unsigned, 2> Ops;
968 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000969 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000970 const MachineOperand &MOj = MI->getOperand(j);
971 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000972 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000973 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000974 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000975 continue;
976 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000977 Ops.push_back(j);
978 HasUse |= MOj.isUse();
979 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000980 }
981 }
982
Evan Cheng018f9b02007-12-05 03:22:34 +0000983 if (TryFold) {
984 // Do not fold load / store here if we are splitting. We'll find an
985 // optimal point to insert a load / store later.
986 if (!TrySplit) {
987 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
988 Ops, FoldSS, FoldSlot, Reg)) {
989 // Folding the load/store can completely change the instruction in
990 // unpredictable ways, rescan it from the beginning.
991 HasUse = false;
992 HasDef = false;
993 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000994 if (isRemoved(MI))
995 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000996 goto RestartInstruction;
997 }
998 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000999 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001000 }
Evan Cheng6e141fd2007-12-12 23:12:09 +00001001 } else
1002 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001003
1004 // Create a new virtual register for the spill interval.
1005 bool CreatedNewVReg = false;
1006 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001007 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001008 vrm.grow();
1009 CreatedNewVReg = true;
1010 }
1011 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001012 if (mop.isImplicit())
1013 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001014
1015 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001016 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1017 MachineOperand &mopj = MI->getOperand(Ops[j]);
1018 mopj.setReg(NewVReg);
1019 if (mopj.isImplicit())
1020 rewriteImplicitOps(li, MI, NewVReg, vrm);
1021 }
Evan Chengcddbb832007-11-30 21:23:43 +00001022
Evan Cheng81a03822007-11-17 00:40:40 +00001023 if (CreatedNewVReg) {
1024 if (DefIsReMat) {
1025 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001026 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001027 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001028 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001029 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001030 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001031 }
1032 if (!CanDelete || (HasUse && HasDef)) {
1033 // If this is a two-addr instruction then its use operands are
1034 // rematerializable but its def is not. It should be assigned a
1035 // stack slot.
1036 vrm.assignVirt2StackSlot(NewVReg, Slot);
1037 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001038 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001039 vrm.assignVirt2StackSlot(NewVReg, Slot);
1040 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001041 } else if (HasUse && HasDef &&
1042 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1043 // If this interval hasn't been assigned a stack slot (because earlier
1044 // def is a deleted remat def), do it now.
1045 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1046 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001047 }
1048
Evan Cheng313d4b82008-02-23 00:33:04 +00001049 // Re-matting an instruction with virtual register use. Add the
1050 // register as an implicit use on the use MI.
1051 if (DefIsReMat && ImpUse)
1052 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1053
Evan Chengf2fbca62007-11-12 06:35:08 +00001054 // create a new register interval for this spill / remat.
1055 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001056 if (CreatedNewVReg) {
1057 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001058 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001059 if (TrySplit)
1060 vrm.setIsSplitFromReg(NewVReg, li.reg);
1061 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001062
1063 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001064 if (CreatedNewVReg) {
1065 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1066 nI.getNextValue(~0U, 0, VNInfoAllocator));
1067 DOUT << " +" << LR;
1068 nI.addRange(LR);
1069 } else {
1070 // Extend the split live interval to this def / use.
1071 unsigned End = getUseIndex(index)+1;
1072 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1073 nI.getValNumInfo(nI.getNumValNums()-1));
1074 DOUT << " +" << LR;
1075 nI.addRange(LR);
1076 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001077 }
1078 if (HasDef) {
1079 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1080 nI.getNextValue(~0U, 0, VNInfoAllocator));
1081 DOUT << " +" << LR;
1082 nI.addRange(LR);
1083 }
Evan Cheng81a03822007-11-17 00:40:40 +00001084
Evan Chengf2fbca62007-11-12 06:35:08 +00001085 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001086 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001087 DOUT << '\n';
1088 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001089 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001090}
Evan Cheng81a03822007-11-17 00:40:40 +00001091bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001092 const VNInfo *VNI,
1093 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001094 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001095 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1096 unsigned KillIdx = VNI->kills[j];
1097 if (KillIdx > Idx && KillIdx < End)
1098 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001099 }
1100 return false;
1101}
1102
Evan Cheng1953d0c2007-11-29 10:12:14 +00001103static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1104 const VNInfo *VNI = NULL;
1105 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1106 e = li.vni_end(); i != e; ++i)
1107 if ((*i)->def == DefIdx) {
1108 VNI = *i;
1109 break;
1110 }
1111 return VNI;
1112}
1113
Evan Cheng063284c2008-02-21 00:34:19 +00001114/// RewriteInfo - Keep track of machine instrs that will be rewritten
1115/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001116namespace {
1117 struct RewriteInfo {
1118 unsigned Index;
1119 MachineInstr *MI;
1120 bool HasUse;
1121 bool HasDef;
1122 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1123 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1124 };
Evan Cheng063284c2008-02-21 00:34:19 +00001125
Dan Gohman844731a2008-05-13 00:00:25 +00001126 struct RewriteInfoCompare {
1127 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1128 return LHS.Index < RHS.Index;
1129 }
1130 };
1131}
Evan Cheng063284c2008-02-21 00:34:19 +00001132
Evan Chengf2fbca62007-11-12 06:35:08 +00001133void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001134rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001135 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001136 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001137 unsigned Slot, int LdSlot,
1138 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001139 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001140 const TargetRegisterClass* rc,
1141 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001142 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001143 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001144 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001145 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001146 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1147 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001148 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001149 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001150 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001151 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001152 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001153
Evan Cheng063284c2008-02-21 00:34:19 +00001154 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001155 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001156 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001157 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1158 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001159 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001160 MachineOperand &O = ri.getOperand();
1161 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001162 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001163 unsigned index = getInstructionIndex(MI);
1164 if (index < start || index >= end)
1165 continue;
1166 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1167 }
1168 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1169
Evan Cheng313d4b82008-02-23 00:33:04 +00001170 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001171 // Now rewrite the defs and uses.
1172 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1173 RewriteInfo &rwi = RewriteMIs[i];
1174 ++i;
1175 unsigned index = rwi.Index;
1176 bool MIHasUse = rwi.HasUse;
1177 bool MIHasDef = rwi.HasDef;
1178 MachineInstr *MI = rwi.MI;
1179 // If MI def and/or use the same register multiple times, then there
1180 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001181 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001182 while (i != e && RewriteMIs[i].MI == MI) {
1183 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001184 bool isUse = RewriteMIs[i].HasUse;
1185 if (isUse) ++NumUses;
1186 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001187 MIHasDef |= RewriteMIs[i].HasDef;
1188 ++i;
1189 }
Evan Cheng81a03822007-11-17 00:40:40 +00001190 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001191
Evan Cheng0a891ed2008-05-23 23:00:04 +00001192 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001193 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001194 // register interval's spill weight to HUGE_VALF to prevent it from
1195 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001196 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001197 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001198 }
1199
Evan Cheng063284c2008-02-21 00:34:19 +00001200 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001201 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001202 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001203 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001204 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001205 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001206 // One common case:
1207 // x = use
1208 // ...
1209 // ...
1210 // def = ...
1211 // = use
1212 // It's better to start a new interval to avoid artifically
1213 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001214 if (MIHasDef && !MIHasUse) {
1215 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001216 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001217 }
1218 }
Evan Chengcada2452007-11-28 01:28:46 +00001219 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001220
1221 bool IsNew = ThisVReg == 0;
1222 if (IsNew) {
1223 // This ends the previous live interval. If all of its def / use
1224 // can be folded, give it a low spill weight.
1225 if (NewVReg && TrySplit && AllCanFold) {
1226 LiveInterval &nI = getOrCreateInterval(NewVReg);
1227 nI.weight /= 10.0F;
1228 }
1229 AllCanFold = true;
1230 }
1231 NewVReg = ThisVReg;
1232
Evan Cheng81a03822007-11-17 00:40:40 +00001233 bool HasDef = false;
1234 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001235 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001236 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1237 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001238 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001239 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001240 if (!HasDef && !HasUse)
1241 continue;
1242
Evan Cheng018f9b02007-12-05 03:22:34 +00001243 AllCanFold &= CanFold;
1244
Evan Cheng81a03822007-11-17 00:40:40 +00001245 // Update weight of spill interval.
1246 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001247 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001248 // The spill weight is now infinity as it cannot be spilled again.
1249 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001250 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001251 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001252
1253 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001254 if (HasDef) {
1255 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001256 bool HasKill = false;
1257 if (!HasUse)
1258 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1259 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001260 // If this is a two-address code, then this index starts a new VNInfo.
1261 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001262 if (VNI)
1263 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1264 }
Evan Chenge3110d02007-12-01 04:42:39 +00001265 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1266 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001267 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001268 if (SII == SpillIdxes.end()) {
1269 std::vector<SRInfo> S;
1270 S.push_back(SRInfo(index, NewVReg, true));
1271 SpillIdxes.insert(std::make_pair(MBBId, S));
1272 } else if (SII->second.back().vreg != NewVReg) {
1273 SII->second.push_back(SRInfo(index, NewVReg, true));
1274 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001275 // If there is an earlier def and this is a two-address
1276 // instruction, then it's not possible to fold the store (which
1277 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001278 SRInfo &Info = SII->second.back();
1279 Info.index = index;
1280 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001281 }
1282 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001283 } else if (SII != SpillIdxes.end() &&
1284 SII->second.back().vreg == NewVReg &&
1285 (int)index > SII->second.back().index) {
1286 // There is an earlier def that's not killed (must be two-address).
1287 // The spill is no longer needed.
1288 SII->second.pop_back();
1289 if (SII->second.empty()) {
1290 SpillIdxes.erase(MBBId);
1291 SpillMBBs.reset(MBBId);
1292 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001293 }
1294 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001295 }
1296
1297 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001298 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001299 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001300 if (SII != SpillIdxes.end() &&
1301 SII->second.back().vreg == NewVReg &&
1302 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001303 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001304 SII->second.back().canFold = false;
1305 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001306 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001307 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001308 // If we are splitting live intervals, only fold if it's the first
1309 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001310 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001311 else if (IsNew) {
1312 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001313 if (RII == RestoreIdxes.end()) {
1314 std::vector<SRInfo> Infos;
1315 Infos.push_back(SRInfo(index, NewVReg, true));
1316 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1317 } else {
1318 RII->second.push_back(SRInfo(index, NewVReg, true));
1319 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001320 RestoreMBBs.set(MBBId);
1321 }
1322 }
1323
1324 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001325 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001326 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001327 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001328
1329 if (NewVReg && TrySplit && AllCanFold) {
1330 // If all of its def / use can be folded, give it a low spill weight.
1331 LiveInterval &nI = getOrCreateInterval(NewVReg);
1332 nI.weight /= 10.0F;
1333 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001334}
1335
Evan Cheng1953d0c2007-11-29 10:12:14 +00001336bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1337 BitVector &RestoreMBBs,
1338 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1339 if (!RestoreMBBs[Id])
1340 return false;
1341 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1342 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1343 if (Restores[i].index == index &&
1344 Restores[i].vreg == vr &&
1345 Restores[i].canFold)
1346 return true;
1347 return false;
1348}
1349
1350void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1351 BitVector &RestoreMBBs,
1352 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1353 if (!RestoreMBBs[Id])
1354 return;
1355 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1356 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1357 if (Restores[i].index == index && Restores[i].vreg)
1358 Restores[i].index = -1;
1359}
Evan Cheng81a03822007-11-17 00:40:40 +00001360
Evan Cheng4cce6b42008-04-11 17:53:36 +00001361/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1362/// spilled and create empty intervals for their uses.
1363void
1364LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1365 const TargetRegisterClass* rc,
1366 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001367 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1368 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001369 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001370 MachineInstr *MI = &*ri;
1371 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001372 if (O.isDef()) {
1373 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1374 "Register def was not rewritten?");
1375 RemoveMachineInstrFromMaps(MI);
1376 vrm.RemoveMachineInstrFromMaps(MI);
1377 MI->eraseFromParent();
1378 } else {
1379 // This must be an use of an implicit_def so it's not part of the live
1380 // interval. Create a new empty live interval for it.
1381 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1382 unsigned NewVReg = mri_->createVirtualRegister(rc);
1383 vrm.grow();
1384 vrm.setIsImplicitlyDefined(NewVReg);
1385 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1386 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1387 MachineOperand &MO = MI->getOperand(i);
1388 if (MO.isReg() && MO.getReg() == li.reg)
1389 MO.setReg(NewVReg);
1390 }
1391 }
Evan Cheng419852c2008-04-03 16:39:43 +00001392 }
1393}
1394
Evan Cheng81a03822007-11-17 00:40:40 +00001395
Evan Chengf2fbca62007-11-12 06:35:08 +00001396std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001397addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001398 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001399 // Since this is called after the analysis is done we don't know if
1400 // LiveVariables is available
1401 lv_ = getAnalysisToUpdate<LiveVariables>();
1402
1403 assert(li.weight != HUGE_VALF &&
1404 "attempt to spill already spilled interval!");
1405
1406 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001407 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001408 DOUT << '\n';
1409
Evan Cheng81a03822007-11-17 00:40:40 +00001410 // Each bit specify whether it a spill is required in the MBB.
1411 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001412 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001413 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001414 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1415 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001416 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001417 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001418
1419 unsigned NumValNums = li.getNumValNums();
1420 SmallVector<MachineInstr*, 4> ReMatDefs;
1421 ReMatDefs.resize(NumValNums, NULL);
1422 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1423 ReMatOrigDefs.resize(NumValNums, NULL);
1424 SmallVector<int, 4> ReMatIds;
1425 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1426 BitVector ReMatDelete(NumValNums);
1427 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1428
Evan Cheng81a03822007-11-17 00:40:40 +00001429 // Spilling a split live interval. It cannot be split any further. Also,
1430 // it's also guaranteed to be a single val# / range interval.
1431 if (vrm.getPreSplitReg(li.reg)) {
1432 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001433 // Unset the split kill marker on the last use.
1434 unsigned KillIdx = vrm.getKillPoint(li.reg);
1435 if (KillIdx) {
1436 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1437 assert(KillMI && "Last use disappeared?");
1438 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1439 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001440 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001441 }
Evan Chengadf85902007-12-05 09:51:10 +00001442 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001443 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1444 Slot = vrm.getStackSlot(li.reg);
1445 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1446 MachineInstr *ReMatDefMI = DefIsReMat ?
1447 vrm.getReMaterializedMI(li.reg) : NULL;
1448 int LdSlot = 0;
1449 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1450 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001451 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001452 bool IsFirstRange = true;
1453 for (LiveInterval::Ranges::const_iterator
1454 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1455 // If this is a split live interval with multiple ranges, it means there
1456 // are two-address instructions that re-defined the value. Only the
1457 // first def can be rematerialized!
1458 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001459 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001460 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1461 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001462 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001463 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001464 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001465 } else {
1466 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1467 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001468 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001469 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001470 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001471 }
1472 IsFirstRange = false;
1473 }
Evan Cheng419852c2008-04-03 16:39:43 +00001474
Evan Cheng4cce6b42008-04-11 17:53:36 +00001475 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001476 return NewLIs;
1477 }
1478
1479 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001480 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1481 TrySplit = false;
1482 if (TrySplit)
1483 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001484 bool NeedStackSlot = false;
1485 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1486 i != e; ++i) {
1487 const VNInfo *VNI = *i;
1488 unsigned VN = VNI->id;
1489 unsigned DefIdx = VNI->def;
1490 if (DefIdx == ~1U)
1491 continue; // Dead val#.
1492 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001493 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1494 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001495 bool dummy;
1496 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001497 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001498 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001499 // Original def may be modified so we have to make a copy here. vrm must
1500 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001501 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001502
1503 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001504 if (VNI->hasPHIKill) {
1505 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001506 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001507 CanDelete = false;
1508 // Need a stack slot if there is any live range where uses cannot be
1509 // rematerialized.
1510 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001511 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001512 if (CanDelete)
1513 ReMatDelete.set(VN);
1514 } else {
1515 // Need a stack slot if there is any live range where uses cannot be
1516 // rematerialized.
1517 NeedStackSlot = true;
1518 }
1519 }
1520
1521 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001522 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001523 Slot = vrm.assignVirt2StackSlot(li.reg);
1524
1525 // Create new intervals and rewrite defs and uses.
1526 for (LiveInterval::Ranges::const_iterator
1527 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001528 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1529 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1530 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001531 bool CanDelete = ReMatDelete[I->valno->id];
1532 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001533 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001534 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001535 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001536 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001537 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001538 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001539 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001540 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001541 }
1542
Evan Cheng0cbb1162007-11-29 01:06:25 +00001543 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001544 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001545 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001546 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001547 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001548
Evan Chengb50bb8c2007-12-05 08:16:32 +00001549 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001550 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001551 if (NeedStackSlot) {
1552 int Id = SpillMBBs.find_first();
1553 while (Id != -1) {
1554 std::vector<SRInfo> &spills = SpillIdxes[Id];
1555 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1556 int index = spills[i].index;
1557 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001558 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001559 bool isReMat = vrm.isReMaterialized(VReg);
1560 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001561 bool CanFold = false;
1562 bool FoundUse = false;
1563 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001564 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001565 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001566 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1567 MachineOperand &MO = MI->getOperand(j);
1568 if (!MO.isRegister() || MO.getReg() != VReg)
1569 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001570
1571 Ops.push_back(j);
1572 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001573 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001574 if (isReMat ||
1575 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1576 RestoreMBBs, RestoreIdxes))) {
1577 // MI has two-address uses of the same register. If the use
1578 // isn't the first and only use in the BB, then we can't fold
1579 // it. FIXME: Move this to rewriteInstructionsForSpills.
1580 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001581 break;
1582 }
Evan Chengaee4af62007-12-02 08:30:39 +00001583 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001584 }
1585 }
1586 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001587 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001588 if (CanFold && !Ops.empty()) {
1589 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001590 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001591 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001592 // Also folded uses, do not issue a load.
1593 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001594 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1595 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001596 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001597 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001598 }
1599
Evan Cheng7e073ba2008-04-09 20:57:25 +00001600 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001601 if (!Folded) {
1602 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1603 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001604 if (!MI->registerDefIsDead(nI.reg))
1605 // No need to spill a dead def.
1606 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001607 if (isKill)
1608 AddedKill.insert(&nI);
1609 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001610 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001611 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001612 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001613 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001614
Evan Cheng1953d0c2007-11-29 10:12:14 +00001615 int Id = RestoreMBBs.find_first();
1616 while (Id != -1) {
1617 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1618 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1619 int index = restores[i].index;
1620 if (index == -1)
1621 continue;
1622 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001623 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001624 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001625 bool CanFold = false;
1626 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001627 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001628 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001629 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1630 MachineOperand &MO = MI->getOperand(j);
1631 if (!MO.isRegister() || MO.getReg() != VReg)
1632 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001633
Evan Cheng0cbb1162007-11-29 01:06:25 +00001634 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001635 // If this restore were to be folded, it would have been folded
1636 // already.
1637 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001638 break;
1639 }
Evan Chengaee4af62007-12-02 08:30:39 +00001640 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001641 }
1642 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001643
1644 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001645 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001646 if (CanFold && !Ops.empty()) {
1647 if (!vrm.isReMaterialized(VReg))
1648 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1649 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001650 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1651 int LdSlot = 0;
1652 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1653 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001654 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001655 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1656 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001657 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1658 if (ImpUse) {
1659 // Re-matting an instruction with virtual register use. Add the
1660 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001661 // interval's spill weight to HUGE_VALF to prevent it from being
1662 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001663 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001664 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001665 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1666 }
Evan Chengaee4af62007-12-02 08:30:39 +00001667 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001668 }
1669 // If folding is not possible / failed, then tell the spiller to issue a
1670 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001671 if (Folded)
1672 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001673 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001674 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001675 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001676 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001677 }
1678
Evan Chengb50bb8c2007-12-05 08:16:32 +00001679 // Finalize intervals: add kills, finalize spill weights, and filter out
1680 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001681 std::vector<LiveInterval*> RetNewLIs;
1682 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1683 LiveInterval *LI = NewLIs[i];
1684 if (!LI->empty()) {
1685 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001686 if (!AddedKill.count(LI)) {
1687 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001688 unsigned LastUseIdx = getBaseIndex(LR->end);
1689 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001690 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001691 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001692 if (LastUse->getOperand(UseIdx).isImplicit() ||
1693 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001694 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001695 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001696 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001697 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001698 RetNewLIs.push_back(LI);
1699 }
1700 }
Evan Cheng81a03822007-11-17 00:40:40 +00001701
Evan Cheng4cce6b42008-04-11 17:53:36 +00001702 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001703 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001704}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001705
1706/// hasAllocatableSuperReg - Return true if the specified physical register has
1707/// any super register that's allocatable.
1708bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1709 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1710 if (allocatableRegs_[*AS] && hasInterval(*AS))
1711 return true;
1712 return false;
1713}
1714
1715/// getRepresentativeReg - Find the largest super register of the specified
1716/// physical register.
1717unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1718 // Find the largest super-register that is allocatable.
1719 unsigned BestReg = Reg;
1720 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1721 unsigned SuperReg = *AS;
1722 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1723 BestReg = SuperReg;
1724 break;
1725 }
1726 }
1727 return BestReg;
1728}
1729
1730/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1731/// specified interval that conflicts with the specified physical register.
1732unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1733 unsigned PhysReg) const {
1734 unsigned NumConflicts = 0;
1735 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1736 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1737 E = mri_->reg_end(); I != E; ++I) {
1738 MachineOperand &O = I.getOperand();
1739 MachineInstr *MI = O.getParent();
1740 unsigned Index = getInstructionIndex(MI);
1741 if (pli.liveAt(Index))
1742 ++NumConflicts;
1743 }
1744 return NumConflicts;
1745}
1746
1747/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1748/// around all defs and uses of the specified interval.
1749void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1750 unsigned PhysReg, VirtRegMap &vrm) {
1751 unsigned SpillReg = getRepresentativeReg(PhysReg);
1752
1753 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1754 // If there are registers which alias PhysReg, but which are not a
1755 // sub-register of the chosen representative super register. Assert
1756 // since we can't handle it yet.
1757 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1758 tri_->isSuperRegister(*AS, SpillReg));
1759
1760 LiveInterval &pli = getInterval(SpillReg);
1761 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1762 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1763 E = mri_->reg_end(); I != E; ++I) {
1764 MachineOperand &O = I.getOperand();
1765 MachineInstr *MI = O.getParent();
1766 if (SeenMIs.count(MI))
1767 continue;
1768 SeenMIs.insert(MI);
1769 unsigned Index = getInstructionIndex(MI);
1770 if (pli.liveAt(Index)) {
1771 vrm.addEmergencySpill(SpillReg, MI);
1772 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1773 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1774 if (!hasInterval(*AS))
1775 continue;
1776 LiveInterval &spli = getInterval(*AS);
1777 if (spli.liveAt(Index))
1778 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1779 }
1780 }
1781 }
1782}