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