blob: 321ae12def5710554a64c8b1efcd55a600f78c69 [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");
Jakob Stoklund Olesenab2d0082010-05-14 22:40:40 +000040STATISTIC(NumCopies, "Number of copies coalesced");
Jim Laskey13ec7022006-08-01 14:21:23 +000041
Dan Gohman844731a2008-05-13 00:00:25 +000042static RegisterRegAlloc
Dan Gohmanb8cab922008-10-14 20:25:08 +000043 localRegAlloc("local", "local register allocator",
Dan Gohman844731a2008-05-13 00:00:25 +000044 createLocalRegisterAllocator);
45
Chris Lattnercd3245a2006-12-19 22:41:21 +000046namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000047 class RALocal : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000048 public:
Devang Patel19974732007-05-03 01:11:54 +000049 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000050 RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
Devang Patel794fd752007-05-01 21:15:47 +000051 private:
Chris Lattner580f9be2002-12-28 20:40:43 +000052 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000053 MachineFunction *MF;
Evan Cheng736f89b2010-05-12 01:29:36 +000054 MachineRegisterInfo *MRI;
Dan Gohman6f0d0242008-02-10 18:45:23 +000055 const TargetRegisterInfo *TRI;
Owen Anderson6425f8b2008-01-07 01:35:56 +000056 const TargetInstrInfo *TII;
Chris Lattnerff863ba2002-12-25 05:05:46 +000057
Chris Lattnerb8822ad2003-08-04 23:36:39 +000058 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
59 // values are spilled.
Evan Chengbdb10fe2008-07-10 18:23:23 +000060 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000061
62 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000063 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000064 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000065
66 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000067 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000068 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000069
Chris Lattner64667b62004-02-09 01:26:13 +000070 // PhysRegsUsed - This array is effectively a map, containing entries for
71 // each physical register that currently has a value (ie, it is in
72 // Virt2PhysRegMap). The value mapped to is the virtual register
73 // corresponding to the physical register (the inverse of the
74 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000075 // because it is used by a future instruction, and to -2 if it is not
76 // allocatable. If the entry for a physical register is -1, then the
77 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000078 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000079 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000080
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +000081 // PhysRegsUseOrder - This contains a list of the physical registers that
82 // currently have a virtual register value in them. This list provides an
83 // ordering of registers, imposing a reallocation order. This list is only
84 // used if all registers are allocated and we have to spill one, in which
85 // case we spill the least recently used register. Entries at the front of
86 // the list are the least recently used registers, entries at the back are
87 // the most recently used.
Chris Lattnerb74e83c2002-12-16 16:15:28 +000088 //
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +000089 std::vector<unsigned> PhysRegsUseOrder;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000090
Evan Cheng839b7592008-01-17 02:08:17 +000091 // Virt2LastUseMap - This maps each virtual register to its last use
92 // (MachineInstr*, operand index pair).
93 IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
94 Virt2LastUseMap;
95
96 std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000097 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Evan Cheng839b7592008-01-17 02:08:17 +000098 return Virt2LastUseMap[Reg];
99 }
100
Chris Lattner91a452b2003-01-13 00:25:40 +0000101 // VirtRegModified - This bitset contains information about which virtual
102 // registers need to be spilled back to memory when their registers are
103 // scavenged. If a virtual register has simply been rematerialized, there
104 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000105 //
Evan Cheng644340a2008-01-17 00:35:26 +0000106 BitVector VirtRegModified;
Owen Anderson491fccc2008-07-08 22:24:50 +0000107
108 // UsedInMultipleBlocks - Tracks whether a particular register is used in
109 // more than one block.
110 BitVector UsedInMultipleBlocks;
Chris Lattner91a452b2003-01-13 00:25:40 +0000111
112 void markVirtRegModified(unsigned Reg, bool Val = true) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000113 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
114 Reg -= TargetRegisterInfo::FirstVirtualRegister;
Evan Cheng644340a2008-01-17 00:35:26 +0000115 if (Val)
116 VirtRegModified.set(Reg);
117 else
118 VirtRegModified.reset(Reg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000119 }
120
121 bool isVirtRegModified(unsigned Reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000122 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner4dd81632010-03-31 05:15:22 +0000123 assert(Reg - TargetRegisterInfo::FirstVirtualRegister <
124 VirtRegModified.size() && "Illegal virtual register!");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000125 return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
Chris Lattner91a452b2003-01-13 00:25:40 +0000126 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000127
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000128 void AddToPhysRegsUseOrder(unsigned Reg) {
129 std::vector<unsigned>::iterator It =
130 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
131 if (It != PhysRegsUseOrder.end())
132 PhysRegsUseOrder.erase(It);
133 PhysRegsUseOrder.push_back(Reg);
134 }
135
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000136 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000137 if (PhysRegsUseOrder.empty() ||
138 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
139
140 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) {
141 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
142 if (!areRegsEqual(Reg, RegMatch)) continue;
143
144 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
145 // Add it to the end of the list
146 PhysRegsUseOrder.push_back(RegMatch);
147 if (RegMatch == Reg)
148 return; // Found an exact match, exit early
149 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000150 }
151
152 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000153 virtual const char *getPassName() const {
154 return "Local Register Allocator";
155 }
156
Chris Lattner91a452b2003-01-13 00:25:40 +0000157 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000158 AU.setPreservesCFG();
Chris Lattner91a452b2003-01-13 00:25:40 +0000159 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000160 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000161 MachineFunctionPass::getAnalysisUsage(AU);
162 }
163
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000164 private:
165 /// runOnMachineFunction - Register allocate the whole function
166 bool runOnMachineFunction(MachineFunction &Fn);
167
168 /// AllocateBasicBlock - Register allocate the specified basic block.
169 void AllocateBasicBlock(MachineBasicBlock &MBB);
170
Chris Lattner82bee0f2002-12-18 08:14:26 +0000171
Chris Lattner82bee0f2002-12-18 08:14:26 +0000172 /// areRegsEqual - This method returns true if the specified registers are
173 /// related to each other. To do this, it checks to see if they are equal
174 /// or if the first register is in the alias set of the second register.
175 ///
176 bool areRegsEqual(unsigned R1, unsigned R2) const {
177 if (R1 == R2) return true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000178 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000179 *AliasSet; ++AliasSet) {
180 if (*AliasSet == R1) return true;
181 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000182 return false;
183 }
184
Chris Lattner580f9be2002-12-28 20:40:43 +0000185 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000186 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000187 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000188
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000189 /// removePhysReg - This method marks the specified physical register as no
190 /// longer being in use.
191 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000192 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000193
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000194 void storeVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
195 unsigned VirtReg, unsigned PhysReg, bool isKill);
196
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000197 /// spillVirtReg - This method spills the value specified by PhysReg into
198 /// the virtual register slot specified by VirtReg. It then updates the RA
199 /// data structures to indicate the fact that PhysReg is now available.
200 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000201 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000202 unsigned VirtReg, unsigned PhysReg);
203
Chris Lattnerc21be922002-12-16 17:44:42 +0000204 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000205 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
206 /// true, then the request is ignored if the physical register does not
207 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000208 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000209 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000210 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000211
Chris Lattner91a452b2003-01-13 00:25:40 +0000212 /// assignVirtToPhysReg - This method updates local state so that we know
213 /// that PhysReg is the proper container for VirtReg now. The physical
214 /// register must not be used for anything else when this is called.
215 ///
216 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
217
Chris Lattnerae640432002-12-17 02:50:10 +0000218 /// isPhysRegAvailable - Return true if the specified physical register is
219 /// free and available for use. This also includes checking to see if
220 /// aliased registers are all free...
221 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000222 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000223
224 /// getFreeReg - Look to see if there is a free register available in the
225 /// specified register class. If not, return 0.
226 ///
227 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000228
Chris Lattner91a452b2003-01-13 00:25:40 +0000229 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000230 /// register. If all compatible physical registers are used, this method
231 /// spills the last used virtual register to the stack, and uses that
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000232 /// register. If NoFree is true, that means the caller knows there isn't
233 /// a free register, do not call getFreeReg().
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000234 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000235 unsigned VirtReg, bool NoFree = false);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000236
Bob Wilsone0f745b2009-05-07 21:19:45 +0000237 /// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000238 /// register use to refer to a physical register. This method may do this
239 /// in one of several ways: if the register is available in a physical
240 /// register already, it uses that physical register. If the value is not
241 /// in a physical register, and if there are physical registers available,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000242 /// it loads it into a register: PhysReg if that is an available physical
243 /// register, otherwise any physical register of the right class.
244 /// If register pressure is high, and it is possible, it tries to fold the
245 /// load of the virtual register into the instruction itself. It avoids
246 /// doing this if register pressure is low to improve the chance that
247 /// subsequent instructions can use the reloaded value. This method
248 /// returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000249 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000250 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000251 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
252 unsigned PhysReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000253
Owen Anderson9094db12008-07-09 20:14:53 +0000254 /// ComputeLocalLiveness - Computes liveness of registers within a basic
255 /// block, setting the killed/dead flags as appropriate.
256 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000257
258 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
259 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000260 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000261 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000262}
263
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000264/// getStackSpaceFor - This allocates space for the specified virtual register
265/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000266int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000267 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000268 int SS = StackSlotForVirtReg[VirtReg];
269 if (SS != -1)
270 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000271
Chris Lattner580f9be2002-12-28 20:40:43 +0000272 // Allocate a new stack object for this spill location...
David Greene3f2bf852009-11-12 20:49:22 +0000273 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
274 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000275
Chris Lattner4dd81632010-03-31 05:15:22 +0000276 // Assign the slot.
Evan Chengbdb10fe2008-07-10 18:23:23 +0000277 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000278 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000279}
280
Chris Lattnerae640432002-12-17 02:50:10 +0000281
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000282/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000283/// longer being in use.
284///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000285void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000286 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000287
288 std::vector<unsigned>::iterator It =
289 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
290 if (It != PhysRegsUseOrder.end())
291 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000292}
293
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000294/// storeVirtReg - Store a virtual register to its assigned stack slot.
295void RALocal::storeVirtReg(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator I,
297 unsigned VirtReg, unsigned PhysReg,
298 bool isKill) {
299 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
300 int FrameIndex = getStackSpaceFor(VirtReg, RC);
301 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Cheng746ad692010-05-06 19:06:44 +0000302 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC, TRI);
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000303 ++NumStores; // Update statistics
Jakob Stoklund Olesendcf77082010-05-18 22:20:09 +0000304
305 // Mark the spill instruction as last use if we're not killing the register.
306 if (!isKill) {
307 MachineInstr *Spill = llvm::prior(I);
308 int OpNum = Spill->findRegisterUseOperandIdx(PhysReg);
309 if (OpNum < 0)
310 getVirtRegLastUse(VirtReg) = std::make_pair((MachineInstr*)0, 0);
311 else
312 getVirtRegLastUse(VirtReg) = std::make_pair(Spill, OpNum);
313 }
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000314}
Chris Lattner91a452b2003-01-13 00:25:40 +0000315
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000316/// spillVirtReg - This method spills the value specified by PhysReg into the
317/// virtual register slot specified by VirtReg. It then updates the RA data
318/// structures to indicate the fact that PhysReg is now available.
319///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000320void RALocal::spillVirtReg(MachineBasicBlock &MBB,
321 MachineBasicBlock::iterator I,
322 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000323 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000324 " Must not have appropriate kill for the register or use exists beyond"
325 " the intended one.");
David Greene44248172010-01-05 01:26:05 +0000326 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000327 << " containing %reg" << VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000328
Evan Cheng839b7592008-01-17 02:08:17 +0000329 if (!isVirtRegModified(VirtReg)) {
David Greene44248172010-01-05 01:26:05 +0000330 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Cheng839b7592008-01-17 02:08:17 +0000331 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
332 if (LastUse.first)
333 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000334 } else {
335 // Otherwise, there is a virtual register corresponding to this physical
336 // register. We only need to spill it into its stack slot if it has been
337 // modified.
Evan Cheng2fc628d2008-02-06 19:16:53 +0000338 // If the instruction reads the register that's spilled, (e.g. this can
339 // happen if it is a move to a physical register), then the spill
340 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000341 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000342 storeVirtReg(MBB, I, VirtReg, PhysReg, isKill);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000343 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000344
345 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000346
David Greene44248172010-01-05 01:26:05 +0000347 DEBUG(dbgs() << '\n');
Chris Lattner82bee0f2002-12-18 08:14:26 +0000348 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000349}
350
Chris Lattnerae640432002-12-17 02:50:10 +0000351
Chris Lattner91a452b2003-01-13 00:25:40 +0000352/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000353/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
354/// then the request is ignored if the physical register does not contain a
355/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000356///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000357void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
358 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000359 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000360 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000361 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
362 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Chris Lattner4dd81632010-03-31 05:15:22 +0000363 return;
364 }
365
366 // If the selected register aliases any other registers, we must make
367 // sure that one of the aliases isn't alive.
368 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
369 *AliasSet; ++AliasSet) {
370 if (PhysRegsUsed[*AliasSet] == -1 || // Spill aliased register.
371 PhysRegsUsed[*AliasSet] == -2) // If allocatable.
372 continue;
373
374 if (PhysRegsUsed[*AliasSet])
375 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000376 }
377}
378
379
380/// assignVirtToPhysReg - This method updates local state so that we know
381/// that PhysReg is the proper container for VirtReg now. The physical
382/// register must not be used for anything else when this is called.
383///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000384void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000385 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000386 // Update information to note the fact that this register was just used, and
387 // it holds VirtReg.
388 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000389 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000390 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000391}
392
393
Chris Lattnerae640432002-12-17 02:50:10 +0000394/// isPhysRegAvailable - Return true if the specified physical register is free
395/// and available for use. This also includes checking to see if aliased
396/// registers are all free...
397///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000398bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000399 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000400
401 // If the selected register aliases any other allocated registers, it is
402 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000403 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000404 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000405 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000406 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000407 return true;
408}
409
410
Chris Lattner91a452b2003-01-13 00:25:40 +0000411/// getFreeReg - Look to see if there is a free register available in the
412/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000413///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000414unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000415 // Get iterators defining the range of registers that are valid to allocate in
416 // this class, which also specifies the preferred allocation order.
417 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
418 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000419
Chris Lattner91a452b2003-01-13 00:25:40 +0000420 for (; RI != RE; ++RI)
421 if (isPhysRegAvailable(*RI)) { // Is reg unused?
422 assert(*RI != 0 && "Cannot use register!");
423 return *RI; // Found an unused register!
424 }
425 return 0;
426}
427
428
Chris Lattner91a452b2003-01-13 00:25:40 +0000429/// getReg - Find a physical register to hold the specified virtual
430/// register. If all compatible physical registers are used, this method spills
431/// the last used virtual register to the stack, and uses that register.
432///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000433unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000434 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000435 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000436
437 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000438 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000439
Chris Lattner4dd81632010-03-31 05:15:22 +0000440 if (PhysReg != 0) {
441 // Assign the register.
442 assignVirtToPhysReg(VirtReg, PhysReg);
443 return PhysReg;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000444 }
445
446 // If we didn't find an unused register, scavenge one now!
447 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000448
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000449 // Loop over all of the preallocated registers from the least recently used
450 // to the most recently used. When we find one that is capable of holding
451 // our register, use it.
452 for (unsigned i = 0; PhysReg == 0; ++i) {
453 assert(i != PhysRegsUseOrder.size() &&
454 "Couldn't find a register of the appropriate class!");
455
456 unsigned R = PhysRegsUseOrder[i];
457
458 // We can only use this register if it holds a virtual register (ie, it
459 // can be spilled). Do not use it if it is an explicitly allocated
460 // physical register!
461 assert(PhysRegsUsed[R] != -1 &&
462 "PhysReg in PhysRegsUseOrder, but is not allocated?");
463 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
464 // If the current register is compatible, use it.
465 if (RC->contains(R)) {
466 PhysReg = R;
467 break;
468 }
469
470 // If one of the registers aliased to the current register is
471 // compatible, use it.
472 for (const unsigned *AliasIt = TRI->getAliasSet(R);
473 *AliasIt; ++AliasIt) {
474 if (!RC->contains(*AliasIt)) continue;
475
476 // If this is pinned down for some reason, don't use it. For
477 // example, if CL is pinned, and we run across CH, don't use
478 // CH as justification for using scavenging ECX (which will
479 // fail).
480 if (PhysRegsUsed[*AliasIt] == 0) continue;
481
482 // Make sure the register is allocatable. Don't allocate SIL on
483 // x86-32.
484 if (PhysRegsUsed[*AliasIt] == -2) continue;
485
486 PhysReg = *AliasIt; // Take an aliased register
487 break;
488 }
489 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000490 }
491
Chris Lattner4dd81632010-03-31 05:15:22 +0000492 assert(PhysReg && "Physical register not assigned!?!?");
493
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000494 // At this point PhysRegsUseOrder[i] is the least recently used register of
495 // compatible register class. Spill it to memory and reap its remains.
Chris Lattner4dd81632010-03-31 05:15:22 +0000496 spillPhysReg(MBB, I, PhysReg);
497
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000498 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000499 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000500 return PhysReg;
501}
502
Chris Lattnerae640432002-12-17 02:50:10 +0000503
Bob Wilson8d24f412009-05-07 21:20:42 +0000504/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000505/// register use to refer to a physical register. This method may do this in
506/// one of several ways: if the register is available in a physical register
507/// already, it uses that physical register. If the value is not in a physical
508/// register, and if there are physical registers available, it loads it into a
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000509/// register: PhysReg if that is an available physical register, otherwise any
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000510/// register. If register pressure is high, and it is possible, it tries to
511/// fold the load of the virtual register into the instruction itself. It
512/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000513/// subsequent instructions can use the reloaded value. This method returns
514/// the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000515///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000516MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000517 unsigned OpNum,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000518 SmallSet<unsigned, 4> &ReloadedRegs,
519 unsigned PhysReg) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000520 unsigned VirtReg = MI->getOperand(OpNum).getReg();
Evan Cheng736f89b2010-05-12 01:29:36 +0000521 unsigned SubIdx = MI->getOperand(OpNum).getSubReg();
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000522
523 // If the virtual register is already available, just update the instruction
524 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000525 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Evan Cheng736f89b2010-05-12 01:29:36 +0000526 if (SubIdx) {
527 PR = TRI->getSubReg(PR, SubIdx);
528 MI->getOperand(OpNum).setSubReg(0);
529 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000530 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesenf463d952010-02-16 01:27:47 +0000531 if (!MI->isDebugValue()) {
532 // Do not do these for DBG_VALUE as they can affect codegen.
533 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesen3da6e092010-02-15 01:45:47 +0000534 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesenf463d952010-02-16 01:27:47 +0000535 }
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000536 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000537 }
538
Chris Lattner1e3812c2004-02-17 04:08:37 +0000539 // Otherwise, we need to fold it into the current instruction, or reload it.
540 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000541 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000542 // If we already have a PhysReg (this happens when the instruction is a
543 // reg-to-reg copy with a PhysReg destination) use that.
544 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
545 !isPhysRegAvailable(PhysReg))
546 PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000547 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000548
Chris Lattner11390e72004-02-17 08:09:40 +0000549 if (PhysReg) { // Register is available, allocate it!
550 assignVirtToPhysReg(VirtReg, PhysReg);
551 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000552 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000553 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000554 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000555 }
556
Chris Lattner91a452b2003-01-13 00:25:40 +0000557 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
558
David Greene44248172010-01-05 01:26:05 +0000559 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000560 << TRI->getName(PhysReg) << "\n");
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000561
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000562 // Add move instruction(s)
Evan Cheng746ad692010-05-06 19:06:44 +0000563 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC, TRI);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000564 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000565
Chris Lattner84bc5422007-12-31 04:13:23 +0000566 MF->getRegInfo().setPhysRegUsed(PhysReg);
Evan Cheng736f89b2010-05-12 01:29:36 +0000567 // Assign the input register.
568 if (SubIdx) {
569 MI->getOperand(OpNum).setSubReg(0);
570 MI->getOperand(OpNum).setReg(TRI->getSubReg(PhysReg, SubIdx));
571 } else
572 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000573 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000574
575 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000576 std::string msg;
577 raw_string_ostream Msg(msg);
578 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000579 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000580 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000581 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000582 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000583 }
Chris Lattner75361b62010-04-07 22:58:41 +0000584 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000585 }
586 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
587 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000588 if (ReloadedRegs.insert(*SubRegs)) continue;
589
590 std::string msg;
591 raw_string_ostream Msg(msg);
592 Msg << "Ran out of registers during register allocation!";
593 if (MI->isInlineAsm()) {
594 Msg << "\nPlease check your inline asm statement for invalid "
595 << "constraints:\n";
596 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000597 }
Chris Lattner75361b62010-04-07 22:58:41 +0000598 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000599 }
600
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000601 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000602}
603
Evan Cheng7ac19af2007-06-26 21:05:13 +0000604/// isReadModWriteImplicitKill - True if this is an implicit kill for a
605/// read/mod/write register, i.e. update partial register.
606static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
607 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000608 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000609 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000610 MO.isDef() && !MO.isDead())
611 return true;
612 }
613 return false;
614}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000615
Evan Cheng7ac19af2007-06-26 21:05:13 +0000616/// isReadModWriteImplicitDef - True if this is an implicit def for a
617/// read/mod/write register, i.e. update partial register.
618static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
619 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000620 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000621 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000622 !MO.isDef() && MO.isKill())
623 return true;
624 }
625 return false;
626}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000627
Owen Anderson491fccc2008-07-08 22:24:50 +0000628// precedes - Helper function to determine with MachineInstr A
629// precedes MachineInstr B within the same MBB.
630static bool precedes(MachineBasicBlock::iterator A,
631 MachineBasicBlock::iterator B) {
632 if (A == B)
633 return false;
634
635 MachineBasicBlock::iterator I = A->getParent()->begin();
636 while (I != A->getParent()->end()) {
637 if (I == A)
638 return true;
639 else if (I == B)
640 return false;
641
642 ++I;
643 }
644
645 return false;
646}
647
Owen Anderson9094db12008-07-09 20:14:53 +0000648/// ComputeLocalLiveness - Computes liveness of registers within a basic
649/// block, setting the killed/dead flags as appropriate.
650void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000651 // Keep track of the most recently seen previous use or def of each reg,
652 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000653 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000654 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
655 I != E; ++I) {
Dale Johannesen3da6e092010-02-15 01:45:47 +0000656 if (I->isDebugValue())
657 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +0000658
Owen Anderson491fccc2008-07-08 22:24:50 +0000659 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000660 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000661 // Uses don't trigger any flags, but we need to save
662 // them for later. Also, we have to process these
663 // _before_ processing the defs, since an instr
664 // uses regs before it defs them.
Chris Lattner4dd81632010-03-31 05:15:22 +0000665 if (!MO.isReg() || !MO.getReg() || !MO.isUse())
666 continue;
Jakob Stoklund Olesena50fba92010-05-03 23:49:20 +0000667
668 // Ignore helpful kill flags from earlier passes.
669 MO.setIsKill(false);
670
Chris Lattner4dd81632010-03-31 05:15:22 +0000671 LastUseDef[MO.getReg()] = std::make_pair(I, i);
672
673 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
674
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000675 const unsigned *Aliases = TRI->getAliasSet(MO.getReg());
676 if (Aliases == 0)
677 continue;
678
679 while (*Aliases) {
680 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
681 alias = LastUseDef.find(*Aliases);
682
683 if (alias != LastUseDef.end() && alias->second.first != I)
684 LastUseDef[*Aliases] = std::make_pair(I, i);
685
686 ++Aliases;
Owen Anderson04764de2008-10-08 04:30:51 +0000687 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000688 }
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000689
Owen Anderson491fccc2008-07-08 22:24:50 +0000690 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000691 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000692 // Defs others than 2-addr redefs _do_ trigger flag changes:
693 // - A def followed by a def is dead
694 // - A use followed by a def is a kill
Chris Lattner4dd81632010-03-31 05:15:22 +0000695 if (!MO.isReg() || !MO.getReg() || !MO.isDef()) continue;
Evan Cheng736f89b2010-05-12 01:29:36 +0000696
697 unsigned SubIdx = MO.getSubReg();
Chris Lattner4dd81632010-03-31 05:15:22 +0000698 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
699 last = LastUseDef.find(MO.getReg());
700 if (last != LastUseDef.end()) {
701 // Check if this is a two address instruction. If so, then
702 // the def does not kill the use.
Evan Cheng736f89b2010-05-12 01:29:36 +0000703 if (last->second.first == I && I->isRegTiedToUseOperand(i))
Chris Lattner4dd81632010-03-31 05:15:22 +0000704 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000705
Chris Lattner4dd81632010-03-31 05:15:22 +0000706 MachineOperand &lastUD =
707 last->second.first->getOperand(last->second.second);
Evan Cheng736f89b2010-05-12 01:29:36 +0000708 if (SubIdx && lastUD.getSubReg() != SubIdx)
709 // Partial re-def, the last def is not dead.
710 // %reg1024:5<def> =
711 // %reg1024:6<def> =
712 // or
713 // %reg1024:5<def> = op %reg1024, 5
714 continue;
715
Chris Lattner4dd81632010-03-31 05:15:22 +0000716 if (lastUD.isDef())
717 lastUD.setIsDead(true);
718 else
719 lastUD.setIsKill(true);
Owen Anderson491fccc2008-07-08 22:24:50 +0000720 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000721
722 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000723 }
724 }
725
726 // Live-out (of the function) registers contain return values of the function,
727 // so we need to make sure they are alive at return time.
Bill Wendlingb0d27662010-03-16 02:01:51 +0000728 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
729 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
730
731 if (BBEndsInReturn)
Owen Anderson491fccc2008-07-08 22:24:50 +0000732 for (MachineRegisterInfo::liveout_iterator
733 I = MF->getRegInfo().liveout_begin(),
734 E = MF->getRegInfo().liveout_end(); I != E; ++I)
735 if (!Ret->readsRegister(*I)) {
736 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
737 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
738 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000739
740 // Finally, loop over the final use/def of each reg
741 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000742 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000743 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000744 MachineInstr *MI = I->second.first;
Owen Anderson491fccc2008-07-08 22:24:50 +0000745 unsigned idx = I->second.second;
Chris Lattner4dd81632010-03-31 05:15:22 +0000746 MachineOperand &MO = MI->getOperand(idx);
Owen Anderson491fccc2008-07-08 22:24:50 +0000747
748 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
749
750 // A crude approximation of "live-out" calculation
751 bool usedOutsideBlock = isPhysReg ? false :
752 UsedInMultipleBlocks.test(MO.getReg() -
753 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000754
755 // If the machine BB ends in a return instruction, then the value isn't used
756 // outside of the BB.
757 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000758 // DBG_VALUE complicates this: if the only refs of a register outside
759 // this block are DBG_VALUE, we can't keep the reg live just for that,
760 // as it will cause the reg to be spilled at the end of this block when
761 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
762 // happens.
763 bool UsedByDebugValueOnly = false;
Evan Cheng736f89b2010-05-12 01:29:36 +0000764 for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()),
765 UE = MRI->reg_end(); UI != UE; ++UI) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000766 // Two cases:
767 // - used in another block
768 // - used in the same block before it is defined (loop)
Chris Lattner4dd81632010-03-31 05:15:22 +0000769 if (UI->getParent() == &MBB &&
770 !(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI)))
771 continue;
772
773 if (UI->isDebugValue()) {
774 UsedByDebugValueOnly = true;
775 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000776 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000777
778 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
779 UsedInMultipleBlocks.set(MO.getReg() -
780 TargetRegisterInfo::FirstVirtualRegister);
781 usedOutsideBlock = true;
782 UsedByDebugValueOnly = false;
783 break;
Bill Wendling8fe347a2010-03-16 01:05:35 +0000784 }
785
Dale Johannesenf463d952010-02-16 01:27:47 +0000786 if (UsedByDebugValueOnly)
Evan Cheng736f89b2010-05-12 01:29:36 +0000787 for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()),
788 UE = MRI->reg_end(); UI != UE; ++UI)
Dale Johannesenf463d952010-02-16 01:27:47 +0000789 if (UI->isDebugValue() &&
790 (UI->getParent() != &MBB ||
791 (MO.isDef() && precedes(&*UI, MI))))
792 UI.getOperand().setReg(0U);
793 }
794
Bill Wendling8fe347a2010-03-16 01:05:35 +0000795 // Physical registers and those that are not live-out of the block are
796 // killed/dead at their last use/def within this block.
Dan Gohman15843902010-03-18 18:07:13 +0000797 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000798 if (MO.isUse()) {
799 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000800 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000801 MO.setIsKill(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000802 } else {
Owen Anderson491fccc2008-07-08 22:24:50 +0000803 MO.setIsDead(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000804 }
Dan Gohman15843902010-03-18 18:07:13 +0000805 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000806 }
Owen Anderson9094db12008-07-09 20:14:53 +0000807}
808
809void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
810 // loop over each instruction
811 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000812
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000813 DEBUG({
814 const BasicBlock *LBB = MBB.getBasicBlock();
815 if (LBB)
David Greene44248172010-01-05 01:26:05 +0000816 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000817 });
Owen Anderson9094db12008-07-09 20:14:53 +0000818
Evan Chengd5a48022009-01-29 18:37:30 +0000819 // Add live-in registers as active.
820 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000821 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000822 unsigned Reg = *I;
823 MF->getRegInfo().setPhysRegUsed(Reg);
824 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000825 AddToPhysRegsUseOrder(Reg);
Evan Chengd5a48022009-01-29 18:37:30 +0000826 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
827 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000828 if (PhysRegsUsed[*SubRegs] == -2) continue;
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000829
830 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +0000831 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
832 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Evan Chengd5a48022009-01-29 18:37:30 +0000833 }
Owen Anderson9094db12008-07-09 20:14:53 +0000834 }
835
836 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000837
Chris Lattner44500e32006-06-15 22:21:53 +0000838 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000839 while (MII != MBB.end()) {
840 MachineInstr *MI = MII++;
Chris Lattner749c6f62008-01-07 07:27:27 +0000841 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000842 DEBUG({
David Greene44248172010-01-05 01:26:05 +0000843 dbgs() << "\nStarting RegAlloc of: " << *MI;
844 dbgs() << " Regs have values: ";
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000845 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000846 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
847 if (PhysRegsUsed[i] && isVirtRegModified(PhysRegsUsed[i]))
848 dbgs() << "*";
David Greene44248172010-01-05 01:26:05 +0000849 dbgs() << "[" << TRI->getName(i)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000850 << ",%reg" << PhysRegsUsed[i] << "] ";
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +0000851 }
David Greene44248172010-01-05 01:26:05 +0000852 dbgs() << '\n';
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000853 });
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000854
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000855 // Determine whether this is a copy instruction. The cases where the
856 // source or destination are phys regs are handled specially.
857 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000858 unsigned SrcCopyPhysReg = 0U;
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000859 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
Evan Chengde7dea22010-05-12 23:59:42 +0000860 SrcCopySubReg, DstCopySubReg) &&
861 SrcCopySubReg == DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000862 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
863 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000864
Chris Lattnerae640432002-12-17 02:50:10 +0000865 // Loop over the implicit uses, making sure that they are at the head of the
866 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000867 if (TID.ImplicitUses) {
868 for (const unsigned *ImplicitUses = TID.ImplicitUses;
869 *ImplicitUses; ++ImplicitUses)
870 MarkPhysRegRecentlyUsed(*ImplicitUses);
871 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000872
Evan Chengddee8422006-11-15 20:55:15 +0000873 SmallVector<unsigned, 8> Kills;
874 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000875 MachineOperand &MO = MI->getOperand(i);
Nick Lewycky403d3122010-05-07 01:45:38 +0000876 if (!MO.isReg() || !MO.isKill()) continue;
877
Chris Lattner4dd81632010-03-31 05:15:22 +0000878 if (!MO.isImplicit())
879 Kills.push_back(MO.getReg());
880 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
881 // These are extra physical register kills when a sub-register
882 // is defined (def of a sub-register is a read/mod/write of the
883 // larger registers). Ignore.
884 Kills.push_back(MO.getReg());
Evan Chengddee8422006-11-15 20:55:15 +0000885 }
886
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000887 // If any physical regs are earlyclobber, spill any value they might
888 // have in them, then mark them unallocatable.
889 // If any virtual regs are earlyclobber, allocate them now (before
890 // freeing inputs that are killed).
Chris Lattner518bb532010-02-09 19:54:29 +0000891 if (MI->isInlineAsm()) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000892 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
893 MachineOperand &MO = MI->getOperand(i);
894 if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber() ||
895 !MO.getReg())
896 continue;
897
898 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
899 unsigned DestVirtReg = MO.getReg();
900 unsigned DestPhysReg;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000901
Chris Lattner4dd81632010-03-31 05:15:22 +0000902 // If DestVirtReg already has a value, use it.
903 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
904 DestPhysReg = getReg(MBB, MI, DestVirtReg);
905 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
906 markVirtRegModified(DestVirtReg);
907 getVirtRegLastUse(DestVirtReg) =
908 std::make_pair((MachineInstr*)0, 0);
909 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
910 << " to %reg" << DestVirtReg << "\n");
Evan Cheng736f89b2010-05-12 01:29:36 +0000911 if (unsigned DestSubIdx = MO.getSubReg()) {
912 MO.setSubReg(0);
913 DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx);
914 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000915 MO.setReg(DestPhysReg); // Assign the earlyclobber register
916 } else {
917 unsigned Reg = MO.getReg();
918 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
919 // These are extra physical register defs when a sub-register
920 // is defined (def of a sub-register is a read/mod/write of the
921 // larger registers). Ignore.
922 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000923
Chris Lattner4dd81632010-03-31 05:15:22 +0000924 MF->getRegInfo().setPhysRegUsed(Reg);
925 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
926 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000927 AddToPhysRegsUseOrder(Reg);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000928
Chris Lattner4dd81632010-03-31 05:15:22 +0000929 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
930 *SubRegs; ++SubRegs) {
931 if (PhysRegsUsed[*SubRegs] == -2) continue;
932 MF->getRegInfo().setPhysRegUsed(*SubRegs);
933 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +0000934 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000935 }
936 }
937 }
938 }
939
Dale Johannesen10fedd22010-02-10 00:11:11 +0000940 // If a DBG_VALUE says something is located in a spilled register,
941 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesenca134612010-01-30 00:57:47 +0000942 // from being reloaded here. Doing that would change the generated
943 // code, unless another use immediately follows this instruction.
Chris Lattner518bb532010-02-09 19:54:29 +0000944 if (MI->isDebugValue() &&
Dale Johannesenca134612010-01-30 00:57:47 +0000945 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
946 unsigned VirtReg = MI->getOperand(0).getReg();
947 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
948 !getVirt2PhysRegMapSlot(VirtReg))
949 MI->getOperand(0).setReg(0U);
950 }
951
Brian Gaeke53b99a02003-08-15 21:19:25 +0000952 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000953 // incoming values if we are out of registers. Note that we completely
954 // ignore physical register uses here. We assume that if an explicit
955 // physical register is referenced by the instruction, that it is guaranteed
956 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000957 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000958 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000959 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000960 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000961 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000962 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000963 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000964 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
965 isCopy ? DstCopyReg : 0);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000966 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000967
Evan Chengddee8422006-11-15 20:55:15 +0000968 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000969 // value, freeing the register being used, so it doesn't need to be
970 // spilled to memory.
971 //
Evan Chengddee8422006-11-15 20:55:15 +0000972 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
973 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000974 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000975 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000976 // If the virtual register was never materialized into a register, it
977 // might not be in the map, but it won't hurt to zero it out anyway.
978 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
979 PhysReg = PhysRegSlot;
980 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000981 } else if (PhysRegsUsed[PhysReg] == -2) {
982 // Unallocatable register dead, ignore.
983 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000984 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000985 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000986 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000987 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000988
Chris Lattner4dd81632010-03-31 05:15:22 +0000989 if (!PhysReg) continue;
990
991 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
992 << "[%reg" << VirtReg <<"], removing it from live set\n");
993 removePhysReg(PhysReg);
994 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
995 *SubRegs; ++SubRegs) {
996 if (PhysRegsUsed[*SubRegs] != -2) {
997 DEBUG(dbgs() << " Last use of "
998 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
999 <<"], removing it from live set\n");
1000 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +00001001 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001002 }
1003 }
1004
1005 // Loop over all of the operands of the instruction, spilling registers that
1006 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001007 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001008 MachineOperand &MO = MI->getOperand(i);
1009 if (!MO.isReg() || !MO.isDef() || MO.isImplicit() || !MO.getReg() ||
1010 MO.isEarlyClobber() ||
1011 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1012 continue;
1013
1014 unsigned Reg = MO.getReg();
1015 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
1016 // These are extra physical register defs when a sub-register
1017 // is defined (def of a sub-register is a read/mod/write of the
1018 // larger registers). Ignore.
1019 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +00001020
Chris Lattner4dd81632010-03-31 05:15:22 +00001021 MF->getRegInfo().setPhysRegUsed(Reg);
1022 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
1023 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001024 AddToPhysRegsUseOrder(Reg);
Evan Cheng7ac19af2007-06-26 21:05:13 +00001025
Chris Lattner4dd81632010-03-31 05:15:22 +00001026 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
1027 *SubRegs; ++SubRegs) {
1028 if (PhysRegsUsed[*SubRegs] == -2) continue;
1029
1030 MF->getRegInfo().setPhysRegUsed(*SubRegs);
1031 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001032 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner91a452b2003-01-13 00:25:40 +00001033 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001034 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001035
1036 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +00001037 if (TID.ImplicitDefs) {
1038 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
1039 *ImplicitDefs; ++ImplicitDefs) {
1040 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +00001041 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +00001042 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001043 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +00001044 PhysRegsUsed[Reg] = 0; // It is free and reserved now
1045 }
Chris Lattner84bc5422007-12-31 04:13:23 +00001046 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +00001047 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
1048 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001049 if (PhysRegsUsed[*SubRegs] == -2) continue;
1050
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001051 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +00001052 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
1053 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Jim Laskeycd4317e2006-07-21 21:15:20 +00001054 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +00001055 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +00001056 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001057
Evan Chengddee8422006-11-15 20:55:15 +00001058 SmallVector<unsigned, 8> DeadDefs;
1059 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001060 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001061 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +00001062 DeadDefs.push_back(MO.getReg());
1063 }
1064
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001065 // Okay, we have allocated all of the source operands and spilled any values
1066 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +00001067 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +00001068 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +00001069 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001070 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001071 MachineOperand &MO = MI->getOperand(i);
1072 if (!MO.isReg() || !MO.isDef() || !MO.getReg() ||
1073 MO.isEarlyClobber() ||
1074 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1075 continue;
1076
1077 unsigned DestVirtReg = MO.getReg();
1078 unsigned DestPhysReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001079
Chris Lattner4dd81632010-03-31 05:15:22 +00001080 // If DestVirtReg already has a value, use it.
1081 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
1082 // If this is a copy try to reuse the input as the output;
1083 // that will make the copy go away.
1084 // If this is a copy, the source reg is a phys reg, and
1085 // that reg is available, use that phys reg for DestPhysReg.
1086 // If this is a copy, the source reg is a virtual reg, and
1087 // the phys reg that was assigned to that virtual reg is now
1088 // available, use that phys reg for DestPhysReg. (If it's now
1089 // available that means this was the last use of the source.)
1090 if (isCopy &&
1091 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
1092 isPhysRegAvailable(SrcCopyReg)) {
1093 DestPhysReg = SrcCopyReg;
1094 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1095 } else if (isCopy &&
1096 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
1097 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
1098 MF->getRegInfo().getRegClass(DestVirtReg)->
1099 contains(SrcCopyPhysReg)) {
1100 DestPhysReg = SrcCopyPhysReg;
1101 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1102 } else
1103 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001104 }
Chris Lattner4dd81632010-03-31 05:15:22 +00001105 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
1106 markVirtRegModified(DestVirtReg);
1107 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
1108 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
1109 << " to %reg" << DestVirtReg << "\n");
Evan Cheng736f89b2010-05-12 01:29:36 +00001110
1111 if (unsigned DestSubIdx = MO.getSubReg()) {
1112 MO.setSubReg(0);
1113 DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx);
1114 }
Chris Lattner4dd81632010-03-31 05:15:22 +00001115 MO.setReg(DestPhysReg); // Assign the output register
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001116 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001117
Chris Lattner56ddada2004-02-17 17:49:10 +00001118 // If this instruction defines any registers that are immediately dead,
1119 // kill them now.
1120 //
Evan Chengddee8422006-11-15 20:55:15 +00001121 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
1122 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +00001123 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001124 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +00001125 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1126 PhysReg = PhysRegSlot;
1127 assert(PhysReg != 0);
1128 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +00001129 } else if (PhysRegsUsed[PhysReg] == -2) {
1130 // Unallocatable register dead, ignore.
1131 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +00001132 } else if (!PhysReg)
1133 continue;
1134
1135 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
1136 << " [%reg" << VirtReg
1137 << "] is never used, removing it from live set\n");
1138 removePhysReg(PhysReg);
1139 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
1140 *AliasSet; ++AliasSet) {
1141 if (PhysRegsUsed[*AliasSet] != -2) {
1142 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
1143 << " [%reg" << *AliasSet
1144 << "] is never used, removing it from live set\n");
1145 removePhysReg(*AliasSet);
Evan Chengddee8422006-11-15 20:55:15 +00001146 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001147 }
1148 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001149
Jakob Stoklund Olesen8387d7d2010-04-30 21:19:29 +00001150 // If this instruction is a call, make sure there are no dirty registers. The
1151 // call might throw an exception, and the landing pad expects to find all
1152 // registers in stack slots.
1153 if (TID.isCall())
1154 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
1155 if (PhysRegsUsed[i] <= 0) continue;
1156 unsigned VirtReg = PhysRegsUsed[i];
1157 if (!isVirtRegModified(VirtReg)) continue;
1158 DEBUG(dbgs() << " Storing dirty %reg" << VirtReg);
1159 storeVirtReg(MBB, MI, VirtReg, i, false);
1160 markVirtRegModified(VirtReg, false);
1161 DEBUG(dbgs() << " because the call might throw\n");
1162 }
1163
Bob Wilson9d928c22009-05-07 23:47:03 +00001164 // Finally, if this is a noop copy instruction, zap it. (Except that if
1165 // the copy is dead, it must be kept to avoid messing up liveness info for
1166 // the register scavenger. See pr4100.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001167 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1168 SrcCopySubReg, DstCopySubReg) &&
Evan Chengde7dea22010-05-12 23:59:42 +00001169 SrcCopyReg == DstCopyReg && SrcCopySubReg == DstCopySubReg &&
Jakob Stoklund Olesenab2d0082010-05-14 22:40:40 +00001170 DeadDefs.empty()) {
1171 ++NumCopies;
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001172 MBB.erase(MI);
Jakob Stoklund Olesenab2d0082010-05-14 22:40:40 +00001173 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001174 }
1175
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001176 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001177
1178 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001179 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001180 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001181 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001182 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001183 else
1184 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001185 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001186
Chris Lattner9a5ef202005-11-09 05:28:45 +00001187#if 0
1188 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001189 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001190 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001191 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001192 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001193 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001194 AllOk = false;
1195 }
1196 assert(AllOk && "Virtual registers still in phys regs?");
1197#endif
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001198
1199 // Clear any physical register which appear live at the end of the basic
1200 // block, but which do not hold any virtual registers. e.g., the stack
1201 // pointer.
1202 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001203}
1204
1205/// runOnMachineFunction - Register allocate the whole function
1206///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001207bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene44248172010-01-05 01:26:05 +00001208 DEBUG(dbgs() << "Machine Function\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001209 MF = &Fn;
Evan Cheng736f89b2010-05-12 01:29:36 +00001210 MRI = &Fn.getRegInfo();
Chris Lattner580f9be2002-12-28 20:40:43 +00001211 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001212 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001213 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001214
Dan Gohman6f0d0242008-02-10 18:45:23 +00001215 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Jakob Stoklund Olesen23eaf262010-04-17 00:38:36 +00001216
Chris Lattner45d57882006-09-08 19:03:30 +00001217 // At various places we want to efficiently check to see whether a register
1218 // is allocatable. To handle this, we mark all unallocatable registers as
1219 // being pinned down, permanently.
1220 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001221 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001222 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1223 if (!Allocable[i])
1224 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1225 }
Chris Lattner64667b62004-02-09 01:26:13 +00001226
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001227 // initialize the virtual->physical register map to have a 'null'
1228 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001229 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001230 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001231 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001232 Virt2LastUseMap.grow(LastVirtReg);
Chris Lattner4dd81632010-03-31 05:15:22 +00001233 VirtRegModified.resize(LastVirtReg+1 -
1234 TargetRegisterInfo::FirstVirtualRegister);
1235 UsedInMultipleBlocks.resize(LastVirtReg+1 -
1236 TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001237
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001238 // Loop over all of the basic blocks, eliminating virtual register references
1239 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1240 MBB != MBBe; ++MBB)
1241 AllocateBasicBlock(*MBB);
1242
Chris Lattner580f9be2002-12-28 20:40:43 +00001243 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001244 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001245 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001246 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001247 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001248 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001249 return true;
1250}
1251
Chris Lattneref09c632004-01-31 21:27:19 +00001252FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001253 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001254}