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 | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetOptions.h" |
| 28 | #include "llvm/Target/TargetRegisterInfo.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include <map> |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 36 | static cl::opt<int> PreSplitLimit("pre-split-limit", cl::init(-1), cl::Hidden); |
| 37 | |
| 38 | STATISTIC(NumSplits, "Number of intervals split"); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 39 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | class VISIBILITY_HIDDEN PreAllocSplitting : public MachineFunctionPass { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 42 | MachineFunction *CurMF; |
| 43 | const TargetMachine *TM; |
| 44 | const TargetInstrInfo *TII; |
| 45 | MachineFrameInfo *MFI; |
| 46 | MachineRegisterInfo *MRI; |
| 47 | LiveIntervals *LIs; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 48 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 49 | // Barrier - Current barrier being processed. |
| 50 | MachineInstr *Barrier; |
| 51 | |
| 52 | // BarrierMBB - Basic block where the barrier resides in. |
| 53 | MachineBasicBlock *BarrierMBB; |
| 54 | |
| 55 | // Barrier - Current barrier index. |
| 56 | unsigned BarrierIdx; |
| 57 | |
| 58 | // CurrLI - Current live interval being split. |
| 59 | LiveInterval *CurrLI; |
| 60 | |
| 61 | // LIValNoSSMap - A map from live interval and val# pairs to spill slots. |
| 62 | // This records what live interval's val# has been split and what spill |
| 63 | // slot was used. |
| 64 | std::map<std::pair<unsigned, unsigned>, int> LIValNoSSMap; |
| 65 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 66 | // RestoreMIs - All the restores inserted due to live interval splitting. |
| 67 | SmallPtrSet<MachineInstr*, 8> RestoreMIs; |
| 68 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 69 | public: |
| 70 | static char ID; |
| 71 | PreAllocSplitting() : MachineFunctionPass(&ID) {} |
| 72 | |
| 73 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 74 | |
| 75 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 76 | AU.addRequired<LiveIntervals>(); |
| 77 | AU.addPreserved<LiveIntervals>(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 78 | AU.addPreserved<RegisterCoalescer>(); |
| 79 | if (StrongPHIElim) |
| 80 | AU.addPreservedID(StrongPHIEliminationID); |
| 81 | else |
| 82 | AU.addPreservedID(PHIEliminationID); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 83 | MachineFunctionPass::getAnalysisUsage(AU); |
| 84 | } |
| 85 | |
| 86 | virtual void releaseMemory() { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 87 | LIValNoSSMap.clear(); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 88 | RestoreMIs.clear(); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | virtual const char *getPassName() const { |
| 92 | return "Pre-Register Allocaton Live Interval Splitting"; |
| 93 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 94 | |
| 95 | /// print - Implement the dump method. |
| 96 | virtual void print(std::ostream &O, const Module* M = 0) const { |
| 97 | LIs->print(O, M); |
| 98 | } |
| 99 | |
| 100 | void print(std::ostream *O, const Module* M = 0) const { |
| 101 | if (O) print(*O, M); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | MachineBasicBlock::iterator |
| 106 | findNextEmptySlot(MachineBasicBlock*, MachineInstr*, |
| 107 | unsigned&); |
| 108 | |
| 109 | MachineBasicBlock::iterator |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 110 | findSpillPoint(MachineBasicBlock*, MachineInstr*, MachineInstr*, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 111 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 112 | |
| 113 | MachineBasicBlock::iterator |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 114 | findRestorePoint(MachineBasicBlock*, MachineInstr*, unsigned, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 115 | SmallPtrSet<MachineInstr*, 4>&, unsigned&); |
| 116 | |
| 117 | void RecordSplit(unsigned, unsigned, unsigned, int); |
| 118 | |
| 119 | bool isAlreadySplit(unsigned, unsigned, int&); |
| 120 | |
| 121 | void UpdateIntervalForSplit(VNInfo*, unsigned, unsigned); |
| 122 | |
| 123 | bool ShrinkWrapToLastUse(MachineBasicBlock*, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 124 | SmallVector<MachineOperand*, 4>&, |
| 125 | SmallPtrSet<MachineInstr*, 4>&); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 126 | |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 127 | void ShrinkWrapLiveInterval(VNInfo*, MachineBasicBlock*, MachineBasicBlock*, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 128 | MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 8>&, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 129 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >&, |
| 130 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >&, |
| 131 | SmallVector<MachineBasicBlock*, 4>&); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 132 | |
| 133 | bool SplitRegLiveInterval(LiveInterval*); |
| 134 | |
| 135 | bool SplitRegLiveIntervals(const TargetRegisterClass **); |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 136 | }; |
| 137 | } // end anonymous namespace |
| 138 | |
| 139 | char PreAllocSplitting::ID = 0; |
| 140 | |
| 141 | static RegisterPass<PreAllocSplitting> |
| 142 | X("pre-alloc-splitting", "Pre-Register Allocation Live Interval Splitting"); |
| 143 | |
| 144 | const PassInfo *const llvm::PreAllocSplittingID = &X; |
| 145 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 146 | |
| 147 | /// findNextEmptySlot - Find a gap after the given machine instruction in the |
| 148 | /// instruction index map. If there isn't one, return end(). |
| 149 | MachineBasicBlock::iterator |
| 150 | PreAllocSplitting::findNextEmptySlot(MachineBasicBlock *MBB, MachineInstr *MI, |
| 151 | unsigned &SpotIndex) { |
| 152 | MachineBasicBlock::iterator MII = MI; |
| 153 | if (++MII != MBB->end()) { |
| 154 | unsigned Index = LIs->findGapBeforeInstr(LIs->getInstructionIndex(MII)); |
| 155 | if (Index) { |
| 156 | SpotIndex = Index; |
| 157 | return MII; |
| 158 | } |
| 159 | } |
| 160 | return MBB->end(); |
| 161 | } |
| 162 | |
| 163 | /// findSpillPoint - Find a gap as far away from the given MI that's suitable |
| 164 | /// for spilling the current live interval. The index must be before any |
| 165 | /// defs and uses of the live interval register in the mbb. Return begin() if |
| 166 | /// none is found. |
| 167 | MachineBasicBlock::iterator |
| 168 | PreAllocSplitting::findSpillPoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 169 | MachineInstr *DefMI, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 170 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 171 | unsigned &SpillIndex) { |
| 172 | MachineBasicBlock::iterator Pt = MBB->begin(); |
| 173 | |
| 174 | // Go top down if RefsInMBB is empty. |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 175 | if (RefsInMBB.empty() && !DefMI) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 176 | MachineBasicBlock::iterator MII = MBB->begin(); |
| 177 | MachineBasicBlock::iterator EndPt = MI; |
| 178 | do { |
| 179 | ++MII; |
| 180 | unsigned Index = LIs->getInstructionIndex(MII); |
| 181 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 182 | if (Gap) { |
| 183 | Pt = MII; |
| 184 | SpillIndex = Gap; |
| 185 | break; |
| 186 | } |
| 187 | } while (MII != EndPt); |
| 188 | } else { |
| 189 | MachineBasicBlock::iterator MII = MI; |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 190 | MachineBasicBlock::iterator EndPt = DefMI |
| 191 | ? MachineBasicBlock::iterator(DefMI) : MBB->begin(); |
| 192 | while (MII != EndPt && !RefsInMBB.count(MII)) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 193 | unsigned Index = LIs->getInstructionIndex(MII); |
| 194 | if (LIs->hasGapBeforeInstr(Index)) { |
| 195 | Pt = MII; |
| 196 | SpillIndex = LIs->findGapBeforeInstr(Index, true); |
| 197 | } |
| 198 | --MII; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return Pt; |
| 203 | } |
| 204 | |
| 205 | /// findRestorePoint - Find a gap in the instruction index map that's suitable |
| 206 | /// for restoring the current live interval value. The index must be before any |
| 207 | /// uses of the live interval register in the mbb. Return end() if none is |
| 208 | /// found. |
| 209 | MachineBasicBlock::iterator |
| 210 | PreAllocSplitting::findRestorePoint(MachineBasicBlock *MBB, MachineInstr *MI, |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 211 | unsigned LastIdx, |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 212 | SmallPtrSet<MachineInstr*, 4> &RefsInMBB, |
| 213 | unsigned &RestoreIndex) { |
| 214 | MachineBasicBlock::iterator Pt = MBB->end(); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 215 | unsigned EndIdx = LIs->getMBBEndIdx(MBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 216 | |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 217 | // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond |
| 218 | // the last index in the live range. |
| 219 | if (RefsInMBB.empty() && LastIdx >= EndIdx) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 220 | MachineBasicBlock::iterator MII = MBB->end(); |
| 221 | MachineBasicBlock::iterator EndPt = MI; |
| 222 | do { |
| 223 | --MII; |
| 224 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | 56ab0de | 2008-10-24 18:46:44 +0000 | [diff] [blame] | 225 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 226 | if (Gap) { |
| 227 | Pt = MII; |
| 228 | RestoreIndex = Gap; |
| 229 | break; |
| 230 | } |
| 231 | } while (MII != EndPt); |
| 232 | } else { |
| 233 | MachineBasicBlock::iterator MII = MI; |
| 234 | MII = ++MII; |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 235 | // FIXME: Limit the number of instructions to examine to reduce |
| 236 | // compile time? |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 237 | while (MII != MBB->end()) { |
| 238 | unsigned Index = LIs->getInstructionIndex(MII); |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 239 | if (Index > LastIdx) |
| 240 | break; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 241 | unsigned Gap = LIs->findGapBeforeInstr(Index); |
| 242 | if (Gap) { |
| 243 | Pt = MII; |
| 244 | RestoreIndex = Gap; |
| 245 | } |
| 246 | if (RefsInMBB.count(MII)) |
| 247 | break; |
| 248 | ++MII; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return Pt; |
| 253 | } |
| 254 | |
| 255 | /// RecordSplit - Given a register live interval is split, remember the spill |
| 256 | /// slot where the val#s are in. |
| 257 | void PreAllocSplitting::RecordSplit(unsigned Reg, unsigned SpillIndex, |
| 258 | unsigned RestoreIndex, int SS) { |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 259 | const LiveRange *LR = NULL; |
| 260 | if (SpillIndex) { |
| 261 | LR = CurrLI->getLiveRangeContaining(LIs->getUseIndex(SpillIndex)); |
| 262 | LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg, |
| 263 | LR->valno->id), SS)); |
| 264 | } |
| 265 | LR = CurrLI->getLiveRangeContaining(LIs->getDefIndex(RestoreIndex)); |
| 266 | LIValNoSSMap.insert(std::make_pair(std::make_pair(CurrLI->reg, |
| 267 | LR->valno->id), SS)); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /// isAlreadySplit - Return if a given val# of a register live interval is already |
| 271 | /// split. Also return by reference the spill stock where the value is. |
| 272 | bool PreAllocSplitting::isAlreadySplit(unsigned Reg, unsigned ValNoId, int &SS){ |
| 273 | std::map<std::pair<unsigned, unsigned>, int>::iterator I = |
| 274 | LIValNoSSMap.find(std::make_pair(Reg, ValNoId)); |
| 275 | if (I == LIValNoSSMap.end()) |
| 276 | return false; |
| 277 | SS = I->second; |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | /// UpdateIntervalForSplit - Given the specified val# of the current live |
| 282 | /// interval is being split, and the split and rejoin indices, update the live |
| 283 | /// interval accordingly. |
| 284 | void |
| 285 | PreAllocSplitting::UpdateIntervalForSplit(VNInfo *ValNo, unsigned SplitIndex, |
| 286 | unsigned JoinIndex) { |
| 287 | SmallVector<std::pair<unsigned,unsigned>, 4> Before; |
| 288 | SmallVector<std::pair<unsigned,unsigned>, 4> After; |
| 289 | SmallVector<unsigned, 4> BeforeKills; |
| 290 | SmallVector<unsigned, 4> AfterKills; |
| 291 | SmallPtrSet<const LiveRange*, 4> Processed; |
| 292 | |
| 293 | // First, let's figure out which parts of the live interval is now defined |
| 294 | // by the restore, which are defined by the original definition. |
| 295 | const LiveRange *LR = CurrLI->getLiveRangeContaining(JoinIndex); |
| 296 | After.push_back(std::make_pair(JoinIndex, LR->end)); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 297 | if (CurrLI->isKill(ValNo, LR->end)) |
| 298 | AfterKills.push_back(LR->end); |
| 299 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 300 | assert(LR->contains(SplitIndex)); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 301 | if (SplitIndex > LR->start) { |
| 302 | Before.push_back(std::make_pair(LR->start, SplitIndex)); |
| 303 | BeforeKills.push_back(SplitIndex); |
| 304 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 305 | Processed.insert(LR); |
| 306 | |
| 307 | SmallVector<MachineBasicBlock*, 4> WorkList; |
| 308 | MachineBasicBlock *MBB = LIs->getMBBFromIndex(LR->end-1); |
| 309 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 310 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 311 | WorkList.push_back(*SI); |
| 312 | |
| 313 | while (!WorkList.empty()) { |
| 314 | MBB = WorkList.back(); |
| 315 | WorkList.pop_back(); |
| 316 | unsigned Idx = LIs->getMBBStartIdx(MBB); |
| 317 | LR = CurrLI->getLiveRangeContaining(Idx); |
| 318 | if (LR && LR->valno == ValNo && !Processed.count(LR)) { |
| 319 | After.push_back(std::make_pair(LR->start, LR->end)); |
| 320 | if (CurrLI->isKill(ValNo, LR->end)) |
| 321 | AfterKills.push_back(LR->end); |
| 322 | Idx = LIs->getMBBEndIdx(MBB); |
| 323 | if (LR->end > Idx) { |
| 324 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 325 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 326 | WorkList.push_back(*SI); |
| 327 | if (LR->end > Idx+1) { |
| 328 | MBB = LIs->getMBBFromIndex(LR->end-1); |
| 329 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 330 | SE = MBB->succ_end(); SI != SE; ++SI) |
| 331 | WorkList.push_back(*SI); |
| 332 | } |
| 333 | } |
| 334 | Processed.insert(LR); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | for (LiveInterval::iterator I = CurrLI->begin(), E = CurrLI->end(); |
| 339 | I != E; ++I) { |
| 340 | LiveRange *LR = I; |
| 341 | if (LR->valno == ValNo && !Processed.count(LR)) { |
| 342 | Before.push_back(std::make_pair(LR->start, LR->end)); |
| 343 | if (CurrLI->isKill(ValNo, LR->end)) |
| 344 | BeforeKills.push_back(LR->end); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // Now create new val#s to represent the live ranges defined by the old def |
| 349 | // those defined by the restore. |
| 350 | unsigned AfterDef = ValNo->def; |
| 351 | MachineInstr *AfterCopy = ValNo->copy; |
| 352 | bool HasPHIKill = ValNo->hasPHIKill; |
| 353 | CurrLI->removeValNo(ValNo); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 354 | VNInfo *BValNo = (Before.empty()) |
| 355 | ? NULL |
| 356 | : CurrLI->getNextValue(AfterDef, AfterCopy, LIs->getVNInfoAllocator()); |
| 357 | if (BValNo) |
| 358 | CurrLI->addKills(BValNo, BeforeKills); |
| 359 | |
| 360 | VNInfo *AValNo = (After.empty()) |
| 361 | ? NULL |
| 362 | : CurrLI->getNextValue(JoinIndex,0, LIs->getVNInfoAllocator()); |
| 363 | if (AValNo) { |
| 364 | AValNo->hasPHIKill = HasPHIKill; |
| 365 | CurrLI->addKills(AValNo, AfterKills); |
| 366 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 367 | |
| 368 | for (unsigned i = 0, e = Before.size(); i != e; ++i) { |
| 369 | unsigned Start = Before[i].first; |
| 370 | unsigned End = Before[i].second; |
| 371 | CurrLI->addRange(LiveRange(Start, End, BValNo)); |
| 372 | } |
| 373 | for (unsigned i = 0, e = After.size(); i != e; ++i) { |
| 374 | unsigned Start = After[i].first; |
| 375 | unsigned End = After[i].second; |
| 376 | CurrLI->addRange(LiveRange(Start, End, AValNo)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /// ShrinkWrapToLastUse - There are uses of the current live interval in the |
| 381 | /// given block, shrink wrap the live interval to the last use (i.e. remove |
| 382 | /// from last use to the end of the mbb). In case mbb is the where the barrier |
| 383 | /// is, remove from the last use to the barrier. |
| 384 | bool |
| 385 | PreAllocSplitting::ShrinkWrapToLastUse(MachineBasicBlock *MBB, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 386 | SmallVector<MachineOperand*, 4> &Uses, |
| 387 | SmallPtrSet<MachineInstr*, 4> &UseMIs) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 388 | MachineOperand *LastMO = 0; |
| 389 | MachineInstr *LastMI = 0; |
| 390 | if (MBB != BarrierMBB && Uses.size() == 1) { |
| 391 | // Single use, no need to traverse the block. We can't assume this for the |
| 392 | // barrier bb though since the use is probably below the barrier. |
| 393 | LastMO = Uses[0]; |
| 394 | LastMI = LastMO->getParent(); |
| 395 | } else { |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 396 | MachineBasicBlock::iterator MEE = MBB->begin(); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 397 | MachineBasicBlock::iterator MII; |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 398 | if (MBB == BarrierMBB) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 399 | MII = Barrier; |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 400 | else |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 401 | MII = MBB->end(); |
Evan Cheng | 2efe3fd | 2008-10-24 05:53:44 +0000 | [diff] [blame] | 402 | while (--MII != MEE) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 403 | MachineInstr *UseMI = &*MII; |
| 404 | if (!UseMIs.count(UseMI)) |
| 405 | continue; |
| 406 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 407 | MachineOperand &MO = UseMI->getOperand(i); |
| 408 | if (MO.isReg() && MO.getReg() == CurrLI->reg) { |
| 409 | LastMO = &MO; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | LastMI = UseMI; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // Cut off live range from last use (or beginning of the mbb if there |
| 419 | // are no uses in it) to the end of the mbb. |
| 420 | unsigned RangeStart, RangeEnd = LIs->getMBBEndIdx(MBB)+1; |
| 421 | if (LastMI) { |
| 422 | RangeStart = LIs->getUseIndex(LIs->getInstructionIndex(LastMI))+1; |
| 423 | assert(!LastMO->isKill() && "Last use already terminates the interval?"); |
| 424 | LastMO->setIsKill(); |
| 425 | } else { |
| 426 | assert(MBB == BarrierMBB); |
| 427 | RangeStart = LIs->getMBBStartIdx(MBB); |
| 428 | } |
| 429 | if (MBB == BarrierMBB) |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 430 | RangeEnd = LIs->getUseIndex(BarrierIdx)+1; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 431 | CurrLI->removeRange(RangeStart, RangeEnd); |
| 432 | |
| 433 | // Return true if the last use becomes a new kill. |
| 434 | return LastMI; |
| 435 | } |
| 436 | |
| 437 | /// ShrinkWrapLiveInterval - Recursively traverse the predecessor |
| 438 | /// chain to find the new 'kills' and shrink wrap the live interval to the |
| 439 | /// new kill indices. |
| 440 | void |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 441 | PreAllocSplitting::ShrinkWrapLiveInterval(VNInfo *ValNo, MachineBasicBlock *MBB, |
| 442 | MachineBasicBlock *SuccMBB, MachineBasicBlock *DefMBB, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 443 | SmallPtrSet<MachineBasicBlock*, 8> &Visited, |
| 444 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > &Uses, |
| 445 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > &UseMIs, |
| 446 | SmallVector<MachineBasicBlock*, 4> &UseMBBs) { |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 447 | if (Visited.count(MBB)) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 448 | return; |
| 449 | |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 450 | // If live interval is live in another successor path, then we can't process |
| 451 | // this block. But we may able to do so after all the successors have been |
| 452 | // processed. |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 453 | if (MBB != BarrierMBB) { |
| 454 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 455 | SE = MBB->succ_end(); SI != SE; ++SI) { |
| 456 | MachineBasicBlock *SMBB = *SI; |
| 457 | if (SMBB == SuccMBB) |
| 458 | continue; |
| 459 | if (CurrLI->liveAt(LIs->getMBBStartIdx(SMBB))) |
| 460 | return; |
| 461 | } |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | Visited.insert(MBB); |
| 465 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 466 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator |
| 467 | UMII = Uses.find(MBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 468 | if (UMII != Uses.end()) { |
| 469 | // At least one use in this mbb, lets look for the kill. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 470 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 471 | UMII2 = UseMIs.find(MBB); |
| 472 | if (ShrinkWrapToLastUse(MBB, UMII->second, UMII2->second)) |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 473 | // Found a kill, shrink wrapping of this path ends here. |
| 474 | return; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 475 | } else if (MBB == DefMBB) { |
| 476 | assert(LIValNoSSMap.find(std::make_pair(CurrLI->reg, ValNo->id)) != |
| 477 | LIValNoSSMap.end() && "Why wasn't def spilled?"); |
| 478 | // There are no uses after the def. |
| 479 | MachineInstr *DefMI = LIs->getInstructionFromIndex(ValNo->def); |
| 480 | assert(RestoreMIs.count(DefMI) && "Not defined by a join?"); |
| 481 | if (UseMBBs.empty()) { |
| 482 | // The only use must be below barrier in the barrier block. It's safe to |
| 483 | // remove the def. |
| 484 | LIs->RemoveMachineInstrFromMaps(DefMI); |
| 485 | DefMI->eraseFromParent(); |
| 486 | CurrLI->removeRange(ValNo->def, LIs->getMBBEndIdx(MBB)+1); |
| 487 | } |
Evan Cheng | 79d5b5a | 2008-10-25 23:49:39 +0000 | [diff] [blame] | 488 | } else if (MBB == BarrierMBB) { |
| 489 | // Remove entire live range from start of mbb to barrier. |
| 490 | CurrLI->removeRange(LIs->getMBBStartIdx(MBB), |
| 491 | LIs->getUseIndex(BarrierIdx)+1); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 492 | } else { |
Evan Cheng | 79d5b5a | 2008-10-25 23:49:39 +0000 | [diff] [blame] | 493 | // Remove entire live range of the mbb out of the live interval. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 494 | CurrLI->removeRange(LIs->getMBBStartIdx(MBB), LIs->getMBBEndIdx(MBB)+1); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (MBB == DefMBB) |
| 498 | // Reached the def mbb, stop traversing this path further. |
| 499 | return; |
| 500 | |
| 501 | // Traverse the pathes up the predecessor chains further. |
| 502 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 503 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 504 | MachineBasicBlock *Pred = *PI; |
| 505 | if (Pred == MBB) |
| 506 | continue; |
| 507 | if (Pred == DefMBB && ValNo->hasPHIKill) |
| 508 | // Pred is the def bb and the def reaches other val#s, we must |
| 509 | // allow the value to be live out of the bb. |
| 510 | continue; |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 511 | ShrinkWrapLiveInterval(ValNo, Pred, MBB, DefMBB, Visited, |
| 512 | Uses, UseMIs, UseMBBs); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | /// SplitRegLiveInterval - Split (spill and restore) the given live interval |
| 519 | /// so it would not cross the barrier that's being processed. Shrink wrap |
| 520 | /// (minimize) the live interval to the last uses. |
| 521 | bool PreAllocSplitting::SplitRegLiveInterval(LiveInterval *LI) { |
| 522 | CurrLI = LI; |
| 523 | |
| 524 | // Find live range where current interval cross the barrier. |
| 525 | LiveInterval::iterator LR = |
| 526 | CurrLI->FindLiveRangeContaining(LIs->getUseIndex(BarrierIdx)); |
| 527 | VNInfo *ValNo = LR->valno; |
| 528 | |
| 529 | if (ValNo->def == ~1U) { |
| 530 | // Defined by a dead def? How can this be? |
| 531 | assert(0 && "Val# is defined by a dead def?"); |
| 532 | abort(); |
| 533 | } |
| 534 | |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 535 | // FIXME: For now, if definition is rematerializable, do not split. |
| 536 | MachineInstr *DefMI = (ValNo->def != ~0U) |
| 537 | ? LIs->getInstructionFromIndex(ValNo->def) : NULL; |
| 538 | if (DefMI && LIs->isReMaterializable(*LI, ValNo, DefMI)) |
| 539 | return false; |
| 540 | |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 541 | // Find all references in the barrier mbb. |
| 542 | SmallPtrSet<MachineInstr*, 4> RefsInMBB; |
| 543 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(CurrLI->reg), |
| 544 | E = MRI->reg_end(); I != E; ++I) { |
| 545 | MachineInstr *RefMI = &*I; |
| 546 | if (RefMI->getParent() == BarrierMBB) |
| 547 | RefsInMBB.insert(RefMI); |
| 548 | } |
| 549 | |
| 550 | // Find a point to restore the value after the barrier. |
| 551 | unsigned RestoreIndex; |
| 552 | MachineBasicBlock::iterator RestorePt = |
Evan Cheng | f62ce37 | 2008-10-28 00:47:49 +0000 | [diff] [blame] | 553 | findRestorePoint(BarrierMBB, Barrier, LR->end, RefsInMBB, RestoreIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 554 | if (RestorePt == BarrierMBB->end()) |
| 555 | return false; |
| 556 | |
| 557 | // Add a spill either before the barrier or after the definition. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 558 | MachineBasicBlock *DefMBB = DefMI ? DefMI->getParent() : NULL; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 559 | const TargetRegisterClass *RC = MRI->getRegClass(CurrLI->reg); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 560 | unsigned SpillIndex = 0; |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 561 | MachineInstr *SpillMI = NULL; |
Evan Cheng | 985921e | 2008-10-27 23:29:28 +0000 | [diff] [blame] | 562 | int SS = -1; |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 563 | bool PrevSpilled = isAlreadySplit(CurrLI->reg, ValNo->id, SS); |
| 564 | if (ValNo->def == ~0U) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 565 | // If it's defined by a phi, we must split just before the barrier. |
| 566 | MachineBasicBlock::iterator SpillPt = |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 567 | findSpillPoint(BarrierMBB, Barrier, NULL, RefsInMBB, SpillIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 568 | if (SpillPt == BarrierMBB->begin()) |
| 569 | return false; // No gap to insert spill. |
| 570 | // Add spill. |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 571 | if (!PrevSpilled) |
| 572 | // If previously split, reuse the spill slot. |
| 573 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 574 | TII->storeRegToStackSlot(*BarrierMBB, SpillPt, CurrLI->reg, true, SS, RC); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 575 | SpillMI = prior(SpillPt); |
| 576 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 577 | } else if (!PrevSpilled) { |
Evan Cheng | b3990d5 | 2008-10-27 23:21:01 +0000 | [diff] [blame] | 578 | if (!DefMI) |
| 579 | // Def is dead. Do nothing. |
| 580 | return false; |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 581 | // If it's already split, just restore the value. There is no need to spill |
| 582 | // the def again. |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 583 | // Check if it's possible to insert a spill after the def MI. |
Evan Cheng | 1f08cc2 | 2008-10-28 05:28:21 +0000 | [diff] [blame^] | 584 | MachineBasicBlock::iterator SpillPt; |
| 585 | if (DefMBB == BarrierMBB) { |
| 586 | // Add spill after the def and the last use before the barrier. |
| 587 | SpillPt = findSpillPoint(BarrierMBB, Barrier, DefMI, RefsInMBB, SpillIndex); |
| 588 | if (SpillPt == DefMBB->begin()) |
| 589 | return false; // No gap to insert spill. |
| 590 | } else { |
| 591 | SpillPt = findNextEmptySlot(DefMBB, DefMI, SpillIndex); |
| 592 | if (SpillPt == DefMBB->end()) |
| 593 | return false; // No gap to insert spill. |
| 594 | } |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 595 | SS = MFI->CreateStackObject(RC->getSize(), RC->getAlignment()); |
| 596 | |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 597 | // Add spill. The store instruction kills the register if def is before |
| 598 | // the barrier in the barrier block. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 599 | TII->storeRegToStackSlot(*DefMBB, SpillPt, CurrLI->reg, |
| 600 | DefMBB == BarrierMBB, SS, RC); |
| 601 | SpillMI = prior(SpillPt); |
| 602 | LIs->InsertMachineInstrInMaps(SpillMI, SpillIndex); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Add restore. |
| 606 | // FIXME: Create live interval for stack slot. |
| 607 | TII->loadRegFromStackSlot(*BarrierMBB, RestorePt, CurrLI->reg, SS, RC); |
| 608 | MachineInstr *LoadMI = prior(RestorePt); |
| 609 | LIs->InsertMachineInstrInMaps(LoadMI, RestoreIndex); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 610 | RestoreMIs.insert(LoadMI); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 611 | |
| 612 | // If live interval is spilled in the same block as the barrier, just |
| 613 | // create a hole in the interval. |
| 614 | if (!DefMBB || |
Evan Cheng | 78dfef7 | 2008-10-25 00:52:41 +0000 | [diff] [blame] | 615 | (SpillMI && SpillMI->getParent() == BarrierMBB)) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 616 | UpdateIntervalForSplit(ValNo, LIs->getUseIndex(SpillIndex)+1, |
| 617 | LIs->getDefIndex(RestoreIndex)); |
| 618 | |
| 619 | // Record val# values are in the specific spill slot. |
| 620 | RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS); |
| 621 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 622 | ++NumSplits; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 623 | return true; |
| 624 | } |
| 625 | |
| 626 | // Shrink wrap the live interval by walking up the CFG and find the |
| 627 | // new kills. |
| 628 | // Now let's find all the uses of the val#. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 629 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> > Uses; |
| 630 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> > UseMIs; |
| 631 | SmallPtrSet<MachineBasicBlock*, 4> Seen; |
| 632 | SmallVector<MachineBasicBlock*, 4> UseMBBs; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 633 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(CurrLI->reg), |
| 634 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 635 | MachineOperand &UseMO = UI.getOperand(); |
| 636 | MachineInstr *UseMI = UseMO.getParent(); |
| 637 | unsigned UseIdx = LIs->getInstructionIndex(UseMI); |
| 638 | LiveInterval::iterator ULR = CurrLI->FindLiveRangeContaining(UseIdx); |
| 639 | if (ULR->valno != ValNo) |
| 640 | continue; |
| 641 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 642 | // Remember which other mbb's use this val#. |
| 643 | if (Seen.insert(UseMBB) && UseMBB != BarrierMBB) |
| 644 | UseMBBs.push_back(UseMBB); |
| 645 | DenseMap<MachineBasicBlock*, SmallVector<MachineOperand*, 4> >::iterator |
| 646 | UMII = Uses.find(UseMBB); |
| 647 | if (UMII != Uses.end()) { |
| 648 | DenseMap<MachineBasicBlock*, SmallPtrSet<MachineInstr*, 4> >::iterator |
| 649 | UMII2 = UseMIs.find(UseMBB); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 650 | UMII->second.push_back(&UseMO); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 651 | UMII2->second.insert(UseMI); |
| 652 | } else { |
| 653 | SmallVector<MachineOperand*, 4> Ops; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 654 | Ops.push_back(&UseMO); |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 655 | Uses.insert(std::make_pair(UseMBB, Ops)); |
| 656 | SmallPtrSet<MachineInstr*, 4> MIs; |
| 657 | MIs.insert(UseMI); |
| 658 | UseMIs.insert(std::make_pair(UseMBB, MIs)); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | // Walk up the predecessor chains. |
| 663 | SmallPtrSet<MachineBasicBlock*, 8> Visited; |
Evan Cheng | aaf510c | 2008-10-26 07:49:03 +0000 | [diff] [blame] | 664 | ShrinkWrapLiveInterval(ValNo, BarrierMBB, NULL, DefMBB, Visited, |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 665 | Uses, UseMIs, UseMBBs); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 666 | |
| 667 | // Remove live range from barrier to the restore. FIXME: Find a better |
| 668 | // point to re-start the live interval. |
| 669 | UpdateIntervalForSplit(ValNo, LIs->getUseIndex(BarrierIdx)+1, |
| 670 | LIs->getDefIndex(RestoreIndex)); |
| 671 | // Record val# values are in the specific spill slot. |
Evan Cheng | 0658749 | 2008-10-24 02:05:00 +0000 | [diff] [blame] | 672 | RecordSplit(CurrLI->reg, SpillIndex, RestoreIndex, SS); |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 673 | |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 674 | ++NumSplits; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
| 678 | /// SplitRegLiveIntervals - Split all register live intervals that cross the |
| 679 | /// barrier that's being processed. |
| 680 | bool |
| 681 | PreAllocSplitting::SplitRegLiveIntervals(const TargetRegisterClass **RCs) { |
| 682 | // First find all the virtual registers whose live intervals are intercepted |
| 683 | // by the current barrier. |
| 684 | SmallVector<LiveInterval*, 8> Intervals; |
| 685 | for (const TargetRegisterClass **RC = RCs; *RC; ++RC) { |
Evan Cheng | 2306628 | 2008-10-27 07:14:50 +0000 | [diff] [blame] | 686 | if (TII->IgnoreRegisterClassBarriers(*RC)) |
| 687 | continue; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 688 | std::vector<unsigned> &VRs = MRI->getRegClassVirtRegs(*RC); |
| 689 | for (unsigned i = 0, e = VRs.size(); i != e; ++i) { |
| 690 | unsigned Reg = VRs[i]; |
| 691 | if (!LIs->hasInterval(Reg)) |
| 692 | continue; |
| 693 | LiveInterval *LI = &LIs->getInterval(Reg); |
| 694 | if (LI->liveAt(BarrierIdx) && !Barrier->readsRegister(Reg)) |
| 695 | // Virtual register live interval is intercepted by the barrier. We |
| 696 | // should split and shrink wrap its interval if possible. |
| 697 | Intervals.push_back(LI); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | // Process the affected live intervals. |
| 702 | bool Change = false; |
| 703 | while (!Intervals.empty()) { |
Evan Cheng | ae7fa5b | 2008-10-28 01:48:24 +0000 | [diff] [blame] | 704 | if (PreSplitLimit != -1 && (int)NumSplits == PreSplitLimit) |
| 705 | break; |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 706 | LiveInterval *LI = Intervals.back(); |
| 707 | Intervals.pop_back(); |
| 708 | Change |= SplitRegLiveInterval(LI); |
| 709 | } |
| 710 | |
| 711 | return Change; |
| 712 | } |
| 713 | |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 714 | bool PreAllocSplitting::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | f5cd4f0 | 2008-10-23 20:43:13 +0000 | [diff] [blame] | 715 | CurMF = &MF; |
| 716 | TM = &MF.getTarget(); |
| 717 | TII = TM->getInstrInfo(); |
| 718 | MFI = MF.getFrameInfo(); |
| 719 | MRI = &MF.getRegInfo(); |
| 720 | LIs = &getAnalysis<LiveIntervals>(); |
| 721 | |
| 722 | bool MadeChange = false; |
| 723 | |
| 724 | // Make sure blocks are numbered in order. |
| 725 | MF.RenumberBlocks(); |
| 726 | |
| 727 | for (MachineFunction::reverse_iterator I = MF.rbegin(), E = MF.rend(); |
| 728 | I != E; ++I) { |
| 729 | BarrierMBB = &*I; |
| 730 | for (MachineBasicBlock::reverse_iterator II = BarrierMBB->rbegin(), |
| 731 | EE = BarrierMBB->rend(); II != EE; ++II) { |
| 732 | Barrier = &*II; |
| 733 | const TargetRegisterClass **BarrierRCs = |
| 734 | Barrier->getDesc().getRegClassBarriers(); |
| 735 | if (!BarrierRCs) |
| 736 | continue; |
| 737 | BarrierIdx = LIs->getInstructionIndex(Barrier); |
| 738 | MadeChange |= SplitRegLiveIntervals(BarrierRCs); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | return MadeChange; |
Evan Cheng | 09e8ca8 | 2008-10-20 21:44:59 +0000 | [diff] [blame] | 743 | } |