blob: 2a93a35b3faf391a19e34b007dbdddd642ac2de5 [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 +000056static cl::opt<bool> EarlyCoalescing("early-coalescing", cl::init(false));
57
58static cl::opt<int> CoalescingLimit("early-coalescing-limit",
59 cl::init(-1), cl::Hidden);
60
61STATISTIC(numIntervals , "Number of original intervals");
62STATISTIC(numFolds , "Number of loads/stores folded into instructions");
63STATISTIC(numSplits , "Number of intervals split");
64STATISTIC(numCoalescing, "Number of early coalescing performed");
Chris Lattnercd3245a2006-12-19 22:41:21 +000065
Devang Patel19974732007-05-03 01:11:54 +000066char LiveIntervals::ID = 0;
Dan Gohman844731a2008-05-13 00:00:25 +000067static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000068
Chris Lattnerf7da2c72006-08-24 22:43:55 +000069void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000070 AU.setPreservesCFG();
Dan Gohman6d69ba82008-07-25 00:02:30 +000071 AU.addRequired<AliasAnalysis>();
72 AU.addPreserved<AliasAnalysis>();
David Greene25133302007-06-08 17:18:56 +000073 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000074 AU.addRequired<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000075 AU.addPreservedID(MachineLoopInfoID);
76 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000077
78 if (!StrongPHIElim) {
79 AU.addPreservedID(PHIEliminationID);
80 AU.addRequiredID(PHIEliminationID);
81 }
82
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 AU.addRequiredID(TwoAddressInstructionPassID);
Lang Hames233a60e2009-11-03 23:52:08 +000084 AU.addPreserved<ProcessImplicitDefs>();
85 AU.addRequired<ProcessImplicitDefs>();
86 AU.addPreserved<SlotIndexes>();
87 AU.addRequiredTransitive<SlotIndexes>();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000088 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000089}
90
Chris Lattnerf7da2c72006-08-24 22:43:55 +000091void LiveIntervals::releaseMemory() {
Owen Anderson03857b22008-08-13 21:49:13 +000092 // Free the live intervals themselves.
Owen Anderson20e28392008-08-13 22:08:30 +000093 for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Owen Anderson03857b22008-08-13 21:49:13 +000094 E = r2iMap_.end(); I != E; ++I)
95 delete I->second;
96
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000097 r2iMap_.clear();
Evan Cheng752195e2009-09-14 21:33:42 +000098 phiJoinCopies.clear();
Lang Hamesffd13262009-07-09 03:57:02 +000099
Evan Chengdd199d22007-09-06 01:07:24 +0000100 // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
101 VNInfoAllocator.Reset();
Evan Cheng752195e2009-09-14 21:33:42 +0000102 while (!CloneMIs.empty()) {
103 MachineInstr *MI = CloneMIs.back();
104 CloneMIs.pop_back();
Evan Cheng1ed99222008-07-19 00:37:25 +0000105 mf_->DeleteMachineInstr(MI);
106 }
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000107}
108
Owen Anderson80b3ce62008-05-28 20:54:50 +0000109/// runOnMachineFunction - Register allocate the whole function
110///
111bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
112 mf_ = &fn;
113 mri_ = &mf_->getRegInfo();
114 tm_ = &fn.getTarget();
115 tri_ = tm_->getRegisterInfo();
116 tii_ = tm_->getInstrInfo();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000117 aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000118 lv_ = &getAnalysis<LiveVariables>();
Lang Hames233a60e2009-11-03 23:52:08 +0000119 indexes_ = &getAnalysis<SlotIndexes>();
Owen Anderson80b3ce62008-05-28 20:54:50 +0000120 allocatableRegs_ = tri_->getAllocatableSet(fn);
121
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 computeIntervals();
Evan Cheng752195e2009-09-14 21:33:42 +0000123 performEarlyCoalescing();
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000124
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000125 numIntervals += getNumIntervals();
126
Chris Lattner70ca3582004-09-30 15:59:17 +0000127 DEBUG(dump());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 return true;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000129}
130
Chris Lattner70ca3582004-09-30 15:59:17 +0000131/// print - Implement the dump method.
Chris Lattner45cfe542009-08-23 06:03:38 +0000132void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000133 OS << "********** INTERVALS **********\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000134 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000135 I->second->print(OS, tri_);
136 OS << "\n";
Chris Lattner8e7a7092005-07-27 23:03:38 +0000137 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000138
Evan Cheng752195e2009-09-14 21:33:42 +0000139 printInstrs(OS);
140}
141
142void LiveIntervals::printInstrs(raw_ostream &OS) const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000143 OS << "********** MACHINEINSTRS **********\n";
144
Chris Lattner3380d5c2009-07-21 21:12:58 +0000145 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
146 mbbi != mbbe; ++mbbi) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000147 OS << ((Value*)mbbi->getBasicBlock())->getName() << ":\n";
Chris Lattner3380d5c2009-07-21 21:12:58 +0000148 for (MachineBasicBlock::iterator mii = mbbi->begin(),
149 mie = mbbi->end(); mii != mie; ++mii) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000150 OS << getInstructionIndex(mii) << '\t' << *mii;
Chris Lattner3380d5c2009-07-21 21:12:58 +0000151 }
152 }
Chris Lattner70ca3582004-09-30 15:59:17 +0000153}
154
Evan Cheng752195e2009-09-14 21:33:42 +0000155void LiveIntervals::dumpInstrs() const {
156 printInstrs(errs());
157}
158
Evan Chengc92da382007-11-03 07:20:12 +0000159/// conflictsWithPhysRegDef - Returns true if the specified register
160/// is defined during the duration of the specified interval.
161bool LiveIntervals::conflictsWithPhysRegDef(const LiveInterval &li,
162 VirtRegMap &vrm, unsigned reg) {
163 for (LiveInterval::Ranges::const_iterator
164 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000165 for (SlotIndex index = I->start.getBaseIndex(),
166 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
167 index != end;
168 index = index.getNextIndex()) {
Evan Chengc92da382007-11-03 07:20:12 +0000169 // skip deleted instructions
170 while (index != end && !getInstructionFromIndex(index))
Lang Hames233a60e2009-11-03 23:52:08 +0000171 index = index.getNextIndex();
Evan Chengc92da382007-11-03 07:20:12 +0000172 if (index == end) break;
173
174 MachineInstr *MI = getInstructionFromIndex(index);
Evan Cheng04ee5a12009-01-20 19:12:24 +0000175 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
176 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng5d446262007-11-15 08:13:29 +0000177 if (SrcReg == li.reg || DstReg == li.reg)
178 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000179 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
180 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000181 if (!mop.isReg())
Evan Chengc92da382007-11-03 07:20:12 +0000182 continue;
183 unsigned PhysReg = mop.getReg();
Evan Cheng5d446262007-11-15 08:13:29 +0000184 if (PhysReg == 0 || PhysReg == li.reg)
Evan Chengc92da382007-11-03 07:20:12 +0000185 continue;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000186 if (TargetRegisterInfo::isVirtualRegister(PhysReg)) {
Evan Cheng5d446262007-11-15 08:13:29 +0000187 if (!vrm.hasPhys(PhysReg))
188 continue;
Evan Chengc92da382007-11-03 07:20:12 +0000189 PhysReg = vrm.getPhys(PhysReg);
Evan Cheng5d446262007-11-15 08:13:29 +0000190 }
Dan Gohman6f0d0242008-02-10 18:45:23 +0000191 if (PhysReg && tri_->regsOverlap(PhysReg, reg))
Evan Chengc92da382007-11-03 07:20:12 +0000192 return true;
193 }
194 }
195 }
196
197 return false;
198}
199
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000200/// conflictsWithPhysRegRef - Similar to conflictsWithPhysRegRef except
201/// it can check use as well.
202bool LiveIntervals::conflictsWithPhysRegRef(LiveInterval &li,
203 unsigned Reg, bool CheckUse,
204 SmallPtrSet<MachineInstr*,32> &JoinedCopies) {
205 for (LiveInterval::Ranges::const_iterator
206 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000207 for (SlotIndex index = I->start.getBaseIndex(),
208 end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
209 index != end;
210 index = index.getNextIndex()) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000211 // Skip deleted instructions.
212 MachineInstr *MI = 0;
213 while (index != end) {
214 MI = getInstructionFromIndex(index);
215 if (MI)
216 break;
Lang Hames233a60e2009-11-03 23:52:08 +0000217 index = index.getNextIndex();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000218 }
219 if (index == end) break;
220
221 if (JoinedCopies.count(MI))
222 continue;
223 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
224 MachineOperand& MO = MI->getOperand(i);
225 if (!MO.isReg())
226 continue;
227 if (MO.isUse() && !CheckUse)
228 continue;
229 unsigned PhysReg = MO.getReg();
230 if (PhysReg == 0 || TargetRegisterInfo::isVirtualRegister(PhysReg))
231 continue;
232 if (tri_->isSubRegister(Reg, PhysReg))
233 return true;
234 }
235 }
236 }
237
238 return false;
239}
240
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000241#ifndef NDEBUG
Evan Cheng752195e2009-09-14 21:33:42 +0000242static void printRegName(unsigned reg, const TargetRegisterInfo* tri_) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000243 if (TargetRegisterInfo::isPhysicalRegister(reg))
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000244 errs() << tri_->getName(reg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000245 else
Daniel Dunbar3f0e8302009-07-24 09:53:24 +0000246 errs() << "%reg" << reg;
Evan Cheng549f27d32007-08-13 23:45:17 +0000247}
Daniel Dunbar504f9a62009-09-15 20:31:12 +0000248#endif
Evan Cheng549f27d32007-08-13 23:45:17 +0000249
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000250void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000251 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000252 SlotIndex MIIdx,
Lang Hames86511252009-09-04 20:41:11 +0000253 MachineOperand& MO,
Evan Chengef0732d2008-07-10 07:35:43 +0000254 unsigned MOIdx,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000255 LiveInterval &interval) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000256 DEBUG({
257 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000258 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000259 });
Evan Cheng419852c2008-04-03 16:39:43 +0000260
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000261 // Virtual registers may be defined multiple times (due to phi
262 // elimination and 2-addr elimination). Much of what we do only has to be
263 // done once for the vreg. We use an empty interval to detect the first
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000264 // time we see a vreg.
Evan Chengd129d732009-07-17 19:43:40 +0000265 LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000266 if (interval.empty()) {
267 // Get the Idx of the defining instructions.
Lang Hames233a60e2009-11-03 23:52:08 +0000268 SlotIndex defIndex = MIIdx.getDefIndex();
Dale Johannesen39faac22009-09-20 00:36:41 +0000269 // Earlyclobbers move back one, so that they overlap the live range
270 // of inputs.
Dale Johannesen86b49f82008-09-24 01:07:17 +0000271 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000272 defIndex = MIIdx.getUseIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000273 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000274 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000275 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000276 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000277 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000278 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000279 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000280 CopyMI = mi;
Evan Cheng5379f412008-12-19 20:58:01 +0000281 // Earlyclobbers move back one.
Lang Hames857c4e02009-06-17 21:01:20 +0000282 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000283
284 assert(ValNo->id == 0 && "First value in interval is not 0?");
Chris Lattner7ac2d312004-07-24 02:59:07 +0000285
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 // Loop over all of the blocks that the vreg is defined in. There are
287 // two cases we have to handle here. The most common case is a vreg
288 // whose lifetime is contained within a basic block. In this case there
289 // will be a single kill, in MBB, which comes after the definition.
290 if (vi.Kills.size() == 1 && vi.Kills[0]->getParent() == mbb) {
291 // FIXME: what about dead vars?
Lang Hames233a60e2009-11-03 23:52:08 +0000292 SlotIndex killIdx;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000293 if (vi.Kills[0] != mi)
Lang Hames233a60e2009-11-03 23:52:08 +0000294 killIdx = getInstructionIndex(vi.Kills[0]).getDefIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 else
Lang Hames233a60e2009-11-03 23:52:08 +0000296 killIdx = defIndex.getStoreIndex();
Chris Lattner6097d132004-07-19 02:15:56 +0000297
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000298 // If the kill happens after the definition, we have an intra-block
299 // live range.
300 if (killIdx > defIndex) {
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000301 assert(vi.AliveBlocks.empty() &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000302 "Shouldn't be alive across any blocks!");
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000303 LiveRange LR(defIndex, killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000304 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000305 DEBUG(errs() << " +" << LR << "\n");
Lang Hames86511252009-09-04 20:41:11 +0000306 ValNo->addKill(killIdx);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000307 return;
308 }
Alkis Evlogimenosdd2cc652003-12-18 08:48:48 +0000309 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000310
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000311 // The other case we handle is when a virtual register lives to the end
312 // of the defining block, potentially live across some blocks, then is
313 // live into some number of blocks, but gets killed. Start by adding a
314 // range that goes from this definition to the end of the defining block.
Lang Hames233a60e2009-11-03 23:52:08 +0000315 LiveRange NewLR(defIndex, getMBBEndIdx(mbb).getNextIndex().getLoadIndex(),
316 ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000317 DEBUG(errs() << " +" << NewLR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000318 interval.addRange(NewLR);
319
320 // Iterate over all of the blocks that the variable is completely
321 // live in, adding [insrtIndex(begin), instrIndex(end)+4) to the
322 // live interval.
Jeffrey Yasskin493a3d02009-05-26 18:27:15 +0000323 for (SparseBitVector<>::iterator I = vi.AliveBlocks.begin(),
324 E = vi.AliveBlocks.end(); I != E; ++I) {
Lang Hames233a60e2009-11-03 23:52:08 +0000325 LiveRange LR(
326 getMBBStartIdx(mf_->getBlockNumbered(*I)),
327 getMBBEndIdx(mf_->getBlockNumbered(*I)).getNextIndex().getLoadIndex(),
328 ValNo);
Dan Gohman4a829ec2008-11-13 16:31:27 +0000329 interval.addRange(LR);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000330 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000331 }
332
333 // Finally, this virtual register is live from the start of any killing
334 // block to the 'use' slot of the killing instruction.
335 for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
336 MachineInstr *Kill = vi.Kills[i];
Lang Hames233a60e2009-11-03 23:52:08 +0000337 SlotIndex killIdx =
338 getInstructionIndex(Kill).getDefIndex();
Evan Chengb0f59732009-09-21 04:32:32 +0000339 LiveRange LR(getMBBStartIdx(Kill->getParent()), killIdx, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000340 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000341 ValNo->addKill(killIdx);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000342 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000343 }
344
345 } else {
346 // If this is the second time we see a virtual register definition, it
347 // must be due to phi elimination or two addr elimination. If this is
Evan Chengbf105c82006-11-03 03:04:46 +0000348 // the result of two address elimination, then the vreg is one of the
349 // def-and-use register operand.
Bob Wilsond9df5012009-04-09 17:16:43 +0000350 if (mi->isRegTiedToUseOperand(MOIdx)) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000351 // If this is a two-address definition, then we have already processed
352 // the live range. The only problem is that we didn't realize there
353 // are actually two values in the live interval. Because of this we
354 // need to take the LiveRegion that defines this register and split it
355 // into two values.
Evan Chenga07cec92008-01-10 08:22:10 +0000356 assert(interval.containsOneValue());
Lang Hames233a60e2009-11-03 23:52:08 +0000357 SlotIndex DefIndex = interval.getValNumInfo(0)->def.getDefIndex();
358 SlotIndex RedefIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000359 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000360 RedefIndex = MIIdx.getUseIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000361
Lang Hames35f291d2009-09-12 03:34:03 +0000362 const LiveRange *OldLR =
Lang Hames233a60e2009-11-03 23:52:08 +0000363 interval.getLiveRangeContaining(RedefIndex.getUseIndex());
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000364 VNInfo *OldValNo = OldLR->valno;
Evan Cheng4f8ff162007-08-11 00:59:19 +0000365
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366 // Delete the initial value, which should be short and continuous,
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000367 // because the 2-addr copy must be in the same MBB as the redef.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000368 interval.removeRange(DefIndex, RedefIndex);
Alkis Evlogimenos70651572004-08-04 09:46:56 +0000369
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000370 // Two-address vregs should always only be redefined once. This means
371 // that at this point, there should be exactly one value number in it.
372 assert(interval.containsOneValue() && "Unexpected 2-addr liveint!");
373
Chris Lattner91725b72006-08-31 05:54:43 +0000374 // The new value number (#1) is defined by the instruction we claimed
375 // defined value #0.
Lang Hames52c1afc2009-08-10 23:43:28 +0000376 VNInfo *ValNo = interval.getNextValue(OldValNo->def, OldValNo->getCopy(),
Lang Hames857c4e02009-06-17 21:01:20 +0000377 false, // update at *
Evan Chengc8d044e2008-02-15 18:24:29 +0000378 VNInfoAllocator);
Lang Hames857c4e02009-06-17 21:01:20 +0000379 ValNo->setFlags(OldValNo->getFlags()); // * <- updating here
380
Chris Lattner91725b72006-08-31 05:54:43 +0000381 // Value#0 is now defined by the 2-addr instruction.
Evan Chengc8d044e2008-02-15 18:24:29 +0000382 OldValNo->def = RedefIndex;
Lang Hames52c1afc2009-08-10 23:43:28 +0000383 OldValNo->setCopy(0);
Evan Chengfb112882009-03-23 08:01:15 +0000384 if (MO.isEarlyClobber())
Lang Hames857c4e02009-06-17 21:01:20 +0000385 OldValNo->setHasRedefByEC(true);
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000386
387 // Add the new live interval which replaces the range for the input copy.
388 LiveRange LR(DefIndex, RedefIndex, ValNo);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000389 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000390 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000391 ValNo->addKill(RedefIndex);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000392
393 // If this redefinition is dead, we need to add a dummy unit live
394 // range covering the def slot.
Owen Anderson6b098de2008-06-25 23:39:39 +0000395 if (MO.isDead())
Lang Hames233a60e2009-11-03 23:52:08 +0000396 interval.addRange(LiveRange(RedefIndex, RedefIndex.getStoreIndex(),
397 OldValNo));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000398
Bill Wendling8e6179f2009-08-22 20:18:03 +0000399 DEBUG({
400 errs() << " RESULT: ";
401 interval.print(errs(), tri_);
402 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000403 } else {
404 // Otherwise, this must be because of phi elimination. If this is the
405 // first redefinition of the vreg that we have seen, go back and change
406 // the live range in the PHI block to be a different value number.
407 if (interval.containsOneValue()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000408 // Remove the old range that we now know has an incorrect number.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000409 VNInfo *VNI = interval.getValNumInfo(0);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000410 MachineInstr *Killer = vi.Kills[0];
Evan Cheng752195e2009-09-14 21:33:42 +0000411 phiJoinCopies.push_back(Killer);
Lang Hames233a60e2009-11-03 23:52:08 +0000412 SlotIndex Start = getMBBStartIdx(Killer->getParent());
413 SlotIndex End = getInstructionIndex(Killer).getDefIndex();
Bill Wendling8e6179f2009-08-22 20:18:03 +0000414 DEBUG({
415 errs() << " Removing [" << Start << "," << End << "] from: ";
416 interval.print(errs(), tri_);
417 errs() << "\n";
418 });
Lang Hamesffd13262009-07-09 03:57:02 +0000419 interval.removeRange(Start, End);
420 assert(interval.ranges.size() == 1 &&
Evan Cheng752195e2009-09-14 21:33:42 +0000421 "Newly discovered PHI interval has >1 ranges.");
Lang Hames86511252009-09-04 20:41:11 +0000422 MachineBasicBlock *killMBB = getMBBFromIndex(interval.endIndex());
Lang Hames233a60e2009-11-03 23:52:08 +0000423 VNI->addKill(indexes_->getTerminatorGap(killMBB));
Lang Hames857c4e02009-06-17 21:01:20 +0000424 VNI->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000425 DEBUG({
426 errs() << " RESULT: ";
427 interval.print(errs(), tri_);
428 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000430 // Replace the interval with one of a NEW value number. Note that this
431 // value number isn't actually defined by an instruction, weird huh? :)
Lang Hames10382fb2009-06-19 02:17:53 +0000432 LiveRange LR(Start, End,
Lang Hames233a60e2009-11-03 23:52:08 +0000433 interval.getNextValue(SlotIndex(getMBBStartIdx(mbb), true),
434 0, false, VNInfoAllocator));
Lang Hames857c4e02009-06-17 21:01:20 +0000435 LR.valno->setIsPHIDef(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000436 DEBUG(errs() << " replace range with " << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000438 LR.valno->addKill(End);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000439 DEBUG({
440 errs() << " RESULT: ";
441 interval.print(errs(), tri_);
442 });
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 }
444
445 // In the case of PHI elimination, each variable definition is only
446 // live until the end of the block. We've already taken care of the
447 // rest of the live range.
Lang Hames233a60e2009-11-03 23:52:08 +0000448 SlotIndex defIndex = MIIdx.getDefIndex();
Evan Chengfb112882009-03-23 08:01:15 +0000449 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000450 defIndex = MIIdx.getUseIndex();
Evan Cheng752195e2009-09-14 21:33:42 +0000451
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000452 VNInfo *ValNo;
Evan Chengc8d044e2008-02-15 18:24:29 +0000453 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000454 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000455 if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000456 mi->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000457 mi->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000458 tii_->isMoveInstr(*mi, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000459 CopyMI = mi;
Lang Hames857c4e02009-06-17 21:01:20 +0000460 ValNo = interval.getNextValue(defIndex, CopyMI, true, VNInfoAllocator);
Chris Lattner91725b72006-08-31 05:54:43 +0000461
Lang Hames233a60e2009-11-03 23:52:08 +0000462 SlotIndex killIndex = getMBBEndIdx(mbb).getNextIndex().getLoadIndex();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000463 LiveRange LR(defIndex, killIndex, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000464 interval.addRange(LR);
Lang Hames233a60e2009-11-03 23:52:08 +0000465 ValNo->addKill(indexes_->getTerminatorGap(mbb));
Lang Hames857c4e02009-06-17 21:01:20 +0000466 ValNo->setHasPHIKill(true);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000467 DEBUG(errs() << " +" << LR);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000468 }
469 }
470
Bill Wendling8e6179f2009-08-22 20:18:03 +0000471 DEBUG(errs() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000472}
473
Chris Lattnerf35fef72004-07-23 21:24:19 +0000474void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000475 MachineBasicBlock::iterator mi,
Lang Hames233a60e2009-11-03 23:52:08 +0000476 SlotIndex MIIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000477 MachineOperand& MO,
Chris Lattner91725b72006-08-31 05:54:43 +0000478 LiveInterval &interval,
Evan Chengc8d044e2008-02-15 18:24:29 +0000479 MachineInstr *CopyMI) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000480 // A physical register cannot be live across basic block, so its
481 // lifetime must end somewhere in its defining basic block.
Bill Wendling8e6179f2009-08-22 20:18:03 +0000482 DEBUG({
483 errs() << "\t\tregister: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000484 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000485 });
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000486
Lang Hames233a60e2009-11-03 23:52:08 +0000487 SlotIndex baseIndex = MIIdx;
488 SlotIndex start = baseIndex.getDefIndex();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000489 // Earlyclobbers move back one.
490 if (MO.isEarlyClobber())
Lang Hames233a60e2009-11-03 23:52:08 +0000491 start = MIIdx.getUseIndex();
492 SlotIndex end = start;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000493
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000494 // If it is not used after definition, it is considered dead at
495 // the instruction defining it. Hence its interval is:
496 // [defSlot(def), defSlot(def)+1)
Dale Johannesen39faac22009-09-20 00:36:41 +0000497 // For earlyclobbers, the defSlot was pushed back one; the extra
498 // advance below compensates.
Owen Anderson6b098de2008-06-25 23:39:39 +0000499 if (MO.isDead()) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000500 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000501 end = start.getStoreIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000502 goto exit;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000503 }
504
505 // If it is not dead on definition, it must be killed by a
506 // subsequent instruction. Hence its interval is:
507 // [defSlot(def), useSlot(kill)+1)
Lang Hames233a60e2009-11-03 23:52:08 +0000508 baseIndex = baseIndex.getNextIndex();
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000509 while (++mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000510
511 if (getInstructionFromIndex(baseIndex) == 0)
512 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
513
Evan Cheng6130f662008-03-05 00:59:57 +0000514 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000515 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000516 end = baseIndex.getDefIndex();
Chris Lattnerab4b66d2005-08-23 22:51:41 +0000517 goto exit;
Evan Chengc45288e2009-04-27 20:42:46 +0000518 } else {
519 int DefIdx = mi->findRegisterDefOperandIdx(interval.reg, false, tri_);
520 if (DefIdx != -1) {
521 if (mi->isRegTiedToUseOperand(DefIdx)) {
522 // Two-address instruction.
Lang Hames233a60e2009-11-03 23:52:08 +0000523 end = baseIndex.getDefIndex();
524 assert(!mi->getOperand(DefIdx).isEarlyClobber() &&
525 "Two address instruction is an early clobber?");
Evan Chengc45288e2009-04-27 20:42:46 +0000526 } else {
527 // Another instruction redefines the register before it is ever read.
528 // Then the register is essentially dead at the instruction that defines
529 // it. Hence its interval is:
530 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000531 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000532 end = start.getStoreIndex();
Evan Chengc45288e2009-04-27 20:42:46 +0000533 }
534 goto exit;
535 }
Alkis Evlogimenosaf254732004-01-13 22:26:14 +0000536 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000537
Lang Hames233a60e2009-11-03 23:52:08 +0000538 baseIndex = baseIndex.getNextIndex();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 }
Chris Lattner5ab6f5f2005-09-02 00:20:32 +0000540
541 // The only case we should have a dead physreg here without a killing or
542 // instruction where we know it's dead is if it is live-in to the function
Evan Chengd521bc92009-04-27 17:36:47 +0000543 // and never used. Another possible case is the implicit use of the
544 // physical register has been deleted by two-address pass.
Lang Hames233a60e2009-11-03 23:52:08 +0000545 end = start.getStoreIndex();
Alkis Evlogimenos02ba13c2004-01-31 23:13:30 +0000546
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000547exit:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000548 assert(start < end && "did not find end of interval?");
Chris Lattnerf768bba2005-03-09 23:05:19 +0000549
Evan Cheng24a3cc42007-04-25 07:30:23 +0000550 // Already exists? Extend old live interval.
551 LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
Evan Cheng5379f412008-12-19 20:58:01 +0000552 bool Extend = OldLR != interval.end();
553 VNInfo *ValNo = Extend
Lang Hames857c4e02009-06-17 21:01:20 +0000554 ? OldLR->valno : interval.getNextValue(start, CopyMI, true, VNInfoAllocator);
Evan Cheng5379f412008-12-19 20:58:01 +0000555 if (MO.isEarlyClobber() && Extend)
Lang Hames857c4e02009-06-17 21:01:20 +0000556 ValNo->setHasRedefByEC(true);
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000557 LiveRange LR(start, end, ValNo);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000558 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000559 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000560 DEBUG(errs() << " +" << LR << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000561}
562
Chris Lattnerf35fef72004-07-23 21:24:19 +0000563void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
564 MachineBasicBlock::iterator MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000565 SlotIndex MIIdx,
Evan Chengef0732d2008-07-10 07:35:43 +0000566 MachineOperand& MO,
567 unsigned MOIdx) {
Owen Anderson6b098de2008-06-25 23:39:39 +0000568 if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Chengef0732d2008-07-10 07:35:43 +0000569 handleVirtualRegisterDef(MBB, MI, MIIdx, MO, MOIdx,
Owen Anderson6b098de2008-06-25 23:39:39 +0000570 getOrCreateInterval(MO.getReg()));
571 else if (allocatableRegs_[MO.getReg()]) {
Evan Chengc8d044e2008-02-15 18:24:29 +0000572 MachineInstr *CopyMI = NULL;
Evan Cheng04ee5a12009-01-20 19:12:24 +0000573 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Evan Chengc8d044e2008-02-15 18:24:29 +0000574 if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
Evan Cheng7e073ba2008-04-09 20:57:25 +0000575 MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
Dan Gohman97121ba2009-04-08 00:15:30 +0000576 MI->getOpcode() == TargetInstrInfo::SUBREG_TO_REG ||
Evan Cheng04ee5a12009-01-20 19:12:24 +0000577 tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000578 CopyMI = MI;
Evan Chengc45288e2009-04-27 20:42:46 +0000579 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000580 getOrCreateInterval(MO.getReg()), CopyMI);
Evan Cheng24a3cc42007-04-25 07:30:23 +0000581 // Def of a register also defines its sub-registers.
Owen Anderson6b098de2008-06-25 23:39:39 +0000582 for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
Evan Cheng6130f662008-03-05 00:59:57 +0000583 // If MI also modifies the sub-register explicitly, avoid processing it
584 // more than once. Do not pass in TRI here so it checks for exact match.
585 if (!MI->modifiesRegister(*AS))
Evan Chengc45288e2009-04-27 20:42:46 +0000586 handlePhysicalRegisterDef(MBB, MI, MIIdx, MO,
Owen Anderson6b098de2008-06-25 23:39:39 +0000587 getOrCreateInterval(*AS), 0);
Chris Lattnerf35fef72004-07-23 21:24:19 +0000588 }
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000589}
590
Evan Chengb371f452007-02-19 21:49:54 +0000591void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +0000592 SlotIndex MIIdx,
Evan Cheng24a3cc42007-04-25 07:30:23 +0000593 LiveInterval &interval, bool isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000594 DEBUG({
595 errs() << "\t\tlivein register: ";
Evan Cheng752195e2009-09-14 21:33:42 +0000596 printRegName(interval.reg, tri_);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000597 });
Evan Chengb371f452007-02-19 21:49:54 +0000598
599 // Look for kills, if it reaches a def before it's killed, then it shouldn't
600 // be considered a livein.
601 MachineBasicBlock::iterator mi = MBB->begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000602 SlotIndex baseIndex = MIIdx;
603 SlotIndex start = baseIndex;
604 if (getInstructionFromIndex(baseIndex) == 0)
605 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
606
607 SlotIndex end = baseIndex;
Evan Cheng0076c612009-03-05 03:34:26 +0000608 bool SeenDefUse = false;
Owen Anderson99500ae2008-09-15 22:00:38 +0000609
Evan Chengb371f452007-02-19 21:49:54 +0000610 while (mi != MBB->end()) {
Evan Cheng6130f662008-03-05 00:59:57 +0000611 if (mi->killsRegister(interval.reg, tri_)) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000612 DEBUG(errs() << " killed");
Lang Hames233a60e2009-11-03 23:52:08 +0000613 end = baseIndex.getDefIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000614 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000615 break;
Evan Cheng6130f662008-03-05 00:59:57 +0000616 } else if (mi->modifiesRegister(interval.reg, tri_)) {
Evan Chengb371f452007-02-19 21:49:54 +0000617 // Another instruction redefines the register before it is ever read.
618 // Then the register is essentially dead at the instruction that defines
619 // it. Hence its interval is:
620 // [defSlot(def), defSlot(def)+1)
Bill Wendling8e6179f2009-08-22 20:18:03 +0000621 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000622 end = start.getStoreIndex();
Evan Cheng0076c612009-03-05 03:34:26 +0000623 SeenDefUse = true;
Lang Hamesd21c3162009-06-18 22:01:47 +0000624 break;
Evan Chengb371f452007-02-19 21:49:54 +0000625 }
626
Evan Chengb371f452007-02-19 21:49:54 +0000627 ++mi;
Evan Cheng0076c612009-03-05 03:34:26 +0000628 if (mi != MBB->end()) {
Lang Hames233a60e2009-11-03 23:52:08 +0000629 baseIndex = indexes_->getNextNonNullIndex(baseIndex);
Evan Cheng0076c612009-03-05 03:34:26 +0000630 }
Evan Chengb371f452007-02-19 21:49:54 +0000631 }
632
Evan Cheng75611fb2007-06-27 01:16:36 +0000633 // Live-in register might not be used at all.
Evan Cheng0076c612009-03-05 03:34:26 +0000634 if (!SeenDefUse) {
Evan Cheng292da942007-06-27 18:47:28 +0000635 if (isAlias) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000636 DEBUG(errs() << " dead");
Lang Hames233a60e2009-11-03 23:52:08 +0000637 end = MIIdx.getStoreIndex();
Evan Cheng292da942007-06-27 18:47:28 +0000638 } else {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000639 DEBUG(errs() << " live through");
Evan Cheng292da942007-06-27 18:47:28 +0000640 end = baseIndex;
641 }
Evan Cheng24a3cc42007-04-25 07:30:23 +0000642 }
643
Lang Hames10382fb2009-06-19 02:17:53 +0000644 VNInfo *vni =
Lang Hames233a60e2009-11-03 23:52:08 +0000645 interval.getNextValue(SlotIndex(getMBBStartIdx(MBB), true),
Lang Hames86511252009-09-04 20:41:11 +0000646 0, false, VNInfoAllocator);
Lang Hamesd21c3162009-06-18 22:01:47 +0000647 vni->setIsPHIDef(true);
648 LiveRange LR(start, end, vni);
649
Jim Laskey9b25b8c2007-02-21 22:41:17 +0000650 interval.addRange(LR);
Lang Hames86511252009-09-04 20:41:11 +0000651 LR.valno->addKill(end);
Bill Wendling8e6179f2009-08-22 20:18:03 +0000652 DEBUG(errs() << " +" << LR << '\n');
Evan Chengb371f452007-02-19 21:49:54 +0000653}
654
Evan Cheng752195e2009-09-14 21:33:42 +0000655bool
656LiveIntervals::isProfitableToCoalesce(LiveInterval &DstInt, LiveInterval &SrcInt,
657 SmallVector<MachineInstr*,16> &IdentCopies,
Evan Cheng3f855492009-09-15 06:45:16 +0000658 SmallVector<MachineInstr*,16> &OtherCopies) {
659 bool HaveConflict = false;
Evan Cheng752195e2009-09-14 21:33:42 +0000660 unsigned NumIdent = 0;
Dan Gohman2bf06492009-09-25 22:26:13 +0000661 for (MachineRegisterInfo::def_iterator ri = mri_->def_begin(SrcInt.reg),
662 re = mri_->def_end(); ri != re; ++ri) {
Evan Cheng752195e2009-09-14 21:33:42 +0000663 MachineInstr *MI = &*ri;
664 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
665 if (!tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Cheng3f855492009-09-15 06:45:16 +0000666 return false;
Evan Cheng752195e2009-09-14 21:33:42 +0000667 if (SrcReg != DstInt.reg) {
668 OtherCopies.push_back(MI);
669 HaveConflict |= DstInt.liveAt(getInstructionIndex(MI));
670 } else {
671 IdentCopies.push_back(MI);
672 ++NumIdent;
673 }
674 }
675
Evan Cheng3f855492009-09-15 06:45:16 +0000676 if (!HaveConflict)
677 return false; // Let coalescer handle it
678 return IdentCopies.size() > OtherCopies.size();
Evan Cheng752195e2009-09-14 21:33:42 +0000679}
680
681void LiveIntervals::performEarlyCoalescing() {
682 if (!EarlyCoalescing)
683 return;
684
685 /// Perform early coalescing: eliminate copies which feed into phi joins
686 /// and whose sources are defined by the phi joins.
687 for (unsigned i = 0, e = phiJoinCopies.size(); i != e; ++i) {
688 MachineInstr *Join = phiJoinCopies[i];
689 if (CoalescingLimit != -1 && (int)numCoalescing == CoalescingLimit)
690 break;
691
692 unsigned PHISrc, PHIDst, SrcSubReg, DstSubReg;
693 bool isMove= tii_->isMoveInstr(*Join, PHISrc, PHIDst, SrcSubReg, DstSubReg);
694#ifndef NDEBUG
695 assert(isMove && "PHI join instruction must be a move!");
696#else
697 isMove = isMove;
698#endif
699
700 LiveInterval &DstInt = getInterval(PHIDst);
701 LiveInterval &SrcInt = getInterval(PHISrc);
702 SmallVector<MachineInstr*, 16> IdentCopies;
703 SmallVector<MachineInstr*, 16> OtherCopies;
Evan Cheng3f855492009-09-15 06:45:16 +0000704 if (!isProfitableToCoalesce(DstInt, SrcInt, IdentCopies, OtherCopies))
Evan Cheng752195e2009-09-14 21:33:42 +0000705 continue;
706
707 DEBUG(errs() << "PHI Join: " << *Join);
708 assert(DstInt.containsOneValue() && "PHI join should have just one val#!");
709 VNInfo *VNI = DstInt.getValNumInfo(0);
Evan Cheng752195e2009-09-14 21:33:42 +0000710
Evan Cheng3f855492009-09-15 06:45:16 +0000711 // Change the non-identity copies to directly target the phi destination.
712 for (unsigned i = 0, e = OtherCopies.size(); i != e; ++i) {
713 MachineInstr *PHICopy = OtherCopies[i];
714 DEBUG(errs() << "Moving: " << *PHICopy);
715
Lang Hames233a60e2009-11-03 23:52:08 +0000716 SlotIndex MIIndex = getInstructionIndex(PHICopy);
717 SlotIndex DefIndex = MIIndex.getDefIndex();
Evan Cheng752195e2009-09-14 21:33:42 +0000718 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hames233a60e2009-11-03 23:52:08 +0000719 SlotIndex StartIndex = SLR->start;
720 SlotIndex EndIndex = SLR->end;
Evan Cheng752195e2009-09-14 21:33:42 +0000721
722 // Delete val# defined by the now identity copy and add the range from
723 // beginning of the mbb to the end of the range.
724 SrcInt.removeValNo(SLR->valno);
Evan Cheng3f855492009-09-15 06:45:16 +0000725 DEBUG(errs() << " added range [" << StartIndex << ','
726 << EndIndex << "] to reg" << DstInt.reg << '\n');
727 if (DstInt.liveAt(StartIndex))
Evan Cheng752195e2009-09-14 21:33:42 +0000728 DstInt.removeRange(StartIndex, EndIndex);
Evan Cheng3f855492009-09-15 06:45:16 +0000729 VNInfo *NewVNI = DstInt.getNextValue(DefIndex, PHICopy, true,
730 VNInfoAllocator);
731 NewVNI->setHasPHIKill(true);
732 DstInt.addRange(LiveRange(StartIndex, EndIndex, NewVNI));
733 for (unsigned j = 0, ee = PHICopy->getNumOperands(); j != ee; ++j) {
734 MachineOperand &MO = PHICopy->getOperand(j);
735 if (!MO.isReg() || MO.getReg() != PHISrc)
736 continue;
737 MO.setReg(PHIDst);
Evan Cheng752195e2009-09-14 21:33:42 +0000738 }
Evan Cheng3f855492009-09-15 06:45:16 +0000739 }
740
741 // Now let's eliminate all the would-be identity copies.
742 for (unsigned i = 0, e = IdentCopies.size(); i != e; ++i) {
743 MachineInstr *PHICopy = IdentCopies[i];
744 DEBUG(errs() << "Coalescing: " << *PHICopy);
745
Lang Hames233a60e2009-11-03 23:52:08 +0000746 SlotIndex MIIndex = getInstructionIndex(PHICopy);
747 SlotIndex DefIndex = MIIndex.getDefIndex();
Evan Cheng3f855492009-09-15 06:45:16 +0000748 LiveRange *SLR = SrcInt.getLiveRangeContaining(DefIndex);
Lang Hames233a60e2009-11-03 23:52:08 +0000749 SlotIndex StartIndex = SLR->start;
750 SlotIndex EndIndex = SLR->end;
Evan Cheng3f855492009-09-15 06:45:16 +0000751
752 // Delete val# defined by the now identity copy and add the range from
753 // beginning of the mbb to the end of the range.
754 SrcInt.removeValNo(SLR->valno);
Evan Cheng752195e2009-09-14 21:33:42 +0000755 RemoveMachineInstrFromMaps(PHICopy);
756 PHICopy->eraseFromParent();
Evan Cheng3f855492009-09-15 06:45:16 +0000757 DEBUG(errs() << " added range [" << StartIndex << ','
758 << EndIndex << "] to reg" << DstInt.reg << '\n');
759 DstInt.addRange(LiveRange(StartIndex, EndIndex, VNI));
Evan Cheng752195e2009-09-14 21:33:42 +0000760 }
Evan Cheng752195e2009-09-14 21:33:42 +0000761
Evan Cheng3f855492009-09-15 06:45:16 +0000762 // Remove the phi join and update the phi block liveness.
Lang Hames233a60e2009-11-03 23:52:08 +0000763 SlotIndex MIIndex = getInstructionIndex(Join);
764 SlotIndex UseIndex = MIIndex.getUseIndex();
765 SlotIndex DefIndex = MIIndex.getDefIndex();
Evan Cheng3f855492009-09-15 06:45:16 +0000766 LiveRange *SLR = SrcInt.getLiveRangeContaining(UseIndex);
767 LiveRange *DLR = DstInt.getLiveRangeContaining(DefIndex);
768 DLR->valno->setCopy(0);
769 DLR->valno->setIsDefAccurate(false);
770 DstInt.addRange(LiveRange(SLR->start, SLR->end, DLR->valno));
771 SrcInt.removeRange(SLR->start, SLR->end);
772 assert(SrcInt.empty());
773 removeInterval(PHISrc);
774 RemoveMachineInstrFromMaps(Join);
775 Join->eraseFromParent();
Evan Cheng752195e2009-09-14 21:33:42 +0000776
777 ++numCoalescing;
778 }
779}
780
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000781/// computeIntervals - computes the live intervals for virtual
Alkis Evlogimenos4d46e1e2004-01-31 14:37:41 +0000782/// registers. for some ordering of the machine instructions [1,N] a
Alkis Evlogimenos08cec002004-01-31 19:59:32 +0000783/// live interval is an interval [i, j) where 1 <= i <= j < N for
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000784/// which a variable is live
Dale Johannesen91aac102008-09-17 21:13:11 +0000785void LiveIntervals::computeIntervals() {
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000786 DEBUG(errs() << "********** COMPUTING LIVE INTERVALS **********\n"
Bill Wendling8e6179f2009-08-22 20:18:03 +0000787 << "********** Function: "
788 << ((Value*)mf_->getFunction())->getName() << '\n');
Evan Chengd129d732009-07-17 19:43:40 +0000789
790 SmallVector<unsigned, 8> UndefUses;
Chris Lattner428b92e2006-09-15 03:57:23 +0000791 for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
792 MBBI != E; ++MBBI) {
793 MachineBasicBlock *MBB = MBBI;
Owen Anderson134eb732008-09-21 20:43:24 +0000794 // Track the index of the current machine instr.
Lang Hames233a60e2009-11-03 23:52:08 +0000795 SlotIndex MIIndex = getMBBStartIdx(MBB);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000796 DEBUG(errs() << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
Alkis Evlogimenos6b4edba2003-12-21 20:19:10 +0000797
Chris Lattner428b92e2006-09-15 03:57:23 +0000798 MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
Evan Cheng0c9f92e2007-02-13 01:30:55 +0000799
Dan Gohmancb406c22007-10-03 19:26:29 +0000800 // Create intervals for live-ins to this BB first.
801 for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
802 LE = MBB->livein_end(); LI != LE; ++LI) {
803 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
804 // Multiple live-ins can alias the same register.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000805 for (const unsigned* AS = tri_->getSubRegisters(*LI); *AS; ++AS)
Dan Gohmancb406c22007-10-03 19:26:29 +0000806 if (!hasInterval(*AS))
807 handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
808 true);
Chris Lattnerdffb2e82006-09-04 18:27:40 +0000809 }
810
Owen Anderson99500ae2008-09-15 22:00:38 +0000811 // Skip over empty initial indices.
Lang Hames233a60e2009-11-03 23:52:08 +0000812 if (getInstructionFromIndex(MIIndex) == 0)
813 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Owen Anderson99500ae2008-09-15 22:00:38 +0000814
Chris Lattner428b92e2006-09-15 03:57:23 +0000815 for (; MI != miEnd; ++MI) {
Bill Wendling8e6179f2009-08-22 20:18:03 +0000816 DEBUG(errs() << MIIndex << "\t" << *MI);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000817
Evan Cheng438f7bc2006-11-10 08:43:01 +0000818 // Handle defs.
Chris Lattner428b92e2006-09-15 03:57:23 +0000819 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
820 MachineOperand &MO = MI->getOperand(i);
Evan Chengd129d732009-07-17 19:43:40 +0000821 if (!MO.isReg() || !MO.getReg())
822 continue;
823
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000824 // handle register defs - build intervals
Evan Chengd129d732009-07-17 19:43:40 +0000825 if (MO.isDef())
Evan Chengef0732d2008-07-10 07:35:43 +0000826 handleRegisterDef(MBB, MI, MIIndex, MO, i);
Evan Chengd129d732009-07-17 19:43:40 +0000827 else if (MO.isUndef())
828 UndefUses.push_back(MO.getReg());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000829 }
Owen Anderson7fbad272008-07-23 21:37:49 +0000830
Lang Hames233a60e2009-11-03 23:52:08 +0000831 // Move to the next instr slot.
832 MIIndex = indexes_->getNextNonNullIndex(MIIndex);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000833 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000834 }
Evan Chengd129d732009-07-17 19:43:40 +0000835
836 // Create empty intervals for registers defined by implicit_def's (except
837 // for those implicit_def that define values which are liveout of their
838 // blocks.
839 for (unsigned i = 0, e = UndefUses.size(); i != e; ++i) {
840 unsigned UndefReg = UndefUses[i];
841 (void)getOrCreateInterval(UndefReg);
842 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000843}
Alkis Evlogimenosb27ef242003-12-05 10:38:28 +0000844
Owen Anderson03857b22008-08-13 21:49:13 +0000845LiveInterval* LiveIntervals::createInterval(unsigned reg) {
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000846 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F;
Owen Anderson03857b22008-08-13 21:49:13 +0000847 return new LiveInterval(reg, Weight);
Alkis Evlogimenos9a8b4902004-04-09 18:07:57 +0000848}
Evan Chengf2fbca62007-11-12 06:35:08 +0000849
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000850/// dupInterval - Duplicate a live interval. The caller is responsible for
851/// managing the allocated memory.
852LiveInterval* LiveIntervals::dupInterval(LiveInterval *li) {
853 LiveInterval *NewLI = createInterval(li->reg);
Evan Cheng90f95f82009-06-14 20:22:55 +0000854 NewLI->Copy(*li, mri_, getVNInfoAllocator());
Evan Cheng0a1fcce2009-02-08 11:04:35 +0000855 return NewLI;
856}
857
Evan Chengc8d044e2008-02-15 18:24:29 +0000858/// getVNInfoSourceReg - Helper function that parses the specified VNInfo
859/// copy field and returns the source register that defines it.
860unsigned LiveIntervals::getVNInfoSourceReg(const VNInfo *VNI) const {
Lang Hames52c1afc2009-08-10 23:43:28 +0000861 if (!VNI->getCopy())
Evan Chengc8d044e2008-02-15 18:24:29 +0000862 return 0;
863
Lang Hames52c1afc2009-08-10 23:43:28 +0000864 if (VNI->getCopy()->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG) {
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000865 // If it's extracting out of a physical register, return the sub-register.
Lang Hames52c1afc2009-08-10 23:43:28 +0000866 unsigned Reg = VNI->getCopy()->getOperand(1).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000867 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Lang Hames52c1afc2009-08-10 23:43:28 +0000868 Reg = tri_->getSubReg(Reg, VNI->getCopy()->getOperand(2).getImm());
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000869 return Reg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000870 } else if (VNI->getCopy()->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
871 VNI->getCopy()->getOpcode() == TargetInstrInfo::SUBREG_TO_REG)
872 return VNI->getCopy()->getOperand(2).getReg();
Evan Cheng8f90b6e2009-01-07 02:08:57 +0000873
Evan Cheng04ee5a12009-01-20 19:12:24 +0000874 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
Lang Hames52c1afc2009-08-10 23:43:28 +0000875 if (tii_->isMoveInstr(*VNI->getCopy(), SrcReg, DstReg, SrcSubReg, DstSubReg))
Evan Chengc8d044e2008-02-15 18:24:29 +0000876 return SrcReg;
Torok Edwinc23197a2009-07-14 16:55:14 +0000877 llvm_unreachable("Unrecognized copy instruction!");
Evan Chengc8d044e2008-02-15 18:24:29 +0000878 return 0;
879}
Evan Chengf2fbca62007-11-12 06:35:08 +0000880
881//===----------------------------------------------------------------------===//
882// Register allocator hooks.
883//
884
Evan Chengd70dbb52008-02-22 09:24:50 +0000885/// getReMatImplicitUse - If the remat definition MI has one (for now, we only
886/// allow one) virtual register operand, then its uses are implicitly using
887/// the register. Returns the virtual register.
888unsigned LiveIntervals::getReMatImplicitUse(const LiveInterval &li,
889 MachineInstr *MI) const {
890 unsigned RegOp = 0;
891 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
892 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000893 if (!MO.isReg() || !MO.isUse())
Evan Chengd70dbb52008-02-22 09:24:50 +0000894 continue;
895 unsigned Reg = MO.getReg();
896 if (Reg == 0 || Reg == li.reg)
897 continue;
Chris Lattner1873d0c2009-06-27 04:06:41 +0000898
899 if (TargetRegisterInfo::isPhysicalRegister(Reg) &&
900 !allocatableRegs_[Reg])
901 continue;
Evan Chengd70dbb52008-02-22 09:24:50 +0000902 // FIXME: For now, only remat MI with at most one register operand.
903 assert(!RegOp &&
904 "Can't rematerialize instruction with multiple register operand!");
905 RegOp = MO.getReg();
Dan Gohman6d69ba82008-07-25 00:02:30 +0000906#ifndef NDEBUG
Evan Chengd70dbb52008-02-22 09:24:50 +0000907 break;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000908#endif
Evan Chengd70dbb52008-02-22 09:24:50 +0000909 }
910 return RegOp;
911}
912
913/// isValNoAvailableAt - Return true if the val# of the specified interval
914/// which reaches the given instruction also reaches the specified use index.
915bool LiveIntervals::isValNoAvailableAt(const LiveInterval &li, MachineInstr *MI,
Lang Hames233a60e2009-11-03 23:52:08 +0000916 SlotIndex UseIdx) const {
917 SlotIndex Index = getInstructionIndex(MI);
Evan Chengd70dbb52008-02-22 09:24:50 +0000918 VNInfo *ValNo = li.FindLiveRangeContaining(Index)->valno;
919 LiveInterval::const_iterator UI = li.FindLiveRangeContaining(UseIdx);
920 return UI != li.end() && UI->valno == ValNo;
921}
922
Evan Chengf2fbca62007-11-12 06:35:08 +0000923/// isReMaterializable - Returns true if the definition MI of the specified
924/// val# of the specified interval is re-materializable.
925bool LiveIntervals::isReMaterializable(const LiveInterval &li,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000926 const VNInfo *ValNo, MachineInstr *MI,
Evan Chengdc377862008-09-30 15:44:16 +0000927 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Cheng5ef3a042007-12-06 00:01:56 +0000928 bool &isLoad) {
Evan Chengf2fbca62007-11-12 06:35:08 +0000929 if (DisableReMat)
930 return false;
931
Dan Gohmana70dca12009-10-09 23:27:56 +0000932 if (!tii_->isTriviallyReMaterializable(MI, aa_))
933 return false;
Evan Chengdd3465e2008-02-23 01:44:27 +0000934
Dan Gohmana70dca12009-10-09 23:27:56 +0000935 // Target-specific code can mark an instruction as being rematerializable
936 // if it has one virtual reg use, though it had better be something like
937 // a PIC base register which is likely to be live everywhere.
Dan Gohman6d69ba82008-07-25 00:02:30 +0000938 unsigned ImpUse = getReMatImplicitUse(li, MI);
939 if (ImpUse) {
940 const LiveInterval &ImpLi = getInterval(ImpUse);
941 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
942 re = mri_->use_end(); ri != re; ++ri) {
943 MachineInstr *UseMI = &*ri;
Lang Hames233a60e2009-11-03 23:52:08 +0000944 SlotIndex UseIdx = getInstructionIndex(UseMI);
Dan Gohman6d69ba82008-07-25 00:02:30 +0000945 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
946 continue;
947 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
948 return false;
949 }
Evan Chengdc377862008-09-30 15:44:16 +0000950
951 // If a register operand of the re-materialized instruction is going to
952 // be spilled next, then it's not legal to re-materialize this instruction.
953 for (unsigned i = 0, e = SpillIs.size(); i != e; ++i)
954 if (ImpUse == SpillIs[i]->reg)
955 return false;
Dan Gohman6d69ba82008-07-25 00:02:30 +0000956 }
957 return true;
Evan Cheng5ef3a042007-12-06 00:01:56 +0000958}
959
Evan Cheng06587492008-10-24 02:05:00 +0000960/// isReMaterializable - Returns true if the definition MI of the specified
961/// val# of the specified interval is re-materializable.
962bool LiveIntervals::isReMaterializable(const LiveInterval &li,
963 const VNInfo *ValNo, MachineInstr *MI) {
964 SmallVector<LiveInterval*, 4> Dummy1;
965 bool Dummy2;
966 return isReMaterializable(li, ValNo, MI, Dummy1, Dummy2);
967}
968
Evan Cheng5ef3a042007-12-06 00:01:56 +0000969/// isReMaterializable - Returns true if every definition of MI of every
970/// val# of the specified interval is re-materializable.
Evan Chengdc377862008-09-30 15:44:16 +0000971bool LiveIntervals::isReMaterializable(const LiveInterval &li,
972 SmallVectorImpl<LiveInterval*> &SpillIs,
973 bool &isLoad) {
Evan Cheng5ef3a042007-12-06 00:01:56 +0000974 isLoad = false;
975 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
976 i != e; ++i) {
977 const VNInfo *VNI = *i;
Lang Hames857c4e02009-06-17 21:01:20 +0000978 if (VNI->isUnused())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000979 continue; // Dead val#.
980 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +0000981 if (!VNI->isDefAccurate())
Evan Cheng5ef3a042007-12-06 00:01:56 +0000982 return false;
Lang Hames857c4e02009-06-17 21:01:20 +0000983 MachineInstr *ReMatDefMI = getInstructionFromIndex(VNI->def);
Evan Cheng5ef3a042007-12-06 00:01:56 +0000984 bool DefIsLoad = false;
Evan Chengd70dbb52008-02-22 09:24:50 +0000985 if (!ReMatDefMI ||
Evan Chengdc377862008-09-30 15:44:16 +0000986 !isReMaterializable(li, VNI, ReMatDefMI, SpillIs, DefIsLoad))
Evan Cheng5ef3a042007-12-06 00:01:56 +0000987 return false;
988 isLoad |= DefIsLoad;
Evan Chengf2fbca62007-11-12 06:35:08 +0000989 }
990 return true;
991}
992
Evan Cheng79a0c1e2008-02-25 08:50:41 +0000993/// FilterFoldedOps - Filter out two-address use operands. Return
994/// true if it finds any issue with the operands that ought to prevent
995/// folding.
996static bool FilterFoldedOps(MachineInstr *MI,
997 SmallVector<unsigned, 2> &Ops,
998 unsigned &MRInfo,
999 SmallVector<unsigned, 2> &FoldOps) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001000 MRInfo = 0;
Evan Chengaee4af62007-12-02 08:30:39 +00001001 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1002 unsigned OpIdx = Ops[i];
Evan Chengd70dbb52008-02-22 09:24:50 +00001003 MachineOperand &MO = MI->getOperand(OpIdx);
Evan Chengaee4af62007-12-02 08:30:39 +00001004 // FIXME: fold subreg use.
Evan Chengd70dbb52008-02-22 09:24:50 +00001005 if (MO.getSubReg())
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001006 return true;
Evan Chengd70dbb52008-02-22 09:24:50 +00001007 if (MO.isDef())
Evan Chengaee4af62007-12-02 08:30:39 +00001008 MRInfo |= (unsigned)VirtRegMap::isMod;
1009 else {
1010 // Filter out two-address use operand(s).
Evan Chenga24752f2009-03-19 20:30:06 +00001011 if (MI->isRegTiedToDefOperand(OpIdx)) {
Evan Chengaee4af62007-12-02 08:30:39 +00001012 MRInfo = VirtRegMap::isModRef;
1013 continue;
1014 }
1015 MRInfo |= (unsigned)VirtRegMap::isRef;
1016 }
1017 FoldOps.push_back(OpIdx);
Evan Chenge62f97c2007-12-01 02:07:52 +00001018 }
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001019 return false;
1020}
1021
1022
1023/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
1024/// slot / to reg or any rematerialized load into ith operand of specified
1025/// MI. If it is successul, MI is updated with the newly created MI and
1026/// returns true.
1027bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
1028 VirtRegMap &vrm, MachineInstr *DefMI,
Lang Hames233a60e2009-11-03 23:52:08 +00001029 SlotIndex InstrIdx,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001030 SmallVector<unsigned, 2> &Ops,
1031 bool isSS, int Slot, unsigned Reg) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001032 // If it is an implicit def instruction, just delete it.
Evan Cheng20ccded2008-03-15 00:19:36 +00001033 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001034 RemoveMachineInstrFromMaps(MI);
1035 vrm.RemoveMachineInstrFromMaps(MI);
1036 MI->eraseFromParent();
1037 ++numFolds;
1038 return true;
1039 }
1040
1041 // Filter the list of operand indexes that are to be folded. Abort if
1042 // any operand will prevent folding.
1043 unsigned MRInfo = 0;
1044 SmallVector<unsigned, 2> FoldOps;
1045 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1046 return false;
Evan Chenge62f97c2007-12-01 02:07:52 +00001047
Evan Cheng427f4c12008-03-31 23:19:51 +00001048 // The only time it's safe to fold into a two address instruction is when
1049 // it's folding reload and spill from / into a spill stack slot.
1050 if (DefMI && (MRInfo & VirtRegMap::isMod))
Evan Cheng249ded32008-02-23 03:38:34 +00001051 return false;
1052
Evan Chengf2f8c2a2008-02-08 22:05:27 +00001053 MachineInstr *fmi = isSS ? tii_->foldMemoryOperand(*mf_, MI, FoldOps, Slot)
1054 : tii_->foldMemoryOperand(*mf_, MI, FoldOps, DefMI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001055 if (fmi) {
Evan Chengd3653122008-02-27 03:04:06 +00001056 // Remember this instruction uses the spill slot.
1057 if (isSS) vrm.addSpillSlotUse(Slot, fmi);
1058
Evan Chengf2fbca62007-11-12 06:35:08 +00001059 // Attempt to fold the memory reference into the instruction. If
1060 // we can do this, we don't need to insert spill code.
Evan Chengf2fbca62007-11-12 06:35:08 +00001061 MachineBasicBlock &MBB = *MI->getParent();
Evan Cheng84802932008-01-10 08:24:38 +00001062 if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
Evan Chengaee4af62007-12-02 08:30:39 +00001063 vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
Evan Cheng81a03822007-11-17 00:40:40 +00001064 vrm.transferSpillPts(MI, fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001065 vrm.transferRestorePts(MI, fmi);
Evan Chengc1f53c72008-03-11 21:34:46 +00001066 vrm.transferEmergencySpills(MI, fmi);
Lang Hames233a60e2009-11-03 23:52:08 +00001067 ReplaceMachineInstrInMaps(MI, fmi);
Evan Chengf2fbca62007-11-12 06:35:08 +00001068 MI = MBB.insert(MBB.erase(MI), fmi);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001069 ++numFolds;
Evan Chengf2fbca62007-11-12 06:35:08 +00001070 return true;
1071 }
1072 return false;
1073}
1074
Evan Cheng018f9b02007-12-05 03:22:34 +00001075/// canFoldMemoryOperand - Returns true if the specified load / store
1076/// folding is possible.
1077bool LiveIntervals::canFoldMemoryOperand(MachineInstr *MI,
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001078 SmallVector<unsigned, 2> &Ops,
Evan Cheng3c75ba82008-04-01 21:37:32 +00001079 bool ReMat) const {
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001080 // Filter the list of operand indexes that are to be folded. Abort if
1081 // any operand will prevent folding.
1082 unsigned MRInfo = 0;
Evan Cheng018f9b02007-12-05 03:22:34 +00001083 SmallVector<unsigned, 2> FoldOps;
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001084 if (FilterFoldedOps(MI, Ops, MRInfo, FoldOps))
1085 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001086
Evan Cheng3c75ba82008-04-01 21:37:32 +00001087 // It's only legal to remat for a use, not a def.
1088 if (ReMat && (MRInfo & VirtRegMap::isMod))
Evan Cheng79a0c1e2008-02-25 08:50:41 +00001089 return false;
Evan Cheng018f9b02007-12-05 03:22:34 +00001090
Evan Chengd70dbb52008-02-22 09:24:50 +00001091 return tii_->canFoldMemoryOperand(MI, FoldOps);
1092}
1093
Evan Cheng81a03822007-11-17 00:40:40 +00001094bool LiveIntervals::intervalIsInOneMBB(const LiveInterval &li) const {
Lang Hames233a60e2009-11-03 23:52:08 +00001095 LiveInterval::Ranges::const_iterator itr = li.ranges.begin();
1096
1097 MachineBasicBlock *mbb = indexes_->getMBBCoveringRange(itr->start, itr->end);
1098
1099 if (mbb == 0)
1100 return false;
1101
1102 for (++itr; itr != li.ranges.end(); ++itr) {
1103 MachineBasicBlock *mbb2 =
1104 indexes_->getMBBCoveringRange(itr->start, itr->end);
1105
1106 if (mbb2 != mbb)
Evan Cheng81a03822007-11-17 00:40:40 +00001107 return false;
1108 }
Lang Hames233a60e2009-11-03 23:52:08 +00001109
Evan Cheng81a03822007-11-17 00:40:40 +00001110 return true;
1111}
1112
Evan Chengd70dbb52008-02-22 09:24:50 +00001113/// rewriteImplicitOps - Rewrite implicit use operands of MI (i.e. uses of
1114/// interval on to-be re-materialized operands of MI) with new register.
1115void LiveIntervals::rewriteImplicitOps(const LiveInterval &li,
1116 MachineInstr *MI, unsigned NewVReg,
1117 VirtRegMap &vrm) {
1118 // There is an implicit use. That means one of the other operand is
1119 // being remat'ed and the remat'ed instruction has li.reg as an
1120 // use operand. Make sure we rewrite that as well.
1121 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1122 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001123 if (!MO.isReg())
Evan Chengd70dbb52008-02-22 09:24:50 +00001124 continue;
1125 unsigned Reg = MO.getReg();
1126 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
1127 continue;
1128 if (!vrm.isReMaterialized(Reg))
1129 continue;
1130 MachineInstr *ReMatMI = vrm.getReMaterializedMI(Reg);
Evan Cheng6130f662008-03-05 00:59:57 +00001131 MachineOperand *UseMO = ReMatMI->findRegisterUseOperand(li.reg);
1132 if (UseMO)
1133 UseMO->setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001134 }
1135}
1136
Evan Chengf2fbca62007-11-12 06:35:08 +00001137/// rewriteInstructionForSpills, rewriteInstructionsForSpills - Helper functions
1138/// for addIntervalsForSpills to rewrite uses / defs for the given live range.
Evan Cheng018f9b02007-12-05 03:22:34 +00001139bool LiveIntervals::
Evan Chengd70dbb52008-02-22 09:24:50 +00001140rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
Lang Hames233a60e2009-11-03 23:52:08 +00001141 bool TrySplit, SlotIndex index, SlotIndex end,
Lang Hames86511252009-09-04 20:41:11 +00001142 MachineInstr *MI,
Evan Cheng81a03822007-11-17 00:40:40 +00001143 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001144 unsigned Slot, int LdSlot,
1145 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001146 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001147 const TargetRegisterClass* rc,
1148 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001149 const MachineLoopInfo *loopInfo,
Evan Cheng313d4b82008-02-23 00:33:04 +00001150 unsigned &NewVReg, unsigned ImpUse, bool &HasDef, bool &HasUse,
Owen Anderson28998312008-08-13 22:28:50 +00001151 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001152 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001153 bool CanFold = false;
Evan Chengf2fbca62007-11-12 06:35:08 +00001154 RestartInstruction:
1155 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1156 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001157 if (!mop.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001158 continue;
1159 unsigned Reg = mop.getReg();
1160 unsigned RegI = Reg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001161 if (Reg == 0 || TargetRegisterInfo::isPhysicalRegister(Reg))
Evan Chengf2fbca62007-11-12 06:35:08 +00001162 continue;
Evan Chengf2fbca62007-11-12 06:35:08 +00001163 if (Reg != li.reg)
1164 continue;
1165
1166 bool TryFold = !DefIsReMat;
Evan Chengcb3c3302007-11-29 23:02:50 +00001167 bool FoldSS = true; // Default behavior unless it's a remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001168 int FoldSlot = Slot;
1169 if (DefIsReMat) {
1170 // If this is the rematerializable definition MI itself and
1171 // all of its uses are rematerialized, simply delete it.
Evan Cheng81a03822007-11-17 00:40:40 +00001172 if (MI == ReMatOrigDefMI && CanDelete) {
Bill Wendling8e6179f2009-08-22 20:18:03 +00001173 DEBUG(errs() << "\t\t\t\tErasing re-materlizable def: "
1174 << MI << '\n');
Evan Chengf2fbca62007-11-12 06:35:08 +00001175 RemoveMachineInstrFromMaps(MI);
Evan Chengcada2452007-11-28 01:28:46 +00001176 vrm.RemoveMachineInstrFromMaps(MI);
Evan Chengf2fbca62007-11-12 06:35:08 +00001177 MI->eraseFromParent();
1178 break;
1179 }
1180
1181 // If def for this use can't be rematerialized, then try folding.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001182 // If def is rematerializable and it's a load, also try folding.
Evan Chengcb3c3302007-11-29 23:02:50 +00001183 TryFold = !ReMatDefMI || (ReMatDefMI && (MI == ReMatOrigDefMI || isLoad));
Evan Chengf2fbca62007-11-12 06:35:08 +00001184 if (isLoad) {
1185 // Try fold loads (from stack slot, constant pool, etc.) into uses.
1186 FoldSS = isLoadSS;
1187 FoldSlot = LdSlot;
1188 }
1189 }
1190
Evan Chengf2fbca62007-11-12 06:35:08 +00001191 // Scan all of the operands of this instruction rewriting operands
1192 // to use NewVReg instead of li.reg as appropriate. We do this for
1193 // two reasons:
1194 //
1195 // 1. If the instr reads the same spilled vreg multiple times, we
1196 // want to reuse the NewVReg.
1197 // 2. If the instr is a two-addr instruction, we are required to
1198 // keep the src/dst regs pinned.
1199 //
1200 // Keep track of whether we replace a use and/or def so that we can
1201 // create the spill interval with the appropriate range.
Evan Chengcddbb832007-11-30 21:23:43 +00001202
Evan Cheng81a03822007-11-17 00:40:40 +00001203 HasUse = mop.isUse();
1204 HasDef = mop.isDef();
Evan Chengaee4af62007-12-02 08:30:39 +00001205 SmallVector<unsigned, 2> Ops;
1206 Ops.push_back(i);
Evan Chengf2fbca62007-11-12 06:35:08 +00001207 for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
Evan Chengaee4af62007-12-02 08:30:39 +00001208 const MachineOperand &MOj = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001209 if (!MOj.isReg())
Evan Chengf2fbca62007-11-12 06:35:08 +00001210 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001211 unsigned RegJ = MOj.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001212 if (RegJ == 0 || TargetRegisterInfo::isPhysicalRegister(RegJ))
Evan Chengf2fbca62007-11-12 06:35:08 +00001213 continue;
1214 if (RegJ == RegI) {
Evan Chengaee4af62007-12-02 08:30:39 +00001215 Ops.push_back(j);
Evan Chengd129d732009-07-17 19:43:40 +00001216 if (!MOj.isUndef()) {
1217 HasUse |= MOj.isUse();
1218 HasDef |= MOj.isDef();
1219 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001220 }
1221 }
1222
David Greene26b86a02008-10-27 17:38:59 +00001223 // Create a new virtual register for the spill interval.
1224 // Create the new register now so we can map the fold instruction
1225 // to the new register so when it is unfolded we get the correct
1226 // answer.
1227 bool CreatedNewVReg = false;
1228 if (NewVReg == 0) {
1229 NewVReg = mri_->createVirtualRegister(rc);
1230 vrm.grow();
1231 CreatedNewVReg = true;
1232 }
1233
Evan Cheng9c3c2212008-06-06 07:54:39 +00001234 if (!TryFold)
1235 CanFold = false;
1236 else {
Evan Cheng018f9b02007-12-05 03:22:34 +00001237 // Do not fold load / store here if we are splitting. We'll find an
1238 // optimal point to insert a load / store later.
1239 if (!TrySplit) {
1240 if (tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
David Greene26b86a02008-10-27 17:38:59 +00001241 Ops, FoldSS, FoldSlot, NewVReg)) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001242 // Folding the load/store can completely change the instruction in
1243 // unpredictable ways, rescan it from the beginning.
David Greene26b86a02008-10-27 17:38:59 +00001244
1245 if (FoldSS) {
1246 // We need to give the new vreg the same stack slot as the
1247 // spilled interval.
1248 vrm.assignVirt2StackSlot(NewVReg, FoldSlot);
1249 }
1250
Evan Cheng018f9b02007-12-05 03:22:34 +00001251 HasUse = false;
1252 HasDef = false;
1253 CanFold = false;
Evan Chengc781a242009-05-03 18:32:42 +00001254 if (isNotInMIMap(MI))
Evan Cheng7e073ba2008-04-09 20:57:25 +00001255 break;
Evan Cheng018f9b02007-12-05 03:22:34 +00001256 goto RestartInstruction;
1257 }
1258 } else {
Evan Cheng9c3c2212008-06-06 07:54:39 +00001259 // We'll try to fold it later if it's profitable.
Evan Cheng3c75ba82008-04-01 21:37:32 +00001260 CanFold = canFoldMemoryOperand(MI, Ops, DefIsReMat);
Evan Cheng018f9b02007-12-05 03:22:34 +00001261 }
Evan Cheng9c3c2212008-06-06 07:54:39 +00001262 }
Evan Chengcddbb832007-11-30 21:23:43 +00001263
Evan Chengcddbb832007-11-30 21:23:43 +00001264 mop.setReg(NewVReg);
Evan Chengd70dbb52008-02-22 09:24:50 +00001265 if (mop.isImplicit())
1266 rewriteImplicitOps(li, MI, NewVReg, vrm);
Evan Chengcddbb832007-11-30 21:23:43 +00001267
1268 // Reuse NewVReg for other reads.
Evan Chengd70dbb52008-02-22 09:24:50 +00001269 for (unsigned j = 0, e = Ops.size(); j != e; ++j) {
1270 MachineOperand &mopj = MI->getOperand(Ops[j]);
1271 mopj.setReg(NewVReg);
1272 if (mopj.isImplicit())
1273 rewriteImplicitOps(li, MI, NewVReg, vrm);
1274 }
Evan Chengcddbb832007-11-30 21:23:43 +00001275
Evan Cheng81a03822007-11-17 00:40:40 +00001276 if (CreatedNewVReg) {
1277 if (DefIsReMat) {
Evan Cheng37844532009-07-16 09:20:10 +00001278 vrm.setVirtIsReMaterialized(NewVReg, ReMatDefMI);
Evan Chengd70dbb52008-02-22 09:24:50 +00001279 if (ReMatIds[VNI->id] == VirtRegMap::MAX_STACK_SLOT) {
Evan Cheng81a03822007-11-17 00:40:40 +00001280 // Each valnum may have its own remat id.
Evan Chengd70dbb52008-02-22 09:24:50 +00001281 ReMatIds[VNI->id] = vrm.assignVirtReMatId(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001282 } else {
Evan Chengd70dbb52008-02-22 09:24:50 +00001283 vrm.assignVirtReMatId(NewVReg, ReMatIds[VNI->id]);
Evan Cheng81a03822007-11-17 00:40:40 +00001284 }
1285 if (!CanDelete || (HasUse && HasDef)) {
1286 // If this is a two-addr instruction then its use operands are
1287 // rematerializable but its def is not. It should be assigned a
1288 // stack slot.
1289 vrm.assignVirt2StackSlot(NewVReg, Slot);
1290 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001291 } else {
Evan Chengf2fbca62007-11-12 06:35:08 +00001292 vrm.assignVirt2StackSlot(NewVReg, Slot);
1293 }
Evan Chengcb3c3302007-11-29 23:02:50 +00001294 } else if (HasUse && HasDef &&
1295 vrm.getStackSlot(NewVReg) == VirtRegMap::NO_STACK_SLOT) {
1296 // If this interval hasn't been assigned a stack slot (because earlier
1297 // def is a deleted remat def), do it now.
1298 assert(Slot != VirtRegMap::NO_STACK_SLOT);
1299 vrm.assignVirt2StackSlot(NewVReg, Slot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001300 }
1301
Evan Cheng313d4b82008-02-23 00:33:04 +00001302 // Re-matting an instruction with virtual register use. Add the
1303 // register as an implicit use on the use MI.
1304 if (DefIsReMat && ImpUse)
1305 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
1306
Evan Cheng5b69eba2009-04-21 22:46:52 +00001307 // Create a new register interval for this spill / remat.
Evan Chengf2fbca62007-11-12 06:35:08 +00001308 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001309 if (CreatedNewVReg) {
1310 NewLIs.push_back(&nI);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001311 MBBVRegsMap.insert(std::make_pair(MI->getParent()->getNumber(), NewVReg));
Evan Cheng81a03822007-11-17 00:40:40 +00001312 if (TrySplit)
1313 vrm.setIsSplitFromReg(NewVReg, li.reg);
1314 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001315
1316 if (HasUse) {
Evan Cheng81a03822007-11-17 00:40:40 +00001317 if (CreatedNewVReg) {
Lang Hames233a60e2009-11-03 23:52:08 +00001318 LiveRange LR(index.getLoadIndex(), index.getDefIndex(),
1319 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001320 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001321 nI.addRange(LR);
1322 } else {
1323 // Extend the split live interval to this def / use.
Lang Hames233a60e2009-11-03 23:52:08 +00001324 SlotIndex End = index.getDefIndex();
Evan Cheng81a03822007-11-17 00:40:40 +00001325 LiveRange LR(nI.ranges[nI.ranges.size()-1].end, End,
1326 nI.getValNumInfo(nI.getNumValNums()-1));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001327 DEBUG(errs() << " +" << LR);
Evan Cheng81a03822007-11-17 00:40:40 +00001328 nI.addRange(LR);
1329 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001330 }
1331 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001332 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1333 nI.getNextValue(SlotIndex(), 0, false, VNInfoAllocator));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001334 DEBUG(errs() << " +" << LR);
Evan Chengf2fbca62007-11-12 06:35:08 +00001335 nI.addRange(LR);
1336 }
Evan Cheng81a03822007-11-17 00:40:40 +00001337
Bill Wendling8e6179f2009-08-22 20:18:03 +00001338 DEBUG({
1339 errs() << "\t\t\t\tAdded new interval: ";
1340 nI.print(errs(), tri_);
1341 errs() << '\n';
1342 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001343 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001344 return CanFold;
Evan Chengf2fbca62007-11-12 06:35:08 +00001345}
Evan Cheng81a03822007-11-17 00:40:40 +00001346bool LiveIntervals::anyKillInMBBAfterIdx(const LiveInterval &li,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001347 const VNInfo *VNI,
Lang Hames86511252009-09-04 20:41:11 +00001348 MachineBasicBlock *MBB,
Lang Hames233a60e2009-11-03 23:52:08 +00001349 SlotIndex Idx) const {
1350 SlotIndex End = getMBBEndIdx(MBB);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001351 for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
Lang Hames233a60e2009-11-03 23:52:08 +00001352 if (VNI->kills[j].isPHI())
Lang Hamesffd13262009-07-09 03:57:02 +00001353 continue;
1354
Lang Hames233a60e2009-11-03 23:52:08 +00001355 SlotIndex KillIdx = VNI->kills[j];
Evan Cheng0cbb1162007-11-29 01:06:25 +00001356 if (KillIdx > Idx && KillIdx < End)
1357 return true;
Evan Cheng81a03822007-11-17 00:40:40 +00001358 }
1359 return false;
1360}
1361
Evan Cheng063284c2008-02-21 00:34:19 +00001362/// RewriteInfo - Keep track of machine instrs that will be rewritten
1363/// during spilling.
Dan Gohman844731a2008-05-13 00:00:25 +00001364namespace {
1365 struct RewriteInfo {
Lang Hames233a60e2009-11-03 23:52:08 +00001366 SlotIndex Index;
Dan Gohman844731a2008-05-13 00:00:25 +00001367 MachineInstr *MI;
1368 bool HasUse;
1369 bool HasDef;
Lang Hames233a60e2009-11-03 23:52:08 +00001370 RewriteInfo(SlotIndex i, MachineInstr *mi, bool u, bool d)
Dan Gohman844731a2008-05-13 00:00:25 +00001371 : Index(i), MI(mi), HasUse(u), HasDef(d) {}
1372 };
Evan Cheng063284c2008-02-21 00:34:19 +00001373
Dan Gohman844731a2008-05-13 00:00:25 +00001374 struct RewriteInfoCompare {
1375 bool operator()(const RewriteInfo &LHS, const RewriteInfo &RHS) const {
1376 return LHS.Index < RHS.Index;
1377 }
1378 };
1379}
Evan Cheng063284c2008-02-21 00:34:19 +00001380
Evan Chengf2fbca62007-11-12 06:35:08 +00001381void LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001382rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
Evan Chengf2fbca62007-11-12 06:35:08 +00001383 LiveInterval::Ranges::const_iterator &I,
Evan Cheng81a03822007-11-17 00:40:40 +00001384 MachineInstr *ReMatOrigDefMI, MachineInstr *ReMatDefMI,
Evan Chengf2fbca62007-11-12 06:35:08 +00001385 unsigned Slot, int LdSlot,
1386 bool isLoad, bool isLoadSS, bool DefIsReMat, bool CanDelete,
Evan Chengd70dbb52008-02-22 09:24:50 +00001387 VirtRegMap &vrm,
Evan Chengf2fbca62007-11-12 06:35:08 +00001388 const TargetRegisterClass* rc,
1389 SmallVector<int, 4> &ReMatIds,
Evan Cheng22f07ff2007-12-11 02:09:15 +00001390 const MachineLoopInfo *loopInfo,
Evan Cheng81a03822007-11-17 00:40:40 +00001391 BitVector &SpillMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001392 DenseMap<unsigned, std::vector<SRInfo> > &SpillIdxes,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001393 BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001394 DenseMap<unsigned, std::vector<SRInfo> > &RestoreIdxes,
1395 DenseMap<unsigned,unsigned> &MBBVRegsMap,
Evan Chengc781a242009-05-03 18:32:42 +00001396 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001397 bool AllCanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001398 unsigned NewVReg = 0;
Lang Hames233a60e2009-11-03 23:52:08 +00001399 SlotIndex start = I->start.getBaseIndex();
1400 SlotIndex end = I->end.getPrevSlot().getBaseIndex().getNextIndex();
Evan Chengf2fbca62007-11-12 06:35:08 +00001401
Evan Cheng063284c2008-02-21 00:34:19 +00001402 // First collect all the def / use in this live range that will be rewritten.
Evan Cheng7e073ba2008-04-09 20:57:25 +00001403 // Make sure they are sorted according to instruction index.
Evan Cheng063284c2008-02-21 00:34:19 +00001404 std::vector<RewriteInfo> RewriteMIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001405 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1406 re = mri_->reg_end(); ri != re; ) {
Evan Cheng419852c2008-04-03 16:39:43 +00001407 MachineInstr *MI = &*ri;
Evan Cheng063284c2008-02-21 00:34:19 +00001408 MachineOperand &O = ri.getOperand();
1409 ++ri;
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001410 assert(!O.isImplicit() && "Spilling register that's used as implicit use?");
Lang Hames233a60e2009-11-03 23:52:08 +00001411 SlotIndex index = getInstructionIndex(MI);
Evan Cheng063284c2008-02-21 00:34:19 +00001412 if (index < start || index >= end)
1413 continue;
Evan Chengd129d732009-07-17 19:43:40 +00001414
1415 if (O.isUndef())
Evan Cheng79a796c2008-07-12 01:56:02 +00001416 // Must be defined by an implicit def. It should not be spilled. Note,
1417 // this is for correctness reason. e.g.
1418 // 8 %reg1024<def> = IMPLICIT_DEF
1419 // 12 %reg1024<def> = INSERT_SUBREG %reg1024<kill>, %reg1025, 2
1420 // The live range [12, 14) are not part of the r1024 live interval since
1421 // it's defined by an implicit def. It will not conflicts with live
1422 // interval of r1025. Now suppose both registers are spilled, you can
Evan Chengb9890ae2008-07-12 02:22:07 +00001423 // easily see a situation where both registers are reloaded before
Evan Cheng79a796c2008-07-12 01:56:02 +00001424 // the INSERT_SUBREG and both target registers that would overlap.
1425 continue;
Evan Cheng063284c2008-02-21 00:34:19 +00001426 RewriteMIs.push_back(RewriteInfo(index, MI, O.isUse(), O.isDef()));
1427 }
1428 std::sort(RewriteMIs.begin(), RewriteMIs.end(), RewriteInfoCompare());
1429
Evan Cheng313d4b82008-02-23 00:33:04 +00001430 unsigned ImpUse = DefIsReMat ? getReMatImplicitUse(li, ReMatDefMI) : 0;
Evan Cheng063284c2008-02-21 00:34:19 +00001431 // Now rewrite the defs and uses.
1432 for (unsigned i = 0, e = RewriteMIs.size(); i != e; ) {
1433 RewriteInfo &rwi = RewriteMIs[i];
1434 ++i;
Lang Hames233a60e2009-11-03 23:52:08 +00001435 SlotIndex index = rwi.Index;
Evan Cheng063284c2008-02-21 00:34:19 +00001436 bool MIHasUse = rwi.HasUse;
1437 bool MIHasDef = rwi.HasDef;
1438 MachineInstr *MI = rwi.MI;
1439 // If MI def and/or use the same register multiple times, then there
1440 // are multiple entries.
Evan Cheng313d4b82008-02-23 00:33:04 +00001441 unsigned NumUses = MIHasUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001442 while (i != e && RewriteMIs[i].MI == MI) {
1443 assert(RewriteMIs[i].Index == index);
Evan Cheng313d4b82008-02-23 00:33:04 +00001444 bool isUse = RewriteMIs[i].HasUse;
1445 if (isUse) ++NumUses;
1446 MIHasUse |= isUse;
Evan Cheng063284c2008-02-21 00:34:19 +00001447 MIHasDef |= RewriteMIs[i].HasDef;
1448 ++i;
1449 }
Evan Cheng81a03822007-11-17 00:40:40 +00001450 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng313d4b82008-02-23 00:33:04 +00001451
Evan Cheng0a891ed2008-05-23 23:00:04 +00001452 if (ImpUse && MI != ReMatDefMI) {
Evan Cheng313d4b82008-02-23 00:33:04 +00001453 // Re-matting an instruction with virtual register use. Update the
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001454 // register interval's spill weight to HUGE_VALF to prevent it from
1455 // being spilled.
Evan Cheng313d4b82008-02-23 00:33:04 +00001456 LiveInterval &ImpLi = getInterval(ImpUse);
Evan Cheng24d2f8a2008-03-31 07:53:30 +00001457 ImpLi.weight = HUGE_VALF;
Evan Cheng313d4b82008-02-23 00:33:04 +00001458 }
1459
Evan Cheng063284c2008-02-21 00:34:19 +00001460 unsigned MBBId = MBB->getNumber();
Evan Cheng018f9b02007-12-05 03:22:34 +00001461 unsigned ThisVReg = 0;
Evan Cheng70306f82007-12-03 09:58:48 +00001462 if (TrySplit) {
Owen Anderson28998312008-08-13 22:28:50 +00001463 DenseMap<unsigned,unsigned>::iterator NVI = MBBVRegsMap.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001464 if (NVI != MBBVRegsMap.end()) {
Evan Cheng018f9b02007-12-05 03:22:34 +00001465 ThisVReg = NVI->second;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001466 // One common case:
1467 // x = use
1468 // ...
1469 // ...
1470 // def = ...
1471 // = use
1472 // It's better to start a new interval to avoid artifically
1473 // extend the new interval.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001474 if (MIHasDef && !MIHasUse) {
1475 MBBVRegsMap.erase(MBB->getNumber());
Evan Cheng018f9b02007-12-05 03:22:34 +00001476 ThisVReg = 0;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001477 }
1478 }
Evan Chengcada2452007-11-28 01:28:46 +00001479 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001480
1481 bool IsNew = ThisVReg == 0;
1482 if (IsNew) {
1483 // This ends the previous live interval. If all of its def / use
1484 // can be folded, give it a low spill weight.
1485 if (NewVReg && TrySplit && AllCanFold) {
1486 LiveInterval &nI = getOrCreateInterval(NewVReg);
1487 nI.weight /= 10.0F;
1488 }
1489 AllCanFold = true;
1490 }
1491 NewVReg = ThisVReg;
1492
Evan Cheng81a03822007-11-17 00:40:40 +00001493 bool HasDef = false;
1494 bool HasUse = false;
Evan Chengd70dbb52008-02-22 09:24:50 +00001495 bool CanFold = rewriteInstructionForSpills(li, I->valno, TrySplit,
Evan Cheng9c3c2212008-06-06 07:54:39 +00001496 index, end, MI, ReMatOrigDefMI, ReMatDefMI,
1497 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
1498 CanDelete, vrm, rc, ReMatIds, loopInfo, NewVReg,
Evan Chengc781a242009-05-03 18:32:42 +00001499 ImpUse, HasDef, HasUse, MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001500 if (!HasDef && !HasUse)
1501 continue;
1502
Evan Cheng018f9b02007-12-05 03:22:34 +00001503 AllCanFold &= CanFold;
1504
Evan Cheng81a03822007-11-17 00:40:40 +00001505 // Update weight of spill interval.
1506 LiveInterval &nI = getOrCreateInterval(NewVReg);
Evan Cheng70306f82007-12-03 09:58:48 +00001507 if (!TrySplit) {
Evan Cheng81a03822007-11-17 00:40:40 +00001508 // The spill weight is now infinity as it cannot be spilled again.
1509 nI.weight = HUGE_VALF;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001510 continue;
Evan Cheng81a03822007-11-17 00:40:40 +00001511 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001512
1513 // Keep track of the last def and first use in each MBB.
Evan Cheng0cbb1162007-11-29 01:06:25 +00001514 if (HasDef) {
1515 if (MI != ReMatOrigDefMI || !CanDelete) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001516 bool HasKill = false;
1517 if (!HasUse)
Lang Hames233a60e2009-11-03 23:52:08 +00001518 HasKill = anyKillInMBBAfterIdx(li, I->valno, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001519 else {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001520 // If this is a two-address code, then this index starts a new VNInfo.
Lang Hames233a60e2009-11-03 23:52:08 +00001521 const VNInfo *VNI = li.findDefinedVNInfoForRegInt(index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001522 if (VNI)
Lang Hames233a60e2009-11-03 23:52:08 +00001523 HasKill = anyKillInMBBAfterIdx(li, VNI, MBB, index.getDefIndex());
Evan Cheng0cbb1162007-11-29 01:06:25 +00001524 }
Owen Anderson28998312008-08-13 22:28:50 +00001525 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Chenge3110d02007-12-01 04:42:39 +00001526 SpillIdxes.find(MBBId);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001527 if (!HasKill) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001528 if (SII == SpillIdxes.end()) {
1529 std::vector<SRInfo> S;
1530 S.push_back(SRInfo(index, NewVReg, true));
1531 SpillIdxes.insert(std::make_pair(MBBId, S));
1532 } else if (SII->second.back().vreg != NewVReg) {
1533 SII->second.push_back(SRInfo(index, NewVReg, true));
Lang Hames86511252009-09-04 20:41:11 +00001534 } else if (index > SII->second.back().index) {
Evan Cheng0cbb1162007-11-29 01:06:25 +00001535 // If there is an earlier def and this is a two-address
1536 // instruction, then it's not possible to fold the store (which
1537 // would also fold the load).
Evan Cheng1953d0c2007-11-29 10:12:14 +00001538 SRInfo &Info = SII->second.back();
1539 Info.index = index;
1540 Info.canFold = !HasUse;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001541 }
1542 SpillMBBs.set(MBBId);
Evan Chenge3110d02007-12-01 04:42:39 +00001543 } else if (SII != SpillIdxes.end() &&
1544 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001545 index > SII->second.back().index) {
Evan Chenge3110d02007-12-01 04:42:39 +00001546 // There is an earlier def that's not killed (must be two-address).
1547 // The spill is no longer needed.
1548 SII->second.pop_back();
1549 if (SII->second.empty()) {
1550 SpillIdxes.erase(MBBId);
1551 SpillMBBs.reset(MBBId);
1552 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001553 }
1554 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001555 }
1556
1557 if (HasUse) {
Owen Anderson28998312008-08-13 22:28:50 +00001558 DenseMap<unsigned, std::vector<SRInfo> >::iterator SII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001559 SpillIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001560 if (SII != SpillIdxes.end() &&
1561 SII->second.back().vreg == NewVReg &&
Lang Hames86511252009-09-04 20:41:11 +00001562 index > SII->second.back().index)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001563 // Use(s) following the last def, it's not safe to fold the spill.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001564 SII->second.back().canFold = false;
Owen Anderson28998312008-08-13 22:28:50 +00001565 DenseMap<unsigned, std::vector<SRInfo> >::iterator RII =
Evan Cheng0cbb1162007-11-29 01:06:25 +00001566 RestoreIdxes.find(MBBId);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001567 if (RII != RestoreIdxes.end() && RII->second.back().vreg == NewVReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001568 // If we are splitting live intervals, only fold if it's the first
1569 // use and there isn't another use later in the MBB.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001570 RII->second.back().canFold = false;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001571 else if (IsNew) {
1572 // Only need a reload if there isn't an earlier def / use.
Evan Cheng1953d0c2007-11-29 10:12:14 +00001573 if (RII == RestoreIdxes.end()) {
1574 std::vector<SRInfo> Infos;
1575 Infos.push_back(SRInfo(index, NewVReg, true));
1576 RestoreIdxes.insert(std::make_pair(MBBId, Infos));
1577 } else {
1578 RII->second.push_back(SRInfo(index, NewVReg, true));
1579 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001580 RestoreMBBs.set(MBBId);
1581 }
1582 }
1583
1584 // Update spill weight.
Evan Cheng22f07ff2007-12-11 02:09:15 +00001585 unsigned loopDepth = loopInfo->getLoopDepth(MBB);
Evan Chengc3417602008-06-21 06:45:54 +00001586 nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
Evan Chengf2fbca62007-11-12 06:35:08 +00001587 }
Evan Cheng018f9b02007-12-05 03:22:34 +00001588
1589 if (NewVReg && TrySplit && AllCanFold) {
1590 // If all of its def / use can be folded, give it a low spill weight.
1591 LiveInterval &nI = getOrCreateInterval(NewVReg);
1592 nI.weight /= 10.0F;
1593 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001594}
1595
Lang Hames233a60e2009-11-03 23:52:08 +00001596bool LiveIntervals::alsoFoldARestore(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001597 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001598 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001599 if (!RestoreMBBs[Id])
1600 return false;
1601 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1602 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1603 if (Restores[i].index == index &&
1604 Restores[i].vreg == vr &&
1605 Restores[i].canFold)
1606 return true;
1607 return false;
1608}
1609
Lang Hames233a60e2009-11-03 23:52:08 +00001610void LiveIntervals::eraseRestoreInfo(int Id, SlotIndex index,
Lang Hames86511252009-09-04 20:41:11 +00001611 unsigned vr, BitVector &RestoreMBBs,
Owen Anderson28998312008-08-13 22:28:50 +00001612 DenseMap<unsigned,std::vector<SRInfo> > &RestoreIdxes) {
Evan Cheng1953d0c2007-11-29 10:12:14 +00001613 if (!RestoreMBBs[Id])
1614 return;
1615 std::vector<SRInfo> &Restores = RestoreIdxes[Id];
1616 for (unsigned i = 0, e = Restores.size(); i != e; ++i)
1617 if (Restores[i].index == index && Restores[i].vreg)
Lang Hames233a60e2009-11-03 23:52:08 +00001618 Restores[i].index = SlotIndex();
Evan Cheng1953d0c2007-11-29 10:12:14 +00001619}
Evan Cheng81a03822007-11-17 00:40:40 +00001620
Evan Cheng4cce6b42008-04-11 17:53:36 +00001621/// handleSpilledImpDefs - Remove IMPLICIT_DEF instructions which are being
1622/// spilled and create empty intervals for their uses.
1623void
1624LiveIntervals::handleSpilledImpDefs(const LiveInterval &li, VirtRegMap &vrm,
1625 const TargetRegisterClass* rc,
1626 std::vector<LiveInterval*> &NewLIs) {
Evan Cheng419852c2008-04-03 16:39:43 +00001627 for (MachineRegisterInfo::reg_iterator ri = mri_->reg_begin(li.reg),
1628 re = mri_->reg_end(); ri != re; ) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001629 MachineOperand &O = ri.getOperand();
Evan Cheng419852c2008-04-03 16:39:43 +00001630 MachineInstr *MI = &*ri;
1631 ++ri;
Evan Cheng4cce6b42008-04-11 17:53:36 +00001632 if (O.isDef()) {
1633 assert(MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF &&
1634 "Register def was not rewritten?");
1635 RemoveMachineInstrFromMaps(MI);
1636 vrm.RemoveMachineInstrFromMaps(MI);
1637 MI->eraseFromParent();
1638 } else {
1639 // This must be an use of an implicit_def so it's not part of the live
1640 // interval. Create a new empty live interval for it.
1641 // FIXME: Can we simply erase some of the instructions? e.g. Stores?
1642 unsigned NewVReg = mri_->createVirtualRegister(rc);
1643 vrm.grow();
1644 vrm.setIsImplicitlyDefined(NewVReg);
1645 NewLIs.push_back(&getOrCreateInterval(NewVReg));
1646 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1647 MachineOperand &MO = MI->getOperand(i);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001648 if (MO.isReg() && MO.getReg() == li.reg) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001649 MO.setReg(NewVReg);
Evan Cheng4784f1f2009-06-30 08:49:04 +00001650 MO.setIsUndef();
Evan Cheng4784f1f2009-06-30 08:49:04 +00001651 }
Evan Cheng4cce6b42008-04-11 17:53:36 +00001652 }
1653 }
Evan Cheng419852c2008-04-03 16:39:43 +00001654 }
1655}
1656
Evan Chengf2fbca62007-11-12 06:35:08 +00001657std::vector<LiveInterval*> LiveIntervals::
Owen Andersond6664312008-08-18 18:05:32 +00001658addIntervalsForSpillsFast(const LiveInterval &li,
1659 const MachineLoopInfo *loopInfo,
Evan Chengc781a242009-05-03 18:32:42 +00001660 VirtRegMap &vrm) {
Owen Anderson17197312008-08-18 23:41:04 +00001661 unsigned slot = vrm.assignVirt2StackSlot(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001662
1663 std::vector<LiveInterval*> added;
1664
1665 assert(li.weight != HUGE_VALF &&
1666 "attempt to spill already spilled interval!");
1667
Bill Wendling8e6179f2009-08-22 20:18:03 +00001668 DEBUG({
1669 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1670 li.dump();
1671 errs() << '\n';
1672 });
Owen Andersond6664312008-08-18 18:05:32 +00001673
1674 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
1675
Owen Andersona41e47a2008-08-19 22:12:11 +00001676 MachineRegisterInfo::reg_iterator RI = mri_->reg_begin(li.reg);
1677 while (RI != mri_->reg_end()) {
1678 MachineInstr* MI = &*RI;
1679
1680 SmallVector<unsigned, 2> Indices;
1681 bool HasUse = false;
1682 bool HasDef = false;
1683
1684 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
1685 MachineOperand& mop = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001686 if (!mop.isReg() || mop.getReg() != li.reg) continue;
Owen Andersona41e47a2008-08-19 22:12:11 +00001687
1688 HasUse |= MI->getOperand(i).isUse();
1689 HasDef |= MI->getOperand(i).isDef();
1690
1691 Indices.push_back(i);
1692 }
1693
1694 if (!tryFoldMemoryOperand(MI, vrm, NULL, getInstructionIndex(MI),
1695 Indices, true, slot, li.reg)) {
1696 unsigned NewVReg = mri_->createVirtualRegister(rc);
Owen Anderson9a032932008-08-18 21:20:32 +00001697 vrm.grow();
Owen Anderson17197312008-08-18 23:41:04 +00001698 vrm.assignVirt2StackSlot(NewVReg, slot);
1699
Owen Andersona41e47a2008-08-19 22:12:11 +00001700 // create a new register for this spill
1701 LiveInterval &nI = getOrCreateInterval(NewVReg);
Owen Andersond6664312008-08-18 18:05:32 +00001702
Owen Andersona41e47a2008-08-19 22:12:11 +00001703 // the spill weight is now infinity as it
1704 // cannot be spilled again
1705 nI.weight = HUGE_VALF;
1706
1707 // Rewrite register operands to use the new vreg.
1708 for (SmallVectorImpl<unsigned>::iterator I = Indices.begin(),
1709 E = Indices.end(); I != E; ++I) {
1710 MI->getOperand(*I).setReg(NewVReg);
1711
1712 if (MI->getOperand(*I).isUse())
1713 MI->getOperand(*I).setIsKill(true);
1714 }
1715
1716 // Fill in the new live interval.
Lang Hames233a60e2009-11-03 23:52:08 +00001717 SlotIndex index = getInstructionIndex(MI);
Owen Andersona41e47a2008-08-19 22:12:11 +00001718 if (HasUse) {
Lang Hames233a60e2009-11-03 23:52:08 +00001719 LiveRange LR(index.getLoadIndex(), index.getUseIndex(),
1720 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001721 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001722 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001723 nI.addRange(LR);
1724 vrm.addRestorePoint(NewVReg, MI);
1725 }
1726 if (HasDef) {
Lang Hames233a60e2009-11-03 23:52:08 +00001727 LiveRange LR(index.getDefIndex(), index.getStoreIndex(),
1728 nI.getNextValue(SlotIndex(), 0, false,
Lang Hames86511252009-09-04 20:41:11 +00001729 getVNInfoAllocator()));
Bill Wendling8e6179f2009-08-22 20:18:03 +00001730 DEBUG(errs() << " +" << LR);
Owen Andersona41e47a2008-08-19 22:12:11 +00001731 nI.addRange(LR);
1732 vrm.addSpillPoint(NewVReg, true, MI);
1733 }
1734
Owen Anderson17197312008-08-18 23:41:04 +00001735 added.push_back(&nI);
Owen Anderson8dc2cbe2008-08-18 18:38:12 +00001736
Bill Wendling8e6179f2009-08-22 20:18:03 +00001737 DEBUG({
1738 errs() << "\t\t\t\tadded new interval: ";
1739 nI.dump();
1740 errs() << '\n';
1741 });
Owen Andersona41e47a2008-08-19 22:12:11 +00001742 }
Owen Anderson9a032932008-08-18 21:20:32 +00001743
Owen Anderson9a032932008-08-18 21:20:32 +00001744
Owen Andersona41e47a2008-08-19 22:12:11 +00001745 RI = mri_->reg_begin(li.reg);
Owen Andersond6664312008-08-18 18:05:32 +00001746 }
Owen Andersond6664312008-08-18 18:05:32 +00001747
1748 return added;
1749}
1750
1751std::vector<LiveInterval*> LiveIntervals::
Evan Cheng81a03822007-11-17 00:40:40 +00001752addIntervalsForSpills(const LiveInterval &li,
Evan Chengdc377862008-09-30 15:44:16 +00001753 SmallVectorImpl<LiveInterval*> &SpillIs,
Evan Chengc781a242009-05-03 18:32:42 +00001754 const MachineLoopInfo *loopInfo, VirtRegMap &vrm) {
Owen Andersonae339ba2008-08-19 00:17:30 +00001755
1756 if (EnableFastSpilling)
Evan Chengc781a242009-05-03 18:32:42 +00001757 return addIntervalsForSpillsFast(li, loopInfo, vrm);
Owen Andersonae339ba2008-08-19 00:17:30 +00001758
Evan Chengf2fbca62007-11-12 06:35:08 +00001759 assert(li.weight != HUGE_VALF &&
1760 "attempt to spill already spilled interval!");
1761
Bill Wendling8e6179f2009-08-22 20:18:03 +00001762 DEBUG({
1763 errs() << "\t\t\t\tadding intervals for spills for interval: ";
1764 li.print(errs(), tri_);
1765 errs() << '\n';
1766 });
Evan Chengf2fbca62007-11-12 06:35:08 +00001767
Evan Cheng72eeb942008-12-05 17:00:16 +00001768 // Each bit specify whether a spill is required in the MBB.
Evan Cheng81a03822007-11-17 00:40:40 +00001769 BitVector SpillMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001770 DenseMap<unsigned, std::vector<SRInfo> > SpillIdxes;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001771 BitVector RestoreMBBs(mf_->getNumBlockIDs());
Owen Anderson28998312008-08-13 22:28:50 +00001772 DenseMap<unsigned, std::vector<SRInfo> > RestoreIdxes;
1773 DenseMap<unsigned,unsigned> MBBVRegsMap;
Evan Chengf2fbca62007-11-12 06:35:08 +00001774 std::vector<LiveInterval*> NewLIs;
Evan Chengd70dbb52008-02-22 09:24:50 +00001775 const TargetRegisterClass* rc = mri_->getRegClass(li.reg);
Evan Chengf2fbca62007-11-12 06:35:08 +00001776
1777 unsigned NumValNums = li.getNumValNums();
1778 SmallVector<MachineInstr*, 4> ReMatDefs;
1779 ReMatDefs.resize(NumValNums, NULL);
1780 SmallVector<MachineInstr*, 4> ReMatOrigDefs;
1781 ReMatOrigDefs.resize(NumValNums, NULL);
1782 SmallVector<int, 4> ReMatIds;
1783 ReMatIds.resize(NumValNums, VirtRegMap::MAX_STACK_SLOT);
1784 BitVector ReMatDelete(NumValNums);
1785 unsigned Slot = VirtRegMap::MAX_STACK_SLOT;
1786
Evan Cheng81a03822007-11-17 00:40:40 +00001787 // Spilling a split live interval. It cannot be split any further. Also,
1788 // it's also guaranteed to be a single val# / range interval.
1789 if (vrm.getPreSplitReg(li.reg)) {
1790 vrm.setIsSplitFromReg(li.reg, 0);
Evan Chengd120ffd2007-12-05 10:24:35 +00001791 // Unset the split kill marker on the last use.
Lang Hames233a60e2009-11-03 23:52:08 +00001792 SlotIndex KillIdx = vrm.getKillPoint(li.reg);
1793 if (KillIdx != SlotIndex()) {
Evan Chengd120ffd2007-12-05 10:24:35 +00001794 MachineInstr *KillMI = getInstructionFromIndex(KillIdx);
1795 assert(KillMI && "Last use disappeared?");
1796 int KillOp = KillMI->findRegisterUseOperandIdx(li.reg, true);
1797 assert(KillOp != -1 && "Last use disappeared?");
Chris Lattnerf7382302007-12-30 21:56:09 +00001798 KillMI->getOperand(KillOp).setIsKill(false);
Evan Chengd120ffd2007-12-05 10:24:35 +00001799 }
Evan Chengadf85902007-12-05 09:51:10 +00001800 vrm.removeKillPoint(li.reg);
Evan Cheng81a03822007-11-17 00:40:40 +00001801 bool DefIsReMat = vrm.isReMaterialized(li.reg);
1802 Slot = vrm.getStackSlot(li.reg);
1803 assert(Slot != VirtRegMap::MAX_STACK_SLOT);
1804 MachineInstr *ReMatDefMI = DefIsReMat ?
1805 vrm.getReMaterializedMI(li.reg) : NULL;
1806 int LdSlot = 0;
1807 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
1808 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001809 (DefIsReMat && (ReMatDefMI->getDesc().canFoldAsLoad()));
Evan Cheng81a03822007-11-17 00:40:40 +00001810 bool IsFirstRange = true;
1811 for (LiveInterval::Ranges::const_iterator
1812 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
1813 // If this is a split live interval with multiple ranges, it means there
1814 // are two-address instructions that re-defined the value. Only the
1815 // first def can be rematerialized!
1816 if (IsFirstRange) {
Evan Chengcb3c3302007-11-29 23:02:50 +00001817 // Note ReMatOrigDefMI has already been deleted.
Evan Cheng81a03822007-11-17 00:40:40 +00001818 rewriteInstructionsForSpills(li, false, I, NULL, ReMatDefMI,
1819 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001820 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001821 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001822 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001823 } else {
1824 rewriteInstructionsForSpills(li, false, I, NULL, 0,
1825 Slot, 0, false, false, false,
Evan Chengd70dbb52008-02-22 09:24:50 +00001826 false, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001827 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001828 MBBVRegsMap, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001829 }
1830 IsFirstRange = false;
1831 }
Evan Cheng419852c2008-04-03 16:39:43 +00001832
Evan Cheng4cce6b42008-04-11 17:53:36 +00001833 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng81a03822007-11-17 00:40:40 +00001834 return NewLIs;
1835 }
1836
Evan Cheng752195e2009-09-14 21:33:42 +00001837 bool TrySplit = !intervalIsInOneMBB(li);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001838 if (TrySplit)
1839 ++numSplits;
Evan Chengf2fbca62007-11-12 06:35:08 +00001840 bool NeedStackSlot = false;
1841 for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
1842 i != e; ++i) {
1843 const VNInfo *VNI = *i;
1844 unsigned VN = VNI->id;
Lang Hames857c4e02009-06-17 21:01:20 +00001845 if (VNI->isUnused())
Evan Chengf2fbca62007-11-12 06:35:08 +00001846 continue; // Dead val#.
1847 // Is the def for the val# rematerializable?
Lang Hames857c4e02009-06-17 21:01:20 +00001848 MachineInstr *ReMatDefMI = VNI->isDefAccurate()
1849 ? getInstructionFromIndex(VNI->def) : 0;
Evan Cheng5ef3a042007-12-06 00:01:56 +00001850 bool dummy;
Evan Chengdc377862008-09-30 15:44:16 +00001851 if (ReMatDefMI && isReMaterializable(li, VNI, ReMatDefMI, SpillIs, dummy)) {
Evan Chengf2fbca62007-11-12 06:35:08 +00001852 // Remember how to remat the def of this val#.
Evan Cheng81a03822007-11-17 00:40:40 +00001853 ReMatOrigDefs[VN] = ReMatDefMI;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +00001854 // Original def may be modified so we have to make a copy here.
Evan Cheng1ed99222008-07-19 00:37:25 +00001855 MachineInstr *Clone = mf_->CloneMachineInstr(ReMatDefMI);
Evan Cheng752195e2009-09-14 21:33:42 +00001856 CloneMIs.push_back(Clone);
Evan Cheng1ed99222008-07-19 00:37:25 +00001857 ReMatDefs[VN] = Clone;
Evan Chengf2fbca62007-11-12 06:35:08 +00001858
1859 bool CanDelete = true;
Lang Hames857c4e02009-06-17 21:01:20 +00001860 if (VNI->hasPHIKill()) {
Evan Chengc3fc7d92007-11-29 09:49:23 +00001861 // A kill is a phi node, not all of its uses can be rematerialized.
Evan Chengf2fbca62007-11-12 06:35:08 +00001862 // It must not be deleted.
Evan Chengc3fc7d92007-11-29 09:49:23 +00001863 CanDelete = false;
1864 // Need a stack slot if there is any live range where uses cannot be
1865 // rematerialized.
1866 NeedStackSlot = true;
Evan Chengf2fbca62007-11-12 06:35:08 +00001867 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001868 if (CanDelete)
1869 ReMatDelete.set(VN);
1870 } else {
1871 // Need a stack slot if there is any live range where uses cannot be
1872 // rematerialized.
1873 NeedStackSlot = true;
1874 }
1875 }
1876
1877 // One stack slot per live interval.
Owen Andersonb98bbb72009-03-26 18:53:38 +00001878 if (NeedStackSlot && vrm.getPreSplitReg(li.reg) == 0) {
1879 if (vrm.getStackSlot(li.reg) == VirtRegMap::NO_STACK_SLOT)
1880 Slot = vrm.assignVirt2StackSlot(li.reg);
1881
1882 // This case only occurs when the prealloc splitter has already assigned
1883 // a stack slot to this vreg.
1884 else
1885 Slot = vrm.getStackSlot(li.reg);
1886 }
Evan Chengf2fbca62007-11-12 06:35:08 +00001887
1888 // Create new intervals and rewrite defs and uses.
1889 for (LiveInterval::Ranges::const_iterator
1890 I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
Evan Cheng81a03822007-11-17 00:40:40 +00001891 MachineInstr *ReMatDefMI = ReMatDefs[I->valno->id];
1892 MachineInstr *ReMatOrigDefMI = ReMatOrigDefs[I->valno->id];
1893 bool DefIsReMat = ReMatDefMI != NULL;
Evan Chengf2fbca62007-11-12 06:35:08 +00001894 bool CanDelete = ReMatDelete[I->valno->id];
1895 int LdSlot = 0;
Evan Cheng81a03822007-11-17 00:40:40 +00001896 bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
Evan Chengf2fbca62007-11-12 06:35:08 +00001897 bool isLoad = isLoadSS ||
Dan Gohman15511cf2008-12-03 18:15:48 +00001898 (DefIsReMat && ReMatDefMI->getDesc().canFoldAsLoad());
Evan Cheng81a03822007-11-17 00:40:40 +00001899 rewriteInstructionsForSpills(li, TrySplit, I, ReMatOrigDefMI, ReMatDefMI,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001900 Slot, LdSlot, isLoad, isLoadSS, DefIsReMat,
Evan Chengd70dbb52008-02-22 09:24:50 +00001901 CanDelete, vrm, rc, ReMatIds, loopInfo,
Evan Cheng0cbb1162007-11-29 01:06:25 +00001902 SpillMBBs, SpillIdxes, RestoreMBBs, RestoreIdxes,
Evan Chengc781a242009-05-03 18:32:42 +00001903 MBBVRegsMap, NewLIs);
Evan Chengf2fbca62007-11-12 06:35:08 +00001904 }
1905
Evan Cheng0cbb1162007-11-29 01:06:25 +00001906 // Insert spills / restores if we are splitting.
Evan Cheng419852c2008-04-03 16:39:43 +00001907 if (!TrySplit) {
Evan Cheng4cce6b42008-04-11 17:53:36 +00001908 handleSpilledImpDefs(li, vrm, rc, NewLIs);
Evan Cheng1953d0c2007-11-29 10:12:14 +00001909 return NewLIs;
Evan Cheng419852c2008-04-03 16:39:43 +00001910 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001911
Evan Chengb50bb8c2007-12-05 08:16:32 +00001912 SmallPtrSet<LiveInterval*, 4> AddedKill;
Evan Chengaee4af62007-12-02 08:30:39 +00001913 SmallVector<unsigned, 2> Ops;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001914 if (NeedStackSlot) {
1915 int Id = SpillMBBs.find_first();
1916 while (Id != -1) {
1917 std::vector<SRInfo> &spills = SpillIdxes[Id];
1918 for (unsigned i = 0, e = spills.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001919 SlotIndex index = spills[i].index;
Evan Cheng1953d0c2007-11-29 10:12:14 +00001920 unsigned VReg = spills[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001921 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001922 bool isReMat = vrm.isReMaterialized(VReg);
1923 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001924 bool CanFold = false;
1925 bool FoundUse = false;
1926 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001927 if (spills[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001928 CanFold = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001929 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1930 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001931 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng0cbb1162007-11-29 01:06:25 +00001932 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001933
1934 Ops.push_back(j);
1935 if (MO.isDef())
Evan Chengcddbb832007-11-30 21:23:43 +00001936 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001937 if (isReMat ||
1938 (!FoundUse && !alsoFoldARestore(Id, index, VReg,
1939 RestoreMBBs, RestoreIdxes))) {
1940 // MI has two-address uses of the same register. If the use
1941 // isn't the first and only use in the BB, then we can't fold
1942 // it. FIXME: Move this to rewriteInstructionsForSpills.
1943 CanFold = false;
Evan Chengcddbb832007-11-30 21:23:43 +00001944 break;
1945 }
Evan Chengaee4af62007-12-02 08:30:39 +00001946 FoundUse = true;
Evan Cheng0cbb1162007-11-29 01:06:25 +00001947 }
1948 }
1949 // Fold the store into the def if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00001950 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00001951 if (CanFold && !Ops.empty()) {
1952 if (tryFoldMemoryOperand(MI, vrm, NULL, index, Ops, true, Slot,VReg)){
Evan Chengcddbb832007-11-30 21:23:43 +00001953 Folded = true;
Sebastian Redl48fe6352009-03-19 23:26:52 +00001954 if (FoundUse) {
Evan Chengaee4af62007-12-02 08:30:39 +00001955 // Also folded uses, do not issue a load.
1956 eraseRestoreInfo(Id, index, VReg, RestoreMBBs, RestoreIdxes);
Lang Hames233a60e2009-11-03 23:52:08 +00001957 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengf38d14f2007-12-05 09:05:34 +00001958 }
Lang Hames233a60e2009-11-03 23:52:08 +00001959 nI.removeRange(index.getDefIndex(), index.getStoreIndex());
Evan Chengcddbb832007-11-30 21:23:43 +00001960 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001961 }
1962
Evan Cheng7e073ba2008-04-09 20:57:25 +00001963 // Otherwise tell the spiller to issue a spill.
Evan Chengb50bb8c2007-12-05 08:16:32 +00001964 if (!Folded) {
1965 LiveRange *LR = &nI.ranges[nI.ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00001966 bool isKill = LR->end == index.getStoreIndex();
Evan Chengb0a6f622008-05-20 08:10:37 +00001967 if (!MI->registerDefIsDead(nI.reg))
1968 // No need to spill a dead def.
1969 vrm.addSpillPoint(VReg, isKill, MI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00001970 if (isKill)
1971 AddedKill.insert(&nI);
1972 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001973 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001974 Id = SpillMBBs.find_next(Id);
Evan Cheng0cbb1162007-11-29 01:06:25 +00001975 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00001976 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00001977
Evan Cheng1953d0c2007-11-29 10:12:14 +00001978 int Id = RestoreMBBs.find_first();
1979 while (Id != -1) {
1980 std::vector<SRInfo> &restores = RestoreIdxes[Id];
1981 for (unsigned i = 0, e = restores.size(); i != e; ++i) {
Lang Hames233a60e2009-11-03 23:52:08 +00001982 SlotIndex index = restores[i].index;
1983 if (index == SlotIndex())
Evan Cheng1953d0c2007-11-29 10:12:14 +00001984 continue;
1985 unsigned VReg = restores[i].vreg;
Evan Cheng597d10d2007-12-04 00:32:23 +00001986 LiveInterval &nI = getOrCreateInterval(VReg);
Evan Cheng9c3c2212008-06-06 07:54:39 +00001987 bool isReMat = vrm.isReMaterialized(VReg);
Evan Cheng81a03822007-11-17 00:40:40 +00001988 MachineInstr *MI = getInstructionFromIndex(index);
Evan Chengaee4af62007-12-02 08:30:39 +00001989 bool CanFold = false;
1990 Ops.clear();
Evan Chengcddbb832007-11-30 21:23:43 +00001991 if (restores[i].canFold) {
Evan Chengaee4af62007-12-02 08:30:39 +00001992 CanFold = true;
Evan Cheng81a03822007-11-17 00:40:40 +00001993 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
1994 MachineOperand &MO = MI->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +00001995 if (!MO.isReg() || MO.getReg() != VReg)
Evan Cheng81a03822007-11-17 00:40:40 +00001996 continue;
Evan Chengaee4af62007-12-02 08:30:39 +00001997
Evan Cheng0cbb1162007-11-29 01:06:25 +00001998 if (MO.isDef()) {
Evan Chengaee4af62007-12-02 08:30:39 +00001999 // If this restore were to be folded, it would have been folded
2000 // already.
2001 CanFold = false;
Evan Cheng81a03822007-11-17 00:40:40 +00002002 break;
2003 }
Evan Chengaee4af62007-12-02 08:30:39 +00002004 Ops.push_back(j);
Evan Cheng81a03822007-11-17 00:40:40 +00002005 }
2006 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002007
2008 // Fold the load into the use if possible.
Evan Chengcddbb832007-11-30 21:23:43 +00002009 bool Folded = false;
Evan Chengaee4af62007-12-02 08:30:39 +00002010 if (CanFold && !Ops.empty()) {
Evan Cheng9c3c2212008-06-06 07:54:39 +00002011 if (!isReMat)
Evan Chengaee4af62007-12-02 08:30:39 +00002012 Folded = tryFoldMemoryOperand(MI, vrm, NULL,index,Ops,true,Slot,VReg);
2013 else {
Evan Cheng0cbb1162007-11-29 01:06:25 +00002014 MachineInstr *ReMatDefMI = vrm.getReMaterializedMI(VReg);
2015 int LdSlot = 0;
2016 bool isLoadSS = tii_->isLoadFromStackSlot(ReMatDefMI, LdSlot);
2017 // If the rematerializable def is a load, also try to fold it.
Dan Gohman15511cf2008-12-03 18:15:48 +00002018 if (isLoadSS || ReMatDefMI->getDesc().canFoldAsLoad())
Evan Chengaee4af62007-12-02 08:30:39 +00002019 Folded = tryFoldMemoryOperand(MI, vrm, ReMatDefMI, index,
2020 Ops, isLoadSS, LdSlot, VReg);
Evan Cheng650d7f32008-12-05 17:41:31 +00002021 if (!Folded) {
2022 unsigned ImpUse = getReMatImplicitUse(li, ReMatDefMI);
2023 if (ImpUse) {
2024 // Re-matting an instruction with virtual register use. Add the
2025 // register as an implicit use on the use MI and update the register
2026 // interval's spill weight to HUGE_VALF to prevent it from being
2027 // spilled.
2028 LiveInterval &ImpLi = getInterval(ImpUse);
2029 ImpLi.weight = HUGE_VALF;
2030 MI->addOperand(MachineOperand::CreateReg(ImpUse, false, true));
2031 }
Evan Chengd70dbb52008-02-22 09:24:50 +00002032 }
Evan Chengaee4af62007-12-02 08:30:39 +00002033 }
Evan Cheng0cbb1162007-11-29 01:06:25 +00002034 }
2035 // If folding is not possible / failed, then tell the spiller to issue a
2036 // load / rematerialization for us.
Evan Cheng597d10d2007-12-04 00:32:23 +00002037 if (Folded)
Lang Hames233a60e2009-11-03 23:52:08 +00002038 nI.removeRange(index.getLoadIndex(), index.getDefIndex());
Evan Chengb50bb8c2007-12-05 08:16:32 +00002039 else
Evan Cheng0cbb1162007-11-29 01:06:25 +00002040 vrm.addRestorePoint(VReg, MI);
Evan Cheng81a03822007-11-17 00:40:40 +00002041 }
Evan Cheng1953d0c2007-11-29 10:12:14 +00002042 Id = RestoreMBBs.find_next(Id);
Evan Cheng81a03822007-11-17 00:40:40 +00002043 }
2044
Evan Chengb50bb8c2007-12-05 08:16:32 +00002045 // Finalize intervals: add kills, finalize spill weights, and filter out
2046 // dead intervals.
Evan Cheng597d10d2007-12-04 00:32:23 +00002047 std::vector<LiveInterval*> RetNewLIs;
2048 for (unsigned i = 0, e = NewLIs.size(); i != e; ++i) {
2049 LiveInterval *LI = NewLIs[i];
2050 if (!LI->empty()) {
Lang Hames233a60e2009-11-03 23:52:08 +00002051 LI->weight /= SlotIndex::NUM * getApproximateInstructionCount(*LI);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002052 if (!AddedKill.count(LI)) {
2053 LiveRange *LR = &LI->ranges[LI->ranges.size()-1];
Lang Hames233a60e2009-11-03 23:52:08 +00002054 SlotIndex LastUseIdx = LR->end.getBaseIndex();
Evan Chengd120ffd2007-12-05 10:24:35 +00002055 MachineInstr *LastUse = getInstructionFromIndex(LastUseIdx);
Evan Cheng6130f662008-03-05 00:59:57 +00002056 int UseIdx = LastUse->findRegisterUseOperandIdx(LI->reg, false);
Evan Chengb50bb8c2007-12-05 08:16:32 +00002057 assert(UseIdx != -1);
Evan Chenga24752f2009-03-19 20:30:06 +00002058 if (!LastUse->isRegTiedToDefOperand(UseIdx)) {
Evan Chengb50bb8c2007-12-05 08:16:32 +00002059 LastUse->getOperand(UseIdx).setIsKill();
Evan Chengd120ffd2007-12-05 10:24:35 +00002060 vrm.addKillPoint(LI->reg, LastUseIdx);
Evan Chengadf85902007-12-05 09:51:10 +00002061 }
Evan Chengb50bb8c2007-12-05 08:16:32 +00002062 }
Evan Cheng597d10d2007-12-04 00:32:23 +00002063 RetNewLIs.push_back(LI);
2064 }
2065 }
Evan Cheng81a03822007-11-17 00:40:40 +00002066
Evan Cheng4cce6b42008-04-11 17:53:36 +00002067 handleSpilledImpDefs(li, vrm, rc, RetNewLIs);
Evan Cheng597d10d2007-12-04 00:32:23 +00002068 return RetNewLIs;
Evan Chengf2fbca62007-11-12 06:35:08 +00002069}
Evan Cheng676dd7c2008-03-11 07:19:34 +00002070
2071/// hasAllocatableSuperReg - Return true if the specified physical register has
2072/// any super register that's allocatable.
2073bool LiveIntervals::hasAllocatableSuperReg(unsigned Reg) const {
2074 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS)
2075 if (allocatableRegs_[*AS] && hasInterval(*AS))
2076 return true;
2077 return false;
2078}
2079
2080/// getRepresentativeReg - Find the largest super register of the specified
2081/// physical register.
2082unsigned LiveIntervals::getRepresentativeReg(unsigned Reg) const {
2083 // Find the largest super-register that is allocatable.
2084 unsigned BestReg = Reg;
2085 for (const unsigned* AS = tri_->getSuperRegisters(Reg); *AS; ++AS) {
2086 unsigned SuperReg = *AS;
2087 if (!hasAllocatableSuperReg(SuperReg) && hasInterval(SuperReg)) {
2088 BestReg = SuperReg;
2089 break;
2090 }
2091 }
2092 return BestReg;
2093}
2094
2095/// getNumConflictsWithPhysReg - Return the number of uses and defs of the
2096/// specified interval that conflicts with the specified physical register.
2097unsigned LiveIntervals::getNumConflictsWithPhysReg(const LiveInterval &li,
2098 unsigned PhysReg) const {
2099 unsigned NumConflicts = 0;
2100 const LiveInterval &pli = getInterval(getRepresentativeReg(PhysReg));
2101 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2102 E = mri_->reg_end(); I != E; ++I) {
2103 MachineOperand &O = I.getOperand();
2104 MachineInstr *MI = O.getParent();
Lang Hames233a60e2009-11-03 23:52:08 +00002105 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng676dd7c2008-03-11 07:19:34 +00002106 if (pli.liveAt(Index))
2107 ++NumConflicts;
2108 }
2109 return NumConflicts;
2110}
2111
2112/// spillPhysRegAroundRegDefsUses - Spill the specified physical register
Evan Cheng2824a652009-03-23 18:24:37 +00002113/// around all defs and uses of the specified interval. Return true if it
2114/// was able to cut its interval.
2115bool LiveIntervals::spillPhysRegAroundRegDefsUses(const LiveInterval &li,
Evan Cheng676dd7c2008-03-11 07:19:34 +00002116 unsigned PhysReg, VirtRegMap &vrm) {
2117 unsigned SpillReg = getRepresentativeReg(PhysReg);
2118
2119 for (const unsigned *AS = tri_->getAliasSet(PhysReg); *AS; ++AS)
2120 // If there are registers which alias PhysReg, but which are not a
2121 // sub-register of the chosen representative super register. Assert
2122 // since we can't handle it yet.
Dan Gohman70f2f652009-04-13 15:22:29 +00002123 assert(*AS == SpillReg || !allocatableRegs_[*AS] || !hasInterval(*AS) ||
Evan Cheng676dd7c2008-03-11 07:19:34 +00002124 tri_->isSuperRegister(*AS, SpillReg));
2125
Evan Cheng2824a652009-03-23 18:24:37 +00002126 bool Cut = false;
Evan Cheng0222a8c2009-10-20 01:31:09 +00002127 SmallVector<unsigned, 4> PRegs;
2128 if (hasInterval(SpillReg))
2129 PRegs.push_back(SpillReg);
2130 else {
2131 SmallSet<unsigned, 4> Added;
2132 for (const unsigned* AS = tri_->getSubRegisters(SpillReg); *AS; ++AS)
2133 if (Added.insert(*AS) && hasInterval(*AS)) {
2134 PRegs.push_back(*AS);
2135 for (const unsigned* ASS = tri_->getSubRegisters(*AS); *ASS; ++ASS)
2136 Added.insert(*ASS);
2137 }
2138 }
2139
Evan Cheng676dd7c2008-03-11 07:19:34 +00002140 SmallPtrSet<MachineInstr*, 8> SeenMIs;
2141 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(li.reg),
2142 E = mri_->reg_end(); I != E; ++I) {
2143 MachineOperand &O = I.getOperand();
2144 MachineInstr *MI = O.getParent();
2145 if (SeenMIs.count(MI))
2146 continue;
2147 SeenMIs.insert(MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002148 SlotIndex Index = getInstructionIndex(MI);
Evan Cheng0222a8c2009-10-20 01:31:09 +00002149 for (unsigned i = 0, e = PRegs.size(); i != e; ++i) {
2150 unsigned PReg = PRegs[i];
2151 LiveInterval &pli = getInterval(PReg);
2152 if (!pli.liveAt(Index))
2153 continue;
2154 vrm.addEmergencySpill(PReg, MI);
Lang Hames233a60e2009-11-03 23:52:08 +00002155 SlotIndex StartIdx = Index.getLoadIndex();
2156 SlotIndex EndIdx = Index.getNextIndex().getBaseIndex();
Evan Cheng2824a652009-03-23 18:24:37 +00002157 if (pli.isInOneLiveRange(StartIdx, EndIdx)) {
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002158 pli.removeRange(StartIdx, EndIdx);
Evan Cheng2824a652009-03-23 18:24:37 +00002159 Cut = true;
2160 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00002161 std::string msg;
2162 raw_string_ostream Msg(msg);
2163 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002164 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +00002165 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng0222a8c2009-10-20 01:31:09 +00002166 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00002167 MI->print(Msg, tm_);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002168 }
Torok Edwin7d696d82009-07-11 13:10:19 +00002169 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +00002170 }
Evan Cheng0222a8c2009-10-20 01:31:09 +00002171 for (const unsigned* AS = tri_->getSubRegisters(PReg); *AS; ++AS) {
Evan Cheng676dd7c2008-03-11 07:19:34 +00002172 if (!hasInterval(*AS))
2173 continue;
2174 LiveInterval &spli = getInterval(*AS);
2175 if (spli.liveAt(Index))
Lang Hames233a60e2009-11-03 23:52:08 +00002176 spli.removeRange(Index.getLoadIndex(),
2177 Index.getNextIndex().getBaseIndex());
Evan Cheng676dd7c2008-03-11 07:19:34 +00002178 }
2179 }
2180 }
Evan Cheng2824a652009-03-23 18:24:37 +00002181 return Cut;
Evan Cheng676dd7c2008-03-11 07:19:34 +00002182}
Owen Andersonc4dc1322008-06-05 17:15:43 +00002183
2184LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg,
Lang Hamesffd13262009-07-09 03:57:02 +00002185 MachineInstr* startInst) {
Owen Andersonc4dc1322008-06-05 17:15:43 +00002186 LiveInterval& Interval = getOrCreateInterval(reg);
2187 VNInfo* VN = Interval.getNextValue(
Lang Hames233a60e2009-11-03 23:52:08 +00002188 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
Lang Hames86511252009-09-04 20:41:11 +00002189 startInst, true, getVNInfoAllocator());
Lang Hames857c4e02009-06-17 21:01:20 +00002190 VN->setHasPHIKill(true);
Lang Hames233a60e2009-11-03 23:52:08 +00002191 VN->kills.push_back(indexes_->getTerminatorGap(startInst->getParent()));
Lang Hames86511252009-09-04 20:41:11 +00002192 LiveRange LR(
Lang Hames233a60e2009-11-03 23:52:08 +00002193 SlotIndex(getInstructionIndex(startInst).getDefIndex()),
2194 getMBBEndIdx(startInst->getParent()).getNextIndex().getBaseIndex(), VN);
Owen Andersonc4dc1322008-06-05 17:15:43 +00002195 Interval.addRange(LR);
2196
2197 return LR;
2198}
David Greeneb5257662009-08-03 21:55:09 +00002199