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 | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 52 | /// 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] | 53 | /// immediately before each use, and stores after each def. No folding or |
| 54 | /// remat is attempted. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 55 | std::vector<LiveInterval*> trivialSpillEverywhere(LiveInterval *li) { |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 56 | DEBUG(errs() << "Spilling everywhere " << *li << "\n"); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 57 | |
| 58 | assert(li->weight != HUGE_VALF && |
| 59 | "Attempting to spill already spilled value."); |
| 60 | |
| 61 | assert(!li->isStackSlot() && |
| 62 | "Trying to spill a stack slot."); |
| 63 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 64 | DEBUG(errs() << "Trivial spill everywhere of reg" << li->reg << "\n"); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 65 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 66 | std::vector<LiveInterval*> added; |
| 67 | |
| 68 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 69 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 70 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 71 | // Iterate over reg uses/defs. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 72 | for (MachineRegisterInfo::reg_iterator |
| 73 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 74 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 75 | // Grab the use/def instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 76 | MachineInstr *mi = &*regItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 77 | |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 78 | DEBUG(errs() << " Processing " << *mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 79 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 80 | // Step regItr to the next use/def instr. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 81 | do { |
| 82 | ++regItr; |
| 83 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 84 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 85 | // Collect uses & defs for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 86 | SmallVector<unsigned, 2> indices; |
| 87 | bool hasUse = false; |
| 88 | bool hasDef = false; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 89 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 90 | MachineOperand &op = mi->getOperand(i); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 91 | if (!op.isReg() || op.getReg() != li->reg) |
| 92 | continue; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 93 | hasUse |= mi->getOperand(i).isUse(); |
| 94 | hasDef |= mi->getOperand(i).isDef(); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 95 | indices.push_back(i); |
| 96 | } |
| 97 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 98 | // Create a new vreg & interval for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 99 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 100 | vrm->grow(); |
| 101 | vrm->assignVirt2StackSlot(newVReg, ss); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 102 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 103 | newLI->weight = HUGE_VALF; |
| 104 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 105 | // Update the reg operands & kill flags. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 106 | for (unsigned i = 0; i < indices.size(); ++i) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 107 | unsigned mopIdx = indices[i]; |
| 108 | MachineOperand &mop = mi->getOperand(mopIdx); |
| 109 | mop.setReg(newVReg); |
| 110 | if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) { |
| 111 | mop.setIsKill(true); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 114 | assert(hasUse || hasDef); |
| 115 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 116 | // Insert reload if necessary. |
| 117 | MachineBasicBlock::iterator miItr(mi); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 118 | if (hasUse) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 119 | tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc); |
| 120 | MachineInstr *loadInstr(prior(miItr)); |
| 121 | SlotIndex loadIndex = |
| 122 | lis->InsertMachineInstrInMaps(loadInstr).getDefIndex(); |
| 123 | SlotIndex endIndex = loadIndex.getNextIndex(); |
| 124 | VNInfo *loadVNI = |
| 125 | newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator()); |
| 126 | loadVNI->addKill(endIndex); |
| 127 | newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 130 | // Insert store if necessary. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 131 | if (hasDef) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 132 | tii->storeRegToStackSlot(*mi->getParent(), next(miItr), newVReg, true, |
| 133 | ss, trc); |
| 134 | MachineInstr *storeInstr(next(miItr)); |
| 135 | SlotIndex storeIndex = |
| 136 | lis->InsertMachineInstrInMaps(storeInstr).getDefIndex(); |
| 137 | SlotIndex beginIndex = storeIndex.getPrevIndex(); |
| 138 | VNInfo *storeVNI = |
| 139 | newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator()); |
| 140 | storeVNI->addKill(storeIndex); |
| 141 | newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 144 | added.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 147 | return added; |
| 148 | } |
| 149 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 150 | }; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 151 | |
| 152 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 153 | /// Spills any live range using the spill-everywhere method with no attempt at |
| 154 | /// folding. |
| 155 | class TrivialSpiller : public SpillerBase { |
| 156 | public: |
Lang Hames | 10382fb | 2009-06-19 02:17:53 +0000 | [diff] [blame] | 157 | |
| 158 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, LiveStacks *ls, |
| 159 | VirtRegMap *vrm) : |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 160 | SpillerBase(mf, lis, ls, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 161 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 162 | std::vector<LiveInterval*> spill(LiveInterval *li) { |
| 163 | return trivialSpillEverywhere(li); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | }; |
| 167 | |
| 168 | } |
| 169 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 170 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 171 | LiveStacks *ls, VirtRegMap *vrm) { |
| 172 | return new TrivialSpiller(mf, lis, ls, vrm); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 173 | } |