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