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" |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Bill Wendling | c75e7d2 | 2009-08-22 20:54:03 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 24 | #include <set> |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 25 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 28 | namespace { |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 29 | enum SpillerName { trivial, standard, splitting }; |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static cl::opt<SpillerName> |
| 33 | spillerOpt("spiller", |
| 34 | cl::desc("Spiller to use: (default: standard)"), |
| 35 | cl::Prefix, |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 36 | cl::values(clEnumVal(trivial, "trivial spiller"), |
| 37 | clEnumVal(standard, "default spiller"), |
| 38 | clEnumVal(splitting, "splitting spiller"), |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 39 | clEnumValEnd), |
| 40 | cl::init(standard)); |
| 41 | |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 42 | // Spiller virtual destructor implementation. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 43 | Spiller::~Spiller() {} |
| 44 | |
| 45 | namespace { |
| 46 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 47 | /// Utility class for spillers. |
| 48 | class SpillerBase : public Spiller { |
| 49 | protected: |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 50 | MachineFunction *mf; |
| 51 | LiveIntervals *lis; |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 52 | MachineFrameInfo *mfi; |
| 53 | MachineRegisterInfo *mri; |
| 54 | const TargetInstrInfo *tii; |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 55 | const TargetRegisterInfo *tri; |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 56 | VirtRegMap *vrm; |
| 57 | |
| 58 | /// Construct a spiller base. |
Lang Hames | 8783e40 | 2009-11-20 00:53:30 +0000 | [diff] [blame] | 59 | SpillerBase(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm) |
| 60 | : mf(mf), lis(lis), vrm(vrm) |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 61 | { |
| 62 | mfi = mf->getFrameInfo(); |
| 63 | mri = &mf->getRegInfo(); |
| 64 | tii = mf->getTarget().getInstrInfo(); |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 65 | tri = mf->getTarget().getRegisterInfo(); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 68 | /// 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] | 69 | /// immediately before each use, and stores after each def. No folding or |
| 70 | /// remat is attempted. |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 71 | void trivialSpillEverywhere(LiveInterval *li, |
| 72 | std::vector<LiveInterval*> &newIntervals) { |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 73 | DEBUG(dbgs() << "Spilling everywhere " << *li << "\n"); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 74 | |
| 75 | assert(li->weight != HUGE_VALF && |
| 76 | "Attempting to spill already spilled value."); |
| 77 | |
| 78 | assert(!li->isStackSlot() && |
| 79 | "Trying to spill a stack slot."); |
| 80 | |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 81 | DEBUG(dbgs() << "Trivial spill everywhere of reg" << li->reg << "\n"); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 82 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 83 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 84 | unsigned ss = vrm->assignVirt2StackSlot(li->reg); |
| 85 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 86 | // Iterate over reg uses/defs. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 87 | for (MachineRegisterInfo::reg_iterator |
| 88 | regItr = mri->reg_begin(li->reg); regItr != mri->reg_end();) { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 89 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 90 | // Grab the use/def instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 91 | MachineInstr *mi = &*regItr; |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 92 | |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 93 | DEBUG(dbgs() << " Processing " << *mi); |
Lang Hames | 6bbc73d | 2009-06-24 20:46:24 +0000 | [diff] [blame] | 94 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 95 | // Step regItr to the next use/def instr. |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 96 | do { |
| 97 | ++regItr; |
| 98 | } while (regItr != mri->reg_end() && (&*regItr == mi)); |
| 99 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 100 | // Collect uses & defs for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 101 | SmallVector<unsigned, 2> indices; |
| 102 | bool hasUse = false; |
| 103 | bool hasDef = false; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 104 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) { |
| 105 | MachineOperand &op = mi->getOperand(i); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 106 | if (!op.isReg() || op.getReg() != li->reg) |
| 107 | continue; |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 108 | hasUse |= mi->getOperand(i).isUse(); |
| 109 | hasDef |= mi->getOperand(i).isDef(); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 110 | indices.push_back(i); |
| 111 | } |
| 112 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 113 | // Create a new vreg & interval for this instr. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 114 | unsigned newVReg = mri->createVirtualRegister(trc); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 115 | vrm->grow(); |
| 116 | vrm->assignVirt2StackSlot(newVReg, ss); |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 117 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 118 | newLI->weight = HUGE_VALF; |
| 119 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 120 | // Update the reg operands & kill flags. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i < indices.size(); ++i) { |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 122 | unsigned mopIdx = indices[i]; |
| 123 | MachineOperand &mop = mi->getOperand(mopIdx); |
| 124 | mop.setReg(newVReg); |
| 125 | if (mop.isUse() && !mi->isRegTiedToDefOperand(mopIdx)) { |
| 126 | mop.setIsKill(true); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Lang Hames | f41538d | 2009-06-02 16:53:25 +0000 | [diff] [blame] | 129 | assert(hasUse || hasDef); |
| 130 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 131 | // Insert reload if necessary. |
| 132 | MachineBasicBlock::iterator miItr(mi); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 133 | if (hasUse) { |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 134 | tii->loadRegFromStackSlot(*mi->getParent(), miItr, newVReg, ss, trc, |
| 135 | tri); |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 136 | MachineInstr *loadInstr(prior(miItr)); |
| 137 | SlotIndex loadIndex = |
| 138 | lis->InsertMachineInstrInMaps(loadInstr).getDefIndex(); |
| 139 | SlotIndex endIndex = loadIndex.getNextIndex(); |
| 140 | VNInfo *loadVNI = |
| 141 | newLI->getNextValue(loadIndex, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 142 | newLI->addRange(LiveRange(loadIndex, endIndex, loadVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 145 | // Insert store if necessary. |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 146 | if (hasDef) { |
Evan Cheng | e9b3ac2 | 2010-05-06 16:33:12 +0000 | [diff] [blame] | 147 | tii->storeRegToStackSlot(*mi->getParent(), llvm::next(miItr), newVReg, |
Evan Cheng | 746ad69 | 2010-05-06 19:06:44 +0000 | [diff] [blame] | 148 | true, ss, trc, tri); |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 149 | MachineInstr *storeInstr(llvm::next(miItr)); |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 150 | SlotIndex storeIndex = |
| 151 | lis->InsertMachineInstrInMaps(storeInstr).getDefIndex(); |
| 152 | SlotIndex beginIndex = storeIndex.getPrevIndex(); |
| 153 | VNInfo *storeVNI = |
| 154 | newLI->getNextValue(beginIndex, 0, true, lis->getVNInfoAllocator()); |
Lang Hames | 38283e2 | 2009-11-18 20:31:20 +0000 | [diff] [blame] | 155 | newLI->addRange(LiveRange(beginIndex, storeIndex, storeVNI)); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 158 | newIntervals.push_back(newLI); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 159 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 1ca6531 | 2010-04-07 22:44:07 +0000 | [diff] [blame] | 163 | } // end anonymous namespace |
| 164 | |
| 165 | namespace { |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 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 | |
Lang Hames | 8783e40 | 2009-11-20 00:53:30 +0000 | [diff] [blame] | 172 | TrivialSpiller(MachineFunction *mf, LiveIntervals *lis, VirtRegMap *vrm) |
| 173 | : SpillerBase(mf, lis, vrm) {} |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 174 | |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 175 | void spill(LiveInterval *li, |
| 176 | std::vector<LiveInterval*> &newIntervals, |
| 177 | SmallVectorImpl<LiveInterval*> &, |
| 178 | SlotIndex*) { |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 179 | // Ignore spillIs - we don't use it. |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 180 | trivialSpillEverywhere(li, newIntervals); |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 181 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
Chris Lattner | 1ca6531 | 2010-04-07 22:44:07 +0000 | [diff] [blame] | 184 | } // end anonymous namespace |
| 185 | |
| 186 | namespace { |
| 187 | |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 188 | /// Falls back on LiveIntervals::addIntervalsForSpills. |
| 189 | class StandardSpiller : public Spiller { |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 190 | protected: |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 191 | LiveIntervals *lis; |
| 192 | const MachineLoopInfo *loopInfo; |
| 193 | VirtRegMap *vrm; |
| 194 | public: |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 195 | StandardSpiller(LiveIntervals *lis, const MachineLoopInfo *loopInfo, |
| 196 | VirtRegMap *vrm) |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 197 | : lis(lis), loopInfo(loopInfo), vrm(vrm) {} |
| 198 | |
| 199 | /// Falls back on LiveIntervals::addIntervalsForSpills. |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 200 | void spill(LiveInterval *li, |
| 201 | std::vector<LiveInterval*> &newIntervals, |
| 202 | SmallVectorImpl<LiveInterval*> &spillIs, |
| 203 | SlotIndex*) { |
| 204 | std::vector<LiveInterval*> added = |
| 205 | lis->addIntervalsForSpills(*li, spillIs, loopInfo, *vrm); |
| 206 | newIntervals.insert(newIntervals.end(), added.begin(), added.end()); |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 207 | } |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Chris Lattner | 1ca6531 | 2010-04-07 22:44:07 +0000 | [diff] [blame] | 210 | } // end anonymous namespace |
| 211 | |
| 212 | namespace { |
| 213 | |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 214 | /// When a call to spill is placed this spiller will first try to break the |
| 215 | /// interval up into its component values (one new interval per value). |
| 216 | /// If this fails, or if a call is placed to spill a previously split interval |
| 217 | /// then the spiller falls back on the standard spilling mechanism. |
| 218 | class SplittingSpiller : public StandardSpiller { |
| 219 | public: |
| 220 | SplittingSpiller(MachineFunction *mf, LiveIntervals *lis, |
| 221 | const MachineLoopInfo *loopInfo, VirtRegMap *vrm) |
| 222 | : StandardSpiller(lis, loopInfo, vrm) { |
| 223 | |
| 224 | mri = &mf->getRegInfo(); |
| 225 | tii = mf->getTarget().getInstrInfo(); |
| 226 | tri = mf->getTarget().getRegisterInfo(); |
| 227 | } |
| 228 | |
Jakob Stoklund Olesen | 67674e2 | 2010-06-24 20:54:29 +0000 | [diff] [blame] | 229 | void spill(LiveInterval *li, |
| 230 | std::vector<LiveInterval*> &newIntervals, |
| 231 | SmallVectorImpl<LiveInterval*> &spillIs, |
| 232 | SlotIndex *earliestStart) { |
| 233 | if (worthTryingToSplit(li)) |
| 234 | tryVNISplit(li, earliestStart); |
| 235 | else |
| 236 | StandardSpiller::spill(li, newIntervals, spillIs, earliestStart); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | private: |
| 240 | |
| 241 | MachineRegisterInfo *mri; |
| 242 | const TargetInstrInfo *tii; |
| 243 | const TargetRegisterInfo *tri; |
| 244 | DenseSet<LiveInterval*> alreadySplit; |
| 245 | |
| 246 | bool worthTryingToSplit(LiveInterval *li) const { |
| 247 | return (!alreadySplit.count(li) && li->getNumValNums() > 1); |
| 248 | } |
| 249 | |
| 250 | /// Try to break a LiveInterval into its component values. |
| 251 | std::vector<LiveInterval*> tryVNISplit(LiveInterval *li, |
| 252 | SlotIndex *earliestStart) { |
| 253 | |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 254 | DEBUG(dbgs() << "Trying VNI split of %reg" << *li << "\n"); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 255 | |
| 256 | std::vector<LiveInterval*> added; |
| 257 | SmallVector<VNInfo*, 4> vnis; |
| 258 | |
| 259 | std::copy(li->vni_begin(), li->vni_end(), std::back_inserter(vnis)); |
| 260 | |
| 261 | for (SmallVectorImpl<VNInfo*>::iterator vniItr = vnis.begin(), |
| 262 | vniEnd = vnis.end(); vniItr != vniEnd; ++vniItr) { |
| 263 | VNInfo *vni = *vniItr; |
| 264 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 265 | // Skip unused VNIs. |
| 266 | if (vni->isUnused()) |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 267 | continue; |
| 268 | |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 269 | DEBUG(dbgs() << " Extracted Val #" << vni->id << " as "); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 270 | LiveInterval *splitInterval = extractVNI(li, vni); |
| 271 | |
| 272 | if (splitInterval != 0) { |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 273 | DEBUG(dbgs() << *splitInterval << "\n"); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 274 | added.push_back(splitInterval); |
| 275 | alreadySplit.insert(splitInterval); |
| 276 | if (earliestStart != 0) { |
| 277 | if (splitInterval->beginIndex() < *earliestStart) |
| 278 | *earliestStart = splitInterval->beginIndex(); |
| 279 | } |
| 280 | } else { |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 281 | DEBUG(dbgs() << "0\n"); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
David Greene | 65de504 | 2010-01-05 01:25:55 +0000 | [diff] [blame] | 285 | DEBUG(dbgs() << "Original LI: " << *li << "\n"); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 286 | |
| 287 | // If there original interval still contains some live ranges |
| 288 | // add it to added and alreadySplit. |
| 289 | if (!li->empty()) { |
| 290 | added.push_back(li); |
| 291 | alreadySplit.insert(li); |
| 292 | if (earliestStart != 0) { |
| 293 | if (li->beginIndex() < *earliestStart) |
| 294 | *earliestStart = li->beginIndex(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return added; |
| 299 | } |
| 300 | |
| 301 | /// Extract the given value number from the interval. |
| 302 | LiveInterval* extractVNI(LiveInterval *li, VNInfo *vni) const { |
| 303 | assert(vni->isDefAccurate() || vni->isPHIDef()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 304 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 305 | // Create a new vreg and live interval, copy VNI ranges over. |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 306 | const TargetRegisterClass *trc = mri->getRegClass(li->reg); |
| 307 | unsigned newVReg = mri->createVirtualRegister(trc); |
| 308 | vrm->grow(); |
| 309 | LiveInterval *newLI = &lis->getOrCreateInterval(newVReg); |
| 310 | VNInfo *newVNI = newLI->createValueCopy(vni, lis->getVNInfoAllocator()); |
| 311 | |
| 312 | // Start by copying all live ranges in the VN to the new interval. |
| 313 | for (LiveInterval::iterator rItr = li->begin(), rEnd = li->end(); |
| 314 | rItr != rEnd; ++rItr) { |
| 315 | if (rItr->valno == vni) { |
| 316 | newLI->addRange(LiveRange(rItr->start, rItr->end, newVNI)); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Erase the old VNI & ranges. |
| 321 | li->removeValNo(vni); |
| 322 | |
| 323 | // Collect all current uses of the register belonging to the given VNI. |
| 324 | // We'll use this to rename the register after we've dealt with the def. |
| 325 | std::set<MachineInstr*> uses; |
| 326 | for (MachineRegisterInfo::use_iterator |
| 327 | useItr = mri->use_begin(li->reg), useEnd = mri->use_end(); |
| 328 | useItr != useEnd; ++useItr) { |
| 329 | uses.insert(&*useItr); |
| 330 | } |
| 331 | |
| 332 | // Process the def instruction for this VNI. |
| 333 | if (newVNI->isPHIDef()) { |
| 334 | // Insert a copy at the start of the MBB. The range proceeding the |
| 335 | // copy will be attached to the original LiveInterval. |
| 336 | MachineBasicBlock *defMBB = lis->getMBBFromIndex(newVNI->def); |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 337 | tii->copyRegToReg(*defMBB, defMBB->begin(), newVReg, li->reg, trc, trc, |
| 338 | DebugLoc()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 339 | MachineInstr *copyMI = defMBB->begin(); |
| 340 | copyMI->addRegisterKilled(li->reg, tri); |
| 341 | SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI); |
| 342 | VNInfo *phiDefVNI = li->getNextValue(lis->getMBBStartIdx(defMBB), |
| 343 | 0, false, lis->getVNInfoAllocator()); |
| 344 | phiDefVNI->setIsPHIDef(true); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 345 | li->addRange(LiveRange(phiDefVNI->def, copyIdx.getDefIndex(), phiDefVNI)); |
| 346 | LiveRange *oldPHIDefRange = |
| 347 | newLI->getLiveRangeContaining(lis->getMBBStartIdx(defMBB)); |
| 348 | |
| 349 | // If the old phi def starts in the middle of the range chop it up. |
| 350 | if (oldPHIDefRange->start < lis->getMBBStartIdx(defMBB)) { |
| 351 | LiveRange oldPHIDefRange2(copyIdx.getDefIndex(), oldPHIDefRange->end, |
| 352 | oldPHIDefRange->valno); |
| 353 | oldPHIDefRange->end = lis->getMBBStartIdx(defMBB); |
| 354 | newLI->addRange(oldPHIDefRange2); |
| 355 | } else if (oldPHIDefRange->start == lis->getMBBStartIdx(defMBB)) { |
| 356 | // Otherwise if it's at the start of the range just trim it. |
| 357 | oldPHIDefRange->start = copyIdx.getDefIndex(); |
| 358 | } else { |
| 359 | assert(false && "PHI def range doesn't cover PHI def?"); |
| 360 | } |
| 361 | |
| 362 | newVNI->def = copyIdx.getDefIndex(); |
| 363 | newVNI->setCopy(copyMI); |
| 364 | newVNI->setIsPHIDef(false); // not a PHI def anymore. |
| 365 | newVNI->setIsDefAccurate(true); |
| 366 | } else { |
| 367 | // non-PHI def. Rename the def. If it's two-addr that means renaming the use |
| 368 | // and inserting a new copy too. |
| 369 | MachineInstr *defInst = lis->getInstructionFromIndex(newVNI->def); |
| 370 | // We'll rename this now, so we can remove it from uses. |
| 371 | uses.erase(defInst); |
| 372 | unsigned defOpIdx = defInst->findRegisterDefOperandIdx(li->reg); |
| 373 | bool isTwoAddr = defInst->isRegTiedToUseOperand(defOpIdx), |
| 374 | twoAddrUseIsUndef = false; |
| 375 | |
| 376 | for (unsigned i = 0; i < defInst->getNumOperands(); ++i) { |
| 377 | MachineOperand &mo = defInst->getOperand(i); |
| 378 | if (mo.isReg() && (mo.isDef() || isTwoAddr) && (mo.getReg()==li->reg)) { |
| 379 | mo.setReg(newVReg); |
| 380 | if (isTwoAddr && mo.isUse() && mo.isUndef()) |
| 381 | twoAddrUseIsUndef = true; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | SlotIndex defIdx = lis->getInstructionIndex(defInst); |
| 386 | newVNI->def = defIdx.getDefIndex(); |
| 387 | |
| 388 | if (isTwoAddr && !twoAddrUseIsUndef) { |
| 389 | MachineBasicBlock *defMBB = defInst->getParent(); |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 390 | tii->copyRegToReg(*defMBB, defInst, newVReg, li->reg, trc, trc, |
| 391 | DebugLoc()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 392 | MachineInstr *copyMI = prior(MachineBasicBlock::iterator(defInst)); |
| 393 | SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI); |
| 394 | copyMI->addRegisterKilled(li->reg, tri); |
| 395 | LiveRange *origUseRange = |
| 396 | li->getLiveRangeContaining(newVNI->def.getUseIndex()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 397 | origUseRange->end = copyIdx.getDefIndex(); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 398 | VNInfo *copyVNI = newLI->getNextValue(copyIdx.getDefIndex(), copyMI, |
| 399 | true, lis->getVNInfoAllocator()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 400 | LiveRange copyRange(copyIdx.getDefIndex(),defIdx.getDefIndex(),copyVNI); |
| 401 | newLI->addRange(copyRange); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | for (std::set<MachineInstr*>::iterator |
| 406 | usesItr = uses.begin(), usesEnd = uses.end(); |
| 407 | usesItr != usesEnd; ++usesItr) { |
| 408 | MachineInstr *useInst = *usesItr; |
| 409 | SlotIndex useIdx = lis->getInstructionIndex(useInst); |
| 410 | LiveRange *useRange = |
| 411 | newLI->getLiveRangeContaining(useIdx.getUseIndex()); |
| 412 | |
| 413 | // If this use doesn't belong to the new interval skip it. |
| 414 | if (useRange == 0) |
| 415 | continue; |
| 416 | |
| 417 | // This use doesn't belong to the VNI, skip it. |
| 418 | if (useRange->valno != newVNI) |
| 419 | continue; |
| 420 | |
| 421 | // Check if this instr is two address. |
| 422 | unsigned useOpIdx = useInst->findRegisterUseOperandIdx(li->reg); |
| 423 | bool isTwoAddress = useInst->isRegTiedToDefOperand(useOpIdx); |
| 424 | |
| 425 | // Rename uses (and defs for two-address instrs). |
| 426 | for (unsigned i = 0; i < useInst->getNumOperands(); ++i) { |
| 427 | MachineOperand &mo = useInst->getOperand(i); |
| 428 | if (mo.isReg() && (mo.isUse() || isTwoAddress) && |
| 429 | (mo.getReg() == li->reg)) { |
| 430 | mo.setReg(newVReg); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // If this is a two address instruction we've got some extra work to do. |
| 435 | if (isTwoAddress) { |
| 436 | // We modified the def operand, so we need to copy back to the original |
| 437 | // reg. |
| 438 | MachineBasicBlock *useMBB = useInst->getParent(); |
| 439 | MachineBasicBlock::iterator useItr(useInst); |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 440 | tii->copyRegToReg(*useMBB, llvm::next(useItr), li->reg, newVReg, trc, trc, |
Dan Gohman | 34dcc6f | 2010-05-06 20:33:48 +0000 | [diff] [blame] | 441 | DebugLoc()); |
Douglas Gregor | 7d9663c | 2010-05-11 06:17:44 +0000 | [diff] [blame] | 442 | MachineInstr *copyMI = llvm::next(useItr); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 443 | copyMI->addRegisterKilled(newVReg, tri); |
| 444 | SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI); |
| 445 | |
| 446 | // Change the old two-address defined range & vni to start at |
| 447 | // (and be defined by) the copy. |
| 448 | LiveRange *origDefRange = |
| 449 | li->getLiveRangeContaining(useIdx.getDefIndex()); |
| 450 | origDefRange->start = copyIdx.getDefIndex(); |
| 451 | origDefRange->valno->def = copyIdx.getDefIndex(); |
| 452 | origDefRange->valno->setCopy(copyMI); |
| 453 | |
| 454 | // Insert a new range & vni for the two-address-to-copy value. This |
| 455 | // will be attached to the new live interval. |
| 456 | VNInfo *copyVNI = |
| 457 | newLI->getNextValue(useIdx.getDefIndex(), 0, true, |
| 458 | lis->getVNInfoAllocator()); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 459 | LiveRange copyRange(useIdx.getDefIndex(),copyIdx.getDefIndex(),copyVNI); |
| 460 | newLI->addRange(copyRange); |
| 461 | } |
| 462 | } |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 463 | |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 464 | // Iterate over any PHI kills - we'll need to insert new copies for them. |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 465 | for (LiveInterval::iterator LRI = newLI->begin(), LRE = newLI->end(); |
| 466 | LRI != LRE; ++LRI) { |
| 467 | if (LRI->valno != newVNI || LRI->end.isPHI()) |
| 468 | continue; |
| 469 | SlotIndex killIdx = LRI->end; |
| 470 | MachineBasicBlock *killMBB = lis->getMBBFromIndex(killIdx); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 471 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 472 | tii->copyRegToReg(*killMBB, killMBB->getFirstTerminator(), |
| 473 | li->reg, newVReg, trc, trc, |
| 474 | DebugLoc()); |
| 475 | MachineInstr *copyMI = prior(killMBB->getFirstTerminator()); |
| 476 | copyMI->addRegisterKilled(newVReg, tri); |
| 477 | SlotIndex copyIdx = lis->InsertMachineInstrInMaps(copyMI); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 478 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 479 | // Save the current end. We may need it to add a new range if the |
| 480 | // current range runs of the end of the MBB. |
| 481 | SlotIndex newKillRangeEnd = LRI->end; |
| 482 | LRI->end = copyIdx.getDefIndex(); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 483 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 484 | if (newKillRangeEnd != lis->getMBBEndIdx(killMBB)) { |
| 485 | assert(newKillRangeEnd > lis->getMBBEndIdx(killMBB) && |
| 486 | "PHI kill range doesn't reach kill-block end. Not sane."); |
| 487 | newLI->addRange(LiveRange(lis->getMBBEndIdx(killMBB), |
| 488 | newKillRangeEnd, newVNI)); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jakob Stoklund Olesen | 15a5714 | 2010-06-25 22:53:05 +0000 | [diff] [blame] | 491 | VNInfo *newKillVNI = li->getNextValue(copyIdx.getDefIndex(), |
| 492 | copyMI, true, |
| 493 | lis->getVNInfoAllocator()); |
| 494 | newKillVNI->setHasPHIKill(true); |
| 495 | li->addRange(LiveRange(copyIdx.getDefIndex(), |
| 496 | lis->getMBBEndIdx(killMBB), |
| 497 | newKillVNI)); |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 498 | } |
Lang Hames | 6194569 | 2009-12-09 05:39:12 +0000 | [diff] [blame] | 499 | newVNI->setHasPHIKill(false); |
| 500 | |
| 501 | return newLI; |
| 502 | } |
| 503 | |
| 504 | }; |
| 505 | |
Chris Lattner | 1ca6531 | 2010-04-07 22:44:07 +0000 | [diff] [blame] | 506 | } // end anonymous namespace |
| 507 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 508 | |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 509 | llvm::Spiller* llvm::createSpiller(MachineFunction *mf, LiveIntervals *lis, |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 510 | const MachineLoopInfo *loopInfo, |
| 511 | VirtRegMap *vrm) { |
| 512 | switch (spillerOpt) { |
Chris Lattner | 1ca6531 | 2010-04-07 22:44:07 +0000 | [diff] [blame] | 513 | default: assert(0 && "unknown spiller"); |
| 514 | case trivial: return new TrivialSpiller(mf, lis, vrm); |
| 515 | case standard: return new StandardSpiller(lis, loopInfo, vrm); |
| 516 | case splitting: return new SplittingSpiller(mf, lis, loopInfo, vrm); |
Lang Hames | 835ca07 | 2009-11-19 04:15:33 +0000 | [diff] [blame] | 517 | } |
Lang Hames | e2b201b | 2009-05-18 19:03:16 +0000 | [diff] [blame] | 518 | } |