Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1 | //===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===// |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 9 | // |
| 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 Lattner | 4cc662b | 2003-08-03 21:47:31 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "regalloc" |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | 22ff3ee | 2008-02-06 08:00:32 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 743a1e6 | 2008-07-10 01:56:35 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/IndexedMap.h" |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 2fc628d | 2008-02-06 19:16:53 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 38 | STATISTIC(NumStores, "Number of stores added"); |
| 39 | STATISTIC(NumLoads , "Number of loads added"); |
Jakob Stoklund Olesen | ab2d008 | 2010-05-14 22:40:40 +0000 | [diff] [blame] | 40 | STATISTIC(NumCopies, "Number of copies coalesced"); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 41 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | static RegisterRegAlloc |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 43 | localRegAlloc("local", "local register allocator", |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 44 | createLocalRegisterAllocator); |
| 45 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 46 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 47 | class RALocal : public MachineFunctionPass { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 48 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 49 | static char ID; |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 50 | RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 51 | private: |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 52 | const TargetMachine *TM; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 53 | MachineFunction *MF; |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 54 | MachineRegisterInfo *MRI; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 55 | const TargetRegisterInfo *TRI; |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 56 | const TargetInstrInfo *TII; |
Chris Lattner | ff863ba | 2002-12-25 05:05:46 +0000 | [diff] [blame] | 57 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 58 | // StackSlotForVirtReg - Maps virtual regs to the frame index where these |
| 59 | // values are spilled. |
Evan Cheng | bdb10fe | 2008-07-10 18:23:23 +0000 | [diff] [blame] | 60 | IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 61 | |
| 62 | // Virt2PhysRegMap - This map contains entries for each virtual register |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 63 | // that is currently available in a physical register. |
Chris Lattner | 94c002a | 2007-02-01 05:32:05 +0000 | [diff] [blame] | 64 | IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) { |
Alkis Evlogimenos | 4d0d864 | 2004-02-25 21:55:45 +0000 | [diff] [blame] | 67 | return Virt2PhysRegMap[VirtReg]; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 68 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 70 | // PhysRegsUsed - This array is effectively a map, containing entries for |
| 71 | // each physical register that currently has a value (ie, it is in |
| 72 | // Virt2PhysRegMap). The value mapped to is the virtual register |
| 73 | // corresponding to the physical register (the inverse of the |
| 74 | // Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 75 | // because it is used by a future instruction, and to -2 if it is not |
| 76 | // allocatable. If the entry for a physical register is -1, then the |
| 77 | // physical register is "not in the map". |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 78 | // |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 79 | std::vector<int> PhysRegsUsed; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 80 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 81 | // PhysRegsUseOrder - This contains a list of the physical registers that |
| 82 | // currently have a virtual register value in them. This list provides an |
| 83 | // ordering of registers, imposing a reallocation order. This list is only |
| 84 | // used if all registers are allocated and we have to spill one, in which |
| 85 | // case we spill the least recently used register. Entries at the front of |
| 86 | // the list are the least recently used registers, entries at the back are |
| 87 | // the most recently used. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 88 | // |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 89 | std::vector<unsigned> PhysRegsUseOrder; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 90 | |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 91 | // Virt2LastUseMap - This maps each virtual register to its last use |
| 92 | // (MachineInstr*, operand index pair). |
| 93 | IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor> |
| 94 | Virt2LastUseMap; |
| 95 | |
| 96 | std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 97 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 98 | return Virt2LastUseMap[Reg]; |
| 99 | } |
| 100 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 101 | // VirtRegModified - This bitset contains information about which virtual |
| 102 | // registers need to be spilled back to memory when their registers are |
| 103 | // scavenged. If a virtual register has simply been rematerialized, there |
| 104 | // is no reason to spill it to memory when we need the register back. |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 105 | // |
Evan Cheng | 644340a | 2008-01-17 00:35:26 +0000 | [diff] [blame] | 106 | BitVector VirtRegModified; |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 107 | |
| 108 | // UsedInMultipleBlocks - Tracks whether a particular register is used in |
| 109 | // more than one block. |
| 110 | BitVector UsedInMultipleBlocks; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 111 | |
| 112 | void markVirtRegModified(unsigned Reg, bool Val = true) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 113 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
| 114 | Reg -= TargetRegisterInfo::FirstVirtualRegister; |
Evan Cheng | 644340a | 2008-01-17 00:35:26 +0000 | [diff] [blame] | 115 | if (Val) |
| 116 | VirtRegModified.set(Reg); |
| 117 | else |
| 118 | VirtRegModified.reset(Reg); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool isVirtRegModified(unsigned Reg) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 122 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!"); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 123 | assert(Reg - TargetRegisterInfo::FirstVirtualRegister < |
| 124 | VirtRegModified.size() && "Illegal virtual register!"); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 125 | return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister]; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 127 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 128 | void AddToPhysRegsUseOrder(unsigned Reg) { |
| 129 | std::vector<unsigned>::iterator It = |
| 130 | std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg); |
| 131 | if (It != PhysRegsUseOrder.end()) |
| 132 | PhysRegsUseOrder.erase(It); |
| 133 | PhysRegsUseOrder.push_back(Reg); |
| 134 | } |
| 135 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 136 | void MarkPhysRegRecentlyUsed(unsigned Reg) { |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 137 | if (PhysRegsUseOrder.empty() || |
| 138 | PhysRegsUseOrder.back() == Reg) return; // Already most recently used |
| 139 | |
| 140 | for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i) { |
| 141 | unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle |
| 142 | if (!areRegsEqual(Reg, RegMatch)) continue; |
| 143 | |
| 144 | PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1); |
| 145 | // Add it to the end of the list |
| 146 | PhysRegsUseOrder.push_back(RegMatch); |
| 147 | if (RegMatch == Reg) |
| 148 | return; // Found an exact match, exit early |
| 149 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public: |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 153 | virtual const char *getPassName() const { |
| 154 | return "Local Register Allocator"; |
| 155 | } |
| 156 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 157 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 158 | AU.setPreservesCFG(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 159 | AU.addRequiredID(PHIEliminationID); |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 160 | AU.addRequiredID(TwoAddressInstructionPassID); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 161 | MachineFunctionPass::getAnalysisUsage(AU); |
| 162 | } |
| 163 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 164 | private: |
| 165 | /// runOnMachineFunction - Register allocate the whole function |
| 166 | bool runOnMachineFunction(MachineFunction &Fn); |
| 167 | |
| 168 | /// AllocateBasicBlock - Register allocate the specified basic block. |
| 169 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
| 170 | |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 172 | /// areRegsEqual - This method returns true if the specified registers are |
| 173 | /// related to each other. To do this, it checks to see if they are equal |
| 174 | /// or if the first register is in the alias set of the second register. |
| 175 | /// |
| 176 | bool areRegsEqual(unsigned R1, unsigned R2) const { |
| 177 | if (R1 == R2) return true; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 178 | for (const unsigned *AliasSet = TRI->getAliasSet(R2); |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 179 | *AliasSet; ++AliasSet) { |
| 180 | if (*AliasSet == R1) return true; |
| 181 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 185 | /// getStackSpaceFor - This returns the frame index of the specified virtual |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 186 | /// register on the stack, allocating space if necessary. |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 187 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 188 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 189 | /// removePhysReg - This method marks the specified physical register as no |
| 190 | /// longer being in use. |
| 191 | /// |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 192 | void removePhysReg(unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 193 | |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 194 | void storeVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 195 | unsigned VirtReg, unsigned PhysReg, bool isKill); |
| 196 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 197 | /// spillVirtReg - This method spills the value specified by PhysReg into |
| 198 | /// the virtual register slot specified by VirtReg. It then updates the RA |
| 199 | /// data structures to indicate the fact that PhysReg is now available. |
| 200 | /// |
Chris Lattner | 688c825 | 2004-02-22 19:08:15 +0000 | [diff] [blame] | 201 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 202 | unsigned VirtReg, unsigned PhysReg); |
| 203 | |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 204 | /// spillPhysReg - This method spills the specified physical register into |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 205 | /// the virtual register slot associated with it. If OnlyVirtRegs is set to |
| 206 | /// true, then the request is ignored if the physical register does not |
| 207 | /// contain a virtual register. |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 208 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 209 | void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 210 | unsigned PhysReg, bool OnlyVirtRegs = false); |
Chris Lattner | c21be92 | 2002-12-16 17:44:42 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 212 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 213 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 214 | /// register must not be used for anything else when this is called. |
| 215 | /// |
| 216 | void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg); |
| 217 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 218 | /// isPhysRegAvailable - Return true if the specified physical register is |
| 219 | /// free and available for use. This also includes checking to see if |
| 220 | /// aliased registers are all free... |
| 221 | /// |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 222 | bool isPhysRegAvailable(unsigned PhysReg) const; |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 223 | |
| 224 | /// getFreeReg - Look to see if there is a free register available in the |
| 225 | /// specified register class. If not, return 0. |
| 226 | /// |
| 227 | unsigned getFreeReg(const TargetRegisterClass *RC); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 229 | /// getReg - Find a physical register to hold the specified virtual |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 230 | /// register. If all compatible physical registers are used, this method |
| 231 | /// spills the last used virtual register to the stack, and uses that |
Evan Cheng | 7ddee0a | 2009-01-29 01:13:00 +0000 | [diff] [blame] | 232 | /// register. If NoFree is true, that means the caller knows there isn't |
| 233 | /// a free register, do not call getFreeReg(). |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 234 | unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Evan Cheng | 7ddee0a | 2009-01-29 01:13:00 +0000 | [diff] [blame] | 235 | unsigned VirtReg, bool NoFree = false); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 236 | |
Bob Wilson | e0f745b | 2009-05-07 21:19:45 +0000 | [diff] [blame] | 237 | /// reloadVirtReg - This method transforms the specified virtual |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 238 | /// register use to refer to a physical register. This method may do this |
| 239 | /// in one of several ways: if the register is available in a physical |
| 240 | /// register already, it uses that physical register. If the value is not |
| 241 | /// in a physical register, and if there are physical registers available, |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 242 | /// it loads it into a register: PhysReg if that is an available physical |
| 243 | /// register, otherwise any physical register of the right class. |
| 244 | /// If register pressure is high, and it is possible, it tries to fold the |
| 245 | /// load of the virtual register into the instruction itself. It avoids |
| 246 | /// doing this if register pressure is low to improve the chance that |
| 247 | /// subsequent instructions can use the reloaded value. This method |
| 248 | /// returns the modified instruction. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 249 | /// |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 250 | MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 251 | unsigned OpNum, SmallSet<unsigned, 4> &RRegs, |
| 252 | unsigned PhysReg); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 253 | |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 254 | /// ComputeLocalLiveness - Computes liveness of registers within a basic |
| 255 | /// block, setting the killed/dead flags as appropriate. |
| 256 | void ComputeLocalLiveness(MachineBasicBlock& MBB); |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 257 | |
| 258 | void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I, |
| 259 | unsigned PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 260 | }; |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 261 | char RALocal::ID = 0; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 264 | /// getStackSpaceFor - This allocates space for the specified virtual register |
| 265 | /// to be held on the stack. |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 266 | int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) { |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 267 | // Find the location Reg would belong... |
Evan Cheng | bdb10fe | 2008-07-10 18:23:23 +0000 | [diff] [blame] | 268 | int SS = StackSlotForVirtReg[VirtReg]; |
| 269 | if (SS != -1) |
| 270 | return SS; // Already has space allocated? |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 271 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 272 | // Allocate a new stack object for this spill location... |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 273 | int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(), |
| 274 | RC->getAlignment()); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 276 | // Assign the slot. |
Evan Cheng | bdb10fe | 2008-07-10 18:23:23 +0000 | [diff] [blame] | 277 | StackSlotForVirtReg[VirtReg] = FrameIdx; |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 278 | return FrameIdx; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 281 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 282 | /// removePhysReg - This method marks the specified physical register as no |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 283 | /// longer being in use. |
| 284 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 285 | void RALocal::removePhysReg(unsigned PhysReg) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 286 | PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 287 | |
| 288 | std::vector<unsigned>::iterator It = |
| 289 | std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg); |
| 290 | if (It != PhysRegsUseOrder.end()) |
| 291 | PhysRegsUseOrder.erase(It); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 294 | /// storeVirtReg - Store a virtual register to its assigned stack slot. |
| 295 | void RALocal::storeVirtReg(MachineBasicBlock &MBB, |
| 296 | MachineBasicBlock::iterator I, |
| 297 | unsigned VirtReg, unsigned PhysReg, |
| 298 | bool isKill) { |
| 299 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
| 300 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
| 301 | DEBUG(dbgs() << " to stack slot #" << FrameIndex); |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 302 | TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC, TRI); |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 303 | ++NumStores; // Update statistics |
| 304 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 305 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 306 | /// spillVirtReg - This method spills the value specified by PhysReg into the |
| 307 | /// virtual register slot specified by VirtReg. It then updates the RA data |
| 308 | /// structures to indicate the fact that PhysReg is now available. |
| 309 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 310 | void RALocal::spillVirtReg(MachineBasicBlock &MBB, |
| 311 | MachineBasicBlock::iterator I, |
| 312 | unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 8c81945 | 2003-08-05 04:13:58 +0000 | [diff] [blame] | 313 | assert(VirtReg && "Spilling a physical register is illegal!" |
Chris Lattner | d9ac6a7 | 2003-08-05 00:49:09 +0000 | [diff] [blame] | 314 | " Must not have appropriate kill for the register or use exists beyond" |
| 315 | " the intended one."); |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 316 | DEBUG(dbgs() << " Spilling register " << TRI->getName(PhysReg) |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 317 | << " containing %reg" << VirtReg); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 318 | |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 319 | if (!isVirtRegModified(VirtReg)) { |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 320 | DEBUG(dbgs() << " which has not been modified, so no store necessary!"); |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 321 | std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg); |
| 322 | if (LastUse.first) |
| 323 | LastUse.first->getOperand(LastUse.second).setIsKill(); |
Evan Cheng | 2fc628d | 2008-02-06 19:16:53 +0000 | [diff] [blame] | 324 | } else { |
| 325 | // Otherwise, there is a virtual register corresponding to this physical |
| 326 | // register. We only need to spill it into its stack slot if it has been |
| 327 | // modified. |
Evan Cheng | 2fc628d | 2008-02-06 19:16:53 +0000 | [diff] [blame] | 328 | // If the instruction reads the register that's spilled, (e.g. this can |
| 329 | // happen if it is a move to a physical register), then the spill |
| 330 | // instruction is not a kill. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 331 | bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg)); |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 332 | storeVirtReg(MBB, I, VirtReg, PhysReg, isKill); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 333 | } |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 334 | |
| 335 | getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 336 | |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 337 | DEBUG(dbgs() << '\n'); |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 338 | removePhysReg(PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 342 | /// spillPhysReg - This method spills the specified physical register into the |
Chris Lattner | 128c2aa | 2003-08-17 18:01:15 +0000 | [diff] [blame] | 343 | /// virtual register slot associated with it. If OnlyVirtRegs is set to true, |
| 344 | /// then the request is ignored if the physical register does not contain a |
| 345 | /// virtual register. |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 346 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 347 | void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I, |
| 348 | unsigned PhysReg, bool OnlyVirtRegs) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 349 | if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used! |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 350 | assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!"); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 351 | if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs) |
| 352 | spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | // If the selected register aliases any other registers, we must make |
| 357 | // sure that one of the aliases isn't alive. |
| 358 | for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg); |
| 359 | *AliasSet; ++AliasSet) { |
| 360 | if (PhysRegsUsed[*AliasSet] == -1 || // Spill aliased register. |
| 361 | PhysRegsUsed[*AliasSet] == -2) // If allocatable. |
| 362 | continue; |
| 363 | |
| 364 | if (PhysRegsUsed[*AliasSet]) |
| 365 | spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /// assignVirtToPhysReg - This method updates local state so that we know |
| 371 | /// that PhysReg is the proper container for VirtReg now. The physical |
| 372 | /// register must not be used for anything else when this is called. |
| 373 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 374 | void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 375 | assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!"); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 376 | // Update information to note the fact that this register was just used, and |
| 377 | // it holds VirtReg. |
| 378 | PhysRegsUsed[PhysReg] = VirtReg; |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 379 | getVirt2PhysRegMapSlot(VirtReg) = PhysReg; |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 380 | AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 384 | /// isPhysRegAvailable - Return true if the specified physical register is free |
| 385 | /// and available for use. This also includes checking to see if aliased |
| 386 | /// registers are all free... |
| 387 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 388 | bool RALocal::isPhysRegAvailable(unsigned PhysReg) const { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 389 | if (PhysRegsUsed[PhysReg] != -1) return false; |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 390 | |
| 391 | // If the selected register aliases any other allocated registers, it is |
| 392 | // not free! |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 393 | for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg); |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 394 | *AliasSet; ++AliasSet) |
Evan Cheng | bcfa1ca | 2008-02-22 20:30:53 +0000 | [diff] [blame] | 395 | if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use? |
Alkis Evlogimenos | 73ff512 | 2003-10-08 05:20:08 +0000 | [diff] [blame] | 396 | return false; // Can't use this reg then. |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
| 400 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 401 | /// getFreeReg - Look to see if there is a free register available in the |
| 402 | /// specified register class. If not, return 0. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 403 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 404 | unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) { |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 405 | // Get iterators defining the range of registers that are valid to allocate in |
| 406 | // this class, which also specifies the preferred allocation order. |
| 407 | TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF); |
| 408 | TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF); |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 410 | for (; RI != RE; ++RI) |
| 411 | if (isPhysRegAvailable(*RI)) { // Is reg unused? |
| 412 | assert(*RI != 0 && "Cannot use register!"); |
| 413 | return *RI; // Found an unused register! |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 419 | /// getReg - Find a physical register to hold the specified virtual |
| 420 | /// register. If all compatible physical registers are used, this method spills |
| 421 | /// the last used virtual register to the stack, and uses that register. |
| 422 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 423 | unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I, |
Evan Cheng | 7ddee0a | 2009-01-29 01:13:00 +0000 | [diff] [blame] | 424 | unsigned VirtReg, bool NoFree) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 425 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 426 | |
| 427 | // First check to see if we have a free register of the requested type... |
Evan Cheng | 7ddee0a | 2009-01-29 01:13:00 +0000 | [diff] [blame] | 428 | unsigned PhysReg = NoFree ? 0 : getFreeReg(RC); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 430 | if (PhysReg != 0) { |
| 431 | // Assign the register. |
| 432 | assignVirtToPhysReg(VirtReg, PhysReg); |
| 433 | return PhysReg; |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // If we didn't find an unused register, scavenge one now! |
| 437 | assert(!PhysRegsUseOrder.empty() && "No allocated registers??"); |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 438 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 439 | // Loop over all of the preallocated registers from the least recently used |
| 440 | // to the most recently used. When we find one that is capable of holding |
| 441 | // our register, use it. |
| 442 | for (unsigned i = 0; PhysReg == 0; ++i) { |
| 443 | assert(i != PhysRegsUseOrder.size() && |
| 444 | "Couldn't find a register of the appropriate class!"); |
| 445 | |
| 446 | unsigned R = PhysRegsUseOrder[i]; |
| 447 | |
| 448 | // We can only use this register if it holds a virtual register (ie, it |
| 449 | // can be spilled). Do not use it if it is an explicitly allocated |
| 450 | // physical register! |
| 451 | assert(PhysRegsUsed[R] != -1 && |
| 452 | "PhysReg in PhysRegsUseOrder, but is not allocated?"); |
| 453 | if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) { |
| 454 | // If the current register is compatible, use it. |
| 455 | if (RC->contains(R)) { |
| 456 | PhysReg = R; |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | // If one of the registers aliased to the current register is |
| 461 | // compatible, use it. |
| 462 | for (const unsigned *AliasIt = TRI->getAliasSet(R); |
| 463 | *AliasIt; ++AliasIt) { |
| 464 | if (!RC->contains(*AliasIt)) continue; |
| 465 | |
| 466 | // If this is pinned down for some reason, don't use it. For |
| 467 | // example, if CL is pinned, and we run across CH, don't use |
| 468 | // CH as justification for using scavenging ECX (which will |
| 469 | // fail). |
| 470 | if (PhysRegsUsed[*AliasIt] == 0) continue; |
| 471 | |
| 472 | // Make sure the register is allocatable. Don't allocate SIL on |
| 473 | // x86-32. |
| 474 | if (PhysRegsUsed[*AliasIt] == -2) continue; |
| 475 | |
| 476 | PhysReg = *AliasIt; // Take an aliased register |
| 477 | break; |
| 478 | } |
| 479 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 482 | assert(PhysReg && "Physical register not assigned!?!?"); |
| 483 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 484 | // At this point PhysRegsUseOrder[i] is the least recently used register of |
| 485 | // compatible register class. Spill it to memory and reap its remains. |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 486 | spillPhysReg(MBB, I, PhysReg); |
| 487 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 488 | // Now that we know which register we need to assign this to, do it now! |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 489 | assignVirtToPhysReg(VirtReg, PhysReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 490 | return PhysReg; |
| 491 | } |
| 492 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 493 | |
Bob Wilson | 8d24f41 | 2009-05-07 21:20:42 +0000 | [diff] [blame] | 494 | /// reloadVirtReg - This method transforms the specified virtual |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 495 | /// register use to refer to a physical register. This method may do this in |
| 496 | /// one of several ways: if the register is available in a physical register |
| 497 | /// already, it uses that physical register. If the value is not in a physical |
| 498 | /// register, and if there are physical registers available, it loads it into a |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 499 | /// register: PhysReg if that is an available physical register, otherwise any |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 500 | /// register. If register pressure is high, and it is possible, it tries to |
| 501 | /// fold the load of the virtual register into the instruction itself. It |
| 502 | /// avoids doing this if register pressure is low to improve the chance that |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 503 | /// subsequent instructions can use the reloaded value. This method returns |
| 504 | /// the modified instruction. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 505 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 506 | MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI, |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 507 | unsigned OpNum, |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 508 | SmallSet<unsigned, 4> &ReloadedRegs, |
| 509 | unsigned PhysReg) { |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 510 | unsigned VirtReg = MI->getOperand(OpNum).getReg(); |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 511 | unsigned SubIdx = MI->getOperand(OpNum).getSubReg(); |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 512 | |
| 513 | // If the virtual register is already available, just update the instruction |
| 514 | // and return. |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 515 | if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) { |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 516 | if (SubIdx) { |
| 517 | PR = TRI->getSubReg(PR, SubIdx); |
| 518 | MI->getOperand(OpNum).setSubReg(0); |
| 519 | } |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 520 | MI->getOperand(OpNum).setReg(PR); // Assign the input register |
Dale Johannesen | f463d95 | 2010-02-16 01:27:47 +0000 | [diff] [blame] | 521 | if (!MI->isDebugValue()) { |
| 522 | // Do not do these for DBG_VALUE as they can affect codegen. |
| 523 | MarkPhysRegRecentlyUsed(PR); // Already have this value available! |
Dale Johannesen | 3da6e09 | 2010-02-15 01:45:47 +0000 | [diff] [blame] | 524 | getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum); |
Dale Johannesen | f463d95 | 2010-02-16 01:27:47 +0000 | [diff] [blame] | 525 | } |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 526 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 529 | // Otherwise, we need to fold it into the current instruction, or reload it. |
| 530 | // If we have registers available to hold the value, use them. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 531 | const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg); |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 532 | // If we already have a PhysReg (this happens when the instruction is a |
| 533 | // reg-to-reg copy with a PhysReg destination) use that. |
| 534 | if (!PhysReg || !TargetRegisterInfo::isPhysicalRegister(PhysReg) || |
| 535 | !isPhysRegAvailable(PhysReg)) |
| 536 | PhysReg = getFreeReg(RC); |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 537 | int FrameIndex = getStackSpaceFor(VirtReg, RC); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 11390e7 | 2004-02-17 08:09:40 +0000 | [diff] [blame] | 539 | if (PhysReg) { // Register is available, allocate it! |
| 540 | assignVirtToPhysReg(VirtReg, PhysReg); |
| 541 | } else { // No registers available. |
Evan Cheng | 27240c7 | 2008-02-07 19:46:55 +0000 | [diff] [blame] | 542 | // Force some poor hapless value out of the register file to |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 543 | // make room for the new register, and reload it. |
Evan Cheng | 7ddee0a | 2009-01-29 01:13:00 +0000 | [diff] [blame] | 544 | PhysReg = getReg(MBB, MI, VirtReg, true); |
Chris Lattner | 1e3812c | 2004-02-17 04:08:37 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 547 | markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded |
| 548 | |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 549 | DEBUG(dbgs() << " Reloading %reg" << VirtReg << " into " |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 550 | << TRI->getName(PhysReg) << "\n"); |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 551 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 552 | // Add move instruction(s) |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 553 | TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC, TRI); |
Alkis Evlogimenos | 2acef2d | 2004-02-19 06:19:09 +0000 | [diff] [blame] | 554 | ++NumLoads; // Update statistics |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 556 | MF->getRegInfo().setPhysRegUsed(PhysReg); |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 557 | // Assign the input register. |
| 558 | if (SubIdx) { |
| 559 | MI->getOperand(OpNum).setSubReg(0); |
| 560 | MI->getOperand(OpNum).setReg(TRI->getSubReg(PhysReg, SubIdx)); |
| 561 | } else |
| 562 | MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 563 | getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 564 | |
| 565 | if (!ReloadedRegs.insert(PhysReg)) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 566 | std::string msg; |
| 567 | raw_string_ostream Msg(msg); |
| 568 | Msg << "Ran out of registers during register allocation!"; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 569 | if (MI->isInlineAsm()) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 570 | Msg << "\nPlease check your inline asm statement for invalid " |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 571 | << "constraints:\n"; |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 572 | MI->print(Msg, TM); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 573 | } |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 574 | report_fatal_error(Msg.str()); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 575 | } |
| 576 | for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg); |
| 577 | *SubRegs; ++SubRegs) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 578 | if (ReloadedRegs.insert(*SubRegs)) continue; |
| 579 | |
| 580 | std::string msg; |
| 581 | raw_string_ostream Msg(msg); |
| 582 | Msg << "Ran out of registers during register allocation!"; |
| 583 | if (MI->isInlineAsm()) { |
| 584 | Msg << "\nPlease check your inline asm statement for invalid " |
| 585 | << "constraints:\n"; |
| 586 | MI->print(Msg, TM); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 587 | } |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 588 | report_fatal_error(Msg.str()); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | 42e0a8f | 2004-02-17 03:57:19 +0000 | [diff] [blame] | 591 | return MI; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 594 | /// isReadModWriteImplicitKill - True if this is an implicit kill for a |
| 595 | /// read/mod/write register, i.e. update partial register. |
| 596 | static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) { |
| 597 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 598 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 599 | if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() && |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 600 | MO.isDef() && !MO.isDead()) |
| 601 | return true; |
| 602 | } |
| 603 | return false; |
| 604 | } |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 605 | |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 606 | /// isReadModWriteImplicitDef - True if this is an implicit def for a |
| 607 | /// read/mod/write register, i.e. update partial register. |
| 608 | static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) { |
| 609 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 610 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 611 | if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() && |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 612 | !MO.isDef() && MO.isKill()) |
| 613 | return true; |
| 614 | } |
| 615 | return false; |
| 616 | } |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 617 | |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 618 | // precedes - Helper function to determine with MachineInstr A |
| 619 | // precedes MachineInstr B within the same MBB. |
| 620 | static bool precedes(MachineBasicBlock::iterator A, |
| 621 | MachineBasicBlock::iterator B) { |
| 622 | if (A == B) |
| 623 | return false; |
| 624 | |
| 625 | MachineBasicBlock::iterator I = A->getParent()->begin(); |
| 626 | while (I != A->getParent()->end()) { |
| 627 | if (I == A) |
| 628 | return true; |
| 629 | else if (I == B) |
| 630 | return false; |
| 631 | |
| 632 | ++I; |
| 633 | } |
| 634 | |
| 635 | return false; |
| 636 | } |
| 637 | |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 638 | /// ComputeLocalLiveness - Computes liveness of registers within a basic |
| 639 | /// block, setting the killed/dead flags as appropriate. |
| 640 | void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) { |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 641 | // Keep track of the most recently seen previous use or def of each reg, |
| 642 | // so that we can update them with dead/kill markers. |
Owen Anderson | 743a1e6 | 2008-07-10 01:56:35 +0000 | [diff] [blame] | 643 | DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef; |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 644 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 645 | I != E; ++I) { |
Dale Johannesen | 3da6e09 | 2010-02-15 01:45:47 +0000 | [diff] [blame] | 646 | if (I->isDebugValue()) |
| 647 | continue; |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 648 | |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 649 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 650 | MachineOperand &MO = I->getOperand(i); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 651 | // Uses don't trigger any flags, but we need to save |
| 652 | // them for later. Also, we have to process these |
| 653 | // _before_ processing the defs, since an instr |
| 654 | // uses regs before it defs them. |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 655 | if (!MO.isReg() || !MO.getReg() || !MO.isUse()) |
| 656 | continue; |
Jakob Stoklund Olesen | a50fba9 | 2010-05-03 23:49:20 +0000 | [diff] [blame] | 657 | |
| 658 | // Ignore helpful kill flags from earlier passes. |
| 659 | MO.setIsKill(false); |
| 660 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 661 | LastUseDef[MO.getReg()] = std::make_pair(I, i); |
| 662 | |
| 663 | if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue; |
| 664 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 665 | const unsigned *Aliases = TRI->getAliasSet(MO.getReg()); |
| 666 | if (Aliases == 0) |
| 667 | continue; |
| 668 | |
| 669 | while (*Aliases) { |
| 670 | DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator |
| 671 | alias = LastUseDef.find(*Aliases); |
| 672 | |
| 673 | if (alias != LastUseDef.end() && alias->second.first != I) |
| 674 | LastUseDef[*Aliases] = std::make_pair(I, i); |
| 675 | |
| 676 | ++Aliases; |
Owen Anderson | 04764de | 2008-10-08 04:30:51 +0000 | [diff] [blame] | 677 | } |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 678 | } |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 679 | |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 680 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 681 | MachineOperand &MO = I->getOperand(i); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 682 | // Defs others than 2-addr redefs _do_ trigger flag changes: |
| 683 | // - A def followed by a def is dead |
| 684 | // - A use followed by a def is a kill |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 685 | if (!MO.isReg() || !MO.getReg() || !MO.isDef()) continue; |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 686 | |
| 687 | unsigned SubIdx = MO.getSubReg(); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 688 | DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator |
| 689 | last = LastUseDef.find(MO.getReg()); |
| 690 | if (last != LastUseDef.end()) { |
| 691 | // Check if this is a two address instruction. If so, then |
| 692 | // the def does not kill the use. |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 693 | if (last->second.first == I && I->isRegTiedToUseOperand(i)) |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 694 | continue; |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 696 | MachineOperand &lastUD = |
| 697 | last->second.first->getOperand(last->second.second); |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 698 | if (SubIdx && lastUD.getSubReg() != SubIdx) |
| 699 | // Partial re-def, the last def is not dead. |
| 700 | // %reg1024:5<def> = |
| 701 | // %reg1024:6<def> = |
| 702 | // or |
| 703 | // %reg1024:5<def> = op %reg1024, 5 |
| 704 | continue; |
| 705 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 706 | if (lastUD.isDef()) |
| 707 | lastUD.setIsDead(true); |
| 708 | else |
| 709 | lastUD.setIsKill(true); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 710 | } |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 711 | |
| 712 | LastUseDef[MO.getReg()] = std::make_pair(I, i); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | // Live-out (of the function) registers contain return values of the function, |
| 717 | // so we need to make sure they are alive at return time. |
Bill Wendling | b0d2766 | 2010-03-16 02:01:51 +0000 | [diff] [blame] | 718 | MachineBasicBlock::iterator Ret = MBB.getFirstTerminator(); |
| 719 | bool BBEndsInReturn = (Ret != MBB.end() && Ret->getDesc().isReturn()); |
| 720 | |
| 721 | if (BBEndsInReturn) |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 722 | for (MachineRegisterInfo::liveout_iterator |
| 723 | I = MF->getRegInfo().liveout_begin(), |
| 724 | E = MF->getRegInfo().liveout_end(); I != E; ++I) |
| 725 | if (!Ret->readsRegister(*I)) { |
| 726 | Ret->addOperand(MachineOperand::CreateReg(*I, false, true)); |
| 727 | LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1); |
| 728 | } |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 729 | |
| 730 | // Finally, loop over the final use/def of each reg |
| 731 | // in the block and determine if it is dead. |
Owen Anderson | 743a1e6 | 2008-07-10 01:56:35 +0000 | [diff] [blame] | 732 | for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 733 | I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 734 | MachineInstr *MI = I->second.first; |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 735 | unsigned idx = I->second.second; |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 736 | MachineOperand &MO = MI->getOperand(idx); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 737 | |
| 738 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg()); |
| 739 | |
| 740 | // A crude approximation of "live-out" calculation |
| 741 | bool usedOutsideBlock = isPhysReg ? false : |
| 742 | UsedInMultipleBlocks.test(MO.getReg() - |
| 743 | TargetRegisterInfo::FirstVirtualRegister); |
Bill Wendling | 8fe347a | 2010-03-16 01:05:35 +0000 | [diff] [blame] | 744 | |
| 745 | // If the machine BB ends in a return instruction, then the value isn't used |
| 746 | // outside of the BB. |
| 747 | if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) { |
Dale Johannesen | f463d95 | 2010-02-16 01:27:47 +0000 | [diff] [blame] | 748 | // DBG_VALUE complicates this: if the only refs of a register outside |
| 749 | // this block are DBG_VALUE, we can't keep the reg live just for that, |
| 750 | // as it will cause the reg to be spilled at the end of this block when |
| 751 | // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that |
| 752 | // happens. |
| 753 | bool UsedByDebugValueOnly = false; |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 754 | for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()), |
| 755 | UE = MRI->reg_end(); UI != UE; ++UI) { |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 756 | // Two cases: |
| 757 | // - used in another block |
| 758 | // - used in the same block before it is defined (loop) |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 759 | if (UI->getParent() == &MBB && |
| 760 | !(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) |
| 761 | continue; |
| 762 | |
| 763 | if (UI->isDebugValue()) { |
| 764 | UsedByDebugValueOnly = true; |
| 765 | continue; |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 766 | } |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 767 | |
| 768 | // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone. |
| 769 | UsedInMultipleBlocks.set(MO.getReg() - |
| 770 | TargetRegisterInfo::FirstVirtualRegister); |
| 771 | usedOutsideBlock = true; |
| 772 | UsedByDebugValueOnly = false; |
| 773 | break; |
Bill Wendling | 8fe347a | 2010-03-16 01:05:35 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Dale Johannesen | f463d95 | 2010-02-16 01:27:47 +0000 | [diff] [blame] | 776 | if (UsedByDebugValueOnly) |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 777 | for (MachineRegisterInfo::reg_iterator UI = MRI->reg_begin(MO.getReg()), |
| 778 | UE = MRI->reg_end(); UI != UE; ++UI) |
Dale Johannesen | f463d95 | 2010-02-16 01:27:47 +0000 | [diff] [blame] | 779 | if (UI->isDebugValue() && |
| 780 | (UI->getParent() != &MBB || |
| 781 | (MO.isDef() && precedes(&*UI, MI)))) |
| 782 | UI.getOperand().setReg(0U); |
| 783 | } |
| 784 | |
Bill Wendling | 8fe347a | 2010-03-16 01:05:35 +0000 | [diff] [blame] | 785 | // Physical registers and those that are not live-out of the block are |
| 786 | // killed/dead at their last use/def within this block. |
Dan Gohman | 1584390 | 2010-03-18 18:07:13 +0000 | [diff] [blame] | 787 | if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) { |
Dan Gohman | 022b21f | 2008-10-04 00:31:14 +0000 | [diff] [blame] | 788 | if (MO.isUse()) { |
| 789 | // Don't mark uses that are tied to defs as kills. |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 790 | if (!MI->isRegTiedToDefOperand(idx)) |
Dan Gohman | 022b21f | 2008-10-04 00:31:14 +0000 | [diff] [blame] | 791 | MO.setIsKill(true); |
Bill Wendling | 8fe347a | 2010-03-16 01:05:35 +0000 | [diff] [blame] | 792 | } else { |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 793 | MO.setIsDead(true); |
Bill Wendling | 8fe347a | 2010-03-16 01:05:35 +0000 | [diff] [blame] | 794 | } |
Dan Gohman | 1584390 | 2010-03-18 18:07:13 +0000 | [diff] [blame] | 795 | } |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 796 | } |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 800 | // loop over each instruction |
| 801 | MachineBasicBlock::iterator MII = MBB.begin(); |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 802 | |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 803 | DEBUG({ |
| 804 | const BasicBlock *LBB = MBB.getBasicBlock(); |
| 805 | if (LBB) |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 806 | dbgs() << "\nStarting RegAlloc of BB: " << LBB->getName(); |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 807 | }); |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 808 | |
Evan Cheng | d5a4802 | 2009-01-29 18:37:30 +0000 | [diff] [blame] | 809 | // Add live-in registers as active. |
| 810 | for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(), |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 811 | E = MBB.livein_end(); I != E; ++I) { |
Evan Cheng | d5a4802 | 2009-01-29 18:37:30 +0000 | [diff] [blame] | 812 | unsigned Reg = *I; |
| 813 | MF->getRegInfo().setPhysRegUsed(Reg); |
| 814 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 815 | AddToPhysRegsUseOrder(Reg); |
Evan Cheng | d5a4802 | 2009-01-29 18:37:30 +0000 | [diff] [blame] | 816 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 817 | *SubRegs; ++SubRegs) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 818 | if (PhysRegsUsed[*SubRegs] == -2) continue; |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 819 | |
| 820 | AddToPhysRegsUseOrder(*SubRegs); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 821 | PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now |
| 822 | MF->getRegInfo().setPhysRegUsed(*SubRegs); |
Evan Cheng | d5a4802 | 2009-01-29 18:37:30 +0000 | [diff] [blame] | 823 | } |
Owen Anderson | 9094db1 | 2008-07-09 20:14:53 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | ComputeLocalLiveness(MBB); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 44500e3 | 2006-06-15 22:21:53 +0000 | [diff] [blame] | 828 | // Otherwise, sequentially allocate each instruction in the MBB. |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 829 | while (MII != MBB.end()) { |
| 830 | MachineInstr *MI = MII++; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 831 | const TargetInstrDesc &TID = MI->getDesc(); |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 832 | DEBUG({ |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 833 | dbgs() << "\nStarting RegAlloc of: " << *MI; |
| 834 | dbgs() << " Regs have values: "; |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 835 | for (unsigned i = 0; i != TRI->getNumRegs(); ++i) |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 836 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) { |
| 837 | if (PhysRegsUsed[i] && isVirtRegModified(PhysRegsUsed[i])) |
| 838 | dbgs() << "*"; |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 839 | dbgs() << "[" << TRI->getName(i) |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 840 | << ",%reg" << PhysRegsUsed[i] << "] "; |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 841 | } |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 842 | dbgs() << '\n'; |
Bill Wendling | fbb594f | 2009-08-22 20:38:09 +0000 | [diff] [blame] | 843 | }); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 844 | |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 845 | // Determine whether this is a copy instruction. The cases where the |
| 846 | // source or destination are phys regs are handled specially. |
| 847 | unsigned SrcCopyReg, DstCopyReg, SrcCopySubReg, DstCopySubReg; |
Dale Johannesen | 9a6636b | 2010-02-03 01:40:33 +0000 | [diff] [blame] | 848 | unsigned SrcCopyPhysReg = 0U; |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 849 | bool isCopy = TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg, |
Evan Cheng | de7dea2 | 2010-05-12 23:59:42 +0000 | [diff] [blame] | 850 | SrcCopySubReg, DstCopySubReg) && |
| 851 | SrcCopySubReg == DstCopySubReg; |
Dale Johannesen | 9a6636b | 2010-02-03 01:40:33 +0000 | [diff] [blame] | 852 | if (isCopy && TargetRegisterInfo::isVirtualRegister(SrcCopyReg)) |
| 853 | SrcCopyPhysReg = getVirt2PhysRegMapSlot(SrcCopyReg); |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 854 | |
Chris Lattner | ae64043 | 2002-12-17 02:50:10 +0000 | [diff] [blame] | 855 | // Loop over the implicit uses, making sure that they are at the head of the |
| 856 | // use order list, so they don't get reallocated. |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 857 | if (TID.ImplicitUses) { |
| 858 | for (const unsigned *ImplicitUses = TID.ImplicitUses; |
| 859 | *ImplicitUses; ++ImplicitUses) |
| 860 | MarkPhysRegRecentlyUsed(*ImplicitUses); |
| 861 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 862 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 863 | SmallVector<unsigned, 8> Kills; |
| 864 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 865 | MachineOperand &MO = MI->getOperand(i); |
Nick Lewycky | 403d312 | 2010-05-07 01:45:38 +0000 | [diff] [blame] | 866 | if (!MO.isReg() || !MO.isKill()) continue; |
| 867 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 868 | if (!MO.isImplicit()) |
| 869 | Kills.push_back(MO.getReg()); |
| 870 | else if (!isReadModWriteImplicitKill(MI, MO.getReg())) |
| 871 | // These are extra physical register kills when a sub-register |
| 872 | // is defined (def of a sub-register is a read/mod/write of the |
| 873 | // larger registers). Ignore. |
| 874 | Kills.push_back(MO.getReg()); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 877 | // If any physical regs are earlyclobber, spill any value they might |
| 878 | // have in them, then mark them unallocatable. |
| 879 | // If any virtual regs are earlyclobber, allocate them now (before |
| 880 | // freeing inputs that are killed). |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 881 | if (MI->isInlineAsm()) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 882 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 883 | MachineOperand &MO = MI->getOperand(i); |
| 884 | if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber() || |
| 885 | !MO.getReg()) |
| 886 | continue; |
| 887 | |
| 888 | if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 889 | unsigned DestVirtReg = MO.getReg(); |
| 890 | unsigned DestPhysReg; |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 892 | // If DestVirtReg already has a value, use it. |
| 893 | if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) |
| 894 | DestPhysReg = getReg(MBB, MI, DestVirtReg); |
| 895 | MF->getRegInfo().setPhysRegUsed(DestPhysReg); |
| 896 | markVirtRegModified(DestVirtReg); |
| 897 | getVirtRegLastUse(DestVirtReg) = |
| 898 | std::make_pair((MachineInstr*)0, 0); |
| 899 | DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg) |
| 900 | << " to %reg" << DestVirtReg << "\n"); |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 901 | if (unsigned DestSubIdx = MO.getSubReg()) { |
| 902 | MO.setSubReg(0); |
| 903 | DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx); |
| 904 | } |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 905 | MO.setReg(DestPhysReg); // Assign the earlyclobber register |
| 906 | } else { |
| 907 | unsigned Reg = MO.getReg(); |
| 908 | if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP. |
| 909 | // These are extra physical register defs when a sub-register |
| 910 | // is defined (def of a sub-register is a read/mod/write of the |
| 911 | // larger registers). Ignore. |
| 912 | if (isReadModWriteImplicitDef(MI, MO.getReg())) continue; |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 914 | MF->getRegInfo().setPhysRegUsed(Reg); |
| 915 | spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg |
| 916 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 917 | AddToPhysRegsUseOrder(Reg); |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 919 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 920 | *SubRegs; ++SubRegs) { |
| 921 | if (PhysRegsUsed[*SubRegs] == -2) continue; |
| 922 | MF->getRegInfo().setPhysRegUsed(*SubRegs); |
| 923 | PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 924 | AddToPhysRegsUseOrder(*SubRegs); |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Dale Johannesen | 10fedd2 | 2010-02-10 00:11:11 +0000 | [diff] [blame] | 930 | // If a DBG_VALUE says something is located in a spilled register, |
| 931 | // change the DBG_VALUE to be undef, which prevents the register |
Dale Johannesen | ca13461 | 2010-01-30 00:57:47 +0000 | [diff] [blame] | 932 | // from being reloaded here. Doing that would change the generated |
| 933 | // code, unless another use immediately follows this instruction. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 934 | if (MI->isDebugValue() && |
Dale Johannesen | ca13461 | 2010-01-30 00:57:47 +0000 | [diff] [blame] | 935 | MI->getNumOperands()==3 && MI->getOperand(0).isReg()) { |
| 936 | unsigned VirtReg = MI->getOperand(0).getReg(); |
| 937 | if (VirtReg && TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 938 | !getVirt2PhysRegMapSlot(VirtReg)) |
| 939 | MI->getOperand(0).setReg(0U); |
| 940 | } |
| 941 | |
Brian Gaeke | 53b99a0 | 2003-08-15 21:19:25 +0000 | [diff] [blame] | 942 | // Get the used operands into registers. This has the potential to spill |
Chris Lattner | b8822ad | 2003-08-04 23:36:39 +0000 | [diff] [blame] | 943 | // incoming values if we are out of registers. Note that we completely |
| 944 | // ignore physical register uses here. We assume that if an explicit |
| 945 | // physical register is referenced by the instruction, that it is guaranteed |
| 946 | // to be live-in, or the input is badly hosed. |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 947 | // |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 948 | SmallSet<unsigned, 4> ReloadedRegs; |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 949 | for (unsigned i = 0; i != MI->getNumOperands(); ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 950 | MachineOperand &MO = MI->getOperand(i); |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 951 | // here we are looking for only used operands (never def&use) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 952 | if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() && |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 953 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 954 | MI = reloadVirtReg(MBB, MI, i, ReloadedRegs, |
| 955 | isCopy ? DstCopyReg : 0); |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 956 | } |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 957 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 958 | // If this instruction is the last user of this register, kill the |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 959 | // value, freeing the register being used, so it doesn't need to be |
| 960 | // spilled to memory. |
| 961 | // |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 962 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) { |
| 963 | unsigned VirtReg = Kills[i]; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 964 | unsigned PhysReg = VirtReg; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 965 | if (TargetRegisterInfo::isVirtualRegister(VirtReg)) { |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 966 | // If the virtual register was never materialized into a register, it |
| 967 | // might not be in the map, but it won't hurt to zero it out anyway. |
| 968 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 969 | PhysReg = PhysRegSlot; |
| 970 | PhysRegSlot = 0; |
Chris Lattner | 0c5b8da | 2006-09-08 20:21:31 +0000 | [diff] [blame] | 971 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 972 | // Unallocatable register dead, ignore. |
| 973 | continue; |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 974 | } else { |
Evan Cheng | 76500d5 | 2007-10-22 19:42:28 +0000 | [diff] [blame] | 975 | assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) && |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 976 | "Silently clearing a virtual register?"); |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 977 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 979 | if (!PhysReg) continue; |
| 980 | |
| 981 | DEBUG(dbgs() << " Last use of " << TRI->getName(PhysReg) |
| 982 | << "[%reg" << VirtReg <<"], removing it from live set\n"); |
| 983 | removePhysReg(PhysReg); |
| 984 | for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg); |
| 985 | *SubRegs; ++SubRegs) { |
| 986 | if (PhysRegsUsed[*SubRegs] != -2) { |
| 987 | DEBUG(dbgs() << " Last use of " |
| 988 | << TRI->getName(*SubRegs) << "[%reg" << VirtReg |
| 989 | <<"], removing it from live set\n"); |
| 990 | removePhysReg(*SubRegs); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 991 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
| 995 | // Loop over all of the operands of the instruction, spilling registers that |
| 996 | // are defined, and marking explicit destinations in the PhysRegsUsed map. |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 997 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 998 | MachineOperand &MO = MI->getOperand(i); |
| 999 | if (!MO.isReg() || !MO.isDef() || MO.isImplicit() || !MO.getReg() || |
| 1000 | MO.isEarlyClobber() || |
| 1001 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 1002 | continue; |
| 1003 | |
| 1004 | unsigned Reg = MO.getReg(); |
| 1005 | if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP. |
| 1006 | // These are extra physical register defs when a sub-register |
| 1007 | // is defined (def of a sub-register is a read/mod/write of the |
| 1008 | // larger registers). Ignore. |
| 1009 | if (isReadModWriteImplicitDef(MI, MO.getReg())) continue; |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1011 | MF->getRegInfo().setPhysRegUsed(Reg); |
| 1012 | spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg |
| 1013 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1014 | AddToPhysRegsUseOrder(Reg); |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1016 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 1017 | *SubRegs; ++SubRegs) { |
| 1018 | if (PhysRegsUsed[*SubRegs] == -2) continue; |
| 1019 | |
| 1020 | MF->getRegInfo().setPhysRegUsed(*SubRegs); |
| 1021 | PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1022 | AddToPhysRegsUseOrder(*SubRegs); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 1023 | } |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 1024 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Loop over the implicit defs, spilling them as well. |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 1027 | if (TID.ImplicitDefs) { |
| 1028 | for (const unsigned *ImplicitDefs = TID.ImplicitDefs; |
| 1029 | *ImplicitDefs; ++ImplicitDefs) { |
| 1030 | unsigned Reg = *ImplicitDefs; |
Evan Cheng | 7ac19af | 2007-06-26 21:05:13 +0000 | [diff] [blame] | 1031 | if (PhysRegsUsed[Reg] != -2) { |
Chris Lattner | 2b41b8e | 2006-09-19 18:02:01 +0000 | [diff] [blame] | 1032 | spillPhysReg(MBB, MI, Reg, true); |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1033 | AddToPhysRegsUseOrder(Reg); |
Chris Lattner | 2b41b8e | 2006-09-19 18:02:01 +0000 | [diff] [blame] | 1034 | PhysRegsUsed[Reg] = 0; // It is free and reserved now |
| 1035 | } |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1036 | MF->getRegInfo().setPhysRegUsed(Reg); |
Evan Cheng | 5a3c6a8 | 2009-01-29 02:20:59 +0000 | [diff] [blame] | 1037 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
| 1038 | *SubRegs; ++SubRegs) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1039 | if (PhysRegsUsed[*SubRegs] == -2) continue; |
| 1040 | |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1041 | AddToPhysRegsUseOrder(*SubRegs); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1042 | PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now |
| 1043 | MF->getRegInfo().setPhysRegUsed(*SubRegs); |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 1044 | } |
Alkis Evlogimenos | 19b6486 | 2004-01-13 06:24:30 +0000 | [diff] [blame] | 1045 | } |
Alkis Evlogimenos | efe995a | 2003-12-13 01:20:58 +0000 | [diff] [blame] | 1046 | } |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 1047 | |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 1048 | SmallVector<unsigned, 8> DeadDefs; |
| 1049 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1050 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1051 | if (MO.isReg() && MO.isDead()) |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 1052 | DeadDefs.push_back(MO.getReg()); |
| 1053 | } |
| 1054 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1055 | // Okay, we have allocated all of the source operands and spilled any values |
| 1056 | // that would be destroyed by defs of this instruction. Loop over the |
Chris Lattner | 0648b16 | 2005-01-23 22:51:56 +0000 | [diff] [blame] | 1057 | // explicit defs and assign them to a register, spilling incoming values if |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 1058 | // we need to scavenge a register. |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 1059 | // |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 1060 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1061 | MachineOperand &MO = MI->getOperand(i); |
| 1062 | if (!MO.isReg() || !MO.isDef() || !MO.getReg() || |
| 1063 | MO.isEarlyClobber() || |
| 1064 | !TargetRegisterInfo::isVirtualRegister(MO.getReg())) |
| 1065 | continue; |
| 1066 | |
| 1067 | unsigned DestVirtReg = MO.getReg(); |
| 1068 | unsigned DestPhysReg; |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1070 | // If DestVirtReg already has a value, use it. |
| 1071 | if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg))) { |
| 1072 | // If this is a copy try to reuse the input as the output; |
| 1073 | // that will make the copy go away. |
| 1074 | // If this is a copy, the source reg is a phys reg, and |
| 1075 | // that reg is available, use that phys reg for DestPhysReg. |
| 1076 | // If this is a copy, the source reg is a virtual reg, and |
| 1077 | // the phys reg that was assigned to that virtual reg is now |
| 1078 | // available, use that phys reg for DestPhysReg. (If it's now |
| 1079 | // available that means this was the last use of the source.) |
| 1080 | if (isCopy && |
| 1081 | TargetRegisterInfo::isPhysicalRegister(SrcCopyReg) && |
| 1082 | isPhysRegAvailable(SrcCopyReg)) { |
| 1083 | DestPhysReg = SrcCopyReg; |
| 1084 | assignVirtToPhysReg(DestVirtReg, DestPhysReg); |
| 1085 | } else if (isCopy && |
| 1086 | TargetRegisterInfo::isVirtualRegister(SrcCopyReg) && |
| 1087 | SrcCopyPhysReg && isPhysRegAvailable(SrcCopyPhysReg) && |
| 1088 | MF->getRegInfo().getRegClass(DestVirtReg)-> |
| 1089 | contains(SrcCopyPhysReg)) { |
| 1090 | DestPhysReg = SrcCopyPhysReg; |
| 1091 | assignVirtToPhysReg(DestVirtReg, DestPhysReg); |
| 1092 | } else |
| 1093 | DestPhysReg = getReg(MBB, MI, DestVirtReg); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1094 | } |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1095 | MF->getRegInfo().setPhysRegUsed(DestPhysReg); |
| 1096 | markVirtRegModified(DestVirtReg); |
| 1097 | getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0); |
| 1098 | DEBUG(dbgs() << " Assigning " << TRI->getName(DestPhysReg) |
| 1099 | << " to %reg" << DestVirtReg << "\n"); |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (unsigned DestSubIdx = MO.getSubReg()) { |
| 1102 | MO.setSubReg(0); |
| 1103 | DestPhysReg = TRI->getSubReg(DestPhysReg, DestSubIdx); |
| 1104 | } |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1105 | MO.setReg(DestPhysReg); // Assign the output register |
Alkis Evlogimenos | 71e353e | 2004-02-26 22:00:20 +0000 | [diff] [blame] | 1106 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 1108 | // If this instruction defines any registers that are immediately dead, |
| 1109 | // kill them now. |
| 1110 | // |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 1111 | for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) { |
| 1112 | unsigned VirtReg = DeadDefs[i]; |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 1113 | unsigned PhysReg = VirtReg; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1114 | if (TargetRegisterInfo::isVirtualRegister(VirtReg)) { |
Chris Lattner | 56ddada | 2004-02-17 17:49:10 +0000 | [diff] [blame] | 1115 | unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg); |
| 1116 | PhysReg = PhysRegSlot; |
| 1117 | assert(PhysReg != 0); |
| 1118 | PhysRegSlot = 0; |
Chris Lattner | 0c5b8da | 2006-09-08 20:21:31 +0000 | [diff] [blame] | 1119 | } else if (PhysRegsUsed[PhysReg] == -2) { |
| 1120 | // Unallocatable register dead, ignore. |
| 1121 | continue; |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1122 | } else if (!PhysReg) |
| 1123 | continue; |
| 1124 | |
| 1125 | DEBUG(dbgs() << " Register " << TRI->getName(PhysReg) |
| 1126 | << " [%reg" << VirtReg |
| 1127 | << "] is never used, removing it from live set\n"); |
| 1128 | removePhysReg(PhysReg); |
| 1129 | for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg); |
| 1130 | *AliasSet; ++AliasSet) { |
| 1131 | if (PhysRegsUsed[*AliasSet] != -2) { |
| 1132 | DEBUG(dbgs() << " Register " << TRI->getName(*AliasSet) |
| 1133 | << " [%reg" << *AliasSet |
| 1134 | << "] is never used, removing it from live set\n"); |
| 1135 | removePhysReg(*AliasSet); |
Evan Cheng | ddee842 | 2006-11-15 20:55:15 +0000 | [diff] [blame] | 1136 | } |
Chris Lattner | 82bee0f | 2002-12-18 08:14:26 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 1139 | |
Jakob Stoklund Olesen | 8387d7d | 2010-04-30 21:19:29 +0000 | [diff] [blame] | 1140 | // If this instruction is a call, make sure there are no dirty registers. The |
| 1141 | // call might throw an exception, and the landing pad expects to find all |
| 1142 | // registers in stack slots. |
| 1143 | if (TID.isCall()) |
| 1144 | for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) { |
| 1145 | if (PhysRegsUsed[i] <= 0) continue; |
| 1146 | unsigned VirtReg = PhysRegsUsed[i]; |
| 1147 | if (!isVirtRegModified(VirtReg)) continue; |
| 1148 | DEBUG(dbgs() << " Storing dirty %reg" << VirtReg); |
| 1149 | storeVirtReg(MBB, MI, VirtReg, i, false); |
| 1150 | markVirtRegModified(VirtReg, false); |
| 1151 | DEBUG(dbgs() << " because the call might throw\n"); |
| 1152 | } |
| 1153 | |
Bob Wilson | 9d928c2 | 2009-05-07 23:47:03 +0000 | [diff] [blame] | 1154 | // Finally, if this is a noop copy instruction, zap it. (Except that if |
| 1155 | // the copy is dead, it must be kept to avoid messing up liveness info for |
| 1156 | // the register scavenger. See pr4100.) |
Dale Johannesen | fc49bd2 | 2009-12-16 00:29:41 +0000 | [diff] [blame] | 1157 | if (TII->isMoveInstr(*MI, SrcCopyReg, DstCopyReg, |
| 1158 | SrcCopySubReg, DstCopySubReg) && |
Evan Cheng | de7dea2 | 2010-05-12 23:59:42 +0000 | [diff] [blame] | 1159 | SrcCopyReg == DstCopyReg && SrcCopySubReg == DstCopySubReg && |
Jakob Stoklund Olesen | ab2d008 | 2010-05-14 22:40:40 +0000 | [diff] [blame] | 1160 | DeadDefs.empty()) { |
| 1161 | ++NumCopies; |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 1162 | MBB.erase(MI); |
Jakob Stoklund Olesen | ab2d008 | 2010-05-14 22:40:40 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Chris Lattner | e6a88ac | 2005-11-09 18:22:42 +0000 | [diff] [blame] | 1166 | MachineBasicBlock::iterator MI = MBB.getFirstTerminator(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Spill all physical registers holding virtual registers now. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1169 | for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 1170 | if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) { |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 1171 | if (unsigned VirtReg = PhysRegsUsed[i]) |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 1172 | spillVirtReg(MBB, MI, VirtReg, i); |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 1173 | else |
| 1174 | removePhysReg(i); |
Anton Korobeynikov | 4aefd6b | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 1175 | } |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1176 | |
Chris Lattner | 9a5ef20 | 2005-11-09 05:28:45 +0000 | [diff] [blame] | 1177 | #if 0 |
| 1178 | // This checking code is very expensive. |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 1179 | bool AllOk = true; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1180 | for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1181 | e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 1182 | if (unsigned PR = Virt2PhysRegMap[i]) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 1183 | cerr << "Register still mapped: " << i << " -> " << PR << "\n"; |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 1184 | AllOk = false; |
| 1185 | } |
| 1186 | assert(AllOk && "Virtual registers still in phys regs?"); |
| 1187 | #endif |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1188 | |
| 1189 | // Clear any physical register which appear live at the end of the basic |
| 1190 | // block, but which do not hold any virtual registers. e.g., the stack |
| 1191 | // pointer. |
| 1192 | PhysRegsUseOrder.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /// runOnMachineFunction - Register allocate the whole function |
| 1196 | /// |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 1197 | bool RALocal::runOnMachineFunction(MachineFunction &Fn) { |
David Greene | 4424817 | 2010-01-05 01:26:05 +0000 | [diff] [blame] | 1198 | DEBUG(dbgs() << "Machine Function\n"); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1199 | MF = &Fn; |
Evan Cheng | 736f89b | 2010-05-12 01:29:36 +0000 | [diff] [blame] | 1200 | MRI = &Fn.getRegInfo(); |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 1201 | TM = &Fn.getTarget(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1202 | TRI = TM->getRegisterInfo(); |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 1203 | TII = TM->getInstrInfo(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1204 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1205 | PhysRegsUsed.assign(TRI->getNumRegs(), -1); |
Jakob Stoklund Olesen | 23eaf26 | 2010-04-17 00:38:36 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 1207 | // At various places we want to efficiently check to see whether a register |
| 1208 | // is allocatable. To handle this, we mark all unallocatable registers as |
| 1209 | // being pinned down, permanently. |
| 1210 | { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1211 | BitVector Allocable = TRI->getAllocatableSet(Fn); |
Chris Lattner | 45d5788 | 2006-09-08 19:03:30 +0000 | [diff] [blame] | 1212 | for (unsigned i = 0, e = Allocable.size(); i != e; ++i) |
| 1213 | if (!Allocable[i]) |
| 1214 | PhysRegsUsed[i] = -2; // Mark the reg unallocable. |
| 1215 | } |
Chris Lattner | 64667b6 | 2004-02-09 01:26:13 +0000 | [diff] [blame] | 1216 | |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 1217 | // initialize the virtual->physical register map to have a 'null' |
| 1218 | // mapping for all virtual registers |
Evan Cheng | 644340a | 2008-01-17 00:35:26 +0000 | [diff] [blame] | 1219 | unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg(); |
Evan Cheng | bdb10fe | 2008-07-10 18:23:23 +0000 | [diff] [blame] | 1220 | StackSlotForVirtReg.grow(LastVirtReg); |
Evan Cheng | 644340a | 2008-01-17 00:35:26 +0000 | [diff] [blame] | 1221 | Virt2PhysRegMap.grow(LastVirtReg); |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 1222 | Virt2LastUseMap.grow(LastVirtReg); |
Chris Lattner | 4dd8163 | 2010-03-31 05:15:22 +0000 | [diff] [blame] | 1223 | VirtRegModified.resize(LastVirtReg+1 - |
| 1224 | TargetRegisterInfo::FirstVirtualRegister); |
| 1225 | UsedInMultipleBlocks.resize(LastVirtReg+1 - |
| 1226 | TargetRegisterInfo::FirstVirtualRegister); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1228 | // Loop over all of the basic blocks, eliminating virtual register references |
| 1229 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 1230 | MBB != MBBe; ++MBB) |
| 1231 | AllocateBasicBlock(*MBB); |
| 1232 | |
Chris Lattner | 580f9be | 2002-12-28 20:40:43 +0000 | [diff] [blame] | 1233 | StackSlotForVirtReg.clear(); |
Alkis Evlogimenos | 4de473b | 2004-02-13 18:20:47 +0000 | [diff] [blame] | 1234 | PhysRegsUsed.clear(); |
Chris Lattner | 91a452b | 2003-01-13 00:25:40 +0000 | [diff] [blame] | 1235 | VirtRegModified.clear(); |
Owen Anderson | 491fccc | 2008-07-08 22:24:50 +0000 | [diff] [blame] | 1236 | UsedInMultipleBlocks.clear(); |
Chris Lattner | ecea563 | 2004-02-09 02:12:04 +0000 | [diff] [blame] | 1237 | Virt2PhysRegMap.clear(); |
Evan Cheng | 839b759 | 2008-01-17 02:08:17 +0000 | [diff] [blame] | 1238 | Virt2LastUseMap.clear(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1239 | return true; |
| 1240 | } |
| 1241 | |
Chris Lattner | ef09c63 | 2004-01-31 21:27:19 +0000 | [diff] [blame] | 1242 | FunctionPass *llvm::createLocalRegisterAllocator() { |
Bill Wendling | e23e00d | 2007-05-08 19:02:46 +0000 | [diff] [blame] | 1243 | return new RALocal(); |
Chris Lattner | b74e83c | 2002-12-16 16:15:28 +0000 | [diff] [blame] | 1244 | } |