Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/Spiller.cpp - Spiller -------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #define DEBUG_TYPE "spiller" |
| 11 | |
| 12 | #include "Spiller.h" |
| 13 | #include "VirtRegMap.h" |
| 14 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
| 21 | #include "llvm/Support/Debug.h" |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 23 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | Spiller::~Spiller() {} |
| 27 | |
| 28 | namespace { |
| 29 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 30 | /// Utility class for spillers. |
| 31 | class SpillerBase : public Spiller { |
| 32 | protected: |
| 33 | |
| 34 | MachineFunction *mf; |
| 35 | LiveIntervals *lis; |
| 36 | LiveStacks *ls; |
| 37 | MachineFrameInfo *mfi; |
| 38 | MachineRegisterInfo *mri; |
| 39 | const TargetInstrInfo *tii; |
| 40 | VirtRegMap *vrm; |
| 41 | |
| 42 | /// Construct a spiller base. |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 43 | SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 44 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 45 | mf(mf), lis(lis), ls(ls), vrm(vrm) |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 46 | { |
| 47 | mfi = mf->getFrameInfo(); |
| 48 | mri = &mf->getRegInfo(); |
| 49 | tii = mf->getTarget().getInstrInfo(); |
| 50 | } |
| 51 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 52 | /// Ensures there is space before the given machine instruction, returns the |
| 53 | /// instruction's new number. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 54 | SlotIndex makeSpaceBefore(MachineInstr *mi) { |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 55 | if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 56 | // FIXME: Should be updated to use rewrite-in-place methods when they're |
| 57 | // introduced. Currently broken. |
| 58 | //lis->scaleNumbering(2); |
| 59 | //ls->scaleNumbering(2); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 60 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 61 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 62 | SlotIndex miIdx = lis->getInstructionIndex(mi); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 63 | |
| 64 | assert(lis->hasGapBeforeInstr(miIdx)); |
| 65 | |
| 66 | return miIdx; |
| 67 | } |
| 68 | |
| 69 | /// Ensure there is space after the given machine instruction, returns the |
| 70 | /// instruction's new number. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 71 | SlotIndex makeSpaceAfter(MachineInstr *mi) { |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 72 | if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 73 | // FIXME: Should be updated to use rewrite-in-place methods when they're |
| 74 | // introduced. Currently broken. |
| 75 | // lis->scaleNumbering(2); |
| 76 | // ls->scaleNumbering(2); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 79 | SlotIndex miIdx = lis->getInstructionIndex(mi); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 80 | |
| 81 | assert(lis->hasGapAfterInstr(miIdx)); |
| 82 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 83 | return miIdx; |
| 84 | } |
| 85 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 86 | /// Insert a store of the given vreg to the given stack slot immediately |
| 87 | /// after the given instruction. Returns the base index of the inserted |
| 88 | /// instruction. The caller is responsible for adding an appropriate |
| 89 | /// LiveInterval to the LiveIntervals analysis. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 90 | SlotIndex insertStoreAfter(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 91 | unsigned vreg, |
| 92 | const TargetRegisterClass *trc) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 93 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 94 | MachineBasicBlock::iterator nextInstItr(next(mi)); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 95 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 96 | SlotIndex miIdx = makeSpaceAfter(mi); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 97 | |
| 98 | tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 99 | true, ss, trc); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 100 | MachineBasicBlock::iterator storeInstItr(next(mi)); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 101 | MachineInstr *storeInst = &*storeInstItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 102 | SlotIndex storeInstIdx = miIdx.getNextIndex(); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 103 | |
| 104 | assert(lis->getInstructionFromIndex(storeInstIdx) == 0 && |
| 105 | "Store inst index already in use."); |
| 106 | |
| 107 | lis->InsertMachineInstrInMaps(storeInst, storeInstIdx); |
| 108 | |
| 109 | return storeInstIdx; |
| 110 | } |
| 111 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 112 | /// Insert a store of the given vreg to the given stack slot immediately |
| 113 | /// before the given instructnion. Returns the base index of the inserted |
| 114 | /// Instruction. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 115 | SlotIndex insertStoreBefore(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 116 | unsigned vreg, |
| 117 | const TargetRegisterClass *trc) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 118 | SlotIndex miIdx = makeSpaceBefore(mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 119 | |
| 120 | tii->storeRegToStackSlot(*mi->getParent(), mi, vreg, true, ss, trc); |
| 121 | MachineBasicBlock::iterator storeInstItr(prior(mi)); |
| 122 | MachineInstr *storeInst = &*storeInstItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 123 | SlotIndex storeInstIdx = miIdx.getPrevIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 124 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 125 | assert(lis->getInstructionFromIndex(storeInstIdx) == 0 && |
| 126 | "Store inst index already in use."); |
| 127 | |
| 128 | lis->InsertMachineInstrInMaps(storeInst, storeInstIdx); |
| 129 | |
| 130 | return storeInstIdx; |
| 131 | } |
| 132 | |
| 133 | void insertStoreAfterInstOnInterval(LiveInterval *li, |
| 134 | MachineInstr *mi, unsigned ss, |
| 135 | unsigned vreg, |
| 136 | const TargetRegisterClass *trc) { |
| 137 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 138 | SlotIndex storeInstIdx = insertStoreAfter(mi, ss, vreg, trc); |
| 139 | SlotIndex start = lis->getInstructionIndex(mi).getDefIndex(), |
| 140 | end = storeInstIdx.getUseIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 141 | |
| 142 | VNInfo *vni = |
| 143 | li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 144 | vni->addKill(storeInstIdx); |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 145 | DEBUG(errs() << " Inserting store range: [" << start |
| 146 | << ", " << end << ")\n"); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 147 | LiveRange lr(start, end, vni); |
| 148 | |
| 149 | li->addRange(lr); |
| 150 | } |
| 151 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 152 | /// Insert a load of the given vreg from the given stack slot immediately |
| 153 | /// after the given instruction. Returns the base index of the inserted |
| 154 | /// instruction. The caller is responsibel for adding/removing an appropriate |
| 155 | /// range vreg's LiveInterval. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 156 | SlotIndex insertLoadAfter(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 157 | unsigned vreg, |
| 158 | const TargetRegisterClass *trc) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 159 | |
| 160 | MachineBasicBlock::iterator nextInstItr(next(mi)); |
| 161 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 162 | SlotIndex miIdx = makeSpaceAfter(mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 163 | |
| 164 | tii->loadRegFromStackSlot(*mi->getParent(), nextInstItr, vreg, ss, trc); |
| 165 | MachineBasicBlock::iterator loadInstItr(next(mi)); |
| 166 | MachineInstr *loadInst = &*loadInstItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 167 | SlotIndex loadInstIdx = miIdx.getNextIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 168 | |
| 169 | assert(lis->getInstructionFromIndex(loadInstIdx) == 0 && |
| 170 | "Store inst index already in use."); |
| 171 | |
| 172 | lis->InsertMachineInstrInMaps(loadInst, loadInstIdx); |
| 173 | |
| 174 | return loadInstIdx; |
| 175 | } |
| 176 | |
| 177 | /// Insert a load of the given vreg from the given stack slot immediately |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 178 | /// before the given instruction. Returns the base index of the inserted |
| 179 | /// instruction. The caller is responsible for adding an appropriate |
| 180 | /// LiveInterval to the LiveIntervals analysis. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 181 | SlotIndex insertLoadBefore(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 182 | unsigned vreg, |
| 183 | const TargetRegisterClass *trc) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 184 | SlotIndex miIdx = makeSpaceBefore(mi); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 185 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 186 | tii->loadRegFromStackSlot(*mi->getParent(), mi, vreg, ss, trc); |
| 187 | MachineBasicBlock::iterator loadInstItr(prior(mi)); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 188 | MachineInstr *loadInst = &*loadInstItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 189 | SlotIndex loadInstIdx = miIdx.getPrevIndex(); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 190 | |
| 191 | assert(lis->getInstructionFromIndex(loadInstIdx) == 0 && |
| 192 | "Load inst index already in use."); |
| 193 | |
| 194 | lis->InsertMachineInstrInMaps(loadInst, loadInstIdx); |
| 195 | |
| 196 | return loadInstIdx; |
| 197 | } |
| 198 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 199 | void insertLoadBeforeInstOnInterval(LiveInterval *li, |
| 200 | MachineInstr *mi, unsigned ss, |
| 201 | unsigned vreg, |
| 202 | const TargetRegisterClass *trc) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 203 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 204 | SlotIndex loadInstIdx = insertLoadBefore(mi, ss, vreg, trc); |
| 205 | SlotIndex start = loadInstIdx.getDefIndex(), |
| 206 | end = lis->getInstructionIndex(mi).getUseIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 207 | |
| 208 | VNInfo *vni = |
| 209 | li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 210 | vni->addKill(lis->getInstructionIndex(mi)); |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 211 | DEBUG(errs() << " Intserting load range: [" << start |
| 212 | << ", " << end << ")\n"); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 213 | LiveRange lr(start, end, vni); |
| 214 | |
| 215 | li->addRange(lr); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 220 | /// Add spill ranges for every use/def of the live interval, inserting loads |
| 221 | /// immediately before each use, and stores after each def. No folding is |
| 222 | /// attempted. |
| 223 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 224 | DEBUG(errs() << "Spilling everywhere " << *li << "\n"); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 225 | |
| 226 | assert(li->weight != HUGE_VALF && |
| 227 | "Attempting to spill already spilled value."); |
| 228 | |
| 229 | assert(!li->isStackSlot() && |
| 230 | "Trying to spill a stack slot."); |
| 231 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 232 | DEBUG(errs() << "Trivial spill everywhere of reg" << li->reg << "\n"); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 233 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 234 | std::vector<LiveInterval*> added; |
| 235 | |
| 236 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 237 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 238 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 239 | for (MachineRegisterInfo::reg_iterator |
| 240 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 241 | |
| 242 | MachineInstr *mi = &*regItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 243 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 244 | DEBUG(errs() << " Processing " << *mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 245 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 246 | do { |
| 247 | ++regItr; |
| 248 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 249 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 250 | SmallVector<unsigned, 2> indices; |
| 251 | bool hasUse = false; |
| 252 | bool hasDef = false; |
| 253 | |
| 254 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 255 | MachineOperand &op = mi->getOperand(i); |
| 256 | |
| 257 | if (!op.isReg() || op.getReg() != li->reg) |
| 258 | continue; |
| 259 | |
| 260 | hasUse |= mi->getOperand(i).isUse(); |
| 261 | hasDef |= mi->getOperand(i).isDef(); |
| 262 | |
| 263 | indices.push_back(i); |
| 264 | } |
| 265 | |
| 266 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 267 | vrm->grow(); |
| 268 | vrm->assignVirt2StackSlot(newVReg, ss); |
| 269 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 270 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 271 | newLI->weight = HUGE_VALF; |
| 272 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 273 | for (unsigned i = 0; i < indices.size(); ++i) { |
| 274 | mi->getOperand(indices[i]).setReg(newVReg); |
| 275 | |
| 276 | if (mi->getOperand(indices[i]).isUse()) { |
| 277 | mi->getOperand(indices[i]).setIsKill(true); |
| 278 | } |
| 279 | } |
| 280 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 281 | assert(hasUse || hasDef); |
| 282 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 283 | if (hasUse) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 284 | insertLoadBeforeInstOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (hasDef) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 288 | insertStoreAfterInstOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 291 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 294 | return added; |
| 295 | } |
| 296 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 297 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 298 | |
| 299 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 300 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 301 | /// folding. |
| 302 | class TrivialSpiller : public SpillerBase { |
| 303 | public: |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 304 | |
| 305 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 306 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 307 | SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 308 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 309 | std::vector<LiveInterval*> spill(LiveInterval *li) { |
| 310 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 313 | std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) { |
| 314 | std::vector<LiveInterval*> spillIntervals; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 315 | |
| 316 | if (!valno->isDefAccurate() && !valno->isPHIDef()) { |
| 317 | // Early out for values which have no well defined def point. |
| 318 | return spillIntervals; |
| 319 | } |
| 320 | |
| 321 | // Ok.. we should be able to proceed... |
| 322 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
| 323 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 324 | vrm->grow(); |
| 325 | vrm->assignVirt2StackSlot(li->reg, ss); |
| 326 | |
| 327 | MachineInstr *mi = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 328 | SlotIndex storeIdx = SlotIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 329 | |
| 330 | if (valno->isDefAccurate()) { |
| 331 | // If we have an accurate def we can just grab an iterator to the instr |
| 332 | // after the def. |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 333 | mi = lis->getInstructionFromIndex(valno->def); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 334 | storeIdx = insertStoreAfter(mi, ss, li->reg, trc).getDefIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 335 | } else { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 336 | // if we get here we have a PHI def. |
| 337 | mi = &lis->getMBBFromIndex(valno->def)->front(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 338 | storeIdx = insertStoreBefore(mi, ss, li->reg, trc).getDefIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 341 | MachineBasicBlock *defBlock = mi->getParent(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 342 | SlotIndex loadIdx = SlotIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 343 | |
| 344 | // Now we need to find the load... |
| 345 | MachineBasicBlock::iterator useItr(mi); |
| 346 | for (; !useItr->readsRegister(li->reg); ++useItr) {} |
| 347 | |
| 348 | if (useItr != defBlock->end()) { |
| 349 | MachineInstr *loadInst = useItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 350 | loadIdx = insertLoadBefore(loadInst, ss, li->reg, trc).getUseIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 351 | } |
| 352 | else { |
| 353 | MachineInstr *loadInst = &defBlock->back(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 354 | loadIdx = insertLoadAfter(loadInst, ss, li->reg, trc).getUseIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | li->removeRange(storeIdx, loadIdx, true); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 358 | |
| 359 | return spillIntervals; |
| 360 | } |
| 361 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | } |
| 365 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 366 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 367 | LiveStacks *ls, VirtRegMap *vrm) { |
| 368 | return new TrivialSpiller(mf, lis, ls, vrm); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 369 | } |