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