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