blob: 40d5e9e5094c5379ea12060b35a4844690226131 [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;
Dan Gohman6f0d0242008-02-10 18:45:23 +000053 const TargetRegisterInfo *TRI;
Owen Anderson6425f8b2008-01-07 01:35:56 +000054 const TargetInstrInfo *TII;
Chris Lattnerff863ba2002-12-25 05:05:46 +000055
Chris Lattnerb8822ad2003-08-04 23:36:39 +000056 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
57 // values are spilled.
Evan Chengbdb10fe2008-07-10 18:23:23 +000058 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000059
60 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000061 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000062 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000063
64 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000065 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000066 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000067
Chris Lattner64667b62004-02-09 01:26:13 +000068 // PhysRegsUsed - This array is effectively a map, containing entries for
69 // each physical register that currently has a value (ie, it is in
70 // Virt2PhysRegMap). The value mapped to is the virtual register
71 // corresponding to the physical register (the inverse of the
72 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000073 // because it is used by a future instruction, and to -2 if it is not
74 // allocatable. If the entry for a physical register is -1, then the
75 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000076 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000077 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000078
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +000079 // InstrNum - Number of the current instruction. This is used for the
80 // PhysLastUse map.
Chris Lattnerb74e83c2002-12-16 16:15:28 +000081 //
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +000082 unsigned InstrNum;
83
84 // PhysLastUse - Store the instruction number of the last use of each physical
85 // register. This is used to find the least recently used register. when
86 // spilling.
87 //
88 std::vector<unsigned> PhysLastUse;
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
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000127 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000128 PhysLastUse[Reg] = InstrNum;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000129 }
130
131 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000132 virtual const char *getPassName() const {
133 return "Local Register Allocator";
134 }
135
Chris Lattner91a452b2003-01-13 00:25:40 +0000136 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000137 AU.setPreservesCFG();
Chris Lattner91a452b2003-01-13 00:25:40 +0000138 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000139 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000140 MachineFunctionPass::getAnalysisUsage(AU);
141 }
142
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000143 private:
144 /// runOnMachineFunction - Register allocate the whole function
145 bool runOnMachineFunction(MachineFunction &Fn);
146
147 /// AllocateBasicBlock - Register allocate the specified basic block.
148 void AllocateBasicBlock(MachineBasicBlock &MBB);
149
Chris Lattner82bee0f2002-12-18 08:14:26 +0000150
Chris Lattner82bee0f2002-12-18 08:14:26 +0000151 /// areRegsEqual - This method returns true if the specified registers are
152 /// related to each other. To do this, it checks to see if they are equal
153 /// or if the first register is in the alias set of the second register.
154 ///
155 bool areRegsEqual(unsigned R1, unsigned R2) const {
156 if (R1 == R2) return true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000157 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000158 *AliasSet; ++AliasSet) {
159 if (*AliasSet == R1) return true;
160 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000161 return false;
162 }
163
Chris Lattner580f9be2002-12-28 20:40:43 +0000164 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000165 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000166 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000167
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000168 /// removePhysReg - This method marks the specified physical register as no
169 /// longer being in use.
170 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000171 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000172
173 /// spillVirtReg - This method spills the value specified by PhysReg into
174 /// the virtual register slot specified by VirtReg. It then updates the RA
175 /// data structures to indicate the fact that PhysReg is now available.
176 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000177 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000178 unsigned VirtReg, unsigned PhysReg);
179
Chris Lattnerc21be922002-12-16 17:44:42 +0000180 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000181 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
182 /// true, then the request is ignored if the physical register does not
183 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000184 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000185 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000186 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000187
Chris Lattner91a452b2003-01-13 00:25:40 +0000188 /// assignVirtToPhysReg - This method updates local state so that we know
189 /// that PhysReg is the proper container for VirtReg now. The physical
190 /// register must not be used for anything else when this is called.
191 ///
192 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
193
Chris Lattnerae640432002-12-17 02:50:10 +0000194 /// isPhysRegAvailable - Return true if the specified physical register is
195 /// free and available for use. This also includes checking to see if
196 /// aliased registers are all free...
197 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000198 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000199
200 /// getFreeReg - Look to see if there is a free register available in the
201 /// specified register class. If not, return 0.
202 ///
203 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000204
Chris Lattner91a452b2003-01-13 00:25:40 +0000205 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000206 /// register. If all compatible physical registers are used, this method
207 /// spills the last used virtual register to the stack, and uses that
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000208 /// register. If NoFree is true, that means the caller knows there isn't
209 /// a free register, do not call getFreeReg().
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000210 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000211 unsigned VirtReg, bool NoFree = false);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000212
Bob Wilsone0f745b2009-05-07 21:19:45 +0000213 /// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000214 /// register use to refer to a physical register. This method may do this
215 /// in one of several ways: if the register is available in a physical
216 /// register already, it uses that physical register. If the value is not
217 /// in a physical register, and if there are physical registers available,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000218 /// it loads it into a register: PhysReg if that is an available physical
219 /// register, otherwise any physical register of the right class.
220 /// If register pressure is high, and it is possible, it tries to fold the
221 /// load of the virtual register into the instruction itself. It avoids
222 /// doing this if register pressure is low to improve the chance that
223 /// subsequent instructions can use the reloaded value. This method
224 /// returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000225 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000226 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000227 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
228 unsigned PhysReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000229
Owen Anderson9094db12008-07-09 20:14:53 +0000230 /// ComputeLocalLiveness - Computes liveness of registers within a basic
231 /// block, setting the killed/dead flags as appropriate.
232 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000233
234 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
235 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000236 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000237 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000238}
239
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000240/// getStackSpaceFor - This allocates space for the specified virtual register
241/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000242int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000243 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000244 int SS = StackSlotForVirtReg[VirtReg];
245 if (SS != -1)
246 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000247
Chris Lattner580f9be2002-12-28 20:40:43 +0000248 // Allocate a new stack object for this spill location...
David Greene3f2bf852009-11-12 20:49:22 +0000249 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
250 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000251
Chris Lattner4dd81632010-03-31 05:15:22 +0000252 // Assign the slot.
Evan Chengbdb10fe2008-07-10 18:23:23 +0000253 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000254 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000255}
256
Chris Lattnerae640432002-12-17 02:50:10 +0000257
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000258/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000259/// longer being in use.
260///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000261void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000262 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000263}
264
Chris Lattner91a452b2003-01-13 00:25:40 +0000265
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266/// spillVirtReg - This method spills the value specified by PhysReg into the
267/// virtual register slot specified by VirtReg. It then updates the RA data
268/// structures to indicate the fact that PhysReg is now available.
269///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000270void RALocal::spillVirtReg(MachineBasicBlock &MBB,
271 MachineBasicBlock::iterator I,
272 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000273 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000274 " Must not have appropriate kill for the register or use exists beyond"
275 " the intended one.");
David Greene44248172010-01-05 01:26:05 +0000276 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000277 << " containing %reg" << VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000278
Evan Cheng839b7592008-01-17 02:08:17 +0000279 if (!isVirtRegModified(VirtReg)) {
David Greene44248172010-01-05 01:26:05 +0000280 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Cheng839b7592008-01-17 02:08:17 +0000281 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
282 if (LastUse.first)
283 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000284 } else {
285 // Otherwise, there is a virtual register corresponding to this physical
286 // register. We only need to spill it into its stack slot if it has been
287 // modified.
Chris Lattner84bc5422007-12-31 04:13:23 +0000288 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000289 int FrameIndex = getStackSpaceFor(VirtReg, RC);
David Greene44248172010-01-05 01:26:05 +0000290 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Cheng2fc628d2008-02-06 19:16:53 +0000291 // If the instruction reads the register that's spilled, (e.g. this can
292 // happen if it is a move to a physical register), then the spill
293 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000294 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Evan Cheng431bfcb2008-02-11 08:30:52 +0000295 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000296 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000297 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000298
299 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000300
David Greene44248172010-01-05 01:26:05 +0000301 DEBUG(dbgs() << '\n');
Chris Lattner82bee0f2002-12-18 08:14:26 +0000302 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000303}
304
Chris Lattnerae640432002-12-17 02:50:10 +0000305
Chris Lattner91a452b2003-01-13 00:25:40 +0000306/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000307/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
308/// then the request is ignored if the physical register does not contain a
309/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000310///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000311void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
312 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000313 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000314 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000315 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
316 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Chris Lattner4dd81632010-03-31 05:15:22 +0000317 return;
318 }
319
320 // If the selected register aliases any other registers, we must make
321 // sure that one of the aliases isn't alive.
322 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
323 *AliasSet; ++AliasSet) {
324 if (PhysRegsUsed[*AliasSet] == -1 || // Spill aliased register.
325 PhysRegsUsed[*AliasSet] == -2) // If allocatable.
326 continue;
327
328 if (PhysRegsUsed[*AliasSet])
329 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000330 }
331}
332
333
334/// assignVirtToPhysReg - This method updates local state so that we know
335/// that PhysReg is the proper container for VirtReg now. The physical
336/// register must not be used for anything else when this is called.
337///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000338void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000339 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000340 // Update information to note the fact that this register was just used, and
341 // it holds VirtReg.
342 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000343 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000344 MarkPhysRegRecentlyUsed(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000345}
346
347
Chris Lattnerae640432002-12-17 02:50:10 +0000348/// isPhysRegAvailable - Return true if the specified physical register is free
349/// and available for use. This also includes checking to see if aliased
350/// registers are all free...
351///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000352bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000353 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000354
355 // If the selected register aliases any other allocated registers, it is
356 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000357 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000358 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000359 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000360 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000361 return true;
362}
363
364
Chris Lattner91a452b2003-01-13 00:25:40 +0000365/// getFreeReg - Look to see if there is a free register available in the
366/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000367///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000368unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000369 // Get iterators defining the range of registers that are valid to allocate in
370 // this class, which also specifies the preferred allocation order.
371 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
372 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000373
Chris Lattner91a452b2003-01-13 00:25:40 +0000374 for (; RI != RE; ++RI)
375 if (isPhysRegAvailable(*RI)) { // Is reg unused?
376 assert(*RI != 0 && "Cannot use register!");
377 return *RI; // Found an unused register!
378 }
379 return 0;
380}
381
382
Chris Lattner91a452b2003-01-13 00:25:40 +0000383/// getReg - Find a physical register to hold the specified virtual
384/// register. If all compatible physical registers are used, this method spills
385/// the last used virtual register to the stack, and uses that register.
386///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000387unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000388 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000389 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000390
391 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000392 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000393
Chris Lattner4dd81632010-03-31 05:15:22 +0000394 if (PhysReg != 0) {
395 // Assign the register.
396 assignVirtToPhysReg(VirtReg, PhysReg);
397 return PhysReg;
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000398 }
Chris Lattnerae640432002-12-17 02:50:10 +0000399
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000400 // Find the least recently used register in the allocation order.
401 unsigned Oldest = 0;
402 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
403 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
404 for (; RI != RE; ++RI) {
405 unsigned Age = InstrNum-PhysLastUse[*RI];
406 if (Age <= Oldest && PhysReg) continue;
407 PhysReg = *RI;
408 Oldest = Age;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000409 }
410
Chris Lattner4dd81632010-03-31 05:15:22 +0000411 assert(PhysReg && "Physical register not assigned!?!?");
412
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000413 // Spill it to memory and reap its remains.
Chris Lattner4dd81632010-03-31 05:15:22 +0000414 spillPhysReg(MBB, I, PhysReg);
415
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000416 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000417 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000418 return PhysReg;
419}
420
Chris Lattnerae640432002-12-17 02:50:10 +0000421
Bob Wilson8d24f412009-05-07 21:20:42 +0000422/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000423/// register use to refer to a physical register. This method may do this in
424/// one of several ways: if the register is available in a physical register
425/// already, it uses that physical register. If the value is not in a physical
426/// register, and if there are physical registers available, it loads it into a
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000427/// register: PhysReg if that is an available physical register, otherwise any
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000428/// register. If register pressure is high, and it is possible, it tries to
429/// fold the load of the virtual register into the instruction itself. It
430/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000431/// subsequent instructions can use the reloaded value. This method returns
432/// the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000433///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000434MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000435 unsigned OpNum,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000436 SmallSet<unsigned, 4> &ReloadedRegs,
437 unsigned PhysReg) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000438 unsigned VirtReg = MI->getOperand(OpNum).getReg();
439
440 // If the virtual register is already available, just update the instruction
441 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000442 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattnere53f4a02006-05-04 17:52:23 +0000443 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesenf463d952010-02-16 01:27:47 +0000444 if (!MI->isDebugValue()) {
445 // Do not do these for DBG_VALUE as they can affect codegen.
446 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesen3da6e092010-02-15 01:45:47 +0000447 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesenf463d952010-02-16 01:27:47 +0000448 }
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000449 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000450 }
451
Chris Lattner1e3812c2004-02-17 04:08:37 +0000452 // Otherwise, we need to fold it into the current instruction, or reload it.
453 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000454 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000455 // If we already have a PhysReg (this happens when the instruction is a
456 // reg-to-reg copy with a PhysReg destination) use that.
457 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
458 !isPhysRegAvailable(PhysReg))
459 PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000460 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000461
Chris Lattner11390e72004-02-17 08:09:40 +0000462 if (PhysReg) { // Register is available, allocate it!
463 assignVirtToPhysReg(VirtReg, PhysReg);
464 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000465 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000466 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000467 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000468 }
469
Chris Lattner91a452b2003-01-13 00:25:40 +0000470 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
471
David Greene44248172010-01-05 01:26:05 +0000472 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000473 << TRI->getName(PhysReg) << "\n");
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000474
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000475 // Add move instruction(s)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000476 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000477 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000478
Chris Lattner84bc5422007-12-31 04:13:23 +0000479 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000480 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000481 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000482
483 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000484 std::string msg;
485 raw_string_ostream Msg(msg);
486 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000487 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000488 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000489 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000490 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000491 }
Chris Lattner75361b62010-04-07 22:58:41 +0000492 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000493 }
494 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
495 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000496 if (ReloadedRegs.insert(*SubRegs)) continue;
497
498 std::string msg;
499 raw_string_ostream Msg(msg);
500 Msg << "Ran out of registers during register allocation!";
501 if (MI->isInlineAsm()) {
502 Msg << "\nPlease check your inline asm statement for invalid "
503 << "constraints:\n";
504 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000505 }
Chris Lattner75361b62010-04-07 22:58:41 +0000506 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000507 }
508
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000509 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000510}
511
Evan Cheng7ac19af2007-06-26 21:05:13 +0000512/// isReadModWriteImplicitKill - True if this is an implicit kill for a
513/// read/mod/write register, i.e. update partial register.
514static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
515 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000516 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000517 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000518 MO.isDef() && !MO.isDead())
519 return true;
520 }
521 return false;
522}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000523
Evan Cheng7ac19af2007-06-26 21:05:13 +0000524/// isReadModWriteImplicitDef - True if this is an implicit def for a
525/// read/mod/write register, i.e. update partial register.
526static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
527 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000528 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000529 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000530 !MO.isDef() && MO.isKill())
531 return true;
532 }
533 return false;
534}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000535
Owen Anderson491fccc2008-07-08 22:24:50 +0000536// precedes - Helper function to determine with MachineInstr A
537// precedes MachineInstr B within the same MBB.
538static bool precedes(MachineBasicBlock::iterator A,
539 MachineBasicBlock::iterator B) {
540 if (A == B)
541 return false;
542
543 MachineBasicBlock::iterator I = A->getParent()->begin();
544 while (I != A->getParent()->end()) {
545 if (I == A)
546 return true;
547 else if (I == B)
548 return false;
549
550 ++I;
551 }
552
553 return false;
554}
555
Owen Anderson9094db12008-07-09 20:14:53 +0000556/// ComputeLocalLiveness - Computes liveness of registers within a basic
557/// block, setting the killed/dead flags as appropriate.
558void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000559 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Owen Anderson491fccc2008-07-08 22:24:50 +0000560 // Keep track of the most recently seen previous use or def of each reg,
561 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000562 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000563 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
564 I != E; ++I) {
Dale Johannesen3da6e092010-02-15 01:45:47 +0000565 if (I->isDebugValue())
566 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +0000567
Owen Anderson491fccc2008-07-08 22:24:50 +0000568 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000569 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000570 // Uses don't trigger any flags, but we need to save
571 // them for later. Also, we have to process these
572 // _before_ processing the defs, since an instr
573 // uses regs before it defs them.
Chris Lattner4dd81632010-03-31 05:15:22 +0000574 if (!MO.isReg() || !MO.getReg() || !MO.isUse())
575 continue;
576
577 LastUseDef[MO.getReg()] = std::make_pair(I, i);
578
579 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
580
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000581 for (const unsigned *A = TRI->getAliasSet(MO.getReg()); *A; ++A) {
582 std::pair<MachineInstr*, unsigned> &LUD = LastUseDef[*A];
583 if (LUD.first != I)
584 LUD = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000585 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000586 }
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000587
Owen Anderson491fccc2008-07-08 22:24:50 +0000588 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000589 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000590 // Defs others than 2-addr redefs _do_ trigger flag changes:
591 // - A def followed by a def is dead
592 // - A use followed by a def is a kill
Chris Lattner4dd81632010-03-31 05:15:22 +0000593 if (!MO.isReg() || !MO.getReg() || !MO.isDef()) continue;
594
595 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
596 last = LastUseDef.find(MO.getReg());
597 if (last != LastUseDef.end()) {
598 // Check if this is a two address instruction. If so, then
599 // the def does not kill the use.
600 if (last->second.first == I &&
601 I->isRegTiedToUseOperand(i))
602 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000603
Chris Lattner4dd81632010-03-31 05:15:22 +0000604 MachineOperand &lastUD =
605 last->second.first->getOperand(last->second.second);
606 if (lastUD.isDef())
607 lastUD.setIsDead(true);
608 else
609 lastUD.setIsKill(true);
Owen Anderson491fccc2008-07-08 22:24:50 +0000610 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000611
612 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000613 }
614 }
615
616 // Live-out (of the function) registers contain return values of the function,
617 // so we need to make sure they are alive at return time.
Bill Wendlingb0d27662010-03-16 02:01:51 +0000618 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
619 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
620
621 if (BBEndsInReturn)
Owen Anderson491fccc2008-07-08 22:24:50 +0000622 for (MachineRegisterInfo::liveout_iterator
623 I = MF->getRegInfo().liveout_begin(),
624 E = MF->getRegInfo().liveout_end(); I != E; ++I)
625 if (!Ret->readsRegister(*I)) {
626 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
627 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
628 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000629
630 // Finally, loop over the final use/def of each reg
631 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000632 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000633 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000634 MachineInstr *MI = I->second.first;
Owen Anderson491fccc2008-07-08 22:24:50 +0000635 unsigned idx = I->second.second;
Chris Lattner4dd81632010-03-31 05:15:22 +0000636 MachineOperand &MO = MI->getOperand(idx);
Owen Anderson491fccc2008-07-08 22:24:50 +0000637
638 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
639
640 // A crude approximation of "live-out" calculation
641 bool usedOutsideBlock = isPhysReg ? false :
642 UsedInMultipleBlocks.test(MO.getReg() -
643 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000644
645 // If the machine BB ends in a return instruction, then the value isn't used
646 // outside of the BB.
647 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000648 // DBG_VALUE complicates this: if the only refs of a register outside
649 // this block are DBG_VALUE, we can't keep the reg live just for that,
650 // as it will cause the reg to be spilled at the end of this block when
651 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
652 // happens.
653 bool UsedByDebugValueOnly = false;
Owen Anderson491fccc2008-07-08 22:24:50 +0000654 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
Bill Wendling8fe347a2010-03-16 01:05:35 +0000655 UE = MRI.reg_end(); UI != UE; ++UI) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000656 // Two cases:
657 // - used in another block
658 // - used in the same block before it is defined (loop)
Chris Lattner4dd81632010-03-31 05:15:22 +0000659 if (UI->getParent() == &MBB &&
660 !(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI)))
661 continue;
662
663 if (UI->isDebugValue()) {
664 UsedByDebugValueOnly = true;
665 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000666 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000667
668 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
669 UsedInMultipleBlocks.set(MO.getReg() -
670 TargetRegisterInfo::FirstVirtualRegister);
671 usedOutsideBlock = true;
672 UsedByDebugValueOnly = false;
673 break;
Bill Wendling8fe347a2010-03-16 01:05:35 +0000674 }
675
Dale Johannesenf463d952010-02-16 01:27:47 +0000676 if (UsedByDebugValueOnly)
677 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
678 UE = MRI.reg_end(); UI != UE; ++UI)
679 if (UI->isDebugValue() &&
680 (UI->getParent() != &MBB ||
681 (MO.isDef() && precedes(&*UI, MI))))
682 UI.getOperand().setReg(0U);
683 }
684
Bill Wendling8fe347a2010-03-16 01:05:35 +0000685 // Physical registers and those that are not live-out of the block are
686 // killed/dead at their last use/def within this block.
Dan Gohman15843902010-03-18 18:07:13 +0000687 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000688 if (MO.isUse()) {
689 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000690 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000691 MO.setIsKill(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000692 } else {
Owen Anderson491fccc2008-07-08 22:24:50 +0000693 MO.setIsDead(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000694 }
Dan Gohman15843902010-03-18 18:07:13 +0000695 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000696 }
Owen Anderson9094db12008-07-09 20:14:53 +0000697}
698
699void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
700 // loop over each instruction
701 MachineBasicBlock::iterator MII = MBB.begin();
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000702
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000703 DEBUG({
704 const BasicBlock *LBB = MBB.getBasicBlock();
705 if (LBB)
David Greene44248172010-01-05 01:26:05 +0000706 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000707 });
Owen Anderson9094db12008-07-09 20:14:53 +0000708
Evan Chengd5a48022009-01-29 18:37:30 +0000709 // Add live-in registers as active.
710 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000711 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000712 unsigned Reg = *I;
713 MF->getRegInfo().setPhysRegUsed(Reg);
714 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000715 MarkPhysRegRecentlyUsed(Reg);
Evan Chengd5a48022009-01-29 18:37:30 +0000716 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
717 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000718 if (PhysRegsUsed[*SubRegs] == -2) continue;
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000719 MarkPhysRegRecentlyUsed(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +0000720 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
721 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Evan Chengd5a48022009-01-29 18:37:30 +0000722 }
Owen Anderson9094db12008-07-09 20:14:53 +0000723 }
724
725 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000726
Chris Lattner44500e32006-06-15 22:21:53 +0000727 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000728 while (MII != MBB.end()) {
729 MachineInstr *MI = MII++;
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000730 ++InstrNum;
Chris Lattner749c6f62008-01-07 07:27:27 +0000731 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000732 DEBUG({
David Greene44248172010-01-05 01:26:05 +0000733 dbgs() << "\nStarting RegAlloc of: " << *MI;
734 dbgs() << " Regs have values: ";
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000735 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
736 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
David Greene44248172010-01-05 01:26:05 +0000737 dbgs() << "[" << TRI->getName(i)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000738 << ",%reg" << PhysRegsUsed[i] << "] ";
David Greene44248172010-01-05 01:26:05 +0000739 dbgs() << '\n';
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000740 });
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000741
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000742 // Determine whether this is a copy instruction. The cases where the
743 // source or destination are phys regs are handled specially.
744 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000745 unsigned SrcCopyPhysReg = 0U;
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000746 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
747 SrcCopySubReg, DstCopySubReg);
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000748 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
749 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000750
Chris Lattnerae640432002-12-17 02:50:10 +0000751 // Loop over the implicit uses, making sure that they are at the head of the
752 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000753 if (TID.ImplicitUses) {
754 for (const unsigned *ImplicitUses = TID.ImplicitUses;
755 *ImplicitUses; ++ImplicitUses)
756 MarkPhysRegRecentlyUsed(*ImplicitUses);
757 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000758
Evan Chengddee8422006-11-15 20:55:15 +0000759 SmallVector<unsigned, 8> Kills;
760 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000761 MachineOperand &MO = MI->getOperand(i);
762 if (!MO.isReg() || !MO.isKill()) continue;
763
764 if (!MO.isImplicit())
765 Kills.push_back(MO.getReg());
766 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
767 // These are extra physical register kills when a sub-register
768 // is defined (def of a sub-register is a read/mod/write of the
769 // larger registers). Ignore.
770 Kills.push_back(MO.getReg());
Evan Chengddee8422006-11-15 20:55:15 +0000771 }
772
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000773 // If any physical regs are earlyclobber, spill any value they might
774 // have in them, then mark them unallocatable.
775 // If any virtual regs are earlyclobber, allocate them now (before
776 // freeing inputs that are killed).
Chris Lattner518bb532010-02-09 19:54:29 +0000777 if (MI->isInlineAsm()) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000778 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
779 MachineOperand &MO = MI->getOperand(i);
780 if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber() ||
781 !MO.getReg())
782 continue;
783
784 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
785 unsigned DestVirtReg = MO.getReg();
786 unsigned DestPhysReg;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000787
Chris Lattner4dd81632010-03-31 05:15:22 +0000788 // If DestVirtReg already has a value, use it.
789 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
790 DestPhysReg = getReg(MBB, MI, DestVirtReg);
791 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
792 markVirtRegModified(DestVirtReg);
793 getVirtRegLastUse(DestVirtReg) =
794 std::make_pair((MachineInstr*)0, 0);
795 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
796 << " to %reg" << DestVirtReg << "\n");
797 MO.setReg(DestPhysReg); // Assign the earlyclobber register
798 } else {
799 unsigned Reg = MO.getReg();
800 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
801 // These are extra physical register defs when a sub-register
802 // is defined (def of a sub-register is a read/mod/write of the
803 // larger registers). Ignore.
804 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000805
Chris Lattner4dd81632010-03-31 05:15:22 +0000806 MF->getRegInfo().setPhysRegUsed(Reg);
807 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
808 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000809 MarkPhysRegRecentlyUsed(Reg);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000810
Chris Lattner4dd81632010-03-31 05:15:22 +0000811 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
812 *SubRegs; ++SubRegs) {
813 if (PhysRegsUsed[*SubRegs] == -2) continue;
814 MF->getRegInfo().setPhysRegUsed(*SubRegs);
815 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000816 MarkPhysRegRecentlyUsed(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000817 }
818 }
819 }
820 }
821
Dale Johannesen10fedd22010-02-10 00:11:11 +0000822 // If a DBG_VALUE says something is located in a spilled register,
823 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesenca134612010-01-30 00:57:47 +0000824 // from being reloaded here. Doing that would change the generated
825 // code, unless another use immediately follows this instruction.
Chris Lattner518bb532010-02-09 19:54:29 +0000826 if (MI->isDebugValue() &&
Dale Johannesenca134612010-01-30 00:57:47 +0000827 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
828 unsigned VirtReg = MI->getOperand(0).getReg();
829 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
830 !getVirt2PhysRegMapSlot(VirtReg))
831 MI->getOperand(0).setReg(0U);
832 }
833
Brian Gaeke53b99a02003-08-15 21:19:25 +0000834 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000835 // incoming values if we are out of registers. Note that we completely
836 // ignore physical register uses here. We assume that if an explicit
837 // physical register is referenced by the instruction, that it is guaranteed
838 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000839 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000840 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000841 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000842 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000843 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000844 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000845 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000846 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
847 isCopy ? DstCopyReg : 0);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000848 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000849
Evan Chengddee8422006-11-15 20:55:15 +0000850 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000851 // value, freeing the register being used, so it doesn't need to be
852 // spilled to memory.
853 //
Evan Chengddee8422006-11-15 20:55:15 +0000854 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
855 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000856 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000857 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000858 // If the virtual register was never materialized into a register, it
859 // might not be in the map, but it won't hurt to zero it out anyway.
860 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
861 PhysReg = PhysRegSlot;
862 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000863 } else if (PhysRegsUsed[PhysReg] == -2) {
864 // Unallocatable register dead, ignore.
865 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000866 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000867 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000868 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000869 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000870
Chris Lattner4dd81632010-03-31 05:15:22 +0000871 if (!PhysReg) continue;
872
873 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
874 << "[%reg" << VirtReg <<"], removing it from live set\n");
875 removePhysReg(PhysReg);
876 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
877 *SubRegs; ++SubRegs) {
878 if (PhysRegsUsed[*SubRegs] != -2) {
879 DEBUG(dbgs() << " Last use of "
880 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
881 <<"], removing it from live set\n");
882 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +0000883 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000884 }
885 }
886
887 // Loop over all of the operands of the instruction, spilling registers that
888 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000889 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000890 MachineOperand &MO = MI->getOperand(i);
891 if (!MO.isReg() || !MO.isDef() || MO.isImplicit() || !MO.getReg() ||
892 MO.isEarlyClobber() ||
893 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
894 continue;
895
896 unsigned Reg = MO.getReg();
897 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
898 // These are extra physical register defs when a sub-register
899 // is defined (def of a sub-register is a read/mod/write of the
900 // larger registers). Ignore.
901 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000902
Chris Lattner4dd81632010-03-31 05:15:22 +0000903 MF->getRegInfo().setPhysRegUsed(Reg);
904 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
905 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000906 MarkPhysRegRecentlyUsed(Reg);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000907
Chris Lattner4dd81632010-03-31 05:15:22 +0000908 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
909 *SubRegs; ++SubRegs) {
910 if (PhysRegsUsed[*SubRegs] == -2) continue;
911
912 MF->getRegInfo().setPhysRegUsed(*SubRegs);
913 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000914 MarkPhysRegRecentlyUsed(*SubRegs);
Chris Lattner91a452b2003-01-13 00:25:40 +0000915 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000916 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000917
918 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000919 if (TID.ImplicitDefs) {
920 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
921 *ImplicitDefs; ++ImplicitDefs) {
922 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000923 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000924 spillPhysReg(MBB, MI, Reg, true);
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000925 MarkPhysRegRecentlyUsed(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000926 PhysRegsUsed[Reg] = 0; // It is free and reserved now
927 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000928 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000929 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
930 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000931 if (PhysRegsUsed[*SubRegs] == -2) continue;
932
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +0000933 MarkPhysRegRecentlyUsed(*SubRegs);
Chris Lattner4dd81632010-03-31 05:15:22 +0000934 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
935 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Jim Laskeycd4317e2006-07-21 21:15:20 +0000936 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000937 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000938 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000939
Evan Chengddee8422006-11-15 20:55:15 +0000940 SmallVector<unsigned, 8> DeadDefs;
941 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000942 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000943 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +0000944 DeadDefs.push_back(MO.getReg());
945 }
946
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000947 // Okay, we have allocated all of the source operands and spilled any values
948 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000949 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000950 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000951 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000952 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000953 MachineOperand &MO = MI->getOperand(i);
954 if (!MO.isReg() || !MO.isDef() || !MO.getReg() ||
955 MO.isEarlyClobber() ||
956 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
957 continue;
958
959 unsigned DestVirtReg = MO.getReg();
960 unsigned DestPhysReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000961
Chris Lattner4dd81632010-03-31 05:15:22 +0000962 // If DestVirtReg already has a value, use it.
963 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
964 // If this is a copy try to reuse the input as the output;
965 // that will make the copy go away.
966 // If this is a copy, the source reg is a phys reg, and
967 // that reg is available, use that phys reg for DestPhysReg.
968 // If this is a copy, the source reg is a virtual reg, and
969 // the phys reg that was assigned to that virtual reg is now
970 // available, use that phys reg for DestPhysReg. (If it's now
971 // available that means this was the last use of the source.)
972 if (isCopy &&
973 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
974 isPhysRegAvailable(SrcCopyReg)) {
975 DestPhysReg = SrcCopyReg;
976 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
977 } else if (isCopy &&
978 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
979 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
980 MF->getRegInfo().getRegClass(DestVirtReg)->
981 contains(SrcCopyPhysReg)) {
982 DestPhysReg = SrcCopyPhysReg;
983 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
984 } else
985 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000986 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000987 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
988 markVirtRegModified(DestVirtReg);
989 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
990 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
991 << " to %reg" << DestVirtReg << "\n");
992 MO.setReg(DestPhysReg); // Assign the output register
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000993 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000994
Chris Lattner56ddada2004-02-17 17:49:10 +0000995 // If this instruction defines any registers that are immediately dead,
996 // kill them now.
997 //
Evan Chengddee8422006-11-15 20:55:15 +0000998 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
999 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +00001000 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001001 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +00001002 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1003 PhysReg = PhysRegSlot;
1004 assert(PhysReg != 0);
1005 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +00001006 } else if (PhysRegsUsed[PhysReg] == -2) {
1007 // Unallocatable register dead, ignore.
1008 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +00001009 } else if (!PhysReg)
1010 continue;
1011
1012 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
1013 << " [%reg" << VirtReg
1014 << "] is never used, removing it from live set\n");
1015 removePhysReg(PhysReg);
1016 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
1017 *AliasSet; ++AliasSet) {
1018 if (PhysRegsUsed[*AliasSet] != -2) {
1019 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
1020 << " [%reg" << *AliasSet
1021 << "] is never used, removing it from live set\n");
1022 removePhysReg(*AliasSet);
Evan Chengddee8422006-11-15 20:55:15 +00001023 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001024 }
1025 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001026
Bob Wilson9d928c22009-05-07 23:47:03 +00001027 // Finally, if this is a noop copy instruction, zap it. (Except that if
1028 // the copy is dead, it must be kept to avoid messing up liveness info for
1029 // the register scavenger. See pr4100.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001030 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1031 SrcCopySubReg, DstCopySubReg) &&
1032 SrcCopyReg == DstCopyReg && DeadDefs.empty())
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001033 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001034 }
1035
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001036 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001037
1038 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001039 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001040 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001041 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001042 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001043 else
1044 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001045 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001046
Chris Lattner9a5ef202005-11-09 05:28:45 +00001047#if 0
1048 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001049 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001050 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001051 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001052 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001053 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001054 AllOk = false;
1055 }
1056 assert(AllOk && "Virtual registers still in phys regs?");
1057#endif
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001058}
1059
1060/// runOnMachineFunction - Register allocate the whole function
1061///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001062bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene44248172010-01-05 01:26:05 +00001063 DEBUG(dbgs() << "Machine Function\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001064 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +00001065 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001066 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001067 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001068
Dan Gohman6f0d0242008-02-10 18:45:23 +00001069 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Jakob Stoklund Olesencf7fbd42010-04-16 23:32:37 +00001070 InstrNum = 0;
1071 PhysLastUse.assign(TRI->getNumRegs(), 0);
1072
Chris Lattner45d57882006-09-08 19:03:30 +00001073 // At various places we want to efficiently check to see whether a register
1074 // is allocatable. To handle this, we mark all unallocatable registers as
1075 // being pinned down, permanently.
1076 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001077 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001078 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1079 if (!Allocable[i])
1080 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1081 }
Chris Lattner64667b62004-02-09 01:26:13 +00001082
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001083 // initialize the virtual->physical register map to have a 'null'
1084 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001085 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001086 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001087 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001088 Virt2LastUseMap.grow(LastVirtReg);
Chris Lattner4dd81632010-03-31 05:15:22 +00001089 VirtRegModified.resize(LastVirtReg+1 -
1090 TargetRegisterInfo::FirstVirtualRegister);
1091 UsedInMultipleBlocks.resize(LastVirtReg+1 -
1092 TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001093
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001094 // Loop over all of the basic blocks, eliminating virtual register references
1095 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1096 MBB != MBBe; ++MBB)
1097 AllocateBasicBlock(*MBB);
1098
Chris Lattner580f9be2002-12-28 20:40:43 +00001099 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001100 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001101 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001102 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001103 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001104 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001105 return true;
1106}
1107
Chris Lattneref09c632004-01-31 21:27:19 +00001108FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001109 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001110}