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" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 33 | #include <iostream> |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
Andrew Lenharth | ed41f1b | 2006-07-20 17:28:38 +0000 | [diff] [blame] | 37 | static Statistic<> NumSpills("spiller", "Number of register spills"); |
| 38 | static Statistic<> NumStores("spiller", "Number of stores added"); |
| 39 | static Statistic<> NumLoads ("spiller", "Number of loads added"); |
| 40 | static Statistic<> NumReused("spiller", "Number of values reused"); |
| 41 | static Statistic<> NumDSE ("spiller", "Number of dead stores elided"); |
| 42 | static Statistic<> NumDCE ("spiller", "Number of copies elided"); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 44 | enum SpillerName { simple, local }; |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 45 | |
Andrew Lenharth | ed41f1b | 2006-07-20 17:28:38 +0000 | [diff] [blame] | 46 | static cl::opt<SpillerName> |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 47 | SpillerOpt("spiller", |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 48 | cl::desc("Spiller to use: (default: local)"), |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 49 | cl::Prefix, |
| 50 | cl::values(clEnumVal(simple, " simple spiller"), |
| 51 | clEnumVal(local, " local spiller"), |
| 52 | clEnumValEnd), |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 53 | cl::init(local)); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | // VirtRegMap implementation |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 60 | VirtRegMap::VirtRegMap(MachineFunction &mf) |
| 61 | : TII(*mf.getTarget().getInstrInfo()), MF(mf), |
| 62 | Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) { |
| 63 | grow(); |
| 64 | } |
| 65 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 66 | void VirtRegMap::grow() { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 67 | Virt2PhysMap.grow(MF.getSSARegMap()->getLastVirtReg()); |
| 68 | Virt2StackSlotMap.grow(MF.getSSARegMap()->getLastVirtReg()); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 71 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
| 72 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 73 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 74 | "attempt to assign stack slot to already spilled register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 75 | const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(virtReg); |
| 76 | int frameIndex = MF.getFrameInfo()->CreateStackObject(RC->getSize(), |
| 77 | RC->getAlignment()); |
| 78 | Virt2StackSlotMap[virtReg] = frameIndex; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 79 | ++NumSpills; |
| 80 | return frameIndex; |
| 81 | } |
| 82 | |
| 83 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) { |
| 84 | assert(MRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 85 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 86 | "attempt to assign stack slot to already spilled register"); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 87 | Virt2StackSlotMap[virtReg] = frameIndex; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 90 | void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI, |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 91 | unsigned OpNo, MachineInstr *NewMI) { |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 92 | // Move previous memory references folded to new instruction. |
| 93 | MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 94 | for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI), |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 95 | E = MI2VirtMap.end(); I != E && I->first == OldMI; ) { |
| 96 | MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second)); |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 97 | MI2VirtMap.erase(I++); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 99 | |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 100 | ModRef MRInfo; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame^] | 101 | if (TII.getOperandConstraint(OldMI->getOpcode(), OpNo, |
| 102 | TargetInstrInfo::TIED_TO)) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 103 | // Folded a two-address operand. |
| 104 | MRInfo = isModRef; |
| 105 | } else if (OldMI->getOperand(OpNo).isDef()) { |
| 106 | MRInfo = isMod; |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 107 | } else { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 108 | MRInfo = isRef; |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 109 | } |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 111 | // add new memory reference |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 112 | MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo))); |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 115 | void VirtRegMap::print(std::ostream &OS) const { |
| 116 | const MRegisterInfo* MRI = MF.getTarget().getRegisterInfo(); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 118 | OS << "********** REGISTER MAP **********\n"; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 119 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 120 | e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) { |
| 121 | if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG) |
| 122 | OS << "[reg" << i << " -> " << MRI->getName(Virt2PhysMap[i]) << "]\n"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | for (unsigned i = MRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 127 | e = MF.getSSARegMap()->getLastVirtReg(); i <= e; ++i) |
| 128 | if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT) |
| 129 | OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n"; |
| 130 | OS << '\n'; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 131 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 133 | void VirtRegMap::dump() const { print(std::cerr); } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 135 | |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | // Simple Spiller Implementation |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | |
| 140 | Spiller::~Spiller() {} |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 141 | |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 142 | namespace { |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 143 | struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller { |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 144 | bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 145 | }; |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 148 | bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 149 | DEBUG(std::cerr << "********** REWRITE MACHINE CODE **********\n"); |
| 150 | DEBUG(std::cerr << "********** Function: " |
| 151 | << MF.getFunction()->getName() << '\n'); |
Chris Lattner | b0f31bf | 2005-01-23 22:45:13 +0000 | [diff] [blame] | 152 | const TargetMachine &TM = MF.getTarget(); |
| 153 | const MRegisterInfo &MRI = *TM.getRegisterInfo(); |
| 154 | bool *PhysRegsUsed = MF.getUsedPhysregs(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 156 | // LoadedRegs - Keep track of which vregs are loaded, so that we only load |
| 157 | // each vreg once (in the case where a spilled vreg is used by multiple |
| 158 | // operands). This is always smaller than the number of operands to the |
| 159 | // current machine instr, so it should be small. |
| 160 | std::vector<unsigned> LoadedRegs; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 162 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 163 | MBBI != E; ++MBBI) { |
| 164 | DEBUG(std::cerr << MBBI->getBasicBlock()->getName() << ":\n"); |
| 165 | MachineBasicBlock &MBB = *MBBI; |
| 166 | for (MachineBasicBlock::iterator MII = MBB.begin(), |
| 167 | E = MBB.end(); MII != E; ++MII) { |
| 168 | MachineInstr &MI = *MII; |
| 169 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 170 | MachineOperand &MO = MI.getOperand(i); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 171 | if (MO.isRegister() && MO.getReg()) |
| 172 | if (MRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 173 | unsigned VirtReg = MO.getReg(); |
| 174 | unsigned PhysReg = VRM.getPhys(VirtReg); |
| 175 | if (VRM.hasStackSlot(VirtReg)) { |
| 176 | int StackSlot = VRM.getStackSlot(VirtReg); |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 177 | const TargetRegisterClass* RC = |
| 178 | MF.getSSARegMap()->getRegClass(VirtReg); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 180 | if (MO.isUse() && |
| 181 | std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg) |
| 182 | == LoadedRegs.end()) { |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 183 | MRI.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 184 | LoadedRegs.push_back(VirtReg); |
| 185 | ++NumLoads; |
| 186 | DEBUG(std::cerr << '\t' << *prior(MII)); |
| 187 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 189 | if (MO.isDef()) { |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 190 | MRI.storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 191 | ++NumStores; |
| 192 | } |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 194 | PhysRegsUsed[PhysReg] = true; |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 195 | MI.getOperand(i).setReg(PhysReg); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 196 | } else { |
| 197 | PhysRegsUsed[MO.getReg()] = true; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 477e455 | 2004-09-30 16:10:45 +0000 | [diff] [blame] | 201 | DEBUG(std::cerr << '\t' << MI); |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 202 | LoadedRegs.clear(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | // Local Spiller Implementation |
| 210 | //===----------------------------------------------------------------------===// |
| 211 | |
| 212 | namespace { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 213 | /// LocalSpiller - This spiller does a simple pass over the machine basic |
| 214 | /// block to attempt to keep spills in registers as much as possible for |
| 215 | /// blocks that have low register pressure (the vreg may be spilled due to |
| 216 | /// register pressure in other blocks). |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 217 | class VISIBILITY_HIDDEN LocalSpiller : public Spiller { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 218 | const MRegisterInfo *MRI; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 219 | const TargetInstrInfo *TII; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 220 | public: |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 221 | bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 222 | MRI = MF.getTarget().getRegisterInfo(); |
| 223 | TII = MF.getTarget().getInstrInfo(); |
| 224 | DEBUG(std::cerr << "\n**** Local spiller rewriting function '" |
| 225 | << MF.getFunction()->getName() << "':\n"); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 227 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
| 228 | MBB != E; ++MBB) |
| 229 | RewriteMBB(*MBB, VRM); |
| 230 | return true; |
| 231 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 232 | private: |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 233 | void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 234 | void ClobberPhysReg(unsigned PR, std::map<int, unsigned> &SpillSlots, |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 235 | std::multimap<unsigned, int> &PhysRegs); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 236 | void ClobberPhysRegOnly(unsigned PR, std::map<int, unsigned> &SpillSlots, |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 237 | std::multimap<unsigned, int> &PhysRegs); |
| 238 | void ModifyStackSlot(int Slot, std::map<int, unsigned> &SpillSlots, |
| 239 | std::multimap<unsigned, int> &PhysRegs); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 240 | }; |
| 241 | } |
| 242 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 243 | /// AvailableSpills - As the local spiller is scanning and rewriting an MBB from |
| 244 | /// top down, keep track of which spills slots are available in each register. |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 245 | /// |
| 246 | /// Note that not all physregs are created equal here. In particular, some |
| 247 | /// physregs are reloads that we are allowed to clobber or ignore at any time. |
| 248 | /// Other physregs are values that the register allocated program is using that |
| 249 | /// we cannot CHANGE, but we can read if we like. We keep track of this on a |
| 250 | /// per-stack-slot basis as the low bit in the value of the SpillSlotsAvailable |
| 251 | /// entries. The predicate 'canClobberPhysReg()' checks this bit and |
| 252 | /// addAvailable sets it if. |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 253 | namespace { |
| 254 | class VISIBILITY_HIDDEN AvailableSpills { |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 255 | const MRegisterInfo *MRI; |
| 256 | const TargetInstrInfo *TII; |
| 257 | |
| 258 | // SpillSlotsAvailable - This map keeps track of all of the spilled virtual |
| 259 | // register values that are still available, due to being loaded or stored to, |
| 260 | // but not invalidated yet. |
| 261 | std::map<int, unsigned> SpillSlotsAvailable; |
| 262 | |
| 263 | // PhysRegsAvailable - This is the inverse of SpillSlotsAvailable, indicating |
| 264 | // which stack slot values are currently held by a physreg. This is used to |
| 265 | // invalidate entries in SpillSlotsAvailable when a physreg is modified. |
| 266 | std::multimap<unsigned, int> PhysRegsAvailable; |
| 267 | |
| 268 | void ClobberPhysRegOnly(unsigned PhysReg); |
| 269 | public: |
| 270 | AvailableSpills(const MRegisterInfo *mri, const TargetInstrInfo *tii) |
| 271 | : MRI(mri), TII(tii) { |
| 272 | } |
| 273 | |
| 274 | /// getSpillSlotPhysReg - If the specified stack slot is available in a |
| 275 | /// physical register, return that PhysReg, otherwise return 0. |
| 276 | unsigned getSpillSlotPhysReg(int Slot) const { |
| 277 | std::map<int, unsigned>::const_iterator I = SpillSlotsAvailable.find(Slot); |
| 278 | if (I != SpillSlotsAvailable.end()) |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 279 | return I->second >> 1; // Remove the CanClobber bit. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 282 | |
| 283 | const MRegisterInfo *getRegInfo() const { return MRI; } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 284 | |
| 285 | /// addAvailable - Mark that the specified stack slot is available in the |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 286 | /// specified physreg. If CanClobber is true, the physreg can be modified at |
| 287 | /// any time without changing the semantics of the program. |
| 288 | void addAvailable(int Slot, unsigned Reg, bool CanClobber = true) { |
Chris Lattner | 8666249 | 2006-02-03 23:50:46 +0000 | [diff] [blame] | 289 | // If this stack slot is thought to be available in some other physreg, |
| 290 | // remove its record. |
| 291 | ModifyStackSlot(Slot); |
| 292 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 293 | PhysRegsAvailable.insert(std::make_pair(Reg, Slot)); |
Jeff Cohen | 003cecb | 2006-02-04 03:27:39 +0000 | [diff] [blame] | 294 | SpillSlotsAvailable[Slot] = (Reg << 1) | (unsigned)CanClobber; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 295 | |
| 296 | DEBUG(std::cerr << "Remembering SS#" << Slot << " in physreg " |
| 297 | << MRI->getName(Reg) << "\n"); |
| 298 | } |
| 299 | |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 300 | /// canClobberPhysReg - Return true if the spiller is allowed to change the |
| 301 | /// value of the specified stackslot register if it desires. The specified |
| 302 | /// stack slot must be available in a physreg for this query to make sense. |
| 303 | bool canClobberPhysReg(int Slot) const { |
| 304 | assert(SpillSlotsAvailable.count(Slot) && "Slot not available!"); |
| 305 | return SpillSlotsAvailable.find(Slot)->second & 1; |
| 306 | } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 307 | |
| 308 | /// ClobberPhysReg - This is called when the specified physreg changes |
| 309 | /// value. We use this to invalidate any info about stuff we thing lives in |
| 310 | /// it and any of its aliases. |
| 311 | void ClobberPhysReg(unsigned PhysReg); |
| 312 | |
| 313 | /// ModifyStackSlot - This method is called when the value in a stack slot |
| 314 | /// changes. This removes information about which register the previous value |
| 315 | /// for this slot lives in (as the previous value is dead now). |
| 316 | void ModifyStackSlot(int Slot); |
| 317 | }; |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 319 | |
| 320 | /// ClobberPhysRegOnly - This is called when the specified physreg changes |
| 321 | /// value. We use this to invalidate any info about stuff we thing lives in it. |
| 322 | void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) { |
| 323 | std::multimap<unsigned, int>::iterator I = |
| 324 | PhysRegsAvailable.lower_bound(PhysReg); |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 325 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 326 | int Slot = I->second; |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 327 | PhysRegsAvailable.erase(I++); |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 328 | assert((SpillSlotsAvailable[Slot] >> 1) == PhysReg && |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 329 | "Bidirectional map mismatch!"); |
| 330 | SpillSlotsAvailable.erase(Slot); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 331 | DEBUG(std::cerr << "PhysReg " << MRI->getName(PhysReg) |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 332 | << " clobbered, invalidating SS#" << Slot << "\n"); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 333 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 336 | /// ClobberPhysReg - This is called when the specified physreg changes |
| 337 | /// value. We use this to invalidate any info about stuff we thing lives in |
| 338 | /// it and any of its aliases. |
| 339 | void AvailableSpills::ClobberPhysReg(unsigned PhysReg) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 340 | for (const unsigned *AS = MRI->getAliasSet(PhysReg); *AS; ++AS) |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 341 | ClobberPhysRegOnly(*AS); |
| 342 | ClobberPhysRegOnly(PhysReg); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 345 | /// ModifyStackSlot - This method is called when the value in a stack slot |
| 346 | /// changes. This removes information about which register the previous value |
| 347 | /// for this slot lives in (as the previous value is dead now). |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 348 | void AvailableSpills::ModifyStackSlot(int Slot) { |
| 349 | std::map<int, unsigned>::iterator It = SpillSlotsAvailable.find(Slot); |
| 350 | if (It == SpillSlotsAvailable.end()) return; |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 351 | unsigned Reg = It->second >> 1; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 352 | SpillSlotsAvailable.erase(It); |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 353 | |
| 354 | // This register may hold the value of multiple stack slots, only remove this |
| 355 | // stack slot from the set of values the register contains. |
| 356 | std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg); |
| 357 | for (; ; ++I) { |
| 358 | assert(I != PhysRegsAvailable.end() && I->first == Reg && |
| 359 | "Map inverse broken!"); |
| 360 | if (I->second == Slot) break; |
| 361 | } |
| 362 | PhysRegsAvailable.erase(I); |
| 363 | } |
| 364 | |
| 365 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 367 | // ReusedOp - For each reused operand, we keep track of a bit of information, in |
| 368 | // case we need to rollback upon processing a new operand. See comments below. |
| 369 | namespace { |
| 370 | struct ReusedOp { |
| 371 | // The MachineInstr operand that reused an available value. |
| 372 | unsigned Operand; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 374 | // StackSlot - The spill slot of the value being reused. |
| 375 | unsigned StackSlot; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 377 | // PhysRegReused - The physical register the value was available in. |
| 378 | unsigned PhysRegReused; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 380 | // AssignedPhysReg - The physreg that was assigned for use by the reload. |
| 381 | unsigned AssignedPhysReg; |
Chris Lattner | 8a61a75 | 2005-10-06 17:19:06 +0000 | [diff] [blame] | 382 | |
| 383 | // VirtReg - The virtual register itself. |
| 384 | unsigned VirtReg; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 8a61a75 | 2005-10-06 17:19:06 +0000 | [diff] [blame] | 386 | ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr, |
| 387 | unsigned vreg) |
| 388 | : Operand(o), StackSlot(ss), PhysRegReused(prr), AssignedPhysReg(apr), |
| 389 | VirtReg(vreg) {} |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 390 | }; |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 391 | |
| 392 | /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that |
| 393 | /// is reused instead of reloaded. |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 394 | class VISIBILITY_HIDDEN ReuseInfo { |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 395 | MachineInstr &MI; |
| 396 | std::vector<ReusedOp> Reuses; |
| 397 | public: |
| 398 | ReuseInfo(MachineInstr &mi) : MI(mi) {} |
| 399 | |
| 400 | bool hasReuses() const { |
| 401 | return !Reuses.empty(); |
| 402 | } |
| 403 | |
| 404 | /// addReuse - If we choose to reuse a virtual register that is already |
| 405 | /// available instead of reloading it, remember that we did so. |
| 406 | void addReuse(unsigned OpNo, unsigned StackSlot, |
| 407 | unsigned PhysRegReused, unsigned AssignedPhysReg, |
| 408 | unsigned VirtReg) { |
| 409 | // If the reload is to the assigned register anyway, no undo will be |
| 410 | // required. |
| 411 | if (PhysRegReused == AssignedPhysReg) return; |
| 412 | |
| 413 | // Otherwise, remember this. |
| 414 | Reuses.push_back(ReusedOp(OpNo, StackSlot, PhysRegReused, |
| 415 | AssignedPhysReg, VirtReg)); |
| 416 | } |
| 417 | |
| 418 | /// GetRegForReload - We are about to emit a reload into PhysReg. If there |
| 419 | /// is some other operand that is using the specified register, either pick |
| 420 | /// a new register to use, or evict the previous reload and use this reg. |
| 421 | unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI, |
| 422 | AvailableSpills &Spills, |
| 423 | std::map<int, MachineInstr*> &MaybeDeadStores) { |
| 424 | if (Reuses.empty()) return PhysReg; // This is most often empty. |
| 425 | |
| 426 | for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) { |
| 427 | ReusedOp &Op = Reuses[ro]; |
| 428 | // If we find some other reuse that was supposed to use this register |
| 429 | // exactly for its reload, we can change this reload to use ITS reload |
| 430 | // register. |
| 431 | if (Op.PhysRegReused == PhysReg) { |
| 432 | // Yup, use the reload register that we didn't use before. |
Chris Lattner | 47cb717 | 2006-02-25 02:03:40 +0000 | [diff] [blame] | 433 | unsigned NewReg = Op.AssignedPhysReg; |
| 434 | |
| 435 | // Remove the record for the previous reuse. We know it can never be |
| 436 | // invalidated now. |
| 437 | Reuses.erase(Reuses.begin()+ro); |
| 438 | return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 439 | } else { |
| 440 | // Otherwise, we might also have a problem if a previously reused |
| 441 | // value aliases the new register. If so, codegen the previous reload |
| 442 | // and use this one. |
| 443 | unsigned PRRU = Op.PhysRegReused; |
| 444 | const MRegisterInfo *MRI = Spills.getRegInfo(); |
| 445 | if (MRI->areAliases(PRRU, PhysReg)) { |
| 446 | // Okay, we found out that an alias of a reused register |
| 447 | // was used. This isn't good because it means we have |
| 448 | // to undo a previous reuse. |
| 449 | MachineBasicBlock *MBB = MI->getParent(); |
| 450 | const TargetRegisterClass *AliasRC = |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 451 | MBB->getParent()->getSSARegMap()->getRegClass(Op.VirtReg); |
| 452 | |
| 453 | // Copy Op out of the vector and remove it, we're going to insert an |
| 454 | // explicit load for it. |
| 455 | ReusedOp NewOp = Op; |
| 456 | Reuses.erase(Reuses.begin()+ro); |
| 457 | |
| 458 | // Ok, we're going to try to reload the assigned physreg into the |
| 459 | // slot that we were supposed to in the first place. However, that |
| 460 | // register could hold a reuse. Check to see if it conflicts or |
| 461 | // would prefer us to use a different register. |
| 462 | unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg, |
| 463 | MI, Spills, MaybeDeadStores); |
| 464 | |
| 465 | MRI->loadRegFromStackSlot(*MBB, MI, NewPhysReg, |
| 466 | NewOp.StackSlot, AliasRC); |
| 467 | Spills.ClobberPhysReg(NewPhysReg); |
| 468 | Spills.ClobberPhysReg(NewOp.PhysRegReused); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 469 | |
| 470 | // Any stores to this stack slot are not dead anymore. |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 471 | MaybeDeadStores.erase(NewOp.StackSlot); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 472 | |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 473 | MI->getOperand(NewOp.Operand).setReg(NewPhysReg); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 475 | Spills.addAvailable(NewOp.StackSlot, NewPhysReg); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 476 | ++NumLoads; |
| 477 | DEBUG(MachineBasicBlock::iterator MII = MI; |
| 478 | std::cerr << '\t' << *prior(MII)); |
| 479 | |
| 480 | DEBUG(std::cerr << "Reuse undone!\n"); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 481 | --NumReused; |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 482 | |
| 483 | // Finally, PhysReg is now available, go ahead and use it. |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 484 | return PhysReg; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | return PhysReg; |
| 489 | } |
| 490 | }; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 493 | |
| 494 | /// rewriteMBB - Keep track of which spills are available even after the |
| 495 | /// register allocator is done with them. If possible, avoid reloading vregs. |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 496 | void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 498 | DEBUG(std::cerr << MBB.getBasicBlock()->getName() << ":\n"); |
| 499 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 500 | // Spills - Keep track of which spilled values are available in physregs so |
| 501 | // that we can choose to reuse the physregs instead of emitting reloads. |
| 502 | AvailableSpills Spills(MRI, TII); |
| 503 | |
Chris Lattner | 52b25db | 2004-10-01 19:47:12 +0000 | [diff] [blame] | 504 | // MaybeDeadStores - When we need to write a value back into a stack slot, |
| 505 | // keep track of the inserted store. If the stack slot value is never read |
| 506 | // (because the value was used from some available register, for example), and |
| 507 | // subsequently stored to, the original store is dead. This map keeps track |
| 508 | // of inserted stores that are not used. If we see a subsequent store to the |
| 509 | // same stack slot, the original store is deleted. |
| 510 | std::map<int, MachineInstr*> MaybeDeadStores; |
| 511 | |
Chris Lattner | b0f31bf | 2005-01-23 22:45:13 +0000 | [diff] [blame] | 512 | bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs(); |
| 513 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 514 | for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end(); |
| 515 | MII != E; ) { |
| 516 | MachineInstr &MI = *MII; |
| 517 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 518 | |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 519 | /// ReusedOperands - Keep track of operand reuse in case we need to undo |
| 520 | /// reuse. |
| 521 | ReuseInfo ReusedOperands(MI); |
| 522 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 523 | // Process all of the spilled uses and all non spilled reg references. |
| 524 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 525 | MachineOperand &MO = MI.getOperand(i); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 526 | if (!MO.isRegister() || MO.getReg() == 0) |
| 527 | continue; // Ignore non-register operands. |
| 528 | |
| 529 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) { |
| 530 | // Ignore physregs for spilling, but remember that it is used by this |
| 531 | // function. |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 532 | PhysRegsUsed[MO.getReg()] = true; |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 533 | continue; |
| 534 | } |
| 535 | |
| 536 | assert(MRegisterInfo::isVirtualRegister(MO.getReg()) && |
| 537 | "Not a virtual or a physical register?"); |
| 538 | |
| 539 | unsigned VirtReg = MO.getReg(); |
| 540 | if (!VRM.hasStackSlot(VirtReg)) { |
| 541 | // This virtual register was assigned a physreg! |
| 542 | unsigned Phys = VRM.getPhys(VirtReg); |
| 543 | PhysRegsUsed[Phys] = true; |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 544 | MI.getOperand(i).setReg(Phys); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 545 | continue; |
| 546 | } |
| 547 | |
| 548 | // This virtual register is now known to be a spilled value. |
| 549 | if (!MO.isUse()) |
| 550 | continue; // Handle defs in the loop below (handle use&def here though) |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 552 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 553 | unsigned PhysReg; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 555 | // Check to see if this stack slot is available. |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 556 | if ((PhysReg = Spills.getSpillSlotPhysReg(StackSlot))) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 558 | // This spilled operand might be part of a two-address operand. If this |
| 559 | // is the case, then changing it will necessarily require changing the |
| 560 | // def part of the instruction as well. However, in some cases, we |
| 561 | // aren't allowed to modify the reused register. If none of these cases |
| 562 | // apply, reuse it. |
| 563 | bool CanReuse = true; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame^] | 564 | int ti = TII->getOperandConstraint(MI.getOpcode(), i, |
| 565 | TargetInstrInfo::TIED_TO); |
| 566 | if (ti != -1 && |
| 567 | MI.getOperand(ti).isReg() && |
| 568 | MI.getOperand(ti).getReg() == VirtReg) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 569 | // Okay, we have a two address operand. We can reuse this physreg as |
| 570 | // long as we are allowed to clobber the value. |
| 571 | CanReuse = Spills.canClobberPhysReg(StackSlot); |
| 572 | } |
| 573 | |
| 574 | if (CanReuse) { |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 575 | // If this stack slot value is already available, reuse it! |
| 576 | DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg " |
| 577 | << MRI->getName(PhysReg) << " for vreg" |
| 578 | << VirtReg <<" instead of reloading into physreg " |
| 579 | << MRI->getName(VRM.getPhys(VirtReg)) << "\n"); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 580 | MI.getOperand(i).setReg(PhysReg); |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 581 | |
| 582 | // The only technical detail we have is that we don't know that |
| 583 | // PhysReg won't be clobbered by a reloaded stack slot that occurs |
| 584 | // later in the instruction. In particular, consider 'op V1, V2'. |
| 585 | // If V1 is available in physreg R0, we would choose to reuse it |
| 586 | // here, instead of reloading it into the register the allocator |
| 587 | // indicated (say R1). However, V2 might have to be reloaded |
| 588 | // later, and it might indicate that it needs to live in R0. When |
| 589 | // this occurs, we need to have information available that |
| 590 | // indicates it is safe to use R1 for the reload instead of R0. |
| 591 | // |
| 592 | // To further complicate matters, we might conflict with an alias, |
| 593 | // or R0 and R1 might not be compatible with each other. In this |
| 594 | // case, we actually insert a reload for V1 in R1, ensuring that |
| 595 | // we can get at R0 or its alias. |
| 596 | ReusedOperands.addReuse(i, StackSlot, PhysReg, |
| 597 | VRM.getPhys(VirtReg), VirtReg); |
| 598 | ++NumReused; |
| 599 | continue; |
| 600 | } |
| 601 | |
| 602 | // Otherwise we have a situation where we have a two-address instruction |
| 603 | // whose mod/ref operand needs to be reloaded. This reload is already |
| 604 | // available in some register "PhysReg", but if we used PhysReg as the |
| 605 | // operand to our 2-addr instruction, the instruction would modify |
| 606 | // PhysReg. This isn't cool if something later uses PhysReg and expects |
| 607 | // to get its initial value. |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 608 | // |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 609 | // To avoid this problem, and to avoid doing a load right after a store, |
| 610 | // we emit a copy from PhysReg into the designated register for this |
| 611 | // operand. |
| 612 | unsigned DesignatedReg = VRM.getPhys(VirtReg); |
| 613 | assert(DesignatedReg && "Must map virtreg to physreg!"); |
| 614 | |
| 615 | // Note that, if we reused a register for a previous operand, the |
| 616 | // register we want to reload into might not actually be |
| 617 | // available. If this occurs, use the register indicated by the |
| 618 | // reuser. |
| 619 | if (ReusedOperands.hasReuses()) |
| 620 | DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI, |
| 621 | Spills, MaybeDeadStores); |
| 622 | |
Chris Lattner | ba1fc3d | 2006-04-28 04:43:18 +0000 | [diff] [blame] | 623 | // If the mapped designated register is actually the physreg we have |
| 624 | // incoming, we don't need to inserted a dead copy. |
| 625 | if (DesignatedReg == PhysReg) { |
| 626 | // If this stack slot value is already available, reuse it! |
| 627 | DEBUG(std::cerr << "Reusing SS#" << StackSlot << " from physreg " |
| 628 | << MRI->getName(PhysReg) << " for vreg" |
| 629 | << VirtReg |
| 630 | << " instead of reloading into same physreg.\n"); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 631 | MI.getOperand(i).setReg(PhysReg); |
Chris Lattner | ba1fc3d | 2006-04-28 04:43:18 +0000 | [diff] [blame] | 632 | ++NumReused; |
| 633 | continue; |
| 634 | } |
| 635 | |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 636 | const TargetRegisterClass* RC = |
| 637 | MBB.getParent()->getSSARegMap()->getRegClass(VirtReg); |
| 638 | |
| 639 | PhysRegsUsed[DesignatedReg] = true; |
| 640 | MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC); |
| 641 | |
| 642 | // This invalidates DesignatedReg. |
| 643 | Spills.ClobberPhysReg(DesignatedReg); |
| 644 | |
| 645 | Spills.addAvailable(StackSlot, DesignatedReg); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 646 | MI.getOperand(i).setReg(DesignatedReg); |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 647 | DEBUG(std::cerr << '\t' << *prior(MII)); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 648 | ++NumReused; |
| 649 | continue; |
| 650 | } |
| 651 | |
| 652 | // Otherwise, reload it and remember that we have it. |
| 653 | PhysReg = VRM.getPhys(VirtReg); |
Chris Lattner | 172c362 | 2006-01-04 06:47:48 +0000 | [diff] [blame] | 654 | assert(PhysReg && "Must map virtreg to physreg!"); |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 655 | const TargetRegisterClass* RC = |
| 656 | MBB.getParent()->getSSARegMap()->getRegClass(VirtReg); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 658 | // Note that, if we reused a register for a previous operand, the |
| 659 | // register we want to reload into might not actually be |
| 660 | // available. If this occurs, use the register indicated by the |
| 661 | // reuser. |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 662 | if (ReusedOperands.hasReuses()) |
| 663 | PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, |
| 664 | Spills, MaybeDeadStores); |
| 665 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 666 | PhysRegsUsed[PhysReg] = true; |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 667 | MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 668 | // This invalidates PhysReg. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 669 | Spills.ClobberPhysReg(PhysReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 670 | |
| 671 | // Any stores to this stack slot are not dead anymore. |
| 672 | MaybeDeadStores.erase(StackSlot); |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 673 | Spills.addAvailable(StackSlot, PhysReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 674 | ++NumLoads; |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 675 | MI.getOperand(i).setReg(PhysReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 676 | DEBUG(std::cerr << '\t' << *prior(MII)); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 679 | // Loop over all of the implicit defs, clearing them from our available |
| 680 | // sets. |
Jim Laskey | cd4317e | 2006-07-21 21:15:20 +0000 | [diff] [blame] | 681 | const unsigned *ImpDef = TII->getImplicitDefs(MI.getOpcode()); |
| 682 | if (ImpDef) { |
| 683 | for ( ; *ImpDef; ++ImpDef) { |
| 684 | PhysRegsUsed[*ImpDef] = true; |
| 685 | Spills.ClobberPhysReg(*ImpDef); |
| 686 | } |
Chris Lattner | b0f31bf | 2005-01-23 22:45:13 +0000 | [diff] [blame] | 687 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 688 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 689 | DEBUG(std::cerr << '\t' << MI); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 691 | // If we have folded references to memory operands, make sure we clear all |
| 692 | // physical registers that may contain the value of the spilled virtual |
| 693 | // register |
Chris Lattner | 8f1d640 | 2005-01-14 15:54:24 +0000 | [diff] [blame] | 694 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
| 695 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ++I) { |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 696 | DEBUG(std::cerr << "Folded vreg: " << I->second.first << " MR: " |
| 697 | << I->second.second); |
| 698 | unsigned VirtReg = I->second.first; |
| 699 | VirtRegMap::ModRef MR = I->second.second; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 700 | if (!VRM.hasStackSlot(VirtReg)) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 701 | DEBUG(std::cerr << ": No stack slot!\n"); |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 702 | continue; |
| 703 | } |
| 704 | int SS = VRM.getStackSlot(VirtReg); |
| 705 | DEBUG(std::cerr << " - StackSlot: " << SS << "\n"); |
| 706 | |
| 707 | // If this folded instruction is just a use, check to see if it's a |
| 708 | // straight load from the virt reg slot. |
| 709 | if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) { |
| 710 | int FrameIdx; |
Chris Lattner | 4083960 | 2006-02-02 20:12:32 +0000 | [diff] [blame] | 711 | if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) { |
Chris Lattner | 6ec3626 | 2006-10-12 17:45:38 +0000 | [diff] [blame] | 712 | if (FrameIdx == SS) { |
| 713 | // If this spill slot is available, turn it into a copy (or nothing) |
| 714 | // instead of leaving it as a load! |
| 715 | if (unsigned InReg = Spills.getSpillSlotPhysReg(SS)) { |
| 716 | DEBUG(std::cerr << "Promoted Load To Copy: " << MI); |
| 717 | MachineFunction &MF = *MBB.getParent(); |
| 718 | if (DestReg != InReg) { |
| 719 | MRI->copyRegToReg(MBB, &MI, DestReg, InReg, |
| 720 | MF.getSSARegMap()->getRegClass(VirtReg)); |
| 721 | // Revisit the copy so we make sure to notice the effects of the |
| 722 | // operation on the destreg (either needing to RA it if it's |
| 723 | // virtual or needing to clobber any values if it's physical). |
| 724 | NextMII = &MI; |
| 725 | --NextMII; // backtrack to the copy. |
| 726 | } |
| 727 | VRM.RemoveFromFoldedVirtMap(&MI); |
| 728 | MBB.erase(&MI); |
| 729 | goto ProcessNextInst; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | // If this reference is not a use, any previous store is now dead. |
| 736 | // Otherwise, the store to this stack slot is not dead anymore. |
| 737 | std::map<int, MachineInstr*>::iterator MDSI = MaybeDeadStores.find(SS); |
| 738 | if (MDSI != MaybeDeadStores.end()) { |
| 739 | if (MR & VirtRegMap::isRef) // Previous store is not dead. |
| 740 | MaybeDeadStores.erase(MDSI); |
| 741 | else { |
| 742 | // If we get here, the store is dead, nuke it now. |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 743 | assert(VirtRegMap::isMod && "Can't be modref!"); |
| 744 | DEBUG(std::cerr << "Removed dead store:\t" << *MDSI->second); |
| 745 | MBB.erase(MDSI->second); |
Chris Lattner | 229924a | 2006-05-01 22:03:24 +0000 | [diff] [blame] | 746 | VRM.RemoveFromFoldedVirtMap(MDSI->second); |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 747 | MaybeDeadStores.erase(MDSI); |
| 748 | ++NumDSE; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | // If the spill slot value is available, and this is a new definition of |
| 753 | // the value, the value is not available anymore. |
| 754 | if (MR & VirtRegMap::isMod) { |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 755 | // Notice that the value in this stack slot has been modified. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 756 | Spills.ModifyStackSlot(SS); |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 757 | |
| 758 | // If this is *just* a mod of the value, check to see if this is just a |
| 759 | // store to the spill slot (i.e. the spill got merged into the copy). If |
| 760 | // so, realize that the vreg is available now, and add the store to the |
| 761 | // MaybeDeadStore info. |
| 762 | int StackSlot; |
| 763 | if (!(MR & VirtRegMap::isRef)) { |
| 764 | if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) { |
| 765 | assert(MRegisterInfo::isPhysicalRegister(SrcReg) && |
| 766 | "Src hasn't been allocated yet?"); |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 767 | // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 768 | // this as a potentially dead store in case there is a subsequent |
| 769 | // store into the stack slot without a read from it. |
| 770 | MaybeDeadStores[StackSlot] = &MI; |
| 771 | |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 772 | // If the stack slot value was previously available in some other |
| 773 | // register, change it now. Otherwise, make the register available, |
| 774 | // in PhysReg. |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 775 | Spills.addAvailable(StackSlot, SrcReg, false /*don't clobber*/); |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 778 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 781 | // Process all of the spilled defs. |
| 782 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 783 | MachineOperand &MO = MI.getOperand(i); |
| 784 | if (MO.isRegister() && MO.getReg() && MO.isDef()) { |
| 785 | unsigned VirtReg = MO.getReg(); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 787 | if (!MRegisterInfo::isVirtualRegister(VirtReg)) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 788 | // Check to see if this is a noop copy. If so, eliminate the |
| 789 | // instruction before considering the dest reg to be changed. |
| 790 | unsigned Src, Dst; |
| 791 | if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) { |
| 792 | ++NumDCE; |
| 793 | DEBUG(std::cerr << "Removing now-noop copy: " << MI); |
| 794 | MBB.erase(&MI); |
| 795 | VRM.RemoveFromFoldedVirtMap(&MI); |
| 796 | goto ProcessNextInst; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 797 | } |
Chris Lattner | 6ec3626 | 2006-10-12 17:45:38 +0000 | [diff] [blame] | 798 | |
| 799 | // If it's not a no-op copy, it clobbers the value in the destreg. |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 800 | Spills.ClobberPhysReg(VirtReg); |
Chris Lattner | 6ec3626 | 2006-10-12 17:45:38 +0000 | [diff] [blame] | 801 | |
| 802 | // Check to see if this instruction is a load from a stack slot into |
| 803 | // a register. If so, this provides the stack slot value in the reg. |
| 804 | int FrameIdx; |
| 805 | if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) { |
| 806 | assert(DestReg == VirtReg && "Unknown load situation!"); |
| 807 | |
| 808 | // Otherwise, if it wasn't available, remember that it is now! |
| 809 | Spills.addAvailable(FrameIdx, DestReg); |
| 810 | goto ProcessNextInst; |
| 811 | } |
| 812 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 813 | continue; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 816 | // The only vregs left are stack slot definitions. |
| 817 | int StackSlot = VRM.getStackSlot(VirtReg); |
| 818 | const TargetRegisterClass *RC = |
| 819 | MBB.getParent()->getSSARegMap()->getRegClass(VirtReg); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 821 | // If this def is part of a two-address operand, make sure to execute |
| 822 | // the store from the correct physical register. |
| 823 | unsigned PhysReg; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame^] | 824 | int TiedOp = TII->getTiedToSrcOperand(MI.getOpcode(), i); |
| 825 | if (TiedOp != -1) |
| 826 | PhysReg = MI.getOperand(TiedOp).getReg(); |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 827 | else |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 828 | PhysReg = VRM.getPhys(VirtReg); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 830 | PhysRegsUsed[PhysReg] = true; |
| 831 | MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC); |
| 832 | DEBUG(std::cerr << "Store:\t" << *next(MII)); |
Chris Lattner | e53f4a0 | 2006-05-04 17:52:23 +0000 | [diff] [blame] | 833 | MI.getOperand(i).setReg(PhysReg); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 109afed | 2006-02-03 03:16:14 +0000 | [diff] [blame] | 835 | // Check to see if this is a noop copy. If so, eliminate the |
| 836 | // instruction before considering the dest reg to be changed. |
| 837 | { |
| 838 | unsigned Src, Dst; |
| 839 | if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) { |
| 840 | ++NumDCE; |
| 841 | DEBUG(std::cerr << "Removing now-noop copy: " << MI); |
| 842 | MBB.erase(&MI); |
Chris Lattner | 229924a | 2006-05-01 22:03:24 +0000 | [diff] [blame] | 843 | VRM.RemoveFromFoldedVirtMap(&MI); |
Chris Lattner | 109afed | 2006-02-03 03:16:14 +0000 | [diff] [blame] | 844 | goto ProcessNextInst; |
| 845 | } |
| 846 | } |
| 847 | |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 848 | // If there is a dead store to this stack slot, nuke it now. |
| 849 | MachineInstr *&LastStore = MaybeDeadStores[StackSlot]; |
| 850 | if (LastStore) { |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 851 | DEBUG(std::cerr << "Removed dead store:\t" << *LastStore); |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 852 | ++NumDSE; |
| 853 | MBB.erase(LastStore); |
Chris Lattner | 229924a | 2006-05-01 22:03:24 +0000 | [diff] [blame] | 854 | VRM.RemoveFromFoldedVirtMap(LastStore); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 855 | } |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 856 | LastStore = next(MII); |
| 857 | |
| 858 | // If the stack slot value was previously available in some other |
| 859 | // register, change it now. Otherwise, make the register available, |
| 860 | // in PhysReg. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 861 | Spills.ModifyStackSlot(StackSlot); |
| 862 | Spills.ClobberPhysReg(PhysReg); |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 863 | Spills.addAvailable(StackSlot, PhysReg); |
Chris Lattner | 84e752a | 2006-02-03 03:06:49 +0000 | [diff] [blame] | 864 | ++NumStores; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 867 | ProcessNextInst: |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 868 | MII = NextMII; |
| 869 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Chris Lattner | f7da2c7 | 2006-08-24 22:43:55 +0000 | [diff] [blame] | 872 | |
| 873 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 874 | llvm::Spiller* llvm::createSpiller() { |
| 875 | switch (SpillerOpt) { |
| 876 | default: assert(0 && "Unreachable!"); |
| 877 | case local: |
| 878 | return new LocalSpiller(); |
| 879 | case simple: |
| 880 | return new SimpleSpiller(); |
| 881 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 882 | } |