Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 1 | //===-- PreAllocSplitting.cpp - Pre-allocation Interval Spltting Pass. ----===// |
| 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 | // This file implements the machine instruction level pre-register allocation |
| 11 | // live interval splitting pass. It finds live interval barriers, i.e. |
| 12 | // instructions which will kill all physical registers in certain register |
| 13 | // classes, and split all live intervals which cross the barrier. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "pre-alloc-split" |
| 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineDominators.h" |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
| 26 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Target/TargetOptions.h" |
| 30 | #include "llvm/Target/TargetRegisterInfo.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DepthFirstIterator.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 39 | static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 40 | static cl::opt<int> DeadSplitLimit("dead-split-limit", cl::init(-1), cl::Hidden); |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 41 | |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 42 | STATISTIC(NumSplits, "Number of intervals split"); |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 43 | STATISTIC(NumRemats, "Number of intervals split by rematerialization"); |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 44 | STATISTIC(NumFolds, "Number of intervals split with spill folding"); |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 45 | STATISTIC(NumRenumbers, "Number of intervals renumbered into new registers"); |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 46 | STATISTIC(NumDeadSpills, "Number of dead spills removed"); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 47 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 50 | MachineFunction *CurrMF; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 51 | const TargetMachine *TM; |
| 52 | const TargetInstrInfo *TII; |
| 53 | MachineFrameInfo *MFI; |
| 54 | MachineRegisterInfo *MRI; |
| 55 | LiveIntervals *LIs; |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 56 | LiveStacks *LSs; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 57 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 58 | // Barrier - Current barrier being processed. |
| 59 | MachineInstr *Barrier; |
| 60 | |
| 61 | // BarrierMBB - Basic block where the barrier resides in. |
| 62 | MachineBasicBlock *BarrierMBB; |
| 63 | |
| 64 | // Barrier - Current barrier index. |
| 65 | unsigned BarrierIdx; |
| 66 | |
| 67 | // CurrLI - Current live interval being split. |
| 68 | LiveInterval *CurrLI; |
| 69 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 70 | // CurrSLI - Current stack slot live interval. |
| 71 | LiveInterval *CurrSLI; |
| 72 | |
| 73 | // CurrSValNo - Current val# for the stack slot live interval. |
| 74 | VNInfo *CurrSValNo; |
| 75 | |
| 76 | // IntervalSSMap - A map from live interval to spill slots. |
| 77 | DenseMap<unsigned, int> IntervalSSMap; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 78 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 79 | // Def2SpillMap - A map from a def instruction index to spill index. |
| 80 | DenseMap<unsigned, unsigned> Def2SpillMap; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 81 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 82 | public: |
| 83 | static char ID; |
| 84 | PreAllocSplitting() : MachineFunctionPass(&ID) {} |
| 85 | |
| 86 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 87 | |
| 88 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 89 | AU.addRequired<LiveIntervals>(); |
| 90 | AU.addPreserved<LiveIntervals>(); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 91 | AU.addRequired<LiveStacks>(); |
| 92 | AU.addPreserved<LiveStacks>(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 93 | AU.addPreserved<RegisterCoalescer>(); |
| 94 | if (StrongPHIElim) |
| 95 | AU.addPreservedID(StrongPHIEliminationID); |
| 96 | else |
| 97 | AU.addPreservedID(PHIEliminationID); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 98 | AU.addRequired<MachineDominatorTree>(); |
| 99 | AU.addRequired<MachineLoopInfo>(); |
| 100 | AU.addPreserved<MachineDominatorTree>(); |
| 101 | AU.addPreserved<MachineLoopInfo>(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 102 | MachineFunctionPass::getAnalysisUsage(AU); |
| 103 | } |
| 104 | |
| 105 | virtual void releaseMemory() { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 106 | IntervalSSMap.clear(); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 107 | Def2SpillMap.clear(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | virtual const char *getPassName() const { |
| 111 | return "Pre-Register Allocaton Live Interval Splitting"; |
| 112 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 113 | |
| 114 | /// print - Implement the dump method. |
| 115 | virtual void print(std::ostream &O, const Module* M = 0) const { |
| 116 | LIs->print(O, M); |
| 117 | } |
| 118 | |
| 119 | void print(std::ostream *O, const Module* M = 0) const { |
| 120 | if (O) print(*O, M); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | MachineBasicBlock::iterator |
| 125 | findNextEmptySlot(MachineBasicBlock*, MachineInstr*, |
| 126 | unsigned&); |
| 127 | |
| 128 | MachineBasicBlock::iterator |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 129 | findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 130 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 131 | |
| 132 | MachineBasicBlock::iterator |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 133 | findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 134 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 135 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 136 | int CreateSpillStackSlot(unsigned, const TargetRegisterClass *); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 137 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 138 | bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned, |
| 139 | unsigned&, int&) const; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 140 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 141 | void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 142 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 143 | bool SplitRegLiveInterval(LiveInterval*); |
| 144 | |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 145 | bool SplitRegLiveIntervals(const TargetRegisterClass **, |
| 146 | SmallPtrSet<LiveInterval*, 8>&); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 147 | |
| 148 | bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB, |
| 149 | MachineBasicBlock* BarrierMBB); |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 150 | bool Rematerialize(unsigned vreg, VNInfo* ValNo, |
| 151 | MachineInstr* DefMI, |
| 152 | MachineBasicBlock::iterator RestorePt, |
| 153 | unsigned RestoreIdx, |
| 154 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB); |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 155 | MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC, |
| 156 | MachineInstr* DefMI, |
| 157 | MachineInstr* Barrier, |
| 158 | MachineBasicBlock* MBB, |
| 159 | int& SS, |
| 160 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB); |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 161 | void RenumberValno(VNInfo* VN); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 162 | void ReconstructLiveInterval(LiveInterval* LI); |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 163 | bool removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 164 | unsigned getNumberOfNonSpills(SmallPtrSet<MachineInstr*, 4>& MIs, |
| 165 | unsigned Reg, int FrameIndex, bool& TwoAddr); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 166 | VNInfo* PerformPHIConstruction(MachineBasicBlock::iterator use, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 167 | MachineBasicBlock* MBB, |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 168 | LiveInterval* LI, |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 169 | SmallPtrSet<MachineInstr*, 4>& Visited, |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 170 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs, |
| 171 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses, |
| 172 | DenseMap<MachineInstr*, VNInfo*>& NewVNs, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 173 | DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut, |
| 174 | DenseMap<MachineBasicBlock*, VNInfo*>& Phis, |
| 175 | bool toplevel, bool intrablock); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 176 | }; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 177 | } // end anonymous namespace |
| 178 | |
| 179 | char PreAllocSplitting::ID = 0; |
| 180 | |
| 181 | static RegisterPass<PreAllocSplitting> |
| 182 | X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting"); |
| 183 | |
| 184 | const PassInfo *const llvm::PreAllocSplittingID = &X; |
| 185 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 186 | |
| 187 | /// findNextEmptySlot - Find a gap after the given machine instruction in the |
| 188 | /// instruction index map. If there isn't one, return end(). |
| 189 | MachineBasicBlock::iterator |
| 190 | PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI, |
| 191 | unsigned &SpotIndex) { |
| 192 | MachineBasicBlock::iterator MII = MI; |
| 193 | if (++MII != MBB->end()) { |
| 194 | unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII)); |
| 195 | if (Index) { |
| 196 | SpotIndex = Index; |
| 197 | return MII; |
| 198 | } |
| 199 | } |
| 200 | return MBB->end(); |
| 201 | } |
| 202 | |
| 203 | /// findSpillPoint - Find a gap as far away from the given MI that's suitable |
| 204 | /// for spilling the current live interval. The index must be before any |
| 205 | /// defs and uses of the live interval register in the mbb. Return begin() if |
| 206 | /// none is found. |
| 207 | MachineBasicBlock::iterator |
| 208 | PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 209 | MachineInstr *DefMI, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 210 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 211 | unsigned &SpillIndex) { |
| 212 | MachineBasicBlock::iterator Pt = MBB->begin(); |
| 213 | |
| 214 | // Go top down if RefsInMBB is empty. |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 215 | if (RefsInMBB.empty() && !DefMI) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 216 | MachineBasicBlock::iterator MII = MBB->begin(); |
| 217 | MachineBasicBlock::iterator EndPt = MI; |
| 218 | do { |
| 219 | ++MII; |
| 220 | unsigned Index = LIs->getInstructionIndex(MII); |
| 221 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 222 | if (Gap) { |
| 223 | Pt = MII; |
| 224 | SpillIndex = Gap; |
| 225 | break; |
| 226 | } |
| 227 | } while (MII != EndPt); |
| 228 | } else { |
| 229 | MachineBasicBlock::iterator MII = MI; |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 230 | MachineBasicBlock::iterator EndPt = DefMI |
| 231 | ? MachineBasicBlock::iterator(DefMI) : MBB->begin(); |
| 232 | while (MII != EndPt && !RefsInMBB.count(MII)) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 233 | unsigned Index = LIs->getInstructionIndex(MII); |
| 234 | if (LIs->hasGapBeforeInstr(Index)) { |
| 235 | Pt = MII; |
| 236 | SpillIndex = LIs->findGapBeforeInstr(Index, true); |
| 237 | } |
| 238 | --MII; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return Pt; |
| 243 | } |
| 244 | |
| 245 | /// findRestorePoint - Find a gap in the instruction index map that's suitable |
| 246 | /// for restoring the current live interval value. The index must be before any |
| 247 | /// uses of the live interval register in the mbb. Return end() if none is |
| 248 | /// found. |
| 249 | MachineBasicBlock::iterator |
| 250 | PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 251 | unsigned LastIdx, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 252 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 253 | unsigned &RestoreIndex) { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 254 | // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb |
| 255 | // begin index accordingly. |
Owen Anderson | 5a92d4e | 2008-11-18 20:53:59 +0000 | [diff] [blame] | 256 | MachineBasicBlock::iterator Pt = MBB->end(); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 257 | unsigned EndIdx = LIs->getMBBEndIdx(MBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 258 | |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 259 | // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond |
| 260 | // the last index in the live range. |
| 261 | if (RefsInMBB.empty() && LastIdx >= EndIdx) { |
Owen Anderson | 711fd3d | 2008-11-13 21:53:14 +0000 | [diff] [blame] | 262 | MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 263 | MachineBasicBlock::iterator EndPt = MI; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 264 | --MII; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 265 | do { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 266 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | 56ab0de | 2008-10-24 18:46:44 +0000 | [diff] [blame] | 267 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 268 | if (Gap) { |
| 269 | Pt = MII; |
| 270 | RestoreIndex = Gap; |
| 271 | break; |
| 272 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 273 | --MII; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 274 | } while (MII != EndPt); |
| 275 | } else { |
| 276 | MachineBasicBlock::iterator MII = MI; |
| 277 | MII = ++MII; |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 278 | // FIXME: Limit the number of instructions to examine to reduce |
| 279 | // compile time? |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 280 | while (MII != MBB->end()) { |
| 281 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 282 | if (Index > LastIdx) |
| 283 | break; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 284 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 285 | if (Gap) { |
| 286 | Pt = MII; |
| 287 | RestoreIndex = Gap; |
| 288 | } |
| 289 | if (RefsInMBB.count(MII)) |
| 290 | break; |
| 291 | ++MII; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return Pt; |
| 296 | } |
| 297 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 298 | /// CreateSpillStackSlot - Create a stack slot for the live interval being |
| 299 | /// split. If the live interval was previously split, just reuse the same |
| 300 | /// slot. |
| 301 | int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg, |
| 302 | const TargetRegisterClass *RC) { |
| 303 | int SS; |
| 304 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg); |
| 305 | if (I != IntervalSSMap.end()) { |
| 306 | SS = I->second; |
| 307 | } else { |
| 308 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
| 309 | IntervalSSMap[Reg] = SS; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 310 | } |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 311 | |
| 312 | // Create live interval for stack slot. |
| 313 | CurrSLI = &LSs->getOrCreateInterval(SS); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 314 | if (CurrSLI->hasAtLeastOneValue()) |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 315 | CurrSValNo = CurrSLI->getValNumInfo(0); |
| 316 | else |
| 317 | CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator()); |
| 318 | return SS; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 321 | /// IsAvailableInStack - Return true if register is available in a split stack |
| 322 | /// slot at the specified index. |
| 323 | bool |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 324 | PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB, |
| 325 | unsigned Reg, unsigned DefIndex, |
| 326 | unsigned RestoreIndex, unsigned &SpillIndex, |
| 327 | int& SS) const { |
| 328 | if (!DefMBB) |
| 329 | return false; |
| 330 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 331 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg); |
| 332 | if (I == IntervalSSMap.end()) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 333 | return false; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 334 | DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex); |
| 335 | if (II == Def2SpillMap.end()) |
| 336 | return false; |
| 337 | |
| 338 | // If last spill of def is in the same mbb as barrier mbb (where restore will |
| 339 | // be), make sure it's not below the intended restore index. |
| 340 | // FIXME: Undo the previous spill? |
| 341 | assert(LIs->getMBBFromIndex(II->second) == DefMBB); |
| 342 | if (DefMBB == BarrierMBB && II->second >= RestoreIndex) |
| 343 | return false; |
| 344 | |
| 345 | SS = I->second; |
| 346 | SpillIndex = II->second; |
| 347 | return true; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 350 | /// UpdateSpillSlotInterval - Given the specified val# of the register live |
| 351 | /// interval being split, and the spill and restore indicies, update the live |
| 352 | /// interval of the spill stack slot. |
| 353 | void |
| 354 | PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex, |
| 355 | unsigned RestoreIndex) { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 356 | assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB && |
| 357 | "Expect restore in the barrier mbb"); |
| 358 | |
| 359 | MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex); |
| 360 | if (MBB == BarrierMBB) { |
| 361 | // Intra-block spill + restore. We are done. |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 362 | LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo); |
| 363 | CurrSLI->addRange(SLR); |
| 364 | return; |
| 365 | } |
| 366 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 367 | SmallPtrSet<MachineBasicBlock*, 4> Processed; |
| 368 | unsigned EndIdx = LIs->getMBBEndIdx(MBB); |
| 369 | LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 370 | CurrSLI->addRange(SLR); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 371 | Processed.insert(MBB); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 372 | |
| 373 | // Start from the spill mbb, figure out the extend of the spill slot's |
| 374 | // live interval. |
| 375 | SmallVector<MachineBasicBlock*, 4> WorkList; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 376 | const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex); |
| 377 | if (LR->end > EndIdx) |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 378 | // If live range extend beyond end of mbb, add successors to work list. |
| 379 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 380 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 381 | WorkList.push_back(*SI); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 382 | |
| 383 | while (!WorkList.empty()) { |
| 384 | MachineBasicBlock *MBB = WorkList.back(); |
| 385 | WorkList.pop_back(); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 386 | if (Processed.count(MBB)) |
| 387 | continue; |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 388 | unsigned Idx = LIs->getMBBStartIdx(MBB); |
| 389 | LR = CurrLI->getLiveRangeContaining(Idx); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 390 | if (LR && LR->valno == ValNo) { |
| 391 | EndIdx = LIs->getMBBEndIdx(MBB); |
| 392 | if (Idx <= RestoreIndex && RestoreIndex < EndIdx) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 393 | // Spill slot live interval stops at the restore. |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 394 | LiveRange SLR(Idx, RestoreIndex, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 395 | CurrSLI->addRange(SLR); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 396 | } else if (LR->end > EndIdx) { |
| 397 | // Live range extends beyond end of mbb, process successors. |
| 398 | LiveRange SLR(Idx, EndIdx+1, CurrSValNo); |
| 399 | CurrSLI->addRange(SLR); |
| 400 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 401 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 402 | WorkList.push_back(*SI); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 403 | } else { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 404 | LiveRange SLR(Idx, LR->end, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 405 | CurrSLI->addRange(SLR); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 406 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 407 | Processed.insert(MBB); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 412 | /// PerformPHIConstruction - From properly set up use and def lists, use a PHI |
| 413 | /// construction algorithm to compute the ranges and valnos for an interval. |
| 414 | VNInfo* PreAllocSplitting::PerformPHIConstruction( |
| 415 | MachineBasicBlock::iterator use, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 416 | MachineBasicBlock* MBB, |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 417 | LiveInterval* LI, |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 418 | SmallPtrSet<MachineInstr*, 4>& Visited, |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 419 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Defs, |
| 420 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> >& Uses, |
| 421 | DenseMap<MachineInstr*, VNInfo*>& NewVNs, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 422 | DenseMap<MachineBasicBlock*, VNInfo*>& LiveOut, |
| 423 | DenseMap<MachineBasicBlock*, VNInfo*>& Phis, |
| 424 | bool toplevel, bool intrablock) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 425 | // Return memoized result if it's available. |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 426 | if (toplevel && Visited.count(use) && NewVNs.count(use)) |
| 427 | return NewVNs[use]; |
| 428 | else if (!toplevel && intrablock && NewVNs.count(use)) |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 429 | return NewVNs[use]; |
| 430 | else if (!intrablock && LiveOut.count(MBB)) |
| 431 | return LiveOut[MBB]; |
| 432 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 433 | typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap; |
| 434 | |
| 435 | // Check if our block contains any uses or defs. |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 436 | bool ContainsDefs = Defs.count(MBB); |
| 437 | bool ContainsUses = Uses.count(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 438 | |
| 439 | VNInfo* ret = 0; |
| 440 | |
| 441 | // Enumerate the cases of use/def contaning blocks. |
| 442 | if (!ContainsDefs && !ContainsUses) { |
| 443 | Fallback: |
| 444 | // NOTE: Because this is the fallback case from other cases, we do NOT |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 445 | // assume that we are not intrablock here. |
| 446 | if (Phis.count(MBB)) return Phis[MBB]; |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 447 | |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 448 | unsigned StartIndex = LIs->getMBBStartIdx(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 449 | |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 450 | if (MBB->pred_size() == 1) { |
| 451 | Phis[MBB] = ret = PerformPHIConstruction((*MBB->pred_begin())->end(), |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 452 | *(MBB->pred_begin()), LI, Visited, |
| 453 | Defs, Uses, NewVNs, LiveOut, Phis, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 454 | false, false); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 455 | unsigned EndIndex = 0; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 456 | if (intrablock) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 457 | EndIndex = LIs->getInstructionIndex(use); |
| 458 | EndIndex = LiveIntervals::getUseIndex(EndIndex); |
| 459 | } else |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 460 | EndIndex = LIs->getMBBEndIdx(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 461 | |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 462 | LI->addRange(LiveRange(StartIndex, EndIndex+1, ret)); |
Owen Anderson | e1762c9 | 2009-01-12 03:10:40 +0000 | [diff] [blame] | 463 | if (intrablock) |
| 464 | LI->addKill(ret, EndIndex); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 465 | } else { |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 466 | Phis[MBB] = ret = LI->getNextValue(~0U, /*FIXME*/ 0, |
| 467 | LIs->getVNInfoAllocator()); |
| 468 | if (!intrablock) LiveOut[MBB] = ret; |
| 469 | |
| 470 | // If there are no uses or defs between our starting point and the |
| 471 | // beginning of the block, then recursive perform phi construction |
| 472 | // on our predecessors. |
| 473 | DenseMap<MachineBasicBlock*, VNInfo*> IncomingVNs; |
| 474 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 475 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 476 | VNInfo* Incoming = PerformPHIConstruction((*PI)->end(), *PI, LI, |
| 477 | Visited, Defs, Uses, NewVNs, |
| 478 | LiveOut, Phis, false, false); |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 479 | if (Incoming != 0) |
| 480 | IncomingVNs[*PI] = Incoming; |
| 481 | } |
| 482 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 483 | // Otherwise, merge the incoming VNInfos with a phi join. Create a new |
| 484 | // VNInfo to represent the joined value. |
| 485 | for (DenseMap<MachineBasicBlock*, VNInfo*>::iterator I = |
| 486 | IncomingVNs.begin(), E = IncomingVNs.end(); I != E; ++I) { |
| 487 | I->second->hasPHIKill = true; |
| 488 | unsigned KillIndex = LIs->getMBBEndIdx(I->first); |
| 489 | LI->addKill(I->second, KillIndex); |
| 490 | } |
| 491 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 492 | unsigned EndIndex = 0; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 493 | if (intrablock) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 494 | EndIndex = LIs->getInstructionIndex(use); |
| 495 | EndIndex = LiveIntervals::getUseIndex(EndIndex); |
| 496 | } else |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 497 | EndIndex = LIs->getMBBEndIdx(MBB); |
| 498 | LI->addRange(LiveRange(StartIndex, EndIndex+1, ret)); |
Owen Anderson | e1762c9 | 2009-01-12 03:10:40 +0000 | [diff] [blame] | 499 | if (intrablock) |
| 500 | LI->addKill(ret, EndIndex); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 501 | } |
| 502 | } else if (ContainsDefs && !ContainsUses) { |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 503 | SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB]; |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 504 | |
| 505 | // Search for the def in this block. If we don't find it before the |
| 506 | // instruction we care about, go to the fallback case. Note that that |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 507 | // should never happen: this cannot be intrablock, so use should |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 508 | // always be an end() iterator. |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 509 | assert(use == MBB->end() && "No use marked in intrablock"); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 510 | |
| 511 | MachineBasicBlock::iterator walker = use; |
| 512 | --walker; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 513 | while (walker != MBB->begin()) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 514 | if (BlockDefs.count(walker)) { |
| 515 | break; |
| 516 | } else |
| 517 | --walker; |
| 518 | |
| 519 | // Once we've found it, extend its VNInfo to our instruction. |
| 520 | unsigned DefIndex = LIs->getInstructionIndex(walker); |
| 521 | DefIndex = LiveIntervals::getDefIndex(DefIndex); |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 522 | unsigned EndIndex = LIs->getMBBEndIdx(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 523 | |
| 524 | ret = NewVNs[walker]; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 525 | LI->addRange(LiveRange(DefIndex, EndIndex+1, ret)); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 526 | } else if (!ContainsDefs && ContainsUses) { |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 527 | SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB]; |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 528 | |
| 529 | // Search for the use in this block that precedes the instruction we care |
| 530 | // about, going to the fallback case if we don't find it. |
| 531 | |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 532 | if (use == MBB->begin()) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 533 | goto Fallback; |
| 534 | |
| 535 | MachineBasicBlock::iterator walker = use; |
| 536 | --walker; |
| 537 | bool found = false; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 538 | while (walker != MBB->begin()) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 539 | if (BlockUses.count(walker)) { |
| 540 | found = true; |
| 541 | break; |
| 542 | } else |
| 543 | --walker; |
| 544 | |
| 545 | // Must check begin() too. |
Duncan Sands | 2b7fc1e | 2008-12-29 08:05:02 +0000 | [diff] [blame] | 546 | if (!found) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 547 | if (BlockUses.count(walker)) |
| 548 | found = true; |
| 549 | else |
| 550 | goto Fallback; |
Duncan Sands | 2b7fc1e | 2008-12-29 08:05:02 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 553 | unsigned UseIndex = LIs->getInstructionIndex(walker); |
| 554 | UseIndex = LiveIntervals::getUseIndex(UseIndex); |
| 555 | unsigned EndIndex = 0; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 556 | if (intrablock) { |
| 557 | EndIndex = LIs->getInstructionIndex(use); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 558 | EndIndex = LiveIntervals::getUseIndex(EndIndex); |
| 559 | } else |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 560 | EndIndex = LIs->getMBBEndIdx(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 561 | |
| 562 | // Now, recursively phi construct the VNInfo for the use we found, |
| 563 | // and then extend it to include the instruction we care about |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 564 | ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 565 | NewVNs, LiveOut, Phis, false, true); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 566 | |
Owen Anderson | b4b8436 | 2009-01-26 21:57:31 +0000 | [diff] [blame] | 567 | LI->addRange(LiveRange(UseIndex, EndIndex+1, ret)); |
| 568 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 569 | // FIXME: Need to set kills properly for inter-block stuff. |
Owen Anderson | d4f6fe5 | 2008-12-28 23:35:13 +0000 | [diff] [blame] | 570 | if (LI->isKill(ret, UseIndex)) LI->removeKill(ret, UseIndex); |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 571 | if (intrablock) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 572 | LI->addKill(ret, EndIndex); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 573 | } else if (ContainsDefs && ContainsUses){ |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 574 | SmallPtrSet<MachineInstr*, 2>& BlockDefs = Defs[MBB]; |
| 575 | SmallPtrSet<MachineInstr*, 2>& BlockUses = Uses[MBB]; |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 576 | |
| 577 | // This case is basically a merging of the two preceding case, with the |
| 578 | // special note that checking for defs must take precedence over checking |
| 579 | // for uses, because of two-address instructions. |
| 580 | |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 581 | if (use == MBB->begin()) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 582 | goto Fallback; |
| 583 | |
| 584 | MachineBasicBlock::iterator walker = use; |
| 585 | --walker; |
| 586 | bool foundDef = false; |
| 587 | bool foundUse = false; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 588 | while (walker != MBB->begin()) |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 589 | if (BlockDefs.count(walker)) { |
| 590 | foundDef = true; |
| 591 | break; |
| 592 | } else if (BlockUses.count(walker)) { |
| 593 | foundUse = true; |
| 594 | break; |
| 595 | } else |
| 596 | --walker; |
| 597 | |
| 598 | // Must check begin() too. |
Duncan Sands | 2b7fc1e | 2008-12-29 08:05:02 +0000 | [diff] [blame] | 599 | if (!foundDef && !foundUse) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 600 | if (BlockDefs.count(walker)) |
| 601 | foundDef = true; |
| 602 | else if (BlockUses.count(walker)) |
| 603 | foundUse = true; |
| 604 | else |
| 605 | goto Fallback; |
Duncan Sands | 2b7fc1e | 2008-12-29 08:05:02 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 608 | unsigned StartIndex = LIs->getInstructionIndex(walker); |
| 609 | StartIndex = foundDef ? LiveIntervals::getDefIndex(StartIndex) : |
| 610 | LiveIntervals::getUseIndex(StartIndex); |
| 611 | unsigned EndIndex = 0; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 612 | if (intrablock) { |
| 613 | EndIndex = LIs->getInstructionIndex(use); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 614 | EndIndex = LiveIntervals::getUseIndex(EndIndex); |
| 615 | } else |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 616 | EndIndex = LIs->getMBBEndIdx(MBB); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 617 | |
| 618 | if (foundDef) |
| 619 | ret = NewVNs[walker]; |
| 620 | else |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 621 | ret = PerformPHIConstruction(walker, MBB, LI, Visited, Defs, Uses, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 622 | NewVNs, LiveOut, Phis, false, true); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 623 | |
Owen Anderson | b4b8436 | 2009-01-26 21:57:31 +0000 | [diff] [blame] | 624 | LI->addRange(LiveRange(StartIndex, EndIndex+1, ret)); |
| 625 | |
Owen Anderson | d4f6fe5 | 2008-12-28 23:35:13 +0000 | [diff] [blame] | 626 | if (foundUse && LI->isKill(ret, StartIndex)) |
| 627 | LI->removeKill(ret, StartIndex); |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 628 | if (intrablock) { |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 629 | LI->addKill(ret, EndIndex); |
| 630 | } |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | // Memoize results so we don't have to recompute them. |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 634 | if (!intrablock) LiveOut[MBB] = ret; |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 635 | else { |
Owen Anderson | e1762c9 | 2009-01-12 03:10:40 +0000 | [diff] [blame] | 636 | if (!NewVNs.count(use)) |
| 637 | NewVNs[use] = ret; |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 638 | Visited.insert(use); |
| 639 | } |
| 640 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | /// ReconstructLiveInterval - Recompute a live interval from scratch. |
| 645 | void PreAllocSplitting::ReconstructLiveInterval(LiveInterval* LI) { |
| 646 | BumpPtrAllocator& Alloc = LIs->getVNInfoAllocator(); |
| 647 | |
| 648 | // Clear the old ranges and valnos; |
| 649 | LI->clear(); |
| 650 | |
| 651 | // Cache the uses and defs of the register |
| 652 | typedef DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 2> > RegMap; |
| 653 | RegMap Defs, Uses; |
| 654 | |
| 655 | // Keep track of the new VNs we're creating. |
| 656 | DenseMap<MachineInstr*, VNInfo*> NewVNs; |
| 657 | SmallPtrSet<VNInfo*, 2> PhiVNs; |
| 658 | |
| 659 | // Cache defs, and create a new VNInfo for each def. |
| 660 | for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg), |
| 661 | DE = MRI->def_end(); DI != DE; ++DI) { |
| 662 | Defs[(*DI).getParent()].insert(&*DI); |
| 663 | |
| 664 | unsigned DefIdx = LIs->getInstructionIndex(&*DI); |
| 665 | DefIdx = LiveIntervals::getDefIndex(DefIdx); |
| 666 | |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 667 | VNInfo* NewVN = LI->getNextValue(DefIdx, 0, Alloc); |
| 668 | |
| 669 | // If the def is a move, set the copy field. |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 670 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 671 | if (TII->isMoveInstr(*DI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) |
| 672 | if (DstReg == LI->reg) |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 673 | NewVN->copy = &*DI; |
| 674 | |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 675 | NewVNs[&*DI] = NewVN; |
| 676 | } |
| 677 | |
| 678 | // Cache uses as a separate pass from actually processing them. |
| 679 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg), |
| 680 | UE = MRI->use_end(); UI != UE; ++UI) |
| 681 | Uses[(*UI).getParent()].insert(&*UI); |
| 682 | |
| 683 | // Now, actually process every use and use a phi construction algorithm |
| 684 | // to walk from it to its reaching definitions, building VNInfos along |
| 685 | // the way. |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 686 | DenseMap<MachineBasicBlock*, VNInfo*> LiveOut; |
| 687 | DenseMap<MachineBasicBlock*, VNInfo*> Phis; |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 688 | SmallPtrSet<MachineInstr*, 4> Visited; |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 689 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(LI->reg), |
| 690 | UE = MRI->use_end(); UI != UE; ++UI) { |
Owen Anderson | 200ee7f | 2009-01-06 07:53:32 +0000 | [diff] [blame] | 691 | PerformPHIConstruction(&*UI, UI->getParent(), LI, Visited, Defs, |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 692 | Uses, NewVNs, LiveOut, Phis, true, true); |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 693 | } |
Owen Anderson | d4f6fe5 | 2008-12-28 23:35:13 +0000 | [diff] [blame] | 694 | |
| 695 | // Add ranges for dead defs |
| 696 | for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(LI->reg), |
| 697 | DE = MRI->def_end(); DI != DE; ++DI) { |
| 698 | unsigned DefIdx = LIs->getInstructionIndex(&*DI); |
| 699 | DefIdx = LiveIntervals::getDefIndex(DefIdx); |
Owen Anderson | d4f6fe5 | 2008-12-28 23:35:13 +0000 | [diff] [blame] | 700 | |
| 701 | if (LI->liveAt(DefIdx)) continue; |
| 702 | |
| 703 | VNInfo* DeadVN = NewVNs[&*DI]; |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 704 | LI->addRange(LiveRange(DefIdx, DefIdx+1, DeadVN)); |
Owen Anderson | d4f6fe5 | 2008-12-28 23:35:13 +0000 | [diff] [blame] | 705 | LI->addKill(DeadVN, DefIdx); |
| 706 | } |
Owen Anderson | 60d4f6d | 2008-12-28 21:48:48 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 709 | /// RenumberValno - Split the given valno out into a new vreg, allowing it to |
| 710 | /// be allocated to a different register. This function creates a new vreg, |
| 711 | /// copies the valno and its live ranges over to the new vreg's interval, |
| 712 | /// removes them from the old interval, and rewrites all uses and defs of |
| 713 | /// the original reg to the new vreg within those ranges. |
| 714 | void PreAllocSplitting::RenumberValno(VNInfo* VN) { |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 715 | SmallVector<VNInfo*, 4> Stack; |
| 716 | SmallVector<VNInfo*, 4> VNsToCopy; |
| 717 | Stack.push_back(VN); |
| 718 | |
| 719 | // Walk through and copy the valno we care about, and any other valnos |
| 720 | // that are two-address redefinitions of the one we care about. These |
| 721 | // will need to be rewritten as well. We also check for safety of the |
| 722 | // renumbering here, by making sure that none of the valno involved has |
| 723 | // phi kills. |
| 724 | while (!Stack.empty()) { |
| 725 | VNInfo* OldVN = Stack.back(); |
| 726 | Stack.pop_back(); |
| 727 | |
| 728 | // Bail out if we ever encounter a valno that has a PHI kill. We can't |
| 729 | // renumber these. |
| 730 | if (OldVN->hasPHIKill) return; |
| 731 | |
| 732 | VNsToCopy.push_back(OldVN); |
| 733 | |
| 734 | // Locate two-address redefinitions |
| 735 | for (SmallVector<unsigned, 4>::iterator KI = OldVN->kills.begin(), |
| 736 | KE = OldVN->kills.end(); KI != KE; ++KI) { |
| 737 | MachineInstr* MI = LIs->getInstructionFromIndex(*KI); |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 738 | unsigned DefIdx = MI->findRegisterDefOperandIdx(CurrLI->reg); |
| 739 | if (DefIdx == ~0U) continue; |
| 740 | if (MI->isRegReDefinedByTwoAddr(DefIdx)) { |
| 741 | VNInfo* NextVN = |
| 742 | CurrLI->findDefinedVNInfo(LiveIntervals::getDefIndex(*KI)); |
Owen Anderson | b4b8436 | 2009-01-26 21:57:31 +0000 | [diff] [blame] | 743 | if (NextVN == OldVN) continue; |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 744 | Stack.push_back(NextVN); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 749 | // Create the new vreg |
| 750 | unsigned NewVReg = MRI->createVirtualRegister(MRI->getRegClass(CurrLI->reg)); |
| 751 | |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 752 | // Create the new live interval |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 753 | LiveInterval& NewLI = LIs->getOrCreateInterval(NewVReg); |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 754 | |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 755 | for (SmallVector<VNInfo*, 4>::iterator OI = VNsToCopy.begin(), OE = |
| 756 | VNsToCopy.end(); OI != OE; ++OI) { |
| 757 | VNInfo* OldVN = *OI; |
| 758 | |
| 759 | // Copy the valno over |
| 760 | VNInfo* NewVN = NewLI.getNextValue(OldVN->def, OldVN->copy, |
| 761 | LIs->getVNInfoAllocator()); |
| 762 | NewLI.copyValNumInfo(NewVN, OldVN); |
| 763 | NewLI.MergeValueInAsValue(*CurrLI, OldVN, NewVN); |
| 764 | |
| 765 | // Remove the valno from the old interval |
| 766 | CurrLI->removeValNo(OldVN); |
| 767 | } |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 768 | |
| 769 | // Rewrite defs and uses. This is done in two stages to avoid invalidating |
| 770 | // the reg_iterator. |
| 771 | SmallVector<std::pair<MachineInstr*, unsigned>, 8> OpsToChange; |
| 772 | |
| 773 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg), |
| 774 | E = MRI->reg_end(); I != E; ++I) { |
| 775 | MachineOperand& MO = I.getOperand(); |
| 776 | unsigned InstrIdx = LIs->getInstructionIndex(&*I); |
| 777 | |
| 778 | if ((MO.isUse() && NewLI.liveAt(LiveIntervals::getUseIndex(InstrIdx))) || |
| 779 | (MO.isDef() && NewLI.liveAt(LiveIntervals::getDefIndex(InstrIdx)))) |
| 780 | OpsToChange.push_back(std::make_pair(&*I, I.getOperandNo())); |
| 781 | } |
| 782 | |
| 783 | for (SmallVector<std::pair<MachineInstr*, unsigned>, 8>::iterator I = |
| 784 | OpsToChange.begin(), E = OpsToChange.end(); I != E; ++I) { |
| 785 | MachineInstr* Inst = I->first; |
| 786 | unsigned OpIdx = I->second; |
| 787 | MachineOperand& MO = Inst->getOperand(OpIdx); |
| 788 | MO.setReg(NewVReg); |
| 789 | } |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 790 | |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 791 | // The renumbered vreg shares a stack slot with the old register. |
| 792 | if (IntervalSSMap.count(CurrLI->reg)) |
| 793 | IntervalSSMap[NewVReg] = IntervalSSMap[CurrLI->reg]; |
| 794 | |
Owen Anderson | 2ebf63f | 2008-12-18 01:27:19 +0000 | [diff] [blame] | 795 | NumRenumbers++; |
Owen Anderson | d0b6a0d | 2008-12-16 21:35:08 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Owen Anderson | 6002e99 | 2008-12-04 21:20:30 +0000 | [diff] [blame] | 798 | bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo, |
| 799 | MachineInstr* DefMI, |
| 800 | MachineBasicBlock::iterator RestorePt, |
| 801 | unsigned RestoreIdx, |
| 802 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB) { |
| 803 | MachineBasicBlock& MBB = *RestorePt->getParent(); |
| 804 | |
| 805 | MachineBasicBlock::iterator KillPt = BarrierMBB->end(); |
| 806 | unsigned KillIdx = 0; |
| 807 | if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB) |
| 808 | KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx); |
| 809 | else |
| 810 | KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx); |
| 811 | |
| 812 | if (KillPt == DefMI->getParent()->end()) |
| 813 | return false; |
| 814 | |
| 815 | TII->reMaterialize(MBB, RestorePt, vreg, DefMI); |
| 816 | LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx); |
| 817 | |
Owen Anderson | b4b8436 | 2009-01-26 21:57:31 +0000 | [diff] [blame] | 818 | ReconstructLiveInterval(CurrLI); |
| 819 | unsigned RematIdx = LIs->getInstructionIndex(prior(RestorePt)); |
| 820 | RematIdx = LiveIntervals::getDefIndex(RematIdx); |
| 821 | RenumberValno(CurrLI->findDefinedVNInfo(RematIdx)); |
Owen Anderson | e1762c9 | 2009-01-12 03:10:40 +0000 | [diff] [blame] | 822 | |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 823 | ++NumSplits; |
| 824 | ++NumRemats; |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 825 | return true; |
| 826 | } |
| 827 | |
| 828 | MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg, |
| 829 | const TargetRegisterClass* RC, |
| 830 | MachineInstr* DefMI, |
| 831 | MachineInstr* Barrier, |
| 832 | MachineBasicBlock* MBB, |
| 833 | int& SS, |
| 834 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB) { |
| 835 | MachineBasicBlock::iterator Pt = MBB->begin(); |
| 836 | |
| 837 | // Go top down if RefsInMBB is empty. |
| 838 | if (RefsInMBB.empty()) |
| 839 | return 0; |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 840 | |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 841 | MachineBasicBlock::iterator FoldPt = Barrier; |
| 842 | while (&*FoldPt != DefMI && FoldPt != MBB->begin() && |
| 843 | !RefsInMBB.count(FoldPt)) |
| 844 | --FoldPt; |
| 845 | |
| 846 | int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false); |
| 847 | if (OpIdx == -1) |
| 848 | return 0; |
| 849 | |
| 850 | SmallVector<unsigned, 1> Ops; |
| 851 | Ops.push_back(OpIdx); |
| 852 | |
| 853 | if (!TII->canFoldMemoryOperand(FoldPt, Ops)) |
| 854 | return 0; |
| 855 | |
| 856 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg); |
| 857 | if (I != IntervalSSMap.end()) { |
| 858 | SS = I->second; |
| 859 | } else { |
| 860 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
| 861 | |
| 862 | } |
| 863 | |
| 864 | MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(), |
| 865 | FoldPt, Ops, SS); |
| 866 | |
| 867 | if (FMI) { |
| 868 | LIs->ReplaceMachineInstrInMaps(FoldPt, FMI); |
| 869 | FMI = MBB->insert(MBB->erase(FoldPt), FMI); |
| 870 | ++NumFolds; |
| 871 | |
| 872 | IntervalSSMap[vreg] = SS; |
| 873 | CurrSLI = &LSs->getOrCreateInterval(SS); |
| 874 | if (CurrSLI->hasAtLeastOneValue()) |
| 875 | CurrSValNo = CurrSLI->getValNumInfo(0); |
| 876 | else |
| 877 | CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator()); |
| 878 | } |
| 879 | |
| 880 | return FMI; |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 883 | /// SplitRegLiveInterval - Split (spill and restore) the given live interval |
| 884 | /// so it would not cross the barrier that's being processed. Shrink wrap |
| 885 | /// (minimize) the live interval to the last uses. |
| 886 | bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) { |
| 887 | CurrLI = LI; |
| 888 | |
| 889 | // Find live range where current interval cross the barrier. |
| 890 | LiveInterval::iterator LR = |
| 891 | CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx)); |
| 892 | VNInfo *ValNo = LR->valno; |
| 893 | |
| 894 | if (ValNo->def == ~1U) { |
| 895 | // Defined by a dead def? How can this be? |
| 896 | assert(0 && "Val# is defined by a dead def?"); |
| 897 | abort(); |
| 898 | } |
| 899 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 900 | MachineInstr *DefMI = (ValNo->def != ~0U) |
| 901 | ? LIs->getInstructionFromIndex(ValNo->def) : NULL; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 902 | |
Owen Anderson | d3be462 | 2009-01-21 08:18:03 +0000 | [diff] [blame] | 903 | // If this would create a new join point, do not split. |
| 904 | if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) |
| 905 | return false; |
| 906 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 907 | // Find all references in the barrier mbb. |
| 908 | SmallPtrSet<MachineInstr*, 4> RefsInMBB; |
| 909 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg), |
| 910 | E = MRI->reg_end(); I != E; ++I) { |
| 911 | MachineInstr *RefMI = &*I; |
| 912 | if (RefMI->getParent() == BarrierMBB) |
| 913 | RefsInMBB.insert(RefMI); |
| 914 | } |
| 915 | |
| 916 | // Find a point to restore the value after the barrier. |
Evan Cheng | b964f33 | 2009-01-26 18:33:51 +0000 | [diff] [blame] | 917 | unsigned RestoreIndex = 0; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 918 | MachineBasicBlock::iterator RestorePt = |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 919 | findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 920 | if (RestorePt == BarrierMBB->end()) |
| 921 | return false; |
| 922 | |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 923 | if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI)) |
| 924 | if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, |
| 925 | RestoreIndex, RefsInMBB)) |
| 926 | return true; |
| 927 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 928 | // Add a spill either before the barrier or after the definition. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 929 | MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 930 | const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 931 | unsigned SpillIndex = 0; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 932 | MachineInstr *SpillMI = NULL; |
Evan Cheng | 985921e | 2008-10-27 23:29:28 +0000 | [diff] [blame] | 933 | int SS = -1; |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 934 | if (ValNo->def == ~0U) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 935 | // If it's defined by a phi, we must split just before the barrier. |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 936 | if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier, |
| 937 | BarrierMBB, SS, RefsInMBB))) { |
| 938 | SpillIndex = LIs->getInstructionIndex(SpillMI); |
| 939 | } else { |
| 940 | MachineBasicBlock::iterator SpillPt = |
| 941 | findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex); |
| 942 | if (SpillPt == BarrierMBB->begin()) |
| 943 | return false; // No gap to insert spill. |
| 944 | // Add spill. |
| 945 | |
| 946 | SS = CreateSpillStackSlot(CurrLI->reg, RC); |
| 947 | TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC); |
| 948 | SpillMI = prior(SpillPt); |
| 949 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
| 950 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 951 | } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def, |
| 952 | RestoreIndex, SpillIndex, SS)) { |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 953 | // If it's already split, just restore the value. There is no need to spill |
| 954 | // the def again. |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 955 | if (!DefMI) |
| 956 | return false; // Def is dead. Do nothing. |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 957 | |
| 958 | if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier, |
| 959 | BarrierMBB, SS, RefsInMBB))) { |
| 960 | SpillIndex = LIs->getInstructionIndex(SpillMI); |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 961 | } else { |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 962 | // Check if it's possible to insert a spill after the def MI. |
| 963 | MachineBasicBlock::iterator SpillPt; |
| 964 | if (DefMBB == BarrierMBB) { |
| 965 | // Add spill after the def and the last use before the barrier. |
| 966 | SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI, |
| 967 | RefsInMBB, SpillIndex); |
| 968 | if (SpillPt == DefMBB->begin()) |
| 969 | return false; // No gap to insert spill. |
| 970 | } else { |
| 971 | SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex); |
| 972 | if (SpillPt == DefMBB->end()) |
| 973 | return false; // No gap to insert spill. |
| 974 | } |
| 975 | // Add spill. The store instruction kills the register if def is before |
| 976 | // the barrier in the barrier block. |
| 977 | SS = CreateSpillStackSlot(CurrLI->reg, RC); |
| 978 | TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, |
| 979 | DefMBB == BarrierMBB, SS, RC); |
| 980 | SpillMI = prior(SpillPt); |
| 981 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 982 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 985 | // Remember def instruction index to spill index mapping. |
| 986 | if (DefMI && SpillMI) |
| 987 | Def2SpillMap[ValNo->def] = SpillIndex; |
| 988 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 989 | // Add restore. |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 990 | TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC); |
| 991 | MachineInstr *LoadMI = prior(RestorePt); |
| 992 | LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex); |
| 993 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 994 | // Update spill stack slot live interval. |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 995 | UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1, |
| 996 | LIs->getDefIndex(RestoreIndex)); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 997 | |
Owen Anderson | b4b8436 | 2009-01-26 21:57:31 +0000 | [diff] [blame] | 998 | ReconstructLiveInterval(CurrLI); |
| 999 | unsigned RestoreIdx = LIs->getInstructionIndex(prior(RestorePt)); |
| 1000 | RestoreIdx = LiveIntervals::getDefIndex(RestoreIdx); |
| 1001 | RenumberValno(CurrLI->findDefinedVNInfo(RestoreIdx)); |
Owen Anderson | 7d211e2 | 2008-12-31 02:00:25 +0000 | [diff] [blame] | 1002 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 1003 | ++NumSplits; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
| 1006 | |
| 1007 | /// SplitRegLiveIntervals - Split all register live intervals that cross the |
| 1008 | /// barrier that's being processed. |
| 1009 | bool |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1010 | PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs, |
| 1011 | SmallPtrSet<LiveInterval*, 8>& Split) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1012 | // First find all the virtual registers whose live intervals are intercepted |
| 1013 | // by the current barrier. |
| 1014 | SmallVector<LiveInterval*, 8> Intervals; |
| 1015 | for (const TargetRegisterClass **RC = RCs; *RC; ++RC) { |
Evan Cheng | 2306628 | 2008-10-27 07:14:50 +0000 | [diff] [blame] | 1016 | if (TII->IgnoreRegisterClassBarriers(*RC)) |
| 1017 | continue; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1018 | std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC); |
| 1019 | for (unsigned i = 0, e = VRs.size(); i != e; ++i) { |
| 1020 | unsigned Reg = VRs[i]; |
| 1021 | if (!LIs->hasInterval(Reg)) |
| 1022 | continue; |
| 1023 | LiveInterval *LI = &LIs->getInterval(Reg); |
| 1024 | if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg)) |
| 1025 | // Virtual register live interval is intercepted by the barrier. We |
| 1026 | // should split and shrink wrap its interval if possible. |
| 1027 | Intervals.push_back(LI); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // Process the affected live intervals. |
| 1032 | bool Change = false; |
| 1033 | while (!Intervals.empty()) { |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 1034 | if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit) |
| 1035 | break; |
Owen Anderson | e1762c9 | 2009-01-12 03:10:40 +0000 | [diff] [blame] | 1036 | else if (NumSplits == 4) |
| 1037 | Change |= Change; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1038 | LiveInterval *LI = Intervals.back(); |
| 1039 | Intervals.pop_back(); |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1040 | bool result = SplitRegLiveInterval(LI); |
| 1041 | if (result) Split.insert(LI); |
| 1042 | Change |= result; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | return Change; |
| 1046 | } |
| 1047 | |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1048 | unsigned PreAllocSplitting::getNumberOfNonSpills( |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1049 | SmallPtrSet<MachineInstr*, 4>& MIs, |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1050 | unsigned Reg, int FrameIndex, |
| 1051 | bool& FeedsTwoAddr) { |
| 1052 | unsigned NonSpills = 0; |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1053 | for (SmallPtrSet<MachineInstr*, 4>::iterator UI = MIs.begin(), UE = MIs.end(); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1054 | UI != UE; ++UI) { |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1055 | int StoreFrameIndex; |
| 1056 | unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1057 | if (StoreVReg != Reg || StoreFrameIndex != FrameIndex) |
| 1058 | NonSpills++; |
| 1059 | |
| 1060 | int DefIdx = (*UI)->findRegisterDefOperandIdx(Reg); |
| 1061 | if (DefIdx != -1 && (*UI)->isRegReDefinedByTwoAddr(DefIdx)) |
| 1062 | FeedsTwoAddr = true; |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1065 | return NonSpills; |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1068 | /// removeDeadSpills - After doing splitting, filter through all intervals we've |
| 1069 | /// split, and see if any of the spills are unnecessary. If so, remove them. |
| 1070 | bool PreAllocSplitting::removeDeadSpills(SmallPtrSet<LiveInterval*, 8>& split) { |
| 1071 | bool changed = false; |
| 1072 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1073 | // Walk over all of the live intervals that were touched by the splitter, |
| 1074 | // and see if we can do any DCE and/or folding. |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1075 | for (SmallPtrSet<LiveInterval*, 8>::iterator LI = split.begin(), |
| 1076 | LE = split.end(); LI != LE; ++LI) { |
Owen Anderson | 9ce499a | 2009-01-23 03:28:53 +0000 | [diff] [blame] | 1077 | DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> > VNUseCount; |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1078 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1079 | // First, collect all the uses of the vreg, and sort them by their |
| 1080 | // reaching definition (VNInfo). |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1081 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin((*LI)->reg), |
| 1082 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 1083 | unsigned index = LIs->getInstructionIndex(&*UI); |
| 1084 | index = LiveIntervals::getUseIndex(index); |
| 1085 | |
| 1086 | const LiveRange* LR = (*LI)->getLiveRangeContaining(index); |
Owen Anderson | 9ce499a | 2009-01-23 03:28:53 +0000 | [diff] [blame] | 1087 | VNUseCount[LR->valno].insert(&*UI); |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1090 | // Now, take the definitions (VNInfo's) one at a time and try to DCE |
| 1091 | // and/or fold them away. |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1092 | for (LiveInterval::vni_iterator VI = (*LI)->vni_begin(), |
| 1093 | VE = (*LI)->vni_end(); VI != VE; ++VI) { |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (DeadSplitLimit != -1 && (int)NumDeadSpills == DeadSplitLimit) |
| 1096 | return changed; |
| 1097 | |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1098 | VNInfo* CurrVN = *VI; |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1099 | |
| 1100 | // We don't currently try to handle definitions with PHI kills, because |
| 1101 | // it would involve processing more than one VNInfo at once. |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1102 | if (CurrVN->hasPHIKill) continue; |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1103 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1104 | // We also don't try to handle the results of PHI joins, since there's |
| 1105 | // no defining instruction to analyze. |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1106 | unsigned DefIdx = CurrVN->def; |
| 1107 | if (DefIdx == ~0U || DefIdx == ~1U) continue; |
Owen Anderson | 9ce499a | 2009-01-23 03:28:53 +0000 | [diff] [blame] | 1108 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1109 | // We're only interested in eliminating cruft introduced by the splitter, |
| 1110 | // is of the form load-use or load-use-store. First, check that the |
| 1111 | // definition is a load, and remember what stack slot we loaded it from. |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1112 | MachineInstr* DefMI = LIs->getInstructionFromIndex(DefIdx); |
| 1113 | int FrameIndex; |
| 1114 | if (!TII->isLoadFromStackSlot(DefMI, FrameIndex)) continue; |
| 1115 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1116 | // If the definition has no uses at all, just DCE it. |
Owen Anderson | 9ce499a | 2009-01-23 03:28:53 +0000 | [diff] [blame] | 1117 | if (VNUseCount[CurrVN].size() == 0) { |
| 1118 | LIs->RemoveMachineInstrFromMaps(DefMI); |
| 1119 | (*LI)->removeValNo(CurrVN); |
| 1120 | DefMI->eraseFromParent(); |
| 1121 | NumDeadSpills++; |
| 1122 | changed = true; |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1123 | continue; |
Owen Anderson | 9ce499a | 2009-01-23 03:28:53 +0000 | [diff] [blame] | 1124 | } |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1125 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1126 | // Second, get the number of non-store uses of the definition, as well as |
| 1127 | // a flag indicating whether it feeds into a later two-address definition. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1128 | bool FeedsTwoAddr = false; |
| 1129 | unsigned NonSpillCount = getNumberOfNonSpills(VNUseCount[CurrVN], |
| 1130 | (*LI)->reg, FrameIndex, |
| 1131 | FeedsTwoAddr); |
| 1132 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1133 | // If there's one non-store use and it doesn't feed a two-addr, then |
| 1134 | // this is a load-use-store case that we can try to fold. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1135 | if (NonSpillCount == 1 && !FeedsTwoAddr) { |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1136 | // Start by finding the non-store use MachineInstr. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1137 | SmallPtrSet<MachineInstr*, 4>::iterator UI = VNUseCount[CurrVN].begin(); |
| 1138 | int StoreFrameIndex; |
| 1139 | unsigned StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex); |
| 1140 | while (UI != VNUseCount[CurrVN].end() && |
| 1141 | (StoreVReg == (*LI)->reg && StoreFrameIndex == FrameIndex)) { |
| 1142 | ++UI; |
| 1143 | if (UI != VNUseCount[CurrVN].end()) |
| 1144 | StoreVReg = TII->isStoreToStackSlot(*UI, StoreFrameIndex); |
| 1145 | } |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1146 | if (UI == VNUseCount[CurrVN].end()) continue; |
| 1147 | |
| 1148 | MachineInstr* use = *UI; |
| 1149 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1150 | // Attempt to fold it away! |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1151 | int OpIdx = use->findRegisterUseOperandIdx((*LI)->reg, false); |
| 1152 | if (OpIdx == -1) continue; |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1153 | SmallVector<unsigned, 1> Ops; |
| 1154 | Ops.push_back(OpIdx); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1155 | if (!TII->canFoldMemoryOperand(use, Ops)) continue; |
| 1156 | |
| 1157 | MachineInstr* NewMI = |
| 1158 | TII->foldMemoryOperand(*use->getParent()->getParent(), |
| 1159 | use, Ops, FrameIndex); |
| 1160 | |
| 1161 | if (!NewMI) continue; |
| 1162 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1163 | // Update relevant analyses. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1164 | LIs->RemoveMachineInstrFromMaps(DefMI); |
| 1165 | LIs->ReplaceMachineInstrInMaps(use, NewMI); |
| 1166 | (*LI)->removeValNo(CurrVN); |
| 1167 | |
| 1168 | DefMI->eraseFromParent(); |
| 1169 | MachineBasicBlock* MBB = use->getParent(); |
| 1170 | NewMI = MBB->insert(MBB->erase(use), NewMI); |
| 1171 | VNUseCount[CurrVN].erase(use); |
| 1172 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1173 | // Remove deleted instructions. Note that we need to remove them from |
| 1174 | // the VNInfo->use map as well, just to be safe. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1175 | for (SmallPtrSet<MachineInstr*, 4>::iterator II = |
| 1176 | VNUseCount[CurrVN].begin(), IE = VNUseCount[CurrVN].end(); |
| 1177 | II != IE; ++II) { |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1178 | for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 1179 | VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI) |
| 1180 | VI->second.erase(*II); |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1181 | LIs->RemoveMachineInstrFromMaps(*II); |
| 1182 | (*II)->eraseFromParent(); |
| 1183 | } |
| 1184 | |
| 1185 | for (DenseMap<VNInfo*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 1186 | VI = VNUseCount.begin(), VE = VNUseCount.end(); VI != VE; ++VI) |
| 1187 | if (VI->second.erase(use)) |
| 1188 | VI->second.insert(NewMI); |
| 1189 | |
| 1190 | NumDeadSpills++; |
| 1191 | changed = true; |
| 1192 | continue; |
| 1193 | } |
| 1194 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1195 | // If there's more than one non-store instruction, we can't profitably |
| 1196 | // fold it, so bail. |
Owen Anderson | 45e6855 | 2009-01-29 05:28:55 +0000 | [diff] [blame] | 1197 | if (NonSpillCount) continue; |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1198 | |
Owen Anderson | 4bfc209 | 2009-01-29 05:41:02 +0000 | [diff] [blame^] | 1199 | // Otherwise, this is a load-store case, so DCE them. |
Owen Anderson | 32ca865 | 2009-01-24 10:07:43 +0000 | [diff] [blame] | 1200 | for (SmallPtrSet<MachineInstr*, 4>::iterator UI = |
| 1201 | VNUseCount[CurrVN].begin(), UE = VNUseCount[CurrVN].end(); |
| 1202 | UI != UI; ++UI) { |
| 1203 | LIs->RemoveMachineInstrFromMaps(*UI); |
| 1204 | (*UI)->eraseFromParent(); |
| 1205 | } |
| 1206 | |
| 1207 | LIs->RemoveMachineInstrFromMaps(DefMI); |
| 1208 | (*LI)->removeValNo(CurrVN); |
| 1209 | DefMI->eraseFromParent(); |
| 1210 | NumDeadSpills++; |
| 1211 | changed = true; |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | return changed; |
| 1216 | } |
| 1217 | |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 1218 | bool PreAllocSplitting::createsNewJoin(LiveRange* LR, |
| 1219 | MachineBasicBlock* DefMBB, |
| 1220 | MachineBasicBlock* BarrierMBB) { |
| 1221 | if (DefMBB == BarrierMBB) |
| 1222 | return false; |
| 1223 | |
| 1224 | if (LR->valno->hasPHIKill) |
| 1225 | return false; |
| 1226 | |
| 1227 | unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB); |
| 1228 | if (LR->end < MBBEnd) |
| 1229 | return false; |
| 1230 | |
| 1231 | MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>(); |
| 1232 | if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB)) |
| 1233 | return true; |
| 1234 | |
| 1235 | MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>(); |
| 1236 | SmallPtrSet<MachineBasicBlock*, 4> Visited; |
| 1237 | typedef std::pair<MachineBasicBlock*, |
| 1238 | MachineBasicBlock::succ_iterator> ItPair; |
| 1239 | SmallVector<ItPair, 4> Stack; |
| 1240 | Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin())); |
| 1241 | |
| 1242 | while (!Stack.empty()) { |
| 1243 | ItPair P = Stack.back(); |
| 1244 | Stack.pop_back(); |
| 1245 | |
| 1246 | MachineBasicBlock* PredMBB = P.first; |
| 1247 | MachineBasicBlock::succ_iterator S = P.second; |
| 1248 | |
| 1249 | if (S == PredMBB->succ_end()) |
| 1250 | continue; |
| 1251 | else if (Visited.count(*S)) { |
| 1252 | Stack.push_back(std::make_pair(PredMBB, ++S)); |
| 1253 | continue; |
| 1254 | } else |
Owen Anderson | b214c69 | 2008-11-05 00:32:13 +0000 | [diff] [blame] | 1255 | Stack.push_back(std::make_pair(PredMBB, S+1)); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 1256 | |
| 1257 | MachineBasicBlock* MBB = *S; |
| 1258 | Visited.insert(MBB); |
| 1259 | |
| 1260 | if (MBB == BarrierMBB) |
| 1261 | return true; |
| 1262 | |
| 1263 | MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB); |
| 1264 | MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB); |
| 1265 | MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom(); |
| 1266 | while (MDTN) { |
| 1267 | if (MDTN == DefMDTN) |
| 1268 | return true; |
| 1269 | else if (MDTN == BarrierMDTN) |
| 1270 | break; |
| 1271 | MDTN = MDTN->getIDom(); |
| 1272 | } |
| 1273 | |
| 1274 | MBBEnd = LIs->getMBBEndIdx(MBB); |
| 1275 | if (LR->end > MBBEnd) |
| 1276 | Stack.push_back(std::make_pair(MBB, MBB->succ_begin())); |
| 1277 | } |
| 1278 | |
| 1279 | return false; |
| 1280 | } |
| 1281 | |
| 1282 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 1283 | bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 1284 | CurrMF = &MF; |
| 1285 | TM = &MF.getTarget(); |
| 1286 | TII = TM->getInstrInfo(); |
| 1287 | MFI = MF.getFrameInfo(); |
| 1288 | MRI = &MF.getRegInfo(); |
| 1289 | LIs = &getAnalysis<LiveIntervals>(); |
| 1290 | LSs = &getAnalysis<LiveStacks>(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1291 | |
| 1292 | bool MadeChange = false; |
| 1293 | |
| 1294 | // Make sure blocks are numbered in order. |
| 1295 | MF.RenumberBlocks(); |
| 1296 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 1297 | MachineBasicBlock *Entry = MF.begin(); |
| 1298 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
| 1299 | |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1300 | SmallPtrSet<LiveInterval*, 8> Split; |
| 1301 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 1302 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > |
| 1303 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 1304 | DFI != E; ++DFI) { |
| 1305 | BarrierMBB = *DFI; |
| 1306 | for (MachineBasicBlock::iterator I = BarrierMBB->begin(), |
| 1307 | E = BarrierMBB->end(); I != E; ++I) { |
| 1308 | Barrier = &*I; |
| 1309 | const TargetRegisterClass **BarrierRCs = |
| 1310 | Barrier->getDesc().getRegClassBarriers(); |
| 1311 | if (!BarrierRCs) |
| 1312 | continue; |
| 1313 | BarrierIdx = LIs->getInstructionIndex(Barrier); |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1314 | MadeChange |= SplitRegLiveIntervals(BarrierRCs, Split); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1317 | |
Owen Anderson | 956ec27 | 2009-01-23 00:23:32 +0000 | [diff] [blame] | 1318 | MadeChange |= removeDeadSpills(Split); |
| 1319 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1320 | return MadeChange; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 1321 | } |