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