blob: f0baba341bb3733a28c31c188d361ca0aa552020 [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(),
Torok Edwin15417382010-03-24 13:50:36 +000088 E = r2iMap_.end(); I != E; ++I) {
89 I->second->clear();
Owen Anderson03857b22008-08-13 21:49:13 +000090 delete I->second;
Torok Edwin15417382010-03-24 13:50:36 +000091 }
Owen Anderson03857b22008-08-13 21:49:13 +000092
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000093 r2iMap_.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000094
Evan Chengdd199d22007-09-06 01:07:24 +000095 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
96 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +000097 while (!CloneMIs.empty()) {
98 MachineInstr *MI = CloneMIs.back();
99 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000100 mf_->DeleteMachineInstr(MI);
101 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000102}
103
Owen Anderson80b3ce62008-05-28 20:54:50 +0000104/// runOnMachineFunction - Register allocate the whole function
105///
106bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
107 mf_ = &fn;
108 mri_ = &mf_->getRegInfo();
109 tm_ = &fn.getTarget();
110 tri_ = tm_->getRegisterInfo();
111 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000112 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000113 lv_ = &getAnalysis<LiveVariables>();
Lang Hames233a60e2009-11-03 23:52:08 +0000114 indexes_ = &getAnalysis<SlotIndexes>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000115 allocatableRegs_ = tri_->getAllocatableSet(fn);
116
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000117 computeIntervals();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000118
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000119 numIntervals += getNumIntervals();
120
Chris Lattner70ca3582004-09-30 15:59:17 +0000121 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000123}
124
Chris Lattner70ca3582004-09-30 15:59:17 +0000125/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000126void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000127 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000128 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000129 I->second->print(OS, tri_);
130 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000131 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000132
Evan Cheng752195e2009-09-14 21:33:42 +0000133 printInstrs(OS);
134}
135
136void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000137 OS << "********** MACHINEINSTRS **********\n";
138
Chris Lattner3380d5c2009-07-21 21:12:58 +0000139 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
140 mbbi != mbbe; ++mbbi) {
Jakob Stoklund Olesen6cd81032009-11-20 18:54:59 +0000141 OS << "BB#" << mbbi->getNumber()
142 << ":\t\t# derived from " << mbbi->getName() << "\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000143 for (MachineBasicBlock::iterator mii = mbbi->begin(),
144 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner518bb532010-02-09 19:54:29 +0000145 if (mii->isDebugValue())
Evan Cheng4507f082010-03-16 21:51:27 +0000146 OS << " \t" << *mii;
Dale Johannesen1caedd02010-01-22 22:38:21 +0000147 else
148 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000149 }
150 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000151}
152
Evan Cheng752195e2009-09-14 21:33:42 +0000153void LiveIntervals::dumpInstrs() const {
David Greene8a342292010-01-04 22:49:02 +0000154 printInstrs(dbgs());
Evan Cheng752195e2009-09-14 21:33:42 +0000155}
156
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000157bool LiveIntervals::conflictsWithPhysReg(const LiveInterval &li,
158 VirtRegMap &vrm, unsigned reg) {
159 // We don't handle fancy stuff crossing basic block boundaries
160 if (li.ranges.size() != 1)
161 return true;
162 const LiveRange &range = li.ranges.front();
163 SlotIndex idx = range.start.getBaseIndex();
164 SlotIndex end = range.end.getPrevSlot().getBaseIndex().getNextIndex();
Jakob Stoklund Olesenf4811a92009-12-03 20:49:10 +0000165
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000166 // Skip deleted instructions
167 MachineInstr *firstMI = getInstructionFromIndex(idx);
168 while (!firstMI && idx != end) {
169 idx = idx.getNextIndex();
170 firstMI = getInstructionFromIndex(idx);
171 }
172 if (!firstMI)
173 return false;
174
175 // Find last instruction in range
176 SlotIndex lastIdx = end.getPrevIndex();
177 MachineInstr *lastMI = getInstructionFromIndex(lastIdx);
178 while (!lastMI && lastIdx != idx) {
179 lastIdx = lastIdx.getPrevIndex();
180 lastMI = getInstructionFromIndex(lastIdx);
181 }
182 if (!lastMI)
183 return false;
184
185 // Range cannot cross basic block boundaries or terminators
186 MachineBasicBlock *MBB = firstMI->getParent();
187 if (MBB != lastMI->getParent() || lastMI->getDesc().isTerminator())
188 return true;
189
190 MachineBasicBlock::const_iterator E = lastMI;
191 ++E;
192 for (MachineBasicBlock::const_iterator I = firstMI; I != E; ++I) {
193 const MachineInstr &MI = *I;
194
195 // Allow copies to and from li.reg
196 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
197 if (tii_->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
198 if (SrcReg == li.reg || DstReg == li.reg)
199 continue;
200
201 // Check for operands using reg
202 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
203 const MachineOperand& mop = MI.getOperand(i);
204 if (!mop.isReg())
205 continue;
206 unsigned PhysReg = mop.getReg();
207 if (PhysReg == 0 || PhysReg == li.reg)
208 continue;
209 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
210 if (!vrm.hasPhys(PhysReg))
Bill Wendlingdc492e02009-12-05 07:30:23 +0000211 continue;
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000212 PhysReg = vrm.getPhys(PhysReg);
Evan Chengc92da382007-11-03 07:20:12 +0000213 }
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000214 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
215 return true;
Evan Chengc92da382007-11-03 07:20:12 +0000216 }
217 }
218
Jakob Stoklund Olesencf970362009-12-10 17:48:32 +0000219 // No conflicts found.
Evan Chengc92da382007-11-03 07:20:12 +0000220 return false;
221}
222
Evan Cheng826cbac2010-03-11 08:20:21 +0000223/// conflictsWithSubPhysRegRef - Similar to conflictsWithPhysRegRef except
224/// it checks for sub-register reference and it can check use as well.
225bool LiveIntervals::conflictsWithSubPhysRegRef(LiveInterval &li,
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000226 unsigned Reg, bool CheckUse,
227 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
228 for (LiveInterval::Ranges::const_iterator
229 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000230 for (SlotIndex index = I->start.getBaseIndex(),
231 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
232 index != end;
233 index = index.getNextIndex()) {
Jakob Stoklund Olesenf4811a92009-12-03 20:49:10 +0000234 MachineInstr *MI = getInstructionFromIndex(index);
235 if (!MI)
236 continue; // skip deleted instructions
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000237
238 if (JoinedCopies.count(MI))
239 continue;
240 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
241 MachineOperand& MO = MI->getOperand(i);
242 if (!MO.isReg())
243 continue;
244 if (MO.isUse() && !CheckUse)
245 continue;
246 unsigned PhysReg = MO.getReg();
247 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
248 continue;
249 if (tri_->isSubRegister(Reg, PhysReg))
250 return true;
251 }
252 }
253 }
254
255 return false;
256}
257
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000258#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000259static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000260 if (TargetRegisterInfo::isPhysicalRegister(reg))
David Greene8a342292010-01-04 22:49:02 +0000261 dbgs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000262 else
David Greene8a342292010-01-04 22:49:02 +0000263 dbgs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000264}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000265#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000266
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000267void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000268 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000269 SlotIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000270 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000271 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000272 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000273 DEBUG({
David Greene8a342292010-01-04 22:49:02 +0000274 dbgs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000275 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000276 });
Evan Cheng419852c2008-04-03 16:39:43 +0000277
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000278 // Virtual registers may be defined multiple times (due to phi
279 // elimination and 2-addr elimination). Much of what we do only has to be
280 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000281 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000282 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 if (interval.empty()) {
284 // Get the Idx of the defining instructions.
Lang Hames233a60e2009-11-03 23:52:08 +0000285 SlotIndex defIndex = MIIdx.getDefIndex();
Dale Johannesen39faac22009-09-20 00:36:41 +0000286 // Earlyclobbers move back one, so that they overlap the live range
287 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000288 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000289 defIndex = MIIdx.getUseIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000290 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000291 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000292 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Chris Lattner518bb532010-02-09 19:54:29 +0000293 if (mi->isExtractSubreg() || mi->isInsertSubreg() || mi->isSubregToReg() ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000294 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000295 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000296 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000297 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000298
299 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000300
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000301 // Loop over all of the blocks that the vreg is defined in. There are
302 // two cases we have to handle here. The most common case is a vreg
303 // whose lifetime is contained within a basic block. In this case there
304 // will be a single kill, in MBB, which comes after the definition.
305 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
306 // FIXME: what about dead vars?
Lang Hames233a60e2009-11-03 23:52:08 +0000307 SlotIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000308 if (vi.Kills[0] != mi)
Lang Hames233a60e2009-11-03 23:52:08 +0000309 killIdx = getInstructionIndex(vi.Kills[0]).getDefIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000310 else
Lang Hames233a60e2009-11-03 23:52:08 +0000311 killIdx = defIndex.getStoreIndex();
Chris Lattner6097d132004-07-19 02:15:56 +0000312
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313 // If the kill happens after the definition, we have an intra-block
314 // live range.
315 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000316 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000318 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000319 interval.addRange(LR);
David Greene8a342292010-01-04 22:49:02 +0000320 DEBUG(dbgs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000321 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000322 return;
323 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000324 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000325
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326 // The other case we handle is when a virtual register lives to the end
327 // of the defining block, potentially live across some blocks, then is
328 // live into some number of blocks, but gets killed. Start by adding a
329 // range that goes from this definition to the end of the defining block.
Lang Hames74ab5ee2009-12-22 00:11:50 +0000330 LiveRange NewLR(defIndex, getMBBEndIdx(mbb), ValNo);
David Greene8a342292010-01-04 22:49:02 +0000331 DEBUG(dbgs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000332 interval.addRange(NewLR);
333
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000334 bool PHIJoin = lv_->isPHIJoin(interval.reg);
335
336 if (PHIJoin) {
337 // A phi join register is killed at the end of the MBB and revived as a new
338 // valno in the killing blocks.
339 assert(vi.AliveBlocks.empty() && "Phi join can't pass through blocks");
340 DEBUG(dbgs() << " phi-join");
341 ValNo->addKill(indexes_->getTerminatorGap(mbb));
342 ValNo->setHasPHIKill(true);
343 } else {
344 // Iterate over all of the blocks that the variable is completely
345 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
346 // live interval.
347 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
348 E = vi.AliveBlocks.end(); I != E; ++I) {
349 MachineBasicBlock *aliveBlock = mf_->getBlockNumbered(*I);
350 LiveRange LR(getMBBStartIdx(aliveBlock), getMBBEndIdx(aliveBlock), ValNo);
351 interval.addRange(LR);
352 DEBUG(dbgs() << " +" << LR);
353 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000354 }
355
356 // Finally, this virtual register is live from the start of any killing
357 // block to the 'use' slot of the killing instruction.
358 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
359 MachineInstr *Kill = vi.Kills[i];
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000360 SlotIndex Start = getMBBStartIdx(Kill->getParent());
361 SlotIndex killIdx = getInstructionIndex(Kill).getDefIndex();
362
363 // Create interval with one of a NEW value number. Note that this value
364 // number isn't actually defined by an instruction, weird huh? :)
365 if (PHIJoin) {
366 ValNo = interval.getNextValue(SlotIndex(Start, true), 0, false,
367 VNInfoAllocator);
368 ValNo->setIsPHIDef(true);
369 }
370 LiveRange LR(Start, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000372 ValNo->addKill(killIdx);
David Greene8a342292010-01-04 22:49:02 +0000373 DEBUG(dbgs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 }
375
376 } else {
377 // If this is the second time we see a virtual register definition, it
378 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000379 // the result of two address elimination, then the vreg is one of the
380 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000381 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000382 // If this is a two-address definition, then we have already processed
383 // the live range. The only problem is that we didn't realize there
384 // are actually two values in the live interval. Because of this we
385 // need to take the LiveRegion that defines this register and split it
386 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000387 assert(interval.containsOneValue());
Lang Hames233a60e2009-11-03 23:52:08 +0000388 SlotIndex DefIndex = interval.getValNumInfo(0)->def.getDefIndex();
389 SlotIndex RedefIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000390 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000391 RedefIndex = MIIdx.getUseIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000392
Lang Hames35f291d2009-09-12 03:34:03 +0000393 const LiveRange *OldLR =
Lang Hames233a60e2009-11-03 23:52:08 +0000394 interval.getLiveRangeContaining(RedefIndex.getUseIndex());
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000395 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000396
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000397 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000398 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000399 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000400
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000401 // Two-address vregs should always only be redefined once. This means
402 // that at this point, there should be exactly one value number in it.
403 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
404
Chris Lattner91725b72006-08-31 05:54:43 +0000405 // The new value number (#1) is defined by the instruction we claimed
406 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000407 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000408 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000409 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000410 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
411
Chris Lattner91725b72006-08-31 05:54:43 +0000412 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000413 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000414 OldValNo->setCopy(0);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000415
416 // Add the new live interval which replaces the range for the input copy.
417 LiveRange LR(DefIndex, RedefIndex, ValNo);
David Greene8a342292010-01-04 22:49:02 +0000418 DEBUG(dbgs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000419 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000420 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000421
422 // If this redefinition is dead, we need to add a dummy unit live
423 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000424 if (MO.isDead())
Lang Hames233a60e2009-11-03 23:52:08 +0000425 interval.addRange(LiveRange(RedefIndex, RedefIndex.getStoreIndex(),
426 OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427
Bill Wendling8e6179f2009-08-22 20:18:03 +0000428 DEBUG({
David Greene8a342292010-01-04 22:49:02 +0000429 dbgs() << " RESULT: ";
430 interval.print(dbgs(), tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000431 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000432 } else {
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000433 assert(lv_->isPHIJoin(interval.reg) && "Multiply defined register");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000434 // In the case of PHI elimination, each variable definition is only
435 // live until the end of the block. We've already taken care of the
436 // rest of the live range.
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000437
Lang Hames233a60e2009-11-03 23:52:08 +0000438 SlotIndex defIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000439 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000440 defIndex = MIIdx.getUseIndex();
Evan Cheng752195e2009-09-14 21:33:42 +0000441
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000442 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000443 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000444 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Chris Lattner518bb532010-02-09 19:54:29 +0000445 if (mi->isExtractSubreg() || mi->isInsertSubreg() || mi->isSubregToReg()||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000446 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000447 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000448 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000449
Lang Hames74ab5ee2009-12-22 00:11:50 +0000450 SlotIndex killIndex = getMBBEndIdx(mbb);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000451 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000452 interval.addRange(LR);
Lang Hames233a60e2009-11-03 23:52:08 +0000453 ValNo->addKill(indexes_->getTerminatorGap(mbb));
Lang Hames857c4e02009-06-17 21:01:20 +0000454 ValNo->setHasPHIKill(true);
Jakob Stoklund Olesendcfe5f32010-02-23 22:43:58 +0000455 DEBUG(dbgs() << " phi-join +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000456 }
457 }
458
David Greene8a342292010-01-04 22:49:02 +0000459 DEBUG(dbgs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000460}
461
Chris Lattnerf35fef72004-07-23 21:24:19 +0000462void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000463 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000464 SlotIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000465 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000466 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000467 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000468 // A physical register cannot be live across basic block, so its
469 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000470 DEBUG({
David Greene8a342292010-01-04 22:49:02 +0000471 dbgs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000472 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000473 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000474
Lang Hames233a60e2009-11-03 23:52:08 +0000475 SlotIndex baseIndex = MIIdx;
476 SlotIndex start = baseIndex.getDefIndex();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000477 // Earlyclobbers move back one.
478 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000479 start = MIIdx.getUseIndex();
480 SlotIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000481
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000482 // If it is not used after definition, it is considered dead at
483 // the instruction defining it. Hence its interval is:
484 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000485 // For earlyclobbers, the defSlot was pushed back one; the extra
486 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000487 if (MO.isDead()) {
David Greene8a342292010-01-04 22:49:02 +0000488 DEBUG(dbgs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000489 end = start.getStoreIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000490 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000491 }
492
493 // If it is not dead on definition, it must be killed by a
494 // subsequent instruction. Hence its interval is:
495 // [defSlot(def), useSlot(kill)+1)
Lang Hames233a60e2009-11-03 23:52:08 +0000496 baseIndex = baseIndex.getNextIndex();
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000497 while (++mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000498
Dale Johannesenbd635202010-02-10 00:55:42 +0000499 if (mi->isDebugValue())
500 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000501 if (getInstructionFromIndex(baseIndex) == 0)
502 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
503
Evan Cheng6130f662008-03-05 00:59:57 +0000504 if (mi->killsRegister(interval.reg, tri_)) {
David Greene8a342292010-01-04 22:49:02 +0000505 DEBUG(dbgs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000506 end = baseIndex.getDefIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000507 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000508 } else {
509 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
510 if (DefIdx != -1) {
511 if (mi->isRegTiedToUseOperand(DefIdx)) {
512 // Two-address instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000513 end = baseIndex.getDefIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000514 } else {
515 // Another instruction redefines the register before it is ever read.
Dale Johannesenbd635202010-02-10 00:55:42 +0000516 // Then the register is essentially dead at the instruction that
517 // defines it. Hence its interval is:
Evan Chengc45288e2009-04-27 20:42:46 +0000518 // [defSlot(def), defSlot(def)+1)
David Greene8a342292010-01-04 22:49:02 +0000519 DEBUG(dbgs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000520 end = start.getStoreIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000521 }
522 goto exit;
523 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000524 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000525
Lang Hames233a60e2009-11-03 23:52:08 +0000526 baseIndex = baseIndex.getNextIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000527 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000528
529 // The only case we should have a dead physreg here without a killing or
530 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000531 // and never used. Another possible case is the implicit use of the
532 // physical register has been deleted by two-address pass.
Lang Hames233a60e2009-11-03 23:52:08 +0000533 end = start.getStoreIndex();
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000534
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000535exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000537
Evan Cheng24a3cc42007-04-25 07:30:23 +0000538 // Already exists? Extend old live interval.
539 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000540 bool Extend = OldLR != interval.end();
541 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000542 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000543 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000544 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000545 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000547 LR.valno->addKill(end);
David Greene8a342292010-01-04 22:49:02 +0000548 DEBUG(dbgs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000549}
550
Chris Lattnerf35fef72004-07-23 21:24:19 +0000551void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
552 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000553 SlotIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000554 MachineOperand& MO,
555 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000556 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000557 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000558 getOrCreateInterval(MO.getReg()));
559 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000560 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000561 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Chris Lattner518bb532010-02-09 19:54:29 +0000562 if (MI->isExtractSubreg() || MI->isInsertSubreg() || MI->isSubregToReg() ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000563 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000564 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000565 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000566 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000567 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000568 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000569 // If MI also modifies the sub-register explicitly, avoid processing it
570 // more than once. Do not pass in TRI here so it checks for exact match.
571 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000572 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000573 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000574 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000575}
576
Evan Chengb371f452007-02-19 21:49:54 +0000577void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000578 SlotIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000579 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000580 DEBUG({
David Greene8a342292010-01-04 22:49:02 +0000581 dbgs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000582 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000583 });
Evan Chengb371f452007-02-19 21:49:54 +0000584
585 // Look for kills, if it reaches a def before it's killed, then it shouldn't
586 // be considered a livein.
587 MachineBasicBlock::iterator mi = MBB->begin();
Evan Cheng4507f082010-03-16 21:51:27 +0000588 MachineBasicBlock::iterator E = MBB->end();
589 // Skip over DBG_VALUE at the start of the MBB.
590 if (mi != E && mi->isDebugValue()) {
591 while (++mi != E && mi->isDebugValue())
592 ;
593 if (mi == E)
594 // MBB is empty except for DBG_VALUE's.
595 return;
596 }
597
Lang Hames233a60e2009-11-03 23:52:08 +0000598 SlotIndex baseIndex = MIIdx;
599 SlotIndex start = baseIndex;
600 if (getInstructionFromIndex(baseIndex) == 0)
601 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
602
603 SlotIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000604 bool SeenDefUse = false;
Evan Chengb371f452007-02-19 21:49:54 +0000605
Dale Johannesenbd635202010-02-10 00:55:42 +0000606 while (mi != E) {
Dale Johannesen1d0aeab2010-02-10 01:31:26 +0000607 if (mi->killsRegister(interval.reg, tri_)) {
608 DEBUG(dbgs() << " killed");
609 end = baseIndex.getDefIndex();
610 SeenDefUse = true;
611 break;
612 } else if (mi->modifiesRegister(interval.reg, tri_)) {
613 // Another instruction redefines the register before it is ever read.
614 // Then the register is essentially dead at the instruction that defines
615 // it. Hence its interval is:
616 // [defSlot(def), defSlot(def)+1)
617 DEBUG(dbgs() << " dead");
618 end = start.getStoreIndex();
619 SeenDefUse = true;
620 break;
621 }
622
Evan Cheng4507f082010-03-16 21:51:27 +0000623 while (++mi != E && mi->isDebugValue())
624 // Skip over DBG_VALUE.
625 ;
626 if (mi != E)
Lang Hames233a60e2009-11-03 23:52:08 +0000627 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
Evan Chengb371f452007-02-19 21:49:54 +0000628 }
629
Evan Cheng75611fb2007-06-27 01:16:36 +0000630 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000631 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000632 if (isAlias) {
David Greene8a342292010-01-04 22:49:02 +0000633 DEBUG(dbgs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000634 end = MIIdx.getStoreIndex();
Evan Cheng292da942007-06-27 18:47:28 +0000635 } else {
David Greene8a342292010-01-04 22:49:02 +0000636 DEBUG(dbgs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +0000637 end = baseIndex;
638 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000639 }
640
Lang Hames10382fb2009-06-19 02:17:53 +0000641 VNInfo *vni =
Lang Hames233a60e2009-11-03 23:52:08 +0000642 interval.getNextValue(SlotIndex(getMBBStartIdx(MBB), true),
Lang Hames86511252009-09-04 20:41:11 +0000643 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000644 vni->setIsPHIDef(true);
645 LiveRange LR(start, end, vni);
Jakob Stoklund Olesen3de23e62009-11-07 01:58:40 +0000646
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000647 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000648 LR.valno->addKill(end);
David Greene8a342292010-01-04 22:49:02 +0000649 DEBUG(dbgs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +0000650}
651
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000652/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000653/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000654/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000655/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000656void LiveIntervals::computeIntervals() {
David Greene8a342292010-01-04 22:49:02 +0000657 DEBUG(dbgs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +0000658 << "********** Function: "
659 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +0000660
661 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000662 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
663 MBBI != E; ++MBBI) {
664 MachineBasicBlock *MBB = MBBI;
Evan Cheng00a99a32010-02-06 09:07:11 +0000665 if (MBB->empty())
666 continue;
667
Owen Anderson134eb732008-09-21 20:43:24 +0000668 // Track the index of the current machine instr.
Lang Hames233a60e2009-11-03 23:52:08 +0000669 SlotIndex MIIndex = getMBBStartIdx(MBB);
David Greene8a342292010-01-04 22:49:02 +0000670 DEBUG(dbgs() << MBB->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000671
Dan Gohmancb406c22007-10-03 19:26:29 +0000672 // Create intervals for live-ins to this BB first.
673 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
674 LE = MBB->livein_end(); LI != LE; ++LI) {
675 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
676 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000677 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000678 if (!hasInterval(*AS))
679 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
680 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000681 }
682
Owen Anderson99500ae2008-09-15 22:00:38 +0000683 // Skip over empty initial indices.
Lang Hames233a60e2009-11-03 23:52:08 +0000684 if (getInstructionFromIndex(MIIndex) == 0)
685 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +0000686
Dale Johannesen1caedd02010-01-22 22:38:21 +0000687 for (MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
688 MI != miEnd; ++MI) {
David Greene8a342292010-01-04 22:49:02 +0000689 DEBUG(dbgs() << MIIndex << "\t" << *MI);
Chris Lattner518bb532010-02-09 19:54:29 +0000690 if (MI->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +0000691 continue;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000692
Evan Cheng438f7bc2006-11-10 08:43:01 +0000693 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000694 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
695 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +0000696 if (!MO.isReg() || !MO.getReg())
697 continue;
698
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +0000700 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000701 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +0000702 else if (MO.isUndef())
703 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000705
Lang Hames233a60e2009-11-03 23:52:08 +0000706 // Move to the next instr slot.
707 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000708 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000709 }
Evan Chengd129d732009-07-17 19:43:40 +0000710
711 // Create empty intervals for registers defined by implicit_def's (except
712 // for those implicit_def that define values which are liveout of their
713 // blocks.
714 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
715 unsigned UndefReg = UndefUses[i];
716 (void)getOrCreateInterval(UndefReg);
717 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000718}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000719
Owen Anderson03857b22008-08-13 21:49:13 +0000720LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000721 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +0000722 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000723}
Evan Chengf2fbca62007-11-12 06:35:08 +0000724
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000725/// dupInterval - Duplicate a live interval. The caller is responsible for
726/// managing the allocated memory.
727LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
728 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000729 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000730 return NewLI;
731}
732
Evan Chengc8d044e2008-02-15 18:24:29 +0000733/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
734/// copy field and returns the source register that defines it.
735unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +0000736 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +0000737 return 0;
738
Chris Lattner518bb532010-02-09 19:54:29 +0000739 if (VNI->getCopy()->isExtractSubreg()) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000740 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +0000741 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Chengac948632009-12-11 06:01:00 +0000742 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
743 unsigned SrcSubReg = VNI->getCopy()->getOperand(2).getImm();
744 unsigned DstSubReg = VNI->getCopy()->getOperand(0).getSubReg();
745 if (SrcSubReg == DstSubReg)
746 // %reg1034:3<def> = EXTRACT_SUBREG %EDX, 3
747 // reg1034 can still be coalesced to EDX.
748 return Reg;
749 assert(DstSubReg == 0);
Lang Hames52c1afc2009-08-10 23:43:28 +0000750 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Chengac948632009-12-11 06:01:00 +0000751 }
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000752 return Reg;
Chris Lattner518bb532010-02-09 19:54:29 +0000753 } else if (VNI->getCopy()->isInsertSubreg() ||
754 VNI->getCopy()->isSubregToReg())
Lang Hames52c1afc2009-08-10 23:43:28 +0000755 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000756
Evan Cheng04ee5a12009-01-20 19:12:24 +0000757 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000758 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000759 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +0000760 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +0000761 return 0;
762}
Evan Chengf2fbca62007-11-12 06:35:08 +0000763
764//===----------------------------------------------------------------------===//
765// Register allocator hooks.
766//
767
Evan Chengd70dbb52008-02-22 09:24:50 +0000768/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
769/// allow one) virtual register operand, then its uses are implicitly using
770/// the register. Returns the virtual register.
771unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
772 MachineInstr *MI) const {
773 unsigned RegOp = 0;
774 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
775 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000776 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +0000777 continue;
778 unsigned Reg = MO.getReg();
779 if (Reg == 0 || Reg == li.reg)
780 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +0000781
782 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
783 !allocatableRegs_[Reg])
784 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +0000785 // FIXME: For now, only remat MI with at most one register operand.
786 assert(!RegOp &&
787 "Can't rematerialize instruction with multiple register operand!");
788 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000789#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +0000790 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000791#endif
Evan Chengd70dbb52008-02-22 09:24:50 +0000792 }
793 return RegOp;
794}
795
796/// isValNoAvailableAt - Return true if the val# of the specified interval
797/// which reaches the given instruction also reaches the specified use index.
798bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000799 SlotIndex UseIdx) const {
800 SlotIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +0000801 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
802 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
803 return UI != li.end() && UI->valno == ValNo;
804}
805
Evan Chengf2fbca62007-11-12 06:35:08 +0000806/// isReMaterializable - Returns true if the definition MI of the specified
807/// val# of the specified interval is re-materializable.
808bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000809 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +0000810 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000811 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000812 if (DisableReMat)
813 return false;
814
Dan Gohmana70dca12009-10-09 23:27:56 +0000815 if (!tii_->isTriviallyReMaterializable(MI, aa_))
816 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +0000817
Dan Gohmana70dca12009-10-09 23:27:56 +0000818 // Target-specific code can mark an instruction as being rematerializable
819 // if it has one virtual reg use, though it had better be something like
820 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +0000821 unsigned ImpUse = getReMatImplicitUse(li, MI);
822 if (ImpUse) {
823 const LiveInterval &ImpLi = getInterval(ImpUse);
824 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
825 re = mri_->use_end(); ri != re; ++ri) {
826 MachineInstr *UseMI = &*ri;
Lang Hames233a60e2009-11-03 23:52:08 +0000827 SlotIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +0000828 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
829 continue;
830 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
831 return false;
832 }
Evan Chengdc377862008-09-30 15:44:16 +0000833
834 // If a register operand of the re-materialized instruction is going to
835 // be spilled next, then it's not legal to re-materialize this instruction.
836 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
837 if (ImpUse == SpillIs[i]->reg)
838 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000839 }
840 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000841}
842
Evan Cheng06587492008-10-24 02:05:00 +0000843/// isReMaterializable - Returns true if the definition MI of the specified
844/// val# of the specified interval is re-materializable.
845bool LiveIntervals::isReMaterializable(const LiveInterval &li,
846 const VNInfo *ValNo, MachineInstr *MI) {
847 SmallVector<LiveInterval*, 4> Dummy1;
848 bool Dummy2;
849 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
850}
851
Evan Cheng5ef3a042007-12-06 00:01:56 +0000852/// isReMaterializable - Returns true if every definition of MI of every
853/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +0000854bool LiveIntervals::isReMaterializable(const LiveInterval &li,
855 SmallVectorImpl<LiveInterval*> &SpillIs,
856 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +0000857 isLoad = false;
858 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
859 i != e; ++i) {
860 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +0000861 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000862 continue; // Dead val#.
863 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +0000864 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000865 return false;
Lang Hames857c4e02009-06-17 21:01:20 +0000866 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000867 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000868 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +0000869 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000870 return false;
871 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000872 }
873 return true;
874}
875
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000876/// FilterFoldedOps - Filter out two-address use operands. Return
877/// true if it finds any issue with the operands that ought to prevent
878/// folding.
879static bool FilterFoldedOps(MachineInstr *MI,
880 SmallVector<unsigned, 2> &Ops,
881 unsigned &MRInfo,
882 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000883 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +0000884 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
885 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +0000886 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +0000887 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +0000888 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000889 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +0000890 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +0000891 MRInfo |= (unsigned)VirtRegMap::isMod;
892 else {
893 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +0000894 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +0000895 MRInfo = VirtRegMap::isModRef;
896 continue;
897 }
898 MRInfo |= (unsigned)VirtRegMap::isRef;
899 }
900 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +0000901 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000902 return false;
903}
904
905
906/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
907/// slot / to reg or any rematerialized load into ith operand of specified
908/// MI. If it is successul, MI is updated with the newly created MI and
909/// returns true.
910bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
911 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames233a60e2009-11-03 23:52:08 +0000912 SlotIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000913 SmallVector<unsigned, 2> &Ops,
914 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000915 // If it is an implicit def instruction, just delete it.
Chris Lattner518bb532010-02-09 19:54:29 +0000916 if (MI->isImplicitDef()) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000917 RemoveMachineInstrFromMaps(MI);
918 vrm.RemoveMachineInstrFromMaps(MI);
919 MI->eraseFromParent();
920 ++numFolds;
921 return true;
922 }
923
924 // Filter the list of operand indexes that are to be folded. Abort if
925 // any operand will prevent folding.
926 unsigned MRInfo = 0;
927 SmallVector<unsigned, 2> FoldOps;
928 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
929 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +0000930
Evan Cheng427f4c12008-03-31 23:19:51 +0000931 // The only time it's safe to fold into a two address instruction is when
932 // it's folding reload and spill from / into a spill stack slot.
933 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +0000934 return false;
935
Evan Chengf2f8c2a2008-02-08 22:05:27 +0000936 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
937 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +0000938 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +0000939 // Remember this instruction uses the spill slot.
940 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
941
Evan Chengf2fbca62007-11-12 06:35:08 +0000942 // Attempt to fold the memory reference into the instruction. If
943 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +0000944 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +0000945 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +0000946 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +0000947 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000948 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +0000949 vrm.transferEmergencySpills(MI, fmi);
Lang Hames233a60e2009-11-03 23:52:08 +0000950 ReplaceMachineInstrInMaps(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +0000951 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +0000952 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +0000953 return true;
954 }
955 return false;
956}
957
Evan Cheng018f9b02007-12-05 03:22:34 +0000958/// canFoldMemoryOperand - Returns true if the specified load / store
959/// folding is possible.
960bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000961 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +0000962 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000963 // Filter the list of operand indexes that are to be folded. Abort if
964 // any operand will prevent folding.
965 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +0000966 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000967 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
968 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000969
Evan Cheng3c75ba82008-04-01 21:37:32 +0000970 // It's only legal to remat for a use, not a def.
971 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000972 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +0000973
Evan Chengd70dbb52008-02-22 09:24:50 +0000974 return tii_->canFoldMemoryOperand(MI, FoldOps);
975}
976
Evan Cheng81a03822007-11-17 00:40:40 +0000977bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
Lang Hames233a60e2009-11-03 23:52:08 +0000978 LiveInterval::Ranges::const_iterator itr = li.ranges.begin();
979
980 MachineBasicBlock *mbb = indexes_->getMBBCoveringRange(itr->start, itr->end);
981
982 if (mbb == 0)
983 return false;
984
985 for (++itr; itr != li.ranges.end(); ++itr) {
986 MachineBasicBlock *mbb2 =
987 indexes_->getMBBCoveringRange(itr->start, itr->end);
988
989 if (mbb2 != mbb)
Evan Cheng81a03822007-11-17 00:40:40 +0000990 return false;
991 }
Lang Hames233a60e2009-11-03 23:52:08 +0000992
Evan Cheng81a03822007-11-17 00:40:40 +0000993 return true;
994}
995
Evan Chengd70dbb52008-02-22 09:24:50 +0000996/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
997/// interval on to-be re-materialized operands of MI) with new register.
998void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
999 MachineInstr *MI, unsigned NewVReg,
1000 VirtRegMap &vrm) {
1001 // There is an implicit use. That means one of the other operand is
1002 // being remat'ed and the remat'ed instruction has li.reg as an
1003 // use operand. Make sure we rewrite that as well.
1004 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1005 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001006 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001007 continue;
1008 unsigned Reg = MO.getReg();
1009 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1010 continue;
1011 if (!vrm.isReMaterialized(Reg))
1012 continue;
1013 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001014 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1015 if (UseMO)
1016 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001017 }
1018}
1019
Evan Chengf2fbca62007-11-12 06:35:08 +00001020/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1021/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001022bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001023rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames233a60e2009-11-03 23:52:08 +00001024 bool TrySplit, SlotIndex index, SlotIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001025 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001026 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001027 unsigned Slot, int LdSlot,
1028 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001029 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001030 const TargetRegisterClass* rc,
1031 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001032 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001033 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001034 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001035 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001036 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001037 RestartInstruction:
1038 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1039 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001040 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001041 continue;
1042 unsigned Reg = mop.getReg();
1043 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001044 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001045 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001046 if (Reg != li.reg)
1047 continue;
1048
1049 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001050 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001051 int FoldSlot = Slot;
1052 if (DefIsReMat) {
1053 // If this is the rematerializable definition MI itself and
1054 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001055 if (MI == ReMatOrigDefMI && CanDelete) {
Dale Johannesenbd635202010-02-10 00:55:42 +00001056 DEBUG(dbgs() << "\t\t\t\tErasing re-materializable def: "
Bill Wendling8e6179f2009-08-22 20:18:03 +00001057 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001058 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001059 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001060 MI->eraseFromParent();
1061 break;
1062 }
1063
1064 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001065 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001066 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001067 if (isLoad) {
1068 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1069 FoldSS = isLoadSS;
1070 FoldSlot = LdSlot;
1071 }
1072 }
1073
Evan Chengf2fbca62007-11-12 06:35:08 +00001074 // Scan all of the operands of this instruction rewriting operands
1075 // to use NewVReg instead of li.reg as appropriate. We do this for
1076 // two reasons:
1077 //
1078 // 1. If the instr reads the same spilled vreg multiple times, we
1079 // want to reuse the NewVReg.
1080 // 2. If the instr is a two-addr instruction, we are required to
1081 // keep the src/dst regs pinned.
1082 //
1083 // Keep track of whether we replace a use and/or def so that we can
1084 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001085
Evan Cheng81a03822007-11-17 00:40:40 +00001086 HasUse = mop.isUse();
1087 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001088 SmallVector<unsigned, 2> Ops;
1089 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001090 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001091 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001092 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001093 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001094 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001095 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001096 continue;
1097 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001098 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001099 if (!MOj.isUndef()) {
1100 HasUse |= MOj.isUse();
1101 HasDef |= MOj.isDef();
1102 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001103 }
1104 }
1105
David Greene26b86a02008-10-27 17:38:59 +00001106 // Create a new virtual register for the spill interval.
1107 // Create the new register now so we can map the fold instruction
1108 // to the new register so when it is unfolded we get the correct
1109 // answer.
1110 bool CreatedNewVReg = false;
1111 if (NewVReg == 0) {
1112 NewVReg = mri_->createVirtualRegister(rc);
1113 vrm.grow();
1114 CreatedNewVReg = true;
Jakob Stoklund Olesence7a6632009-11-30 22:55:54 +00001115
1116 // The new virtual register should get the same allocation hints as the
1117 // old one.
1118 std::pair<unsigned, unsigned> Hint = mri_->getRegAllocationHint(Reg);
1119 if (Hint.first || Hint.second)
1120 mri_->setRegAllocationHint(NewVReg, Hint.first, Hint.second);
David Greene26b86a02008-10-27 17:38:59 +00001121 }
1122
Evan Cheng9c3c2212008-06-06 07:54:39 +00001123 if (!TryFold)
1124 CanFold = false;
1125 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001126 // Do not fold load / store here if we are splitting. We'll find an
1127 // optimal point to insert a load / store later.
1128 if (!TrySplit) {
1129 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001130 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001131 // Folding the load/store can completely change the instruction in
1132 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001133
1134 if (FoldSS) {
1135 // We need to give the new vreg the same stack slot as the
1136 // spilled interval.
1137 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1138 }
1139
Evan Cheng018f9b02007-12-05 03:22:34 +00001140 HasUse = false;
1141 HasDef = false;
1142 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001143 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001144 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001145 goto RestartInstruction;
1146 }
1147 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001148 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001149 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001150 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001151 }
Evan Chengcddbb832007-11-30 21:23:43 +00001152
Evan Chengcddbb832007-11-30 21:23:43 +00001153 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001154 if (mop.isImplicit())
1155 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001156
1157 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001158 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1159 MachineOperand &mopj = MI->getOperand(Ops[j]);
1160 mopj.setReg(NewVReg);
1161 if (mopj.isImplicit())
1162 rewriteImplicitOps(li, MI, NewVReg, vrm);
1163 }
Evan Chengcddbb832007-11-30 21:23:43 +00001164
Evan Cheng81a03822007-11-17 00:40:40 +00001165 if (CreatedNewVReg) {
1166 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001167 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001168 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001169 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001170 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001171 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001172 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001173 }
1174 if (!CanDelete || (HasUse && HasDef)) {
1175 // If this is a two-addr instruction then its use operands are
1176 // rematerializable but its def is not. It should be assigned a
1177 // stack slot.
1178 vrm.assignVirt2StackSlot(NewVReg, Slot);
1179 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001180 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001181 vrm.assignVirt2StackSlot(NewVReg, Slot);
1182 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001183 } else if (HasUse && HasDef &&
1184 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1185 // If this interval hasn't been assigned a stack slot (because earlier
1186 // def is a deleted remat def), do it now.
1187 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1188 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001189 }
1190
Evan Cheng313d4b82008-02-23 00:33:04 +00001191 // Re-matting an instruction with virtual register use. Add the
1192 // register as an implicit use on the use MI.
1193 if (DefIsReMat && ImpUse)
1194 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1195
Evan Cheng5b69eba2009-04-21 22:46:52 +00001196 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001197 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001198 if (CreatedNewVReg) {
1199 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001200 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001201 if (TrySplit)
1202 vrm.setIsSplitFromReg(NewVReg, li.reg);
1203 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001204
1205 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001206 if (CreatedNewVReg) {
Lang Hames233a60e2009-11-03 23:52:08 +00001207 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1208 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
David Greene8a342292010-01-04 22:49:02 +00001209 DEBUG(dbgs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001210 nI.addRange(LR);
1211 } else {
1212 // Extend the split live interval to this def / use.
Lang Hames233a60e2009-11-03 23:52:08 +00001213 SlotIndex End = index.getDefIndex();
Evan Cheng81a03822007-11-17 00:40:40 +00001214 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1215 nI.getValNumInfo(nI.getNumValNums()-1));
David Greene8a342292010-01-04 22:49:02 +00001216 DEBUG(dbgs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001217 nI.addRange(LR);
1218 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001219 }
1220 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001221 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1222 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
David Greene8a342292010-01-04 22:49:02 +00001223 DEBUG(dbgs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001224 nI.addRange(LR);
1225 }
Evan Cheng81a03822007-11-17 00:40:40 +00001226
Bill Wendling8e6179f2009-08-22 20:18:03 +00001227 DEBUG({
David Greene8a342292010-01-04 22:49:02 +00001228 dbgs() << "\t\t\t\tAdded new interval: ";
1229 nI.print(dbgs(), tri_);
1230 dbgs() << '\n';
Bill Wendling8e6179f2009-08-22 20:18:03 +00001231 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001232 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001233 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001234}
Evan Cheng81a03822007-11-17 00:40:40 +00001235bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001236 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001237 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +00001238 SlotIndex Idx) const {
1239 SlotIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001240 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames233a60e2009-11-03 23:52:08 +00001241 if (VNI->kills[j].isPHI())
Lang Hamesffd13262009-07-09 03:57:02 +00001242 continue;
1243
Lang Hames233a60e2009-11-03 23:52:08 +00001244 SlotIndex KillIdx = VNI->kills[j];
Lang Hames74ab5ee2009-12-22 00:11:50 +00001245 if (KillIdx > Idx && KillIdx <= End)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001246 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001247 }
1248 return false;
1249}
1250
Evan Cheng063284c2008-02-21 00:34:19 +00001251/// RewriteInfo - Keep track of machine instrs that will be rewritten
1252/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001253namespace {
1254 struct RewriteInfo {
Lang Hames233a60e2009-11-03 23:52:08 +00001255 SlotIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001256 MachineInstr *MI;
1257 bool HasUse;
1258 bool HasDef;
Lang Hames233a60e2009-11-03 23:52:08 +00001259 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001260 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1261 };
Evan Cheng063284c2008-02-21 00:34:19 +00001262
Dan Gohman844731a2008-05-13 00:00:25 +00001263 struct RewriteInfoCompare {
1264 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1265 return LHS.Index < RHS.Index;
1266 }
1267 };
1268}
Evan Cheng063284c2008-02-21 00:34:19 +00001269
Evan Chengf2fbca62007-11-12 06:35:08 +00001270void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001271rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001272 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001273 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001274 unsigned Slot, int LdSlot,
1275 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001276 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001277 const TargetRegisterClass* rc,
1278 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001279 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001280 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001281 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001282 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001283 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1284 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001285 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001286 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001287 unsigned NewVReg = 0;
Lang Hames233a60e2009-11-03 23:52:08 +00001288 SlotIndex start = I->start.getBaseIndex();
1289 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001290
Evan Cheng063284c2008-02-21 00:34:19 +00001291 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001292 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001293 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001294 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1295 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001296 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001297 MachineOperand &O = ri.getOperand();
1298 ++ri;
Dale Johannesenbd635202010-02-10 00:55:42 +00001299 if (MI->isDebugValue()) {
1300 // Remove debug info for now.
1301 O.setReg(0U);
1302 DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI);
1303 continue;
1304 }
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001305 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames233a60e2009-11-03 23:52:08 +00001306 SlotIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001307 if (index < start || index >= end)
1308 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001309
1310 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001311 // Must be defined by an implicit def. It should not be spilled. Note,
1312 // this is for correctness reason. e.g.
1313 // 8 %reg1024<def> = IMPLICIT_DEF
1314 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1315 // The live range [12, 14) are not part of the r1024 live interval since
1316 // it's defined by an implicit def. It will not conflicts with live
1317 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001318 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001319 // the INSERT_SUBREG and both target registers that would overlap.
1320 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001321 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1322 }
1323 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1324
Evan Cheng313d4b82008-02-23 00:33:04 +00001325 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001326 // Now rewrite the defs and uses.
1327 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1328 RewriteInfo &rwi = RewriteMIs[i];
1329 ++i;
Lang Hames233a60e2009-11-03 23:52:08 +00001330 SlotIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001331 bool MIHasUse = rwi.HasUse;
1332 bool MIHasDef = rwi.HasDef;
1333 MachineInstr *MI = rwi.MI;
1334 // If MI def and/or use the same register multiple times, then there
1335 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001336 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001337 while (i != e && RewriteMIs[i].MI == MI) {
1338 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001339 bool isUse = RewriteMIs[i].HasUse;
1340 if (isUse) ++NumUses;
1341 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001342 MIHasDef |= RewriteMIs[i].HasDef;
1343 ++i;
1344 }
Evan Cheng81a03822007-11-17 00:40:40 +00001345 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001346
Evan Cheng0a891ed2008-05-23 23:00:04 +00001347 if (ImpUse && MI != ReMatDefMI) {
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001348 // Re-matting an instruction with virtual register use. Prevent interval
1349 // from being spilled.
1350 getInterval(ImpUse).markNotSpillable();
Evan Cheng313d4b82008-02-23 00:33:04 +00001351 }
1352
Evan Cheng063284c2008-02-21 00:34:19 +00001353 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001354 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001355 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001356 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001357 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001358 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001359 // One common case:
1360 // x = use
1361 // ...
1362 // ...
1363 // def = ...
1364 // = use
1365 // It's better to start a new interval to avoid artifically
1366 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001367 if (MIHasDef && !MIHasUse) {
1368 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001369 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001370 }
1371 }
Evan Chengcada2452007-11-28 01:28:46 +00001372 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001373
1374 bool IsNew = ThisVReg == 0;
1375 if (IsNew) {
1376 // This ends the previous live interval. If all of its def / use
1377 // can be folded, give it a low spill weight.
1378 if (NewVReg && TrySplit && AllCanFold) {
1379 LiveInterval &nI = getOrCreateInterval(NewVReg);
1380 nI.weight /= 10.0F;
1381 }
1382 AllCanFold = true;
1383 }
1384 NewVReg = ThisVReg;
1385
Evan Cheng81a03822007-11-17 00:40:40 +00001386 bool HasDef = false;
1387 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001388 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001389 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1390 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1391 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001392 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001393 if (!HasDef && !HasUse)
1394 continue;
1395
Evan Cheng018f9b02007-12-05 03:22:34 +00001396 AllCanFold &= CanFold;
1397
Evan Cheng81a03822007-11-17 00:40:40 +00001398 // Update weight of spill interval.
1399 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001400 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001401 // The spill weight is now infinity as it cannot be spilled again.
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001402 nI.markNotSpillable();
Evan Cheng0cbb1162007-11-29 01:06:25 +00001403 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001404 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001405
1406 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001407 if (HasDef) {
1408 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001409 bool HasKill = false;
1410 if (!HasUse)
Lang Hames233a60e2009-11-03 23:52:08 +00001411 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001412 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001413 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames233a60e2009-11-03 23:52:08 +00001414 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001415 if (VNI)
Lang Hames233a60e2009-11-03 23:52:08 +00001416 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001417 }
Owen Anderson28998312008-08-13 22:28:50 +00001418 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001419 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001420 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001421 if (SII == SpillIdxes.end()) {
1422 std::vector<SRInfo> S;
1423 S.push_back(SRInfo(index, NewVReg, true));
1424 SpillIdxes.insert(std::make_pair(MBBId, S));
1425 } else if (SII->second.back().vreg != NewVReg) {
1426 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001427 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001428 // If there is an earlier def and this is a two-address
1429 // instruction, then it's not possible to fold the store (which
1430 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001431 SRInfo &Info = SII->second.back();
1432 Info.index = index;
1433 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001434 }
1435 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001436 } else if (SII != SpillIdxes.end() &&
1437 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001438 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001439 // There is an earlier def that's not killed (must be two-address).
1440 // The spill is no longer needed.
1441 SII->second.pop_back();
1442 if (SII->second.empty()) {
1443 SpillIdxes.erase(MBBId);
1444 SpillMBBs.reset(MBBId);
1445 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001446 }
1447 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001448 }
1449
1450 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001451 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001452 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001453 if (SII != SpillIdxes.end() &&
1454 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001455 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001456 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001457 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001458 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001459 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001460 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001461 // If we are splitting live intervals, only fold if it's the first
1462 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001463 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001464 else if (IsNew) {
1465 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001466 if (RII == RestoreIdxes.end()) {
1467 std::vector<SRInfo> Infos;
1468 Infos.push_back(SRInfo(index, NewVReg, true));
1469 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1470 } else {
1471 RII->second.push_back(SRInfo(index, NewVReg, true));
1472 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001473 RestoreMBBs.set(MBBId);
1474 }
1475 }
1476
1477 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001478 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001479 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001480 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001481
1482 if (NewVReg && TrySplit && AllCanFold) {
1483 // If all of its def / use can be folded, give it a low spill weight.
1484 LiveInterval &nI = getOrCreateInterval(NewVReg);
1485 nI.weight /= 10.0F;
1486 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001487}
1488
Lang Hames233a60e2009-11-03 23:52:08 +00001489bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001490 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001491 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001492 if (!RestoreMBBs[Id])
1493 return false;
1494 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1495 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1496 if (Restores[i].index == index &&
1497 Restores[i].vreg == vr &&
1498 Restores[i].canFold)
1499 return true;
1500 return false;
1501}
1502
Lang Hames233a60e2009-11-03 23:52:08 +00001503void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001504 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001505 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001506 if (!RestoreMBBs[Id])
1507 return;
1508 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1509 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1510 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames233a60e2009-11-03 23:52:08 +00001511 Restores[i].index = SlotIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00001512}
Evan Cheng81a03822007-11-17 00:40:40 +00001513
Evan Cheng4cce6b42008-04-11 17:53:36 +00001514/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1515/// spilled and create empty intervals for their uses.
1516void
1517LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1518 const TargetRegisterClass* rc,
1519 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001520 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1521 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001522 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001523 MachineInstr *MI = &*ri;
1524 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001525 if (O.isDef()) {
Chris Lattner518bb532010-02-09 19:54:29 +00001526 assert(MI->isImplicitDef() &&
Evan Cheng4cce6b42008-04-11 17:53:36 +00001527 "Register def was not rewritten?");
1528 RemoveMachineInstrFromMaps(MI);
1529 vrm.RemoveMachineInstrFromMaps(MI);
1530 MI->eraseFromParent();
1531 } else {
1532 // This must be an use of an implicit_def so it's not part of the live
1533 // interval. Create a new empty live interval for it.
1534 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1535 unsigned NewVReg = mri_->createVirtualRegister(rc);
1536 vrm.grow();
1537 vrm.setIsImplicitlyDefined(NewVReg);
1538 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1539 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1540 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001541 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001542 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001543 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001544 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001545 }
1546 }
Evan Cheng419852c2008-04-03 16:39:43 +00001547 }
1548}
1549
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001550float
1551LiveIntervals::getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) {
1552 // Limit the loop depth ridiculousness.
1553 if (loopDepth > 200)
1554 loopDepth = 200;
1555
1556 // The loop depth is used to roughly estimate the number of times the
1557 // instruction is executed. Something like 10^d is simple, but will quickly
1558 // overflow a float. This expression behaves like 10^d for small d, but is
1559 // more tempered for large d. At d=200 we get 6.7e33 which leaves a bit of
1560 // headroom before overflow.
1561 float lc = powf(1 + (100.0f / (loopDepth+10)), (float)loopDepth);
1562
1563 return (isDef + isUse) * lc;
1564}
1565
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +00001566void
1567LiveIntervals::normalizeSpillWeights(std::vector<LiveInterval*> &NewLIs) {
1568 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i)
1569 normalizeSpillWeight(*NewLIs[i]);
1570}
1571
Evan Chengf2fbca62007-11-12 06:35:08 +00001572std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001573addIntervalsForSpillsFast(const LiveInterval &li,
1574 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001575 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001576 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001577
1578 std::vector<LiveInterval*> added;
1579
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001580 assert(li.isSpillable() && "attempt to spill already spilled interval!");
Owen Andersond6664312008-08-18 18:05:32 +00001581
Bill Wendling8e6179f2009-08-22 20:18:03 +00001582 DEBUG({
David Greene8a342292010-01-04 22:49:02 +00001583 dbgs() << "\t\t\t\tadding intervals for spills for interval: ";
Bill Wendling8e6179f2009-08-22 20:18:03 +00001584 li.dump();
David Greene8a342292010-01-04 22:49:02 +00001585 dbgs() << '\n';
Bill Wendling8e6179f2009-08-22 20:18:03 +00001586 });
Owen Andersond6664312008-08-18 18:05:32 +00001587
1588 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1589
Owen Andersona41e47a2008-08-19 22:12:11 +00001590 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1591 while (RI != mri_->reg_end()) {
1592 MachineInstr* MI = &*RI;
1593
1594 SmallVector<unsigned, 2> Indices;
1595 bool HasUse = false;
1596 bool HasDef = false;
1597
1598 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1599 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001600 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001601
1602 HasUse |= MI->getOperand(i).isUse();
1603 HasDef |= MI->getOperand(i).isDef();
1604
1605 Indices.push_back(i);
1606 }
1607
1608 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1609 Indices, true, slot, li.reg)) {
1610 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00001611 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00001612 vrm.assignVirt2StackSlot(NewVReg, slot);
1613
Owen Andersona41e47a2008-08-19 22:12:11 +00001614 // create a new register for this spill
1615 LiveInterval &nI = getOrCreateInterval(NewVReg);
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001616 nI.markNotSpillable();
Owen Andersona41e47a2008-08-19 22:12:11 +00001617
1618 // Rewrite register operands to use the new vreg.
1619 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1620 E = Indices.end(); I != E; ++I) {
1621 MI->getOperand(*I).setReg(NewVReg);
1622
1623 if (MI->getOperand(*I).isUse())
1624 MI->getOperand(*I).setIsKill(true);
1625 }
1626
1627 // Fill in the new live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001628 SlotIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00001629 if (HasUse) {
Lang Hames233a60e2009-11-03 23:52:08 +00001630 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1631 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001632 getVNInfoAllocator()));
David Greene8a342292010-01-04 22:49:02 +00001633 DEBUG(dbgs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001634 nI.addRange(LR);
1635 vrm.addRestorePoint(NewVReg, MI);
1636 }
1637 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001638 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1639 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001640 getVNInfoAllocator()));
David Greene8a342292010-01-04 22:49:02 +00001641 DEBUG(dbgs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001642 nI.addRange(LR);
1643 vrm.addSpillPoint(NewVReg, true, MI);
1644 }
1645
Owen Anderson17197312008-08-18 23:41:04 +00001646 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00001647
Bill Wendling8e6179f2009-08-22 20:18:03 +00001648 DEBUG({
David Greene8a342292010-01-04 22:49:02 +00001649 dbgs() << "\t\t\t\tadded new interval: ";
Bill Wendling8e6179f2009-08-22 20:18:03 +00001650 nI.dump();
David Greene8a342292010-01-04 22:49:02 +00001651 dbgs() << '\n';
Bill Wendling8e6179f2009-08-22 20:18:03 +00001652 });
Owen Andersona41e47a2008-08-19 22:12:11 +00001653 }
Owen Anderson9a032932008-08-18 21:20:32 +00001654
Owen Anderson9a032932008-08-18 21:20:32 +00001655
Owen Andersona41e47a2008-08-19 22:12:11 +00001656 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001657 }
Owen Andersond6664312008-08-18 18:05:32 +00001658
1659 return added;
1660}
1661
1662std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001663addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00001664 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00001665 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00001666
1667 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00001668 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00001669
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001670 assert(li.isSpillable() && "attempt to spill already spilled interval!");
Evan Chengf2fbca62007-11-12 06:35:08 +00001671
Bill Wendling8e6179f2009-08-22 20:18:03 +00001672 DEBUG({
David Greene8a342292010-01-04 22:49:02 +00001673 dbgs() << "\t\t\t\tadding intervals for spills for interval: ";
1674 li.print(dbgs(), tri_);
1675 dbgs() << '\n';
Bill Wendling8e6179f2009-08-22 20:18:03 +00001676 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001677
Evan Cheng72eeb942008-12-05 17:00:16 +00001678 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00001679 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001680 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001681 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001682 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1683 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001684 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001685 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001686
1687 unsigned NumValNums = li.getNumValNums();
1688 SmallVector<MachineInstr*, 4> ReMatDefs;
1689 ReMatDefs.resize(NumValNums, NULL);
1690 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1691 ReMatOrigDefs.resize(NumValNums, NULL);
1692 SmallVector<int, 4> ReMatIds;
1693 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1694 BitVector ReMatDelete(NumValNums);
1695 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1696
Evan Cheng81a03822007-11-17 00:40:40 +00001697 // Spilling a split live interval. It cannot be split any further. Also,
1698 // it's also guaranteed to be a single val# / range interval.
1699 if (vrm.getPreSplitReg(li.reg)) {
1700 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001701 // Unset the split kill marker on the last use.
Lang Hames233a60e2009-11-03 23:52:08 +00001702 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1703 if (KillIdx != SlotIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00001704 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1705 assert(KillMI && "Last use disappeared?");
1706 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1707 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001708 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001709 }
Evan Chengadf85902007-12-05 09:51:10 +00001710 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001711 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1712 Slot = vrm.getStackSlot(li.reg);
1713 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1714 MachineInstr *ReMatDefMI = DefIsReMat ?
1715 vrm.getReMaterializedMI(li.reg) : NULL;
1716 int LdSlot = 0;
1717 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1718 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001719 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001720 bool IsFirstRange = true;
1721 for (LiveInterval::Ranges::const_iterator
1722 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1723 // If this is a split live interval with multiple ranges, it means there
1724 // are two-address instructions that re-defined the value. Only the
1725 // first def can be rematerialized!
1726 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001727 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001728 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1729 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001730 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001731 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001732 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001733 } else {
1734 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1735 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001736 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001737 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001738 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001739 }
1740 IsFirstRange = false;
1741 }
Evan Cheng419852c2008-04-03 16:39:43 +00001742
Evan Cheng4cce6b42008-04-11 17:53:36 +00001743 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +00001744 normalizeSpillWeights(NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001745 return NewLIs;
1746 }
1747
Evan Cheng752195e2009-09-14 21:33:42 +00001748 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001749 if (TrySplit)
1750 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001751 bool NeedStackSlot = false;
1752 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1753 i != e; ++i) {
1754 const VNInfo *VNI = *i;
1755 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00001756 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00001757 continue; // Dead val#.
1758 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001759 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1760 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001761 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00001762 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001763 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001764 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001765 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001766 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00001767 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00001768 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001769
1770 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00001771 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00001772 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001773 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001774 CanDelete = false;
1775 // Need a stack slot if there is any live range where uses cannot be
1776 // rematerialized.
1777 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001778 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001779 if (CanDelete)
1780 ReMatDelete.set(VN);
1781 } else {
1782 // Need a stack slot if there is any live range where uses cannot be
1783 // rematerialized.
1784 NeedStackSlot = true;
1785 }
1786 }
1787
1788 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00001789 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1790 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1791 Slot = vrm.assignVirt2StackSlot(li.reg);
1792
1793 // This case only occurs when the prealloc splitter has already assigned
1794 // a stack slot to this vreg.
1795 else
1796 Slot = vrm.getStackSlot(li.reg);
1797 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001798
1799 // Create new intervals and rewrite defs and uses.
1800 for (LiveInterval::Ranges::const_iterator
1801 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001802 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1803 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1804 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001805 bool CanDelete = ReMatDelete[I->valno->id];
1806 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001807 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001808 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001809 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001810 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001811 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001812 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001813 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001814 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001815 }
1816
Evan Cheng0cbb1162007-11-29 01:06:25 +00001817 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001818 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001819 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +00001820 normalizeSpillWeights(NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001821 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001822 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001823
Evan Chengb50bb8c2007-12-05 08:16:32 +00001824 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001825 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001826 if (NeedStackSlot) {
1827 int Id = SpillMBBs.find_first();
1828 while (Id != -1) {
1829 std::vector<SRInfo> &spills = SpillIdxes[Id];
1830 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001831 SlotIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001832 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001833 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001834 bool isReMat = vrm.isReMaterialized(VReg);
1835 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001836 bool CanFold = false;
1837 bool FoundUse = false;
1838 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001839 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001840 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001841 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1842 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001843 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001844 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001845
1846 Ops.push_back(j);
1847 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001848 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001849 if (isReMat ||
1850 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1851 RestoreMBBs, RestoreIdxes))) {
1852 // MI has two-address uses of the same register. If the use
1853 // isn't the first and only use in the BB, then we can't fold
1854 // it. FIXME: Move this to rewriteInstructionsForSpills.
1855 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001856 break;
1857 }
Evan Chengaee4af62007-12-02 08:30:39 +00001858 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001859 }
1860 }
1861 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001862 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001863 if (CanFold && !Ops.empty()) {
1864 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001865 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00001866 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00001867 // Also folded uses, do not issue a load.
1868 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +00001869 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengf38d14f2007-12-05 09:05:34 +00001870 }
Lang Hames233a60e2009-11-03 23:52:08 +00001871 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
Evan Chengcddbb832007-11-30 21:23:43 +00001872 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001873 }
1874
Evan Cheng7e073ba2008-04-09 20:57:25 +00001875 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001876 if (!Folded) {
1877 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001878 bool isKill = LR->end == index.getStoreIndex();
Evan Chengb0a6f622008-05-20 08:10:37 +00001879 if (!MI->registerDefIsDead(nI.reg))
1880 // No need to spill a dead def.
1881 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001882 if (isKill)
1883 AddedKill.insert(&nI);
1884 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001885 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001886 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001887 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001888 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001889
Evan Cheng1953d0c2007-11-29 10:12:14 +00001890 int Id = RestoreMBBs.find_first();
1891 while (Id != -1) {
1892 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1893 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001894 SlotIndex index = restores[i].index;
1895 if (index == SlotIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00001896 continue;
1897 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001898 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001899 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001900 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001901 bool CanFold = false;
1902 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001903 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001904 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001905 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1906 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001907 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00001908 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001909
Evan Cheng0cbb1162007-11-29 01:06:25 +00001910 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001911 // If this restore were to be folded, it would have been folded
1912 // already.
1913 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00001914 break;
1915 }
Evan Chengaee4af62007-12-02 08:30:39 +00001916 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00001917 }
1918 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001919
1920 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001921 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001922 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001923 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00001924 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
1925 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001926 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
1927 int LdSlot = 0;
1928 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1929 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00001930 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00001931 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
1932 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00001933 if (!Folded) {
1934 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
1935 if (ImpUse) {
1936 // Re-matting an instruction with virtual register use. Add the
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001937 // register as an implicit use on the use MI and mark the register
1938 // interval as unspillable.
Evan Cheng650d7f32008-12-05 17:41:31 +00001939 LiveInterval &ImpLi = getInterval(ImpUse);
Jakob Stoklund Olesene5d90412010-03-01 20:59:38 +00001940 ImpLi.markNotSpillable();
Evan Cheng650d7f32008-12-05 17:41:31 +00001941 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1942 }
Evan Chengd70dbb52008-02-22 09:24:50 +00001943 }
Evan Chengaee4af62007-12-02 08:30:39 +00001944 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001945 }
1946 // If folding is not possible / failed, then tell the spiller to issue a
1947 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00001948 if (Folded)
Lang Hames233a60e2009-11-03 23:52:08 +00001949 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengb50bb8c2007-12-05 08:16:32 +00001950 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00001951 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00001952 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001953 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00001954 }
1955
Evan Chengb50bb8c2007-12-05 08:16:32 +00001956 // Finalize intervals: add kills, finalize spill weights, and filter out
1957 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00001958 std::vector<LiveInterval*> RetNewLIs;
1959 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
1960 LiveInterval *LI = NewLIs[i];
1961 if (!LI->empty()) {
Lang Hames233a60e2009-11-03 23:52:08 +00001962 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001963 if (!AddedKill.count(LI)) {
1964 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001965 SlotIndex LastUseIdx = LR->end.getBaseIndex();
Evan Chengd120ffd2007-12-05 10:24:35 +00001966 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00001967 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001968 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00001969 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00001970 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00001971 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00001972 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00001973 }
Evan Cheng597d10d2007-12-04 00:32:23 +00001974 RetNewLIs.push_back(LI);
1975 }
1976 }
Evan Cheng81a03822007-11-17 00:40:40 +00001977
Evan Cheng4cce6b42008-04-11 17:53:36 +00001978 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Jakob Stoklund Olesen352d3522010-02-18 21:33:05 +00001979 normalizeSpillWeights(RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00001980 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00001981}
Evan Cheng676dd7c2008-03-11 07:19:34 +00001982
1983/// hasAllocatableSuperReg - Return true if the specified physical register has
1984/// any super register that's allocatable.
1985bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
1986 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
1987 if (allocatableRegs_[*AS] && hasInterval(*AS))
1988 return true;
1989 return false;
1990}
1991
1992/// getRepresentativeReg - Find the largest super register of the specified
1993/// physical register.
1994unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
1995 // Find the largest super-register that is allocatable.
1996 unsigned BestReg = Reg;
1997 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
1998 unsigned SuperReg = *AS;
1999 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2000 BestReg = SuperReg;
2001 break;
2002 }
2003 }
2004 return BestReg;
2005}
2006
2007/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2008/// specified interval that conflicts with the specified physical register.
2009unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2010 unsigned PhysReg) const {
2011 unsigned NumConflicts = 0;
2012 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2013 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2014 E = mri_->reg_end(); I != E; ++I) {
2015 MachineOperand &O = I.getOperand();
2016 MachineInstr *MI = O.getParent();
Lang Hames233a60e2009-11-03 23:52:08 +00002017 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002018 if (pli.liveAt(Index))
2019 ++NumConflicts;
2020 }
2021 return NumConflicts;
2022}
2023
2024/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002025/// around all defs and uses of the specified interval. Return true if it
2026/// was able to cut its interval.
2027bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002028 unsigned PhysReg, VirtRegMap &vrm) {
2029 unsigned SpillReg = getRepresentativeReg(PhysReg);
2030
2031 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2032 // If there are registers which alias PhysReg, but which are not a
2033 // sub-register of the chosen representative super register. Assert
2034 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002035 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002036 tri_->isSuperRegister(*AS, SpillReg));
2037
Evan Cheng2824a652009-03-23 18:24:37 +00002038 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00002039 SmallVector<unsigned, 4> PRegs;
2040 if (hasInterval(SpillReg))
2041 PRegs.push_back(SpillReg);
2042 else {
2043 SmallSet<unsigned, 4> Added;
2044 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2045 if (Added.insert(*AS) && hasInterval(*AS)) {
2046 PRegs.push_back(*AS);
2047 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2048 Added.insert(*ASS);
2049 }
2050 }
2051
Evan Cheng676dd7c2008-03-11 07:19:34 +00002052 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2053 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2054 E = mri_->reg_end(); I != E; ++I) {
2055 MachineOperand &O = I.getOperand();
2056 MachineInstr *MI = O.getParent();
2057 if (SeenMIs.count(MI))
2058 continue;
2059 SeenMIs.insert(MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002060 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002061 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2062 unsigned PReg = PRegs[i];
2063 LiveInterval &pli = getInterval(PReg);
2064 if (!pli.liveAt(Index))
2065 continue;
2066 vrm.addEmergencySpill(PReg, MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002067 SlotIndex StartIdx = Index.getLoadIndex();
2068 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
Evan Cheng2824a652009-03-23 18:24:37 +00002069 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002070 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002071 Cut = true;
2072 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002073 std::string msg;
2074 raw_string_ostream Msg(msg);
2075 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +00002076 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002077 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002078 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002079 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002080 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002081 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002082 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002083 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002084 if (!hasInterval(*AS))
2085 continue;
2086 LiveInterval &spli = getInterval(*AS);
2087 if (spli.liveAt(Index))
Lang Hames233a60e2009-11-03 23:52:08 +00002088 spli.removeRange(Index.getLoadIndex(),
2089 Index.getNextIndex().getBaseIndex());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002090 }
2091 }
2092 }
Evan Cheng2824a652009-03-23 18:24:37 +00002093 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002094}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002095
2096LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002097 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002098 LiveInterval& Interval = getOrCreateInterval(reg);
2099 VNInfo* VN = Interval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +00002100 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames86511252009-09-04 20:41:11 +00002101 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002102 VN->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +00002103 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
Lang Hames86511252009-09-04 20:41:11 +00002104 LiveRange LR(
Lang Hames233a60e2009-11-03 23:52:08 +00002105 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames74ab5ee2009-12-22 00:11:50 +00002106 getMBBEndIdx(startInst->getParent()), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002107 Interval.addRange(LR);
2108
2109 return LR;
2110}
David Greeneb5257662009-08-03 21:55:09 +00002111