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