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