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. |
| 42 | SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, VirtRegMap *vrm) : |
| 43 | mf(mf), lis(lis), ls(ls), vrm(vrm) |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 44 | { |
| 45 | mfi = mf->getFrameInfo(); |
| 46 | mri = &mf->getRegInfo(); |
| 47 | tii = mf->getTarget().getInstrInfo(); |
| 48 | } |
| 49 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 50 | /// Insert a store of the given vreg to the given stack slot immediately |
| 51 | /// after the given instruction. Returns the base index of the inserted |
| 52 | /// instruction. The caller is responsible for adding an appropriate |
| 53 | /// LiveInterval to the LiveIntervals analysis. |
| 54 | unsigned insertStoreFor(MachineInstr *mi, unsigned ss, |
| 55 | unsigned newVReg, |
| 56 | const TargetRegisterClass *trc) { |
| 57 | MachineBasicBlock::iterator nextInstItr(mi); |
| 58 | ++nextInstItr; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 59 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 60 | if (!lis->hasGapAfterInstr(lis->getInstructionIndex(mi))) { |
| 61 | lis->scaleNumbering(2); |
| 62 | ls->scaleNumbering(2); |
| 63 | } |
| 64 | |
| 65 | unsigned miIdx = lis->getInstructionIndex(mi); |
| 66 | |
| 67 | assert(lis->hasGapAfterInstr(miIdx)); |
| 68 | |
| 69 | tii->storeRegToStackSlot(*mi->getParent(), nextInstItr, newVReg, |
| 70 | true, ss, trc); |
| 71 | MachineBasicBlock::iterator storeInstItr(mi); |
| 72 | ++storeInstItr; |
| 73 | MachineInstr *storeInst = &*storeInstItr; |
| 74 | unsigned storeInstIdx = miIdx + LiveInterval::InstrSlots::NUM; |
| 75 | |
| 76 | assert(lis->getInstructionFromIndex(storeInstIdx) == 0 && |
| 77 | "Store inst index already in use."); |
| 78 | |
| 79 | lis->InsertMachineInstrInMaps(storeInst, storeInstIdx); |
| 80 | |
| 81 | return storeInstIdx; |
| 82 | } |
| 83 | |
| 84 | /// Insert a load of the given veg from the given stack slot immediately |
| 85 | /// before the given instruction. Returns the base index of the inserted |
| 86 | /// instruction. The caller is responsible for adding an appropriate |
| 87 | /// LiveInterval to the LiveIntervals analysis. |
| 88 | unsigned insertLoadFor(MachineInstr *mi, unsigned ss, |
| 89 | unsigned newVReg, |
| 90 | const TargetRegisterClass *trc) { |
| 91 | MachineBasicBlock::iterator useInstItr(mi); |
| 92 | |
| 93 | if (!lis->hasGapBeforeInstr(lis->getInstructionIndex(mi))) { |
| 94 | lis->scaleNumbering(2); |
| 95 | ls->scaleNumbering(2); |
| 96 | } |
| 97 | |
| 98 | unsigned miIdx = lis->getInstructionIndex(mi); |
| 99 | |
| 100 | assert(lis->hasGapBeforeInstr(miIdx)); |
| 101 | |
| 102 | tii->loadRegFromStackSlot(*mi->getParent(), useInstItr, newVReg, ss, trc); |
| 103 | MachineBasicBlock::iterator loadInstItr(mi); |
| 104 | --loadInstItr; |
| 105 | MachineInstr *loadInst = &*loadInstItr; |
| 106 | unsigned loadInstIdx = miIdx - LiveInterval::InstrSlots::NUM; |
| 107 | |
| 108 | assert(lis->getInstructionFromIndex(loadInstIdx) == 0 && |
| 109 | "Load inst index already in use."); |
| 110 | |
| 111 | lis->InsertMachineInstrInMaps(loadInst, loadInstIdx); |
| 112 | |
| 113 | return loadInstIdx; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /// Add spill ranges for every use/def of the live interval, inserting loads |
| 118 | /// immediately before each use, and stores after each def. No folding is |
| 119 | /// attempted. |
| 120 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
| 121 | DOUT << "Spilling everywhere " << *li << "\n"; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 122 | |
| 123 | assert(li->weight != HUGE_VALF && |
| 124 | "Attempting to spill already spilled value."); |
| 125 | |
| 126 | assert(!li->isStackSlot() && |
| 127 | "Trying to spill a stack slot."); |
| 128 | |
| 129 | std::vector<LiveInterval*> added; |
| 130 | |
| 131 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 132 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 133 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 134 | for (MachineRegisterInfo::reg_iterator |
| 135 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 136 | |
| 137 | MachineInstr *mi = &*regItr; |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 138 | do { |
| 139 | ++regItr; |
| 140 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 141 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 142 | SmallVector<unsigned, 2> indices; |
| 143 | bool hasUse = false; |
| 144 | bool hasDef = false; |
| 145 | |
| 146 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 147 | MachineOperand &op = mi->getOperand(i); |
| 148 | |
| 149 | if (!op.isReg() || op.getReg() != li->reg) |
| 150 | continue; |
| 151 | |
| 152 | hasUse |= mi->getOperand(i).isUse(); |
| 153 | hasDef |= mi->getOperand(i).isDef(); |
| 154 | |
| 155 | indices.push_back(i); |
| 156 | } |
| 157 | |
| 158 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 159 | vrm->grow(); |
| 160 | vrm->assignVirt2StackSlot(newVReg, ss); |
| 161 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 162 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 163 | newLI->weight = HUGE_VALF; |
| 164 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 165 | for (unsigned i = 0; i < indices.size(); ++i) { |
| 166 | mi->getOperand(indices[i]).setReg(newVReg); |
| 167 | |
| 168 | if (mi->getOperand(indices[i]).isUse()) { |
| 169 | mi->getOperand(indices[i]).setIsKill(true); |
| 170 | } |
| 171 | } |
| 172 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 173 | assert(hasUse || hasDef); |
| 174 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 175 | if (hasUse) { |
| 176 | unsigned loadInstIdx = insertLoadFor(mi, ss, newVReg, trc); |
| 177 | unsigned start = lis->getDefIndex(loadInstIdx), |
| 178 | end = lis->getUseIndex(lis->getInstructionIndex(mi)); |
| 179 | |
| 180 | VNInfo *vni = |
| 181 | newLI->getNextValue(loadInstIdx, 0, lis->getVNInfoAllocator()); |
| 182 | vni->kills.push_back(lis->getInstructionIndex(mi)); |
| 183 | LiveRange lr(start, end, vni); |
| 184 | |
| 185 | newLI->addRange(lr); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (hasDef) { |
| 189 | unsigned storeInstIdx = insertStoreFor(mi, ss, newVReg, trc); |
| 190 | unsigned start = lis->getDefIndex(lis->getInstructionIndex(mi)), |
| 191 | end = lis->getUseIndex(storeInstIdx); |
| 192 | |
| 193 | VNInfo *vni = |
| 194 | newLI->getNextValue(storeInstIdx, 0, lis->getVNInfoAllocator()); |
| 195 | vni->kills.push_back(storeInstIdx); |
| 196 | LiveRange lr(start, end, vni); |
| 197 | |
| 198 | newLI->addRange(lr); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 201 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | return added; |
| 206 | } |
| 207 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 208 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 209 | |
| 210 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 211 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 212 | /// folding. |
| 213 | class TrivialSpiller : public SpillerBase { |
| 214 | public: |
| 215 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, VirtRegMap *vrm) : |
| 216 | SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 217 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 218 | std::vector<LiveInterval*> spill(LiveInterval *li) { |
| 219 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | }; |
| 223 | |
| 224 | } |
| 225 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 226 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 227 | LiveStacks *ls, VirtRegMap *vrm) { |
| 228 | return new TrivialSpiller(mf, lis, ls, vrm); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 229 | } |