blob: 4727f2437f7d3ee10b4a50cefe06e18b8b48744e [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];
David Blaikie0252265b2013-06-16 20:34:15 +0000302 const MDNode *MDPtr = DBG->getOperand(2).getMetadata();
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000303 bool IsIndirect = DBG->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000304 uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
Devang Patel8a18aee2010-08-06 00:26:18 +0000305 DebugLoc DL;
306 if (MI == MBB->end()) {
307 // If MI is at basic block end then use last instruction's location.
308 MachineBasicBlock::iterator EI = MI;
309 DL = (--EI)->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000310 } else
Devang Patel8a18aee2010-08-06 00:26:18 +0000311 DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000312 MachineBasicBlock *MBB = DBG->getParent();
313 MachineInstr *NewDV =
314 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
315 .addFrameIndex(FI).addImm(Offset).addMetadata(MDPtr);
316 (void)NewDV;
317 DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
Devang Pateld71bc1a2010-08-04 18:42:02 +0000318 }
Jakob Stoklund Olesenbd5e0762012-02-22 16:50:46 +0000319 // Now this register is spilled there is should not be any DBG_VALUE
320 // pointing to this register because they are all pointing to spilled value
321 // now.
Devang Pateld88b8ba2011-06-21 23:02:36 +0000322 LRIDbgValues.clear();
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000323 if (SpillKill)
Craig Topperc0196b12014-04-14 00:51:57 +0000324 LR.LastUse = nullptr; // Don't kill register again
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000325 }
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000326 killVirtReg(LRI);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000327}
328
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000329/// spillAll - Spill all dirty virtregs without killing them.
Akira Hatanakad837be72012-10-31 00:56:01 +0000330void RAFast::spillAll(MachineBasicBlock::iterator MI) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000331 if (LiveVirtRegs.empty()) return;
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000332 isBulkSpilling = true;
Jakob Stoklund Olesen70563bb2010-05-17 20:01:22 +0000333 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
334 // of spilling here is deterministic, if arbitrary.
335 for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
336 i != e; ++i)
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000337 spillVirtReg(MI, i);
338 LiveVirtRegs.clear();
339 isBulkSpilling = false;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000340}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000341
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000342/// usePhysReg - Handle the direct use of a physical register.
343/// Check that the register is not used by a virtreg.
344/// Kill the physreg, marking it free.
345/// This may add implicit kills to MO->getParent() and invalidate MO.
346void RAFast::usePhysReg(MachineOperand &MO) {
347 unsigned PhysReg = MO.getReg();
348 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
349 "Bad usePhysReg operand");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000350 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000351 switch (PhysRegState[PhysReg]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000352 case regDisabled:
353 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000354 case regReserved:
355 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000356 // Fall through
357 case regFree:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000358 MO.setIsKill();
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000359 return;
360 default:
Eric Christopher66a8bf52010-12-08 21:35:09 +0000361 // The physreg was allocated to a virtual register. That means the value we
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000362 // wanted has been clobbered.
363 llvm_unreachable("Instruction uses an allocated register");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000364 }
365
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000366 // Maybe a superregister is reserved?
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000367 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
368 unsigned Alias = *AI;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000369 switch (PhysRegState[Alias]) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000370 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000371 break;
372 case regReserved:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000373 assert(TRI->isSuperRegister(PhysReg, Alias) &&
374 "Instruction is not using a subregister of a reserved register");
375 // Leave the superregister in the working set.
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000376 PhysRegState[Alias] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000377 MO.getParent()->addRegisterKilled(Alias, TRI, true);
378 return;
379 case regFree:
380 if (TRI->isSuperRegister(PhysReg, Alias)) {
381 // Leave the superregister in the working set.
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000382 MO.getParent()->addRegisterKilled(Alias, TRI, true);
383 return;
384 }
385 // Some other alias was in the working set - clear it.
386 PhysRegState[Alias] = regDisabled;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000387 break;
388 default:
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000389 llvm_unreachable("Instruction uses an alias of an allocated register");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000390 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000391 }
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000392
393 // All aliases are disabled, bring register into working set.
394 PhysRegState[PhysReg] = regFree;
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000395 MO.setIsKill();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000396}
397
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000398/// definePhysReg - Mark PhysReg as reserved or free after spilling any
399/// virtregs. This is very similar to defineVirtReg except the physreg is
400/// reserved instead of allocated.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000401void RAFast::definePhysReg(MachineInstr *MI, unsigned PhysReg,
402 RegState NewState) {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000403 markRegUsedInInstr(PhysReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000404 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
405 case regDisabled:
406 break;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000407 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000408 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000409 // Fall through.
410 case regFree:
411 case regReserved:
412 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000413 return;
414 }
415
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000416 // This is a disabled register, disable all aliases.
417 PhysRegState[PhysReg] = NewState;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000418 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
419 unsigned Alias = *AI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000420 switch (unsigned VirtReg = PhysRegState[Alias]) {
421 case regDisabled:
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000422 break;
423 default:
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +0000424 spillVirtReg(MI, VirtReg);
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000425 // Fall through.
426 case regFree:
427 case regReserved:
428 PhysRegState[Alias] = regDisabled;
429 if (TRI->isSuperRegister(PhysReg, Alias))
430 return;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000431 break;
432 }
433 }
434}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000435
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000436
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000437// calcSpillCost - Return the cost of spilling clearing out PhysReg and
438// aliases so it is free for allocation.
439// Returns 0 when PhysReg is free or disabled with all aliases disabled - it
440// can be allocated directly.
441// Returns spillImpossible when PhysReg or an alias can't be spilled.
442unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000443 if (isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000444 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
Jakob Stoklund Olesen58579272010-05-17 21:02:08 +0000445 return spillImpossible;
Eric Christopherde9d5852011-04-12 22:17:44 +0000446 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000447 switch (unsigned VirtReg = PhysRegState[PhysReg]) {
448 case regDisabled:
449 break;
450 case regFree:
451 return 0;
452 case regReserved:
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000453 DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
454 << PrintReg(PhysReg, TRI) << " is reserved already.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000455 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000456 default: {
457 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
458 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
459 return I->Dirty ? spillDirty : spillClean;
460 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000461 }
462
Eric Christopherc3783362011-04-12 00:48:08 +0000463 // This is a disabled register, add up cost of aliases.
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000464 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000465 unsigned Cost = 0;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000466 for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
467 unsigned Alias = *AI;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000468 switch (unsigned VirtReg = PhysRegState[Alias]) {
469 case regDisabled:
470 break;
471 case regFree:
472 ++Cost;
473 break;
474 case regReserved:
475 return spillImpossible;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000476 default: {
477 LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
478 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
479 Cost += I->Dirty ? spillDirty : spillClean;
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000480 break;
481 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000482 }
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000483 }
484 return Cost;
485}
486
487
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000488/// assignVirtToPhysReg - This method updates local state so that we know
489/// that PhysReg is the proper container for VirtReg now. The physical
490/// register must not be used for anything else when this is called.
491///
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000492void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
493 DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000494 << PrintReg(PhysReg, TRI) << "\n");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000495 PhysRegState[PhysReg] = LR.VirtReg;
496 assert(!LR.PhysReg && "Already assigned a physreg");
497 LR.PhysReg = PhysReg;
498}
499
500RAFast::LiveRegMap::iterator
501RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
502 LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
503 assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
504 assignVirtToPhysReg(*LRI, PhysReg);
505 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000506}
507
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000508/// allocVirtReg - Allocate a physical register for VirtReg.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000509RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr *MI,
510 LiveRegMap::iterator LRI,
511 unsigned Hint) {
512 const unsigned VirtReg = LRI->VirtReg;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000513
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000514 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
515 "Can only allocate virtual registers");
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000516
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000517 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000518
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000519 // Ignore invalid hints.
520 if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000521 !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000522 Hint = 0;
523
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000524 // Take hint when possible.
525 if (Hint) {
Jakob Stoklund Olesenfb03a922011-06-13 03:26:46 +0000526 // Ignore the hint if we would have to spill a dirty register.
527 unsigned Cost = calcSpillCost(Hint);
528 if (Cost < spillDirty) {
529 if (Cost)
530 definePhysReg(MI, Hint, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000531 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
532 // That invalidates LRI, so run a new lookup for VirtReg.
533 return assignVirtToPhysReg(VirtReg, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000534 }
535 }
536
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000537 ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000538
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000539 // First try to find a completely free register.
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000540 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000541 unsigned PhysReg = *I;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000542 if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000543 assignVirtToPhysReg(*LRI, PhysReg);
544 return LRI;
545 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000546 }
547
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000548 DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
549 << RC->getName() << "\n");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000550
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000551 unsigned BestReg = 0, BestCost = spillImpossible;
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000552 for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
Jakob Stoklund Olesen6649cda2010-05-17 15:30:32 +0000553 unsigned Cost = calcSpillCost(*I);
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000554 DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
Eric Christopherde9d5852011-04-12 22:17:44 +0000555 DEBUG(dbgs() << "\tCost: " << Cost << "\n");
556 DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000557 // Cost is 0 when all aliases are already disabled.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000558 if (Cost == 0) {
559 assignVirtToPhysReg(*LRI, *I);
560 return LRI;
561 }
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000562 if (Cost < BestCost)
563 BestReg = *I, BestCost = Cost;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000564 }
565
566 if (BestReg) {
Jakob Stoklund Olesenf5e8c862010-05-17 15:30:37 +0000567 definePhysReg(MI, BestReg, regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000568 // definePhysReg may kill virtual registers and modify LiveVirtRegs.
569 // That invalidates LRI, so run a new lookup for VirtReg.
570 return assignVirtToPhysReg(VirtReg, BestReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000571 }
572
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000573 // Nothing we can do. Report an error and keep going with a bad allocation.
Benjamin Kramer7200a462013-10-05 19:33:37 +0000574 if (MI->isInlineAsm())
575 MI->emitError("inline assembly requires more registers than available");
576 else
577 MI->emitError("ran out of registers during register allocation");
Jakob Stoklund Olesen54f7c592011-07-02 07:17:37 +0000578 definePhysReg(MI, *AO.begin(), regFree);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000579 return assignVirtToPhysReg(VirtReg, *AO.begin());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000580}
581
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000582/// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000583RAFast::LiveRegMap::iterator
584RAFast::defineVirtReg(MachineInstr *MI, unsigned OpNum,
585 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000586 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
587 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000588 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000589 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000590 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000591 if (New) {
592 // If there is no hint, peek at the only use of this register.
593 if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
594 MRI->hasOneNonDBGUse(VirtReg)) {
Owen Anderson16c6bf42014-03-13 23:12:04 +0000595 const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000596 // It's a copy, use the destination register as a hint.
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000597 if (UseMI.isCopyLike())
598 Hint = UseMI.getOperand(0).getReg();
Jakob Stoklund Olesen7d22a81b2010-05-17 04:50:57 +0000599 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000600 LRI = allocVirtReg(MI, LRI, Hint);
601 } else if (LRI->LastUse) {
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000602 // Redefining a live register - kill at the last use, unless it is this
603 // instruction defining VirtReg multiple times.
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000604 if (LRI->LastUse != MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
605 addKillFlag(*LRI);
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000606 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000607 assert(LRI->PhysReg && "Register not assigned");
608 LRI->LastUse = MI;
609 LRI->LastOpNum = OpNum;
610 LRI->Dirty = true;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000611 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000612 return LRI;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000613}
614
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000615/// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000616RAFast::LiveRegMap::iterator
617RAFast::reloadVirtReg(MachineInstr *MI, unsigned OpNum,
618 unsigned VirtReg, unsigned Hint) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000619 assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
620 "Not a virtual register");
Jakob Stoklund Olesen397068d2010-05-17 02:49:15 +0000621 LiveRegMap::iterator LRI;
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000622 bool New;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000623 std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000624 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesend2ef1fb2010-05-17 02:07:29 +0000625 if (New) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000626 LRI = allocVirtReg(MI, LRI, Hint);
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000627 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000628 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000629 DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000630 << PrintReg(LRI->PhysReg, TRI) << "\n");
631 TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000632 ++NumLoads;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000633 } else if (LRI->Dirty) {
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000634 if (isLastUseOfLocalReg(MO)) {
635 DEBUG(dbgs() << "Killing last use: " << MO << "\n");
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000636 if (MO.isUse())
637 MO.setIsKill();
638 else
639 MO.setIsDead();
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000640 } else if (MO.isKill()) {
641 DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
642 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000643 } else if (MO.isDead()) {
644 DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
645 MO.setIsDead(false);
Jakob Stoklund Olesen84ce2902010-05-15 06:09:08 +0000646 }
Jakob Stoklund Olesenedd3d9d2010-05-17 03:26:06 +0000647 } else if (MO.isKill()) {
648 // We must remove kill flags from uses of reloaded registers because the
649 // register would be killed immediately, and there might be a second use:
650 // %foo = OR %x<kill>, %x
651 // This would cause a second reload of %x into a different register.
652 DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
653 MO.setIsKill(false);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000654 } else if (MO.isDead()) {
655 DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
656 MO.setIsDead(false);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000657 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000658 assert(LRI->PhysReg && "Register not assigned");
659 LRI->LastUse = MI;
660 LRI->LastOpNum = OpNum;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000661 markRegUsedInInstr(LRI->PhysReg);
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000662 return LRI;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000663}
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000664
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000665// setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
666// subregs. This may invalidate any operand pointers.
667// Return true if the operand kills its register.
668bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
669 MachineOperand &MO = MI->getOperand(OpNum);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000670 bool Dead = MO.isDead();
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000671 if (!MO.getSubReg()) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000672 MO.setReg(PhysReg);
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000673 return MO.isKill() || Dead;
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000674 }
675
676 // Handle subregister index.
677 MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
678 MO.setSubReg(0);
Jakob Stoklund Olesene0eddb22010-05-19 21:36:05 +0000679
680 // A kill flag implies killing the full register. Add corresponding super
681 // register kill.
682 if (MO.isKill()) {
683 MI->addRegisterKilled(PhysReg, TRI, true);
Jakob Stoklund Olesene07a4082010-05-17 02:49:21 +0000684 return true;
685 }
Jakob Stoklund Olesendc2e0cd2012-05-14 21:10:25 +0000686
687 // A <def,read-undef> of a sub-register requires an implicit def of the full
688 // register.
689 if (MO.isDef() && MO.isUndef())
690 MI->addRegisterDefined(PhysReg, TRI);
691
Jakob Stoklund Olesena13fd122012-05-14 21:30:58 +0000692 return Dead;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000693}
694
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000695// Handle special instruction operand like early clobbers and tied ops when
696// there are additional physreg defines.
697void RAFast::handleThroughOperands(MachineInstr *MI,
698 SmallVectorImpl<unsigned> &VirtDead) {
699 DEBUG(dbgs() << "Scanning for through registers:");
700 SmallSet<unsigned, 8> ThroughRegs;
701 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
702 MachineOperand &MO = MI->getOperand(i);
703 if (!MO.isReg()) continue;
704 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000705 if (!TargetRegisterInfo::isVirtualRegister(Reg))
706 continue;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000707 if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
708 (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000709 if (ThroughRegs.insert(Reg))
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000710 DEBUG(dbgs() << ' ' << PrintReg(Reg));
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000711 }
712 }
713
714 // If any physreg defines collide with preallocated through registers,
715 // we must spill and reallocate.
716 DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
717 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
718 MachineOperand &MO = MI->getOperand(i);
719 if (!MO.isReg() || !MO.isDef()) continue;
720 unsigned Reg = MO.getReg();
721 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000722 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000723 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000724 if (ThroughRegs.count(PhysRegState[*AI]))
725 definePhysReg(MI, *AI, regFree);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000726 }
727 }
728
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000729 SmallVector<unsigned, 8> PartialDefs;
Rafael Espindola2021f382011-11-22 06:27:18 +0000730 DEBUG(dbgs() << "Allocating tied uses.\n");
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000731 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
732 MachineOperand &MO = MI->getOperand(i);
733 if (!MO.isReg()) continue;
734 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000735 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000736 if (MO.isUse()) {
737 unsigned DefIdx = 0;
738 if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
739 DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
740 << DefIdx << ".\n");
741 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000742 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000743 setPhysReg(MI, i, PhysReg);
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000744 // Note: we don't update the def operand yet. That would cause the normal
745 // def-scan to attempt spilling.
746 } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
747 DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
748 // Reload the register, but don't assign to the operand just yet.
749 // That would confuse the later phys-def processing pass.
750 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000751 PartialDefs.push_back(LRI->PhysReg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000752 }
753 }
754
Rafael Espindola2021f382011-11-22 06:27:18 +0000755 DEBUG(dbgs() << "Allocating early clobbers.\n");
756 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
757 MachineOperand &MO = MI->getOperand(i);
758 if (!MO.isReg()) continue;
759 unsigned Reg = MO.getReg();
760 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
761 if (!MO.isEarlyClobber())
762 continue;
763 // Note: defineVirtReg may invalidate MO.
764 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, 0);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000765 unsigned PhysReg = LRI->PhysReg;
Rafael Espindola2021f382011-11-22 06:27:18 +0000766 if (setPhysReg(MI, i, PhysReg))
767 VirtDead.push_back(Reg);
768 }
769
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000770 // Restore UsedInInstr to a state usable for allocating normal virtual uses.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000771 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000772 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
773 MachineOperand &MO = MI->getOperand(i);
774 if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
775 unsigned Reg = MO.getReg();
776 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesena1dceb02011-06-28 17:24:32 +0000777 DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
778 << " as used in instr\n");
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000779 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000780 }
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000781
782 // Also mark PartialDefs as used to avoid reallocation.
783 for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000784 markRegUsedInInstr(PartialDefs[i]);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000785}
786
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000787void RAFast::AllocateBasicBlock() {
788 DEBUG(dbgs() << "\nAllocating " << *MBB);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000789
790 PhysRegState.assign(TRI->getNumRegs(), regDisabled);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000791 assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000792
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000793 MachineBasicBlock::iterator MII = MBB->begin();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000794
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000795 // Add live-in registers as live.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000796 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
797 E = MBB->livein_end(); I != E; ++I)
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000798 if (MRI->isAllocatable(*I))
Jakob Stoklund Olesen2c325dc2010-08-31 19:54:25 +0000799 definePhysReg(MII, *I, regReserved);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000800
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000801 SmallVector<unsigned, 8> VirtDead;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000802 SmallVector<MachineInstr*, 32> Coalesced;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000803
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000804 // Otherwise, sequentially allocate each instruction in the MBB.
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +0000805 while (MII != MBB->end()) {
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000806 MachineInstr *MI = MII++;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000807 const MCInstrDesc &MCID = MI->getDesc();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000808 DEBUG({
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000809 dbgs() << "\n>> " << *MI << "Regs:";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000810 for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
811 if (PhysRegState[Reg] == regDisabled) continue;
812 dbgs() << " " << TRI->getName(Reg);
813 switch(PhysRegState[Reg]) {
814 case regFree:
815 break;
816 case regReserved:
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +0000817 dbgs() << "*";
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000818 break;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000819 default: {
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000820 dbgs() << '=' << PrintReg(PhysRegState[Reg]);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000821 LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
822 assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
823 if (I->Dirty)
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000824 dbgs() << "*";
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000825 assert(I->PhysReg == Reg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000826 break;
827 }
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000828 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000829 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000830 dbgs() << '\n';
Jakob Stoklund Olesen13266812010-05-11 23:24:45 +0000831 // Check that LiveVirtRegs is the inverse.
832 for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
833 e = LiveVirtRegs.end(); i != e; ++i) {
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000834 assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000835 "Bad map key");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000836 assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000837 "Bad map value");
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000838 assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000839 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000840 });
841
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000842 // Debug values are not allowed to change codegen in any way.
843 if (MI->isDebugValue()) {
Devang Pateld61b7352010-07-19 23:25:39 +0000844 bool ScanDbgValue = true;
845 while (ScanDbgValue) {
846 ScanDbgValue = false;
847 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
848 MachineOperand &MO = MI->getOperand(i);
849 if (!MO.isReg()) continue;
850 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000851 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000852 LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
Devang Pateld61b7352010-07-19 23:25:39 +0000853 if (LRI != LiveVirtRegs.end())
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000854 setPhysReg(MI, i, LRI->PhysReg);
Devang Patel57e72372010-07-09 21:48:31 +0000855 else {
Devang Pateld61b7352010-07-19 23:25:39 +0000856 int SS = StackSlotForVirtReg[Reg];
Devang Patel6095d812010-09-10 20:32:09 +0000857 if (SS == -1) {
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000858 // We can't allocate a physreg for a DebugValue, sorry!
Devang Patel6095d812010-09-10 20:32:09 +0000859 DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000860 MO.setReg(0);
Devang Patel6095d812010-09-10 20:32:09 +0000861 }
Devang Pateld61b7352010-07-19 23:25:39 +0000862 else {
863 // Modify DBG_VALUE now that the value is in a spill slot.
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000864 bool IsIndirect = MI->isIndirectDebugValue();
Adrian Prantld3f6fe52013-07-10 16:56:52 +0000865 uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000866 const MDNode *MDPtr =
Devang Pateld61b7352010-07-19 23:25:39 +0000867 MI->getOperand(MI->getNumOperands()-1).getMetadata();
868 DebugLoc DL = MI->getDebugLoc();
David Blaikie0252265b2013-06-16 20:34:15 +0000869 MachineBasicBlock *MBB = MI->getParent();
870 MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
871 TII->get(TargetOpcode::DBG_VALUE))
872 .addFrameIndex(SS).addImm(Offset).addMetadata(MDPtr);
873 DEBUG(dbgs() << "Modifying debug info due to spill:"
874 << "\t" << *NewDV);
875 // Scan NewDV operands from the beginning.
876 MI = NewDV;
877 ScanDbgValue = true;
878 break;
Devang Pateld61b7352010-07-19 23:25:39 +0000879 }
Devang Patel57e72372010-07-09 21:48:31 +0000880 }
Devang Patel43bde962011-11-15 21:03:58 +0000881 LiveDbgValueMap[Reg].push_back(MI);
Devang Patel57e72372010-07-09 21:48:31 +0000882 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000883 }
884 // Next instruction.
885 continue;
886 }
887
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000888 // If this is a copy, we may be able to coalesce.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000889 unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
Jakob Stoklund Olesen4c82a9e2010-07-03 00:04:37 +0000890 if (MI->isCopy()) {
891 CopyDst = MI->getOperand(0).getReg();
892 CopySrc = MI->getOperand(1).getReg();
893 CopyDstSub = MI->getOperand(0).getSubReg();
894 CopySrcSub = MI->getOperand(1).getSubReg();
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000895 }
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +0000896
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000897 // Track registers used by instruction.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000898 UsedInInstr.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000899
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000900 // First scan.
901 // Mark physreg uses and early clobbers as used.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000902 // Find the end of the virtreg operands
903 unsigned VirtOpEnd = 0;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000904 bool hasTiedOps = false;
905 bool hasEarlyClobbers = false;
906 bool hasPartialRedefs = false;
907 bool hasPhysDefs = false;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000908 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
909 MachineOperand &MO = MI->getOperand(i);
Chad Rosier8d2c2292012-11-06 22:52:42 +0000910 // Make sure MRI knows about registers clobbered by regmasks.
911 if (MO.isRegMask()) {
912 MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
913 continue;
914 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000915 if (!MO.isReg()) continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000916 unsigned Reg = MO.getReg();
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000917 if (!Reg) continue;
918 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
919 VirtOpEnd = i+1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000920 if (MO.isUse()) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000921 hasTiedOps = hasTiedOps ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000922 MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000923 } else {
924 if (MO.isEarlyClobber())
925 hasEarlyClobbers = true;
926 if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
927 hasPartialRedefs = true;
928 }
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000929 continue;
930 }
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000931 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000932 if (MO.isUse()) {
Jakob Stoklund Olesen4d5c1062010-05-14 18:03:25 +0000933 usePhysReg(MO);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000934 } else if (MO.isEarlyClobber()) {
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +0000935 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
936 regFree : regReserved);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000937 hasEarlyClobbers = true;
938 } else
939 hasPhysDefs = true;
940 }
941
942 // The instruction may have virtual register operands that must be allocated
943 // the same register at use-time and def-time: early clobbers and tied
944 // operands. If there are also physical defs, these registers must avoid
945 // both physical defs and uses, making them more constrained than normal
946 // operands.
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000947 // Similarly, if there are multiple defs and tied operands, we must make
948 // sure the same register is allocated to uses and defs.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000949 // We didn't detect inline asm tied operands above, so just make this extra
950 // pass for all inline asm.
Jakob Stoklund Olesendadea5b2010-06-29 19:15:30 +0000951 if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
Evan Cheng6cc775f2011-06-28 19:10:37 +0000952 (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000953 handleThroughOperands(MI, VirtDead);
954 // Don't attempt coalescing when we have funny stuff going on.
955 CopyDst = 0;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000956 // Pretend we have early clobbers so the use operands get marked below.
957 // This is not necessary for the common case of a single tied use.
958 hasEarlyClobbers = true;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +0000959 }
960
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000961 // Second scan.
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000962 // Allocate virtreg uses.
Jakob Stoklund Olesene68b8142010-05-14 21:55:52 +0000963 for (unsigned i = 0; i != VirtOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000964 MachineOperand &MO = MI->getOperand(i);
965 if (!MO.isReg()) continue;
966 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000967 if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000968 if (MO.isUse()) {
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000969 LiveRegMap::iterator LRI = reloadVirtReg(MI, i, Reg, CopyDst);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +0000970 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +0000971 CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +0000972 if (setPhysReg(MI, i, PhysReg))
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +0000973 killVirtReg(LRI);
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000974 }
975 }
976
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000977 for (UsedInInstrSet::iterator
978 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000979 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesen3f0241e2010-05-11 20:30:28 +0000980
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000981 // Track registers defined by instruction - early clobbers and tied uses at
982 // this point.
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +0000983 UsedInInstr.clear();
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000984 if (hasEarlyClobbers) {
985 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
986 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000987 if (!MO.isReg()) continue;
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000988 unsigned Reg = MO.getReg();
989 if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Jakob Stoklund Olesen36cf1192010-07-29 00:52:19 +0000990 // Look for physreg defs and tied uses.
991 if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +0000992 markRegUsedInInstr(Reg);
Jakob Stoklund Olesen0d94d7a2010-06-28 18:34:34 +0000993 }
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +0000994 }
995
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +0000996 unsigned DefOpEnd = MI->getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000997 if (MI->isCall()) {
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +0000998 // Spill all virtregs before a call. This serves two purposes: 1. If an
Jim Grosbachcb2e56f2010-09-01 19:16:29 +0000999 // exception is thrown, the landing pad is going to expect to find
1000 // registers in their spill slots, and 2. we don't have to wade through
1001 // all the <imp-def> operands on the call instruction.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001002 DefOpEnd = VirtOpEnd;
1003 DEBUG(dbgs() << " Spilling remaining registers before call.\n");
1004 spillAll(MI);
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001005
1006 // The imp-defs are skipped below, but we still need to mark those
1007 // registers as used by the function.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001008 SkippedInstrs.insert(&MCID);
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001009 }
1010
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001011 // Third scan.
1012 // Allocate defs and collect dead defs.
Jakob Stoklund Olesen1069a092010-05-17 02:49:18 +00001013 for (unsigned i = 0; i != DefOpEnd; ++i) {
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001014 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen246e9a02010-06-15 16:20:57 +00001015 if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1016 continue;
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001017 unsigned Reg = MO.getReg();
1018
1019 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +00001020 if (!MRI->isAllocatable(Reg)) continue;
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001021 definePhysReg(MI, Reg, (MO.isImplicit() || MO.isDead()) ?
1022 regFree : regReserved);
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001023 continue;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001024 }
Jakob Stoklund Olesenf915d142010-05-17 03:26:09 +00001025 LiveRegMap::iterator LRI = defineVirtReg(MI, i, Reg, CopySrc);
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001026 unsigned PhysReg = LRI->PhysReg;
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001027 if (setPhysReg(MI, i, PhysReg)) {
1028 VirtDead.push_back(Reg);
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001029 CopyDst = 0; // cancel coalescing;
1030 } else
1031 CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001032 }
1033
Jakob Stoklund Olesen663543b42010-05-18 21:10:50 +00001034 // Kill dead defs after the scan to ensure that multiple defs of the same
1035 // register are allocated identically. We didn't need to do this for uses
1036 // because we are crerating our own kill flags, and they are always at the
1037 // last use.
1038 for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1039 killVirtReg(VirtDead[i]);
1040 VirtDead.clear();
1041
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001042 for (UsedInInstrSet::iterator
1043 I = UsedInInstr.begin(), E = UsedInInstr.end(); I != E; ++I)
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001044 MRI->setRegUnitUsed(*I);
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001045
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001046 if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1047 DEBUG(dbgs() << "-- coalescing: " << *MI);
1048 Coalesced.push_back(MI);
1049 } else {
1050 DEBUG(dbgs() << "<< " << *MI);
1051 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001052 }
1053
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001054 // Spill all physical registers holding virtual registers now.
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001055 DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1056 spillAll(MBB->getFirstTerminator());
Jakob Stoklund Olesenf1b30292010-05-11 18:54:45 +00001057
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001058 // Erase all the coalesced copies. We are delaying it until now because
Jakob Stoklund Olesen8044c982010-05-17 02:07:32 +00001059 // LiveVirtRegs might refer to the instrs.
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001060 for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001061 MBB->erase(Coalesced[i]);
Jakob Stoklund Olesen6c038e32010-05-14 21:55:50 +00001062 NumCopies += Coalesced.size();
Jakob Stoklund Olesenceb5a7a2010-05-14 04:30:51 +00001063
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001064 DEBUG(MBB->dump());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001065}
1066
1067/// runOnMachineFunction - Register allocate the whole function
1068///
1069bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
Jakob Stoklund Olesend74a5642010-05-13 20:43:17 +00001070 DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00001071 << "********** Function: " << Fn.getName() << '\n');
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001072 MF = &Fn;
Jakob Stoklund Olesen0ba2e2a2010-05-13 00:19:43 +00001073 MRI = &MF->getRegInfo();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001074 TM = &Fn.getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +00001075 TRI = TM->getSubtargetImpl()->getRegisterInfo();
1076 TII = TM->getSubtargetImpl()->getInstrInfo();
Chad Rosiered119d52012-11-28 00:21:29 +00001077 MRI->freezeReservedRegs(Fn);
Jakob Stoklund Olesen50663b72011-06-02 18:35:30 +00001078 RegClassInfo.runOnMachineFunction(Fn);
Jakob Stoklund Olesena2136be2012-10-17 01:37:59 +00001079 UsedInInstr.clear();
Jakob Stoklund Olesen2ff4dc02013-02-21 19:35:21 +00001080 UsedInInstr.setUniverse(TRI->getNumRegUnits());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001081
Andrew Trickd3f8fe82012-02-10 04:10:36 +00001082 assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1083
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001084 // initialize the virtual->physical register map to have a 'null'
1085 // mapping for all virtual registers
Jakob Stoklund Olesend82ac372011-01-09 21:58:20 +00001086 StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen9c4cd1b2012-02-22 01:02:37 +00001087 LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001088
1089 // Loop over all of the basic blocks, eliminating virtual register references
Jakob Stoklund Olesenfb43e062010-05-17 02:07:22 +00001090 for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1091 MBBi != MBBe; ++MBBi) {
1092 MBB = &*MBBi;
1093 AllocateBasicBlock();
1094 }
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001095
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001096 // Add the clobber lists for all the instructions we skipped earlier.
Craig Topper46276792014-08-24 23:23:06 +00001097 for (const MCInstrDesc *Desc : SkippedInstrs)
1098 if (const uint16_t *Defs = Desc->getImplicitDefs())
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001099 while (*Defs)
1100 MRI->setPhysRegUsed(*Defs++);
1101
Andrew Trickda84e642012-02-21 04:51:23 +00001102 // All machine operands and other references to virtual registers have been
1103 // replaced. Remove the virtual registers.
1104 MRI->clearVirtRegs();
1105
Jakob Stoklund Olesen864827a2010-06-04 18:08:29 +00001106 SkippedInstrs.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001107 StackSlotForVirtReg.clear();
Devang Pateld71bc1a2010-08-04 18:42:02 +00001108 LiveDbgValueMap.clear();
Jakob Stoklund Olesen8a070a52010-04-21 18:02:42 +00001109 return true;
1110}
1111
1112FunctionPass *llvm::createFastRegisterAllocator() {
1113 return new RAFast();
1114}