blob: 63725598248bf6a9aa10ffd66b7ee4a6ca70f701 [file] [log] [blame]
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb74e83c2002-12-16 16:15:28 +00009//
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
Chris Lattner4cc662b2003-08-03 21:47:31 +000015#define DEBUG_TYPE "regalloc"
Evan Chengddee8422006-11-15 20:55:15 +000016#include "llvm/BasicBlock.h"
Chris Lattner580f9be2002-12-28 20:40:43 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng22ff3ee2008-02-06 08:00:32 +000021#include "llvm/CodeGen/Passes.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000022#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000023#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerb74e83c2002-12-16 16:15:28 +000024#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Owen Anderson743a1e62008-07-10 01:56:35 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000030#include "llvm/ADT/IndexedMap.h"
Evan Cheng5a3c6a82009-01-29 02:20:59 +000031#include "llvm/ADT/SmallSet.h"
Evan Chengddee8422006-11-15 20:55:15 +000032#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/Statistic.h"
Evan Cheng2fc628d2008-02-06 19:16:53 +000034#include "llvm/ADT/STLExtras.h"
Chris Lattner27f29162004-10-26 15:35:58 +000035#include <algorithm>
Chris Lattneref09c632004-01-31 21:27:19 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattnercd3245a2006-12-19 22:41:21 +000038STATISTIC(NumStores, "Number of stores added");
39STATISTIC(NumLoads , "Number of loads added");
Jim Laskey13ec7022006-08-01 14:21:23 +000040
Dan Gohman844731a2008-05-13 00:00:25 +000041static RegisterRegAlloc
Dan Gohmanb8cab922008-10-14 20:25:08 +000042 localRegAlloc("local", "local register allocator",
Dan Gohman844731a2008-05-13 00:00:25 +000043 createLocalRegisterAllocator);
44
Chris Lattnercd3245a2006-12-19 22:41:21 +000045namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000046 class RALocal : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000047 public:
Devang Patel19974732007-05-03 01:11:54 +000048 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000049 RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
Devang Patel794fd752007-05-01 21:15:47 +000050 private:
Chris Lattner580f9be2002-12-28 20:40:43 +000051 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000052 MachineFunction *MF;
Evan Cheng736f89b2010-05-12 01:29:36 +000053 MachineRegisterInfo *MRI;
Dan Gohman6f0d0242008-02-10 18:45:23 +000054 const TargetRegisterInfo *TRI;
Owen Anderson6425f8b2008-01-07 01:35:56 +000055 const TargetInstrInfo *TII;
Chris Lattnerff863ba2002-12-25 05:05:46 +000056
Chris Lattnerb8822ad2003-08-04 23:36:39 +000057 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
58 // values are spilled.
Evan Chengbdb10fe2008-07-10 18:23:23 +000059 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000060
61 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000062 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000063 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000064
65 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000066 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000067 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000068
Chris Lattner64667b62004-02-09 01:26:13 +000069 // PhysRegsUsed - This array is effectively a map, containing entries for
70 // each physical register that currently has a value (ie, it is in
71 // Virt2PhysRegMap). The value mapped to is the virtual register
72 // corresponding to the physical register (the inverse of the
73 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000074 // because it is used by a future instruction, and to -2 if it is not
75 // allocatable. If the entry for a physical register is -1, then the
76 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000077 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000078 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000079
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +000080 // PhysRegsUseOrder - This contains a list of the physical registers that
81 // currently have a virtual register value in them. This list provides an
82 // ordering of registers, imposing a reallocation order. This list is only
83 // used if all registers are allocated and we have to spill one, in which
84 // case we spill the least recently used register. Entries at the front of
85 // the list are the least recently used registers, entries at the back are
86 // the most recently used.
Chris Lattnerb74e83c2002-12-16 16:15:28 +000087 //
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +000088 std::vector<unsigned> PhysRegsUseOrder;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000089
Evan Cheng839b7592008-01-17 02:08:17 +000090 // Virt2LastUseMap - This maps each virtual register to its last use
91 // (MachineInstr*, operand index pair).
92 IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
93 Virt2LastUseMap;
94
95 std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000096 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Evan Cheng839b7592008-01-17 02:08:17 +000097 return Virt2LastUseMap[Reg];
98 }
99
Chris Lattner91a452b2003-01-13 00:25:40 +0000100 // VirtRegModified - This bitset contains information about which virtual
101 // registers need to be spilled back to memory when their registers are
102 // scavenged. If a virtual register has simply been rematerialized, there
103 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000104 //
Evan Cheng644340a2008-01-17 00:35:26 +0000105 BitVector VirtRegModified;
Owen Anderson491fccc2008-07-08 22:24:50 +0000106
107 // UsedInMultipleBlocks - Tracks whether a particular register is used in
108 // more than one block.
109 BitVector UsedInMultipleBlocks;
Chris Lattner91a452b2003-01-13 00:25:40 +0000110
111 void markVirtRegModified(unsigned Reg, bool Val = true) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000112 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
113 Reg -= TargetRegisterInfo::FirstVirtualRegister;
Evan Cheng644340a2008-01-17 00:35:26 +0000114 if (Val)
115 VirtRegModified.set(Reg);
116 else
117 VirtRegModified.reset(Reg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000118 }
119
120 bool isVirtRegModified(unsigned Reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000121 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner4dd81632010-03-31 05:15:22 +0000122 assert(Reg - TargetRegisterInfo::FirstVirtualRegister <
123 VirtRegModified.size() && "Illegal virtual register!");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000124 return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
Chris Lattner91a452b2003-01-13 00:25:40 +0000125 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000126
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000127 void AddToPhysRegsUseOrder(unsigned Reg) {
128 std::vector<unsigned>::iterator It =
129 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
130 if (It != PhysRegsUseOrder.end())
131 PhysRegsUseOrder.erase(It);
132 PhysRegsUseOrder.push_back(Reg);
133 }
134
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000135 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000136 if (PhysRegsUseOrder.empty() ||
137 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
138
139 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) {
140 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
141 if (!areRegsEqual(Reg, RegMatch)) continue;
142
143 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
144 // Add it to the end of the list
145 PhysRegsUseOrder.push_back(RegMatch);
146 if (RegMatch == Reg)
147 return; // Found an exact match, exit early
148 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000149 }
150
151 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000152 virtual const char *getPassName() const {
153 return "Local Register Allocator";
154 }
155
Chris Lattner91a452b2003-01-13 00:25:40 +0000156 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000157 AU.setPreservesCFG();
Chris Lattner91a452b2003-01-13 00:25:40 +0000158 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000159 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000160 MachineFunctionPass::getAnalysisUsage(AU);
161 }
162
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000163 private:
164 /// runOnMachineFunction - Register allocate the whole function
165 bool runOnMachineFunction(MachineFunction &Fn);
166
167 /// AllocateBasicBlock - Register allocate the specified basic block.
168 void AllocateBasicBlock(MachineBasicBlock &MBB);
169
Chris Lattner82bee0f2002-12-18 08:14:26 +0000170
Chris Lattner82bee0f2002-12-18 08:14:26 +0000171 /// areRegsEqual - This method returns true if the specified registers are
172 /// related to each other. To do this, it checks to see if they are equal
173 /// or if the first register is in the alias set of the second register.
174 ///
175 bool areRegsEqual(unsigned R1, unsigned R2) const {
176 if (R1 == R2) return true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000177 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000178 *AliasSet; ++AliasSet) {
179 if (*AliasSet == R1) return true;
180 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000181 return false;
182 }
183
Chris Lattner580f9be2002-12-28 20:40:43 +0000184 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000185 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000186 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000187
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000188 /// removePhysReg - This method marks the specified physical register as no
189 /// longer being in use.
190 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000191 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000192
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000193 void storeVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
194 unsigned VirtReg, unsigned PhysReg, bool isKill);
195
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000196 /// spillVirtReg - This method spills the value specified by PhysReg into
197 /// the virtual register slot specified by VirtReg. It then updates the RA
198 /// data structures to indicate the fact that PhysReg is now available.
199 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000200 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000201 unsigned VirtReg, unsigned PhysReg);
202
Chris Lattnerc21be922002-12-16 17:44:42 +0000203 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000204 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
205 /// true, then the request is ignored if the physical register does not
206 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000207 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000208 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000209 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000210
Chris Lattner91a452b2003-01-13 00:25:40 +0000211 /// assignVirtToPhysReg - This method updates local state so that we know
212 /// that PhysReg is the proper container for VirtReg now. The physical
213 /// register must not be used for anything else when this is called.
214 ///
215 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
216
Chris Lattnerae640432002-12-17 02:50:10 +0000217 /// isPhysRegAvailable - Return true if the specified physical register is
218 /// free and available for use. This also includes checking to see if
219 /// aliased registers are all free...
220 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000221 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000222
223 /// getFreeReg - Look to see if there is a free register available in the
224 /// specified register class. If not, return 0.
225 ///
226 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000227
Chris Lattner91a452b2003-01-13 00:25:40 +0000228 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000229 /// register. If all compatible physical registers are used, this method
230 /// spills the last used virtual register to the stack, and uses that
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000231 /// register. If NoFree is true, that means the caller knows there isn't
232 /// a free register, do not call getFreeReg().
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000233 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000234 unsigned VirtReg, bool NoFree = false);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000235
Bob Wilsone0f745b2009-05-07 21:19:45 +0000236 /// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000237 /// register use to refer to a physical register. This method may do this
238 /// in one of several ways: if the register is available in a physical
239 /// register already, it uses that physical register. If the value is not
240 /// in a physical register, and if there are physical registers available,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000241 /// it loads it into a register: PhysReg if that is an available physical
242 /// register, otherwise any physical register of the right class.
243 /// If register pressure is high, and it is possible, it tries to fold the
244 /// load of the virtual register into the instruction itself. It avoids
245 /// doing this if register pressure is low to improve the chance that
246 /// subsequent instructions can use the reloaded value. This method
247 /// returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000248 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000249 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000250 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
251 unsigned PhysReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252
Owen Anderson9094db12008-07-09 20:14:53 +0000253 /// ComputeLocalLiveness - Computes liveness of registers within a basic
254 /// block, setting the killed/dead flags as appropriate.
255 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000256
257 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
258 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000259 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000260 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000261}
262
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000263/// getStackSpaceFor - This allocates space for the specified virtual register
264/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000265int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000266 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000267 int SS = StackSlotForVirtReg[VirtReg];
268 if (SS != -1)
269 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000270
Chris Lattner580f9be2002-12-28 20:40:43 +0000271 // Allocate a new stack object for this spill location...
David Greene3f2bf852009-11-12 20:49:22 +0000272 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
273 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000274
Chris Lattner4dd81632010-03-31 05:15:22 +0000275 // Assign the slot.
Evan Chengbdb10fe2008-07-10 18:23:23 +0000276 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000277 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000278}
279
Chris Lattnerae640432002-12-17 02:50:10 +0000280
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000281/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000282/// longer being in use.
283///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000284void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000285 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000286
287 std::vector<unsigned>::iterator It =
288 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
289 if (It != PhysRegsUseOrder.end())
290 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000291}
292
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000293/// storeVirtReg - Store a virtual register to its assigned stack slot.
294void RALocal::storeVirtReg(MachineBasicBlock &MBB,
295 MachineBasicBlock::iterator I,
296 unsigned VirtReg, unsigned PhysReg,
297 bool isKill) {
298 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
299 int FrameIndex = getStackSpaceFor(VirtReg, RC);
300 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Cheng746ad692010-05-06 19:06:44 +0000301 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC, TRI);
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000302 ++NumStores; // Update statistics
303}
Chris Lattner91a452b2003-01-13 00:25:40 +0000304
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000305/// spillVirtReg - This method spills the value specified by PhysReg into the
306/// virtual register slot specified by VirtReg. It then updates the RA data
307/// structures to indicate the fact that PhysReg is now available.
308///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000309void RALocal::spillVirtReg(MachineBasicBlock &MBB,
310 MachineBasicBlock::iterator I,
311 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000312 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000313 " Must not have appropriate kill for the register or use exists beyond"
314 " the intended one.");
David Greene44248172010-01-05 01:26:05 +0000315 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000316 << " containing %reg" << VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000317
Evan Cheng839b7592008-01-17 02:08:17 +0000318 if (!isVirtRegModified(VirtReg)) {
David Greene44248172010-01-05 01:26:05 +0000319 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Cheng839b7592008-01-17 02:08:17 +0000320 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
321 if (LastUse.first)
322 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000323 } else {
324 // Otherwise, there is a virtual register corresponding to this physical
325 // register. We only need to spill it into its stack slot if it has been
326 // modified.
Evan Cheng2fc628d2008-02-06 19:16:53 +0000327 // If the instruction reads the register that's spilled, (e.g. this can
328 // happen if it is a move to a physical register), then the spill
329 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000330 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000331 storeVirtReg(MBB, I, VirtReg, PhysReg, isKill);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000332 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000333
334 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000335
David Greene44248172010-01-05 01:26:05 +0000336 DEBUG(dbgs() << '\n');
Chris Lattner82bee0f2002-12-18 08:14:26 +0000337 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000338}
339
Chris Lattnerae640432002-12-17 02:50:10 +0000340
Chris Lattner91a452b2003-01-13 00:25:40 +0000341/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000342/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
343/// then the request is ignored if the physical register does not contain a
344/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000345///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000346void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
347 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000348 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000349 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000350 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
351 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Chris Lattner4dd81632010-03-31 05:15:22 +0000352 return;
353 }
354
355 // If the selected register aliases any other registers, we must make
356 // sure that one of the aliases isn't alive.
357 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
358 *AliasSet; ++AliasSet) {
359 if (PhysRegsUsed[*AliasSet] == -1 || // Spill aliased register.
360 PhysRegsUsed[*AliasSet] == -2) // If allocatable.
361 continue;
362
363 if (PhysRegsUsed[*AliasSet])
364 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000365 }
366}
367
368
369/// assignVirtToPhysReg - This method updates local state so that we know
370/// that PhysReg is the proper container for VirtReg now. The physical
371/// register must not be used for anything else when this is called.
372///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000373void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000374 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000375 // Update information to note the fact that this register was just used, and
376 // it holds VirtReg.
377 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000378 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000379 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000380}
381
382
Chris Lattnerae640432002-12-17 02:50:10 +0000383/// isPhysRegAvailable - Return true if the specified physical register is free
384/// and available for use. This also includes checking to see if aliased
385/// registers are all free...
386///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000387bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000388 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000389
390 // If the selected register aliases any other allocated registers, it is
391 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000392 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000393 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000394 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000395 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000396 return true;
397}
398
399
Chris Lattner91a452b2003-01-13 00:25:40 +0000400/// getFreeReg - Look to see if there is a free register available in the
401/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000402///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000403unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000404 // Get iterators defining the range of registers that are valid to allocate in
405 // this class, which also specifies the preferred allocation order.
406 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
407 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000408
Chris Lattner91a452b2003-01-13 00:25:40 +0000409 for (; RI != RE; ++RI)
410 if (isPhysRegAvailable(*RI)) { // Is reg unused?
411 assert(*RI != 0 && "Cannot use register!");
412 return *RI; // Found an unused register!
413 }
414 return 0;
415}
416
417
Chris Lattner91a452b2003-01-13 00:25:40 +0000418/// getReg - Find a physical register to hold the specified virtual
419/// register. If all compatible physical registers are used, this method spills
420/// the last used virtual register to the stack, and uses that register.
421///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000422unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000423 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000424 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000425
426 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000427 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000428
Chris Lattner4dd81632010-03-31 05:15:22 +0000429 if (PhysReg != 0) {
430 // Assign the register.
431 assignVirtToPhysReg(VirtReg, PhysReg);
432 return PhysReg;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000433 }
434
435 // If we didn't find an unused register, scavenge one now!
436 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000437
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000438 // Loop over all of the preallocated registers from the least recently used
439 // to the most recently used. When we find one that is capable of holding
440 // our register, use it.
441 for (unsigned i = 0; PhysReg == 0; ++i) {
442 assert(i != PhysRegsUseOrder.size() &&
443 "Couldn't find a register of the appropriate class!");
444
445 unsigned R = PhysRegsUseOrder[i];
446
447 // We can only use this register if it holds a virtual register (ie, it
448 // can be spilled). Do not use it if it is an explicitly allocated
449 // physical register!
450 assert(PhysRegsUsed[R] != -1 &&
451 "PhysReg in PhysRegsUseOrder, but is not allocated?");
452 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
453 // If the current register is compatible, use it.
454 if (RC->contains(R)) {
455 PhysReg = R;
456 break;
457 }
458
459 // If one of the registers aliased to the current register is
460 // compatible, use it.
461 for (const unsigned *AliasIt = TRI->getAliasSet(R);
462 *AliasIt; ++AliasIt) {
463 if (!RC->contains(*AliasIt)) continue;
464
465 // If this is pinned down for some reason, don't use it. For
466 // example, if CL is pinned, and we run across CH, don't use
467 // CH as justification for using scavenging ECX (which will
468 // fail).
469 if (PhysRegsUsed[*AliasIt] == 0) continue;
470
471 // Make sure the register is allocatable. Don't allocate SIL on
472 // x86-32.
473 if (PhysRegsUsed[*AliasIt] == -2) continue;
474
475 PhysReg = *AliasIt; // Take an aliased register
476 break;
477 }
478 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000479 }
480
Chris Lattner4dd81632010-03-31 05:15:22 +0000481 assert(PhysReg && "Physical register not assigned!?!?");
482
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000483 // At this point PhysRegsUseOrder[i] is the least recently used register of
484 // compatible register class. Spill it to memory and reap its remains.
Chris Lattner4dd81632010-03-31 05:15:22 +0000485 spillPhysReg(MBB, I, PhysReg);
486
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000487 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000488 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000489 return PhysReg;
490}
491
Chris Lattnerae640432002-12-17 02:50:10 +0000492
Bob Wilson8d24f412009-05-07 21:20:42 +0000493/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000494/// register use to refer to a physical register. This method may do this in
495/// one of several ways: if the register is available in a physical register
496/// already, it uses that physical register. If the value is not in a physical
497/// register, and if there are physical registers available, it loads it into a
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000498/// register: PhysReg if that is an available physical register, otherwise any
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000499/// register. If register pressure is high, and it is possible, it tries to
500/// fold the load of the virtual register into the instruction itself. It
501/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000502/// subsequent instructions can use the reloaded value. This method returns
503/// the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000504///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000505MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000506 unsigned OpNum,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000507 SmallSet<unsigned, 4> &ReloadedRegs,
508 unsigned PhysReg) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000509 unsigned VirtReg = MI->getOperand(OpNum).getReg();
Evan Cheng736f89b2010-05-12 01:29:36 +0000510 unsigned SubIdx = MI->getOperand(OpNum).getSubReg();
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000511
512 // If the virtual register is already available, just update the instruction
513 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000514 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Evan Cheng736f89b2010-05-12 01:29:36 +0000515 if (SubIdx) {
516 PR = TRI->getSubReg(PR, SubIdx);
517 MI->getOperand(OpNum).setSubReg(0);
518 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000519 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesenf463d952010-02-16 01:27:47 +0000520 if (!MI->isDebugValue()) {
521 // Do not do these for DBG_VALUE as they can affect codegen.
522 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesen3da6e092010-02-15 01:45:47 +0000523 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesenf463d952010-02-16 01:27:47 +0000524 }
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000525 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000526 }
527
Chris Lattner1e3812c2004-02-17 04:08:37 +0000528 // Otherwise, we need to fold it into the current instruction, or reload it.
529 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000530 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000531 // If we already have a PhysReg (this happens when the instruction is a
532 // reg-to-reg copy with a PhysReg destination) use that.
533 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
534 !isPhysRegAvailable(PhysReg))
535 PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000536 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000537
Chris Lattner11390e72004-02-17 08:09:40 +0000538 if (PhysReg) { // Register is available, allocate it!
539 assignVirtToPhysReg(VirtReg, PhysReg);
540 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000541 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000542 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000543 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000544 }
545
Chris Lattner91a452b2003-01-13 00:25:40 +0000546 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
547
David Greene44248172010-01-05 01:26:05 +0000548 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000549 << TRI->getName(PhysReg) << "\n");
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000550
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000551 // Add move instruction(s)
Evan Cheng746ad692010-05-06 19:06:44 +0000552 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC, TRI);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000553 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000554
Chris Lattner84bc5422007-12-31 04:13:23 +0000555 MF->getRegInfo().setPhysRegUsed(PhysReg);
Evan Cheng736f89b2010-05-12 01:29:36 +0000556 // Assign the input register.
557 if (SubIdx) {
558 MI->getOperand(OpNum).setSubReg(0);
559 MI->getOperand(OpNum).setReg(TRI->getSubReg(PhysReg, SubIdx));
560 } else
561 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000562 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000563
564 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000565 std::string msg;
566 raw_string_ostream Msg(msg);
567 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000568 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000569 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000570 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000571 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000572 }
Chris Lattner75361b62010-04-07 22:58:41 +0000573 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000574 }
575 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
576 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000577 if (ReloadedRegs.insert(*SubRegs)) continue;
578
579 std::string msg;
580 raw_string_ostream Msg(msg);
581 Msg << "Ran out of registers during register allocation!";
582 if (MI->isInlineAsm()) {
583 Msg << "\nPlease check your inline asm statement for invalid "
584 << "constraints:\n";
585 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000586 }
Chris Lattner75361b62010-04-07 22:58:41 +0000587 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000588 }
589
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000590 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000591}
592
Evan Cheng7ac19af2007-06-26 21:05:13 +0000593/// isReadModWriteImplicitKill - True if this is an implicit kill for a
594/// read/mod/write register, i.e. update partial register.
595static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
596 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000597 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000598 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000599 MO.isDef() && !MO.isDead())
600 return true;
601 }
602 return false;
603}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000604
Evan Cheng7ac19af2007-06-26 21:05:13 +0000605/// isReadModWriteImplicitDef - True if this is an implicit def for a
606/// read/mod/write register, i.e. update partial register.
607static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
608 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000609 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000610 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000611 !MO.isDef() && MO.isKill())
612 return true;
613 }
614 return false;
615}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000616
Owen Anderson491fccc2008-07-08 22:24:50 +0000617// precedes - Helper function to determine with MachineInstr A
618// precedes MachineInstr B within the same MBB.
619static bool precedes(MachineBasicBlock::iterator A,
620 MachineBasicBlock::iterator B) {
621 if (A == B)
622 return false;
623
624 MachineBasicBlock::iterator I = A->getParent()->begin();
625 while (I != A->getParent()->end()) {
626 if (I == A)
627 return true;
628 else if (I == B)
629 return false;
630
631 ++I;
632 }
633
634 return false;
635}
636
Owen Anderson9094db12008-07-09 20:14:53 +0000637/// ComputeLocalLiveness - Computes liveness of registers within a basic
638/// block, setting the killed/dead flags as appropriate.
639void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000640 // Keep track of the most recently seen previous use or def of each reg,
641 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000642 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000643 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
644 I != E; ++I) {
Dale Johannesen3da6e092010-02-15 01:45:47 +0000645 if (I->isDebugValue())
646 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +0000647
Owen Anderson491fccc2008-07-08 22:24:50 +0000648 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000649 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000650 // Uses don't trigger any flags, but we need to save
651 // them for later. Also, we have to process these
652 // _before_ processing the defs, since an instr
653 // uses regs before it defs them.
Chris Lattner4dd81632010-03-31 05:15:22 +0000654 if (!MO.isReg() || !MO.getReg() || !MO.isUse())
655 continue;
Jakob Stoklund Olesena50fba92010-05-03 23:49:20 +0000656
657 // Ignore helpful kill flags from earlier passes.
658 MO.setIsKill(false);
659
Chris Lattner4dd81632010-03-31 05:15:22 +0000660 LastUseDef[MO.getReg()] = std::make_pair(I, i);
661
662 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
663
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000664 const unsigned *Aliases = TRI->getAliasSet(MO.getReg());
665 if (Aliases == 0)
666 continue;
667
668 while (*Aliases) {
669 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
670 alias = LastUseDef.find(*Aliases);
671
672 if (alias != LastUseDef.end() && alias->second.first != I)
673 LastUseDef[*Aliases] = std::make_pair(I, i);
674
675 ++Aliases;
Owen Anderson04764de2008-10-08 04:30:51 +0000676 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000677 }
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000678
Owen Anderson491fccc2008-07-08 22:24:50 +0000679 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000680 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000681 // Defs others than 2-addr redefs _do_ trigger flag changes:
682 // - A def followed by a def is dead
683 // - A use followed by a def is a kill
Chris Lattner4dd81632010-03-31 05:15:22 +0000684 if (!MO.isReg() || !MO.getReg() || !MO.isDef()) continue;
Evan Cheng736f89b2010-05-12 01:29:36 +0000685
686 unsigned SubIdx = MO.getSubReg();
Chris Lattner4dd81632010-03-31 05:15:22 +0000687 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
688 last = LastUseDef.find(MO.getReg());
689 if (last != LastUseDef.end()) {
690 // Check if this is a two address instruction. If so, then
691 // the def does not kill the use.
Evan Cheng736f89b2010-05-12 01:29:36 +0000692 if (last->second.first == I && I->isRegTiedToUseOperand(i))
Chris Lattner4dd81632010-03-31 05:15:22 +0000693 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000694
Chris Lattner4dd81632010-03-31 05:15:22 +0000695 MachineOperand &lastUD =
696 last->second.first->getOperand(last->second.second);
Evan Cheng736f89b2010-05-12 01:29:36 +0000697 if (SubIdx && lastUD.getSubReg() != SubIdx)
698 // Partial re-def, the last def is not dead.
699 // %reg1024:5<def> =
700 // %reg1024:6<def> =
701 // or
702 // %reg1024:5<def> = op %reg1024, 5
703 continue;
704
Chris Lattner4dd81632010-03-31 05:15:22 +0000705 if (lastUD.isDef())
706 lastUD.setIsDead(true);
707 else
708 lastUD.setIsKill(true);
Owen Anderson491fccc2008-07-08 22:24:50 +0000709 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000710
711 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000712 }
713 }
714
715 // Live-out (of the function) registers contain return values of the function,
716 // so we need to make sure they are alive at return time.
Bill Wendlingb0d27662010-03-16 02:01:51 +0000717 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
718 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
719
720 if (BBEndsInReturn)
Owen Anderson491fccc2008-07-08 22:24:50 +0000721 for (MachineRegisterInfo::liveout_iterator
722 I = MF->getRegInfo().liveout_begin(),
723 E = MF->getRegInfo().liveout_end(); I != E; ++I)
724 if (!Ret->readsRegister(*I)) {
725 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
726 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
727 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000728
729 // Finally, loop over the final use/def of each reg
730 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000731 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000732 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000733 MachineInstr *MI = I->second.first;
Owen Anderson491fccc2008-07-08 22:24:50 +0000734 unsigned idx = I->second.second;
Chris Lattner4dd81632010-03-31 05:15:22 +0000735 MachineOperand &MO = MI->getOperand(idx);
Owen Anderson491fccc2008-07-08 22:24:50 +0000736
737 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
738
739 // A crude approximation of "live-out" calculation
740 bool usedOutsideBlock = isPhysReg ? false :
741 UsedInMultipleBlocks.test(MO.getReg() -
742 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000743
744 // If the machine BB ends in a return instruction, then the value isn't used
745 // outside of the BB.
746 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000747 // DBG_VALUE complicates this: if the only refs of a register outside
748 // this block are DBG_VALUE, we can't keep the reg live just for that,
749 // as it will cause the reg to be spilled at the end of this block when
750 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
751 // happens.
752 bool UsedByDebugValueOnly = false;
Evan Cheng736f89b2010-05-12 01:29:36 +0000753 for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()),
754 UE = MRI->reg_end(); UI != UE; ++UI) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000755 // Two cases:
756 // - used in another block
757 // - used in the same block before it is defined (loop)
Chris Lattner4dd81632010-03-31 05:15:22 +0000758 if (UI->getParent() == &MBB &&
759 !(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI)))
760 continue;
761
762 if (UI->isDebugValue()) {
763 UsedByDebugValueOnly = true;
764 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000765 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000766
767 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
768 UsedInMultipleBlocks.set(MO.getReg() -
769 TargetRegisterInfo::FirstVirtualRegister);
770 usedOutsideBlock = true;
771 UsedByDebugValueOnly = false;
772 break;
Bill Wendling8fe347a2010-03-16 01:05:35 +0000773 }
774
Dale Johannesenf463d952010-02-16 01:27:47 +0000775 if (UsedByDebugValueOnly)
Evan Cheng736f89b2010-05-12 01:29:36 +0000776 for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()),
777 UE = MRI->reg_end(); UI != UE; ++UI)
Dale Johannesenf463d952010-02-16 01:27:47 +0000778 if (UI->isDebugValue() &&
779 (UI->getParent() != &MBB ||
780 (MO.isDef() && precedes(&*UI, MI))))
781 UI.getOperand().setReg(0U);
782 }
783
Bill Wendling8fe347a2010-03-16 01:05:35 +0000784 // Physical registers and those that are not live-out of the block are
785 // killed/dead at their last use/def within this block.
Dan Gohman15843902010-03-18 18:07:13 +0000786 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000787 if (MO.isUse()) {
788 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000789 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000790 MO.setIsKill(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000791 } else {
Owen Anderson491fccc2008-07-08 22:24:50 +0000792 MO.setIsDead(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000793 }
Dan Gohman15843902010-03-18 18:07:13 +0000794 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000795 }
Owen Anderson9094db12008-07-09 20:14:53 +0000796}
797
798void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
799 // loop over each instruction
800 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000801
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000802 DEBUG({
803 const BasicBlock *LBB = MBB.getBasicBlock();
804 if (LBB)
David Greene44248172010-01-05 01:26:05 +0000805 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000806 });
Owen Anderson9094db12008-07-09 20:14:53 +0000807
Evan Chengd5a48022009-01-29 18:37:30 +0000808 // Add live-in registers as active.
809 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000810 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000811 unsigned Reg = *I;
812 MF->getRegInfo().setPhysRegUsed(Reg);
813 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000814 AddToPhysRegsUseOrder(Reg);
Evan Chengd5a48022009-01-29 18:37:30 +0000815 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
816 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000817 if (PhysRegsUsed[*SubRegs] == -2) continue;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000818
819 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +0000820 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
821 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Evan Chengd5a48022009-01-29 18:37:30 +0000822 }
Owen Anderson9094db12008-07-09 20:14:53 +0000823 }
824
825 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000826
Chris Lattner44500e32006-06-15 22:21:53 +0000827 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000828 while (MII != MBB.end()) {
829 MachineInstr *MI = MII++;
Chris Lattner749c6f62008-01-07 07:27:27 +0000830 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000831 DEBUG({
David Greene44248172010-01-05 01:26:05 +0000832 dbgs() << "\nStarting RegAlloc of: " << *MI;
833 dbgs() << " Regs have values: ";
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000834 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000835 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
836 if (PhysRegsUsed[i] && isVirtRegModified(PhysRegsUsed[i]))
837 dbgs() << "*";
David Greene44248172010-01-05 01:26:05 +0000838 dbgs() << "[" << TRI->getName(i)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000839 << ",%reg" << PhysRegsUsed[i] << "] ";
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000840 }
David Greene44248172010-01-05 01:26:05 +0000841 dbgs() << '\n';
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000842 });
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000843
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000844 // Determine whether this is a copy instruction. The cases where the
845 // source or destination are phys regs are handled specially.
846 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000847 unsigned SrcCopyPhysReg = 0U;
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000848 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
Evan Chengde7dea22010-05-12 23:59:42 +0000849 SrcCopySubReg, DstCopySubReg) &&
850 SrcCopySubReg == DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000851 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
852 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000853
Chris Lattnerae640432002-12-17 02:50:10 +0000854 // Loop over the implicit uses, making sure that they are at the head of the
855 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000856 if (TID.ImplicitUses) {
857 for (const unsigned *ImplicitUses = TID.ImplicitUses;
858 *ImplicitUses; ++ImplicitUses)
859 MarkPhysRegRecentlyUsed(*ImplicitUses);
860 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000861
Evan Chengddee8422006-11-15 20:55:15 +0000862 SmallVector<unsigned, 8> Kills;
863 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000864 MachineOperand &MO = MI->getOperand(i);
Nick Lewycky403d3122010-05-07 01:45:38 +0000865 if (!MO.isReg() || !MO.isKill()) continue;
866
Chris Lattner4dd81632010-03-31 05:15:22 +0000867 if (!MO.isImplicit())
868 Kills.push_back(MO.getReg());
869 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
870 // These are extra physical register kills when a sub-register
871 // is defined (def of a sub-register is a read/mod/write of the
872 // larger registers). Ignore.
873 Kills.push_back(MO.getReg());
Evan Chengddee8422006-11-15 20:55:15 +0000874 }
875
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000876 // If any physical regs are earlyclobber, spill any value they might
877 // have in them, then mark them unallocatable.
878 // If any virtual regs are earlyclobber, allocate them now (before
879 // freeing inputs that are killed).
Chris Lattner518bb532010-02-09 19:54:29 +0000880 if (MI->isInlineAsm()) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000881 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
882 MachineOperand &MO = MI->getOperand(i);
883 if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber() ||
884 !MO.getReg())
885 continue;
886
887 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
888 unsigned DestVirtReg = MO.getReg();
889 unsigned DestPhysReg;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000890
Chris Lattner4dd81632010-03-31 05:15:22 +0000891 // If DestVirtReg already has a value, use it.
892 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
893 DestPhysReg = getReg(MBB, MI, DestVirtReg);
894 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
895 markVirtRegModified(DestVirtReg);
896 getVirtRegLastUse(DestVirtReg) =
897 std::make_pair((MachineInstr*)0, 0);
898 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
899 << " to %reg" << DestVirtReg << "\n");
Evan Cheng736f89b2010-05-12 01:29:36 +0000900 if (unsigned DestSubIdx = MO.getSubReg()) {
901 MO.setSubReg(0);
902 DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx);
903 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000904 MO.setReg(DestPhysReg); // Assign the earlyclobber register
905 } else {
906 unsigned Reg = MO.getReg();
907 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
908 // These are extra physical register defs when a sub-register
909 // is defined (def of a sub-register is a read/mod/write of the
910 // larger registers). Ignore.
911 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000912
Chris Lattner4dd81632010-03-31 05:15:22 +0000913 MF->getRegInfo().setPhysRegUsed(Reg);
914 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
915 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000916 AddToPhysRegsUseOrder(Reg);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000917
Chris Lattner4dd81632010-03-31 05:15:22 +0000918 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
919 *SubRegs; ++SubRegs) {
920 if (PhysRegsUsed[*SubRegs] == -2) continue;
921 MF->getRegInfo().setPhysRegUsed(*SubRegs);
922 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000923 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000924 }
925 }
926 }
927 }
928
Dale Johannesen10fedd22010-02-10 00:11:11 +0000929 // If a DBG_VALUE says something is located in a spilled register,
930 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesenca134612010-01-30 00:57:47 +0000931 // from being reloaded here. Doing that would change the generated
932 // code, unless another use immediately follows this instruction.
Chris Lattner518bb532010-02-09 19:54:29 +0000933 if (MI->isDebugValue() &&
Dale Johannesenca134612010-01-30 00:57:47 +0000934 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
935 unsigned VirtReg = MI->getOperand(0).getReg();
936 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
937 !getVirt2PhysRegMapSlot(VirtReg))
938 MI->getOperand(0).setReg(0U);
939 }
940
Brian Gaeke53b99a02003-08-15 21:19:25 +0000941 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000942 // incoming values if we are out of registers. Note that we completely
943 // ignore physical register uses here. We assume that if an explicit
944 // physical register is referenced by the instruction, that it is guaranteed
945 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000946 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000947 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000948 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000949 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000950 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000951 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000952 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000953 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
954 isCopy ? DstCopyReg : 0);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000955 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000956
Evan Chengddee8422006-11-15 20:55:15 +0000957 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000958 // value, freeing the register being used, so it doesn't need to be
959 // spilled to memory.
960 //
Evan Chengddee8422006-11-15 20:55:15 +0000961 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
962 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000963 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000964 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000965 // If the virtual register was never materialized into a register, it
966 // might not be in the map, but it won't hurt to zero it out anyway.
967 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
968 PhysReg = PhysRegSlot;
969 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000970 } else if (PhysRegsUsed[PhysReg] == -2) {
971 // Unallocatable register dead, ignore.
972 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000973 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000974 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000975 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000976 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000977
Chris Lattner4dd81632010-03-31 05:15:22 +0000978 if (!PhysReg) continue;
979
980 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
981 << "[%reg" << VirtReg <<"], removing it from live set\n");
982 removePhysReg(PhysReg);
983 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
984 *SubRegs; ++SubRegs) {
985 if (PhysRegsUsed[*SubRegs] != -2) {
986 DEBUG(dbgs() << " Last use of "
987 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
988 <<"], removing it from live set\n");
989 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +0000990 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000991 }
992 }
993
994 // Loop over all of the operands of the instruction, spilling registers that
995 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000996 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000997 MachineOperand &MO = MI->getOperand(i);
998 if (!MO.isReg() || !MO.isDef() || MO.isImplicit() || !MO.getReg() ||
999 MO.isEarlyClobber() ||
1000 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1001 continue;
1002
1003 unsigned Reg = MO.getReg();
1004 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
1005 // These are extra physical register defs when a sub-register
1006 // is defined (def of a sub-register is a read/mod/write of the
1007 // larger registers). Ignore.
1008 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +00001009
Chris Lattner4dd81632010-03-31 05:15:22 +00001010 MF->getRegInfo().setPhysRegUsed(Reg);
1011 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
1012 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001013 AddToPhysRegsUseOrder(Reg);
Evan Cheng7ac19af2007-06-26 21:05:13 +00001014
Chris Lattner4dd81632010-03-31 05:15:22 +00001015 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
1016 *SubRegs; ++SubRegs) {
1017 if (PhysRegsUsed[*SubRegs] == -2) continue;
1018
1019 MF->getRegInfo().setPhysRegUsed(*SubRegs);
1020 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001021 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner91a452b2003-01-13 00:25:40 +00001022 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001023 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001024
1025 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +00001026 if (TID.ImplicitDefs) {
1027 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
1028 *ImplicitDefs; ++ImplicitDefs) {
1029 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +00001030 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +00001031 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001032 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +00001033 PhysRegsUsed[Reg] = 0; // It is free and reserved now
1034 }
Chris Lattner84bc5422007-12-31 04:13:23 +00001035 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00001036 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
1037 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001038 if (PhysRegsUsed[*SubRegs] == -2) continue;
1039
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001040 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +00001041 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
1042 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Jim Laskeycd4317e2006-07-21 21:15:20 +00001043 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +00001044 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +00001045 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001046
Evan Chengddee8422006-11-15 20:55:15 +00001047 SmallVector<unsigned, 8> DeadDefs;
1048 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001049 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001050 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +00001051 DeadDefs.push_back(MO.getReg());
1052 }
1053
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001054 // Okay, we have allocated all of the source operands and spilled any values
1055 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +00001056 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +00001057 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +00001058 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001059 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001060 MachineOperand &MO = MI->getOperand(i);
1061 if (!MO.isReg() || !MO.isDef() || !MO.getReg() ||
1062 MO.isEarlyClobber() ||
1063 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1064 continue;
1065
1066 unsigned DestVirtReg = MO.getReg();
1067 unsigned DestPhysReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001068
Chris Lattner4dd81632010-03-31 05:15:22 +00001069 // If DestVirtReg already has a value, use it.
1070 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
1071 // If this is a copy try to reuse the input as the output;
1072 // that will make the copy go away.
1073 // If this is a copy, the source reg is a phys reg, and
1074 // that reg is available, use that phys reg for DestPhysReg.
1075 // If this is a copy, the source reg is a virtual reg, and
1076 // the phys reg that was assigned to that virtual reg is now
1077 // available, use that phys reg for DestPhysReg. (If it's now
1078 // available that means this was the last use of the source.)
1079 if (isCopy &&
1080 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
1081 isPhysRegAvailable(SrcCopyReg)) {
1082 DestPhysReg = SrcCopyReg;
1083 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1084 } else if (isCopy &&
1085 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
1086 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
1087 MF->getRegInfo().getRegClass(DestVirtReg)->
1088 contains(SrcCopyPhysReg)) {
1089 DestPhysReg = SrcCopyPhysReg;
1090 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1091 } else
1092 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001093 }
Chris Lattner4dd81632010-03-31 05:15:22 +00001094 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
1095 markVirtRegModified(DestVirtReg);
1096 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
1097 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
1098 << " to %reg" << DestVirtReg << "\n");
Evan Cheng736f89b2010-05-12 01:29:36 +00001099
1100 if (unsigned DestSubIdx = MO.getSubReg()) {
1101 MO.setSubReg(0);
1102 DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx);
1103 }
Chris Lattner4dd81632010-03-31 05:15:22 +00001104 MO.setReg(DestPhysReg); // Assign the output register
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001105 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001106
Chris Lattner56ddada2004-02-17 17:49:10 +00001107 // If this instruction defines any registers that are immediately dead,
1108 // kill them now.
1109 //
Evan Chengddee8422006-11-15 20:55:15 +00001110 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
1111 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +00001112 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001113 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +00001114 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1115 PhysReg = PhysRegSlot;
1116 assert(PhysReg != 0);
1117 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +00001118 } else if (PhysRegsUsed[PhysReg] == -2) {
1119 // Unallocatable register dead, ignore.
1120 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +00001121 } else if (!PhysReg)
1122 continue;
1123
1124 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
1125 << " [%reg" << VirtReg
1126 << "] is never used, removing it from live set\n");
1127 removePhysReg(PhysReg);
1128 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
1129 *AliasSet; ++AliasSet) {
1130 if (PhysRegsUsed[*AliasSet] != -2) {
1131 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
1132 << " [%reg" << *AliasSet
1133 << "] is never used, removing it from live set\n");
1134 removePhysReg(*AliasSet);
Evan Chengddee8422006-11-15 20:55:15 +00001135 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001136 }
1137 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001138
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +00001139 // If this instruction is a call, make sure there are no dirty registers. The
1140 // call might throw an exception, and the landing pad expects to find all
1141 // registers in stack slots.
1142 if (TID.isCall())
1143 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
1144 if (PhysRegsUsed[i] <= 0) continue;
1145 unsigned VirtReg = PhysRegsUsed[i];
1146 if (!isVirtRegModified(VirtReg)) continue;
1147 DEBUG(dbgs() << " Storing dirty %reg" << VirtReg);
1148 storeVirtReg(MBB, MI, VirtReg, i, false);
1149 markVirtRegModified(VirtReg, false);
1150 DEBUG(dbgs() << " because the call might throw\n");
1151 }
1152
Bob Wilson9d928c22009-05-07 23:47:03 +00001153 // Finally, if this is a noop copy instruction, zap it. (Except that if
1154 // the copy is dead, it must be kept to avoid messing up liveness info for
1155 // the register scavenger. See pr4100.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001156 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1157 SrcCopySubReg, DstCopySubReg) &&
Evan Chengde7dea22010-05-12 23:59:42 +00001158 SrcCopyReg == DstCopyReg && SrcCopySubReg == DstCopySubReg &&
1159 DeadDefs.empty())
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001160 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001161 }
1162
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001163 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001164
1165 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001166 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001167 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001168 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001169 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001170 else
1171 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001172 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001173
Chris Lattner9a5ef202005-11-09 05:28:45 +00001174#if 0
1175 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001176 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001177 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001178 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001179 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001180 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001181 AllOk = false;
1182 }
1183 assert(AllOk && "Virtual registers still in phys regs?");
1184#endif
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001185
1186 // Clear any physical register which appear live at the end of the basic
1187 // block, but which do not hold any virtual registers. e.g., the stack
1188 // pointer.
1189 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001190}
1191
1192/// runOnMachineFunction - Register allocate the whole function
1193///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001194bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene44248172010-01-05 01:26:05 +00001195 DEBUG(dbgs() << "Machine Function\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001196 MF = &Fn;
Evan Cheng736f89b2010-05-12 01:29:36 +00001197 MRI = &Fn.getRegInfo();
Chris Lattner580f9be2002-12-28 20:40:43 +00001198 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001199 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001200 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001201
Dan Gohman6f0d0242008-02-10 18:45:23 +00001202 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001203
Chris Lattner45d57882006-09-08 19:03:30 +00001204 // At various places we want to efficiently check to see whether a register
1205 // is allocatable. To handle this, we mark all unallocatable registers as
1206 // being pinned down, permanently.
1207 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001208 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001209 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1210 if (!Allocable[i])
1211 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1212 }
Chris Lattner64667b62004-02-09 01:26:13 +00001213
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001214 // initialize the virtual->physical register map to have a 'null'
1215 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001216 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001217 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001218 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001219 Virt2LastUseMap.grow(LastVirtReg);
Chris Lattner4dd81632010-03-31 05:15:22 +00001220 VirtRegModified.resize(LastVirtReg+1 -
1221 TargetRegisterInfo::FirstVirtualRegister);
1222 UsedInMultipleBlocks.resize(LastVirtReg+1 -
1223 TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001224
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001225 // Loop over all of the basic blocks, eliminating virtual register references
1226 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1227 MBB != MBBe; ++MBB)
1228 AllocateBasicBlock(*MBB);
1229
Chris Lattner580f9be2002-12-28 20:40:43 +00001230 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001231 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001232 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001233 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001234 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001235 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001236 return true;
1237}
1238
Chris Lattneref09c632004-01-31 21:27:19 +00001239FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001240 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001241}