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