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