blob: 24adf364e710c02440430f2aa9cca5a496351512 [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);
Evan Chengfb112882009-03-23 08:01:15 +0000377 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000378 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000379
380 // Add the new live interval which replaces the range for the input copy.
381 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000382 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000383 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000384 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385
386 // If this redefinition is dead, we need to add a dummy unit live
387 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000388 if (MO.isDead())
Lang Hames233a60e2009-11-03 23:52:08 +0000389 interval.addRange(LiveRange(RedefIndex, RedefIndex.getStoreIndex(),
390 OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000391
Bill Wendling8e6179f2009-08-22 20:18:03 +0000392 DEBUG({
393 errs() << " RESULT: ";
394 interval.print(errs(), tri_);
395 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000396 } else {
397 // Otherwise, this must be because of phi elimination. If this is the
398 // first redefinition of the vreg that we have seen, go back and change
399 // the live range in the PHI block to be a different value number.
400 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000401 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000402 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000403 MachineInstr *Killer = vi.Kills[0];
Lang Hames233a60e2009-11-03 23:52:08 +0000404 SlotIndex Start = getMBBStartIdx(Killer->getParent());
405 SlotIndex End = getInstructionIndex(Killer).getDefIndex();
Bill Wendling8e6179f2009-08-22 20:18:03 +0000406 DEBUG({
407 errs() << " Removing [" << Start << "," << End << "] from: ";
408 interval.print(errs(), tri_);
409 errs() << "\n";
410 });
Lang Hamesffd13262009-07-09 03:57:02 +0000411 interval.removeRange(Start, End);
412 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000413 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000414 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
Lang Hames233a60e2009-11-03 23:52:08 +0000415 VNI->addKill(indexes_->getTerminatorGap(killMBB));
Lang Hames857c4e02009-06-17 21:01:20 +0000416 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000417 DEBUG({
418 errs() << " RESULT: ";
419 interval.print(errs(), tri_);
420 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000422 // Replace the interval with one of a NEW value number. Note that this
423 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000424 LiveRange LR(Start, End,
Lang Hames233a60e2009-11-03 23:52:08 +0000425 interval.getNextValue(SlotIndex(getMBBStartIdx(mbb), true),
426 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000427 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000428 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000430 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000431 DEBUG({
432 errs() << " RESULT: ";
433 interval.print(errs(), tri_);
434 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000435 }
436
437 // In the case of PHI elimination, each variable definition is only
438 // live until the end of the block. We've already taken care of the
439 // rest of the live range.
Lang Hames233a60e2009-11-03 23:52:08 +0000440 SlotIndex defIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000441 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000442 defIndex = MIIdx.getUseIndex();
Evan Cheng752195e2009-09-14 21:33:42 +0000443
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000444 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000445 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000446 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000447 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000448 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000449 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000450 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000451 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000452 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000453
Lang Hames233a60e2009-11-03 23:52:08 +0000454 SlotIndex killIndex = getMBBEndIdx(mbb).getNextIndex().getLoadIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000455 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 interval.addRange(LR);
Lang Hames233a60e2009-11-03 23:52:08 +0000457 ValNo->addKill(indexes_->getTerminatorGap(mbb));
Lang Hames857c4e02009-06-17 21:01:20 +0000458 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000459 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000460 }
461 }
462
Bill Wendling8e6179f2009-08-22 20:18:03 +0000463 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000464}
465
Chris Lattnerf35fef72004-07-23 21:24:19 +0000466void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000467 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000468 SlotIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000469 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000470 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000471 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000472 // A physical register cannot be live across basic block, so its
473 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000474 DEBUG({
475 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000476 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000477 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000478
Lang Hames233a60e2009-11-03 23:52:08 +0000479 SlotIndex baseIndex = MIIdx;
480 SlotIndex start = baseIndex.getDefIndex();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000481 // Earlyclobbers move back one.
482 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000483 start = MIIdx.getUseIndex();
484 SlotIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000485
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000486 // If it is not used after definition, it is considered dead at
487 // the instruction defining it. Hence its interval is:
488 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000489 // For earlyclobbers, the defSlot was pushed back one; the extra
490 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000491 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000492 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000493 end = start.getStoreIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000494 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000495 }
496
497 // If it is not dead on definition, it must be killed by a
498 // subsequent instruction. Hence its interval is:
499 // [defSlot(def), useSlot(kill)+1)
Lang Hames233a60e2009-11-03 23:52:08 +0000500 baseIndex = baseIndex.getNextIndex();
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000501 while (++mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000502
503 if (getInstructionFromIndex(baseIndex) == 0)
504 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
505
Evan Cheng6130f662008-03-05 00:59:57 +0000506 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000507 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000508 end = baseIndex.getDefIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000509 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000510 } else {
511 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
512 if (DefIdx != -1) {
513 if (mi->isRegTiedToUseOperand(DefIdx)) {
514 // Two-address instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000515 end = baseIndex.getDefIndex();
516 assert(!mi->getOperand(DefIdx).isEarlyClobber() &&
517 "Two address instruction is an early clobber?");
Evan Chengc45288e2009-04-27 20:42:46 +0000518 } else {
519 // Another instruction redefines the register before it is ever read.
520 // Then the register is essentially dead at the instruction that defines
521 // it. Hence its interval is:
522 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000523 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000524 end = start.getStoreIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000525 }
526 goto exit;
527 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000528 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000529
Lang Hames233a60e2009-11-03 23:52:08 +0000530 baseIndex = baseIndex.getNextIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000531 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000532
533 // The only case we should have a dead physreg here without a killing or
534 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000535 // and never used. Another possible case is the implicit use of the
536 // physical register has been deleted by two-address pass.
Lang Hames233a60e2009-11-03 23:52:08 +0000537 end = start.getStoreIndex();
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000538
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000539exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000540 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000541
Evan Cheng24a3cc42007-04-25 07:30:23 +0000542 // Already exists? Extend old live interval.
543 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000544 bool Extend = OldLR != interval.end();
545 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000546 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000547 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000548 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000549 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000550 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000551 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000552 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000553}
554
Chris Lattnerf35fef72004-07-23 21:24:19 +0000555void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
556 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000557 SlotIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000558 MachineOperand& MO,
559 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000560 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000561 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000562 getOrCreateInterval(MO.getReg()));
563 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000564 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000565 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000566 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000567 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000568 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000569 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000570 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000571 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000572 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000573 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000574 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000575 // If MI also modifies the sub-register explicitly, avoid processing it
576 // more than once. Do not pass in TRI here so it checks for exact match.
577 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000578 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000579 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000580 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000581}
582
Evan Chengb371f452007-02-19 21:49:54 +0000583void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000584 SlotIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000585 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000586 DEBUG({
587 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000588 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000589 });
Evan Chengb371f452007-02-19 21:49:54 +0000590
591 // Look for kills, if it reaches a def before it's killed, then it shouldn't
592 // be considered a livein.
593 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000594 SlotIndex baseIndex = MIIdx;
595 SlotIndex start = baseIndex;
596 if (getInstructionFromIndex(baseIndex) == 0)
597 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
598
599 SlotIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000600 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000601
Evan Chengb371f452007-02-19 21:49:54 +0000602 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000603 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000604 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000605 end = baseIndex.getDefIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000606 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000607 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000608 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000609 // Another instruction redefines the register before it is ever read.
610 // Then the register is essentially dead at the instruction that defines
611 // it. Hence its interval is:
612 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000613 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000614 end = start.getStoreIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000615 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000616 break;
Evan Chengb371f452007-02-19 21:49:54 +0000617 }
618
Evan Chengb371f452007-02-19 21:49:54 +0000619 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000620 if (mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000621 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +0000622 }
Evan Chengb371f452007-02-19 21:49:54 +0000623 }
624
Evan Cheng75611fb2007-06-27 01:16:36 +0000625 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000626 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000627 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000628 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000629 end = MIIdx.getStoreIndex();
Evan Cheng292da942007-06-27 18:47:28 +0000630 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000631 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +0000632 end = baseIndex;
633 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000634 }
635
Lang Hames10382fb2009-06-19 02:17:53 +0000636 VNInfo *vni =
Lang Hames233a60e2009-11-03 23:52:08 +0000637 interval.getNextValue(SlotIndex(getMBBStartIdx(MBB), true),
Lang Hames86511252009-09-04 20:41:11 +0000638 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000639 vni->setIsPHIDef(true);
640 LiveRange LR(start, end, vni);
Jakob Stoklund Olesen3de23e62009-11-07 01:58:40 +0000641
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000642 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000643 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000644 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +0000645}
646
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000647/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000648/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000649/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000650/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000651void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000652 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +0000653 << "********** Function: "
654 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +0000655
656 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000657 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
658 MBBI != E; ++MBBI) {
659 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000660 // Track the index of the current machine instr.
Lang Hames233a60e2009-11-03 23:52:08 +0000661 SlotIndex MIIndex = getMBBStartIdx(MBB);
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000662 DEBUG(errs() << MBB->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000663
Chris Lattner428b92e2006-09-15 03:57:23 +0000664 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000665
Dan Gohmancb406c22007-10-03 19:26:29 +0000666 // Create intervals for live-ins to this BB first.
667 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
668 LE = MBB->livein_end(); LI != LE; ++LI) {
669 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
670 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000671 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000672 if (!hasInterval(*AS))
673 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
674 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000675 }
676
Owen Anderson99500ae2008-09-15 22:00:38 +0000677 // Skip over empty initial indices.
Lang Hames233a60e2009-11-03 23:52:08 +0000678 if (getInstructionFromIndex(MIIndex) == 0)
679 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +0000680
Chris Lattner428b92e2006-09-15 03:57:23 +0000681 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000682 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000683
Evan Cheng438f7bc2006-11-10 08:43:01 +0000684 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000685 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
686 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +0000687 if (!MO.isReg() || !MO.getReg())
688 continue;
689
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000690 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +0000691 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000692 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +0000693 else if (MO.isUndef())
694 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000695 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000696
Lang Hames233a60e2009-11-03 23:52:08 +0000697 // Move to the next instr slot.
698 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000699 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000700 }
Evan Chengd129d732009-07-17 19:43:40 +0000701
702 // Create empty intervals for registers defined by implicit_def's (except
703 // for those implicit_def that define values which are liveout of their
704 // blocks.
705 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
706 unsigned UndefReg = UndefUses[i];
707 (void)getOrCreateInterval(UndefReg);
708 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000709}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000710
Owen Anderson03857b22008-08-13 21:49:13 +0000711LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000712 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +0000713 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000714}
Evan Chengf2fbca62007-11-12 06:35:08 +0000715
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000716/// dupInterval - Duplicate a live interval. The caller is responsible for
717/// managing the allocated memory.
718LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
719 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000720 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000721 return NewLI;
722}
723
Evan Chengc8d044e2008-02-15 18:24:29 +0000724/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
725/// copy field and returns the source register that defines it.
726unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +0000727 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +0000728 return 0;
729
Lang Hames52c1afc2009-08-10 23:43:28 +0000730 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000731 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +0000732 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000733 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +0000734 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000735 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000736 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
737 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
738 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000739
Evan Cheng04ee5a12009-01-20 19:12:24 +0000740 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000741 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000742 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +0000743 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +0000744 return 0;
745}
Evan Chengf2fbca62007-11-12 06:35:08 +0000746
747//===----------------------------------------------------------------------===//
748// Register allocator hooks.
749//
750
Evan Chengd70dbb52008-02-22 09:24:50 +0000751/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
752/// allow one) virtual register operand, then its uses are implicitly using
753/// the register. Returns the virtual register.
754unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
755 MachineInstr *MI) const {
756 unsigned RegOp = 0;
757 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
758 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000759 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +0000760 continue;
761 unsigned Reg = MO.getReg();
762 if (Reg == 0 || Reg == li.reg)
763 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +0000764
765 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
766 !allocatableRegs_[Reg])
767 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +0000768 // FIXME: For now, only remat MI with at most one register operand.
769 assert(!RegOp &&
770 "Can't rematerialize instruction with multiple register operand!");
771 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000772#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +0000773 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000774#endif
Evan Chengd70dbb52008-02-22 09:24:50 +0000775 }
776 return RegOp;
777}
778
779/// isValNoAvailableAt - Return true if the val# of the specified interval
780/// which reaches the given instruction also reaches the specified use index.
781bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000782 SlotIndex UseIdx) const {
783 SlotIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +0000784 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
785 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
786 return UI != li.end() && UI->valno == ValNo;
787}
788
Evan Chengf2fbca62007-11-12 06:35:08 +0000789/// isReMaterializable - Returns true if the definition MI of the specified
790/// val# of the specified interval is re-materializable.
791bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000792 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +0000793 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000794 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000795 if (DisableReMat)
796 return false;
797
Dan Gohmana70dca12009-10-09 23:27:56 +0000798 if (!tii_->isTriviallyReMaterializable(MI, aa_))
799 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +0000800
Dan Gohmana70dca12009-10-09 23:27:56 +0000801 // Target-specific code can mark an instruction as being rematerializable
802 // if it has one virtual reg use, though it had better be something like
803 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +0000804 unsigned ImpUse = getReMatImplicitUse(li, MI);
805 if (ImpUse) {
806 const LiveInterval &ImpLi = getInterval(ImpUse);
807 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
808 re = mri_->use_end(); ri != re; ++ri) {
809 MachineInstr *UseMI = &*ri;
Lang Hames233a60e2009-11-03 23:52:08 +0000810 SlotIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +0000811 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
812 continue;
813 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
814 return false;
815 }
Evan Chengdc377862008-09-30 15:44:16 +0000816
817 // If a register operand of the re-materialized instruction is going to
818 // be spilled next, then it's not legal to re-materialize this instruction.
819 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
820 if (ImpUse == SpillIs[i]->reg)
821 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000822 }
823 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000824}
825
Evan Cheng06587492008-10-24 02:05:00 +0000826/// isReMaterializable - Returns true if the definition MI of the specified
827/// val# of the specified interval is re-materializable.
828bool LiveIntervals::isReMaterializable(const LiveInterval &li,
829 const VNInfo *ValNo, MachineInstr *MI) {
830 SmallVector<LiveInterval*, 4> Dummy1;
831 bool Dummy2;
832 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
833}
834
Evan Cheng5ef3a042007-12-06 00:01:56 +0000835/// isReMaterializable - Returns true if every definition of MI of every
836/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +0000837bool LiveIntervals::isReMaterializable(const LiveInterval &li,
838 SmallVectorImpl<LiveInterval*> &SpillIs,
839 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +0000840 isLoad = false;
841 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
842 i != e; ++i) {
843 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +0000844 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000845 continue; // Dead val#.
846 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +0000847 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000848 return false;
Lang Hames857c4e02009-06-17 21:01:20 +0000849 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000850 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000851 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +0000852 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000853 return false;
854 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000855 }
856 return true;
857}
858
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000859/// FilterFoldedOps - Filter out two-address use operands. Return
860/// true if it finds any issue with the operands that ought to prevent
861/// folding.
862static bool FilterFoldedOps(MachineInstr *MI,
863 SmallVector<unsigned, 2> &Ops,
864 unsigned &MRInfo,
865 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000866 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000867 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
868 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000869 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000870 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000871 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000872 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000873 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000874 MRInfo |= (unsigned)VirtRegMap::isMod;
875 else {
876 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +0000877 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +0000878 MRInfo = VirtRegMap::isModRef;
879 continue;
880 }
881 MRInfo |= (unsigned)VirtRegMap::isRef;
882 }
883 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000884 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000885 return false;
886}
887
888
889/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
890/// slot / to reg or any rematerialized load into ith operand of specified
891/// MI. If it is successul, MI is updated with the newly created MI and
892/// returns true.
893bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
894 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames233a60e2009-11-03 23:52:08 +0000895 SlotIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000896 SmallVector<unsigned, 2> &Ops,
897 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000898 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +0000899 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000900 RemoveMachineInstrFromMaps(MI);
901 vrm.RemoveMachineInstrFromMaps(MI);
902 MI->eraseFromParent();
903 ++numFolds;
904 return true;
905 }
906
907 // Filter the list of operand indexes that are to be folded. Abort if
908 // any operand will prevent folding.
909 unsigned MRInfo = 0;
910 SmallVector<unsigned, 2> FoldOps;
911 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
912 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000913
Evan Cheng427f4c12008-03-31 23:19:51 +0000914 // The only time it's safe to fold into a two address instruction is when
915 // it's folding reload and spill from / into a spill stack slot.
916 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000917 return false;
918
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000919 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
920 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000921 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000922 // Remember this instruction uses the spill slot.
923 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
924
Evan Chengf2fbca62007-11-12 06:35:08 +0000925 // Attempt to fold the memory reference into the instruction. If
926 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +0000927 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000928 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000929 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000930 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000931 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000932 vrm.transferEmergencySpills(MI, fmi);
Lang Hames233a60e2009-11-03 23:52:08 +0000933 ReplaceMachineInstrInMaps(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000934 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000935 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000936 return true;
937 }
938 return false;
939}
940
Evan Cheng018f9b02007-12-05 03:22:34 +0000941/// canFoldMemoryOperand - Returns true if the specified load / store
942/// folding is possible.
943bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000944 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000945 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000946 // Filter the list of operand indexes that are to be folded. Abort if
947 // any operand will prevent folding.
948 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000949 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000950 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
951 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000952
Evan Cheng3c75ba82008-04-01 21:37:32 +0000953 // It's only legal to remat for a use, not a def.
954 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000955 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000956
Evan Chengd70dbb52008-02-22 09:24:50 +0000957 return tii_->canFoldMemoryOperand(MI, FoldOps);
958}
959
Evan Cheng81a03822007-11-17 00:40:40 +0000960bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
Lang Hames233a60e2009-11-03 23:52:08 +0000961 LiveInterval::Ranges::const_iterator itr = li.ranges.begin();
962
963 MachineBasicBlock *mbb = indexes_->getMBBCoveringRange(itr->start, itr->end);
964
965 if (mbb == 0)
966 return false;
967
968 for (++itr; itr != li.ranges.end(); ++itr) {
969 MachineBasicBlock *mbb2 =
970 indexes_->getMBBCoveringRange(itr->start, itr->end);
971
972 if (mbb2 != mbb)
Evan Cheng81a03822007-11-17 00:40:40 +0000973 return false;
974 }
Lang Hames233a60e2009-11-03 23:52:08 +0000975
Evan Cheng81a03822007-11-17 00:40:40 +0000976 return true;
977}
978
Evan Chengd70dbb52008-02-22 09:24:50 +0000979/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
980/// interval on to-be re-materialized operands of MI) with new register.
981void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
982 MachineInstr *MI, unsigned NewVReg,
983 VirtRegMap &vrm) {
984 // There is an implicit use. That means one of the other operand is
985 // being remat'ed and the remat'ed instruction has li.reg as an
986 // use operand. Make sure we rewrite that as well.
987 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
988 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000989 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +0000990 continue;
991 unsigned Reg = MO.getReg();
992 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
993 continue;
994 if (!vrm.isReMaterialized(Reg))
995 continue;
996 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +0000997 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
998 if (UseMO)
999 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001000 }
1001}
1002
Evan Chengf2fbca62007-11-12 06:35:08 +00001003/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1004/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001005bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001006rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames233a60e2009-11-03 23:52:08 +00001007 bool TrySplit, SlotIndex index, SlotIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001008 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001009 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001010 unsigned Slot, int LdSlot,
1011 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001012 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001013 const TargetRegisterClass* rc,
1014 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001015 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001016 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001017 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001018 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001019 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001020 RestartInstruction:
1021 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1022 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001023 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001024 continue;
1025 unsigned Reg = mop.getReg();
1026 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001027 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001028 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001029 if (Reg != li.reg)
1030 continue;
1031
1032 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001033 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001034 int FoldSlot = Slot;
1035 if (DefIsReMat) {
1036 // If this is the rematerializable definition MI itself and
1037 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001038 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001039 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1040 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001041 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001042 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001043 MI->eraseFromParent();
1044 break;
1045 }
1046
1047 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001048 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001049 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001050 if (isLoad) {
1051 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1052 FoldSS = isLoadSS;
1053 FoldSlot = LdSlot;
1054 }
1055 }
1056
Evan Chengf2fbca62007-11-12 06:35:08 +00001057 // Scan all of the operands of this instruction rewriting operands
1058 // to use NewVReg instead of li.reg as appropriate. We do this for
1059 // two reasons:
1060 //
1061 // 1. If the instr reads the same spilled vreg multiple times, we
1062 // want to reuse the NewVReg.
1063 // 2. If the instr is a two-addr instruction, we are required to
1064 // keep the src/dst regs pinned.
1065 //
1066 // Keep track of whether we replace a use and/or def so that we can
1067 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001068
Evan Cheng81a03822007-11-17 00:40:40 +00001069 HasUse = mop.isUse();
1070 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001071 SmallVector<unsigned, 2> Ops;
1072 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001073 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001074 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001075 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001076 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001077 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001078 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001079 continue;
1080 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001081 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001082 if (!MOj.isUndef()) {
1083 HasUse |= MOj.isUse();
1084 HasDef |= MOj.isDef();
1085 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001086 }
1087 }
1088
David Greene26b86a02008-10-27 17:38:59 +00001089 // Create a new virtual register for the spill interval.
1090 // Create the new register now so we can map the fold instruction
1091 // to the new register so when it is unfolded we get the correct
1092 // answer.
1093 bool CreatedNewVReg = false;
1094 if (NewVReg == 0) {
1095 NewVReg = mri_->createVirtualRegister(rc);
1096 vrm.grow();
1097 CreatedNewVReg = true;
Jakob Stoklund Olesence7a6632009-11-30 22:55:54 +00001098
1099 // The new virtual register should get the same allocation hints as the
1100 // old one.
1101 std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(Reg);
1102 if (Hint.first || Hint.second)
1103 mri_->setRegAllocationHint(NewVReg, Hint.first, Hint.second);
David Greene26b86a02008-10-27 17:38:59 +00001104 }
1105
Evan Cheng9c3c2212008-06-06 07:54:39 +00001106 if (!TryFold)
1107 CanFold = false;
1108 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001109 // Do not fold load / store here if we are splitting. We'll find an
1110 // optimal point to insert a load / store later.
1111 if (!TrySplit) {
1112 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001113 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001114 // Folding the load/store can completely change the instruction in
1115 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001116
1117 if (FoldSS) {
1118 // We need to give the new vreg the same stack slot as the
1119 // spilled interval.
1120 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1121 }
1122
Evan Cheng018f9b02007-12-05 03:22:34 +00001123 HasUse = false;
1124 HasDef = false;
1125 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001126 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001127 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001128 goto RestartInstruction;
1129 }
1130 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001131 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001132 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001133 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001134 }
Evan Chengcddbb832007-11-30 21:23:43 +00001135
Evan Chengcddbb832007-11-30 21:23:43 +00001136 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001137 if (mop.isImplicit())
1138 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001139
1140 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001141 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1142 MachineOperand &mopj = MI->getOperand(Ops[j]);
1143 mopj.setReg(NewVReg);
1144 if (mopj.isImplicit())
1145 rewriteImplicitOps(li, MI, NewVReg, vrm);
1146 }
Evan Chengcddbb832007-11-30 21:23:43 +00001147
Evan Cheng81a03822007-11-17 00:40:40 +00001148 if (CreatedNewVReg) {
1149 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001150 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001151 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001152 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001153 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001154 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001155 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001156 }
1157 if (!CanDelete || (HasUse && HasDef)) {
1158 // If this is a two-addr instruction then its use operands are
1159 // rematerializable but its def is not. It should be assigned a
1160 // stack slot.
1161 vrm.assignVirt2StackSlot(NewVReg, Slot);
1162 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001163 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001164 vrm.assignVirt2StackSlot(NewVReg, Slot);
1165 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001166 } else if (HasUse && HasDef &&
1167 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1168 // If this interval hasn't been assigned a stack slot (because earlier
1169 // def is a deleted remat def), do it now.
1170 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1171 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001172 }
1173
Evan Cheng313d4b82008-02-23 00:33:04 +00001174 // Re-matting an instruction with virtual register use. Add the
1175 // register as an implicit use on the use MI.
1176 if (DefIsReMat && ImpUse)
1177 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1178
Evan Cheng5b69eba2009-04-21 22:46:52 +00001179 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001180 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001181 if (CreatedNewVReg) {
1182 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001183 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001184 if (TrySplit)
1185 vrm.setIsSplitFromReg(NewVReg, li.reg);
1186 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001187
1188 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001189 if (CreatedNewVReg) {
Lang Hames233a60e2009-11-03 23:52:08 +00001190 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1191 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001192 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001193 nI.addRange(LR);
1194 } else {
1195 // Extend the split live interval to this def / use.
Lang Hames233a60e2009-11-03 23:52:08 +00001196 SlotIndex End = index.getDefIndex();
Evan Cheng81a03822007-11-17 00:40:40 +00001197 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1198 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001199 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001200 nI.addRange(LR);
1201 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001202 }
1203 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001204 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1205 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001206 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001207 nI.addRange(LR);
1208 }
Evan Cheng81a03822007-11-17 00:40:40 +00001209
Bill Wendling8e6179f2009-08-22 20:18:03 +00001210 DEBUG({
1211 errs() << "\t\t\t\tAdded new interval: ";
1212 nI.print(errs(), tri_);
1213 errs() << '\n';
1214 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001215 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001216 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001217}
Evan Cheng81a03822007-11-17 00:40:40 +00001218bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001219 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001220 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +00001221 SlotIndex Idx) const {
1222 SlotIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001223 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames233a60e2009-11-03 23:52:08 +00001224 if (VNI->kills[j].isPHI())
Lang Hamesffd13262009-07-09 03:57:02 +00001225 continue;
1226
Lang Hames233a60e2009-11-03 23:52:08 +00001227 SlotIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001228 if (KillIdx > Idx && KillIdx < End)
1229 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001230 }
1231 return false;
1232}
1233
Evan Cheng063284c2008-02-21 00:34:19 +00001234/// RewriteInfo - Keep track of machine instrs that will be rewritten
1235/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001236namespace {
1237 struct RewriteInfo {
Lang Hames233a60e2009-11-03 23:52:08 +00001238 SlotIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001239 MachineInstr *MI;
1240 bool HasUse;
1241 bool HasDef;
Lang Hames233a60e2009-11-03 23:52:08 +00001242 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001243 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1244 };
Evan Cheng063284c2008-02-21 00:34:19 +00001245
Dan Gohman844731a2008-05-13 00:00:25 +00001246 struct RewriteInfoCompare {
1247 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1248 return LHS.Index < RHS.Index;
1249 }
1250 };
1251}
Evan Cheng063284c2008-02-21 00:34:19 +00001252
Evan Chengf2fbca62007-11-12 06:35:08 +00001253void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001254rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001255 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001256 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001257 unsigned Slot, int LdSlot,
1258 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001259 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001260 const TargetRegisterClass* rc,
1261 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001262 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001263 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001264 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001265 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001266 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1267 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001268 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001269 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001270 unsigned NewVReg = 0;
Lang Hames233a60e2009-11-03 23:52:08 +00001271 SlotIndex start = I->start.getBaseIndex();
1272 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001273
Evan Cheng063284c2008-02-21 00:34:19 +00001274 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001275 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001276 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001277 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1278 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001279 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001280 MachineOperand &O = ri.getOperand();
1281 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001282 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames233a60e2009-11-03 23:52:08 +00001283 SlotIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001284 if (index < start || index >= end)
1285 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001286
1287 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001288 // Must be defined by an implicit def. It should not be spilled. Note,
1289 // this is for correctness reason. e.g.
1290 // 8 %reg1024<def> = IMPLICIT_DEF
1291 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1292 // The live range [12, 14) are not part of the r1024 live interval since
1293 // it's defined by an implicit def. It will not conflicts with live
1294 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001295 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001296 // the INSERT_SUBREG and both target registers that would overlap.
1297 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001298 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1299 }
1300 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1301
Evan Cheng313d4b82008-02-23 00:33:04 +00001302 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001303 // Now rewrite the defs and uses.
1304 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1305 RewriteInfo &rwi = RewriteMIs[i];
1306 ++i;
Lang Hames233a60e2009-11-03 23:52:08 +00001307 SlotIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001308 bool MIHasUse = rwi.HasUse;
1309 bool MIHasDef = rwi.HasDef;
1310 MachineInstr *MI = rwi.MI;
1311 // If MI def and/or use the same register multiple times, then there
1312 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001313 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001314 while (i != e && RewriteMIs[i].MI == MI) {
1315 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001316 bool isUse = RewriteMIs[i].HasUse;
1317 if (isUse) ++NumUses;
1318 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001319 MIHasDef |= RewriteMIs[i].HasDef;
1320 ++i;
1321 }
Evan Cheng81a03822007-11-17 00:40:40 +00001322 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001323
Evan Cheng0a891ed2008-05-23 23:00:04 +00001324 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001325 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001326 // register interval's spill weight to HUGE_VALF to prevent it from
1327 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001328 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001329 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001330 }
1331
Evan Cheng063284c2008-02-21 00:34:19 +00001332 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001333 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001334 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001335 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001336 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001337 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001338 // One common case:
1339 // x = use
1340 // ...
1341 // ...
1342 // def = ...
1343 // = use
1344 // It's better to start a new interval to avoid artifically
1345 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001346 if (MIHasDef && !MIHasUse) {
1347 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001348 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001349 }
1350 }
Evan Chengcada2452007-11-28 01:28:46 +00001351 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001352
1353 bool IsNew = ThisVReg == 0;
1354 if (IsNew) {
1355 // This ends the previous live interval. If all of its def / use
1356 // can be folded, give it a low spill weight.
1357 if (NewVReg && TrySplit && AllCanFold) {
1358 LiveInterval &nI = getOrCreateInterval(NewVReg);
1359 nI.weight /= 10.0F;
1360 }
1361 AllCanFold = true;
1362 }
1363 NewVReg = ThisVReg;
1364
Evan Cheng81a03822007-11-17 00:40:40 +00001365 bool HasDef = false;
1366 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001367 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001368 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1369 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1370 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001371 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001372 if (!HasDef && !HasUse)
1373 continue;
1374
Evan Cheng018f9b02007-12-05 03:22:34 +00001375 AllCanFold &= CanFold;
1376
Evan Cheng81a03822007-11-17 00:40:40 +00001377 // Update weight of spill interval.
1378 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001379 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001380 // The spill weight is now infinity as it cannot be spilled again.
1381 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001382 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001383 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001384
1385 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001386 if (HasDef) {
1387 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001388 bool HasKill = false;
1389 if (!HasUse)
Lang Hames233a60e2009-11-03 23:52:08 +00001390 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001391 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001392 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames233a60e2009-11-03 23:52:08 +00001393 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001394 if (VNI)
Lang Hames233a60e2009-11-03 23:52:08 +00001395 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001396 }
Owen Anderson28998312008-08-13 22:28:50 +00001397 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001398 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001399 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001400 if (SII == SpillIdxes.end()) {
1401 std::vector<SRInfo> S;
1402 S.push_back(SRInfo(index, NewVReg, true));
1403 SpillIdxes.insert(std::make_pair(MBBId, S));
1404 } else if (SII->second.back().vreg != NewVReg) {
1405 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001406 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001407 // If there is an earlier def and this is a two-address
1408 // instruction, then it's not possible to fold the store (which
1409 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001410 SRInfo &Info = SII->second.back();
1411 Info.index = index;
1412 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001413 }
1414 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001415 } else if (SII != SpillIdxes.end() &&
1416 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001417 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001418 // There is an earlier def that's not killed (must be two-address).
1419 // The spill is no longer needed.
1420 SII->second.pop_back();
1421 if (SII->second.empty()) {
1422 SpillIdxes.erase(MBBId);
1423 SpillMBBs.reset(MBBId);
1424 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001425 }
1426 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001427 }
1428
1429 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001430 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001431 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001432 if (SII != SpillIdxes.end() &&
1433 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001434 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001435 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001436 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001437 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001438 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001439 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001440 // If we are splitting live intervals, only fold if it's the first
1441 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001442 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001443 else if (IsNew) {
1444 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001445 if (RII == RestoreIdxes.end()) {
1446 std::vector<SRInfo> Infos;
1447 Infos.push_back(SRInfo(index, NewVReg, true));
1448 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1449 } else {
1450 RII->second.push_back(SRInfo(index, NewVReg, true));
1451 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001452 RestoreMBBs.set(MBBId);
1453 }
1454 }
1455
1456 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001457 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001458 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001459 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001460
1461 if (NewVReg && TrySplit && AllCanFold) {
1462 // If all of its def / use can be folded, give it a low spill weight.
1463 LiveInterval &nI = getOrCreateInterval(NewVReg);
1464 nI.weight /= 10.0F;
1465 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001466}
1467
Lang Hames233a60e2009-11-03 23:52:08 +00001468bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001469 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001470 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001471 if (!RestoreMBBs[Id])
1472 return false;
1473 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1474 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1475 if (Restores[i].index == index &&
1476 Restores[i].vreg == vr &&
1477 Restores[i].canFold)
1478 return true;
1479 return false;
1480}
1481
Lang Hames233a60e2009-11-03 23:52:08 +00001482void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001483 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001484 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001485 if (!RestoreMBBs[Id])
1486 return;
1487 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1488 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1489 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames233a60e2009-11-03 23:52:08 +00001490 Restores[i].index = SlotIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00001491}
Evan Cheng81a03822007-11-17 00:40:40 +00001492
Evan Cheng4cce6b42008-04-11 17:53:36 +00001493/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1494/// spilled and create empty intervals for their uses.
1495void
1496LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1497 const TargetRegisterClass* rc,
1498 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001499 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1500 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001501 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001502 MachineInstr *MI = &*ri;
1503 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001504 if (O.isDef()) {
1505 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1506 "Register def was not rewritten?");
1507 RemoveMachineInstrFromMaps(MI);
1508 vrm.RemoveMachineInstrFromMaps(MI);
1509 MI->eraseFromParent();
1510 } else {
1511 // This must be an use of an implicit_def so it's not part of the live
1512 // interval. Create a new empty live interval for it.
1513 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1514 unsigned NewVReg = mri_->createVirtualRegister(rc);
1515 vrm.grow();
1516 vrm.setIsImplicitlyDefined(NewVReg);
1517 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1518 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1519 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001520 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001521 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001522 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001523 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001524 }
1525 }
Evan Cheng419852c2008-04-03 16:39:43 +00001526 }
1527}
1528
Evan Chengf2fbca62007-11-12 06:35:08 +00001529std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001530addIntervalsForSpillsFast(const LiveInterval &li,
1531 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001532 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001533 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001534
1535 std::vector<LiveInterval*> added;
1536
1537 assert(li.weight != HUGE_VALF &&
1538 "attempt to spill already spilled interval!");
1539
Bill Wendling8e6179f2009-08-22 20:18:03 +00001540 DEBUG({
1541 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1542 li.dump();
1543 errs() << '\n';
1544 });
Owen Andersond6664312008-08-18 18:05:32 +00001545
1546 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1547
Owen Andersona41e47a2008-08-19 22:12:11 +00001548 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1549 while (RI != mri_->reg_end()) {
1550 MachineInstr* MI = &*RI;
1551
1552 SmallVector<unsigned, 2> Indices;
1553 bool HasUse = false;
1554 bool HasDef = false;
1555
1556 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1557 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001558 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001559
1560 HasUse |= MI->getOperand(i).isUse();
1561 HasDef |= MI->getOperand(i).isDef();
1562
1563 Indices.push_back(i);
1564 }
1565
1566 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1567 Indices, true, slot, li.reg)) {
1568 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00001569 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00001570 vrm.assignVirt2StackSlot(NewVReg, slot);
1571
Owen Andersona41e47a2008-08-19 22:12:11 +00001572 // create a new register for this spill
1573 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00001574
Owen Andersona41e47a2008-08-19 22:12:11 +00001575 // the spill weight is now infinity as it
1576 // cannot be spilled again
1577 nI.weight = HUGE_VALF;
1578
1579 // Rewrite register operands to use the new vreg.
1580 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1581 E = Indices.end(); I != E; ++I) {
1582 MI->getOperand(*I).setReg(NewVReg);
1583
1584 if (MI->getOperand(*I).isUse())
1585 MI->getOperand(*I).setIsKill(true);
1586 }
1587
1588 // Fill in the new live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001589 SlotIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00001590 if (HasUse) {
Lang Hames233a60e2009-11-03 23:52:08 +00001591 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1592 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001593 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001594 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001595 nI.addRange(LR);
1596 vrm.addRestorePoint(NewVReg, MI);
1597 }
1598 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001599 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1600 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001601 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001602 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001603 nI.addRange(LR);
1604 vrm.addSpillPoint(NewVReg, true, MI);
1605 }
1606
Owen Anderson17197312008-08-18 23:41:04 +00001607 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00001608
Bill Wendling8e6179f2009-08-22 20:18:03 +00001609 DEBUG({
1610 errs() << "\t\t\t\tadded new interval: ";
1611 nI.dump();
1612 errs() << '\n';
1613 });
Owen Andersona41e47a2008-08-19 22:12:11 +00001614 }
Owen Anderson9a032932008-08-18 21:20:32 +00001615
Owen Anderson9a032932008-08-18 21:20:32 +00001616
Owen Andersona41e47a2008-08-19 22:12:11 +00001617 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001618 }
Owen Andersond6664312008-08-18 18:05:32 +00001619
1620 return added;
1621}
1622
1623std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001624addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00001625 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00001626 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00001627
1628 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00001629 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00001630
Evan Chengf2fbca62007-11-12 06:35:08 +00001631 assert(li.weight != HUGE_VALF &&
1632 "attempt to spill already spilled interval!");
1633
Bill Wendling8e6179f2009-08-22 20:18:03 +00001634 DEBUG({
1635 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1636 li.print(errs(), tri_);
1637 errs() << '\n';
1638 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001639
Evan Cheng72eeb942008-12-05 17:00:16 +00001640 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00001641 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001642 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001643 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001644 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1645 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001646 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001647 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001648
1649 unsigned NumValNums = li.getNumValNums();
1650 SmallVector<MachineInstr*, 4> ReMatDefs;
1651 ReMatDefs.resize(NumValNums, NULL);
1652 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1653 ReMatOrigDefs.resize(NumValNums, NULL);
1654 SmallVector<int, 4> ReMatIds;
1655 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1656 BitVector ReMatDelete(NumValNums);
1657 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1658
Evan Cheng81a03822007-11-17 00:40:40 +00001659 // Spilling a split live interval. It cannot be split any further. Also,
1660 // it's also guaranteed to be a single val# / range interval.
1661 if (vrm.getPreSplitReg(li.reg)) {
1662 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001663 // Unset the split kill marker on the last use.
Lang Hames233a60e2009-11-03 23:52:08 +00001664 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1665 if (KillIdx != SlotIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00001666 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1667 assert(KillMI && "Last use disappeared?");
1668 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1669 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001670 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001671 }
Evan Chengadf85902007-12-05 09:51:10 +00001672 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001673 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1674 Slot = vrm.getStackSlot(li.reg);
1675 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1676 MachineInstr *ReMatDefMI = DefIsReMat ?
1677 vrm.getReMaterializedMI(li.reg) : NULL;
1678 int LdSlot = 0;
1679 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1680 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001681 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001682 bool IsFirstRange = true;
1683 for (LiveInterval::Ranges::const_iterator
1684 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1685 // If this is a split live interval with multiple ranges, it means there
1686 // are two-address instructions that re-defined the value. Only the
1687 // first def can be rematerialized!
1688 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001689 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001690 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1691 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001692 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001693 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001694 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001695 } else {
1696 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1697 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001698 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001699 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001700 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001701 }
1702 IsFirstRange = false;
1703 }
Evan Cheng419852c2008-04-03 16:39:43 +00001704
Evan Cheng4cce6b42008-04-11 17:53:36 +00001705 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001706 return NewLIs;
1707 }
1708
Evan Cheng752195e2009-09-14 21:33:42 +00001709 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001710 if (TrySplit)
1711 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001712 bool NeedStackSlot = false;
1713 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1714 i != e; ++i) {
1715 const VNInfo *VNI = *i;
1716 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00001717 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00001718 continue; // Dead val#.
1719 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001720 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1721 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001722 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00001723 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001724 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001725 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001726 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001727 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00001728 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00001729 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001730
1731 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00001732 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00001733 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001734 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001735 CanDelete = false;
1736 // Need a stack slot if there is any live range where uses cannot be
1737 // rematerialized.
1738 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001739 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001740 if (CanDelete)
1741 ReMatDelete.set(VN);
1742 } else {
1743 // Need a stack slot if there is any live range where uses cannot be
1744 // rematerialized.
1745 NeedStackSlot = true;
1746 }
1747 }
1748
1749 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00001750 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1751 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1752 Slot = vrm.assignVirt2StackSlot(li.reg);
1753
1754 // This case only occurs when the prealloc splitter has already assigned
1755 // a stack slot to this vreg.
1756 else
1757 Slot = vrm.getStackSlot(li.reg);
1758 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001759
1760 // Create new intervals and rewrite defs and uses.
1761 for (LiveInterval::Ranges::const_iterator
1762 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001763 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1764 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1765 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001766 bool CanDelete = ReMatDelete[I->valno->id];
1767 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001768 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001769 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001770 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001771 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001772 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001773 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001774 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001775 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001776 }
1777
Evan Cheng0cbb1162007-11-29 01:06:25 +00001778 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001779 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001780 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001781 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001782 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001783
Evan Chengb50bb8c2007-12-05 08:16:32 +00001784 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001785 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001786 if (NeedStackSlot) {
1787 int Id = SpillMBBs.find_first();
1788 while (Id != -1) {
1789 std::vector<SRInfo> &spills = SpillIdxes[Id];
1790 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001791 SlotIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001792 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001793 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001794 bool isReMat = vrm.isReMaterialized(VReg);
1795 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001796 bool CanFold = false;
1797 bool FoundUse = false;
1798 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001799 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001800 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001801 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1802 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001803 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001804 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001805
1806 Ops.push_back(j);
1807 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001808 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001809 if (isReMat ||
1810 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1811 RestoreMBBs, RestoreIdxes))) {
1812 // MI has two-address uses of the same register. If the use
1813 // isn't the first and only use in the BB, then we can't fold
1814 // it. FIXME: Move this to rewriteInstructionsForSpills.
1815 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001816 break;
1817 }
Evan Chengaee4af62007-12-02 08:30:39 +00001818 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001819 }
1820 }
1821 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001822 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001823 if (CanFold && !Ops.empty()) {
1824 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001825 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00001826 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00001827 // Also folded uses, do not issue a load.
1828 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +00001829 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengf38d14f2007-12-05 09:05:34 +00001830 }
Lang Hames233a60e2009-11-03 23:52:08 +00001831 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
Evan Chengcddbb832007-11-30 21:23:43 +00001832 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001833 }
1834
Evan Cheng7e073ba2008-04-09 20:57:25 +00001835 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001836 if (!Folded) {
1837 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001838 bool isKill = LR->end == index.getStoreIndex();
Evan Chengb0a6f622008-05-20 08:10:37 +00001839 if (!MI->registerDefIsDead(nI.reg))
1840 // No need to spill a dead def.
1841 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001842 if (isKill)
1843 AddedKill.insert(&nI);
1844 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001845 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001846 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001847 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001848 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001849
Evan Cheng1953d0c2007-11-29 10:12:14 +00001850 int Id = RestoreMBBs.find_first();
1851 while (Id != -1) {
1852 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1853 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001854 SlotIndex index = restores[i].index;
1855 if (index == SlotIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00001856 continue;
1857 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001858 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001859 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001860 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001861 bool CanFold = false;
1862 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001863 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001864 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001865 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1866 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001867 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00001868 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001869
Evan Cheng0cbb1162007-11-29 01:06:25 +00001870 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001871 // If this restore were to be folded, it would have been folded
1872 // already.
1873 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001874 break;
1875 }
Evan Chengaee4af62007-12-02 08:30:39 +00001876 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001877 }
1878 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001879
1880 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001881 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001882 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001883 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001884 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1885 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001886 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1887 int LdSlot = 0;
1888 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1889 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00001890 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001891 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1892 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00001893 if (!Folded) {
1894 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1895 if (ImpUse) {
1896 // Re-matting an instruction with virtual register use. Add the
1897 // register as an implicit use on the use MI and update the register
1898 // interval's spill weight to HUGE_VALF to prevent it from being
1899 // spilled.
1900 LiveInterval &ImpLi = getInterval(ImpUse);
1901 ImpLi.weight = HUGE_VALF;
1902 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1903 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001904 }
Evan Chengaee4af62007-12-02 08:30:39 +00001905 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001906 }
1907 // If folding is not possible / failed, then tell the spiller to issue a
1908 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001909 if (Folded)
Lang Hames233a60e2009-11-03 23:52:08 +00001910 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengb50bb8c2007-12-05 08:16:32 +00001911 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001912 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001913 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001914 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001915 }
1916
Evan Chengb50bb8c2007-12-05 08:16:32 +00001917 // Finalize intervals: add kills, finalize spill weights, and filter out
1918 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001919 std::vector<LiveInterval*> RetNewLIs;
1920 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1921 LiveInterval *LI = NewLIs[i];
1922 if (!LI->empty()) {
Lang Hames233a60e2009-11-03 23:52:08 +00001923 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001924 if (!AddedKill.count(LI)) {
1925 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001926 SlotIndex LastUseIdx = LR->end.getBaseIndex();
Evan Chengd120ffd2007-12-05 10:24:35 +00001927 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001928 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001929 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00001930 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001931 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001932 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001933 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001934 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001935 RetNewLIs.push_back(LI);
1936 }
1937 }
Evan Cheng81a03822007-11-17 00:40:40 +00001938
Evan Cheng4cce6b42008-04-11 17:53:36 +00001939 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001940 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001941}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001942
1943/// hasAllocatableSuperReg - Return true if the specified physical register has
1944/// any super register that's allocatable.
1945bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1946 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1947 if (allocatableRegs_[*AS] && hasInterval(*AS))
1948 return true;
1949 return false;
1950}
1951
1952/// getRepresentativeReg - Find the largest super register of the specified
1953/// physical register.
1954unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1955 // Find the largest super-register that is allocatable.
1956 unsigned BestReg = Reg;
1957 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1958 unsigned SuperReg = *AS;
1959 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1960 BestReg = SuperReg;
1961 break;
1962 }
1963 }
1964 return BestReg;
1965}
1966
1967/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1968/// specified interval that conflicts with the specified physical register.
1969unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1970 unsigned PhysReg) const {
1971 unsigned NumConflicts = 0;
1972 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1973 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1974 E = mri_->reg_end(); I != E; ++I) {
1975 MachineOperand &O = I.getOperand();
1976 MachineInstr *MI = O.getParent();
Lang Hames233a60e2009-11-03 23:52:08 +00001977 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00001978 if (pli.liveAt(Index))
1979 ++NumConflicts;
1980 }
1981 return NumConflicts;
1982}
1983
1984/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00001985/// around all defs and uses of the specified interval. Return true if it
1986/// was able to cut its interval.
1987bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00001988 unsigned PhysReg, VirtRegMap &vrm) {
1989 unsigned SpillReg = getRepresentativeReg(PhysReg);
1990
1991 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1992 // If there are registers which alias PhysReg, but which are not a
1993 // sub-register of the chosen representative super register. Assert
1994 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00001995 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00001996 tri_->isSuperRegister(*AS, SpillReg));
1997
Evan Cheng2824a652009-03-23 18:24:37 +00001998 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00001999 SmallVector<unsigned, 4> PRegs;
2000 if (hasInterval(SpillReg))
2001 PRegs.push_back(SpillReg);
2002 else {
2003 SmallSet<unsigned, 4> Added;
2004 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2005 if (Added.insert(*AS) && hasInterval(*AS)) {
2006 PRegs.push_back(*AS);
2007 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2008 Added.insert(*ASS);
2009 }
2010 }
2011
Evan Cheng676dd7c2008-03-11 07:19:34 +00002012 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2013 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2014 E = mri_->reg_end(); I != E; ++I) {
2015 MachineOperand &O = I.getOperand();
2016 MachineInstr *MI = O.getParent();
2017 if (SeenMIs.count(MI))
2018 continue;
2019 SeenMIs.insert(MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002020 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002021 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2022 unsigned PReg = PRegs[i];
2023 LiveInterval &pli = getInterval(PReg);
2024 if (!pli.liveAt(Index))
2025 continue;
2026 vrm.addEmergencySpill(PReg, MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002027 SlotIndex StartIdx = Index.getLoadIndex();
2028 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
Evan Cheng2824a652009-03-23 18:24:37 +00002029 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002030 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002031 Cut = true;
2032 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002033 std::string msg;
2034 raw_string_ostream Msg(msg);
2035 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002036 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002037 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002038 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002039 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002040 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002041 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002042 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002043 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002044 if (!hasInterval(*AS))
2045 continue;
2046 LiveInterval &spli = getInterval(*AS);
2047 if (spli.liveAt(Index))
Lang Hames233a60e2009-11-03 23:52:08 +00002048 spli.removeRange(Index.getLoadIndex(),
2049 Index.getNextIndex().getBaseIndex());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002050 }
2051 }
2052 }
Evan Cheng2824a652009-03-23 18:24:37 +00002053 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002054}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002055
2056LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002057 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002058 LiveInterval& Interval = getOrCreateInterval(reg);
2059 VNInfo* VN = Interval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +00002060 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames86511252009-09-04 20:41:11 +00002061 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002062 VN->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +00002063 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
Lang Hames86511252009-09-04 20:41:11 +00002064 LiveRange LR(
Lang Hames233a60e2009-11-03 23:52:08 +00002065 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2066 getMBBEndIdx(startInst->getParent()).getNextIndex().getBaseIndex(), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002067 Interval.addRange(LR);
2068
2069 return LR;
2070}
David Greeneb5257662009-08-03 21:55:09 +00002071