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