blob: 1885fabab857b483daa4a6c75c6835b1b0949fd6 [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
79 // PhysRegsUseOrder - This contains a list of the physical registers that
80 // currently have a virtual register value in them. This list provides an
81 // ordering of registers, imposing a reallocation order. This list is only
82 // used if all registers are allocated and we have to spill one, in which
83 // case we spill the least recently used register. Entries at the front of
84 // the list are the least recently used registers, entries at the back are
85 // the most recently used.
86 //
87 std::vector<unsigned> PhysRegsUseOrder;
88
Evan Cheng839b7592008-01-17 02:08:17 +000089 // Virt2LastUseMap - This maps each virtual register to its last use
90 // (MachineInstr*, operand index pair).
91 IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
92 Virt2LastUseMap;
93
94 std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000095 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Evan Cheng839b7592008-01-17 02:08:17 +000096 return Virt2LastUseMap[Reg];
97 }
98
Chris Lattner91a452b2003-01-13 00:25:40 +000099 // VirtRegModified - This bitset contains information about which virtual
100 // registers need to be spilled back to memory when their registers are
101 // scavenged. If a virtual register has simply been rematerialized, there
102 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000103 //
Evan Cheng644340a2008-01-17 00:35:26 +0000104 BitVector VirtRegModified;
Owen Anderson491fccc2008-07-08 22:24:50 +0000105
106 // UsedInMultipleBlocks - Tracks whether a particular register is used in
107 // more than one block.
108 BitVector UsedInMultipleBlocks;
Chris Lattner91a452b2003-01-13 00:25:40 +0000109
110 void markVirtRegModified(unsigned Reg, bool Val = true) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000111 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
112 Reg -= TargetRegisterInfo::FirstVirtualRegister;
Evan Cheng644340a2008-01-17 00:35:26 +0000113 if (Val)
114 VirtRegModified.set(Reg);
115 else
116 VirtRegModified.reset(Reg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000117 }
118
119 bool isVirtRegModified(unsigned Reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Chris Lattner4dd81632010-03-31 05:15:22 +0000121 assert(Reg - TargetRegisterInfo::FirstVirtualRegister <
122 VirtRegModified.size() && "Illegal virtual register!");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000123 return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
Chris Lattner91a452b2003-01-13 00:25:40 +0000124 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000125
Evan Cheng7ac19af2007-06-26 21:05:13 +0000126 void AddToPhysRegsUseOrder(unsigned Reg) {
127 std::vector<unsigned>::iterator It =
128 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
129 if (It != PhysRegsUseOrder.end())
130 PhysRegsUseOrder.erase(It);
131 PhysRegsUseOrder.push_back(Reg);
132 }
133
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000134 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000135 if (PhysRegsUseOrder.empty() ||
136 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000137
Chris Lattner4dd81632010-03-31 05:15:22 +0000138 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) {
139 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
140 if (!areRegsEqual(Reg, RegMatch)) continue;
141
142 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
143 // Add it to the end of the list
144 PhysRegsUseOrder.push_back(RegMatch);
145 if (RegMatch == Reg)
146 return; // Found an exact match, exit early
147 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000148 }
149
150 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000151 virtual const char *getPassName() const {
152 return "Local Register Allocator";
153 }
154
Chris Lattner91a452b2003-01-13 00:25:40 +0000155 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000156 AU.setPreservesCFG();
Chris Lattner91a452b2003-01-13 00:25:40 +0000157 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000158 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000159 MachineFunctionPass::getAnalysisUsage(AU);
160 }
161
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000162 private:
163 /// runOnMachineFunction - Register allocate the whole function
164 bool runOnMachineFunction(MachineFunction &Fn);
165
166 /// AllocateBasicBlock - Register allocate the specified basic block.
167 void AllocateBasicBlock(MachineBasicBlock &MBB);
168
Chris Lattner82bee0f2002-12-18 08:14:26 +0000169
Chris Lattner82bee0f2002-12-18 08:14:26 +0000170 /// areRegsEqual - This method returns true if the specified registers are
171 /// related to each other. To do this, it checks to see if they are equal
172 /// or if the first register is in the alias set of the second register.
173 ///
174 bool areRegsEqual(unsigned R1, unsigned R2) const {
175 if (R1 == R2) return true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000176 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000177 *AliasSet; ++AliasSet) {
178 if (*AliasSet == R1) return true;
179 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000180 return false;
181 }
182
Chris Lattner580f9be2002-12-28 20:40:43 +0000183 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000184 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000185 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000186
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000187 /// removePhysReg - This method marks the specified physical register as no
188 /// longer being in use.
189 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000190 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000191
192 /// spillVirtReg - This method spills the value specified by PhysReg into
193 /// the virtual register slot specified by VirtReg. It then updates the RA
194 /// data structures to indicate the fact that PhysReg is now available.
195 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000196 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000197 unsigned VirtReg, unsigned PhysReg);
198
Chris Lattnerc21be922002-12-16 17:44:42 +0000199 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000200 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
201 /// true, then the request is ignored if the physical register does not
202 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000203 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000204 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000205 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000206
Chris Lattner91a452b2003-01-13 00:25:40 +0000207 /// assignVirtToPhysReg - This method updates local state so that we know
208 /// that PhysReg is the proper container for VirtReg now. The physical
209 /// register must not be used for anything else when this is called.
210 ///
211 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
212
Chris Lattnerae640432002-12-17 02:50:10 +0000213 /// isPhysRegAvailable - Return true if the specified physical register is
214 /// free and available for use. This also includes checking to see if
215 /// aliased registers are all free...
216 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000217 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000218
219 /// getFreeReg - Look to see if there is a free register available in the
220 /// specified register class. If not, return 0.
221 ///
222 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000223
Chris Lattner91a452b2003-01-13 00:25:40 +0000224 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000225 /// register. If all compatible physical registers are used, this method
226 /// spills the last used virtual register to the stack, and uses that
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000227 /// register. If NoFree is true, that means the caller knows there isn't
228 /// a free register, do not call getFreeReg().
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000229 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000230 unsigned VirtReg, bool NoFree = false);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000231
Bob Wilsone0f745b2009-05-07 21:19:45 +0000232 /// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000233 /// register use to refer to a physical register. This method may do this
234 /// in one of several ways: if the register is available in a physical
235 /// register already, it uses that physical register. If the value is not
236 /// in a physical register, and if there are physical registers available,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000237 /// it loads it into a register: PhysReg if that is an available physical
238 /// register, otherwise any physical register of the right class.
239 /// If register pressure is high, and it is possible, it tries to fold the
240 /// load of the virtual register into the instruction itself. It avoids
241 /// doing this if register pressure is low to improve the chance that
242 /// subsequent instructions can use the reloaded value. This method
243 /// returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000244 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000245 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000246 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
247 unsigned PhysReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000248
Owen Anderson9094db12008-07-09 20:14:53 +0000249 /// ComputeLocalLiveness - Computes liveness of registers within a basic
250 /// block, setting the killed/dead flags as appropriate.
251 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000252
253 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
254 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000255 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000256 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000257}
258
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000259/// getStackSpaceFor - This allocates space for the specified virtual register
260/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000261int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000262 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000263 int SS = StackSlotForVirtReg[VirtReg];
264 if (SS != -1)
265 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000266
Chris Lattner580f9be2002-12-28 20:40:43 +0000267 // Allocate a new stack object for this spill location...
David Greene3f2bf852009-11-12 20:49:22 +0000268 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
269 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000270
Chris Lattner4dd81632010-03-31 05:15:22 +0000271 // Assign the slot.
Evan Chengbdb10fe2008-07-10 18:23:23 +0000272 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000273 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000274}
275
Chris Lattnerae640432002-12-17 02:50:10 +0000276
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000277/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000278/// longer being in use.
279///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000280void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000281 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000282
283 std::vector<unsigned>::iterator It =
284 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000285 if (It != PhysRegsUseOrder.end())
286 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000287}
288
Chris Lattner91a452b2003-01-13 00:25:40 +0000289
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000290/// spillVirtReg - This method spills the value specified by PhysReg into the
291/// virtual register slot specified by VirtReg. It then updates the RA data
292/// structures to indicate the fact that PhysReg is now available.
293///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000294void RALocal::spillVirtReg(MachineBasicBlock &MBB,
295 MachineBasicBlock::iterator I,
296 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000297 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000298 " Must not have appropriate kill for the register or use exists beyond"
299 " the intended one.");
David Greene44248172010-01-05 01:26:05 +0000300 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000301 << " containing %reg" << VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000302
Evan Cheng839b7592008-01-17 02:08:17 +0000303 if (!isVirtRegModified(VirtReg)) {
David Greene44248172010-01-05 01:26:05 +0000304 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Cheng839b7592008-01-17 02:08:17 +0000305 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
306 if (LastUse.first)
307 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000308 } else {
309 // Otherwise, there is a virtual register corresponding to this physical
310 // register. We only need to spill it into its stack slot if it has been
311 // modified.
Chris Lattner84bc5422007-12-31 04:13:23 +0000312 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000313 int FrameIndex = getStackSpaceFor(VirtReg, RC);
David Greene44248172010-01-05 01:26:05 +0000314 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Cheng2fc628d2008-02-06 19:16:53 +0000315 // If the instruction reads the register that's spilled, (e.g. this can
316 // happen if it is a move to a physical register), then the spill
317 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000318 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Evan Cheng431bfcb2008-02-11 08:30:52 +0000319 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000320 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000321 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000322
323 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000324
David Greene44248172010-01-05 01:26:05 +0000325 DEBUG(dbgs() << '\n');
Chris Lattner82bee0f2002-12-18 08:14:26 +0000326 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000327}
328
Chris Lattnerae640432002-12-17 02:50:10 +0000329
Chris Lattner91a452b2003-01-13 00:25:40 +0000330/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000331/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
332/// then the request is ignored if the physical register does not contain a
333/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000334///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000335void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
336 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000337 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000338 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000339 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
340 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Chris Lattner4dd81632010-03-31 05:15:22 +0000341 return;
342 }
343
344 // If the selected register aliases any other registers, we must make
345 // sure that one of the aliases isn't alive.
346 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
347 *AliasSet; ++AliasSet) {
348 if (PhysRegsUsed[*AliasSet] == -1 || // Spill aliased register.
349 PhysRegsUsed[*AliasSet] == -2) // If allocatable.
350 continue;
351
352 if (PhysRegsUsed[*AliasSet])
353 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000354 }
355}
356
357
358/// assignVirtToPhysReg - This method updates local state so that we know
359/// that PhysReg is the proper container for VirtReg now. The physical
360/// register must not be used for anything else when this is called.
361///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000362void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000363 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000364 // Update information to note the fact that this register was just used, and
365 // it holds VirtReg.
366 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000367 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000368 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000369}
370
371
Chris Lattnerae640432002-12-17 02:50:10 +0000372/// isPhysRegAvailable - Return true if the specified physical register is free
373/// and available for use. This also includes checking to see if aliased
374/// registers are all free...
375///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000376bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000377 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000378
379 // If the selected register aliases any other allocated registers, it is
380 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000381 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000382 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000383 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000384 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000385 return true;
386}
387
388
Chris Lattner91a452b2003-01-13 00:25:40 +0000389/// getFreeReg - Look to see if there is a free register available in the
390/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000391///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000392unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000393 // Get iterators defining the range of registers that are valid to allocate in
394 // this class, which also specifies the preferred allocation order.
395 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
396 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000397
Chris Lattner91a452b2003-01-13 00:25:40 +0000398 for (; RI != RE; ++RI)
399 if (isPhysRegAvailable(*RI)) { // Is reg unused?
400 assert(*RI != 0 && "Cannot use register!");
401 return *RI; // Found an unused register!
402 }
403 return 0;
404}
405
406
Chris Lattner91a452b2003-01-13 00:25:40 +0000407/// getReg - Find a physical register to hold the specified virtual
408/// register. If all compatible physical registers are used, this method spills
409/// the last used virtual register to the stack, and uses that register.
410///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000411unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000412 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000413 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000414
415 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000416 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000417
Chris Lattner4dd81632010-03-31 05:15:22 +0000418 if (PhysReg != 0) {
419 // Assign the register.
420 assignVirtToPhysReg(VirtReg, PhysReg);
421 return PhysReg;
422 }
423
Chris Lattnerae640432002-12-17 02:50:10 +0000424 // If we didn't find an unused register, scavenge one now!
Chris Lattner4dd81632010-03-31 05:15:22 +0000425 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000426
Chris Lattner4dd81632010-03-31 05:15:22 +0000427 // Loop over all of the preallocated registers from the least recently used
428 // to the most recently used. When we find one that is capable of holding
429 // our register, use it.
430 for (unsigned i = 0; PhysReg == 0; ++i) {
431 assert(i != PhysRegsUseOrder.size() &&
432 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000433
Chris Lattner4dd81632010-03-31 05:15:22 +0000434 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000435
Chris Lattner4dd81632010-03-31 05:15:22 +0000436 // We can only use this register if it holds a virtual register (ie, it
437 // can be spilled). Do not use it if it is an explicitly allocated
438 // physical register!
439 assert(PhysRegsUsed[R] != -1 &&
440 "PhysReg in PhysRegsUseOrder, but is not allocated?");
441 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
442 // If the current register is compatible, use it.
443 if (RC->contains(R)) {
444 PhysReg = R;
445 break;
446 }
447
448 // If one of the registers aliased to the current register is
449 // compatible, use it.
450 for (const unsigned *AliasIt = TRI->getAliasSet(R);
451 *AliasIt; ++AliasIt) {
452 if (!RC->contains(*AliasIt)) continue;
453
454 // If this is pinned down for some reason, don't use it. For
455 // example, if CL is pinned, and we run across CH, don't use
456 // CH as justification for using scavenging ECX (which will
457 // fail).
458 if (PhysRegsUsed[*AliasIt] == 0) continue;
459
460 // Make sure the register is allocatable. Don't allocate SIL on
461 // x86-32.
462 if (PhysRegsUsed[*AliasIt] == -2) continue;
463
464 PhysReg = *AliasIt; // Take an aliased register
465 break;
Chris Lattnerae640432002-12-17 02:50:10 +0000466 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000467 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000468 }
469
Chris Lattner4dd81632010-03-31 05:15:22 +0000470 assert(PhysReg && "Physical register not assigned!?!?");
471
472 // At this point PhysRegsUseOrder[i] is the least recently used register of
473 // compatible register class. Spill it to memory and reap its remains.
474 spillPhysReg(MBB, I, PhysReg);
475
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000476 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000477 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000478 return PhysReg;
479}
480
Chris Lattnerae640432002-12-17 02:50:10 +0000481
Bob Wilson8d24f412009-05-07 21:20:42 +0000482/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000483/// register use to refer to a physical register. This method may do this in
484/// one of several ways: if the register is available in a physical register
485/// already, it uses that physical register. If the value is not in a physical
486/// register, and if there are physical registers available, it loads it into a
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000487/// register: PhysReg if that is an available physical register, otherwise any
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000488/// register. If register pressure is high, and it is possible, it tries to
489/// fold the load of the virtual register into the instruction itself. It
490/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000491/// subsequent instructions can use the reloaded value. This method returns
492/// the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000493///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000494MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000495 unsigned OpNum,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000496 SmallSet<unsigned, 4> &ReloadedRegs,
497 unsigned PhysReg) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000498 unsigned VirtReg = MI->getOperand(OpNum).getReg();
499
500 // If the virtual register is already available, just update the instruction
501 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000502 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattnere53f4a02006-05-04 17:52:23 +0000503 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesenf463d952010-02-16 01:27:47 +0000504 if (!MI->isDebugValue()) {
505 // Do not do these for DBG_VALUE as they can affect codegen.
506 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesen3da6e092010-02-15 01:45:47 +0000507 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesenf463d952010-02-16 01:27:47 +0000508 }
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000509 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000510 }
511
Chris Lattner1e3812c2004-02-17 04:08:37 +0000512 // Otherwise, we need to fold it into the current instruction, or reload it.
513 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000514 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000515 // If we already have a PhysReg (this happens when the instruction is a
516 // reg-to-reg copy with a PhysReg destination) use that.
517 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
518 !isPhysRegAvailable(PhysReg))
519 PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000520 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000521
Chris Lattner11390e72004-02-17 08:09:40 +0000522 if (PhysReg) { // Register is available, allocate it!
523 assignVirtToPhysReg(VirtReg, PhysReg);
524 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000525 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000526 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000527 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000528 }
529
Chris Lattner91a452b2003-01-13 00:25:40 +0000530 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
531
David Greene44248172010-01-05 01:26:05 +0000532 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000533 << TRI->getName(PhysReg) << "\n");
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000534
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000535 // Add move instruction(s)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000536 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000537 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000538
Chris Lattner84bc5422007-12-31 04:13:23 +0000539 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000540 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000541 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000542
543 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000544 std::string msg;
545 raw_string_ostream Msg(msg);
546 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000547 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000548 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000549 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000550 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000551 }
Chris Lattner75361b62010-04-07 22:58:41 +0000552 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000553 }
554 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
555 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000556 if (ReloadedRegs.insert(*SubRegs)) continue;
557
558 std::string msg;
559 raw_string_ostream Msg(msg);
560 Msg << "Ran out of registers during register allocation!";
561 if (MI->isInlineAsm()) {
562 Msg << "\nPlease check your inline asm statement for invalid "
563 << "constraints:\n";
564 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000565 }
Chris Lattner75361b62010-04-07 22:58:41 +0000566 report_fatal_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000567 }
568
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000569 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000570}
571
Evan Cheng7ac19af2007-06-26 21:05:13 +0000572/// isReadModWriteImplicitKill - True if this is an implicit kill for a
573/// read/mod/write register, i.e. update partial register.
574static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
575 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000576 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000577 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000578 MO.isDef() && !MO.isDead())
579 return true;
580 }
581 return false;
582}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000583
Evan Cheng7ac19af2007-06-26 21:05:13 +0000584/// isReadModWriteImplicitDef - True if this is an implicit def for a
585/// read/mod/write register, i.e. update partial register.
586static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
587 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000588 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000589 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000590 !MO.isDef() && MO.isKill())
591 return true;
592 }
593 return false;
594}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000595
Owen Anderson491fccc2008-07-08 22:24:50 +0000596// precedes - Helper function to determine with MachineInstr A
597// precedes MachineInstr B within the same MBB.
598static bool precedes(MachineBasicBlock::iterator A,
599 MachineBasicBlock::iterator B) {
600 if (A == B)
601 return false;
602
603 MachineBasicBlock::iterator I = A->getParent()->begin();
604 while (I != A->getParent()->end()) {
605 if (I == A)
606 return true;
607 else if (I == B)
608 return false;
609
610 ++I;
611 }
612
613 return false;
614}
615
Owen Anderson9094db12008-07-09 20:14:53 +0000616/// ComputeLocalLiveness - Computes liveness of registers within a basic
617/// block, setting the killed/dead flags as appropriate.
618void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000619 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Owen Anderson491fccc2008-07-08 22:24:50 +0000620 // Keep track of the most recently seen previous use or def of each reg,
621 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000622 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000623 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
624 I != E; ++I) {
Dale Johannesen3da6e092010-02-15 01:45:47 +0000625 if (I->isDebugValue())
626 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +0000627
Owen Anderson491fccc2008-07-08 22:24:50 +0000628 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000629 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000630 // Uses don't trigger any flags, but we need to save
631 // them for later. Also, we have to process these
632 // _before_ processing the defs, since an instr
633 // uses regs before it defs them.
Chris Lattner4dd81632010-03-31 05:15:22 +0000634 if (!MO.isReg() || !MO.getReg() || !MO.isUse())
635 continue;
636
637 LastUseDef[MO.getReg()] = std::make_pair(I, i);
638
639 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
640
641 const unsigned *Aliases = TRI->getAliasSet(MO.getReg());
642 if (Aliases == 0)
643 continue;
644
645 while (*Aliases) {
646 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
647 alias = LastUseDef.find(*Aliases);
Owen Anderson04764de2008-10-08 04:30:51 +0000648
Chris Lattner4dd81632010-03-31 05:15:22 +0000649 if (alias != LastUseDef.end() && alias->second.first != I)
650 LastUseDef[*Aliases] = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000651
Chris Lattner4dd81632010-03-31 05:15:22 +0000652 ++Aliases;
Owen Anderson04764de2008-10-08 04:30:51 +0000653 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000654 }
655
656 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000657 MachineOperand &MO = I->getOperand(i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000658 // Defs others than 2-addr redefs _do_ trigger flag changes:
659 // - A def followed by a def is dead
660 // - A use followed by a def is a kill
Chris Lattner4dd81632010-03-31 05:15:22 +0000661 if (!MO.isReg() || !MO.getReg() || !MO.isDef()) continue;
662
663 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
664 last = LastUseDef.find(MO.getReg());
665 if (last != LastUseDef.end()) {
666 // Check if this is a two address instruction. If so, then
667 // the def does not kill the use.
668 if (last->second.first == I &&
669 I->isRegTiedToUseOperand(i))
670 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000671
Chris Lattner4dd81632010-03-31 05:15:22 +0000672 MachineOperand &lastUD =
673 last->second.first->getOperand(last->second.second);
674 if (lastUD.isDef())
675 lastUD.setIsDead(true);
676 else
677 lastUD.setIsKill(true);
Owen Anderson491fccc2008-07-08 22:24:50 +0000678 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000679
680 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson491fccc2008-07-08 22:24:50 +0000681 }
682 }
683
684 // Live-out (of the function) registers contain return values of the function,
685 // so we need to make sure they are alive at return time.
Bill Wendlingb0d27662010-03-16 02:01:51 +0000686 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
687 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
688
689 if (BBEndsInReturn)
Owen Anderson491fccc2008-07-08 22:24:50 +0000690 for (MachineRegisterInfo::liveout_iterator
691 I = MF->getRegInfo().liveout_begin(),
692 E = MF->getRegInfo().liveout_end(); I != E; ++I)
693 if (!Ret->readsRegister(*I)) {
694 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
695 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
696 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000697
698 // Finally, loop over the final use/def of each reg
699 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000700 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000701 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000702 MachineInstr *MI = I->second.first;
Owen Anderson491fccc2008-07-08 22:24:50 +0000703 unsigned idx = I->second.second;
Chris Lattner4dd81632010-03-31 05:15:22 +0000704 MachineOperand &MO = MI->getOperand(idx);
Owen Anderson491fccc2008-07-08 22:24:50 +0000705
706 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
707
708 // A crude approximation of "live-out" calculation
709 bool usedOutsideBlock = isPhysReg ? false :
710 UsedInMultipleBlocks.test(MO.getReg() -
711 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000712
713 // If the machine BB ends in a return instruction, then the value isn't used
714 // outside of the BB.
715 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000716 // DBG_VALUE complicates this: if the only refs of a register outside
717 // this block are DBG_VALUE, we can't keep the reg live just for that,
718 // as it will cause the reg to be spilled at the end of this block when
719 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
720 // happens.
721 bool UsedByDebugValueOnly = false;
Owen Anderson491fccc2008-07-08 22:24:50 +0000722 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
Bill Wendling8fe347a2010-03-16 01:05:35 +0000723 UE = MRI.reg_end(); UI != UE; ++UI) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000724 // Two cases:
725 // - used in another block
726 // - used in the same block before it is defined (loop)
Chris Lattner4dd81632010-03-31 05:15:22 +0000727 if (UI->getParent() == &MBB &&
728 !(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI)))
729 continue;
730
731 if (UI->isDebugValue()) {
732 UsedByDebugValueOnly = true;
733 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000734 }
Chris Lattner4dd81632010-03-31 05:15:22 +0000735
736 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
737 UsedInMultipleBlocks.set(MO.getReg() -
738 TargetRegisterInfo::FirstVirtualRegister);
739 usedOutsideBlock = true;
740 UsedByDebugValueOnly = false;
741 break;
Bill Wendling8fe347a2010-03-16 01:05:35 +0000742 }
743
Dale Johannesenf463d952010-02-16 01:27:47 +0000744 if (UsedByDebugValueOnly)
745 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
746 UE = MRI.reg_end(); UI != UE; ++UI)
747 if (UI->isDebugValue() &&
748 (UI->getParent() != &MBB ||
749 (MO.isDef() && precedes(&*UI, MI))))
750 UI.getOperand().setReg(0U);
751 }
752
Bill Wendling8fe347a2010-03-16 01:05:35 +0000753 // Physical registers and those that are not live-out of the block are
754 // killed/dead at their last use/def within this block.
Dan Gohman15843902010-03-18 18:07:13 +0000755 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000756 if (MO.isUse()) {
757 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000758 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000759 MO.setIsKill(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000760 } else {
Owen Anderson491fccc2008-07-08 22:24:50 +0000761 MO.setIsDead(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000762 }
Dan Gohman15843902010-03-18 18:07:13 +0000763 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000764 }
Owen Anderson9094db12008-07-09 20:14:53 +0000765}
766
767void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
768 // loop over each instruction
769 MachineBasicBlock::iterator MII = MBB.begin();
770
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000771 DEBUG({
772 const BasicBlock *LBB = MBB.getBasicBlock();
773 if (LBB)
David Greene44248172010-01-05 01:26:05 +0000774 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000775 });
Owen Anderson9094db12008-07-09 20:14:53 +0000776
Evan Chengd5a48022009-01-29 18:37:30 +0000777 // Add live-in registers as active.
778 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000779 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000780 unsigned Reg = *I;
781 MF->getRegInfo().setPhysRegUsed(Reg);
782 PhysRegsUsed[Reg] = 0; // It is free and reserved now
783 AddToPhysRegsUseOrder(Reg);
784 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
785 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000786 if (PhysRegsUsed[*SubRegs] == -2) continue;
787
788 AddToPhysRegsUseOrder(*SubRegs);
789 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
790 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Evan Chengd5a48022009-01-29 18:37:30 +0000791 }
Owen Anderson9094db12008-07-09 20:14:53 +0000792 }
793
794 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000795
Chris Lattner44500e32006-06-15 22:21:53 +0000796 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000797 while (MII != MBB.end()) {
798 MachineInstr *MI = MII++;
Chris Lattner749c6f62008-01-07 07:27:27 +0000799 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000800 DEBUG({
David Greene44248172010-01-05 01:26:05 +0000801 dbgs() << "\nStarting RegAlloc of: " << *MI;
802 dbgs() << " Regs have values: ";
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000803 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
804 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
David Greene44248172010-01-05 01:26:05 +0000805 dbgs() << "[" << TRI->getName(i)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000806 << ",%reg" << PhysRegsUsed[i] << "] ";
David Greene44248172010-01-05 01:26:05 +0000807 dbgs() << '\n';
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000808 });
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000809
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000810 // Determine whether this is a copy instruction. The cases where the
811 // source or destination are phys regs are handled specially.
812 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000813 unsigned SrcCopyPhysReg = 0U;
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000814 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
815 SrcCopySubReg, DstCopySubReg);
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000816 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
817 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000818
Chris Lattnerae640432002-12-17 02:50:10 +0000819 // Loop over the implicit uses, making sure that they are at the head of the
820 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000821 if (TID.ImplicitUses) {
822 for (const unsigned *ImplicitUses = TID.ImplicitUses;
823 *ImplicitUses; ++ImplicitUses)
824 MarkPhysRegRecentlyUsed(*ImplicitUses);
825 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000826
Evan Chengddee8422006-11-15 20:55:15 +0000827 SmallVector<unsigned, 8> Kills;
828 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000829 MachineOperand &MO = MI->getOperand(i);
830 if (!MO.isReg() || !MO.isKill()) continue;
831
832 if (!MO.isImplicit())
833 Kills.push_back(MO.getReg());
834 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
835 // These are extra physical register kills when a sub-register
836 // is defined (def of a sub-register is a read/mod/write of the
837 // larger registers). Ignore.
838 Kills.push_back(MO.getReg());
Evan Chengddee8422006-11-15 20:55:15 +0000839 }
840
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000841 // If any physical regs are earlyclobber, spill any value they might
842 // have in them, then mark them unallocatable.
843 // If any virtual regs are earlyclobber, allocate them now (before
844 // freeing inputs that are killed).
Chris Lattner518bb532010-02-09 19:54:29 +0000845 if (MI->isInlineAsm()) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000846 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
847 MachineOperand &MO = MI->getOperand(i);
848 if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber() ||
849 !MO.getReg())
850 continue;
851
852 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
853 unsigned DestVirtReg = MO.getReg();
854 unsigned DestPhysReg;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000855
Chris Lattner4dd81632010-03-31 05:15:22 +0000856 // If DestVirtReg already has a value, use it.
857 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
858 DestPhysReg = getReg(MBB, MI, DestVirtReg);
859 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
860 markVirtRegModified(DestVirtReg);
861 getVirtRegLastUse(DestVirtReg) =
862 std::make_pair((MachineInstr*)0, 0);
863 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
864 << " to %reg" << DestVirtReg << "\n");
865 MO.setReg(DestPhysReg); // Assign the earlyclobber register
866 } else {
867 unsigned Reg = MO.getReg();
868 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
869 // These are extra physical register defs when a sub-register
870 // is defined (def of a sub-register is a read/mod/write of the
871 // larger registers). Ignore.
872 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000873
Chris Lattner4dd81632010-03-31 05:15:22 +0000874 MF->getRegInfo().setPhysRegUsed(Reg);
875 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
876 PhysRegsUsed[Reg] = 0; // It is free and reserved now
877 AddToPhysRegsUseOrder(Reg);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000878
Chris Lattner4dd81632010-03-31 05:15:22 +0000879 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
880 *SubRegs; ++SubRegs) {
881 if (PhysRegsUsed[*SubRegs] == -2) continue;
882 MF->getRegInfo().setPhysRegUsed(*SubRegs);
883 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
884 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000885 }
886 }
887 }
888 }
889
Dale Johannesen10fedd22010-02-10 00:11:11 +0000890 // If a DBG_VALUE says something is located in a spilled register,
891 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesenca134612010-01-30 00:57:47 +0000892 // from being reloaded here. Doing that would change the generated
893 // code, unless another use immediately follows this instruction.
Chris Lattner518bb532010-02-09 19:54:29 +0000894 if (MI->isDebugValue() &&
Dale Johannesenca134612010-01-30 00:57:47 +0000895 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
896 unsigned VirtReg = MI->getOperand(0).getReg();
897 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
898 !getVirt2PhysRegMapSlot(VirtReg))
899 MI->getOperand(0).setReg(0U);
900 }
901
Brian Gaeke53b99a02003-08-15 21:19:25 +0000902 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000903 // incoming values if we are out of registers. Note that we completely
904 // ignore physical register uses here. We assume that if an explicit
905 // physical register is referenced by the instruction, that it is guaranteed
906 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000907 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000908 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000909 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000910 MachineOperand &MO = MI->getOperand(i);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000911 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000912 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000913 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000914 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
915 isCopy ? DstCopyReg : 0);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000916 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000917
Evan Chengddee8422006-11-15 20:55:15 +0000918 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000919 // value, freeing the register being used, so it doesn't need to be
920 // spilled to memory.
921 //
Evan Chengddee8422006-11-15 20:55:15 +0000922 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
923 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000924 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000925 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000926 // If the virtual register was never materialized into a register, it
927 // might not be in the map, but it won't hurt to zero it out anyway.
928 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
929 PhysReg = PhysRegSlot;
930 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000931 } else if (PhysRegsUsed[PhysReg] == -2) {
932 // Unallocatable register dead, ignore.
933 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000934 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000935 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000936 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000937 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000938
Chris Lattner4dd81632010-03-31 05:15:22 +0000939 if (!PhysReg) continue;
940
941 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
942 << "[%reg" << VirtReg <<"], removing it from live set\n");
943 removePhysReg(PhysReg);
944 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
945 *SubRegs; ++SubRegs) {
946 if (PhysRegsUsed[*SubRegs] != -2) {
947 DEBUG(dbgs() << " Last use of "
948 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
949 <<"], removing it from live set\n");
950 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +0000951 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000952 }
953 }
954
955 // Loop over all of the operands of the instruction, spilling registers that
956 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000957 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000958 MachineOperand &MO = MI->getOperand(i);
959 if (!MO.isReg() || !MO.isDef() || MO.isImplicit() || !MO.getReg() ||
960 MO.isEarlyClobber() ||
961 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
962 continue;
963
964 unsigned Reg = MO.getReg();
965 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
966 // These are extra physical register defs when a sub-register
967 // is defined (def of a sub-register is a read/mod/write of the
968 // larger registers). Ignore.
969 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000970
Chris Lattner4dd81632010-03-31 05:15:22 +0000971 MF->getRegInfo().setPhysRegUsed(Reg);
972 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
973 PhysRegsUsed[Reg] = 0; // It is free and reserved now
974 AddToPhysRegsUseOrder(Reg);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000975
Chris Lattner4dd81632010-03-31 05:15:22 +0000976 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
977 *SubRegs; ++SubRegs) {
978 if (PhysRegsUsed[*SubRegs] == -2) continue;
979
980 MF->getRegInfo().setPhysRegUsed(*SubRegs);
981 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
982 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner91a452b2003-01-13 00:25:40 +0000983 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000984 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000985
986 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000987 if (TID.ImplicitDefs) {
988 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
989 *ImplicitDefs; ++ImplicitDefs) {
990 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000991 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000992 spillPhysReg(MBB, MI, Reg, true);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000993 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000994 PhysRegsUsed[Reg] = 0; // It is free and reserved now
995 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000996 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000997 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
998 *SubRegs; ++SubRegs) {
Chris Lattner4dd81632010-03-31 05:15:22 +0000999 if (PhysRegsUsed[*SubRegs] == -2) continue;
1000
1001 AddToPhysRegsUseOrder(*SubRegs);
1002 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
1003 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Jim Laskeycd4317e2006-07-21 21:15:20 +00001004 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +00001005 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +00001006 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001007
Evan Chengddee8422006-11-15 20:55:15 +00001008 SmallVector<unsigned, 8> DeadDefs;
1009 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001010 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001011 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +00001012 DeadDefs.push_back(MO.getReg());
1013 }
1014
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001015 // Okay, we have allocated all of the source operands and spilled any values
1016 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +00001017 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +00001018 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +00001019 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001020 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
Chris Lattner4dd81632010-03-31 05:15:22 +00001021 MachineOperand &MO = MI->getOperand(i);
1022 if (!MO.isReg() || !MO.isDef() || !MO.getReg() ||
1023 MO.isEarlyClobber() ||
1024 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1025 continue;
1026
1027 unsigned DestVirtReg = MO.getReg();
1028 unsigned DestPhysReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001029
Chris Lattner4dd81632010-03-31 05:15:22 +00001030 // If DestVirtReg already has a value, use it.
1031 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
1032 // If this is a copy try to reuse the input as the output;
1033 // that will make the copy go away.
1034 // If this is a copy, the source reg is a phys reg, and
1035 // that reg is available, use that phys reg for DestPhysReg.
1036 // If this is a copy, the source reg is a virtual reg, and
1037 // the phys reg that was assigned to that virtual reg is now
1038 // available, use that phys reg for DestPhysReg. (If it's now
1039 // available that means this was the last use of the source.)
1040 if (isCopy &&
1041 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
1042 isPhysRegAvailable(SrcCopyReg)) {
1043 DestPhysReg = SrcCopyReg;
1044 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1045 } else if (isCopy &&
1046 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
1047 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
1048 MF->getRegInfo().getRegClass(DestVirtReg)->
1049 contains(SrcCopyPhysReg)) {
1050 DestPhysReg = SrcCopyPhysReg;
1051 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
1052 } else
1053 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001054 }
Chris Lattner4dd81632010-03-31 05:15:22 +00001055 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
1056 markVirtRegModified(DestVirtReg);
1057 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
1058 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
1059 << " to %reg" << DestVirtReg << "\n");
1060 MO.setReg(DestPhysReg); // Assign the output register
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001061 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001062
Chris Lattner56ddada2004-02-17 17:49:10 +00001063 // If this instruction defines any registers that are immediately dead,
1064 // kill them now.
1065 //
Evan Chengddee8422006-11-15 20:55:15 +00001066 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
1067 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +00001068 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001069 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +00001070 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1071 PhysReg = PhysRegSlot;
1072 assert(PhysReg != 0);
1073 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +00001074 } else if (PhysRegsUsed[PhysReg] == -2) {
1075 // Unallocatable register dead, ignore.
1076 continue;
Chris Lattner4dd81632010-03-31 05:15:22 +00001077 } else if (!PhysReg)
1078 continue;
1079
1080 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
1081 << " [%reg" << VirtReg
1082 << "] is never used, removing it from live set\n");
1083 removePhysReg(PhysReg);
1084 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
1085 *AliasSet; ++AliasSet) {
1086 if (PhysRegsUsed[*AliasSet] != -2) {
1087 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
1088 << " [%reg" << *AliasSet
1089 << "] is never used, removing it from live set\n");
1090 removePhysReg(*AliasSet);
Evan Chengddee8422006-11-15 20:55:15 +00001091 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001092 }
1093 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001094
Bob Wilson9d928c22009-05-07 23:47:03 +00001095 // Finally, if this is a noop copy instruction, zap it. (Except that if
1096 // the copy is dead, it must be kept to avoid messing up liveness info for
1097 // the register scavenger. See pr4100.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001098 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1099 SrcCopySubReg, DstCopySubReg) &&
1100 SrcCopyReg == DstCopyReg && DeadDefs.empty())
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001101 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001102 }
1103
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001104 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001105
1106 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001107 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001108 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001109 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001110 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001111 else
1112 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001113 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001114
Chris Lattner9a5ef202005-11-09 05:28:45 +00001115#if 0
1116 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001117 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001118 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001119 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001120 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001121 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001122 AllOk = false;
1123 }
1124 assert(AllOk && "Virtual registers still in phys regs?");
1125#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001126
Chris Lattner128c2aa2003-08-17 18:01:15 +00001127 // Clear any physical register which appear live at the end of the basic
1128 // block, but which do not hold any virtual registers. e.g., the stack
1129 // pointer.
1130 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001131}
1132
1133/// runOnMachineFunction - Register allocate the whole function
1134///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001135bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene44248172010-01-05 01:26:05 +00001136 DEBUG(dbgs() << "Machine Function\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001137 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +00001138 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001139 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001140 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001141
Dan Gohman6f0d0242008-02-10 18:45:23 +00001142 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +00001143
1144 // At various places we want to efficiently check to see whether a register
1145 // is allocatable. To handle this, we mark all unallocatable registers as
1146 // being pinned down, permanently.
1147 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001148 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001149 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1150 if (!Allocable[i])
1151 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1152 }
Chris Lattner64667b62004-02-09 01:26:13 +00001153
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001154 // initialize the virtual->physical register map to have a 'null'
1155 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001156 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001157 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001158 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001159 Virt2LastUseMap.grow(LastVirtReg);
Chris Lattner4dd81632010-03-31 05:15:22 +00001160 VirtRegModified.resize(LastVirtReg+1 -
1161 TargetRegisterInfo::FirstVirtualRegister);
1162 UsedInMultipleBlocks.resize(LastVirtReg+1 -
1163 TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001164
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001165 // Loop over all of the basic blocks, eliminating virtual register references
1166 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1167 MBB != MBBe; ++MBB)
1168 AllocateBasicBlock(*MBB);
1169
Chris Lattner580f9be2002-12-28 20:40:43 +00001170 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001171 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001172 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001173 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001174 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001175 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001176 return true;
1177}
1178
Chris Lattneref09c632004-01-31 21:27:19 +00001179FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001180 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001181}