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