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