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