blob: 976aa5a9245a7ffb8b859b52a3da07d43ae33e61 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
63 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000068 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mi2iMap_.clear();
70 i2miMap_.clear();
71 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000072 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
73 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000074 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
75 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000076}
77
Owen Anderson80b3ce62008-05-28 20:54:50 +000078void LiveIntervals::computeNumbering() {
79 Index2MiMap OldI2MI = i2miMap_;
80
81 Idx2MBBMap.clear();
82 MBB2IdxMap.clear();
83 mi2iMap_.clear();
84 i2miMap_.clear();
85
Chris Lattner428b92e2006-09-15 03:57:23 +000086 // Number MachineInstrs and MachineBasicBlocks.
87 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000088 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000089
90 unsigned MIIndex = 0;
91 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
92 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000093 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000094
Chris Lattner428b92e2006-09-15 03:57:23 +000095 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
96 I != E; ++I) {
97 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000099 i2miMap_.push_back(I);
100 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000101 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000102
103 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000104 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
105 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
106 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000107 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000109 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000110
111 if (!OldI2MI.empty())
112 for (iterator I = begin(), E = end(); I != E; ++I)
113 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
114 LI != LE; ++LI) {
115 LI->start = mi2iMap_[OldI2MI[LI->start]];
116 LI->end = mi2iMap_[OldI2MI[LI->end]];
Owen Anderson745825f42008-05-28 22:40:08 +0000117
118 VNInfo* vni = LI->valno;
119 vni->def = mi2iMap_[OldI2MI[vni->def]];
120
121 for (size_t i = 0; i < vni->kills.size(); ++i)
122 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i]]];
Owen Anderson80b3ce62008-05-28 20:54:50 +0000123 }
124}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000125
Owen Anderson80b3ce62008-05-28 20:54:50 +0000126/// runOnMachineFunction - Register allocate the whole function
127///
128bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
129 mf_ = &fn;
130 mri_ = &mf_->getRegInfo();
131 tm_ = &fn.getTarget();
132 tri_ = tm_->getRegisterInfo();
133 tii_ = tm_->getInstrInfo();
134 lv_ = &getAnalysis<LiveVariables>();
135 allocatableRegs_ = tri_->getAllocatableSet(fn);
136
137 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000138 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000139
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140 numIntervals += getNumIntervals();
141
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000142 DOUT << "********** INTERVALS **********\n";
143 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000144 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000145 DOUT << "\n";
146 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000147
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000148 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000149 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000150 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000151}
152
Chris Lattner70ca3582004-09-30 15:59:17 +0000153/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000154void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000155 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000156 for (const_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";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000159 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000160
161 O << "********** MACHINEINSTRS **********\n";
162 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
163 mbbi != mbbe; ++mbbi) {
164 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
165 for (MachineBasicBlock::iterator mii = mbbi->begin(),
166 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000167 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000168 }
169 }
170}
171
Evan Chengc92da382007-11-03 07:20:12 +0000172/// conflictsWithPhysRegDef - Returns true if the specified register
173/// is defined during the duration of the specified interval.
174bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
175 VirtRegMap &vrm, unsigned reg) {
176 for (LiveInterval::Ranges::const_iterator
177 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
178 for (unsigned index = getBaseIndex(I->start),
179 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
180 index += InstrSlots::NUM) {
181 // skip deleted instructions
182 while (index != end && !getInstructionFromIndex(index))
183 index += InstrSlots::NUM;
184 if (index == end) break;
185
186 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000187 unsigned SrcReg, DstReg;
188 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
189 if (SrcReg == li.reg || DstReg == li.reg)
190 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000191 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
192 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000193 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000194 continue;
195 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000196 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000197 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000198 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000199 if (!vrm.hasPhys(PhysReg))
200 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000201 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000202 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000203 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000204 return true;
205 }
206 }
207 }
208
209 return false;
210}
211
Evan Cheng549f27d32007-08-13 23:45:17 +0000212void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000213 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000214 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000215 else
216 cerr << "%reg" << reg;
217}
218
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000219void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000220 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000221 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000222 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000223 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000224 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000225
Evan Cheng419852c2008-04-03 16:39:43 +0000226 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
227 DOUT << "is a implicit_def\n";
228 return;
229 }
230
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000231 // Virtual registers may be defined multiple times (due to phi
232 // elimination and 2-addr elimination). Much of what we do only has to be
233 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000234 // time we see a vreg.
235 if (interval.empty()) {
236 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000237 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000238 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000239 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000240 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000241 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000242 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000243 tii_->isMoveInstr(*mi, SrcReg, DstReg))
244 CopyMI = mi;
245 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000246
247 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000248
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 // Loop over all of the blocks that the vreg is defined in. There are
250 // two cases we have to handle here. The most common case is a vreg
251 // whose lifetime is contained within a basic block. In this case there
252 // will be a single kill, in MBB, which comes after the definition.
253 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
254 // FIXME: what about dead vars?
255 unsigned killIdx;
256 if (vi.Kills[0] != mi)
257 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
258 else
259 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000260
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000261 // If the kill happens after the definition, we have an intra-block
262 // live range.
263 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000264 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000265 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000266 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000267 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000268 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000269 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000270 return;
271 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000272 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000273
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000274 // The other case we handle is when a virtual register lives to the end
275 // of the defining block, potentially live across some blocks, then is
276 // live into some number of blocks, but gets killed. Start by adding a
277 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000278 LiveRange NewLR(defIndex,
279 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000280 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000281 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000282 interval.addRange(NewLR);
283
284 // Iterate over all of the blocks that the variable is completely
285 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
286 // live interval.
287 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
288 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000289 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
290 if (!MBB->empty()) {
291 LiveRange LR(getMBBStartIdx(i),
292 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000293 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000295 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296 }
297 }
298 }
299
300 // Finally, this virtual register is live from the start of any killing
301 // block to the 'use' slot of the killing instruction.
302 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
303 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000304 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000305 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000306 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000308 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000309 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 }
311
312 } else {
313 // If this is the second time we see a virtual register definition, it
314 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000315 // the result of two address elimination, then the vreg is one of the
316 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000317 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000318 // If this is a two-address definition, then we have already processed
319 // the live range. The only problem is that we didn't realize there
320 // are actually two values in the live interval. Because of this we
321 // need to take the LiveRegion that defines this register and split it
322 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000323 assert(interval.containsOneValue());
324 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000325 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326
Evan Cheng4f8ff162007-08-11 00:59:19 +0000327 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000328 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000329
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000331 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000333
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000334 // Two-address vregs should always only be redefined once. This means
335 // that at this point, there should be exactly one value number in it.
336 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
337
Chris Lattner91725b72006-08-31 05:54:43 +0000338 // The new value number (#1) is defined by the instruction we claimed
339 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000340 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
341 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000342
Chris Lattner91725b72006-08-31 05:54:43 +0000343 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000344 OldValNo->def = RedefIndex;
345 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000346
347 // Add the new live interval which replaces the range for the input copy.
348 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000349 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000350 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000351 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000352
353 // If this redefinition is dead, we need to add a dummy unit live
354 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000355 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000356 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000358 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000359 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000360
361 } else {
362 // Otherwise, this must be because of phi elimination. If this is the
363 // first redefinition of the vreg that we have seen, go back and change
364 // the live range in the PHI block to be a different value number.
365 if (interval.containsOneValue()) {
366 assert(vi.Kills.size() == 1 &&
367 "PHI elimination vreg should have one kill, the PHI itself!");
368
369 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000370 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000372 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000373 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000374 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000375 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000376 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000377 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000378 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000379
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000380 // Replace the interval with one of a NEW value number. Note that this
381 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000382 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000383 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000384 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000385 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000386 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000387 }
388
389 // In the case of PHI elimination, each variable definition is only
390 // live until the end of the block. We've already taken care of the
391 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000392 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000393
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000394 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000395 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000396 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000397 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000398 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000399 tii_->isMoveInstr(*mi, SrcReg, DstReg))
400 CopyMI = mi;
401 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000402
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000403 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000404 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000405 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000406 interval.addKill(ValNo, killIndex);
407 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000408 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000409 }
410 }
411
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000412 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000413}
414
Chris Lattnerf35fef72004-07-23 21:24:19 +0000415void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000416 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000417 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000418 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000419 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 // A physical register cannot be live across basic block, so its
421 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000422 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000423
Chris Lattner6b128bd2006-09-03 08:07:11 +0000424 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000425 unsigned start = getDefIndex(baseIndex);
426 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000427
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428 // If it is not used after definition, it is considered dead at
429 // the instruction defining it. Hence its interval is:
430 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000431 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000432 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000433 end = getDefIndex(start) + 1;
434 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435 }
436
437 // If it is not dead on definition, it must be killed by a
438 // subsequent instruction. Hence its interval is:
439 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000440 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000441 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000442 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000443 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000444 end = getUseIndex(baseIndex) + 1;
445 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000446 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000447 // Another instruction redefines the register before it is ever read.
448 // Then the register is essentially dead at the instruction that defines
449 // it. Hence its interval is:
450 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000451 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000452 end = getDefIndex(start) + 1;
453 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000454 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000455 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000456
457 // The only case we should have a dead physreg here without a killing or
458 // instruction where we know it's dead is if it is live-in to the function
459 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000460 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000461 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000462
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000463exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000464 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000465
Evan Cheng24a3cc42007-04-25 07:30:23 +0000466 // Already exists? Extend old live interval.
467 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000468 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000469 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000470 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000471 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000472 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000473 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000474}
475
Chris Lattnerf35fef72004-07-23 21:24:19 +0000476void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
477 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000478 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000479 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000480 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000481 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000482 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000483 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000484 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000485 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000486 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000487 tii_->isMoveInstr(*MI, SrcReg, DstReg))
488 CopyMI = MI;
489 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000490 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000491 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000492 // If MI also modifies the sub-register explicitly, avoid processing it
493 // more than once. Do not pass in TRI here so it checks for exact match.
494 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000495 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000496 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000497}
498
Evan Chengb371f452007-02-19 21:49:54 +0000499void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000500 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000501 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000502 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
503
504 // Look for kills, if it reaches a def before it's killed, then it shouldn't
505 // be considered a livein.
506 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000507 unsigned baseIndex = MIIdx;
508 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000509 unsigned end = start;
510 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000511 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000512 DOUT << " killed";
513 end = getUseIndex(baseIndex) + 1;
514 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000515 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000516 // Another instruction redefines the register before it is ever read.
517 // Then the register is essentially dead at the instruction that defines
518 // it. Hence its interval is:
519 // [defSlot(def), defSlot(def)+1)
520 DOUT << " dead";
521 end = getDefIndex(start) + 1;
522 goto exit;
523 }
524
525 baseIndex += InstrSlots::NUM;
526 ++mi;
527 }
528
529exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000530 // Live-in register might not be used at all.
531 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000532 if (isAlias) {
533 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000534 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000535 } else {
536 DOUT << " live through";
537 end = baseIndex;
538 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000539 }
540
Evan Chengf3bb2e62007-09-05 21:46:51 +0000541 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000542 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000543 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000544 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000545}
546
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000547/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000548/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000549/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000550/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000551void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000552 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
553 << "********** Function: "
554 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000555 // Track the index of the current machine instr.
556 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000557 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
558 MBBI != E; ++MBBI) {
559 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000560 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000561
Chris Lattner428b92e2006-09-15 03:57:23 +0000562 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000563
Dan Gohmancb406c22007-10-03 19:26:29 +0000564 // Create intervals for live-ins to this BB first.
565 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
566 LE = MBB->livein_end(); LI != LE; ++LI) {
567 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
568 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000569 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000570 if (!hasInterval(*AS))
571 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
572 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000573 }
574
Chris Lattner428b92e2006-09-15 03:57:23 +0000575 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000576 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000577
Evan Cheng438f7bc2006-11-10 08:43:01 +0000578 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000579 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
580 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000581 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000582 if (MO.isRegister() && MO.getReg() && MO.isDef())
583 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000584 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000585
586 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000587 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000588 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000589}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000590
Evan Cheng4ca980e2007-10-17 02:10:22 +0000591bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000592 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000593 std::vector<IdxMBBPair>::const_iterator I =
594 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
595
596 bool ResVal = false;
597 while (I != Idx2MBBMap.end()) {
598 if (LR.end <= I->first)
599 break;
600 MBBs.push_back(I->second);
601 ResVal = true;
602 ++I;
603 }
604 return ResVal;
605}
606
607
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000608LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000609 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000610 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000611 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000612}
Evan Chengf2fbca62007-11-12 06:35:08 +0000613
Evan Chengc8d044e2008-02-15 18:24:29 +0000614/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
615/// copy field and returns the source register that defines it.
616unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
617 if (!VNI->copy)
618 return 0;
619
620 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
621 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000622 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
623 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000624 unsigned SrcReg, DstReg;
625 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
626 return SrcReg;
627 assert(0 && "Unrecognized copy instruction!");
628 return 0;
629}
Evan Chengf2fbca62007-11-12 06:35:08 +0000630
631//===----------------------------------------------------------------------===//
632// Register allocator hooks.
633//
634
Evan Chengd70dbb52008-02-22 09:24:50 +0000635/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
636/// allow one) virtual register operand, then its uses are implicitly using
637/// the register. Returns the virtual register.
638unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
639 MachineInstr *MI) const {
640 unsigned RegOp = 0;
641 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
642 MachineOperand &MO = MI->getOperand(i);
643 if (!MO.isRegister() || !MO.isUse())
644 continue;
645 unsigned Reg = MO.getReg();
646 if (Reg == 0 || Reg == li.reg)
647 continue;
648 // FIXME: For now, only remat MI with at most one register operand.
649 assert(!RegOp &&
650 "Can't rematerialize instruction with multiple register operand!");
651 RegOp = MO.getReg();
652 break;
653 }
654 return RegOp;
655}
656
657/// isValNoAvailableAt - Return true if the val# of the specified interval
658/// which reaches the given instruction also reaches the specified use index.
659bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
660 unsigned UseIdx) const {
661 unsigned Index = getInstructionIndex(MI);
662 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
663 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
664 return UI != li.end() && UI->valno == ValNo;
665}
666
Evan Chengf2fbca62007-11-12 06:35:08 +0000667/// isReMaterializable - Returns true if the definition MI of the specified
668/// val# of the specified interval is re-materializable.
669bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000670 const VNInfo *ValNo, MachineInstr *MI,
671 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000672 if (DisableReMat)
673 return false;
674
Evan Cheng5ef3a042007-12-06 00:01:56 +0000675 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000676 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000677 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000678
679 int FrameIdx = 0;
680 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000681 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000682 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
683 // this but remember this is not safe to fold into a two-address
684 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000685 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000686 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000687
Evan Chengd70dbb52008-02-22 09:24:50 +0000688 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000689 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000690 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000691
692 unsigned ImpUse = getReMatImplicitUse(li, MI);
693 if (ImpUse) {
694 const LiveInterval &ImpLi = getInterval(ImpUse);
695 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
696 re = mri_->use_end(); ri != re; ++ri) {
697 MachineInstr *UseMI = &*ri;
698 unsigned UseIdx = getInstructionIndex(UseMI);
699 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
700 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000701 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000702 return false;
703 }
704 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000705 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000706 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000707
Evan Chengdd3465e2008-02-23 01:44:27 +0000708 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000709}
710
711/// isReMaterializable - Returns true if every definition of MI of every
712/// val# of the specified interval is re-materializable.
713bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
714 isLoad = false;
715 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
716 i != e; ++i) {
717 const VNInfo *VNI = *i;
718 unsigned DefIdx = VNI->def;
719 if (DefIdx == ~1U)
720 continue; // Dead val#.
721 // Is the def for the val# rematerializable?
722 if (DefIdx == ~0u)
723 return false;
724 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
725 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000726 if (!ReMatDefMI ||
727 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000728 return false;
729 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000730 }
731 return true;
732}
733
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000734/// FilterFoldedOps - Filter out two-address use operands. Return
735/// true if it finds any issue with the operands that ought to prevent
736/// folding.
737static bool FilterFoldedOps(MachineInstr *MI,
738 SmallVector<unsigned, 2> &Ops,
739 unsigned &MRInfo,
740 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000741 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000742
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000743 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000744 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
745 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000746 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000747 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000748 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000749 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000750 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000751 MRInfo |= (unsigned)VirtRegMap::isMod;
752 else {
753 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000754 if (!MO.isImplicit() &&
755 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000756 MRInfo = VirtRegMap::isModRef;
757 continue;
758 }
759 MRInfo |= (unsigned)VirtRegMap::isRef;
760 }
761 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000762 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000763 return false;
764}
765
766
767/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
768/// slot / to reg or any rematerialized load into ith operand of specified
769/// MI. If it is successul, MI is updated with the newly created MI and
770/// returns true.
771bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
772 VirtRegMap &vrm, MachineInstr *DefMI,
773 unsigned InstrIdx,
774 SmallVector<unsigned, 2> &Ops,
775 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000776 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000777 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000778 RemoveMachineInstrFromMaps(MI);
779 vrm.RemoveMachineInstrFromMaps(MI);
780 MI->eraseFromParent();
781 ++numFolds;
782 return true;
783 }
784
785 // Filter the list of operand indexes that are to be folded. Abort if
786 // any operand will prevent folding.
787 unsigned MRInfo = 0;
788 SmallVector<unsigned, 2> FoldOps;
789 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
790 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000791
Evan Cheng427f4c12008-03-31 23:19:51 +0000792 // The only time it's safe to fold into a two address instruction is when
793 // it's folding reload and spill from / into a spill stack slot.
794 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000795 return false;
796
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000797 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
798 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000799 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000800 // Remember this instruction uses the spill slot.
801 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
802
Evan Chengf2fbca62007-11-12 06:35:08 +0000803 // Attempt to fold the memory reference into the instruction. If
804 // we can do this, we don't need to insert spill code.
805 if (lv_)
806 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000807 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000808 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000809 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000810 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000811 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000812 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000813 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000814 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000815 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000816 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
817 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000818 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000819 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000820 return true;
821 }
822 return false;
823}
824
Evan Cheng018f9b02007-12-05 03:22:34 +0000825/// canFoldMemoryOperand - Returns true if the specified load / store
826/// folding is possible.
827bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000828 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000829 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000830 // Filter the list of operand indexes that are to be folded. Abort if
831 // any operand will prevent folding.
832 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000833 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000834 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
835 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000836
Evan Cheng3c75ba82008-04-01 21:37:32 +0000837 // It's only legal to remat for a use, not a def.
838 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000839 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000840
Evan Chengd70dbb52008-02-22 09:24:50 +0000841 return tii_->canFoldMemoryOperand(MI, FoldOps);
842}
843
Evan Cheng81a03822007-11-17 00:40:40 +0000844bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
845 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
846 for (LiveInterval::Ranges::const_iterator
847 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
848 std::vector<IdxMBBPair>::const_iterator II =
849 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
850 if (II == Idx2MBBMap.end())
851 continue;
852 if (I->end > II->first) // crossing a MBB.
853 return false;
854 MBBs.insert(II->second);
855 if (MBBs.size() > 1)
856 return false;
857 }
858 return true;
859}
860
Evan Chengd70dbb52008-02-22 09:24:50 +0000861/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
862/// interval on to-be re-materialized operands of MI) with new register.
863void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
864 MachineInstr *MI, unsigned NewVReg,
865 VirtRegMap &vrm) {
866 // There is an implicit use. That means one of the other operand is
867 // being remat'ed and the remat'ed instruction has li.reg as an
868 // use operand. Make sure we rewrite that as well.
869 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
870 MachineOperand &MO = MI->getOperand(i);
871 if (!MO.isRegister())
872 continue;
873 unsigned Reg = MO.getReg();
874 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
875 continue;
876 if (!vrm.isReMaterialized(Reg))
877 continue;
878 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000879 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
880 if (UseMO)
881 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000882 }
883}
884
Evan Chengf2fbca62007-11-12 06:35:08 +0000885/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
886/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000887bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000888rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
889 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000890 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000891 unsigned Slot, int LdSlot,
892 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000893 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000894 const TargetRegisterClass* rc,
895 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000896 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000897 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000898 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000899 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000900 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000901 RestartInstruction:
902 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
903 MachineOperand& mop = MI->getOperand(i);
904 if (!mop.isRegister())
905 continue;
906 unsigned Reg = mop.getReg();
907 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000908 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000909 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000910 if (Reg != li.reg)
911 continue;
912
913 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000914 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000915 int FoldSlot = Slot;
916 if (DefIsReMat) {
917 // If this is the rematerializable definition MI itself and
918 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000919 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000920 DOUT << "\t\t\t\tErasing re-materlizable def: ";
921 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000922 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000923 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000924 MI->eraseFromParent();
925 break;
926 }
927
928 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000929 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000930 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000931 if (isLoad) {
932 // Try fold loads (from stack slot, constant pool, etc.) into uses.
933 FoldSS = isLoadSS;
934 FoldSlot = LdSlot;
935 }
936 }
937
Evan Chengf2fbca62007-11-12 06:35:08 +0000938 // Scan all of the operands of this instruction rewriting operands
939 // to use NewVReg instead of li.reg as appropriate. We do this for
940 // two reasons:
941 //
942 // 1. If the instr reads the same spilled vreg multiple times, we
943 // want to reuse the NewVReg.
944 // 2. If the instr is a two-addr instruction, we are required to
945 // keep the src/dst regs pinned.
946 //
947 // Keep track of whether we replace a use and/or def so that we can
948 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000949
Evan Cheng81a03822007-11-17 00:40:40 +0000950 HasUse = mop.isUse();
951 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000952 SmallVector<unsigned, 2> Ops;
953 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000954 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000955 const MachineOperand &MOj = MI->getOperand(j);
956 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000957 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000958 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000959 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000960 continue;
961 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000962 Ops.push_back(j);
963 HasUse |= MOj.isUse();
964 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000965 }
966 }
967
Evan Cheng018f9b02007-12-05 03:22:34 +0000968 if (TryFold) {
969 // Do not fold load / store here if we are splitting. We'll find an
970 // optimal point to insert a load / store later.
971 if (!TrySplit) {
972 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
973 Ops, FoldSS, FoldSlot, Reg)) {
974 // Folding the load/store can completely change the instruction in
975 // unpredictable ways, rescan it from the beginning.
976 HasUse = false;
977 HasDef = false;
978 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000979 if (isRemoved(MI))
980 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000981 goto RestartInstruction;
982 }
983 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000984 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000985 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000986 } else
987 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000988
989 // Create a new virtual register for the spill interval.
990 bool CreatedNewVReg = false;
991 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +0000992 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000993 vrm.grow();
994 CreatedNewVReg = true;
995 }
996 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000997 if (mop.isImplicit())
998 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +0000999
1000 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001001 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1002 MachineOperand &mopj = MI->getOperand(Ops[j]);
1003 mopj.setReg(NewVReg);
1004 if (mopj.isImplicit())
1005 rewriteImplicitOps(li, MI, NewVReg, vrm);
1006 }
Evan Chengcddbb832007-11-30 21:23:43 +00001007
Evan Cheng81a03822007-11-17 00:40:40 +00001008 if (CreatedNewVReg) {
1009 if (DefIsReMat) {
1010 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001011 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001012 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001013 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001014 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001015 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001016 }
1017 if (!CanDelete || (HasUse && HasDef)) {
1018 // If this is a two-addr instruction then its use operands are
1019 // rematerializable but its def is not. It should be assigned a
1020 // stack slot.
1021 vrm.assignVirt2StackSlot(NewVReg, Slot);
1022 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001023 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001024 vrm.assignVirt2StackSlot(NewVReg, Slot);
1025 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001026 } else if (HasUse && HasDef &&
1027 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1028 // If this interval hasn't been assigned a stack slot (because earlier
1029 // def is a deleted remat def), do it now.
1030 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1031 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001032 }
1033
Evan Cheng313d4b82008-02-23 00:33:04 +00001034 // Re-matting an instruction with virtual register use. Add the
1035 // register as an implicit use on the use MI.
1036 if (DefIsReMat && ImpUse)
1037 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1038
Evan Chengf2fbca62007-11-12 06:35:08 +00001039 // create a new register interval for this spill / remat.
1040 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001041 if (CreatedNewVReg) {
1042 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001043 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001044 if (TrySplit)
1045 vrm.setIsSplitFromReg(NewVReg, li.reg);
1046 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001047
1048 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001049 if (CreatedNewVReg) {
1050 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1051 nI.getNextValue(~0U, 0, VNInfoAllocator));
1052 DOUT << " +" << LR;
1053 nI.addRange(LR);
1054 } else {
1055 // Extend the split live interval to this def / use.
1056 unsigned End = getUseIndex(index)+1;
1057 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1058 nI.getValNumInfo(nI.getNumValNums()-1));
1059 DOUT << " +" << LR;
1060 nI.addRange(LR);
1061 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001062 }
1063 if (HasDef) {
1064 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1065 nI.getNextValue(~0U, 0, VNInfoAllocator));
1066 DOUT << " +" << LR;
1067 nI.addRange(LR);
1068 }
Evan Cheng81a03822007-11-17 00:40:40 +00001069
Evan Chengf2fbca62007-11-12 06:35:08 +00001070 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001071 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001072 DOUT << '\n';
1073 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001074 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001075}
Evan Cheng81a03822007-11-17 00:40:40 +00001076bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001077 const VNInfo *VNI,
1078 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001079 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001080 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1081 unsigned KillIdx = VNI->kills[j];
1082 if (KillIdx > Idx && KillIdx < End)
1083 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001084 }
1085 return false;
1086}
1087
Evan Cheng1953d0c2007-11-29 10:12:14 +00001088static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1089 const VNInfo *VNI = NULL;
1090 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1091 e = li.vni_end(); i != e; ++i)
1092 if ((*i)->def == DefIdx) {
1093 VNI = *i;
1094 break;
1095 }
1096 return VNI;
1097}
1098
Evan Cheng063284c2008-02-21 00:34:19 +00001099/// RewriteInfo - Keep track of machine instrs that will be rewritten
1100/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001101namespace {
1102 struct RewriteInfo {
1103 unsigned Index;
1104 MachineInstr *MI;
1105 bool HasUse;
1106 bool HasDef;
1107 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1108 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1109 };
Evan Cheng063284c2008-02-21 00:34:19 +00001110
Dan Gohman844731a2008-05-13 00:00:25 +00001111 struct RewriteInfoCompare {
1112 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1113 return LHS.Index < RHS.Index;
1114 }
1115 };
1116}
Evan Cheng063284c2008-02-21 00:34:19 +00001117
Evan Chengf2fbca62007-11-12 06:35:08 +00001118void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001119rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001120 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001121 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001122 unsigned Slot, int LdSlot,
1123 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001124 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001125 const TargetRegisterClass* rc,
1126 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001127 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001128 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001129 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001130 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001131 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1132 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001133 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001134 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001135 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001136 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001137 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001138
Evan Cheng063284c2008-02-21 00:34:19 +00001139 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001140 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001141 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001142 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1143 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001144 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001145 MachineOperand &O = ri.getOperand();
1146 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001147 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001148 unsigned index = getInstructionIndex(MI);
1149 if (index < start || index >= end)
1150 continue;
1151 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1152 }
1153 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1154
Evan Cheng313d4b82008-02-23 00:33:04 +00001155 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001156 // Now rewrite the defs and uses.
1157 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1158 RewriteInfo &rwi = RewriteMIs[i];
1159 ++i;
1160 unsigned index = rwi.Index;
1161 bool MIHasUse = rwi.HasUse;
1162 bool MIHasDef = rwi.HasDef;
1163 MachineInstr *MI = rwi.MI;
1164 // If MI def and/or use the same register multiple times, then there
1165 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001166 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001167 while (i != e && RewriteMIs[i].MI == MI) {
1168 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001169 bool isUse = RewriteMIs[i].HasUse;
1170 if (isUse) ++NumUses;
1171 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001172 MIHasDef |= RewriteMIs[i].HasDef;
1173 ++i;
1174 }
Evan Cheng81a03822007-11-17 00:40:40 +00001175 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001176
Evan Cheng0a891ed2008-05-23 23:00:04 +00001177 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001178 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001179 // register interval's spill weight to HUGE_VALF to prevent it from
1180 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001181 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001182 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001183 }
1184
Evan Cheng063284c2008-02-21 00:34:19 +00001185 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001186 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001187 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001188 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001189 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001190 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001191 // One common case:
1192 // x = use
1193 // ...
1194 // ...
1195 // def = ...
1196 // = use
1197 // It's better to start a new interval to avoid artifically
1198 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001199 if (MIHasDef && !MIHasUse) {
1200 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001201 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001202 }
1203 }
Evan Chengcada2452007-11-28 01:28:46 +00001204 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001205
1206 bool IsNew = ThisVReg == 0;
1207 if (IsNew) {
1208 // This ends the previous live interval. If all of its def / use
1209 // can be folded, give it a low spill weight.
1210 if (NewVReg && TrySplit && AllCanFold) {
1211 LiveInterval &nI = getOrCreateInterval(NewVReg);
1212 nI.weight /= 10.0F;
1213 }
1214 AllCanFold = true;
1215 }
1216 NewVReg = ThisVReg;
1217
Evan Cheng81a03822007-11-17 00:40:40 +00001218 bool HasDef = false;
1219 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001220 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001221 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1222 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001223 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001224 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001225 if (!HasDef && !HasUse)
1226 continue;
1227
Evan Cheng018f9b02007-12-05 03:22:34 +00001228 AllCanFold &= CanFold;
1229
Evan Cheng81a03822007-11-17 00:40:40 +00001230 // Update weight of spill interval.
1231 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001232 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001233 // The spill weight is now infinity as it cannot be spilled again.
1234 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001235 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001236 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001237
1238 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001239 if (HasDef) {
1240 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001241 bool HasKill = false;
1242 if (!HasUse)
1243 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1244 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001245 // If this is a two-address code, then this index starts a new VNInfo.
1246 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001247 if (VNI)
1248 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1249 }
Evan Chenge3110d02007-12-01 04:42:39 +00001250 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1251 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001252 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001253 if (SII == SpillIdxes.end()) {
1254 std::vector<SRInfo> S;
1255 S.push_back(SRInfo(index, NewVReg, true));
1256 SpillIdxes.insert(std::make_pair(MBBId, S));
1257 } else if (SII->second.back().vreg != NewVReg) {
1258 SII->second.push_back(SRInfo(index, NewVReg, true));
1259 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001260 // If there is an earlier def and this is a two-address
1261 // instruction, then it's not possible to fold the store (which
1262 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001263 SRInfo &Info = SII->second.back();
1264 Info.index = index;
1265 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001266 }
1267 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001268 } else if (SII != SpillIdxes.end() &&
1269 SII->second.back().vreg == NewVReg &&
1270 (int)index > SII->second.back().index) {
1271 // There is an earlier def that's not killed (must be two-address).
1272 // The spill is no longer needed.
1273 SII->second.pop_back();
1274 if (SII->second.empty()) {
1275 SpillIdxes.erase(MBBId);
1276 SpillMBBs.reset(MBBId);
1277 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001278 }
1279 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001280 }
1281
1282 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001283 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001284 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001285 if (SII != SpillIdxes.end() &&
1286 SII->second.back().vreg == NewVReg &&
1287 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001288 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001289 SII->second.back().canFold = false;
1290 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001291 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001292 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001293 // If we are splitting live intervals, only fold if it's the first
1294 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001295 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001296 else if (IsNew) {
1297 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001298 if (RII == RestoreIdxes.end()) {
1299 std::vector<SRInfo> Infos;
1300 Infos.push_back(SRInfo(index, NewVReg, true));
1301 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1302 } else {
1303 RII->second.push_back(SRInfo(index, NewVReg, true));
1304 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001305 RestoreMBBs.set(MBBId);
1306 }
1307 }
1308
1309 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001310 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001311 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001312 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001313
1314 if (NewVReg && TrySplit && AllCanFold) {
1315 // If all of its def / use can be folded, give it a low spill weight.
1316 LiveInterval &nI = getOrCreateInterval(NewVReg);
1317 nI.weight /= 10.0F;
1318 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001319}
1320
Evan Cheng1953d0c2007-11-29 10:12:14 +00001321bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1322 BitVector &RestoreMBBs,
1323 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1324 if (!RestoreMBBs[Id])
1325 return false;
1326 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1327 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1328 if (Restores[i].index == index &&
1329 Restores[i].vreg == vr &&
1330 Restores[i].canFold)
1331 return true;
1332 return false;
1333}
1334
1335void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1336 BitVector &RestoreMBBs,
1337 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1338 if (!RestoreMBBs[Id])
1339 return;
1340 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1341 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1342 if (Restores[i].index == index && Restores[i].vreg)
1343 Restores[i].index = -1;
1344}
Evan Cheng81a03822007-11-17 00:40:40 +00001345
Evan Cheng4cce6b42008-04-11 17:53:36 +00001346/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1347/// spilled and create empty intervals for their uses.
1348void
1349LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1350 const TargetRegisterClass* rc,
1351 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001352 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1353 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001354 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001355 MachineInstr *MI = &*ri;
1356 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001357 if (O.isDef()) {
1358 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1359 "Register def was not rewritten?");
1360 RemoveMachineInstrFromMaps(MI);
1361 vrm.RemoveMachineInstrFromMaps(MI);
1362 MI->eraseFromParent();
1363 } else {
1364 // This must be an use of an implicit_def so it's not part of the live
1365 // interval. Create a new empty live interval for it.
1366 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1367 unsigned NewVReg = mri_->createVirtualRegister(rc);
1368 vrm.grow();
1369 vrm.setIsImplicitlyDefined(NewVReg);
1370 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1371 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1372 MachineOperand &MO = MI->getOperand(i);
1373 if (MO.isReg() && MO.getReg() == li.reg)
1374 MO.setReg(NewVReg);
1375 }
1376 }
Evan Cheng419852c2008-04-03 16:39:43 +00001377 }
1378}
1379
Evan Cheng81a03822007-11-17 00:40:40 +00001380
Evan Chengf2fbca62007-11-12 06:35:08 +00001381std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001382addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001383 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001384 // Since this is called after the analysis is done we don't know if
1385 // LiveVariables is available
1386 lv_ = getAnalysisToUpdate<LiveVariables>();
1387
1388 assert(li.weight != HUGE_VALF &&
1389 "attempt to spill already spilled interval!");
1390
1391 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001392 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001393 DOUT << '\n';
1394
Evan Cheng81a03822007-11-17 00:40:40 +00001395 // Each bit specify whether it a spill is required in the MBB.
1396 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001397 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001398 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001399 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1400 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001401 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001402 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001403
1404 unsigned NumValNums = li.getNumValNums();
1405 SmallVector<MachineInstr*, 4> ReMatDefs;
1406 ReMatDefs.resize(NumValNums, NULL);
1407 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1408 ReMatOrigDefs.resize(NumValNums, NULL);
1409 SmallVector<int, 4> ReMatIds;
1410 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1411 BitVector ReMatDelete(NumValNums);
1412 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1413
Evan Cheng81a03822007-11-17 00:40:40 +00001414 // Spilling a split live interval. It cannot be split any further. Also,
1415 // it's also guaranteed to be a single val# / range interval.
1416 if (vrm.getPreSplitReg(li.reg)) {
1417 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001418 // Unset the split kill marker on the last use.
1419 unsigned KillIdx = vrm.getKillPoint(li.reg);
1420 if (KillIdx) {
1421 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1422 assert(KillMI && "Last use disappeared?");
1423 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1424 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001425 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001426 }
Evan Chengadf85902007-12-05 09:51:10 +00001427 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001428 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1429 Slot = vrm.getStackSlot(li.reg);
1430 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1431 MachineInstr *ReMatDefMI = DefIsReMat ?
1432 vrm.getReMaterializedMI(li.reg) : NULL;
1433 int LdSlot = 0;
1434 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1435 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001436 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001437 bool IsFirstRange = true;
1438 for (LiveInterval::Ranges::const_iterator
1439 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1440 // If this is a split live interval with multiple ranges, it means there
1441 // are two-address instructions that re-defined the value. Only the
1442 // first def can be rematerialized!
1443 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001444 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001445 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1446 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001447 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001448 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001449 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001450 } else {
1451 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1452 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001453 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001454 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001455 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001456 }
1457 IsFirstRange = false;
1458 }
Evan Cheng419852c2008-04-03 16:39:43 +00001459
Evan Cheng4cce6b42008-04-11 17:53:36 +00001460 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001461 return NewLIs;
1462 }
1463
1464 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001465 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1466 TrySplit = false;
1467 if (TrySplit)
1468 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001469 bool NeedStackSlot = false;
1470 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1471 i != e; ++i) {
1472 const VNInfo *VNI = *i;
1473 unsigned VN = VNI->id;
1474 unsigned DefIdx = VNI->def;
1475 if (DefIdx == ~1U)
1476 continue; // Dead val#.
1477 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001478 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1479 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001480 bool dummy;
1481 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001482 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001483 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001484 // Original def may be modified so we have to make a copy here. vrm must
1485 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001486 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001487
1488 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001489 if (VNI->hasPHIKill) {
1490 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001491 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001492 CanDelete = false;
1493 // Need a stack slot if there is any live range where uses cannot be
1494 // rematerialized.
1495 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001496 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001497 if (CanDelete)
1498 ReMatDelete.set(VN);
1499 } else {
1500 // Need a stack slot if there is any live range where uses cannot be
1501 // rematerialized.
1502 NeedStackSlot = true;
1503 }
1504 }
1505
1506 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001507 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001508 Slot = vrm.assignVirt2StackSlot(li.reg);
1509
1510 // Create new intervals and rewrite defs and uses.
1511 for (LiveInterval::Ranges::const_iterator
1512 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001513 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1514 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1515 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001516 bool CanDelete = ReMatDelete[I->valno->id];
1517 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001518 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001519 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001520 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001521 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001522 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001523 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001524 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001525 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001526 }
1527
Evan Cheng0cbb1162007-11-29 01:06:25 +00001528 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001529 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001530 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001531 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001532 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001533
Evan Chengb50bb8c2007-12-05 08:16:32 +00001534 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001535 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001536 if (NeedStackSlot) {
1537 int Id = SpillMBBs.find_first();
1538 while (Id != -1) {
1539 std::vector<SRInfo> &spills = SpillIdxes[Id];
1540 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1541 int index = spills[i].index;
1542 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001543 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001544 bool isReMat = vrm.isReMaterialized(VReg);
1545 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001546 bool CanFold = false;
1547 bool FoundUse = false;
1548 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001549 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001550 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001551 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1552 MachineOperand &MO = MI->getOperand(j);
1553 if (!MO.isRegister() || MO.getReg() != VReg)
1554 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001555
1556 Ops.push_back(j);
1557 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001558 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001559 if (isReMat ||
1560 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1561 RestoreMBBs, RestoreIdxes))) {
1562 // MI has two-address uses of the same register. If the use
1563 // isn't the first and only use in the BB, then we can't fold
1564 // it. FIXME: Move this to rewriteInstructionsForSpills.
1565 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001566 break;
1567 }
Evan Chengaee4af62007-12-02 08:30:39 +00001568 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001569 }
1570 }
1571 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001572 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001573 if (CanFold && !Ops.empty()) {
1574 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001575 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001576 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001577 // Also folded uses, do not issue a load.
1578 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001579 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1580 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001581 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001582 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001583 }
1584
Evan Cheng7e073ba2008-04-09 20:57:25 +00001585 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001586 if (!Folded) {
1587 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1588 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001589 if (!MI->registerDefIsDead(nI.reg))
1590 // No need to spill a dead def.
1591 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001592 if (isKill)
1593 AddedKill.insert(&nI);
1594 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001595 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001596 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001597 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001598 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001599
Evan Cheng1953d0c2007-11-29 10:12:14 +00001600 int Id = RestoreMBBs.find_first();
1601 while (Id != -1) {
1602 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1603 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1604 int index = restores[i].index;
1605 if (index == -1)
1606 continue;
1607 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001608 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001609 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001610 bool CanFold = false;
1611 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001612 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001613 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001614 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1615 MachineOperand &MO = MI->getOperand(j);
1616 if (!MO.isRegister() || MO.getReg() != VReg)
1617 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001618
Evan Cheng0cbb1162007-11-29 01:06:25 +00001619 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001620 // If this restore were to be folded, it would have been folded
1621 // already.
1622 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001623 break;
1624 }
Evan Chengaee4af62007-12-02 08:30:39 +00001625 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001626 }
1627 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001628
1629 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001630 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001631 if (CanFold && !Ops.empty()) {
1632 if (!vrm.isReMaterialized(VReg))
1633 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1634 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001635 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1636 int LdSlot = 0;
1637 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1638 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001639 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001640 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1641 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001642 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1643 if (ImpUse) {
1644 // Re-matting an instruction with virtual register use. Add the
1645 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001646 // interval's spill weight to HUGE_VALF to prevent it from being
1647 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001648 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001649 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001650 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1651 }
Evan Chengaee4af62007-12-02 08:30:39 +00001652 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001653 }
1654 // If folding is not possible / failed, then tell the spiller to issue a
1655 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001656 if (Folded)
1657 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001658 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001659 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001660 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001661 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001662 }
1663
Evan Chengb50bb8c2007-12-05 08:16:32 +00001664 // Finalize intervals: add kills, finalize spill weights, and filter out
1665 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001666 std::vector<LiveInterval*> RetNewLIs;
1667 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1668 LiveInterval *LI = NewLIs[i];
1669 if (!LI->empty()) {
1670 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001671 if (!AddedKill.count(LI)) {
1672 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001673 unsigned LastUseIdx = getBaseIndex(LR->end);
1674 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001675 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001676 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001677 if (LastUse->getOperand(UseIdx).isImplicit() ||
1678 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001679 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001680 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001681 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001682 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001683 RetNewLIs.push_back(LI);
1684 }
1685 }
Evan Cheng81a03822007-11-17 00:40:40 +00001686
Evan Cheng4cce6b42008-04-11 17:53:36 +00001687 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001688 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001689}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001690
1691/// hasAllocatableSuperReg - Return true if the specified physical register has
1692/// any super register that's allocatable.
1693bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1694 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1695 if (allocatableRegs_[*AS] && hasInterval(*AS))
1696 return true;
1697 return false;
1698}
1699
1700/// getRepresentativeReg - Find the largest super register of the specified
1701/// physical register.
1702unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1703 // Find the largest super-register that is allocatable.
1704 unsigned BestReg = Reg;
1705 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1706 unsigned SuperReg = *AS;
1707 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1708 BestReg = SuperReg;
1709 break;
1710 }
1711 }
1712 return BestReg;
1713}
1714
1715/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1716/// specified interval that conflicts with the specified physical register.
1717unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1718 unsigned PhysReg) const {
1719 unsigned NumConflicts = 0;
1720 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1721 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1722 E = mri_->reg_end(); I != E; ++I) {
1723 MachineOperand &O = I.getOperand();
1724 MachineInstr *MI = O.getParent();
1725 unsigned Index = getInstructionIndex(MI);
1726 if (pli.liveAt(Index))
1727 ++NumConflicts;
1728 }
1729 return NumConflicts;
1730}
1731
1732/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1733/// around all defs and uses of the specified interval.
1734void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1735 unsigned PhysReg, VirtRegMap &vrm) {
1736 unsigned SpillReg = getRepresentativeReg(PhysReg);
1737
1738 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1739 // If there are registers which alias PhysReg, but which are not a
1740 // sub-register of the chosen representative super register. Assert
1741 // since we can't handle it yet.
1742 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1743 tri_->isSuperRegister(*AS, SpillReg));
1744
1745 LiveInterval &pli = getInterval(SpillReg);
1746 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1747 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1748 E = mri_->reg_end(); I != E; ++I) {
1749 MachineOperand &O = I.getOperand();
1750 MachineInstr *MI = O.getParent();
1751 if (SeenMIs.count(MI))
1752 continue;
1753 SeenMIs.insert(MI);
1754 unsigned Index = getInstructionIndex(MI);
1755 if (pli.liveAt(Index)) {
1756 vrm.addEmergencySpill(SpillReg, MI);
1757 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1758 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1759 if (!hasInterval(*AS))
1760 continue;
1761 LiveInterval &spli = getInterval(*AS);
1762 if (spli.liveAt(Index))
1763 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1764 }
1765 }
1766 }
1767}