blob: 228c0a92a0ef29e472ec3daa5d9020ebd3accea0 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000063}
64
Chris Lattnerf7da2c72006-08-24 22:43:55 +000065void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000066 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000067 mi2iMap_.clear();
68 i2miMap_.clear();
69 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000070 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
71 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000072 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
73 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000074}
75
Owen Anderson4b5b2092008-05-29 18:15:49 +000076#include <iostream>
77
Owen Anderson80b3ce62008-05-28 20:54:50 +000078void LiveIntervals::computeNumbering() {
79 Index2MiMap OldI2MI = i2miMap_;
80
81 Idx2MBBMap.clear();
82 MBB2IdxMap.clear();
83 mi2iMap_.clear();
84 i2miMap_.clear();
85
Chris Lattner428b92e2006-09-15 03:57:23 +000086 // Number MachineInstrs and MachineBasicBlocks.
87 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000088 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000089
90 unsigned MIIndex = 0;
91 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
92 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000093 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000094
Chris Lattner428b92e2006-09-15 03:57:23 +000095 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
96 I != E; ++I) {
97 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000099 i2miMap_.push_back(I);
100 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000101 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000102
103 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000104 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
105 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
106 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000107 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000109 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000110
111 if (!OldI2MI.empty())
112 for (iterator I = begin(), E = end(); I != E; ++I)
113 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
114 LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000115 unsigned offset = LI->start % InstrSlots::NUM;
116 LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
117
118 if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
119 // FIXME: Not correct when the instruction at LI->end has
120 // been removed
121 offset = LI->end % InstrSlots::NUM;
122 LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
123 } else {
124 LI->end = i2miMap_.size() * InstrSlots::NUM;
125 }
Owen Anderson745825f42008-05-28 22:40:08 +0000126
127 VNInfo* vni = LI->valno;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000128 offset = vni->def % InstrSlots::NUM;
129 vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
Owen Anderson745825f42008-05-28 22:40:08 +0000130
Owen Anderson4b5b2092008-05-29 18:15:49 +0000131 for (size_t i = 0; i < vni->kills.size(); ++i) {
132 offset = vni->kills[i] % InstrSlots::NUM;
133 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
134 offset;
135 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000136 }
137}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000138
Owen Anderson80b3ce62008-05-28 20:54:50 +0000139/// runOnMachineFunction - Register allocate the whole function
140///
141bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
142 mf_ = &fn;
143 mri_ = &mf_->getRegInfo();
144 tm_ = &fn.getTarget();
145 tri_ = tm_->getRegisterInfo();
146 tii_ = tm_->getInstrInfo();
147 lv_ = &getAnalysis<LiveVariables>();
148 allocatableRegs_ = tri_->getAllocatableSet(fn);
149
150 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000151 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000152
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000153 numIntervals += getNumIntervals();
154
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000155 DOUT << "********** INTERVALS **********\n";
156 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000157 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000158 DOUT << "\n";
159 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000160
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000161 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000162 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000163 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000164}
165
Chris Lattner70ca3582004-09-30 15:59:17 +0000166/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000167void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000168 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000169 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000170 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000171 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000172 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000173
174 O << "********** MACHINEINSTRS **********\n";
175 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
176 mbbi != mbbe; ++mbbi) {
177 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
178 for (MachineBasicBlock::iterator mii = mbbi->begin(),
179 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000180 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000181 }
182 }
183}
184
Evan Chengc92da382007-11-03 07:20:12 +0000185/// conflictsWithPhysRegDef - Returns true if the specified register
186/// is defined during the duration of the specified interval.
187bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
188 VirtRegMap &vrm, unsigned reg) {
189 for (LiveInterval::Ranges::const_iterator
190 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
191 for (unsigned index = getBaseIndex(I->start),
192 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
193 index += InstrSlots::NUM) {
194 // skip deleted instructions
195 while (index != end && !getInstructionFromIndex(index))
196 index += InstrSlots::NUM;
197 if (index == end) break;
198
199 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000200 unsigned SrcReg, DstReg;
201 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
202 if (SrcReg == li.reg || DstReg == li.reg)
203 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000204 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
205 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000206 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000207 continue;
208 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000209 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000210 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000211 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000212 if (!vrm.hasPhys(PhysReg))
213 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000214 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000215 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000216 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000217 return true;
218 }
219 }
220 }
221
222 return false;
223}
224
Evan Cheng549f27d32007-08-13 23:45:17 +0000225void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000226 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000227 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000228 else
229 cerr << "%reg" << reg;
230}
231
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000232void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000233 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000234 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000235 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000236 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000237 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000238
Evan Cheng419852c2008-04-03 16:39:43 +0000239 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
240 DOUT << "is a implicit_def\n";
241 return;
242 }
243
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000244 // Virtual registers may be defined multiple times (due to phi
245 // elimination and 2-addr elimination). Much of what we do only has to be
246 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000247 // time we see a vreg.
248 if (interval.empty()) {
249 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000250 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000251 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000252 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000253 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000254 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000255 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000256 tii_->isMoveInstr(*mi, SrcReg, DstReg))
257 CopyMI = mi;
258 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000259
260 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000261
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 // Loop over all of the blocks that the vreg is defined in. There are
263 // two cases we have to handle here. The most common case is a vreg
264 // whose lifetime is contained within a basic block. In this case there
265 // will be a single kill, in MBB, which comes after the definition.
266 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
267 // FIXME: what about dead vars?
268 unsigned killIdx;
269 if (vi.Kills[0] != mi)
270 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
271 else
272 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000273
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000274 // If the kill happens after the definition, we have an intra-block
275 // live range.
276 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000277 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000278 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000279 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000281 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000282 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 return;
284 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000285 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000286
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 // The other case we handle is when a virtual register lives to the end
288 // of the defining block, potentially live across some blocks, then is
289 // live into some number of blocks, but gets killed. Start by adding a
290 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000291 LiveRange NewLR(defIndex,
292 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000294 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 interval.addRange(NewLR);
296
297 // Iterate over all of the blocks that the variable is completely
298 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
299 // live interval.
300 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
301 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000302 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
303 if (!MBB->empty()) {
304 LiveRange LR(getMBBStartIdx(i),
305 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000306 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000308 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 }
310 }
311 }
312
313 // Finally, this virtual register is live from the start of any killing
314 // block to the 'use' slot of the killing instruction.
315 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
316 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000317 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000318 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000319 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000321 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000322 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000323 }
324
325 } else {
326 // If this is the second time we see a virtual register definition, it
327 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000328 // the result of two address elimination, then the vreg is one of the
329 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000330 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000331 // If this is a two-address definition, then we have already processed
332 // the live range. The only problem is that we didn't realize there
333 // are actually two values in the live interval. Because of this we
334 // need to take the LiveRegion that defines this register and split it
335 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000336 assert(interval.containsOneValue());
337 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000338 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000339
Evan Cheng4f8ff162007-08-11 00:59:19 +0000340 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000341 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000342
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000343 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000344 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000346
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000347 // Two-address vregs should always only be redefined once. This means
348 // that at this point, there should be exactly one value number in it.
349 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
350
Chris Lattner91725b72006-08-31 05:54:43 +0000351 // The new value number (#1) is defined by the instruction we claimed
352 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000353 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
354 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000355
Chris Lattner91725b72006-08-31 05:54:43 +0000356 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000357 OldValNo->def = RedefIndex;
358 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000359
360 // Add the new live interval which replaces the range for the input copy.
361 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000362 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000363 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000364 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365
366 // If this redefinition is dead, we need to add a dummy unit live
367 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000368 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000369 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000370
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000371 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000372 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373
374 } else {
375 // Otherwise, this must be because of phi elimination. If this is the
376 // first redefinition of the vreg that we have seen, go back and change
377 // the live range in the PHI block to be a different value number.
378 if (interval.containsOneValue()) {
379 assert(vi.Kills.size() == 1 &&
380 "PHI elimination vreg should have one kill, the PHI itself!");
381
382 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000383 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000384 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000385 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000386 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000387 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000388 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000390 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000391 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000392
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000393 // Replace the interval with one of a NEW value number. Note that this
394 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000395 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000396 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000397 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000398 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000399 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000400 }
401
402 // In the case of PHI elimination, each variable definition is only
403 // live until the end of the block. We've already taken care of the
404 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000405 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000406
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000407 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000408 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000409 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000410 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000411 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000412 tii_->isMoveInstr(*mi, SrcReg, DstReg))
413 CopyMI = mi;
414 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000415
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000416 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000417 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000418 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000419 interval.addKill(ValNo, killIndex);
420 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000421 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 }
423 }
424
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000425 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000426}
427
Chris Lattnerf35fef72004-07-23 21:24:19 +0000428void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000429 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000430 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000431 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000432 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000433 // A physical register cannot be live across basic block, so its
434 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000435 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000436
Chris Lattner6b128bd2006-09-03 08:07:11 +0000437 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000438 unsigned start = getDefIndex(baseIndex);
439 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000440
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000441 // If it is not used after definition, it is considered dead at
442 // the instruction defining it. Hence its interval is:
443 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000444 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000445 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000446 end = getDefIndex(start) + 1;
447 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000448 }
449
450 // If it is not dead on definition, it must be killed by a
451 // subsequent instruction. Hence its interval is:
452 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000453 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000454 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000455 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000456 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000457 end = getUseIndex(baseIndex) + 1;
458 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000459 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000460 // Another instruction redefines the register before it is ever read.
461 // Then the register is essentially dead at the instruction that defines
462 // it. Hence its interval is:
463 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000464 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000465 end = getDefIndex(start) + 1;
466 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000467 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000468 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000469
470 // The only case we should have a dead physreg here without a killing or
471 // instruction where we know it's dead is if it is live-in to the function
472 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000473 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000474 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000475
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000476exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000477 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000478
Evan Cheng24a3cc42007-04-25 07:30:23 +0000479 // Already exists? Extend old live interval.
480 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000481 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000482 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000483 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000484 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000485 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000486 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000487}
488
Chris Lattnerf35fef72004-07-23 21:24:19 +0000489void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
490 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000491 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000492 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000493 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000494 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000495 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000496 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000497 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000498 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000499 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000500 tii_->isMoveInstr(*MI, SrcReg, DstReg))
501 CopyMI = MI;
502 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000503 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000504 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000505 // If MI also modifies the sub-register explicitly, avoid processing it
506 // more than once. Do not pass in TRI here so it checks for exact match.
507 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000508 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000509 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000510}
511
Evan Chengb371f452007-02-19 21:49:54 +0000512void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000513 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000514 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000515 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
516
517 // Look for kills, if it reaches a def before it's killed, then it shouldn't
518 // be considered a livein.
519 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000520 unsigned baseIndex = MIIdx;
521 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000522 unsigned end = start;
523 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000524 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000525 DOUT << " killed";
526 end = getUseIndex(baseIndex) + 1;
527 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000528 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000529 // Another instruction redefines the register before it is ever read.
530 // Then the register is essentially dead at the instruction that defines
531 // it. Hence its interval is:
532 // [defSlot(def), defSlot(def)+1)
533 DOUT << " dead";
534 end = getDefIndex(start) + 1;
535 goto exit;
536 }
537
538 baseIndex += InstrSlots::NUM;
539 ++mi;
540 }
541
542exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000543 // Live-in register might not be used at all.
544 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000545 if (isAlias) {
546 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000547 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000548 } else {
549 DOUT << " live through";
550 end = baseIndex;
551 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000552 }
553
Evan Chengf3bb2e62007-09-05 21:46:51 +0000554 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000555 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000556 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000557 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000558}
559
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000560/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000561/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000562/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000563/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000564void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000565 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
566 << "********** Function: "
567 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000568 // Track the index of the current machine instr.
569 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000570 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
571 MBBI != E; ++MBBI) {
572 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000573 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000574
Chris Lattner428b92e2006-09-15 03:57:23 +0000575 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000576
Dan Gohmancb406c22007-10-03 19:26:29 +0000577 // Create intervals for live-ins to this BB first.
578 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
579 LE = MBB->livein_end(); LI != LE; ++LI) {
580 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
581 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000582 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000583 if (!hasInterval(*AS))
584 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
585 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000586 }
587
Chris Lattner428b92e2006-09-15 03:57:23 +0000588 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000589 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000590
Evan Cheng438f7bc2006-11-10 08:43:01 +0000591 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000592 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
593 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000595 if (MO.isRegister() && MO.getReg() && MO.isDef())
596 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000598
599 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000600 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000601 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000602}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000603
Evan Cheng4ca980e2007-10-17 02:10:22 +0000604bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000605 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000606 std::vector<IdxMBBPair>::const_iterator I =
607 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
608
609 bool ResVal = false;
610 while (I != Idx2MBBMap.end()) {
611 if (LR.end <= I->first)
612 break;
613 MBBs.push_back(I->second);
614 ResVal = true;
615 ++I;
616 }
617 return ResVal;
618}
619
620
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000621LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000622 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000623 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000624 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000625}
Evan Chengf2fbca62007-11-12 06:35:08 +0000626
Evan Chengc8d044e2008-02-15 18:24:29 +0000627/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
628/// copy field and returns the source register that defines it.
629unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
630 if (!VNI->copy)
631 return 0;
632
633 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
634 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000635 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
636 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000637 unsigned SrcReg, DstReg;
638 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
639 return SrcReg;
640 assert(0 && "Unrecognized copy instruction!");
641 return 0;
642}
Evan Chengf2fbca62007-11-12 06:35:08 +0000643
644//===----------------------------------------------------------------------===//
645// Register allocator hooks.
646//
647
Evan Chengd70dbb52008-02-22 09:24:50 +0000648/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
649/// allow one) virtual register operand, then its uses are implicitly using
650/// the register. Returns the virtual register.
651unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
652 MachineInstr *MI) const {
653 unsigned RegOp = 0;
654 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
655 MachineOperand &MO = MI->getOperand(i);
656 if (!MO.isRegister() || !MO.isUse())
657 continue;
658 unsigned Reg = MO.getReg();
659 if (Reg == 0 || Reg == li.reg)
660 continue;
661 // FIXME: For now, only remat MI with at most one register operand.
662 assert(!RegOp &&
663 "Can't rematerialize instruction with multiple register operand!");
664 RegOp = MO.getReg();
665 break;
666 }
667 return RegOp;
668}
669
670/// isValNoAvailableAt - Return true if the val# of the specified interval
671/// which reaches the given instruction also reaches the specified use index.
672bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
673 unsigned UseIdx) const {
674 unsigned Index = getInstructionIndex(MI);
675 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
676 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
677 return UI != li.end() && UI->valno == ValNo;
678}
679
Evan Chengf2fbca62007-11-12 06:35:08 +0000680/// isReMaterializable - Returns true if the definition MI of the specified
681/// val# of the specified interval is re-materializable.
682bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000683 const VNInfo *ValNo, MachineInstr *MI,
684 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000685 if (DisableReMat)
686 return false;
687
Evan Cheng5ef3a042007-12-06 00:01:56 +0000688 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000689 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000690 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000691
692 int FrameIdx = 0;
693 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000694 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000695 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
696 // this but remember this is not safe to fold into a two-address
697 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000698 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000699 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000700
Evan Chengd70dbb52008-02-22 09:24:50 +0000701 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000702 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000703 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000704
705 unsigned ImpUse = getReMatImplicitUse(li, MI);
706 if (ImpUse) {
707 const LiveInterval &ImpLi = getInterval(ImpUse);
708 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
709 re = mri_->use_end(); ri != re; ++ri) {
710 MachineInstr *UseMI = &*ri;
711 unsigned UseIdx = getInstructionIndex(UseMI);
712 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
713 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000714 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000715 return false;
716 }
717 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000718 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000719 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000720
Evan Chengdd3465e2008-02-23 01:44:27 +0000721 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000722}
723
724/// isReMaterializable - Returns true if every definition of MI of every
725/// val# of the specified interval is re-materializable.
726bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
727 isLoad = false;
728 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
729 i != e; ++i) {
730 const VNInfo *VNI = *i;
731 unsigned DefIdx = VNI->def;
732 if (DefIdx == ~1U)
733 continue; // Dead val#.
734 // Is the def for the val# rematerializable?
735 if (DefIdx == ~0u)
736 return false;
737 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
738 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000739 if (!ReMatDefMI ||
740 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000741 return false;
742 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000743 }
744 return true;
745}
746
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000747/// FilterFoldedOps - Filter out two-address use operands. Return
748/// true if it finds any issue with the operands that ought to prevent
749/// folding.
750static bool FilterFoldedOps(MachineInstr *MI,
751 SmallVector<unsigned, 2> &Ops,
752 unsigned &MRInfo,
753 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000754 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000755
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000756 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000757 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
758 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000759 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000760 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000761 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000762 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000763 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000764 MRInfo |= (unsigned)VirtRegMap::isMod;
765 else {
766 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000767 if (!MO.isImplicit() &&
768 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000769 MRInfo = VirtRegMap::isModRef;
770 continue;
771 }
772 MRInfo |= (unsigned)VirtRegMap::isRef;
773 }
774 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000775 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000776 return false;
777}
778
779
780/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
781/// slot / to reg or any rematerialized load into ith operand of specified
782/// MI. If it is successul, MI is updated with the newly created MI and
783/// returns true.
784bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
785 VirtRegMap &vrm, MachineInstr *DefMI,
786 unsigned InstrIdx,
787 SmallVector<unsigned, 2> &Ops,
788 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000789 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000790 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000791 RemoveMachineInstrFromMaps(MI);
792 vrm.RemoveMachineInstrFromMaps(MI);
793 MI->eraseFromParent();
794 ++numFolds;
795 return true;
796 }
797
798 // Filter the list of operand indexes that are to be folded. Abort if
799 // any operand will prevent folding.
800 unsigned MRInfo = 0;
801 SmallVector<unsigned, 2> FoldOps;
802 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
803 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000804
Evan Cheng427f4c12008-03-31 23:19:51 +0000805 // The only time it's safe to fold into a two address instruction is when
806 // it's folding reload and spill from / into a spill stack slot.
807 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000808 return false;
809
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000810 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
811 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000812 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000813 // Remember this instruction uses the spill slot.
814 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
815
Evan Chengf2fbca62007-11-12 06:35:08 +0000816 // Attempt to fold the memory reference into the instruction. If
817 // we can do this, we don't need to insert spill code.
818 if (lv_)
819 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000820 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000821 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000822 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000823 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000824 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000825 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000826 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000827 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000828 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000829 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
830 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000831 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000832 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000833 return true;
834 }
835 return false;
836}
837
Evan Cheng018f9b02007-12-05 03:22:34 +0000838/// canFoldMemoryOperand - Returns true if the specified load / store
839/// folding is possible.
840bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000841 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000842 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000843 // Filter the list of operand indexes that are to be folded. Abort if
844 // any operand will prevent folding.
845 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000846 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000847 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
848 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000849
Evan Cheng3c75ba82008-04-01 21:37:32 +0000850 // It's only legal to remat for a use, not a def.
851 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000852 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000853
Evan Chengd70dbb52008-02-22 09:24:50 +0000854 return tii_->canFoldMemoryOperand(MI, FoldOps);
855}
856
Evan Cheng81a03822007-11-17 00:40:40 +0000857bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
858 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
859 for (LiveInterval::Ranges::const_iterator
860 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
861 std::vector<IdxMBBPair>::const_iterator II =
862 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
863 if (II == Idx2MBBMap.end())
864 continue;
865 if (I->end > II->first) // crossing a MBB.
866 return false;
867 MBBs.insert(II->second);
868 if (MBBs.size() > 1)
869 return false;
870 }
871 return true;
872}
873
Evan Chengd70dbb52008-02-22 09:24:50 +0000874/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
875/// interval on to-be re-materialized operands of MI) with new register.
876void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
877 MachineInstr *MI, unsigned NewVReg,
878 VirtRegMap &vrm) {
879 // There is an implicit use. That means one of the other operand is
880 // being remat'ed and the remat'ed instruction has li.reg as an
881 // use operand. Make sure we rewrite that as well.
882 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
883 MachineOperand &MO = MI->getOperand(i);
884 if (!MO.isRegister())
885 continue;
886 unsigned Reg = MO.getReg();
887 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
888 continue;
889 if (!vrm.isReMaterialized(Reg))
890 continue;
891 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000892 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
893 if (UseMO)
894 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000895 }
896}
897
Evan Chengf2fbca62007-11-12 06:35:08 +0000898/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
899/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000900bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000901rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
902 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000903 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000904 unsigned Slot, int LdSlot,
905 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000906 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000907 const TargetRegisterClass* rc,
908 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000909 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000910 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000911 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000912 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000913 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000914 RestartInstruction:
915 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
916 MachineOperand& mop = MI->getOperand(i);
917 if (!mop.isRegister())
918 continue;
919 unsigned Reg = mop.getReg();
920 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000921 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000922 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000923 if (Reg != li.reg)
924 continue;
925
926 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000927 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000928 int FoldSlot = Slot;
929 if (DefIsReMat) {
930 // If this is the rematerializable definition MI itself and
931 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000932 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000933 DOUT << "\t\t\t\tErasing re-materlizable def: ";
934 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000935 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000936 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000937 MI->eraseFromParent();
938 break;
939 }
940
941 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000942 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000943 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000944 if (isLoad) {
945 // Try fold loads (from stack slot, constant pool, etc.) into uses.
946 FoldSS = isLoadSS;
947 FoldSlot = LdSlot;
948 }
949 }
950
Evan Chengf2fbca62007-11-12 06:35:08 +0000951 // Scan all of the operands of this instruction rewriting operands
952 // to use NewVReg instead of li.reg as appropriate. We do this for
953 // two reasons:
954 //
955 // 1. If the instr reads the same spilled vreg multiple times, we
956 // want to reuse the NewVReg.
957 // 2. If the instr is a two-addr instruction, we are required to
958 // keep the src/dst regs pinned.
959 //
960 // Keep track of whether we replace a use and/or def so that we can
961 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000962
Evan Cheng81a03822007-11-17 00:40:40 +0000963 HasUse = mop.isUse();
964 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000965 SmallVector<unsigned, 2> Ops;
966 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000967 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000968 const MachineOperand &MOj = MI->getOperand(j);
969 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000970 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000971 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000972 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000973 continue;
974 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000975 Ops.push_back(j);
976 HasUse |= MOj.isUse();
977 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000978 }
979 }
980
Evan Cheng018f9b02007-12-05 03:22:34 +0000981 if (TryFold) {
982 // Do not fold load / store here if we are splitting. We'll find an
983 // optimal point to insert a load / store later.
984 if (!TrySplit) {
985 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
986 Ops, FoldSS, FoldSlot, Reg)) {
987 // Folding the load/store can completely change the instruction in
988 // unpredictable ways, rescan it from the beginning.
989 HasUse = false;
990 HasDef = false;
991 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000992 if (isRemoved(MI))
993 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000994 goto RestartInstruction;
995 }
996 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000997 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000998 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000999 } else
1000 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001001
1002 // Create a new virtual register for the spill interval.
1003 bool CreatedNewVReg = false;
1004 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001005 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001006 vrm.grow();
1007 CreatedNewVReg = true;
1008 }
1009 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001010 if (mop.isImplicit())
1011 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001012
1013 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001014 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1015 MachineOperand &mopj = MI->getOperand(Ops[j]);
1016 mopj.setReg(NewVReg);
1017 if (mopj.isImplicit())
1018 rewriteImplicitOps(li, MI, NewVReg, vrm);
1019 }
Evan Chengcddbb832007-11-30 21:23:43 +00001020
Evan Cheng81a03822007-11-17 00:40:40 +00001021 if (CreatedNewVReg) {
1022 if (DefIsReMat) {
1023 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001024 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001025 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001026 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001027 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001028 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001029 }
1030 if (!CanDelete || (HasUse && HasDef)) {
1031 // If this is a two-addr instruction then its use operands are
1032 // rematerializable but its def is not. It should be assigned a
1033 // stack slot.
1034 vrm.assignVirt2StackSlot(NewVReg, Slot);
1035 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001036 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001037 vrm.assignVirt2StackSlot(NewVReg, Slot);
1038 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001039 } else if (HasUse && HasDef &&
1040 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1041 // If this interval hasn't been assigned a stack slot (because earlier
1042 // def is a deleted remat def), do it now.
1043 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1044 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001045 }
1046
Evan Cheng313d4b82008-02-23 00:33:04 +00001047 // Re-matting an instruction with virtual register use. Add the
1048 // register as an implicit use on the use MI.
1049 if (DefIsReMat && ImpUse)
1050 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1051
Evan Chengf2fbca62007-11-12 06:35:08 +00001052 // create a new register interval for this spill / remat.
1053 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001054 if (CreatedNewVReg) {
1055 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001056 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001057 if (TrySplit)
1058 vrm.setIsSplitFromReg(NewVReg, li.reg);
1059 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001060
1061 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001062 if (CreatedNewVReg) {
1063 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1064 nI.getNextValue(~0U, 0, VNInfoAllocator));
1065 DOUT << " +" << LR;
1066 nI.addRange(LR);
1067 } else {
1068 // Extend the split live interval to this def / use.
1069 unsigned End = getUseIndex(index)+1;
1070 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1071 nI.getValNumInfo(nI.getNumValNums()-1));
1072 DOUT << " +" << LR;
1073 nI.addRange(LR);
1074 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001075 }
1076 if (HasDef) {
1077 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1078 nI.getNextValue(~0U, 0, VNInfoAllocator));
1079 DOUT << " +" << LR;
1080 nI.addRange(LR);
1081 }
Evan Cheng81a03822007-11-17 00:40:40 +00001082
Evan Chengf2fbca62007-11-12 06:35:08 +00001083 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001084 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001085 DOUT << '\n';
1086 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001087 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001088}
Evan Cheng81a03822007-11-17 00:40:40 +00001089bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001090 const VNInfo *VNI,
1091 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001092 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001093 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1094 unsigned KillIdx = VNI->kills[j];
1095 if (KillIdx > Idx && KillIdx < End)
1096 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001097 }
1098 return false;
1099}
1100
Evan Cheng1953d0c2007-11-29 10:12:14 +00001101static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1102 const VNInfo *VNI = NULL;
1103 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1104 e = li.vni_end(); i != e; ++i)
1105 if ((*i)->def == DefIdx) {
1106 VNI = *i;
1107 break;
1108 }
1109 return VNI;
1110}
1111
Evan Cheng063284c2008-02-21 00:34:19 +00001112/// RewriteInfo - Keep track of machine instrs that will be rewritten
1113/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001114namespace {
1115 struct RewriteInfo {
1116 unsigned Index;
1117 MachineInstr *MI;
1118 bool HasUse;
1119 bool HasDef;
1120 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1121 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1122 };
Evan Cheng063284c2008-02-21 00:34:19 +00001123
Dan Gohman844731a2008-05-13 00:00:25 +00001124 struct RewriteInfoCompare {
1125 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1126 return LHS.Index < RHS.Index;
1127 }
1128 };
1129}
Evan Cheng063284c2008-02-21 00:34:19 +00001130
Evan Chengf2fbca62007-11-12 06:35:08 +00001131void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001132rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001133 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001134 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001135 unsigned Slot, int LdSlot,
1136 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001137 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001138 const TargetRegisterClass* rc,
1139 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001140 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001141 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001142 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001143 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001144 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1145 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001146 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001147 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001148 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001149 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001150 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001151
Evan Cheng063284c2008-02-21 00:34:19 +00001152 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001153 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001154 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001155 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1156 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001157 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001158 MachineOperand &O = ri.getOperand();
1159 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001160 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001161 unsigned index = getInstructionIndex(MI);
1162 if (index < start || index >= end)
1163 continue;
1164 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1165 }
1166 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1167
Evan Cheng313d4b82008-02-23 00:33:04 +00001168 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001169 // Now rewrite the defs and uses.
1170 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1171 RewriteInfo &rwi = RewriteMIs[i];
1172 ++i;
1173 unsigned index = rwi.Index;
1174 bool MIHasUse = rwi.HasUse;
1175 bool MIHasDef = rwi.HasDef;
1176 MachineInstr *MI = rwi.MI;
1177 // If MI def and/or use the same register multiple times, then there
1178 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001179 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001180 while (i != e && RewriteMIs[i].MI == MI) {
1181 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001182 bool isUse = RewriteMIs[i].HasUse;
1183 if (isUse) ++NumUses;
1184 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001185 MIHasDef |= RewriteMIs[i].HasDef;
1186 ++i;
1187 }
Evan Cheng81a03822007-11-17 00:40:40 +00001188 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001189
Evan Cheng0a891ed2008-05-23 23:00:04 +00001190 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001191 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001192 // register interval's spill weight to HUGE_VALF to prevent it from
1193 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001194 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001195 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001196 }
1197
Evan Cheng063284c2008-02-21 00:34:19 +00001198 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001199 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001200 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001201 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001202 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001203 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001204 // One common case:
1205 // x = use
1206 // ...
1207 // ...
1208 // def = ...
1209 // = use
1210 // It's better to start a new interval to avoid artifically
1211 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001212 if (MIHasDef && !MIHasUse) {
1213 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001214 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001215 }
1216 }
Evan Chengcada2452007-11-28 01:28:46 +00001217 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001218
1219 bool IsNew = ThisVReg == 0;
1220 if (IsNew) {
1221 // This ends the previous live interval. If all of its def / use
1222 // can be folded, give it a low spill weight.
1223 if (NewVReg && TrySplit && AllCanFold) {
1224 LiveInterval &nI = getOrCreateInterval(NewVReg);
1225 nI.weight /= 10.0F;
1226 }
1227 AllCanFold = true;
1228 }
1229 NewVReg = ThisVReg;
1230
Evan Cheng81a03822007-11-17 00:40:40 +00001231 bool HasDef = false;
1232 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001233 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001234 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1235 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001236 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001237 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001238 if (!HasDef && !HasUse)
1239 continue;
1240
Evan Cheng018f9b02007-12-05 03:22:34 +00001241 AllCanFold &= CanFold;
1242
Evan Cheng81a03822007-11-17 00:40:40 +00001243 // Update weight of spill interval.
1244 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001245 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001246 // The spill weight is now infinity as it cannot be spilled again.
1247 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001248 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001249 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001250
1251 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001252 if (HasDef) {
1253 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001254 bool HasKill = false;
1255 if (!HasUse)
1256 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1257 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001258 // If this is a two-address code, then this index starts a new VNInfo.
1259 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001260 if (VNI)
1261 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1262 }
Evan Chenge3110d02007-12-01 04:42:39 +00001263 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1264 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001265 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001266 if (SII == SpillIdxes.end()) {
1267 std::vector<SRInfo> S;
1268 S.push_back(SRInfo(index, NewVReg, true));
1269 SpillIdxes.insert(std::make_pair(MBBId, S));
1270 } else if (SII->second.back().vreg != NewVReg) {
1271 SII->second.push_back(SRInfo(index, NewVReg, true));
1272 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001273 // If there is an earlier def and this is a two-address
1274 // instruction, then it's not possible to fold the store (which
1275 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001276 SRInfo &Info = SII->second.back();
1277 Info.index = index;
1278 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001279 }
1280 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001281 } else if (SII != SpillIdxes.end() &&
1282 SII->second.back().vreg == NewVReg &&
1283 (int)index > SII->second.back().index) {
1284 // There is an earlier def that's not killed (must be two-address).
1285 // The spill is no longer needed.
1286 SII->second.pop_back();
1287 if (SII->second.empty()) {
1288 SpillIdxes.erase(MBBId);
1289 SpillMBBs.reset(MBBId);
1290 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001291 }
1292 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001293 }
1294
1295 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001296 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001297 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001298 if (SII != SpillIdxes.end() &&
1299 SII->second.back().vreg == NewVReg &&
1300 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001301 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001302 SII->second.back().canFold = false;
1303 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001304 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001305 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001306 // If we are splitting live intervals, only fold if it's the first
1307 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001308 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001309 else if (IsNew) {
1310 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001311 if (RII == RestoreIdxes.end()) {
1312 std::vector<SRInfo> Infos;
1313 Infos.push_back(SRInfo(index, NewVReg, true));
1314 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1315 } else {
1316 RII->second.push_back(SRInfo(index, NewVReg, true));
1317 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001318 RestoreMBBs.set(MBBId);
1319 }
1320 }
1321
1322 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001323 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001324 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001325 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001326
1327 if (NewVReg && TrySplit && AllCanFold) {
1328 // If all of its def / use can be folded, give it a low spill weight.
1329 LiveInterval &nI = getOrCreateInterval(NewVReg);
1330 nI.weight /= 10.0F;
1331 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001332}
1333
Evan Cheng1953d0c2007-11-29 10:12:14 +00001334bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1335 BitVector &RestoreMBBs,
1336 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1337 if (!RestoreMBBs[Id])
1338 return false;
1339 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1340 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1341 if (Restores[i].index == index &&
1342 Restores[i].vreg == vr &&
1343 Restores[i].canFold)
1344 return true;
1345 return false;
1346}
1347
1348void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1349 BitVector &RestoreMBBs,
1350 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1351 if (!RestoreMBBs[Id])
1352 return;
1353 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1354 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1355 if (Restores[i].index == index && Restores[i].vreg)
1356 Restores[i].index = -1;
1357}
Evan Cheng81a03822007-11-17 00:40:40 +00001358
Evan Cheng4cce6b42008-04-11 17:53:36 +00001359/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1360/// spilled and create empty intervals for their uses.
1361void
1362LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1363 const TargetRegisterClass* rc,
1364 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001365 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1366 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001367 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001368 MachineInstr *MI = &*ri;
1369 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001370 if (O.isDef()) {
1371 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1372 "Register def was not rewritten?");
1373 RemoveMachineInstrFromMaps(MI);
1374 vrm.RemoveMachineInstrFromMaps(MI);
1375 MI->eraseFromParent();
1376 } else {
1377 // This must be an use of an implicit_def so it's not part of the live
1378 // interval. Create a new empty live interval for it.
1379 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1380 unsigned NewVReg = mri_->createVirtualRegister(rc);
1381 vrm.grow();
1382 vrm.setIsImplicitlyDefined(NewVReg);
1383 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1384 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1385 MachineOperand &MO = MI->getOperand(i);
1386 if (MO.isReg() && MO.getReg() == li.reg)
1387 MO.setReg(NewVReg);
1388 }
1389 }
Evan Cheng419852c2008-04-03 16:39:43 +00001390 }
1391}
1392
Evan Cheng81a03822007-11-17 00:40:40 +00001393
Evan Chengf2fbca62007-11-12 06:35:08 +00001394std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001395addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001396 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001397 // Since this is called after the analysis is done we don't know if
1398 // LiveVariables is available
1399 lv_ = getAnalysisToUpdate<LiveVariables>();
1400
1401 assert(li.weight != HUGE_VALF &&
1402 "attempt to spill already spilled interval!");
1403
1404 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001405 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001406 DOUT << '\n';
1407
Evan Cheng81a03822007-11-17 00:40:40 +00001408 // Each bit specify whether it a spill is required in the MBB.
1409 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001410 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001411 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001412 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1413 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001414 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001415 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001416
1417 unsigned NumValNums = li.getNumValNums();
1418 SmallVector<MachineInstr*, 4> ReMatDefs;
1419 ReMatDefs.resize(NumValNums, NULL);
1420 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1421 ReMatOrigDefs.resize(NumValNums, NULL);
1422 SmallVector<int, 4> ReMatIds;
1423 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1424 BitVector ReMatDelete(NumValNums);
1425 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1426
Evan Cheng81a03822007-11-17 00:40:40 +00001427 // Spilling a split live interval. It cannot be split any further. Also,
1428 // it's also guaranteed to be a single val# / range interval.
1429 if (vrm.getPreSplitReg(li.reg)) {
1430 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001431 // Unset the split kill marker on the last use.
1432 unsigned KillIdx = vrm.getKillPoint(li.reg);
1433 if (KillIdx) {
1434 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1435 assert(KillMI && "Last use disappeared?");
1436 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1437 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001438 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001439 }
Evan Chengadf85902007-12-05 09:51:10 +00001440 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001441 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1442 Slot = vrm.getStackSlot(li.reg);
1443 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1444 MachineInstr *ReMatDefMI = DefIsReMat ?
1445 vrm.getReMaterializedMI(li.reg) : NULL;
1446 int LdSlot = 0;
1447 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1448 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001449 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001450 bool IsFirstRange = true;
1451 for (LiveInterval::Ranges::const_iterator
1452 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1453 // If this is a split live interval with multiple ranges, it means there
1454 // are two-address instructions that re-defined the value. Only the
1455 // first def can be rematerialized!
1456 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001457 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001458 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1459 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001460 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001461 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001462 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001463 } else {
1464 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1465 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001466 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001467 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001468 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001469 }
1470 IsFirstRange = false;
1471 }
Evan Cheng419852c2008-04-03 16:39:43 +00001472
Evan Cheng4cce6b42008-04-11 17:53:36 +00001473 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001474 return NewLIs;
1475 }
1476
1477 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001478 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1479 TrySplit = false;
1480 if (TrySplit)
1481 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001482 bool NeedStackSlot = false;
1483 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1484 i != e; ++i) {
1485 const VNInfo *VNI = *i;
1486 unsigned VN = VNI->id;
1487 unsigned DefIdx = VNI->def;
1488 if (DefIdx == ~1U)
1489 continue; // Dead val#.
1490 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001491 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1492 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001493 bool dummy;
1494 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001495 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001496 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001497 // Original def may be modified so we have to make a copy here. vrm must
1498 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001499 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001500
1501 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001502 if (VNI->hasPHIKill) {
1503 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001504 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001505 CanDelete = false;
1506 // Need a stack slot if there is any live range where uses cannot be
1507 // rematerialized.
1508 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001509 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001510 if (CanDelete)
1511 ReMatDelete.set(VN);
1512 } else {
1513 // Need a stack slot if there is any live range where uses cannot be
1514 // rematerialized.
1515 NeedStackSlot = true;
1516 }
1517 }
1518
1519 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001520 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001521 Slot = vrm.assignVirt2StackSlot(li.reg);
1522
1523 // Create new intervals and rewrite defs and uses.
1524 for (LiveInterval::Ranges::const_iterator
1525 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001526 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1527 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1528 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001529 bool CanDelete = ReMatDelete[I->valno->id];
1530 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001531 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001532 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001533 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001534 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001535 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001536 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001537 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001538 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001539 }
1540
Evan Cheng0cbb1162007-11-29 01:06:25 +00001541 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001542 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001543 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001544 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001545 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001546
Evan Chengb50bb8c2007-12-05 08:16:32 +00001547 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001548 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001549 if (NeedStackSlot) {
1550 int Id = SpillMBBs.find_first();
1551 while (Id != -1) {
1552 std::vector<SRInfo> &spills = SpillIdxes[Id];
1553 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1554 int index = spills[i].index;
1555 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001556 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001557 bool isReMat = vrm.isReMaterialized(VReg);
1558 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001559 bool CanFold = false;
1560 bool FoundUse = false;
1561 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001562 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001563 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001564 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1565 MachineOperand &MO = MI->getOperand(j);
1566 if (!MO.isRegister() || MO.getReg() != VReg)
1567 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001568
1569 Ops.push_back(j);
1570 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001571 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001572 if (isReMat ||
1573 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1574 RestoreMBBs, RestoreIdxes))) {
1575 // MI has two-address uses of the same register. If the use
1576 // isn't the first and only use in the BB, then we can't fold
1577 // it. FIXME: Move this to rewriteInstructionsForSpills.
1578 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001579 break;
1580 }
Evan Chengaee4af62007-12-02 08:30:39 +00001581 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001582 }
1583 }
1584 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001585 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001586 if (CanFold && !Ops.empty()) {
1587 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001588 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001589 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001590 // Also folded uses, do not issue a load.
1591 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001592 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1593 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001594 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001595 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001596 }
1597
Evan Cheng7e073ba2008-04-09 20:57:25 +00001598 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001599 if (!Folded) {
1600 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1601 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001602 if (!MI->registerDefIsDead(nI.reg))
1603 // No need to spill a dead def.
1604 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001605 if (isKill)
1606 AddedKill.insert(&nI);
1607 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001608 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001609 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001610 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001611 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001612
Evan Cheng1953d0c2007-11-29 10:12:14 +00001613 int Id = RestoreMBBs.find_first();
1614 while (Id != -1) {
1615 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1616 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1617 int index = restores[i].index;
1618 if (index == -1)
1619 continue;
1620 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001621 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001622 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001623 bool CanFold = false;
1624 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001625 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001626 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001627 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1628 MachineOperand &MO = MI->getOperand(j);
1629 if (!MO.isRegister() || MO.getReg() != VReg)
1630 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001631
Evan Cheng0cbb1162007-11-29 01:06:25 +00001632 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001633 // If this restore were to be folded, it would have been folded
1634 // already.
1635 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001636 break;
1637 }
Evan Chengaee4af62007-12-02 08:30:39 +00001638 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001639 }
1640 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001641
1642 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001643 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001644 if (CanFold && !Ops.empty()) {
1645 if (!vrm.isReMaterialized(VReg))
1646 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1647 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001648 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1649 int LdSlot = 0;
1650 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1651 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001652 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001653 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1654 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001655 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1656 if (ImpUse) {
1657 // Re-matting an instruction with virtual register use. Add the
1658 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001659 // interval's spill weight to HUGE_VALF to prevent it from being
1660 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001661 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001662 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001663 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1664 }
Evan Chengaee4af62007-12-02 08:30:39 +00001665 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001666 }
1667 // If folding is not possible / failed, then tell the spiller to issue a
1668 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001669 if (Folded)
1670 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001671 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001672 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001673 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001674 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001675 }
1676
Evan Chengb50bb8c2007-12-05 08:16:32 +00001677 // Finalize intervals: add kills, finalize spill weights, and filter out
1678 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001679 std::vector<LiveInterval*> RetNewLIs;
1680 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1681 LiveInterval *LI = NewLIs[i];
1682 if (!LI->empty()) {
1683 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001684 if (!AddedKill.count(LI)) {
1685 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001686 unsigned LastUseIdx = getBaseIndex(LR->end);
1687 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001688 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001689 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001690 if (LastUse->getOperand(UseIdx).isImplicit() ||
1691 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001692 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001693 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001694 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001695 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001696 RetNewLIs.push_back(LI);
1697 }
1698 }
Evan Cheng81a03822007-11-17 00:40:40 +00001699
Evan Cheng4cce6b42008-04-11 17:53:36 +00001700 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001701 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001702}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001703
1704/// hasAllocatableSuperReg - Return true if the specified physical register has
1705/// any super register that's allocatable.
1706bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1707 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1708 if (allocatableRegs_[*AS] && hasInterval(*AS))
1709 return true;
1710 return false;
1711}
1712
1713/// getRepresentativeReg - Find the largest super register of the specified
1714/// physical register.
1715unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1716 // Find the largest super-register that is allocatable.
1717 unsigned BestReg = Reg;
1718 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1719 unsigned SuperReg = *AS;
1720 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1721 BestReg = SuperReg;
1722 break;
1723 }
1724 }
1725 return BestReg;
1726}
1727
1728/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1729/// specified interval that conflicts with the specified physical register.
1730unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1731 unsigned PhysReg) const {
1732 unsigned NumConflicts = 0;
1733 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1734 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1735 E = mri_->reg_end(); I != E; ++I) {
1736 MachineOperand &O = I.getOperand();
1737 MachineInstr *MI = O.getParent();
1738 unsigned Index = getInstructionIndex(MI);
1739 if (pli.liveAt(Index))
1740 ++NumConflicts;
1741 }
1742 return NumConflicts;
1743}
1744
1745/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1746/// around all defs and uses of the specified interval.
1747void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1748 unsigned PhysReg, VirtRegMap &vrm) {
1749 unsigned SpillReg = getRepresentativeReg(PhysReg);
1750
1751 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1752 // If there are registers which alias PhysReg, but which are not a
1753 // sub-register of the chosen representative super register. Assert
1754 // since we can't handle it yet.
1755 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1756 tri_->isSuperRegister(*AS, SpillReg));
1757
1758 LiveInterval &pli = getInterval(SpillReg);
1759 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1760 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1761 E = mri_->reg_end(); I != E; ++I) {
1762 MachineOperand &O = I.getOperand();
1763 MachineInstr *MI = O.getParent();
1764 if (SeenMIs.count(MI))
1765 continue;
1766 SeenMIs.insert(MI);
1767 unsigned Index = getInstructionIndex(MI);
1768 if (pli.liveAt(Index)) {
1769 vrm.addEmergencySpill(SpillReg, MI);
1770 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1771 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1772 if (!hasInterval(*AS))
1773 continue;
1774 LiveInterval &spli = getInterval(*AS);
1775 if (spli.liveAt(Index))
1776 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1777 }
1778 }
1779 }
1780}