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"); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 44 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 47 | MachineFunction *CurrMF; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 48 | const TargetMachine *TM; |
| 49 | const TargetInstrInfo *TII; |
| 50 | MachineFrameInfo *MFI; |
| 51 | MachineRegisterInfo *MRI; |
| 52 | LiveIntervals *LIs; |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 53 | LiveStacks *LSs; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 54 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 55 | // Barrier - Current barrier being processed. |
| 56 | MachineInstr *Barrier; |
| 57 | |
| 58 | // BarrierMBB - Basic block where the barrier resides in. |
| 59 | MachineBasicBlock *BarrierMBB; |
| 60 | |
| 61 | // Barrier - Current barrier index. |
| 62 | unsigned BarrierIdx; |
| 63 | |
| 64 | // CurrLI - Current live interval being split. |
| 65 | LiveInterval *CurrLI; |
| 66 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 67 | // CurrSLI - Current stack slot live interval. |
| 68 | LiveInterval *CurrSLI; |
| 69 | |
| 70 | // CurrSValNo - Current val# for the stack slot live interval. |
| 71 | VNInfo *CurrSValNo; |
| 72 | |
| 73 | // IntervalSSMap - A map from live interval to spill slots. |
| 74 | DenseMap<unsigned, int> IntervalSSMap; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 75 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 76 | // Def2SpillMap - A map from a def instruction index to spill index. |
| 77 | DenseMap<unsigned, unsigned> Def2SpillMap; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 78 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 79 | public: |
| 80 | static char ID; |
| 81 | PreAllocSplitting() : MachineFunctionPass(&ID) {} |
| 82 | |
| 83 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 84 | |
| 85 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 86 | AU.addRequired<LiveIntervals>(); |
| 87 | AU.addPreserved<LiveIntervals>(); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 88 | AU.addRequired<LiveStacks>(); |
| 89 | AU.addPreserved<LiveStacks>(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 90 | AU.addPreserved<RegisterCoalescer>(); |
| 91 | if (StrongPHIElim) |
| 92 | AU.addPreservedID(StrongPHIEliminationID); |
| 93 | else |
| 94 | AU.addPreservedID(PHIEliminationID); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 95 | AU.addRequired<MachineDominatorTree>(); |
| 96 | AU.addRequired<MachineLoopInfo>(); |
| 97 | AU.addPreserved<MachineDominatorTree>(); |
| 98 | AU.addPreserved<MachineLoopInfo>(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 99 | MachineFunctionPass::getAnalysisUsage(AU); |
| 100 | } |
| 101 | |
| 102 | virtual void releaseMemory() { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 103 | IntervalSSMap.clear(); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 104 | Def2SpillMap.clear(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | virtual const char *getPassName() const { |
| 108 | return "Pre-Register Allocaton Live Interval Splitting"; |
| 109 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 110 | |
| 111 | /// print - Implement the dump method. |
| 112 | virtual void print(std::ostream &O, const Module* M = 0) const { |
| 113 | LIs->print(O, M); |
| 114 | } |
| 115 | |
| 116 | void print(std::ostream *O, const Module* M = 0) const { |
| 117 | if (O) print(*O, M); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | MachineBasicBlock::iterator |
| 122 | findNextEmptySlot(MachineBasicBlock*, MachineInstr*, |
| 123 | unsigned&); |
| 124 | |
| 125 | MachineBasicBlock::iterator |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 126 | findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 127 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 128 | |
| 129 | MachineBasicBlock::iterator |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 130 | findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 131 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 132 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 133 | int CreateSpillStackSlot(unsigned, const TargetRegisterClass *); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 134 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 135 | bool IsAvailableInStack(MachineBasicBlock*, unsigned, unsigned, unsigned, |
| 136 | unsigned&, int&) const; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 137 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 138 | void UpdateSpillSlotInterval(VNInfo*, unsigned, unsigned); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 139 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 140 | void UpdateRegisterInterval(VNInfo*, unsigned, unsigned); |
| 141 | |
| 142 | bool ShrinkWrapToLastUse(MachineBasicBlock*, VNInfo*, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 143 | SmallVector<MachineOperand*, 4>&, |
| 144 | SmallPtrSet<MachineInstr*, 4>&); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 145 | |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 146 | void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 147 | MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 148 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&, |
| 149 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&, |
| 150 | SmallVector<MachineBasicBlock*, 4>&); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 151 | |
| 152 | bool SplitRegLiveInterval(LiveInterval*); |
| 153 | |
| 154 | bool SplitRegLiveIntervals(const TargetRegisterClass **); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 155 | |
Owen Anderson | 6002e99 | 2008-12-04 21:20:30 +0000 | [diff] [blame] | 156 | void RepairLiveInterval(LiveInterval* CurrLI, VNInfo* ValNo, |
| 157 | MachineInstr* DefMI, unsigned RestoreIdx); |
| 158 | |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 159 | bool createsNewJoin(LiveRange* LR, MachineBasicBlock* DefMBB, |
| 160 | MachineBasicBlock* BarrierMBB); |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 161 | bool Rematerialize(unsigned vreg, VNInfo* ValNo, |
| 162 | MachineInstr* DefMI, |
| 163 | MachineBasicBlock::iterator RestorePt, |
| 164 | unsigned RestoreIdx, |
| 165 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB); |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 166 | MachineInstr* FoldSpill(unsigned vreg, const TargetRegisterClass* RC, |
| 167 | MachineInstr* DefMI, |
| 168 | MachineInstr* Barrier, |
| 169 | MachineBasicBlock* MBB, |
| 170 | int& SS, |
| 171 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 172 | }; |
| 173 | } // end anonymous namespace |
| 174 | |
| 175 | char PreAllocSplitting::ID = 0; |
| 176 | |
| 177 | static RegisterPass<PreAllocSplitting> |
| 178 | X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting"); |
| 179 | |
| 180 | const PassInfo *const llvm::PreAllocSplittingID = &X; |
| 181 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 182 | |
| 183 | /// findNextEmptySlot - Find a gap after the given machine instruction in the |
| 184 | /// instruction index map. If there isn't one, return end(). |
| 185 | MachineBasicBlock::iterator |
| 186 | PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI, |
| 187 | unsigned &SpotIndex) { |
| 188 | MachineBasicBlock::iterator MII = MI; |
| 189 | if (++MII != MBB->end()) { |
| 190 | unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII)); |
| 191 | if (Index) { |
| 192 | SpotIndex = Index; |
| 193 | return MII; |
| 194 | } |
| 195 | } |
| 196 | return MBB->end(); |
| 197 | } |
| 198 | |
| 199 | /// findSpillPoint - Find a gap as far away from the given MI that's suitable |
| 200 | /// for spilling the current live interval. The index must be before any |
| 201 | /// defs and uses of the live interval register in the mbb. Return begin() if |
| 202 | /// none is found. |
| 203 | MachineBasicBlock::iterator |
| 204 | PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 205 | MachineInstr *DefMI, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 206 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 207 | unsigned &SpillIndex) { |
| 208 | MachineBasicBlock::iterator Pt = MBB->begin(); |
| 209 | |
| 210 | // Go top down if RefsInMBB is empty. |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 211 | if (RefsInMBB.empty() && !DefMI) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 212 | MachineBasicBlock::iterator MII = MBB->begin(); |
| 213 | MachineBasicBlock::iterator EndPt = MI; |
| 214 | do { |
| 215 | ++MII; |
| 216 | unsigned Index = LIs->getInstructionIndex(MII); |
| 217 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 218 | if (Gap) { |
| 219 | Pt = MII; |
| 220 | SpillIndex = Gap; |
| 221 | break; |
| 222 | } |
| 223 | } while (MII != EndPt); |
| 224 | } else { |
| 225 | MachineBasicBlock::iterator MII = MI; |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 226 | MachineBasicBlock::iterator EndPt = DefMI |
| 227 | ? MachineBasicBlock::iterator(DefMI) : MBB->begin(); |
| 228 | while (MII != EndPt && !RefsInMBB.count(MII)) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 229 | unsigned Index = LIs->getInstructionIndex(MII); |
| 230 | if (LIs->hasGapBeforeInstr(Index)) { |
| 231 | Pt = MII; |
| 232 | SpillIndex = LIs->findGapBeforeInstr(Index, true); |
| 233 | } |
| 234 | --MII; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return Pt; |
| 239 | } |
| 240 | |
| 241 | /// findRestorePoint - Find a gap in the instruction index map that's suitable |
| 242 | /// for restoring the current live interval value. The index must be before any |
| 243 | /// uses of the live interval register in the mbb. Return end() if none is |
| 244 | /// found. |
| 245 | MachineBasicBlock::iterator |
| 246 | PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 247 | unsigned LastIdx, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 248 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 249 | unsigned &RestoreIndex) { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 250 | // FIXME: Allow spill to be inserted to the beginning of the mbb. Update mbb |
| 251 | // begin index accordingly. |
Owen Anderson | 5a92d4e | 2008-11-18 20:53:59 +0000 | [diff] [blame] | 252 | MachineBasicBlock::iterator Pt = MBB->end(); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 253 | unsigned EndIdx = LIs->getMBBEndIdx(MBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 254 | |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 255 | // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond |
| 256 | // the last index in the live range. |
| 257 | if (RefsInMBB.empty() && LastIdx >= EndIdx) { |
Owen Anderson | 711fd3d | 2008-11-13 21:53:14 +0000 | [diff] [blame] | 258 | MachineBasicBlock::iterator MII = MBB->getFirstTerminator(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 259 | MachineBasicBlock::iterator EndPt = MI; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 260 | --MII; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 261 | do { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 262 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | 56ab0de | 2008-10-24 18:46:44 +0000 | [diff] [blame] | 263 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 264 | if (Gap) { |
| 265 | Pt = MII; |
| 266 | RestoreIndex = Gap; |
| 267 | break; |
| 268 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 269 | --MII; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 270 | } while (MII != EndPt); |
| 271 | } else { |
| 272 | MachineBasicBlock::iterator MII = MI; |
| 273 | MII = ++MII; |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 274 | // FIXME: Limit the number of instructions to examine to reduce |
| 275 | // compile time? |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 276 | while (MII != MBB->end()) { |
| 277 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 278 | if (Index > LastIdx) |
| 279 | break; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 280 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 281 | if (Gap) { |
| 282 | Pt = MII; |
| 283 | RestoreIndex = Gap; |
| 284 | } |
| 285 | if (RefsInMBB.count(MII)) |
| 286 | break; |
| 287 | ++MII; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return Pt; |
| 292 | } |
| 293 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 294 | /// CreateSpillStackSlot - Create a stack slot for the live interval being |
| 295 | /// split. If the live interval was previously split, just reuse the same |
| 296 | /// slot. |
| 297 | int PreAllocSplitting::CreateSpillStackSlot(unsigned Reg, |
| 298 | const TargetRegisterClass *RC) { |
| 299 | int SS; |
| 300 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg); |
| 301 | if (I != IntervalSSMap.end()) { |
| 302 | SS = I->second; |
| 303 | } else { |
| 304 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
| 305 | IntervalSSMap[Reg] = SS; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 306 | } |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 307 | |
| 308 | // Create live interval for stack slot. |
| 309 | CurrSLI = &LSs->getOrCreateInterval(SS); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 310 | if (CurrSLI->hasAtLeastOneValue()) |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 311 | CurrSValNo = CurrSLI->getValNumInfo(0); |
| 312 | else |
| 313 | CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator()); |
| 314 | return SS; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 317 | /// IsAvailableInStack - Return true if register is available in a split stack |
| 318 | /// slot at the specified index. |
| 319 | bool |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 320 | PreAllocSplitting::IsAvailableInStack(MachineBasicBlock *DefMBB, |
| 321 | unsigned Reg, unsigned DefIndex, |
| 322 | unsigned RestoreIndex, unsigned &SpillIndex, |
| 323 | int& SS) const { |
| 324 | if (!DefMBB) |
| 325 | return false; |
| 326 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 327 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(Reg); |
| 328 | if (I == IntervalSSMap.end()) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 329 | return false; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 330 | DenseMap<unsigned, unsigned>::iterator II = Def2SpillMap.find(DefIndex); |
| 331 | if (II == Def2SpillMap.end()) |
| 332 | return false; |
| 333 | |
| 334 | // If last spill of def is in the same mbb as barrier mbb (where restore will |
| 335 | // be), make sure it's not below the intended restore index. |
| 336 | // FIXME: Undo the previous spill? |
| 337 | assert(LIs->getMBBFromIndex(II->second) == DefMBB); |
| 338 | if (DefMBB == BarrierMBB && II->second >= RestoreIndex) |
| 339 | return false; |
| 340 | |
| 341 | SS = I->second; |
| 342 | SpillIndex = II->second; |
| 343 | return true; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 346 | /// UpdateSpillSlotInterval - Given the specified val# of the register live |
| 347 | /// interval being split, and the spill and restore indicies, update the live |
| 348 | /// interval of the spill stack slot. |
| 349 | void |
| 350 | PreAllocSplitting::UpdateSpillSlotInterval(VNInfo *ValNo, unsigned SpillIndex, |
| 351 | unsigned RestoreIndex) { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 352 | assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB && |
| 353 | "Expect restore in the barrier mbb"); |
| 354 | |
| 355 | MachineBasicBlock *MBB = LIs->getMBBFromIndex(SpillIndex); |
| 356 | if (MBB == BarrierMBB) { |
| 357 | // Intra-block spill + restore. We are done. |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 358 | LiveRange SLR(SpillIndex, RestoreIndex, CurrSValNo); |
| 359 | CurrSLI->addRange(SLR); |
| 360 | return; |
| 361 | } |
| 362 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 363 | SmallPtrSet<MachineBasicBlock*, 4> Processed; |
| 364 | unsigned EndIdx = LIs->getMBBEndIdx(MBB); |
| 365 | LiveRange SLR(SpillIndex, EndIdx+1, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 366 | CurrSLI->addRange(SLR); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 367 | Processed.insert(MBB); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 368 | |
| 369 | // Start from the spill mbb, figure out the extend of the spill slot's |
| 370 | // live interval. |
| 371 | SmallVector<MachineBasicBlock*, 4> WorkList; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 372 | const LiveRange *LR = CurrLI->getLiveRangeContaining(SpillIndex); |
| 373 | if (LR->end > EndIdx) |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 374 | // If live range extend beyond end of mbb, add successors to work list. |
| 375 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 376 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 377 | WorkList.push_back(*SI); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 378 | |
| 379 | while (!WorkList.empty()) { |
| 380 | MachineBasicBlock *MBB = WorkList.back(); |
| 381 | WorkList.pop_back(); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 382 | if (Processed.count(MBB)) |
| 383 | continue; |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 384 | unsigned Idx = LIs->getMBBStartIdx(MBB); |
| 385 | LR = CurrLI->getLiveRangeContaining(Idx); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 386 | if (LR && LR->valno == ValNo) { |
| 387 | EndIdx = LIs->getMBBEndIdx(MBB); |
| 388 | if (Idx <= RestoreIndex && RestoreIndex < EndIdx) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 389 | // Spill slot live interval stops at the restore. |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 390 | LiveRange SLR(Idx, RestoreIndex, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 391 | CurrSLI->addRange(SLR); |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 392 | } else if (LR->end > EndIdx) { |
| 393 | // Live range extends beyond end of mbb, process successors. |
| 394 | LiveRange SLR(Idx, EndIdx+1, CurrSValNo); |
| 395 | CurrSLI->addRange(SLR); |
| 396 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 397 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 398 | WorkList.push_back(*SI); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 399 | } else { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 400 | LiveRange SLR(Idx, LR->end, CurrSValNo); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 401 | CurrSLI->addRange(SLR); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 402 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 403 | Processed.insert(MBB); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /// UpdateRegisterInterval - Given the specified val# of the current live |
| 409 | /// 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] | 410 | /// interval accordingly. |
| 411 | void |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 412 | PreAllocSplitting::UpdateRegisterInterval(VNInfo *ValNo, unsigned SpillIndex, |
| 413 | unsigned RestoreIndex) { |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 414 | assert(LIs->getMBBFromIndex(RestoreIndex) == BarrierMBB && |
| 415 | "Expect restore in the barrier mbb"); |
| 416 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 417 | SmallVector<std::pair<unsigned,unsigned>, 4> Before; |
| 418 | SmallVector<std::pair<unsigned,unsigned>, 4> After; |
| 419 | SmallVector<unsigned, 4> BeforeKills; |
| 420 | SmallVector<unsigned, 4> AfterKills; |
| 421 | SmallPtrSet<const LiveRange*, 4> Processed; |
| 422 | |
| 423 | // First, let's figure out which parts of the live interval is now defined |
| 424 | // by the restore, which are defined by the original definition. |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 425 | const LiveRange *LR = CurrLI->getLiveRangeContaining(RestoreIndex); |
| 426 | After.push_back(std::make_pair(RestoreIndex, LR->end)); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 427 | if (CurrLI->isKill(ValNo, LR->end)) |
| 428 | AfterKills.push_back(LR->end); |
| 429 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 430 | assert(LR->contains(SpillIndex)); |
| 431 | if (SpillIndex > LR->start) { |
| 432 | Before.push_back(std::make_pair(LR->start, SpillIndex)); |
| 433 | BeforeKills.push_back(SpillIndex); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 434 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 435 | Processed.insert(LR); |
| 436 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 437 | // Start from the restore mbb, figure out what part of the live interval |
| 438 | // are defined by the restore. |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 439 | SmallVector<MachineBasicBlock*, 4> WorkList; |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 440 | MachineBasicBlock *MBB = BarrierMBB; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 441 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 442 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 443 | WorkList.push_back(*SI); |
| 444 | |
| 445 | while (!WorkList.empty()) { |
| 446 | MBB = WorkList.back(); |
| 447 | WorkList.pop_back(); |
| 448 | unsigned Idx = LIs->getMBBStartIdx(MBB); |
| 449 | LR = CurrLI->getLiveRangeContaining(Idx); |
| 450 | if (LR && LR->valno == ValNo && !Processed.count(LR)) { |
| 451 | After.push_back(std::make_pair(LR->start, LR->end)); |
| 452 | if (CurrLI->isKill(ValNo, LR->end)) |
| 453 | AfterKills.push_back(LR->end); |
| 454 | Idx = LIs->getMBBEndIdx(MBB); |
| 455 | if (LR->end > Idx) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 456 | // Live range extend beyond at least one mbb. Let's see what other |
| 457 | // mbbs it reaches. |
| 458 | LIs->findReachableMBBs(LR->start, LR->end, WorkList); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 459 | } |
| 460 | Processed.insert(LR); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end(); |
| 465 | I != E; ++I) { |
| 466 | LiveRange *LR = I; |
| 467 | if (LR->valno == ValNo && !Processed.count(LR)) { |
| 468 | Before.push_back(std::make_pair(LR->start, LR->end)); |
| 469 | if (CurrLI->isKill(ValNo, LR->end)) |
| 470 | BeforeKills.push_back(LR->end); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Now create new val#s to represent the live ranges defined by the old def |
| 475 | // those defined by the restore. |
| 476 | unsigned AfterDef = ValNo->def; |
| 477 | MachineInstr *AfterCopy = ValNo->copy; |
| 478 | bool HasPHIKill = ValNo->hasPHIKill; |
| 479 | CurrLI->removeValNo(ValNo); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 480 | VNInfo *BValNo = (Before.empty()) |
| 481 | ? NULL |
| 482 | : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator()); |
| 483 | if (BValNo) |
| 484 | CurrLI->addKills(BValNo, BeforeKills); |
| 485 | |
| 486 | VNInfo *AValNo = (After.empty()) |
| 487 | ? NULL |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 488 | : CurrLI->getNextValue(RestoreIndex, 0, LIs->getVNInfoAllocator()); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 489 | if (AValNo) { |
| 490 | AValNo->hasPHIKill = HasPHIKill; |
| 491 | CurrLI->addKills(AValNo, AfterKills); |
| 492 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 493 | |
| 494 | for (unsigned i = 0, e = Before.size(); i != e; ++i) { |
| 495 | unsigned Start = Before[i].first; |
| 496 | unsigned End = Before[i].second; |
| 497 | CurrLI->addRange(LiveRange(Start, End, BValNo)); |
| 498 | } |
| 499 | for (unsigned i = 0, e = After.size(); i != e; ++i) { |
| 500 | unsigned Start = After[i].first; |
| 501 | unsigned End = After[i].second; |
| 502 | CurrLI->addRange(LiveRange(Start, End, AValNo)); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /// ShrinkWrapToLastUse - There are uses of the current live interval in the |
| 507 | /// given block, shrink wrap the live interval to the last use (i.e. remove |
| 508 | /// from last use to the end of the mbb). In case mbb is the where the barrier |
| 509 | /// is, remove from the last use to the barrier. |
| 510 | bool |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 511 | PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, VNInfo *ValNo, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 512 | SmallVector<MachineOperand*, 4> &Uses, |
| 513 | SmallPtrSet<MachineInstr*, 4> &UseMIs) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 514 | MachineOperand *LastMO = 0; |
| 515 | MachineInstr *LastMI = 0; |
| 516 | if (MBB != BarrierMBB && Uses.size() == 1) { |
| 517 | // Single use, no need to traverse the block. We can't assume this for the |
| 518 | // barrier bb though since the use is probably below the barrier. |
| 519 | LastMO = Uses[0]; |
| 520 | LastMI = LastMO->getParent(); |
| 521 | } else { |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 522 | MachineBasicBlock::iterator MEE = MBB->begin(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 523 | MachineBasicBlock::iterator MII; |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 524 | if (MBB == BarrierMBB) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 525 | MII = Barrier; |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 526 | else |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 527 | MII = MBB->end(); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 528 | while (MII != MEE) { |
| 529 | --MII; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 530 | MachineInstr *UseMI = &*MII; |
| 531 | if (!UseMIs.count(UseMI)) |
| 532 | continue; |
| 533 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 534 | MachineOperand &MO = UseMI->getOperand(i); |
| 535 | if (MO.isReg() && MO.getReg() == CurrLI->reg) { |
| 536 | LastMO = &MO; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | LastMI = UseMI; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // Cut off live range from last use (or beginning of the mbb if there |
| 546 | // are no uses in it) to the end of the mbb. |
| 547 | unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1; |
| 548 | if (LastMI) { |
| 549 | RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1; |
| 550 | assert(!LastMO->isKill() && "Last use already terminates the interval?"); |
| 551 | LastMO->setIsKill(); |
| 552 | } else { |
| 553 | assert(MBB == BarrierMBB); |
| 554 | RangeStart = LIs->getMBBStartIdx(MBB); |
| 555 | } |
| 556 | if (MBB == BarrierMBB) |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 557 | RangeEnd = LIs->getUseIndex(BarrierIdx)+1; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 558 | CurrLI->removeRange(RangeStart, RangeEnd); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 559 | if (LastMI) |
| 560 | CurrLI->addKill(ValNo, RangeStart); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 561 | |
| 562 | // Return true if the last use becomes a new kill. |
| 563 | return LastMI; |
| 564 | } |
| 565 | |
| 566 | /// ShrinkWrapLiveInterval - Recursively traverse the predecessor |
| 567 | /// chain to find the new 'kills' and shrink wrap the live interval to the |
| 568 | /// new kill indices. |
| 569 | void |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 570 | PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB, |
| 571 | MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 572 | SmallPtrSet<MachineBasicBlock*, 8> &Visited, |
| 573 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses, |
| 574 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs, |
| 575 | SmallVector<MachineBasicBlock*, 4> &UseMBBs) { |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 576 | if (Visited.count(MBB)) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 577 | return; |
| 578 | |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 579 | // If live interval is live in another successor path, then we can't process |
| 580 | // this block. But we may able to do so after all the successors have been |
| 581 | // processed. |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 582 | if (MBB != BarrierMBB) { |
| 583 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 584 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 585 | MachineBasicBlock *SMBB = *SI; |
| 586 | if (SMBB == SuccMBB) |
| 587 | continue; |
| 588 | if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB))) |
| 589 | return; |
| 590 | } |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | Visited.insert(MBB); |
| 594 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 595 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator |
| 596 | UMII = Uses.find(MBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 597 | if (UMII != Uses.end()) { |
| 598 | // At least one use in this mbb, lets look for the kill. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 599 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 600 | UMII2 = UseMIs.find(MBB); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 601 | if (ShrinkWrapToLastUse(MBB, ValNo, UMII->second, UMII2->second)) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 602 | // Found a kill, shrink wrapping of this path ends here. |
| 603 | return; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 604 | } else if (MBB == DefMBB) { |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 605 | // There are no uses after the def. |
| 606 | MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 607 | if (UseMBBs.empty()) { |
| 608 | // The only use must be below barrier in the barrier block. It's safe to |
| 609 | // remove the def. |
| 610 | LIs->RemoveMachineInstrFromMaps(DefMI); |
| 611 | DefMI->eraseFromParent(); |
| 612 | CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1); |
| 613 | } |
Evan Cheng | 79d5b5a | 2008-10-25 23:49:39 +0000 | [diff] [blame] | 614 | } else if (MBB == BarrierMBB) { |
| 615 | // Remove entire live range from start of mbb to barrier. |
| 616 | CurrLI->removeRange(LIs->getMBBStartIdx(MBB), |
| 617 | LIs->getUseIndex(BarrierIdx)+1); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 618 | } else { |
Evan Cheng | 79d5b5a | 2008-10-25 23:49:39 +0000 | [diff] [blame] | 619 | // Remove entire live range of the mbb out of the live interval. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 620 | CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | if (MBB == DefMBB) |
| 624 | // Reached the def mbb, stop traversing this path further. |
| 625 | return; |
| 626 | |
| 627 | // Traverse the pathes up the predecessor chains further. |
| 628 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 629 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 630 | MachineBasicBlock *Pred = *PI; |
| 631 | if (Pred == MBB) |
| 632 | continue; |
| 633 | if (Pred == DefMBB && ValNo->hasPHIKill) |
| 634 | // Pred is the def bb and the def reaches other val#s, we must |
| 635 | // allow the value to be live out of the bb. |
| 636 | continue; |
Owen Anderson | 80fe873 | 2008-11-11 22:11:27 +0000 | [diff] [blame] | 637 | if (!CurrLI->liveAt(LIs->getMBBEndIdx(Pred)-1)) |
| 638 | return; |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 639 | ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited, |
| 640 | Uses, UseMIs, UseMBBs); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | return; |
| 644 | } |
| 645 | |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 646 | |
Owen Anderson | 6002e99 | 2008-12-04 21:20:30 +0000 | [diff] [blame] | 647 | void PreAllocSplitting::RepairLiveInterval(LiveInterval* CurrLI, |
| 648 | VNInfo* ValNo, |
| 649 | MachineInstr* DefMI, |
| 650 | unsigned RestoreIdx) { |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 651 | // Shrink wrap the live interval by walking up the CFG and find the |
| 652 | // new kills. |
| 653 | // Now let's find all the uses of the val#. |
| 654 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses; |
| 655 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs; |
| 656 | SmallPtrSet<MachineBasicBlock*, 4> Seen; |
| 657 | SmallVector<MachineBasicBlock*, 4> UseMBBs; |
| 658 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg), |
| 659 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 660 | MachineOperand &UseMO = UI.getOperand(); |
| 661 | MachineInstr *UseMI = UseMO.getParent(); |
| 662 | unsigned UseIdx = LIs->getInstructionIndex(UseMI); |
| 663 | LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx); |
| 664 | if (ULR->valno != ValNo) |
| 665 | continue; |
| 666 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 667 | // Remember which other mbb's use this val#. |
| 668 | if (Seen.insert(UseMBB) && UseMBB != BarrierMBB) |
| 669 | UseMBBs.push_back(UseMBB); |
| 670 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator |
| 671 | UMII = Uses.find(UseMBB); |
| 672 | if (UMII != Uses.end()) { |
| 673 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 674 | UMII2 = UseMIs.find(UseMBB); |
| 675 | UMII->second.push_back(&UseMO); |
| 676 | UMII2->second.insert(UseMI); |
| 677 | } else { |
| 678 | SmallVector<MachineOperand*, 4> Ops; |
| 679 | Ops.push_back(&UseMO); |
| 680 | Uses.insert(std::make_pair(UseMBB, Ops)); |
| 681 | SmallPtrSet<MachineInstr*, 4> MIs; |
| 682 | MIs.insert(UseMI); |
| 683 | UseMIs.insert(std::make_pair(UseMBB, MIs)); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // Walk up the predecessor chains. |
| 688 | SmallPtrSet<MachineBasicBlock*, 8> Visited; |
| 689 | ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMI->getParent(), Visited, |
| 690 | Uses, UseMIs, UseMBBs); |
| 691 | |
| 692 | // FIXME: If ValNo->hasPHIKill is false, then renumber the val# by |
| 693 | // the restore. |
| 694 | |
| 695 | // Remove live range from barrier to the restore. FIXME: Find a better |
| 696 | // point to re-start the live interval. |
| 697 | UpdateRegisterInterval(ValNo, LIs->getUseIndex(BarrierIdx)+1, |
| 698 | LIs->getDefIndex(RestoreIdx)); |
Owen Anderson | 6002e99 | 2008-12-04 21:20:30 +0000 | [diff] [blame] | 699 | } |
| 700 | bool PreAllocSplitting::Rematerialize(unsigned vreg, VNInfo* ValNo, |
| 701 | MachineInstr* DefMI, |
| 702 | MachineBasicBlock::iterator RestorePt, |
| 703 | unsigned RestoreIdx, |
| 704 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB) { |
| 705 | MachineBasicBlock& MBB = *RestorePt->getParent(); |
| 706 | |
| 707 | MachineBasicBlock::iterator KillPt = BarrierMBB->end(); |
| 708 | unsigned KillIdx = 0; |
| 709 | if (ValNo->def == ~0U || DefMI->getParent() == BarrierMBB) |
| 710 | KillPt = findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, KillIdx); |
| 711 | else |
| 712 | KillPt = findNextEmptySlot(DefMI->getParent(), DefMI, KillIdx); |
| 713 | |
| 714 | if (KillPt == DefMI->getParent()->end()) |
| 715 | return false; |
| 716 | |
| 717 | TII->reMaterialize(MBB, RestorePt, vreg, DefMI); |
| 718 | LIs->InsertMachineInstrInMaps(prior(RestorePt), RestoreIdx); |
| 719 | |
| 720 | if (KillPt->getParent() == BarrierMBB) { |
| 721 | UpdateRegisterInterval(ValNo, LIs->getUseIndex(KillIdx)+1, |
| 722 | LIs->getDefIndex(RestoreIdx)); |
| 723 | |
| 724 | ++NumSplits; |
| 725 | ++NumRemats; |
| 726 | return true; |
| 727 | } |
| 728 | |
| 729 | RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIdx); |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 730 | |
| 731 | ++NumSplits; |
| 732 | ++NumRemats; |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 733 | return true; |
| 734 | } |
| 735 | |
| 736 | MachineInstr* PreAllocSplitting::FoldSpill(unsigned vreg, |
| 737 | const TargetRegisterClass* RC, |
| 738 | MachineInstr* DefMI, |
| 739 | MachineInstr* Barrier, |
| 740 | MachineBasicBlock* MBB, |
| 741 | int& SS, |
| 742 | SmallPtrSet<MachineInstr*, 4>& RefsInMBB) { |
| 743 | MachineBasicBlock::iterator Pt = MBB->begin(); |
| 744 | |
| 745 | // Go top down if RefsInMBB is empty. |
| 746 | if (RefsInMBB.empty()) |
| 747 | return 0; |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 748 | |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 749 | MachineBasicBlock::iterator FoldPt = Barrier; |
| 750 | while (&*FoldPt != DefMI && FoldPt != MBB->begin() && |
| 751 | !RefsInMBB.count(FoldPt)) |
| 752 | --FoldPt; |
| 753 | |
| 754 | int OpIdx = FoldPt->findRegisterDefOperandIdx(vreg, false); |
| 755 | if (OpIdx == -1) |
| 756 | return 0; |
| 757 | |
| 758 | SmallVector<unsigned, 1> Ops; |
| 759 | Ops.push_back(OpIdx); |
| 760 | |
| 761 | if (!TII->canFoldMemoryOperand(FoldPt, Ops)) |
| 762 | return 0; |
| 763 | |
| 764 | DenseMap<unsigned, int>::iterator I = IntervalSSMap.find(vreg); |
| 765 | if (I != IntervalSSMap.end()) { |
| 766 | SS = I->second; |
| 767 | } else { |
| 768 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
| 769 | |
| 770 | } |
| 771 | |
| 772 | MachineInstr* FMI = TII->foldMemoryOperand(*MBB->getParent(), |
| 773 | FoldPt, Ops, SS); |
| 774 | |
| 775 | if (FMI) { |
| 776 | LIs->ReplaceMachineInstrInMaps(FoldPt, FMI); |
| 777 | FMI = MBB->insert(MBB->erase(FoldPt), FMI); |
| 778 | ++NumFolds; |
| 779 | |
| 780 | IntervalSSMap[vreg] = SS; |
| 781 | CurrSLI = &LSs->getOrCreateInterval(SS); |
| 782 | if (CurrSLI->hasAtLeastOneValue()) |
| 783 | CurrSValNo = CurrSLI->getValNumInfo(0); |
| 784 | else |
| 785 | CurrSValNo = CurrSLI->getNextValue(~0U, 0, LSs->getVNInfoAllocator()); |
| 786 | } |
| 787 | |
| 788 | return FMI; |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 791 | /// SplitRegLiveInterval - Split (spill and restore) the given live interval |
| 792 | /// so it would not cross the barrier that's being processed. Shrink wrap |
| 793 | /// (minimize) the live interval to the last uses. |
| 794 | bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) { |
| 795 | CurrLI = LI; |
| 796 | |
| 797 | // Find live range where current interval cross the barrier. |
| 798 | LiveInterval::iterator LR = |
| 799 | CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx)); |
| 800 | VNInfo *ValNo = LR->valno; |
| 801 | |
| 802 | if (ValNo->def == ~1U) { |
| 803 | // Defined by a dead def? How can this be? |
| 804 | assert(0 && "Val# is defined by a dead def?"); |
| 805 | abort(); |
| 806 | } |
| 807 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 808 | MachineInstr *DefMI = (ValNo->def != ~0U) |
| 809 | ? LIs->getInstructionFromIndex(ValNo->def) : NULL; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 810 | |
Owen Anderson | b214c69 | 2008-11-05 00:32:13 +0000 | [diff] [blame] | 811 | // If this would create a new join point, do not split. |
| 812 | if (DefMI && createsNewJoin(LR, DefMI->getParent(), Barrier->getParent())) |
| 813 | return false; |
| 814 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 815 | // Find all references in the barrier mbb. |
| 816 | SmallPtrSet<MachineInstr*, 4> RefsInMBB; |
| 817 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg), |
| 818 | E = MRI->reg_end(); I != E; ++I) { |
| 819 | MachineInstr *RefMI = &*I; |
| 820 | if (RefMI->getParent() == BarrierMBB) |
| 821 | RefsInMBB.insert(RefMI); |
| 822 | } |
| 823 | |
| 824 | // Find a point to restore the value after the barrier. |
| 825 | unsigned RestoreIndex; |
| 826 | MachineBasicBlock::iterator RestorePt = |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 827 | findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 828 | if (RestorePt == BarrierMBB->end()) |
| 829 | return false; |
| 830 | |
Owen Anderson | 75fa96b | 2008-11-19 04:28:29 +0000 | [diff] [blame] | 831 | if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI)) |
| 832 | if (Rematerialize(LI->reg, ValNo, DefMI, RestorePt, |
| 833 | RestoreIndex, RefsInMBB)) |
| 834 | return true; |
| 835 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 836 | // Add a spill either before the barrier or after the definition. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 837 | MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 838 | const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 839 | unsigned SpillIndex = 0; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 840 | MachineInstr *SpillMI = NULL; |
Evan Cheng | 985921e | 2008-10-27 23:29:28 +0000 | [diff] [blame] | 841 | int SS = -1; |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 842 | if (ValNo->def == ~0U) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 843 | // 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] | 844 | if ((SpillMI = FoldSpill(LI->reg, RC, 0, Barrier, |
| 845 | BarrierMBB, SS, RefsInMBB))) { |
| 846 | SpillIndex = LIs->getInstructionIndex(SpillMI); |
| 847 | } else { |
| 848 | MachineBasicBlock::iterator SpillPt = |
| 849 | findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex); |
| 850 | if (SpillPt == BarrierMBB->begin()) |
| 851 | return false; // No gap to insert spill. |
| 852 | // Add spill. |
| 853 | |
| 854 | SS = CreateSpillStackSlot(CurrLI->reg, RC); |
| 855 | TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC); |
| 856 | SpillMI = prior(SpillPt); |
| 857 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
| 858 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 859 | } else if (!IsAvailableInStack(DefMBB, CurrLI->reg, ValNo->def, |
| 860 | RestoreIndex, SpillIndex, SS)) { |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 861 | // If it's already split, just restore the value. There is no need to spill |
| 862 | // the def again. |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 863 | if (!DefMI) |
| 864 | return false; // Def is dead. Do nothing. |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 865 | |
| 866 | if ((SpillMI = FoldSpill(LI->reg, RC, DefMI, Barrier, |
| 867 | BarrierMBB, SS, RefsInMBB))) { |
| 868 | SpillIndex = LIs->getInstructionIndex(SpillMI); |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 869 | } else { |
Owen Anderson | 7b9d67c | 2008-12-02 18:53:47 +0000 | [diff] [blame] | 870 | // Check if it's possible to insert a spill after the def MI. |
| 871 | MachineBasicBlock::iterator SpillPt; |
| 872 | if (DefMBB == BarrierMBB) { |
| 873 | // Add spill after the def and the last use before the barrier. |
| 874 | SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI, |
| 875 | RefsInMBB, SpillIndex); |
| 876 | if (SpillPt == DefMBB->begin()) |
| 877 | return false; // No gap to insert spill. |
| 878 | } else { |
| 879 | SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex); |
| 880 | if (SpillPt == DefMBB->end()) |
| 881 | return false; // No gap to insert spill. |
| 882 | } |
| 883 | // Add spill. The store instruction kills the register if def is before |
| 884 | // the barrier in the barrier block. |
| 885 | SS = CreateSpillStackSlot(CurrLI->reg, RC); |
| 886 | TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, |
| 887 | DefMBB == BarrierMBB, SS, RC); |
| 888 | SpillMI = prior(SpillPt); |
| 889 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame] | 890 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 893 | // Remember def instruction index to spill index mapping. |
| 894 | if (DefMI && SpillMI) |
| 895 | Def2SpillMap[ValNo->def] = SpillIndex; |
| 896 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 897 | // Add restore. |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 898 | TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC); |
| 899 | MachineInstr *LoadMI = prior(RestorePt); |
| 900 | LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex); |
| 901 | |
| 902 | // If live interval is spilled in the same block as the barrier, just |
| 903 | // create a hole in the interval. |
| 904 | if (!DefMBB || |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 905 | (SpillMI && SpillMI->getParent() == BarrierMBB)) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 906 | // Update spill stack slot live interval. |
| 907 | UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1, |
| 908 | LIs->getDefIndex(RestoreIndex)); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 909 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 910 | UpdateRegisterInterval(ValNo, LIs->getUseIndex(SpillIndex)+1, |
| 911 | LIs->getDefIndex(RestoreIndex)); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 912 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 913 | ++NumSplits; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 914 | return true; |
| 915 | } |
| 916 | |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 917 | // Update spill stack slot live interval. |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 918 | UpdateSpillSlotInterval(ValNo, LIs->getUseIndex(SpillIndex)+1, |
| 919 | LIs->getDefIndex(RestoreIndex)); |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 920 | |
Owen Anderson | 6002e99 | 2008-12-04 21:20:30 +0000 | [diff] [blame] | 921 | RepairLiveInterval(CurrLI, ValNo, DefMI, RestoreIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 922 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 923 | ++NumSplits; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 924 | return true; |
| 925 | } |
| 926 | |
| 927 | /// SplitRegLiveIntervals - Split all register live intervals that cross the |
| 928 | /// barrier that's being processed. |
| 929 | bool |
| 930 | PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) { |
| 931 | // First find all the virtual registers whose live intervals are intercepted |
| 932 | // by the current barrier. |
| 933 | SmallVector<LiveInterval*, 8> Intervals; |
| 934 | for (const TargetRegisterClass **RC = RCs; *RC; ++RC) { |
Evan Cheng | 2306628 | 2008-10-27 07:14:50 +0000 | [diff] [blame] | 935 | if (TII->IgnoreRegisterClassBarriers(*RC)) |
| 936 | continue; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 937 | std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC); |
| 938 | for (unsigned i = 0, e = VRs.size(); i != e; ++i) { |
| 939 | unsigned Reg = VRs[i]; |
| 940 | if (!LIs->hasInterval(Reg)) |
| 941 | continue; |
| 942 | LiveInterval *LI = &LIs->getInterval(Reg); |
| 943 | if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg)) |
| 944 | // Virtual register live interval is intercepted by the barrier. We |
| 945 | // should split and shrink wrap its interval if possible. |
| 946 | Intervals.push_back(LI); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Process the affected live intervals. |
| 951 | bool Change = false; |
| 952 | while (!Intervals.empty()) { |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 953 | if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit) |
| 954 | break; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 955 | LiveInterval *LI = Intervals.back(); |
| 956 | Intervals.pop_back(); |
| 957 | Change |= SplitRegLiveInterval(LI); |
| 958 | } |
| 959 | |
| 960 | return Change; |
| 961 | } |
| 962 | |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 963 | bool PreAllocSplitting::createsNewJoin(LiveRange* LR, |
| 964 | MachineBasicBlock* DefMBB, |
| 965 | MachineBasicBlock* BarrierMBB) { |
| 966 | if (DefMBB == BarrierMBB) |
| 967 | return false; |
| 968 | |
| 969 | if (LR->valno->hasPHIKill) |
| 970 | return false; |
| 971 | |
| 972 | unsigned MBBEnd = LIs->getMBBEndIdx(BarrierMBB); |
| 973 | if (LR->end < MBBEnd) |
| 974 | return false; |
| 975 | |
| 976 | MachineLoopInfo& MLI = getAnalysis<MachineLoopInfo>(); |
| 977 | if (MLI.getLoopFor(DefMBB) != MLI.getLoopFor(BarrierMBB)) |
| 978 | return true; |
| 979 | |
| 980 | MachineDominatorTree& MDT = getAnalysis<MachineDominatorTree>(); |
| 981 | SmallPtrSet<MachineBasicBlock*, 4> Visited; |
| 982 | typedef std::pair<MachineBasicBlock*, |
| 983 | MachineBasicBlock::succ_iterator> ItPair; |
| 984 | SmallVector<ItPair, 4> Stack; |
| 985 | Stack.push_back(std::make_pair(BarrierMBB, BarrierMBB->succ_begin())); |
| 986 | |
| 987 | while (!Stack.empty()) { |
| 988 | ItPair P = Stack.back(); |
| 989 | Stack.pop_back(); |
| 990 | |
| 991 | MachineBasicBlock* PredMBB = P.first; |
| 992 | MachineBasicBlock::succ_iterator S = P.second; |
| 993 | |
| 994 | if (S == PredMBB->succ_end()) |
| 995 | continue; |
| 996 | else if (Visited.count(*S)) { |
| 997 | Stack.push_back(std::make_pair(PredMBB, ++S)); |
| 998 | continue; |
| 999 | } else |
Owen Anderson | b214c69 | 2008-11-05 00:32:13 +0000 | [diff] [blame] | 1000 | Stack.push_back(std::make_pair(PredMBB, S+1)); |
Owen Anderson | f1f75b1 | 2008-11-04 22:22:41 +0000 | [diff] [blame] | 1001 | |
| 1002 | MachineBasicBlock* MBB = *S; |
| 1003 | Visited.insert(MBB); |
| 1004 | |
| 1005 | if (MBB == BarrierMBB) |
| 1006 | return true; |
| 1007 | |
| 1008 | MachineDomTreeNode* DefMDTN = MDT.getNode(DefMBB); |
| 1009 | MachineDomTreeNode* BarrierMDTN = MDT.getNode(BarrierMBB); |
| 1010 | MachineDomTreeNode* MDTN = MDT.getNode(MBB)->getIDom(); |
| 1011 | while (MDTN) { |
| 1012 | if (MDTN == DefMDTN) |
| 1013 | return true; |
| 1014 | else if (MDTN == BarrierMDTN) |
| 1015 | break; |
| 1016 | MDTN = MDTN->getIDom(); |
| 1017 | } |
| 1018 | |
| 1019 | MBBEnd = LIs->getMBBEndIdx(MBB); |
| 1020 | if (LR->end > MBBEnd) |
| 1021 | Stack.push_back(std::make_pair(MBB, MBB->succ_begin())); |
| 1022 | } |
| 1023 | |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
| 1027 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 1028 | bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d0e32c5 | 2008-10-29 05:06:14 +0000 | [diff] [blame] | 1029 | CurrMF = &MF; |
| 1030 | TM = &MF.getTarget(); |
| 1031 | TII = TM->getInstrInfo(); |
| 1032 | MFI = MF.getFrameInfo(); |
| 1033 | MRI = &MF.getRegInfo(); |
| 1034 | LIs = &getAnalysis<LiveIntervals>(); |
| 1035 | LSs = &getAnalysis<LiveStacks>(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1036 | |
| 1037 | bool MadeChange = false; |
| 1038 | |
| 1039 | // Make sure blocks are numbered in order. |
| 1040 | MF.RenumberBlocks(); |
| 1041 | |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 1042 | #if 0 |
| 1043 | // FIXME: Go top down. |
| 1044 | MachineBasicBlock *Entry = MF.begin(); |
| 1045 | SmallPtrSet<MachineBasicBlock*,16> Visited; |
| 1046 | |
| 1047 | for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> > |
| 1048 | DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited); |
| 1049 | DFI != E; ++DFI) { |
| 1050 | BarrierMBB = *DFI; |
| 1051 | for (MachineBasicBlock::iterator I = BarrierMBB->begin(), |
| 1052 | E = BarrierMBB->end(); I != E; ++I) { |
| 1053 | Barrier = &*I; |
| 1054 | const TargetRegisterClass **BarrierRCs = |
| 1055 | Barrier->getDesc().getRegClassBarriers(); |
| 1056 | if (!BarrierRCs) |
| 1057 | continue; |
| 1058 | BarrierIdx = LIs->getInstructionIndex(Barrier); |
| 1059 | MadeChange |= SplitRegLiveIntervals(BarrierRCs); |
| 1060 | } |
| 1061 | } |
| 1062 | #else |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1063 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 1064 | I != E; ++I) { |
| 1065 | BarrierMBB = &*I; |
| 1066 | for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(), |
| 1067 | EE = BarrierMBB->rend(); II != EE; ++II) { |
| 1068 | Barrier = &*II; |
| 1069 | const TargetRegisterClass **BarrierRCs = |
| 1070 | Barrier->getDesc().getRegClassBarriers(); |
| 1071 | if (!BarrierRCs) |
| 1072 | continue; |
| 1073 | BarrierIdx = LIs->getInstructionIndex(Barrier); |
| 1074 | MadeChange |= SplitRegLiveIntervals(BarrierRCs); |
| 1075 | } |
| 1076 | } |
Evan Cheng | 5489893 | 2008-10-29 08:39:34 +0000 | [diff] [blame] | 1077 | #endif |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 1078 | |
| 1079 | return MadeChange; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 1080 | } |