blob: 5efbb6306b60eb139cd3cdde92fccc4ac835dde0 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.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) {}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +000056 private:
57 const TargetMachine *TM;
58 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
164 private:
Craig Topper4584cd52014-03-07 09:26:03 +0000165 bool runOnMachineFunction(MachineFunction &Fn) override;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000166 void AllocateBasicBlock();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000167 void handleThroughOperands(MachineInstr *MI,
168 SmallVectorImpl<unsigned> &VirtDead);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000169 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000170 bool isLastUseOfLocalReg(MachineOperand&);
171
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000172 void addKillFlag(const LiveReg&);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000173 void killVirtReg(LiveRegMap::iterator);
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000174 void killVirtReg(unsigned VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000175 void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000176 void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000177
178 void usePhysReg(MachineOperand&);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000179 void definePhysReg(MachineInstr *MI, unsigned PhysReg, RegState NewState);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000180 unsigned calcSpillCost(unsigned PhysReg) const;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000181 void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
182 LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
183 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
184 }
185 LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
186 return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
187 }
188 LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
189 LiveRegMap::iterator allocVirtReg(MachineInstr *MI, LiveRegMap::iterator,
190 unsigned Hint);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000191 LiveRegMap::iterator defineVirtReg(MachineInstr *MI, unsigned OpNum,
192 unsigned VirtReg, unsigned Hint);
193 LiveRegMap::iterator reloadVirtReg(MachineInstr *MI, unsigned OpNum,
194 unsigned VirtReg, unsigned Hint);
Akira Hatanakad837be72012-10-31 00:56:01 +0000195 void spillAll(MachineBasicBlock::iterator MI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000196 bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000197 };
198 char RAFast::ID = 0;
199}
200
201/// getStackSpaceFor - This allocates space for the specified virtual register
202/// to be held on the stack.
203int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
204 // Find the location Reg would belong...
205 int SS = StackSlotForVirtReg[VirtReg];
206 if (SS != -1)
207 return SS; // Already has space allocated?
208
209 // Allocate a new stack object for this spill location...
210 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
211 RC->getAlignment());
212
213 // Assign the slot.
214 StackSlotForVirtReg[VirtReg] = FrameIdx;
215 return FrameIdx;
216}
217
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000218/// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
219/// its virtual register, and it is guaranteed to be a block-local register.
220///
221bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000222 // If the register has ever been spilled or reloaded, we conservatively assume
223 // it is a global register used in multiple blocks.
224 if (StackSlotForVirtReg[MO.getReg()] != -1)
225 return false;
226
227 // Check that the use/def chain has exactly one operand - MO.
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000228 MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
Owen Anderson16c6bf42014-03-13 23:12:04 +0000229 if (&*I != &MO)
Jakob Stoklund Olesenf71bc7b2012-08-08 23:44:01 +0000230 return false;
231 return ++I == MRI->reg_nodbg_end();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000232}
233
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000234/// addKillFlag - Set kill flags on last use of a virtual register.
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000235void RAFast::addKillFlag(const LiveReg &LR) {
236 if (!LR.LastUse) return;
237 MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000238 if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
239 if (MO.getReg() == LR.PhysReg)
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000240 MO.setIsKill();
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000241 else
242 LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
243 }
Jakob Stoklund Olesen955a0e72010-05-12 18:46:03 +0000244}
245
246/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000247void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000248 addKillFlag(*LRI);
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000249 assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
250 "Broken RegState mapping");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000251 PhysRegState[LRI->PhysReg] = regFree;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000252 // Erase from LiveVirtRegs unless we're spilling in bulk.
253 if (!isBulkSpilling)
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000254 LiveVirtRegs.erase(LRI);
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000255}
256
257/// killVirtReg - Mark virtreg as no longer available.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000258void RAFast::killVirtReg(unsigned VirtReg) {
259 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
260 "killVirtReg needs a virtual register");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000261 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000262 if (LRI != LiveVirtRegs.end())
263 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000264}
265
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000266/// spillVirtReg - This method spills the value specified by VirtReg into the
Eli Friedmanac305d22010-08-21 20:19:51 +0000267/// corresponding stack slot if needed.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000268void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000269 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
270 "Spilling a physical register is illegal!");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000271 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000272 assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
273 spillVirtReg(MI, LRI);
Jakob Stoklund Olesen41f8dc82010-05-14 00:02:20 +0000274}
275
276/// spillVirtReg - Do the actual work of spilling.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000277void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000278 LiveRegMap::iterator LRI) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000279 LiveReg &LR = *LRI;
280 assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000281
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000282 if (LR.Dirty) {
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000283 // If this physreg is used by the instruction, we want to kill it on the
284 // instruction, not on the spill.
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000285 bool SpillKill = LR.LastUse != MI;
Jakob Stoklund Olesen11f1ba12010-05-11 23:24:47 +0000286 LR.Dirty = false;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000287 DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000288 << " in " << PrintReg(LR.PhysReg, TRI));
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000289 const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
290 int FI = getStackSpaceFor(LRI->VirtReg, RC);
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000291 DEBUG(dbgs() << " to stack slot #" << FI << "\n");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000292 TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000293 ++NumStores; // Update statistics
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000294
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000295 // If this register is used by DBG_VALUE then insert new DBG_VALUE to
Devang Pateld71bc1a2010-08-04 18:42:02 +0000296 // identify spilled location as the place to find corresponding variable's
297 // value.
Craig Topperb94011f2013-07-14 04:42:23 +0000298 SmallVectorImpl<MachineInstr *> &LRIDbgValues =
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000299 LiveDbgValueMap[LRI->VirtReg];
Devang Patel0ab77672011-06-21 22:36:03 +0000300 for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
301 MachineInstr *DBG = LRIDbgValues[li];
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000302 const MDNode *Var = DBG->getDebugVariable();
303 const MDNode *Expr = DBG->getDebugExpression();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000304 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000305 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Devang Patel8a18aee2010-08-06 00:26:18 +0000306 DebugLoc DL;
307 if (MI == MBB->end()) {
308 // If MI is at basic block end then use last instruction's location.
309 MachineBasicBlock::iterator EI = MI;
310 DL = (--EI)->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000311 } else
Devang Patel8a18aee2010-08-06 00:26:18 +0000312 DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000313 MachineInstr *NewDV =
314 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000315 .addFrameIndex(FI)
316 .addImm(Offset)
317 .addMetadata(Var)
318 .addMetadata(Expr);
Adrian Prantle5e8ce62014-09-05 17:10:10 +0000319 assert(NewDV->getParent() == MBB && "dangling parent pointer");
David Blaikie0252265b2013-06-16 20:34:15 +0000320 (void)NewDV;
321 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000322 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000323 // Now this register is spilled there is should not be any DBG_VALUE
324 // pointing to this register because they are all pointing to spilled value
325 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000326 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000327 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000328 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000329 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000330 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000331}
332
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000333/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000334void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000335 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000336 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000337 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
338 // of spilling here is deterministic, if arbitrary.
339 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
340 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000341 spillVirtReg(MI, i);
342 LiveVirtRegs.clear();
343 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000344}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000345
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000346/// usePhysReg - Handle the direct use of a physical register.
347/// Check that the register is not used by a virtreg.
348/// Kill the physreg, marking it free.
349/// This may add implicit kills to MO->getParent() and invalidate MO.
350void RAFast::usePhysReg(MachineOperand &MO) {
351 unsigned PhysReg = MO.getReg();
352 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
353 "Bad usePhysReg operand");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000354 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000355 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000356 case regDisabled:
357 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000358 case regReserved:
359 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000360 // Fall through
361 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000362 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000363 return;
364 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000365 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000366 // wanted has been clobbered.
367 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000368 }
369
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000370 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000371 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
372 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000373 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000374 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000375 break;
376 case regReserved:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000377 assert(TRI->isSuperRegister(PhysReg, Alias) &&
378 "Instruction is not using a subregister of a reserved register");
379 // Leave the superregister in the working set.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000380 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000381 MO.getParent()->addRegisterKilled(Alias, TRI, true);
382 return;
383 case regFree:
384 if (TRI->isSuperRegister(PhysReg, Alias)) {
385 // Leave the superregister in the working set.
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000386 MO.getParent()->addRegisterKilled(Alias, TRI, true);
387 return;
388 }
389 // Some other alias was in the working set - clear it.
390 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000391 break;
392 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000393 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000394 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000395 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000396
397 // All aliases are disabled, bring register into working set.
398 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000399 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000400}
401
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000402/// definePhysReg - Mark PhysReg as reserved or free after spilling any
403/// virtregs. This is very similar to defineVirtReg except the physreg is
404/// reserved instead of allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000405void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
406 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000407 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000408 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
409 case regDisabled:
410 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000411 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000412 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000413 // Fall through.
414 case regFree:
415 case regReserved:
416 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000417 return;
418 }
419
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000420 // This is a disabled register, disable all aliases.
421 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000422 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
423 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000424 switch (unsigned VirtReg = PhysRegState[Alias]) {
425 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000426 break;
427 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000428 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000429 // Fall through.
430 case regFree:
431 case regReserved:
432 PhysRegState[Alias] = regDisabled;
433 if (TRI->isSuperRegister(PhysReg, Alias))
434 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000435 break;
436 }
437 }
438}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000439
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000440
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000441// calcSpillCost - Return the cost of spilling clearing out PhysReg and
442// aliases so it is free for allocation.
443// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
444// can be allocated directly.
445// Returns spillImpossible when PhysReg or an alias can't be spilled.
446unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000447 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000448 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000449 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000450 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000451 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
452 case regDisabled:
453 break;
454 case regFree:
455 return 0;
456 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000457 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
458 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000459 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000460 default: {
461 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
462 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
463 return I->Dirty ? spillDirty : spillClean;
464 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000465 }
466
Eric Christopherc3783362011-04-12 00:48:08 +0000467 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000468 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000469 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000470 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
471 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000472 switch (unsigned VirtReg = PhysRegState[Alias]) {
473 case regDisabled:
474 break;
475 case regFree:
476 ++Cost;
477 break;
478 case regReserved:
479 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000480 default: {
481 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
482 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
483 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000484 break;
485 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000486 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000487 }
488 return Cost;
489}
490
491
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000492/// assignVirtToPhysReg - This method updates local state so that we know
493/// that PhysReg is the proper container for VirtReg now. The physical
494/// register must not be used for anything else when this is called.
495///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000496void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
497 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000498 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000499 PhysRegState[PhysReg] = LR.VirtReg;
500 assert(!LR.PhysReg && "Already assigned a physreg");
501 LR.PhysReg = PhysReg;
502}
503
504RAFast::LiveRegMap::iterator
505RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
506 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
507 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
508 assignVirtToPhysReg(*LRI, PhysReg);
509 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000510}
511
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000512/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000513RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
514 LiveRegMap::iterator LRI,
515 unsigned Hint) {
516 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000517
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000518 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
519 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000520
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000521 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000522
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000523 // Ignore invalid hints.
524 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000525 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000526 Hint = 0;
527
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000528 // Take hint when possible.
529 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000530 // Ignore the hint if we would have to spill a dirty register.
531 unsigned Cost = calcSpillCost(Hint);
532 if (Cost < spillDirty) {
533 if (Cost)
534 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000535 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
536 // That invalidates LRI, so run a new lookup for VirtReg.
537 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000538 }
539 }
540
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000541 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000542
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000543 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000544 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000545 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000546 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000547 assignVirtToPhysReg(*LRI, PhysReg);
548 return LRI;
549 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000550 }
551
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000552 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
553 << RC->getName() << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000554
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000555 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000556 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000557 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000558 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000559 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
560 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000561 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000562 if (Cost == 0) {
563 assignVirtToPhysReg(*LRI, *I);
564 return LRI;
565 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000566 if (Cost < BestCost)
567 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000568 }
569
570 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000571 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000572 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
573 // That invalidates LRI, so run a new lookup for VirtReg.
574 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000575 }
576
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000577 // Nothing we can do. Report an error and keep going with a bad allocation.
Benjamin Kramer7200a462013-10-05 19:33:37 +0000578 if (MI->isInlineAsm())
579 MI->emitError("inline assembly requires more registers than available");
580 else
581 MI->emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000582 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000583 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000584}
585
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000586/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000587RAFast::LiveRegMap::iterator
588RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
589 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000590 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
591 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000592 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000593 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000594 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000595 if (New) {
596 // If there is no hint, peek at the only use of this register.
597 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
598 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000599 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000600 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000601 if (UseMI.isCopyLike())
602 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000603 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000604 LRI = allocVirtReg(MI, LRI, Hint);
605 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000606 // Redefining a live register - kill at the last use, unless it is this
607 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000608 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
609 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000610 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000611 assert(LRI->PhysReg && "Register not assigned");
612 LRI->LastUse = MI;
613 LRI->LastOpNum = OpNum;
614 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000615 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000616 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000617}
618
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000619/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000620RAFast::LiveRegMap::iterator
621RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
622 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000623 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
624 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000625 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000626 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000627 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000628 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000629 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000630 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000631 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000632 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000633 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000634 << PrintReg(LRI->PhysReg, TRI) << "\n");
635 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000636 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000637 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000638 if (isLastUseOfLocalReg(MO)) {
639 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000640 if (MO.isUse())
641 MO.setIsKill();
642 else
643 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000644 } else if (MO.isKill()) {
645 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
646 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000647 } else if (MO.isDead()) {
648 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
649 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000650 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000651 } else if (MO.isKill()) {
652 // We must remove kill flags from uses of reloaded registers because the
653 // register would be killed immediately, and there might be a second use:
654 // %foo = OR %x<kill>, %x
655 // This would cause a second reload of %x into a different register.
656 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
657 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000658 } else if (MO.isDead()) {
659 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
660 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000661 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000662 assert(LRI->PhysReg && "Register not assigned");
663 LRI->LastUse = MI;
664 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000665 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000666 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000667}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000668
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000669// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
670// subregs. This may invalidate any operand pointers.
671// Return true if the operand kills its register.
672bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
673 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000674 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000675 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000676 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000677 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000678 }
679
680 // Handle subregister index.
681 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
682 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000683
684 // A kill flag implies killing the full register. Add corresponding super
685 // register kill.
686 if (MO.isKill()) {
687 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000688 return true;
689 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000690
691 // A <def,read-undef> of a sub-register requires an implicit def of the full
692 // register.
693 if (MO.isDef() && MO.isUndef())
694 MI->addRegisterDefined(PhysReg, TRI);
695
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000696 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000697}
698
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000699// Handle special instruction operand like early clobbers and tied ops when
700// there are additional physreg defines.
701void RAFast::handleThroughOperands(MachineInstr *MI,
702 SmallVectorImpl<unsigned> &VirtDead) {
703 DEBUG(dbgs() << "Scanning for through registers:");
704 SmallSet<unsigned, 8> ThroughRegs;
705 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
706 MachineOperand &MO = MI->getOperand(i);
707 if (!MO.isReg()) continue;
708 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000709 if (!TargetRegisterInfo::isVirtualRegister(Reg))
710 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000711 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
712 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000713 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000714 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000715 }
716 }
717
718 // If any physreg defines collide with preallocated through registers,
719 // we must spill and reallocate.
720 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
721 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
722 MachineOperand &MO = MI->getOperand(i);
723 if (!MO.isReg() || !MO.isDef()) continue;
724 unsigned Reg = MO.getReg();
725 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000726 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000727 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000728 if (ThroughRegs.count(PhysRegState[*AI]))
729 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000730 }
731 }
732
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000733 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000734 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000735 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
736 MachineOperand &MO = MI->getOperand(i);
737 if (!MO.isReg()) continue;
738 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000739 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000740 if (MO.isUse()) {
741 unsigned DefIdx = 0;
742 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
743 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
744 << DefIdx << ".\n");
745 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000746 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000747 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000748 // Note: we don't update the def operand yet. That would cause the normal
749 // def-scan to attempt spilling.
750 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
751 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
752 // Reload the register, but don't assign to the operand just yet.
753 // That would confuse the later phys-def processing pass.
754 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000755 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000756 }
757 }
758
Rafael Espindola2021f382011-11-22 06:27:18 +0000759 DEBUG(dbgs() << "Allocating early clobbers.\n");
760 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
761 MachineOperand &MO = MI->getOperand(i);
762 if (!MO.isReg()) continue;
763 unsigned Reg = MO.getReg();
764 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
765 if (!MO.isEarlyClobber())
766 continue;
767 // Note: defineVirtReg may invalidate MO.
768 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000769 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000770 if (setPhysReg(MI, i, PhysReg))
771 VirtDead.push_back(Reg);
772 }
773
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000774 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000775 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000776 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
777 MachineOperand &MO = MI->getOperand(i);
778 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
779 unsigned Reg = MO.getReg();
780 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000781 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
782 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000783 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000784 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000785
786 // Also mark PartialDefs as used to avoid reallocation.
787 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000788 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000789}
790
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000791void RAFast::AllocateBasicBlock() {
792 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000793
794 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000795 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000796
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000797 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000798
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000799 // Add live-in registers as live.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000800 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
801 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000802 if (MRI->isAllocatable(*I))
Jakob Stoklund Olesen2c325dc2010-08-31 19:54:25 +0000803 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000804
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000805 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000806 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000807
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000808 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000809 while (MII != MBB->end()) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000810 MachineInstr *MI = MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000811 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000812 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000813 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000814 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
815 if (PhysRegState[Reg] == regDisabled) continue;
816 dbgs() << " " << TRI->getName(Reg);
817 switch(PhysRegState[Reg]) {
818 case regFree:
819 break;
820 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000821 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000822 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000823 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000824 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000825 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
826 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
827 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000828 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000829 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000830 break;
831 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000832 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000833 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000834 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000835 // Check that LiveVirtRegs is the inverse.
836 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
837 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000838 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000839 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000840 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000841 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000842 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000843 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000844 });
845
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000846 // Debug values are not allowed to change codegen in any way.
847 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000848 bool ScanDbgValue = true;
849 while (ScanDbgValue) {
850 ScanDbgValue = false;
851 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
852 MachineOperand &MO = MI->getOperand(i);
853 if (!MO.isReg()) continue;
854 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000855 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000856 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000857 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000858 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000859 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000860 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000861 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000862 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000863 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000864 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000865 }
Devang Pateld61b7352010-07-19 23:25:39 +0000866 else {
867 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000868 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000869 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000870 const MDNode *Var = MI->getDebugVariable();
871 const MDNode *Expr = MI->getDebugExpression();
Devang Pateld61b7352010-07-19 23:25:39 +0000872 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000873 MachineBasicBlock *MBB = MI->getParent();
874 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
875 TII->get(TargetOpcode::DBG_VALUE))
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000876 .addFrameIndex(SS)
877 .addImm(Offset)
878 .addMetadata(Var)
879 .addMetadata(Expr);
David Blaikie0252265b2013-06-16 20:34:15 +0000880 DEBUG(dbgs() << "Modifying debug info due to spill:"
881 << "\t" << *NewDV);
882 // Scan NewDV operands from the beginning.
883 MI = NewDV;
884 ScanDbgValue = true;
885 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000886 }
Devang Patel57e72372010-07-09 21:48:31 +0000887 }
Devang Patel43bde962011-11-15 21:03:58 +0000888 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000889 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000890 }
891 // Next instruction.
892 continue;
893 }
894
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000895 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000896 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000897 if (MI->isCopy()) {
898 CopyDst = MI->getOperand(0).getReg();
899 CopySrc = MI->getOperand(1).getReg();
900 CopyDstSub = MI->getOperand(0).getSubReg();
901 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000902 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000903
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000904 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000905 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000906
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000907 // First scan.
908 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000909 // Find the end of the virtreg operands
910 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000911 bool hasTiedOps = false;
912 bool hasEarlyClobbers = false;
913 bool hasPartialRedefs = false;
914 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000915 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
916 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000917 // Make sure MRI knows about registers clobbered by regmasks.
918 if (MO.isRegMask()) {
919 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
920 continue;
921 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000922 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000923 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000924 if (!Reg) continue;
925 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
926 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000927 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000928 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000929 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000930 } else {
931 if (MO.isEarlyClobber())
932 hasEarlyClobbers = true;
933 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
934 hasPartialRedefs = true;
935 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000936 continue;
937 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000938 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000939 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000940 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000941 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +0000942 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
943 regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000944 hasEarlyClobbers = true;
945 } else
946 hasPhysDefs = true;
947 }
948
949 // The instruction may have virtual register operands that must be allocated
950 // the same register at use-time and def-time: early clobbers and tied
951 // operands. If there are also physical defs, these registers must avoid
952 // both physical defs and uses, making them more constrained than normal
953 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000954 // Similarly, if there are multiple defs and tied operands, we must make
955 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000956 // We didn't detect inline asm tied operands above, so just make this extra
957 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000958 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000959 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000960 handleThroughOperands(MI, VirtDead);
961 // Don't attempt coalescing when we have funny stuff going on.
962 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000963 // Pretend we have early clobbers so the use operands get marked below.
964 // This is not necessary for the common case of a single tied use.
965 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000966 }
967
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000968 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000969 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000970 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000971 MachineOperand &MO = MI->getOperand(i);
972 if (!MO.isReg()) continue;
973 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000974 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000975 if (MO.isUse()) {
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000976 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000977 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000978 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000979 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000980 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000981 }
982 }
983
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000984 for (UsedInInstrSet::iterator
985 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000986 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesen3f0241e2010-05-11 20:30:28 +0000987
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000988 // Track registers defined by instruction - early clobbers and tied uses at
989 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000990 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000991 if (hasEarlyClobbers) {
992 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
993 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000994 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000995 unsigned Reg = MO.getReg();
996 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000997 // Look for physreg defs and tied uses.
998 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000999 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +00001000 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001001 }
1002
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001003 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +00001004 if (MI->isCall()) {
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001005 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +00001006 // exception is thrown, the landing pad is going to expect to find
1007 // registers in their spill slots, and 2. we don't have to wade through
1008 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001009 DefOpEnd = VirtOpEnd;
1010 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1011 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001012
1013 // The imp-defs are skipped below, but we still need to mark those
1014 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001015 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001016 }
1017
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001018 // Third scan.
1019 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001020 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001021 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001022 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1023 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001024 unsigned Reg = MO.getReg();
1025
1026 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001027 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001028 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1029 regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001030 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001031 }
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001032 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001033 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001034 if (setPhysReg(MI, i, PhysReg)) {
1035 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001036 CopyDst = 0; // cancel coalescing;
1037 } else
1038 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001039 }
1040
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001041 // Kill dead defs after the scan to ensure that multiple defs of the same
1042 // register are allocated identically. We didn't need to do this for uses
1043 // because we are crerating our own kill flags, and they are always at the
1044 // last use.
1045 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1046 killVirtReg(VirtDead[i]);
1047 VirtDead.clear();
1048
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001049 for (UsedInInstrSet::iterator
1050 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001051 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001052
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001053 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1054 DEBUG(dbgs() << "-- coalescing: " << *MI);
1055 Coalesced.push_back(MI);
1056 } else {
1057 DEBUG(dbgs() << "<< " << *MI);
1058 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001059 }
1060
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001061 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001062 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1063 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001064
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001065 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001066 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001067 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001068 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001069 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001070
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001071 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072}
1073
1074/// runOnMachineFunction - Register allocate the whole function
1075///
1076bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001077 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001078 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001079 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001080 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001081 TM = &Fn.getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +00001082 TRI = TM->getSubtargetImpl()->getRegisterInfo();
1083 TII = TM->getSubtargetImpl()->getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001084 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001085 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001086 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001087 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001088
Andrew Trickd3f8fe82012-02-10 04:10:36 +00001089 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1090
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001091 // initialize the virtual->physical register map to have a 'null'
1092 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001093 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001094 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001095
1096 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001097 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1098 MBBi != MBBe; ++MBBi) {
1099 MBB = &*MBBi;
1100 AllocateBasicBlock();
1101 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001102
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001103 // Add the clobber lists for all the instructions we skipped earlier.
Craig Topper46276792014-08-24 23:23:06 +00001104 for (const MCInstrDesc *Desc : SkippedInstrs)
1105 if (const uint16_t *Defs = Desc->getImplicitDefs())
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001106 while (*Defs)
1107 MRI->setPhysRegUsed(*Defs++);
1108
Andrew Trickda84e642012-02-21 04:51:23 +00001109 // All machine operands and other references to virtual registers have been
1110 // replaced. Remove the virtual registers.
1111 MRI->clearVirtRegs();
1112
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001113 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001114 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001115 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001116 return true;
1117}
1118
1119FunctionPass *llvm::createFastRegisterAllocator() {
1120 return new RAFast();
1121}