blob: ae208ce1b3ce2d76455752b11c775eb36204fe52 [file] [log] [blame]
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001//===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This register allocator allocates registers to a basic block at a time,
11// attempting to keep values in registers and reusing registers as appropriate.
12//
13//===----------------------------------------------------------------------===//
14
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/IndexedMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000019#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000021#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000022#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/RegAllocRegistry.h"
29#include "llvm/CodeGen/RegisterClassInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/BasicBlock.h"
Reid Kleckner28865802016-04-14 18:29:59 +000031#include "llvm/IR/DebugInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000038#include <algorithm>
39using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "regalloc"
42
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000043STATISTIC(NumStores, "Number of stores added");
44STATISTIC(NumLoads , "Number of loads added");
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +000045STATISTIC(NumCopies, "Number of copies coalesced");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000046
47static RegisterRegAlloc
48 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
49
50namespace {
51 class RAFast : public MachineFunctionPass {
52 public:
53 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000054 RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
Andrew Trickd3f8fe82012-02-10 04:10:36 +000055 isBulkSpilling(false) {}
Derek Schuffad154c82016-03-28 17:05:30 +000056
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000057 private:
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000058 MachineFunction *MF;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +000059 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000060 const TargetRegisterInfo *TRI;
61 const TargetInstrInfo *TII;
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +000062 RegisterClassInfo RegClassInfo;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000063
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +000064 // Basic block currently being allocated.
65 MachineBasicBlock *MBB;
66
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000067 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
68 // values are spilled.
69 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
70
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000071 // Everything we know about a live virtual register.
72 struct LiveReg {
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000073 MachineInstr *LastUse; // Last instr to use reg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000074 unsigned VirtReg; // Virtual register number.
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +000075 unsigned PhysReg; // Currently held here.
76 unsigned short LastOpNum; // OpNum on LastUse.
77 bool Dirty; // Register needs spill.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000078
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000079 explicit LiveReg(unsigned v)
Craig Topperc0196b12014-04-14 00:51:57 +000080 : LastUse(nullptr), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false){}
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000081
Andrew Trick1eb4a0d2012-04-20 20:05:28 +000082 unsigned getSparseSetIndex() const {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000083 return TargetRegisterInfo::virtReg2Index(VirtReg);
84 }
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000085 };
86
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +000087 typedef SparseSet<LiveReg> LiveRegMap;
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000088
89 // LiveVirtRegs - This map contains entries for each virtual register
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000090 // that is currently available in a physical register.
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +000091 LiveRegMap LiveVirtRegs;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000092
Devang Patel0ab77672011-06-21 22:36:03 +000093 DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
Devang Pateld71bc1a2010-08-04 18:42:02 +000094
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +000095 // RegState - Track the state of a physical register.
96 enum RegState {
97 // A disabled register is not available for allocation, but an alias may
98 // be in use. A register can only be moved out of the disabled state if
99 // all aliases are disabled.
100 regDisabled,
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000101
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000102 // A free register is not currently in use and can be allocated
103 // immediately without checking aliases.
104 regFree,
105
Evan Cheng8ea3af42011-04-22 01:40:20 +0000106 // A reserved register has been assigned explicitly (e.g., setting up a
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000107 // call parameter), and it remains reserved until it is used.
108 regReserved
109
110 // A register state may also be a virtual register number, indication that
111 // the physical register is currently allocated to a virtual register. In
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000112 // that case, LiveVirtRegs contains the inverse mapping.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000113 };
114
115 // PhysRegState - One of the RegState enums, or a virtreg.
116 std::vector<unsigned> PhysRegState;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000117
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000118 // Set of register units.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000119 typedef SparseSet<unsigned> UsedInInstrSet;
120
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000121 // Set of register units that are used in the current instruction, and so
122 // cannot be allocated.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000123 UsedInInstrSet UsedInInstr;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000124
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000125 // Mark a physreg as used in this instruction.
126 void markRegUsedInInstr(unsigned PhysReg) {
127 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
128 UsedInInstr.insert(*Units);
129 }
130
131 // Check if a physreg or any of its aliases are used in this instruction.
132 bool isRegUsedInInstr(unsigned PhysReg) const {
133 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
134 if (UsedInInstr.count(*Units))
135 return true;
136 return false;
137 }
138
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000139 // SkippedInstrs - Descriptors of instructions whose clobber list was
140 // ignored because all registers were spilled. It is still necessary to
141 // mark all the clobbered registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000142 SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +0000143
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000144 // isBulkSpilling - This flag is set when LiveRegMap will be cleared
145 // completely after spilling all live registers. LiveRegMap entries should
146 // not be erased.
147 bool isBulkSpilling;
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000148
Alp Toker61007d82014-03-02 03:20:38 +0000149 enum : unsigned {
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000150 spillClean = 1,
151 spillDirty = 100,
152 spillImpossible = ~0u
153 };
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000154 public:
Craig Topper4584cd52014-03-07 09:26:03 +0000155 const char *getPassName() const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000156 return "Fast Register Allocator";
157 }
158
Craig Topper4584cd52014-03-07 09:26:03 +0000159 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000160 AU.setPreservesCFG();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000161 MachineFunctionPass::getAnalysisUsage(AU);
162 }
163
Derek Schuffad154c82016-03-28 17:05:30 +0000164 MachineFunctionProperties getSetProperties() const override {
165 return MachineFunctionProperties().set(
166 MachineFunctionProperties::Property::AllVRegsAllocated);
167 }
168
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000169 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000170 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000171 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000172 void handleThroughOperands(MachineInstr *MI,
173 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000174 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000175 bool isLastUseOfLocalReg(MachineOperand&);
176
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000177 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000178 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000179 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000180 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000181 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000182
183 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000184 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000185 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000186 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
187 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
188 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
189 }
190 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
191 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
192 }
193 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
194 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
195 unsigned Hint);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000196 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
197 unsigned VirtReg, unsigned Hint);
198 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
199 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000200 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000201 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000202 };
203 char RAFast::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000204}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000205
206/// getStackSpaceFor - This allocates space for the specified virtual register
207/// to be held on the stack.
208int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
209 // Find the location Reg would belong...
210 int SS = StackSlotForVirtReg[VirtReg];
211 if (SS != -1)
212 return SS; // Already has space allocated?
213
214 // Allocate a new stack object for this spill location...
215 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
216 RC->getAlignment());
217
218 // Assign the slot.
219 StackSlotForVirtReg[VirtReg] = FrameIdx;
220 return FrameIdx;
221}
222
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000223/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
224/// its virtual register, and it is guaranteed to be a block-local register.
225///
226bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000227 // If the register has ever been spilled or reloaded, we conservatively assume
228 // it is a global register used in multiple blocks.
229 if (StackSlotForVirtReg[MO.getReg()] != -1)
230 return false;
231
232 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000233 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000234 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000235 return false;
236 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000237}
238
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000239/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000240void RAFast::addKillFlag(const LiveReg &LR) {
241 if (!LR.LastUse) return;
242 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000243 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
244 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000245 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000246 else
247 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
248 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000249}
250
251/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000252void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000253 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000254 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
255 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000256 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000257 // Erase from LiveVirtRegs unless we're spilling in bulk.
258 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000259 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000260}
261
262/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000263void RAFast::killVirtReg(unsigned VirtReg) {
264 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
265 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000266 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000267 if (LRI != LiveVirtRegs.end())
268 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000269}
270
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000271/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000272/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000273void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000274 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
275 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000276 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000277 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
278 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000279}
280
281/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000282void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000283 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000284 LiveReg &LR = *LRI;
285 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000286
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000287 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000288 // If this physreg is used by the instruction, we want to kill it on the
289 // instruction, not on the spill.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000290 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000291 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000292 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000293 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000294 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
295 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000296 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000297 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000298 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000299
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000300 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000301 // identify spilled location as the place to find corresponding variable's
302 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000303 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000304 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000305 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
306 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000307 const MDNode *Var = DBG->getDebugVariable();
308 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000309 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000310 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000311 DebugLoc DL = DBG->getDebugLoc();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000312 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000313 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000314 MachineInstr *NewDV =
315 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000316 .addFrameIndex(FI)
317 .addImm(Offset)
318 .addMetadata(Var)
319 .addMetadata(Expr);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000320 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000321 (void)NewDV;
322 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000323 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000324 // Now this register is spilled there is should not be any DBG_VALUE
325 // pointing to this register because they are all pointing to spilled value
326 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000327 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000328 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000329 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000330 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000331 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000332}
333
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000334/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000335void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000336 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000337 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000338 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
339 // of spilling here is deterministic, if arbitrary.
340 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
341 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000342 spillVirtReg(MI, i);
343 LiveVirtRegs.clear();
344 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000345}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000346
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000347/// usePhysReg - Handle the direct use of a physical register.
348/// Check that the register is not used by a virtreg.
349/// Kill the physreg, marking it free.
350/// This may add implicit kills to MO->getParent() and invalidate MO.
351void RAFast::usePhysReg(MachineOperand &MO) {
352 unsigned PhysReg = MO.getReg();
353 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
354 "Bad usePhysReg operand");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000355 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000356 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000357 case regDisabled:
358 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000359 case regReserved:
360 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000361 // Fall through
362 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000363 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000364 return;
365 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000366 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000367 // wanted has been clobbered.
368 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000369 }
370
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000371 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000372 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
373 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000374 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000375 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000376 break;
377 case regReserved:
Quentin Colombet079aba72014-12-03 23:38:08 +0000378 // Either PhysReg is a subregister of Alias and we mark the
379 // whole register as free, or PhysReg is the superregister of
380 // Alias and we mark all the aliases as disabled before freeing
381 // PhysReg.
382 // In the latter case, since PhysReg was disabled, this means that
383 // its value is defined only by physical sub-registers. This check
384 // is performed by the assert of the default case in this loop.
385 // Note: The value of the superregister may only be partial
386 // defined, that is why regDisabled is a valid state for aliases.
387 assert((TRI->isSuperRegister(PhysReg, Alias) ||
388 TRI->isSuperRegister(Alias, PhysReg)) &&
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000389 "Instruction is not using a subregister of a reserved register");
Quentin Colombet079aba72014-12-03 23:38:08 +0000390 // Fall through.
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000391 case regFree:
392 if (TRI->isSuperRegister(PhysReg, Alias)) {
393 // Leave the superregister in the working set.
Quentin Colombet079aba72014-12-03 23:38:08 +0000394 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000395 MO.getParent()->addRegisterKilled(Alias, TRI, true);
396 return;
397 }
398 // Some other alias was in the working set - clear it.
399 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000400 break;
401 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000402 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000403 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000404 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000405
406 // All aliases are disabled, bring register into working set.
407 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000408 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000409}
410
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000411/// definePhysReg - Mark PhysReg as reserved or free after spilling any
412/// virtregs. This is very similar to defineVirtReg except the physreg is
413/// reserved instead of allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000414void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
415 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000416 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000417 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
418 case regDisabled:
419 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000420 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000421 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000422 // Fall through.
423 case regFree:
424 case regReserved:
425 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000426 return;
427 }
428
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000429 // This is a disabled register, disable all aliases.
430 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000431 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
432 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000433 switch (unsigned VirtReg = PhysRegState[Alias]) {
434 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000435 break;
436 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000437 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000438 // Fall through.
439 case regFree:
440 case regReserved:
441 PhysRegState[Alias] = regDisabled;
442 if (TRI->isSuperRegister(PhysReg, Alias))
443 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000444 break;
445 }
446 }
447}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000448
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000449
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000450// calcSpillCost - Return the cost of spilling clearing out PhysReg and
451// aliases so it is free for allocation.
452// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
453// can be allocated directly.
454// Returns spillImpossible when PhysReg or an alias can't be spilled.
455unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000456 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000457 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000458 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000459 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000460 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
461 case regDisabled:
462 break;
463 case regFree:
464 return 0;
465 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000466 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
467 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000468 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000469 default: {
470 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
471 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
472 return I->Dirty ? spillDirty : spillClean;
473 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000474 }
475
Eric Christopherc3783362011-04-12 00:48:08 +0000476 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000477 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000478 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000479 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
480 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000481 switch (unsigned VirtReg = PhysRegState[Alias]) {
482 case regDisabled:
483 break;
484 case regFree:
485 ++Cost;
486 break;
487 case regReserved:
488 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000489 default: {
490 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
491 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
492 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000493 break;
494 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000495 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000496 }
497 return Cost;
498}
499
500
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000501/// assignVirtToPhysReg - This method updates local state so that we know
502/// that PhysReg is the proper container for VirtReg now. The physical
503/// register must not be used for anything else when this is called.
504///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000505void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
506 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000507 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000508 PhysRegState[PhysReg] = LR.VirtReg;
509 assert(!LR.PhysReg && "Already assigned a physreg");
510 LR.PhysReg = PhysReg;
511}
512
513RAFast::LiveRegMap::iterator
514RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
515 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
516 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
517 assignVirtToPhysReg(*LRI, PhysReg);
518 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000519}
520
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000521/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000522RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
523 LiveRegMap::iterator LRI,
524 unsigned Hint) {
525 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000526
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000527 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
528 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000529
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000530 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000531
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000532 // Ignore invalid hints.
533 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000534 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000535 Hint = 0;
536
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000537 // Take hint when possible.
538 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000539 // Ignore the hint if we would have to spill a dirty register.
540 unsigned Cost = calcSpillCost(Hint);
541 if (Cost < spillDirty) {
542 if (Cost)
543 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000544 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
545 // That invalidates LRI, so run a new lookup for VirtReg.
546 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000547 }
548 }
549
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000550 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000551
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000552 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000553 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000554 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000555 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000556 assignVirtToPhysReg(*LRI, PhysReg);
557 return LRI;
558 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000559 }
560
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000561 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
Craig Toppercf0444b2014-11-17 05:50:14 +0000562 << TRI->getRegClassName(RC) << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000563
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000564 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000565 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000566 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000567 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000568 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
569 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000570 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000571 if (Cost == 0) {
572 assignVirtToPhysReg(*LRI, *I);
573 return LRI;
574 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000575 if (Cost < BestCost)
576 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000577 }
578
579 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000580 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000581 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
582 // That invalidates LRI, so run a new lookup for VirtReg.
583 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000584 }
585
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000586 // Nothing we can do. Report an error and keep going with a bad allocation.
Benjamin Kramer7200a462013-10-05 19:33:37 +0000587 if (MI->isInlineAsm())
588 MI->emitError("inline assembly requires more registers than available");
589 else
590 MI->emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000591 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000592 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000593}
594
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000595/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000596RAFast::LiveRegMap::iterator
597RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
598 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000599 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
600 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000601 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000602 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000603 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000604 if (New) {
605 // If there is no hint, peek at the only use of this register.
606 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
607 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000608 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000609 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000610 if (UseMI.isCopyLike())
611 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000612 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000613 LRI = allocVirtReg(MI, LRI, Hint);
614 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000615 // Redefining a live register - kill at the last use, unless it is this
616 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000617 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
618 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000619 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000620 assert(LRI->PhysReg && "Register not assigned");
621 LRI->LastUse = MI;
622 LRI->LastOpNum = OpNum;
623 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000624 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000625 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000626}
627
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000628/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000629RAFast::LiveRegMap::iterator
630RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
631 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000632 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
633 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000634 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000635 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000636 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000637 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000638 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000639 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000640 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000641 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000642 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000643 << PrintReg(LRI->PhysReg, TRI) << "\n");
644 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000645 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000646 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000647 if (isLastUseOfLocalReg(MO)) {
648 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000649 if (MO.isUse())
650 MO.setIsKill();
651 else
652 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000653 } else if (MO.isKill()) {
654 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
655 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000656 } else if (MO.isDead()) {
657 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
658 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000659 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000660 } else if (MO.isKill()) {
661 // We must remove kill flags from uses of reloaded registers because the
662 // register would be killed immediately, and there might be a second use:
663 // %foo = OR %x<kill>, %x
664 // This would cause a second reload of %x into a different register.
665 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
666 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000667 } else if (MO.isDead()) {
668 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
669 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000670 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000671 assert(LRI->PhysReg && "Register not assigned");
672 LRI->LastUse = MI;
673 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000674 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000675 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000676}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000677
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000678// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
679// subregs. This may invalidate any operand pointers.
680// Return true if the operand kills its register.
681bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
682 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000683 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000684 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000685 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000686 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000687 }
688
689 // Handle subregister index.
690 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
691 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000692
693 // A kill flag implies killing the full register. Add corresponding super
694 // register kill.
695 if (MO.isKill()) {
696 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000697 return true;
698 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000699
700 // A <def,read-undef> of a sub-register requires an implicit def of the full
701 // register.
702 if (MO.isDef() && MO.isUndef())
703 MI->addRegisterDefined(PhysReg, TRI);
704
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000705 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000706}
707
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000708// Handle special instruction operand like early clobbers and tied ops when
709// there are additional physreg defines.
710void RAFast::handleThroughOperands(MachineInstr *MI,
711 SmallVectorImpl<unsigned> &VirtDead) {
712 DEBUG(dbgs() << "Scanning for through registers:");
713 SmallSet<unsigned, 8> ThroughRegs;
714 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
715 MachineOperand &MO = MI->getOperand(i);
716 if (!MO.isReg()) continue;
717 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000718 if (!TargetRegisterInfo::isVirtualRegister(Reg))
719 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000720 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
721 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
David Blaikie70573dc2014-11-19 07:49:26 +0000722 if (ThroughRegs.insert(Reg).second)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000723 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000724 }
725 }
726
727 // If any physreg defines collide with preallocated through registers,
728 // we must spill and reallocate.
729 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
730 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
731 MachineOperand &MO = MI->getOperand(i);
732 if (!MO.isReg() || !MO.isDef()) continue;
733 unsigned Reg = MO.getReg();
734 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000735 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000736 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000737 if (ThroughRegs.count(PhysRegState[*AI]))
738 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000739 }
740 }
741
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000742 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000743 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000744 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
745 MachineOperand &MO = MI->getOperand(i);
746 if (!MO.isReg()) continue;
747 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000748 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000749 if (MO.isUse()) {
750 unsigned DefIdx = 0;
751 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
752 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
753 << DefIdx << ".\n");
754 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000755 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000756 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000757 // Note: we don't update the def operand yet. That would cause the normal
758 // def-scan to attempt spilling.
759 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
760 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
761 // Reload the register, but don't assign to the operand just yet.
762 // That would confuse the later phys-def processing pass.
763 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000764 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000765 }
766 }
767
Rafael Espindola2021f382011-11-22 06:27:18 +0000768 DEBUG(dbgs() << "Allocating early clobbers.\n");
769 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
770 MachineOperand &MO = MI->getOperand(i);
771 if (!MO.isReg()) continue;
772 unsigned Reg = MO.getReg();
773 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
774 if (!MO.isEarlyClobber())
775 continue;
776 // Note: defineVirtReg may invalidate MO.
777 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000778 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000779 if (setPhysReg(MI, i, PhysReg))
780 VirtDead.push_back(Reg);
781 }
782
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000783 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000784 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000785 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
786 MachineOperand &MO = MI->getOperand(i);
787 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
788 unsigned Reg = MO.getReg();
789 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000790 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
791 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000792 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000793 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000794
795 // Also mark PartialDefs as used to avoid reallocation.
796 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000797 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000798}
799
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000800void RAFast::AllocateBasicBlock() {
801 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000802
803 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000804 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000805
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000806 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000807
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000808 // Add live-in registers as live.
Matthias Braund9da1622015-09-09 18:08:03 +0000809 for (const auto &LI : MBB->liveins())
810 if (MRI->isAllocatable(LI.PhysReg))
811 definePhysReg(MII, LI.PhysReg, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000812
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000813 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000814 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000815
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000816 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000817 while (MII != MBB->end()) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000818 MachineInstr *MI = MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000819 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000820 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000821 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000822 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
823 if (PhysRegState[Reg] == regDisabled) continue;
824 dbgs() << " " << TRI->getName(Reg);
825 switch(PhysRegState[Reg]) {
826 case regFree:
827 break;
828 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000829 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000830 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000831 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000832 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000833 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
834 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
835 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000836 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000837 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000838 break;
839 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000840 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000841 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000842 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000843 // Check that LiveVirtRegs is the inverse.
844 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
845 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000846 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000847 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000848 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000849 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000850 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000851 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000852 });
853
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000854 // Debug values are not allowed to change codegen in any way.
855 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000856 bool ScanDbgValue = true;
857 while (ScanDbgValue) {
858 ScanDbgValue = false;
859 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
860 MachineOperand &MO = MI->getOperand(i);
861 if (!MO.isReg()) continue;
862 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000863 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000864 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000865 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000866 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000867 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000868 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000869 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000870 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000871 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000872 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000873 }
Devang Pateld61b7352010-07-19 23:25:39 +0000874 else {
875 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000876 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000877 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000878 const MDNode *Var = MI->getDebugVariable();
879 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000880 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000881 MachineBasicBlock *MBB = MI->getParent();
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000882 assert(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000883 cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000884 "Expected inlined-at fields to agree");
David Blaikie0252265b2013-06-16 20:34:15 +0000885 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
886 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000887 .addFrameIndex(SS)
888 .addImm(Offset)
889 .addMetadata(Var)
890 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000891 DEBUG(dbgs() << "Modifying debug info due to spill:"
892 << "\t" << *NewDV);
893 // Scan NewDV operands from the beginning.
894 MI = NewDV;
895 ScanDbgValue = true;
896 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000897 }
Devang Patel57e72372010-07-09 21:48:31 +0000898 }
Devang Patel43bde962011-11-15 21:03:58 +0000899 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000900 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000901 }
902 // Next instruction.
903 continue;
904 }
905
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000906 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000907 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000908 if (MI->isCopy()) {
909 CopyDst = MI->getOperand(0).getReg();
910 CopySrc = MI->getOperand(1).getReg();
911 CopyDstSub = MI->getOperand(0).getSubReg();
912 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000913 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000914
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000915 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000916 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000917
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000918 // First scan.
919 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000920 // Find the end of the virtreg operands
921 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000922 bool hasTiedOps = false;
923 bool hasEarlyClobbers = false;
924 bool hasPartialRedefs = false;
925 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000926 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
927 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000928 // Make sure MRI knows about registers clobbered by regmasks.
929 if (MO.isRegMask()) {
930 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
931 continue;
932 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000933 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000934 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000935 if (!Reg) continue;
936 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
937 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000938 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000939 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000940 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000941 } else {
942 if (MO.isEarlyClobber())
943 hasEarlyClobbers = true;
944 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
945 hasPartialRedefs = true;
946 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000947 continue;
948 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000949 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000950 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000951 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000952 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +0000953 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
954 regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000955 hasEarlyClobbers = true;
956 } else
957 hasPhysDefs = true;
958 }
959
960 // The instruction may have virtual register operands that must be allocated
961 // the same register at use-time and def-time: early clobbers and tied
962 // operands. If there are also physical defs, these registers must avoid
963 // both physical defs and uses, making them more constrained than normal
964 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000965 // Similarly, if there are multiple defs and tied operands, we must make
966 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000967 // We didn't detect inline asm tied operands above, so just make this extra
968 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000969 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000970 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000971 handleThroughOperands(MI, VirtDead);
972 // Don't attempt coalescing when we have funny stuff going on.
973 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000974 // Pretend we have early clobbers so the use operands get marked below.
975 // This is not necessary for the common case of a single tied use.
976 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000977 }
978
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000979 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000980 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000981 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000982 MachineOperand &MO = MI->getOperand(i);
983 if (!MO.isReg()) continue;
984 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000985 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000986 if (MO.isUse()) {
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000987 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000988 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000989 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000990 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000991 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000992 }
993 }
994
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000995 // Track registers defined by instruction - early clobbers and tied uses at
996 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000997 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000998 if (hasEarlyClobbers) {
999 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1000 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001001 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001002 unsigned Reg = MO.getReg();
1003 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +00001004 // Look for physreg defs and tied uses.
1005 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001006 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001007 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001008 }
1009
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001010 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001011 if (MI->isCall()) {
Quentin Colombete6116982016-02-20 00:32:29 +00001012 // Spill all virtregs before a call. This serves one purpose: If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001013 // exception is thrown, the landing pad is going to expect to find
Quentin Colombete6116982016-02-20 00:32:29 +00001014 // registers in their spill slots.
1015 // Note: although this is appealing to just consider all definitions
1016 // as call-clobbered, this is not correct because some of those
1017 // definitions may be used later on and we do not want to reuse
1018 // those for virtual registers in between.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001019 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1020 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001021
1022 // The imp-defs are skipped below, but we still need to mark those
1023 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001024 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001025 }
1026
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001027 // Third scan.
1028 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001029 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001030 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001031 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1032 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001033 unsigned Reg = MO.getReg();
1034
1035 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001036 if (!MRI->isAllocatable(Reg)) continue;
Quentin Colombet079aba72014-12-03 23:38:08 +00001037 definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001038 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001039 }
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001040 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001041 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001042 if (setPhysReg(MI, i, PhysReg)) {
1043 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001044 CopyDst = 0; // cancel coalescing;
1045 } else
1046 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001047 }
1048
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001049 // Kill dead defs after the scan to ensure that multiple defs of the same
1050 // register are allocated identically. We didn't need to do this for uses
1051 // because we are crerating our own kill flags, and they are always at the
1052 // last use.
1053 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1054 killVirtReg(VirtDead[i]);
1055 VirtDead.clear();
1056
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001057 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1058 DEBUG(dbgs() << "-- coalescing: " << *MI);
1059 Coalesced.push_back(MI);
1060 } else {
1061 DEBUG(dbgs() << "<< " << *MI);
1062 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001063 }
1064
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001065 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001066 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1067 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001068
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001069 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001070 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001071 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001072 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001073 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001074
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001075 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001076}
1077
1078/// runOnMachineFunction - Register allocate the whole function
1079///
1080bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001081 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001082 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001083 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001084 MRI = &MF->getRegInfo();
Eric Christopher60621802014-10-14 07:22:00 +00001085 TRI = MF->getSubtarget().getRegisterInfo();
1086 TII = MF->getSubtarget().getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001087 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001088 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001089 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001090 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001091
Andrew Trickd3f8fe82012-02-10 04:10:36 +00001092 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1093
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001094 // initialize the virtual->physical register map to have a 'null'
1095 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001096 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001097 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001098
1099 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001100 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1101 MBBi != MBBe; ++MBBi) {
1102 MBB = &*MBBi;
1103 AllocateBasicBlock();
1104 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001105
Andrew Trickda84e642012-02-21 04:51:23 +00001106 // All machine operands and other references to virtual registers have been
1107 // replaced. Remove the virtual registers.
1108 MRI->clearVirtRegs();
1109
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001110 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001111 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001112 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001113 return true;
1114}
1115
1116FunctionPass *llvm::createFastRegisterAllocator() {
1117 return new RAFast();
1118}