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