blob: 4412c1b020a443ff0f5ef944246383776b4e3875 [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;
1098 }
1099
Evan Cheng9c3c2212008-06-06 07:54:39 +00001100 if (!TryFold)
1101 CanFold = false;
1102 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001103 // Do not fold load / store here if we are splitting. We'll find an
1104 // optimal point to insert a load / store later.
1105 if (!TrySplit) {
1106 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001107 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001108 // Folding the load/store can completely change the instruction in
1109 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001110
1111 if (FoldSS) {
1112 // We need to give the new vreg the same stack slot as the
1113 // spilled interval.
1114 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1115 }
1116
Evan Cheng018f9b02007-12-05 03:22:34 +00001117 HasUse = false;
1118 HasDef = false;
1119 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001120 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001121 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001122 goto RestartInstruction;
1123 }
1124 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001125 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001126 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001127 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001128 }
Evan Chengcddbb832007-11-30 21:23:43 +00001129
Evan Chengcddbb832007-11-30 21:23:43 +00001130 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001131 if (mop.isImplicit())
1132 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001133
1134 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001135 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1136 MachineOperand &mopj = MI->getOperand(Ops[j]);
1137 mopj.setReg(NewVReg);
1138 if (mopj.isImplicit())
1139 rewriteImplicitOps(li, MI, NewVReg, vrm);
1140 }
Evan Chengcddbb832007-11-30 21:23:43 +00001141
Evan Cheng81a03822007-11-17 00:40:40 +00001142 if (CreatedNewVReg) {
1143 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001144 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001145 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001146 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001147 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001148 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001149 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001150 }
1151 if (!CanDelete || (HasUse && HasDef)) {
1152 // If this is a two-addr instruction then its use operands are
1153 // rematerializable but its def is not. It should be assigned a
1154 // stack slot.
1155 vrm.assignVirt2StackSlot(NewVReg, Slot);
1156 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001157 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001158 vrm.assignVirt2StackSlot(NewVReg, Slot);
1159 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001160 } else if (HasUse && HasDef &&
1161 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1162 // If this interval hasn't been assigned a stack slot (because earlier
1163 // def is a deleted remat def), do it now.
1164 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1165 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001166 }
1167
Evan Cheng313d4b82008-02-23 00:33:04 +00001168 // Re-matting an instruction with virtual register use. Add the
1169 // register as an implicit use on the use MI.
1170 if (DefIsReMat && ImpUse)
1171 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1172
Evan Cheng5b69eba2009-04-21 22:46:52 +00001173 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001174 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001175 if (CreatedNewVReg) {
1176 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001177 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001178 if (TrySplit)
1179 vrm.setIsSplitFromReg(NewVReg, li.reg);
1180 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001181
1182 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001183 if (CreatedNewVReg) {
Lang Hames233a60e2009-11-03 23:52:08 +00001184 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1185 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001186 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001187 nI.addRange(LR);
1188 } else {
1189 // Extend the split live interval to this def / use.
Lang Hames233a60e2009-11-03 23:52:08 +00001190 SlotIndex End = index.getDefIndex();
Evan Cheng81a03822007-11-17 00:40:40 +00001191 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1192 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001193 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001194 nI.addRange(LR);
1195 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001196 }
1197 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001198 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1199 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001200 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001201 nI.addRange(LR);
1202 }
Evan Cheng81a03822007-11-17 00:40:40 +00001203
Bill Wendling8e6179f2009-08-22 20:18:03 +00001204 DEBUG({
1205 errs() << "\t\t\t\tAdded new interval: ";
1206 nI.print(errs(), tri_);
1207 errs() << '\n';
1208 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001209 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001210 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001211}
Evan Cheng81a03822007-11-17 00:40:40 +00001212bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001213 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001214 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +00001215 SlotIndex Idx) const {
1216 SlotIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001217 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames233a60e2009-11-03 23:52:08 +00001218 if (VNI->kills[j].isPHI())
Lang Hamesffd13262009-07-09 03:57:02 +00001219 continue;
1220
Lang Hames233a60e2009-11-03 23:52:08 +00001221 SlotIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001222 if (KillIdx > Idx && KillIdx < End)
1223 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001224 }
1225 return false;
1226}
1227
Evan Cheng063284c2008-02-21 00:34:19 +00001228/// RewriteInfo - Keep track of machine instrs that will be rewritten
1229/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001230namespace {
1231 struct RewriteInfo {
Lang Hames233a60e2009-11-03 23:52:08 +00001232 SlotIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001233 MachineInstr *MI;
1234 bool HasUse;
1235 bool HasDef;
Lang Hames233a60e2009-11-03 23:52:08 +00001236 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001237 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1238 };
Evan Cheng063284c2008-02-21 00:34:19 +00001239
Dan Gohman844731a2008-05-13 00:00:25 +00001240 struct RewriteInfoCompare {
1241 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1242 return LHS.Index < RHS.Index;
1243 }
1244 };
1245}
Evan Cheng063284c2008-02-21 00:34:19 +00001246
Evan Chengf2fbca62007-11-12 06:35:08 +00001247void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001248rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001249 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001250 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001251 unsigned Slot, int LdSlot,
1252 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001253 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001254 const TargetRegisterClass* rc,
1255 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001256 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001257 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001258 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001259 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001260 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1261 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001262 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001263 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001264 unsigned NewVReg = 0;
Lang Hames233a60e2009-11-03 23:52:08 +00001265 SlotIndex start = I->start.getBaseIndex();
1266 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001267
Evan Cheng063284c2008-02-21 00:34:19 +00001268 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001269 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001270 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001271 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1272 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001273 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001274 MachineOperand &O = ri.getOperand();
1275 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001276 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames233a60e2009-11-03 23:52:08 +00001277 SlotIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001278 if (index < start || index >= end)
1279 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001280
1281 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001282 // Must be defined by an implicit def. It should not be spilled. Note,
1283 // this is for correctness reason. e.g.
1284 // 8 %reg1024<def> = IMPLICIT_DEF
1285 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1286 // The live range [12, 14) are not part of the r1024 live interval since
1287 // it's defined by an implicit def. It will not conflicts with live
1288 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001289 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001290 // the INSERT_SUBREG and both target registers that would overlap.
1291 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001292 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1293 }
1294 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1295
Evan Cheng313d4b82008-02-23 00:33:04 +00001296 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001297 // Now rewrite the defs and uses.
1298 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1299 RewriteInfo &rwi = RewriteMIs[i];
1300 ++i;
Lang Hames233a60e2009-11-03 23:52:08 +00001301 SlotIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001302 bool MIHasUse = rwi.HasUse;
1303 bool MIHasDef = rwi.HasDef;
1304 MachineInstr *MI = rwi.MI;
1305 // If MI def and/or use the same register multiple times, then there
1306 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001307 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001308 while (i != e && RewriteMIs[i].MI == MI) {
1309 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001310 bool isUse = RewriteMIs[i].HasUse;
1311 if (isUse) ++NumUses;
1312 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001313 MIHasDef |= RewriteMIs[i].HasDef;
1314 ++i;
1315 }
Evan Cheng81a03822007-11-17 00:40:40 +00001316 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001317
Evan Cheng0a891ed2008-05-23 23:00:04 +00001318 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001319 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001320 // register interval's spill weight to HUGE_VALF to prevent it from
1321 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001322 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001323 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001324 }
1325
Evan Cheng063284c2008-02-21 00:34:19 +00001326 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001327 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001328 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001329 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001330 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001331 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001332 // One common case:
1333 // x = use
1334 // ...
1335 // ...
1336 // def = ...
1337 // = use
1338 // It's better to start a new interval to avoid artifically
1339 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001340 if (MIHasDef && !MIHasUse) {
1341 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001342 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001343 }
1344 }
Evan Chengcada2452007-11-28 01:28:46 +00001345 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001346
1347 bool IsNew = ThisVReg == 0;
1348 if (IsNew) {
1349 // This ends the previous live interval. If all of its def / use
1350 // can be folded, give it a low spill weight.
1351 if (NewVReg && TrySplit && AllCanFold) {
1352 LiveInterval &nI = getOrCreateInterval(NewVReg);
1353 nI.weight /= 10.0F;
1354 }
1355 AllCanFold = true;
1356 }
1357 NewVReg = ThisVReg;
1358
Evan Cheng81a03822007-11-17 00:40:40 +00001359 bool HasDef = false;
1360 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001361 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001362 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1363 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1364 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001365 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001366 if (!HasDef && !HasUse)
1367 continue;
1368
Evan Cheng018f9b02007-12-05 03:22:34 +00001369 AllCanFold &= CanFold;
1370
Evan Cheng81a03822007-11-17 00:40:40 +00001371 // Update weight of spill interval.
1372 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001373 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001374 // The spill weight is now infinity as it cannot be spilled again.
1375 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001376 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001377 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001378
1379 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001380 if (HasDef) {
1381 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001382 bool HasKill = false;
1383 if (!HasUse)
Lang Hames233a60e2009-11-03 23:52:08 +00001384 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001385 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001386 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames233a60e2009-11-03 23:52:08 +00001387 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001388 if (VNI)
Lang Hames233a60e2009-11-03 23:52:08 +00001389 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001390 }
Owen Anderson28998312008-08-13 22:28:50 +00001391 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001392 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001393 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001394 if (SII == SpillIdxes.end()) {
1395 std::vector<SRInfo> S;
1396 S.push_back(SRInfo(index, NewVReg, true));
1397 SpillIdxes.insert(std::make_pair(MBBId, S));
1398 } else if (SII->second.back().vreg != NewVReg) {
1399 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001400 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001401 // If there is an earlier def and this is a two-address
1402 // instruction, then it's not possible to fold the store (which
1403 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001404 SRInfo &Info = SII->second.back();
1405 Info.index = index;
1406 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001407 }
1408 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001409 } else if (SII != SpillIdxes.end() &&
1410 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001411 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001412 // There is an earlier def that's not killed (must be two-address).
1413 // The spill is no longer needed.
1414 SII->second.pop_back();
1415 if (SII->second.empty()) {
1416 SpillIdxes.erase(MBBId);
1417 SpillMBBs.reset(MBBId);
1418 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001419 }
1420 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001421 }
1422
1423 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001424 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001425 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001426 if (SII != SpillIdxes.end() &&
1427 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001428 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001429 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001430 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001431 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001432 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001433 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001434 // If we are splitting live intervals, only fold if it's the first
1435 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001436 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001437 else if (IsNew) {
1438 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001439 if (RII == RestoreIdxes.end()) {
1440 std::vector<SRInfo> Infos;
1441 Infos.push_back(SRInfo(index, NewVReg, true));
1442 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1443 } else {
1444 RII->second.push_back(SRInfo(index, NewVReg, true));
1445 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001446 RestoreMBBs.set(MBBId);
1447 }
1448 }
1449
1450 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001451 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001452 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001453 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001454
1455 if (NewVReg && TrySplit && AllCanFold) {
1456 // If all of its def / use can be folded, give it a low spill weight.
1457 LiveInterval &nI = getOrCreateInterval(NewVReg);
1458 nI.weight /= 10.0F;
1459 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001460}
1461
Lang Hames233a60e2009-11-03 23:52:08 +00001462bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001463 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001464 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001465 if (!RestoreMBBs[Id])
1466 return false;
1467 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1468 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1469 if (Restores[i].index == index &&
1470 Restores[i].vreg == vr &&
1471 Restores[i].canFold)
1472 return true;
1473 return false;
1474}
1475
Lang Hames233a60e2009-11-03 23:52:08 +00001476void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001477 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001478 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001479 if (!RestoreMBBs[Id])
1480 return;
1481 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1482 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1483 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames233a60e2009-11-03 23:52:08 +00001484 Restores[i].index = SlotIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00001485}
Evan Cheng81a03822007-11-17 00:40:40 +00001486
Evan Cheng4cce6b42008-04-11 17:53:36 +00001487/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1488/// spilled and create empty intervals for their uses.
1489void
1490LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1491 const TargetRegisterClass* rc,
1492 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001493 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1494 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001495 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001496 MachineInstr *MI = &*ri;
1497 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001498 if (O.isDef()) {
1499 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1500 "Register def was not rewritten?");
1501 RemoveMachineInstrFromMaps(MI);
1502 vrm.RemoveMachineInstrFromMaps(MI);
1503 MI->eraseFromParent();
1504 } else {
1505 // This must be an use of an implicit_def so it's not part of the live
1506 // interval. Create a new empty live interval for it.
1507 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1508 unsigned NewVReg = mri_->createVirtualRegister(rc);
1509 vrm.grow();
1510 vrm.setIsImplicitlyDefined(NewVReg);
1511 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1512 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1513 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001514 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001515 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001516 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001517 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001518 }
1519 }
Evan Cheng419852c2008-04-03 16:39:43 +00001520 }
1521}
1522
Evan Chengf2fbca62007-11-12 06:35:08 +00001523std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001524addIntervalsForSpillsFast(const LiveInterval &li,
1525 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001526 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001527 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001528
1529 std::vector<LiveInterval*> added;
1530
1531 assert(li.weight != HUGE_VALF &&
1532 "attempt to spill already spilled interval!");
1533
Bill Wendling8e6179f2009-08-22 20:18:03 +00001534 DEBUG({
1535 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1536 li.dump();
1537 errs() << '\n';
1538 });
Owen Andersond6664312008-08-18 18:05:32 +00001539
1540 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1541
Owen Andersona41e47a2008-08-19 22:12:11 +00001542 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1543 while (RI != mri_->reg_end()) {
1544 MachineInstr* MI = &*RI;
1545
1546 SmallVector<unsigned, 2> Indices;
1547 bool HasUse = false;
1548 bool HasDef = false;
1549
1550 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1551 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001552 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001553
1554 HasUse |= MI->getOperand(i).isUse();
1555 HasDef |= MI->getOperand(i).isDef();
1556
1557 Indices.push_back(i);
1558 }
1559
1560 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1561 Indices, true, slot, li.reg)) {
1562 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00001563 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00001564 vrm.assignVirt2StackSlot(NewVReg, slot);
1565
Owen Andersona41e47a2008-08-19 22:12:11 +00001566 // create a new register for this spill
1567 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00001568
Owen Andersona41e47a2008-08-19 22:12:11 +00001569 // the spill weight is now infinity as it
1570 // cannot be spilled again
1571 nI.weight = HUGE_VALF;
1572
1573 // Rewrite register operands to use the new vreg.
1574 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1575 E = Indices.end(); I != E; ++I) {
1576 MI->getOperand(*I).setReg(NewVReg);
1577
1578 if (MI->getOperand(*I).isUse())
1579 MI->getOperand(*I).setIsKill(true);
1580 }
1581
1582 // Fill in the new live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001583 SlotIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00001584 if (HasUse) {
Lang Hames233a60e2009-11-03 23:52:08 +00001585 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1586 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001587 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001588 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001589 nI.addRange(LR);
1590 vrm.addRestorePoint(NewVReg, MI);
1591 }
1592 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001593 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1594 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001595 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001596 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001597 nI.addRange(LR);
1598 vrm.addSpillPoint(NewVReg, true, MI);
1599 }
1600
Owen Anderson17197312008-08-18 23:41:04 +00001601 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00001602
Bill Wendling8e6179f2009-08-22 20:18:03 +00001603 DEBUG({
1604 errs() << "\t\t\t\tadded new interval: ";
1605 nI.dump();
1606 errs() << '\n';
1607 });
Owen Andersona41e47a2008-08-19 22:12:11 +00001608 }
Owen Anderson9a032932008-08-18 21:20:32 +00001609
Owen Anderson9a032932008-08-18 21:20:32 +00001610
Owen Andersona41e47a2008-08-19 22:12:11 +00001611 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001612 }
Owen Andersond6664312008-08-18 18:05:32 +00001613
1614 return added;
1615}
1616
1617std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001618addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00001619 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00001620 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00001621
1622 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00001623 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00001624
Evan Chengf2fbca62007-11-12 06:35:08 +00001625 assert(li.weight != HUGE_VALF &&
1626 "attempt to spill already spilled interval!");
1627
Bill Wendling8e6179f2009-08-22 20:18:03 +00001628 DEBUG({
1629 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1630 li.print(errs(), tri_);
1631 errs() << '\n';
1632 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001633
Evan Cheng72eeb942008-12-05 17:00:16 +00001634 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00001635 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001636 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001637 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001638 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1639 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001640 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001641 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001642
1643 unsigned NumValNums = li.getNumValNums();
1644 SmallVector<MachineInstr*, 4> ReMatDefs;
1645 ReMatDefs.resize(NumValNums, NULL);
1646 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1647 ReMatOrigDefs.resize(NumValNums, NULL);
1648 SmallVector<int, 4> ReMatIds;
1649 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1650 BitVector ReMatDelete(NumValNums);
1651 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1652
Evan Cheng81a03822007-11-17 00:40:40 +00001653 // Spilling a split live interval. It cannot be split any further. Also,
1654 // it's also guaranteed to be a single val# / range interval.
1655 if (vrm.getPreSplitReg(li.reg)) {
1656 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001657 // Unset the split kill marker on the last use.
Lang Hames233a60e2009-11-03 23:52:08 +00001658 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1659 if (KillIdx != SlotIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00001660 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1661 assert(KillMI && "Last use disappeared?");
1662 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1663 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001664 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001665 }
Evan Chengadf85902007-12-05 09:51:10 +00001666 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001667 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1668 Slot = vrm.getStackSlot(li.reg);
1669 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1670 MachineInstr *ReMatDefMI = DefIsReMat ?
1671 vrm.getReMaterializedMI(li.reg) : NULL;
1672 int LdSlot = 0;
1673 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1674 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001675 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001676 bool IsFirstRange = true;
1677 for (LiveInterval::Ranges::const_iterator
1678 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1679 // If this is a split live interval with multiple ranges, it means there
1680 // are two-address instructions that re-defined the value. Only the
1681 // first def can be rematerialized!
1682 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001683 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001684 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1685 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001686 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001687 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001688 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001689 } else {
1690 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1691 Slot, 0, false, false, false,
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 }
1696 IsFirstRange = false;
1697 }
Evan Cheng419852c2008-04-03 16:39:43 +00001698
Evan Cheng4cce6b42008-04-11 17:53:36 +00001699 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001700 return NewLIs;
1701 }
1702
Evan Cheng752195e2009-09-14 21:33:42 +00001703 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001704 if (TrySplit)
1705 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001706 bool NeedStackSlot = false;
1707 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1708 i != e; ++i) {
1709 const VNInfo *VNI = *i;
1710 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00001711 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00001712 continue; // Dead val#.
1713 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001714 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1715 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001716 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00001717 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001718 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001719 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001720 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001721 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00001722 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00001723 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001724
1725 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00001726 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00001727 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001728 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001729 CanDelete = false;
1730 // Need a stack slot if there is any live range where uses cannot be
1731 // rematerialized.
1732 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001733 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001734 if (CanDelete)
1735 ReMatDelete.set(VN);
1736 } else {
1737 // Need a stack slot if there is any live range where uses cannot be
1738 // rematerialized.
1739 NeedStackSlot = true;
1740 }
1741 }
1742
1743 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00001744 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1745 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1746 Slot = vrm.assignVirt2StackSlot(li.reg);
1747
1748 // This case only occurs when the prealloc splitter has already assigned
1749 // a stack slot to this vreg.
1750 else
1751 Slot = vrm.getStackSlot(li.reg);
1752 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001753
1754 // Create new intervals and rewrite defs and uses.
1755 for (LiveInterval::Ranges::const_iterator
1756 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001757 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1758 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1759 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001760 bool CanDelete = ReMatDelete[I->valno->id];
1761 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001762 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001763 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001764 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001765 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001766 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001767 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001768 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001769 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001770 }
1771
Evan Cheng0cbb1162007-11-29 01:06:25 +00001772 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001773 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001774 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001775 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001776 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001777
Evan Chengb50bb8c2007-12-05 08:16:32 +00001778 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001779 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001780 if (NeedStackSlot) {
1781 int Id = SpillMBBs.find_first();
1782 while (Id != -1) {
1783 std::vector<SRInfo> &spills = SpillIdxes[Id];
1784 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001785 SlotIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001786 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001787 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001788 bool isReMat = vrm.isReMaterialized(VReg);
1789 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001790 bool CanFold = false;
1791 bool FoundUse = false;
1792 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001793 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001794 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001795 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1796 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001797 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001798 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001799
1800 Ops.push_back(j);
1801 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001802 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001803 if (isReMat ||
1804 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1805 RestoreMBBs, RestoreIdxes))) {
1806 // MI has two-address uses of the same register. If the use
1807 // isn't the first and only use in the BB, then we can't fold
1808 // it. FIXME: Move this to rewriteInstructionsForSpills.
1809 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001810 break;
1811 }
Evan Chengaee4af62007-12-02 08:30:39 +00001812 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001813 }
1814 }
1815 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001816 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001817 if (CanFold && !Ops.empty()) {
1818 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001819 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00001820 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00001821 // Also folded uses, do not issue a load.
1822 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +00001823 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengf38d14f2007-12-05 09:05:34 +00001824 }
Lang Hames233a60e2009-11-03 23:52:08 +00001825 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
Evan Chengcddbb832007-11-30 21:23:43 +00001826 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001827 }
1828
Evan Cheng7e073ba2008-04-09 20:57:25 +00001829 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001830 if (!Folded) {
1831 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001832 bool isKill = LR->end == index.getStoreIndex();
Evan Chengb0a6f622008-05-20 08:10:37 +00001833 if (!MI->registerDefIsDead(nI.reg))
1834 // No need to spill a dead def.
1835 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001836 if (isKill)
1837 AddedKill.insert(&nI);
1838 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001839 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001840 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001841 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001842 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001843
Evan Cheng1953d0c2007-11-29 10:12:14 +00001844 int Id = RestoreMBBs.find_first();
1845 while (Id != -1) {
1846 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1847 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001848 SlotIndex index = restores[i].index;
1849 if (index == SlotIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00001850 continue;
1851 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001852 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001853 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001854 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001855 bool CanFold = false;
1856 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001857 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001858 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001859 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1860 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001861 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00001862 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001863
Evan Cheng0cbb1162007-11-29 01:06:25 +00001864 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001865 // If this restore were to be folded, it would have been folded
1866 // already.
1867 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001868 break;
1869 }
Evan Chengaee4af62007-12-02 08:30:39 +00001870 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001871 }
1872 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001873
1874 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001875 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001876 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001877 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001878 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1879 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001880 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1881 int LdSlot = 0;
1882 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1883 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00001884 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001885 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1886 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00001887 if (!Folded) {
1888 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1889 if (ImpUse) {
1890 // Re-matting an instruction with virtual register use. Add the
1891 // register as an implicit use on the use MI and update the register
1892 // interval's spill weight to HUGE_VALF to prevent it from being
1893 // spilled.
1894 LiveInterval &ImpLi = getInterval(ImpUse);
1895 ImpLi.weight = HUGE_VALF;
1896 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1897 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001898 }
Evan Chengaee4af62007-12-02 08:30:39 +00001899 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001900 }
1901 // If folding is not possible / failed, then tell the spiller to issue a
1902 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001903 if (Folded)
Lang Hames233a60e2009-11-03 23:52:08 +00001904 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengb50bb8c2007-12-05 08:16:32 +00001905 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001906 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001907 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001908 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001909 }
1910
Evan Chengb50bb8c2007-12-05 08:16:32 +00001911 // Finalize intervals: add kills, finalize spill weights, and filter out
1912 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001913 std::vector<LiveInterval*> RetNewLIs;
1914 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1915 LiveInterval *LI = NewLIs[i];
1916 if (!LI->empty()) {
Lang Hames233a60e2009-11-03 23:52:08 +00001917 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001918 if (!AddedKill.count(LI)) {
1919 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001920 SlotIndex LastUseIdx = LR->end.getBaseIndex();
Evan Chengd120ffd2007-12-05 10:24:35 +00001921 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001922 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001923 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00001924 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001925 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001926 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001927 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001928 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001929 RetNewLIs.push_back(LI);
1930 }
1931 }
Evan Cheng81a03822007-11-17 00:40:40 +00001932
Evan Cheng4cce6b42008-04-11 17:53:36 +00001933 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001934 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001935}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001936
1937/// hasAllocatableSuperReg - Return true if the specified physical register has
1938/// any super register that's allocatable.
1939bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1940 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1941 if (allocatableRegs_[*AS] && hasInterval(*AS))
1942 return true;
1943 return false;
1944}
1945
1946/// getRepresentativeReg - Find the largest super register of the specified
1947/// physical register.
1948unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1949 // Find the largest super-register that is allocatable.
1950 unsigned BestReg = Reg;
1951 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1952 unsigned SuperReg = *AS;
1953 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
1954 BestReg = SuperReg;
1955 break;
1956 }
1957 }
1958 return BestReg;
1959}
1960
1961/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
1962/// specified interval that conflicts with the specified physical register.
1963unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
1964 unsigned PhysReg) const {
1965 unsigned NumConflicts = 0;
1966 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
1967 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
1968 E = mri_->reg_end(); I != E; ++I) {
1969 MachineOperand &O = I.getOperand();
1970 MachineInstr *MI = O.getParent();
Lang Hames233a60e2009-11-03 23:52:08 +00001971 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00001972 if (pli.liveAt(Index))
1973 ++NumConflicts;
1974 }
1975 return NumConflicts;
1976}
1977
1978/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00001979/// around all defs and uses of the specified interval. Return true if it
1980/// was able to cut its interval.
1981bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00001982 unsigned PhysReg, VirtRegMap &vrm) {
1983 unsigned SpillReg = getRepresentativeReg(PhysReg);
1984
1985 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
1986 // If there are registers which alias PhysReg, but which are not a
1987 // sub-register of the chosen representative super register. Assert
1988 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00001989 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00001990 tri_->isSuperRegister(*AS, SpillReg));
1991
Evan Cheng2824a652009-03-23 18:24:37 +00001992 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00001993 SmallVector<unsigned, 4> PRegs;
1994 if (hasInterval(SpillReg))
1995 PRegs.push_back(SpillReg);
1996 else {
1997 SmallSet<unsigned, 4> Added;
1998 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
1999 if (Added.insert(*AS) && hasInterval(*AS)) {
2000 PRegs.push_back(*AS);
2001 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2002 Added.insert(*ASS);
2003 }
2004 }
2005
Evan Cheng676dd7c2008-03-11 07:19:34 +00002006 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2007 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2008 E = mri_->reg_end(); I != E; ++I) {
2009 MachineOperand &O = I.getOperand();
2010 MachineInstr *MI = O.getParent();
2011 if (SeenMIs.count(MI))
2012 continue;
2013 SeenMIs.insert(MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002014 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002015 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2016 unsigned PReg = PRegs[i];
2017 LiveInterval &pli = getInterval(PReg);
2018 if (!pli.liveAt(Index))
2019 continue;
2020 vrm.addEmergencySpill(PReg, MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002021 SlotIndex StartIdx = Index.getLoadIndex();
2022 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
Evan Cheng2824a652009-03-23 18:24:37 +00002023 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002024 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002025 Cut = true;
2026 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002027 std::string msg;
2028 raw_string_ostream Msg(msg);
2029 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002030 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002031 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002032 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002033 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002034 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002035 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002036 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002037 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002038 if (!hasInterval(*AS))
2039 continue;
2040 LiveInterval &spli = getInterval(*AS);
2041 if (spli.liveAt(Index))
Lang Hames233a60e2009-11-03 23:52:08 +00002042 spli.removeRange(Index.getLoadIndex(),
2043 Index.getNextIndex().getBaseIndex());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002044 }
2045 }
2046 }
Evan Cheng2824a652009-03-23 18:24:37 +00002047 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002048}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002049
2050LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002051 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002052 LiveInterval& Interval = getOrCreateInterval(reg);
2053 VNInfo* VN = Interval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +00002054 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames86511252009-09-04 20:41:11 +00002055 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002056 VN->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +00002057 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
Lang Hames86511252009-09-04 20:41:11 +00002058 LiveRange LR(
Lang Hames233a60e2009-11-03 23:52:08 +00002059 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2060 getMBBEndIdx(startInst->getParent()).getNextIndex().getBaseIndex(), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002061 Interval.addRange(LR);
2062
2063 return LR;
2064}
David Greeneb5257662009-08-03 21:55:09 +00002065