Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 1 | //===- MachineSink.cpp - Sinking for machine instructions -----------------===// |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 10 | // This pass moves instructions into successor blocks when possible, so that |
Dan Gohman | a5225ad | 2009-08-05 01:19:01 +0000 | [diff] [blame] | 11 | // they aren't executed on paths where their results aren't needed. |
| 12 | // |
| 13 | // This pass is not intended to be a replacement or a complete alternative |
| 14 | // for an LLVM-IR-level sinking pass. It is only designed to sink simple |
| 15 | // constructs that are not exposed before lowering and instruction selection. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SetVector.h" |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Matthias Braun | 3ac70da | 2015-05-16 03:11:07 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SparseBitVector.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunction.h" |
| 30 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 31 | #include "llvm/CodeGen/MachineInstr.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineOperand.h" |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachinePostDominators.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 38 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 39 | #include "llvm/IR/BasicBlock.h" |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 40 | #include "llvm/IR/LLVMContext.h" |
Paul Robinson | 39737bf | 2017-12-09 00:17:01 +0000 | [diff] [blame] | 41 | #include "llvm/IR/DebugInfoMetadata.h" |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 42 | #include "llvm/Pass.h" |
| 43 | #include "llvm/Support/BranchProbability.h" |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Bill Wendling | 1e973aa | 2009-08-22 20:26:23 +0000 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 47 | #include <algorithm> |
| 48 | #include <cassert> |
| 49 | #include <cstdint> |
| 50 | #include <map> |
| 51 | #include <utility> |
| 52 | #include <vector> |
| 53 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 54 | using namespace llvm; |
| 55 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "machine-sink" |
| 57 | |
Andrew Trick | 1df91b0 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 59 | SplitEdges("machine-sink-split", |
| 60 | cl::desc("Split critical edges during machine sinking"), |
Evan Cheng | 44be1a8 | 2010-09-20 22:52:00 +0000 | [diff] [blame] | 61 | cl::init(true), cl::Hidden); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 62 | |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 63 | static cl::opt<bool> |
| 64 | UseBlockFreqInfo("machine-sink-bfi", |
| 65 | cl::desc("Use block frequency info to find successors to sink"), |
| 66 | cl::init(true), cl::Hidden); |
| 67 | |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 68 | static cl::opt<unsigned> SplitEdgeProbabilityThreshold( |
| 69 | "machine-sink-split-probability-threshold", |
| 70 | cl::desc( |
| 71 | "Percentage threshold for splitting single-instruction critical edge. " |
| 72 | "If the branch threshold is higher than this threshold, we allow " |
| 73 | "speculative execution of up to 1 instruction to avoid branching to " |
| 74 | "splitted critical edge"), |
| 75 | cl::init(40), cl::Hidden); |
| 76 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 77 | STATISTIC(NumSunk, "Number of machine instructions sunk"); |
| 78 | STATISTIC(NumSplit, "Number of critical edges split"); |
| 79 | STATISTIC(NumCoalesces, "Number of copies coalesced"); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 80 | STATISTIC(NumPostRACopySink, "Number of copies sunk after RA"); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 81 | |
| 82 | namespace { |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 83 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 84 | class MachineSinking : public MachineFunctionPass { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 85 | const TargetInstrInfo *TII; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 86 | const TargetRegisterInfo *TRI; |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 87 | MachineRegisterInfo *MRI; // Machine register information |
| 88 | MachineDominatorTree *DT; // Machine dominator tree |
| 89 | MachinePostDominatorTree *PDT; // Machine post dominator tree |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 90 | MachineLoopInfo *LI; |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 91 | const MachineBlockFrequencyInfo *MBFI; |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 92 | const MachineBranchProbabilityInfo *MBPI; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 93 | AliasAnalysis *AA; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 94 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 95 | // Remember which edges have been considered for breaking. |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 96 | SmallSet<std::pair<MachineBasicBlock*, MachineBasicBlock*>, 8> |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 97 | CEBCandidates; |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 98 | // Remember which edges we are about to split. |
| 99 | // This is different from CEBCandidates since those edges |
| 100 | // will be split. |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 101 | SetVector<std::pair<MachineBasicBlock *, MachineBasicBlock *>> ToSplit; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 102 | |
Matthias Braun | 3ac70da | 2015-05-16 03:11:07 +0000 | [diff] [blame] | 103 | SparseBitVector<> RegsToClearKillFlags; |
| 104 | |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 105 | using AllSuccsCache = |
| 106 | std::map<MachineBasicBlock *, SmallVector<MachineBasicBlock *, 4>>; |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 107 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 108 | public: |
| 109 | static char ID; // Pass identification |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 110 | |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 111 | MachineSinking() : MachineFunctionPass(ID) { |
| 112 | initializeMachineSinkingPass(*PassRegistry::getPassRegistry()); |
| 113 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 114 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 115 | bool runOnMachineFunction(MachineFunction &MF) override; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 116 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 117 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 118 | AU.setPreservesCFG(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 119 | MachineFunctionPass::getAnalysisUsage(AU); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 120 | AU.addRequired<AAResultsWrapperPass>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 121 | AU.addRequired<MachineDominatorTree>(); |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 122 | AU.addRequired<MachinePostDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 123 | AU.addRequired<MachineLoopInfo>(); |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 124 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 125 | AU.addPreserved<MachineDominatorTree>(); |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 126 | AU.addPreserved<MachinePostDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 127 | AU.addPreserved<MachineLoopInfo>(); |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 128 | if (UseBlockFreqInfo) |
| 129 | AU.addRequired<MachineBlockFrequencyInfo>(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 130 | } |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 131 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 132 | void releaseMemory() override { |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 133 | CEBCandidates.clear(); |
| 134 | } |
| 135 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 136 | private: |
| 137 | bool ProcessBlock(MachineBasicBlock &MBB); |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 138 | bool isWorthBreakingCriticalEdge(MachineInstr &MI, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 139 | MachineBasicBlock *From, |
| 140 | MachineBasicBlock *To); |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 141 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 142 | /// Postpone the splitting of the given critical |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 143 | /// edge (\p From, \p To). |
| 144 | /// |
| 145 | /// We do not split the edges on the fly. Indeed, this invalidates |
| 146 | /// the dominance information and thus triggers a lot of updates |
| 147 | /// of that information underneath. |
| 148 | /// Instead, we postpone all the splits after each iteration of |
| 149 | /// the main loop. That way, the information is at least valid |
| 150 | /// for the lifetime of an iteration. |
| 151 | /// |
| 152 | /// \return True if the edge is marked as toSplit, false otherwise. |
Patrik Hagglund | cfb121f | 2014-12-04 10:36:42 +0000 | [diff] [blame] | 153 | /// False can be returned if, for instance, this is not profitable. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 154 | bool PostponeSplitCriticalEdge(MachineInstr &MI, |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 155 | MachineBasicBlock *From, |
| 156 | MachineBasicBlock *To, |
| 157 | bool BreakPHIEdge); |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 158 | bool SinkInstruction(MachineInstr &MI, bool &SawStore, |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 159 | |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 160 | AllSuccsCache &AllSuccessors); |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 161 | bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 162 | MachineBasicBlock *DefMBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 163 | bool &BreakPHIEdge, bool &LocalUse) const; |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 164 | MachineBasicBlock *FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 165 | bool &BreakPHIEdge, AllSuccsCache &AllSuccessors); |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 166 | bool isProfitableToSinkTo(unsigned Reg, MachineInstr &MI, |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 167 | MachineBasicBlock *MBB, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 168 | MachineBasicBlock *SuccToSinkTo, |
| 169 | AllSuccsCache &AllSuccessors); |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 170 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 171 | bool PerformTrivialForwardCoalescing(MachineInstr &MI, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 172 | MachineBasicBlock *MBB); |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 173 | |
| 174 | SmallVector<MachineBasicBlock *, 4> & |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 175 | GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 176 | AllSuccsCache &AllSuccessors) const; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 177 | }; |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 178 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 179 | } // end anonymous namespace |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 180 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 181 | char MachineSinking::ID = 0; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 182 | |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 183 | char &llvm::MachineSinkingID = MachineSinking::ID; |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 184 | |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 185 | INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE, |
| 186 | "Machine code sinking", false, false) |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 187 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 188 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 189 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Matthias Braun | 94c4904 | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 191 | INITIALIZE_PASS_END(MachineSinking, DEBUG_TYPE, |
| 192 | "Machine code sinking", false, false) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 193 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 194 | bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr &MI, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 195 | MachineBasicBlock *MBB) { |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 196 | if (!MI.isCopy()) |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 197 | return false; |
| 198 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 199 | unsigned SrcReg = MI.getOperand(1).getReg(); |
| 200 | unsigned DstReg = MI.getOperand(0).getReg(); |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 201 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || |
| 202 | !TargetRegisterInfo::isVirtualRegister(DstReg) || |
| 203 | !MRI->hasOneNonDBGUse(SrcReg)) |
| 204 | return false; |
| 205 | |
| 206 | const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg); |
| 207 | const TargetRegisterClass *DRC = MRI->getRegClass(DstReg); |
| 208 | if (SRC != DRC) |
| 209 | return false; |
| 210 | |
| 211 | MachineInstr *DefMI = MRI->getVRegDef(SrcReg); |
| 212 | if (DefMI->isCopyLike()) |
| 213 | return false; |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 214 | LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI); |
| 215 | LLVM_DEBUG(dbgs() << "*** to: " << MI); |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 216 | MRI->replaceRegWith(DstReg, SrcReg); |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 217 | MI.eraseFromParent(); |
Patrik Hagglund | 3008bee | 2014-09-09 07:47:00 +0000 | [diff] [blame] | 218 | |
| 219 | // Conservatively, clear any kill flags, since it's possible that they are no |
| 220 | // longer correct. |
| 221 | MRI->clearKillFlags(SrcReg); |
| 222 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 223 | ++NumCoalesces; |
| 224 | return true; |
| 225 | } |
| 226 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 227 | /// AllUsesDominatedByBlock - Return true if all uses of the specified register |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 228 | /// occur in blocks dominated by the specified block. If any use is in the |
| 229 | /// definition block, then return false since it is never legal to move def |
| 230 | /// after uses. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 231 | bool |
| 232 | MachineSinking::AllUsesDominatedByBlock(unsigned Reg, |
| 233 | MachineBasicBlock *MBB, |
| 234 | MachineBasicBlock *DefMBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 235 | bool &BreakPHIEdge, |
| 236 | bool &LocalUse) const { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 237 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 238 | "Only makes sense for vregs"); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 239 | |
Devang Patel | f5b9a74 | 2011-12-09 01:25:04 +0000 | [diff] [blame] | 240 | // Ignore debug uses because debug info doesn't affect the code. |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 241 | if (MRI->use_nodbg_empty(Reg)) |
| 242 | return true; |
| 243 | |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 244 | // BreakPHIEdge is true if all the uses are in the successor MBB being sunken |
| 245 | // into and they are all PHI nodes. In this case, machine-sink must break |
| 246 | // the critical edge first. e.g. |
| 247 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 248 | // %bb.1: derived from LLVM BB %bb4.preheader |
| 249 | // Predecessors according to CFG: %bb.0 |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 250 | // ... |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 251 | // %reg16385 = DEC64_32r %reg16437, implicit-def dead %eflags |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 252 | // ... |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 253 | // JE_4 <%bb.37>, implicit %eflags |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 254 | // Successors according to CFG: %bb.37 %bb.2 |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 255 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 256 | // %bb.2: derived from LLVM BB %bb.nph |
| 257 | // Predecessors according to CFG: %bb.0 %bb.1 |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 258 | // %reg16386 = PHI %reg16434, %bb.0, %reg16385, %bb.1 |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 259 | BreakPHIEdge = true; |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 260 | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
| 261 | MachineInstr *UseInst = MO.getParent(); |
| 262 | unsigned OpNo = &MO - &UseInst->getOperand(0); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 263 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
| 264 | if (!(UseBlock == MBB && UseInst->isPHI() && |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 265 | UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) { |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 266 | BreakPHIEdge = false; |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | } |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 270 | if (BreakPHIEdge) |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 271 | return true; |
| 272 | |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 273 | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 274 | // Determine the block of the use. |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 275 | MachineInstr *UseInst = MO.getParent(); |
| 276 | unsigned OpNo = &MO - &UseInst->getOperand(0); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 277 | MachineBasicBlock *UseBlock = UseInst->getParent(); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 278 | if (UseInst->isPHI()) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 279 | // PHI nodes use the operand in the predecessor block, not the block with |
| 280 | // the PHI. |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 281 | UseBlock = UseInst->getOperand(OpNo+1).getMBB(); |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 282 | } else if (UseBlock == DefMBB) { |
| 283 | LocalUse = true; |
| 284 | return false; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 285 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 286 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 287 | // Check that it dominates. |
| 288 | if (!DT->dominates(MBB, UseBlock)) |
| 289 | return false; |
| 290 | } |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 291 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 295 | bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 296 | if (skipFunction(MF.getFunction())) |
Paul Robinson | 5fa58a5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 297 | return false; |
| 298 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 299 | LLVM_DEBUG(dbgs() << "******** Machine Sinking ********\n"); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 300 | |
Eric Christopher | d879de1 | 2014-10-14 07:00:33 +0000 | [diff] [blame] | 301 | TII = MF.getSubtarget().getInstrInfo(); |
| 302 | TRI = MF.getSubtarget().getRegisterInfo(); |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 303 | MRI = &MF.getRegInfo(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 304 | DT = &getAnalysis<MachineDominatorTree>(); |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 305 | PDT = &getAnalysis<MachinePostDominatorTree>(); |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 306 | LI = &getAnalysis<MachineLoopInfo>(); |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 307 | MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr; |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 308 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 309 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 310 | |
| 311 | bool EverMadeChange = false; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 312 | |
Eugene Zelenko | 3d7ca1c | 2016-08-25 00:45:04 +0000 | [diff] [blame] | 313 | while (true) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 314 | bool MadeChange = false; |
| 315 | |
| 316 | // Process all basic blocks. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 317 | CEBCandidates.clear(); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 318 | ToSplit.clear(); |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 319 | for (auto &MBB: MF) |
| 320 | MadeChange |= ProcessBlock(MBB); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 321 | |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 322 | // If we have anything we marked as toSplit, split it now. |
| 323 | for (auto &Pair : ToSplit) { |
Quentin Colombet | f2cd157 | 2016-04-21 21:01:13 +0000 | [diff] [blame] | 324 | auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, *this); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 325 | if (NewSucc != nullptr) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 326 | LLVM_DEBUG(dbgs() << " *** Splitting critical edge: " |
| 327 | << printMBBReference(*Pair.first) << " -- " |
| 328 | << printMBBReference(*NewSucc) << " -- " |
| 329 | << printMBBReference(*Pair.second) << '\n'); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 330 | MadeChange = true; |
| 331 | ++NumSplit; |
| 332 | } else |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 333 | LLVM_DEBUG(dbgs() << " *** Not legal to break critical edge\n"); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 335 | // If this iteration over the code changed anything, keep iterating. |
| 336 | if (!MadeChange) break; |
| 337 | EverMadeChange = true; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 338 | } |
Matthias Braun | 3ac70da | 2015-05-16 03:11:07 +0000 | [diff] [blame] | 339 | |
| 340 | // Now clear any kill flags for recorded registers. |
| 341 | for (auto I : RegsToClearKillFlags) |
| 342 | MRI->clearKillFlags(I); |
| 343 | RegsToClearKillFlags.clear(); |
| 344 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 345 | return EverMadeChange; |
| 346 | } |
| 347 | |
| 348 | bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 349 | // Can't sink anything out of a block that has less than two successors. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 350 | if (MBB.succ_size() <= 1 || MBB.empty()) return false; |
| 351 | |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 352 | // Don't bother sinking code out of unreachable blocks. In addition to being |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 353 | // unprofitable, it can also lead to infinite looping, because in an |
| 354 | // unreachable loop there may be nowhere to stop. |
Dan Gohman | c4ae94d | 2010-04-05 19:17:22 +0000 | [diff] [blame] | 355 | if (!DT->isReachableFromEntry(&MBB)) return false; |
| 356 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 357 | bool MadeChange = false; |
| 358 | |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 359 | // Cache all successors, sorted by frequency info and loop depth. |
| 360 | AllSuccsCache AllSuccessors; |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 361 | |
Chris Lattner | aad193a | 2008-01-12 00:17:41 +0000 | [diff] [blame] | 362 | // Walk the basic block bottom-up. Remember if we saw a store. |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 363 | MachineBasicBlock::iterator I = MBB.end(); |
| 364 | --I; |
| 365 | bool ProcessedBegin, SawStore = false; |
| 366 | do { |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 367 | MachineInstr &MI = *I; // The instruction to sink. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 369 | // Predecrement I (if it's not begin) so that it isn't invalidated by |
| 370 | // sinking. |
| 371 | ProcessedBegin = I == MBB.begin(); |
| 372 | if (!ProcessedBegin) |
| 373 | --I; |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 374 | |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 375 | if (MI.isDebugInstr()) |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 376 | continue; |
| 377 | |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 378 | bool Joined = PerformTrivialForwardCoalescing(MI, &MBB); |
| 379 | if (Joined) { |
| 380 | MadeChange = true; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 381 | continue; |
Evan Cheng | cfea985 | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 382 | } |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 383 | |
Richard Trieu | 1b96cbe | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 384 | if (SinkInstruction(MI, SawStore, AllSuccessors)) { |
| 385 | ++NumSunk; |
| 386 | MadeChange = true; |
| 387 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 296185c | 2009-04-10 16:38:36 +0000 | [diff] [blame] | 389 | // If we just processed the first instruction in the block, we're done. |
| 390 | } while (!ProcessedBegin); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 391 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 392 | return MadeChange; |
| 393 | } |
| 394 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 395 | bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr &MI, |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 396 | MachineBasicBlock *From, |
| 397 | MachineBasicBlock *To) { |
| 398 | // FIXME: Need much better heuristics. |
| 399 | |
| 400 | // If the pass has already considered breaking this edge (during this pass |
| 401 | // through the function), then let's go ahead and break it. This means |
| 402 | // sinking multiple "cheap" instructions into the same block. |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 403 | if (!CEBCandidates.insert(std::make_pair(From, To)).second) |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 404 | return true; |
| 405 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 406 | if (!MI.isCopy() && !TII->isAsCheapAsAMove(MI)) |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 407 | return true; |
| 408 | |
Dehao Chen | 0872bb2 | 2016-10-20 18:06:52 +0000 | [diff] [blame] | 409 | if (From->isSuccessor(To) && MBPI->getEdgeProbability(From, To) <= |
| 410 | BranchProbability(SplitEdgeProbabilityThreshold, 100)) |
| 411 | return true; |
| 412 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 413 | // MI is cheap, we probably don't want to break the critical edge for it. |
| 414 | // However, if this would allow some definitions of its source operands |
| 415 | // to be sunk then it's probably worth it. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 416 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 417 | const MachineOperand &MO = MI.getOperand(i); |
Will Dietz | e4b44c1 | 2013-10-14 16:57:17 +0000 | [diff] [blame] | 418 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 419 | continue; |
Will Dietz | e4b44c1 | 2013-10-14 16:57:17 +0000 | [diff] [blame] | 420 | unsigned Reg = MO.getReg(); |
| 421 | if (Reg == 0) |
| 422 | continue; |
| 423 | |
| 424 | // We don't move live definitions of physical registers, |
| 425 | // so sinking their uses won't enable any opportunities. |
| 426 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 427 | continue; |
| 428 | |
| 429 | // If this instruction is the only user of a virtual register, |
| 430 | // check if breaking the edge will enable sinking |
| 431 | // both this instruction and the defining instruction. |
| 432 | if (MRI->hasOneNonDBGUse(Reg)) { |
| 433 | // If the definition resides in same MBB, |
| 434 | // claim it's likely we can sink these together. |
| 435 | // If definition resides elsewhere, we aren't |
| 436 | // blocking it from being sunk so don't break the edge. |
| 437 | MachineInstr *DefMI = MRI->getVRegDef(Reg); |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 438 | if (DefMI->getParent() == MI.getParent()) |
Will Dietz | e4b44c1 | 2013-10-14 16:57:17 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | return false; |
| 444 | } |
| 445 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 446 | bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr &MI, |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 447 | MachineBasicBlock *FromBB, |
| 448 | MachineBasicBlock *ToBB, |
| 449 | bool BreakPHIEdge) { |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 450 | if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB)) |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 451 | return false; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 452 | |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 453 | // Avoid breaking back edge. From == To means backedge for single BB loop. |
Evan Cheng | 44be1a8 | 2010-09-20 22:52:00 +0000 | [diff] [blame] | 454 | if (!SplitEdges || FromBB == ToBB) |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 455 | return false; |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 456 | |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 457 | // Check for backedges of more "complex" loops. |
| 458 | if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) && |
| 459 | LI->isLoopHeader(ToBB)) |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 460 | return false; |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 461 | |
| 462 | // It's not always legal to break critical edges and sink the computation |
| 463 | // to the edge. |
| 464 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 465 | // %bb.1: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 466 | // v1024 |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 467 | // Beq %bb.3 |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 468 | // <fallthrough> |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 469 | // %bb.2: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 470 | // ... no uses of v1024 |
| 471 | // <fallthrough> |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 472 | // %bb.3: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 473 | // ... |
| 474 | // = v1024 |
| 475 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 476 | // If %bb.1 -> %bb.3 edge is broken and computation of v1024 is inserted: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 477 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 478 | // %bb.1: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 479 | // ... |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 480 | // Bne %bb.2 |
| 481 | // %bb.4: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 482 | // v1024 = |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 483 | // B %bb.3 |
| 484 | // %bb.2: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 485 | // ... no uses of v1024 |
| 486 | // <fallthrough> |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 487 | // %bb.3: |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 488 | // ... |
| 489 | // = v1024 |
| 490 | // |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 491 | // This is incorrect since v1024 is not computed along the %bb.1->%bb.2->%bb.3 |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 492 | // flow. We need to ensure the new basic block where the computation is |
| 493 | // sunk to dominates all the uses. |
| 494 | // It's only legal to break critical edge and sink the computation to the |
| 495 | // new block if all the predecessors of "To", except for "From", are |
| 496 | // not dominated by "From". Given SSA property, this means these |
| 497 | // predecessors are dominated by "To". |
| 498 | // |
| 499 | // There is no need to do this check if all the uses are PHI nodes. PHI |
| 500 | // sources are only defined on the specific predecessor edges. |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 501 | if (!BreakPHIEdge) { |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 502 | for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(), |
| 503 | E = ToBB->pred_end(); PI != E; ++PI) { |
| 504 | if (*PI == FromBB) |
| 505 | continue; |
| 506 | if (!DT->dominates(ToBB, *PI)) |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 507 | return false; |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 508 | } |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 511 | ToSplit.insert(std::make_pair(FromBB, ToBB)); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 512 | |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 513 | return true; |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 516 | /// isProfitableToSinkTo - Return true if it is profitable to sink MI. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 517 | bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr &MI, |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 518 | MachineBasicBlock *MBB, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 519 | MachineBasicBlock *SuccToSinkTo, |
| 520 | AllSuccsCache &AllSuccessors) { |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 521 | assert (SuccToSinkTo && "Invalid SinkTo Candidate BB"); |
| 522 | |
| 523 | if (MBB == SuccToSinkTo) |
| 524 | return false; |
| 525 | |
| 526 | // It is profitable if SuccToSinkTo does not post dominate current block. |
Jingyue Wu | 75d77cd | 2014-10-15 03:27:43 +0000 | [diff] [blame] | 527 | if (!PDT->dominates(SuccToSinkTo, MBB)) |
| 528 | return true; |
| 529 | |
| 530 | // It is profitable to sink an instruction from a deeper loop to a shallower |
| 531 | // loop, even if the latter post-dominates the former (PR21115). |
| 532 | if (LI->getLoopDepth(MBB) > LI->getLoopDepth(SuccToSinkTo)) |
| 533 | return true; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 534 | |
| 535 | // Check if only use in post dominated block is PHI instruction. |
| 536 | bool NonPHIUse = false; |
Owen Anderson | 92fca73 | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 537 | for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) { |
| 538 | MachineBasicBlock *UseBlock = UseInst.getParent(); |
| 539 | if (UseBlock == SuccToSinkTo && !UseInst.isPHI()) |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 540 | NonPHIUse = true; |
| 541 | } |
| 542 | if (!NonPHIUse) |
| 543 | return true; |
| 544 | |
| 545 | // If SuccToSinkTo post dominates then also it may be profitable if MI |
| 546 | // can further profitably sinked into another block in next round. |
| 547 | bool BreakPHIEdge = false; |
Patrik Hagglund | cfb121f | 2014-12-04 10:36:42 +0000 | [diff] [blame] | 548 | // FIXME - If finding successor is compile time expensive then cache results. |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 549 | if (MachineBasicBlock *MBB2 = |
| 550 | FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge, AllSuccessors)) |
| 551 | return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2, AllSuccessors); |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 552 | |
| 553 | // If SuccToSinkTo is final destination and it is a post dominator of current |
| 554 | // block then it is not profitable to sink MI into SuccToSinkTo block. |
| 555 | return false; |
| 556 | } |
| 557 | |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 558 | /// Get the sorted sequence of successors for this MachineBasicBlock, possibly |
| 559 | /// computing it if it was not already cached. |
| 560 | SmallVector<MachineBasicBlock *, 4> & |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 561 | MachineSinking::GetAllSortedSuccessors(MachineInstr &MI, MachineBasicBlock *MBB, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 562 | AllSuccsCache &AllSuccessors) const { |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 563 | // Do we have the sorted successors in cache ? |
| 564 | auto Succs = AllSuccessors.find(MBB); |
| 565 | if (Succs != AllSuccessors.end()) |
| 566 | return Succs->second; |
| 567 | |
| 568 | SmallVector<MachineBasicBlock *, 4> AllSuccs(MBB->succ_begin(), |
| 569 | MBB->succ_end()); |
| 570 | |
| 571 | // Handle cases where sinking can happen but where the sink point isn't a |
| 572 | // successor. For example: |
| 573 | // |
| 574 | // x = computation |
| 575 | // if () {} else {} |
| 576 | // use x |
| 577 | // |
| 578 | const std::vector<MachineDomTreeNode *> &Children = |
| 579 | DT->getNode(MBB)->getChildren(); |
| 580 | for (const auto &DTChild : Children) |
| 581 | // DomTree children of MBB that have MBB as immediate dominator are added. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 582 | if (DTChild->getIDom()->getBlock() == MI.getParent() && |
Arnaud A. de Grandmaison | 981ffd1 | 2015-06-15 09:09:06 +0000 | [diff] [blame] | 583 | // Skip MBBs already added to the AllSuccs vector above. |
| 584 | !MBB->isSuccessor(DTChild->getBlock())) |
| 585 | AllSuccs.push_back(DTChild->getBlock()); |
| 586 | |
| 587 | // Sort Successors according to their loop depth or block frequency info. |
| 588 | std::stable_sort( |
| 589 | AllSuccs.begin(), AllSuccs.end(), |
| 590 | [this](const MachineBasicBlock *L, const MachineBasicBlock *R) { |
| 591 | uint64_t LHSFreq = MBFI ? MBFI->getBlockFreq(L).getFrequency() : 0; |
| 592 | uint64_t RHSFreq = MBFI ? MBFI->getBlockFreq(R).getFrequency() : 0; |
| 593 | bool HasBlockFreq = LHSFreq != 0 && RHSFreq != 0; |
| 594 | return HasBlockFreq ? LHSFreq < RHSFreq |
| 595 | : LI->getLoopDepth(L) < LI->getLoopDepth(R); |
| 596 | }); |
| 597 | |
| 598 | auto it = AllSuccessors.insert(std::make_pair(MBB, AllSuccs)); |
| 599 | |
| 600 | return it.first->second; |
| 601 | } |
| 602 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 603 | /// FindSuccToSinkTo - Find a successor to sink this instruction to. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 604 | MachineBasicBlock * |
| 605 | MachineSinking::FindSuccToSinkTo(MachineInstr &MI, MachineBasicBlock *MBB, |
| 606 | bool &BreakPHIEdge, |
| 607 | AllSuccsCache &AllSuccessors) { |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 608 | assert (MBB && "Invalid MachineBasicBlock!"); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 609 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 610 | // Loop over all the operands of the specified instruction. If there is |
| 611 | // anything we can't handle, bail out. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 612 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 613 | // SuccToSinkTo - This is the successor to sink this instruction to, once we |
| 614 | // decide. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 615 | MachineBasicBlock *SuccToSinkTo = nullptr; |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 616 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 617 | const MachineOperand &MO = MI.getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 618 | if (!MO.isReg()) continue; // Ignore non-register operands. |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 619 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 620 | unsigned Reg = MO.getReg(); |
| 621 | if (Reg == 0) continue; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 622 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 623 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 624 | if (MO.isUse()) { |
| 625 | // If the physreg has no defs anywhere, it's just an ambient register |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 626 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 627 | // it could get allocated to something with a def during allocation. |
Matthias Braun | 15cdf2c | 2016-10-28 18:05:09 +0000 | [diff] [blame] | 628 | if (!MRI->isConstantPhysReg(Reg)) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 629 | return nullptr; |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 630 | } else if (!MO.isDead()) { |
| 631 | // A def that isn't dead. We can't move it. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 632 | return nullptr; |
Dan Gohman | 19778e7 | 2009-09-25 22:53:29 +0000 | [diff] [blame] | 633 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 634 | } else { |
| 635 | // Virtual register uses are always safe to sink. |
| 636 | if (MO.isUse()) continue; |
Evan Cheng | b6f5417 | 2009-02-07 01:21:47 +0000 | [diff] [blame] | 637 | |
| 638 | // If it's not safe to move defs of the register class, then abort. |
Evan Cheng | 6edb0ea | 2010-09-17 22:28:18 +0000 | [diff] [blame] | 639 | if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg))) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 640 | return nullptr; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 641 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 642 | // Virtual register defs can only be sunk if all their uses are in blocks |
| 643 | // dominated by one of the successors. |
| 644 | if (SuccToSinkTo) { |
| 645 | // If a previous operand picked a block to sink to, then this operand |
| 646 | // must be sinkable to the same block. |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 647 | bool LocalUse = false; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 648 | if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 649 | BreakPHIEdge, LocalUse)) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 650 | return nullptr; |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 651 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 652 | continue; |
| 653 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 654 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 655 | // Otherwise, we should look at all the successors and decide which one |
Bruno Cardoso Lopes | f423025 | 2014-09-25 23:14:26 +0000 | [diff] [blame] | 656 | // we should sink to. If we have reliable block frequency information |
| 657 | // (frequency != 0) available, give successors with smaller frequencies |
| 658 | // higher priority, otherwise prioritize smaller loop depths. |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 659 | for (MachineBasicBlock *SuccBlock : |
| 660 | GetAllSortedSuccessors(MI, MBB, AllSuccessors)) { |
Evan Cheng | e5e7946 | 2010-08-19 18:33:29 +0000 | [diff] [blame] | 661 | bool LocalUse = false; |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 662 | if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB, |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 663 | BreakPHIEdge, LocalUse)) { |
Devang Patel | cf405ba | 2011-12-08 21:33:23 +0000 | [diff] [blame] | 664 | SuccToSinkTo = SuccBlock; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 665 | break; |
| 666 | } |
Evan Cheng | c3439ad | 2010-08-18 23:09:25 +0000 | [diff] [blame] | 667 | if (LocalUse) |
| 668 | // Def is used locally, it's never safe to move this def. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 669 | return nullptr; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 670 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 671 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 672 | // If we couldn't find a block to sink to, ignore this instruction. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 673 | if (!SuccToSinkTo) |
| 674 | return nullptr; |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 675 | if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo, AllSuccessors)) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 676 | return nullptr; |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Devang Patel | 7f7f090 | 2011-12-08 23:52:00 +0000 | [diff] [blame] | 679 | |
| 680 | // It is not possible to sink an instruction into its own block. This can |
| 681 | // happen with loops. |
Devang Patel | 5211134 | 2011-12-14 23:20:38 +0000 | [diff] [blame] | 682 | if (MBB == SuccToSinkTo) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 683 | return nullptr; |
Devang Patel | 7f7f090 | 2011-12-08 23:52:00 +0000 | [diff] [blame] | 684 | |
| 685 | // It's not safe to sink instructions to EH landing pad. Control flow into |
| 686 | // landing pad is implicitly defined. |
Reid Kleckner | c0e64ad | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 687 | if (SuccToSinkTo && SuccToSinkTo->isEHPad()) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 688 | return nullptr; |
Devang Patel | 7f7f090 | 2011-12-08 23:52:00 +0000 | [diff] [blame] | 689 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 690 | return SuccToSinkTo; |
| 691 | } |
| 692 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 693 | /// Return true if MI is likely to be usable as a memory operation by the |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 694 | /// implicit null check optimization. |
| 695 | /// |
| 696 | /// This is a "best effort" heuristic, and should not be relied upon for |
| 697 | /// correctness. This returning true does not guarantee that the implicit null |
| 698 | /// check optimization is legal over MI, and this returning false does not |
| 699 | /// guarantee MI cannot possibly be used to do a null check. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 700 | static bool SinkingPreventsImplicitNullCheck(MachineInstr &MI, |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 701 | const TargetInstrInfo *TII, |
| 702 | const TargetRegisterInfo *TRI) { |
Eugene Zelenko | 7cf6af5 | 2017-08-29 22:32:07 +0000 | [diff] [blame] | 703 | using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate; |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 704 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 705 | auto *MBB = MI.getParent(); |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 706 | if (MBB->pred_size() != 1) |
| 707 | return false; |
| 708 | |
| 709 | auto *PredMBB = *MBB->pred_begin(); |
| 710 | auto *PredBB = PredMBB->getBasicBlock(); |
| 711 | |
| 712 | // Frontends that don't use implicit null checks have no reason to emit |
| 713 | // branches with make.implicit metadata, and this function should always |
| 714 | // return false for them. |
| 715 | if (!PredBB || |
| 716 | !PredBB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit)) |
| 717 | return false; |
| 718 | |
Francis Visoiu Mistrih | 83895b3 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 719 | MachineOperand *BaseOp; |
Chad Rosier | cd3a68c | 2016-03-09 16:00:35 +0000 | [diff] [blame] | 720 | int64_t Offset; |
Francis Visoiu Mistrih | 83895b3 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 721 | if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI)) |
| 722 | return false; |
| 723 | |
| 724 | if (!BaseOp->isReg()) |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 725 | return false; |
| 726 | |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 727 | if (!(MI.mayLoad() && !MI.isPredicable())) |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 728 | return false; |
| 729 | |
| 730 | MachineBranchPredicate MBP; |
Jacques Pienaar | 48ed4ab | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 731 | if (TII->analyzeBranchPredicate(*PredMBB, MBP, false)) |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 732 | return false; |
| 733 | |
| 734 | return MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 && |
| 735 | (MBP.Predicate == MachineBranchPredicate::PRED_NE || |
| 736 | MBP.Predicate == MachineBranchPredicate::PRED_EQ) && |
Francis Visoiu Mistrih | 83895b3 | 2018-11-28 12:00:20 +0000 | [diff] [blame] | 737 | MBP.LHS.getReg() == BaseOp->getReg(); |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 740 | /// Sink an instruction and its associated debug instructions. If the debug |
| 741 | /// instructions to be sunk are already known, they can be provided in DbgVals. |
Matt Davis | 50feec5 | 2018-06-21 17:59:52 +0000 | [diff] [blame] | 742 | static void performSink(MachineInstr &MI, MachineBasicBlock &SuccToSinkTo, |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 743 | MachineBasicBlock::iterator InsertPos, |
| 744 | SmallVectorImpl<MachineInstr *> *DbgVals = nullptr) { |
| 745 | // If debug values are provided use those, otherwise call collectDebugValues. |
Matt Davis | 50feec5 | 2018-06-21 17:59:52 +0000 | [diff] [blame] | 746 | SmallVector<MachineInstr *, 2> DbgValuesToSink; |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 747 | if (DbgVals) |
| 748 | DbgValuesToSink.insert(DbgValuesToSink.begin(), |
| 749 | DbgVals->begin(), DbgVals->end()); |
| 750 | else |
| 751 | MI.collectDebugValues(DbgValuesToSink); |
Matt Davis | 50feec5 | 2018-06-21 17:59:52 +0000 | [diff] [blame] | 752 | |
| 753 | // If we cannot find a location to use (merge with), then we erase the debug |
| 754 | // location to prevent debug-info driven tools from potentially reporting |
| 755 | // wrong location information. |
| 756 | if (!SuccToSinkTo.empty() && InsertPos != SuccToSinkTo.end()) |
| 757 | MI.setDebugLoc(DILocation::getMergedLocation(MI.getDebugLoc(), |
| 758 | InsertPos->getDebugLoc())); |
| 759 | else |
| 760 | MI.setDebugLoc(DebugLoc()); |
| 761 | |
| 762 | // Move the instruction. |
| 763 | MachineBasicBlock *ParentBlock = MI.getParent(); |
| 764 | SuccToSinkTo.splice(InsertPos, ParentBlock, MI, |
| 765 | ++MachineBasicBlock::iterator(MI)); |
| 766 | |
| 767 | // Move previously adjacent debug value instructions to the insert position. |
| 768 | for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(), |
| 769 | DBE = DbgValuesToSink.end(); |
| 770 | DBI != DBE; ++DBI) { |
| 771 | MachineInstr *DbgMI = *DBI; |
| 772 | SuccToSinkTo.splice(InsertPos, ParentBlock, DbgMI, |
| 773 | ++MachineBasicBlock::iterator(DbgMI)); |
| 774 | } |
| 775 | } |
| 776 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 777 | /// SinkInstruction - Determine whether it is safe to sink the specified machine |
| 778 | /// instruction out of its current block into a successor. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 779 | bool MachineSinking::SinkInstruction(MachineInstr &MI, bool &SawStore, |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 780 | AllSuccsCache &AllSuccessors) { |
Fiona Glaser | 513c245 | 2016-03-29 22:44:57 +0000 | [diff] [blame] | 781 | // Don't sink instructions that the target prefers not to sink. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 782 | if (!TII->shouldSink(MI)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 783 | return false; |
| 784 | |
| 785 | // Check if it's safe to move the instruction. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 786 | if (!MI.isSafeToMove(AA, SawStore)) |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 787 | return false; |
| 788 | |
Owen Anderson | 983d814 | 2015-10-09 18:06:13 +0000 | [diff] [blame] | 789 | // Convergent operations may not be made control-dependent on additional |
| 790 | // values. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 791 | if (MI.isConvergent()) |
Owen Anderson | 6f9474c | 2015-06-01 17:26:30 +0000 | [diff] [blame] | 792 | return false; |
| 793 | |
Sanjoy Das | e0f8e1a | 2016-01-20 00:06:14 +0000 | [diff] [blame] | 794 | // Don't break implicit null checks. This is a performance heuristic, and not |
| 795 | // required for correctness. |
| 796 | if (SinkingPreventsImplicitNullCheck(MI, TII, TRI)) |
| 797 | return false; |
| 798 | |
Devang Patel | e265bcf | 2011-12-08 21:48:01 +0000 | [diff] [blame] | 799 | // FIXME: This should include support for sinking instructions within the |
| 800 | // block they are currently in to shorten the live ranges. We often get |
| 801 | // instructions sunk into the top of a large block, but it would be better to |
| 802 | // also sink them down before their first use in the block. This xform has to |
| 803 | // be careful not to *increase* register pressure though, e.g. sinking |
| 804 | // "x = y + z" down if it kills y and z would increase the live ranges of y |
| 805 | // and z and only shrink the live range of x. |
| 806 | |
| 807 | bool BreakPHIEdge = false; |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 808 | MachineBasicBlock *ParentBlock = MI.getParent(); |
Arnaud A. de Grandmaison | 443838d | 2015-06-16 08:57:21 +0000 | [diff] [blame] | 809 | MachineBasicBlock *SuccToSinkTo = |
| 810 | FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge, AllSuccessors); |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 811 | |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 812 | // If there are no outputs, it must have side-effects. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 813 | if (!SuccToSinkTo) |
Chris Lattner | 9bb459b | 2008-01-05 01:39:17 +0000 | [diff] [blame] | 814 | return false; |
Evan Cheng | b599979 | 2009-02-15 08:36:12 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | d24c9d5 | 2010-06-23 00:48:25 +0000 | [diff] [blame] | 816 | // If the instruction to move defines a dead physical register which is live |
| 817 | // when leaving the basic block, don't move it because it could turn into a |
| 818 | // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>) |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 819 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 820 | const MachineOperand &MO = MI.getOperand(I); |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 821 | if (!MO.isReg()) continue; |
| 822 | unsigned Reg = MO.getReg(); |
| 823 | if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
| 824 | if (SuccToSinkTo->isLiveIn(Reg)) |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 825 | return false; |
Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 826 | } |
Bill Wendling | 869d60d | 2010-06-03 07:54:20 +0000 | [diff] [blame] | 827 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 828 | LLVM_DEBUG(dbgs() << "Sink instr " << MI << "\tinto block " << *SuccToSinkTo); |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 829 | |
Will Dietz | e4b44c1 | 2013-10-14 16:57:17 +0000 | [diff] [blame] | 830 | // If the block has multiple predecessors, this is a critical edge. |
| 831 | // Decide if we can sink along it or need to break the edge. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 832 | if (SuccToSinkTo->pred_size() > 1) { |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 833 | // We cannot sink a load across a critical edge - there may be stores in |
| 834 | // other code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 835 | bool TryBreak = false; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 836 | bool store = true; |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 837 | if (!MI.isSafeToMove(AA, store)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 838 | LLVM_DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 839 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | // We don't want to sink across a critical edge if we don't dominate the |
| 843 | // successor. We could be introducing calculations to new code paths. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 844 | if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 845 | LLVM_DEBUG(dbgs() << " *** NOTE: Critical edge found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 846 | TryBreak = true; |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 849 | // Don't sink instructions into a loop. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 850 | if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 851 | LLVM_DEBUG(dbgs() << " *** NOTE: Loop header found\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 852 | TryBreak = true; |
Jakob Stoklund Olesen | 626f3d7 | 2010-04-15 23:41:02 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Jakob Stoklund Olesen | 8d17160 | 2010-04-13 19:06:14 +0000 | [diff] [blame] | 855 | // Otherwise we are OK with sinking along a critical edge. |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 856 | if (!TryBreak) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 857 | LLVM_DEBUG(dbgs() << "Sinking along critical edge.\n"); |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 858 | else { |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 859 | // Mark this edge as to be split. |
| 860 | // If the edge can actually be split, the next iteration of the main loop |
| 861 | // will sink MI in the newly created block. |
| 862 | bool Status = |
| 863 | PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge); |
| 864 | if (!Status) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 865 | LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " |
| 866 | "break critical edge\n"); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 867 | // The instruction will not be sunk this time. |
| 868 | return false; |
Evan Cheng | 4dc301a | 2010-08-19 17:33:11 +0000 | [diff] [blame] | 869 | } |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 870 | } |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 871 | |
Evan Cheng | 7af6dc4 | 2010-09-20 19:12:55 +0000 | [diff] [blame] | 872 | if (BreakPHIEdge) { |
| 873 | // BreakPHIEdge is true if all the uses are in the successor MBB being |
| 874 | // sunken into and they are all PHI nodes. In this case, machine-sink must |
| 875 | // break the critical edge first. |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 876 | bool Status = PostponeSplitCriticalEdge(MI, ParentBlock, |
| 877 | SuccToSinkTo, BreakPHIEdge); |
| 878 | if (!Status) |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 879 | LLVM_DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " |
| 880 | "break critical edge\n"); |
Quentin Colombet | bc7206a | 2014-08-11 23:52:01 +0000 | [diff] [blame] | 881 | // The instruction will not be sunk this time. |
| 882 | return false; |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Bill Wendling | 05c6837 | 2010-06-02 23:04:26 +0000 | [diff] [blame] | 885 | // Determine where to insert into. Skip phi nodes. |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 886 | MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); |
Evan Cheng | 2399786 | 2010-09-18 06:42:17 +0000 | [diff] [blame] | 887 | while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 888 | ++InsertPos; |
Jim Grosbach | 6ee358b | 2010-06-03 23:49:57 +0000 | [diff] [blame] | 889 | |
Matt Davis | 50feec5 | 2018-06-21 17:59:52 +0000 | [diff] [blame] | 890 | performSink(MI, *SuccToSinkTo, InsertPos); |
Devang Patel | 541a81c | 2011-09-07 00:07:58 +0000 | [diff] [blame] | 891 | |
Juergen Ributzka | cd72c21 | 2014-09-04 02:07:36 +0000 | [diff] [blame] | 892 | // Conservatively, clear any kill flags, since it's possible that they are no |
| 893 | // longer correct. |
Pete Cooper | d90099d | 2015-05-08 17:54:32 +0000 | [diff] [blame] | 894 | // Note that we have to clear the kill flags for any register this instruction |
| 895 | // uses as we may sink over another instruction which currently kills the |
| 896 | // used registers. |
Duncan P. N. Exon Smith | dbcbdf3 | 2016-07-01 00:11:48 +0000 | [diff] [blame] | 897 | for (MachineOperand &MO : MI.operands()) { |
Pete Cooper | d90099d | 2015-05-08 17:54:32 +0000 | [diff] [blame] | 898 | if (MO.isReg() && MO.isUse()) |
Matthias Braun | 3ac70da | 2015-05-16 03:11:07 +0000 | [diff] [blame] | 899 | RegsToClearKillFlags.set(MO.getReg()); // Remember to clear kill flags. |
Pete Cooper | d90099d | 2015-05-08 17:54:32 +0000 | [diff] [blame] | 900 | } |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 901 | |
Chris Lattner | c4ce73f | 2008-01-04 07:36:53 +0000 | [diff] [blame] | 902 | return true; |
| 903 | } |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 904 | |
| 905 | //===----------------------------------------------------------------------===// |
| 906 | // This pass is not intended to be a replacement or a complete alternative |
| 907 | // for the pre-ra machine sink pass. It is only designed to sink COPY |
| 908 | // instructions which should be handled after RA. |
| 909 | // |
| 910 | // This pass sinks COPY instructions into a successor block, if the COPY is not |
| 911 | // used in the current block and the COPY is live-in to a single successor |
| 912 | // (i.e., doesn't require the COPY to be duplicated). This avoids executing the |
| 913 | // copy on paths where their results aren't needed. This also exposes |
| 914 | // additional opportunites for dead copy elimination and shrink wrapping. |
| 915 | // |
| 916 | // These copies were either not handled by or are inserted after the MachineSink |
| 917 | // pass. As an example of the former case, the MachineSink pass cannot sink |
| 918 | // COPY instructions with allocatable source registers; for AArch64 these type |
| 919 | // of copy instructions are frequently used to move function parameters (PhyReg) |
| 920 | // into virtual registers in the entry block. |
| 921 | // |
| 922 | // For the machine IR below, this pass will sink %w19 in the entry into its |
| 923 | // successor (%bb.1) because %w19 is only live-in in %bb.1. |
| 924 | // %bb.0: |
| 925 | // %wzr = SUBSWri %w1, 1 |
| 926 | // %w19 = COPY %w0 |
| 927 | // Bcc 11, %bb.2 |
| 928 | // %bb.1: |
| 929 | // Live Ins: %w19 |
| 930 | // BL @fun |
| 931 | // %w0 = ADDWrr %w0, %w19 |
| 932 | // RET %w0 |
| 933 | // %bb.2: |
| 934 | // %w0 = COPY %wzr |
| 935 | // RET %w0 |
| 936 | // As we sink %w19 (CSR in AArch64) into %bb.1, the shrink-wrapping pass will be |
| 937 | // able to see %bb.0 as a candidate. |
| 938 | //===----------------------------------------------------------------------===// |
| 939 | namespace { |
| 940 | |
| 941 | class PostRAMachineSinking : public MachineFunctionPass { |
| 942 | public: |
| 943 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 944 | |
| 945 | static char ID; |
| 946 | PostRAMachineSinking() : MachineFunctionPass(ID) {} |
| 947 | StringRef getPassName() const override { return "PostRA Machine Sink"; } |
| 948 | |
Jun Bum Lim | e6597a2 | 2018-03-28 19:56:26 +0000 | [diff] [blame] | 949 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 950 | AU.setPreservesCFG(); |
| 951 | MachineFunctionPass::getAnalysisUsage(AU); |
| 952 | } |
| 953 | |
Jun Bum Lim | c23bb98 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 954 | MachineFunctionProperties getRequiredProperties() const override { |
| 955 | return MachineFunctionProperties().set( |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 956 | MachineFunctionProperties::Property::NoVRegs); |
Jun Bum Lim | c23bb98 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 959 | private: |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 960 | /// Track which register units have been modified and used. |
| 961 | LiveRegUnits ModifiedRegUnits, UsedRegUnits; |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 962 | |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 963 | /// Track DBG_VALUEs of (unmodified) register units. |
| 964 | DenseMap<unsigned, TinyPtrVector<MachineInstr*>> SeenDbgInstrs; |
| 965 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 966 | /// Sink Copy instructions unused in the same block close to their uses in |
| 967 | /// successors. |
| 968 | bool tryToSinkCopy(MachineBasicBlock &BB, MachineFunction &MF, |
| 969 | const TargetRegisterInfo *TRI, const TargetInstrInfo *TII); |
| 970 | }; |
| 971 | } // namespace |
| 972 | |
| 973 | char PostRAMachineSinking::ID = 0; |
| 974 | char &llvm::PostRAMachineSinkingID = PostRAMachineSinking::ID; |
| 975 | |
| 976 | INITIALIZE_PASS(PostRAMachineSinking, "postra-machine-sink", |
| 977 | "PostRA Machine Sink", false, false) |
| 978 | |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 979 | static bool aliasWithRegsInLiveIn(MachineBasicBlock &MBB, unsigned Reg, |
| 980 | const TargetRegisterInfo *TRI) { |
| 981 | LiveRegUnits LiveInRegUnits(*TRI); |
| 982 | LiveInRegUnits.addLiveIns(MBB); |
| 983 | return !LiveInRegUnits.available(Reg); |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 986 | static MachineBasicBlock * |
| 987 | getSingleLiveInSuccBB(MachineBasicBlock &CurBB, |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 988 | const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs, |
| 989 | unsigned Reg, const TargetRegisterInfo *TRI) { |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 990 | // Try to find a single sinkable successor in which Reg is live-in. |
| 991 | MachineBasicBlock *BB = nullptr; |
| 992 | for (auto *SI : SinkableBBs) { |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 993 | if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) { |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 994 | // If BB is set here, Reg is live-in to at least two sinkable successors, |
| 995 | // so quit. |
| 996 | if (BB) |
| 997 | return nullptr; |
| 998 | BB = SI; |
| 999 | } |
| 1000 | } |
| 1001 | // Reg is not live-in to any sinkable successors. |
| 1002 | if (!BB) |
| 1003 | return nullptr; |
| 1004 | |
| 1005 | // Check if any register aliased with Reg is live-in in other successors. |
| 1006 | for (auto *SI : CurBB.successors()) { |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 1007 | if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI)) |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1008 | return nullptr; |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1009 | } |
| 1010 | return BB; |
| 1011 | } |
| 1012 | |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 1013 | static MachineBasicBlock * |
| 1014 | getSingleLiveInSuccBB(MachineBasicBlock &CurBB, |
| 1015 | const SmallPtrSetImpl<MachineBasicBlock *> &SinkableBBs, |
| 1016 | ArrayRef<unsigned> DefedRegsInCopy, |
| 1017 | const TargetRegisterInfo *TRI) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1018 | MachineBasicBlock *SingleBB = nullptr; |
| 1019 | for (auto DefReg : DefedRegsInCopy) { |
| 1020 | MachineBasicBlock *BB = |
| 1021 | getSingleLiveInSuccBB(CurBB, SinkableBBs, DefReg, TRI); |
| 1022 | if (!BB || (SingleBB && SingleBB != BB)) |
| 1023 | return nullptr; |
| 1024 | SingleBB = BB; |
| 1025 | } |
| 1026 | return SingleBB; |
| 1027 | } |
| 1028 | |
| 1029 | static void clearKillFlags(MachineInstr *MI, MachineBasicBlock &CurBB, |
| 1030 | SmallVectorImpl<unsigned> &UsedOpsInCopy, |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1031 | LiveRegUnits &UsedRegUnits, |
| 1032 | const TargetRegisterInfo *TRI) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1033 | for (auto U : UsedOpsInCopy) { |
| 1034 | MachineOperand &MO = MI->getOperand(U); |
| 1035 | unsigned SrcReg = MO.getReg(); |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1036 | if (!UsedRegUnits.available(SrcReg)) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1037 | MachineBasicBlock::iterator NI = std::next(MI->getIterator()); |
| 1038 | for (MachineInstr &UI : make_range(NI, CurBB.end())) { |
| 1039 | if (UI.killsRegister(SrcReg, TRI)) { |
| 1040 | UI.clearRegisterKills(SrcReg, TRI); |
| 1041 | MO.setIsKill(true); |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | static void updateLiveIn(MachineInstr *MI, MachineBasicBlock *SuccBB, |
| 1050 | SmallVectorImpl<unsigned> &UsedOpsInCopy, |
| 1051 | SmallVectorImpl<unsigned> &DefedRegsInCopy) { |
Krzysztof Parzyszek | 00a938c | 2018-09-18 16:10:51 +0000 | [diff] [blame] | 1052 | MachineFunction &MF = *SuccBB->getParent(); |
| 1053 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 1054 | for (unsigned DefReg : DefedRegsInCopy) |
| 1055 | for (MCSubRegIterator S(DefReg, TRI, true); S.isValid(); ++S) |
| 1056 | SuccBB->removeLiveIn(*S); |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1057 | for (auto U : UsedOpsInCopy) { |
| 1058 | unsigned Reg = MI->getOperand(U).getReg(); |
| 1059 | if (!SuccBB->isLiveIn(Reg)) |
| 1060 | SuccBB->addLiveIn(Reg); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | static bool hasRegisterDependency(MachineInstr *MI, |
| 1065 | SmallVectorImpl<unsigned> &UsedOpsInCopy, |
| 1066 | SmallVectorImpl<unsigned> &DefedRegsInCopy, |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1067 | LiveRegUnits &ModifiedRegUnits, |
| 1068 | LiveRegUnits &UsedRegUnits) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1069 | bool HasRegDependency = false; |
| 1070 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1071 | MachineOperand &MO = MI->getOperand(i); |
| 1072 | if (!MO.isReg()) |
| 1073 | continue; |
| 1074 | unsigned Reg = MO.getReg(); |
| 1075 | if (!Reg) |
| 1076 | continue; |
| 1077 | if (MO.isDef()) { |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1078 | if (!ModifiedRegUnits.available(Reg) || !UsedRegUnits.available(Reg)) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1079 | HasRegDependency = true; |
| 1080 | break; |
| 1081 | } |
| 1082 | DefedRegsInCopy.push_back(Reg); |
| 1083 | |
| 1084 | // FIXME: instead of isUse(), readsReg() would be a better fix here, |
| 1085 | // For example, we can ignore modifications in reg with undef. However, |
| 1086 | // it's not perfectly clear if skipping the internal read is safe in all |
| 1087 | // other targets. |
| 1088 | } else if (MO.isUse()) { |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1089 | if (!ModifiedRegUnits.available(Reg)) { |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1090 | HasRegDependency = true; |
| 1091 | break; |
| 1092 | } |
| 1093 | UsedOpsInCopy.push_back(i); |
| 1094 | } |
| 1095 | } |
| 1096 | return HasRegDependency; |
| 1097 | } |
| 1098 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1099 | bool PostRAMachineSinking::tryToSinkCopy(MachineBasicBlock &CurBB, |
| 1100 | MachineFunction &MF, |
| 1101 | const TargetRegisterInfo *TRI, |
| 1102 | const TargetInstrInfo *TII) { |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 1103 | SmallPtrSet<MachineBasicBlock *, 2> SinkableBBs; |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1104 | // FIXME: For now, we sink only to a successor which has a single predecessor |
| 1105 | // so that we can directly sink COPY instructions to the successor without |
| 1106 | // adding any new block or branch instruction. |
| 1107 | for (MachineBasicBlock *SI : CurBB.successors()) |
| 1108 | if (!SI->livein_empty() && SI->pred_size() == 1) |
Jun Bum Lim | e76c819 | 2018-04-27 19:59:20 +0000 | [diff] [blame] | 1109 | SinkableBBs.insert(SI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (SinkableBBs.empty()) |
| 1112 | return false; |
| 1113 | |
| 1114 | bool Changed = false; |
| 1115 | |
| 1116 | // Track which registers have been modified and used between the end of the |
| 1117 | // block and the current instruction. |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1118 | ModifiedRegUnits.clear(); |
| 1119 | UsedRegUnits.clear(); |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 1120 | SeenDbgInstrs.clear(); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1121 | |
| 1122 | for (auto I = CurBB.rbegin(), E = CurBB.rend(); I != E;) { |
| 1123 | MachineInstr *MI = &*I; |
| 1124 | ++I; |
| 1125 | |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 1126 | // Track the operand index for use in Copy. |
| 1127 | SmallVector<unsigned, 2> UsedOpsInCopy; |
| 1128 | // Track the register number defed in Copy. |
| 1129 | SmallVector<unsigned, 2> DefedRegsInCopy; |
| 1130 | |
| 1131 | // We must sink this DBG_VALUE if its operand is sunk. To avoid searching |
| 1132 | // for DBG_VALUEs later, record them when they're encountered. |
| 1133 | if (MI->isDebugValue()) { |
| 1134 | auto &MO = MI->getOperand(0); |
| 1135 | if (MO.isReg() && TRI->isPhysicalRegister(MO.getReg())) { |
| 1136 | // Bail if we can already tell the sink would be rejected, rather |
| 1137 | // than needlessly accumulating lots of DBG_VALUEs. |
| 1138 | if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy, |
| 1139 | ModifiedRegUnits, UsedRegUnits)) |
| 1140 | continue; |
| 1141 | |
| 1142 | // Record debug use of this register. |
| 1143 | SeenDbgInstrs[MO.getReg()].push_back(MI); |
| 1144 | } |
| 1145 | continue; |
| 1146 | } |
| 1147 | |
Matt Davis | 50feec5 | 2018-06-21 17:59:52 +0000 | [diff] [blame] | 1148 | if (MI->isDebugInstr()) |
| 1149 | continue; |
| 1150 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1151 | // Do not move any instruction across function call. |
| 1152 | if (MI->isCall()) |
| 1153 | return false; |
| 1154 | |
| 1155 | if (!MI->isCopy() || !MI->getOperand(0).isRenamable()) { |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1156 | LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits, |
| 1157 | TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1158 | continue; |
| 1159 | } |
| 1160 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1161 | // Don't sink the COPY if it would violate a register dependency. |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1162 | if (hasRegisterDependency(MI, UsedOpsInCopy, DefedRegsInCopy, |
| 1163 | ModifiedRegUnits, UsedRegUnits)) { |
| 1164 | LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits, |
| 1165 | TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1166 | continue; |
| 1167 | } |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1168 | assert((!UsedOpsInCopy.empty() && !DefedRegsInCopy.empty()) && |
| 1169 | "Unexpect SrcReg or DefReg"); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1170 | MachineBasicBlock *SuccBB = |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1171 | getSingleLiveInSuccBB(CurBB, SinkableBBs, DefedRegsInCopy, TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1172 | // Don't sink if we cannot find a single sinkable successor in which Reg |
| 1173 | // is live-in. |
| 1174 | if (!SuccBB) { |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1175 | LiveRegUnits::accumulateUsedDefed(*MI, ModifiedRegUnits, UsedRegUnits, |
| 1176 | TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1177 | continue; |
| 1178 | } |
| 1179 | assert((SuccBB->pred_size() == 1 && *SuccBB->pred_begin() == &CurBB) && |
| 1180 | "Unexpected predecessor"); |
| 1181 | |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 1182 | // Collect DBG_VALUEs that must sink with this copy. |
| 1183 | SmallVector<MachineInstr *, 4> DbgValsToSink; |
| 1184 | for (auto &MO : MI->operands()) { |
| 1185 | if (!MO.isReg() || !MO.isDef()) |
| 1186 | continue; |
| 1187 | unsigned reg = MO.getReg(); |
| 1188 | for (auto *MI : SeenDbgInstrs.lookup(reg)) |
| 1189 | DbgValsToSink.push_back(MI); |
| 1190 | } |
| 1191 | |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1192 | // Clear the kill flag if SrcReg is killed between MI and the end of the |
| 1193 | // block. |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1194 | clearKillFlags(MI, CurBB, UsedOpsInCopy, UsedRegUnits, TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1195 | MachineBasicBlock::iterator InsertPos = SuccBB->getFirstNonPHI(); |
Jeremy Morse | 2033b5a | 2018-11-02 16:52:48 +0000 | [diff] [blame] | 1196 | performSink(*MI, *SuccBB, InsertPos, &DbgValsToSink); |
Jun Bum Lim | 13bac7c | 2018-04-13 14:23:09 +0000 | [diff] [blame] | 1197 | updateLiveIn(MI, SuccBB, UsedOpsInCopy, DefedRegsInCopy); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1198 | |
| 1199 | Changed = true; |
| 1200 | ++NumPostRACopySink; |
| 1201 | } |
| 1202 | return Changed; |
| 1203 | } |
| 1204 | |
| 1205 | bool PostRAMachineSinking::runOnMachineFunction(MachineFunction &MF) { |
| 1206 | bool Changed = false; |
| 1207 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
| 1208 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1209 | |
Jun Bum Lim | a7632db | 2018-04-27 18:44:37 +0000 | [diff] [blame] | 1210 | ModifiedRegUnits.init(*TRI); |
| 1211 | UsedRegUnits.init(*TRI); |
Jun Bum Lim | af56683 | 2018-03-22 20:06:47 +0000 | [diff] [blame] | 1212 | for (auto &BB : MF) |
| 1213 | Changed |= tryToSinkCopy(BB, MF, TRI, TII); |
| 1214 | |
| 1215 | return Changed; |
| 1216 | } |