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