Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 10 | // This file implements the VirtRegMap class. |
| 11 | // |
| 12 | // It also contains implementations of the the Spiller interface, which, given a |
| 13 | // virtual register map and a machine function, eliminates all virtual |
| 14 | // references by replacing them with physical register references - adding spill |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 15 | // code as necessary. |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "spiller" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 20 | #include "VirtRegMap.h" |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/SSARegMap.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 34 | Statistic<> NumSpills("spiller", "Number of register spills"); |
| 35 | Statistic<> NumStores("spiller", "Number of stores added"); |
| 36 | Statistic<> NumLoads ("spiller", "Number of loads added"); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 37 | Statistic<> NumReused("spiller", "Number of values reused"); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 39 | enum SpillerName { simple, local }; |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 41 | cl::opt<SpillerName> |
| 42 | SpillerOpt("spiller", |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 43 | cl::desc("Spiller to use: (default: local)"), |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 44 | cl::Prefix, |
| 45 | cl::values(clEnumVal(simple, " simple spiller"), |
| 46 | clEnumVal(local, " local spiller"), |
| 47 | clEnumValEnd), |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 48 | cl::init(local)); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
| 52 | // VirtRegMap implementation |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
| 55 | void VirtRegMap::grow() { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 56 | Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg()); |
| 57 | Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg()); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 60 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
| 61 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 62 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 63 | "attempt to assign stack slot to already spilled register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 64 | const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg); |
| 65 | int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(), |
| 66 | RC->getAlignment()); |
| 67 | Virt2StackSlotMap[virtReg] = frameIndex; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 68 | ++NumSpills; |
| 69 | return frameIndex; |
| 70 | } |
| 71 | |
| 72 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) { |
| 73 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 74 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 75 | "attempt to assign stack slot to already spilled register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 76 | Virt2StackSlotMap[virtReg] = frameIndex; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 79 | void VirtRegMap::virtFolded(unsigned virtReg, |
| 80 | MachineInstr* oldMI, |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 81 | MachineInstr* newMI) { |
| 82 | // move previous memory references folded to new instruction |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 83 | std::vector<MI2VirtMapTy::mapped_type> regs; |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 84 | for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(oldMI), |
| 85 | E = MI2VirtMap.end(); I != E && I->first == oldMI; ) { |
| 86 | regs.push_back(I->second); |
| 87 | MI2VirtMap.erase(I++); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 89 | |
| 90 | MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(newMI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 91 | for (unsigned i = 0, e = regs.size(); i != e; ++i) |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 92 | MI2VirtMap.insert(IP, std::make_pair(newMI, regs[i])); |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 94 | // add new memory reference |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 95 | MI2VirtMap.insert(IP, std::make_pair(newMI, virtReg)); |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 98 | void VirtRegMap::print(std::ostream &OS) const { |
| 99 | const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo(); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 101 | OS << "********** REGISTER MAP **********\n"; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 102 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 103 | e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) { |
| 104 | if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG) |
| 105 | OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n"; |
| 106 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 110 | e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) |
| 111 | if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT) |
| 112 | OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n"; |
| 113 | OS << '\n'; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 114 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 116 | void VirtRegMap::dump() const { print(std::cerr); } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 118 | |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | // Simple Spiller Implementation |
| 121 | //===----------------------------------------------------------------------===// |
| 122 | |
| 123 | Spiller::~Spiller() {} |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 124 | |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 125 | namespace { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 126 | struct SimpleSpiller : public Spiller { |
| 127 | bool runOnMachineFunction(MachineFunction& mf, const VirtRegMap &VRM); |
| 128 | }; |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 131 | bool SimpleSpiller::runOnMachineFunction(MachineFunction& MF, |
| 132 | const VirtRegMap& VRM) { |
| 133 | DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n"); |
| 134 | DEBUG(std::cerr << "********** Function: " |
| 135 | << MF.getFunction()->getName() << '\n'); |
| 136 | const TargetMachine& TM = MF.getTarget(); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 137 | const MRegisterInfo& MRI = *TM.getRegisterInfo(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 139 | // LoadedRegs - Keep track of which vregs are loaded, so that we only load |
| 140 | // each vreg once (in the case where a spilled vreg is used by multiple |
| 141 | // operands). This is always smaller than the number of operands to the |
| 142 | // current machine instr, so it should be small. |
| 143 | std::vector<unsigned> LoadedRegs; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 145 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 146 | MBBI != E; ++MBBI) { |
| 147 | DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n"); |
| 148 | MachineBasicBlock &MBB = *MBBI; |
| 149 | for (MachineBasicBlock::iterator MII = MBB.begin(), |
| 150 | E = MBB.end(); MII != E; ++MII) { |
| 151 | MachineInstr &MI = *MII; |
| 152 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 153 | MachineOperand &MO = MI.getOperand(i); |
| 154 | if (MO.isRegister() && MO.getReg() && |
| 155 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 156 | unsigned VirtReg = MO.getReg(); |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 157 | unsigned PhysReg = VRM.getPhys(VirtReg); |
| 158 | if (VRM.hasStackSlot(VirtReg)) { |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 159 | int StackSlot = VRM.getStackSlot(VirtReg); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 161 | if (MO.isUse() && |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 162 | std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg) |
| 163 | == LoadedRegs.end()) { |
| 164 | MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot); |
| 165 | LoadedRegs.push_back(VirtReg); |
| 166 | ++NumLoads; |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 167 | DEBUG(std::cerr << '\t' << *prior(MII)); |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 170 | if (MO.isDef()) { |
| 171 | MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot); |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 172 | ++NumStores; |
| 173 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 175 | MI.SetMachineOperandReg(i, PhysReg); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 178 | DEBUG(std::cerr << '\t' << MI); |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 179 | LoadedRegs.clear(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | //===----------------------------------------------------------------------===// |
| 186 | // Local Spiller Implementation |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
| 189 | namespace { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 190 | /// LocalSpiller - This spiller does a simple pass over the machine basic |
| 191 | /// block to attempt to keep spills in registers as much as possible for |
| 192 | /// blocks that have low register pressure (the vreg may be spilled due to |
| 193 | /// register pressure in other blocks). |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 194 | class LocalSpiller : public Spiller { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 195 | const MRegisterInfo *MRI; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 196 | const TargetInstrInfo *TII; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 197 | public: |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 198 | bool runOnMachineFunction(MachineFunction &MF, const VirtRegMap &VRM) { |
| 199 | MRI = MF.getTarget().getRegisterInfo(); |
| 200 | TII = MF.getTarget().getInstrInfo(); |
| 201 | DEBUG(std::cerr << "\n**** Local spiller rewriting function '" |
| 202 | << MF.getFunction()->getName() << "':\n"); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 204 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 205 | MBB != E; ++MBB) |
| 206 | RewriteMBB(*MBB, VRM); |
| 207 | return true; |
| 208 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 209 | private: |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 210 | void RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM); |
| 211 | void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots, |
| 212 | std::map<unsigned, int> &PhysRegs); |
| 213 | void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots, |
| 214 | std::map<unsigned, int> &PhysRegs); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 215 | }; |
| 216 | } |
| 217 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 218 | void LocalSpiller::ClobberPhysRegOnly(unsigned PhysReg, |
| 219 | std::map<int, unsigned> &SpillSlots, |
| 220 | std::map<unsigned, int> &PhysRegs) { |
| 221 | std::map<unsigned, int>::iterator I = PhysRegs.find(PhysReg); |
| 222 | if (I != PhysRegs.end()) { |
| 223 | int Slot = I->second; |
| 224 | PhysRegs.erase(I); |
| 225 | assert(SpillSlots[Slot] == PhysReg && "Bidirectional map mismatch!"); |
| 226 | SpillSlots.erase(Slot); |
| 227 | DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg) |
| 228 | << " clobbered, invalidating SS#" << Slot << "\n"); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 233 | void LocalSpiller::ClobberPhysReg(unsigned PhysReg, |
| 234 | std::map<int, unsigned> &SpillSlots, |
| 235 | std::map<unsigned, int> &PhysRegs) { |
| 236 | for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS) |
| 237 | ClobberPhysRegOnly(*AS, SpillSlots, PhysRegs); |
| 238 | ClobberPhysRegOnly(PhysReg, SpillSlots, PhysRegs); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 242 | // ReusedOp - For each reused operand, we keep track of a bit of information, in |
| 243 | // case we need to rollback upon processing a new operand. See comments below. |
| 244 | namespace { |
| 245 | struct ReusedOp { |
| 246 | // The MachineInstr operand that reused an available value. |
| 247 | unsigned Operand; |
| 248 | |
| 249 | // StackSlot - The spill slot of the value being reused. |
| 250 | unsigned StackSlot; |
| 251 | |
| 252 | // PhysRegReused - The physical register the value was available in. |
| 253 | unsigned PhysRegReused; |
| 254 | |
| 255 | // AssignedPhysReg - The physreg that was assigned for use by the reload. |
| 256 | unsigned AssignedPhysReg; |
| 257 | |
| 258 | ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr) |
| 259 | : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr) {} |
| 260 | }; |
| 261 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 263 | |
| 264 | /// rewriteMBB - Keep track of which spills are available even after the |
| 265 | /// register allocator is done with them. If possible, avoid reloading vregs. |
| 266 | void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, const VirtRegMap &VRM) { |
| 267 | |
| 268 | // SpillSlotsAvailable - This map keeps track of all of the spilled virtual |
| 269 | // register values that are still available, due to being loaded to stored to, |
| 270 | // but not invalidated yet. |
| 271 | std::map<int, unsigned> SpillSlotsAvailable; |
| 272 | |
| 273 | // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating |
| 274 | // which physregs are in use holding a stack slot value. |
| 275 | std::map<unsigned, int> PhysRegsAvailable; |
| 276 | |
| 277 | DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n"); |
| 278 | |
| 279 | std::vector<ReusedOp> ReusedOperands; |
| 280 | |
| 281 | // DefAndUseVReg - When we see a def&use operand that is spilled, keep track |
| 282 | // of it. ".first" is the machine operand index (should always be 0 for now), |
| 283 | // and ".second" is the virtual register that is spilled. |
| 284 | std::vector<std::pair<unsigned, unsigned> > DefAndUseVReg; |
| 285 | |
| 286 | for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end(); |
| 287 | MII != E; ) { |
| 288 | MachineInstr &MI = *MII; |
| 289 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 290 | |
| 291 | ReusedOperands.clear(); |
| 292 | DefAndUseVReg.clear(); |
| 293 | |
| 294 | // Process all of the spilled uses and all non spilled reg references. |
| 295 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 296 | MachineOperand &MO = MI.getOperand(i); |
| 297 | if (MO.isRegister() && MO.getReg() && |
| 298 | MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 299 | unsigned VirtReg = MO.getReg(); |
| 300 | |
| 301 | if (!VRM.hasStackSlot(VirtReg)) { |
| 302 | // This virtual register was assigned a physreg! |
| 303 | MI.SetMachineOperandReg(i, VRM.getPhys(VirtReg)); |
| 304 | } else { |
| 305 | // Is this virtual register a spilled value? |
| 306 | if (MO.isUse()) { |
| 307 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 308 | unsigned PhysReg; |
| 309 | |
| 310 | // Check to see if this stack slot is available. |
| 311 | std::map<int, unsigned>::iterator SSI = |
| 312 | SpillSlotsAvailable.find(StackSlot); |
| 313 | if (SSI != SpillSlotsAvailable.end()) { |
| 314 | // If this stack slot value is already available, reuse it! |
| 315 | PhysReg = SSI->second; |
| 316 | MI.SetMachineOperandReg(i, PhysReg); |
| 317 | DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg " |
| 318 | << MRI->getName(SSI->second) << "\n"); |
| 319 | |
| 320 | // The only technical detail we have is that we don't know that |
| 321 | // PhysReg won't be clobbered by a reloaded stack slot that occurs |
| 322 | // later in the instruction. In particular, consider 'op V1, V2'. |
| 323 | // If V1 is available in physreg R0, we would choose to reuse it |
| 324 | // here, instead of reloading it into the register the allocator |
| 325 | // indicated (say R1). However, V2 might have to be reloaded |
| 326 | // later, and it might indicate that it needs to live in R0. When |
| 327 | // this occurs, we need to have information available that |
| 328 | // indicates it is safe to use R1 for the reload instead of R0. |
| 329 | // |
| 330 | // To further complicate matters, we might conflict with an alias, |
| 331 | // or R0 and R1 might not be compatible with each other. In this |
| 332 | // case, we actually insert a reload for V1 in R1, ensuring that |
| 333 | // we can get at R0 or its alias. |
| 334 | ReusedOperands.push_back(ReusedOp(i, StackSlot, PhysReg, |
| 335 | VRM.getPhys(VirtReg))); |
| 336 | ++NumReused; |
| 337 | } else { |
| 338 | // Otherwise, reload it and remember that we have it. |
| 339 | PhysReg = VRM.getPhys(VirtReg); |
| 340 | |
| 341 | // Note that, if we reused a register for a previous operand, the |
| 342 | // register we want to reload into might not actually be |
| 343 | // available. If this occurs, use the register indicated by the |
| 344 | // reuser. |
| 345 | if (!ReusedOperands.empty()) // This is most often empty. |
| 346 | for (unsigned ro = 0, e = ReusedOperands.size(); ro != e; ++ro) |
| 347 | if (ReusedOperands[ro].PhysRegReused == PhysReg) { |
| 348 | // Yup, use the reload register that we didn't use before. |
| 349 | PhysReg = ReusedOperands[ro].AssignedPhysReg; |
| 350 | break; |
| 351 | } else { |
| 352 | ReusedOp &Op = ReusedOperands[ro]; |
| 353 | unsigned PRRU = Op.PhysRegReused; |
| 354 | for (const unsigned *AS = MRI->getAliasSet(PRRU); *AS; ++AS) |
| 355 | if (*AS == PhysReg) { |
| 356 | // Okay, we found out that an alias of a reused register |
| 357 | // was used. This isn't good because it means we have |
| 358 | // to undo a previous reuse. |
| 359 | MRI->loadRegFromStackSlot(MBB, &MI, Op.AssignedPhysReg, |
| 360 | Op.StackSlot); |
| 361 | ClobberPhysReg(Op.AssignedPhysReg, SpillSlotsAvailable, |
| 362 | PhysRegsAvailable); |
| 363 | |
| 364 | MI.SetMachineOperandReg(Op.Operand, Op.AssignedPhysReg); |
| 365 | PhysRegsAvailable[Op.AssignedPhysReg] = Op.StackSlot; |
| 366 | SpillSlotsAvailable[Op.StackSlot] = Op.AssignedPhysReg; |
| 367 | PhysRegsAvailable.erase(Op.PhysRegReused); |
| 368 | DEBUG(std::cerr << "Remembering SS#" << Op.StackSlot |
| 369 | << " in physreg " |
| 370 | << MRI->getName(Op.AssignedPhysReg) << "\n"); |
| 371 | ++NumLoads; |
| 372 | DEBUG(std::cerr << '\t' << *prior(MII)); |
| 373 | |
| 374 | DEBUG(std::cerr << "Reuse undone!\n"); |
| 375 | ReusedOperands.erase(ReusedOperands.begin()+ro); |
| 376 | --NumReused; |
| 377 | goto ContinueReload; |
| 378 | } |
| 379 | } |
| 380 | ContinueReload: |
| 381 | |
| 382 | MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot); |
| 383 | // This invalidates PhysReg. |
| 384 | ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable); |
| 385 | |
| 386 | MI.SetMachineOperandReg(i, PhysReg); |
| 387 | PhysRegsAvailable[PhysReg] = StackSlot; |
| 388 | SpillSlotsAvailable[StackSlot] = PhysReg; |
| 389 | DEBUG(std::cerr << "Remembering SS#" << StackSlot <<" in physreg " |
| 390 | << MRI->getName(PhysReg) << "\n"); |
| 391 | ++NumLoads; |
| 392 | DEBUG(std::cerr << '\t' << *prior(MII)); |
| 393 | } |
| 394 | |
| 395 | // If this is both a def and a use, we need to emit a store to the |
| 396 | // stack slot after the instruction. Keep track of D&U operands |
| 397 | // because we already changed it to a physreg here. |
| 398 | if (MO.isDef()) { |
| 399 | // Remember that this was a def-and-use operand, and that the |
| 400 | // stack slot is live after this instruction executes. |
| 401 | DefAndUseVReg.push_back(std::make_pair(i, VirtReg)); |
| 402 | } |
| 403 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 408 | // Loop over all of the implicit defs, clearing them from our available |
| 409 | // sets. |
| 410 | const TargetInstrDescriptor &InstrDesc = TII->get(MI.getOpcode()); |
| 411 | for (const unsigned* ImpDef = InstrDesc.ImplicitDefs; *ImpDef; ++ImpDef) |
| 412 | ClobberPhysReg(*ImpDef, SpillSlotsAvailable, PhysRegsAvailable); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 414 | DEBUG(std::cerr << '\t' << MI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 416 | // If we have folded references to memory operands, make sure we clear all |
| 417 | // physical registers that may contain the value of the spilled virtual |
| 418 | // register |
| 419 | VirtRegMap::MI2VirtMapTy::const_iterator I, E; |
| 420 | for (tie(I, E) = VRM.getFoldedVirts(&MI); I != E; ++I) { |
| 421 | DEBUG(std::cerr << "Folded vreg: " << I->second); |
| 422 | if (VRM.hasStackSlot(I->second)) { |
| 423 | int SS = VRM.getStackSlot(I->second); |
| 424 | DEBUG(std::cerr << " - StackSlot: " << SS << "\n"); |
| 425 | |
| 426 | std::map<int, unsigned>::iterator I = SpillSlotsAvailable.find(SS); |
| 427 | if (I != SpillSlotsAvailable.end()) { |
| 428 | PhysRegsAvailable.erase(I->second); |
| 429 | SpillSlotsAvailable.erase(I); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 430 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 431 | } else { |
| 432 | DEBUG(std::cerr << ": No stack slot!\n"); |
| 433 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 436 | // Process all of the spilled defs. |
| 437 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 438 | MachineOperand &MO = MI.getOperand(i); |
| 439 | if (MO.isRegister() && MO.getReg() && MO.isDef()) { |
| 440 | unsigned VirtReg = MO.getReg(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 442 | bool TakenCareOf = false; |
| 443 | if (!MRegisterInfo::isVirtualRegister(VirtReg)) { |
| 444 | // Check to see if this is a def-and-use vreg operand that we do need |
| 445 | // to insert a store for. |
| 446 | bool OpTakenCareOf = false; |
| 447 | if (MO.isUse() && !DefAndUseVReg.empty()) { |
| 448 | for (unsigned dau = 0, e = DefAndUseVReg.size(); dau != e; ++dau) |
| 449 | if (DefAndUseVReg[dau].first == i) { |
| 450 | VirtReg = DefAndUseVReg[dau].second; |
| 451 | OpTakenCareOf = true; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (!OpTakenCareOf) { |
| 457 | ClobberPhysReg(VirtReg, SpillSlotsAvailable, PhysRegsAvailable); |
| 458 | TakenCareOf = true; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (!TakenCareOf) { |
| 463 | // The only vregs left are stack slot definitions. |
| 464 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 465 | unsigned PhysReg; |
| 466 | |
| 467 | // If this is a def&use operand, and we used a different physreg for |
| 468 | // it than the one assigned, make sure to execute the store from the |
| 469 | // correct physical register. |
| 470 | if (MO.getReg() == VirtReg) |
| 471 | PhysReg = VRM.getPhys(VirtReg); |
| 472 | else |
| 473 | PhysReg = MO.getReg(); |
| 474 | |
| 475 | MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot); |
| 476 | DEBUG(std::cerr << "Store:\t" << *next(MII)); |
| 477 | MI.SetMachineOperandReg(i, PhysReg); |
| 478 | |
| 479 | // If the stack slot value was previously available in some other |
| 480 | // register, change it now. Otherwise, make the register available, |
| 481 | // in PhysReg. |
| 482 | std::map<int, unsigned>::iterator SSA = |
| 483 | SpillSlotsAvailable.find(StackSlot); |
| 484 | if (SSA != SpillSlotsAvailable.end()) { |
| 485 | // Remove the record for physreg. |
| 486 | PhysRegsAvailable.erase(SSA->second); |
| 487 | SpillSlotsAvailable.erase(SSA); |
| 488 | } |
| 489 | ClobberPhysReg(PhysReg, SpillSlotsAvailable, PhysRegsAvailable); |
| 490 | |
| 491 | PhysRegsAvailable[PhysReg] = StackSlot; |
| 492 | SpillSlotsAvailable[StackSlot] = PhysReg; |
| 493 | DEBUG(std::cerr << "Updating SS#" << StackSlot <<" in physreg " |
| 494 | << MRI->getName(PhysReg) << "\n"); |
| 495 | |
| 496 | ++NumStores; |
| 497 | VirtReg = PhysReg; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | MII = NextMII; |
| 502 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame^] | 506 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 507 | llvm::Spiller* llvm::createSpiller() { |
| 508 | switch (SpillerOpt) { |
| 509 | default: assert(0 && "Unreachable!"); |
| 510 | case local: |
| 511 | return new LocalSpiller(); |
| 512 | case simple: |
| 513 | return new SimpleSpiller(); |
| 514 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 515 | } |