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