Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 1 | //===-- MachineSink.cpp - Sinking for machine instructions ----------------===// |
| 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 | // |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 10 | // This pass moves instructions into successor blocks when possible, so that |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 11 | // they aren't executed on paths where their results aren't needed. |
| 12 | // |
| 13 | // This pass is not intended to be a replacement or a complete alternative |
| 14 | // for an LLVM-IR-level sinking pass. It is only designed to sink simple |
| 15 | // constructs that are not exposed before lowering and instruction selection. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #define DEBUG_TYPE "machine-sink" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 22 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Bill Wendling | 1e973aa | 2009-08-22 20:26:23 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 35 | static cl::opt<bool> |
| 36 | SplitEdges("machine-sink-split", |
| 37 | cl::desc("Split critical edges during machine sinking"), |
Evan Cheng | 44be1a8 | 2010-09-20 22:52:00 +0000 | [diff] [blame] | 38 | cl::init(true), cl::Hidden); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 39 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 40 | STATISTIC(NumSunk, "Number of machine instructions sunk"); |
| 41 | STATISTIC(NumSplit, "Number of critical edges split"); |
| 42 | STATISTIC(NumCoalesces, "Number of copies coalesced"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 43 | |
| 44 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 45 | class MachineSinking : public MachineFunctionPass { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 46 | const TargetInstrInfo *TII; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 47 | const TargetRegisterInfo *TRI; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 48 | MachineRegisterInfo *MRI; // Machine register information |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 49 | MachineDominatorTree *DT; // Machine dominator tree |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 50 | MachineLoopInfo *LI; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 51 | AliasAnalysis *AA; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 52 | BitVector AllocatableSet; // Which physregs are allocatable? |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 53 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 54 | // Remember which edges have been considered for breaking. |
| 55 | SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8> |
| 56 | CEBCandidates; |
| 57 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 58 | public: |
| 59 | static char ID; // Pass identification |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 60 | MachineSinking() : MachineFunctionPass(ID) { |
| 61 | initializeMachineSinkingPass(*PassRegistry::getPassRegistry()); |
| 62 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 63 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 64 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 65 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 67 | AU.setPreservesCFG(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 68 | MachineFunctionPass::getAnalysisUsage(AU); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 69 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 70 | AU.addRequired<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 71 | AU.addRequired<MachineLoopInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 72 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 73 | AU.addPreserved<MachineLoopInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 74 | } |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 75 | |
| 76 | virtual void releaseMemory() { |
| 77 | CEBCandidates.clear(); |
| 78 | } |
| 79 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 80 | private: |
| 81 | bool ProcessBlock(MachineBasicBlock &MBB); |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 82 | bool isWorthBreakingCriticalEdge(MachineInstr *MI, |
| 83 | MachineBasicBlock *From, |
| 84 | MachineBasicBlock *To); |
| 85 | MachineBasicBlock *SplitCriticalEdge(MachineInstr *MI, |
| 86 | MachineBasicBlock *From, |
| 87 | MachineBasicBlock *To, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 88 | bool BreakPHIEdge); |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 89 | bool SinkInstruction(MachineInstr *MI, bool &SawStore); |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 90 | bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 91 | MachineBasicBlock *DefMBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 92 | bool &BreakPHIEdge, bool &LocalUse) const; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 93 | MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB, |
| 94 | bool &BreakPHIEdge); |
| 95 | bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI, |
| 96 | MachineBasicBlock *MBB, |
| 97 | MachineBasicBlock *SuccToSinkTo); |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 98 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 99 | bool PerformTrivialForwardCoalescing(MachineInstr *MI, |
| 100 | MachineBasicBlock *MBB); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 101 | }; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 102 | } // end anonymous namespace |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 104 | char MachineSinking::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 105 | INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink", |
| 106 | "Machine code sinking", false, false) |
| 107 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 108 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 109 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 110 | INITIALIZE_PASS_END(MachineSinking, "machine-sink", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 111 | "Machine code sinking", false, false) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 112 | |
| 113 | FunctionPass *llvm::createMachineSinkingPass() { return new MachineSinking(); } |
| 114 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 115 | bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI, |
| 116 | MachineBasicBlock *MBB) { |
| 117 | if (!MI->isCopy()) |
| 118 | return false; |
| 119 | |
| 120 | unsigned SrcReg = MI->getOperand(1).getReg(); |
| 121 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 122 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || |
| 123 | !TargetRegisterInfo::isVirtualRegister(DstReg) || |
| 124 | !MRI->hasOneNonDBGUse(SrcReg)) |
| 125 | return false; |
| 126 | |
| 127 | const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg); |
| 128 | const TargetRegisterClass *DRC = MRI->getRegClass(DstReg); |
| 129 | if (SRC != DRC) |
| 130 | return false; |
| 131 | |
| 132 | MachineInstr *DefMI = MRI->getVRegDef(SrcReg); |
| 133 | if (DefMI->isCopyLike()) |
| 134 | return false; |
| 135 | DEBUG(dbgs() << "Coalescing: " << *DefMI); |
| 136 | DEBUG(dbgs() << "*** to: " << *MI); |
| 137 | MRI->replaceRegWith(DstReg, SrcReg); |
| 138 | MI->eraseFromParent(); |
| 139 | ++NumCoalesces; |
| 140 | return true; |
| 141 | } |
| 142 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 143 | /// AllUsesDominatedByBlock - Return true if all uses of the specified register |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 144 | /// occur in blocks dominated by the specified block. If any use is in the |
| 145 | /// definition block, then return false since it is never legal to move def |
| 146 | /// after uses. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 147 | bool |
| 148 | MachineSinking::AllUsesDominatedByBlock(unsigned Reg, |
| 149 | MachineBasicBlock *MBB, |
| 150 | MachineBasicBlock *DefMBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 151 | bool &BreakPHIEdge, |
| 152 | bool &LocalUse) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 153 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 154 | "Only makes sense for vregs"); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 155 | |
Devang Patel | f5b9a74 | 2011-12-09 01:25:04 +0000 | [diff] [blame] | 156 | // Ignore debug uses because debug info doesn't affect the code. |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 157 | if (MRI->use_nodbg_empty(Reg)) |
| 158 | return true; |
| 159 | |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 160 | // BreakPHIEdge is true if all the uses are in the successor MBB being sunken |
| 161 | // into and they are all PHI nodes. In this case, machine-sink must break |
| 162 | // the critical edge first. e.g. |
| 163 | // |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 164 | // BB#1: derived from LLVM BB %bb4.preheader |
| 165 | // Predecessors according to CFG: BB#0 |
| 166 | // ... |
| 167 | // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead> |
| 168 | // ... |
| 169 | // JE_4 <BB#37>, %EFLAGS<imp-use> |
| 170 | // Successors according to CFG: BB#37 BB#2 |
| 171 | // |
| 172 | // BB#2: derived from LLVM BB %bb.nph |
| 173 | // Predecessors according to CFG: BB#0 BB#1 |
| 174 | // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1> |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 175 | BreakPHIEdge = true; |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 176 | for (MachineRegisterInfo::use_nodbg_iterator |
| 177 | I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end(); |
| 178 | I != E; ++I) { |
| 179 | MachineInstr *UseInst = &*I; |
| 180 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
| 181 | if (!(UseBlock == MBB && UseInst->isPHI() && |
| 182 | UseInst->getOperand(I.getOperandNo()+1).getMBB() == DefMBB)) { |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 183 | BreakPHIEdge = false; |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 187 | if (BreakPHIEdge) |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 188 | return true; |
| 189 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 190 | for (MachineRegisterInfo::use_nodbg_iterator |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 191 | I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end(); |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 192 | I != E; ++I) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 193 | // Determine the block of the use. |
| 194 | MachineInstr *UseInst = &*I; |
| 195 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 196 | if (UseInst->isPHI()) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 197 | // PHI nodes use the operand in the predecessor block, not the block with |
| 198 | // the PHI. |
| 199 | UseBlock = UseInst->getOperand(I.getOperandNo()+1).getMBB(); |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 200 | } else if (UseBlock == DefMBB) { |
| 201 | LocalUse = true; |
| 202 | return false; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 203 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 204 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 205 | // Check that it dominates. |
| 206 | if (!DT->dominates(MBB, UseBlock)) |
| 207 | return false; |
| 208 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 209 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 213 | bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { |
David Greene | c19a9cd | 2010-01-05 01:26:00 +0000 | [diff] [blame] | 214 | DEBUG(dbgs() << "******** Machine Sinking ********\n"); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 215 | |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 216 | const TargetMachine &TM = MF.getTarget(); |
| 217 | TII = TM.getInstrInfo(); |
| 218 | TRI = TM.getRegisterInfo(); |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 219 | MRI = &MF.getRegInfo(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 220 | DT = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 221 | LI = &getAnalysis<MachineLoopInfo>(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 222 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | 4e9785e | 2009-10-19 14:52:05 +0000 | [diff] [blame] | 223 | AllocatableSet = TRI->getAllocatableSet(MF); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 224 | |
| 225 | bool EverMadeChange = false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 226 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 227 | while (1) { |
| 228 | bool MadeChange = false; |
| 229 | |
| 230 | // Process all basic blocks. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 231 | CEBCandidates.clear(); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 232 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 233 | I != E; ++I) |
| 234 | MadeChange |= ProcessBlock(*I); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 235 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 236 | // If this iteration over the code changed anything, keep iterating. |
| 237 | if (!MadeChange) break; |
| 238 | EverMadeChange = true; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 240 | return EverMadeChange; |
| 241 | } |
| 242 | |
| 243 | bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 244 | // Can't sink anything out of a block that has less than two successors. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 245 | if (MBB.succ_size() <= 1 || MBB.empty()) return false; |
| 246 | |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 247 | // Don't bother sinking code out of unreachable blocks. In addition to being |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 248 | // unprofitable, it can also lead to infinite looping, because in an |
| 249 | // unreachable loop there may be nowhere to stop. |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 250 | if (!DT->isReachableFromEntry(&MBB)) return false; |
| 251 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 252 | bool MadeChange = false; |
| 253 | |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 254 | // Walk the basic block bottom-up. Remember if we saw a store. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 255 | MachineBasicBlock::iterator I = MBB.end(); |
| 256 | --I; |
| 257 | bool ProcessedBegin, SawStore = false; |
| 258 | do { |
| 259 | MachineInstr *MI = I; // The instruction to sink. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 261 | // Predecrement I (if it's not begin) so that it isn't invalidated by |
| 262 | // sinking. |
| 263 | ProcessedBegin = I == MBB.begin(); |
| 264 | if (!ProcessedBegin) |
| 265 | --I; |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 266 | |
| 267 | if (MI->isDebugValue()) |
| 268 | continue; |
| 269 | |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 270 | bool Joined = PerformTrivialForwardCoalescing(MI, &MBB); |
| 271 | if (Joined) { |
| 272 | MadeChange = true; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 273 | continue; |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 274 | } |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 276 | if (SinkInstruction(MI, SawStore)) |
| 277 | ++NumSunk, MadeChange = true; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 279 | // If we just processed the first instruction in the block, we're done. |
| 280 | } while (!ProcessedBegin); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 281 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 282 | return MadeChange; |
| 283 | } |
| 284 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 285 | bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI, |
| 286 | MachineBasicBlock *From, |
| 287 | MachineBasicBlock *To) { |
| 288 | // FIXME: Need much better heuristics. |
| 289 | |
| 290 | // If the pass has already considered breaking this edge (during this pass |
| 291 | // through the function), then let's go ahead and break it. This means |
| 292 | // sinking multiple "cheap" instructions into the same block. |
| 293 | if (!CEBCandidates.insert(std::make_pair(From, To))) |
| 294 | return true; |
| 295 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 296 | if (!MI->isCopy() && !MI->isAsCheapAsAMove()) |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 297 | return true; |
| 298 | |
| 299 | // MI is cheap, we probably don't want to break the critical edge for it. |
| 300 | // However, if this would allow some definitions of its source operands |
| 301 | // to be sunk then it's probably worth it. |
| 302 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 303 | const MachineOperand &MO = MI->getOperand(i); |
| 304 | if (!MO.isReg()) continue; |
| 305 | unsigned Reg = MO.getReg(); |
| 306 | if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 307 | continue; |
| 308 | if (MRI->hasOneNonDBGUse(Reg)) |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | MachineBasicBlock *MachineSinking::SplitCriticalEdge(MachineInstr *MI, |
| 316 | MachineBasicBlock *FromBB, |
| 317 | MachineBasicBlock *ToBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 318 | bool BreakPHIEdge) { |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 319 | if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB)) |
| 320 | return 0; |
| 321 | |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 322 | // Avoid breaking back edge. From == To means backedge for single BB loop. |
Evan Cheng | 44be1a8 | 2010-09-20 22:52:00 +0000 | [diff] [blame] | 323 | if (!SplitEdges || FromBB == ToBB) |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 326 | // Check for backedges of more "complex" loops. |
| 327 | if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) && |
| 328 | LI->isLoopHeader(ToBB)) |
| 329 | return 0; |
| 330 | |
| 331 | // It's not always legal to break critical edges and sink the computation |
| 332 | // to the edge. |
| 333 | // |
| 334 | // BB#1: |
| 335 | // v1024 |
| 336 | // Beq BB#3 |
| 337 | // <fallthrough> |
| 338 | // BB#2: |
| 339 | // ... no uses of v1024 |
| 340 | // <fallthrough> |
| 341 | // BB#3: |
| 342 | // ... |
| 343 | // = v1024 |
| 344 | // |
| 345 | // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted: |
| 346 | // |
| 347 | // BB#1: |
| 348 | // ... |
| 349 | // Bne BB#2 |
| 350 | // BB#4: |
| 351 | // v1024 = |
| 352 | // B BB#3 |
| 353 | // BB#2: |
| 354 | // ... no uses of v1024 |
| 355 | // <fallthrough> |
| 356 | // BB#3: |
| 357 | // ... |
| 358 | // = v1024 |
| 359 | // |
| 360 | // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3 |
| 361 | // flow. We need to ensure the new basic block where the computation is |
| 362 | // sunk to dominates all the uses. |
| 363 | // It's only legal to break critical edge and sink the computation to the |
| 364 | // new block if all the predecessors of "To", except for "From", are |
| 365 | // not dominated by "From". Given SSA property, this means these |
| 366 | // predecessors are dominated by "To". |
| 367 | // |
| 368 | // There is no need to do this check if all the uses are PHI nodes. PHI |
| 369 | // sources are only defined on the specific predecessor edges. |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 370 | if (!BreakPHIEdge) { |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 371 | for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(), |
| 372 | E = ToBB->pred_end(); PI != E; ++PI) { |
| 373 | if (*PI == FromBB) |
| 374 | continue; |
| 375 | if (!DT->dominates(ToBB, *PI)) |
| 376 | return 0; |
| 377 | } |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 380 | return FromBB->SplitCriticalEdge(ToBB, this); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Evan Cheng | b0cdf8a | 2010-09-23 06:53:00 +0000 | [diff] [blame] | 383 | static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) { |
| 384 | return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence(); |
| 385 | } |
| 386 | |
Devang Patel | 541a81c | 2011-09-07 00:07:58 +0000 | [diff] [blame] | 387 | /// collectDebgValues - Scan instructions following MI and collect any |
| 388 | /// matching DBG_VALUEs. |
| 389 | static void collectDebugValues(MachineInstr *MI, |
| 390 | SmallVector<MachineInstr *, 2> & DbgValues) { |
| 391 | DbgValues.clear(); |
| 392 | if (!MI->getOperand(0).isReg()) |
| 393 | return; |
| 394 | |
| 395 | MachineBasicBlock::iterator DI = MI; ++DI; |
| 396 | for (MachineBasicBlock::iterator DE = MI->getParent()->end(); |
| 397 | DI != DE; ++DI) { |
| 398 | if (!DI->isDebugValue()) |
| 399 | return; |
| 400 | if (DI->getOperand(0).isReg() && |
| 401 | DI->getOperand(0).getReg() == MI->getOperand(0).getReg()) |
| 402 | DbgValues.push_back(DI); |
| 403 | } |
| 404 | } |
| 405 | |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 406 | /// isPostDominatedBy - Return true if A is post dominated by B. |
| 407 | static bool isPostDominatedBy(MachineBasicBlock *A, MachineBasicBlock *B) { |
| 408 | |
| 409 | // FIXME - Use real post dominator. |
| 410 | if (A->succ_size() != 2) |
| 411 | return false; |
| 412 | MachineBasicBlock::succ_iterator I = A->succ_begin(); |
| 413 | if (B == *I) |
| 414 | ++I; |
| 415 | MachineBasicBlock *OtherSuccBlock = *I; |
| 416 | if (OtherSuccBlock->succ_size() != 1 || |
| 417 | *(OtherSuccBlock->succ_begin()) != B) |
| 418 | return false; |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | /// isProfitableToSinkTo - Return true if it is profitable to sink MI. |
| 424 | bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI, |
| 425 | MachineBasicBlock *MBB, |
| 426 | MachineBasicBlock *SuccToSinkTo) { |
| 427 | assert (MI && "Invalid MachineInstr!"); |
| 428 | assert (SuccToSinkTo && "Invalid SinkTo Candidate BB"); |
| 429 | |
| 430 | if (MBB == SuccToSinkTo) |
| 431 | return false; |
| 432 | |
| 433 | // It is profitable if SuccToSinkTo does not post dominate current block. |
| 434 | if (!isPostDominatedBy(MBB, SuccToSinkTo)) |
| 435 | return true; |
| 436 | |
| 437 | // Check if only use in post dominated block is PHI instruction. |
| 438 | bool NonPHIUse = false; |
| 439 | for (MachineRegisterInfo::use_nodbg_iterator |
| 440 | I = MRI->use_nodbg_begin(Reg), E = MRI->use_nodbg_end(); |
| 441 | I != E; ++I) { |
| 442 | MachineInstr *UseInst = &*I; |
| 443 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
| 444 | if (UseBlock == SuccToSinkTo && !UseInst->isPHI()) |
| 445 | NonPHIUse = true; |
| 446 | } |
| 447 | if (!NonPHIUse) |
| 448 | return true; |
| 449 | |
| 450 | // If SuccToSinkTo post dominates then also it may be profitable if MI |
| 451 | // can further profitably sinked into another block in next round. |
| 452 | bool BreakPHIEdge = false; |
| 453 | // FIXME - If finding successor is compile time expensive then catch results. |
| 454 | if (MachineBasicBlock *MBB2 = FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge)) |
| 455 | return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2); |
| 456 | |
| 457 | // If SuccToSinkTo is final destination and it is a post dominator of current |
| 458 | // block then it is not profitable to sink MI into SuccToSinkTo block. |
| 459 | return false; |
| 460 | } |
| 461 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 462 | /// FindSuccToSinkTo - Find a successor to sink this instruction to. |
| 463 | MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI, |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 464 | MachineBasicBlock *MBB, |
| 465 | bool &BreakPHIEdge) { |
| 466 | |
| 467 | assert (MI && "Invalid MachineInstr!"); |
| 468 | assert (MBB && "Invalid MachineBasicBlock!"); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 469 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 470 | // Loop over all the operands of the specified instruction. If there is |
| 471 | // anything we can't handle, bail out. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 472 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 473 | // SuccToSinkTo - This is the successor to sink this instruction to, once we |
| 474 | // decide. |
| 475 | MachineBasicBlock *SuccToSinkTo = 0; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 476 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 477 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 478 | if (!MO.isReg()) continue; // Ignore non-register operands. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 479 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 480 | unsigned Reg = MO.getReg(); |
| 481 | if (Reg == 0) continue; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 482 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 483 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 484 | if (MO.isUse()) { |
| 485 | // If the physreg has no defs anywhere, it's just an ambient register |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 486 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 487 | // it could get allocated to something with a def during allocation. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 488 | if (!MRI->def_empty(Reg)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 489 | return NULL; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 490 | |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 491 | if (AllocatableSet.test(Reg)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 492 | return NULL; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 493 | |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 494 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 495 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 496 | unsigned AliasReg = *Alias; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 497 | if (!MRI->def_empty(AliasReg)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 498 | return NULL; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 499 | |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 500 | if (AllocatableSet.test(AliasReg)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 501 | return NULL; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 502 | } |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 503 | } else if (!MO.isDead()) { |
| 504 | // A def that isn't dead. We can't move it. |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 505 | return NULL; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 507 | } else { |
| 508 | // Virtual register uses are always safe to sink. |
| 509 | if (MO.isUse()) continue; |
Evan Cheng | b6f5417 | 2009-02-07 01:21:47 +0000 | [diff] [blame] | 510 | |
| 511 | // If it's not safe to move defs of the register class, then abort. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 512 | if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg))) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 513 | return NULL; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 514 | |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 515 | // FIXME: This picks a successor to sink into based on having one |
| 516 | // successor that dominates all the uses. However, there are cases where |
| 517 | // sinking can happen but where the sink point isn't a successor. For |
| 518 | // example: |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 519 | // |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 520 | // x = computation |
| 521 | // if () {} else {} |
| 522 | // use x |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 523 | // |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 524 | // the instruction could be sunk over the whole diamond for the |
Chris Lattner | e430e1c | 2008-01-05 06:47:58 +0000 | [diff] [blame] | 525 | // if/then/else (or loop, etc), allowing it to be sunk into other blocks |
| 526 | // after that. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 527 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 528 | // Virtual register defs can only be sunk if all their uses are in blocks |
| 529 | // dominated by one of the successors. |
| 530 | if (SuccToSinkTo) { |
| 531 | // If a previous operand picked a block to sink to, then this operand |
| 532 | // must be sinkable to the same block. |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 533 | bool LocalUse = false; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 534 | if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 535 | BreakPHIEdge, LocalUse)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 536 | return NULL; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 537 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 538 | continue; |
| 539 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 540 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 541 | // Otherwise, we should look at all the successors and decide which one |
| 542 | // we should sink to. |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 543 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 544 | E = MBB->succ_end(); SI != E; ++SI) { |
| 545 | MachineBasicBlock *SuccBlock = *SI; |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 546 | bool LocalUse = false; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 547 | if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 548 | BreakPHIEdge, LocalUse)) { |
Devang Patel | cf405ba | 2011-12-08 21:33:23 +0000 | [diff] [blame] | 549 | SuccToSinkTo = SuccBlock; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 552 | if (LocalUse) |
| 553 | // Def is used locally, it's never safe to move this def. |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 554 | return NULL; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 555 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 556 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 557 | // If we couldn't find a block to sink to, ignore this instruction. |
| 558 | if (SuccToSinkTo == 0) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 559 | return NULL; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 560 | else if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo)) |
| 561 | return NULL; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
Devang Patel | 7f7f090 | 2011-12-08 23:52:00 +0000 | [diff] [blame] | 564 | |
| 565 | // It is not possible to sink an instruction into its own block. This can |
| 566 | // happen with loops. |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 567 | if (MBB == SuccToSinkTo) |
Devang Patel | 7f7f090 | 2011-12-08 23:52:00 +0000 | [diff] [blame] | 568 | return NULL; |
| 569 | |
| 570 | // It's not safe to sink instructions to EH landing pad. Control flow into |
| 571 | // landing pad is implicitly defined. |
| 572 | if (SuccToSinkTo && SuccToSinkTo->isLandingPad()) |
| 573 | return NULL; |
| 574 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 575 | return SuccToSinkTo; |
| 576 | } |
| 577 | |
| 578 | /// SinkInstruction - Determine whether it is safe to sink the specified machine |
| 579 | /// instruction out of its current block into a successor. |
| 580 | bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) { |
| 581 | // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to |
| 582 | // be close to the source to make it easier to coalesce. |
| 583 | if (AvoidsSinking(MI, MRI)) |
| 584 | return false; |
| 585 | |
| 586 | // Check if it's safe to move the instruction. |
| 587 | if (!MI->isSafeToMove(TII, AA, SawStore)) |
| 588 | return false; |
| 589 | |
| 590 | // FIXME: This should include support for sinking instructions within the |
| 591 | // block they are currently in to shorten the live ranges. We often get |
| 592 | // instructions sunk into the top of a large block, but it would be better to |
| 593 | // also sink them down before their first use in the block. This xform has to |
| 594 | // be careful not to *increase* register pressure though, e.g. sinking |
| 595 | // "x = y + z" down if it kills y and z would increase the live ranges of y |
| 596 | // and z and only shrink the live range of x. |
| 597 | |
| 598 | bool BreakPHIEdge = false; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame^] | 599 | MachineBasicBlock *ParentBlock = MI->getParent(); |
| 600 | MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 602 | // If there are no outputs, it must have side-effects. |
| 603 | if (SuccToSinkTo == 0) |
| 604 | return false; |
Evan Cheng | b599979 | 2009-02-15 08:36:12 +0000 | [diff] [blame] | 605 | |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 606 | |
Daniel Dunbar | d24c9d5 | 2010-06-23 00:48:25 +0000 | [diff] [blame] | 607 | // If the instruction to move defines a dead physical register which is live |
| 608 | // when leaving the basic block, don't move it because it could turn into a |
| 609 | // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>) |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 610 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { |
| 611 | const MachineOperand &MO = MI->getOperand(I); |
| 612 | if (!MO.isReg()) continue; |
| 613 | unsigned Reg = MO.getReg(); |
| 614 | if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
| 615 | if (SuccToSinkTo->isLiveIn(Reg)) |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 616 | return false; |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 617 | } |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 618 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 619 | DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo); |
| 620 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 621 | // If the block has multiple predecessors, this would introduce computation on |
| 622 | // a path that it doesn't already exist. We could split the critical edge, |
| 623 | // but for now we just punt. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 624 | if (SuccToSinkTo->pred_size() > 1) { |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 625 | // We cannot sink a load across a critical edge - there may be stores in |
| 626 | // other code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 627 | bool TryBreak = false; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 628 | bool store = true; |
| 629 | if (!MI->isSafeToMove(TII, AA, store)) { |
Evan Cheng | f942c13 | 2010-08-19 23:33:02 +0000 | [diff] [blame] | 630 | DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 631 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // We don't want to sink across a critical edge if we don't dominate the |
| 635 | // successor. We could be introducing calculations to new code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 636 | if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) { |
Evan Cheng | f942c13 | 2010-08-19 23:33:02 +0000 | [diff] [blame] | 637 | DEBUG(dbgs() << " *** NOTE: Critical edge found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 638 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 641 | // Don't sink instructions into a loop. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 642 | if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) { |
Evan Cheng | f942c13 | 2010-08-19 23:33:02 +0000 | [diff] [blame] | 643 | DEBUG(dbgs() << " *** NOTE: Loop header found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 644 | TryBreak = true; |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 647 | // Otherwise we are OK with sinking along a critical edge. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 648 | if (!TryBreak) |
| 649 | DEBUG(dbgs() << "Sinking along critical edge.\n"); |
| 650 | else { |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 651 | MachineBasicBlock *NewSucc = |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 652 | SplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 653 | if (!NewSucc) { |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 654 | DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " |
| 655 | "break critical edge\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 656 | return false; |
| 657 | } else { |
Evan Cheng | f942c13 | 2010-08-19 23:33:02 +0000 | [diff] [blame] | 658 | DEBUG(dbgs() << " *** Splitting critical edge:" |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 659 | " BB#" << ParentBlock->getNumber() |
| 660 | << " -- BB#" << NewSucc->getNumber() |
| 661 | << " -- BB#" << SuccToSinkTo->getNumber() << '\n'); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 662 | SuccToSinkTo = NewSucc; |
| 663 | ++NumSplit; |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 664 | BreakPHIEdge = false; |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 667 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 668 | |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 669 | if (BreakPHIEdge) { |
| 670 | // BreakPHIEdge is true if all the uses are in the successor MBB being |
| 671 | // sunken into and they are all PHI nodes. In this case, machine-sink must |
| 672 | // break the critical edge first. |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 673 | MachineBasicBlock *NewSucc = SplitCriticalEdge(MI, ParentBlock, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 674 | SuccToSinkTo, BreakPHIEdge); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 675 | if (!NewSucc) { |
| 676 | DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " |
| 677 | "break critical edge\n"); |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | DEBUG(dbgs() << " *** Splitting critical edge:" |
| 682 | " BB#" << ParentBlock->getNumber() |
| 683 | << " -- BB#" << NewSucc->getNumber() |
| 684 | << " -- BB#" << SuccToSinkTo->getNumber() << '\n'); |
| 685 | SuccToSinkTo = NewSucc; |
| 686 | ++NumSplit; |
| 687 | } |
| 688 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 689 | // Determine where to insert into. Skip phi nodes. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 690 | MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 691 | while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 692 | ++InsertPos; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 693 | |
Devang Patel | 541a81c | 2011-09-07 00:07:58 +0000 | [diff] [blame] | 694 | // collect matching debug values. |
| 695 | SmallVector<MachineInstr *, 2> DbgValuesToSink; |
| 696 | collectDebugValues(MI, DbgValuesToSink); |
| 697 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 698 | // Move the instruction. |
| 699 | SuccToSinkTo->splice(InsertPos, ParentBlock, MI, |
| 700 | ++MachineBasicBlock::iterator(MI)); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 701 | |
Devang Patel | 541a81c | 2011-09-07 00:07:58 +0000 | [diff] [blame] | 702 | // Move debug values. |
| 703 | for (SmallVector<MachineInstr *, 2>::iterator DBI = DbgValuesToSink.begin(), |
| 704 | DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) { |
| 705 | MachineInstr *DbgMI = *DBI; |
| 706 | SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI, |
| 707 | ++MachineBasicBlock::iterator(DbgMI)); |
| 708 | } |
| 709 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 710 | // Conservatively, clear any kill flags, since it's possible that they are no |
| 711 | // longer correct. |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 712 | MI->clearKillInfo(); |
| 713 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 714 | return true; |
| 715 | } |