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