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 | b366158 | 2009-11-14 00:02:51 +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 | b366158 | 2009-11-14 00:02:51 +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 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 64 | //assert(lis->hasGapBeforeInstr(miIdx)); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 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 | b366158 | 2009-11-14 00:02:51 +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 | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 77 | //} |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 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 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 81 | //assert(lis->hasGapAfterInstr(miIdx)); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 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 | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 102 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 103 | return lis->InsertMachineInstrInMaps(storeInst); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 106 | /// Insert a store of the given vreg to the given stack slot immediately |
| 107 | /// before the given instructnion. Returns the base index of the inserted |
| 108 | /// Instruction. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 109 | SlotIndex insertStoreBefore(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 110 | unsigned vreg, |
| 111 | const TargetRegisterClass *trc) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 112 | SlotIndex miIdx = makeSpaceBefore(mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 113 | |
| 114 | tii->storeRegToStackSlot(*mi->getParent(), mi, vreg, true, ss, trc); |
| 115 | MachineBasicBlock::iterator storeInstItr(prior(mi)); |
| 116 | MachineInstr *storeInst = &*storeInstItr; |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 117 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 118 | return lis->InsertMachineInstrInMaps(storeInst); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void insertStoreAfterInstOnInterval(LiveInterval *li, |
| 122 | MachineInstr *mi, unsigned ss, |
| 123 | unsigned vreg, |
| 124 | const TargetRegisterClass *trc) { |
| 125 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 126 | SlotIndex storeInstIdx = insertStoreAfter(mi, ss, vreg, trc); |
| 127 | SlotIndex start = lis->getInstructionIndex(mi).getDefIndex(), |
| 128 | end = storeInstIdx.getUseIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 129 | |
| 130 | VNInfo *vni = |
| 131 | li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 132 | vni->addKill(storeInstIdx); |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 133 | DEBUG(errs() << " Inserting store range: [" << start |
| 134 | << ", " << end << ")\n"); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 135 | LiveRange lr(start, end, vni); |
| 136 | |
| 137 | li->addRange(lr); |
| 138 | } |
| 139 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 140 | /// Insert a load of the given vreg from the given stack slot immediately |
| 141 | /// after the given instruction. Returns the base index of the inserted |
| 142 | /// instruction. The caller is responsibel for adding/removing an appropriate |
| 143 | /// range vreg's LiveInterval. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 144 | SlotIndex insertLoadAfter(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 145 | unsigned vreg, |
| 146 | const TargetRegisterClass *trc) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 147 | |
| 148 | MachineBasicBlock::iterator nextInstItr(next(mi)); |
| 149 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 150 | SlotIndex miIdx = makeSpaceAfter(mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 151 | |
| 152 | tii->loadRegFromStackSlot(*mi->getParent(), nextInstItr, vreg, ss, trc); |
| 153 | MachineBasicBlock::iterator loadInstItr(next(mi)); |
| 154 | MachineInstr *loadInst = &*loadInstItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 155 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 156 | return lis->InsertMachineInstrInMaps(loadInst); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /// 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] | 160 | /// before the given instruction. Returns the base index of the inserted |
| 161 | /// instruction. The caller is responsible for adding an appropriate |
| 162 | /// LiveInterval to the LiveIntervals analysis. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 163 | SlotIndex insertLoadBefore(MachineInstr *mi, unsigned ss, |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 164 | unsigned vreg, |
| 165 | const TargetRegisterClass *trc) { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 166 | SlotIndex miIdx = makeSpaceBefore(mi); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 167 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 168 | tii->loadRegFromStackSlot(*mi->getParent(), mi, vreg, ss, trc); |
| 169 | MachineBasicBlock::iterator loadInstItr(prior(mi)); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 170 | MachineInstr *loadInst = &*loadInstItr; |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 171 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 172 | return lis->InsertMachineInstrInMaps(loadInst); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 175 | void insertLoadBeforeInstOnInterval(LiveInterval *li, |
| 176 | MachineInstr *mi, unsigned ss, |
| 177 | unsigned vreg, |
| 178 | const TargetRegisterClass *trc) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 179 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 180 | SlotIndex loadInstIdx = insertLoadBefore(mi, ss, vreg, trc); |
| 181 | SlotIndex start = loadInstIdx.getDefIndex(), |
| 182 | end = lis->getInstructionIndex(mi).getUseIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 183 | |
| 184 | VNInfo *vni = |
| 185 | li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 8651125 | 2009-09-04 20:41:11 +0000 | [diff] [blame] | 186 | vni->addKill(lis->getInstructionIndex(mi)); |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 187 | DEBUG(errs() << " Intserting load range: [" << start |
| 188 | << ", " << end << ")\n"); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 189 | LiveRange lr(start, end, vni); |
| 190 | |
| 191 | li->addRange(lr); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 196 | /// Add spill ranges for every use/def of the live interval, inserting loads |
| 197 | /// immediately before each use, and stores after each def. No folding is |
| 198 | /// attempted. |
| 199 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 200 | DEBUG(errs() << "Spilling everywhere " << *li << "\n"); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 201 | |
| 202 | assert(li->weight != HUGE_VALF && |
| 203 | "Attempting to spill already spilled value."); |
| 204 | |
| 205 | assert(!li->isStackSlot() && |
| 206 | "Trying to spill a stack slot."); |
| 207 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 208 | DEBUG(errs() << "Trivial spill everywhere of reg" << li->reg << "\n"); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 209 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 210 | std::vector<LiveInterval*> added; |
| 211 | |
| 212 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 213 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 214 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 215 | for (MachineRegisterInfo::reg_iterator |
| 216 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 217 | |
| 218 | MachineInstr *mi = &*regItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 219 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 220 | DEBUG(errs() << " Processing " << *mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 221 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 222 | do { |
| 223 | ++regItr; |
| 224 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 225 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 226 | SmallVector<unsigned, 2> indices; |
| 227 | bool hasUse = false; |
| 228 | bool hasDef = false; |
| 229 | |
| 230 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 231 | MachineOperand &op = mi->getOperand(i); |
| 232 | |
| 233 | if (!op.isReg() || op.getReg() != li->reg) |
| 234 | continue; |
| 235 | |
| 236 | hasUse |= mi->getOperand(i).isUse(); |
| 237 | hasDef |= mi->getOperand(i).isDef(); |
| 238 | |
| 239 | indices.push_back(i); |
| 240 | } |
| 241 | |
| 242 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 243 | vrm->grow(); |
| 244 | vrm->assignVirt2StackSlot(newVReg, ss); |
| 245 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 246 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 247 | newLI->weight = HUGE_VALF; |
| 248 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 249 | for (unsigned i = 0; i < indices.size(); ++i) { |
| 250 | mi->getOperand(indices[i]).setReg(newVReg); |
| 251 | |
| 252 | if (mi->getOperand(indices[i]).isUse()) { |
| 253 | mi->getOperand(indices[i]).setIsKill(true); |
| 254 | } |
| 255 | } |
| 256 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 257 | assert(hasUse || hasDef); |
| 258 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 259 | if (hasUse) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 260 | insertLoadBeforeInstOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (hasDef) { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 264 | insertStoreAfterInstOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 267 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 270 | return added; |
| 271 | } |
| 272 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 273 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 274 | |
| 275 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 276 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 277 | /// folding. |
| 278 | class TrivialSpiller : public SpillerBase { |
| 279 | public: |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 280 | |
| 281 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 282 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 283 | SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 284 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 285 | std::vector<LiveInterval*> spill(LiveInterval *li) { |
| 286 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 289 | std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) { |
| 290 | std::vector<LiveInterval*> spillIntervals; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 291 | |
| 292 | if (!valno->isDefAccurate() && !valno->isPHIDef()) { |
| 293 | // Early out for values which have no well defined def point. |
| 294 | return spillIntervals; |
| 295 | } |
| 296 | |
| 297 | // Ok.. we should be able to proceed... |
| 298 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
| 299 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 300 | vrm->grow(); |
| 301 | vrm->assignVirt2StackSlot(li->reg, ss); |
| 302 | |
| 303 | MachineInstr *mi = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 304 | SlotIndex storeIdx = SlotIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 305 | |
| 306 | if (valno->isDefAccurate()) { |
| 307 | // If we have an accurate def we can just grab an iterator to the instr |
| 308 | // after the def. |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 309 | mi = lis->getInstructionFromIndex(valno->def); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 310 | storeIdx = insertStoreAfter(mi, ss, li->reg, trc).getDefIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 311 | } else { |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 312 | // if we get here we have a PHI def. |
| 313 | mi = &lis->getMBBFromIndex(valno->def)->front(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 314 | storeIdx = insertStoreBefore(mi, ss, li->reg, trc).getDefIndex(); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 317 | MachineBasicBlock *defBlock = mi->getParent(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 318 | SlotIndex loadIdx = SlotIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 319 | |
| 320 | // Now we need to find the load... |
| 321 | MachineBasicBlock::iterator useItr(mi); |
| 322 | for (; !useItr->readsRegister(li->reg); ++useItr) {} |
| 323 | |
| 324 | if (useItr != defBlock->end()) { |
| 325 | MachineInstr *loadInst = useItr; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 326 | loadIdx = insertLoadBefore(loadInst, ss, li->reg, trc).getUseIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 327 | } |
| 328 | else { |
| 329 | MachineInstr *loadInst = &defBlock->back(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 330 | loadIdx = insertLoadAfter(loadInst, ss, li->reg, trc).getUseIndex(); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | li->removeRange(storeIdx, loadIdx, true); |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 334 | |
| 335 | return spillIntervals; |
| 336 | } |
| 337 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | } |
| 341 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 342 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 343 | LiveStacks *ls, VirtRegMap *vrm) { |
| 344 | return new TrivialSpiller(mf, lis, ls, vrm); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 345 | } |