blob: 2c69065b14274c0ccd55c3edeafc7d9b0a00dfee [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!");
121 assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000122 && "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
138 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000139 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
140 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
141 PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
142 // Add it to the end of the list
143 PhysRegsUseOrder.push_back(RegMatch);
144 if (RegMatch == Reg)
145 return; // Found an exact match, exit early
146 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000147 }
148
149 public:
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000150 virtual const char *getPassName() const {
151 return "Local Register Allocator";
152 }
153
Chris Lattner91a452b2003-01-13 00:25:40 +0000154 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000155 AU.setPreservesCFG();
Chris Lattner91a452b2003-01-13 00:25:40 +0000156 AU.addRequiredID(PHIEliminationID);
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000157 AU.addRequiredID(TwoAddressInstructionPassID);
Chris Lattner91a452b2003-01-13 00:25:40 +0000158 MachineFunctionPass::getAnalysisUsage(AU);
159 }
160
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000161 private:
162 /// runOnMachineFunction - Register allocate the whole function
163 bool runOnMachineFunction(MachineFunction &Fn);
164
165 /// AllocateBasicBlock - Register allocate the specified basic block.
166 void AllocateBasicBlock(MachineBasicBlock &MBB);
167
Chris Lattner82bee0f2002-12-18 08:14:26 +0000168
Chris Lattner82bee0f2002-12-18 08:14:26 +0000169 /// areRegsEqual - This method returns true if the specified registers are
170 /// related to each other. To do this, it checks to see if they are equal
171 /// or if the first register is in the alias set of the second register.
172 ///
173 bool areRegsEqual(unsigned R1, unsigned R2) const {
174 if (R1 == R2) return true;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000175 for (const unsigned *AliasSet = TRI->getAliasSet(R2);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000176 *AliasSet; ++AliasSet) {
177 if (*AliasSet == R1) return true;
178 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000179 return false;
180 }
181
Chris Lattner580f9be2002-12-28 20:40:43 +0000182 /// getStackSpaceFor - This returns the frame index of the specified virtual
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000183 /// register on the stack, allocating space if necessary.
Chris Lattner580f9be2002-12-28 20:40:43 +0000184 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000185
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000186 /// removePhysReg - This method marks the specified physical register as no
187 /// longer being in use.
188 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000189 void removePhysReg(unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000190
191 /// spillVirtReg - This method spills the value specified by PhysReg into
192 /// the virtual register slot specified by VirtReg. It then updates the RA
193 /// data structures to indicate the fact that PhysReg is now available.
194 ///
Chris Lattner688c8252004-02-22 19:08:15 +0000195 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000196 unsigned VirtReg, unsigned PhysReg);
197
Chris Lattnerc21be922002-12-16 17:44:42 +0000198 /// spillPhysReg - This method spills the specified physical register into
Chris Lattner128c2aa2003-08-17 18:01:15 +0000199 /// the virtual register slot associated with it. If OnlyVirtRegs is set to
200 /// true, then the request is ignored if the physical register does not
201 /// contain a virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000202 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000203 void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
Chris Lattner128c2aa2003-08-17 18:01:15 +0000204 unsigned PhysReg, bool OnlyVirtRegs = false);
Chris Lattnerc21be922002-12-16 17:44:42 +0000205
Chris Lattner91a452b2003-01-13 00:25:40 +0000206 /// assignVirtToPhysReg - This method updates local state so that we know
207 /// that PhysReg is the proper container for VirtReg now. The physical
208 /// register must not be used for anything else when this is called.
209 ///
210 void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
211
Chris Lattnerae640432002-12-17 02:50:10 +0000212 /// isPhysRegAvailable - Return true if the specified physical register is
213 /// free and available for use. This also includes checking to see if
214 /// aliased registers are all free...
215 ///
Chris Lattner82bee0f2002-12-18 08:14:26 +0000216 bool isPhysRegAvailable(unsigned PhysReg) const;
Chris Lattner91a452b2003-01-13 00:25:40 +0000217
218 /// getFreeReg - Look to see if there is a free register available in the
219 /// specified register class. If not, return 0.
220 ///
221 unsigned getFreeReg(const TargetRegisterClass *RC);
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000222
Chris Lattner91a452b2003-01-13 00:25:40 +0000223 /// getReg - Find a physical register to hold the specified virtual
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000224 /// register. If all compatible physical registers are used, this method
225 /// spills the last used virtual register to the stack, and uses that
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000226 /// register. If NoFree is true, that means the caller knows there isn't
227 /// a free register, do not call getFreeReg().
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000228 unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000229 unsigned VirtReg, bool NoFree = false);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000230
Bob Wilsone0f745b2009-05-07 21:19:45 +0000231 /// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000232 /// register use to refer to a physical register. This method may do this
233 /// in one of several ways: if the register is available in a physical
234 /// register already, it uses that physical register. If the value is not
235 /// in a physical register, and if there are physical registers available,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000236 /// it loads it into a register: PhysReg if that is an available physical
237 /// register, otherwise any physical register of the right class.
238 /// If register pressure is high, and it is possible, it tries to fold the
239 /// load of the virtual register into the instruction itself. It avoids
240 /// doing this if register pressure is low to improve the chance that
241 /// subsequent instructions can use the reloaded value. This method
242 /// returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000243 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000244 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000245 unsigned OpNum, SmallSet<unsigned, 4> &RRegs,
246 unsigned PhysReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000247
Owen Anderson9094db12008-07-09 20:14:53 +0000248 /// ComputeLocalLiveness - Computes liveness of registers within a basic
249 /// block, setting the killed/dead flags as appropriate.
250 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000251
252 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
253 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000254 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000255 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000256}
257
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000258/// getStackSpaceFor - This allocates space for the specified virtual register
259/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000260int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000261 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000262 int SS = StackSlotForVirtReg[VirtReg];
263 if (SS != -1)
264 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000265
Chris Lattner580f9be2002-12-28 20:40:43 +0000266 // Allocate a new stack object for this spill location...
David Greene3f2bf852009-11-12 20:49:22 +0000267 int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
268 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000269
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000270 // Assign the slot...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000271 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000272 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000273}
274
Chris Lattnerae640432002-12-17 02:50:10 +0000275
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000276/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000277/// longer being in use.
278///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000279void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000280 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000281
282 std::vector<unsigned>::iterator It =
283 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000284 if (It != PhysRegsUseOrder.end())
285 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000286}
287
Chris Lattner91a452b2003-01-13 00:25:40 +0000288
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000289/// spillVirtReg - This method spills the value specified by PhysReg into the
290/// virtual register slot specified by VirtReg. It then updates the RA data
291/// structures to indicate the fact that PhysReg is now available.
292///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000293void RALocal::spillVirtReg(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator I,
295 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000296 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000297 " Must not have appropriate kill for the register or use exists beyond"
298 " the intended one.");
David Greene44248172010-01-05 01:26:05 +0000299 DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000300 << " containing %reg" << VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000301
Evan Cheng839b7592008-01-17 02:08:17 +0000302 if (!isVirtRegModified(VirtReg)) {
David Greene44248172010-01-05 01:26:05 +0000303 DEBUG(dbgs() << " which has not been modified, so no store necessary!");
Evan Cheng839b7592008-01-17 02:08:17 +0000304 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
305 if (LastUse.first)
306 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000307 } else {
308 // Otherwise, there is a virtual register corresponding to this physical
309 // register. We only need to spill it into its stack slot if it has been
310 // modified.
Chris Lattner84bc5422007-12-31 04:13:23 +0000311 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000312 int FrameIndex = getStackSpaceFor(VirtReg, RC);
David Greene44248172010-01-05 01:26:05 +0000313 DEBUG(dbgs() << " to stack slot #" << FrameIndex);
Evan Cheng2fc628d2008-02-06 19:16:53 +0000314 // If the instruction reads the register that's spilled, (e.g. this can
315 // happen if it is a move to a physical register), then the spill
316 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000317 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Evan Cheng431bfcb2008-02-11 08:30:52 +0000318 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000319 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000320 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000321
322 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000323
David Greene44248172010-01-05 01:26:05 +0000324 DEBUG(dbgs() << '\n');
Chris Lattner82bee0f2002-12-18 08:14:26 +0000325 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000326}
327
Chris Lattnerae640432002-12-17 02:50:10 +0000328
Chris Lattner91a452b2003-01-13 00:25:40 +0000329/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000330/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
331/// then the request is ignored if the physical register does not contain a
332/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000333///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000334void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
335 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000336 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000337 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000338 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
339 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000340 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000341 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000342 // sure that one of the aliases isn't alive.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000343 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000344 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000345 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
346 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000347 if (PhysRegsUsed[*AliasSet])
348 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000349 }
350}
351
352
353/// assignVirtToPhysReg - This method updates local state so that we know
354/// that PhysReg is the proper container for VirtReg now. The physical
355/// register must not be used for anything else when this is called.
356///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000357void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000358 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000359 // Update information to note the fact that this register was just used, and
360 // it holds VirtReg.
361 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000362 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000363 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000364}
365
366
Chris Lattnerae640432002-12-17 02:50:10 +0000367/// isPhysRegAvailable - Return true if the specified physical register is free
368/// and available for use. This also includes checking to see if aliased
369/// registers are all free...
370///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000371bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000372 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000373
374 // If the selected register aliases any other allocated registers, it is
375 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000376 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000377 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000378 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000379 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000380 return true;
381}
382
383
Chris Lattner91a452b2003-01-13 00:25:40 +0000384/// getFreeReg - Look to see if there is a free register available in the
385/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000386///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000387unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000388 // Get iterators defining the range of registers that are valid to allocate in
389 // this class, which also specifies the preferred allocation order.
390 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
391 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000392
Chris Lattner91a452b2003-01-13 00:25:40 +0000393 for (; RI != RE; ++RI)
394 if (isPhysRegAvailable(*RI)) { // Is reg unused?
395 assert(*RI != 0 && "Cannot use register!");
396 return *RI; // Found an unused register!
397 }
398 return 0;
399}
400
401
Chris Lattner91a452b2003-01-13 00:25:40 +0000402/// getReg - Find a physical register to hold the specified virtual
403/// register. If all compatible physical registers are used, this method spills
404/// the last used virtual register to the stack, and uses that register.
405///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000406unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000407 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000408 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000409
410 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000411 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000412
Chris Lattnerae640432002-12-17 02:50:10 +0000413 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000414 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000415 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000416
417 // Loop over all of the preallocated registers from the least recently used
418 // to the most recently used. When we find one that is capable of holding
419 // our register, use it.
420 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000421 assert(i != PhysRegsUseOrder.size() &&
422 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000423
Chris Lattnerae640432002-12-17 02:50:10 +0000424 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000425
426 // We can only use this register if it holds a virtual register (ie, it
427 // can be spilled). Do not use it if it is an explicitly allocated
428 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000429 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000430 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000431 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000432 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000433 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000434 PhysReg = R;
435 break;
436 } else {
437 // If one of the registers aliased to the current register is
438 // compatible, use it.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000439 for (const unsigned *AliasIt = TRI->getAliasSet(R);
Chris Lattner5e503492006-09-03 07:15:37 +0000440 *AliasIt; ++AliasIt) {
441 if (RC->contains(*AliasIt) &&
442 // If this is pinned down for some reason, don't use it. For
443 // example, if CL is pinned, and we run across CH, don't use
444 // CH as justification for using scavenging ECX (which will
445 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000446 PhysRegsUsed[*AliasIt] != 0 &&
447
448 // Make sure the register is allocatable. Don't allocate SIL on
449 // x86-32.
450 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000451 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000452 break;
453 }
454 }
Chris Lattner41822c72003-08-23 23:49:42 +0000455 }
Chris Lattnerae640432002-12-17 02:50:10 +0000456 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000457 }
458
Chris Lattnerae640432002-12-17 02:50:10 +0000459 assert(PhysReg && "Physical register not assigned!?!?");
460
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000461 // At this point PhysRegsUseOrder[i] is the least recently used register of
462 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000463 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000464 }
465
466 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000467 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000468 return PhysReg;
469}
470
Chris Lattnerae640432002-12-17 02:50:10 +0000471
Bob Wilson8d24f412009-05-07 21:20:42 +0000472/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000473/// register use to refer to a physical register. This method may do this in
474/// one of several ways: if the register is available in a physical register
475/// already, it uses that physical register. If the value is not in a physical
476/// register, and if there are physical registers available, it loads it into a
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000477/// register: PhysReg if that is an available physical register, otherwise any
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000478/// register. If register pressure is high, and it is possible, it tries to
479/// fold the load of the virtual register into the instruction itself. It
480/// avoids doing this if register pressure is low to improve the chance that
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000481/// subsequent instructions can use the reloaded value. This method returns
482/// the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000483///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000484MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000485 unsigned OpNum,
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000486 SmallSet<unsigned, 4> &ReloadedRegs,
487 unsigned PhysReg) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000488 unsigned VirtReg = MI->getOperand(OpNum).getReg();
489
490 // If the virtual register is already available, just update the instruction
491 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000492 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Chris Lattnere53f4a02006-05-04 17:52:23 +0000493 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Dale Johannesenf463d952010-02-16 01:27:47 +0000494 if (!MI->isDebugValue()) {
495 // Do not do these for DBG_VALUE as they can affect codegen.
496 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Dale Johannesen3da6e092010-02-15 01:45:47 +0000497 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Dale Johannesenf463d952010-02-16 01:27:47 +0000498 }
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000499 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000500 }
501
Chris Lattner1e3812c2004-02-17 04:08:37 +0000502 // Otherwise, we need to fold it into the current instruction, or reload it.
503 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000504 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000505 // If we already have a PhysReg (this happens when the instruction is a
506 // reg-to-reg copy with a PhysReg destination) use that.
507 if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) ||
508 !isPhysRegAvailable(PhysReg))
509 PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000510 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000511
Chris Lattner11390e72004-02-17 08:09:40 +0000512 if (PhysReg) { // Register is available, allocate it!
513 assignVirtToPhysReg(VirtReg, PhysReg);
514 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000515 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000516 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000517 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000518 }
519
Chris Lattner91a452b2003-01-13 00:25:40 +0000520 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
521
David Greene44248172010-01-05 01:26:05 +0000522 DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000523 << TRI->getName(PhysReg) << "\n");
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000524
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000525 // Add move instruction(s)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000526 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000527 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000528
Chris Lattner84bc5422007-12-31 04:13:23 +0000529 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000530 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000531 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000532
533 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000534 std::string msg;
535 raw_string_ostream Msg(msg);
536 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000537 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000538 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000539 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000540 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000541 }
Torok Edwin7d696d82009-07-11 13:10:19 +0000542 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000543 }
544 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
545 *SubRegs; ++SubRegs) {
546 if (!ReloadedRegs.insert(*SubRegs)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000547 std::string msg;
548 raw_string_ostream Msg(msg);
549 Msg << "Ran out of registers during register allocation!";
Chris Lattner518bb532010-02-09 19:54:29 +0000550 if (MI->isInlineAsm()) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000551 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000552 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000553 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000554 }
Torok Edwin7d696d82009-07-11 13:10:19 +0000555 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000556 }
557 }
558
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000559 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000560}
561
Evan Cheng7ac19af2007-06-26 21:05:13 +0000562/// isReadModWriteImplicitKill - True if this is an implicit kill for a
563/// read/mod/write register, i.e. update partial register.
564static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
565 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
566 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000567 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000568 MO.isDef() && !MO.isDead())
569 return true;
570 }
571 return false;
572}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000573
Evan Cheng7ac19af2007-06-26 21:05:13 +0000574/// isReadModWriteImplicitDef - True if this is an implicit def for a
575/// read/mod/write register, i.e. update partial register.
576static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
577 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
578 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000579 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000580 !MO.isDef() && MO.isKill())
581 return true;
582 }
583 return false;
584}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000585
Owen Anderson491fccc2008-07-08 22:24:50 +0000586// precedes - Helper function to determine with MachineInstr A
587// precedes MachineInstr B within the same MBB.
588static bool precedes(MachineBasicBlock::iterator A,
589 MachineBasicBlock::iterator B) {
590 if (A == B)
591 return false;
592
593 MachineBasicBlock::iterator I = A->getParent()->begin();
594 while (I != A->getParent()->end()) {
595 if (I == A)
596 return true;
597 else if (I == B)
598 return false;
599
600 ++I;
601 }
602
603 return false;
604}
605
Owen Anderson9094db12008-07-09 20:14:53 +0000606/// ComputeLocalLiveness - Computes liveness of registers within a basic
607/// block, setting the killed/dead flags as appropriate.
608void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000609 MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
610 // Keep track of the most recently seen previous use or def of each reg,
611 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000612 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000613 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
614 I != E; ++I) {
Dale Johannesen3da6e092010-02-15 01:45:47 +0000615 if (I->isDebugValue())
616 continue;
Owen Anderson491fccc2008-07-08 22:24:50 +0000617 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
618 MachineOperand& MO = I->getOperand(i);
619 // Uses don't trigger any flags, but we need to save
620 // them for later. Also, we have to process these
621 // _before_ processing the defs, since an instr
622 // uses regs before it defs them.
Owen Anderson04764de2008-10-08 04:30:51 +0000623 if (MO.isReg() && MO.getReg() && MO.isUse()) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000624 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000625
626
627 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
628
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000629 const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
630 if (Aliases) {
631 while (*Aliases) {
Owen Anderson04764de2008-10-08 04:30:51 +0000632 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000633 alias = LastUseDef.find(*Aliases);
Owen Anderson04764de2008-10-08 04:30:51 +0000634
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000635 if (alias != LastUseDef.end() && alias->second.first != I)
636 LastUseDef[*Aliases] = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000637
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000638 ++Aliases;
Owen Anderson04764de2008-10-08 04:30:51 +0000639 }
640 }
641 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000642 }
643
644 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
645 MachineOperand& MO = I->getOperand(i);
646 // Defs others than 2-addr redefs _do_ trigger flag changes:
647 // - A def followed by a def is dead
648 // - A use followed by a def is a kill
Dan Gohmand735b802008-10-03 15:45:36 +0000649 if (MO.isReg() && MO.getReg() && MO.isDef()) {
Owen Anderson743a1e62008-07-10 01:56:35 +0000650 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000651 last = LastUseDef.find(MO.getReg());
652 if (last != LastUseDef.end()) {
Owen Andersonecee36e2008-07-10 01:53:01 +0000653 // Check if this is a two address instruction. If so, then
654 // the def does not kill the use.
Evan Chengef0732d2008-07-10 07:35:43 +0000655 if (last->second.first == I &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000656 I->isRegTiedToUseOperand(i))
Evan Chengef0732d2008-07-10 07:35:43 +0000657 continue;
Owen Andersondd4b47c2008-07-09 21:15:10 +0000658
Owen Anderson491fccc2008-07-08 22:24:50 +0000659 MachineOperand& lastUD =
660 last->second.first->getOperand(last->second.second);
661 if (lastUD.isDef())
662 lastUD.setIsDead(true);
Evan Chengef0732d2008-07-10 07:35:43 +0000663 else
Owen Anderson491fccc2008-07-08 22:24:50 +0000664 lastUD.setIsKill(true);
665 }
666
667 LastUseDef[MO.getReg()] = std::make_pair(I, i);
668 }
669 }
670 }
671
672 // Live-out (of the function) registers contain return values of the function,
673 // so we need to make sure they are alive at return time.
Bill Wendlingb0d27662010-03-16 02:01:51 +0000674 MachineBasicBlock::iterator Ret = MBB.getFirstTerminator();
675 bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn());
676
677 if (BBEndsInReturn)
Owen Anderson491fccc2008-07-08 22:24:50 +0000678 for (MachineRegisterInfo::liveout_iterator
679 I = MF->getRegInfo().liveout_begin(),
680 E = MF->getRegInfo().liveout_end(); I != E; ++I)
681 if (!Ret->readsRegister(*I)) {
682 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
683 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
684 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000685
686 // Finally, loop over the final use/def of each reg
687 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000688 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000689 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
690 MachineInstr* MI = I->second.first;
691 unsigned idx = I->second.second;
692 MachineOperand& MO = MI->getOperand(idx);
693
694 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
695
696 // A crude approximation of "live-out" calculation
697 bool usedOutsideBlock = isPhysReg ? false :
698 UsedInMultipleBlocks.test(MO.getReg() -
699 TargetRegisterInfo::FirstVirtualRegister);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000700
701 // If the machine BB ends in a return instruction, then the value isn't used
702 // outside of the BB.
703 if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000704 // DBG_VALUE complicates this: if the only refs of a register outside
705 // this block are DBG_VALUE, we can't keep the reg live just for that,
706 // as it will cause the reg to be spilled at the end of this block when
707 // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
708 // happens.
709 bool UsedByDebugValueOnly = false;
Owen Anderson491fccc2008-07-08 22:24:50 +0000710 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
Bill Wendling8fe347a2010-03-16 01:05:35 +0000711 UE = MRI.reg_end(); UI != UE; ++UI) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000712 // Two cases:
713 // - used in another block
714 // - used in the same block before it is defined (loop)
715 if (UI->getParent() != &MBB ||
Owen Anderson0966f0f2008-07-08 23:36:37 +0000716 (MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
Dale Johannesenf463d952010-02-16 01:27:47 +0000717 if (UI->isDebugValue()) {
718 UsedByDebugValueOnly = true;
719 continue;
720 }
Bill Wendling8fe347a2010-03-16 01:05:35 +0000721
Dale Johannesenf463d952010-02-16 01:27:47 +0000722 // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
Owen Anderson491fccc2008-07-08 22:24:50 +0000723 UsedInMultipleBlocks.set(MO.getReg() -
724 TargetRegisterInfo::FirstVirtualRegister);
725 usedOutsideBlock = true;
Dale Johannesenf463d952010-02-16 01:27:47 +0000726 UsedByDebugValueOnly = false;
Owen Anderson491fccc2008-07-08 22:24:50 +0000727 break;
728 }
Bill Wendling8fe347a2010-03-16 01:05:35 +0000729 }
730
Dale Johannesenf463d952010-02-16 01:27:47 +0000731 if (UsedByDebugValueOnly)
732 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
733 UE = MRI.reg_end(); UI != UE; ++UI)
734 if (UI->isDebugValue() &&
735 (UI->getParent() != &MBB ||
736 (MO.isDef() && precedes(&*UI, MI))))
737 UI.getOperand().setReg(0U);
738 }
739
Bill Wendling8fe347a2010-03-16 01:05:35 +0000740 // Physical registers and those that are not live-out of the block are
741 // killed/dead at their last use/def within this block.
Dan Gohman15843902010-03-18 18:07:13 +0000742 if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000743 if (MO.isUse()) {
744 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000745 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000746 MO.setIsKill(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000747 } else {
Owen Anderson491fccc2008-07-08 22:24:50 +0000748 MO.setIsDead(true);
Bill Wendling8fe347a2010-03-16 01:05:35 +0000749 }
Dan Gohman15843902010-03-18 18:07:13 +0000750 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000751 }
Owen Anderson9094db12008-07-09 20:14:53 +0000752}
753
754void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
755 // loop over each instruction
756 MachineBasicBlock::iterator MII = MBB.begin();
757
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000758 DEBUG({
759 const BasicBlock *LBB = MBB.getBasicBlock();
760 if (LBB)
David Greene44248172010-01-05 01:26:05 +0000761 dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000762 });
Owen Anderson9094db12008-07-09 20:14:53 +0000763
Evan Chengd5a48022009-01-29 18:37:30 +0000764 // Add live-in registers as active.
765 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000766 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000767 unsigned Reg = *I;
768 MF->getRegInfo().setPhysRegUsed(Reg);
769 PhysRegsUsed[Reg] = 0; // It is free and reserved now
770 AddToPhysRegsUseOrder(Reg);
771 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
772 *SubRegs; ++SubRegs) {
773 if (PhysRegsUsed[*SubRegs] != -2) {
774 AddToPhysRegsUseOrder(*SubRegs);
775 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
776 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Owen Anderson9094db12008-07-09 20:14:53 +0000777 }
Evan Chengd5a48022009-01-29 18:37:30 +0000778 }
Owen Anderson9094db12008-07-09 20:14:53 +0000779 }
780
781 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000782
Chris Lattner44500e32006-06-15 22:21:53 +0000783 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000784 while (MII != MBB.end()) {
785 MachineInstr *MI = MII++;
Chris Lattner749c6f62008-01-07 07:27:27 +0000786 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000787 DEBUG({
David Greene44248172010-01-05 01:26:05 +0000788 dbgs() << "\nStarting RegAlloc of: " << *MI;
789 dbgs() << " Regs have values: ";
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000790 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
791 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
David Greene44248172010-01-05 01:26:05 +0000792 dbgs() << "[" << TRI->getName(i)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000793 << ",%reg" << PhysRegsUsed[i] << "] ";
David Greene44248172010-01-05 01:26:05 +0000794 dbgs() << '\n';
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000795 });
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000796
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000797 // Determine whether this is a copy instruction. The cases where the
798 // source or destination are phys regs are handled specially.
799 unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg;
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000800 unsigned SrcCopyPhysReg = 0U;
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000801 bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
802 SrcCopySubReg, DstCopySubReg);
Dale Johannesen9a6636b2010-02-03 01:40:33 +0000803 if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg))
804 SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000805
Chris Lattnerae640432002-12-17 02:50:10 +0000806 // Loop over the implicit uses, making sure that they are at the head of the
807 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000808 if (TID.ImplicitUses) {
809 for (const unsigned *ImplicitUses = TID.ImplicitUses;
810 *ImplicitUses; ++ImplicitUses)
811 MarkPhysRegRecentlyUsed(*ImplicitUses);
812 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000813
Evan Chengddee8422006-11-15 20:55:15 +0000814 SmallVector<unsigned, 8> Kills;
815 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
816 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000817 if (MO.isReg() && MO.isKill()) {
Evan Cheng7ac19af2007-06-26 21:05:13 +0000818 if (!MO.isImplicit())
819 Kills.push_back(MO.getReg());
820 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
821 // These are extra physical register kills when a sub-register
822 // is defined (def of a sub-register is a read/mod/write of the
823 // larger registers). Ignore.
824 Kills.push_back(MO.getReg());
825 }
Evan Chengddee8422006-11-15 20:55:15 +0000826 }
827
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000828 // If any physical regs are earlyclobber, spill any value they might
829 // have in them, then mark them unallocatable.
830 // If any virtual regs are earlyclobber, allocate them now (before
831 // freeing inputs that are killed).
Chris Lattner518bb532010-02-09 19:54:29 +0000832 if (MI->isInlineAsm()) {
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000833 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
834 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000835 if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000836 MO.getReg()) {
837 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
838 unsigned DestVirtReg = MO.getReg();
839 unsigned DestPhysReg;
840
841 // If DestVirtReg already has a value, use it.
842 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
843 DestPhysReg = getReg(MBB, MI, DestVirtReg);
844 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
845 markVirtRegModified(DestVirtReg);
846 getVirtRegLastUse(DestVirtReg) =
847 std::make_pair((MachineInstr*)0, 0);
David Greene44248172010-01-05 01:26:05 +0000848 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000849 << " to %reg" << DestVirtReg << "\n");
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000850 MO.setReg(DestPhysReg); // Assign the earlyclobber register
851 } else {
852 unsigned Reg = MO.getReg();
853 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
854 // These are extra physical register defs when a sub-register
855 // is defined (def of a sub-register is a read/mod/write of the
856 // larger registers). Ignore.
857 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
858
859 MF->getRegInfo().setPhysRegUsed(Reg);
860 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
861 PhysRegsUsed[Reg] = 0; // It is free and reserved now
862 AddToPhysRegsUseOrder(Reg);
863
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000864 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
865 *SubRegs; ++SubRegs) {
866 if (PhysRegsUsed[*SubRegs] != -2) {
867 MF->getRegInfo().setPhysRegUsed(*SubRegs);
868 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
869 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000870 }
871 }
872 }
873 }
874 }
875 }
876
Dale Johannesen10fedd22010-02-10 00:11:11 +0000877 // If a DBG_VALUE says something is located in a spilled register,
878 // change the DBG_VALUE to be undef, which prevents the register
Dale Johannesenca134612010-01-30 00:57:47 +0000879 // from being reloaded here. Doing that would change the generated
880 // code, unless another use immediately follows this instruction.
Chris Lattner518bb532010-02-09 19:54:29 +0000881 if (MI->isDebugValue() &&
Dale Johannesenca134612010-01-30 00:57:47 +0000882 MI->getNumOperands()==3 && MI->getOperand(0).isReg()) {
883 unsigned VirtReg = MI->getOperand(0).getReg();
884 if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) &&
885 !getVirt2PhysRegMapSlot(VirtReg))
886 MI->getOperand(0).setReg(0U);
887 }
888
Brian Gaeke53b99a02003-08-15 21:19:25 +0000889 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000890 // incoming values if we are out of registers. Note that we completely
891 // ignore physical register uses here. We assume that if an explicit
892 // physical register is referenced by the instruction, that it is guaranteed
893 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000894 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000895 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000896 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
897 MachineOperand& MO = MI->getOperand(i);
898 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000899 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000900 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Dale Johannesenfc49bd22009-12-16 00:29:41 +0000901 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs,
902 isCopy ? DstCopyReg : 0);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000903 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000904
Evan Chengddee8422006-11-15 20:55:15 +0000905 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000906 // value, freeing the register being used, so it doesn't need to be
907 // spilled to memory.
908 //
Evan Chengddee8422006-11-15 20:55:15 +0000909 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
910 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000911 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000912 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000913 // If the virtual register was never materialized into a register, it
914 // might not be in the map, but it won't hurt to zero it out anyway.
915 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
916 PhysReg = PhysRegSlot;
917 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000918 } else if (PhysRegsUsed[PhysReg] == -2) {
919 // Unallocatable register dead, ignore.
920 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000921 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000922 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000923 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000924 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000925
Chris Lattner56ddada2004-02-17 17:49:10 +0000926 if (PhysReg) {
David Greene44248172010-01-05 01:26:05 +0000927 DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000928 << "[%reg" << VirtReg <<"], removing it from live set\n");
Chris Lattner56ddada2004-02-17 17:49:10 +0000929 removePhysReg(PhysReg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000930 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
931 *SubRegs; ++SubRegs) {
932 if (PhysRegsUsed[*SubRegs] != -2) {
David Greene44248172010-01-05 01:26:05 +0000933 DEBUG(dbgs() << " Last use of "
Bill Wendlingfbb594f2009-08-22 20:38:09 +0000934 << TRI->getName(*SubRegs) << "[%reg" << VirtReg
935 <<"], removing it from live set\n");
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000936 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +0000937 }
938 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000939 }
940 }
941
942 // Loop over all of the operands of the instruction, spilling registers that
943 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000944 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
945 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000946 if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000947 !MO.isEarlyClobber() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000948 TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000949 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000950 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000951 // These are extra physical register defs when a sub-register
952 // is defined (def of a sub-register is a read/mod/write of the
953 // larger registers). Ignore.
954 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
955
Chris Lattner84bc5422007-12-31 04:13:23 +0000956 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000957 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000958 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng7ac19af2007-06-26 21:05:13 +0000959 AddToPhysRegsUseOrder(Reg);
960
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000961 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
962 *SubRegs; ++SubRegs) {
963 if (PhysRegsUsed[*SubRegs] != -2) {
964 MF->getRegInfo().setPhysRegUsed(*SubRegs);
965 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
966 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner45d57882006-09-08 19:03:30 +0000967 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000968 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000969 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000970 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000971
972 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000973 if (TID.ImplicitDefs) {
974 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
975 *ImplicitDefs; ++ImplicitDefs) {
976 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000977 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000978 spillPhysReg(MBB, MI, Reg, true);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000979 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000980 PhysRegsUsed[Reg] = 0; // It is free and reserved now
981 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000982 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000983 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
984 *SubRegs; ++SubRegs) {
985 if (PhysRegsUsed[*SubRegs] != -2) {
986 AddToPhysRegsUseOrder(*SubRegs);
987 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
988 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Chris Lattner45d57882006-09-08 19:03:30 +0000989 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000990 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000991 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000992 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000993
Evan Chengddee8422006-11-15 20:55:15 +0000994 SmallVector<unsigned, 8> DeadDefs;
995 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
996 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000997 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +0000998 DeadDefs.push_back(MO.getReg());
999 }
1000
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001001 // Okay, we have allocated all of the source operands and spilled any values
1002 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +00001003 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +00001004 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +00001005 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001006 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1007 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001008 if (MO.isReg() && MO.isDef() && MO.getReg() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +00001009 !MO.isEarlyClobber() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +00001010 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001011 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001012 unsigned DestPhysReg;
1013
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +00001014 // If DestVirtReg already has a value, use it.
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001015 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) {
Dale Johannesen9a6636b2010-02-03 01:40:33 +00001016 // If this is a copy try to reuse the input as the output;
1017 // that will make the copy go away.
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001018 // If this is a copy, the source reg is a phys reg, and
1019 // that reg is available, use that phys reg for DestPhysReg.
Dale Johannesen9a6636b2010-02-03 01:40:33 +00001020 // If this is a copy, the source reg is a virtual reg, and
1021 // the phys reg that was assigned to that virtual reg is now
1022 // available, use that phys reg for DestPhysReg. (If it's now
1023 // available that means this was the last use of the source.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001024 if (isCopy &&
1025 TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) &&
1026 isPhysRegAvailable(SrcCopyReg)) {
1027 DestPhysReg = SrcCopyReg;
1028 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
Dale Johannesen9a6636b2010-02-03 01:40:33 +00001029 } else if (isCopy &&
1030 TargetRegisterInfo::isVirtualRegister(SrcCopyReg) &&
1031 SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) &&
1032 MF->getRegInfo().getRegClass(DestVirtReg)->
1033 contains(SrcCopyPhysReg)) {
1034 DestPhysReg = SrcCopyPhysReg;
1035 assignVirtToPhysReg(DestVirtReg, DestPhysReg);
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001036 } else
1037 DestPhysReg = getReg(MBB, MI, DestVirtReg);
1038 }
Chris Lattner84bc5422007-12-31 04:13:23 +00001039 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +00001040 markVirtRegModified(DestVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001041 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
David Greene44248172010-01-05 01:26:05 +00001042 DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +00001043 << " to %reg" << DestVirtReg << "\n");
Dan Gohman85e68152008-07-09 20:12:26 +00001044 MO.setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001045 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +00001046 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001047
Chris Lattner56ddada2004-02-17 17:49:10 +00001048 // If this instruction defines any registers that are immediately dead,
1049 // kill them now.
1050 //
Evan Chengddee8422006-11-15 20:55:15 +00001051 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
1052 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +00001053 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001054 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +00001055 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
1056 PhysReg = PhysRegSlot;
1057 assert(PhysReg != 0);
1058 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +00001059 } else if (PhysRegsUsed[PhysReg] == -2) {
1060 // Unallocatable register dead, ignore.
1061 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +00001062 }
Chris Lattner91a452b2003-01-13 00:25:40 +00001063
Chris Lattner56ddada2004-02-17 17:49:10 +00001064 if (PhysReg) {
David Greene44248172010-01-05 01:26:05 +00001065 DEBUG(dbgs() << " Register " << TRI->getName(PhysReg)
Bill Wendlingfbb594f2009-08-22 20:38:09 +00001066 << " [%reg" << VirtReg
1067 << "] is never used, removing it from live set\n");
Chris Lattner56ddada2004-02-17 17:49:10 +00001068 removePhysReg(PhysReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001069 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +00001070 *AliasSet; ++AliasSet) {
1071 if (PhysRegsUsed[*AliasSet] != -2) {
David Greene44248172010-01-05 01:26:05 +00001072 DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet)
Bill Wendlingfbb594f2009-08-22 20:38:09 +00001073 << " [%reg" << *AliasSet
1074 << "] is never used, removing it from live set\n");
Evan Chengddee8422006-11-15 20:55:15 +00001075 removePhysReg(*AliasSet);
1076 }
1077 }
Chris Lattner82bee0f2002-12-18 08:14:26 +00001078 }
1079 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001080
Bob Wilson9d928c22009-05-07 23:47:03 +00001081 // Finally, if this is a noop copy instruction, zap it. (Except that if
1082 // the copy is dead, it must be kept to avoid messing up liveness info for
1083 // the register scavenger. See pr4100.)
Dale Johannesenfc49bd22009-12-16 00:29:41 +00001084 if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg,
1085 SrcCopySubReg, DstCopySubReg) &&
1086 SrcCopyReg == DstCopyReg && DeadDefs.empty())
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001087 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001088 }
1089
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001090 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001091
1092 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001093 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001094 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001095 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001096 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001097 else
1098 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001099 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001100
Chris Lattner9a5ef202005-11-09 05:28:45 +00001101#if 0
1102 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001103 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001104 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001105 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001106 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001107 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001108 AllOk = false;
1109 }
1110 assert(AllOk && "Virtual registers still in phys regs?");
1111#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001112
Chris Lattner128c2aa2003-08-17 18:01:15 +00001113 // Clear any physical register which appear live at the end of the basic
1114 // block, but which do not hold any virtual registers. e.g., the stack
1115 // pointer.
1116 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001117}
1118
1119/// runOnMachineFunction - Register allocate the whole function
1120///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001121bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
David Greene44248172010-01-05 01:26:05 +00001122 DEBUG(dbgs() << "Machine Function\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001123 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +00001124 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001125 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001126 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001127
Dan Gohman6f0d0242008-02-10 18:45:23 +00001128 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +00001129
1130 // At various places we want to efficiently check to see whether a register
1131 // is allocatable. To handle this, we mark all unallocatable registers as
1132 // being pinned down, permanently.
1133 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001134 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001135 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1136 if (!Allocable[i])
1137 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1138 }
Chris Lattner64667b62004-02-09 01:26:13 +00001139
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001140 // initialize the virtual->physical register map to have a 'null'
1141 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001142 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001143 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001144 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001145 Virt2LastUseMap.grow(LastVirtReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001146 VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001147 UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
1148
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001149 // Loop over all of the basic blocks, eliminating virtual register references
1150 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1151 MBB != MBBe; ++MBB)
1152 AllocateBasicBlock(*MBB);
1153
Chris Lattner580f9be2002-12-28 20:40:43 +00001154 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001155 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001156 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001157 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001158 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001159 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001160 return true;
1161}
1162
Chris Lattneref09c632004-01-31 21:27:19 +00001163FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001164 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001165}