blob: e5d3a48babdf48e5cfef485225fb203d9e3590ad [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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Owen Anderson743a1e62008-07-10 01:56:35 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000031#include "llvm/ADT/IndexedMap.h"
Evan Cheng5a3c6a82009-01-29 02:20:59 +000032#include "llvm/ADT/SmallSet.h"
Evan Chengddee8422006-11-15 20:55:15 +000033#include "llvm/ADT/SmallVector.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/ADT/Statistic.h"
Evan Cheng2fc628d2008-02-06 19:16:53 +000035#include "llvm/ADT/STLExtras.h"
Chris Lattner27f29162004-10-26 15:35:58 +000036#include <algorithm>
Chris Lattneref09c632004-01-31 21:27:19 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattnercd3245a2006-12-19 22:41:21 +000039STATISTIC(NumStores, "Number of stores added");
40STATISTIC(NumLoads , "Number of loads added");
Jim Laskey13ec7022006-08-01 14:21:23 +000041
Dan Gohman844731a2008-05-13 00:00:25 +000042static RegisterRegAlloc
Dan Gohmanb8cab922008-10-14 20:25:08 +000043 localRegAlloc("local", "local register allocator",
Dan Gohman844731a2008-05-13 00:00:25 +000044 createLocalRegisterAllocator);
45
Chris Lattnercd3245a2006-12-19 22:41:21 +000046namespace {
Bill Wendlinge23e00d2007-05-08 19:02:46 +000047 class VISIBILITY_HIDDEN RALocal : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000048 public:
Devang Patel19974732007-05-03 01:11:54 +000049 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000050 RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
Devang Patel794fd752007-05-01 21:15:47 +000051 private:
Chris Lattner580f9be2002-12-28 20:40:43 +000052 const TargetMachine *TM;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000053 MachineFunction *MF;
Dan Gohman6f0d0242008-02-10 18:45:23 +000054 const TargetRegisterInfo *TRI;
Owen Anderson6425f8b2008-01-07 01:35:56 +000055 const TargetInstrInfo *TII;
Chris Lattnerff863ba2002-12-25 05:05:46 +000056
Chris Lattnerb8822ad2003-08-04 23:36:39 +000057 // StackSlotForVirtReg - Maps virtual regs to the frame index where these
58 // values are spilled.
Evan Chengbdb10fe2008-07-10 18:23:23 +000059 IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000060
61 // Virt2PhysRegMap - This map contains entries for each virtual register
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000062 // that is currently available in a physical register.
Chris Lattner94c002a2007-02-01 05:32:05 +000063 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
Chris Lattnerecea5632004-02-09 02:12:04 +000064
65 unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
Alkis Evlogimenos4d0d8642004-02-25 21:55:45 +000066 return Virt2PhysRegMap[VirtReg];
Chris Lattnerecea5632004-02-09 02:12:04 +000067 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000068
Chris Lattner64667b62004-02-09 01:26:13 +000069 // PhysRegsUsed - This array is effectively a map, containing entries for
70 // each physical register that currently has a value (ie, it is in
71 // Virt2PhysRegMap). The value mapped to is the virtual register
72 // corresponding to the physical register (the inverse of the
73 // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
Chris Lattner45d57882006-09-08 19:03:30 +000074 // because it is used by a future instruction, and to -2 if it is not
75 // allocatable. If the entry for a physical register is -1, then the
76 // physical register is "not in the map".
Chris Lattnerb74e83c2002-12-16 16:15:28 +000077 //
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +000078 std::vector<int> PhysRegsUsed;
Chris Lattnerb74e83c2002-12-16 16:15:28 +000079
80 // PhysRegsUseOrder - This contains a list of the physical registers that
81 // currently have a virtual register value in them. This list provides an
82 // ordering of registers, imposing a reallocation order. This list is only
83 // used if all registers are allocated and we have to spill one, in which
84 // case we spill the least recently used register. Entries at the front of
85 // the list are the least recently used registers, entries at the back are
86 // the most recently used.
87 //
88 std::vector<unsigned> PhysRegsUseOrder;
89
Evan Cheng839b7592008-01-17 02:08:17 +000090 // Virt2LastUseMap - This maps each virtual register to its last use
91 // (MachineInstr*, operand index pair).
92 IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
93 Virt2LastUseMap;
94
95 std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000096 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
Evan Cheng839b7592008-01-17 02:08:17 +000097 return Virt2LastUseMap[Reg];
98 }
99
Chris Lattner91a452b2003-01-13 00:25:40 +0000100 // VirtRegModified - This bitset contains information about which virtual
101 // registers need to be spilled back to memory when their registers are
102 // scavenged. If a virtual register has simply been rematerialized, there
103 // is no reason to spill it to memory when we need the register back.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000104 //
Evan Cheng644340a2008-01-17 00:35:26 +0000105 BitVector VirtRegModified;
Owen Anderson491fccc2008-07-08 22:24:50 +0000106
107 // UsedInMultipleBlocks - Tracks whether a particular register is used in
108 // more than one block.
109 BitVector UsedInMultipleBlocks;
Chris Lattner91a452b2003-01-13 00:25:40 +0000110
111 void markVirtRegModified(unsigned Reg, bool Val = true) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000112 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
113 Reg -= TargetRegisterInfo::FirstVirtualRegister;
Evan Cheng644340a2008-01-17 00:35:26 +0000114 if (Val)
115 VirtRegModified.set(Reg);
116 else
117 VirtRegModified.reset(Reg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000118 }
119
120 bool isVirtRegModified(unsigned Reg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000121 assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
122 assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000123 && "Illegal virtual register!");
Dan Gohman6f0d0242008-02-10 18:45:23 +0000124 return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
Chris Lattner91a452b2003-01-13 00:25:40 +0000125 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000126
Evan Cheng7ac19af2007-06-26 21:05:13 +0000127 void AddToPhysRegsUseOrder(unsigned Reg) {
128 std::vector<unsigned>::iterator It =
129 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
130 if (It != PhysRegsUseOrder.end())
131 PhysRegsUseOrder.erase(It);
132 PhysRegsUseOrder.push_back(Reg);
133 }
134
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000135 void MarkPhysRegRecentlyUsed(unsigned Reg) {
Chris Lattner5e503492006-09-03 07:15:37 +0000136 if (PhysRegsUseOrder.empty() ||
137 PhysRegsUseOrder.back() == Reg) return; // Already most recently used
Chris Lattner0eb172c2002-12-24 00:04:55 +0000138
139 for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000140 if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
141 unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
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,
237 /// it loads it into a register. If register pressure is high, and it is
238 /// possible, it tries to fold the load of the virtual register into the
239 /// instruction itself. It avoids doing this if register pressure is low to
240 /// improve the chance that subsequent instructions can use the reloaded
241 /// value. This method returns the modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000242 ///
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000243 MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000244 unsigned OpNum, SmallSet<unsigned, 4> &RRegs);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000245
Owen Anderson9094db12008-07-09 20:14:53 +0000246 /// ComputeLocalLiveness - Computes liveness of registers within a basic
247 /// block, setting the killed/dead flags as appropriate.
248 void ComputeLocalLiveness(MachineBasicBlock& MBB);
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000249
250 void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
251 unsigned PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000252 };
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000253 char RALocal::ID = 0;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000254}
255
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000256/// getStackSpaceFor - This allocates space for the specified virtual register
257/// to be held on the stack.
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000258int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000259 // Find the location Reg would belong...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000260 int SS = StackSlotForVirtReg[VirtReg];
261 if (SS != -1)
262 return SS; // Already has space allocated?
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000263
Chris Lattner580f9be2002-12-28 20:40:43 +0000264 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000265 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
266 RC->getAlignment());
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000267
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000268 // Assign the slot...
Evan Chengbdb10fe2008-07-10 18:23:23 +0000269 StackSlotForVirtReg[VirtReg] = FrameIdx;
Chris Lattner580f9be2002-12-28 20:40:43 +0000270 return FrameIdx;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000271}
272
Chris Lattnerae640432002-12-17 02:50:10 +0000273
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000274/// removePhysReg - This method marks the specified physical register as no
Chris Lattner82bee0f2002-12-18 08:14:26 +0000275/// longer being in use.
276///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000277void RALocal::removePhysReg(unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000278 PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
Chris Lattner82bee0f2002-12-18 08:14:26 +0000279
280 std::vector<unsigned>::iterator It =
281 std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000282 if (It != PhysRegsUseOrder.end())
283 PhysRegsUseOrder.erase(It);
Chris Lattner82bee0f2002-12-18 08:14:26 +0000284}
285
Chris Lattner91a452b2003-01-13 00:25:40 +0000286
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000287/// spillVirtReg - This method spills the value specified by PhysReg into the
288/// virtual register slot specified by VirtReg. It then updates the RA data
289/// structures to indicate the fact that PhysReg is now available.
290///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000291void RALocal::spillVirtReg(MachineBasicBlock &MBB,
292 MachineBasicBlock::iterator I,
293 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner8c819452003-08-05 04:13:58 +0000294 assert(VirtReg && "Spilling a physical register is illegal!"
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000295 " Must not have appropriate kill for the register or use exists beyond"
296 " the intended one.");
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000297 DOUT << " Spilling register " << TRI->getName(PhysReg)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000298 << " containing %reg" << VirtReg;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000299
Evan Cheng839b7592008-01-17 02:08:17 +0000300 if (!isVirtRegModified(VirtReg)) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000301 DOUT << " which has not been modified, so no store necessary!";
Evan Cheng839b7592008-01-17 02:08:17 +0000302 std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
303 if (LastUse.first)
304 LastUse.first->getOperand(LastUse.second).setIsKill();
Evan Cheng2fc628d2008-02-06 19:16:53 +0000305 } else {
306 // Otherwise, there is a virtual register corresponding to this physical
307 // register. We only need to spill it into its stack slot if it has been
308 // modified.
Chris Lattner84bc5422007-12-31 04:13:23 +0000309 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattnerd9ac6a72003-08-05 00:49:09 +0000310 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000311 DOUT << " to stack slot #" << FrameIndex;
Evan Cheng2fc628d2008-02-06 19:16:53 +0000312 // If the instruction reads the register that's spilled, (e.g. this can
313 // happen if it is a move to a physical register), then the spill
314 // instruction is not a kill.
Evan Cheng6130f662008-03-05 00:59:57 +0000315 bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
Evan Cheng431bfcb2008-02-11 08:30:52 +0000316 TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000317 ++NumStores; // Update statistics
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000318 }
Chris Lattnerecea5632004-02-09 02:12:04 +0000319
320 getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000321
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000322 DOUT << "\n";
Chris Lattner82bee0f2002-12-18 08:14:26 +0000323 removePhysReg(PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000324}
325
Chris Lattnerae640432002-12-17 02:50:10 +0000326
Chris Lattner91a452b2003-01-13 00:25:40 +0000327/// spillPhysReg - This method spills the specified physical register into the
Chris Lattner128c2aa2003-08-17 18:01:15 +0000328/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
329/// then the request is ignored if the physical register does not contain a
330/// virtual register.
Chris Lattner91a452b2003-01-13 00:25:40 +0000331///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000332void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
333 unsigned PhysReg, bool OnlyVirtRegs) {
Chris Lattner64667b62004-02-09 01:26:13 +0000334 if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
Chris Lattner45d57882006-09-08 19:03:30 +0000335 assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
Chris Lattner64667b62004-02-09 01:26:13 +0000336 if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
337 spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000338 } else {
Chris Lattner91a452b2003-01-13 00:25:40 +0000339 // If the selected register aliases any other registers, we must make
Chris Lattner45d57882006-09-08 19:03:30 +0000340 // sure that one of the aliases isn't alive.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000341 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Chris Lattner64667b62004-02-09 01:26:13 +0000342 *AliasSet; ++AliasSet)
Chris Lattner45d57882006-09-08 19:03:30 +0000343 if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
344 PhysRegsUsed[*AliasSet] != -2) // If allocatable.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000345 if (PhysRegsUsed[*AliasSet])
346 spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
Chris Lattner91a452b2003-01-13 00:25:40 +0000347 }
348}
349
350
351/// assignVirtToPhysReg - This method updates local state so that we know
352/// that PhysReg is the proper container for VirtReg now. The physical
353/// register must not be used for anything else when this is called.
354///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000355void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
Chris Lattner64667b62004-02-09 01:26:13 +0000356 assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
Chris Lattner91a452b2003-01-13 00:25:40 +0000357 // Update information to note the fact that this register was just used, and
358 // it holds VirtReg.
359 PhysRegsUsed[PhysReg] = VirtReg;
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000360 getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000361 AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
Chris Lattner91a452b2003-01-13 00:25:40 +0000362}
363
364
Chris Lattnerae640432002-12-17 02:50:10 +0000365/// isPhysRegAvailable - Return true if the specified physical register is free
366/// and available for use. This also includes checking to see if aliased
367/// registers are all free...
368///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000369bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
Chris Lattner64667b62004-02-09 01:26:13 +0000370 if (PhysRegsUsed[PhysReg] != -1) return false;
Chris Lattnerae640432002-12-17 02:50:10 +0000371
372 // If the selected register aliases any other allocated registers, it is
373 // not free!
Dan Gohman6f0d0242008-02-10 18:45:23 +0000374 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000375 *AliasSet; ++AliasSet)
Evan Chengbcfa1ca2008-02-22 20:30:53 +0000376 if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000377 return false; // Can't use this reg then.
Chris Lattnerae640432002-12-17 02:50:10 +0000378 return true;
379}
380
381
Chris Lattner91a452b2003-01-13 00:25:40 +0000382/// getFreeReg - Look to see if there is a free register available in the
383/// specified register class. If not, return 0.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000384///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000385unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
Chris Lattner580f9be2002-12-28 20:40:43 +0000386 // Get iterators defining the range of registers that are valid to allocate in
387 // this class, which also specifies the preferred allocation order.
388 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
389 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Chris Lattnerae640432002-12-17 02:50:10 +0000390
Chris Lattner91a452b2003-01-13 00:25:40 +0000391 for (; RI != RE; ++RI)
392 if (isPhysRegAvailable(*RI)) { // Is reg unused?
393 assert(*RI != 0 && "Cannot use register!");
394 return *RI; // Found an unused register!
395 }
396 return 0;
397}
398
399
Chris Lattner91a452b2003-01-13 00:25:40 +0000400/// getReg - Find a physical register to hold the specified virtual
401/// register. If all compatible physical registers are used, this method spills
402/// the last used virtual register to the stack, and uses that register.
403///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000404unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000405 unsigned VirtReg, bool NoFree) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000406 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner91a452b2003-01-13 00:25:40 +0000407
408 // First check to see if we have a free register of the requested type...
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000409 unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000410
Chris Lattnerae640432002-12-17 02:50:10 +0000411 // If we didn't find an unused register, scavenge one now!
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000412 if (PhysReg == 0) {
Chris Lattnerc21be922002-12-16 17:44:42 +0000413 assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
Chris Lattnerae640432002-12-17 02:50:10 +0000414
415 // Loop over all of the preallocated registers from the least recently used
416 // to the most recently used. When we find one that is capable of holding
417 // our register, use it.
418 for (unsigned i = 0; PhysReg == 0; ++i) {
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000419 assert(i != PhysRegsUseOrder.size() &&
420 "Couldn't find a register of the appropriate class!");
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000421
Chris Lattnerae640432002-12-17 02:50:10 +0000422 unsigned R = PhysRegsUseOrder[i];
Chris Lattner41822c72003-08-23 23:49:42 +0000423
424 // We can only use this register if it holds a virtual register (ie, it
425 // can be spilled). Do not use it if it is an explicitly allocated
426 // physical register!
Chris Lattner64667b62004-02-09 01:26:13 +0000427 assert(PhysRegsUsed[R] != -1 &&
Chris Lattner41822c72003-08-23 23:49:42 +0000428 "PhysReg in PhysRegsUseOrder, but is not allocated?");
Chris Lattner45d57882006-09-08 19:03:30 +0000429 if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
Chris Lattner41822c72003-08-23 23:49:42 +0000430 // If the current register is compatible, use it.
Chris Lattner3bba0262004-08-15 22:23:09 +0000431 if (RC->contains(R)) {
Chris Lattner41822c72003-08-23 23:49:42 +0000432 PhysReg = R;
433 break;
434 } else {
435 // If one of the registers aliased to the current register is
436 // compatible, use it.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000437 for (const unsigned *AliasIt = TRI->getAliasSet(R);
Chris Lattner5e503492006-09-03 07:15:37 +0000438 *AliasIt; ++AliasIt) {
439 if (RC->contains(*AliasIt) &&
440 // If this is pinned down for some reason, don't use it. For
441 // example, if CL is pinned, and we run across CH, don't use
442 // CH as justification for using scavenging ECX (which will
443 // fail).
Chris Lattner45d57882006-09-08 19:03:30 +0000444 PhysRegsUsed[*AliasIt] != 0 &&
445
446 // Make sure the register is allocatable. Don't allocate SIL on
447 // x86-32.
448 PhysRegsUsed[*AliasIt] != -2) {
Chris Lattner5e503492006-09-03 07:15:37 +0000449 PhysReg = *AliasIt; // Take an aliased register
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000450 break;
451 }
452 }
Chris Lattner41822c72003-08-23 23:49:42 +0000453 }
Chris Lattnerae640432002-12-17 02:50:10 +0000454 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000455 }
456
Chris Lattnerae640432002-12-17 02:50:10 +0000457 assert(PhysReg && "Physical register not assigned!?!?");
458
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000459 // At this point PhysRegsUseOrder[i] is the least recently used register of
460 // compatible register class. Spill it to memory and reap its remains.
Chris Lattnerc21be922002-12-16 17:44:42 +0000461 spillPhysReg(MBB, I, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000462 }
463
464 // Now that we know which register we need to assign this to, do it now!
Chris Lattner91a452b2003-01-13 00:25:40 +0000465 assignVirtToPhysReg(VirtReg, PhysReg);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000466 return PhysReg;
467}
468
Chris Lattnerae640432002-12-17 02:50:10 +0000469
Bob Wilson8d24f412009-05-07 21:20:42 +0000470/// reloadVirtReg - This method transforms the specified virtual
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000471/// register use to refer to a physical register. This method may do this in
472/// one of several ways: if the register is available in a physical register
473/// already, it uses that physical register. If the value is not in a physical
474/// register, and if there are physical registers available, it loads it into a
475/// register. If register pressure is high, and it is possible, it tries to
476/// fold the load of the virtual register into the instruction itself. It
477/// avoids doing this if register pressure is low to improve the chance that
478/// subsequent instructions can use the reloaded value. This method returns the
479/// modified instruction.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000480///
Bill Wendlinge23e00d2007-05-08 19:02:46 +0000481MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000482 unsigned OpNum,
483 SmallSet<unsigned, 4> &ReloadedRegs) {
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000484 unsigned VirtReg = MI->getOperand(OpNum).getReg();
485
486 // If the virtual register is already available, just update the instruction
487 // and return.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000488 if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
Bill Wendling97e3c012008-02-29 18:52:01 +0000489 MarkPhysRegRecentlyUsed(PR); // Already have this value available!
Chris Lattnere53f4a02006-05-04 17:52:23 +0000490 MI->getOperand(OpNum).setReg(PR); // Assign the input register
Bill Wendling97e3c012008-02-29 18:52:01 +0000491 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000492 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000493 }
494
Chris Lattner1e3812c2004-02-17 04:08:37 +0000495 // Otherwise, we need to fold it into the current instruction, or reload it.
496 // If we have registers available to hold the value, use them.
Chris Lattner84bc5422007-12-31 04:13:23 +0000497 const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000498 unsigned PhysReg = getFreeReg(RC);
Chris Lattner11390e72004-02-17 08:09:40 +0000499 int FrameIndex = getStackSpaceFor(VirtReg, RC);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000500
Chris Lattner11390e72004-02-17 08:09:40 +0000501 if (PhysReg) { // Register is available, allocate it!
502 assignVirtToPhysReg(VirtReg, PhysReg);
503 } else { // No registers available.
Evan Cheng27240c72008-02-07 19:46:55 +0000504 // Force some poor hapless value out of the register file to
Chris Lattner1e3812c2004-02-17 04:08:37 +0000505 // make room for the new register, and reload it.
Evan Cheng7ddee0a2009-01-29 01:13:00 +0000506 PhysReg = getReg(MBB, MI, VirtReg, true);
Chris Lattner1e3812c2004-02-17 04:08:37 +0000507 }
508
Chris Lattner91a452b2003-01-13 00:25:40 +0000509 markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
510
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000511 DOUT << " Reloading %reg" << VirtReg << " into "
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000512 << TRI->getName(PhysReg) << "\n";
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000513
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000514 // Add move instruction(s)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000515 TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000516 ++NumLoads; // Update statistics
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000517
Chris Lattner84bc5422007-12-31 04:13:23 +0000518 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattnere53f4a02006-05-04 17:52:23 +0000519 MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
Evan Cheng839b7592008-01-17 02:08:17 +0000520 getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000521
522 if (!ReloadedRegs.insert(PhysReg)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000523 std::string msg;
524 raw_string_ostream Msg(msg);
525 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000526 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000527 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000528 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000529 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000530 }
Torok Edwin7d696d82009-07-11 13:10:19 +0000531 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000532 }
533 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
534 *SubRegs; ++SubRegs) {
535 if (!ReloadedRegs.insert(*SubRegs)) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000536 std::string msg;
537 raw_string_ostream Msg(msg);
538 Msg << "Ran out of registers during register allocation!";
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000539 if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
Torok Edwin7d696d82009-07-11 13:10:19 +0000540 Msg << "\nPlease check your inline asm statement for invalid "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000541 << "constraints:\n";
Torok Edwin7d696d82009-07-11 13:10:19 +0000542 MI->print(Msg, TM);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000543 }
Torok Edwin7d696d82009-07-11 13:10:19 +0000544 llvm_report_error(Msg.str());
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000545 }
546 }
547
Chris Lattner42e0a8f2004-02-17 03:57:19 +0000548 return MI;
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000549}
550
Evan Cheng7ac19af2007-06-26 21:05:13 +0000551/// isReadModWriteImplicitKill - True if this is an implicit kill for a
552/// read/mod/write register, i.e. update partial register.
553static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
554 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
555 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000556 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000557 MO.isDef() && !MO.isDead())
558 return true;
559 }
560 return false;
561}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000562
Evan Cheng7ac19af2007-06-26 21:05:13 +0000563/// isReadModWriteImplicitDef - True if this is an implicit def for a
564/// read/mod/write register, i.e. update partial register.
565static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
566 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
567 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000568 if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000569 !MO.isDef() && MO.isKill())
570 return true;
571 }
572 return false;
573}
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000574
Owen Anderson491fccc2008-07-08 22:24:50 +0000575// precedes - Helper function to determine with MachineInstr A
576// precedes MachineInstr B within the same MBB.
577static bool precedes(MachineBasicBlock::iterator A,
578 MachineBasicBlock::iterator B) {
579 if (A == B)
580 return false;
581
582 MachineBasicBlock::iterator I = A->getParent()->begin();
583 while (I != A->getParent()->end()) {
584 if (I == A)
585 return true;
586 else if (I == B)
587 return false;
588
589 ++I;
590 }
591
592 return false;
593}
594
Owen Anderson9094db12008-07-09 20:14:53 +0000595/// ComputeLocalLiveness - Computes liveness of registers within a basic
596/// block, setting the killed/dead flags as appropriate.
597void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000598 MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
599 // Keep track of the most recently seen previous use or def of each reg,
600 // so that we can update them with dead/kill markers.
Owen Anderson743a1e62008-07-10 01:56:35 +0000601 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
Owen Anderson491fccc2008-07-08 22:24:50 +0000602 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
603 I != E; ++I) {
604 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
605 MachineOperand& MO = I->getOperand(i);
606 // Uses don't trigger any flags, but we need to save
607 // them for later. Also, we have to process these
608 // _before_ processing the defs, since an instr
609 // uses regs before it defs them.
Owen Anderson04764de2008-10-08 04:30:51 +0000610 if (MO.isReg() && MO.getReg() && MO.isUse()) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000611 LastUseDef[MO.getReg()] = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000612
613
614 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
615
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000616 const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
617 if (Aliases) {
618 while (*Aliases) {
Owen Anderson04764de2008-10-08 04:30:51 +0000619 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000620 alias = LastUseDef.find(*Aliases);
Owen Anderson04764de2008-10-08 04:30:51 +0000621
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000622 if (alias != LastUseDef.end() && alias->second.first != I)
623 LastUseDef[*Aliases] = std::make_pair(I, i);
Owen Anderson04764de2008-10-08 04:30:51 +0000624
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000625 ++Aliases;
Owen Anderson04764de2008-10-08 04:30:51 +0000626 }
627 }
628 }
Owen Anderson491fccc2008-07-08 22:24:50 +0000629 }
630
631 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
632 MachineOperand& MO = I->getOperand(i);
633 // Defs others than 2-addr redefs _do_ trigger flag changes:
634 // - A def followed by a def is dead
635 // - A use followed by a def is a kill
Dan Gohmand735b802008-10-03 15:45:36 +0000636 if (MO.isReg() && MO.getReg() && MO.isDef()) {
Owen Anderson743a1e62008-07-10 01:56:35 +0000637 DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000638 last = LastUseDef.find(MO.getReg());
639 if (last != LastUseDef.end()) {
Owen Andersonecee36e2008-07-10 01:53:01 +0000640 // Check if this is a two address instruction. If so, then
641 // the def does not kill the use.
Evan Chengef0732d2008-07-10 07:35:43 +0000642 if (last->second.first == I &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000643 I->isRegTiedToUseOperand(i))
Evan Chengef0732d2008-07-10 07:35:43 +0000644 continue;
Owen Andersondd4b47c2008-07-09 21:15:10 +0000645
Owen Anderson491fccc2008-07-08 22:24:50 +0000646 MachineOperand& lastUD =
647 last->second.first->getOperand(last->second.second);
648 if (lastUD.isDef())
649 lastUD.setIsDead(true);
Evan Chengef0732d2008-07-10 07:35:43 +0000650 else
Owen Anderson491fccc2008-07-08 22:24:50 +0000651 lastUD.setIsKill(true);
652 }
653
654 LastUseDef[MO.getReg()] = std::make_pair(I, i);
655 }
656 }
657 }
658
659 // Live-out (of the function) registers contain return values of the function,
660 // so we need to make sure they are alive at return time.
661 if (!MBB.empty() && MBB.back().getDesc().isReturn()) {
662 MachineInstr* Ret = &MBB.back();
663 for (MachineRegisterInfo::liveout_iterator
664 I = MF->getRegInfo().liveout_begin(),
665 E = MF->getRegInfo().liveout_end(); I != E; ++I)
666 if (!Ret->readsRegister(*I)) {
667 Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
668 LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
669 }
670 }
671
672 // Finally, loop over the final use/def of each reg
673 // in the block and determine if it is dead.
Owen Anderson743a1e62008-07-10 01:56:35 +0000674 for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
Owen Anderson491fccc2008-07-08 22:24:50 +0000675 I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
676 MachineInstr* MI = I->second.first;
677 unsigned idx = I->second.second;
678 MachineOperand& MO = MI->getOperand(idx);
679
680 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
681
682 // A crude approximation of "live-out" calculation
683 bool usedOutsideBlock = isPhysReg ? false :
684 UsedInMultipleBlocks.test(MO.getReg() -
685 TargetRegisterInfo::FirstVirtualRegister);
686 if (!isPhysReg && !usedOutsideBlock)
687 for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
688 UE = MRI.reg_end(); UI != UE; ++UI)
689 // Two cases:
690 // - used in another block
691 // - used in the same block before it is defined (loop)
692 if (UI->getParent() != &MBB ||
Owen Anderson0966f0f2008-07-08 23:36:37 +0000693 (MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
Owen Anderson491fccc2008-07-08 22:24:50 +0000694 UsedInMultipleBlocks.set(MO.getReg() -
695 TargetRegisterInfo::FirstVirtualRegister);
696 usedOutsideBlock = true;
697 break;
698 }
699
700 // Physical registers and those that are not live-out of the block
701 // are killed/dead at their last use/def within this block.
702 if (isPhysReg || !usedOutsideBlock) {
Dan Gohman022b21f2008-10-04 00:31:14 +0000703 if (MO.isUse()) {
704 // Don't mark uses that are tied to defs as kills.
Evan Chenga24752f2009-03-19 20:30:06 +0000705 if (!MI->isRegTiedToDefOperand(idx))
Dan Gohman022b21f2008-10-04 00:31:14 +0000706 MO.setIsKill(true);
707 } else
Owen Anderson491fccc2008-07-08 22:24:50 +0000708 MO.setIsDead(true);
709 }
710 }
Owen Anderson9094db12008-07-09 20:14:53 +0000711}
712
713void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
714 // loop over each instruction
715 MachineBasicBlock::iterator MII = MBB.begin();
716
717 DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000718 if (LBB) errs() << "\nStarting RegAlloc of BB: " << LBB->getName());
Owen Anderson9094db12008-07-09 20:14:53 +0000719
Evan Chengd5a48022009-01-29 18:37:30 +0000720 // Add live-in registers as active.
721 for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
Owen Anderson9094db12008-07-09 20:14:53 +0000722 E = MBB.livein_end(); I != E; ++I) {
Evan Chengd5a48022009-01-29 18:37:30 +0000723 unsigned Reg = *I;
724 MF->getRegInfo().setPhysRegUsed(Reg);
725 PhysRegsUsed[Reg] = 0; // It is free and reserved now
726 AddToPhysRegsUseOrder(Reg);
727 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
728 *SubRegs; ++SubRegs) {
729 if (PhysRegsUsed[*SubRegs] != -2) {
730 AddToPhysRegsUseOrder(*SubRegs);
731 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
732 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Owen Anderson9094db12008-07-09 20:14:53 +0000733 }
Evan Chengd5a48022009-01-29 18:37:30 +0000734 }
Owen Anderson9094db12008-07-09 20:14:53 +0000735 }
736
737 ComputeLocalLiveness(MBB);
Owen Anderson491fccc2008-07-08 22:24:50 +0000738
Chris Lattner44500e32006-06-15 22:21:53 +0000739 // Otherwise, sequentially allocate each instruction in the MBB.
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000740 while (MII != MBB.end()) {
741 MachineInstr *MI = MII++;
Chris Lattner749c6f62008-01-07 07:27:27 +0000742 const TargetInstrDesc &TID = MI->getDesc();
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000743 DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
744 DOUT << " Regs have values: ";
Dan Gohman6f0d0242008-02-10 18:45:23 +0000745 for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
Chris Lattner45d57882006-09-08 19:03:30 +0000746 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000747 DOUT << "[" << TRI->getName(i)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000748 << ",%reg" << PhysRegsUsed[i] << "] ";
749 DOUT << "\n");
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000750
Chris Lattnerae640432002-12-17 02:50:10 +0000751 // Loop over the implicit uses, making sure that they are at the head of the
752 // use order list, so they don't get reallocated.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000753 if (TID.ImplicitUses) {
754 for (const unsigned *ImplicitUses = TID.ImplicitUses;
755 *ImplicitUses; ++ImplicitUses)
756 MarkPhysRegRecentlyUsed(*ImplicitUses);
757 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000758
Evan Chengddee8422006-11-15 20:55:15 +0000759 SmallVector<unsigned, 8> Kills;
760 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
761 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000762 if (MO.isReg() && MO.isKill()) {
Evan Cheng7ac19af2007-06-26 21:05:13 +0000763 if (!MO.isImplicit())
764 Kills.push_back(MO.getReg());
765 else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
766 // These are extra physical register kills when a sub-register
767 // is defined (def of a sub-register is a read/mod/write of the
768 // larger registers). Ignore.
769 Kills.push_back(MO.getReg());
770 }
Evan Chengddee8422006-11-15 20:55:15 +0000771 }
772
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000773 // If any physical regs are earlyclobber, spill any value they might
774 // have in them, then mark them unallocatable.
775 // If any virtual regs are earlyclobber, allocate them now (before
776 // freeing inputs that are killed).
777 if (MI->getOpcode()==TargetInstrInfo::INLINEASM) {
778 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
779 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000780 if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000781 MO.getReg()) {
782 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
783 unsigned DestVirtReg = MO.getReg();
784 unsigned DestPhysReg;
785
786 // If DestVirtReg already has a value, use it.
787 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
788 DestPhysReg = getReg(MBB, MI, DestVirtReg);
789 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
790 markVirtRegModified(DestVirtReg);
791 getVirtRegLastUse(DestVirtReg) =
792 std::make_pair((MachineInstr*)0, 0);
793 DOUT << " Assigning " << TRI->getName(DestPhysReg)
794 << " to %reg" << DestVirtReg << "\n";
795 MO.setReg(DestPhysReg); // Assign the earlyclobber register
796 } else {
797 unsigned Reg = MO.getReg();
798 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
799 // These are extra physical register defs when a sub-register
800 // is defined (def of a sub-register is a read/mod/write of the
801 // larger registers). Ignore.
802 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
803
804 MF->getRegInfo().setPhysRegUsed(Reg);
805 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
806 PhysRegsUsed[Reg] = 0; // It is free and reserved now
807 AddToPhysRegsUseOrder(Reg);
808
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000809 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
810 *SubRegs; ++SubRegs) {
811 if (PhysRegsUsed[*SubRegs] != -2) {
812 MF->getRegInfo().setPhysRegUsed(*SubRegs);
813 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
814 AddToPhysRegsUseOrder(*SubRegs);
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000815 }
816 }
817 }
818 }
819 }
820 }
821
Brian Gaeke53b99a02003-08-15 21:19:25 +0000822 // Get the used operands into registers. This has the potential to spill
Chris Lattnerb8822ad2003-08-04 23:36:39 +0000823 // incoming values if we are out of registers. Note that we completely
824 // ignore physical register uses here. We assume that if an explicit
825 // physical register is referenced by the instruction, that it is guaranteed
826 // to be live-in, or the input is badly hosed.
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000827 //
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000828 SmallSet<unsigned, 4> ReloadedRegs;
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000829 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
830 MachineOperand& MO = MI->getOperand(i);
831 // here we are looking for only used operands (never def&use)
Dan Gohmand735b802008-10-03 15:45:36 +0000832 if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000833 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000834 MI = reloadVirtReg(MBB, MI, i, ReloadedRegs);
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000835 }
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000836
Evan Chengddee8422006-11-15 20:55:15 +0000837 // If this instruction is the last user of this register, kill the
Chris Lattner56ddada2004-02-17 17:49:10 +0000838 // value, freeing the register being used, so it doesn't need to be
839 // spilled to memory.
840 //
Evan Chengddee8422006-11-15 20:55:15 +0000841 for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
842 unsigned VirtReg = Kills[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000843 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000844 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000845 // If the virtual register was never materialized into a register, it
846 // might not be in the map, but it won't hurt to zero it out anyway.
847 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
848 PhysReg = PhysRegSlot;
849 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000850 } else if (PhysRegsUsed[PhysReg] == -2) {
851 // Unallocatable register dead, ignore.
852 continue;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000853 } else {
Evan Cheng76500d52007-10-22 19:42:28 +0000854 assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
Evan Cheng7ac19af2007-06-26 21:05:13 +0000855 "Silently clearing a virtual register?");
Chris Lattner56ddada2004-02-17 17:49:10 +0000856 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000857
Chris Lattner56ddada2004-02-17 17:49:10 +0000858 if (PhysReg) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000859 DOUT << " Last use of " << TRI->getName(PhysReg)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000860 << "[%reg" << VirtReg <<"], removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000861 removePhysReg(PhysReg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000862 for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
863 *SubRegs; ++SubRegs) {
864 if (PhysRegsUsed[*SubRegs] != -2) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000865 DOUT << " Last use of "
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000866 << TRI->getName(*SubRegs)
Bill Wendlingb2b9c202006-11-17 02:09:07 +0000867 << "[%reg" << VirtReg <<"], removing it from live set\n";
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000868 removePhysReg(*SubRegs);
Evan Chengddee8422006-11-15 20:55:15 +0000869 }
870 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000871 }
872 }
873
874 // Loop over all of the operands of the instruction, spilling registers that
875 // are defined, and marking explicit destinations in the PhysRegsUsed map.
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000876 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
877 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000878 if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000879 !MO.isEarlyClobber() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000880 TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000881 unsigned Reg = MO.getReg();
Chris Lattnercc406322006-09-08 19:11:11 +0000882 if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
Evan Cheng7ac19af2007-06-26 21:05:13 +0000883 // These are extra physical register defs when a sub-register
884 // is defined (def of a sub-register is a read/mod/write of the
885 // larger registers). Ignore.
886 if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
887
Chris Lattner84bc5422007-12-31 04:13:23 +0000888 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Chengddee8422006-11-15 20:55:15 +0000889 spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
Chris Lattner91a452b2003-01-13 00:25:40 +0000890 PhysRegsUsed[Reg] = 0; // It is free and reserved now
Evan Cheng7ac19af2007-06-26 21:05:13 +0000891 AddToPhysRegsUseOrder(Reg);
892
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000893 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
894 *SubRegs; ++SubRegs) {
895 if (PhysRegsUsed[*SubRegs] != -2) {
896 MF->getRegInfo().setPhysRegUsed(*SubRegs);
897 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
898 AddToPhysRegsUseOrder(*SubRegs);
Chris Lattner45d57882006-09-08 19:03:30 +0000899 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000900 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000901 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000902 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000903
904 // Loop over the implicit defs, spilling them as well.
Jim Laskeycd4317e2006-07-21 21:15:20 +0000905 if (TID.ImplicitDefs) {
906 for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
907 *ImplicitDefs; ++ImplicitDefs) {
908 unsigned Reg = *ImplicitDefs;
Evan Cheng7ac19af2007-06-26 21:05:13 +0000909 if (PhysRegsUsed[Reg] != -2) {
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000910 spillPhysReg(MBB, MI, Reg, true);
Evan Cheng7ac19af2007-06-26 21:05:13 +0000911 AddToPhysRegsUseOrder(Reg);
Chris Lattner2b41b8e2006-09-19 18:02:01 +0000912 PhysRegsUsed[Reg] = 0; // It is free and reserved now
913 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000914 MF->getRegInfo().setPhysRegUsed(Reg);
Evan Cheng5a3c6a82009-01-29 02:20:59 +0000915 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
916 *SubRegs; ++SubRegs) {
917 if (PhysRegsUsed[*SubRegs] != -2) {
918 AddToPhysRegsUseOrder(*SubRegs);
919 PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
920 MF->getRegInfo().setPhysRegUsed(*SubRegs);
Chris Lattner45d57882006-09-08 19:03:30 +0000921 }
Jim Laskeycd4317e2006-07-21 21:15:20 +0000922 }
Alkis Evlogimenos19b64862004-01-13 06:24:30 +0000923 }
Alkis Evlogimenosefe995a2003-12-13 01:20:58 +0000924 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000925
Evan Chengddee8422006-11-15 20:55:15 +0000926 SmallVector<unsigned, 8> DeadDefs;
927 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
928 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000929 if (MO.isReg() && MO.isDead())
Evan Chengddee8422006-11-15 20:55:15 +0000930 DeadDefs.push_back(MO.getReg());
931 }
932
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000933 // Okay, we have allocated all of the source operands and spilled any values
934 // that would be destroyed by defs of this instruction. Loop over the
Chris Lattner0648b162005-01-23 22:51:56 +0000935 // explicit defs and assign them to a register, spilling incoming values if
Chris Lattner91a452b2003-01-13 00:25:40 +0000936 // we need to scavenge a register.
Chris Lattner82bee0f2002-12-18 08:14:26 +0000937 //
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000938 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
939 MachineOperand& MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000940 if (MO.isReg() && MO.isDef() && MO.getReg() &&
Dale Johannesen8e3455b2008-09-24 23:13:09 +0000941 !MO.isEarlyClobber() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000942 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000943 unsigned DestVirtReg = MO.getReg();
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000944 unsigned DestPhysReg;
945
Alkis Evlogimenos9af9dbd2003-12-18 13:08:52 +0000946 // If DestVirtReg already has a value, use it.
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +0000947 if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000948 DestPhysReg = getReg(MBB, MI, DestVirtReg);
Chris Lattner84bc5422007-12-31 04:13:23 +0000949 MF->getRegInfo().setPhysRegUsed(DestPhysReg);
Chris Lattnerd5725632003-05-12 03:54:14 +0000950 markVirtRegModified(DestVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +0000951 getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000952 DOUT << " Assigning " << TRI->getName(DestPhysReg)
Evan Cheng9af70902008-02-22 19:57:06 +0000953 << " to %reg" << DestVirtReg << "\n";
Dan Gohman85e68152008-07-09 20:12:26 +0000954 MO.setReg(DestPhysReg); // Assign the output register
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000955 }
Alkis Evlogimenos71e353e2004-02-26 22:00:20 +0000956 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000957
Chris Lattner56ddada2004-02-17 17:49:10 +0000958 // If this instruction defines any registers that are immediately dead,
959 // kill them now.
960 //
Evan Chengddee8422006-11-15 20:55:15 +0000961 for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
962 unsigned VirtReg = DeadDefs[i];
Chris Lattner56ddada2004-02-17 17:49:10 +0000963 unsigned PhysReg = VirtReg;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000964 if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
Chris Lattner56ddada2004-02-17 17:49:10 +0000965 unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
966 PhysReg = PhysRegSlot;
967 assert(PhysReg != 0);
968 PhysRegSlot = 0;
Chris Lattner0c5b8da2006-09-08 20:21:31 +0000969 } else if (PhysRegsUsed[PhysReg] == -2) {
970 // Unallocatable register dead, ignore.
971 continue;
Chris Lattner56ddada2004-02-17 17:49:10 +0000972 }
Chris Lattner91a452b2003-01-13 00:25:40 +0000973
Chris Lattner56ddada2004-02-17 17:49:10 +0000974 if (PhysReg) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000975 DOUT << " Register " << TRI->getName(PhysReg)
Chris Lattner56ddada2004-02-17 17:49:10 +0000976 << " [%reg" << VirtReg
Matthijs Kooijmanfaa3d822008-11-24 16:01:21 +0000977 << "] is never used, removing it from live set\n";
Chris Lattner56ddada2004-02-17 17:49:10 +0000978 removePhysReg(PhysReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000979 for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
Evan Chengddee8422006-11-15 20:55:15 +0000980 *AliasSet; ++AliasSet) {
981 if (PhysRegsUsed[*AliasSet] != -2) {
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000982 DOUT << " Register " << TRI->getName(*AliasSet)
Evan Chengddee8422006-11-15 20:55:15 +0000983 << " [%reg" << *AliasSet
Matthijs Kooijmanfaa3d822008-11-24 16:01:21 +0000984 << "] is never used, removing it from live set\n";
Evan Chengddee8422006-11-15 20:55:15 +0000985 removePhysReg(*AliasSet);
986 }
987 }
Chris Lattner82bee0f2002-12-18 08:14:26 +0000988 }
989 }
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000990
Bob Wilson9d928c22009-05-07 23:47:03 +0000991 // Finally, if this is a noop copy instruction, zap it. (Except that if
992 // the copy is dead, it must be kept to avoid messing up liveness info for
993 // the register scavenger. See pr4100.)
Evan Cheng04ee5a12009-01-20 19:12:24 +0000994 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
995 if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
Bob Wilson9d928c22009-05-07 23:47:03 +0000996 SrcReg == DstReg && DeadDefs.empty())
Chris Lattnere6a88ac2005-11-09 18:22:42 +0000997 MBB.erase(MI);
Chris Lattnerb74e83c2002-12-16 16:15:28 +0000998 }
999
Chris Lattnere6a88ac2005-11-09 18:22:42 +00001000 MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001001
1002 // Spill all physical registers holding virtual registers now.
Dan Gohman6f0d0242008-02-10 18:45:23 +00001003 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001004 if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
Chris Lattner64667b62004-02-09 01:26:13 +00001005 if (unsigned VirtReg = PhysRegsUsed[i])
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +00001006 spillVirtReg(MBB, MI, VirtReg, i);
Chris Lattner64667b62004-02-09 01:26:13 +00001007 else
1008 removePhysReg(i);
Anton Korobeynikov4aefd6b2008-02-20 12:07:57 +00001009 }
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001010
Chris Lattner9a5ef202005-11-09 05:28:45 +00001011#if 0
1012 // This checking code is very expensive.
Chris Lattnerecea5632004-02-09 02:12:04 +00001013 bool AllOk = true;
Dan Gohman6f0d0242008-02-10 18:45:23 +00001014 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner84bc5422007-12-31 04:13:23 +00001015 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Chris Lattnerecea5632004-02-09 02:12:04 +00001016 if (unsigned PR = Virt2PhysRegMap[i]) {
Bill Wendling832171c2006-12-07 20:04:42 +00001017 cerr << "Register still mapped: " << i << " -> " << PR << "\n";
Chris Lattnerecea5632004-02-09 02:12:04 +00001018 AllOk = false;
1019 }
1020 assert(AllOk && "Virtual registers still in phys regs?");
1021#endif
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001022
Chris Lattner128c2aa2003-08-17 18:01:15 +00001023 // Clear any physical register which appear live at the end of the basic
1024 // block, but which do not hold any virtual registers. e.g., the stack
1025 // pointer.
1026 PhysRegsUseOrder.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001027}
1028
1029/// runOnMachineFunction - Register allocate the whole function
1030///
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001031bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingb2b9c202006-11-17 02:09:07 +00001032 DOUT << "Machine Function " << "\n";
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001033 MF = &Fn;
Chris Lattner580f9be2002-12-28 20:40:43 +00001034 TM = &Fn.getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +00001035 TRI = TM->getRegisterInfo();
Owen Anderson6425f8b2008-01-07 01:35:56 +00001036 TII = TM->getInstrInfo();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001037
Dan Gohman6f0d0242008-02-10 18:45:23 +00001038 PhysRegsUsed.assign(TRI->getNumRegs(), -1);
Chris Lattner45d57882006-09-08 19:03:30 +00001039
1040 // At various places we want to efficiently check to see whether a register
1041 // is allocatable. To handle this, we mark all unallocatable registers as
1042 // being pinned down, permanently.
1043 {
Dan Gohman6f0d0242008-02-10 18:45:23 +00001044 BitVector Allocable = TRI->getAllocatableSet(Fn);
Chris Lattner45d57882006-09-08 19:03:30 +00001045 for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
1046 if (!Allocable[i])
1047 PhysRegsUsed[i] = -2; // Mark the reg unallocable.
1048 }
Chris Lattner64667b62004-02-09 01:26:13 +00001049
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001050 // initialize the virtual->physical register map to have a 'null'
1051 // mapping for all virtual registers
Evan Cheng644340a2008-01-17 00:35:26 +00001052 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Chengbdb10fe2008-07-10 18:23:23 +00001053 StackSlotForVirtReg.grow(LastVirtReg);
Evan Cheng644340a2008-01-17 00:35:26 +00001054 Virt2PhysRegMap.grow(LastVirtReg);
Evan Cheng839b7592008-01-17 02:08:17 +00001055 Virt2LastUseMap.grow(LastVirtReg);
Dan Gohman6f0d0242008-02-10 18:45:23 +00001056 VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
Owen Anderson491fccc2008-07-08 22:24:50 +00001057 UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
1058
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001059 // Loop over all of the basic blocks, eliminating virtual register references
1060 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
1061 MBB != MBBe; ++MBB)
1062 AllocateBasicBlock(*MBB);
1063
Chris Lattner580f9be2002-12-28 20:40:43 +00001064 StackSlotForVirtReg.clear();
Alkis Evlogimenos4de473b2004-02-13 18:20:47 +00001065 PhysRegsUsed.clear();
Chris Lattner91a452b2003-01-13 00:25:40 +00001066 VirtRegModified.clear();
Owen Anderson491fccc2008-07-08 22:24:50 +00001067 UsedInMultipleBlocks.clear();
Chris Lattnerecea5632004-02-09 02:12:04 +00001068 Virt2PhysRegMap.clear();
Evan Cheng839b7592008-01-17 02:08:17 +00001069 Virt2LastUseMap.clear();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001070 return true;
1071}
1072
Chris Lattneref09c632004-01-31 21:27:19 +00001073FunctionPass *llvm::createLocalRegisterAllocator() {
Bill Wendlinge23e00d2007-05-08 19:02:46 +00001074 return new RALocal();
Chris Lattnerb74e83c2002-12-16 16:15:28 +00001075}