Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1 | //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===// |
| 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 | // The inline spiller modifies the machine function directly instead of |
| 11 | // inserting spills and restores in VirtRegMap. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "spiller" |
| 16 | #include "Spiller.h" |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 17 | #include "LiveRangeEdit.h" |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 18 | #include "SplitKit.h" |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 19 | #include "VirtRegMap.h" |
| 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | 0a12b80 | 2010-10-26 00:11:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
Jakob Stoklund Olesen | 9529a1c | 2010-07-19 18:41:20 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | class InlineSpiller : public Spiller { |
Jakob Stoklund Olesen | 6d108e2 | 2010-08-06 18:47:06 +0000 | [diff] [blame] | 35 | MachineFunctionPass &pass_; |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 36 | MachineFunction &mf_; |
| 37 | LiveIntervals &lis_; |
Jakob Stoklund Olesen | 0a12b80 | 2010-10-26 00:11:35 +0000 | [diff] [blame] | 38 | LiveStacks &lss_; |
Jakob Stoklund Olesen | 9529a1c | 2010-07-19 18:41:20 +0000 | [diff] [blame] | 39 | MachineLoopInfo &loops_; |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 40 | VirtRegMap &vrm_; |
| 41 | MachineFrameInfo &mfi_; |
| 42 | MachineRegisterInfo &mri_; |
| 43 | const TargetInstrInfo &tii_; |
| 44 | const TargetRegisterInfo &tri_; |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 45 | const BitVector reserved_; |
| 46 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 47 | SplitAnalysis splitAnalysis_; |
| 48 | |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 49 | // Variables that are valid during spill(), but used by multiple methods. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 50 | LiveRangeEdit *edit_; |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 51 | const TargetRegisterClass *rc_; |
| 52 | int stackSlot_; |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 53 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 54 | // Values that failed to remat at some point. |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 55 | SmallPtrSet<VNInfo*, 8> usedValues_; |
| 56 | |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 57 | ~InlineSpiller() {} |
| 58 | |
| 59 | public: |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 60 | InlineSpiller(MachineFunctionPass &pass, |
| 61 | MachineFunction &mf, |
| 62 | VirtRegMap &vrm) |
Jakob Stoklund Olesen | 6d108e2 | 2010-08-06 18:47:06 +0000 | [diff] [blame] | 63 | : pass_(pass), |
| 64 | mf_(mf), |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 65 | lis_(pass.getAnalysis<LiveIntervals>()), |
Jakob Stoklund Olesen | 0a12b80 | 2010-10-26 00:11:35 +0000 | [diff] [blame] | 66 | lss_(pass.getAnalysis<LiveStacks>()), |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 67 | loops_(pass.getAnalysis<MachineLoopInfo>()), |
| 68 | vrm_(vrm), |
| 69 | mfi_(*mf.getFrameInfo()), |
| 70 | mri_(mf.getRegInfo()), |
| 71 | tii_(*mf.getTarget().getInstrInfo()), |
| 72 | tri_(*mf.getTarget().getRegisterInfo()), |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 73 | reserved_(tri_.getReservedRegs(mf_)), |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 74 | splitAnalysis_(mf, lis_, loops_) {} |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 75 | |
| 76 | void spill(LiveInterval *li, |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 77 | SmallVectorImpl<LiveInterval*> &newIntervals, |
| 78 | SmallVectorImpl<LiveInterval*> &spillIs); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 79 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 80 | void spill(LiveRangeEdit &); |
| 81 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 82 | private: |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 83 | bool split(); |
| 84 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 85 | bool reMaterializeFor(MachineBasicBlock::iterator MI); |
| 86 | void reMaterializeAll(); |
| 87 | |
Jakob Stoklund Olesen | 1a0f91b | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 88 | bool coalesceStackAccess(MachineInstr *MI); |
Jakob Stoklund Olesen | e72a5c5 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 89 | bool foldMemoryOperand(MachineBasicBlock::iterator MI, |
| 90 | const SmallVectorImpl<unsigned> &Ops); |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 91 | void insertReload(LiveInterval &NewLI, MachineBasicBlock::iterator MI); |
| 92 | void insertSpill(LiveInterval &NewLI, MachineBasicBlock::iterator MI); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 93 | }; |
| 94 | } |
| 95 | |
| 96 | namespace llvm { |
Jakob Stoklund Olesen | f2c6e36 | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 97 | Spiller *createInlineSpiller(MachineFunctionPass &pass, |
| 98 | MachineFunction &mf, |
| 99 | VirtRegMap &vrm) { |
| 100 | return new InlineSpiller(pass, mf, vrm); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 104 | /// split - try splitting the current interval into pieces that may allocate |
| 105 | /// separately. Return true if successful. |
| 106 | bool InlineSpiller::split() { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 107 | splitAnalysis_.analyze(&edit_->getParent()); |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 108 | |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 109 | // Try splitting around loops. |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 110 | if (const MachineLoop *loop = splitAnalysis_.getBestSplitLoop()) { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 111 | SplitEditor(splitAnalysis_, lis_, vrm_, *edit_) |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 112 | .splitAroundLoop(loop); |
| 113 | return true; |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 114 | } |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 115 | |
| 116 | // Try splitting into single block intervals. |
| 117 | SplitAnalysis::BlockPtrSet blocks; |
| 118 | if (splitAnalysis_.getMultiUseBlocks(blocks)) { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 119 | SplitEditor(splitAnalysis_, lis_, vrm_, *edit_) |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 120 | .splitSingleBlocks(blocks); |
| 121 | return true; |
Jakob Stoklund Olesen | f1b05f2 | 2010-08-12 17:07:14 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 124 | // Try splitting inside a basic block. |
| 125 | if (const MachineBasicBlock *MBB = splitAnalysis_.getBlockForInsideSplit()) { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 126 | SplitEditor(splitAnalysis_, lis_, vrm_, *edit_) |
Jakob Stoklund Olesen | 57d0f2d | 2010-10-05 22:19:33 +0000 | [diff] [blame] | 127 | .splitInsideBlock(MBB); |
| 128 | return true; |
Jakob Stoklund Olesen | fc412d8 | 2010-08-13 21:18:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 134 | /// reMaterializeFor - Attempt to rematerialize edit_->getReg() before MI instead of |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 135 | /// reloading it. |
| 136 | bool InlineSpiller::reMaterializeFor(MachineBasicBlock::iterator MI) { |
| 137 | SlotIndex UseIdx = lis_.getInstructionIndex(MI).getUseIndex(); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 138 | VNInfo *OrigVNI = edit_->getParent().getVNInfoAt(UseIdx); |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 139 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 140 | if (!OrigVNI) { |
| 141 | DEBUG(dbgs() << "\tadding <undef> flags: "); |
| 142 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 143 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 144 | if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg()) |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 145 | MO.setIsUndef(); |
| 146 | } |
| 147 | DEBUG(dbgs() << UseIdx << '\t' << *MI); |
| 148 | return true; |
| 149 | } |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 150 | |
| 151 | LiveRangeEdit::Remat RM = edit_->canRematerializeAt(OrigVNI, UseIdx, false, |
| 152 | lis_); |
| 153 | if (!RM) { |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 154 | usedValues_.insert(OrigVNI); |
| 155 | DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI); |
| 156 | return false; |
| 157 | } |
| 158 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 159 | // If the instruction also writes edit_->getReg(), it had better not require |
| 160 | // the same register for uses and defs. |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 161 | bool Reads, Writes; |
| 162 | SmallVector<unsigned, 8> Ops; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 163 | tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit_->getReg(), &Ops); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 164 | if (Writes) { |
| 165 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 166 | MachineOperand &MO = MI->getOperand(Ops[i]); |
| 167 | if (MO.isUse() ? MI->isRegTiedToDefOperand(Ops[i]) : MO.getSubReg()) { |
| 168 | usedValues_.insert(OrigVNI); |
| 169 | DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI); |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Alocate a new register for the remat. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 176 | LiveInterval &NewLI = edit_->create(mri_, lis_, vrm_); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 177 | NewLI.markNotSpillable(); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 178 | |
| 179 | // Finally we can rematerialize OrigMI before MI. |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 180 | SlotIndex DefIdx = edit_->rematerializeAt(*MI->getParent(), MI, NewLI.reg, RM, |
| 181 | lis_, tii_, tri_); |
| 182 | DEBUG(dbgs() << "\tremat: " << DefIdx << '\n'); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 183 | |
| 184 | // Replace operands |
| 185 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 186 | MachineOperand &MO = MI->getOperand(Ops[i]); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 187 | if (MO.isReg() && MO.isUse() && MO.getReg() == edit_->getReg()) { |
| 188 | MO.setReg(NewLI.reg); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 189 | MO.setIsKill(); |
| 190 | } |
| 191 | } |
| 192 | DEBUG(dbgs() << "\t " << UseIdx << '\t' << *MI); |
| 193 | |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 194 | VNInfo *DefVNI = NewLI.getNextValue(DefIdx, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 195 | NewLI.addRange(LiveRange(DefIdx, UseIdx.getDefIndex(), DefVNI)); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 196 | DEBUG(dbgs() << "\tinterval: " << NewLI << '\n'); |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 200 | /// reMaterializeAll - Try to rematerialize as many uses as possible, |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 201 | /// and trim the live ranges after. |
| 202 | void InlineSpiller::reMaterializeAll() { |
| 203 | // Do a quick scan of the interval values to find if any are remattable. |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 204 | if (!edit_->anyRematerializable(lis_, tii_, 0)) |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 205 | return; |
| 206 | |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 207 | usedValues_.clear(); |
| 208 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 209 | // Try to remat before all uses of edit_->getReg(). |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 210 | bool anyRemat = false; |
| 211 | for (MachineRegisterInfo::use_nodbg_iterator |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 212 | RI = mri_.use_nodbg_begin(edit_->getReg()); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 213 | MachineInstr *MI = RI.skipInstruction();) |
| 214 | anyRemat |= reMaterializeFor(MI); |
| 215 | |
| 216 | if (!anyRemat) |
| 217 | return; |
| 218 | |
| 219 | // Remove any values that were completely rematted. |
| 220 | bool anyRemoved = false; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 221 | for (LiveInterval::vni_iterator I = edit_->getParent().vni_begin(), |
| 222 | E = edit_->getParent().vni_end(); I != E; ++I) { |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 223 | VNInfo *VNI = *I; |
Jakob Stoklund Olesen | 080c316 | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 224 | if (VNI->hasPHIKill() || !edit_->didRematerialize(VNI) || |
| 225 | usedValues_.count(VNI)) |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 226 | continue; |
| 227 | MachineInstr *DefMI = lis_.getInstructionFromIndex(VNI->def); |
| 228 | DEBUG(dbgs() << "\tremoving dead def: " << VNI->def << '\t' << *DefMI); |
| 229 | lis_.RemoveMachineInstrFromMaps(DefMI); |
| 230 | vrm_.RemoveMachineInstrFromMaps(DefMI); |
| 231 | DefMI->eraseFromParent(); |
Lang Hames | cec2945 | 2010-09-26 03:37:09 +0000 | [diff] [blame] | 232 | VNI->def = SlotIndex(); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 233 | anyRemoved = true; |
| 234 | } |
| 235 | |
| 236 | if (!anyRemoved) |
| 237 | return; |
| 238 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 239 | // Removing values may cause debug uses where parent is not live. |
| 240 | for (MachineRegisterInfo::use_iterator RI = mri_.use_begin(edit_->getReg()); |
Jakob Stoklund Olesen | 3b9c7eb | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 241 | MachineInstr *MI = RI.skipInstruction();) { |
| 242 | if (!MI->isDebugValue()) |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 243 | continue; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 244 | // Try to preserve the debug value if parent is live immediately after it. |
Jakob Stoklund Olesen | 3b9c7eb | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 245 | MachineBasicBlock::iterator NextMI = MI; |
| 246 | ++NextMI; |
| 247 | if (NextMI != MI->getParent()->end() && !lis_.isNotInMIMap(NextMI)) { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 248 | SlotIndex Idx = lis_.getInstructionIndex(NextMI); |
| 249 | VNInfo *VNI = edit_->getParent().getVNInfoAt(Idx); |
Jakob Stoklund Olesen | b67b12e | 2010-08-10 20:45:07 +0000 | [diff] [blame] | 250 | if (VNI && (VNI->hasPHIKill() || usedValues_.count(VNI))) |
Jakob Stoklund Olesen | 3b9c7eb | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | DEBUG(dbgs() << "Removing debug info due to remat:" << "\t" << *MI); |
Jakob Stoklund Olesen | 3b9c7eb | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 254 | MI->eraseFromParent(); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Jakob Stoklund Olesen | 1a0f91b | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 258 | /// If MI is a load or store of stackSlot_, it can be removed. |
| 259 | bool InlineSpiller::coalesceStackAccess(MachineInstr *MI) { |
| 260 | int FI = 0; |
| 261 | unsigned reg; |
| 262 | if (!(reg = tii_.isLoadFromStackSlot(MI, FI)) && |
| 263 | !(reg = tii_.isStoreToStackSlot(MI, FI))) |
| 264 | return false; |
| 265 | |
| 266 | // We have a stack access. Is it the right register and slot? |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 267 | if (reg != edit_->getReg() || FI != stackSlot_) |
Jakob Stoklund Olesen | 1a0f91b | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 268 | return false; |
| 269 | |
| 270 | DEBUG(dbgs() << "Coalescing stack access: " << *MI); |
| 271 | lis_.RemoveMachineInstrFromMaps(MI); |
| 272 | MI->eraseFromParent(); |
| 273 | return true; |
| 274 | } |
| 275 | |
Jakob Stoklund Olesen | e72a5c5 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 276 | /// foldMemoryOperand - Try folding stack slot references in Ops into MI. |
| 277 | /// Return true on success, and MI will be erased. |
| 278 | bool InlineSpiller::foldMemoryOperand(MachineBasicBlock::iterator MI, |
| 279 | const SmallVectorImpl<unsigned> &Ops) { |
| 280 | // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied |
| 281 | // operands. |
| 282 | SmallVector<unsigned, 8> FoldOps; |
| 283 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 284 | unsigned Idx = Ops[i]; |
| 285 | MachineOperand &MO = MI->getOperand(Idx); |
| 286 | if (MO.isImplicit()) |
| 287 | continue; |
| 288 | // FIXME: Teach targets to deal with subregs. |
| 289 | if (MO.getSubReg()) |
| 290 | return false; |
| 291 | // Tied use operands should not be passed to foldMemoryOperand. |
| 292 | if (!MI->isRegTiedToDefOperand(Idx)) |
| 293 | FoldOps.push_back(Idx); |
| 294 | } |
| 295 | |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 296 | MachineInstr *FoldMI = tii_.foldMemoryOperand(MI, FoldOps, stackSlot_); |
Jakob Stoklund Olesen | e72a5c5 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 297 | if (!FoldMI) |
| 298 | return false; |
Jakob Stoklund Olesen | e72a5c5 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 299 | lis_.ReplaceMachineInstrInMaps(MI, FoldMI); |
| 300 | vrm_.addSpillSlotUse(stackSlot_, FoldMI); |
Jakob Stoklund Olesen | e05442d | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 301 | MI->eraseFromParent(); |
Jakob Stoklund Olesen | e72a5c5 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 302 | DEBUG(dbgs() << "\tfolded: " << *FoldMI); |
| 303 | return true; |
| 304 | } |
| 305 | |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 306 | /// insertReload - Insert a reload of NewLI.reg before MI. |
| 307 | void InlineSpiller::insertReload(LiveInterval &NewLI, |
| 308 | MachineBasicBlock::iterator MI) { |
| 309 | MachineBasicBlock &MBB = *MI->getParent(); |
| 310 | SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex(); |
| 311 | tii_.loadRegFromStackSlot(MBB, MI, NewLI.reg, stackSlot_, rc_, &tri_); |
| 312 | --MI; // Point to load instruction. |
| 313 | SlotIndex LoadIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 314 | vrm_.addSpillSlotUse(stackSlot_, MI); |
| 315 | DEBUG(dbgs() << "\treload: " << LoadIdx << '\t' << *MI); |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 316 | VNInfo *LoadVNI = NewLI.getNextValue(LoadIdx, 0, |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 317 | lis_.getVNInfoAllocator()); |
| 318 | NewLI.addRange(LiveRange(LoadIdx, Idx, LoadVNI)); |
| 319 | } |
| 320 | |
| 321 | /// insertSpill - Insert a spill of NewLI.reg after MI. |
| 322 | void InlineSpiller::insertSpill(LiveInterval &NewLI, |
| 323 | MachineBasicBlock::iterator MI) { |
| 324 | MachineBasicBlock &MBB = *MI->getParent(); |
| 325 | SlotIndex Idx = lis_.getInstructionIndex(MI).getDefIndex(); |
| 326 | tii_.storeRegToStackSlot(MBB, ++MI, NewLI.reg, true, stackSlot_, rc_, &tri_); |
| 327 | --MI; // Point to store instruction. |
| 328 | SlotIndex StoreIdx = lis_.InsertMachineInstrInMaps(MI).getDefIndex(); |
| 329 | vrm_.addSpillSlotUse(stackSlot_, MI); |
| 330 | DEBUG(dbgs() << "\tspilled: " << StoreIdx << '\t' << *MI); |
Lang Hames | 6e2968c | 2010-09-25 12:04:16 +0000 | [diff] [blame] | 331 | VNInfo *StoreVNI = NewLI.getNextValue(Idx, 0, lis_.getVNInfoAllocator()); |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 332 | NewLI.addRange(LiveRange(Idx, StoreIdx, StoreVNI)); |
| 333 | } |
| 334 | |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 335 | void InlineSpiller::spill(LiveInterval *li, |
Jakob Stoklund Olesen | 0a2b2a1 | 2010-08-13 22:56:53 +0000 | [diff] [blame] | 336 | SmallVectorImpl<LiveInterval*> &newIntervals, |
| 337 | SmallVectorImpl<LiveInterval*> &spillIs) { |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 338 | LiveRangeEdit edit(*li, newIntervals, spillIs); |
| 339 | spill(edit); |
| 340 | } |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 341 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 342 | void InlineSpiller::spill(LiveRangeEdit &edit) { |
| 343 | edit_ = &edit; |
| 344 | DEBUG(dbgs() << "Inline spilling " << edit.getParent() << "\n"); |
| 345 | assert(edit.getParent().isSpillable() && |
| 346 | "Attempting to spill already spilled value."); |
| 347 | assert(!edit.getParent().isStackSlot() && "Trying to spill a stack slot."); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 348 | |
Jakob Stoklund Olesen | 8ae0263 | 2010-07-20 15:41:07 +0000 | [diff] [blame] | 349 | if (split()) |
| 350 | return; |
| 351 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 352 | reMaterializeAll(); |
| 353 | |
| 354 | // Remat may handle everything. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 355 | if (edit_->getParent().empty()) |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 356 | return; |
| 357 | |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 358 | rc_ = mri_.getRegClass(edit.getReg()); |
Jakob Stoklund Olesen | 2a0180f | 2010-10-15 00:16:55 +0000 | [diff] [blame] | 359 | stackSlot_ = edit.assignStackSlot(vrm_); |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 360 | |
Jakob Stoklund Olesen | 0a12b80 | 2010-10-26 00:11:35 +0000 | [diff] [blame] | 361 | // Update LiveStacks now that we are committed to spilling. |
| 362 | LiveInterval &stacklvr = lss_.getOrCreateInterval(stackSlot_, rc_); |
| 363 | if (!stacklvr.hasAtLeastOneValue()) |
| 364 | stacklvr.getNextValue(SlotIndex(), 0, lss_.getVNInfoAllocator()); |
| 365 | stacklvr.MergeRangesInAsValue(edit_->getParent(), stacklvr.getValNumInfo(0)); |
| 366 | |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 367 | // Iterate over instructions using register. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 368 | for (MachineRegisterInfo::reg_iterator RI = mri_.reg_begin(edit.getReg()); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 369 | MachineInstr *MI = RI.skipInstruction();) { |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 370 | |
Jakob Stoklund Olesen | 3b9c7eb | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 371 | // Debug values are not allowed to affect codegen. |
| 372 | if (MI->isDebugValue()) { |
| 373 | // Modify DBG_VALUE now that the value is in a spill slot. |
| 374 | uint64_t Offset = MI->getOperand(1).getImm(); |
| 375 | const MDNode *MDPtr = MI->getOperand(2).getMetadata(); |
| 376 | DebugLoc DL = MI->getDebugLoc(); |
| 377 | if (MachineInstr *NewDV = tii_.emitFrameIndexDebugValue(mf_, stackSlot_, |
| 378 | Offset, MDPtr, DL)) { |
| 379 | DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI); |
| 380 | MachineBasicBlock *MBB = MI->getParent(); |
| 381 | MBB->insert(MBB->erase(MI), NewDV); |
| 382 | } else { |
| 383 | DEBUG(dbgs() << "Removing debug info due to spill:" << "\t" << *MI); |
| 384 | MI->eraseFromParent(); |
| 385 | } |
| 386 | continue; |
| 387 | } |
| 388 | |
Jakob Stoklund Olesen | 1a0f91b | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 389 | // Stack slot accesses may coalesce away. |
| 390 | if (coalesceStackAccess(MI)) |
| 391 | continue; |
| 392 | |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 393 | // Analyze instruction. |
| 394 | bool Reads, Writes; |
| 395 | SmallVector<unsigned, 8> Ops; |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 396 | tie(Reads, Writes) = MI->readsWritesVirtualRegister(edit.getReg(), &Ops); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 397 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 398 | // Attempt to fold memory ops. |
| 399 | if (foldMemoryOperand(MI, Ops)) |
| 400 | continue; |
| 401 | |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 402 | // Allocate interval around instruction. |
| 403 | // FIXME: Infer regclass from instruction alone. |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 404 | LiveInterval &NewLI = edit.create(mri_, lis_, vrm_); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 405 | NewLI.markNotSpillable(); |
| 406 | |
Jakob Stoklund Olesen | 8de3b1e | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 407 | if (Reads) |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 408 | insertReload(NewLI, MI); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 409 | |
| 410 | // Rewrite instruction operands. |
| 411 | bool hasLiveDef = false; |
| 412 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 413 | MachineOperand &MO = MI->getOperand(Ops[i]); |
Jakob Stoklund Olesen | a17768f | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 414 | MO.setReg(NewLI.reg); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 415 | if (MO.isUse()) { |
| 416 | if (!MI->isRegTiedToDefOperand(Ops[i])) |
| 417 | MO.setIsKill(); |
| 418 | } else { |
| 419 | if (!MO.isDead()) |
| 420 | hasLiveDef = true; |
| 421 | } |
| 422 | } |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 423 | |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 424 | // FIXME: Use a second vreg if instruction has no tied ops. |
Jakob Stoklund Olesen | 9e55afb | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 425 | if (Writes && hasLiveDef) |
| 426 | insertSpill(NewLI, MI); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 427 | |
| 428 | DEBUG(dbgs() << "\tinterval: " << NewLI << '\n'); |
Jakob Stoklund Olesen | 914f2ff | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 429 | } |
| 430 | } |