blob: 55094e3e5d6347d064e16d31679e80e4dc81c5e5 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/MRegisterInfo.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000034#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000036using namespace llvm;
37
Evan Chengbc165e42007-08-16 07:24:22 +000038namespace {
39 // Hidden options for help debugging.
40 cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
42}
43
Chris Lattnercd3245a2006-12-19 22:41:21 +000044STATISTIC(numIntervals, "Number of original intervals");
45STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046STATISTIC(numFolded , "Number of loads/stores folded into instructions");
47
Devang Patel19974732007-05-03 01:11:54 +000048char LiveIntervals::ID = 0;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000049namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000050 RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000051}
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000052
Chris Lattnerf7da2c72006-08-24 22:43:55 +000053void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000054 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000055 AU.addRequired<LiveVariables>();
56 AU.addPreservedID(PHIEliminationID);
57 AU.addRequiredID(PHIEliminationID);
58 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000060}
61
Chris Lattnerf7da2c72006-08-24 22:43:55 +000062void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000063 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 mi2iMap_.clear();
65 i2miMap_.clear();
66 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000067 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
68 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000069 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
70 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000071}
72
Evan Cheng4ca980e2007-10-17 02:10:22 +000073namespace llvm {
74 inline bool operator<(unsigned V, const IdxMBBPair &IM) {
75 return V < IM.first;
76 }
77
78 inline bool operator<(const IdxMBBPair &IM, unsigned V) {
79 return IM.first < V;
80 }
81
82 struct Idx2MBBCompare {
83 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
84 return LHS.first < RHS.first;
85 }
86 };
87}
88
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000089/// runOnMachineFunction - Register allocate the whole function
90///
91bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000092 mf_ = &fn;
93 tm_ = &fn.getTarget();
94 mri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000095 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 lv_ = &getAnalysis<LiveVariables>();
Evan Cheng20b0abc2007-04-17 20:32:26 +000097 allocatableRegs_ = mri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000098
Chris Lattner428b92e2006-09-15 03:57:23 +000099 // Number MachineInstrs and MachineBasicBlocks.
100 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +0000101 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +0000102
103 unsigned MIIndex = 0;
104 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
105 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +0000106 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000107
Chris Lattner428b92e2006-09-15 03:57:23 +0000108 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
109 I != E; ++I) {
110 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000111 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000112 i2miMap_.push_back(I);
113 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000114 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000115
116 // Set the MBB2IdxMap entry for this MBB.
117 MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000118 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000119 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000120 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000121
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000123
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000124 numIntervals += getNumIntervals();
125
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000126 DOUT << "********** INTERVALS **********\n";
127 for (iterator I = begin(), E = end(); I != E; ++I) {
128 I->second.print(DOUT, mri_);
129 DOUT << "\n";
130 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000133 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000134 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000135}
136
Chris Lattner70ca3582004-09-30 15:59:17 +0000137/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000138void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000139 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000140 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000141 I->second.print(DOUT, mri_);
142 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000143 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000144
145 O << "********** MACHINEINSTRS **********\n";
146 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
147 mbbi != mbbe; ++mbbi) {
148 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
149 for (MachineBasicBlock::iterator mii = mbbi->begin(),
150 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000151 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000152 }
153 }
154}
155
Evan Chengc92da382007-11-03 07:20:12 +0000156/// conflictsWithPhysRegDef - Returns true if the specified register
157/// is defined during the duration of the specified interval.
158bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
159 VirtRegMap &vrm, unsigned reg) {
160 for (LiveInterval::Ranges::const_iterator
161 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
162 for (unsigned index = getBaseIndex(I->start),
163 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
164 index += InstrSlots::NUM) {
165 // skip deleted instructions
166 while (index != end && !getInstructionFromIndex(index))
167 index += InstrSlots::NUM;
168 if (index == end) break;
169
170 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000171 unsigned SrcReg, DstReg;
172 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
173 if (SrcReg == li.reg || DstReg == li.reg)
174 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000175 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
176 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000177 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000178 continue;
179 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000180 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000181 continue;
Evan Cheng5d446262007-11-15 08:13:29 +0000182 if (MRegisterInfo::isVirtualRegister(PhysReg)) {
183 if (!vrm.hasPhys(PhysReg))
184 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000185 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000186 }
Evan Cheng5f5f3b62007-11-05 00:59:10 +0000187 if (PhysReg && mri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000188 return true;
189 }
190 }
191 }
192
193 return false;
194}
195
Evan Cheng549f27d32007-08-13 23:45:17 +0000196void LiveIntervals::printRegName(unsigned reg) const {
197 if (MRegisterInfo::isPhysicalRegister(reg))
198 cerr << mri_->getName(reg);
199 else
200 cerr << "%reg" << reg;
201}
202
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000203void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000204 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000205 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000206 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000207 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000209
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000210 // Virtual registers may be defined multiple times (due to phi
211 // elimination and 2-addr elimination). Much of what we do only has to be
212 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000213 // time we see a vreg.
214 if (interval.empty()) {
215 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000216 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000217 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000218 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000219 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000220 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng48ff2822007-10-12 17:16:50 +0000221 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
Evan Cheng32dfbea2007-10-12 08:50:34 +0000222 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
223 VNInfoAllocator);
224 else
225 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000226
227 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000228
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000229 // Loop over all of the blocks that the vreg is defined in. There are
230 // two cases we have to handle here. The most common case is a vreg
231 // whose lifetime is contained within a basic block. In this case there
232 // will be a single kill, in MBB, which comes after the definition.
233 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
234 // FIXME: what about dead vars?
235 unsigned killIdx;
236 if (vi.Kills[0] != mi)
237 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
238 else
239 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000240
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000241 // If the kill happens after the definition, we have an intra-block
242 // live range.
243 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000244 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000245 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000246 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000247 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000248 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000249 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 return;
251 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000252 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000253
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000254 // The other case we handle is when a virtual register lives to the end
255 // of the defining block, potentially live across some blocks, then is
256 // live into some number of blocks, but gets killed. Start by adding a
257 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000258 LiveRange NewLR(defIndex,
259 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000260 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000261 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262 interval.addRange(NewLR);
263
264 // Iterate over all of the blocks that the variable is completely
265 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
266 // live interval.
267 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
268 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000269 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
270 if (!MBB->empty()) {
271 LiveRange LR(getMBBStartIdx(i),
272 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000273 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000274 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000275 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 }
277 }
278 }
279
280 // Finally, this virtual register is live from the start of any killing
281 // block to the 'use' slot of the killing instruction.
282 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
283 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000284 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000285 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000286 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000288 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000289 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 }
291
292 } else {
293 // If this is the second time we see a virtual register definition, it
294 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000295 // the result of two address elimination, then the vreg is one of the
296 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000297 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298 // If this is a two-address definition, then we have already processed
299 // the live range. The only problem is that we didn't realize there
300 // are actually two values in the live interval. Because of this we
301 // need to take the LiveRegion that defines this register and split it
302 // into two values.
303 unsigned DefIndex = getDefIndex(getInstructionIndex(vi.DefInst));
Chris Lattner6b128bd2006-09-03 08:07:11 +0000304 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000305
Evan Cheng4f8ff162007-08-11 00:59:19 +0000306 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000307 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000308 unsigned OldEnd = OldLR->end;
309
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000311 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000312 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000313
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000314 // Two-address vregs should always only be redefined once. This means
315 // that at this point, there should be exactly one value number in it.
316 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
317
Chris Lattner91725b72006-08-31 05:54:43 +0000318 // The new value number (#1) is defined by the instruction we claimed
319 // defined value #0.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000320 VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
321 interval.copyValNumInfo(ValNo, OldValNo);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000322
Chris Lattner91725b72006-08-31 05:54:43 +0000323 // Value#0 is now defined by the 2-addr instruction.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000324 OldValNo->def = RedefIndex;
325 OldValNo->reg = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000326
327 // Add the new live interval which replaces the range for the input copy.
328 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000329 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000331 interval.addKill(ValNo, RedefIndex);
332 interval.removeKills(ValNo, RedefIndex, OldEnd);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000333
334 // If this redefinition is dead, we need to add a dummy unit live
335 // range covering the def slot.
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000336 if (lv_->RegisterDefIsDead(mi, interval.reg))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000337 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000338
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000339 DOUT << " RESULT: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000340 interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341
342 } else {
343 // Otherwise, this must be because of phi elimination. If this is the
344 // first redefinition of the vreg that we have seen, go back and change
345 // the live range in the PHI block to be a different value number.
346 if (interval.containsOneValue()) {
347 assert(vi.Kills.size() == 1 &&
348 "PHI elimination vreg should have one kill, the PHI itself!");
349
350 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000351 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000352 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000353 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000355 DOUT << " Removing [" << Start << "," << End << "] from: ";
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000356 interval.print(DOUT, mri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000357 interval.removeRange(Start, End);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000358 interval.addKill(VNI, Start+1); // odd # means phi node
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000359 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000360
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000361 // Replace the interval with one of a NEW value number. Note that this
362 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000363 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000364 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000365 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000366 interval.addKill(LR.valno, End);
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000367 DOUT << " RESULT: "; interval.print(DOUT, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000368 }
369
370 // In the case of PHI elimination, each variable definition is only
371 // live until the end of the block. We've already taken care of the
372 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000373 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000374
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000375 VNInfo *ValNo;
Chris Lattner91725b72006-08-31 05:54:43 +0000376 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000377 if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
Evan Chengf3bb2e62007-09-05 21:46:51 +0000378 ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000379 else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
380 ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
381 VNInfoAllocator);
382 else
383 ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000384
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000385 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000386 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000387 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000388 interval.addKill(ValNo, killIndex-1); // odd # means phi node
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000389 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000390 }
391 }
392
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000393 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000394}
395
Chris Lattnerf35fef72004-07-23 21:24:19 +0000396void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000397 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000398 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000399 LiveInterval &interval,
400 unsigned SrcReg) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 // A physical register cannot be live across basic block, so its
402 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000403 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000404
Chris Lattner6b128bd2006-09-03 08:07:11 +0000405 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000406 unsigned start = getDefIndex(baseIndex);
407 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000408
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000409 // If it is not used after definition, it is considered dead at
410 // the instruction defining it. Hence its interval is:
411 // [defSlot(def), defSlot(def)+1)
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000412 if (lv_->RegisterDefIsDead(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000413 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000414 end = getDefIndex(start) + 1;
415 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000416 }
417
418 // If it is not dead on definition, it must be killed by a
419 // subsequent instruction. Hence its interval is:
420 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000421 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 baseIndex += InstrSlots::NUM;
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000423 if (lv_->KillsRegister(mi, interval.reg)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000424 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000425 end = getUseIndex(baseIndex) + 1;
426 goto exit;
Evan Cheng9a1956a2006-11-15 20:54:11 +0000427 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
428 // Another instruction redefines the register before it is ever read.
429 // Then the register is essentially dead at the instruction that defines
430 // it. Hence its interval is:
431 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000432 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000433 end = getDefIndex(start) + 1;
434 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000435 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000436 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000437
438 // The only case we should have a dead physreg here without a killing or
439 // instruction where we know it's dead is if it is live-in to the function
440 // and never used.
Chris Lattner91725b72006-08-31 05:54:43 +0000441 assert(!SrcReg && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000442 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000443
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000444exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000445 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000446
Evan Cheng24a3cc42007-04-25 07:30:23 +0000447 // Already exists? Extend old live interval.
448 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000449 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengf3bb2e62007-09-05 21:46:51 +0000450 ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000451 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000452 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000453 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000454 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000455}
456
Chris Lattnerf35fef72004-07-23 21:24:19 +0000457void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
458 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000459 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000460 unsigned reg) {
461 if (MRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000462 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000463 else if (allocatableRegs_[reg]) {
Chris Lattner91725b72006-08-31 05:54:43 +0000464 unsigned SrcReg, DstReg;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000465 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
466 SrcReg = MI->getOperand(1).getReg();
467 else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
Chris Lattner91725b72006-08-31 05:54:43 +0000468 SrcReg = 0;
Chris Lattner6b128bd2006-09-03 08:07:11 +0000469 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000470 // Def of a register also defines its sub-registers.
471 for (const unsigned* AS = mri_->getSubRegisters(reg); *AS; ++AS)
472 // Avoid processing some defs more than once.
473 if (!MI->findRegisterDefOperand(*AS))
474 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000475 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000476}
477
Evan Chengb371f452007-02-19 21:49:54 +0000478void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000479 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000480 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000481 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
482
483 // Look for kills, if it reaches a def before it's killed, then it shouldn't
484 // be considered a livein.
485 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000486 unsigned baseIndex = MIIdx;
487 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000488 unsigned end = start;
489 while (mi != MBB->end()) {
490 if (lv_->KillsRegister(mi, interval.reg)) {
491 DOUT << " killed";
492 end = getUseIndex(baseIndex) + 1;
493 goto exit;
494 } else if (lv_->ModifiesRegister(mi, interval.reg)) {
495 // Another instruction redefines the register before it is ever read.
496 // Then the register is essentially dead at the instruction that defines
497 // it. Hence its interval is:
498 // [defSlot(def), defSlot(def)+1)
499 DOUT << " dead";
500 end = getDefIndex(start) + 1;
501 goto exit;
502 }
503
504 baseIndex += InstrSlots::NUM;
505 ++mi;
506 }
507
508exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000509 // Live-in register might not be used at all.
510 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000511 if (isAlias) {
512 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000513 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000514 } else {
515 DOUT << " live through";
516 end = baseIndex;
517 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000518 }
519
Evan Chengf3bb2e62007-09-05 21:46:51 +0000520 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000521 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000522 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000523 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000524}
525
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000526/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000527/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000528/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000529/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000530void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000531 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
532 << "********** Function: "
533 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000534 // Track the index of the current machine instr.
535 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000536 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
537 MBBI != E; ++MBBI) {
538 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000539 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000540
Chris Lattner428b92e2006-09-15 03:57:23 +0000541 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000542
Dan Gohmancb406c22007-10-03 19:26:29 +0000543 // Create intervals for live-ins to this BB first.
544 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
545 LE = MBB->livein_end(); LI != LE; ++LI) {
546 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
547 // Multiple live-ins can alias the same register.
548 for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
549 if (!hasInterval(*AS))
550 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
551 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000552 }
553
Chris Lattner428b92e2006-09-15 03:57:23 +0000554 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000555 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000556
Evan Cheng438f7bc2006-11-10 08:43:01 +0000557 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000558 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
559 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000560 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000561 if (MO.isRegister() && MO.getReg() && MO.isDef())
562 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000563 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000564
565 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000566 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000567 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000568}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000569
Evan Cheng4ca980e2007-10-17 02:10:22 +0000570bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000571 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000572 std::vector<IdxMBBPair>::const_iterator I =
573 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
574
575 bool ResVal = false;
576 while (I != Idx2MBBMap.end()) {
577 if (LR.end <= I->first)
578 break;
579 MBBs.push_back(I->second);
580 ResVal = true;
581 ++I;
582 }
583 return ResVal;
584}
585
586
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000587LiveInterval LiveIntervals::createInterval(unsigned reg) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000588 float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000589 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000590 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000591}
Evan Chengf2fbca62007-11-12 06:35:08 +0000592
593
594//===----------------------------------------------------------------------===//
595// Register allocator hooks.
596//
597
598/// isReMaterializable - Returns true if the definition MI of the specified
599/// val# of the specified interval is re-materializable.
600bool LiveIntervals::isReMaterializable(const LiveInterval &li,
601 const VNInfo *ValNo, MachineInstr *MI) {
602 if (DisableReMat)
603 return false;
604
605 if (tii_->isTriviallyReMaterializable(MI))
606 return true;
607
608 int FrameIdx = 0;
609 if (!tii_->isLoadFromStackSlot(MI, FrameIdx) ||
610 !mf_->getFrameInfo()->isFixedObjectIndex(FrameIdx))
611 return false;
612
613 // This is a load from fixed stack slot. It can be rematerialized unless it's
614 // re-defined by a two-address instruction.
615 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
616 i != e; ++i) {
617 const VNInfo *VNI = *i;
618 if (VNI == ValNo)
619 continue;
620 unsigned DefIdx = VNI->def;
621 if (DefIdx == ~1U)
622 continue; // Dead val#.
623 MachineInstr *DefMI = (DefIdx == ~0u)
624 ? NULL : getInstructionFromIndex(DefIdx);
625 if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
626 return false;
627 }
628 return true;
629}
630
631/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
632/// slot / to reg or any rematerialized load into ith operand of specified
633/// MI. If it is successul, MI is updated with the newly created MI and
634/// returns true.
635bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
636 MachineInstr *DefMI,
637 unsigned index, unsigned i,
638 bool isSS, int slot, unsigned reg) {
639 MachineInstr *fmi = isSS
640 ? mri_->foldMemoryOperand(MI, i, slot)
641 : mri_->foldMemoryOperand(MI, i, DefMI);
642 if (fmi) {
643 // Attempt to fold the memory reference into the instruction. If
644 // we can do this, we don't need to insert spill code.
645 if (lv_)
646 lv_->instructionChanged(MI, fmi);
647 MachineBasicBlock &MBB = *MI->getParent();
648 vrm.virtFolded(reg, MI, i, fmi);
649 mi2iMap_.erase(MI);
650 i2miMap_[index/InstrSlots::NUM] = fmi;
651 mi2iMap_[fmi] = index;
652 MI = MBB.insert(MBB.erase(MI), fmi);
653 ++numFolded;
654 return true;
655 }
656 return false;
657}
658
659/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
660/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
661void LiveIntervals::
662rewriteInstructionForSpills(const LiveInterval &li,
663 unsigned id, unsigned index, unsigned end,
664 MachineInstr *MI, MachineInstr *OrigDefMI, MachineInstr *DefMI,
665 unsigned Slot, int LdSlot,
666 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
667 VirtRegMap &vrm, SSARegMap *RegMap,
668 const TargetRegisterClass* rc,
669 SmallVector<int, 4> &ReMatIds,
670 std::vector<LiveInterval*> &NewLIs) {
671 RestartInstruction:
672 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
673 MachineOperand& mop = MI->getOperand(i);
674 if (!mop.isRegister())
675 continue;
676 unsigned Reg = mop.getReg();
677 unsigned RegI = Reg;
678 if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
679 continue;
Evan Chengc498b022007-11-14 07:59:08 +0000680 unsigned SubIdx = mop.getSubReg();
681 bool isSubReg = SubIdx != 0;
Evan Chengf2fbca62007-11-12 06:35:08 +0000682 if (Reg != li.reg)
683 continue;
684
685 bool TryFold = !DefIsReMat;
686 bool FoldSS = true;
687 int FoldSlot = Slot;
688 if (DefIsReMat) {
689 // If this is the rematerializable definition MI itself and
690 // all of its uses are rematerialized, simply delete it.
691 if (MI == OrigDefMI && CanDelete) {
692 RemoveMachineInstrFromMaps(MI);
693 MI->eraseFromParent();
694 break;
695 }
696
697 // If def for this use can't be rematerialized, then try folding.
698 TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
699 if (isLoad) {
700 // Try fold loads (from stack slot, constant pool, etc.) into uses.
701 FoldSS = isLoadSS;
702 FoldSlot = LdSlot;
703 }
704 }
705
706 // FIXME: fold subreg use
707 if (!isSubReg && TryFold &&
708 tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
709 // Folding the load/store can completely change the instruction in
710 // unpredictable ways, rescan it from the beginning.
711 goto RestartInstruction;
712
713 // Create a new virtual register for the spill interval.
714 unsigned NewVReg = RegMap->createVirtualRegister(rc);
715 vrm.grow();
Evan Chengf2fbca62007-11-12 06:35:08 +0000716
717 // Scan all of the operands of this instruction rewriting operands
718 // to use NewVReg instead of li.reg as appropriate. We do this for
719 // two reasons:
720 //
721 // 1. If the instr reads the same spilled vreg multiple times, we
722 // want to reuse the NewVReg.
723 // 2. If the instr is a two-addr instruction, we are required to
724 // keep the src/dst regs pinned.
725 //
726 // Keep track of whether we replace a use and/or def so that we can
727 // create the spill interval with the appropriate range.
728 mop.setReg(NewVReg);
729
730 bool HasUse = mop.isUse();
731 bool HasDef = mop.isDef();
732 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
733 if (!MI->getOperand(j).isRegister())
734 continue;
735 unsigned RegJ = MI->getOperand(j).getReg();
736 if (RegJ == 0 || MRegisterInfo::isPhysicalRegister(RegJ))
737 continue;
738 if (RegJ == RegI) {
739 MI->getOperand(j).setReg(NewVReg);
740 HasUse |= MI->getOperand(j).isUse();
741 HasDef |= MI->getOperand(j).isDef();
742 }
743 }
744
745 if (DefIsReMat) {
746 vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
747 if (ReMatIds[id] == VirtRegMap::MAX_STACK_SLOT) {
748 // Each valnum may have its own remat id.
749 ReMatIds[id] = vrm.assignVirtReMatId(NewVReg);
750 } else {
751 vrm.assignVirtReMatId(NewVReg, ReMatIds[id]);
752 }
753 if (!CanDelete || (HasUse && HasDef)) {
754 // If this is a two-addr instruction then its use operands are
755 // rematerializable but its def is not. It should be assigned a
756 // stack slot.
757 vrm.assignVirt2StackSlot(NewVReg, Slot);
758 }
759 } else {
760 vrm.assignVirt2StackSlot(NewVReg, Slot);
761 }
762
763 // create a new register interval for this spill / remat.
764 LiveInterval &nI = getOrCreateInterval(NewVReg);
765 assert(nI.empty());
766 NewLIs.push_back(&nI);
767
768 // the spill weight is now infinity as it
769 // cannot be spilled again
770 nI.weight = HUGE_VALF;
771
772 if (HasUse) {
773 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
774 nI.getNextValue(~0U, 0, VNInfoAllocator));
775 DOUT << " +" << LR;
776 nI.addRange(LR);
777 }
778 if (HasDef) {
779 LiveRange LR(getDefIndex(index), getStoreIndex(index),
780 nI.getNextValue(~0U, 0, VNInfoAllocator));
781 DOUT << " +" << LR;
782 nI.addRange(LR);
783 }
784
785 // update live variables if it is available
786 if (lv_)
787 lv_->addVirtualRegisterKilled(NewVReg, MI);
788
789 DOUT << "\t\t\t\tAdded new interval: ";
790 nI.print(DOUT, mri_);
791 DOUT << '\n';
792 }
793}
794
795void LiveIntervals::
796rewriteInstructionsForSpills(const LiveInterval &li,
797 LiveInterval::Ranges::const_iterator &I,
798 MachineInstr *OrigDefMI, MachineInstr *DefMI,
799 unsigned Slot, int LdSlot,
800 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
801 VirtRegMap &vrm, SSARegMap *RegMap,
802 const TargetRegisterClass* rc,
803 SmallVector<int, 4> &ReMatIds,
804 std::vector<LiveInterval*> &NewLIs) {
805 unsigned index = getBaseIndex(I->start);
806 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
807 for (; index != end; index += InstrSlots::NUM) {
808 // skip deleted instructions
809 while (index != end && !getInstructionFromIndex(index))
810 index += InstrSlots::NUM;
811 if (index == end) break;
812
813 MachineInstr *MI = getInstructionFromIndex(index);
814 rewriteInstructionForSpills(li, I->valno->id, index, end, MI,
815 OrigDefMI, DefMI, Slot, LdSlot, isLoad,
816 isLoadSS, DefIsReMat, CanDelete, vrm,
817 RegMap, rc, ReMatIds, NewLIs);
818 }
819}
820
821std::vector<LiveInterval*> LiveIntervals::
822addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm) {
823 // Since this is called after the analysis is done we don't know if
824 // LiveVariables is available
825 lv_ = getAnalysisToUpdate<LiveVariables>();
826
827 assert(li.weight != HUGE_VALF &&
828 "attempt to spill already spilled interval!");
829
830 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
831 li.print(DOUT, mri_);
832 DOUT << '\n';
833
834 std::vector<LiveInterval*> NewLIs;
835 SSARegMap *RegMap = mf_->getSSARegMap();
836 const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
837
838 unsigned NumValNums = li.getNumValNums();
839 SmallVector<MachineInstr*, 4> ReMatDefs;
840 ReMatDefs.resize(NumValNums, NULL);
841 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
842 ReMatOrigDefs.resize(NumValNums, NULL);
843 SmallVector<int, 4> ReMatIds;
844 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
845 BitVector ReMatDelete(NumValNums);
846 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
847
848 bool NeedStackSlot = false;
849 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
850 i != e; ++i) {
851 const VNInfo *VNI = *i;
852 unsigned VN = VNI->id;
853 unsigned DefIdx = VNI->def;
854 if (DefIdx == ~1U)
855 continue; // Dead val#.
856 // Is the def for the val# rematerializable?
857 MachineInstr *DefMI = (DefIdx == ~0u) ? 0 : getInstructionFromIndex(DefIdx);
858 if (DefMI && isReMaterializable(li, VNI, DefMI)) {
859 // Remember how to remat the def of this val#.
860 ReMatOrigDefs[VN] = DefMI;
861 // Original def may be modified so we have to make a copy here. vrm must
862 // delete these!
863 ReMatDefs[VN] = DefMI = DefMI->clone();
864 vrm.setVirtIsReMaterialized(li.reg, DefMI);
865
866 bool CanDelete = true;
867 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
868 unsigned KillIdx = VNI->kills[j];
869 MachineInstr *KillMI = (KillIdx & 1)
870 ? NULL : getInstructionFromIndex(KillIdx);
871 // Kill is a phi node, not all of its uses can be rematerialized.
872 // It must not be deleted.
873 if (!KillMI) {
874 CanDelete = false;
875 // Need a stack slot if there is any live range where uses cannot be
876 // rematerialized.
877 NeedStackSlot = true;
878 break;
879 }
880 }
881
882 if (CanDelete)
883 ReMatDelete.set(VN);
884 } else {
885 // Need a stack slot if there is any live range where uses cannot be
886 // rematerialized.
887 NeedStackSlot = true;
888 }
889 }
890
891 // One stack slot per live interval.
892 if (NeedStackSlot)
893 Slot = vrm.assignVirt2StackSlot(li.reg);
894
895 // Create new intervals and rewrite defs and uses.
896 for (LiveInterval::Ranges::const_iterator
897 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
898 MachineInstr *DefMI = ReMatDefs[I->valno->id];
899 MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
900 bool DefIsReMat = DefMI != NULL;
901 bool CanDelete = ReMatDelete[I->valno->id];
902 int LdSlot = 0;
903 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
904 bool isLoad = isLoadSS ||
905 (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
906 rewriteInstructionsForSpills(li, I, OrigDefMI, DefMI, Slot, LdSlot,
907 isLoad, isLoadSS, DefIsReMat, CanDelete,
908 vrm, RegMap, rc, ReMatIds, NewLIs);
909 }
910
911 return NewLIs;
912}