blob: 54738539fa9f12d6b09406a184e6f4557e82d6cf [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Owen Andersonfcc63502008-05-29 18:35:21 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000068 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mi2iMap_.clear();
70 i2miMap_.clear();
71 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000072 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
73 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000074 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
75 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000076}
77
Owen Anderson80b3ce62008-05-28 20:54:50 +000078void LiveIntervals::computeNumbering() {
79 Index2MiMap OldI2MI = i2miMap_;
80
81 Idx2MBBMap.clear();
82 MBB2IdxMap.clear();
83 mi2iMap_.clear();
84 i2miMap_.clear();
85
Chris Lattner428b92e2006-09-15 03:57:23 +000086 // Number MachineInstrs and MachineBasicBlocks.
87 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000088 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000089
90 unsigned MIIndex = 0;
91 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
92 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000093 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000094
Chris Lattner428b92e2006-09-15 03:57:23 +000095 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
96 I != E; ++I) {
97 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +000099 i2miMap_.push_back(I);
100 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000101 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000102
103 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000104 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
105 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
106 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000107 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000109 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Owen Anderson80b3ce62008-05-28 20:54:50 +0000110
111 if (!OldI2MI.empty())
112 for (iterator I = begin(), E = end(); I != E; ++I)
113 for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
114 LI != LE; ++LI) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000115
Owen Anderson7eec0c22008-05-29 23:01:22 +0000116 // Remap the start index of the live range to the corresponding new
117 // number, or our best guess at what it _should_ correspond to if the
118 // original instruction has been erased. This is either the following
119 // instruction or its predecessor.
120 unsigned offset = LI->start % InstrSlots::NUM;
121 if (OldI2MI[LI->start / InstrSlots::NUM])
122 LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
123 else {
124 unsigned i = 0;
125 MachineInstr* newInstr = 0;
126 do {
127 newInstr = OldI2MI[LI->start / InstrSlots::NUM + i];
128 i++;
129 } while (!newInstr);
130
Owen Andersone3abb0a2008-06-02 17:36:36 +0000131 if (mi2iMap_[newInstr] ==
132 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
Owen Anderson7eec0c22008-05-29 23:01:22 +0000133 LI->start = mi2iMap_[newInstr];
Owen Andersone3abb0a2008-06-02 17:36:36 +0000134 else
135 LI->start = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000136 }
137
138 // Remap the ending index in the same way that we remapped the start,
139 // except for the final step where we always map to the immediately
140 // following instruction.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000141 if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
Owen Anderson4b5b2092008-05-29 18:15:49 +0000142 offset = LI->end % InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000143 if (OldI2MI[LI->end / InstrSlots::NUM])
144 LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
145 else {
146 unsigned i = 0;
147 MachineInstr* newInstr = 0;
148 do {
149 newInstr = OldI2MI[LI->end / InstrSlots::NUM + i];
150 i++;
151 } while (!newInstr);
152
153 LI->end = mi2iMap_[newInstr];
154 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000155 } else {
156 LI->end = i2miMap_.size() * InstrSlots::NUM;
157 }
Owen Anderson745825f42008-05-28 22:40:08 +0000158
Owen Anderson7eec0c22008-05-29 23:01:22 +0000159 // Remap the VNInfo def index, which works the same as the
160 // start indices above.
Owen Anderson745825f42008-05-28 22:40:08 +0000161 VNInfo* vni = LI->valno;
Owen Anderson4b5b2092008-05-29 18:15:49 +0000162 offset = vni->def % InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000163 if (OldI2MI[vni->def / InstrSlots::NUM])
164 vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
165 else {
166 unsigned i = 0;
167 MachineInstr* newInstr = 0;
168 do {
169 newInstr = OldI2MI[vni->def / InstrSlots::NUM + i];
170 i++;
171 } while (!newInstr);
172
Owen Andersone3abb0a2008-06-02 17:36:36 +0000173 if (mi2iMap_[newInstr] ==
174 MBB2IdxMap[newInstr->getParent()->getNumber()].first)
Owen Anderson7eec0c22008-05-29 23:01:22 +0000175 vni->def = mi2iMap_[newInstr];
Owen Andersone3abb0a2008-06-02 17:36:36 +0000176 else
177 vni->def = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000178 }
Owen Anderson745825f42008-05-28 22:40:08 +0000179
Owen Anderson7eec0c22008-05-29 23:01:22 +0000180 // Remap the VNInfo kill indices, which works the same as
181 // the end indices above.
Owen Anderson4b5b2092008-05-29 18:15:49 +0000182 for (size_t i = 0; i < vni->kills.size(); ++i) {
183 offset = vni->kills[i] % InstrSlots::NUM;
Owen Anderson7eec0c22008-05-29 23:01:22 +0000184 if (OldI2MI[vni->kills[i] / InstrSlots::NUM])
185 vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
186 offset;
187 else {
188 unsigned e = 0;
189 MachineInstr* newInstr = 0;
190 do {
191 newInstr = OldI2MI[vni->kills[i] / InstrSlots::NUM + e];
192 e++;
193 } while (!newInstr);
194
195 vni->kills[i] = mi2iMap_[newInstr];
196 }
Owen Anderson4b5b2092008-05-29 18:15:49 +0000197 }
Owen Anderson80b3ce62008-05-28 20:54:50 +0000198 }
199}
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000200
Owen Anderson80b3ce62008-05-28 20:54:50 +0000201/// runOnMachineFunction - Register allocate the whole function
202///
203bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
204 mf_ = &fn;
205 mri_ = &mf_->getRegInfo();
206 tm_ = &fn.getTarget();
207 tri_ = tm_->getRegisterInfo();
208 tii_ = tm_->getInstrInfo();
209 lv_ = &getAnalysis<LiveVariables>();
210 allocatableRegs_ = tri_->getAllocatableSet(fn);
211
212 computeNumbering();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000213 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000214
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000215 numIntervals += getNumIntervals();
216
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000217 DOUT << "********** INTERVALS **********\n";
218 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000219 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000220 DOUT << "\n";
221 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000223 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000224 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000225 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000226}
227
Chris Lattner70ca3582004-09-30 15:59:17 +0000228/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000229void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000230 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000231 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000232 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000233 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000234 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000235
236 O << "********** MACHINEINSTRS **********\n";
237 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
238 mbbi != mbbe; ++mbbi) {
239 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
240 for (MachineBasicBlock::iterator mii = mbbi->begin(),
241 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000242 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000243 }
244 }
245}
246
Evan Chengc92da382007-11-03 07:20:12 +0000247/// conflictsWithPhysRegDef - Returns true if the specified register
248/// is defined during the duration of the specified interval.
249bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
250 VirtRegMap &vrm, unsigned reg) {
251 for (LiveInterval::Ranges::const_iterator
252 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
253 for (unsigned index = getBaseIndex(I->start),
254 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
255 index += InstrSlots::NUM) {
256 // skip deleted instructions
257 while (index != end && !getInstructionFromIndex(index))
258 index += InstrSlots::NUM;
259 if (index == end) break;
260
261 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000262 unsigned SrcReg, DstReg;
263 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
264 if (SrcReg == li.reg || DstReg == li.reg)
265 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000266 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
267 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000268 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000269 continue;
270 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000271 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000272 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000273 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000274 if (!vrm.hasPhys(PhysReg))
275 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000276 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000277 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000278 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000279 return true;
280 }
281 }
282 }
283
284 return false;
285}
286
Evan Cheng549f27d32007-08-13 23:45:17 +0000287void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000288 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000289 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000290 else
291 cerr << "%reg" << reg;
292}
293
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000294void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000295 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000296 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000297 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000298 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000300
Evan Cheng419852c2008-04-03 16:39:43 +0000301 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
302 DOUT << "is a implicit_def\n";
303 return;
304 }
305
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000306 // Virtual registers may be defined multiple times (due to phi
307 // elimination and 2-addr elimination). Much of what we do only has to be
308 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000309 // time we see a vreg.
310 if (interval.empty()) {
311 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000312 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000313 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000314 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000315 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000316 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000317 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000318 tii_->isMoveInstr(*mi, SrcReg, DstReg))
319 CopyMI = mi;
320 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000321
322 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000323
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 // Loop over all of the blocks that the vreg is defined in. There are
325 // two cases we have to handle here. The most common case is a vreg
326 // whose lifetime is contained within a basic block. In this case there
327 // will be a single kill, in MBB, which comes after the definition.
328 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
329 // FIXME: what about dead vars?
330 unsigned killIdx;
331 if (vi.Kills[0] != mi)
332 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
333 else
334 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000335
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336 // If the kill happens after the definition, we have an intra-block
337 // live range.
338 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000339 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000340 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000341 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000342 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000343 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000344 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 return;
346 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000347 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000348
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349 // The other case we handle is when a virtual register lives to the end
350 // of the defining block, potentially live across some blocks, then is
351 // live into some number of blocks, but gets killed. Start by adding a
352 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000353 LiveRange NewLR(defIndex,
354 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000355 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000356 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357 interval.addRange(NewLR);
358
359 // Iterate over all of the blocks that the variable is completely
360 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
361 // live interval.
362 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
363 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000364 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
365 if (!MBB->empty()) {
366 LiveRange LR(getMBBStartIdx(i),
367 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000368 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000369 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000370 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 }
372 }
373 }
374
375 // Finally, this virtual register is live from the start of any killing
376 // block to the 'use' slot of the killing instruction.
377 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
378 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000379 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000380 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000381 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000382 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000383 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000384 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 }
386
387 } else {
388 // If this is the second time we see a virtual register definition, it
389 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000390 // the result of two address elimination, then the vreg is one of the
391 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000392 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000393 // If this is a two-address definition, then we have already processed
394 // the live range. The only problem is that we didn't realize there
395 // are actually two values in the live interval. Because of this we
396 // need to take the LiveRegion that defines this register and split it
397 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000398 assert(interval.containsOneValue());
399 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000400 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401
Evan Cheng4f8ff162007-08-11 00:59:19 +0000402 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000403 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000404
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000405 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000406 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000407 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000408
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000409 // Two-address vregs should always only be redefined once. This means
410 // that at this point, there should be exactly one value number in it.
411 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
412
Chris Lattner91725b72006-08-31 05:54:43 +0000413 // The new value number (#1) is defined by the instruction we claimed
414 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000415 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
416 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000417
Chris Lattner91725b72006-08-31 05:54:43 +0000418 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000419 OldValNo->def = RedefIndex;
420 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000421
422 // Add the new live interval which replaces the range for the input copy.
423 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000424 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000425 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000426 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427
428 // If this redefinition is dead, we need to add a dummy unit live
429 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000430 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000431 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000432
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000433 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000434 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435
436 } else {
437 // Otherwise, this must be because of phi elimination. If this is the
438 // first redefinition of the vreg that we have seen, go back and change
439 // the live range in the PHI block to be a different value number.
440 if (interval.containsOneValue()) {
441 assert(vi.Kills.size() == 1 &&
442 "PHI elimination vreg should have one kill, the PHI itself!");
443
444 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000445 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000446 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000447 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000448 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000449 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000450 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000451 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000452 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000453 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000454
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000455 // Replace the interval with one of a NEW value number. Note that this
456 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000457 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000458 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000459 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000460 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000461 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000462 }
463
464 // In the case of PHI elimination, each variable definition is only
465 // live until the end of the block. We've already taken care of the
466 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000467 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000468
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000469 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000470 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000471 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000472 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000473 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000474 tii_->isMoveInstr(*mi, SrcReg, DstReg))
475 CopyMI = mi;
476 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000477
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000478 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000479 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000480 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000481 interval.addKill(ValNo, killIndex);
482 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000483 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000484 }
485 }
486
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000487 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000488}
489
Chris Lattnerf35fef72004-07-23 21:24:19 +0000490void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000491 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000492 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000493 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000494 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000495 // A physical register cannot be live across basic block, so its
496 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000497 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000498
Chris Lattner6b128bd2006-09-03 08:07:11 +0000499 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000500 unsigned start = getDefIndex(baseIndex);
501 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000502
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000503 // If it is not used after definition, it is considered dead at
504 // the instruction defining it. Hence its interval is:
505 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000506 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000507 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000508 end = getDefIndex(start) + 1;
509 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 }
511
512 // If it is not dead on definition, it must be killed by a
513 // subsequent instruction. Hence its interval is:
514 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000515 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000516 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000517 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000518 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000519 end = getUseIndex(baseIndex) + 1;
520 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000521 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000522 // Another instruction redefines the register before it is ever read.
523 // Then the register is essentially dead at the instruction that defines
524 // it. Hence its interval is:
525 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000526 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000527 end = getDefIndex(start) + 1;
528 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000529 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000530 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000531
532 // The only case we should have a dead physreg here without a killing or
533 // instruction where we know it's dead is if it is live-in to the function
534 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000535 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000536 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000537
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000538exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000540
Evan Cheng24a3cc42007-04-25 07:30:23 +0000541 // Already exists? Extend old live interval.
542 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000543 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000544 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000547 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000548 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000549}
550
Chris Lattnerf35fef72004-07-23 21:24:19 +0000551void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
552 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000553 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000554 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000555 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000556 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000557 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000558 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000559 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000560 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000561 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000562 tii_->isMoveInstr(*MI, SrcReg, DstReg))
563 CopyMI = MI;
564 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000565 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000566 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000567 // If MI also modifies the sub-register explicitly, avoid processing it
568 // more than once. Do not pass in TRI here so it checks for exact match.
569 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000570 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000571 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000572}
573
Evan Chengb371f452007-02-19 21:49:54 +0000574void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000575 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000576 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000577 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
578
579 // Look for kills, if it reaches a def before it's killed, then it shouldn't
580 // be considered a livein.
581 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000582 unsigned baseIndex = MIIdx;
583 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000584 unsigned end = start;
585 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000586 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000587 DOUT << " killed";
588 end = getUseIndex(baseIndex) + 1;
589 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000590 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000591 // Another instruction redefines the register before it is ever read.
592 // Then the register is essentially dead at the instruction that defines
593 // it. Hence its interval is:
594 // [defSlot(def), defSlot(def)+1)
595 DOUT << " dead";
596 end = getDefIndex(start) + 1;
597 goto exit;
598 }
599
600 baseIndex += InstrSlots::NUM;
601 ++mi;
602 }
603
604exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000605 // Live-in register might not be used at all.
606 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000607 if (isAlias) {
608 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000609 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000610 } else {
611 DOUT << " live through";
612 end = baseIndex;
613 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000614 }
615
Evan Chengf3bb2e62007-09-05 21:46:51 +0000616 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000617 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000618 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000619 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000620}
621
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000622/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000623/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000624/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000625/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000626void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000627 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
628 << "********** Function: "
629 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000630 // Track the index of the current machine instr.
631 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000632 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
633 MBBI != E; ++MBBI) {
634 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000635 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000636
Chris Lattner428b92e2006-09-15 03:57:23 +0000637 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000638
Dan Gohmancb406c22007-10-03 19:26:29 +0000639 // Create intervals for live-ins to this BB first.
640 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
641 LE = MBB->livein_end(); LI != LE; ++LI) {
642 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
643 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000644 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000645 if (!hasInterval(*AS))
646 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
647 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000648 }
649
Chris Lattner428b92e2006-09-15 03:57:23 +0000650 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000651 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000652
Evan Cheng438f7bc2006-11-10 08:43:01 +0000653 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000654 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
655 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000656 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000657 if (MO.isRegister() && MO.getReg() && MO.isDef())
658 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000660
661 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000662 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000663 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000664}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000665
Evan Cheng4ca980e2007-10-17 02:10:22 +0000666bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000667 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000668 std::vector<IdxMBBPair>::const_iterator I =
669 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
670
671 bool ResVal = false;
672 while (I != Idx2MBBMap.end()) {
673 if (LR.end <= I->first)
674 break;
675 MBBs.push_back(I->second);
676 ResVal = true;
677 ++I;
678 }
679 return ResVal;
680}
681
682
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000683LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000684 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000685 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000686 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000687}
Evan Chengf2fbca62007-11-12 06:35:08 +0000688
Evan Chengc8d044e2008-02-15 18:24:29 +0000689/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
690/// copy field and returns the source register that defines it.
691unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
692 if (!VNI->copy)
693 return 0;
694
695 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
696 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000697 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
698 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000699 unsigned SrcReg, DstReg;
700 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
701 return SrcReg;
702 assert(0 && "Unrecognized copy instruction!");
703 return 0;
704}
Evan Chengf2fbca62007-11-12 06:35:08 +0000705
706//===----------------------------------------------------------------------===//
707// Register allocator hooks.
708//
709
Evan Chengd70dbb52008-02-22 09:24:50 +0000710/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
711/// allow one) virtual register operand, then its uses are implicitly using
712/// the register. Returns the virtual register.
713unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
714 MachineInstr *MI) const {
715 unsigned RegOp = 0;
716 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
717 MachineOperand &MO = MI->getOperand(i);
718 if (!MO.isRegister() || !MO.isUse())
719 continue;
720 unsigned Reg = MO.getReg();
721 if (Reg == 0 || Reg == li.reg)
722 continue;
723 // FIXME: For now, only remat MI with at most one register operand.
724 assert(!RegOp &&
725 "Can't rematerialize instruction with multiple register operand!");
726 RegOp = MO.getReg();
727 break;
728 }
729 return RegOp;
730}
731
732/// isValNoAvailableAt - Return true if the val# of the specified interval
733/// which reaches the given instruction also reaches the specified use index.
734bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
735 unsigned UseIdx) const {
736 unsigned Index = getInstructionIndex(MI);
737 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
738 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
739 return UI != li.end() && UI->valno == ValNo;
740}
741
Evan Chengf2fbca62007-11-12 06:35:08 +0000742/// isReMaterializable - Returns true if the definition MI of the specified
743/// val# of the specified interval is re-materializable.
744bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000745 const VNInfo *ValNo, MachineInstr *MI,
746 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000747 if (DisableReMat)
748 return false;
749
Evan Cheng5ef3a042007-12-06 00:01:56 +0000750 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000751 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000752 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000753
754 int FrameIdx = 0;
755 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000756 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000757 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
758 // this but remember this is not safe to fold into a two-address
759 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000760 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000761 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000762
Evan Chengd70dbb52008-02-22 09:24:50 +0000763 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000764 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000765 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000766
767 unsigned ImpUse = getReMatImplicitUse(li, MI);
768 if (ImpUse) {
769 const LiveInterval &ImpLi = getInterval(ImpUse);
770 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
771 re = mri_->use_end(); ri != re; ++ri) {
772 MachineInstr *UseMI = &*ri;
773 unsigned UseIdx = getInstructionIndex(UseMI);
774 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
775 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000776 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000777 return false;
778 }
779 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000780 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000781 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000782
Evan Chengdd3465e2008-02-23 01:44:27 +0000783 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000784}
785
786/// isReMaterializable - Returns true if every definition of MI of every
787/// val# of the specified interval is re-materializable.
788bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
789 isLoad = false;
790 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
791 i != e; ++i) {
792 const VNInfo *VNI = *i;
793 unsigned DefIdx = VNI->def;
794 if (DefIdx == ~1U)
795 continue; // Dead val#.
796 // Is the def for the val# rematerializable?
797 if (DefIdx == ~0u)
798 return false;
799 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
800 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000801 if (!ReMatDefMI ||
802 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000803 return false;
804 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000805 }
806 return true;
807}
808
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000809/// FilterFoldedOps - Filter out two-address use operands. Return
810/// true if it finds any issue with the operands that ought to prevent
811/// folding.
812static bool FilterFoldedOps(MachineInstr *MI,
813 SmallVector<unsigned, 2> &Ops,
814 unsigned &MRInfo,
815 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000816 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000817
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000818 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000819 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
820 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000821 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000822 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000823 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000824 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000825 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000826 MRInfo |= (unsigned)VirtRegMap::isMod;
827 else {
828 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000829 if (!MO.isImplicit() &&
830 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000831 MRInfo = VirtRegMap::isModRef;
832 continue;
833 }
834 MRInfo |= (unsigned)VirtRegMap::isRef;
835 }
836 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000837 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000838 return false;
839}
840
841
842/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
843/// slot / to reg or any rematerialized load into ith operand of specified
844/// MI. If it is successul, MI is updated with the newly created MI and
845/// returns true.
846bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
847 VirtRegMap &vrm, MachineInstr *DefMI,
848 unsigned InstrIdx,
849 SmallVector<unsigned, 2> &Ops,
850 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000851 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000852 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000853 RemoveMachineInstrFromMaps(MI);
854 vrm.RemoveMachineInstrFromMaps(MI);
855 MI->eraseFromParent();
856 ++numFolds;
857 return true;
858 }
859
860 // Filter the list of operand indexes that are to be folded. Abort if
861 // any operand will prevent folding.
862 unsigned MRInfo = 0;
863 SmallVector<unsigned, 2> FoldOps;
864 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
865 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000866
Evan Cheng427f4c12008-03-31 23:19:51 +0000867 // The only time it's safe to fold into a two address instruction is when
868 // it's folding reload and spill from / into a spill stack slot.
869 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000870 return false;
871
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000872 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
873 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000874 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000875 // Remember this instruction uses the spill slot.
876 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
877
Evan Chengf2fbca62007-11-12 06:35:08 +0000878 // Attempt to fold the memory reference into the instruction. If
879 // we can do this, we don't need to insert spill code.
880 if (lv_)
881 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000882 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000883 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000884 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000885 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000886 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000887 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000888 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000889 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000890 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000891 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
892 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000893 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000894 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000895 return true;
896 }
897 return false;
898}
899
Evan Cheng018f9b02007-12-05 03:22:34 +0000900/// canFoldMemoryOperand - Returns true if the specified load / store
901/// folding is possible.
902bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000903 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000904 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000905 // Filter the list of operand indexes that are to be folded. Abort if
906 // any operand will prevent folding.
907 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000908 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000909 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
910 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000911
Evan Cheng3c75ba82008-04-01 21:37:32 +0000912 // It's only legal to remat for a use, not a def.
913 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000914 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000915
Evan Chengd70dbb52008-02-22 09:24:50 +0000916 return tii_->canFoldMemoryOperand(MI, FoldOps);
917}
918
Evan Cheng81a03822007-11-17 00:40:40 +0000919bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
920 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
921 for (LiveInterval::Ranges::const_iterator
922 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
923 std::vector<IdxMBBPair>::const_iterator II =
924 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
925 if (II == Idx2MBBMap.end())
926 continue;
927 if (I->end > II->first) // crossing a MBB.
928 return false;
929 MBBs.insert(II->second);
930 if (MBBs.size() > 1)
931 return false;
932 }
933 return true;
934}
935
Evan Chengd70dbb52008-02-22 09:24:50 +0000936/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
937/// interval on to-be re-materialized operands of MI) with new register.
938void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
939 MachineInstr *MI, unsigned NewVReg,
940 VirtRegMap &vrm) {
941 // There is an implicit use. That means one of the other operand is
942 // being remat'ed and the remat'ed instruction has li.reg as an
943 // use operand. Make sure we rewrite that as well.
944 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
945 MachineOperand &MO = MI->getOperand(i);
946 if (!MO.isRegister())
947 continue;
948 unsigned Reg = MO.getReg();
949 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
950 continue;
951 if (!vrm.isReMaterialized(Reg))
952 continue;
953 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000954 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
955 if (UseMO)
956 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000957 }
958}
959
Evan Chengf2fbca62007-11-12 06:35:08 +0000960/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
961/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000962bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000963rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
964 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000965 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000966 unsigned Slot, int LdSlot,
967 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000968 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000969 const TargetRegisterClass* rc,
970 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000971 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000972 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000973 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000974 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000975 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000976 RestartInstruction:
977 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
978 MachineOperand& mop = MI->getOperand(i);
979 if (!mop.isRegister())
980 continue;
981 unsigned Reg = mop.getReg();
982 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000983 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000984 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000985 if (Reg != li.reg)
986 continue;
987
988 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000989 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000990 int FoldSlot = Slot;
991 if (DefIsReMat) {
992 // If this is the rematerializable definition MI itself and
993 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000994 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000995 DOUT << "\t\t\t\tErasing re-materlizable def: ";
996 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000997 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000998 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000999 MI->eraseFromParent();
1000 break;
1001 }
1002
1003 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001004 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001005 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001006 if (isLoad) {
1007 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1008 FoldSS = isLoadSS;
1009 FoldSlot = LdSlot;
1010 }
1011 }
1012
Evan Chengf2fbca62007-11-12 06:35:08 +00001013 // Scan all of the operands of this instruction rewriting operands
1014 // to use NewVReg instead of li.reg as appropriate. We do this for
1015 // two reasons:
1016 //
1017 // 1. If the instr reads the same spilled vreg multiple times, we
1018 // want to reuse the NewVReg.
1019 // 2. If the instr is a two-addr instruction, we are required to
1020 // keep the src/dst regs pinned.
1021 //
1022 // Keep track of whether we replace a use and/or def so that we can
1023 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001024
Evan Cheng81a03822007-11-17 00:40:40 +00001025 HasUse = mop.isUse();
1026 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001027 SmallVector<unsigned, 2> Ops;
1028 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001029 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001030 const MachineOperand &MOj = MI->getOperand(j);
1031 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +00001032 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001033 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001034 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001035 continue;
1036 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001037 Ops.push_back(j);
1038 HasUse |= MOj.isUse();
1039 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +00001040 }
1041 }
1042
Evan Cheng018f9b02007-12-05 03:22:34 +00001043 if (TryFold) {
1044 // Do not fold load / store here if we are splitting. We'll find an
1045 // optimal point to insert a load / store later.
1046 if (!TrySplit) {
1047 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1048 Ops, FoldSS, FoldSlot, Reg)) {
1049 // Folding the load/store can completely change the instruction in
1050 // unpredictable ways, rescan it from the beginning.
1051 HasUse = false;
1052 HasDef = false;
1053 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +00001054 if (isRemoved(MI))
1055 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001056 goto RestartInstruction;
1057 }
1058 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +00001059 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001060 }
Evan Cheng6e141fd2007-12-12 23:12:09 +00001061 } else
1062 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001063
1064 // Create a new virtual register for the spill interval.
1065 bool CreatedNewVReg = false;
1066 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +00001067 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +00001068 vrm.grow();
1069 CreatedNewVReg = true;
1070 }
1071 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001072 if (mop.isImplicit())
1073 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001074
1075 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001076 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1077 MachineOperand &mopj = MI->getOperand(Ops[j]);
1078 mopj.setReg(NewVReg);
1079 if (mopj.isImplicit())
1080 rewriteImplicitOps(li, MI, NewVReg, vrm);
1081 }
Evan Chengcddbb832007-11-30 21:23:43 +00001082
Evan Cheng81a03822007-11-17 00:40:40 +00001083 if (CreatedNewVReg) {
1084 if (DefIsReMat) {
1085 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +00001086 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001087 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001088 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001089 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001090 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001091 }
1092 if (!CanDelete || (HasUse && HasDef)) {
1093 // If this is a two-addr instruction then its use operands are
1094 // rematerializable but its def is not. It should be assigned a
1095 // stack slot.
1096 vrm.assignVirt2StackSlot(NewVReg, Slot);
1097 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001098 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001099 vrm.assignVirt2StackSlot(NewVReg, Slot);
1100 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001101 } else if (HasUse && HasDef &&
1102 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1103 // If this interval hasn't been assigned a stack slot (because earlier
1104 // def is a deleted remat def), do it now.
1105 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1106 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001107 }
1108
Evan Cheng313d4b82008-02-23 00:33:04 +00001109 // Re-matting an instruction with virtual register use. Add the
1110 // register as an implicit use on the use MI.
1111 if (DefIsReMat && ImpUse)
1112 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1113
Evan Chengf2fbca62007-11-12 06:35:08 +00001114 // create a new register interval for this spill / remat.
1115 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001116 if (CreatedNewVReg) {
1117 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001118 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001119 if (TrySplit)
1120 vrm.setIsSplitFromReg(NewVReg, li.reg);
1121 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001122
1123 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001124 if (CreatedNewVReg) {
1125 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1126 nI.getNextValue(~0U, 0, VNInfoAllocator));
1127 DOUT << " +" << LR;
1128 nI.addRange(LR);
1129 } else {
1130 // Extend the split live interval to this def / use.
1131 unsigned End = getUseIndex(index)+1;
1132 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1133 nI.getValNumInfo(nI.getNumValNums()-1));
1134 DOUT << " +" << LR;
1135 nI.addRange(LR);
1136 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001137 }
1138 if (HasDef) {
1139 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1140 nI.getNextValue(~0U, 0, VNInfoAllocator));
1141 DOUT << " +" << LR;
1142 nI.addRange(LR);
1143 }
Evan Cheng81a03822007-11-17 00:40:40 +00001144
Evan Chengf2fbca62007-11-12 06:35:08 +00001145 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001146 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001147 DOUT << '\n';
1148 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001149 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001150}
Evan Cheng81a03822007-11-17 00:40:40 +00001151bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001152 const VNInfo *VNI,
1153 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001154 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001155 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1156 unsigned KillIdx = VNI->kills[j];
1157 if (KillIdx > Idx && KillIdx < End)
1158 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001159 }
1160 return false;
1161}
1162
Evan Cheng1953d0c2007-11-29 10:12:14 +00001163static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1164 const VNInfo *VNI = NULL;
1165 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1166 e = li.vni_end(); i != e; ++i)
1167 if ((*i)->def == DefIdx) {
1168 VNI = *i;
1169 break;
1170 }
1171 return VNI;
1172}
1173
Evan Cheng063284c2008-02-21 00:34:19 +00001174/// RewriteInfo - Keep track of machine instrs that will be rewritten
1175/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001176namespace {
1177 struct RewriteInfo {
1178 unsigned Index;
1179 MachineInstr *MI;
1180 bool HasUse;
1181 bool HasDef;
1182 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1183 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1184 };
Evan Cheng063284c2008-02-21 00:34:19 +00001185
Dan Gohman844731a2008-05-13 00:00:25 +00001186 struct RewriteInfoCompare {
1187 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1188 return LHS.Index < RHS.Index;
1189 }
1190 };
1191}
Evan Cheng063284c2008-02-21 00:34:19 +00001192
Evan Chengf2fbca62007-11-12 06:35:08 +00001193void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001194rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001195 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001196 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001197 unsigned Slot, int LdSlot,
1198 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001199 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001200 const TargetRegisterClass* rc,
1201 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001202 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001203 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001204 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001205 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001206 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1207 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001208 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001209 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001210 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001211 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001212 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001213
Evan Cheng063284c2008-02-21 00:34:19 +00001214 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001215 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001216 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001217 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1218 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001219 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001220 MachineOperand &O = ri.getOperand();
1221 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001222 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001223 unsigned index = getInstructionIndex(MI);
1224 if (index < start || index >= end)
1225 continue;
1226 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1227 }
1228 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1229
Evan Cheng313d4b82008-02-23 00:33:04 +00001230 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001231 // Now rewrite the defs and uses.
1232 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1233 RewriteInfo &rwi = RewriteMIs[i];
1234 ++i;
1235 unsigned index = rwi.Index;
1236 bool MIHasUse = rwi.HasUse;
1237 bool MIHasDef = rwi.HasDef;
1238 MachineInstr *MI = rwi.MI;
1239 // If MI def and/or use the same register multiple times, then there
1240 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001241 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001242 while (i != e && RewriteMIs[i].MI == MI) {
1243 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001244 bool isUse = RewriteMIs[i].HasUse;
1245 if (isUse) ++NumUses;
1246 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001247 MIHasDef |= RewriteMIs[i].HasDef;
1248 ++i;
1249 }
Evan Cheng81a03822007-11-17 00:40:40 +00001250 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001251
Evan Cheng0a891ed2008-05-23 23:00:04 +00001252 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001253 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001254 // register interval's spill weight to HUGE_VALF to prevent it from
1255 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001256 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001257 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001258 }
1259
Evan Cheng063284c2008-02-21 00:34:19 +00001260 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001261 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001262 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001263 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001264 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001265 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001266 // One common case:
1267 // x = use
1268 // ...
1269 // ...
1270 // def = ...
1271 // = use
1272 // It's better to start a new interval to avoid artifically
1273 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001274 if (MIHasDef && !MIHasUse) {
1275 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001276 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001277 }
1278 }
Evan Chengcada2452007-11-28 01:28:46 +00001279 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001280
1281 bool IsNew = ThisVReg == 0;
1282 if (IsNew) {
1283 // This ends the previous live interval. If all of its def / use
1284 // can be folded, give it a low spill weight.
1285 if (NewVReg && TrySplit && AllCanFold) {
1286 LiveInterval &nI = getOrCreateInterval(NewVReg);
1287 nI.weight /= 10.0F;
1288 }
1289 AllCanFold = true;
1290 }
1291 NewVReg = ThisVReg;
1292
Evan Cheng81a03822007-11-17 00:40:40 +00001293 bool HasDef = false;
1294 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001295 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001296 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1297 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001298 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001299 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001300 if (!HasDef && !HasUse)
1301 continue;
1302
Evan Cheng018f9b02007-12-05 03:22:34 +00001303 AllCanFold &= CanFold;
1304
Evan Cheng81a03822007-11-17 00:40:40 +00001305 // Update weight of spill interval.
1306 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001307 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001308 // The spill weight is now infinity as it cannot be spilled again.
1309 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001310 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001311 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001312
1313 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001314 if (HasDef) {
1315 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001316 bool HasKill = false;
1317 if (!HasUse)
1318 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1319 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001320 // If this is a two-address code, then this index starts a new VNInfo.
1321 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001322 if (VNI)
1323 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1324 }
Evan Chenge3110d02007-12-01 04:42:39 +00001325 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1326 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001327 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001328 if (SII == SpillIdxes.end()) {
1329 std::vector<SRInfo> S;
1330 S.push_back(SRInfo(index, NewVReg, true));
1331 SpillIdxes.insert(std::make_pair(MBBId, S));
1332 } else if (SII->second.back().vreg != NewVReg) {
1333 SII->second.push_back(SRInfo(index, NewVReg, true));
1334 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001335 // If there is an earlier def and this is a two-address
1336 // instruction, then it's not possible to fold the store (which
1337 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001338 SRInfo &Info = SII->second.back();
1339 Info.index = index;
1340 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001341 }
1342 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001343 } else if (SII != SpillIdxes.end() &&
1344 SII->second.back().vreg == NewVReg &&
1345 (int)index > SII->second.back().index) {
1346 // There is an earlier def that's not killed (must be two-address).
1347 // The spill is no longer needed.
1348 SII->second.pop_back();
1349 if (SII->second.empty()) {
1350 SpillIdxes.erase(MBBId);
1351 SpillMBBs.reset(MBBId);
1352 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001353 }
1354 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001355 }
1356
1357 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001358 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001359 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001360 if (SII != SpillIdxes.end() &&
1361 SII->second.back().vreg == NewVReg &&
1362 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001363 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001364 SII->second.back().canFold = false;
1365 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001366 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001367 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001368 // If we are splitting live intervals, only fold if it's the first
1369 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001370 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001371 else if (IsNew) {
1372 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001373 if (RII == RestoreIdxes.end()) {
1374 std::vector<SRInfo> Infos;
1375 Infos.push_back(SRInfo(index, NewVReg, true));
1376 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1377 } else {
1378 RII->second.push_back(SRInfo(index, NewVReg, true));
1379 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001380 RestoreMBBs.set(MBBId);
1381 }
1382 }
1383
1384 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001385 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001386 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001387 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001388
1389 if (NewVReg && TrySplit && AllCanFold) {
1390 // If all of its def / use can be folded, give it a low spill weight.
1391 LiveInterval &nI = getOrCreateInterval(NewVReg);
1392 nI.weight /= 10.0F;
1393 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001394}
1395
Evan Cheng1953d0c2007-11-29 10:12:14 +00001396bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1397 BitVector &RestoreMBBs,
1398 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1399 if (!RestoreMBBs[Id])
1400 return false;
1401 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1402 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1403 if (Restores[i].index == index &&
1404 Restores[i].vreg == vr &&
1405 Restores[i].canFold)
1406 return true;
1407 return false;
1408}
1409
1410void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1411 BitVector &RestoreMBBs,
1412 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1413 if (!RestoreMBBs[Id])
1414 return;
1415 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1416 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1417 if (Restores[i].index == index && Restores[i].vreg)
1418 Restores[i].index = -1;
1419}
Evan Cheng81a03822007-11-17 00:40:40 +00001420
Evan Cheng4cce6b42008-04-11 17:53:36 +00001421/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1422/// spilled and create empty intervals for their uses.
1423void
1424LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1425 const TargetRegisterClass* rc,
1426 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001427 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1428 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001429 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001430 MachineInstr *MI = &*ri;
1431 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001432 if (O.isDef()) {
1433 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1434 "Register def was not rewritten?");
1435 RemoveMachineInstrFromMaps(MI);
1436 vrm.RemoveMachineInstrFromMaps(MI);
1437 MI->eraseFromParent();
1438 } else {
1439 // This must be an use of an implicit_def so it's not part of the live
1440 // interval. Create a new empty live interval for it.
1441 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1442 unsigned NewVReg = mri_->createVirtualRegister(rc);
1443 vrm.grow();
1444 vrm.setIsImplicitlyDefined(NewVReg);
1445 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1446 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1447 MachineOperand &MO = MI->getOperand(i);
1448 if (MO.isReg() && MO.getReg() == li.reg)
1449 MO.setReg(NewVReg);
1450 }
1451 }
Evan Cheng419852c2008-04-03 16:39:43 +00001452 }
1453}
1454
Evan Cheng81a03822007-11-17 00:40:40 +00001455
Evan Chengf2fbca62007-11-12 06:35:08 +00001456std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001457addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001458 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001459 // Since this is called after the analysis is done we don't know if
1460 // LiveVariables is available
1461 lv_ = getAnalysisToUpdate<LiveVariables>();
1462
1463 assert(li.weight != HUGE_VALF &&
1464 "attempt to spill already spilled interval!");
1465
1466 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001467 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001468 DOUT << '\n';
1469
Evan Cheng81a03822007-11-17 00:40:40 +00001470 // Each bit specify whether it a spill is required in the MBB.
1471 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001472 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001473 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001474 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1475 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001476 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001477 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001478
1479 unsigned NumValNums = li.getNumValNums();
1480 SmallVector<MachineInstr*, 4> ReMatDefs;
1481 ReMatDefs.resize(NumValNums, NULL);
1482 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1483 ReMatOrigDefs.resize(NumValNums, NULL);
1484 SmallVector<int, 4> ReMatIds;
1485 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1486 BitVector ReMatDelete(NumValNums);
1487 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1488
Evan Cheng81a03822007-11-17 00:40:40 +00001489 // Spilling a split live interval. It cannot be split any further. Also,
1490 // it's also guaranteed to be a single val# / range interval.
1491 if (vrm.getPreSplitReg(li.reg)) {
1492 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001493 // Unset the split kill marker on the last use.
1494 unsigned KillIdx = vrm.getKillPoint(li.reg);
1495 if (KillIdx) {
1496 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1497 assert(KillMI && "Last use disappeared?");
1498 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1499 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001500 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001501 }
Evan Chengadf85902007-12-05 09:51:10 +00001502 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001503 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1504 Slot = vrm.getStackSlot(li.reg);
1505 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1506 MachineInstr *ReMatDefMI = DefIsReMat ?
1507 vrm.getReMaterializedMI(li.reg) : NULL;
1508 int LdSlot = 0;
1509 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1510 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001511 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001512 bool IsFirstRange = true;
1513 for (LiveInterval::Ranges::const_iterator
1514 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1515 // If this is a split live interval with multiple ranges, it means there
1516 // are two-address instructions that re-defined the value. Only the
1517 // first def can be rematerialized!
1518 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001519 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001520 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1521 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001522 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001523 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001524 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001525 } else {
1526 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1527 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001528 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001529 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001530 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001531 }
1532 IsFirstRange = false;
1533 }
Evan Cheng419852c2008-04-03 16:39:43 +00001534
Evan Cheng4cce6b42008-04-11 17:53:36 +00001535 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001536 return NewLIs;
1537 }
1538
1539 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001540 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1541 TrySplit = false;
1542 if (TrySplit)
1543 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001544 bool NeedStackSlot = false;
1545 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1546 i != e; ++i) {
1547 const VNInfo *VNI = *i;
1548 unsigned VN = VNI->id;
1549 unsigned DefIdx = VNI->def;
1550 if (DefIdx == ~1U)
1551 continue; // Dead val#.
1552 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001553 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1554 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001555 bool dummy;
1556 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001557 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001558 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001559 // Original def may be modified so we have to make a copy here. vrm must
1560 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001561 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001562
1563 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001564 if (VNI->hasPHIKill) {
1565 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001566 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001567 CanDelete = false;
1568 // Need a stack slot if there is any live range where uses cannot be
1569 // rematerialized.
1570 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001571 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001572 if (CanDelete)
1573 ReMatDelete.set(VN);
1574 } else {
1575 // Need a stack slot if there is any live range where uses cannot be
1576 // rematerialized.
1577 NeedStackSlot = true;
1578 }
1579 }
1580
1581 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001582 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001583 Slot = vrm.assignVirt2StackSlot(li.reg);
1584
1585 // Create new intervals and rewrite defs and uses.
1586 for (LiveInterval::Ranges::const_iterator
1587 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001588 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1589 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1590 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001591 bool CanDelete = ReMatDelete[I->valno->id];
1592 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001593 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001594 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001595 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001596 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001597 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001598 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001599 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001600 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001601 }
1602
Evan Cheng0cbb1162007-11-29 01:06:25 +00001603 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001604 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001605 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001606 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001607 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001608
Evan Chengb50bb8c2007-12-05 08:16:32 +00001609 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001610 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001611 if (NeedStackSlot) {
1612 int Id = SpillMBBs.find_first();
1613 while (Id != -1) {
1614 std::vector<SRInfo> &spills = SpillIdxes[Id];
1615 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1616 int index = spills[i].index;
1617 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001618 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001619 bool isReMat = vrm.isReMaterialized(VReg);
1620 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001621 bool CanFold = false;
1622 bool FoundUse = false;
1623 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001624 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001625 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001626 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1627 MachineOperand &MO = MI->getOperand(j);
1628 if (!MO.isRegister() || MO.getReg() != VReg)
1629 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001630
1631 Ops.push_back(j);
1632 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001633 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001634 if (isReMat ||
1635 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1636 RestoreMBBs, RestoreIdxes))) {
1637 // MI has two-address uses of the same register. If the use
1638 // isn't the first and only use in the BB, then we can't fold
1639 // it. FIXME: Move this to rewriteInstructionsForSpills.
1640 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001641 break;
1642 }
Evan Chengaee4af62007-12-02 08:30:39 +00001643 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001644 }
1645 }
1646 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001647 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001648 if (CanFold && !Ops.empty()) {
1649 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001650 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001651 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001652 // Also folded uses, do not issue a load.
1653 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001654 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1655 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001656 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001657 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001658 }
1659
Evan Cheng7e073ba2008-04-09 20:57:25 +00001660 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001661 if (!Folded) {
1662 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1663 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001664 if (!MI->registerDefIsDead(nI.reg))
1665 // No need to spill a dead def.
1666 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001667 if (isKill)
1668 AddedKill.insert(&nI);
1669 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001670 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001671 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001672 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001673 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001674
Evan Cheng1953d0c2007-11-29 10:12:14 +00001675 int Id = RestoreMBBs.find_first();
1676 while (Id != -1) {
1677 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1678 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1679 int index = restores[i].index;
1680 if (index == -1)
1681 continue;
1682 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001683 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001684 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001685 bool CanFold = false;
1686 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001687 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001688 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001689 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1690 MachineOperand &MO = MI->getOperand(j);
1691 if (!MO.isRegister() || MO.getReg() != VReg)
1692 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001693
Evan Cheng0cbb1162007-11-29 01:06:25 +00001694 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001695 // If this restore were to be folded, it would have been folded
1696 // already.
1697 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001698 break;
1699 }
Evan Chengaee4af62007-12-02 08:30:39 +00001700 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001701 }
1702 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001703
1704 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001705 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001706 if (CanFold && !Ops.empty()) {
1707 if (!vrm.isReMaterialized(VReg))
1708 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1709 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001710 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1711 int LdSlot = 0;
1712 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1713 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001714 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001715 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1716 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001717 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1718 if (ImpUse) {
1719 // Re-matting an instruction with virtual register use. Add the
1720 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001721 // interval's spill weight to HUGE_VALF to prevent it from being
1722 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001723 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001724 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001725 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1726 }
Evan Chengaee4af62007-12-02 08:30:39 +00001727 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001728 }
1729 // If folding is not possible / failed, then tell the spiller to issue a
1730 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001731 if (Folded)
1732 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001733 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001734 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001735 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001736 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001737 }
1738
Evan Chengb50bb8c2007-12-05 08:16:32 +00001739 // Finalize intervals: add kills, finalize spill weights, and filter out
1740 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001741 std::vector<LiveInterval*> RetNewLIs;
1742 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1743 LiveInterval *LI = NewLIs[i];
1744 if (!LI->empty()) {
1745 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001746 if (!AddedKill.count(LI)) {
1747 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001748 unsigned LastUseIdx = getBaseIndex(LR->end);
1749 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001750 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001751 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001752 if (LastUse->getOperand(UseIdx).isImplicit() ||
1753 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001754 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001755 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001756 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001757 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001758 RetNewLIs.push_back(LI);
1759 }
1760 }
Evan Cheng81a03822007-11-17 00:40:40 +00001761
Evan Cheng4cce6b42008-04-11 17:53:36 +00001762 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001763 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001764}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001765
1766/// hasAllocatableSuperReg - Return true if the specified physical register has
1767/// any super register that's allocatable.
1768bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1769 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1770 if (allocatableRegs_[*AS] && hasInterval(*AS))
1771 return true;
1772 return false;
1773}
1774
1775/// getRepresentativeReg - Find the largest super register of the specified
1776/// physical register.
1777unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1778 // Find the largest super-register that is allocatable.
1779 unsigned BestReg = Reg;
1780 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1781 unsigned SuperReg = *AS;
1782 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1783 BestReg = SuperReg;
1784 break;
1785 }
1786 }
1787 return BestReg;
1788}
1789
1790/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1791/// specified interval that conflicts with the specified physical register.
1792unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1793 unsigned PhysReg) const {
1794 unsigned NumConflicts = 0;
1795 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1796 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1797 E = mri_->reg_end(); I != E; ++I) {
1798 MachineOperand &O = I.getOperand();
1799 MachineInstr *MI = O.getParent();
1800 unsigned Index = getInstructionIndex(MI);
1801 if (pli.liveAt(Index))
1802 ++NumConflicts;
1803 }
1804 return NumConflicts;
1805}
1806
1807/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1808/// around all defs and uses of the specified interval.
1809void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1810 unsigned PhysReg, VirtRegMap &vrm) {
1811 unsigned SpillReg = getRepresentativeReg(PhysReg);
1812
1813 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1814 // If there are registers which alias PhysReg, but which are not a
1815 // sub-register of the chosen representative super register. Assert
1816 // since we can't handle it yet.
1817 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1818 tri_->isSuperRegister(*AS, SpillReg));
1819
1820 LiveInterval &pli = getInterval(SpillReg);
1821 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1822 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1823 E = mri_->reg_end(); I != E; ++I) {
1824 MachineOperand &O = I.getOperand();
1825 MachineInstr *MI = O.getParent();
1826 if (SeenMIs.count(MI))
1827 continue;
1828 SeenMIs.insert(MI);
1829 unsigned Index = getInstructionIndex(MI);
1830 if (pli.liveAt(Index)) {
1831 vrm.addEmergencySpill(SpillReg, MI);
1832 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1833 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1834 if (!hasInterval(*AS))
1835 continue;
1836 LiveInterval &spli = getInterval(*AS);
1837 if (spli.liveAt(Index))
1838 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1839 }
1840 }
1841 }
1842}