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