| Jakob Stoklund Olesen | f888911 | 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 |  | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 15 | #include "Spiller.h" | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/TinyPtrVector.h" | 
| Jakob Stoklund Olesen | 868dd4e | 2010-11-10 23:55:56 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" | 
| Pete Cooper | 3ca96f9 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveRangeEdit.h" | 
| Jakob Stoklund Olesen | e2c340c | 2010-10-26 00:11:35 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveStackAnalysis.h" | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | 
| Manman Ren | c935560 | 2014-03-21 21:46:24 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" | 
| Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" | 
|  | 27 | #include "llvm/CodeGen/MachineFunction.h" | 
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBundle.h" | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/VirtRegMap.h" | 
| Jakob Stoklund Olesen | bceb9e5 | 2011-09-15 21:06:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" | 
|  | 35 | #include "llvm/Support/raw_ostream.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetInstrInfo.h" | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 37 |  | 
|  | 38 | using namespace llvm; | 
|  | 39 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "regalloc" | 
|  | 41 |  | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 42 | STATISTIC(NumSpilledRanges,   "Number of spilled live ranges"); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 43 | STATISTIC(NumSnippets,        "Number of spilled snippets"); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 44 | STATISTIC(NumSpills,          "Number of spills inserted"); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 45 | STATISTIC(NumSpillsRemoved,   "Number of spills removed"); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 46 | STATISTIC(NumReloads,         "Number of reloads inserted"); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 47 | STATISTIC(NumReloadsRemoved,  "Number of reloads removed"); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 48 | STATISTIC(NumFolded,          "Number of folded stack accesses"); | 
|  | 49 | STATISTIC(NumFoldedLoads,     "Number of folded loads"); | 
|  | 50 | STATISTIC(NumRemats,          "Number of rematerialized defs for spilling"); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 51 | STATISTIC(NumOmitReloadSpill, "Number of omitted spills of reloads"); | 
|  | 52 | STATISTIC(NumHoists,          "Number of hoisted spills"); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 53 |  | 
| Jakob Stoklund Olesen | bceb9e5 | 2011-09-15 21:06:00 +0000 | [diff] [blame] | 54 | static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden, | 
|  | 55 | cl::desc("Disable inline spill hoisting")); | 
|  | 56 |  | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 57 | namespace { | 
|  | 58 | class InlineSpiller : public Spiller { | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 59 | MachineFunction &MF; | 
|  | 60 | LiveIntervals &LIS; | 
|  | 61 | LiveStacks &LSS; | 
|  | 62 | AliasAnalysis *AA; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 63 | MachineDominatorTree &MDT; | 
|  | 64 | MachineLoopInfo &Loops; | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 65 | VirtRegMap &VRM; | 
|  | 66 | MachineFrameInfo &MFI; | 
|  | 67 | MachineRegisterInfo &MRI; | 
|  | 68 | const TargetInstrInfo &TII; | 
|  | 69 | const TargetRegisterInfo &TRI; | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 70 | const MachineBlockFrequencyInfo &MBFI; | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | // Variables that are valid during spill(), but used by multiple methods. | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 73 | LiveRangeEdit *Edit; | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 74 | LiveInterval *StackInt; | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 75 | int StackSlot; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 76 | unsigned Original; | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 77 |  | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 78 | // All registers to spill to StackSlot, including the main register. | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 79 | SmallVector<unsigned, 8> RegsToSpill; | 
|  | 80 |  | 
|  | 81 | // All COPY instructions to/from snippets. | 
|  | 82 | // They are ignored since both operands refer to the same stack slot. | 
|  | 83 | SmallPtrSet<MachineInstr*, 8> SnippetCopies; | 
|  | 84 |  | 
| Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 85 | // Values that failed to remat at some point. | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 86 | SmallPtrSet<VNInfo*, 8> UsedValues; | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 87 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 88 | public: | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 89 | // Information about a value that was defined by a copy from a sibling | 
|  | 90 | // register. | 
|  | 91 | struct SibValueInfo { | 
|  | 92 | // True when all reaching defs were reloads: No spill is necessary. | 
|  | 93 | bool AllDefsAreReloads; | 
|  | 94 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 95 | // True when value is defined by an original PHI not from splitting. | 
|  | 96 | bool DefByOrigPHI; | 
|  | 97 |  | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 98 | // True when the COPY defining this value killed its source. | 
|  | 99 | bool KillsSource; | 
|  | 100 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 101 | // The preferred register to spill. | 
|  | 102 | unsigned SpillReg; | 
|  | 103 |  | 
|  | 104 | // The value of SpillReg that should be spilled. | 
|  | 105 | VNInfo *SpillVNI; | 
|  | 106 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 107 | // The block where SpillVNI should be spilled. Currently, this must be the | 
|  | 108 | // block containing SpillVNI->def. | 
|  | 109 | MachineBasicBlock *SpillMBB; | 
|  | 110 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 111 | // A defining instruction that is not a sibling copy or a reload, or NULL. | 
|  | 112 | // This can be used as a template for rematerialization. | 
|  | 113 | MachineInstr *DefMI; | 
|  | 114 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 115 | // List of values that depend on this one.  These values are actually the | 
|  | 116 | // same, but live range splitting has placed them in different registers, | 
|  | 117 | // or SSA update needed to insert PHI-defs to preserve SSA form.  This is | 
|  | 118 | // copies of the current value and phi-kills.  Usually only phi-kills cause | 
|  | 119 | // more than one dependent value. | 
|  | 120 | TinyPtrVector<VNInfo*> Deps; | 
|  | 121 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 122 | SibValueInfo(unsigned Reg, VNInfo *VNI) | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 123 | : AllDefsAreReloads(true), DefByOrigPHI(false), KillsSource(false), | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 124 | SpillReg(Reg), SpillVNI(VNI), SpillMBB(nullptr), DefMI(nullptr) {} | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | // Returns true when a def has been found. | 
|  | 127 | bool hasDef() const { return DefByOrigPHI || DefMI; } | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 128 | }; | 
|  | 129 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 130 | private: | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 131 | // Values in RegsToSpill defined by sibling copies. | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 132 | typedef DenseMap<VNInfo*, SibValueInfo> SibValueMap; | 
|  | 133 | SibValueMap SibValues; | 
|  | 134 |  | 
|  | 135 | // Dead defs generated during spilling. | 
|  | 136 | SmallVector<MachineInstr*, 8> DeadDefs; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 137 |  | 
| Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 138 | ~InlineSpiller() override {} | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 139 |  | 
|  | 140 | public: | 
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 141 | InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm) | 
|  | 142 | : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()), | 
|  | 143 | LSS(pass.getAnalysis<LiveStacks>()), | 
|  | 144 | AA(&pass.getAnalysis<AliasAnalysis>()), | 
|  | 145 | MDT(pass.getAnalysis<MachineDominatorTree>()), | 
|  | 146 | Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm), | 
|  | 147 | MFI(*mf.getFrameInfo()), MRI(mf.getRegInfo()), | 
| Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 148 | TII(*mf.getSubtarget().getInstrInfo()), | 
|  | 149 | TRI(*mf.getSubtarget().getRegisterInfo()), | 
| Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 150 | MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()) {} | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 151 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 152 | void spill(LiveRangeEdit &) override; | 
| Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 153 |  | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 154 | private: | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 155 | bool isSnippet(const LiveInterval &SnipLI); | 
|  | 156 | void collectRegsToSpill(); | 
|  | 157 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 158 | bool isRegToSpill(unsigned Reg) { | 
|  | 159 | return std::find(RegsToSpill.begin(), | 
|  | 160 | RegsToSpill.end(), Reg) != RegsToSpill.end(); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | bool isSibling(unsigned Reg); | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 164 | MachineInstr *traceSiblingValue(unsigned, VNInfo*, VNInfo*); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 165 | void propagateSiblingValue(SibValueMap::iterator, VNInfo *VNI = nullptr); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 166 | void analyzeSiblingValues(); | 
|  | 167 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 168 | bool hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI); | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 169 | void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 170 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 171 | void markValueUsed(LiveInterval*, VNInfo*); | 
|  | 172 | bool reMaterializeFor(LiveInterval&, MachineBasicBlock::iterator MI); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 173 | void reMaterializeAll(); | 
|  | 174 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 175 | bool coalesceStackAccess(MachineInstr *MI, unsigned Reg); | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 176 | bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >, | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 177 | MachineInstr *LoadMI = nullptr); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 178 | void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI); | 
|  | 179 | void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 180 |  | 
|  | 181 | void spillAroundUses(unsigned Reg); | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 182 | void spillAll(); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 183 | }; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | namespace llvm { | 
| Lang Hames | cdd9077 | 2014-11-06 19:12:38 +0000 | [diff] [blame] | 187 |  | 
|  | 188 | Spiller::~Spiller() { } | 
|  | 189 | void Spiller::anchor() { } | 
|  | 190 |  | 
| Jakob Stoklund Olesen | 0fef9dd | 2010-07-20 23:50:15 +0000 | [diff] [blame] | 191 | Spiller *createInlineSpiller(MachineFunctionPass &pass, | 
|  | 192 | MachineFunction &mf, | 
|  | 193 | VirtRegMap &vrm) { | 
|  | 194 | return new InlineSpiller(pass, mf, vrm); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 195 | } | 
| Lang Hames | cdd9077 | 2014-11-06 19:12:38 +0000 | [diff] [blame] | 196 |  | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// | 
|  | 200 | //                                Snippets | 
|  | 201 | //===----------------------------------------------------------------------===// | 
|  | 202 |  | 
|  | 203 | // When spilling a virtual register, we also spill any snippets it is connected | 
|  | 204 | // to. The snippets are small live ranges that only have a single real use, | 
|  | 205 | // leftovers from live range splitting. Spilling them enables memory operand | 
|  | 206 | // folding or tightens the live range around the single use. | 
|  | 207 | // | 
|  | 208 | // This minimizes register pressure and maximizes the store-to-load distance for | 
|  | 209 | // spill slots which can be important in tight loops. | 
|  | 210 |  | 
|  | 211 | /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register, | 
|  | 212 | /// otherwise return 0. | 
|  | 213 | static unsigned isFullCopyOf(const MachineInstr *MI, unsigned Reg) { | 
| Rafael Espindola | 070f96c | 2011-06-30 21:15:52 +0000 | [diff] [blame] | 214 | if (!MI->isFullCopy()) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 215 | return 0; | 
|  | 216 | if (MI->getOperand(0).getReg() == Reg) | 
|  | 217 | return MI->getOperand(1).getReg(); | 
|  | 218 | if (MI->getOperand(1).getReg() == Reg) | 
|  | 219 | return MI->getOperand(0).getReg(); | 
|  | 220 | return 0; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | /// isSnippet - Identify if a live interval is a snippet that should be spilled. | 
|  | 224 | /// It is assumed that SnipLI is a virtual register with the same original as | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 225 | /// Edit->getReg(). | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 226 | bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) { | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 227 | unsigned Reg = Edit->getReg(); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 228 |  | 
|  | 229 | // A snippet is a tiny live range with only a single instruction using it | 
|  | 230 | // besides copies to/from Reg or spills/fills. We accept: | 
|  | 231 | // | 
|  | 232 | //   %snip = COPY %Reg / FILL fi# | 
|  | 233 | //   %snip = USE %snip | 
|  | 234 | //   %Reg = COPY %snip / SPILL %snip, fi# | 
|  | 235 | // | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 236 | if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI)) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 237 | return false; | 
|  | 238 |  | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 239 | MachineInstr *UseMI = nullptr; | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 240 |  | 
|  | 241 | // Check that all uses satisfy our criteria. | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 242 | for (MachineRegisterInfo::reg_instr_nodbg_iterator | 
|  | 243 | RI = MRI.reg_instr_nodbg_begin(SnipLI.reg), | 
|  | 244 | E = MRI.reg_instr_nodbg_end(); RI != E; ) { | 
|  | 245 | MachineInstr *MI = &*(RI++); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 246 |  | 
|  | 247 | // Allow copies to/from Reg. | 
|  | 248 | if (isFullCopyOf(MI, Reg)) | 
|  | 249 | continue; | 
|  | 250 |  | 
|  | 251 | // Allow stack slot loads. | 
|  | 252 | int FI; | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 253 | if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 254 | continue; | 
|  | 255 |  | 
|  | 256 | // Allow stack slot stores. | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 257 | if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 258 | continue; | 
|  | 259 |  | 
|  | 260 | // Allow a single additional instruction. | 
|  | 261 | if (UseMI && MI != UseMI) | 
|  | 262 | return false; | 
|  | 263 | UseMI = MI; | 
|  | 264 | } | 
|  | 265 | return true; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | /// collectRegsToSpill - Collect live range snippets that only have a single | 
|  | 269 | /// real use. | 
|  | 270 | void InlineSpiller::collectRegsToSpill() { | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 271 | unsigned Reg = Edit->getReg(); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 272 |  | 
|  | 273 | // Main register always spills. | 
|  | 274 | RegsToSpill.assign(1, Reg); | 
|  | 275 | SnippetCopies.clear(); | 
|  | 276 |  | 
|  | 277 | // Snippets all have the same original, so there can't be any for an original | 
|  | 278 | // register. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 279 | if (Original == Reg) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 280 | return; | 
|  | 281 |  | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 282 | for (MachineRegisterInfo::reg_instr_iterator | 
|  | 283 | RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end(); RI != E; ) { | 
|  | 284 | MachineInstr *MI = &*(RI++); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 285 | unsigned SnipReg = isFullCopyOf(MI, Reg); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 286 | if (!isSibling(SnipReg)) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 287 | continue; | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 288 | LiveInterval &SnipLI = LIS.getInterval(SnipReg); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 289 | if (!isSnippet(SnipLI)) | 
|  | 290 | continue; | 
|  | 291 | SnippetCopies.insert(MI); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 292 | if (isRegToSpill(SnipReg)) | 
|  | 293 | continue; | 
|  | 294 | RegsToSpill.push_back(SnipReg); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 295 | DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n'); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 296 | ++NumSnippets; | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 297 | } | 
|  | 298 | } | 
|  | 299 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 300 |  | 
|  | 301 | //===----------------------------------------------------------------------===// | 
|  | 302 | //                            Sibling Values | 
|  | 303 | //===----------------------------------------------------------------------===// | 
|  | 304 |  | 
|  | 305 | // After live range splitting, some values to be spilled may be defined by | 
|  | 306 | // copies from sibling registers. We trace the sibling copies back to the | 
|  | 307 | // original value if it still exists. We need it for rematerialization. | 
|  | 308 | // | 
|  | 309 | // Even when the value can't be rematerialized, we still want to determine if | 
|  | 310 | // the value has already been spilled, or we may want to hoist the spill from a | 
|  | 311 | // loop. | 
|  | 312 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 313 | bool InlineSpiller::isSibling(unsigned Reg) { | 
|  | 314 | return TargetRegisterInfo::isVirtualRegister(Reg) && | 
|  | 315 | VRM.getOriginal(Reg) == Original; | 
|  | 316 | } | 
|  | 317 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 318 | #ifndef NDEBUG | 
|  | 319 | static raw_ostream &operator<<(raw_ostream &OS, | 
|  | 320 | const InlineSpiller::SibValueInfo &SVI) { | 
|  | 321 | OS << "spill " << PrintReg(SVI.SpillReg) << ':' | 
|  | 322 | << SVI.SpillVNI->id << '@' << SVI.SpillVNI->def; | 
|  | 323 | if (SVI.SpillMBB) | 
|  | 324 | OS << " in BB#" << SVI.SpillMBB->getNumber(); | 
|  | 325 | if (SVI.AllDefsAreReloads) | 
|  | 326 | OS << " all-reloads"; | 
|  | 327 | if (SVI.DefByOrigPHI) | 
|  | 328 | OS << " orig-phi"; | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 329 | if (SVI.KillsSource) | 
|  | 330 | OS << " kill"; | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 331 | OS << " deps["; | 
|  | 332 | for (unsigned i = 0, e = SVI.Deps.size(); i != e; ++i) | 
|  | 333 | OS << ' ' << SVI.Deps[i]->id << '@' << SVI.Deps[i]->def; | 
|  | 334 | OS << " ]"; | 
|  | 335 | if (SVI.DefMI) | 
|  | 336 | OS << " def: " << *SVI.DefMI; | 
|  | 337 | else | 
|  | 338 | OS << '\n'; | 
|  | 339 | return OS; | 
|  | 340 | } | 
|  | 341 | #endif | 
|  | 342 |  | 
|  | 343 | /// propagateSiblingValue - Propagate the value in SVI to dependents if it is | 
|  | 344 | /// known.  Otherwise remember the dependency for later. | 
|  | 345 | /// | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 346 | /// @param SVIIter SibValues entry to propagate. | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 347 | /// @param VNI Dependent value, or NULL to propagate to all saved dependents. | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 348 | void InlineSpiller::propagateSiblingValue(SibValueMap::iterator SVIIter, | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 349 | VNInfo *VNI) { | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 350 | SibValueMap::value_type *SVI = &*SVIIter; | 
|  | 351 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 352 | // When VNI is non-NULL, add it to SVI's deps, and only propagate to that. | 
|  | 353 | TinyPtrVector<VNInfo*> FirstDeps; | 
|  | 354 | if (VNI) { | 
|  | 355 | FirstDeps.push_back(VNI); | 
|  | 356 | SVI->second.Deps.push_back(VNI); | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | // Has the value been completely determined yet?  If not, defer propagation. | 
|  | 360 | if (!SVI->second.hasDef()) | 
|  | 361 | return; | 
|  | 362 |  | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 363 | // Work list of values to propagate. | 
|  | 364 | SmallSetVector<SibValueMap::value_type *, 8> WorkList; | 
|  | 365 | WorkList.insert(SVI); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 366 |  | 
|  | 367 | do { | 
|  | 368 | SVI = WorkList.pop_back_val(); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 369 | TinyPtrVector<VNInfo*> *Deps = VNI ? &FirstDeps : &SVI->second.Deps; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 370 | VNI = nullptr; | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 371 |  | 
|  | 372 | SibValueInfo &SV = SVI->second; | 
|  | 373 | if (!SV.SpillMBB) | 
|  | 374 | SV.SpillMBB = LIS.getMBBFromIndex(SV.SpillVNI->def); | 
|  | 375 |  | 
|  | 376 | DEBUG(dbgs() << "  prop to " << Deps->size() << ": " | 
|  | 377 | << SVI->first->id << '@' << SVI->first->def << ":\t" << SV); | 
|  | 378 |  | 
|  | 379 | assert(SV.hasDef() && "Propagating undefined value"); | 
|  | 380 |  | 
|  | 381 | // Should this value be propagated as a preferred spill candidate?  We don't | 
|  | 382 | // propagate values of registers that are about to spill. | 
| Jakob Stoklund Olesen | bceb9e5 | 2011-09-15 21:06:00 +0000 | [diff] [blame] | 383 | bool PropSpill = !DisableHoisting && !isRegToSpill(SV.SpillReg); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 384 | unsigned SpillDepth = ~0u; | 
|  | 385 |  | 
|  | 386 | for (TinyPtrVector<VNInfo*>::iterator DepI = Deps->begin(), | 
|  | 387 | DepE = Deps->end(); DepI != DepE; ++DepI) { | 
|  | 388 | SibValueMap::iterator DepSVI = SibValues.find(*DepI); | 
|  | 389 | assert(DepSVI != SibValues.end() && "Dependent value not in SibValues"); | 
|  | 390 | SibValueInfo &DepSV = DepSVI->second; | 
|  | 391 | if (!DepSV.SpillMBB) | 
|  | 392 | DepSV.SpillMBB = LIS.getMBBFromIndex(DepSV.SpillVNI->def); | 
|  | 393 |  | 
|  | 394 | bool Changed = false; | 
|  | 395 |  | 
|  | 396 | // Propagate defining instruction. | 
|  | 397 | if (!DepSV.hasDef()) { | 
|  | 398 | Changed = true; | 
|  | 399 | DepSV.DefMI = SV.DefMI; | 
|  | 400 | DepSV.DefByOrigPHI = SV.DefByOrigPHI; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | // Propagate AllDefsAreReloads.  For PHI values, this computes an AND of | 
|  | 404 | // all predecessors. | 
|  | 405 | if (!SV.AllDefsAreReloads && DepSV.AllDefsAreReloads) { | 
|  | 406 | Changed = true; | 
|  | 407 | DepSV.AllDefsAreReloads = false; | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | // Propagate best spill value. | 
|  | 411 | if (PropSpill && SV.SpillVNI != DepSV.SpillVNI) { | 
|  | 412 | if (SV.SpillMBB == DepSV.SpillMBB) { | 
|  | 413 | // DepSV is in the same block.  Hoist when dominated. | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 414 | if (DepSV.KillsSource && SV.SpillVNI->def < DepSV.SpillVNI->def) { | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 415 | // This is an alternative def earlier in the same MBB. | 
|  | 416 | // Hoist the spill as far as possible in SpillMBB. This can ease | 
|  | 417 | // register pressure: | 
|  | 418 | // | 
|  | 419 | //   x = def | 
|  | 420 | //   y = use x | 
|  | 421 | //   s = copy x | 
|  | 422 | // | 
|  | 423 | // Hoisting the spill of s to immediately after the def removes the | 
|  | 424 | // interference between x and y: | 
|  | 425 | // | 
|  | 426 | //   x = def | 
|  | 427 | //   spill x | 
|  | 428 | //   y = use x<kill> | 
|  | 429 | // | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 430 | // This hoist only helps when the DepSV copy kills its source. | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 431 | Changed = true; | 
|  | 432 | DepSV.SpillReg = SV.SpillReg; | 
|  | 433 | DepSV.SpillVNI = SV.SpillVNI; | 
|  | 434 | DepSV.SpillMBB = SV.SpillMBB; | 
|  | 435 | } | 
|  | 436 | } else { | 
|  | 437 | // DepSV is in a different block. | 
|  | 438 | if (SpillDepth == ~0u) | 
|  | 439 | SpillDepth = Loops.getLoopDepth(SV.SpillMBB); | 
|  | 440 |  | 
|  | 441 | // Also hoist spills to blocks with smaller loop depth, but make sure | 
|  | 442 | // that the new value dominates.  Non-phi dependents are always | 
|  | 443 | // dominated, phis need checking. | 
| Manman Ren | c935560 | 2014-03-21 21:46:24 +0000 | [diff] [blame] | 444 |  | 
|  | 445 | const BranchProbability MarginProb(4, 5); // 80% | 
|  | 446 | // Hoist a spill to outer loop if there are multiple dependents (it | 
|  | 447 | // can be beneficial if more than one dependents are hoisted) or | 
|  | 448 | // if DepSV (the hoisting source) is hotter than SV (the hoisting | 
|  | 449 | // destination) (we add a 80% margin to bias a little towards | 
|  | 450 | // loop depth). | 
|  | 451 | bool HoistCondition = | 
|  | 452 | (MBFI.getBlockFreq(DepSV.SpillMBB) >= | 
|  | 453 | (MBFI.getBlockFreq(SV.SpillMBB) * MarginProb)) || | 
|  | 454 | Deps->size() > 1; | 
|  | 455 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 456 | if ((Loops.getLoopDepth(DepSV.SpillMBB) > SpillDepth) && | 
| Manman Ren | c935560 | 2014-03-21 21:46:24 +0000 | [diff] [blame] | 457 | HoistCondition && | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 458 | (!DepSVI->first->isPHIDef() || | 
|  | 459 | MDT.dominates(SV.SpillMBB, DepSV.SpillMBB))) { | 
|  | 460 | Changed = true; | 
|  | 461 | DepSV.SpillReg = SV.SpillReg; | 
|  | 462 | DepSV.SpillVNI = SV.SpillVNI; | 
|  | 463 | DepSV.SpillMBB = SV.SpillMBB; | 
|  | 464 | } | 
|  | 465 | } | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | if (!Changed) | 
|  | 469 | continue; | 
|  | 470 |  | 
|  | 471 | // Something changed in DepSVI. Propagate to dependents. | 
| Benjamin Kramer | bc6666b | 2013-05-23 15:42:57 +0000 | [diff] [blame] | 472 | WorkList.insert(&*DepSVI); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 473 |  | 
|  | 474 | DEBUG(dbgs() << "  update " << DepSVI->first->id << '@' | 
|  | 475 | << DepSVI->first->def << " to:\t" << DepSV); | 
|  | 476 | } | 
|  | 477 | } while (!WorkList.empty()); | 
|  | 478 | } | 
|  | 479 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 480 | /// traceSiblingValue - Trace a value that is about to be spilled back to the | 
|  | 481 | /// real defining instructions by looking through sibling copies. Always stay | 
|  | 482 | /// within the range of OrigVNI so the registers are known to carry the same | 
|  | 483 | /// value. | 
|  | 484 | /// | 
|  | 485 | /// Determine if the value is defined by all reloads, so spilling isn't | 
|  | 486 | /// necessary - the value is already in the stack slot. | 
|  | 487 | /// | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 488 | /// Return a defining instruction that may be a candidate for rematerialization. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 489 | /// | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 490 | MachineInstr *InlineSpiller::traceSiblingValue(unsigned UseReg, VNInfo *UseVNI, | 
|  | 491 | VNInfo *OrigVNI) { | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 492 | // Check if a cached value already exists. | 
|  | 493 | SibValueMap::iterator SVI; | 
|  | 494 | bool Inserted; | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 495 | std::tie(SVI, Inserted) = | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 496 | SibValues.insert(std::make_pair(UseVNI, SibValueInfo(UseReg, UseVNI))); | 
|  | 497 | if (!Inserted) { | 
|  | 498 | DEBUG(dbgs() << "Cached value " << PrintReg(UseReg) << ':' | 
|  | 499 | << UseVNI->id << '@' << UseVNI->def << ' ' << SVI->second); | 
|  | 500 | return SVI->second.DefMI; | 
|  | 501 | } | 
|  | 502 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 503 | DEBUG(dbgs() << "Tracing value " << PrintReg(UseReg) << ':' | 
|  | 504 | << UseVNI->id << '@' << UseVNI->def << '\n'); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 505 |  | 
|  | 506 | // List of (Reg, VNI) that have been inserted into SibValues, but need to be | 
|  | 507 | // processed. | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 508 | SmallVector<std::pair<unsigned, VNInfo*>, 8> WorkList; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 509 | WorkList.push_back(std::make_pair(UseReg, UseVNI)); | 
|  | 510 |  | 
| Patrik Hagglund | cb06a36 | 2014-12-11 10:40:17 +0000 | [diff] [blame] | 511 | LiveInterval &OrigLI = LIS.getInterval(Original); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 512 | do { | 
|  | 513 | unsigned Reg; | 
|  | 514 | VNInfo *VNI; | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 515 | std::tie(Reg, VNI) = WorkList.pop_back_val(); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 516 | DEBUG(dbgs() << "  " << PrintReg(Reg) << ':' << VNI->id << '@' << VNI->def | 
|  | 517 | << ":\t"); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 518 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 519 | // First check if this value has already been computed. | 
|  | 520 | SVI = SibValues.find(VNI); | 
|  | 521 | assert(SVI != SibValues.end() && "Missing SibValues entry"); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 522 |  | 
|  | 523 | // Trace through PHI-defs created by live range splitting. | 
|  | 524 | if (VNI->isPHIDef()) { | 
| Patrik Hagglund | cb06a36 | 2014-12-11 10:40:17 +0000 | [diff] [blame] | 525 | // Stop at original PHIs.  We don't know the value at the | 
|  | 526 | // predecessors. Look up the VNInfo for the current definition | 
|  | 527 | // in OrigLI, to properly determine whether or not this phi was | 
|  | 528 | // added by splitting. | 
|  | 529 | if (VNI->def == OrigLI.getVNInfoAt(VNI->def)->def) { | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 530 | DEBUG(dbgs() << "orig phi value\n"); | 
|  | 531 | SVI->second.DefByOrigPHI = true; | 
|  | 532 | SVI->second.AllDefsAreReloads = false; | 
|  | 533 | propagateSiblingValue(SVI); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 534 | continue; | 
|  | 535 | } | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 536 |  | 
|  | 537 | // This is a PHI inserted by live range splitting.  We could trace the | 
|  | 538 | // live-out value from predecessor blocks, but that search can be very | 
|  | 539 | // expensive if there are many predecessors and many more PHIs as | 
|  | 540 | // generated by tail-dup when it sees an indirectbr.  Instead, look at | 
|  | 541 | // all the non-PHI defs that have the same value as OrigVNI.  They must | 
|  | 542 | // jointly dominate VNI->def.  This is not optimal since VNI may actually | 
|  | 543 | // be jointly dominated by a smaller subset of defs, so there is a change | 
|  | 544 | // we will miss a AllDefsAreReloads optimization. | 
|  | 545 |  | 
|  | 546 | // Separate all values dominated by OrigVNI into PHIs and non-PHIs. | 
|  | 547 | SmallVector<VNInfo*, 8> PHIs, NonPHIs; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 548 | LiveInterval &LI = LIS.getInterval(Reg); | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 549 |  | 
|  | 550 | for (LiveInterval::vni_iterator VI = LI.vni_begin(), VE = LI.vni_end(); | 
|  | 551 | VI != VE; ++VI) { | 
|  | 552 | VNInfo *VNI2 = *VI; | 
|  | 553 | if (VNI2->isUnused()) | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 554 | continue; | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 555 | if (!OrigLI.containsOneValue() && | 
|  | 556 | OrigLI.getVNInfoAt(VNI2->def) != OrigVNI) | 
|  | 557 | continue; | 
|  | 558 | if (VNI2->isPHIDef() && VNI2->def != OrigVNI->def) | 
|  | 559 | PHIs.push_back(VNI2); | 
|  | 560 | else | 
|  | 561 | NonPHIs.push_back(VNI2); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 562 | } | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 563 | DEBUG(dbgs() << "split phi value, checking " << PHIs.size() | 
|  | 564 | << " phi-defs, and " << NonPHIs.size() | 
|  | 565 | << " non-phi/orig defs\n"); | 
|  | 566 |  | 
|  | 567 | // Create entries for all the PHIs.  Don't add them to the worklist, we | 
|  | 568 | // are processing all of them in one go here. | 
|  | 569 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) | 
|  | 570 | SibValues.insert(std::make_pair(PHIs[i], SibValueInfo(Reg, PHIs[i]))); | 
|  | 571 |  | 
|  | 572 | // Add every PHI as a dependent of all the non-PHIs. | 
|  | 573 | for (unsigned i = 0, e = NonPHIs.size(); i != e; ++i) { | 
|  | 574 | VNInfo *NonPHI = NonPHIs[i]; | 
|  | 575 | // Known value? Try an insertion. | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 576 | std::tie(SVI, Inserted) = | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 577 | SibValues.insert(std::make_pair(NonPHI, SibValueInfo(Reg, NonPHI))); | 
|  | 578 | // Add all the PHIs as dependents of NonPHI. | 
| Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 579 | SVI->second.Deps.insert(SVI->second.Deps.end(), PHIs.begin(), | 
|  | 580 | PHIs.end()); | 
| Jakob Stoklund Olesen | 07b3503 | 2011-09-15 16:41:12 +0000 | [diff] [blame] | 581 | // This is the first time we see NonPHI, add it to the worklist. | 
|  | 582 | if (Inserted) | 
|  | 583 | WorkList.push_back(std::make_pair(Reg, NonPHI)); | 
|  | 584 | else | 
|  | 585 | // Propagate to all inserted PHIs, not just VNI. | 
|  | 586 | propagateSiblingValue(SVI); | 
|  | 587 | } | 
|  | 588 |  | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 589 | // Next work list item. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 590 | continue; | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 | MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def); | 
|  | 594 | assert(MI && "Missing def"); | 
|  | 595 |  | 
|  | 596 | // Trace through sibling copies. | 
|  | 597 | if (unsigned SrcReg = isFullCopyOf(MI, Reg)) { | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 598 | if (isSibling(SrcReg)) { | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 599 | LiveInterval &SrcLI = LIS.getInterval(SrcReg); | 
| Matthias Braun | 88dd0ab | 2013-10-10 21:28:52 +0000 | [diff] [blame] | 600 | LiveQueryResult SrcQ = SrcLI.Query(VNI->def); | 
| Jakob Stoklund Olesen | 2aeead4 | 2012-05-20 02:44:33 +0000 | [diff] [blame] | 601 | assert(SrcQ.valueIn() && "Copy from non-existing value"); | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 602 | // Check if this COPY kills its source. | 
| Jakob Stoklund Olesen | 2aeead4 | 2012-05-20 02:44:33 +0000 | [diff] [blame] | 603 | SVI->second.KillsSource = SrcQ.isKill(); | 
|  | 604 | VNInfo *SrcVNI = SrcQ.valueIn(); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 605 | DEBUG(dbgs() << "copy of " << PrintReg(SrcReg) << ':' | 
| Jakob Stoklund Olesen | e8339b2 | 2011-09-16 00:03:33 +0000 | [diff] [blame] | 606 | << SrcVNI->id << '@' << SrcVNI->def | 
|  | 607 | << " kill=" << unsigned(SVI->second.KillsSource) << '\n'); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 608 | // Known sibling source value? Try an insertion. | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 609 | std::tie(SVI, Inserted) = SibValues.insert( | 
|  | 610 | std::make_pair(SrcVNI, SibValueInfo(SrcReg, SrcVNI))); | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 611 | // This is the first time we see Src, add it to the worklist. | 
|  | 612 | if (Inserted) | 
|  | 613 | WorkList.push_back(std::make_pair(SrcReg, SrcVNI)); | 
|  | 614 | propagateSiblingValue(SVI, VNI); | 
|  | 615 | // Next work list item. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 616 | continue; | 
|  | 617 | } | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | // Track reachable reloads. | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 621 | SVI->second.DefMI = MI; | 
|  | 622 | SVI->second.SpillMBB = MI->getParent(); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 623 | int FI; | 
|  | 624 | if (Reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot) { | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 625 | DEBUG(dbgs() << "reload\n"); | 
|  | 626 | propagateSiblingValue(SVI); | 
|  | 627 | // Next work list item. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 628 | continue; | 
|  | 629 | } | 
|  | 630 |  | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 631 | // Potential remat candidate. | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 632 | DEBUG(dbgs() << "def " << *MI); | 
|  | 633 | SVI->second.AllDefsAreReloads = false; | 
|  | 634 | propagateSiblingValue(SVI); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 635 | } while (!WorkList.empty()); | 
|  | 636 |  | 
| Logan Chien | 64f361e | 2012-09-01 12:11:41 +0000 | [diff] [blame] | 637 | // Look up the value we were looking for.  We already did this lookup at the | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 638 | // top of the function, but SibValues may have been invalidated. | 
|  | 639 | SVI = SibValues.find(UseVNI); | 
|  | 640 | assert(SVI != SibValues.end() && "Didn't compute requested info"); | 
|  | 641 | DEBUG(dbgs() << "  traced to:\t" << SVI->second); | 
|  | 642 | return SVI->second.DefMI; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 643 | } | 
|  | 644 |  | 
|  | 645 | /// analyzeSiblingValues - Trace values defined by sibling copies back to | 
|  | 646 | /// something that isn't a sibling copy. | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 647 | /// | 
|  | 648 | /// Keep track of values that may be rematerializable. | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 649 | void InlineSpiller::analyzeSiblingValues() { | 
|  | 650 | SibValues.clear(); | 
|  | 651 |  | 
|  | 652 | // No siblings at all? | 
|  | 653 | if (Edit->getReg() == Original) | 
|  | 654 | return; | 
|  | 655 |  | 
|  | 656 | LiveInterval &OrigLI = LIS.getInterval(Original); | 
|  | 657 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) { | 
|  | 658 | unsigned Reg = RegsToSpill[i]; | 
|  | 659 | LiveInterval &LI = LIS.getInterval(Reg); | 
|  | 660 | for (LiveInterval::const_vni_iterator VI = LI.vni_begin(), | 
|  | 661 | VE = LI.vni_end(); VI != VE; ++VI) { | 
|  | 662 | VNInfo *VNI = *VI; | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 663 | if (VNI->isUnused()) | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 664 | continue; | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 665 | MachineInstr *DefMI = nullptr; | 
| Jakob Stoklund Olesen | ad6b22e | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 666 | if (!VNI->isPHIDef()) { | 
|  | 667 | DefMI = LIS.getInstructionFromIndex(VNI->def); | 
|  | 668 | assert(DefMI && "No defining instruction"); | 
|  | 669 | } | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 670 | // Check possible sibling copies. | 
| Jakob Stoklund Olesen | ad6b22e | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 671 | if (VNI->isPHIDef() || DefMI->isCopy()) { | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 672 | VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def); | 
| Jakob Stoklund Olesen | bbad3bc | 2011-07-05 15:38:41 +0000 | [diff] [blame] | 673 | assert(OrigVNI && "Def outside original live range"); | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 674 | if (OrigVNI->def != VNI->def) | 
|  | 675 | DefMI = traceSiblingValue(Reg, VNI, OrigVNI); | 
|  | 676 | } | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 677 | if (DefMI && Edit->checkRematerializable(VNI, DefMI, AA)) { | 
| Jakob Stoklund Olesen | 86e53ce | 2011-04-20 22:14:20 +0000 | [diff] [blame] | 678 | DEBUG(dbgs() << "Value " << PrintReg(Reg) << ':' << VNI->id << '@' | 
|  | 679 | << VNI->def << " may remat from " << *DefMI); | 
|  | 680 | } | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 681 | } | 
|  | 682 | } | 
|  | 683 | } | 
|  | 684 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 685 | /// hoistSpill - Given a sibling copy that defines a value to be spilled, insert | 
|  | 686 | /// a spill at a better location. | 
|  | 687 | bool InlineSpiller::hoistSpill(LiveInterval &SpillLI, MachineInstr *CopyMI) { | 
|  | 688 | SlotIndex Idx = LIS.getInstructionIndex(CopyMI); | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 689 | VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot()); | 
|  | 690 | assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy"); | 
| Jakob Stoklund Olesen | ec9b4a6 | 2011-04-30 06:42:21 +0000 | [diff] [blame] | 691 | SibValueMap::iterator I = SibValues.find(VNI); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 692 | if (I == SibValues.end()) | 
|  | 693 | return false; | 
|  | 694 |  | 
|  | 695 | const SibValueInfo &SVI = I->second; | 
|  | 696 |  | 
|  | 697 | // Let the normal folding code deal with the boring case. | 
|  | 698 | if (!SVI.AllDefsAreReloads && SVI.SpillVNI == VNI) | 
|  | 699 | return false; | 
|  | 700 |  | 
| Jakob Stoklund Olesen | ec9b4a6 | 2011-04-30 06:42:21 +0000 | [diff] [blame] | 701 | // SpillReg may have been deleted by remat and DCE. | 
|  | 702 | if (!LIS.hasInterval(SVI.SpillReg)) { | 
|  | 703 | DEBUG(dbgs() << "Stale interval: " << PrintReg(SVI.SpillReg) << '\n'); | 
|  | 704 | SibValues.erase(I); | 
|  | 705 | return false; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | LiveInterval &SibLI = LIS.getInterval(SVI.SpillReg); | 
|  | 709 | if (!SibLI.containsValue(SVI.SpillVNI)) { | 
|  | 710 | DEBUG(dbgs() << "Stale value: " << PrintReg(SVI.SpillReg) << '\n'); | 
|  | 711 | SibValues.erase(I); | 
|  | 712 | return false; | 
|  | 713 | } | 
|  | 714 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 715 | // Conservatively extend the stack slot range to the range of the original | 
|  | 716 | // value. We may be able to do better with stack slot coloring by being more | 
|  | 717 | // careful here. | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 718 | assert(StackInt && "No stack slot assigned yet."); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 719 | LiveInterval &OrigLI = LIS.getInterval(Original); | 
|  | 720 | VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx); | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 721 | StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0)); | 
| Jakob Stoklund Olesen | 8698507 | 2011-03-19 23:02:47 +0000 | [diff] [blame] | 722 | DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": " | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 723 | << *StackInt << '\n'); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 724 |  | 
|  | 725 | // Already spilled everywhere. | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 726 | if (SVI.AllDefsAreReloads) { | 
| Jakob Stoklund Olesen | 278bf02 | 2011-09-09 18:11:41 +0000 | [diff] [blame] | 727 | DEBUG(dbgs() << "\tno spill needed: " << SVI); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 728 | ++NumOmitReloadSpill; | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 729 | return true; | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 730 | } | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 731 | // We are going to spill SVI.SpillVNI immediately after its def, so clear out | 
|  | 732 | // any later spills of the same value. | 
| Jakob Stoklund Olesen | ec9b4a6 | 2011-04-30 06:42:21 +0000 | [diff] [blame] | 733 | eliminateRedundantSpills(SibLI, SVI.SpillVNI); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 734 |  | 
|  | 735 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(SVI.SpillVNI->def); | 
|  | 736 | MachineBasicBlock::iterator MII; | 
|  | 737 | if (SVI.SpillVNI->isPHIDef()) | 
|  | 738 | MII = MBB->SkipPHIsAndLabels(MBB->begin()); | 
|  | 739 | else { | 
| Jakob Stoklund Olesen | ec9b4a6 | 2011-04-30 06:42:21 +0000 | [diff] [blame] | 740 | MachineInstr *DefMI = LIS.getInstructionFromIndex(SVI.SpillVNI->def); | 
|  | 741 | assert(DefMI && "Defining instruction disappeared"); | 
|  | 742 | MII = DefMI; | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 743 | ++MII; | 
|  | 744 | } | 
|  | 745 | // Insert spill without kill flag immediately after def. | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 746 | TII.storeRegToStackSlot(*MBB, MII, SVI.SpillReg, false, StackSlot, | 
|  | 747 | MRI.getRegClass(SVI.SpillReg), &TRI); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 748 | --MII; // Point to store instruction. | 
|  | 749 | LIS.InsertMachineInstrInMaps(MII); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 750 | DEBUG(dbgs() << "\thoisted: " << SVI.SpillVNI->def << '\t' << *MII); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 751 |  | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 752 | ++NumSpills; | 
|  | 753 | ++NumHoists; | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 754 | return true; | 
|  | 755 | } | 
|  | 756 |  | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 757 | /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any | 
|  | 758 | /// redundant spills of this value in SLI.reg and sibling copies. | 
|  | 759 | void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) { | 
| Jakob Stoklund Olesen | e55003f | 2011-03-20 05:44:58 +0000 | [diff] [blame] | 760 | assert(VNI && "Missing value"); | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 761 | SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList; | 
|  | 762 | WorkList.push_back(std::make_pair(&SLI, VNI)); | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 763 | assert(StackInt && "No stack slot assigned yet."); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 764 |  | 
|  | 765 | do { | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 766 | LiveInterval *LI; | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 767 | std::tie(LI, VNI) = WorkList.pop_back_val(); | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 768 | unsigned Reg = LI->reg; | 
| Jakob Stoklund Olesen | ec9b4a6 | 2011-04-30 06:42:21 +0000 | [diff] [blame] | 769 | DEBUG(dbgs() << "Checking redundant spills for " | 
|  | 770 | << VNI->id << '@' << VNI->def << " in " << *LI << '\n'); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 771 |  | 
|  | 772 | // Regs to spill are taken care of. | 
|  | 773 | if (isRegToSpill(Reg)) | 
|  | 774 | continue; | 
|  | 775 |  | 
|  | 776 | // Add all of VNI's live range to StackInt. | 
| Jakob Stoklund Olesen | e466345 | 2011-03-26 22:16:41 +0000 | [diff] [blame] | 777 | StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0)); | 
|  | 778 | DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n'); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 779 |  | 
|  | 780 | // Find all spills and copies of VNI. | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 781 | for (MachineRegisterInfo::use_instr_nodbg_iterator | 
|  | 782 | UI = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end(); | 
|  | 783 | UI != E; ) { | 
|  | 784 | MachineInstr *MI = &*(UI++); | 
| Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 785 | if (!MI->isCopy() && !MI->mayStore()) | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 786 | continue; | 
|  | 787 | SlotIndex Idx = LIS.getInstructionIndex(MI); | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 788 | if (LI->getVNInfoAt(Idx) != VNI) | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 789 | continue; | 
|  | 790 |  | 
|  | 791 | // Follow sibling copies down the dominator tree. | 
|  | 792 | if (unsigned DstReg = isFullCopyOf(MI, Reg)) { | 
|  | 793 | if (isSibling(DstReg)) { | 
|  | 794 | LiveInterval &DstLI = LIS.getInterval(DstReg); | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 795 | VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot()); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 796 | assert(DstVNI && "Missing defined value"); | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 797 | assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot"); | 
| Jakob Stoklund Olesen | 3948864 | 2011-03-20 05:44:55 +0000 | [diff] [blame] | 798 | WorkList.push_back(std::make_pair(&DstLI, DstVNI)); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 799 | } | 
|  | 800 | continue; | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | // Erase spills. | 
|  | 804 | int FI; | 
|  | 805 | if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) { | 
|  | 806 | DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << *MI); | 
|  | 807 | // eliminateDeadDefs won't normally remove stores, so switch opcode. | 
|  | 808 | MI->setDesc(TII.get(TargetOpcode::KILL)); | 
|  | 809 | DeadDefs.push_back(MI); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 810 | ++NumSpillsRemoved; | 
|  | 811 | --NumSpills; | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 812 | } | 
|  | 813 | } | 
|  | 814 | } while (!WorkList.empty()); | 
|  | 815 | } | 
|  | 816 |  | 
| Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 817 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 818 | //===----------------------------------------------------------------------===// | 
|  | 819 | //                            Rematerialization | 
|  | 820 | //===----------------------------------------------------------------------===// | 
|  | 821 |  | 
|  | 822 | /// markValueUsed - Remember that VNI failed to rematerialize, so its defining | 
|  | 823 | /// instruction cannot be eliminated. See through snippet copies | 
|  | 824 | void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) { | 
|  | 825 | SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList; | 
|  | 826 | WorkList.push_back(std::make_pair(LI, VNI)); | 
|  | 827 | do { | 
| Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 828 | std::tie(LI, VNI) = WorkList.pop_back_val(); | 
| David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 829 | if (!UsedValues.insert(VNI).second) | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 830 | continue; | 
|  | 831 |  | 
|  | 832 | if (VNI->isPHIDef()) { | 
|  | 833 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def); | 
|  | 834 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), | 
|  | 835 | PE = MBB->pred_end(); PI != PE; ++PI) { | 
| Jakob Stoklund Olesen | d7bcf43 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 836 | VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)); | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 837 | if (PVNI) | 
|  | 838 | WorkList.push_back(std::make_pair(LI, PVNI)); | 
|  | 839 | } | 
|  | 840 | continue; | 
|  | 841 | } | 
|  | 842 |  | 
|  | 843 | // Follow snippet copies. | 
|  | 844 | MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def); | 
|  | 845 | if (!SnippetCopies.count(MI)) | 
|  | 846 | continue; | 
|  | 847 | LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg()); | 
|  | 848 | assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy"); | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 849 | VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true)); | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 850 | assert(SnipVNI && "Snippet undefined before copy"); | 
|  | 851 | WorkList.push_back(std::make_pair(&SnipLI, SnipVNI)); | 
|  | 852 | } while (!WorkList.empty()); | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading. | 
|  | 856 | bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, | 
|  | 857 | MachineBasicBlock::iterator MI) { | 
| Patrik Hagglund | 296acbf | 2014-09-01 11:04:07 +0000 | [diff] [blame] | 858 |  | 
|  | 859 | // Analyze instruction | 
|  | 860 | SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops; | 
|  | 861 | MIBundleOperands::VirtRegInfo RI = | 
|  | 862 | MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops); | 
|  | 863 |  | 
|  | 864 | if (!RI.Reads) | 
|  | 865 | return false; | 
|  | 866 |  | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 867 | SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true); | 
| Jakob Stoklund Olesen | c0dd3da | 2011-07-18 05:31:59 +0000 | [diff] [blame] | 868 | VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex()); | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 869 |  | 
|  | 870 | if (!ParentVNI) { | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 871 | DEBUG(dbgs() << "\tadding <undef> flags: "); | 
|  | 872 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 873 | MachineOperand &MO = MI->getOperand(i); | 
| Jakob Stoklund Olesen | 0ed9ebc | 2011-03-29 17:47:02 +0000 | [diff] [blame] | 874 | if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 875 | MO.setIsUndef(); | 
|  | 876 | } | 
|  | 877 | DEBUG(dbgs() << UseIdx << '\t' << *MI); | 
|  | 878 | return true; | 
|  | 879 | } | 
| Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 880 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 881 | if (SnippetCopies.count(MI)) | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 882 | return false; | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 883 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 884 | // Use an OrigVNI from traceSiblingValue when ParentVNI is a sibling copy. | 
|  | 885 | LiveRangeEdit::Remat RM(ParentVNI); | 
|  | 886 | SibValueMap::const_iterator SibI = SibValues.find(ParentVNI); | 
|  | 887 | if (SibI != SibValues.end()) | 
|  | 888 | RM.OrigMI = SibI->second.DefMI; | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 889 | if (!Edit->canRematerializeAt(RM, UseIdx, false)) { | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 890 | markValueUsed(&VirtReg, ParentVNI); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 891 | DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << *MI); | 
|  | 892 | return false; | 
|  | 893 | } | 
|  | 894 |  | 
| Jakob Stoklund Olesen | 0ed9ebc | 2011-03-29 17:47:02 +0000 | [diff] [blame] | 895 | // If the instruction also writes VirtReg.reg, it had better not require the | 
|  | 896 | // same register for uses and defs. | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 897 | if (RI.Tied) { | 
|  | 898 | markValueUsed(&VirtReg, ParentVNI); | 
|  | 899 | DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << *MI); | 
|  | 900 | return false; | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 901 | } | 
|  | 902 |  | 
| Jakob Stoklund Olesen | 3b2966d | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 903 | // Before rematerializing into a register for a single instruction, try to | 
|  | 904 | // fold a load into the instruction. That avoids allocating a new register. | 
| Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 905 | if (RM.OrigMI->canFoldAsLoad() && | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 906 | foldMemoryOperand(Ops, RM.OrigMI)) { | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 907 | Edit->markRematerialized(RM.ParentVNI); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 908 | ++NumFoldedLoads; | 
| Jakob Stoklund Olesen | 3b2966d | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 909 | return true; | 
|  | 910 | } | 
|  | 911 |  | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 912 | // Alocate a new register for the remat. | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 913 | unsigned NewVReg = Edit->createFrom(Original); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 914 |  | 
|  | 915 | // Finally we can rematerialize OrigMI before MI. | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 916 | SlotIndex DefIdx = Edit->rematerializeAt(*MI->getParent(), MI, NewVReg, RM, | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 917 | TRI); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 918 | (void)DefIdx; | 
| Jakob Stoklund Olesen | c6a2041 | 2011-02-08 19:33:55 +0000 | [diff] [blame] | 919 | DEBUG(dbgs() << "\tremat:  " << DefIdx << '\t' | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 920 | << *LIS.getInstructionFromIndex(DefIdx)); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 921 |  | 
|  | 922 | // Replace operands | 
|  | 923 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 924 | MachineOperand &MO = MI->getOperand(Ops[i].second); | 
| Jakob Stoklund Olesen | 0ed9ebc | 2011-03-29 17:47:02 +0000 | [diff] [blame] | 925 | if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) { | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 926 | MO.setReg(NewVReg); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 927 | MO.setIsKill(); | 
|  | 928 | } | 
|  | 929 | } | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 930 | DEBUG(dbgs() << "\t        " << UseIdx << '\t' << *MI << '\n'); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 931 |  | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 932 | ++NumRemats; | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 933 | return true; | 
|  | 934 | } | 
|  | 935 |  | 
| Jakob Stoklund Olesen | 72911e4 | 2010-10-14 23:49:52 +0000 | [diff] [blame] | 936 | /// reMaterializeAll - Try to rematerialize as many uses as possible, | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 937 | /// and trim the live ranges after. | 
|  | 938 | void InlineSpiller::reMaterializeAll() { | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 939 | // analyzeSiblingValues has already tested all relevant defining instructions. | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 940 | if (!Edit->anyRematerializable(AA)) | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 941 | return; | 
|  | 942 |  | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 943 | UsedValues.clear(); | 
| Jakob Stoklund Olesen | 2edaa2f | 2010-10-20 22:00:51 +0000 | [diff] [blame] | 944 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 945 | // Try to remat before all uses of snippets. | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 946 | bool anyRemat = false; | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 947 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) { | 
|  | 948 | unsigned Reg = RegsToSpill[i]; | 
|  | 949 | LiveInterval &LI = LIS.getInterval(Reg); | 
| Patrik Hagglund | 296acbf | 2014-09-01 11:04:07 +0000 | [diff] [blame] | 950 | for (MachineRegisterInfo::reg_bundle_iterator | 
|  | 951 | RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end(); | 
|  | 952 | RegI != E; ) { | 
|  | 953 | MachineInstr *MI = &*(RegI++); | 
|  | 954 |  | 
|  | 955 | // Debug values are not allowed to affect codegen. | 
|  | 956 | if (MI->isDebugValue()) | 
|  | 957 | continue; | 
|  | 958 |  | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 959 | anyRemat |= reMaterializeFor(LI, MI); | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 960 | } | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 961 | } | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 962 | if (!anyRemat) | 
|  | 963 | return; | 
|  | 964 |  | 
|  | 965 | // Remove any values that were completely rematted. | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 966 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) { | 
|  | 967 | unsigned Reg = RegsToSpill[i]; | 
|  | 968 | LiveInterval &LI = LIS.getInterval(Reg); | 
|  | 969 | for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end(); | 
|  | 970 | I != E; ++I) { | 
|  | 971 | VNInfo *VNI = *I; | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 972 | if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI)) | 
| Jakob Stoklund Olesen | cf6c5c9 | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 973 | continue; | 
| Jakob Stoklund Olesen | d8af529 | 2011-03-29 03:12:02 +0000 | [diff] [blame] | 974 | MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def); | 
|  | 975 | MI->addRegisterDead(Reg, &TRI); | 
|  | 976 | if (!MI->allDefsAreDead()) | 
|  | 977 | continue; | 
|  | 978 | DEBUG(dbgs() << "All defs dead: " << *MI); | 
|  | 979 | DeadDefs.push_back(MI); | 
| Jakob Stoklund Olesen | cf6c5c9 | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 980 | } | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 981 | } | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 982 |  | 
|  | 983 | // Eliminate dead code after remat. Note that some snippet copies may be | 
|  | 984 | // deleted here. | 
|  | 985 | if (DeadDefs.empty()) | 
|  | 986 | return; | 
|  | 987 | DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n"); | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 988 | Edit->eliminateDeadDefs(DeadDefs, RegsToSpill); | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 989 |  | 
|  | 990 | // Get rid of deleted and empty intervals. | 
| Benjamin Kramer | 391f5a6 | 2013-05-05 11:29:14 +0000 | [diff] [blame] | 991 | unsigned ResultPos = 0; | 
|  | 992 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) { | 
|  | 993 | unsigned Reg = RegsToSpill[i]; | 
|  | 994 | if (!LIS.hasInterval(Reg)) | 
|  | 995 | continue; | 
|  | 996 |  | 
|  | 997 | LiveInterval &LI = LIS.getInterval(Reg); | 
|  | 998 | if (LI.empty()) { | 
|  | 999 | Edit->eraseVirtReg(Reg); | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 1000 | continue; | 
|  | 1001 | } | 
| Benjamin Kramer | 391f5a6 | 2013-05-05 11:29:14 +0000 | [diff] [blame] | 1002 |  | 
|  | 1003 | RegsToSpill[ResultPos++] = Reg; | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 1004 | } | 
| Benjamin Kramer | 391f5a6 | 2013-05-05 11:29:14 +0000 | [diff] [blame] | 1005 | RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end()); | 
| Jakob Stoklund Olesen | add79c6 | 2011-03-29 17:47:00 +0000 | [diff] [blame] | 1006 | DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n"); | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 1007 | } | 
|  | 1008 |  | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1009 |  | 
|  | 1010 | //===----------------------------------------------------------------------===// | 
|  | 1011 | //                                 Spilling | 
|  | 1012 | //===----------------------------------------------------------------------===// | 
|  | 1013 |  | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1014 | /// If MI is a load or store of StackSlot, it can be removed. | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1015 | bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) { | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1016 | int FI = 0; | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 1017 | unsigned InstrReg = TII.isLoadFromStackSlot(MI, FI); | 
|  | 1018 | bool IsLoad = InstrReg; | 
|  | 1019 | if (!IsLoad) | 
|  | 1020 | InstrReg = TII.isStoreToStackSlot(MI, FI); | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1021 |  | 
|  | 1022 | // We have a stack access. Is it the right register and slot? | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1023 | if (InstrReg != Reg || FI != StackSlot) | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1024 | return false; | 
|  | 1025 |  | 
|  | 1026 | DEBUG(dbgs() << "Coalescing stack access: " << *MI); | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1027 | LIS.RemoveMachineInstrFromMaps(MI); | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1028 | MI->eraseFromParent(); | 
| Jakob Stoklund Olesen | 37eb696 | 2011-09-15 17:54:28 +0000 | [diff] [blame] | 1029 |  | 
|  | 1030 | if (IsLoad) { | 
|  | 1031 | ++NumReloadsRemoved; | 
|  | 1032 | --NumReloads; | 
|  | 1033 | } else { | 
|  | 1034 | ++NumSpillsRemoved; | 
|  | 1035 | --NumSpills; | 
|  | 1036 | } | 
|  | 1037 |  | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1038 | return true; | 
|  | 1039 | } | 
|  | 1040 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1041 | #if !defined(NDEBUG) | 
|  | 1042 | // Dump the range of instructions from B to E with their slot indexes. | 
|  | 1043 | static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B, | 
|  | 1044 | MachineBasicBlock::iterator E, | 
|  | 1045 | LiveIntervals const &LIS, | 
|  | 1046 | const char *const header, | 
|  | 1047 | unsigned VReg =0) { | 
|  | 1048 | char NextLine = '\n'; | 
|  | 1049 | char SlotIndent = '\t'; | 
|  | 1050 |  | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1051 | if (std::next(B) == E) { | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1052 | NextLine = ' '; | 
|  | 1053 | SlotIndent = ' '; | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | dbgs() << '\t' << header << ": " << NextLine; | 
|  | 1057 |  | 
|  | 1058 | for (MachineBasicBlock::iterator I = B; I != E; ++I) { | 
|  | 1059 | SlotIndex Idx = LIS.getInstructionIndex(I).getRegSlot(); | 
|  | 1060 |  | 
|  | 1061 | // If a register was passed in and this instruction has it as a | 
|  | 1062 | // destination that is marked as an early clobber, print the | 
|  | 1063 | // early-clobber slot index. | 
|  | 1064 | if (VReg) { | 
|  | 1065 | MachineOperand *MO = I->findRegisterDefOperand(VReg); | 
|  | 1066 | if (MO && MO->isEarlyClobber()) | 
|  | 1067 | Idx = Idx.getRegSlot(true); | 
|  | 1068 | } | 
|  | 1069 |  | 
|  | 1070 | dbgs() << SlotIndent << Idx << '\t' << *I; | 
|  | 1071 | } | 
|  | 1072 | } | 
|  | 1073 | #endif | 
|  | 1074 |  | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1075 | /// foldMemoryOperand - Try folding stack slot references in Ops into their | 
|  | 1076 | /// instructions. | 
|  | 1077 | /// | 
|  | 1078 | /// @param Ops    Operand indices from analyzeVirtReg(). | 
| Jakob Stoklund Olesen | 3b2966d | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 1079 | /// @param LoadMI Load instruction to use instead of stack slot when non-null. | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1080 | /// @return       True on success. | 
|  | 1081 | bool InlineSpiller:: | 
|  | 1082 | foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops, | 
|  | 1083 | MachineInstr *LoadMI) { | 
|  | 1084 | if (Ops.empty()) | 
|  | 1085 | return false; | 
|  | 1086 | // Don't attempt folding in bundles. | 
|  | 1087 | MachineInstr *MI = Ops.front().first; | 
|  | 1088 | if (Ops.back().first != MI || MI->isBundled()) | 
|  | 1089 | return false; | 
|  | 1090 |  | 
| Jakob Stoklund Olesen | c94c967 | 2011-09-15 18:22:52 +0000 | [diff] [blame] | 1091 | bool WasCopy = MI->isCopy(); | 
| Jakob Stoklund Olesen | eef48b6 | 2011-11-10 00:17:03 +0000 | [diff] [blame] | 1092 | unsigned ImpReg = 0; | 
|  | 1093 |  | 
| Philip Reames | 0365f1a | 2014-12-01 22:52:56 +0000 | [diff] [blame] | 1094 | bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::STATEPOINT || | 
|  | 1095 | MI->getOpcode() == TargetOpcode::PATCHPOINT || | 
| Andrew Trick | 10d5be4 | 2013-11-17 01:36:23 +0000 | [diff] [blame] | 1096 | MI->getOpcode() == TargetOpcode::STACKMAP); | 
|  | 1097 |  | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1098 | // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied | 
|  | 1099 | // operands. | 
|  | 1100 | SmallVector<unsigned, 8> FoldOps; | 
|  | 1101 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1102 | unsigned Idx = Ops[i].second; | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1103 | MachineOperand &MO = MI->getOperand(Idx); | 
| Jakob Stoklund Olesen | eef48b6 | 2011-11-10 00:17:03 +0000 | [diff] [blame] | 1104 | if (MO.isImplicit()) { | 
|  | 1105 | ImpReg = MO.getReg(); | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1106 | continue; | 
| Jakob Stoklund Olesen | eef48b6 | 2011-11-10 00:17:03 +0000 | [diff] [blame] | 1107 | } | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1108 | // FIXME: Teach targets to deal with subregs. | 
| Andrew Trick | 10d5be4 | 2013-11-17 01:36:23 +0000 | [diff] [blame] | 1109 | if (!SpillSubRegs && MO.getSubReg()) | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1110 | return false; | 
| Jakob Stoklund Olesen | c6a2041 | 2011-02-08 19:33:55 +0000 | [diff] [blame] | 1111 | // We cannot fold a load instruction into a def. | 
|  | 1112 | if (LoadMI && MO.isDef()) | 
|  | 1113 | return false; | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1114 | // Tied use operands should not be passed to foldMemoryOperand. | 
|  | 1115 | if (!MI->isRegTiedToDefOperand(Idx)) | 
|  | 1116 | FoldOps.push_back(Idx); | 
|  | 1117 | } | 
|  | 1118 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1119 | MachineInstrSpan MIS(MI); | 
|  | 1120 |  | 
| Jakob Stoklund Olesen | 3b2966d | 2010-12-18 03:04:14 +0000 | [diff] [blame] | 1121 | MachineInstr *FoldMI = | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1122 | LoadMI ? TII.foldMemoryOperand(MI, FoldOps, LoadMI) | 
|  | 1123 | : TII.foldMemoryOperand(MI, FoldOps, StackSlot); | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1124 | if (!FoldMI) | 
|  | 1125 | return false; | 
| Andrew Trick | 5749b8b | 2013-06-21 18:33:26 +0000 | [diff] [blame] | 1126 |  | 
|  | 1127 | // Remove LIS for any dead defs in the original MI not in FoldMI. | 
|  | 1128 | for (MIBundleOperands MO(MI); MO.isValid(); ++MO) { | 
|  | 1129 | if (!MO->isReg()) | 
|  | 1130 | continue; | 
|  | 1131 | unsigned Reg = MO->getReg(); | 
|  | 1132 | if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) || | 
|  | 1133 | MRI.isReserved(Reg)) { | 
|  | 1134 | continue; | 
|  | 1135 | } | 
| Andrew Trick | dfacda3 | 2014-01-07 07:31:10 +0000 | [diff] [blame] | 1136 | // Skip non-Defs, including undef uses and internal reads. | 
|  | 1137 | if (MO->isUse()) | 
|  | 1138 | continue; | 
| Andrew Trick | 5749b8b | 2013-06-21 18:33:26 +0000 | [diff] [blame] | 1139 | MIBundleOperands::PhysRegInfo RI = | 
|  | 1140 | MIBundleOperands(FoldMI).analyzePhysReg(Reg, &TRI); | 
| Andrew Trick | 5749b8b | 2013-06-21 18:33:26 +0000 | [diff] [blame] | 1141 | if (RI.Defines) | 
|  | 1142 | continue; | 
|  | 1143 | // FoldMI does not define this physreg. Remove the LI segment. | 
|  | 1144 | assert(MO->isDead() && "Cannot fold physreg def"); | 
| Matthias Braun | cfb8ad2 | 2015-01-21 18:50:21 +0000 | [diff] [blame] | 1145 | SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); | 
|  | 1146 | LIS.removePhysRegDefAt(Reg, Idx); | 
| Andrew Trick | 5749b8b | 2013-06-21 18:33:26 +0000 | [diff] [blame] | 1147 | } | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1148 |  | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1149 | LIS.ReplaceMachineInstrInMaps(MI, FoldMI); | 
| Jakob Stoklund Olesen | bd953d1 | 2010-07-09 17:29:08 +0000 | [diff] [blame] | 1150 | MI->eraseFromParent(); | 
| Jakob Stoklund Olesen | eef48b6 | 2011-11-10 00:17:03 +0000 | [diff] [blame] | 1151 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1152 | // Insert any new instructions other than FoldMI into the LIS maps. | 
|  | 1153 | assert(!MIS.empty() && "Unexpected empty span of instructions!"); | 
|  | 1154 | for (MachineBasicBlock::iterator MII = MIS.begin(), End = MIS.end(); | 
|  | 1155 | MII != End; ++MII) | 
|  | 1156 | if (&*MII != FoldMI) | 
|  | 1157 | LIS.InsertMachineInstrInMaps(&*MII); | 
|  | 1158 |  | 
| Jakob Stoklund Olesen | eef48b6 | 2011-11-10 00:17:03 +0000 | [diff] [blame] | 1159 | // TII.foldMemoryOperand may have left some implicit operands on the | 
|  | 1160 | // instruction.  Strip them. | 
|  | 1161 | if (ImpReg) | 
|  | 1162 | for (unsigned i = FoldMI->getNumOperands(); i; --i) { | 
|  | 1163 | MachineOperand &MO = FoldMI->getOperand(i - 1); | 
|  | 1164 | if (!MO.isReg() || !MO.isImplicit()) | 
|  | 1165 | break; | 
|  | 1166 | if (MO.getReg() == ImpReg) | 
|  | 1167 | FoldMI->RemoveOperand(i - 1); | 
|  | 1168 | } | 
|  | 1169 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1170 | DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS, | 
|  | 1171 | "folded")); | 
|  | 1172 |  | 
| Jakob Stoklund Olesen | c94c967 | 2011-09-15 18:22:52 +0000 | [diff] [blame] | 1173 | if (!WasCopy) | 
|  | 1174 | ++NumFolded; | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1175 | else if (Ops.front().second == 0) | 
| Jakob Stoklund Olesen | c94c967 | 2011-09-15 18:22:52 +0000 | [diff] [blame] | 1176 | ++NumSpills; | 
|  | 1177 | else | 
|  | 1178 | ++NumReloads; | 
| Jakob Stoklund Olesen | 8656a45 | 2010-07-01 00:13:04 +0000 | [diff] [blame] | 1179 | return true; | 
|  | 1180 | } | 
|  | 1181 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1182 | void InlineSpiller::insertReload(unsigned NewVReg, | 
| Jakob Stoklund Olesen | 9f294a9 | 2011-04-18 20:23:27 +0000 | [diff] [blame] | 1183 | SlotIndex Idx, | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 1184 | MachineBasicBlock::iterator MI) { | 
|  | 1185 | MachineBasicBlock &MBB = *MI->getParent(); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1186 |  | 
|  | 1187 | MachineInstrSpan MIS(MI); | 
|  | 1188 | TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot, | 
|  | 1189 | MRI.getRegClass(NewVReg), &TRI); | 
|  | 1190 |  | 
|  | 1191 | LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI); | 
|  | 1192 |  | 
|  | 1193 | DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload", | 
|  | 1194 | NewVReg)); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 1195 | ++NumReloads; | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 1196 | } | 
|  | 1197 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1198 | /// insertSpill - Insert a spill of NewVReg after MI. | 
|  | 1199 | void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill, | 
|  | 1200 | MachineBasicBlock::iterator MI) { | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 1201 | MachineBasicBlock &MBB = *MI->getParent(); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1202 |  | 
|  | 1203 | MachineInstrSpan MIS(MI); | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1204 | TII.storeRegToStackSlot(MBB, std::next(MI), NewVReg, isKill, StackSlot, | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1205 | MRI.getRegClass(NewVReg), &TRI); | 
|  | 1206 |  | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1207 | LIS.InsertMachineInstrRangeInMaps(std::next(MI), MIS.end()); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1208 |  | 
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1209 | DEBUG(dumpMachineInstrRangeWithSlotIndex(std::next(MI), MIS.end(), LIS, | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1210 | "spill")); | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 1211 | ++NumSpills; | 
| Jakob Stoklund Olesen | bde96ad | 2010-06-30 23:03:52 +0000 | [diff] [blame] | 1212 | } | 
|  | 1213 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1214 | /// spillAroundUses - insert spill code around each use of Reg. | 
|  | 1215 | void InlineSpiller::spillAroundUses(unsigned Reg) { | 
| Jakob Stoklund Olesen | 31a0b5e | 2011-05-11 18:25:10 +0000 | [diff] [blame] | 1216 | DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n'); | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1217 | LiveInterval &OldLI = LIS.getInterval(Reg); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1218 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1219 | // Iterate over instructions using Reg. | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 1220 | for (MachineRegisterInfo::reg_bundle_iterator | 
|  | 1221 | RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end(); | 
|  | 1222 | RegI != E; ) { | 
| Owen Anderson | ec5d480 | 2014-03-14 05:02:18 +0000 | [diff] [blame] | 1223 | MachineInstr *MI = &*(RegI++); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1224 |  | 
| Jakob Stoklund Olesen | cf6c5c9 | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 1225 | // Debug values are not allowed to affect codegen. | 
|  | 1226 | if (MI->isDebugValue()) { | 
|  | 1227 | // Modify DBG_VALUE now that the value is in a spill slot. | 
| Adrian Prantl | db3e26d | 2013-09-16 23:29:03 +0000 | [diff] [blame] | 1228 | bool IsIndirect = MI->isIndirectDebugValue(); | 
| Adrian Prantl | c31ec1c | 2013-07-10 16:56:47 +0000 | [diff] [blame] | 1229 | uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0; | 
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1230 | const MDNode *Var = MI->getDebugVariable(); | 
|  | 1231 | const MDNode *Expr = MI->getDebugExpression(); | 
| Jakob Stoklund Olesen | cf6c5c9 | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 1232 | DebugLoc DL = MI->getDebugLoc(); | 
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1233 | DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI); | 
|  | 1234 | MachineBasicBlock *MBB = MI->getParent(); | 
| Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1235 | assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) && | 
| Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 1236 | "Expected inlined-at fields to agree"); | 
| David Blaikie | 0252265b | 2013-06-16 20:34:15 +0000 | [diff] [blame] | 1237 | BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE)) | 
| Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 1238 | .addFrameIndex(StackSlot) | 
|  | 1239 | .addImm(Offset) | 
|  | 1240 | .addMetadata(Var) | 
|  | 1241 | .addMetadata(Expr); | 
| Jakob Stoklund Olesen | cf6c5c9 | 2010-07-02 19:54:40 +0000 | [diff] [blame] | 1242 | continue; | 
|  | 1243 | } | 
|  | 1244 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1245 | // Ignore copies to/from snippets. We'll delete them. | 
|  | 1246 | if (SnippetCopies.count(MI)) | 
|  | 1247 | continue; | 
|  | 1248 |  | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1249 | // Stack slot accesses may coalesce away. | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1250 | if (coalesceStackAccess(MI, Reg)) | 
| Jakob Stoklund Olesen | 7fd4905 | 2010-08-04 22:35:11 +0000 | [diff] [blame] | 1251 | continue; | 
|  | 1252 |  | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1253 | // Analyze instruction. | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1254 | SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops; | 
| James Molloy | 381fab9 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 1255 | MIBundleOperands::VirtRegInfo RI = | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1256 | MIBundleOperands(MI).analyzeVirtReg(Reg, &Ops); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1257 |  | 
| Jakob Stoklund Olesen | 9f294a9 | 2011-04-18 20:23:27 +0000 | [diff] [blame] | 1258 | // Find the slot index where this instruction reads and writes OldLI. | 
|  | 1259 | // This is usually the def slot, except for tied early clobbers. | 
| Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 1260 | SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); | 
|  | 1261 | if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true))) | 
| Jakob Stoklund Olesen | 9f294a9 | 2011-04-18 20:23:27 +0000 | [diff] [blame] | 1262 | if (SlotIndex::isSameInstr(Idx, VNI->def)) | 
|  | 1263 | Idx = VNI->def; | 
|  | 1264 |  | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 1265 | // Check for a sibling copy. | 
|  | 1266 | unsigned SibReg = isFullCopyOf(MI, Reg); | 
| Jakob Stoklund Olesen | e55003f | 2011-03-20 05:44:58 +0000 | [diff] [blame] | 1267 | if (SibReg && isSibling(SibReg)) { | 
| Jakob Stoklund Olesen | 31a0b5e | 2011-05-11 18:25:10 +0000 | [diff] [blame] | 1268 | // This may actually be a copy between snippets. | 
|  | 1269 | if (isRegToSpill(SibReg)) { | 
|  | 1270 | DEBUG(dbgs() << "Found new snippet copy: " << *MI); | 
|  | 1271 | SnippetCopies.insert(MI); | 
|  | 1272 | continue; | 
|  | 1273 | } | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1274 | if (RI.Writes) { | 
| Jakob Stoklund Olesen | e55003f | 2011-03-20 05:44:58 +0000 | [diff] [blame] | 1275 | // Hoist the spill of a sib-reg copy. | 
|  | 1276 | if (hoistSpill(OldLI, MI)) { | 
|  | 1277 | // This COPY is now dead, the value is already in the stack slot. | 
|  | 1278 | MI->getOperand(0).setIsDead(); | 
|  | 1279 | DeadDefs.push_back(MI); | 
|  | 1280 | continue; | 
|  | 1281 | } | 
|  | 1282 | } else { | 
|  | 1283 | // This is a reload for a sib-reg copy. Drop spills downstream. | 
| Jakob Stoklund Olesen | e55003f | 2011-03-20 05:44:58 +0000 | [diff] [blame] | 1284 | LiveInterval &SibLI = LIS.getInterval(SibReg); | 
|  | 1285 | eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx)); | 
|  | 1286 | // The COPY will fold to a reload below. | 
|  | 1287 | } | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 1288 | } | 
|  | 1289 |  | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 1290 | // Attempt to fold memory ops. | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1291 | if (foldMemoryOperand(Ops)) | 
| Jakob Stoklund Olesen | 9603718 | 2010-07-02 17:44:57 +0000 | [diff] [blame] | 1292 | continue; | 
|  | 1293 |  | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1294 | // Create a new virtual register for spill/fill. | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1295 | // FIXME: Infer regclass from instruction alone. | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1296 | unsigned NewVReg = Edit->createFrom(Reg); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1297 |  | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1298 | if (RI.Reads) | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1299 | insertReload(NewVReg, Idx, MI); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1300 |  | 
|  | 1301 | // Rewrite instruction operands. | 
|  | 1302 | bool hasLiveDef = false; | 
|  | 1303 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1304 | MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second); | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1305 | MO.setReg(NewVReg); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1306 | if (MO.isUse()) { | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1307 | if (!Ops[i].first->isRegTiedToDefOperand(Ops[i].second)) | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1308 | MO.setIsKill(); | 
|  | 1309 | } else { | 
|  | 1310 | if (!MO.isDead()) | 
|  | 1311 | hasLiveDef = true; | 
|  | 1312 | } | 
|  | 1313 | } | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1314 | DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n'); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1315 |  | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1316 | // FIXME: Use a second vreg if instruction has no tied ops. | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1317 | if (RI.Writes) | 
| Jakob Stoklund Olesen | abe8c09 | 2012-03-01 01:43:25 +0000 | [diff] [blame] | 1318 | if (hasLiveDef) | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1319 | insertSpill(NewVReg, true, MI); | 
| Jakob Stoklund Olesen | f888911 | 2010-06-29 23:58:39 +0000 | [diff] [blame] | 1320 | } | 
|  | 1321 | } | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1322 |  | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1323 | /// spillAll - Spill all registers remaining after rematerialization. | 
|  | 1324 | void InlineSpiller::spillAll() { | 
|  | 1325 | // Update LiveStacks now that we are committed to spilling. | 
|  | 1326 | if (StackSlot == VirtRegMap::NO_STACK_SLOT) { | 
|  | 1327 | StackSlot = VRM.assignVirt2StackSlot(Original); | 
|  | 1328 | StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original)); | 
| Jakob Stoklund Olesen | ad6b22e | 2012-02-04 05:20:49 +0000 | [diff] [blame] | 1329 | StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator()); | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1330 | } else | 
|  | 1331 | StackInt = &LSS.getInterval(StackSlot); | 
|  | 1332 |  | 
|  | 1333 | if (Original != Edit->getReg()) | 
|  | 1334 | VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot); | 
|  | 1335 |  | 
|  | 1336 | assert(StackInt->getNumValNums() == 1 && "Bad stack interval values"); | 
|  | 1337 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) | 
| Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1338 | StackInt->MergeSegmentsInAsValue(LIS.getInterval(RegsToSpill[i]), | 
|  | 1339 | StackInt->getValNumInfo(0)); | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1340 | DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n'); | 
|  | 1341 |  | 
|  | 1342 | // Spill around uses of all RegsToSpill. | 
|  | 1343 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) | 
|  | 1344 | spillAroundUses(RegsToSpill[i]); | 
|  | 1345 |  | 
|  | 1346 | // Hoisted spills may cause dead code. | 
|  | 1347 | if (!DeadDefs.empty()) { | 
|  | 1348 | DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n"); | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 1349 | Edit->eliminateDeadDefs(DeadDefs, RegsToSpill); | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1350 | } | 
|  | 1351 |  | 
|  | 1352 | // Finally delete the SnippetCopies. | 
| Jakob Stoklund Olesen | 31a0b5e | 2011-05-11 18:25:10 +0000 | [diff] [blame] | 1353 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) { | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 1354 | for (MachineRegisterInfo::reg_instr_iterator | 
|  | 1355 | RI = MRI.reg_instr_begin(RegsToSpill[i]), E = MRI.reg_instr_end(); | 
|  | 1356 | RI != E; ) { | 
|  | 1357 | MachineInstr *MI = &*(RI++); | 
| Jakob Stoklund Olesen | 31a0b5e | 2011-05-11 18:25:10 +0000 | [diff] [blame] | 1358 | assert(SnippetCopies.count(MI) && "Remaining use wasn't a snippet copy"); | 
|  | 1359 | // FIXME: Do this with a LiveRangeEdit callback. | 
| Jakob Stoklund Olesen | 31a0b5e | 2011-05-11 18:25:10 +0000 | [diff] [blame] | 1360 | LIS.RemoveMachineInstrFromMaps(MI); | 
|  | 1361 | MI->eraseFromParent(); | 
|  | 1362 | } | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1363 | } | 
|  | 1364 |  | 
|  | 1365 | // Delete all spilled registers. | 
|  | 1366 | for (unsigned i = 0, e = RegsToSpill.size(); i != e; ++i) | 
| Pete Cooper | 2bde2f4 | 2012-04-02 22:22:53 +0000 | [diff] [blame] | 1367 | Edit->eraseVirtReg(RegsToSpill[i]); | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1368 | } | 
|  | 1369 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1370 | void InlineSpiller::spill(LiveRangeEdit &edit) { | 
| Jakob Stoklund Olesen | c5a8c08 | 2011-05-05 17:22:53 +0000 | [diff] [blame] | 1371 | ++NumSpilledRanges; | 
| Jakob Stoklund Olesen | a00bab2 | 2011-03-14 19:56:43 +0000 | [diff] [blame] | 1372 | Edit = &edit; | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1373 | assert(!TargetRegisterInfo::isStackSlot(edit.getReg()) | 
|  | 1374 | && "Trying to spill a stack slot."); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 1375 | // Share a stack slot among all descendants of Original. | 
|  | 1376 | Original = VRM.getOriginal(edit.getReg()); | 
|  | 1377 | StackSlot = VRM.getStackSlot(Original); | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1378 | StackInt = nullptr; | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 1379 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1380 | DEBUG(dbgs() << "Inline spilling " | 
| Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1381 | << TRI.getRegClassName(MRI.getRegClass(edit.getReg())) | 
| Matthias Braun | f6fe6bf | 2013-10-10 21:29:05 +0000 | [diff] [blame] | 1382 | << ':' << edit.getParent() | 
| Mark Lacey | 9d8103d | 2013-08-14 23:50:16 +0000 | [diff] [blame] | 1383 | << "\nFrom original " << PrintReg(Original) << '\n'); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1384 | assert(edit.getParent().isSpillable() && | 
|  | 1385 | "Attempting to spill already spilled value."); | 
| Jakob Stoklund Olesen | 27320cb | 2011-03-18 04:23:06 +0000 | [diff] [blame] | 1386 | assert(DeadDefs.empty() && "Previous spill didn't remove dead defs"); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1387 |  | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1388 | collectRegsToSpill(); | 
| Jakob Stoklund Olesen | a0d5ec1 | 2011-03-15 21:13:25 +0000 | [diff] [blame] | 1389 | analyzeSiblingValues(); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1390 | reMaterializeAll(); | 
|  | 1391 |  | 
|  | 1392 | // Remat may handle everything. | 
| Jakob Stoklund Olesen | e991f72 | 2011-03-29 21:20:19 +0000 | [diff] [blame] | 1393 | if (!RegsToSpill.empty()) | 
|  | 1394 | spillAll(); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1395 |  | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 1396 | Edit->calculateRegClassAndHint(MF, Loops, MBFI); | 
| Jakob Stoklund Olesen | a86595e | 2011-03-12 04:17:20 +0000 | [diff] [blame] | 1397 | } |