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