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