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