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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 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" |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Evan Cheng | 957840b | 2007-02-21 02:22:03 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/BitVector.h" |
Evan Cheng | cb74266 | 2008-06-04 09:16:33 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DepthFirstIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
| 35 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 08a4d5a | 2007-01-23 00:59:48 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 27f2916 | 2004-10-26 15:35:58 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 40 | STATISTIC(NumSpills , "Number of register spills"); |
Evan Cheng | 625986a | 2008-06-18 07:47:28 +0000 | [diff] [blame] | 41 | STATISTIC(NumPSpills , "Number of physical register spills"); |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 42 | STATISTIC(NumReMats , "Number of re-materialization"); |
| 43 | STATISTIC(NumDRM , "Number of re-materializable defs elided"); |
| 44 | STATISTIC(NumStores , "Number of stores added"); |
| 45 | STATISTIC(NumLoads , "Number of loads added"); |
| 46 | STATISTIC(NumReused , "Number of values reused"); |
| 47 | STATISTIC(NumDSE , "Number of dead stores elided"); |
| 48 | STATISTIC(NumDCE , "Number of copies elided"); |
| 49 | STATISTIC(NumDSS , "Number of dead spill slots removed"); |
| 50 | STATISTIC(NumCommutes, "Number of instructions commuted"); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 51 | STATISTIC(NumOmitted , "Number of reloads omited"); |
| 52 | STATISTIC(NumCopified, "Number of available reloads turned into copies"); |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 53 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 54 | namespace { |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 55 | enum SpillerName { simple, local }; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 58 | static cl::opt<SpillerName> |
| 59 | SpillerOpt("spiller", |
| 60 | cl::desc("Spiller to use: (default: local)"), |
| 61 | cl::Prefix, |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 62 | cl::values(clEnumVal(simple, "simple spiller"), |
| 63 | clEnumVal(local, "local spiller"), |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 64 | clEnumValEnd), |
| 65 | cl::init(local)); |
| 66 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // VirtRegMap implementation |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 71 | VirtRegMap::VirtRegMap(MachineFunction &mf) |
| 72 | : TII(*mf.getTarget().getInstrInfo()), MF(mf), |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 73 | Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT), |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 74 | Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0), |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 75 | Virt2SplitKillMap(0), ReMatMap(NULL), ReMatId(MAX_STACK_SLOT+1), |
| 76 | LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { |
| 77 | SpillSlotToUsesMap.resize(8); |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 78 | ImplicitDefed.resize(MF.getRegInfo().getLastVirtReg()+1- |
| 79 | TargetRegisterInfo::FirstVirtualRegister); |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 80 | grow(); |
| 81 | } |
| 82 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 83 | void VirtRegMap::grow() { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 84 | unsigned LastVirtReg = MF.getRegInfo().getLastVirtReg(); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 85 | Virt2PhysMap.grow(LastVirtReg); |
| 86 | Virt2StackSlotMap.grow(LastVirtReg); |
| 87 | Virt2ReMatIdMap.grow(LastVirtReg); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 88 | Virt2SplitMap.grow(LastVirtReg); |
Evan Cheng | adf8590 | 2007-12-05 09:51:10 +0000 | [diff] [blame] | 89 | Virt2SplitKillMap.grow(LastVirtReg); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 90 | ReMatMap.grow(LastVirtReg); |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 91 | ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 94 | int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 95 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 96 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 97 | "attempt to assign stack slot to already spilled register"); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 98 | const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(virtReg); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 99 | int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(), |
| 100 | RC->getAlignment()); |
| 101 | if (LowSpillSlot == NO_STACK_SLOT) |
| 102 | LowSpillSlot = SS; |
| 103 | if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot) |
| 104 | HighSpillSlot = SS; |
| 105 | unsigned Idx = SS-LowSpillSlot; |
| 106 | while (Idx >= SpillSlotToUsesMap.size()) |
| 107 | SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2); |
| 108 | Virt2StackSlotMap[virtReg] = SS; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 109 | ++NumSpills; |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 110 | return SS; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 113 | void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 114 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 115 | assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT && |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 116 | "attempt to assign stack slot to already spilled register"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 117 | assert((SS >= 0 || |
| 118 | (SS >= MF.getFrameInfo()->getObjectIndexBegin())) && |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 119 | "illegal fixed frame index"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 120 | Virt2StackSlotMap[virtReg] = SS; |
Alkis Evlogimenos | 38af59a | 2004-05-29 20:38:05 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 123 | int VirtRegMap::assignVirtReMatId(unsigned virtReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 124 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 125 | assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT && |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 126 | "attempt to assign re-mat id to already spilled register"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 127 | Virt2ReMatIdMap[virtReg] = ReMatId; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 128 | return ReMatId++; |
| 129 | } |
| 130 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 131 | void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 132 | assert(TargetRegisterInfo::isVirtualRegister(virtReg)); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 133 | assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT && |
| 134 | "attempt to assign re-mat id to already spilled register"); |
| 135 | Virt2ReMatIdMap[virtReg] = id; |
| 136 | } |
| 137 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 138 | int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) { |
| 139 | std::map<const TargetRegisterClass*, int>::iterator I = |
| 140 | EmergencySpillSlots.find(RC); |
| 141 | if (I != EmergencySpillSlots.end()) |
| 142 | return I->second; |
| 143 | int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(), |
| 144 | RC->getAlignment()); |
| 145 | if (LowSpillSlot == NO_STACK_SLOT) |
| 146 | LowSpillSlot = SS; |
| 147 | if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot) |
| 148 | HighSpillSlot = SS; |
Dan Gohman | 4daa907 | 2008-10-06 18:00:07 +0000 | [diff] [blame] | 149 | EmergencySpillSlots[RC] = SS; |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 150 | return SS; |
| 151 | } |
| 152 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 153 | void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) { |
| 154 | if (!MF.getFrameInfo()->isFixedObjectIndex(FI)) { |
David Greene | cff8608 | 2008-05-22 21:12:21 +0000 | [diff] [blame] | 155 | // If FI < LowSpillSlot, this stack reference was produced by |
| 156 | // instruction selection and is not a spill |
| 157 | if (FI >= LowSpillSlot) { |
| 158 | assert(FI >= 0 && "Spill slot index should not be negative!"); |
Bill Wendling | f3061f8 | 2008-05-23 01:29:08 +0000 | [diff] [blame] | 159 | assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size() |
David Greene | cff8608 | 2008-05-22 21:12:21 +0000 | [diff] [blame] | 160 | && "Invalid spill slot"); |
| 161 | SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI); |
| 162 | } |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 166 | void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI, |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 167 | MachineInstr *NewMI, ModRef MRInfo) { |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 168 | // Move previous memory references folded to new instruction. |
| 169 | MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 170 | for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI), |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 171 | E = MI2VirtMap.end(); I != E && I->first == OldMI; ) { |
| 172 | MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second)); |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 173 | MI2VirtMap.erase(I++); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | dbea973 | 2004-09-30 16:35:08 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 176 | // add new memory reference |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 177 | MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo))); |
Alkis Evlogimenos | 5f37502 | 2004-03-01 20:05:10 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 180 | void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) { |
| 181 | MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI); |
| 182 | MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo))); |
| 183 | } |
| 184 | |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 185 | void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) { |
| 186 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 187 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 188 | if (!MO.isFI()) |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 189 | continue; |
| 190 | int FI = MO.getIndex(); |
| 191 | if (MF.getFrameInfo()->isFixedObjectIndex(FI)) |
| 192 | continue; |
David Greene | cff8608 | 2008-05-22 21:12:21 +0000 | [diff] [blame] | 193 | // This stack reference was produced by instruction selection and |
| 194 | // is not a spill |
| 195 | if (FI < LowSpillSlot) |
| 196 | continue; |
Bill Wendling | f3061f8 | 2008-05-23 01:29:08 +0000 | [diff] [blame] | 197 | assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size() |
David Greene | cff8608 | 2008-05-22 21:12:21 +0000 | [diff] [blame] | 198 | && "Invalid spill slot"); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 199 | SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI); |
| 200 | } |
| 201 | MI2VirtMap.erase(MI); |
| 202 | SpillPt2VirtMap.erase(MI); |
| 203 | RestorePt2VirtMap.erase(MI); |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 204 | EmergencySpillMap.erase(MI); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 207 | void VirtRegMap::print(std::ostream &OS) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 208 | const TargetRegisterInfo* TRI = MF.getTarget().getRegisterInfo(); |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 210 | OS << "********** REGISTER MAP **********\n"; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 211 | for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 212 | e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) { |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 213 | if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 214 | OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i]) |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 215 | << "]\n"; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 218 | for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 219 | e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) |
Chris Lattner | 7f690e6 | 2004-09-30 02:15:18 +0000 | [diff] [blame] | 220 | if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT) |
| 221 | OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n"; |
| 222 | OS << '\n'; |
Alkis Evlogimenos | 34d9bc9 | 2004-02-23 23:08:11 +0000 | [diff] [blame] | 223 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 224 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 225 | void VirtRegMap::dump() const { |
Dan Gohman | b576931 | 2008-03-12 20:52:10 +0000 | [diff] [blame] | 226 | print(cerr); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 227 | } |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 229 | |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | // Simple Spiller Implementation |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | |
| 234 | Spiller::~Spiller() {} |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 235 | |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 236 | namespace { |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 237 | struct VISIBILITY_HIDDEN SimpleSpiller : public Spiller { |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 238 | bool runOnMachineFunction(MachineFunction& mf, VirtRegMap &VRM); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 239 | }; |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 35f2705 | 2006-05-01 21:16:03 +0000 | [diff] [blame] | 242 | bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 243 | DOUT << "********** REWRITE MACHINE CODE **********\n"; |
| 244 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
Chris Lattner | b0f31bf | 2005-01-23 22:45:13 +0000 | [diff] [blame] | 245 | const TargetMachine &TM = MF.getTarget(); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 246 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Owen Anderson | 724651a | 2008-08-19 01:05:33 +0000 | [diff] [blame] | 247 | const TargetRegisterInfo &TRI = *TM.getRegisterInfo(); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 250 | // LoadedRegs - Keep track of which vregs are loaded, so that we only load |
| 251 | // each vreg once (in the case where a spilled vreg is used by multiple |
| 252 | // operands). This is always smaller than the number of operands to the |
| 253 | // current machine instr, so it should be small. |
| 254 | std::vector<unsigned> LoadedRegs; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 256 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 257 | MBBI != E; ++MBBI) { |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 258 | DOUT << MBBI->getBasicBlock()->getName() << ":\n"; |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 259 | MachineBasicBlock &MBB = *MBBI; |
| 260 | for (MachineBasicBlock::iterator MII = MBB.begin(), |
| 261 | E = MBB.end(); MII != E; ++MII) { |
| 262 | MachineInstr &MI = *MII; |
| 263 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 264 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 265 | if (MO.isReg() && MO.getReg()) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 266 | if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 267 | unsigned VirtReg = MO.getReg(); |
Owen Anderson | 724651a | 2008-08-19 01:05:33 +0000 | [diff] [blame] | 268 | unsigned SubIdx = MO.getSubReg(); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 269 | unsigned PhysReg = VRM.getPhys(VirtReg); |
Owen Anderson | 724651a | 2008-08-19 01:05:33 +0000 | [diff] [blame] | 270 | unsigned RReg = SubIdx ? TRI.getSubReg(PhysReg, SubIdx) : PhysReg; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 271 | if (!VRM.isAssignedReg(VirtReg)) { |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 272 | int StackSlot = VRM.getStackSlot(VirtReg); |
Chris Lattner | bf9716b | 2005-09-30 01:29:00 +0000 | [diff] [blame] | 273 | const TargetRegisterClass* RC = |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 274 | MF.getRegInfo().getRegClass(VirtReg); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 276 | if (MO.isUse() && |
| 277 | std::find(LoadedRegs.begin(), LoadedRegs.end(), VirtReg) |
| 278 | == LoadedRegs.end()) { |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 279 | TII.loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 280 | MachineInstr *LoadMI = prior(MII); |
| 281 | VRM.addSpillSlotUse(StackSlot, LoadMI); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 282 | LoadedRegs.push_back(VirtReg); |
| 283 | ++NumLoads; |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 284 | DOUT << '\t' << *LoadMI; |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 285 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 287 | if (MO.isDef()) { |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 288 | TII.storeRegToStackSlot(MBB, next(MII), PhysReg, true, |
Evan Cheng | d64b5c8 | 2007-12-05 03:14:33 +0000 | [diff] [blame] | 289 | StackSlot, RC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 290 | MachineInstr *StoreMI = next(MII); |
| 291 | VRM.addSpillSlotUse(StackSlot, StoreMI); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 292 | ++NumStores; |
| 293 | } |
Chris Lattner | 0fc27cc | 2004-09-30 02:59:33 +0000 | [diff] [blame] | 294 | } |
Owen Anderson | 724651a | 2008-08-19 01:05:33 +0000 | [diff] [blame] | 295 | MF.getRegInfo().setPhysRegUsed(RReg); |
| 296 | MI.getOperand(i).setReg(RReg); |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 297 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 298 | MF.getRegInfo().setPhysRegUsed(MO.getReg()); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 299 | } |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 886dd91 | 2005-04-04 21:35:34 +0000 | [diff] [blame] | 302 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 303 | DOUT << '\t' << MI; |
Chris Lattner | 4ea1b82 | 2004-09-30 02:33:48 +0000 | [diff] [blame] | 304 | LoadedRegs.clear(); |
Alkis Evlogimenos | dd420e0 | 2004-03-01 23:18:15 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | //===----------------------------------------------------------------------===// |
| 311 | // Local Spiller Implementation |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 314 | /// AvailableSpills - As the local spiller is scanning and rewriting an MBB from |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 315 | /// top down, keep track of which spills slots or remat are available in each |
| 316 | /// register. |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 317 | /// |
| 318 | /// Note that not all physregs are created equal here. In particular, some |
| 319 | /// physregs are reloads that we are allowed to clobber or ignore at any time. |
| 320 | /// Other physregs are values that the register allocated program is using that |
| 321 | /// we cannot CHANGE, but we can read if we like. We keep track of this on a |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 322 | /// per-stack-slot / remat id basis as the low bit in the value of the |
| 323 | /// SpillSlotsAvailable entries. The predicate 'canClobberPhysReg()' checks |
| 324 | /// this bit and addAvailable sets it if. |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 325 | namespace { |
| 326 | class VISIBILITY_HIDDEN AvailableSpills { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 327 | const TargetRegisterInfo *TRI; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 328 | const TargetInstrInfo *TII; |
| 329 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 330 | // SpillSlotsOrReMatsAvailable - This map keeps track of all of the spilled |
| 331 | // or remat'ed virtual register values that are still available, due to being |
| 332 | // loaded or stored to, but not invalidated yet. |
| 333 | std::map<int, unsigned> SpillSlotsOrReMatsAvailable; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 334 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 335 | // PhysRegsAvailable - This is the inverse of SpillSlotsOrReMatsAvailable, |
| 336 | // indicating which stack slot values are currently held by a physreg. This |
| 337 | // is used to invalidate entries in SpillSlotsOrReMatsAvailable when a |
| 338 | // physreg is modified. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 339 | std::multimap<unsigned, int> PhysRegsAvailable; |
| 340 | |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 341 | void disallowClobberPhysRegOnly(unsigned PhysReg); |
| 342 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 343 | void ClobberPhysRegOnly(unsigned PhysReg); |
| 344 | public: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 345 | AvailableSpills(const TargetRegisterInfo *tri, const TargetInstrInfo *tii) |
| 346 | : TRI(tri), TII(tii) { |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 347 | } |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 348 | |
| 349 | /// clear - Reset the state. |
| 350 | void clear() { |
| 351 | SpillSlotsOrReMatsAvailable.clear(); |
| 352 | PhysRegsAvailable.clear(); |
| 353 | } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 354 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 355 | const TargetRegisterInfo *getRegInfo() const { return TRI; } |
Evan Cheng | 91e2390 | 2007-02-23 01:13:26 +0000 | [diff] [blame] | 356 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 357 | /// getSpillSlotOrReMatPhysReg - If the specified stack slot or remat is |
| 358 | /// available in a physical register, return that PhysReg, otherwise |
| 359 | /// return 0. |
| 360 | unsigned getSpillSlotOrReMatPhysReg(int Slot) const { |
| 361 | std::map<int, unsigned>::const_iterator I = |
| 362 | SpillSlotsOrReMatsAvailable.find(Slot); |
| 363 | if (I != SpillSlotsOrReMatsAvailable.end()) { |
Evan Cheng | b9591c6 | 2007-07-11 08:47:44 +0000 | [diff] [blame] | 364 | return I->second >> 1; // Remove the CanClobber bit. |
Evan Cheng | 91e2390 | 2007-02-23 01:13:26 +0000 | [diff] [blame] | 365 | } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | } |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 368 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 369 | /// addAvailable - Mark that the specified stack slot / remat is available in |
| 370 | /// the specified physreg. If CanClobber is true, the physreg can be modified |
| 371 | /// at any time without changing the semantics of the program. |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 372 | void addAvailable(int SlotOrReMat, unsigned Reg, bool CanClobber = true) { |
Chris Lattner | 8666249 | 2006-02-03 23:50:46 +0000 | [diff] [blame] | 373 | // If this stack slot is thought to be available in some other physreg, |
| 374 | // remove its record. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 375 | ModifyStackSlotOrReMat(SlotOrReMat); |
Chris Lattner | 8666249 | 2006-02-03 23:50:46 +0000 | [diff] [blame] | 376 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 377 | PhysRegsAvailable.insert(std::make_pair(Reg, SlotOrReMat)); |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 378 | SpillSlotsOrReMatsAvailable[SlotOrReMat]= (Reg << 1) | (unsigned)CanClobber; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 379 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 380 | if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) |
| 381 | DOUT << "Remembering RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 382 | else |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 383 | DOUT << "Remembering SS#" << SlotOrReMat; |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 384 | DOUT << " in physreg " << TRI->getName(Reg) << "\n"; |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 385 | } |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 387 | /// canClobberPhysReg - Return true if the spiller is allowed to change the |
| 388 | /// value of the specified stackslot register if it desires. The specified |
| 389 | /// stack slot must be available in a physreg for this query to make sense. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 390 | bool canClobberPhysReg(int SlotOrReMat) const { |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 391 | assert(SpillSlotsOrReMatsAvailable.count(SlotOrReMat) && |
| 392 | "Value not available!"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 393 | return SpillSlotsOrReMatsAvailable.find(SlotOrReMat)->second & 1; |
Chris Lattner | 593c958 | 2006-02-03 23:28:46 +0000 | [diff] [blame] | 394 | } |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 395 | |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 396 | /// disallowClobberPhysReg - Unset the CanClobber bit of the specified |
| 397 | /// stackslot register. The register is still available but is no longer |
| 398 | /// allowed to be modifed. |
| 399 | void disallowClobberPhysReg(unsigned PhysReg); |
| 400 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 401 | /// ClobberPhysReg - This is called when the specified physreg changes |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 402 | /// value. We use this to invalidate any info about stuff that lives in |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 403 | /// it and any of its aliases. |
| 404 | void ClobberPhysReg(unsigned PhysReg); |
| 405 | |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 406 | /// ModifyStackSlotOrReMat - This method is called when the value in a stack |
| 407 | /// slot changes. This removes information about which register the previous |
| 408 | /// value for this slot lives in (as the previous value is dead now). |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 409 | void ModifyStackSlotOrReMat(int SlotOrReMat); |
Evan Cheng | 6d209c4 | 2009-02-12 09:43:23 +0000 | [diff] [blame] | 410 | |
| 411 | void AddAvailableRegsToLiveIn(MachineBasicBlock &MBB); |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 412 | }; |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 414 | |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 415 | /// disallowClobberPhysRegOnly - Unset the CanClobber bit of the specified |
| 416 | /// stackslot register. The register is still available but is no longer |
| 417 | /// allowed to be modifed. |
| 418 | void AvailableSpills::disallowClobberPhysRegOnly(unsigned PhysReg) { |
| 419 | std::multimap<unsigned, int>::iterator I = |
| 420 | PhysRegsAvailable.lower_bound(PhysReg); |
| 421 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 422 | int SlotOrReMat = I->second; |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 423 | I++; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 424 | assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 425 | "Bidirectional map mismatch!"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 426 | SpillSlotsOrReMatsAvailable[SlotOrReMat] &= ~1; |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 427 | DOUT << "PhysReg " << TRI->getName(PhysReg) |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 428 | << " copied, it is available for use but can no longer be modified\n"; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /// disallowClobberPhysReg - Unset the CanClobber bit of the specified |
| 433 | /// stackslot register and its aliases. The register and its aliases may |
| 434 | /// still available but is no longer allowed to be modifed. |
| 435 | void AvailableSpills::disallowClobberPhysReg(unsigned PhysReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 436 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) |
Evan Cheng | 7a0d51c | 2006-12-14 07:54:05 +0000 | [diff] [blame] | 437 | disallowClobberPhysRegOnly(*AS); |
| 438 | disallowClobberPhysRegOnly(PhysReg); |
| 439 | } |
| 440 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 441 | /// ClobberPhysRegOnly - This is called when the specified physreg changes |
| 442 | /// value. We use this to invalidate any info about stuff we thing lives in it. |
| 443 | void AvailableSpills::ClobberPhysRegOnly(unsigned PhysReg) { |
| 444 | std::multimap<unsigned, int>::iterator I = |
| 445 | PhysRegsAvailable.lower_bound(PhysReg); |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 446 | while (I != PhysRegsAvailable.end() && I->first == PhysReg) { |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 447 | int SlotOrReMat = I->second; |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 448 | PhysRegsAvailable.erase(I++); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 449 | assert((SpillSlotsOrReMatsAvailable[SlotOrReMat] >> 1) == PhysReg && |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 450 | "Bidirectional map mismatch!"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 451 | SpillSlotsOrReMatsAvailable.erase(SlotOrReMat); |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 452 | DOUT << "PhysReg " << TRI->getName(PhysReg) |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 453 | << " clobbered, invalidating "; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 454 | if (SlotOrReMat > VirtRegMap::MAX_STACK_SLOT) |
| 455 | DOUT << "RM#" << SlotOrReMat-VirtRegMap::MAX_STACK_SLOT-1 << "\n"; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 456 | else |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 457 | DOUT << "SS#" << SlotOrReMat << "\n"; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 458 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 461 | /// ClobberPhysReg - This is called when the specified physreg changes |
| 462 | /// value. We use this to invalidate any info about stuff we thing lives in |
| 463 | /// it and any of its aliases. |
| 464 | void AvailableSpills::ClobberPhysReg(unsigned PhysReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 465 | for (const unsigned *AS = TRI->getAliasSet(PhysReg); *AS; ++AS) |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 466 | ClobberPhysRegOnly(*AS); |
| 467 | ClobberPhysRegOnly(PhysReg); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 470 | /// ModifyStackSlotOrReMat - This method is called when the value in a stack |
| 471 | /// slot changes. This removes information about which register the previous |
| 472 | /// value for this slot lives in (as the previous value is dead now). |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 473 | void AvailableSpills::ModifyStackSlotOrReMat(int SlotOrReMat) { |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 474 | std::map<int, unsigned>::iterator It = |
| 475 | SpillSlotsOrReMatsAvailable.find(SlotOrReMat); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 476 | if (It == SpillSlotsOrReMatsAvailable.end()) return; |
Evan Cheng | b9591c6 | 2007-07-11 08:47:44 +0000 | [diff] [blame] | 477 | unsigned Reg = It->second >> 1; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 478 | SpillSlotsOrReMatsAvailable.erase(It); |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 479 | |
| 480 | // This register may hold the value of multiple stack slots, only remove this |
| 481 | // stack slot from the set of values the register contains. |
| 482 | std::multimap<unsigned, int>::iterator I = PhysRegsAvailable.lower_bound(Reg); |
| 483 | for (; ; ++I) { |
| 484 | assert(I != PhysRegsAvailable.end() && I->first == Reg && |
| 485 | "Map inverse broken!"); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 486 | if (I->second == SlotOrReMat) break; |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 487 | } |
| 488 | PhysRegsAvailable.erase(I); |
| 489 | } |
| 490 | |
Evan Cheng | 6d209c4 | 2009-02-12 09:43:23 +0000 | [diff] [blame] | 491 | /// AddAvailableRegsToLiveIn - Availability information is being kept coming |
| 492 | /// into the specified MBB. Add available physical registers as live-in's |
| 493 | /// so register scavenger and post-allocation scheduler are happy. |
| 494 | void AvailableSpills::AddAvailableRegsToLiveIn(MachineBasicBlock &MBB) { |
| 495 | for (std::multimap<unsigned, int>::iterator |
| 496 | I = PhysRegsAvailable.begin(), E = PhysRegsAvailable.end(); |
| 497 | I != E; ++I) { |
| 498 | unsigned Reg = (*I).first; |
Evan Cheng | 8679119 | 2009-02-12 10:32:17 +0000 | [diff] [blame] | 499 | const TargetRegisterClass* RC = TRI->getPhysicalRegisterRegClass(Reg); |
| 500 | // FIXME: A temporary workaround. We can't reuse available value if it's |
| 501 | // not safe to move the def of the virtual register's class. e.g. |
| 502 | // X86::RFP* register classes. Do not add it as a live-in. |
| 503 | if (!TII->isSafeToMoveRegClassDefs(RC)) |
| 504 | continue; |
Evan Cheng | 6d209c4 | 2009-02-12 09:43:23 +0000 | [diff] [blame] | 505 | if (!MBB.isLiveIn(Reg)) |
| 506 | MBB.addLiveIn(Reg); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /// findSinglePredSuccessor - Return via reference a vector of machine basic |
| 511 | /// blocks each of which is a successor of the specified BB and has no other |
| 512 | /// predecessor. |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 513 | static void findSinglePredSuccessor(MachineBasicBlock *MBB, |
| 514 | SmallVectorImpl<MachineBasicBlock *> &Succs) { |
| 515 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 516 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 517 | MachineBasicBlock *SuccMBB = *SI; |
| 518 | if (SuccMBB->pred_size() == 1) |
| 519 | Succs.push_back(SuccMBB); |
| 520 | } |
| 521 | } |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 522 | |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 523 | namespace { |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 524 | /// LocalSpiller - This spiller does a simple pass over the machine basic |
| 525 | /// block to attempt to keep spills in registers as much as possible for |
| 526 | /// blocks that have low register pressure (the vreg may be spilled due to |
| 527 | /// register pressure in other blocks). |
| 528 | class VISIBILITY_HIDDEN LocalSpiller : public Spiller { |
| 529 | MachineRegisterInfo *RegInfo; |
| 530 | const TargetRegisterInfo *TRI; |
| 531 | const TargetInstrInfo *TII; |
| 532 | DenseMap<MachineInstr*, unsigned> DistanceMap; |
| 533 | public: |
| 534 | bool runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) { |
| 535 | RegInfo = &MF.getRegInfo(); |
| 536 | TRI = MF.getTarget().getRegisterInfo(); |
| 537 | TII = MF.getTarget().getInstrInfo(); |
| 538 | DOUT << "\n**** Local spiller rewriting function '" |
| 539 | << MF.getFunction()->getName() << "':\n"; |
| 540 | DOUT << "**** Machine Instrs (NOTE! Does not include spills and reloads!)" |
| 541 | " ****\n"; |
| 542 | DEBUG(MF.dump()); |
| 543 | |
| 544 | // Spills - Keep track of which spilled values are available in physregs |
| 545 | // so that we can choose to reuse the physregs instead of emitting |
| 546 | // reloads. This is usually refreshed per basic block. |
| 547 | AvailableSpills Spills(TRI, TII); |
| 548 | |
| 549 | // SingleEntrySuccs - Successor blocks which have a single predecessor. |
| 550 | SmallVector<MachineBasicBlock*, 4> SinglePredSuccs; |
| 551 | SmallPtrSet<MachineBasicBlock*,16> EarlyVisited; |
| 552 | |
| 553 | // Traverse the basic blocks depth first. |
| 554 | MachineBasicBlock *Entry = MF.begin(); |
| 555 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
| 556 | for (df_ext_iterator<MachineBasicBlock*, |
| 557 | SmallPtrSet<MachineBasicBlock*,16> > |
| 558 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 559 | DFI != E; ++DFI) { |
| 560 | MachineBasicBlock *MBB = *DFI; |
| 561 | if (!EarlyVisited.count(MBB)) |
| 562 | RewriteMBB(*MBB, VRM, Spills); |
| 563 | |
| 564 | // If this MBB is the only predecessor of a successor. Keep the |
| 565 | // availability information and visit it next. |
| 566 | do { |
| 567 | // Keep visiting single predecessor successor as long as possible. |
| 568 | SinglePredSuccs.clear(); |
| 569 | findSinglePredSuccessor(MBB, SinglePredSuccs); |
| 570 | if (SinglePredSuccs.empty()) |
| 571 | MBB = 0; |
| 572 | else { |
| 573 | // FIXME: More than one successors, each of which has MBB has |
| 574 | // the only predecessor. |
| 575 | MBB = SinglePredSuccs[0]; |
Evan Cheng | 6d209c4 | 2009-02-12 09:43:23 +0000 | [diff] [blame] | 576 | if (!Visited.count(MBB) && EarlyVisited.insert(MBB)) { |
| 577 | Spills.AddAvailableRegsToLiveIn(*MBB); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 578 | RewriteMBB(*MBB, VRM, Spills); |
Evan Cheng | 6d209c4 | 2009-02-12 09:43:23 +0000 | [diff] [blame] | 579 | } |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 580 | } |
| 581 | } while (MBB); |
| 582 | |
| 583 | // Clear the availability info. |
| 584 | Spills.clear(); |
| 585 | } |
| 586 | |
| 587 | DOUT << "**** Post Machine Instrs ****\n"; |
| 588 | DEBUG(MF.dump()); |
| 589 | |
| 590 | // Mark unused spill slots. |
| 591 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 592 | int SS = VRM.getLowSpillSlot(); |
| 593 | if (SS != VirtRegMap::NO_STACK_SLOT) |
| 594 | for (int e = VRM.getHighSpillSlot(); SS <= e; ++SS) |
| 595 | if (!VRM.isSpillSlotUsed(SS)) { |
| 596 | MFI->RemoveStackObject(SS); |
| 597 | ++NumDSS; |
| 598 | } |
| 599 | |
| 600 | return true; |
| 601 | } |
| 602 | private: |
| 603 | void TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist, |
| 604 | unsigned Reg, BitVector &RegKills, |
| 605 | std::vector<MachineOperand*> &KillOps); |
| 606 | bool PrepForUnfoldOpti(MachineBasicBlock &MBB, |
| 607 | MachineBasicBlock::iterator &MII, |
| 608 | std::vector<MachineInstr*> &MaybeDeadStores, |
| 609 | AvailableSpills &Spills, BitVector &RegKills, |
| 610 | std::vector<MachineOperand*> &KillOps, |
| 611 | VirtRegMap &VRM); |
| 612 | bool CommuteToFoldReload(MachineBasicBlock &MBB, |
| 613 | MachineBasicBlock::iterator &MII, |
| 614 | unsigned VirtReg, unsigned SrcReg, int SS, |
| 615 | BitVector &RegKills, |
| 616 | std::vector<MachineOperand*> &KillOps, |
| 617 | const TargetRegisterInfo *TRI, |
| 618 | VirtRegMap &VRM); |
| 619 | void SpillRegToStackSlot(MachineBasicBlock &MBB, |
| 620 | MachineBasicBlock::iterator &MII, |
| 621 | int Idx, unsigned PhysReg, int StackSlot, |
| 622 | const TargetRegisterClass *RC, |
| 623 | bool isAvailable, MachineInstr *&LastStore, |
| 624 | AvailableSpills &Spills, |
| 625 | SmallSet<MachineInstr*, 4> &ReMatDefs, |
| 626 | BitVector &RegKills, |
| 627 | std::vector<MachineOperand*> &KillOps, |
| 628 | VirtRegMap &VRM); |
| 629 | void RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM, |
| 630 | AvailableSpills &Spills); |
| 631 | }; |
| 632 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 633 | |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 634 | /// InvalidateKills - MI is going to be deleted. If any of its operands are |
| 635 | /// marked kill, then invalidate the information. |
| 636 | static void InvalidateKills(MachineInstr &MI, BitVector &RegKills, |
Evan Cheng | c91f0b8 | 2007-08-14 20:23:13 +0000 | [diff] [blame] | 637 | std::vector<MachineOperand*> &KillOps, |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 638 | SmallVector<unsigned, 2> *KillRegs = NULL) { |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 639 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 640 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 641 | if (!MO.isReg() || !MO.isUse() || !MO.isKill()) |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 642 | continue; |
| 643 | unsigned Reg = MO.getReg(); |
Evan Cheng | e3b8a48 | 2008-08-05 21:51:46 +0000 | [diff] [blame] | 644 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 645 | continue; |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 646 | if (KillRegs) |
| 647 | KillRegs->push_back(Reg); |
Evan Cheng | e3b8a48 | 2008-08-05 21:51:46 +0000 | [diff] [blame] | 648 | assert(Reg < KillOps.size()); |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 649 | if (KillOps[Reg] == &MO) { |
| 650 | RegKills.reset(Reg); |
| 651 | KillOps[Reg] = NULL; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Evan Cheng | 39c883c | 2007-12-11 23:36:57 +0000 | [diff] [blame] | 656 | /// InvalidateKill - A MI that defines the specified register is being deleted, |
| 657 | /// invalidate the register kill information. |
| 658 | static void InvalidateKill(unsigned Reg, BitVector &RegKills, |
| 659 | std::vector<MachineOperand*> &KillOps) { |
| 660 | if (RegKills[Reg]) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 661 | KillOps[Reg]->setIsKill(false); |
Evan Cheng | 39c883c | 2007-12-11 23:36:57 +0000 | [diff] [blame] | 662 | KillOps[Reg] = NULL; |
| 663 | RegKills.reset(Reg); |
| 664 | } |
| 665 | } |
| 666 | |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 667 | /// InvalidateRegDef - If the def operand of the specified def MI is now dead |
| 668 | /// (since it's spill instruction is removed), mark it isDead. Also checks if |
| 669 | /// the def MI has other definition operands that are not dead. Returns it by |
| 670 | /// reference. |
| 671 | static bool InvalidateRegDef(MachineBasicBlock::iterator I, |
| 672 | MachineInstr &NewDef, unsigned Reg, |
| 673 | bool &HasLiveDef) { |
| 674 | // Due to remat, it's possible this reg isn't being reused. That is, |
| 675 | // the def of this reg (by prev MI) is now dead. |
| 676 | MachineInstr *DefMI = I; |
| 677 | MachineOperand *DefOp = NULL; |
| 678 | for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) { |
| 679 | MachineOperand &MO = DefMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 680 | if (MO.isReg() && MO.isDef()) { |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 681 | if (MO.getReg() == Reg) |
| 682 | DefOp = &MO; |
| 683 | else if (!MO.isDead()) |
| 684 | HasLiveDef = true; |
| 685 | } |
| 686 | } |
| 687 | if (!DefOp) |
| 688 | return false; |
| 689 | |
| 690 | bool FoundUse = false, Done = false; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 691 | MachineBasicBlock::iterator E = &NewDef; |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 692 | ++I; ++E; |
| 693 | for (; !Done && I != E; ++I) { |
| 694 | MachineInstr *NMI = I; |
| 695 | for (unsigned j = 0, ee = NMI->getNumOperands(); j != ee; ++j) { |
| 696 | MachineOperand &MO = NMI->getOperand(j); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 697 | if (!MO.isReg() || MO.getReg() != Reg) |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 698 | continue; |
| 699 | if (MO.isUse()) |
| 700 | FoundUse = true; |
| 701 | Done = true; // Stop after scanning all the operands of this MI. |
| 702 | } |
| 703 | } |
| 704 | if (!FoundUse) { |
| 705 | // Def is dead! |
| 706 | DefOp->setIsDead(); |
| 707 | return true; |
| 708 | } |
| 709 | return false; |
| 710 | } |
| 711 | |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 712 | /// UpdateKills - Track and update kill info. If a MI reads a register that is |
| 713 | /// marked kill, then it must be due to register reuse. Transfer the kill info |
| 714 | /// over. |
| 715 | static void UpdateKills(MachineInstr &MI, BitVector &RegKills, |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 716 | std::vector<MachineOperand*> &KillOps, |
| 717 | const TargetRegisterInfo* TRI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 718 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 719 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 720 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 721 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 722 | continue; |
| 723 | unsigned Reg = MO.getReg(); |
| 724 | if (Reg == 0) |
| 725 | continue; |
| 726 | |
Evan Cheng | 70366b9 | 2008-03-21 19:09:30 +0000 | [diff] [blame] | 727 | if (RegKills[Reg] && KillOps[Reg]->getParent() != &MI) { |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 728 | // That can't be right. Register is killed but not re-defined and it's |
| 729 | // being reused. Let's fix that. |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 730 | KillOps[Reg]->setIsKill(false); |
Evan Cheng | 39c883c | 2007-12-11 23:36:57 +0000 | [diff] [blame] | 731 | KillOps[Reg] = NULL; |
| 732 | RegKills.reset(Reg); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 733 | if (i < TID.getNumOperands() && |
| 734 | TID.getOperandConstraint(i, TOI::TIED_TO) == -1) |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 735 | // Unless it's a two-address operand, this is the new kill. |
| 736 | MO.setIsKill(); |
| 737 | } |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 738 | if (MO.isKill()) { |
| 739 | RegKills.set(Reg); |
| 740 | KillOps[Reg] = &MO; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 745 | const MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 746 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 747 | continue; |
| 748 | unsigned Reg = MO.getReg(); |
| 749 | RegKills.reset(Reg); |
| 750 | KillOps[Reg] = NULL; |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 751 | // It also defines (or partially define) aliases. |
| 752 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) { |
| 753 | RegKills.reset(*AS); |
| 754 | KillOps[*AS] = NULL; |
| 755 | } |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 759 | /// ReMaterialize - Re-materialize definition for Reg targetting DestReg. |
| 760 | /// |
| 761 | static void ReMaterialize(MachineBasicBlock &MBB, |
| 762 | MachineBasicBlock::iterator &MII, |
| 763 | unsigned DestReg, unsigned Reg, |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 764 | const TargetInstrInfo *TII, |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 765 | const TargetRegisterInfo *TRI, |
| 766 | VirtRegMap &VRM) { |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 767 | TII->reMaterialize(MBB, MII, DestReg, VRM.getReMaterializedMI(Reg)); |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 768 | MachineInstr *NewMI = prior(MII); |
| 769 | for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { |
| 770 | MachineOperand &MO = NewMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 771 | if (!MO.isReg() || MO.getReg() == 0) |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 772 | continue; |
| 773 | unsigned VirtReg = MO.getReg(); |
| 774 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) |
| 775 | continue; |
| 776 | assert(MO.isUse()); |
| 777 | unsigned SubIdx = MO.getSubReg(); |
| 778 | unsigned Phys = VRM.getPhys(VirtReg); |
| 779 | assert(Phys); |
| 780 | unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys; |
| 781 | MO.setReg(RReg); |
| 782 | } |
| 783 | ++NumReMats; |
| 784 | } |
| 785 | |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 787 | // ReusedOp - For each reused operand, we keep track of a bit of information, in |
| 788 | // case we need to rollback upon processing a new operand. See comments below. |
| 789 | namespace { |
| 790 | struct ReusedOp { |
| 791 | // The MachineInstr operand that reused an available value. |
| 792 | unsigned Operand; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 793 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 794 | // StackSlotOrReMat - The spill slot or remat id of the value being reused. |
| 795 | unsigned StackSlotOrReMat; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 797 | // PhysRegReused - The physical register the value was available in. |
| 798 | unsigned PhysRegReused; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 800 | // AssignedPhysReg - The physreg that was assigned for use by the reload. |
| 801 | unsigned AssignedPhysReg; |
Chris Lattner | 8a61a75 | 2005-10-06 17:19:06 +0000 | [diff] [blame] | 802 | |
| 803 | // VirtReg - The virtual register itself. |
| 804 | unsigned VirtReg; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 8a61a75 | 2005-10-06 17:19:06 +0000 | [diff] [blame] | 806 | ReusedOp(unsigned o, unsigned ss, unsigned prr, unsigned apr, |
| 807 | unsigned vreg) |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 808 | : Operand(o), StackSlotOrReMat(ss), PhysRegReused(prr), |
| 809 | AssignedPhysReg(apr), VirtReg(vreg) {} |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 810 | }; |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 811 | |
| 812 | /// ReuseInfo - This maintains a collection of ReuseOp's for each operand that |
| 813 | /// is reused instead of reloaded. |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 814 | class VISIBILITY_HIDDEN ReuseInfo { |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 815 | MachineInstr &MI; |
| 816 | std::vector<ReusedOp> Reuses; |
Evan Cheng | 957840b | 2007-02-21 02:22:03 +0000 | [diff] [blame] | 817 | BitVector PhysRegsClobbered; |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 818 | public: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 819 | ReuseInfo(MachineInstr &mi, const TargetRegisterInfo *tri) : MI(mi) { |
| 820 | PhysRegsClobbered.resize(tri->getNumRegs()); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 821 | } |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 822 | |
| 823 | bool hasReuses() const { |
| 824 | return !Reuses.empty(); |
| 825 | } |
| 826 | |
| 827 | /// addReuse - If we choose to reuse a virtual register that is already |
| 828 | /// available instead of reloading it, remember that we did so. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 829 | void addReuse(unsigned OpNo, unsigned StackSlotOrReMat, |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 830 | unsigned PhysRegReused, unsigned AssignedPhysReg, |
| 831 | unsigned VirtReg) { |
| 832 | // If the reload is to the assigned register anyway, no undo will be |
| 833 | // required. |
| 834 | if (PhysRegReused == AssignedPhysReg) return; |
| 835 | |
| 836 | // Otherwise, remember this. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 837 | Reuses.push_back(ReusedOp(OpNo, StackSlotOrReMat, PhysRegReused, |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 838 | AssignedPhysReg, VirtReg)); |
| 839 | } |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 840 | |
| 841 | void markClobbered(unsigned PhysReg) { |
Evan Cheng | 957840b | 2007-02-21 02:22:03 +0000 | [diff] [blame] | 842 | PhysRegsClobbered.set(PhysReg); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | bool isClobbered(unsigned PhysReg) const { |
Evan Cheng | 957840b | 2007-02-21 02:22:03 +0000 | [diff] [blame] | 846 | return PhysRegsClobbered.test(PhysReg); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 847 | } |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 848 | |
| 849 | /// GetRegForReload - We are about to emit a reload into PhysReg. If there |
| 850 | /// is some other operand that is using the specified register, either pick |
| 851 | /// a new register to use, or evict the previous reload and use this reg. |
| 852 | unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI, |
| 853 | AvailableSpills &Spills, |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 854 | std::vector<MachineInstr*> &MaybeDeadStores, |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 855 | SmallSet<unsigned, 8> &Rejected, |
| 856 | BitVector &RegKills, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 857 | std::vector<MachineOperand*> &KillOps, |
| 858 | VirtRegMap &VRM) { |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 859 | const TargetInstrInfo* TII = MI->getParent()->getParent()->getTarget() |
| 860 | .getInstrInfo(); |
| 861 | |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 862 | if (Reuses.empty()) return PhysReg; // This is most often empty. |
| 863 | |
| 864 | for (unsigned ro = 0, e = Reuses.size(); ro != e; ++ro) { |
| 865 | ReusedOp &Op = Reuses[ro]; |
| 866 | // If we find some other reuse that was supposed to use this register |
| 867 | // 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] | 868 | // register. That is, unless its reload register has already been |
| 869 | // considered and subsequently rejected because it has also been reused |
| 870 | // by another operand. |
| 871 | if (Op.PhysRegReused == PhysReg && |
| 872 | Rejected.count(Op.AssignedPhysReg) == 0) { |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 873 | // Yup, use the reload register that we didn't use before. |
Evan Cheng | 3c82cab | 2007-01-19 22:40:14 +0000 | [diff] [blame] | 874 | unsigned NewReg = Op.AssignedPhysReg; |
| 875 | Rejected.insert(PhysReg); |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 876 | return GetRegForReload(NewReg, MI, Spills, MaybeDeadStores, Rejected, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 877 | RegKills, KillOps, VRM); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 878 | } else { |
| 879 | // Otherwise, we might also have a problem if a previously reused |
| 880 | // value aliases the new register. If so, codegen the previous reload |
| 881 | // and use this one. |
| 882 | unsigned PRRU = Op.PhysRegReused; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 883 | const TargetRegisterInfo *TRI = Spills.getRegInfo(); |
| 884 | if (TRI->areAliases(PRRU, PhysReg)) { |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 885 | // Okay, we found out that an alias of a reused register |
| 886 | // was used. This isn't good because it means we have |
| 887 | // to undo a previous reuse. |
| 888 | MachineBasicBlock *MBB = MI->getParent(); |
| 889 | const TargetRegisterClass *AliasRC = |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 890 | MBB->getParent()->getRegInfo().getRegClass(Op.VirtReg); |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 891 | |
| 892 | // Copy Op out of the vector and remove it, we're going to insert an |
| 893 | // explicit load for it. |
| 894 | ReusedOp NewOp = Op; |
| 895 | Reuses.erase(Reuses.begin()+ro); |
| 896 | |
| 897 | // Ok, we're going to try to reload the assigned physreg into the |
| 898 | // slot that we were supposed to in the first place. However, that |
| 899 | // register could hold a reuse. Check to see if it conflicts or |
| 900 | // would prefer us to use a different register. |
| 901 | unsigned NewPhysReg = GetRegForReload(NewOp.AssignedPhysReg, |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 902 | MI, Spills, MaybeDeadStores, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 903 | Rejected, RegKills, KillOps, VRM); |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 904 | |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 905 | MachineBasicBlock::iterator MII = MI; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 906 | if (NewOp.StackSlotOrReMat > VirtRegMap::MAX_STACK_SLOT) { |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 907 | ReMaterialize(*MBB, MII, NewPhysReg, NewOp.VirtReg, TII, TRI,VRM); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 908 | } else { |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 909 | TII->loadRegFromStackSlot(*MBB, MII, NewPhysReg, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 910 | NewOp.StackSlotOrReMat, AliasRC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 911 | MachineInstr *LoadMI = prior(MII); |
| 912 | VRM.addSpillSlotUse(NewOp.StackSlotOrReMat, LoadMI); |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 913 | // Any stores to this stack slot are not dead anymore. |
| 914 | MaybeDeadStores[NewOp.StackSlotOrReMat] = NULL; |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 915 | ++NumLoads; |
| 916 | } |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 917 | Spills.ClobberPhysReg(NewPhysReg); |
| 918 | Spills.ClobberPhysReg(NewOp.PhysRegReused); |
Evan Cheng | 014264b | 2008-09-10 20:08:45 +0000 | [diff] [blame] | 919 | |
| 920 | unsigned SubIdx = MI->getOperand(NewOp.Operand).getSubReg(); |
| 921 | unsigned RReg = SubIdx ? TRI->getSubReg(NewPhysReg, SubIdx) : NewPhysReg; |
| 922 | MI->getOperand(NewOp.Operand).setReg(RReg); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 923 | |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 924 | Spills.addAvailable(NewOp.StackSlotOrReMat, NewPhysReg); |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 925 | --MII; |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 926 | UpdateKills(*MII, RegKills, KillOps, TRI); |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 927 | DOUT << '\t' << *MII; |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 928 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 929 | DOUT << "Reuse undone!\n"; |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 930 | --NumReused; |
Chris Lattner | 28bad08 | 2006-02-25 02:17:31 +0000 | [diff] [blame] | 931 | |
| 932 | // Finally, PhysReg is now available, go ahead and use it. |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 933 | return PhysReg; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | return PhysReg; |
| 938 | } |
Evan Cheng | 3c82cab | 2007-01-19 22:40:14 +0000 | [diff] [blame] | 939 | |
| 940 | /// GetRegForReload - Helper for the above GetRegForReload(). Add a |
| 941 | /// 'Rejected' set to remember which registers have been considered and |
| 942 | /// rejected for the reload. This avoids infinite looping in case like |
| 943 | /// this: |
| 944 | /// t1 := op t2, t3 |
| 945 | /// t2 <- assigned r0 for use by the reload but ended up reuse r1 |
| 946 | /// t3 <- assigned r1 for use by the reload but ended up reuse r0 |
| 947 | /// t1 <- desires r1 |
| 948 | /// sees r1 is taken by t2, tries t2's reload register r0 |
| 949 | /// sees r0 is taken by t3, tries t3's reload register r1 |
| 950 | /// sees r1 is taken by t2, tries t2's reload register r0 ... |
| 951 | unsigned GetRegForReload(unsigned PhysReg, MachineInstr *MI, |
| 952 | AvailableSpills &Spills, |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 953 | std::vector<MachineInstr*> &MaybeDeadStores, |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 954 | BitVector &RegKills, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 955 | std::vector<MachineOperand*> &KillOps, |
| 956 | VirtRegMap &VRM) { |
Chris Lattner | 08a4d5a | 2007-01-23 00:59:48 +0000 | [diff] [blame] | 957 | SmallSet<unsigned, 8> Rejected; |
Evan Cheng | 28bb462 | 2007-07-11 19:17:18 +0000 | [diff] [blame] | 958 | return GetRegForReload(PhysReg, MI, Spills, MaybeDeadStores, Rejected, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 959 | RegKills, KillOps, VRM); |
Evan Cheng | 3c82cab | 2007-01-19 22:40:14 +0000 | [diff] [blame] | 960 | } |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 961 | }; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 962 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 963 | |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 964 | /// PrepForUnfoldOpti - Turn a store folding instruction into a load folding |
| 965 | /// instruction. e.g. |
| 966 | /// xorl %edi, %eax |
| 967 | /// movl %eax, -32(%ebp) |
| 968 | /// movl -36(%ebp), %eax |
Bill Wendling | f059deb | 2008-02-26 10:51:52 +0000 | [diff] [blame] | 969 | /// orl %eax, -32(%ebp) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 970 | /// ==> |
| 971 | /// xorl %edi, %eax |
| 972 | /// orl -36(%ebp), %eax |
| 973 | /// mov %eax, -32(%ebp) |
| 974 | /// This enables unfolding optimization for a subsequent instruction which will |
| 975 | /// also eliminate the newly introduced store instruction. |
| 976 | bool LocalSpiller::PrepForUnfoldOpti(MachineBasicBlock &MBB, |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 977 | MachineBasicBlock::iterator &MII, |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 978 | std::vector<MachineInstr*> &MaybeDeadStores, |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 979 | AvailableSpills &Spills, |
| 980 | BitVector &RegKills, |
| 981 | std::vector<MachineOperand*> &KillOps, |
| 982 | VirtRegMap &VRM) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 983 | MachineFunction &MF = *MBB.getParent(); |
| 984 | MachineInstr &MI = *MII; |
| 985 | unsigned UnfoldedOpc = 0; |
| 986 | unsigned UnfoldPR = 0; |
| 987 | unsigned UnfoldVR = 0; |
| 988 | int FoldedSS = VirtRegMap::NO_STACK_SLOT; |
| 989 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
Evan Cheng | c17ba8a | 2008-03-14 20:44:01 +0000 | [diff] [blame] | 990 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 991 | // Only transform a MI that folds a single register. |
| 992 | if (UnfoldedOpc) |
| 993 | return false; |
| 994 | UnfoldVR = I->second.first; |
| 995 | VirtRegMap::ModRef MR = I->second.second; |
Evan Cheng | c17ba8a | 2008-03-14 20:44:01 +0000 | [diff] [blame] | 996 | // MI2VirtMap be can updated which invalidate the iterator. |
| 997 | // Increment the iterator first. |
| 998 | ++I; |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 999 | if (VRM.isAssignedReg(UnfoldVR)) |
| 1000 | continue; |
| 1001 | // If this reference is not a use, any previous store is now dead. |
| 1002 | // Otherwise, the store to this stack slot is not dead anymore. |
| 1003 | FoldedSS = VRM.getStackSlot(UnfoldVR); |
| 1004 | MachineInstr* DeadStore = MaybeDeadStores[FoldedSS]; |
| 1005 | if (DeadStore && (MR & VirtRegMap::isModRef)) { |
| 1006 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(FoldedSS); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1007 | if (!PhysReg || !DeadStore->readsRegister(PhysReg)) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1008 | continue; |
| 1009 | UnfoldPR = PhysReg; |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 1010 | UnfoldedOpc = TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1011 | false, true); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (!UnfoldedOpc) |
| 1016 | return false; |
| 1017 | |
| 1018 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1019 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1020 | if (!MO.isReg() || MO.getReg() == 0 || !MO.isUse()) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1021 | continue; |
| 1022 | unsigned VirtReg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1023 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg) || MO.getSubReg()) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1024 | continue; |
| 1025 | if (VRM.isAssignedReg(VirtReg)) { |
| 1026 | unsigned PhysReg = VRM.getPhys(VirtReg); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1027 | if (PhysReg && TRI->regsOverlap(PhysReg, UnfoldPR)) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | } else if (VRM.isReMaterialized(VirtReg)) |
| 1030 | continue; |
| 1031 | int SS = VRM.getStackSlot(VirtReg); |
| 1032 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 1033 | if (PhysReg) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1034 | if (TRI->regsOverlap(PhysReg, UnfoldPR)) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1035 | return false; |
| 1036 | continue; |
| 1037 | } |
Evan Cheng | e3b8a48 | 2008-08-05 21:51:46 +0000 | [diff] [blame] | 1038 | if (VRM.hasPhys(VirtReg)) { |
| 1039 | PhysReg = VRM.getPhys(VirtReg); |
| 1040 | if (!TRI->regsOverlap(PhysReg, UnfoldPR)) |
| 1041 | continue; |
| 1042 | } |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Ok, we'll need to reload the value into a register which makes |
| 1045 | // it impossible to perform the store unfolding optimization later. |
| 1046 | // Let's see if it is possible to fold the load if the store is |
| 1047 | // unfolded. This allows us to perform the store unfolding |
| 1048 | // optimization. |
| 1049 | SmallVector<MachineInstr*, 4> NewMIs; |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 1050 | if (TII->unfoldMemoryOperand(MF, &MI, UnfoldVR, false, false, NewMIs)) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1051 | assert(NewMIs.size() == 1); |
| 1052 | MachineInstr *NewMI = NewMIs.back(); |
| 1053 | NewMIs.clear(); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 1054 | int Idx = NewMI->findRegisterUseOperandIdx(VirtReg, false); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1055 | assert(Idx != -1); |
Evan Cheng | aee4af6 | 2007-12-02 08:30:39 +0000 | [diff] [blame] | 1056 | SmallVector<unsigned, 2> Ops; |
| 1057 | Ops.push_back(Idx); |
Evan Cheng | f2f8c2a | 2008-02-08 22:05:27 +0000 | [diff] [blame] | 1058 | MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, NewMI, Ops, SS); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1059 | if (FoldedMI) { |
Evan Cheng | 21b3f31 | 2008-02-27 19:57:11 +0000 | [diff] [blame] | 1060 | VRM.addSpillSlotUse(SS, FoldedMI); |
Evan Cheng | cbfb9b2 | 2007-10-22 03:01:44 +0000 | [diff] [blame] | 1061 | if (!VRM.hasPhys(UnfoldVR)) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1062 | VRM.assignVirt2Phys(UnfoldVR, UnfoldPR); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1063 | VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); |
| 1064 | MII = MBB.insert(MII, FoldedMI); |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1065 | InvalidateKills(MI, RegKills, KillOps); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1066 | VRM.RemoveMachineInstrFromMaps(&MI); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1067 | MBB.erase(&MI); |
Dan Gohman | fa82857 | 2008-07-18 18:28:56 +0000 | [diff] [blame] | 1068 | MF.DeleteMachineInstr(NewMI); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1071 | MF.DeleteMachineInstr(NewMI); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | return false; |
| 1075 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1076 | |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1077 | /// CommuteToFoldReload - |
| 1078 | /// Look for |
| 1079 | /// r1 = load fi#1 |
| 1080 | /// r1 = op r1, r2<kill> |
| 1081 | /// store r1, fi#1 |
| 1082 | /// |
| 1083 | /// If op is commutable and r2 is killed, then we can xform these to |
| 1084 | /// r2 = op r2, fi#1 |
| 1085 | /// store r2, fi#1 |
| 1086 | bool LocalSpiller::CommuteToFoldReload(MachineBasicBlock &MBB, |
| 1087 | MachineBasicBlock::iterator &MII, |
| 1088 | unsigned VirtReg, unsigned SrcReg, int SS, |
| 1089 | BitVector &RegKills, |
| 1090 | std::vector<MachineOperand*> &KillOps, |
| 1091 | const TargetRegisterInfo *TRI, |
| 1092 | VirtRegMap &VRM) { |
| 1093 | if (MII == MBB.begin() || !MII->killsRegister(SrcReg)) |
| 1094 | return false; |
| 1095 | |
| 1096 | MachineFunction &MF = *MBB.getParent(); |
| 1097 | MachineInstr &MI = *MII; |
| 1098 | MachineBasicBlock::iterator DefMII = prior(MII); |
| 1099 | MachineInstr *DefMI = DefMII; |
| 1100 | const TargetInstrDesc &TID = DefMI->getDesc(); |
| 1101 | unsigned NewDstIdx; |
| 1102 | if (DefMII != MBB.begin() && |
| 1103 | TID.isCommutable() && |
| 1104 | TII->CommuteChangesDestination(DefMI, NewDstIdx)) { |
| 1105 | MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx); |
| 1106 | unsigned NewReg = NewDstMO.getReg(); |
| 1107 | if (!NewDstMO.isKill() || TRI->regsOverlap(NewReg, SrcReg)) |
| 1108 | return false; |
| 1109 | MachineInstr *ReloadMI = prior(DefMII); |
| 1110 | int FrameIdx; |
| 1111 | unsigned DestReg = TII->isLoadFromStackSlot(ReloadMI, FrameIdx); |
| 1112 | if (DestReg != SrcReg || FrameIdx != SS) |
| 1113 | return false; |
| 1114 | int UseIdx = DefMI->findRegisterUseOperandIdx(DestReg, false); |
| 1115 | if (UseIdx == -1) |
| 1116 | return false; |
| 1117 | int DefIdx = TID.getOperandConstraint(UseIdx, TOI::TIED_TO); |
| 1118 | if (DefIdx == -1) |
| 1119 | return false; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1120 | assert(DefMI->getOperand(DefIdx).isReg() && |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1121 | DefMI->getOperand(DefIdx).getReg() == SrcReg); |
| 1122 | |
| 1123 | // Now commute def instruction. |
Evan Cheng | 7a15391 | 2008-06-16 07:34:17 +0000 | [diff] [blame] | 1124 | MachineInstr *CommutedMI = TII->commuteInstruction(DefMI, true); |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1125 | if (!CommutedMI) |
| 1126 | return false; |
| 1127 | SmallVector<unsigned, 2> Ops; |
| 1128 | Ops.push_back(NewDstIdx); |
| 1129 | MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1130 | // Not needed since foldMemoryOperand returns new MI. |
| 1131 | MF.DeleteMachineInstr(CommutedMI); |
Evan Cheng | 7a15391 | 2008-06-16 07:34:17 +0000 | [diff] [blame] | 1132 | if (!FoldedMI) |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1133 | return false; |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1134 | |
| 1135 | VRM.addSpillSlotUse(SS, FoldedMI); |
| 1136 | VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef); |
| 1137 | // Insert new def MI and spill MI. |
| 1138 | const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1139 | TII->storeRegToStackSlot(MBB, &MI, NewReg, true, SS, RC); |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1140 | MII = prior(MII); |
| 1141 | MachineInstr *StoreMI = MII; |
| 1142 | VRM.addSpillSlotUse(SS, StoreMI); |
| 1143 | VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); |
| 1144 | MII = MBB.insert(MII, FoldedMI); // Update MII to backtrack. |
| 1145 | |
| 1146 | // Delete all 3 old instructions. |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1147 | InvalidateKills(*ReloadMI, RegKills, KillOps); |
| 1148 | VRM.RemoveMachineInstrFromMaps(ReloadMI); |
| 1149 | MBB.erase(ReloadMI); |
Evan Cheng | 7a15391 | 2008-06-16 07:34:17 +0000 | [diff] [blame] | 1150 | InvalidateKills(*DefMI, RegKills, KillOps); |
| 1151 | VRM.RemoveMachineInstrFromMaps(DefMI); |
| 1152 | MBB.erase(DefMI); |
| 1153 | InvalidateKills(MI, RegKills, KillOps); |
| 1154 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 1155 | MBB.erase(&MI); |
| 1156 | |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1157 | ++NumCommutes; |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1164 | /// findSuperReg - Find the SubReg's super-register of given register class |
| 1165 | /// where its SubIdx sub-register is SubReg. |
| 1166 | static unsigned findSuperReg(const TargetRegisterClass *RC, unsigned SubReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1167 | unsigned SubIdx, const TargetRegisterInfo *TRI) { |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1168 | for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); |
| 1169 | I != E; ++I) { |
| 1170 | unsigned Reg = *I; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1171 | if (TRI->getSubReg(Reg, SubIdx) == SubReg) |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1172 | return Reg; |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1177 | /// SpillRegToStackSlot - Spill a register to a specified stack slot. Check if |
| 1178 | /// the last store to the same slot is now dead. If so, remove the last store. |
| 1179 | void LocalSpiller::SpillRegToStackSlot(MachineBasicBlock &MBB, |
| 1180 | MachineBasicBlock::iterator &MII, |
| 1181 | int Idx, unsigned PhysReg, int StackSlot, |
| 1182 | const TargetRegisterClass *RC, |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 1183 | bool isAvailable, MachineInstr *&LastStore, |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1184 | AvailableSpills &Spills, |
| 1185 | SmallSet<MachineInstr*, 4> &ReMatDefs, |
| 1186 | BitVector &RegKills, |
| 1187 | std::vector<MachineOperand*> &KillOps, |
Evan Cheng | e4b3900 | 2007-12-03 21:31:55 +0000 | [diff] [blame] | 1188 | VirtRegMap &VRM) { |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1189 | TII->storeRegToStackSlot(MBB, next(MII), PhysReg, true, StackSlot, RC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1190 | MachineInstr *StoreMI = next(MII); |
| 1191 | VRM.addSpillSlotUse(StackSlot, StoreMI); |
| 1192 | DOUT << "Store:\t" << *StoreMI; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1193 | |
| 1194 | // If there is a dead store to this stack slot, nuke it now. |
| 1195 | if (LastStore) { |
| 1196 | DOUT << "Removed dead store:\t" << *LastStore; |
| 1197 | ++NumDSE; |
| 1198 | SmallVector<unsigned, 2> KillRegs; |
| 1199 | InvalidateKills(*LastStore, RegKills, KillOps, &KillRegs); |
| 1200 | MachineBasicBlock::iterator PrevMII = LastStore; |
| 1201 | bool CheckDef = PrevMII != MBB.begin(); |
| 1202 | if (CheckDef) |
| 1203 | --PrevMII; |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1204 | VRM.RemoveMachineInstrFromMaps(LastStore); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1205 | MBB.erase(LastStore); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1206 | if (CheckDef) { |
| 1207 | // Look at defs of killed registers on the store. Mark the defs |
| 1208 | // as dead since the store has been deleted and they aren't |
| 1209 | // being reused. |
| 1210 | for (unsigned j = 0, ee = KillRegs.size(); j != ee; ++j) { |
| 1211 | bool HasOtherDef = false; |
| 1212 | if (InvalidateRegDef(PrevMII, *MII, KillRegs[j], HasOtherDef)) { |
| 1213 | MachineInstr *DeadDef = PrevMII; |
| 1214 | if (ReMatDefs.count(DeadDef) && !HasOtherDef) { |
| 1215 | // FIXME: This assumes a remat def does not have side |
| 1216 | // effects. |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1217 | VRM.RemoveMachineInstrFromMaps(DeadDef); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1218 | MBB.erase(DeadDef); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1219 | ++NumDRM; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
Evan Cheng | e4b3900 | 2007-12-03 21:31:55 +0000 | [diff] [blame] | 1226 | LastStore = next(MII); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1227 | |
| 1228 | // If the stack slot value was previously available in some other |
| 1229 | // register, change it now. Otherwise, make the register available, |
| 1230 | // in PhysReg. |
| 1231 | Spills.ModifyStackSlotOrReMat(StackSlot); |
| 1232 | Spills.ClobberPhysReg(PhysReg); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1233 | Spills.addAvailable(StackSlot, PhysReg, isAvailable); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1234 | ++NumStores; |
| 1235 | } |
| 1236 | |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1237 | /// TransferDeadness - A identity copy definition is dead and it's being |
| 1238 | /// removed. Find the last def or use and mark it as dead / kill. |
| 1239 | void LocalSpiller::TransferDeadness(MachineBasicBlock *MBB, unsigned CurDist, |
| 1240 | unsigned Reg, BitVector &RegKills, |
| 1241 | std::vector<MachineOperand*> &KillOps) { |
| 1242 | int LastUDDist = -1; |
| 1243 | MachineInstr *LastUDMI = NULL; |
| 1244 | for (MachineRegisterInfo::reg_iterator RI = RegInfo->reg_begin(Reg), |
| 1245 | RE = RegInfo->reg_end(); RI != RE; ++RI) { |
| 1246 | MachineInstr *UDMI = &*RI; |
| 1247 | if (UDMI->getParent() != MBB) |
| 1248 | continue; |
| 1249 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UDMI); |
| 1250 | if (DI == DistanceMap.end() || DI->second > CurDist) |
| 1251 | continue; |
| 1252 | if ((int)DI->second < LastUDDist) |
| 1253 | continue; |
| 1254 | LastUDDist = DI->second; |
| 1255 | LastUDMI = UDMI; |
| 1256 | } |
| 1257 | |
| 1258 | if (LastUDMI) { |
| 1259 | const TargetInstrDesc &TID = LastUDMI->getDesc(); |
| 1260 | MachineOperand *LastUD = NULL; |
| 1261 | for (unsigned i = 0, e = LastUDMI->getNumOperands(); i != e; ++i) { |
| 1262 | MachineOperand &MO = LastUDMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1263 | if (!MO.isReg() || MO.getReg() != Reg) |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1264 | continue; |
| 1265 | if (!LastUD || (LastUD->isUse() && MO.isDef())) |
| 1266 | LastUD = &MO; |
| 1267 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) |
| 1268 | return; |
| 1269 | } |
| 1270 | if (LastUD->isDef()) |
| 1271 | LastUD->setIsDead(); |
| 1272 | else { |
| 1273 | LastUD->setIsKill(); |
| 1274 | RegKills.set(Reg); |
| 1275 | KillOps[Reg] = LastUD; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1280 | /// rewriteMBB - Keep track of which spills are available even after the |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1281 | /// register allocator is done with them. If possible, avid reloading vregs. |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1282 | void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM, |
| 1283 | AvailableSpills &Spills) { |
| 1284 | DOUT << "\n**** Local spiller rewriting MBB '" |
| 1285 | << MBB.getBasicBlock()->getName() << ":\n"; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1286 | |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1287 | MachineFunction &MF = *MBB.getParent(); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 52b25db | 2004-10-01 19:47:12 +0000 | [diff] [blame] | 1289 | // MaybeDeadStores - When we need to write a value back into a stack slot, |
| 1290 | // keep track of the inserted store. If the stack slot value is never read |
| 1291 | // (because the value was used from some available register, for example), and |
| 1292 | // subsequently stored to, the original store is dead. This map keeps track |
| 1293 | // of inserted stores that are not used. If we see a subsequent store to the |
| 1294 | // same stack slot, the original store is deleted. |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1295 | std::vector<MachineInstr*> MaybeDeadStores; |
| 1296 | MaybeDeadStores.resize(MF.getFrameInfo()->getObjectIndexEnd(), NULL); |
Chris Lattner | 52b25db | 2004-10-01 19:47:12 +0000 | [diff] [blame] | 1297 | |
Evan Cheng | b6ca4b3 | 2007-08-14 23:25:37 +0000 | [diff] [blame] | 1298 | // ReMatDefs - These are rematerializable def MIs which are not deleted. |
| 1299 | SmallSet<MachineInstr*, 4> ReMatDefs; |
| 1300 | |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1301 | // Keep track of kill information. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1302 | BitVector RegKills(TRI->getNumRegs()); |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1303 | std::vector<MachineOperand*> KillOps; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1304 | KillOps.resize(TRI->getNumRegs(), NULL); |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1305 | |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1306 | unsigned Dist = 0; |
| 1307 | DistanceMap.clear(); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1308 | for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end(); |
| 1309 | MII != E; ) { |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1310 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1311 | |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1312 | VirtRegMap::MI2VirtMapTy::const_iterator I, End; |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1313 | bool Erased = false; |
| 1314 | bool BackTracked = false; |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1315 | if (PrepForUnfoldOpti(MBB, MII, |
| 1316 | MaybeDeadStores, Spills, RegKills, KillOps, VRM)) |
| 1317 | NextMII = next(MII); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1318 | |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1319 | MachineInstr &MI = *MII; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1320 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1321 | |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1322 | if (VRM.hasEmergencySpills(&MI)) { |
| 1323 | // Spill physical register(s) in the rare case the allocator has run out |
| 1324 | // of registers to allocate. |
| 1325 | SmallSet<int, 4> UsedSS; |
| 1326 | std::vector<unsigned> &EmSpills = VRM.getEmergencySpills(&MI); |
| 1327 | for (unsigned i = 0, e = EmSpills.size(); i != e; ++i) { |
| 1328 | unsigned PhysReg = EmSpills[i]; |
| 1329 | const TargetRegisterClass *RC = |
| 1330 | TRI->getPhysicalRegisterRegClass(PhysReg); |
| 1331 | assert(RC && "Unable to determine register class!"); |
| 1332 | int SS = VRM.getEmergencySpillSlot(RC); |
| 1333 | if (UsedSS.count(SS)) |
| 1334 | assert(0 && "Need to spill more than one physical registers!"); |
| 1335 | UsedSS.insert(SS); |
| 1336 | TII->storeRegToStackSlot(MBB, MII, PhysReg, true, SS, RC); |
| 1337 | MachineInstr *StoreMI = prior(MII); |
| 1338 | VRM.addSpillSlotUse(SS, StoreMI); |
| 1339 | TII->loadRegFromStackSlot(MBB, next(MII), PhysReg, SS, RC); |
| 1340 | MachineInstr *LoadMI = next(MII); |
| 1341 | VRM.addSpillSlotUse(SS, LoadMI); |
Evan Cheng | c1f53c7 | 2008-03-11 21:34:46 +0000 | [diff] [blame] | 1342 | ++NumPSpills; |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1343 | } |
Evan Cheng | 17d5f54 | 2008-03-12 00:14:07 +0000 | [diff] [blame] | 1344 | NextMII = next(MII); |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1347 | // Insert restores here if asked to. |
| 1348 | if (VRM.isRestorePt(&MI)) { |
| 1349 | std::vector<unsigned> &RestoreRegs = VRM.getRestorePtRestores(&MI); |
| 1350 | for (unsigned i = 0, e = RestoreRegs.size(); i != e; ++i) { |
Evan Cheng | d70dbb5 | 2008-02-22 09:24:50 +0000 | [diff] [blame] | 1351 | unsigned VirtReg = RestoreRegs[e-i-1]; // Reverse order. |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1352 | if (!VRM.getPreSplitReg(VirtReg)) |
| 1353 | continue; // Split interval spilled again. |
| 1354 | unsigned Phys = VRM.getPhys(VirtReg); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1355 | RegInfo->setPhysRegUsed(Phys); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Check if the value being restored if available. If so, it must be |
| 1358 | // from a predecessor BB that fallthrough into this BB. We do not |
| 1359 | // expect: |
| 1360 | // BB1: |
| 1361 | // r1 = load fi#1 |
| 1362 | // ... |
| 1363 | // = r1<kill> |
| 1364 | // ... # r1 not clobbered |
| 1365 | // ... |
| 1366 | // = load fi#1 |
| 1367 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 1368 | int SSorRMId = DoReMat |
| 1369 | ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg); |
Evan Cheng | 8679119 | 2009-02-12 10:32:17 +0000 | [diff] [blame] | 1370 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1371 | // FIXME: A temporary workaround. Don't reuse available value if it's |
| 1372 | // not safe to move the def of the virtual register's class. e.g. |
| 1373 | // X86::RFP* register classes. |
| 1374 | unsigned InReg = TII->isSafeToMoveRegClassDefs(RC) ? |
| 1375 | Spills.getSpillSlotOrReMatPhysReg(SSorRMId) : 0; |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1376 | if (InReg == Phys) { |
| 1377 | // If the value is already available in the expected register, save |
| 1378 | // a reload / remat. |
| 1379 | if (SSorRMId) |
| 1380 | DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1; |
| 1381 | else |
| 1382 | DOUT << "Reusing SS#" << SSorRMId; |
| 1383 | DOUT << " from physreg " |
| 1384 | << TRI->getName(InReg) << " for vreg" |
| 1385 | << VirtReg <<" instead of reloading into physreg " |
| 1386 | << TRI->getName(Phys) << "\n"; |
| 1387 | ++NumOmitted; |
| 1388 | continue; |
| 1389 | } else if (InReg && InReg != Phys) { |
| 1390 | if (SSorRMId) |
| 1391 | DOUT << "Reusing RM#" << SSorRMId-VirtRegMap::MAX_STACK_SLOT-1; |
| 1392 | else |
| 1393 | DOUT << "Reusing SS#" << SSorRMId; |
| 1394 | DOUT << " from physreg " |
| 1395 | << TRI->getName(InReg) << " for vreg" |
| 1396 | << VirtReg <<" by copying it into physreg " |
| 1397 | << TRI->getName(Phys) << "\n"; |
| 1398 | |
| 1399 | // If the reloaded / remat value is available in another register, |
| 1400 | // copy it to the desired register. |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1401 | TII->copyRegToReg(MBB, &MI, Phys, InReg, RC, RC); |
| 1402 | |
| 1403 | // This invalidates Phys. |
| 1404 | Spills.ClobberPhysReg(Phys); |
| 1405 | // Remember it's available. |
| 1406 | Spills.addAvailable(SSorRMId, Phys); |
| 1407 | |
| 1408 | // Mark is killed. |
| 1409 | MachineInstr *CopyMI = prior(MII); |
| 1410 | MachineOperand *KillOpnd = CopyMI->findRegisterUseOperand(InReg); |
| 1411 | KillOpnd->setIsKill(); |
| 1412 | UpdateKills(*CopyMI, RegKills, KillOps, TRI); |
| 1413 | |
| 1414 | DOUT << '\t' << *CopyMI; |
| 1415 | ++NumCopified; |
| 1416 | continue; |
| 1417 | } |
| 1418 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1419 | if (VRM.isReMaterialized(VirtReg)) { |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 1420 | ReMaterialize(MBB, MII, Phys, VirtReg, TII, TRI, VRM); |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1421 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1422 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1423 | TII->loadRegFromStackSlot(MBB, &MI, Phys, SSorRMId, RC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1424 | MachineInstr *LoadMI = prior(MII); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1425 | VRM.addSpillSlotUse(SSorRMId, LoadMI); |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1426 | ++NumLoads; |
| 1427 | } |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1428 | |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1429 | // This invalidates Phys. |
| 1430 | Spills.ClobberPhysReg(Phys); |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1431 | // Remember it's available. |
| 1432 | Spills.addAvailable(SSorRMId, Phys); |
| 1433 | |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 1434 | UpdateKills(*prior(MII), RegKills, KillOps, TRI); |
Evan Cheng | 0cbb116 | 2007-11-29 01:06:25 +0000 | [diff] [blame] | 1435 | DOUT << '\t' << *prior(MII); |
| 1436 | } |
| 1437 | } |
| 1438 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1439 | // Insert spills here if asked to. |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1440 | if (VRM.isSpillPt(&MI)) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 1441 | std::vector<std::pair<unsigned,bool> > &SpillRegs = |
| 1442 | VRM.getSpillPtSpills(&MI); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1443 | for (unsigned i = 0, e = SpillRegs.size(); i != e; ++i) { |
Evan Cheng | b50bb8c | 2007-12-05 08:16:32 +0000 | [diff] [blame] | 1444 | unsigned VirtReg = SpillRegs[i].first; |
| 1445 | bool isKill = SpillRegs[i].second; |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1446 | if (!VRM.getPreSplitReg(VirtReg)) |
| 1447 | continue; // Split interval spilled again. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1448 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1449 | unsigned Phys = VRM.getPhys(VirtReg); |
| 1450 | int StackSlot = VRM.getStackSlot(VirtReg); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1451 | TII->storeRegToStackSlot(MBB, next(MII), Phys, isKill, StackSlot, RC); |
Evan Cheng | d64b5c8 | 2007-12-05 03:14:33 +0000 | [diff] [blame] | 1452 | MachineInstr *StoreMI = next(MII); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1453 | VRM.addSpillSlotUse(StackSlot, StoreMI); |
Evan Cheng | 4191b96 | 2008-03-12 00:02:46 +0000 | [diff] [blame] | 1454 | DOUT << "Store:\t" << *StoreMI; |
Evan Cheng | d64b5c8 | 2007-12-05 03:14:33 +0000 | [diff] [blame] | 1455 | VRM.virtFolded(VirtReg, StoreMI, VirtRegMap::isMod); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1456 | } |
Evan Cheng | e4b3900 | 2007-12-03 21:31:55 +0000 | [diff] [blame] | 1457 | NextMII = next(MII); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /// ReusedOperands - Keep track of operand reuse in case we need to undo |
| 1461 | /// reuse. |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1462 | ReuseInfo ReusedOperands(MI, TRI); |
Evan Cheng | b2fd65f | 2008-02-22 19:22:06 +0000 | [diff] [blame] | 1463 | SmallVector<unsigned, 4> VirtUseOps; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1464 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1465 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1466 | if (!MO.isReg() || MO.getReg() == 0) |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1467 | continue; // Ignore non-register operands. |
| 1468 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1469 | unsigned VirtReg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1470 | if (TargetRegisterInfo::isPhysicalRegister(VirtReg)) { |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1471 | // Ignore physregs for spilling, but remember that it is used by this |
| 1472 | // function. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1473 | RegInfo->setPhysRegUsed(VirtReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1474 | continue; |
| 1475 | } |
Evan Cheng | b2fd65f | 2008-02-22 19:22:06 +0000 | [diff] [blame] | 1476 | |
| 1477 | // We want to process implicit virtual register uses first. |
| 1478 | if (MO.isImplicit()) |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 1479 | // If the virtual register is implicitly defined, emit a implicit_def |
| 1480 | // before so scavenger knows it's "defined". |
Evan Cheng | b2fd65f | 2008-02-22 19:22:06 +0000 | [diff] [blame] | 1481 | VirtUseOps.insert(VirtUseOps.begin(), i); |
| 1482 | else |
| 1483 | VirtUseOps.push_back(i); |
| 1484 | } |
| 1485 | |
| 1486 | // Process all of the spilled uses and all non spilled reg references. |
Evan Cheng | af42fe3 | 2008-10-17 20:56:41 +0000 | [diff] [blame] | 1487 | SmallVector<int, 2> PotentialDeadStoreSlots; |
Evan Cheng | b2fd65f | 2008-02-22 19:22:06 +0000 | [diff] [blame] | 1488 | for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) { |
| 1489 | unsigned i = VirtUseOps[j]; |
| 1490 | MachineOperand &MO = MI.getOperand(i); |
| 1491 | unsigned VirtReg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1492 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
Evan Cheng | b2fd65f | 2008-02-22 19:22:06 +0000 | [diff] [blame] | 1493 | "Not a virtual register?"); |
Evan Cheng | 70306f8 | 2007-12-03 09:58:48 +0000 | [diff] [blame] | 1494 | |
Evan Cheng | c498b02 | 2007-11-14 07:59:08 +0000 | [diff] [blame] | 1495 | unsigned SubIdx = MO.getSubReg(); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1496 | if (VRM.isAssignedReg(VirtReg)) { |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1497 | // This virtual register was assigned a physreg! |
| 1498 | unsigned Phys = VRM.getPhys(VirtReg); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1499 | RegInfo->setPhysRegUsed(Phys); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1500 | if (MO.isDef()) |
| 1501 | ReusedOperands.markClobbered(Phys); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1502 | unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1503 | MI.getOperand(i).setReg(RReg); |
Evan Cheng | 4cce6b4 | 2008-04-11 17:53:36 +0000 | [diff] [blame] | 1504 | if (VRM.isImplicitlyDefined(VirtReg)) |
Bill Wendling | d62e06c | 2009-02-03 02:29:34 +0000 | [diff] [blame] | 1505 | BuildMI(MBB, &MI, MI.getDebugLoc(), |
| 1506 | TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1507 | continue; |
| 1508 | } |
| 1509 | |
| 1510 | // This virtual register is now known to be a spilled value. |
| 1511 | if (!MO.isUse()) |
| 1512 | continue; // Handle defs in the loop below (handle use&def here though) |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1513 | |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1514 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 1515 | int SSorRMId = DoReMat |
| 1516 | ? VRM.getReMatId(VirtReg) : VRM.getStackSlot(VirtReg); |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1517 | int ReuseSlot = SSorRMId; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1519 | // Check to see if this stack slot is available. |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1520 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SSorRMId); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1521 | |
| 1522 | // If this is a sub-register use, make sure the reuse register is in the |
| 1523 | // right register class. For example, for x86 not all of the 32-bit |
| 1524 | // registers have accessible sub-registers. |
| 1525 | // Similarly so for EXTRACT_SUBREG. Consider this: |
| 1526 | // EDI = op |
| 1527 | // MOV32_mr fi#1, EDI |
| 1528 | // ... |
| 1529 | // = EXTRACT_SUBREG fi#1 |
| 1530 | // fi#1 is available in EDI, but it cannot be reused because it's not in |
| 1531 | // the right register file. |
| 1532 | if (PhysReg && |
Evan Cheng | c498b02 | 2007-11-14 07:59:08 +0000 | [diff] [blame] | 1533 | (SubIdx || MI.getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1534 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1535 | if (!RC->contains(PhysReg)) |
| 1536 | PhysReg = 0; |
| 1537 | } |
| 1538 | |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1539 | if (PhysReg) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1540 | // This spilled operand might be part of a two-address operand. If this |
| 1541 | // is the case, then changing it will necessarily require changing the |
| 1542 | // def part of the instruction as well. However, in some cases, we |
| 1543 | // aren't allowed to modify the reused register. If none of these cases |
| 1544 | // apply, reuse it. |
| 1545 | bool CanReuse = true; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1546 | int ti = TID.getOperandConstraint(i, TOI::TIED_TO); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 1547 | if (ti != -1 && |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1548 | MI.getOperand(ti).isReg() && |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 1549 | MI.getOperand(ti).getReg() == VirtReg) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1550 | // 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] | 1551 | // long as we are allowed to clobber the value and there isn't an |
| 1552 | // earlier def that has already clobbered the physreg. |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1553 | CanReuse = Spills.canClobberPhysReg(ReuseSlot) && |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1554 | !ReusedOperands.isClobbered(PhysReg); |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | if (CanReuse) { |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1558 | // If this stack slot value is already available, reuse it! |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1559 | if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) |
| 1560 | DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 1561 | else |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1562 | DOUT << "Reusing SS#" << ReuseSlot; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 1563 | DOUT << " from physreg " |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 1564 | << TRI->getName(PhysReg) << " for vreg" |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1565 | << VirtReg <<" instead of reloading into physreg " |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 1566 | << TRI->getName(VRM.getPhys(VirtReg)) << "\n"; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1567 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1568 | MI.getOperand(i).setReg(RReg); |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1569 | |
| 1570 | // The only technical detail we have is that we don't know that |
| 1571 | // PhysReg won't be clobbered by a reloaded stack slot that occurs |
| 1572 | // later in the instruction. In particular, consider 'op V1, V2'. |
| 1573 | // If V1 is available in physreg R0, we would choose to reuse it |
| 1574 | // here, instead of reloading it into the register the allocator |
| 1575 | // indicated (say R1). However, V2 might have to be reloaded |
| 1576 | // later, and it might indicate that it needs to live in R0. When |
| 1577 | // this occurs, we need to have information available that |
| 1578 | // indicates it is safe to use R1 for the reload instead of R0. |
| 1579 | // |
| 1580 | // To further complicate matters, we might conflict with an alias, |
| 1581 | // or R0 and R1 might not be compatible with each other. In this |
| 1582 | // case, we actually insert a reload for V1 in R1, ensuring that |
| 1583 | // we can get at R0 or its alias. |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1584 | ReusedOperands.addReuse(i, ReuseSlot, PhysReg, |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1585 | VRM.getPhys(VirtReg), VirtReg); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1586 | if (ti != -1) |
| 1587 | // Only mark it clobbered if this is a use&def operand. |
| 1588 | ReusedOperands.markClobbered(PhysReg); |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1589 | ++NumReused; |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1590 | |
| 1591 | if (MI.getOperand(i).isKill() && |
| 1592 | ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) { |
Evan Cheng | af42fe3 | 2008-10-17 20:56:41 +0000 | [diff] [blame] | 1593 | |
| 1594 | // The store of this spilled value is potentially dead, but we |
| 1595 | // won't know for certain until we've confirmed that the re-use |
| 1596 | // above is valid, which means waiting until the other operands |
| 1597 | // are processed. For now we just track the spill slot, we'll |
| 1598 | // remove it after the other operands are processed if valid. |
| 1599 | |
| 1600 | PotentialDeadStoreSlots.push_back(ReuseSlot); |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1601 | } |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1602 | continue; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1603 | } // CanReuse |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Otherwise we have a situation where we have a two-address instruction |
| 1606 | // whose mod/ref operand needs to be reloaded. This reload is already |
| 1607 | // available in some register "PhysReg", but if we used PhysReg as the |
| 1608 | // operand to our 2-addr instruction, the instruction would modify |
| 1609 | // PhysReg. This isn't cool if something later uses PhysReg and expects |
| 1610 | // to get its initial value. |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1611 | // |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1612 | // To avoid this problem, and to avoid doing a load right after a store, |
| 1613 | // we emit a copy from PhysReg into the designated register for this |
| 1614 | // operand. |
| 1615 | unsigned DesignatedReg = VRM.getPhys(VirtReg); |
| 1616 | assert(DesignatedReg && "Must map virtreg to physreg!"); |
| 1617 | |
| 1618 | // Note that, if we reused a register for a previous operand, the |
| 1619 | // register we want to reload into might not actually be |
| 1620 | // available. If this occurs, use the register indicated by the |
| 1621 | // reuser. |
| 1622 | if (ReusedOperands.hasReuses()) |
| 1623 | DesignatedReg = ReusedOperands.GetRegForReload(DesignatedReg, &MI, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1624 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1625 | |
Chris Lattner | ba1fc3d | 2006-04-28 04:43:18 +0000 | [diff] [blame] | 1626 | // If the mapped designated register is actually the physreg we have |
| 1627 | // incoming, we don't need to inserted a dead copy. |
| 1628 | if (DesignatedReg == PhysReg) { |
| 1629 | // If this stack slot value is already available, reuse it! |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1630 | if (ReuseSlot > VirtRegMap::MAX_STACK_SLOT) |
| 1631 | DOUT << "Reusing RM#" << ReuseSlot-VirtRegMap::MAX_STACK_SLOT-1; |
Evan Cheng | 2638e1a | 2007-03-20 08:13:50 +0000 | [diff] [blame] | 1632 | else |
Evan Cheng | dc6be19 | 2007-08-14 05:42:54 +0000 | [diff] [blame] | 1633 | DOUT << "Reusing SS#" << ReuseSlot; |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 1634 | DOUT << " from physreg " << TRI->getName(PhysReg) |
Bill Wendling | 6ef781f | 2008-02-27 06:33:05 +0000 | [diff] [blame] | 1635 | << " for vreg" << VirtReg |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1636 | << " instead of reloading into same physreg.\n"; |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1637 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1638 | MI.getOperand(i).setReg(RReg); |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1639 | ReusedOperands.markClobbered(RReg); |
Chris Lattner | ba1fc3d | 2006-04-28 04:43:18 +0000 | [diff] [blame] | 1640 | ++NumReused; |
| 1641 | continue; |
| 1642 | } |
| 1643 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1644 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
| 1645 | RegInfo->setPhysRegUsed(DesignatedReg); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1646 | ReusedOperands.markClobbered(DesignatedReg); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 1647 | TII->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC, RC); |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 1648 | |
Evan Cheng | 6b44809 | 2007-03-02 08:52:00 +0000 | [diff] [blame] | 1649 | MachineInstr *CopyMI = prior(MII); |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 1650 | UpdateKills(*CopyMI, RegKills, KillOps, TRI); |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | addc55a | 2006-04-28 01:46:50 +0000 | [diff] [blame] | 1652 | // This invalidates DesignatedReg. |
| 1653 | Spills.ClobberPhysReg(DesignatedReg); |
| 1654 | |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1655 | Spills.addAvailable(ReuseSlot, DesignatedReg); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1656 | unsigned RReg = |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1657 | SubIdx ? TRI->getSubReg(DesignatedReg, SubIdx) : DesignatedReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1658 | MI.getOperand(i).setReg(RReg); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1659 | DOUT << '\t' << *prior(MII); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1660 | ++NumReused; |
| 1661 | continue; |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1662 | } // if (PhysReg) |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1663 | |
| 1664 | // Otherwise, reload it and remember that we have it. |
| 1665 | PhysReg = VRM.getPhys(VirtReg); |
Chris Lattner | 172c362 | 2006-01-04 06:47:48 +0000 | [diff] [blame] | 1666 | assert(PhysReg && "Must map virtreg to physreg!"); |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1667 | |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1668 | // Note that, if we reused a register for a previous operand, the |
| 1669 | // register we want to reload into might not actually be |
| 1670 | // available. If this occurs, use the register indicated by the |
| 1671 | // reuser. |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 1672 | if (ReusedOperands.hasReuses()) |
| 1673 | PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1674 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
Chris Lattner | 540fec6 | 2006-02-25 01:51:33 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1676 | RegInfo->setPhysRegUsed(PhysReg); |
Evan Cheng | e077ef6 | 2006-11-04 00:21:55 +0000 | [diff] [blame] | 1677 | ReusedOperands.markClobbered(PhysReg); |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1678 | if (DoReMat) { |
Evan Cheng | ca1267c | 2008-03-31 20:40:39 +0000 | [diff] [blame] | 1679 | ReMaterialize(MBB, MII, PhysReg, VirtReg, TII, TRI, VRM); |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 1680 | } else { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1681 | const TargetRegisterClass* RC = RegInfo->getRegClass(VirtReg); |
Owen Anderson | f6372aa | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 1682 | TII->loadRegFromStackSlot(MBB, &MI, PhysReg, SSorRMId, RC); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1683 | MachineInstr *LoadMI = prior(MII); |
| 1684 | VRM.addSpillSlotUse(SSorRMId, LoadMI); |
Evan Cheng | 9193514 | 2007-04-04 07:40:01 +0000 | [diff] [blame] | 1685 | ++NumLoads; |
| 1686 | } |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1687 | // This invalidates PhysReg. |
Chris Lattner | 66cf80f | 2006-02-03 23:13:58 +0000 | [diff] [blame] | 1688 | Spills.ClobberPhysReg(PhysReg); |
Chris Lattner | 50ea01e | 2005-09-09 20:29:51 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Any stores to this stack slot are not dead anymore. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1691 | if (!DoReMat) |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1692 | MaybeDeadStores[SSorRMId] = NULL; |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1693 | Spills.addAvailable(SSorRMId, PhysReg); |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 1694 | // Assumes this is the last use. IsKill will be unset if reg is reused |
| 1695 | // unless it's a two-address operand. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1696 | if (TID.getOperandConstraint(i, TOI::TIED_TO) == -1) |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 1697 | MI.getOperand(i).setIsKill(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1698 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1699 | MI.getOperand(i).setReg(RReg); |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 1700 | UpdateKills(*prior(MII), RegKills, KillOps, TRI); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1701 | DOUT << '\t' << *prior(MII); |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Evan Cheng | af42fe3 | 2008-10-17 20:56:41 +0000 | [diff] [blame] | 1704 | // Ok - now we can remove stores that have been confirmed dead. |
| 1705 | for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) { |
| 1706 | // This was the last use and the spilled value is still available |
| 1707 | // for reuse. That means the spill was unnecessary! |
| 1708 | int PDSSlot = PotentialDeadStoreSlots[j]; |
| 1709 | MachineInstr* DeadStore = MaybeDeadStores[PDSSlot]; |
| 1710 | if (DeadStore) { |
| 1711 | DOUT << "Removed dead store:\t" << *DeadStore; |
| 1712 | InvalidateKills(*DeadStore, RegKills, KillOps); |
| 1713 | VRM.RemoveMachineInstrFromMaps(DeadStore); |
| 1714 | MBB.erase(DeadStore); |
| 1715 | MaybeDeadStores[PDSSlot] = NULL; |
| 1716 | ++NumDSE; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1721 | DOUT << '\t' << MI; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 1722 | |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1723 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1724 | // If we have folded references to memory operands, make sure we clear all |
| 1725 | // physical registers that may contain the value of the spilled virtual |
| 1726 | // register |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1727 | SmallSet<int, 2> FoldedSS; |
Evan Cheng | c17ba8a | 2008-03-14 20:44:01 +0000 | [diff] [blame] | 1728 | for (tie(I, End) = VRM.getFoldedVirts(&MI); I != End; ) { |
Chris Lattner | bec6a9e | 2004-10-01 23:15:36 +0000 | [diff] [blame] | 1729 | unsigned VirtReg = I->second.first; |
| 1730 | VirtRegMap::ModRef MR = I->second.second; |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1731 | DOUT << "Folded vreg: " << VirtReg << " MR: " << MR; |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1732 | |
Evan Cheng | c17ba8a | 2008-03-14 20:44:01 +0000 | [diff] [blame] | 1733 | // MI2VirtMap be can updated which invalidate the iterator. |
| 1734 | // Increment the iterator first. |
| 1735 | ++I; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1736 | int SS = VRM.getStackSlot(VirtReg); |
Evan Cheng | 81a0382 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 1737 | if (SS == VirtRegMap::NO_STACK_SLOT) |
| 1738 | continue; |
Evan Cheng | 90a43c3 | 2007-08-15 20:20:34 +0000 | [diff] [blame] | 1739 | FoldedSS.insert(SS); |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1740 | DOUT << " - StackSlot: " << SS << "\n"; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1741 | |
| 1742 | // If this folded instruction is just a use, check to see if it's a |
| 1743 | // straight load from the virt reg slot. |
| 1744 | if ((MR & VirtRegMap::isRef) && !(MR & VirtRegMap::isMod)) { |
| 1745 | int FrameIdx; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1746 | unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx); |
| 1747 | if (DestReg && FrameIdx == SS) { |
| 1748 | // If this spill slot is available, turn it into a copy (or nothing) |
| 1749 | // instead of leaving it as a load! |
| 1750 | if (unsigned InReg = Spills.getSpillSlotOrReMatPhysReg(SS)) { |
| 1751 | DOUT << "Promoted Load To Copy: " << MI; |
| 1752 | if (DestReg != InReg) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1753 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
Owen Anderson | d10fd97 | 2007-12-31 06:32:00 +0000 | [diff] [blame] | 1754 | TII->copyRegToReg(MBB, &MI, DestReg, InReg, RC, RC); |
Evan Cheng | d9c553f | 2008-09-11 01:02:12 +0000 | [diff] [blame] | 1755 | MachineOperand *DefMO = MI.findRegisterDefOperand(DestReg); |
| 1756 | unsigned SubIdx = DefMO->getSubReg(); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1757 | // Revisit the copy so we make sure to notice the effects of the |
| 1758 | // operation on the destreg (either needing to RA it if it's |
| 1759 | // virtual or needing to clobber any values if it's physical). |
| 1760 | NextMII = &MI; |
| 1761 | --NextMII; // backtrack to the copy. |
Evan Cheng | d9c553f | 2008-09-11 01:02:12 +0000 | [diff] [blame] | 1762 | // Propagate the sub-register index over. |
| 1763 | if (SubIdx) { |
| 1764 | DefMO = NextMII->findRegisterDefOperand(DestReg); |
| 1765 | DefMO->setSubReg(SubIdx); |
| 1766 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1767 | BackTracked = true; |
Evan Cheng | 39c883c | 2007-12-11 23:36:57 +0000 | [diff] [blame] | 1768 | } else { |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1769 | DOUT << "Removing now-noop copy: " << MI; |
Evan Cheng | 39c883c | 2007-12-11 23:36:57 +0000 | [diff] [blame] | 1770 | // Unset last kill since it's being reused. |
| 1771 | InvalidateKill(InReg, RegKills, KillOps); |
| 1772 | } |
Evan Cheng | de4e942 | 2007-02-25 09:51:27 +0000 | [diff] [blame] | 1773 | |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1774 | InvalidateKills(MI, RegKills, KillOps); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1775 | VRM.RemoveMachineInstrFromMaps(&MI); |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 1776 | MBB.erase(&MI); |
| 1777 | Erased = true; |
| 1778 | goto ProcessNextInst; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1779 | } |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1780 | } else { |
| 1781 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 1782 | SmallVector<MachineInstr*, 4> NewMIs; |
| 1783 | if (PhysReg && |
Owen Anderson | 6425f8b | 2008-01-07 01:35:56 +0000 | [diff] [blame] | 1784 | TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, false, NewMIs)) { |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1785 | MBB.insert(MII, NewMIs[0]); |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1786 | InvalidateKills(MI, RegKills, KillOps); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1787 | VRM.RemoveMachineInstrFromMaps(&MI); |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1788 | MBB.erase(&MI); |
| 1789 | Erased = true; |
| 1790 | --NextMII; // backtrack to the unfolded instruction. |
| 1791 | BackTracked = true; |
| 1792 | goto ProcessNextInst; |
| 1793 | } |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | // If this reference is not a use, any previous store is now dead. |
| 1798 | // Otherwise, the store to this stack slot is not dead anymore. |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1799 | MachineInstr* DeadStore = MaybeDeadStores[SS]; |
| 1800 | if (DeadStore) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1801 | bool isDead = !(MR & VirtRegMap::isRef); |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1802 | MachineInstr *NewStore = NULL; |
Evan Cheng | cbfb9b2 | 2007-10-22 03:01:44 +0000 | [diff] [blame] | 1803 | if (MR & VirtRegMap::isModRef) { |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1804 | unsigned PhysReg = Spills.getSpillSlotOrReMatPhysReg(SS); |
| 1805 | SmallVector<MachineInstr*, 4> NewMIs; |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 1806 | // We can reuse this physreg as long as we are allowed to clobber |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1807 | // the value and there isn't an earlier def that has already clobbered |
| 1808 | // the physreg. |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1809 | if (PhysReg && |
Evan Cheng | 7ebc06b | 2008-05-07 00:49:28 +0000 | [diff] [blame] | 1810 | !TII->isStoreToStackSlot(&MI, SS)) { // Not profitable! |
| 1811 | MachineOperand *KillOpnd = |
| 1812 | DeadStore->findRegisterUseOperand(PhysReg, true); |
| 1813 | // Note, if the store is storing a sub-register, it's possible the |
| 1814 | // super-register is needed below. |
| 1815 | if (KillOpnd && !KillOpnd->getSubReg() && |
| 1816 | TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){ |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 1817 | MBB.insert(MII, NewMIs[0]); |
Evan Cheng | 7ebc06b | 2008-05-07 00:49:28 +0000 | [diff] [blame] | 1818 | NewStore = NewMIs[1]; |
| 1819 | MBB.insert(MII, NewStore); |
| 1820 | VRM.addSpillSlotUse(SS, NewStore); |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1821 | InvalidateKills(MI, RegKills, KillOps); |
Evan Cheng | 7ebc06b | 2008-05-07 00:49:28 +0000 | [diff] [blame] | 1822 | VRM.RemoveMachineInstrFromMaps(&MI); |
| 1823 | MBB.erase(&MI); |
| 1824 | Erased = true; |
| 1825 | --NextMII; |
| 1826 | --NextMII; // backtrack to the unfolded instruction. |
| 1827 | BackTracked = true; |
| 1828 | isDead = true; |
| 1829 | } |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1830 | } |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | if (isDead) { // Previous store is dead. |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1834 | // If we get here, the store is dead, nuke it now. |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1835 | DOUT << "Removed dead store:\t" << *DeadStore; |
| 1836 | InvalidateKills(*DeadStore, RegKills, KillOps); |
Evan Cheng | cada245 | 2007-11-28 01:28:46 +0000 | [diff] [blame] | 1837 | VRM.RemoveMachineInstrFromMaps(DeadStore); |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1838 | MBB.erase(DeadStore); |
| 1839 | if (!NewStore) |
| 1840 | ++NumDSE; |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1841 | } |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1842 | |
Evan Cheng | fff3e19 | 2007-08-14 09:11:18 +0000 | [diff] [blame] | 1843 | MaybeDeadStores[SS] = NULL; |
Evan Cheng | 7f56625 | 2007-10-13 02:50:24 +0000 | [diff] [blame] | 1844 | if (NewStore) { |
| 1845 | // Treat this store as a spill merged into a copy. That makes the |
| 1846 | // stack slot value available. |
| 1847 | VRM.virtFolded(VirtReg, NewStore, VirtRegMap::isMod); |
| 1848 | goto ProcessNextInst; |
| 1849 | } |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | // If the spill slot value is available, and this is a new definition of |
| 1853 | // the value, the value is not available anymore. |
| 1854 | if (MR & VirtRegMap::isMod) { |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 1855 | // Notice that the value in this stack slot has been modified. |
Evan Cheng | 549f27d3 | 2007-08-13 23:45:17 +0000 | [diff] [blame] | 1856 | Spills.ModifyStackSlotOrReMat(SS); |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 1857 | |
| 1858 | // If this is *just* a mod of the value, check to see if this is just a |
| 1859 | // store to the spill slot (i.e. the spill got merged into the copy). If |
| 1860 | // so, realize that the vreg is available now, and add the store to the |
| 1861 | // MaybeDeadStore info. |
| 1862 | int StackSlot; |
| 1863 | if (!(MR & VirtRegMap::isRef)) { |
| 1864 | if (unsigned SrcReg = TII->isStoreToStackSlot(&MI, StackSlot)) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1865 | assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 1866 | "Src hasn't been allocated yet?"); |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (CommuteToFoldReload(MBB, MII, VirtReg, SrcReg, StackSlot, |
| 1869 | RegKills, KillOps, TRI, VRM)) { |
| 1870 | NextMII = next(MII); |
| 1871 | BackTracked = true; |
| 1872 | goto ProcessNextInst; |
| 1873 | } |
| 1874 | |
Chris Lattner | 07cf141 | 2006-02-03 00:36:31 +0000 | [diff] [blame] | 1875 | // Okay, this is certainly a store of SrcReg to [StackSlot]. Mark |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 1876 | // this as a potentially dead store in case there is a subsequent |
| 1877 | // store into the stack slot without a read from it. |
| 1878 | MaybeDeadStores[StackSlot] = &MI; |
| 1879 | |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 1880 | // If the stack slot value was previously available in some other |
Evan Cheng | 87bb991 | 2008-06-13 23:58:02 +0000 | [diff] [blame] | 1881 | // register, change it now. Otherwise, make the register |
| 1882 | // available in PhysReg. |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1883 | Spills.addAvailable(StackSlot, SrcReg, false/*!clobber*/); |
Chris Lattner | cd81639 | 2006-02-02 23:29:36 +0000 | [diff] [blame] | 1884 | } |
| 1885 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1886 | } |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1889 | // Process all of the spilled defs. |
| 1890 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1891 | MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1892 | if (!(MO.isReg() && MO.getReg() && MO.isDef())) |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1893 | continue; |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 1894 | |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1895 | unsigned VirtReg = MO.getReg(); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1896 | if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1897 | // Check to see if this is a noop copy. If so, eliminate the |
| 1898 | // instruction before considering the dest reg to be changed. |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 1899 | unsigned Src, Dst, SrcSR, DstSR; |
| 1900 | if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1901 | ++NumDCE; |
| 1902 | DOUT << "Removing now-noop copy: " << MI; |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1903 | SmallVector<unsigned, 2> KillRegs; |
| 1904 | InvalidateKills(MI, RegKills, KillOps, &KillRegs); |
| 1905 | if (MO.isDead() && !KillRegs.empty()) { |
Evan Cheng | bbe4105cd | 2008-12-02 02:15:36 +0000 | [diff] [blame] | 1906 | // Source register or an implicit super/sub-register use is killed. |
| 1907 | assert(KillRegs[0] == Dst || |
| 1908 | TRI->isSubRegister(KillRegs[0], Dst) || |
| 1909 | TRI->isSuperRegister(KillRegs[0], Dst)); |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1910 | // Last def is now dead. |
| 1911 | TransferDeadness(&MBB, Dist, Src, RegKills, KillOps); |
| 1912 | } |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1913 | VRM.RemoveMachineInstrFromMaps(&MI); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1914 | MBB.erase(&MI); |
| 1915 | Erased = true; |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1916 | Spills.disallowClobberPhysReg(VirtReg); |
| 1917 | goto ProcessNextInst; |
| 1918 | } |
| 1919 | |
| 1920 | // If it's not a no-op copy, it clobbers the value in the destreg. |
| 1921 | Spills.ClobberPhysReg(VirtReg); |
| 1922 | ReusedOperands.markClobbered(VirtReg); |
| 1923 | |
| 1924 | // Check to see if this instruction is a load from a stack slot into |
| 1925 | // a register. If so, this provides the stack slot value in the reg. |
| 1926 | int FrameIdx; |
| 1927 | if (unsigned DestReg = TII->isLoadFromStackSlot(&MI, FrameIdx)) { |
| 1928 | assert(DestReg == VirtReg && "Unknown load situation!"); |
| 1929 | |
| 1930 | // If it is a folded reference, then it's not safe to clobber. |
| 1931 | bool Folded = FoldedSS.count(FrameIdx); |
| 1932 | // Otherwise, if it wasn't available, remember that it is now! |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 1933 | Spills.addAvailable(FrameIdx, DestReg, !Folded); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1934 | goto ProcessNextInst; |
| 1935 | } |
| 1936 | |
| 1937 | continue; |
| 1938 | } |
| 1939 | |
Evan Cheng | c498b02 | 2007-11-14 07:59:08 +0000 | [diff] [blame] | 1940 | unsigned SubIdx = MO.getSubReg(); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1941 | bool DoReMat = VRM.isReMaterialized(VirtReg); |
| 1942 | if (DoReMat) |
| 1943 | ReMatDefs.insert(&MI); |
| 1944 | |
| 1945 | // The only vregs left are stack slot definitions. |
| 1946 | int StackSlot = VRM.getStackSlot(VirtReg); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1947 | const TargetRegisterClass *RC = RegInfo->getRegClass(VirtReg); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1948 | |
| 1949 | // If this def is part of a two-address operand, make sure to execute |
| 1950 | // the store from the correct physical register. |
| 1951 | unsigned PhysReg; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1952 | int TiedOp = MI.getDesc().findTiedToSrcOperand(i); |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1953 | if (TiedOp != -1) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1954 | PhysReg = MI.getOperand(TiedOp).getReg(); |
Evan Cheng | c498b02 | 2007-11-14 07:59:08 +0000 | [diff] [blame] | 1955 | if (SubIdx) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1956 | unsigned SuperReg = findSuperReg(RC, PhysReg, SubIdx, TRI); |
| 1957 | assert(SuperReg && TRI->getSubReg(SuperReg, SubIdx) == PhysReg && |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1958 | "Can't find corresponding super-register!"); |
| 1959 | PhysReg = SuperReg; |
| 1960 | } |
| 1961 | } else { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1962 | PhysReg = VRM.getPhys(VirtReg); |
| 1963 | if (ReusedOperands.isClobbered(PhysReg)) { |
| 1964 | // Another def has taken the assigned physreg. It must have been a |
| 1965 | // use&def which got it due to reuse. Undo the reuse! |
| 1966 | PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, |
| 1967 | Spills, MaybeDeadStores, RegKills, KillOps, VRM); |
| 1968 | } |
| 1969 | } |
| 1970 | |
Evan Cheng | ed70cbb3 | 2008-03-26 19:03:01 +0000 | [diff] [blame] | 1971 | assert(PhysReg && "VR not assigned a physical register?"); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1972 | RegInfo->setPhysRegUsed(PhysReg); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1973 | unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg; |
Evan Cheng | 7277a7d | 2007-11-02 17:35:08 +0000 | [diff] [blame] | 1974 | ReusedOperands.markClobbered(RReg); |
| 1975 | MI.getOperand(i).setReg(RReg); |
| 1976 | |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1977 | if (!MO.isDead()) { |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1978 | MachineInstr *&LastStore = MaybeDeadStores[StackSlot]; |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 1979 | SpillRegToStackSlot(MBB, MII, -1, PhysReg, StackSlot, RC, true, |
| 1980 | LastStore, Spills, ReMatDefs, RegKills, KillOps, VRM); |
Evan Cheng | e4b3900 | 2007-12-03 21:31:55 +0000 | [diff] [blame] | 1981 | NextMII = next(MII); |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Check to see if this is a noop copy. If so, eliminate the |
| 1984 | // instruction before considering the dest reg to be changed. |
| 1985 | { |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 1986 | unsigned Src, Dst, SrcSR, DstSR; |
| 1987 | if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) { |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1988 | ++NumDCE; |
Bill Wendling | b2b9c20 | 2006-11-17 02:09:07 +0000 | [diff] [blame] | 1989 | DOUT << "Removing now-noop copy: " << MI; |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 1990 | InvalidateKills(MI, RegKills, KillOps); |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 1991 | VRM.RemoveMachineInstrFromMaps(&MI); |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1992 | MBB.erase(&MI); |
Evan Cheng | 0c40d72 | 2007-07-11 05:28:39 +0000 | [diff] [blame] | 1993 | Erased = true; |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 1994 | UpdateKills(*LastStore, RegKills, KillOps, TRI); |
Chris Lattner | 2926869 | 2006-09-05 02:12:02 +0000 | [diff] [blame] | 1995 | goto ProcessNextInst; |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1996 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1997 | } |
Evan Cheng | 66f7163 | 2007-10-19 21:23:22 +0000 | [diff] [blame] | 1998 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 1999 | } |
Chris Lattner | cea8688 | 2005-09-19 06:56:21 +0000 | [diff] [blame] | 2000 | ProcessNextInst: |
Evan Cheng | 7a0f185 | 2008-05-20 08:13:21 +0000 | [diff] [blame] | 2001 | DistanceMap.insert(std::make_pair(&MI, Dist++)); |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 2002 | if (!Erased && !BackTracked) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 2003 | for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II) |
Evan Cheng | 6784598 | 2008-10-17 06:16:07 +0000 | [diff] [blame] | 2004 | UpdateKills(*II, RegKills, KillOps, TRI); |
Evan Cheng | 35a3e4a | 2007-12-04 19:19:45 +0000 | [diff] [blame] | 2005 | } |
Chris Lattner | 7fb6434 | 2004-10-01 19:04:51 +0000 | [diff] [blame] | 2006 | MII = NextMII; |
| 2007 | } |
Evan Cheng | 752272a | 2009-02-11 08:24:21 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Chris Lattner | 8c4d88d | 2004-09-30 01:54:45 +0000 | [diff] [blame] | 2011 | llvm::Spiller* llvm::createSpiller() { |
| 2012 | switch (SpillerOpt) { |
| 2013 | default: assert(0 && "Unreachable!"); |
| 2014 | case local: |
| 2015 | return new LocalSpiller(); |
| 2016 | case simple: |
| 2017 | return new SimpleSpiller(); |
| 2018 | } |
Alkis Evlogimenos | 0d6c5b6 | 2004-02-24 08:58:30 +0000 | [diff] [blame] | 2019 | } |