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