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