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