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" |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/CommandLine.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 24 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 27 | namespace { |
| 28 | enum SpillerName { trivial, standard }; |
| 29 | } |
| 30 | |
| 31 | static cl::opt<SpillerName> |
| 32 | spillerOpt("spiller", |
| 33 | cl::desc("Spiller to use: (default: standard)"), |
| 34 | cl::Prefix, |
| 35 | cl::values(clEnumVal(trivial, "trivial spiller"), |
| 36 | clEnumVal(standard, "default spiller"), |
| 37 | clEnumValEnd), |
| 38 | cl::init(standard)); |
| 39 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 40 | Spiller::~Spiller() {} |
| 41 | |
| 42 | namespace { |
| 43 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 44 | /// Utility class for spillers. |
| 45 | class SpillerBase : public Spiller { |
| 46 | protected: |
| 47 | |
| 48 | MachineFunction *mf; |
| 49 | LiveIntervals *lis; |
| 50 | LiveStacks *ls; |
| 51 | MachineFrameInfo *mfi; |
| 52 | MachineRegisterInfo *mri; |
| 53 | const TargetInstrInfo *tii; |
| 54 | VirtRegMap *vrm; |
| 55 | |
| 56 | /// Construct a spiller base. |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 57 | SpillerBase(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 58 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 59 | mf(mf), lis(lis), ls(ls), vrm(vrm) |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 60 | { |
| 61 | mfi = mf->getFrameInfo(); |
| 62 | mri = &mf->getRegInfo(); |
| 63 | tii = mf->getTarget().getInstrInfo(); |
| 64 | } |
| 65 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 66 | /// Add spill ranges for every use/def of the live interval, inserting loads |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 67 | /// immediately before each use, and stores after each def. No folding or |
| 68 | /// remat is attempted. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 69 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 70 | DEBUG(errs() << "Spilling everywhere " << *li << "\n"); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 71 | |
| 72 | assert(li->weight != HUGE_VALF && |
| 73 | "Attempting to spill already spilled value."); |
| 74 | |
| 75 | assert(!li->isStackSlot() && |
| 76 | "Trying to spill a stack slot."); |
| 77 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 78 | DEBUG(errs() << "Trivial spill everywhere of reg" << li->reg << "\n"); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 79 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 80 | std::vector<LiveInterval*> added; |
| 81 | |
| 82 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 83 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 84 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 85 | // Iterate over reg uses/defs. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 86 | for (MachineRegisterInfo::reg_iterator |
| 87 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 88 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 89 | // Grab the use/def instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 90 | MachineInstr *mi = &*regItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 91 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 92 | DEBUG(errs() << " Processing " << *mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 93 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 94 | // Step regItr to the next use/def instr. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 95 | do { |
| 96 | ++regItr; |
| 97 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 98 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 99 | // Collect uses & defs for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 100 | SmallVector<unsigned, 2> indices; |
| 101 | bool hasUse = false; |
| 102 | bool hasDef = false; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 103 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 104 | MachineOperand &op = mi->getOperand(i); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 105 | if (!op.isReg() || op.getReg() != li->reg) |
| 106 | continue; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 107 | hasUse |= mi->getOperand(i).isUse(); |
| 108 | hasDef |= mi->getOperand(i).isDef(); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 109 | indices.push_back(i); |
| 110 | } |
| 111 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 112 | // Create a new vreg & interval for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 113 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 114 | vrm->grow(); |
| 115 | vrm->assignVirt2StackSlot(newVReg, ss); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 116 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 117 | newLI->weight = HUGE_VALF; |
| 118 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 119 | // Update the reg operands & kill flags. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 120 | for (unsigned i = 0; i < indices.size(); ++i) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 121 | unsigned mopIdx = indices[i]; |
| 122 | MachineOperand &mop = mi->getOperand(mopIdx); |
| 123 | mop.setReg(newVReg); |
| 124 | if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) { |
| 125 | mop.setIsKill(true); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 128 | assert(hasUse || hasDef); |
| 129 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 130 | // Insert reload if necessary. |
| 131 | MachineBasicBlock::iterator miItr(mi); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 132 | if (hasUse) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 133 | tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc); |
| 134 | MachineInstr *loadInstr(prior(miItr)); |
| 135 | SlotIndex loadIndex = |
| 136 | lis->InsertMachineInstrInMaps(loadInstr).getDefIndex(); |
| 137 | SlotIndex endIndex = loadIndex.getNextIndex(); |
| 138 | VNInfo *loadVNI = |
| 139 | newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator()); |
| 140 | loadVNI->addKill(endIndex); |
| 141 | newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 144 | // Insert store if necessary. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 145 | if (hasDef) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 146 | tii->storeRegToStackSlot(*mi->getParent(), next(miItr), newVReg, true, |
| 147 | ss, trc); |
| 148 | MachineInstr *storeInstr(next(miItr)); |
| 149 | SlotIndex storeIndex = |
| 150 | lis->InsertMachineInstrInMaps(storeInstr).getDefIndex(); |
| 151 | SlotIndex beginIndex = storeIndex.getPrevIndex(); |
| 152 | VNInfo *storeVNI = |
| 153 | newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator()); |
| 154 | storeVNI->addKill(storeIndex); |
| 155 | newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 158 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 161 | return added; |
| 162 | } |
| 163 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 164 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 165 | |
| 166 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 167 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 168 | /// folding. |
| 169 | class TrivialSpiller : public SpillerBase { |
| 170 | public: |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 171 | |
| 172 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 173 | VirtRegMap *vrm) |
| 174 | : SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 175 | |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 176 | std::vector<LiveInterval*> spill(LiveInterval *li, |
| 177 | SmallVectorImpl<LiveInterval*> &spillIs) { |
| 178 | // Ignore spillIs - we don't use it. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 179 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | }; |
| 183 | |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 184 | /// Falls back on LiveIntervals::addIntervalsForSpills. |
| 185 | class StandardSpiller : public Spiller { |
| 186 | private: |
| 187 | LiveIntervals *lis; |
| 188 | const MachineLoopInfo *loopInfo; |
| 189 | VirtRegMap *vrm; |
| 190 | public: |
| 191 | StandardSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 192 | const MachineLoopInfo *loopInfo, VirtRegMap *vrm) |
| 193 | : lis(lis), loopInfo(loopInfo), vrm(vrm) {} |
| 194 | |
| 195 | /// Falls back on LiveIntervals::addIntervalsForSpills. |
| 196 | std::vector<LiveInterval*> spill(LiveInterval *li, |
| 197 | SmallVectorImpl<LiveInterval*> &spillIs) { |
| 198 | return lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm); |
| 199 | } |
| 200 | |
| 201 | }; |
| 202 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 205 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame^] | 206 | LiveStacks *ls, |
| 207 | const MachineLoopInfo *loopInfo, |
| 208 | VirtRegMap *vrm) { |
| 209 | switch (spillerOpt) { |
| 210 | case trivial: return new TrivialSpiller(mf, lis, ls, vrm); break; |
| 211 | case standard: return new StandardSpiller(mf, lis, ls, loopInfo, vrm); break; |
| 212 | default: llvm_unreachable("Unreachable!"); break; |
| 213 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 214 | } |