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