blob: 2558b09ad60f124b9c1dc460f0cd3332893cfac1 [file] [log] [blame]
Chris Lattnera3b8b5c2004-07-23 17:56:30 +00001//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LiveInterval analysis pass which is used
11// by the Linear Scan Register allocator. This pass linearizes the
12// basic blocks of the function in DFS order and uses the
13// LiveVariables pass to conservatively compute live intervals for
14// each virtual and physical register.
15//
16//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "liveintervals"
Chris Lattner3c3fe462005-09-21 04:19:09 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Misha Brukman08a6c762004-09-03 18:25:53 +000020#include "VirtRegMap.h"
Chris Lattner015959e2004-05-01 21:24:39 +000021#include "llvm/Value.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/LiveVariables.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000027#include "llvm/CodeGen/Passes.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000036#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
Dan Gohman844731a2008-05-13 00:00:25 +000039// Hidden options for help debugging.
40static cl::opt<bool> DisableReMat("disable-rematerialization",
41 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000042
Dan Gohman844731a2008-05-13 00:00:25 +000043static cl::opt<bool> SplitAtBB("split-intervals-at-bb",
44 cl::init(true), cl::Hidden);
45static cl::opt<int> SplitLimit("split-limit",
46 cl::init(-1), cl::Hidden);
Evan Chengbc165e42007-08-16 07:24:22 +000047
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(numIntervals, "Number of original intervals");
49STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
Evan Cheng0cbb1162007-11-29 01:06:25 +000050STATISTIC(numFolds , "Number of loads/stores folded into instructions");
51STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000052
Devang Patel19974732007-05-03 01:11:54 +000053char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000054static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000055
Chris Lattnerf7da2c72006-08-24 22:43:55 +000056void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
David Greene25133302007-06-08 17:18:56 +000057 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000058 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000059 AU.addPreservedID(MachineLoopInfoID);
60 AU.addPreservedID(MachineDominatorsID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 AU.addPreservedID(PHIEliminationID);
62 AU.addRequiredID(PHIEliminationID);
63 AU.addRequiredID(TwoAddressInstructionPassID);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000064 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000065}
66
Chris Lattnerf7da2c72006-08-24 22:43:55 +000067void LiveIntervals::releaseMemory() {
Evan Cheng4ca980e2007-10-17 02:10:22 +000068 Idx2MBBMap.clear();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 mi2iMap_.clear();
70 i2miMap_.clear();
71 r2iMap_.clear();
Evan Chengdd199d22007-09-06 01:07:24 +000072 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
73 VNInfoAllocator.Reset();
Evan Cheng549f27d32007-08-13 23:45:17 +000074 for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
75 delete ClonedMIs[i];
Alkis Evlogimenos08cec002004-01-31 19:59:32 +000076}
77
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000078/// runOnMachineFunction - Register allocate the whole function
79///
80bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000081 mf_ = &fn;
Evan Chengd70dbb52008-02-22 09:24:50 +000082 mri_ = &mf_->getRegInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 tm_ = &fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +000084 tri_ = tm_->getRegisterInfo();
Chris Lattnerf768bba2005-03-09 23:05:19 +000085 tii_ = tm_->getInstrInfo();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 lv_ = &getAnalysis<LiveVariables>();
Dan Gohman6f0d0242008-02-10 18:45:23 +000087 allocatableRegs_ = tri_->getAllocatableSet(fn);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000088
Chris Lattner428b92e2006-09-15 03:57:23 +000089 // Number MachineInstrs and MachineBasicBlocks.
90 // Initialize MBB indexes to a sentinal.
Evan Cheng549f27d32007-08-13 23:45:17 +000091 MBB2IdxMap.resize(mf_->getNumBlockIDs(), std::make_pair(~0U,~0U));
Chris Lattner428b92e2006-09-15 03:57:23 +000092
93 unsigned MIIndex = 0;
94 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
95 MBB != E; ++MBB) {
Evan Cheng549f27d32007-08-13 23:45:17 +000096 unsigned StartIdx = MIIndex;
Evan Cheng0c9f92e2007-02-13 01:30:55 +000097
Chris Lattner428b92e2006-09-15 03:57:23 +000098 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
99 I != E; ++I) {
100 bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000101 assert(inserted && "multiple MachineInstr -> index mappings");
Chris Lattner428b92e2006-09-15 03:57:23 +0000102 i2miMap_.push_back(I);
103 MIIndex += InstrSlots::NUM;
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000104 }
Evan Cheng549f27d32007-08-13 23:45:17 +0000105
106 // Set the MBB2IdxMap entry for this MBB.
Evan Cheng76249962008-04-16 18:01:08 +0000107 MBB2IdxMap[MBB->getNumber()] = (StartIdx == MIIndex)
108 ? std::make_pair(StartIdx, StartIdx) // Empty MBB
109 : std::make_pair(StartIdx, MIIndex - 1);
Evan Cheng4ca980e2007-10-17 02:10:22 +0000110 Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
Chris Lattner428b92e2006-09-15 03:57:23 +0000111 }
Evan Cheng4ca980e2007-10-17 02:10:22 +0000112 std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
Alkis Evlogimenosd6e40a62004-01-14 10:44:29 +0000113
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000114 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000115
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116 numIntervals += getNumIntervals();
117
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000118 DOUT << "********** INTERVALS **********\n";
119 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000121 DOUT << "\n";
122 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000123
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000124 numIntervalsAfter += getNumIntervals();
Chris Lattner70ca3582004-09-30 15:59:17 +0000125 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000126 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000127}
128
Chris Lattner70ca3582004-09-30 15:59:17 +0000129/// print - Implement the dump method.
Reid Spencerce9653c2004-12-07 04:03:45 +0000130void LiveIntervals::print(std::ostream &O, const Module* ) const {
Chris Lattner70ca3582004-09-30 15:59:17 +0000131 O << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000132 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000133 I->second.print(DOUT, tri_);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000134 DOUT << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000135 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000136
137 O << "********** MACHINEINSTRS **********\n";
138 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
139 mbbi != mbbe; ++mbbi) {
140 O << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
141 for (MachineBasicBlock::iterator mii = mbbi->begin(),
142 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner477e4552004-09-30 16:10:45 +0000143 O << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner70ca3582004-09-30 15:59:17 +0000144 }
145 }
146}
147
Evan Chengc92da382007-11-03 07:20:12 +0000148/// conflictsWithPhysRegDef - Returns true if the specified register
149/// is defined during the duration of the specified interval.
150bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
151 VirtRegMap &vrm, unsigned reg) {
152 for (LiveInterval::Ranges::const_iterator
153 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
154 for (unsigned index = getBaseIndex(I->start),
155 end = getBaseIndex(I->end-1) + InstrSlots::NUM; index != end;
156 index += InstrSlots::NUM) {
157 // skip deleted instructions
158 while (index != end && !getInstructionFromIndex(index))
159 index += InstrSlots::NUM;
160 if (index == end) break;
161
162 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng5d446262007-11-15 08:13:29 +0000163 unsigned SrcReg, DstReg;
164 if (tii_->isMoveInstr(*MI, SrcReg, DstReg))
165 if (SrcReg == li.reg || DstReg == li.reg)
166 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000167 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
168 MachineOperand& mop = MI->getOperand(i);
Evan Cheng5d446262007-11-15 08:13:29 +0000169 if (!mop.isRegister())
Evan Chengc92da382007-11-03 07:20:12 +0000170 continue;
171 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000172 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000173 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000174 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000175 if (!vrm.hasPhys(PhysReg))
176 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000177 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000178 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000179 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000180 return true;
181 }
182 }
183 }
184
185 return false;
186}
187
Evan Cheng549f27d32007-08-13 23:45:17 +0000188void LiveIntervals::printRegName(unsigned reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000189 if (TargetRegisterInfo::isPhysicalRegister(reg))
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000190 cerr << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000191 else
192 cerr << "%reg" << reg;
193}
194
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000195void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000196 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000197 unsigned MIIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000198 LiveInterval &interval) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000199 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000200 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000201
Evan Cheng419852c2008-04-03 16:39:43 +0000202 if (mi->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
203 DOUT << "is a implicit_def\n";
204 return;
205 }
206
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000207 // Virtual registers may be defined multiple times (due to phi
208 // elimination and 2-addr elimination). Much of what we do only has to be
209 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000210 // time we see a vreg.
211 if (interval.empty()) {
212 // Get the Idx of the defining instructions.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000213 unsigned defIndex = getDefIndex(MIIdx);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000214 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000215 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000216 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000217 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000218 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000219 tii_->isMoveInstr(*mi, SrcReg, DstReg))
220 CopyMI = mi;
221 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000222
223 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000224
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000225 // Loop over all of the blocks that the vreg is defined in. There are
226 // two cases we have to handle here. The most common case is a vreg
227 // whose lifetime is contained within a basic block. In this case there
228 // will be a single kill, in MBB, which comes after the definition.
229 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
230 // FIXME: what about dead vars?
231 unsigned killIdx;
232 if (vi.Kills[0] != mi)
233 killIdx = getUseIndex(getInstructionIndex(vi.Kills[0]))+1;
234 else
235 killIdx = defIndex+1;
Chris Lattner6097d132004-07-19 02:15:56 +0000236
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000237 // If the kill happens after the definition, we have an intra-block
238 // live range.
239 if (killIdx > defIndex) {
Evan Cheng61de82d2007-02-15 05:59:24 +0000240 assert(vi.AliveBlocks.none() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000241 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000242 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000243 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000244 DOUT << " +" << LR << "\n";
Evan Chengf3bb2e62007-09-05 21:46:51 +0000245 interval.addKill(ValNo, killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000246 return;
247 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000248 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000249
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000250 // The other case we handle is when a virtual register lives to the end
251 // of the defining block, potentially live across some blocks, then is
252 // live into some number of blocks, but gets killed. Start by adding a
253 // range that goes from this definition to the end of the defining block.
Alkis Evlogimenosd19e2902004-08-31 17:39:15 +0000254 LiveRange NewLR(defIndex,
255 getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000256 ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000257 DOUT << " +" << NewLR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000258 interval.addRange(NewLR);
259
260 // Iterate over all of the blocks that the variable is completely
261 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
262 // live interval.
263 for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
264 if (vi.AliveBlocks[i]) {
Chris Lattner428b92e2006-09-15 03:57:23 +0000265 MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
266 if (!MBB->empty()) {
267 LiveRange LR(getMBBStartIdx(i),
268 getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000269 ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000270 interval.addRange(LR);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000271 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000272 }
273 }
274 }
275
276 // Finally, this virtual register is live from the start of any killing
277 // block to the 'use' slot of the killing instruction.
278 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
279 MachineInstr *Kill = vi.Kills[i];
Evan Cheng8df78602007-08-08 03:00:28 +0000280 unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
Chris Lattner428b92e2006-09-15 03:57:23 +0000281 LiveRange LR(getMBBStartIdx(Kill->getParent()),
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000282 killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000284 interval.addKill(ValNo, killIdx);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000285 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 }
287
288 } else {
289 // If this is the second time we see a virtual register definition, it
290 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000291 // the result of two address elimination, then the vreg is one of the
292 // def-and-use register operand.
Evan Cheng32dfbea2007-10-12 08:50:34 +0000293 if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 // If this is a two-address definition, then we have already processed
295 // the live range. The only problem is that we didn't realize there
296 // are actually two values in the live interval. Because of this we
297 // need to take the LiveRegion that defines this register and split it
298 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000299 assert(interval.containsOneValue());
300 unsigned DefIndex = getDefIndex(interval.getValNumInfo(0)->def);
Chris Lattner6b128bd2006-09-03 08:07:11 +0000301 unsigned RedefIndex = getDefIndex(MIIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302
Evan Cheng4f8ff162007-08-11 00:59:19 +0000303 const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000304 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000305
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000306 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000307 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000308 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000309
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000310 // Two-address vregs should always only be redefined once. This means
311 // that at this point, there should be exactly one value number in it.
312 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
313
Chris Lattner91725b72006-08-31 05:54:43 +0000314 // The new value number (#1) is defined by the instruction we claimed
315 // defined value #0.
Evan Chengc8d044e2008-02-15 18:24:29 +0000316 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->copy,
317 VNInfoAllocator);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000318
Chris Lattner91725b72006-08-31 05:54:43 +0000319 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000320 OldValNo->def = RedefIndex;
321 OldValNo->copy = 0;
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000322
323 // Add the new live interval which replaces the range for the input copy.
324 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000325 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000327 interval.addKill(ValNo, RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328
329 // If this redefinition is dead, we need to add a dummy unit live
330 // range covering the def slot.
Evan Cheng6130f662008-03-05 00:59:57 +0000331 if (mi->registerDefIsDead(interval.reg, tri_))
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000332 interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000333
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000334 DOUT << " RESULT: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000335 interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336
337 } else {
338 // Otherwise, this must be because of phi elimination. If this is the
339 // first redefinition of the vreg that we have seen, go back and change
340 // the live range in the PHI block to be a different value number.
341 if (interval.containsOneValue()) {
342 assert(vi.Kills.size() == 1 &&
343 "PHI elimination vreg should have one kill, the PHI itself!");
344
345 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000346 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000347 MachineInstr *Killer = vi.Kills[0];
Chris Lattner428b92e2006-09-15 03:57:23 +0000348 unsigned Start = getMBBStartIdx(Killer->getParent());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000349 unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
Evan Cheng56fdd7a2007-03-15 21:19:28 +0000350 DOUT << " Removing [" << Start << "," << End << "] from: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000351 interval.print(DOUT, tri_); DOUT << "\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000352 interval.removeRange(Start, End);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000353 VNI->hasPHIKill = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000354 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000355
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000356 // Replace the interval with one of a NEW value number. Note that this
357 // value number isn't actually defined by an instruction, weird huh? :)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000358 LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000359 DOUT << " replace range with " << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000360 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000361 interval.addKill(LR.valno, End);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000362 DOUT << " RESULT: "; interval.print(DOUT, tri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000363 }
364
365 // In the case of PHI elimination, each variable definition is only
366 // live until the end of the block. We've already taken care of the
367 // rest of the live range.
Chris Lattner6b128bd2006-09-03 08:07:11 +0000368 unsigned defIndex = getDefIndex(MIIdx);
Chris Lattner91725b72006-08-31 05:54:43 +0000369
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000370 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000371 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000372 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000373 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000374 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000375 tii_->isMoveInstr(*mi, SrcReg, DstReg))
376 CopyMI = mi;
377 ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000378
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000379 unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000380 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 interval.addRange(LR);
Evan Chengc3fc7d92007-11-29 09:49:23 +0000382 interval.addKill(ValNo, killIndex);
383 ValNo->hasPHIKill = true;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000384 DOUT << " +" << LR;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 }
386 }
387
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000388 DOUT << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000389}
390
Chris Lattnerf35fef72004-07-23 21:24:19 +0000391void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000392 MachineBasicBlock::iterator mi,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000393 unsigned MIIdx,
Chris Lattner91725b72006-08-31 05:54:43 +0000394 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000395 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000396 // A physical register cannot be live across basic block, so its
397 // lifetime must end somewhere in its defining basic block.
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000398 DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000399
Chris Lattner6b128bd2006-09-03 08:07:11 +0000400 unsigned baseIndex = MIIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 unsigned start = getDefIndex(baseIndex);
402 unsigned end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000403
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000404 // If it is not used after definition, it is considered dead at
405 // the instruction defining it. Hence its interval is:
406 // [defSlot(def), defSlot(def)+1)
Evan Cheng6130f662008-03-05 00:59:57 +0000407 if (mi->registerDefIsDead(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000408 DOUT << " dead";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000409 end = getDefIndex(start) + 1;
410 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000411 }
412
413 // If it is not dead on definition, it must be killed by a
414 // subsequent instruction. Hence its interval is:
415 // [defSlot(def), useSlot(kill)+1)
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000416 while (++mi != MBB->end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000417 baseIndex += InstrSlots::NUM;
Evan Cheng6130f662008-03-05 00:59:57 +0000418 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000419 DOUT << " killed";
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000420 end = getUseIndex(baseIndex) + 1;
421 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000422 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Cheng9a1956a2006-11-15 20:54:11 +0000423 // Another instruction redefines the register before it is ever read.
424 // Then the register is essentially dead at the instruction that defines
425 // it. Hence its interval is:
426 // [defSlot(def), defSlot(def)+1)
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000427 DOUT << " dead";
Evan Cheng9a1956a2006-11-15 20:54:11 +0000428 end = getDefIndex(start) + 1;
429 goto exit;
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000430 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000431 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000432
433 // The only case we should have a dead physreg here without a killing or
434 // instruction where we know it's dead is if it is live-in to the function
435 // and never used.
Evan Chengc8d044e2008-02-15 18:24:29 +0000436 assert(!CopyMI && "physreg was not killed in defining block!");
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000437 end = getDefIndex(start) + 1; // It's dead.
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000438
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000439exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000440 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000441
Evan Cheng24a3cc42007-04-25 07:30:23 +0000442 // Already exists? Extend old live interval.
443 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000444 VNInfo *ValNo = (OldLR != interval.end())
Evan Chengc8d044e2008-02-15 18:24:29 +0000445 ? OldLR->valno : interval.getNextValue(start, CopyMI, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000446 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000447 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000448 interval.addKill(LR.valno, end);
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000449 DOUT << " +" << LR << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000450}
451
Chris Lattnerf35fef72004-07-23 21:24:19 +0000452void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
453 MachineBasicBlock::iterator MI,
Chris Lattner6b128bd2006-09-03 08:07:11 +0000454 unsigned MIIdx,
Chris Lattnerf35fef72004-07-23 21:24:19 +0000455 unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000456 if (TargetRegisterInfo::isVirtualRegister(reg))
Chris Lattner6b128bd2006-09-03 08:07:11 +0000457 handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
Alkis Evlogimenos53278012004-08-26 22:22:38 +0000458 else if (allocatableRegs_[reg]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000459 MachineInstr *CopyMI = NULL;
Chris Lattner91725b72006-08-31 05:54:43 +0000460 unsigned SrcReg, DstReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000461 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000462 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Evan Chengc8d044e2008-02-15 18:24:29 +0000463 tii_->isMoveInstr(*MI, SrcReg, DstReg))
464 CopyMI = MI;
465 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000466 // Def of a register also defines its sub-registers.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000467 for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000468 // If MI also modifies the sub-register explicitly, avoid processing it
469 // more than once. Do not pass in TRI here so it checks for exact match.
470 if (!MI->modifiesRegister(*AS))
Evan Cheng24a3cc42007-04-25 07:30:23 +0000471 handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000472 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000473}
474
Evan Chengb371f452007-02-19 21:49:54 +0000475void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000476 unsigned MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000477 LiveInterval &interval, bool isAlias) {
Evan Chengb371f452007-02-19 21:49:54 +0000478 DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
479
480 // Look for kills, if it reaches a def before it's killed, then it shouldn't
481 // be considered a livein.
482 MachineBasicBlock::iterator mi = MBB->begin();
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000483 unsigned baseIndex = MIIdx;
484 unsigned start = baseIndex;
Evan Chengb371f452007-02-19 21:49:54 +0000485 unsigned end = start;
486 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000487 if (mi->killsRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000488 DOUT << " killed";
489 end = getUseIndex(baseIndex) + 1;
490 goto exit;
Evan Cheng6130f662008-03-05 00:59:57 +0000491 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000492 // Another instruction redefines the register before it is ever read.
493 // Then the register is essentially dead at the instruction that defines
494 // it. Hence its interval is:
495 // [defSlot(def), defSlot(def)+1)
496 DOUT << " dead";
497 end = getDefIndex(start) + 1;
498 goto exit;
499 }
500
501 baseIndex += InstrSlots::NUM;
502 ++mi;
503 }
504
505exit:
Evan Cheng75611fb2007-06-27 01:16:36 +0000506 // Live-in register might not be used at all.
507 if (end == MIIdx) {
Evan Cheng292da942007-06-27 18:47:28 +0000508 if (isAlias) {
509 DOUT << " dead";
Evan Cheng75611fb2007-06-27 01:16:36 +0000510 end = getDefIndex(MIIdx) + 1;
Evan Cheng292da942007-06-27 18:47:28 +0000511 } else {
512 DOUT << " live through";
513 end = baseIndex;
514 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000515 }
516
Evan Chengf3bb2e62007-09-05 21:46:51 +0000517 LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000518 interval.addRange(LR);
Evan Chengf3bb2e62007-09-05 21:46:51 +0000519 interval.addKill(LR.valno, end);
Evan Cheng24c2e5c2007-08-08 07:03:29 +0000520 DOUT << " +" << LR << '\n';
Evan Chengb371f452007-02-19 21:49:54 +0000521}
522
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000523/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000524/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000525/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000526/// which a variable is live
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000527void LiveIntervals::computeIntervals() {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000528 DOUT << "********** COMPUTING LIVE INTERVALS **********\n"
529 << "********** Function: "
530 << ((Value*)mf_->getFunction())->getName() << '\n';
Chris Lattner6b128bd2006-09-03 08:07:11 +0000531 // Track the index of the current machine instr.
532 unsigned MIIndex = 0;
Chris Lattner428b92e2006-09-15 03:57:23 +0000533 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
534 MBBI != E; ++MBBI) {
535 MachineBasicBlock *MBB = MBBI;
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000536 DOUT << ((Value*)MBB->getBasicBlock())->getName() << ":\n";
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000537
Chris Lattner428b92e2006-09-15 03:57:23 +0000538 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000539
Dan Gohmancb406c22007-10-03 19:26:29 +0000540 // Create intervals for live-ins to this BB first.
541 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
542 LE = MBB->livein_end(); LI != LE; ++LI) {
543 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
544 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000545 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000546 if (!hasInterval(*AS))
547 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
548 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000549 }
550
Chris Lattner428b92e2006-09-15 03:57:23 +0000551 for (; MI != miEnd; ++MI) {
Bill Wendlingbdc679d2006-11-29 00:39:47 +0000552 DOUT << MIIndex << "\t" << *MI;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000553
Evan Cheng438f7bc2006-11-10 08:43:01 +0000554 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000555 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
556 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000557 // handle register defs - build intervals
Chris Lattner428b92e2006-09-15 03:57:23 +0000558 if (MO.isRegister() && MO.getReg() && MO.isDef())
559 handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000560 }
Chris Lattner6b128bd2006-09-03 08:07:11 +0000561
562 MIIndex += InstrSlots::NUM;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000563 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000565}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000566
Evan Cheng4ca980e2007-10-17 02:10:22 +0000567bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
Evan Chenga5bfc972007-10-17 06:53:44 +0000568 SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
Evan Cheng4ca980e2007-10-17 02:10:22 +0000569 std::vector<IdxMBBPair>::const_iterator I =
570 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
571
572 bool ResVal = false;
573 while (I != Idx2MBBMap.end()) {
574 if (LR.end <= I->first)
575 break;
576 MBBs.push_back(I->second);
577 ResVal = true;
578 ++I;
579 }
580 return ResVal;
581}
582
583
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000584LiveInterval LiveIntervals::createInterval(unsigned reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000585 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
Jim Laskey7902c752006-11-07 12:25:45 +0000586 HUGE_VALF : 0.0F;
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000587 return LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000588}
Evan Chengf2fbca62007-11-12 06:35:08 +0000589
Evan Chengc8d044e2008-02-15 18:24:29 +0000590/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
591/// copy field and returns the source register that defines it.
592unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
593 if (!VNI->copy)
594 return 0;
595
596 if (VNI->copy->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
597 return VNI->copy->getOperand(1).getReg();
Evan Cheng7e073ba2008-04-09 20:57:25 +0000598 if (VNI->copy->getOpcode() == TargetInstrInfo::INSERT_SUBREG)
599 return VNI->copy->getOperand(2).getReg();
Evan Chengc8d044e2008-02-15 18:24:29 +0000600 unsigned SrcReg, DstReg;
601 if (tii_->isMoveInstr(*VNI->copy, SrcReg, DstReg))
602 return SrcReg;
603 assert(0 && "Unrecognized copy instruction!");
604 return 0;
605}
Evan Chengf2fbca62007-11-12 06:35:08 +0000606
607//===----------------------------------------------------------------------===//
608// Register allocator hooks.
609//
610
Evan Chengd70dbb52008-02-22 09:24:50 +0000611/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
612/// allow one) virtual register operand, then its uses are implicitly using
613/// the register. Returns the virtual register.
614unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
615 MachineInstr *MI) const {
616 unsigned RegOp = 0;
617 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
618 MachineOperand &MO = MI->getOperand(i);
619 if (!MO.isRegister() || !MO.isUse())
620 continue;
621 unsigned Reg = MO.getReg();
622 if (Reg == 0 || Reg == li.reg)
623 continue;
624 // FIXME: For now, only remat MI with at most one register operand.
625 assert(!RegOp &&
626 "Can't rematerialize instruction with multiple register operand!");
627 RegOp = MO.getReg();
628 break;
629 }
630 return RegOp;
631}
632
633/// isValNoAvailableAt - Return true if the val# of the specified interval
634/// which reaches the given instruction also reaches the specified use index.
635bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
636 unsigned UseIdx) const {
637 unsigned Index = getInstructionIndex(MI);
638 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
639 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
640 return UI != li.end() && UI->valno == ValNo;
641}
642
Evan Chengf2fbca62007-11-12 06:35:08 +0000643/// isReMaterializable - Returns true if the definition MI of the specified
644/// val# of the specified interval is re-materializable.
645bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000646 const VNInfo *ValNo, MachineInstr *MI,
647 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000648 if (DisableReMat)
649 return false;
650
Evan Cheng5ef3a042007-12-06 00:01:56 +0000651 isLoad = false;
Evan Cheng20ccded2008-03-15 00:19:36 +0000652 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
Evan Chengd70dbb52008-02-22 09:24:50 +0000653 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000654
655 int FrameIdx = 0;
656 if (tii_->isLoadFromStackSlot(MI, FrameIdx) &&
Evan Cheng249ded32008-02-23 03:38:34 +0000657 mf_->getFrameInfo()->isImmutableObjectIndex(FrameIdx))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000658 // FIXME: Let target specific isReallyTriviallyReMaterializable determines
659 // this but remember this is not safe to fold into a two-address
660 // instruction.
Evan Cheng249ded32008-02-23 03:38:34 +0000661 // This is a load from fixed stack slot. It can be rematerialized.
Evan Chengdd3465e2008-02-23 01:44:27 +0000662 return true;
Evan Chengdd3465e2008-02-23 01:44:27 +0000663
Evan Chengd70dbb52008-02-22 09:24:50 +0000664 if (tii_->isTriviallyReMaterializable(MI)) {
Evan Cheng20ccded2008-03-15 00:19:36 +0000665 const TargetInstrDesc &TID = MI->getDesc();
Chris Lattner749c6f62008-01-07 07:27:27 +0000666 isLoad = TID.isSimpleLoad();
Evan Chengd70dbb52008-02-22 09:24:50 +0000667
668 unsigned ImpUse = getReMatImplicitUse(li, MI);
669 if (ImpUse) {
670 const LiveInterval &ImpLi = getInterval(ImpUse);
671 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
672 re = mri_->use_end(); ri != re; ++ri) {
673 MachineInstr *UseMI = &*ri;
674 unsigned UseIdx = getInstructionIndex(UseMI);
675 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
676 continue;
Evan Cheng298bbe82008-02-23 02:14:42 +0000677 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
Evan Chengd70dbb52008-02-22 09:24:50 +0000678 return false;
679 }
680 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000681 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000682 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000683
Evan Chengdd3465e2008-02-23 01:44:27 +0000684 return false;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000685}
686
687/// isReMaterializable - Returns true if every definition of MI of every
688/// val# of the specified interval is re-materializable.
689bool LiveIntervals::isReMaterializable(const LiveInterval &li, bool &isLoad) {
690 isLoad = false;
691 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
692 i != e; ++i) {
693 const VNInfo *VNI = *i;
694 unsigned DefIdx = VNI->def;
695 if (DefIdx == ~1U)
696 continue; // Dead val#.
697 // Is the def for the val# rematerializable?
698 if (DefIdx == ~0u)
699 return false;
700 MachineInstr *ReMatDefMI = getInstructionFromIndex(DefIdx);
701 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000702 if (!ReMatDefMI ||
703 !isReMaterializable(li, VNI, ReMatDefMI, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000704 return false;
705 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000706 }
707 return true;
708}
709
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000710/// FilterFoldedOps - Filter out two-address use operands. Return
711/// true if it finds any issue with the operands that ought to prevent
712/// folding.
713static bool FilterFoldedOps(MachineInstr *MI,
714 SmallVector<unsigned, 2> &Ops,
715 unsigned &MRInfo,
716 SmallVector<unsigned, 2> &FoldOps) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000717 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng6e141fd2007-12-12 23:12:09 +0000718
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000719 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000720 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
721 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000722 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000723 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000724 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000725 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000726 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000727 MRInfo |= (unsigned)VirtRegMap::isMod;
728 else {
729 // Filter out two-address use operand(s).
Evan Chengd70dbb52008-02-22 09:24:50 +0000730 if (!MO.isImplicit() &&
731 TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) {
Evan Chengaee4af62007-12-02 08:30:39 +0000732 MRInfo = VirtRegMap::isModRef;
733 continue;
734 }
735 MRInfo |= (unsigned)VirtRegMap::isRef;
736 }
737 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000738 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000739 return false;
740}
741
742
743/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
744/// slot / to reg or any rematerialized load into ith operand of specified
745/// MI. If it is successul, MI is updated with the newly created MI and
746/// returns true.
747bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
748 VirtRegMap &vrm, MachineInstr *DefMI,
749 unsigned InstrIdx,
750 SmallVector<unsigned, 2> &Ops,
751 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000752 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000753 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000754 RemoveMachineInstrFromMaps(MI);
755 vrm.RemoveMachineInstrFromMaps(MI);
756 MI->eraseFromParent();
757 ++numFolds;
758 return true;
759 }
760
761 // Filter the list of operand indexes that are to be folded. Abort if
762 // any operand will prevent folding.
763 unsigned MRInfo = 0;
764 SmallVector<unsigned, 2> FoldOps;
765 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
766 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000767
Evan Cheng427f4c12008-03-31 23:19:51 +0000768 // The only time it's safe to fold into a two address instruction is when
769 // it's folding reload and spill from / into a spill stack slot.
770 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000771 return false;
772
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000773 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
774 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000775 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000776 // Remember this instruction uses the spill slot.
777 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
778
Evan Chengf2fbca62007-11-12 06:35:08 +0000779 // Attempt to fold the memory reference into the instruction. If
780 // we can do this, we don't need to insert spill code.
781 if (lv_)
782 lv_->instructionChanged(MI, fmi);
Evan Cheng81a03822007-11-17 00:40:40 +0000783 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000784 fmi->copyKillDeadInfo(MI, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +0000785 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000786 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000787 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000788 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000789 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000790 vrm.transferEmergencySpills(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000791 mi2iMap_.erase(MI);
Evan Chengcddbb832007-11-30 21:23:43 +0000792 i2miMap_[InstrIdx /InstrSlots::NUM] = fmi;
793 mi2iMap_[fmi] = InstrIdx;
Evan Chengf2fbca62007-11-12 06:35:08 +0000794 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000795 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000796 return true;
797 }
798 return false;
799}
800
Evan Cheng018f9b02007-12-05 03:22:34 +0000801/// canFoldMemoryOperand - Returns true if the specified load / store
802/// folding is possible.
803bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000804 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000805 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000806 // Filter the list of operand indexes that are to be folded. Abort if
807 // any operand will prevent folding.
808 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000809 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000810 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
811 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000812
Evan Cheng3c75ba82008-04-01 21:37:32 +0000813 // It's only legal to remat for a use, not a def.
814 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000815 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000816
Evan Chengd70dbb52008-02-22 09:24:50 +0000817 return tii_->canFoldMemoryOperand(MI, FoldOps);
818}
819
Evan Cheng81a03822007-11-17 00:40:40 +0000820bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
821 SmallPtrSet<MachineBasicBlock*, 4> MBBs;
822 for (LiveInterval::Ranges::const_iterator
823 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
824 std::vector<IdxMBBPair>::const_iterator II =
825 std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), I->start);
826 if (II == Idx2MBBMap.end())
827 continue;
828 if (I->end > II->first) // crossing a MBB.
829 return false;
830 MBBs.insert(II->second);
831 if (MBBs.size() > 1)
832 return false;
833 }
834 return true;
835}
836
Evan Chengd70dbb52008-02-22 09:24:50 +0000837/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
838/// interval on to-be re-materialized operands of MI) with new register.
839void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
840 MachineInstr *MI, unsigned NewVReg,
841 VirtRegMap &vrm) {
842 // There is an implicit use. That means one of the other operand is
843 // being remat'ed and the remat'ed instruction has li.reg as an
844 // use operand. Make sure we rewrite that as well.
845 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
846 MachineOperand &MO = MI->getOperand(i);
847 if (!MO.isRegister())
848 continue;
849 unsigned Reg = MO.getReg();
850 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
851 continue;
852 if (!vrm.isReMaterialized(Reg))
853 continue;
854 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000855 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
856 if (UseMO)
857 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000858 }
859}
860
Evan Chengf2fbca62007-11-12 06:35:08 +0000861/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
862/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +0000863bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +0000864rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
865 bool TrySplit, unsigned index, unsigned end, MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +0000866 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +0000867 unsigned Slot, int LdSlot,
868 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +0000869 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +0000870 const TargetRegisterClass* rc,
871 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +0000872 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +0000873 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Evan Cheng1953d0c2007-11-29 10:12:14 +0000874 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +0000875 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +0000876 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +0000877 RestartInstruction:
878 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
879 MachineOperand& mop = MI->getOperand(i);
880 if (!mop.isRegister())
881 continue;
882 unsigned Reg = mop.getReg();
883 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000884 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +0000885 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +0000886 if (Reg != li.reg)
887 continue;
888
889 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +0000890 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +0000891 int FoldSlot = Slot;
892 if (DefIsReMat) {
893 // If this is the rematerializable definition MI itself and
894 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +0000895 if (MI == ReMatOrigDefMI && CanDelete) {
Evan Chengcddbb832007-11-30 21:23:43 +0000896 DOUT << "\t\t\t\tErasing re-materlizable def: ";
897 DOUT << MI << '\n';
Evan Chengf2fbca62007-11-12 06:35:08 +0000898 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +0000899 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000900 MI->eraseFromParent();
901 break;
902 }
903
904 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +0000905 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +0000906 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +0000907 if (isLoad) {
908 // Try fold loads (from stack slot, constant pool, etc.) into uses.
909 FoldSS = isLoadSS;
910 FoldSlot = LdSlot;
911 }
912 }
913
Evan Chengf2fbca62007-11-12 06:35:08 +0000914 // Scan all of the operands of this instruction rewriting operands
915 // to use NewVReg instead of li.reg as appropriate. We do this for
916 // two reasons:
917 //
918 // 1. If the instr reads the same spilled vreg multiple times, we
919 // want to reuse the NewVReg.
920 // 2. If the instr is a two-addr instruction, we are required to
921 // keep the src/dst regs pinned.
922 //
923 // Keep track of whether we replace a use and/or def so that we can
924 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +0000925
Evan Cheng81a03822007-11-17 00:40:40 +0000926 HasUse = mop.isUse();
927 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +0000928 SmallVector<unsigned, 2> Ops;
929 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +0000930 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +0000931 const MachineOperand &MOj = MI->getOperand(j);
932 if (!MOj.isRegister())
Evan Chengf2fbca62007-11-12 06:35:08 +0000933 continue;
Evan Chengaee4af62007-12-02 08:30:39 +0000934 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000935 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +0000936 continue;
937 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +0000938 Ops.push_back(j);
939 HasUse |= MOj.isUse();
940 HasDef |= MOj.isDef();
Evan Chengf2fbca62007-11-12 06:35:08 +0000941 }
942 }
943
Evan Cheng018f9b02007-12-05 03:22:34 +0000944 if (TryFold) {
945 // Do not fold load / store here if we are splitting. We'll find an
946 // optimal point to insert a load / store later.
947 if (!TrySplit) {
948 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
949 Ops, FoldSS, FoldSlot, Reg)) {
950 // Folding the load/store can completely change the instruction in
951 // unpredictable ways, rescan it from the beginning.
952 HasUse = false;
953 HasDef = false;
954 CanFold = false;
Evan Cheng7e073ba2008-04-09 20:57:25 +0000955 if (isRemoved(MI))
956 break;
Evan Cheng018f9b02007-12-05 03:22:34 +0000957 goto RestartInstruction;
958 }
959 } else {
Evan Cheng3c75ba82008-04-01 21:37:32 +0000960 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +0000961 }
Evan Cheng6e141fd2007-12-12 23:12:09 +0000962 } else
963 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +0000964
965 // Create a new virtual register for the spill interval.
966 bool CreatedNewVReg = false;
967 if (NewVReg == 0) {
Evan Chengd70dbb52008-02-22 09:24:50 +0000968 NewVReg = mri_->createVirtualRegister(rc);
Evan Chengcddbb832007-11-30 21:23:43 +0000969 vrm.grow();
970 CreatedNewVReg = true;
971 }
972 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000973 if (mop.isImplicit())
974 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +0000975
976 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +0000977 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
978 MachineOperand &mopj = MI->getOperand(Ops[j]);
979 mopj.setReg(NewVReg);
980 if (mopj.isImplicit())
981 rewriteImplicitOps(li, MI, NewVReg, vrm);
982 }
Evan Chengcddbb832007-11-30 21:23:43 +0000983
Evan Cheng81a03822007-11-17 00:40:40 +0000984 if (CreatedNewVReg) {
985 if (DefIsReMat) {
986 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI/*, CanDelete*/);
Evan Chengd70dbb52008-02-22 09:24:50 +0000987 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +0000988 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +0000989 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +0000990 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +0000991 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +0000992 }
993 if (!CanDelete || (HasUse && HasDef)) {
994 // If this is a two-addr instruction then its use operands are
995 // rematerializable but its def is not. It should be assigned a
996 // stack slot.
997 vrm.assignVirt2StackSlot(NewVReg, Slot);
998 }
Evan Chengf2fbca62007-11-12 06:35:08 +0000999 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001000 vrm.assignVirt2StackSlot(NewVReg, Slot);
1001 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001002 } else if (HasUse && HasDef &&
1003 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1004 // If this interval hasn't been assigned a stack slot (because earlier
1005 // def is a deleted remat def), do it now.
1006 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1007 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001008 }
1009
Evan Cheng313d4b82008-02-23 00:33:04 +00001010 // Re-matting an instruction with virtual register use. Add the
1011 // register as an implicit use on the use MI.
1012 if (DefIsReMat && ImpUse)
1013 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1014
Evan Chengf2fbca62007-11-12 06:35:08 +00001015 // create a new register interval for this spill / remat.
1016 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001017 if (CreatedNewVReg) {
1018 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001019 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001020 if (TrySplit)
1021 vrm.setIsSplitFromReg(NewVReg, li.reg);
1022 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001023
1024 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001025 if (CreatedNewVReg) {
1026 LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
1027 nI.getNextValue(~0U, 0, VNInfoAllocator));
1028 DOUT << " +" << LR;
1029 nI.addRange(LR);
1030 } else {
1031 // Extend the split live interval to this def / use.
1032 unsigned End = getUseIndex(index)+1;
1033 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1034 nI.getValNumInfo(nI.getNumValNums()-1));
1035 DOUT << " +" << LR;
1036 nI.addRange(LR);
1037 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001038 }
1039 if (HasDef) {
1040 LiveRange LR(getDefIndex(index), getStoreIndex(index),
1041 nI.getNextValue(~0U, 0, VNInfoAllocator));
1042 DOUT << " +" << LR;
1043 nI.addRange(LR);
1044 }
Evan Cheng81a03822007-11-17 00:40:40 +00001045
Evan Chengf2fbca62007-11-12 06:35:08 +00001046 DOUT << "\t\t\t\tAdded new interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001047 nI.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001048 DOUT << '\n';
1049 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001050 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001051}
Evan Cheng81a03822007-11-17 00:40:40 +00001052bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001053 const VNInfo *VNI,
1054 MachineBasicBlock *MBB, unsigned Idx) const {
Evan Cheng81a03822007-11-17 00:40:40 +00001055 unsigned End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001056 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
1057 unsigned KillIdx = VNI->kills[j];
1058 if (KillIdx > Idx && KillIdx < End)
1059 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001060 }
1061 return false;
1062}
1063
Evan Cheng1953d0c2007-11-29 10:12:14 +00001064static const VNInfo *findDefinedVNInfo(const LiveInterval &li, unsigned DefIdx) {
1065 const VNInfo *VNI = NULL;
1066 for (LiveInterval::const_vni_iterator i = li.vni_begin(),
1067 e = li.vni_end(); i != e; ++i)
1068 if ((*i)->def == DefIdx) {
1069 VNI = *i;
1070 break;
1071 }
1072 return VNI;
1073}
1074
Evan Cheng063284c2008-02-21 00:34:19 +00001075/// RewriteInfo - Keep track of machine instrs that will be rewritten
1076/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001077namespace {
1078 struct RewriteInfo {
1079 unsigned Index;
1080 MachineInstr *MI;
1081 bool HasUse;
1082 bool HasDef;
1083 RewriteInfo(unsigned i, MachineInstr *mi, bool u, bool d)
1084 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1085 };
Evan Cheng063284c2008-02-21 00:34:19 +00001086
Dan Gohman844731a2008-05-13 00:00:25 +00001087 struct RewriteInfoCompare {
1088 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1089 return LHS.Index < RHS.Index;
1090 }
1091 };
1092}
Evan Cheng063284c2008-02-21 00:34:19 +00001093
Evan Chengf2fbca62007-11-12 06:35:08 +00001094void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001095rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001096 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001097 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001098 unsigned Slot, int LdSlot,
1099 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001100 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001101 const TargetRegisterClass* rc,
1102 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001103 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001104 BitVector &SpillMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001105 std::map<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001106 BitVector &RestoreMBBs,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001107 std::map<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1108 std::map<unsigned,unsigned> &MBBVRegsMap,
Evan Chengf2fbca62007-11-12 06:35:08 +00001109 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001110 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001111 unsigned NewVReg = 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001112 unsigned start = getBaseIndex(I->start);
Evan Chengf2fbca62007-11-12 06:35:08 +00001113 unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
Evan Chengf2fbca62007-11-12 06:35:08 +00001114
Evan Cheng063284c2008-02-21 00:34:19 +00001115 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001116 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001117 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001118 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1119 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001120 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001121 MachineOperand &O = ri.getOperand();
1122 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001123 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Evan Cheng063284c2008-02-21 00:34:19 +00001124 unsigned index = getInstructionIndex(MI);
1125 if (index < start || index >= end)
1126 continue;
1127 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1128 }
1129 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1130
Evan Cheng313d4b82008-02-23 00:33:04 +00001131 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001132 // Now rewrite the defs and uses.
1133 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1134 RewriteInfo &rwi = RewriteMIs[i];
1135 ++i;
1136 unsigned index = rwi.Index;
1137 bool MIHasUse = rwi.HasUse;
1138 bool MIHasDef = rwi.HasDef;
1139 MachineInstr *MI = rwi.MI;
1140 // If MI def and/or use the same register multiple times, then there
1141 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001142 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001143 while (i != e && RewriteMIs[i].MI == MI) {
1144 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001145 bool isUse = RewriteMIs[i].HasUse;
1146 if (isUse) ++NumUses;
1147 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001148 MIHasDef |= RewriteMIs[i].HasDef;
1149 ++i;
1150 }
Evan Cheng81a03822007-11-17 00:40:40 +00001151 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001152
1153 if (ImpUse && MI != ReMatDefMI) {
1154 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001155 // register interval's spill weight to HUGE_VALF to prevent it from
1156 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001157 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001158 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001159 }
1160
Evan Cheng063284c2008-02-21 00:34:19 +00001161 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001162 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001163 if (TrySplit) {
Evan Cheng063284c2008-02-21 00:34:19 +00001164 std::map<unsigned,unsigned>::const_iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001165 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001166 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001167 // One common case:
1168 // x = use
1169 // ...
1170 // ...
1171 // def = ...
1172 // = use
1173 // It's better to start a new interval to avoid artifically
1174 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001175 if (MIHasDef && !MIHasUse) {
1176 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001177 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001178 }
1179 }
Evan Chengcada2452007-11-28 01:28:46 +00001180 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001181
1182 bool IsNew = ThisVReg == 0;
1183 if (IsNew) {
1184 // This ends the previous live interval. If all of its def / use
1185 // can be folded, give it a low spill weight.
1186 if (NewVReg && TrySplit && AllCanFold) {
1187 LiveInterval &nI = getOrCreateInterval(NewVReg);
1188 nI.weight /= 10.0F;
1189 }
1190 AllCanFold = true;
1191 }
1192 NewVReg = ThisVReg;
1193
Evan Cheng81a03822007-11-17 00:40:40 +00001194 bool HasDef = false;
1195 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001196 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng018f9b02007-12-05 03:22:34 +00001197 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1198 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001199 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Cheng313d4b82008-02-23 00:33:04 +00001200 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001201 if (!HasDef && !HasUse)
1202 continue;
1203
Evan Cheng018f9b02007-12-05 03:22:34 +00001204 AllCanFold &= CanFold;
1205
Evan Cheng81a03822007-11-17 00:40:40 +00001206 // Update weight of spill interval.
1207 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001208 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001209 // The spill weight is now infinity as it cannot be spilled again.
1210 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001211 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001212 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001213
1214 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001215 if (HasDef) {
1216 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001217 bool HasKill = false;
1218 if (!HasUse)
1219 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, getDefIndex(index));
1220 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001221 // If this is a two-address code, then this index starts a new VNInfo.
1222 const VNInfo *VNI = findDefinedVNInfo(li, getDefIndex(index));
Evan Cheng0cbb1162007-11-29 01:06:25 +00001223 if (VNI)
1224 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, getDefIndex(index));
1225 }
Evan Chenge3110d02007-12-01 04:42:39 +00001226 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
1227 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001228 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001229 if (SII == SpillIdxes.end()) {
1230 std::vector<SRInfo> S;
1231 S.push_back(SRInfo(index, NewVReg, true));
1232 SpillIdxes.insert(std::make_pair(MBBId, S));
1233 } else if (SII->second.back().vreg != NewVReg) {
1234 SII->second.push_back(SRInfo(index, NewVReg, true));
1235 } else if ((int)index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001236 // If there is an earlier def and this is a two-address
1237 // instruction, then it's not possible to fold the store (which
1238 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001239 SRInfo &Info = SII->second.back();
1240 Info.index = index;
1241 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001242 }
1243 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001244 } else if (SII != SpillIdxes.end() &&
1245 SII->second.back().vreg == NewVReg &&
1246 (int)index > SII->second.back().index) {
1247 // There is an earlier def that's not killed (must be two-address).
1248 // The spill is no longer needed.
1249 SII->second.pop_back();
1250 if (SII->second.empty()) {
1251 SpillIdxes.erase(MBBId);
1252 SpillMBBs.reset(MBBId);
1253 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001254 }
1255 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001256 }
1257
1258 if (HasUse) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001259 std::map<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001260 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001261 if (SII != SpillIdxes.end() &&
1262 SII->second.back().vreg == NewVReg &&
1263 (int)index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001264 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001265 SII->second.back().canFold = false;
1266 std::map<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001267 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001268 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001269 // If we are splitting live intervals, only fold if it's the first
1270 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001271 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001272 else if (IsNew) {
1273 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001274 if (RII == RestoreIdxes.end()) {
1275 std::vector<SRInfo> Infos;
1276 Infos.push_back(SRInfo(index, NewVReg, true));
1277 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1278 } else {
1279 RII->second.push_back(SRInfo(index, NewVReg, true));
1280 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001281 RestoreMBBs.set(MBBId);
1282 }
1283 }
1284
1285 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001286 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001287 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001288 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001289
1290 if (NewVReg && TrySplit && AllCanFold) {
1291 // If all of its def / use can be folded, give it a low spill weight.
1292 LiveInterval &nI = getOrCreateInterval(NewVReg);
1293 nI.weight /= 10.0F;
1294 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001295}
1296
Evan Cheng1953d0c2007-11-29 10:12:14 +00001297bool LiveIntervals::alsoFoldARestore(int Id, int index, unsigned vr,
1298 BitVector &RestoreMBBs,
1299 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1300 if (!RestoreMBBs[Id])
1301 return false;
1302 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1303 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1304 if (Restores[i].index == index &&
1305 Restores[i].vreg == vr &&
1306 Restores[i].canFold)
1307 return true;
1308 return false;
1309}
1310
1311void LiveIntervals::eraseRestoreInfo(int Id, int index, unsigned vr,
1312 BitVector &RestoreMBBs,
1313 std::map<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
1314 if (!RestoreMBBs[Id])
1315 return;
1316 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1317 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1318 if (Restores[i].index == index && Restores[i].vreg)
1319 Restores[i].index = -1;
1320}
Evan Cheng81a03822007-11-17 00:40:40 +00001321
Evan Cheng4cce6b42008-04-11 17:53:36 +00001322/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1323/// spilled and create empty intervals for their uses.
1324void
1325LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1326 const TargetRegisterClass* rc,
1327 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001328 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1329 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001330 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001331 MachineInstr *MI = &*ri;
1332 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001333 if (O.isDef()) {
1334 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1335 "Register def was not rewritten?");
1336 RemoveMachineInstrFromMaps(MI);
1337 vrm.RemoveMachineInstrFromMaps(MI);
1338 MI->eraseFromParent();
1339 } else {
1340 // This must be an use of an implicit_def so it's not part of the live
1341 // interval. Create a new empty live interval for it.
1342 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1343 unsigned NewVReg = mri_->createVirtualRegister(rc);
1344 vrm.grow();
1345 vrm.setIsImplicitlyDefined(NewVReg);
1346 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1347 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1348 MachineOperand &MO = MI->getOperand(i);
1349 if (MO.isReg() && MO.getReg() == li.reg)
1350 MO.setReg(NewVReg);
1351 }
1352 }
Evan Cheng419852c2008-04-03 16:39:43 +00001353 }
1354}
1355
Evan Cheng81a03822007-11-17 00:40:40 +00001356
Evan Chengf2fbca62007-11-12 06:35:08 +00001357std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001358addIntervalsForSpills(const LiveInterval &li,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001359 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001360 // Since this is called after the analysis is done we don't know if
1361 // LiveVariables is available
1362 lv_ = getAnalysisToUpdate<LiveVariables>();
1363
1364 assert(li.weight != HUGE_VALF &&
1365 "attempt to spill already spilled interval!");
1366
1367 DOUT << "\t\t\t\tadding intervals for spills for interval: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +00001368 li.print(DOUT, tri_);
Evan Chengf2fbca62007-11-12 06:35:08 +00001369 DOUT << '\n';
1370
Evan Cheng81a03822007-11-17 00:40:40 +00001371 // Each bit specify whether it a spill is required in the MBB.
1372 BitVector SpillMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001373 std::map<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001374 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Evan Cheng1953d0c2007-11-29 10:12:14 +00001375 std::map<unsigned, std::vector<SRInfo> > RestoreIdxes;
1376 std::map<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001377 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001378 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001379
1380 unsigned NumValNums = li.getNumValNums();
1381 SmallVector<MachineInstr*, 4> ReMatDefs;
1382 ReMatDefs.resize(NumValNums, NULL);
1383 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1384 ReMatOrigDefs.resize(NumValNums, NULL);
1385 SmallVector<int, 4> ReMatIds;
1386 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1387 BitVector ReMatDelete(NumValNums);
1388 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1389
Evan Cheng81a03822007-11-17 00:40:40 +00001390 // Spilling a split live interval. It cannot be split any further. Also,
1391 // it's also guaranteed to be a single val# / range interval.
1392 if (vrm.getPreSplitReg(li.reg)) {
1393 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001394 // Unset the split kill marker on the last use.
1395 unsigned KillIdx = vrm.getKillPoint(li.reg);
1396 if (KillIdx) {
1397 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1398 assert(KillMI && "Last use disappeared?");
1399 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1400 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001401 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001402 }
Evan Chengadf85902007-12-05 09:51:10 +00001403 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001404 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1405 Slot = vrm.getStackSlot(li.reg);
1406 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1407 MachineInstr *ReMatDefMI = DefIsReMat ?
1408 vrm.getReMaterializedMI(li.reg) : NULL;
1409 int LdSlot = 0;
1410 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1411 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001412 (DefIsReMat && (ReMatDefMI->getDesc().isSimpleLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001413 bool IsFirstRange = true;
1414 for (LiveInterval::Ranges::const_iterator
1415 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1416 // If this is a split live interval with multiple ranges, it means there
1417 // are two-address instructions that re-defined the value. Only the
1418 // first def can be rematerialized!
1419 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001420 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001421 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1422 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001423 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001424 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001425 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001426 } else {
1427 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1428 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001429 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001430 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001431 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001432 }
1433 IsFirstRange = false;
1434 }
Evan Cheng419852c2008-04-03 16:39:43 +00001435
Evan Cheng4cce6b42008-04-11 17:53:36 +00001436 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001437 return NewLIs;
1438 }
1439
1440 bool TrySplit = SplitAtBB && !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001441 if (SplitLimit != -1 && (int)numSplits >= SplitLimit)
1442 TrySplit = false;
1443 if (TrySplit)
1444 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001445 bool NeedStackSlot = false;
1446 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1447 i != e; ++i) {
1448 const VNInfo *VNI = *i;
1449 unsigned VN = VNI->id;
1450 unsigned DefIdx = VNI->def;
1451 if (DefIdx == ~1U)
1452 continue; // Dead val#.
1453 // Is the def for the val# rematerializable?
Evan Cheng81a03822007-11-17 00:40:40 +00001454 MachineInstr *ReMatDefMI = (DefIdx == ~0u)
1455 ? 0 : getInstructionFromIndex(DefIdx);
Evan Cheng5ef3a042007-12-06 00:01:56 +00001456 bool dummy;
1457 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001458 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001459 ReMatOrigDefs[VN] = ReMatDefMI;
Evan Chengf2fbca62007-11-12 06:35:08 +00001460 // Original def may be modified so we have to make a copy here. vrm must
1461 // delete these!
Evan Cheng81a03822007-11-17 00:40:40 +00001462 ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
Evan Chengf2fbca62007-11-12 06:35:08 +00001463
1464 bool CanDelete = true;
Evan Chengc3fc7d92007-11-29 09:49:23 +00001465 if (VNI->hasPHIKill) {
1466 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001467 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001468 CanDelete = false;
1469 // Need a stack slot if there is any live range where uses cannot be
1470 // rematerialized.
1471 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001472 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001473 if (CanDelete)
1474 ReMatDelete.set(VN);
1475 } else {
1476 // Need a stack slot if there is any live range where uses cannot be
1477 // rematerialized.
1478 NeedStackSlot = true;
1479 }
1480 }
1481
1482 // One stack slot per live interval.
Evan Cheng81a03822007-11-17 00:40:40 +00001483 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0)
Evan Chengf2fbca62007-11-12 06:35:08 +00001484 Slot = vrm.assignVirt2StackSlot(li.reg);
1485
1486 // Create new intervals and rewrite defs and uses.
1487 for (LiveInterval::Ranges::const_iterator
1488 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001489 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1490 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1491 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001492 bool CanDelete = ReMatDelete[I->valno->id];
1493 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001494 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001495 bool isLoad = isLoadSS ||
Chris Lattner749c6f62008-01-07 07:27:27 +00001496 (DefIsReMat && ReMatDefMI->getDesc().isSimpleLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001497 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001498 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001499 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001500 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Cheng1953d0c2007-11-29 10:12:14 +00001501 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001502 }
1503
Evan Cheng0cbb1162007-11-29 01:06:25 +00001504 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001505 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001506 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001507 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001508 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001509
Evan Chengb50bb8c2007-12-05 08:16:32 +00001510 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001511 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001512 if (NeedStackSlot) {
1513 int Id = SpillMBBs.find_first();
1514 while (Id != -1) {
1515 std::vector<SRInfo> &spills = SpillIdxes[Id];
1516 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
1517 int index = spills[i].index;
1518 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001519 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001520 bool isReMat = vrm.isReMaterialized(VReg);
1521 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001522 bool CanFold = false;
1523 bool FoundUse = false;
1524 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001525 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001526 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001527 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1528 MachineOperand &MO = MI->getOperand(j);
1529 if (!MO.isRegister() || MO.getReg() != VReg)
1530 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001531
1532 Ops.push_back(j);
1533 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001534 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001535 if (isReMat ||
1536 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1537 RestoreMBBs, RestoreIdxes))) {
1538 // MI has two-address uses of the same register. If the use
1539 // isn't the first and only use in the BB, then we can't fold
1540 // it. FIXME: Move this to rewriteInstructionsForSpills.
1541 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001542 break;
1543 }
Evan Chengaee4af62007-12-02 08:30:39 +00001544 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001545 }
1546 }
1547 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001548 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001549 if (CanFold && !Ops.empty()) {
1550 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001551 Folded = true;
Evan Chengf38d14f2007-12-05 09:05:34 +00001552 if (FoundUse > 0) {
Evan Chengaee4af62007-12-02 08:30:39 +00001553 // Also folded uses, do not issue a load.
1554 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Evan Chengf38d14f2007-12-05 09:05:34 +00001555 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
1556 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001557 nI.removeRange(getDefIndex(index), getStoreIndex(index));
Evan Chengcddbb832007-11-30 21:23:43 +00001558 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001559 }
1560
Evan Cheng7e073ba2008-04-09 20:57:25 +00001561 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001562 if (!Folded) {
1563 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
1564 bool isKill = LR->end == getStoreIndex(index);
Evan Chengb0a6f622008-05-20 08:10:37 +00001565 if (!MI->registerDefIsDead(nI.reg))
1566 // No need to spill a dead def.
1567 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001568 if (isKill)
1569 AddedKill.insert(&nI);
1570 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001571 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001572 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001573 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001574 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001575
Evan Cheng1953d0c2007-11-29 10:12:14 +00001576 int Id = RestoreMBBs.find_first();
1577 while (Id != -1) {
1578 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1579 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
1580 int index = restores[i].index;
1581 if (index == -1)
1582 continue;
1583 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001584 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001585 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001586 bool CanFold = false;
1587 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001588 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001589 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001590 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1591 MachineOperand &MO = MI->getOperand(j);
1592 if (!MO.isRegister() || MO.getReg() != VReg)
1593 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001594
Evan Cheng0cbb1162007-11-29 01:06:25 +00001595 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001596 // If this restore were to be folded, it would have been folded
1597 // already.
1598 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001599 break;
1600 }
Evan Chengaee4af62007-12-02 08:30:39 +00001601 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001602 }
1603 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001604
1605 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001606 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001607 if (CanFold && !Ops.empty()) {
1608 if (!vrm.isReMaterialized(VReg))
1609 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1610 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001611 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1612 int LdSlot = 0;
1613 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1614 // If the rematerializable def is a load, also try to fold it.
Chris Lattner749c6f62008-01-07 07:27:27 +00001615 if (isLoadSS || ReMatDefMI->getDesc().isSimpleLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001616 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1617 Ops, isLoadSS, LdSlot, VReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001618 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1619 if (ImpUse) {
1620 // Re-matting an instruction with virtual register use. Add the
1621 // register as an implicit use on the use MI and update the register
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001622 // interval's spill weight to HUGE_VALF to prevent it from being
1623 // spilled.
Evan Chengd70dbb52008-02-22 09:24:50 +00001624 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001625 ImpLi.weight = HUGE_VALF;
Evan Chengd70dbb52008-02-22 09:24:50 +00001626 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1627 }
Evan Chengaee4af62007-12-02 08:30:39 +00001628 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001629 }
1630 // If folding is not possible / failed, then tell the spiller to issue a
1631 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001632 if (Folded)
1633 nI.removeRange(getLoadIndex(index), getUseIndex(index)+1);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001634 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001635 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001636 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001637 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001638 }
1639
Evan Chengb50bb8c2007-12-05 08:16:32 +00001640 // Finalize intervals: add kills, finalize spill weights, and filter out
1641 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001642 std::vector<LiveInterval*> RetNewLIs;
1643 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1644 LiveInterval *LI = NewLIs[i];
1645 if (!LI->empty()) {
1646 LI->weight /= LI->getSize();
Evan Chengb50bb8c2007-12-05 08:16:32 +00001647 if (!AddedKill.count(LI)) {
1648 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Evan Chengd120ffd2007-12-05 10:24:35 +00001649 unsigned LastUseIdx = getBaseIndex(LR->end);
1650 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001651 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001652 assert(UseIdx != -1);
Evan Chengd70dbb52008-02-22 09:24:50 +00001653 if (LastUse->getOperand(UseIdx).isImplicit() ||
1654 LastUse->getDesc().getOperandConstraint(UseIdx,TOI::TIED_TO) == -1){
Evan Chengb50bb8c2007-12-05 08:16:32 +00001655 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001656 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001657 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001658 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001659 RetNewLIs.push_back(LI);
1660 }
1661 }
Evan Cheng81a03822007-11-17 00:40:40 +00001662
Evan Cheng4cce6b42008-04-11 17:53:36 +00001663 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001664 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001665}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001666
1667/// hasAllocatableSuperReg - Return true if the specified physical register has
1668/// any super register that's allocatable.
1669bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1670 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1671 if (allocatableRegs_[*AS] && hasInterval(*AS))
1672 return true;
1673 return false;
1674}
1675
1676/// getRepresentativeReg - Find the largest super register of the specified
1677/// physical register.
1678unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1679 // Find the largest super-register that is allocatable.
1680 unsigned BestReg = Reg;
1681 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1682 unsigned SuperReg = *AS;
1683 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1684 BestReg = SuperReg;
1685 break;
1686 }
1687 }
1688 return BestReg;
1689}
1690
1691/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1692/// specified interval that conflicts with the specified physical register.
1693unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1694 unsigned PhysReg) const {
1695 unsigned NumConflicts = 0;
1696 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1697 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1698 E = mri_->reg_end(); I != E; ++I) {
1699 MachineOperand &O = I.getOperand();
1700 MachineInstr *MI = O.getParent();
1701 unsigned Index = getInstructionIndex(MI);
1702 if (pli.liveAt(Index))
1703 ++NumConflicts;
1704 }
1705 return NumConflicts;
1706}
1707
1708/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
1709/// around all defs and uses of the specified interval.
1710void LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
1711 unsigned PhysReg, VirtRegMap &vrm) {
1712 unsigned SpillReg = getRepresentativeReg(PhysReg);
1713
1714 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1715 // If there are registers which alias PhysReg, but which are not a
1716 // sub-register of the chosen representative super register. Assert
1717 // since we can't handle it yet.
1718 assert(*AS == SpillReg || !allocatableRegs_[*AS] ||
1719 tri_->isSuperRegister(*AS, SpillReg));
1720
1721 LiveInterval &pli = getInterval(SpillReg);
1722 SmallPtrSet<MachineInstr*, 8> SeenMIs;
1723 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1724 E = mri_->reg_end(); I != E; ++I) {
1725 MachineOperand &O = I.getOperand();
1726 MachineInstr *MI = O.getParent();
1727 if (SeenMIs.count(MI))
1728 continue;
1729 SeenMIs.insert(MI);
1730 unsigned Index = getInstructionIndex(MI);
1731 if (pli.liveAt(Index)) {
1732 vrm.addEmergencySpill(SpillReg, MI);
1733 pli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1734 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS) {
1735 if (!hasInterval(*AS))
1736 continue;
1737 LiveInterval &spli = getInterval(*AS);
1738 if (spli.liveAt(Index))
1739 spli.removeRange(getLoadIndex(Index), getStoreIndex(Index)+1);
1740 }
1741 }
1742 }
1743}