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