blob: 3c1d4b3ccd755686b195aebb17ddfc9134eb7919 [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"
Dan Gohman6d69ba82008-07-25 00:02:30 +000022#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#include "llvm/CodeGen/LiveVariables.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng22f07ff2007-12-11 02:09:15 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000030#include "llvm/CodeGen/Passes.h"
Lang Hames233a60e2009-11-03 23:52:08 +000031#include "llvm/CodeGen/ProcessImplicitDefs.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetMachine.h"
Owen Anderson95dad832008-10-07 20:22:28 +000035#include "llvm/Target/TargetOptions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000038#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/raw_ostream.h"
Evan Cheng2578ba22009-07-01 01:59:31 +000040#include "llvm/ADT/DepthFirstIterator.h"
41#include "llvm/ADT/SmallSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000044#include <algorithm>
Lang Hamesf41538d2009-06-02 16:53:25 +000045#include <limits>
Jeff Cohen97af7512006-12-02 02:22:01 +000046#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000047using namespace llvm;
48
Dan Gohman844731a2008-05-13 00:00:25 +000049// Hidden options for help debugging.
50static cl::opt<bool> DisableReMat("disable-rematerialization",
51 cl::init(false), cl::Hidden);
Evan Cheng81a03822007-11-17 00:40:40 +000052
Owen Andersonae339ba2008-08-19 00:17:30 +000053static cl::opt<bool> EnableFastSpilling("fast-spill",
54 cl::init(false), cl::Hidden);
55
Evan Cheng752195e2009-09-14 21:33:42 +000056STATISTIC(numIntervals , "Number of original intervals");
57STATISTIC(numFolds , "Number of loads/stores folded into instructions");
58STATISTIC(numSplits , "Number of intervals split");
Chris Lattnercd3245a2006-12-19 22:41:21 +000059
Devang Patel19974732007-05-03 01:11:54 +000060char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000061static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000062
Chris Lattnerf7da2c72006-08-24 22:43:55 +000063void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000064 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000065 AU.addRequired<AliasAnalysis>();
66 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000067 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000068 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000069 AU.addPreservedID(MachineLoopInfoID);
70 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000071
72 if (!StrongPHIElim) {
73 AU.addPreservedID(PHIEliminationID);
74 AU.addRequiredID(PHIEliminationID);
75 }
76
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 AU.addRequiredID(TwoAddressInstructionPassID);
Lang Hames233a60e2009-11-03 23:52:08 +000078 AU.addPreserved<ProcessImplicitDefs>();
79 AU.addRequired<ProcessImplicitDefs>();
80 AU.addPreserved<SlotIndexes>();
81 AU.addRequiredTransitive<SlotIndexes>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000082 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000083}
84
Chris Lattnerf7da2c72006-08-24 22:43:55 +000085void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000086 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000087 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000088 E = r2iMap_.end(); I != E; ++I)
89 delete I->second;
90
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000091 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000092
Evan Chengdd199d22007-09-06 01:07:24 +000093 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
94 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +000095 while (!CloneMIs.empty()) {
96 MachineInstr *MI = CloneMIs.back();
97 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +000098 mf_->DeleteMachineInstr(MI);
99 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000100}
101
Owen Anderson80b3ce62008-05-28 20:54:50 +0000102/// runOnMachineFunction - Register allocate the whole function
103///
104bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
105 mf_ = &fn;
106 mri_ = &mf_->getRegInfo();
107 tm_ = &fn.getTarget();
108 tri_ = tm_->getRegisterInfo();
109 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000110 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000111 lv_ = &getAnalysis<LiveVariables>();
Lang Hames233a60e2009-11-03 23:52:08 +0000112 indexes_ = &getAnalysis<SlotIndexes>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000113 allocatableRegs_ = tri_->getAllocatableSet(fn);
114
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000116
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000117 numIntervals += getNumIntervals();
118
Chris Lattner70ca3582004-09-30 15:59:17 +0000119 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000121}
122
Chris Lattner70ca3582004-09-30 15:59:17 +0000123/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000124void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000125 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000126 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000127 I->second->print(OS, tri_);
128 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000129 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000130
Evan Cheng752195e2009-09-14 21:33:42 +0000131 printInstrs(OS);
132}
133
134void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000135 OS << "********** MACHINEINSTRS **********\n";
136
Chris Lattner3380d5c2009-07-21 21:12:58 +0000137 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
138 mbbi != mbbe; ++mbbi) {
Jakob Stoklund Olesen6cd81032009-11-20 18:54:59 +0000139 OS << "BB#" << mbbi->getNumber()
140 << ":\t\t# derived from " << mbbi->getName() << "\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000141 for (MachineBasicBlock::iterator mii = mbbi->begin(),
142 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000143 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000144 }
145 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000146}
147
Evan Cheng752195e2009-09-14 21:33:42 +0000148void LiveIntervals::dumpInstrs() const {
149 printInstrs(errs());
150}
151
Evan Chengc92da382007-11-03 07:20:12 +0000152/// conflictsWithPhysRegDef - Returns true if the specified register
153/// is defined during the duration of the specified interval.
154bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
155 VirtRegMap &vrm, unsigned reg) {
156 for (LiveInterval::Ranges::const_iterator
157 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000158 for (SlotIndex index = I->start.getBaseIndex(),
159 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
160 index != end;
161 index = index.getNextIndex()) {
Evan Chengc92da382007-11-03 07:20:12 +0000162 // skip deleted instructions
163 while (index != end && !getInstructionFromIndex(index))
Lang Hames233a60e2009-11-03 23:52:08 +0000164 index = index.getNextIndex();
Evan Chengc92da382007-11-03 07:20:12 +0000165 if (index == end) break;
166
167 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000168 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
169 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000170 if (SrcReg == li.reg || DstReg == li.reg)
171 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000172 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
173 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000174 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000175 continue;
176 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000177 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000178 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000179 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000180 if (!vrm.hasPhys(PhysReg))
181 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000182 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000183 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000184 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000185 return true;
186 }
187 }
188 }
189
190 return false;
191}
192
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000193/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
194/// it can check use as well.
195bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
196 unsigned Reg, bool CheckUse,
197 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
198 for (LiveInterval::Ranges::const_iterator
199 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000200 for (SlotIndex index = I->start.getBaseIndex(),
201 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
202 index != end;
203 index = index.getNextIndex()) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000204 // Skip deleted instructions.
205 MachineInstr *MI = 0;
206 while (index != end) {
207 MI = getInstructionFromIndex(index);
208 if (MI)
209 break;
Lang Hames233a60e2009-11-03 23:52:08 +0000210 index = index.getNextIndex();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000211 }
212 if (index == end) break;
213
214 if (JoinedCopies.count(MI))
215 continue;
216 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
217 MachineOperand& MO = MI->getOperand(i);
218 if (!MO.isReg())
219 continue;
220 if (MO.isUse() && !CheckUse)
221 continue;
222 unsigned PhysReg = MO.getReg();
223 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
224 continue;
225 if (tri_->isSubRegister(Reg, PhysReg))
226 return true;
227 }
228 }
229 }
230
231 return false;
232}
233
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000234#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000235static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000236 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000237 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000238 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000239 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000240}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000241#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000242
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000243void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000244 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000245 SlotIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000246 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000247 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000248 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000249 DEBUG({
250 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000251 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000252 });
Evan Cheng419852c2008-04-03 16:39:43 +0000253
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000254 // Virtual registers may be defined multiple times (due to phi
255 // elimination and 2-addr elimination). Much of what we do only has to be
256 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000257 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000258 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259 if (interval.empty()) {
260 // Get the Idx of the defining instructions.
Lang Hames233a60e2009-11-03 23:52:08 +0000261 SlotIndex defIndex = MIIdx.getDefIndex();
Dale Johannesen39faac22009-09-20 00:36:41 +0000262 // Earlyclobbers move back one, so that they overlap the live range
263 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000264 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000265 defIndex = MIIdx.getUseIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000266 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000267 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000268 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000269 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000270 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000271 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000272 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000273 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000274 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000275 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000276
277 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000278
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000279 // Loop over all of the blocks that the vreg is defined in. There are
280 // two cases we have to handle here. The most common case is a vreg
281 // whose lifetime is contained within a basic block. In this case there
282 // will be a single kill, in MBB, which comes after the definition.
283 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
284 // FIXME: what about dead vars?
Lang Hames233a60e2009-11-03 23:52:08 +0000285 SlotIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 if (vi.Kills[0] != mi)
Lang Hames233a60e2009-11-03 23:52:08 +0000287 killIdx = getInstructionIndex(vi.Kills[0]).getDefIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000288 else
Lang Hames233a60e2009-11-03 23:52:08 +0000289 killIdx = defIndex.getStoreIndex();
Chris Lattner6097d132004-07-19 02:15:56 +0000290
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 // If the kill happens after the definition, we have an intra-block
292 // live range.
293 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000294 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000296 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000297 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000298 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000299 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000300 return;
301 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000302 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000303
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000304 // The other case we handle is when a virtual register lives to the end
305 // of the defining block, potentially live across some blocks, then is
306 // live into some number of blocks, but gets killed. Start by adding a
307 // range that goes from this definition to the end of the defining block.
Lang Hames233a60e2009-11-03 23:52:08 +0000308 LiveRange NewLR(defIndex, getMBBEndIdx(mbb).getNextIndex().getLoadIndex(),
309 ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000310 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 interval.addRange(NewLR);
312
313 // Iterate over all of the blocks that the variable is completely
314 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
315 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000316 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
317 E = vi.AliveBlocks.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000318 LiveRange LR(
319 getMBBStartIdx(mf_->getBlockNumbered(*I)),
320 getMBBEndIdx(mf_->getBlockNumbered(*I)).getNextIndex().getLoadIndex(),
321 ValNo);
Dan Gohman4a829ec2008-11-13 16:31:27 +0000322 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000323 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000324 }
325
326 // Finally, this virtual register is live from the start of any killing
327 // block to the 'use' slot of the killing instruction.
328 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
329 MachineInstr *Kill = vi.Kills[i];
Lang Hames233a60e2009-11-03 23:52:08 +0000330 SlotIndex killIdx =
331 getInstructionIndex(Kill).getDefIndex();
Evan Chengb0f59732009-09-21 04:32:32 +0000332 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000333 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000334 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000335 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000336 }
337
338 } else {
339 // If this is the second time we see a virtual register definition, it
340 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000341 // the result of two address elimination, then the vreg is one of the
342 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000343 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 // If this is a two-address definition, then we have already processed
345 // the live range. The only problem is that we didn't realize there
346 // are actually two values in the live interval. Because of this we
347 // need to take the LiveRegion that defines this register and split it
348 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000349 assert(interval.containsOneValue());
Lang Hames233a60e2009-11-03 23:52:08 +0000350 SlotIndex DefIndex = interval.getValNumInfo(0)->def.getDefIndex();
351 SlotIndex RedefIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000352 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000353 RedefIndex = MIIdx.getUseIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354
Lang Hames35f291d2009-09-12 03:34:03 +0000355 const LiveRange *OldLR =
Lang Hames233a60e2009-11-03 23:52:08 +0000356 interval.getLiveRangeContaining(RedefIndex.getUseIndex());
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000357 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000358
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000359 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000360 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000362
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000363 // Two-address vregs should always only be redefined once. This means
364 // that at this point, there should be exactly one value number in it.
365 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
366
Chris Lattner91725b72006-08-31 05:54:43 +0000367 // The new value number (#1) is defined by the instruction we claimed
368 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000369 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000370 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000371 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000372 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
373
Chris Lattner91725b72006-08-31 05:54:43 +0000374 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000375 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000376 OldValNo->setCopy(0);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000377
378 // Add the new live interval which replaces the range for the input copy.
379 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000380 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000382 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000383
384 // If this redefinition is dead, we need to add a dummy unit live
385 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000386 if (MO.isDead())
Lang Hames233a60e2009-11-03 23:52:08 +0000387 interval.addRange(LiveRange(RedefIndex, RedefIndex.getStoreIndex(),
388 OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389
Bill Wendling8e6179f2009-08-22 20:18:03 +0000390 DEBUG({
391 errs() << " RESULT: ";
392 interval.print(errs(), tri_);
393 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000394 } else {
395 // Otherwise, this must be because of phi elimination. If this is the
396 // first redefinition of the vreg that we have seen, go back and change
397 // the live range in the PHI block to be a different value number.
398 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000399 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000400 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 MachineInstr *Killer = vi.Kills[0];
Lang Hames233a60e2009-11-03 23:52:08 +0000402 SlotIndex Start = getMBBStartIdx(Killer->getParent());
403 SlotIndex End = getInstructionIndex(Killer).getDefIndex();
Bill Wendling8e6179f2009-08-22 20:18:03 +0000404 DEBUG({
405 errs() << " Removing [" << Start << "," << End << "] from: ";
406 interval.print(errs(), tri_);
407 errs() << "\n";
408 });
Lang Hamesffd13262009-07-09 03:57:02 +0000409 interval.removeRange(Start, End);
410 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000411 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000412 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
Lang Hames233a60e2009-11-03 23:52:08 +0000413 VNI->addKill(indexes_->getTerminatorGap(killMBB));
Lang Hames857c4e02009-06-17 21:01:20 +0000414 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000415 DEBUG({
416 errs() << " RESULT: ";
417 interval.print(errs(), tri_);
418 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000419
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000420 // Replace the interval with one of a NEW value number. Note that this
421 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000422 LiveRange LR(Start, End,
Lang Hames233a60e2009-11-03 23:52:08 +0000423 interval.getNextValue(SlotIndex(getMBBStartIdx(mbb), true),
424 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000425 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000426 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000428 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000429 DEBUG({
430 errs() << " RESULT: ";
431 interval.print(errs(), tri_);
432 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000433 }
434
435 // In the case of PHI elimination, each variable definition is only
436 // live until the end of the block. We've already taken care of the
437 // rest of the live range.
Lang Hames233a60e2009-11-03 23:52:08 +0000438 SlotIndex defIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000439 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000440 defIndex = MIIdx.getUseIndex();
Evan Cheng752195e2009-09-14 21:33:42 +0000441
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000442 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000443 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000444 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000445 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000446 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000447 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000448 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000449 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000450 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000451
Lang Hames233a60e2009-11-03 23:52:08 +0000452 SlotIndex killIndex = getMBBEndIdx(mbb).getNextIndex().getLoadIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000453 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000454 interval.addRange(LR);
Lang Hames233a60e2009-11-03 23:52:08 +0000455 ValNo->addKill(indexes_->getTerminatorGap(mbb));
Lang Hames857c4e02009-06-17 21:01:20 +0000456 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000457 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000458 }
459 }
460
Bill Wendling8e6179f2009-08-22 20:18:03 +0000461 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000462}
463
Chris Lattnerf35fef72004-07-23 21:24:19 +0000464void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000465 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000466 SlotIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000467 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000468 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000469 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000470 // A physical register cannot be live across basic block, so its
471 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000472 DEBUG({
473 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000474 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000475 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000476
Lang Hames233a60e2009-11-03 23:52:08 +0000477 SlotIndex baseIndex = MIIdx;
478 SlotIndex start = baseIndex.getDefIndex();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000479 // Earlyclobbers move back one.
480 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000481 start = MIIdx.getUseIndex();
482 SlotIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000483
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000484 // If it is not used after definition, it is considered dead at
485 // the instruction defining it. Hence its interval is:
486 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000487 // For earlyclobbers, the defSlot was pushed back one; the extra
488 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000489 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000490 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000491 end = start.getStoreIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000492 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000493 }
494
495 // If it is not dead on definition, it must be killed by a
496 // subsequent instruction. Hence its interval is:
497 // [defSlot(def), useSlot(kill)+1)
Lang Hames233a60e2009-11-03 23:52:08 +0000498 baseIndex = baseIndex.getNextIndex();
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000499 while (++mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000500
501 if (getInstructionFromIndex(baseIndex) == 0)
502 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
503
Evan Cheng6130f662008-03-05 00:59:57 +0000504 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000505 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000506 end = baseIndex.getDefIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000507 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000508 } else {
509 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
510 if (DefIdx != -1) {
511 if (mi->isRegTiedToUseOperand(DefIdx)) {
512 // Two-address instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000513 end = baseIndex.getDefIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000514 } else {
515 // Another instruction redefines the register before it is ever read.
516 // Then the register is essentially dead at the instruction that defines
517 // it. Hence its interval is:
518 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000519 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000520 end = start.getStoreIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000521 }
522 goto exit;
523 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000524 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000525
Lang Hames233a60e2009-11-03 23:52:08 +0000526 baseIndex = baseIndex.getNextIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000527 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000528
529 // The only case we should have a dead physreg here without a killing or
530 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000531 // and never used. Another possible case is the implicit use of the
532 // physical register has been deleted by two-address pass.
Lang Hames233a60e2009-11-03 23:52:08 +0000533 end = start.getStoreIndex();
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000534
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000535exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000537
Evan Cheng24a3cc42007-04-25 07:30:23 +0000538 // Already exists? Extend old live interval.
539 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000540 bool Extend = OldLR != interval.end();
541 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000542 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000543 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000544 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000547 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000548 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000549}
550
Chris Lattnerf35fef72004-07-23 21:24:19 +0000551void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
552 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000553 SlotIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000554 MachineOperand& MO,
555 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000556 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000557 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000558 getOrCreateInterval(MO.getReg()));
559 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000560 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000561 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000562 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000563 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000564 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000565 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000566 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000567 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000568 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000569 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000570 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000571 // If MI also modifies the sub-register explicitly, avoid processing it
572 // more than once. Do not pass in TRI here so it checks for exact match.
573 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000574 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000575 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000576 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000577}
578
Evan Chengb371f452007-02-19 21:49:54 +0000579void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000580 SlotIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000581 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000582 DEBUG({
583 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000584 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000585 });
Evan Chengb371f452007-02-19 21:49:54 +0000586
587 // Look for kills, if it reaches a def before it's killed, then it shouldn't
588 // be considered a livein.
589 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000590 SlotIndex baseIndex = MIIdx;
591 SlotIndex start = baseIndex;
592 if (getInstructionFromIndex(baseIndex) == 0)
593 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
594
595 SlotIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000596 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000597
Evan Chengb371f452007-02-19 21:49:54 +0000598 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000599 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000600 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000601 end = baseIndex.getDefIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000602 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000603 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000604 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000605 // Another instruction redefines the register before it is ever read.
606 // Then the register is essentially dead at the instruction that defines
607 // it. Hence its interval is:
608 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000609 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000610 end = start.getStoreIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000611 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000612 break;
Evan Chengb371f452007-02-19 21:49:54 +0000613 }
614
Evan Chengb371f452007-02-19 21:49:54 +0000615 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000616 if (mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000617 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +0000618 }
Evan Chengb371f452007-02-19 21:49:54 +0000619 }
620
Evan Cheng75611fb2007-06-27 01:16:36 +0000621 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000622 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000623 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000624 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000625 end = MIIdx.getStoreIndex();
Evan Cheng292da942007-06-27 18:47:28 +0000626 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000627 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +0000628 end = baseIndex;
629 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000630 }
631
Lang Hames10382fb2009-06-19 02:17:53 +0000632 VNInfo *vni =
Lang Hames233a60e2009-11-03 23:52:08 +0000633 interval.getNextValue(SlotIndex(getMBBStartIdx(MBB), true),
Lang Hames86511252009-09-04 20:41:11 +0000634 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000635 vni->setIsPHIDef(true);
636 LiveRange LR(start, end, vni);
Jakob Stoklund Olesen3de23e62009-11-07 01:58:40 +0000637
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000638 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000639 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000640 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +0000641}
642
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000643/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000644/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000645/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000646/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000647void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000648 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +0000649 << "********** Function: "
650 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +0000651
652 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000653 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
654 MBBI != E; ++MBBI) {
655 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000656 // Track the index of the current machine instr.
Lang Hames233a60e2009-11-03 23:52:08 +0000657 SlotIndex MIIndex = getMBBStartIdx(MBB);
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000658 DEBUG(errs() << MBB->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000659
Chris Lattner428b92e2006-09-15 03:57:23 +0000660 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000661
Dan Gohmancb406c22007-10-03 19:26:29 +0000662 // Create intervals for live-ins to this BB first.
663 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
664 LE = MBB->livein_end(); LI != LE; ++LI) {
665 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
666 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000667 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000668 if (!hasInterval(*AS))
669 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
670 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000671 }
672
Owen Anderson99500ae2008-09-15 22:00:38 +0000673 // Skip over empty initial indices.
Lang Hames233a60e2009-11-03 23:52:08 +0000674 if (getInstructionFromIndex(MIIndex) == 0)
675 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +0000676
Chris Lattner428b92e2006-09-15 03:57:23 +0000677 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000678 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000679
Evan Cheng438f7bc2006-11-10 08:43:01 +0000680 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000681 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
682 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +0000683 if (!MO.isReg() || !MO.getReg())
684 continue;
685
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000686 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +0000687 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000688 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +0000689 else if (MO.isUndef())
690 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000691 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000692
Lang Hames233a60e2009-11-03 23:52:08 +0000693 // Move to the next instr slot.
694 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000695 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000696 }
Evan Chengd129d732009-07-17 19:43:40 +0000697
698 // Create empty intervals for registers defined by implicit_def's (except
699 // for those implicit_def that define values which are liveout of their
700 // blocks.
701 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
702 unsigned UndefReg = UndefUses[i];
703 (void)getOrCreateInterval(UndefReg);
704 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000705}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000706
Owen Anderson03857b22008-08-13 21:49:13 +0000707LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000708 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +0000709 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000710}
Evan Chengf2fbca62007-11-12 06:35:08 +0000711
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000712/// dupInterval - Duplicate a live interval. The caller is responsible for
713/// managing the allocated memory.
714LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
715 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000716 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000717 return NewLI;
718}
719
Evan Chengc8d044e2008-02-15 18:24:29 +0000720/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
721/// copy field and returns the source register that defines it.
722unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +0000723 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +0000724 return 0;
725
Lang Hames52c1afc2009-08-10 23:43:28 +0000726 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000727 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +0000728 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000729 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +0000730 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000731 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000732 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
733 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
734 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000735
Evan Cheng04ee5a12009-01-20 19:12:24 +0000736 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000737 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000738 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +0000739 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +0000740 return 0;
741}
Evan Chengf2fbca62007-11-12 06:35:08 +0000742
743//===----------------------------------------------------------------------===//
744// Register allocator hooks.
745//
746
Evan Chengd70dbb52008-02-22 09:24:50 +0000747/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
748/// allow one) virtual register operand, then its uses are implicitly using
749/// the register. Returns the virtual register.
750unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
751 MachineInstr *MI) const {
752 unsigned RegOp = 0;
753 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
754 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000755 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +0000756 continue;
757 unsigned Reg = MO.getReg();
758 if (Reg == 0 || Reg == li.reg)
759 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +0000760
761 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
762 !allocatableRegs_[Reg])
763 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +0000764 // FIXME: For now, only remat MI with at most one register operand.
765 assert(!RegOp &&
766 "Can't rematerialize instruction with multiple register operand!");
767 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000768#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +0000769 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000770#endif
Evan Chengd70dbb52008-02-22 09:24:50 +0000771 }
772 return RegOp;
773}
774
775/// isValNoAvailableAt - Return true if the val# of the specified interval
776/// which reaches the given instruction also reaches the specified use index.
777bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000778 SlotIndex UseIdx) const {
779 SlotIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +0000780 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
781 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
782 return UI != li.end() && UI->valno == ValNo;
783}
784
Evan Chengf2fbca62007-11-12 06:35:08 +0000785/// isReMaterializable - Returns true if the definition MI of the specified
786/// val# of the specified interval is re-materializable.
787bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000788 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +0000789 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000790 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000791 if (DisableReMat)
792 return false;
793
Dan Gohmana70dca12009-10-09 23:27:56 +0000794 if (!tii_->isTriviallyReMaterializable(MI, aa_))
795 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +0000796
Dan Gohmana70dca12009-10-09 23:27:56 +0000797 // Target-specific code can mark an instruction as being rematerializable
798 // if it has one virtual reg use, though it had better be something like
799 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +0000800 unsigned ImpUse = getReMatImplicitUse(li, MI);
801 if (ImpUse) {
802 const LiveInterval &ImpLi = getInterval(ImpUse);
803 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
804 re = mri_->use_end(); ri != re; ++ri) {
805 MachineInstr *UseMI = &*ri;
Lang Hames233a60e2009-11-03 23:52:08 +0000806 SlotIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +0000807 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
808 continue;
809 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
810 return false;
811 }
Evan Chengdc377862008-09-30 15:44:16 +0000812
813 // If a register operand of the re-materialized instruction is going to
814 // be spilled next, then it's not legal to re-materialize this instruction.
815 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
816 if (ImpUse == SpillIs[i]->reg)
817 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000818 }
819 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000820}
821
Evan Cheng06587492008-10-24 02:05:00 +0000822/// isReMaterializable - Returns true if the definition MI of the specified
823/// val# of the specified interval is re-materializable.
824bool LiveIntervals::isReMaterializable(const LiveInterval &li,
825 const VNInfo *ValNo, MachineInstr *MI) {
826 SmallVector<LiveInterval*, 4> Dummy1;
827 bool Dummy2;
828 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
829}
830
Evan Cheng5ef3a042007-12-06 00:01:56 +0000831/// isReMaterializable - Returns true if every definition of MI of every
832/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +0000833bool LiveIntervals::isReMaterializable(const LiveInterval &li,
834 SmallVectorImpl<LiveInterval*> &SpillIs,
835 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +0000836 isLoad = false;
837 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
838 i != e; ++i) {
839 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +0000840 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000841 continue; // Dead val#.
842 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +0000843 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000844 return false;
Lang Hames857c4e02009-06-17 21:01:20 +0000845 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000846 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000847 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +0000848 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000849 return false;
850 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000851 }
852 return true;
853}
854
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000855/// FilterFoldedOps - Filter out two-address use operands. Return
856/// true if it finds any issue with the operands that ought to prevent
857/// folding.
858static bool FilterFoldedOps(MachineInstr *MI,
859 SmallVector<unsigned, 2> &Ops,
860 unsigned &MRInfo,
861 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000862 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000863 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
864 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000865 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000866 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000867 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000868 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000869 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000870 MRInfo |= (unsigned)VirtRegMap::isMod;
871 else {
872 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +0000873 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +0000874 MRInfo = VirtRegMap::isModRef;
875 continue;
876 }
877 MRInfo |= (unsigned)VirtRegMap::isRef;
878 }
879 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000880 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000881 return false;
882}
883
884
885/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
886/// slot / to reg or any rematerialized load into ith operand of specified
887/// MI. If it is successul, MI is updated with the newly created MI and
888/// returns true.
889bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
890 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames233a60e2009-11-03 23:52:08 +0000891 SlotIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000892 SmallVector<unsigned, 2> &Ops,
893 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000894 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000895 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000896 RemoveMachineInstrFromMaps(MI);
897 vrm.RemoveMachineInstrFromMaps(MI);
898 MI->eraseFromParent();
899 ++numFolds;
900 return true;
901 }
902
903 // Filter the list of operand indexes that are to be folded. Abort if
904 // any operand will prevent folding.
905 unsigned MRInfo = 0;
906 SmallVector<unsigned, 2> FoldOps;
907 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
908 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000909
Evan Cheng427f4c12008-03-31 23:19:51 +0000910 // The only time it's safe to fold into a two address instruction is when
911 // it's folding reload and spill from / into a spill stack slot.
912 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000913 return false;
914
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000915 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
916 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000917 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000918 // Remember this instruction uses the spill slot.
919 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
920
Evan Chengf2fbca62007-11-12 06:35:08 +0000921 // Attempt to fold the memory reference into the instruction. If
922 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +0000923 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000924 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000925 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000926 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000927 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000928 vrm.transferEmergencySpills(MI, fmi);
Lang Hames233a60e2009-11-03 23:52:08 +0000929 ReplaceMachineInstrInMaps(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000930 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000931 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000932 return true;
933 }
934 return false;
935}
936
Evan Cheng018f9b02007-12-05 03:22:34 +0000937/// canFoldMemoryOperand - Returns true if the specified load / store
938/// folding is possible.
939bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000940 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000941 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000942 // Filter the list of operand indexes that are to be folded. Abort if
943 // any operand will prevent folding.
944 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000945 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000946 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
947 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000948
Evan Cheng3c75ba82008-04-01 21:37:32 +0000949 // It's only legal to remat for a use, not a def.
950 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000951 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000952
Evan Chengd70dbb52008-02-22 09:24:50 +0000953 return tii_->canFoldMemoryOperand(MI, FoldOps);
954}
955
Evan Cheng81a03822007-11-17 00:40:40 +0000956bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
Lang Hames233a60e2009-11-03 23:52:08 +0000957 LiveInterval::Ranges::const_iterator itr = li.ranges.begin();
958
959 MachineBasicBlock *mbb = indexes_->getMBBCoveringRange(itr->start, itr->end);
960
961 if (mbb == 0)
962 return false;
963
964 for (++itr; itr != li.ranges.end(); ++itr) {
965 MachineBasicBlock *mbb2 =
966 indexes_->getMBBCoveringRange(itr->start, itr->end);
967
968 if (mbb2 != mbb)
Evan Cheng81a03822007-11-17 00:40:40 +0000969 return false;
970 }
Lang Hames233a60e2009-11-03 23:52:08 +0000971
Evan Cheng81a03822007-11-17 00:40:40 +0000972 return true;
973}
974
Evan Chengd70dbb52008-02-22 09:24:50 +0000975/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
976/// interval on to-be re-materialized operands of MI) with new register.
977void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
978 MachineInstr *MI, unsigned NewVReg,
979 VirtRegMap &vrm) {
980 // There is an implicit use. That means one of the other operand is
981 // being remat'ed and the remat'ed instruction has li.reg as an
982 // use operand. Make sure we rewrite that as well.
983 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
984 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000985 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +0000986 continue;
987 unsigned Reg = MO.getReg();
988 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
989 continue;
990 if (!vrm.isReMaterialized(Reg))
991 continue;
992 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000993 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
994 if (UseMO)
995 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +0000996 }
997}
998
Evan Chengf2fbca62007-11-12 06:35:08 +0000999/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1000/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001001bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001002rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames233a60e2009-11-03 23:52:08 +00001003 bool TrySplit, SlotIndex index, SlotIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001004 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001005 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001006 unsigned Slot, int LdSlot,
1007 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001008 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001009 const TargetRegisterClass* rc,
1010 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001011 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001012 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001013 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001014 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001015 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001016 RestartInstruction:
1017 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1018 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001019 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001020 continue;
1021 unsigned Reg = mop.getReg();
1022 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001023 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001024 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001025 if (Reg != li.reg)
1026 continue;
1027
1028 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001029 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001030 int FoldSlot = Slot;
1031 if (DefIsReMat) {
1032 // If this is the rematerializable definition MI itself and
1033 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001034 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001035 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1036 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001037 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001038 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001039 MI->eraseFromParent();
1040 break;
1041 }
1042
1043 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001044 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001045 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001046 if (isLoad) {
1047 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1048 FoldSS = isLoadSS;
1049 FoldSlot = LdSlot;
1050 }
1051 }
1052
Evan Chengf2fbca62007-11-12 06:35:08 +00001053 // Scan all of the operands of this instruction rewriting operands
1054 // to use NewVReg instead of li.reg as appropriate. We do this for
1055 // two reasons:
1056 //
1057 // 1. If the instr reads the same spilled vreg multiple times, we
1058 // want to reuse the NewVReg.
1059 // 2. If the instr is a two-addr instruction, we are required to
1060 // keep the src/dst regs pinned.
1061 //
1062 // Keep track of whether we replace a use and/or def so that we can
1063 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001064
Evan Cheng81a03822007-11-17 00:40:40 +00001065 HasUse = mop.isUse();
1066 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001067 SmallVector<unsigned, 2> Ops;
1068 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001069 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001070 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001071 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001072 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001073 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001074 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001075 continue;
1076 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001077 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001078 if (!MOj.isUndef()) {
1079 HasUse |= MOj.isUse();
1080 HasDef |= MOj.isDef();
1081 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001082 }
1083 }
1084
David Greene26b86a02008-10-27 17:38:59 +00001085 // Create a new virtual register for the spill interval.
1086 // Create the new register now so we can map the fold instruction
1087 // to the new register so when it is unfolded we get the correct
1088 // answer.
1089 bool CreatedNewVReg = false;
1090 if (NewVReg == 0) {
1091 NewVReg = mri_->createVirtualRegister(rc);
1092 vrm.grow();
1093 CreatedNewVReg = true;
Jakob Stoklund Olesence7a6632009-11-30 22:55:54 +00001094
1095 // The new virtual register should get the same allocation hints as the
1096 // old one.
1097 std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(Reg);
1098 if (Hint.first || Hint.second)
1099 mri_->setRegAllocationHint(NewVReg, Hint.first, Hint.second);
David Greene26b86a02008-10-27 17:38:59 +00001100 }
1101
Evan Cheng9c3c2212008-06-06 07:54:39 +00001102 if (!TryFold)
1103 CanFold = false;
1104 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001105 // Do not fold load / store here if we are splitting. We'll find an
1106 // optimal point to insert a load / store later.
1107 if (!TrySplit) {
1108 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001109 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001110 // Folding the load/store can completely change the instruction in
1111 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001112
1113 if (FoldSS) {
1114 // We need to give the new vreg the same stack slot as the
1115 // spilled interval.
1116 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1117 }
1118
Evan Cheng018f9b02007-12-05 03:22:34 +00001119 HasUse = false;
1120 HasDef = false;
1121 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001122 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001123 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001124 goto RestartInstruction;
1125 }
1126 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001127 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001128 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001129 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001130 }
Evan Chengcddbb832007-11-30 21:23:43 +00001131
Evan Chengcddbb832007-11-30 21:23:43 +00001132 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001133 if (mop.isImplicit())
1134 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001135
1136 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001137 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1138 MachineOperand &mopj = MI->getOperand(Ops[j]);
1139 mopj.setReg(NewVReg);
1140 if (mopj.isImplicit())
1141 rewriteImplicitOps(li, MI, NewVReg, vrm);
1142 }
Evan Chengcddbb832007-11-30 21:23:43 +00001143
Evan Cheng81a03822007-11-17 00:40:40 +00001144 if (CreatedNewVReg) {
1145 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001146 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001147 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001148 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001149 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001150 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001151 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001152 }
1153 if (!CanDelete || (HasUse && HasDef)) {
1154 // If this is a two-addr instruction then its use operands are
1155 // rematerializable but its def is not. It should be assigned a
1156 // stack slot.
1157 vrm.assignVirt2StackSlot(NewVReg, Slot);
1158 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001159 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001160 vrm.assignVirt2StackSlot(NewVReg, Slot);
1161 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001162 } else if (HasUse && HasDef &&
1163 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1164 // If this interval hasn't been assigned a stack slot (because earlier
1165 // def is a deleted remat def), do it now.
1166 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1167 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001168 }
1169
Evan Cheng313d4b82008-02-23 00:33:04 +00001170 // Re-matting an instruction with virtual register use. Add the
1171 // register as an implicit use on the use MI.
1172 if (DefIsReMat && ImpUse)
1173 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1174
Evan Cheng5b69eba2009-04-21 22:46:52 +00001175 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001176 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001177 if (CreatedNewVReg) {
1178 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001179 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001180 if (TrySplit)
1181 vrm.setIsSplitFromReg(NewVReg, li.reg);
1182 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001183
1184 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001185 if (CreatedNewVReg) {
Lang Hames233a60e2009-11-03 23:52:08 +00001186 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1187 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001188 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001189 nI.addRange(LR);
1190 } else {
1191 // Extend the split live interval to this def / use.
Lang Hames233a60e2009-11-03 23:52:08 +00001192 SlotIndex End = index.getDefIndex();
Evan Cheng81a03822007-11-17 00:40:40 +00001193 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1194 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001195 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001196 nI.addRange(LR);
1197 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001198 }
1199 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001200 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1201 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001202 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001203 nI.addRange(LR);
1204 }
Evan Cheng81a03822007-11-17 00:40:40 +00001205
Bill Wendling8e6179f2009-08-22 20:18:03 +00001206 DEBUG({
1207 errs() << "\t\t\t\tAdded new interval: ";
1208 nI.print(errs(), tri_);
1209 errs() << '\n';
1210 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001211 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001212 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001213}
Evan Cheng81a03822007-11-17 00:40:40 +00001214bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001215 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001216 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +00001217 SlotIndex Idx) const {
1218 SlotIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001219 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames233a60e2009-11-03 23:52:08 +00001220 if (VNI->kills[j].isPHI())
Lang Hamesffd13262009-07-09 03:57:02 +00001221 continue;
1222
Lang Hames233a60e2009-11-03 23:52:08 +00001223 SlotIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001224 if (KillIdx > Idx && KillIdx < End)
1225 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001226 }
1227 return false;
1228}
1229
Evan Cheng063284c2008-02-21 00:34:19 +00001230/// RewriteInfo - Keep track of machine instrs that will be rewritten
1231/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001232namespace {
1233 struct RewriteInfo {
Lang Hames233a60e2009-11-03 23:52:08 +00001234 SlotIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001235 MachineInstr *MI;
1236 bool HasUse;
1237 bool HasDef;
Lang Hames233a60e2009-11-03 23:52:08 +00001238 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001239 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1240 };
Evan Cheng063284c2008-02-21 00:34:19 +00001241
Dan Gohman844731a2008-05-13 00:00:25 +00001242 struct RewriteInfoCompare {
1243 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1244 return LHS.Index < RHS.Index;
1245 }
1246 };
1247}
Evan Cheng063284c2008-02-21 00:34:19 +00001248
Evan Chengf2fbca62007-11-12 06:35:08 +00001249void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001250rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001251 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001252 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001253 unsigned Slot, int LdSlot,
1254 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001255 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001256 const TargetRegisterClass* rc,
1257 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001258 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001259 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001260 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001261 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001262 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1263 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001264 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001265 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001266 unsigned NewVReg = 0;
Lang Hames233a60e2009-11-03 23:52:08 +00001267 SlotIndex start = I->start.getBaseIndex();
1268 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001269
Evan Cheng063284c2008-02-21 00:34:19 +00001270 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001271 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001272 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001273 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1274 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001275 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001276 MachineOperand &O = ri.getOperand();
1277 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001278 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames233a60e2009-11-03 23:52:08 +00001279 SlotIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001280 if (index < start || index >= end)
1281 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001282
1283 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001284 // Must be defined by an implicit def. It should not be spilled. Note,
1285 // this is for correctness reason. e.g.
1286 // 8 %reg1024<def> = IMPLICIT_DEF
1287 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1288 // The live range [12, 14) are not part of the r1024 live interval since
1289 // it's defined by an implicit def. It will not conflicts with live
1290 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001291 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001292 // the INSERT_SUBREG and both target registers that would overlap.
1293 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001294 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1295 }
1296 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1297
Evan Cheng313d4b82008-02-23 00:33:04 +00001298 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001299 // Now rewrite the defs and uses.
1300 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1301 RewriteInfo &rwi = RewriteMIs[i];
1302 ++i;
Lang Hames233a60e2009-11-03 23:52:08 +00001303 SlotIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001304 bool MIHasUse = rwi.HasUse;
1305 bool MIHasDef = rwi.HasDef;
1306 MachineInstr *MI = rwi.MI;
1307 // If MI def and/or use the same register multiple times, then there
1308 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001309 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001310 while (i != e && RewriteMIs[i].MI == MI) {
1311 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001312 bool isUse = RewriteMIs[i].HasUse;
1313 if (isUse) ++NumUses;
1314 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001315 MIHasDef |= RewriteMIs[i].HasDef;
1316 ++i;
1317 }
Evan Cheng81a03822007-11-17 00:40:40 +00001318 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001319
Evan Cheng0a891ed2008-05-23 23:00:04 +00001320 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001321 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001322 // register interval's spill weight to HUGE_VALF to prevent it from
1323 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001324 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001325 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001326 }
1327
Evan Cheng063284c2008-02-21 00:34:19 +00001328 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001329 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001330 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001331 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001332 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001333 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001334 // One common case:
1335 // x = use
1336 // ...
1337 // ...
1338 // def = ...
1339 // = use
1340 // It's better to start a new interval to avoid artifically
1341 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001342 if (MIHasDef && !MIHasUse) {
1343 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001344 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001345 }
1346 }
Evan Chengcada2452007-11-28 01:28:46 +00001347 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001348
1349 bool IsNew = ThisVReg == 0;
1350 if (IsNew) {
1351 // This ends the previous live interval. If all of its def / use
1352 // can be folded, give it a low spill weight.
1353 if (NewVReg && TrySplit && AllCanFold) {
1354 LiveInterval &nI = getOrCreateInterval(NewVReg);
1355 nI.weight /= 10.0F;
1356 }
1357 AllCanFold = true;
1358 }
1359 NewVReg = ThisVReg;
1360
Evan Cheng81a03822007-11-17 00:40:40 +00001361 bool HasDef = false;
1362 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001363 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001364 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1365 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1366 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001367 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001368 if (!HasDef && !HasUse)
1369 continue;
1370
Evan Cheng018f9b02007-12-05 03:22:34 +00001371 AllCanFold &= CanFold;
1372
Evan Cheng81a03822007-11-17 00:40:40 +00001373 // Update weight of spill interval.
1374 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001375 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001376 // The spill weight is now infinity as it cannot be spilled again.
1377 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001378 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001379 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001380
1381 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001382 if (HasDef) {
1383 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001384 bool HasKill = false;
1385 if (!HasUse)
Lang Hames233a60e2009-11-03 23:52:08 +00001386 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001387 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001388 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames233a60e2009-11-03 23:52:08 +00001389 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001390 if (VNI)
Lang Hames233a60e2009-11-03 23:52:08 +00001391 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001392 }
Owen Anderson28998312008-08-13 22:28:50 +00001393 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001394 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001395 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001396 if (SII == SpillIdxes.end()) {
1397 std::vector<SRInfo> S;
1398 S.push_back(SRInfo(index, NewVReg, true));
1399 SpillIdxes.insert(std::make_pair(MBBId, S));
1400 } else if (SII->second.back().vreg != NewVReg) {
1401 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001402 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001403 // If there is an earlier def and this is a two-address
1404 // instruction, then it's not possible to fold the store (which
1405 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001406 SRInfo &Info = SII->second.back();
1407 Info.index = index;
1408 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001409 }
1410 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001411 } else if (SII != SpillIdxes.end() &&
1412 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001413 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001414 // There is an earlier def that's not killed (must be two-address).
1415 // The spill is no longer needed.
1416 SII->second.pop_back();
1417 if (SII->second.empty()) {
1418 SpillIdxes.erase(MBBId);
1419 SpillMBBs.reset(MBBId);
1420 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001421 }
1422 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001423 }
1424
1425 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001426 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001427 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001428 if (SII != SpillIdxes.end() &&
1429 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001430 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001431 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001432 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001433 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001434 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001435 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001436 // If we are splitting live intervals, only fold if it's the first
1437 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001438 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001439 else if (IsNew) {
1440 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001441 if (RII == RestoreIdxes.end()) {
1442 std::vector<SRInfo> Infos;
1443 Infos.push_back(SRInfo(index, NewVReg, true));
1444 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1445 } else {
1446 RII->second.push_back(SRInfo(index, NewVReg, true));
1447 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001448 RestoreMBBs.set(MBBId);
1449 }
1450 }
1451
1452 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001453 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001454 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001455 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001456
1457 if (NewVReg && TrySplit && AllCanFold) {
1458 // If all of its def / use can be folded, give it a low spill weight.
1459 LiveInterval &nI = getOrCreateInterval(NewVReg);
1460 nI.weight /= 10.0F;
1461 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001462}
1463
Lang Hames233a60e2009-11-03 23:52:08 +00001464bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001465 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001466 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001467 if (!RestoreMBBs[Id])
1468 return false;
1469 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1470 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1471 if (Restores[i].index == index &&
1472 Restores[i].vreg == vr &&
1473 Restores[i].canFold)
1474 return true;
1475 return false;
1476}
1477
Lang Hames233a60e2009-11-03 23:52:08 +00001478void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001479 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001480 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001481 if (!RestoreMBBs[Id])
1482 return;
1483 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1484 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1485 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames233a60e2009-11-03 23:52:08 +00001486 Restores[i].index = SlotIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00001487}
Evan Cheng81a03822007-11-17 00:40:40 +00001488
Evan Cheng4cce6b42008-04-11 17:53:36 +00001489/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1490/// spilled and create empty intervals for their uses.
1491void
1492LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1493 const TargetRegisterClass* rc,
1494 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001495 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1496 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001497 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001498 MachineInstr *MI = &*ri;
1499 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001500 if (O.isDef()) {
1501 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1502 "Register def was not rewritten?");
1503 RemoveMachineInstrFromMaps(MI);
1504 vrm.RemoveMachineInstrFromMaps(MI);
1505 MI->eraseFromParent();
1506 } else {
1507 // This must be an use of an implicit_def so it's not part of the live
1508 // interval. Create a new empty live interval for it.
1509 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1510 unsigned NewVReg = mri_->createVirtualRegister(rc);
1511 vrm.grow();
1512 vrm.setIsImplicitlyDefined(NewVReg);
1513 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1514 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1515 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001516 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001517 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001518 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001519 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001520 }
1521 }
Evan Cheng419852c2008-04-03 16:39:43 +00001522 }
1523}
1524
Evan Chengf2fbca62007-11-12 06:35:08 +00001525std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001526addIntervalsForSpillsFast(const LiveInterval &li,
1527 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001528 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001529 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001530
1531 std::vector<LiveInterval*> added;
1532
1533 assert(li.weight != HUGE_VALF &&
1534 "attempt to spill already spilled interval!");
1535
Bill Wendling8e6179f2009-08-22 20:18:03 +00001536 DEBUG({
1537 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1538 li.dump();
1539 errs() << '\n';
1540 });
Owen Andersond6664312008-08-18 18:05:32 +00001541
1542 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1543
Owen Andersona41e47a2008-08-19 22:12:11 +00001544 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1545 while (RI != mri_->reg_end()) {
1546 MachineInstr* MI = &*RI;
1547
1548 SmallVector<unsigned, 2> Indices;
1549 bool HasUse = false;
1550 bool HasDef = false;
1551
1552 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1553 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001554 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001555
1556 HasUse |= MI->getOperand(i).isUse();
1557 HasDef |= MI->getOperand(i).isDef();
1558
1559 Indices.push_back(i);
1560 }
1561
1562 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1563 Indices, true, slot, li.reg)) {
1564 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00001565 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00001566 vrm.assignVirt2StackSlot(NewVReg, slot);
1567
Owen Andersona41e47a2008-08-19 22:12:11 +00001568 // create a new register for this spill
1569 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00001570
Owen Andersona41e47a2008-08-19 22:12:11 +00001571 // the spill weight is now infinity as it
1572 // cannot be spilled again
1573 nI.weight = HUGE_VALF;
1574
1575 // Rewrite register operands to use the new vreg.
1576 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1577 E = Indices.end(); I != E; ++I) {
1578 MI->getOperand(*I).setReg(NewVReg);
1579
1580 if (MI->getOperand(*I).isUse())
1581 MI->getOperand(*I).setIsKill(true);
1582 }
1583
1584 // Fill in the new live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001585 SlotIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00001586 if (HasUse) {
Lang Hames233a60e2009-11-03 23:52:08 +00001587 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1588 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001589 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001590 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001591 nI.addRange(LR);
1592 vrm.addRestorePoint(NewVReg, MI);
1593 }
1594 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001595 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1596 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001597 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001598 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001599 nI.addRange(LR);
1600 vrm.addSpillPoint(NewVReg, true, MI);
1601 }
1602
Owen Anderson17197312008-08-18 23:41:04 +00001603 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00001604
Bill Wendling8e6179f2009-08-22 20:18:03 +00001605 DEBUG({
1606 errs() << "\t\t\t\tadded new interval: ";
1607 nI.dump();
1608 errs() << '\n';
1609 });
Owen Andersona41e47a2008-08-19 22:12:11 +00001610 }
Owen Anderson9a032932008-08-18 21:20:32 +00001611
Owen Anderson9a032932008-08-18 21:20:32 +00001612
Owen Andersona41e47a2008-08-19 22:12:11 +00001613 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001614 }
Owen Andersond6664312008-08-18 18:05:32 +00001615
1616 return added;
1617}
1618
1619std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001620addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00001621 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00001622 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00001623
1624 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00001625 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00001626
Evan Chengf2fbca62007-11-12 06:35:08 +00001627 assert(li.weight != HUGE_VALF &&
1628 "attempt to spill already spilled interval!");
1629
Bill Wendling8e6179f2009-08-22 20:18:03 +00001630 DEBUG({
1631 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1632 li.print(errs(), tri_);
1633 errs() << '\n';
1634 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001635
Evan Cheng72eeb942008-12-05 17:00:16 +00001636 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00001637 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001638 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001639 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001640 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1641 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001642 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001643 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001644
1645 unsigned NumValNums = li.getNumValNums();
1646 SmallVector<MachineInstr*, 4> ReMatDefs;
1647 ReMatDefs.resize(NumValNums, NULL);
1648 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1649 ReMatOrigDefs.resize(NumValNums, NULL);
1650 SmallVector<int, 4> ReMatIds;
1651 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1652 BitVector ReMatDelete(NumValNums);
1653 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1654
Evan Cheng81a03822007-11-17 00:40:40 +00001655 // Spilling a split live interval. It cannot be split any further. Also,
1656 // it's also guaranteed to be a single val# / range interval.
1657 if (vrm.getPreSplitReg(li.reg)) {
1658 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001659 // Unset the split kill marker on the last use.
Lang Hames233a60e2009-11-03 23:52:08 +00001660 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1661 if (KillIdx != SlotIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00001662 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1663 assert(KillMI && "Last use disappeared?");
1664 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1665 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001666 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001667 }
Evan Chengadf85902007-12-05 09:51:10 +00001668 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001669 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1670 Slot = vrm.getStackSlot(li.reg);
1671 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1672 MachineInstr *ReMatDefMI = DefIsReMat ?
1673 vrm.getReMaterializedMI(li.reg) : NULL;
1674 int LdSlot = 0;
1675 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1676 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001677 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001678 bool IsFirstRange = true;
1679 for (LiveInterval::Ranges::const_iterator
1680 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1681 // If this is a split live interval with multiple ranges, it means there
1682 // are two-address instructions that re-defined the value. Only the
1683 // first def can be rematerialized!
1684 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001685 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001686 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1687 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001688 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001689 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001690 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001691 } else {
1692 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1693 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001694 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001695 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001696 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001697 }
1698 IsFirstRange = false;
1699 }
Evan Cheng419852c2008-04-03 16:39:43 +00001700
Evan Cheng4cce6b42008-04-11 17:53:36 +00001701 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001702 return NewLIs;
1703 }
1704
Evan Cheng752195e2009-09-14 21:33:42 +00001705 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001706 if (TrySplit)
1707 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001708 bool NeedStackSlot = false;
1709 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1710 i != e; ++i) {
1711 const VNInfo *VNI = *i;
1712 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00001713 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00001714 continue; // Dead val#.
1715 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001716 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1717 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001718 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00001719 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001720 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001721 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001722 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001723 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00001724 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00001725 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001726
1727 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00001728 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00001729 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001730 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001731 CanDelete = false;
1732 // Need a stack slot if there is any live range where uses cannot be
1733 // rematerialized.
1734 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001735 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001736 if (CanDelete)
1737 ReMatDelete.set(VN);
1738 } else {
1739 // Need a stack slot if there is any live range where uses cannot be
1740 // rematerialized.
1741 NeedStackSlot = true;
1742 }
1743 }
1744
1745 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00001746 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1747 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1748 Slot = vrm.assignVirt2StackSlot(li.reg);
1749
1750 // This case only occurs when the prealloc splitter has already assigned
1751 // a stack slot to this vreg.
1752 else
1753 Slot = vrm.getStackSlot(li.reg);
1754 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001755
1756 // Create new intervals and rewrite defs and uses.
1757 for (LiveInterval::Ranges::const_iterator
1758 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001759 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1760 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1761 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001762 bool CanDelete = ReMatDelete[I->valno->id];
1763 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001764 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001765 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001766 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001767 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001768 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001769 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001770 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001771 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001772 }
1773
Evan Cheng0cbb1162007-11-29 01:06:25 +00001774 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001775 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001776 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001777 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001778 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001779
Evan Chengb50bb8c2007-12-05 08:16:32 +00001780 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001781 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001782 if (NeedStackSlot) {
1783 int Id = SpillMBBs.find_first();
1784 while (Id != -1) {
1785 std::vector<SRInfo> &spills = SpillIdxes[Id];
1786 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001787 SlotIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001788 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001789 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001790 bool isReMat = vrm.isReMaterialized(VReg);
1791 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001792 bool CanFold = false;
1793 bool FoundUse = false;
1794 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001795 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001796 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001797 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1798 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001799 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001800 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001801
1802 Ops.push_back(j);
1803 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001804 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001805 if (isReMat ||
1806 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1807 RestoreMBBs, RestoreIdxes))) {
1808 // MI has two-address uses of the same register. If the use
1809 // isn't the first and only use in the BB, then we can't fold
1810 // it. FIXME: Move this to rewriteInstructionsForSpills.
1811 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001812 break;
1813 }
Evan Chengaee4af62007-12-02 08:30:39 +00001814 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001815 }
1816 }
1817 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001818 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001819 if (CanFold && !Ops.empty()) {
1820 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001821 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00001822 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00001823 // Also folded uses, do not issue a load.
1824 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +00001825 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengf38d14f2007-12-05 09:05:34 +00001826 }
Lang Hames233a60e2009-11-03 23:52:08 +00001827 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
Evan Chengcddbb832007-11-30 21:23:43 +00001828 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001829 }
1830
Evan Cheng7e073ba2008-04-09 20:57:25 +00001831 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001832 if (!Folded) {
1833 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001834 bool isKill = LR->end == index.getStoreIndex();
Evan Chengb0a6f622008-05-20 08:10:37 +00001835 if (!MI->registerDefIsDead(nI.reg))
1836 // No need to spill a dead def.
1837 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001838 if (isKill)
1839 AddedKill.insert(&nI);
1840 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001841 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001842 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001843 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001844 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001845
Evan Cheng1953d0c2007-11-29 10:12:14 +00001846 int Id = RestoreMBBs.find_first();
1847 while (Id != -1) {
1848 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1849 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001850 SlotIndex index = restores[i].index;
1851 if (index == SlotIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00001852 continue;
1853 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001854 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001855 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001856 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001857 bool CanFold = false;
1858 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001859 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001860 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001861 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1862 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001863 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00001864 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001865
Evan Cheng0cbb1162007-11-29 01:06:25 +00001866 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001867 // If this restore were to be folded, it would have been folded
1868 // already.
1869 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001870 break;
1871 }
Evan Chengaee4af62007-12-02 08:30:39 +00001872 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001873 }
1874 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001875
1876 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001877 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001878 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001879 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001880 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1881 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001882 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1883 int LdSlot = 0;
1884 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1885 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00001886 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001887 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1888 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00001889 if (!Folded) {
1890 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1891 if (ImpUse) {
1892 // Re-matting an instruction with virtual register use. Add the
1893 // register as an implicit use on the use MI and update the register
1894 // interval's spill weight to HUGE_VALF to prevent it from being
1895 // spilled.
1896 LiveInterval &ImpLi = getInterval(ImpUse);
1897 ImpLi.weight = HUGE_VALF;
1898 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1899 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001900 }
Evan Chengaee4af62007-12-02 08:30:39 +00001901 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001902 }
1903 // If folding is not possible / failed, then tell the spiller to issue a
1904 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001905 if (Folded)
Lang Hames233a60e2009-11-03 23:52:08 +00001906 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengb50bb8c2007-12-05 08:16:32 +00001907 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001908 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001909 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001910 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001911 }
1912
Evan Chengb50bb8c2007-12-05 08:16:32 +00001913 // Finalize intervals: add kills, finalize spill weights, and filter out
1914 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001915 std::vector<LiveInterval*> RetNewLIs;
1916 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1917 LiveInterval *LI = NewLIs[i];
1918 if (!LI->empty()) {
Lang Hames233a60e2009-11-03 23:52:08 +00001919 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001920 if (!AddedKill.count(LI)) {
1921 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001922 SlotIndex LastUseIdx = LR->end.getBaseIndex();
Evan Chengd120ffd2007-12-05 10:24:35 +00001923 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001924 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001925 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00001926 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001927 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001928 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001929 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001930 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001931 RetNewLIs.push_back(LI);
1932 }
1933 }
Evan Cheng81a03822007-11-17 00:40:40 +00001934
Evan Cheng4cce6b42008-04-11 17:53:36 +00001935 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001936 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001937}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001938
1939/// hasAllocatableSuperReg - Return true if the specified physical register has
1940/// any super register that's allocatable.
1941bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1942 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1943 if (allocatableRegs_[*AS] && hasInterval(*AS))
1944 return true;
1945 return false;
1946}
1947
1948/// getRepresentativeReg - Find the largest super register of the specified
1949/// physical register.
1950unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1951 // Find the largest super-register that is allocatable.
1952 unsigned BestReg = Reg;
1953 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1954 unsigned SuperReg = *AS;
1955 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1956 BestReg = SuperReg;
1957 break;
1958 }
1959 }
1960 return BestReg;
1961}
1962
1963/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1964/// specified interval that conflicts with the specified physical register.
1965unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1966 unsigned PhysReg) const {
1967 unsigned NumConflicts = 0;
1968 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1969 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1970 E = mri_->reg_end(); I != E; ++I) {
1971 MachineOperand &O = I.getOperand();
1972 MachineInstr *MI = O.getParent();
Lang Hames233a60e2009-11-03 23:52:08 +00001973 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00001974 if (pli.liveAt(Index))
1975 ++NumConflicts;
1976 }
1977 return NumConflicts;
1978}
1979
1980/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00001981/// around all defs and uses of the specified interval. Return true if it
1982/// was able to cut its interval.
1983bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00001984 unsigned PhysReg, VirtRegMap &vrm) {
1985 unsigned SpillReg = getRepresentativeReg(PhysReg);
1986
1987 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1988 // If there are registers which alias PhysReg, but which are not a
1989 // sub-register of the chosen representative super register. Assert
1990 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00001991 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00001992 tri_->isSuperRegister(*AS, SpillReg));
1993
Evan Cheng2824a652009-03-23 18:24:37 +00001994 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00001995 SmallVector<unsigned, 4> PRegs;
1996 if (hasInterval(SpillReg))
1997 PRegs.push_back(SpillReg);
1998 else {
1999 SmallSet<unsigned, 4> Added;
2000 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2001 if (Added.insert(*AS) && hasInterval(*AS)) {
2002 PRegs.push_back(*AS);
2003 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2004 Added.insert(*ASS);
2005 }
2006 }
2007
Evan Cheng676dd7c2008-03-11 07:19:34 +00002008 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2009 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2010 E = mri_->reg_end(); I != E; ++I) {
2011 MachineOperand &O = I.getOperand();
2012 MachineInstr *MI = O.getParent();
2013 if (SeenMIs.count(MI))
2014 continue;
2015 SeenMIs.insert(MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002016 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002017 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2018 unsigned PReg = PRegs[i];
2019 LiveInterval &pli = getInterval(PReg);
2020 if (!pli.liveAt(Index))
2021 continue;
2022 vrm.addEmergencySpill(PReg, MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002023 SlotIndex StartIdx = Index.getLoadIndex();
2024 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
Evan Cheng2824a652009-03-23 18:24:37 +00002025 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002026 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002027 Cut = true;
2028 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002029 std::string msg;
2030 raw_string_ostream Msg(msg);
2031 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002032 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002033 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002034 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002035 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002036 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002037 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002038 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002039 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002040 if (!hasInterval(*AS))
2041 continue;
2042 LiveInterval &spli = getInterval(*AS);
2043 if (spli.liveAt(Index))
Lang Hames233a60e2009-11-03 23:52:08 +00002044 spli.removeRange(Index.getLoadIndex(),
2045 Index.getNextIndex().getBaseIndex());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002046 }
2047 }
2048 }
Evan Cheng2824a652009-03-23 18:24:37 +00002049 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002050}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002051
2052LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002053 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002054 LiveInterval& Interval = getOrCreateInterval(reg);
2055 VNInfo* VN = Interval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +00002056 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames86511252009-09-04 20:41:11 +00002057 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002058 VN->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +00002059 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
Lang Hames86511252009-09-04 20:41:11 +00002060 LiveRange LR(
Lang Hames233a60e2009-11-03 23:52:08 +00002061 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2062 getMBBEndIdx(startInst->getParent()).getNextIndex().getBaseIndex(), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002063 Interval.addRange(LR);
2064
2065 return LR;
2066}
David Greeneb5257662009-08-03 21:55:09 +00002067