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" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | Spiller::~Spiller() {} |
| 26 | |
| 27 | namespace { |
| 28 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 29 | /// Utility class for spillers. |
| 30 | class SpillerBase : public Spiller { |
| 31 | protected: |
| 32 | |
| 33 | MachineFunction *mf; |
| 34 | LiveIntervals *lis; |
| 35 | LiveStacks *ls; |
| 36 | MachineFrameInfo *mfi; |
| 37 | MachineRegisterInfo *mri; |
| 38 | const TargetInstrInfo *tii; |
| 39 | VirtRegMap *vrm; |
| 40 | |
| 41 | /// Construct a spiller base. |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 42 | SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 43 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 44 | mf(mf), lis(lis), ls(ls), vrm(vrm) |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 45 | { |
| 46 | mfi = mf->getFrameInfo(); |
| 47 | mri = &mf->getRegInfo(); |
| 48 | tii = mf->getTarget().getInstrInfo(); |
| 49 | } |
| 50 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 51 | /// Ensures there is space before the given machine instruction, returns the |
| 52 | /// instruction's new number. |
| 53 | unsigned makeSpaceBefore(MachineInstr *mi) { |
| 54 | if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) { |
| 55 | lis->scaleNumbering(2); |
| 56 | ls->scaleNumbering(2); |
| 57 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 58 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 59 | unsigned miIdx = lis->getInstructionIndex(mi); |
| 60 | |
| 61 | assert(lis->hasGapBeforeInstr(miIdx)); |
| 62 | |
| 63 | return miIdx; |
| 64 | } |
| 65 | |
| 66 | /// Ensure there is space after the given machine instruction, returns the |
| 67 | /// instruction's new number. |
| 68 | unsigned makeSpaceAfter(MachineInstr *mi) { |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 69 | if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) { |
| 70 | lis->scaleNumbering(2); |
| 71 | ls->scaleNumbering(2); |
| 72 | } |
| 73 | |
| 74 | unsigned miIdx = lis->getInstructionIndex(mi); |
| 75 | |
| 76 | assert(lis->hasGapAfterInstr(miIdx)); |
| 77 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 78 | return miIdx; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /// Insert a store of the given vreg to the given stack slot immediately |
| 83 | /// after the given instruction. Returns the base index of the inserted |
| 84 | /// instruction. The caller is responsible for adding an appropriate |
| 85 | /// LiveInterval to the LiveIntervals analysis. |
| 86 | unsigned insertStoreFor(MachineInstr *mi, unsigned ss, |
| 87 | unsigned vreg, |
| 88 | const TargetRegisterClass *trc) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 89 | |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 90 | MachineBasicBlock::iterator nextInstItr(mi); |
| 91 | ++nextInstItr; |
| 92 | |
| 93 | unsigned miIdx = makeSpaceAfter(mi); |
| 94 | |
| 95 | tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, vreg, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 96 | true, ss, trc); |
| 97 | MachineBasicBlock::iterator storeInstItr(mi); |
| 98 | ++storeInstItr; |
| 99 | MachineInstr *storeInst = &*storeInstItr; |
| 100 | unsigned storeInstIdx = miIdx + LiveInterval::InstrSlots::NUM; |
| 101 | |
| 102 | assert(lis->getInstructionFromIndex(storeInstIdx) == 0 && |
| 103 | "Store inst index already in use."); |
| 104 | |
| 105 | lis->InsertMachineInstrInMaps(storeInst, storeInstIdx); |
| 106 | |
| 107 | return storeInstIdx; |
| 108 | } |
| 109 | |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 110 | void insertStoreOnInterval(LiveInterval *li, |
| 111 | MachineInstr *mi, unsigned ss, |
| 112 | unsigned vreg, |
| 113 | const TargetRegisterClass *trc) { |
| 114 | |
| 115 | unsigned storeInstIdx = insertStoreFor(mi, ss, vreg, trc); |
| 116 | unsigned start = lis->getDefIndex(lis->getInstructionIndex(mi)), |
| 117 | end = lis->getUseIndex(storeInstIdx); |
| 118 | |
| 119 | VNInfo *vni = |
| 120 | li->getNextValue(storeInstIdx, 0, true, lis->getVNInfoAllocator()); |
| 121 | vni->kills.push_back(storeInstIdx); |
| 122 | LiveRange lr(start, end, vni); |
| 123 | |
| 124 | li->addRange(lr); |
| 125 | } |
| 126 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 127 | /// Insert a load of the given veg from the given stack slot immediately |
| 128 | /// before the given instruction. Returns the base index of the inserted |
| 129 | /// instruction. The caller is responsible for adding an appropriate |
| 130 | /// LiveInterval to the LiveIntervals analysis. |
| 131 | unsigned insertLoadFor(MachineInstr *mi, unsigned ss, |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 132 | unsigned vreg, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 133 | const TargetRegisterClass *trc) { |
| 134 | MachineBasicBlock::iterator useInstItr(mi); |
Lang Hames | 857c4e0 | 2009-06-17 21:01:20 +0000 | [diff] [blame] | 135 | |
| 136 | unsigned miIdx = makeSpaceBefore(mi); |
| 137 | |
| 138 | tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, vreg, ss, trc); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 139 | MachineBasicBlock::iterator loadInstItr(mi); |
| 140 | --loadInstItr; |
| 141 | MachineInstr *loadInst = &*loadInstItr; |
| 142 | unsigned loadInstIdx = miIdx - LiveInterval::InstrSlots::NUM; |
| 143 | |
| 144 | assert(lis->getInstructionFromIndex(loadInstIdx) == 0 && |
| 145 | "Load inst index already in use."); |
| 146 | |
| 147 | lis->InsertMachineInstrInMaps(loadInst, loadInstIdx); |
| 148 | |
| 149 | return loadInstIdx; |
| 150 | } |
| 151 | |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 152 | void insertLoadOnInterval(LiveInterval *li, |
| 153 | MachineInstr *mi, unsigned ss, |
| 154 | unsigned vreg, |
| 155 | const TargetRegisterClass *trc) { |
| 156 | |
| 157 | unsigned loadInstIdx = insertLoadFor(mi, ss, vreg, trc); |
| 158 | unsigned start = lis->getDefIndex(loadInstIdx), |
| 159 | end = lis->getUseIndex(lis->getInstructionIndex(mi)); |
| 160 | |
| 161 | VNInfo *vni = |
| 162 | li->getNextValue(loadInstIdx, 0, true, lis->getVNInfoAllocator()); |
| 163 | vni->kills.push_back(lis->getInstructionIndex(mi)); |
| 164 | LiveRange lr(start, end, vni); |
| 165 | |
| 166 | li->addRange(lr); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 171 | /// Add spill ranges for every use/def of the live interval, inserting loads |
| 172 | /// immediately before each use, and stores after each def. No folding is |
| 173 | /// attempted. |
| 174 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
| 175 | DOUT << "Spilling everywhere " << *li << "\n"; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 176 | |
| 177 | assert(li->weight != HUGE_VALF && |
| 178 | "Attempting to spill already spilled value."); |
| 179 | |
| 180 | assert(!li->isStackSlot() && |
| 181 | "Trying to spill a stack slot."); |
| 182 | |
| 183 | std::vector<LiveInterval*> added; |
| 184 | |
| 185 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 186 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 187 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 188 | for (MachineRegisterInfo::reg_iterator |
| 189 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 190 | |
| 191 | MachineInstr *mi = &*regItr; |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 192 | do { |
| 193 | ++regItr; |
| 194 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 195 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 196 | SmallVector<unsigned, 2> indices; |
| 197 | bool hasUse = false; |
| 198 | bool hasDef = false; |
| 199 | |
| 200 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 201 | MachineOperand &op = mi->getOperand(i); |
| 202 | |
| 203 | if (!op.isReg() || op.getReg() != li->reg) |
| 204 | continue; |
| 205 | |
| 206 | hasUse |= mi->getOperand(i).isUse(); |
| 207 | hasDef |= mi->getOperand(i).isDef(); |
| 208 | |
| 209 | indices.push_back(i); |
| 210 | } |
| 211 | |
| 212 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 213 | vrm->grow(); |
| 214 | vrm->assignVirt2StackSlot(newVReg, ss); |
| 215 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 216 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 217 | newLI->weight = HUGE_VALF; |
| 218 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 219 | for (unsigned i = 0; i < indices.size(); ++i) { |
| 220 | mi->getOperand(indices[i]).setReg(newVReg); |
| 221 | |
| 222 | if (mi->getOperand(indices[i]).isUse()) { |
| 223 | mi->getOperand(indices[i]).setIsKill(true); |
| 224 | } |
| 225 | } |
| 226 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 227 | assert(hasUse || hasDef); |
| 228 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 229 | if (hasUse) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 230 | insertLoadOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (hasDef) { |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 234 | insertStoreOnInterval(newLI, mi, ss, newVReg, trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 237 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 240 | return added; |
| 241 | } |
| 242 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 243 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 244 | |
| 245 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 246 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 247 | /// folding. |
| 248 | class TrivialSpiller : public SpillerBase { |
| 249 | public: |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 250 | |
| 251 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 252 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 253 | SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 254 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 255 | std::vector<LiveInterval*> spill(LiveInterval *li) { |
| 256 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 259 | std::vector<LiveInterval*> intraBlockSplit(LiveInterval *li, VNInfo *valno) { |
| 260 | std::vector<LiveInterval*> spillIntervals; |
| 261 | MachineBasicBlock::iterator storeInsertPoint; |
| 262 | |
| 263 | if (valno->isDefAccurate()) { |
| 264 | // If we have an accurate def we can just grab an iterator to the instr |
| 265 | // after the def. |
| 266 | storeInsertPoint = |
| 267 | next(MachineBasicBlock::iterator(lis->getInstructionFromIndex(valno->def))); |
| 268 | } else { |
| 269 | // If the def info isn't accurate we check if this is a PHI def. |
| 270 | // If it is then def holds the index of the defining Basic Block, and we |
| 271 | // can use that to get an insertion point. |
| 272 | if (valno->isPHIDef()) { |
| 273 | |
| 274 | } else { |
| 275 | // We have no usable def info. We can't split this value sensibly. |
| 276 | // FIXME: Need sensible feedback for "failure to split", an empty |
| 277 | // set of spill intervals could be reasonably returned from a |
| 278 | // split where both the store and load are folded. |
| 279 | return spillIntervals; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | return spillIntervals; |
| 286 | } |
| 287 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | } |
| 291 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 292 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 293 | LiveStacks *ls, VirtRegMap *vrm) { |
| 294 | return new TrivialSpiller(mf, lis, ls, vrm); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 295 | } |