Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 1 | //===- MachineCSE.cpp - Machine Common Subexpression Elimination Pass -----===// |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass performs global common subexpression elimination on machine |
Evan Cheng | 10194a4 | 2010-03-02 19:02:27 +0000 | [diff] [blame] | 10 | // instructions using a scoped hash table based value numbering scheme. It |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 11 | // must be run while the machine function is still in SSA form. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ScopedHashTable.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CFG.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 34 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 36 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCInstrDesc.h" |
| 38 | #include "llvm/MC/MCRegisterInfo.h" |
| 39 | #include "llvm/Pass.h" |
| 40 | #include "llvm/Support/Allocator.h" |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Cameron Zwarich | 18f164f | 2011-01-03 04:07:46 +0000 | [diff] [blame] | 42 | #include "llvm/Support/RecyclingAllocator.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 44 | #include <cassert> |
| 45 | #include <iterator> |
| 46 | #include <utility> |
| 47 | #include <vector> |
| 48 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 49 | using namespace llvm; |
| 50 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "machine-cse" |
| 52 | |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 53 | STATISTIC(NumCoalesces, "Number of copies coalesced"); |
| 54 | STATISTIC(NumCSEs, "Number of common subexpression eliminated"); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 55 | STATISTIC(NumPREs, "Number of partial redundant expression" |
| 56 | " transformed to fully redundant"); |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 57 | STATISTIC(NumPhysCSEs, |
| 58 | "Number of physreg referencing common subexpr eliminated"); |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 59 | STATISTIC(NumCrossBBCSEs, |
| 60 | "Number of cross-MBB physreg referencing CS eliminated"); |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 61 | STATISTIC(NumCommutes, "Number of copies coalesced after commuting"); |
Bob Wilson | 30093b5 | 2010-06-03 18:28:31 +0000 | [diff] [blame] | 62 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 63 | namespace { |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 64 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 65 | class MachineCSE : public MachineFunctionPass { |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 66 | const TargetInstrInfo *TII; |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 67 | const TargetRegisterInfo *TRI; |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 68 | AliasAnalysis *AA; |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 69 | MachineDominatorTree *DT; |
| 70 | MachineRegisterInfo *MRI; |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 71 | MachineBlockFrequencyInfo *MBFI; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 72 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 73 | public: |
| 74 | static char ID; // Pass identification |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 75 | |
| 76 | MachineCSE() : MachineFunctionPass(ID) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 77 | initializeMachineCSEPass(*PassRegistry::getPassRegistry()); |
| 78 | } |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 79 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 80 | bool runOnMachineFunction(MachineFunction &MF) override; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 81 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 82 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 83 | AU.setPreservesCFG(); |
| 84 | MachineFunctionPass::getAnalysisUsage(AU); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 85 | AU.addRequired<AAResultsWrapperPass>(); |
Evan Cheng | e0db9d0 | 2010-08-17 20:57:42 +0000 | [diff] [blame] | 86 | AU.addPreservedID(MachineLoopInfoID); |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 87 | AU.addRequired<MachineDominatorTree>(); |
| 88 | AU.addPreserved<MachineDominatorTree>(); |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 89 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 90 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 93 | void releaseMemory() override { |
Evan Cheng | b08377e | 2010-09-17 21:59:42 +0000 | [diff] [blame] | 94 | ScopeMap.clear(); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 95 | PREMap.clear(); |
Evan Cheng | b08377e | 2010-09-17 21:59:42 +0000 | [diff] [blame] | 96 | Exps.clear(); |
| 97 | } |
| 98 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 99 | private: |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 100 | using AllocatorTy = RecyclingAllocator<BumpPtrAllocator, |
| 101 | ScopedHashTableVal<MachineInstr *, unsigned>>; |
| 102 | using ScopedHTType = |
| 103 | ScopedHashTable<MachineInstr *, unsigned, MachineInstrExpressionTrait, |
| 104 | AllocatorTy>; |
| 105 | using ScopeType = ScopedHTType::ScopeTy; |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 106 | using PhysDefVector = SmallVector<std::pair<unsigned, unsigned>, 2>; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 107 | |
| 108 | unsigned LookAheadLimit = 0; |
| 109 | DenseMap<MachineBasicBlock *, ScopeType *> ScopeMap; |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 110 | DenseMap<MachineInstr *, MachineBasicBlock *, MachineInstrExpressionTrait> |
| 111 | PREMap; |
Cameron Zwarich | 18f164f | 2011-01-03 04:07:46 +0000 | [diff] [blame] | 112 | ScopedHTType VNT; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 113 | SmallVector<MachineInstr *, 64> Exps; |
| 114 | unsigned CurrVN = 0; |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 115 | |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 116 | bool PerformTrivialCopyPropagation(MachineInstr *MI, |
| 117 | MachineBasicBlock *MBB); |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 118 | bool isPhysDefTriviallyDead(unsigned Reg, |
| 119 | MachineBasicBlock::const_iterator I, |
Nick Lewycky | 765c699 | 2012-07-05 06:19:21 +0000 | [diff] [blame] | 120 | MachineBasicBlock::const_iterator E) const; |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 121 | bool hasLivePhysRegDefUses(const MachineInstr *MI, |
| 122 | const MachineBasicBlock *MBB, |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 123 | SmallSet<unsigned, 8> &PhysRefs, |
| 124 | PhysDefVector &PhysDefs, bool &PhysUseDef) const; |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 125 | bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI, |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 126 | SmallSet<unsigned, 8> &PhysRefs, |
| 127 | PhysDefVector &PhysDefs, bool &NonLocal) const; |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 128 | bool isCSECandidate(MachineInstr *MI); |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 129 | bool isProfitableToCSE(unsigned CSReg, unsigned Reg, |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 130 | MachineBasicBlock *CSBB, MachineInstr *MI); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 131 | void EnterScope(MachineBasicBlock *MBB); |
| 132 | void ExitScope(MachineBasicBlock *MBB); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 133 | bool ProcessBlockCSE(MachineBasicBlock *MBB); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 134 | void ExitScopeIfDone(MachineDomTreeNode *Node, |
Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 135 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 136 | bool PerformCSE(MachineDomTreeNode *Node); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 137 | |
| 138 | bool isPRECandidate(MachineInstr *MI); |
| 139 | bool ProcessBlockPRE(MachineDominatorTree *MDT, MachineBasicBlock *MBB); |
| 140 | bool PerformSimplePRE(MachineDominatorTree *DT); |
Kai Luo | 02b8056 | 2019-08-07 05:40:21 +0000 | [diff] [blame] | 141 | /// Heuristics to see if it's profitable to move common computations of MBB |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 142 | /// and MBB1 to CandidateBB. |
Kai Luo | 02b8056 | 2019-08-07 05:40:21 +0000 | [diff] [blame] | 143 | bool isProfitableToHoistInto(MachineBasicBlock *CandidateBB, |
| 144 | MachineBasicBlock *MBB, |
| 145 | MachineBasicBlock *MBB1); |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 146 | }; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 147 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 148 | } // end anonymous namespace |
| 149 | |
| 150 | char MachineCSE::ID = 0; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 151 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 152 | char &llvm::MachineCSEID = MachineCSE::ID; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 153 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 154 | INITIALIZE_PASS_BEGIN(MachineCSE, DEBUG_TYPE, |
| 155 | "Machine Common Subexpression Elimination", false, false) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 156 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 157 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 158 | INITIALIZE_PASS_END(MachineCSE, DEBUG_TYPE, |
| 159 | "Machine Common Subexpression Elimination", false, false) |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 160 | |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 161 | /// The source register of a COPY machine instruction can be propagated to all |
| 162 | /// its users, and this propagation could increase the probability of finding |
| 163 | /// common subexpressions. If the COPY has only one user, the COPY itself can |
| 164 | /// be removed. |
| 165 | bool MachineCSE::PerformTrivialCopyPropagation(MachineInstr *MI, |
| 166 | MachineBasicBlock *MBB) { |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 167 | bool Changed = false; |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 168 | for (MachineOperand &MO : MI->operands()) { |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 169 | if (!MO.isReg() || !MO.isUse()) |
| 170 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 171 | Register Reg = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 172 | if (!Register::isVirtualRegister(Reg)) |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 173 | continue; |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 174 | bool OnlyOneUse = MRI->hasOneNonDBGUse(Reg); |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 175 | MachineInstr *DefMI = MRI->getVRegDef(Reg); |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 176 | if (!DefMI->isCopy()) |
| 177 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 178 | Register SrcReg = DefMI->getOperand(1).getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 179 | if (!Register::isVirtualRegister(SrcReg)) |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 180 | continue; |
Andrew Trick | e339828 | 2013-12-17 04:50:45 +0000 | [diff] [blame] | 181 | if (DefMI->getOperand(0).getSubReg()) |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 182 | continue; |
Andrew Trick | e4083f9 | 2013-12-17 19:29:36 +0000 | [diff] [blame] | 183 | // FIXME: We should trivially coalesce subregister copies to expose CSE |
| 184 | // opportunities on instructions with truncated operands (see |
| 185 | // cse-add-with-overflow.ll). This can be done here as follows: |
| 186 | // if (SrcSubReg) |
| 187 | // RC = TRI->getMatchingSuperRegClass(MRI->getRegClass(SrcReg), RC, |
| 188 | // SrcSubReg); |
| 189 | // MO.substVirtReg(SrcReg, SrcSubReg, *TRI); |
| 190 | // |
| 191 | // The 2-addr pass has been updated to handle coalesced subregs. However, |
| 192 | // some machine-specific code still can't handle it. |
| 193 | // To handle it properly we also need a way find a constrained subregister |
| 194 | // class given a super-reg class and subreg index. |
| 195 | if (DefMI->getOperand(1).getSubReg()) |
| 196 | continue; |
Justin Bogner | a9346e0 | 2018-01-18 02:06:56 +0000 | [diff] [blame] | 197 | if (!MRI->constrainRegAttrs(SrcReg, Reg)) |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 198 | continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 199 | LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI); |
| 200 | LLVM_DEBUG(dbgs() << "*** to: " << *MI); |
Carlos Alberto Enciso | 06adfa1 | 2018-08-30 07:17:41 +0000 | [diff] [blame] | 201 | |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 202 | // Propagate SrcReg of copies to MI. |
Andrew Trick | e4083f9 | 2013-12-17 19:29:36 +0000 | [diff] [blame] | 203 | MO.setReg(SrcReg); |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 204 | MRI->clearKillFlags(SrcReg); |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 205 | // Coalesce single use copies. |
| 206 | if (OnlyOneUse) { |
Jeremy Morse | 22493f66 | 2019-09-02 12:28:36 +0000 | [diff] [blame] | 207 | // If (and only if) we've eliminated all uses of the copy, also |
| 208 | // copy-propagate to any debug-users of MI, or they'll be left using |
| 209 | // an undefined value. |
| 210 | DefMI->changeDebugValuesDefReg(SrcReg); |
| 211 | |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 212 | DefMI->eraseFromParent(); |
| 213 | ++NumCoalesces; |
| 214 | } |
Jakob Stoklund Olesen | 0026462 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 215 | Changed = true; |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return Changed; |
| 219 | } |
| 220 | |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 221 | bool |
| 222 | MachineCSE::isPhysDefTriviallyDead(unsigned Reg, |
| 223 | MachineBasicBlock::const_iterator I, |
| 224 | MachineBasicBlock::const_iterator E) const { |
Eric Christopher | 53ff992 | 2010-05-21 23:40:03 +0000 | [diff] [blame] | 225 | unsigned LookAheadLeft = LookAheadLimit; |
Evan Cheng | c7d721a | 2010-03-23 20:33:48 +0000 | [diff] [blame] | 226 | while (LookAheadLeft) { |
Evan Cheng | cf7be39 | 2010-03-24 01:50:28 +0000 | [diff] [blame] | 227 | // Skip over dbg_value's. |
Florian Hahn | 3c8b8c9 | 2016-12-16 11:10:26 +0000 | [diff] [blame] | 228 | I = skipDebugInstructionsForward(I, E); |
Evan Cheng | cf7be39 | 2010-03-24 01:50:28 +0000 | [diff] [blame] | 229 | |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 230 | if (I == E) |
Mikael Holmen | 2676f82 | 2017-05-24 09:35:23 +0000 | [diff] [blame] | 231 | // Reached end of block, we don't know if register is dead or not. |
| 232 | return false; |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 233 | |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 234 | bool SeenDef = false; |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 235 | for (const MachineOperand &MO : I->operands()) { |
Jakob Stoklund Olesen | 4c5ad2b | 2012-02-28 02:08:50 +0000 | [diff] [blame] | 236 | if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) |
| 237 | SeenDef = true; |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 238 | if (!MO.isReg() || !MO.getReg()) |
| 239 | continue; |
| 240 | if (!TRI->regsOverlap(MO.getReg(), Reg)) |
| 241 | continue; |
| 242 | if (MO.isUse()) |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 243 | // Found a use! |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 244 | return false; |
| 245 | SeenDef = true; |
| 246 | } |
| 247 | if (SeenDef) |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 248 | // See a def of Reg (or an alias) before encountering any use, it's |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 249 | // trivially dead. |
| 250 | return true; |
Evan Cheng | c7d721a | 2010-03-23 20:33:48 +0000 | [diff] [blame] | 251 | |
| 252 | --LookAheadLeft; |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 253 | ++I; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
Roman Tereshin | 8d6ff4c | 2018-10-20 00:06:15 +0000 | [diff] [blame] | 258 | static bool isCallerPreservedOrConstPhysReg(unsigned Reg, |
| 259 | const MachineFunction &MF, |
| 260 | const TargetRegisterInfo &TRI) { |
| 261 | // MachineRegisterInfo::isConstantPhysReg directly called by |
| 262 | // MachineRegisterInfo::isCallerPreservedOrConstPhysReg expects the |
| 263 | // reserved registers to be frozen. That doesn't cause a problem post-ISel as |
| 264 | // most (if not all) targets freeze reserved registers right after ISel. |
| 265 | // |
| 266 | // It does cause issues mid-GlobalISel, however, hence the additional |
| 267 | // reservedRegsFrozen check. |
| 268 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 269 | return TRI.isCallerPreservedPhysReg(Reg, MF) || |
| 270 | (MRI.reservedRegsFrozen() && MRI.isConstantPhysReg(Reg)); |
| 271 | } |
| 272 | |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 273 | /// hasLivePhysRegDefUses - Return true if the specified instruction read/write |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 274 | /// physical registers (except for dead defs of physical registers). It also |
Evan Cheng | a03e6f8 | 2010-06-04 23:28:13 +0000 | [diff] [blame] | 275 | /// returns the physical register def by reference if it's the only one and the |
| 276 | /// instruction does not uses a physical register. |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 277 | bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI, |
| 278 | const MachineBasicBlock *MBB, |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 279 | SmallSet<unsigned, 8> &PhysRefs, |
| 280 | PhysDefVector &PhysDefs, |
| 281 | bool &PhysUseDef) const { |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 282 | // First, add all uses to PhysRefs. |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 283 | for (const MachineOperand &MO : MI->operands()) { |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 284 | if (!MO.isReg() || MO.isDef()) |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 285 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 286 | Register Reg = MO.getReg(); |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 287 | if (!Reg) |
| 288 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 289 | if (Register::isVirtualRegister(Reg)) |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 290 | continue; |
Tony Jiang | f75f4d6 | 2017-11-20 16:55:07 +0000 | [diff] [blame] | 291 | // Reading either caller preserved or constant physregs is ok. |
Roman Tereshin | 8d6ff4c | 2018-10-20 00:06:15 +0000 | [diff] [blame] | 292 | if (!isCallerPreservedOrConstPhysReg(Reg, *MI->getMF(), *TRI)) |
Benjamin Kramer | 59c8b41 | 2012-08-11 20:42:59 +0000 | [diff] [blame] | 293 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
Benjamin Kramer | ef6494f | 2012-08-11 19:05:13 +0000 | [diff] [blame] | 294 | PhysRefs.insert(*AI); |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Next, collect all defs into PhysDefs. If any is already in PhysRefs |
| 298 | // (which currently contains only uses), set the PhysUseDef flag. |
| 299 | PhysUseDef = false; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 300 | MachineBasicBlock::const_iterator I = MI; I = std::next(I); |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 301 | for (const auto &MOP : llvm::enumerate(MI->operands())) { |
| 302 | const MachineOperand &MO = MOP.value(); |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 303 | if (!MO.isReg() || !MO.isDef()) |
| 304 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 305 | Register Reg = MO.getReg(); |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 306 | if (!Reg) |
| 307 | continue; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 308 | if (Register::isVirtualRegister(Reg)) |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 309 | continue; |
| 310 | // Check against PhysRefs even if the def is "dead". |
| 311 | if (PhysRefs.count(Reg)) |
| 312 | PhysUseDef = true; |
| 313 | // If the def is dead, it's ok. But the def may not marked "dead". That's |
| 314 | // common since this pass is run before livevariables. We can scan |
| 315 | // forward a few instructions and check if it is obviously dead. |
| 316 | if (!MO.isDead() && !isPhysDefTriviallyDead(Reg, I, MBB->end())) |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 317 | PhysDefs.push_back(std::make_pair(MOP.index(), Reg)); |
Evan Cheng | 36f8aab | 2010-03-04 01:33:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 320 | // Finally, add all defs to PhysRefs as well. |
| 321 | for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 322 | for (MCRegAliasIterator AI(PhysDefs[i].second, TRI, true); AI.isValid(); |
| 323 | ++AI) |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 324 | PhysRefs.insert(*AI); |
| 325 | |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 326 | return !PhysRefs.empty(); |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 329 | bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI, |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 330 | SmallSet<unsigned, 8> &PhysRefs, |
| 331 | PhysDefVector &PhysDefs, |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 332 | bool &NonLocal) const { |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 333 | // For now conservatively returns false if the common subexpression is |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 334 | // not in the same basic block as the given instruction. The only exception |
| 335 | // is if the common subexpression is in the sole predecessor block. |
| 336 | const MachineBasicBlock *MBB = MI->getParent(); |
| 337 | const MachineBasicBlock *CSMBB = CSMI->getParent(); |
| 338 | |
| 339 | bool CrossMBB = false; |
| 340 | if (CSMBB != MBB) { |
Evan Cheng | d9725a3 | 2012-01-11 00:38:11 +0000 | [diff] [blame] | 341 | if (MBB->pred_size() != 1 || *MBB->pred_begin() != CSMBB) |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 342 | return false; |
Evan Cheng | d9725a3 | 2012-01-11 00:38:11 +0000 | [diff] [blame] | 343 | |
| 344 | for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) { |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 345 | if (MRI->isAllocatable(PhysDefs[i].second) || |
| 346 | MRI->isReserved(PhysDefs[i].second)) |
Lang Hames | 5bade3d | 2012-02-17 00:27:16 +0000 | [diff] [blame] | 347 | // Avoid extending live range of physical registers if they are |
| 348 | //allocatable or reserved. |
Evan Cheng | d9725a3 | 2012-01-11 00:38:11 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | CrossMBB = true; |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 352 | } |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 353 | MachineBasicBlock::const_iterator I = CSMI; I = std::next(I); |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 354 | MachineBasicBlock::const_iterator E = MI; |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 355 | MachineBasicBlock::const_iterator EE = CSMBB->end(); |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 356 | unsigned LookAheadLeft = LookAheadLimit; |
| 357 | while (LookAheadLeft) { |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 358 | // Skip over dbg_value's. |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 359 | while (I != E && I != EE && I->isDebugInstr()) |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 360 | ++I; |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 361 | |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 362 | if (I == EE) { |
| 363 | assert(CrossMBB && "Reaching end-of-MBB without finding MI?"); |
Duncan Sands | ae22c60 | 2012-02-05 14:20:11 +0000 | [diff] [blame] | 364 | (void)CrossMBB; |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 365 | CrossMBB = false; |
| 366 | NonLocal = true; |
| 367 | I = MBB->begin(); |
| 368 | EE = MBB->end(); |
| 369 | continue; |
| 370 | } |
| 371 | |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 372 | if (I == E) |
| 373 | return true; |
| 374 | |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 375 | for (const MachineOperand &MO : I->operands()) { |
Jakob Stoklund Olesen | 4c5ad2b | 2012-02-28 02:08:50 +0000 | [diff] [blame] | 376 | // RegMasks go on instructions like calls that clobber lots of physregs. |
| 377 | // Don't attempt to CSE across such an instruction. |
| 378 | if (MO.isRegMask()) |
| 379 | return false; |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 380 | if (!MO.isReg() || !MO.isDef()) |
| 381 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 382 | Register MOReg = MO.getReg(); |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 383 | if (Register::isVirtualRegister(MOReg)) |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 384 | continue; |
| 385 | if (PhysRefs.count(MOReg)) |
| 386 | return false; |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 387 | } |
Eli Friedman | 5401962 | 2011-05-06 05:23:07 +0000 | [diff] [blame] | 388 | |
| 389 | --LookAheadLeft; |
| 390 | ++I; |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return false; |
| 394 | } |
| 395 | |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 396 | bool MachineCSE::isCSECandidate(MachineInstr *MI) { |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 397 | if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() || |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 398 | MI->isInlineAsm() || MI->isDebugInstr()) |
Evan Cheng | c9e8621 | 2010-03-08 23:49:12 +0000 | [diff] [blame] | 399 | return false; |
| 400 | |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 401 | // Ignore copies. |
Jakob Stoklund Olesen | 37c42a3 | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 402 | if (MI->isCopyLike()) |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 403 | return false; |
| 404 | |
| 405 | // Ignore stuff that we obviously can't move. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 406 | if (MI->mayStore() || MI->isCall() || MI->isTerminator() || |
Ulrich Weigand | 6c5d5ce | 2019-06-05 22:33:10 +0000 | [diff] [blame] | 407 | MI->mayRaiseFPException() || MI->hasUnmodeledSideEffects()) |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 408 | return false; |
| 409 | |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 410 | if (MI->mayLoad()) { |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 411 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 412 | // to decide whether the loaded value is actually a constant. If so, we can |
| 413 | // actually use it as a load. |
Justin Lebar | d98cf00 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 414 | if (!MI->isDereferenceableInvariantLoad(AA)) |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 415 | // FIXME: we should be able to hoist loads with no other side effects if |
| 416 | // there are no other instructions which can change memory in this loop. |
| 417 | // This is a trivial form of alias analysis. |
| 418 | return false; |
| 419 | } |
Tim Shen | e885d5e | 2016-04-19 19:40:37 +0000 | [diff] [blame] | 420 | |
| 421 | // Ignore stack guard loads, otherwise the register that holds CSEed value may |
| 422 | // be spilled and get loaded back with corrupted data. |
| 423 | if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD) |
| 424 | return false; |
| 425 | |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 429 | /// isProfitableToCSE - Return true if it's profitable to eliminate MI with a |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 430 | /// common expression that defines Reg. CSBB is basic block where CSReg is |
| 431 | /// defined. |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 432 | bool MachineCSE::isProfitableToCSE(unsigned CSReg, unsigned Reg, |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 433 | MachineBasicBlock *CSBB, MachineInstr *MI) { |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 434 | // FIXME: Heuristics that works around the lack the live range splitting. |
| 435 | |
Manman Ren | cb36b8c | 2012-08-07 06:16:46 +0000 | [diff] [blame] | 436 | // If CSReg is used at all uses of Reg, CSE should not increase register |
| 437 | // pressure of CSReg. |
| 438 | bool MayIncreasePressure = true; |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 439 | if (Register::isVirtualRegister(CSReg) && Register::isVirtualRegister(Reg)) { |
Manman Ren | cb36b8c | 2012-08-07 06:16:46 +0000 | [diff] [blame] | 440 | MayIncreasePressure = false; |
| 441 | SmallPtrSet<MachineInstr*, 8> CSUses; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 442 | for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) { |
| 443 | CSUses.insert(&MI); |
Manman Ren | cb36b8c | 2012-08-07 06:16:46 +0000 | [diff] [blame] | 444 | } |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 445 | for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) { |
| 446 | if (!CSUses.count(&MI)) { |
Manman Ren | cb36b8c | 2012-08-07 06:16:46 +0000 | [diff] [blame] | 447 | MayIncreasePressure = true; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | if (!MayIncreasePressure) return true; |
| 453 | |
Chris Lattner | 6c8b8dd | 2011-01-10 07:51:31 +0000 | [diff] [blame] | 454 | // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in |
| 455 | // an immediate predecessor. We don't want to increase register pressure and |
| 456 | // end up causing other computation to be spilled. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 457 | if (TII->isAsCheapAsAMove(*MI)) { |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 458 | MachineBasicBlock *BB = MI->getParent(); |
Chris Lattner | 6c8b8dd | 2011-01-10 07:51:31 +0000 | [diff] [blame] | 459 | if (CSBB != BB && !CSBB->isSuccessor(BB)) |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 460 | return false; |
| 461 | } |
| 462 | |
| 463 | // Heuristics #2: If the expression doesn't not use a vr and the only use |
| 464 | // of the redundant computation are copies, do not cse. |
| 465 | bool HasVRegUse = false; |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 466 | for (const MachineOperand &MO : MI->operands()) { |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 467 | if (MO.isReg() && MO.isUse() && Register::isVirtualRegister(MO.getReg())) { |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 468 | HasVRegUse = true; |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | if (!HasVRegUse) { |
| 473 | bool HasNonCopyUse = false; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 474 | for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) { |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 475 | // Ignore copies. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 476 | if (!MI.isCopyLike()) { |
Evan Cheng | 4c5f7a7 | 2010-03-10 02:12:03 +0000 | [diff] [blame] | 477 | HasNonCopyUse = true; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | if (!HasNonCopyUse) |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | // Heuristics #3: If the common subexpression is used by PHIs, do not reuse |
| 486 | // it unless the defined value is already used in the BB of the new use. |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 487 | bool HasPHI = false; |
Michael Zolotukhin | 131e749 | 2018-05-04 01:40:05 +0000 | [diff] [blame] | 488 | for (MachineInstr &UseMI : MRI->use_nodbg_instructions(CSReg)) { |
| 489 | HasPHI |= UseMI.isPHI(); |
| 490 | if (UseMI.getParent() == MI->getParent()) |
| 491 | return true; |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Michael Zolotukhin | 131e749 | 2018-05-04 01:40:05 +0000 | [diff] [blame] | 494 | return !HasPHI; |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 497 | void MachineCSE::EnterScope(MachineBasicBlock *MBB) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 498 | LLVM_DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n'); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 499 | ScopeType *Scope = new ScopeType(VNT); |
| 500 | ScopeMap[MBB] = Scope; |
| 501 | } |
| 502 | |
| 503 | void MachineCSE::ExitScope(MachineBasicBlock *MBB) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 504 | LLVM_DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n'); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 505 | DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB); |
| 506 | assert(SI != ScopeMap.end()); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 507 | delete SI->second; |
Jakub Staszak | f18753b | 2012-11-26 22:14:19 +0000 | [diff] [blame] | 508 | ScopeMap.erase(SI); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 511 | bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) { |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 512 | bool Changed = false; |
| 513 | |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 514 | SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs; |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 515 | SmallVector<unsigned, 2> ImplicitDefsToUpdate; |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 516 | SmallVector<unsigned, 2> ImplicitDefs; |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 517 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) { |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 518 | MachineInstr *MI = &*I; |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 519 | ++I; |
Evan Cheng | 1abd1a9 | 2010-03-04 21:18:08 +0000 | [diff] [blame] | 520 | |
| 521 | if (!isCSECandidate(MI)) |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 522 | continue; |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 523 | |
| 524 | bool FoundCSE = VNT.count(MI); |
| 525 | if (!FoundCSE) { |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 526 | // Using trivial copy propagation to find more CSE opportunities. |
| 527 | if (PerformTrivialCopyPropagation(MI, MBB)) { |
Evan Cheng | fe917ef | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 528 | Changed = true; |
| 529 | |
Evan Cheng | 604bc16 | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 530 | // After coalescing MI itself may become a copy. |
Jakob Stoklund Olesen | 37c42a3 | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 531 | if (MI->isCopyLike()) |
Evan Cheng | 604bc16 | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 532 | continue; |
Jiangning Liu | dd6e12d | 2014-08-11 05:17:19 +0000 | [diff] [blame] | 533 | |
| 534 | // Try again to see if CSE is possible. |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 535 | FoundCSE = VNT.count(MI); |
Evan Cheng | 604bc16 | 2010-04-02 02:21:24 +0000 | [diff] [blame] | 536 | } |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 537 | } |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 538 | |
| 539 | // Commute commutable instructions. |
| 540 | bool Commuted = false; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 541 | if (!FoundCSE && MI->isCommutable()) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 542 | if (MachineInstr *NewMI = TII->commuteInstruction(*MI)) { |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 543 | Commuted = true; |
| 544 | FoundCSE = VNT.count(NewMI); |
Evan Cheng | fe917ef | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 545 | if (NewMI != MI) { |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 546 | // New instruction. It doesn't need to be kept. |
| 547 | NewMI->eraseFromParent(); |
Evan Cheng | fe917ef | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 548 | Changed = true; |
| 549 | } else if (!FoundCSE) |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 550 | // MI was changed but it didn't help, commute it back! |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 551 | (void)TII->commuteInstruction(*MI); |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 554 | |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 555 | // If the instruction defines physical registers and the values *may* be |
Evan Cheng | 2922641 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 556 | // used, then it's not safe to replace it with a common subexpression. |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 557 | // It's also not safe if the instruction uses physical registers. |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 558 | bool CrossMBBPhysDef = false; |
Nick Lewycky | 765c699 | 2012-07-05 06:19:21 +0000 | [diff] [blame] | 559 | SmallSet<unsigned, 8> PhysRefs; |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 560 | PhysDefVector PhysDefs; |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 561 | bool PhysUseDef = false; |
| 562 | if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs, |
| 563 | PhysDefs, PhysUseDef)) { |
Evan Cheng | 2922641 | 2010-03-03 23:59:08 +0000 | [diff] [blame] | 564 | FoundCSE = false; |
| 565 | |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 566 | // ... Unless the CS is local or is in the sole predecessor block |
| 567 | // and it also defines the physical register which is not clobbered |
| 568 | // in between and the physical register uses were not clobbered. |
Ulrich Weigand | 3946877 | 2012-11-13 18:40:58 +0000 | [diff] [blame] | 569 | // This can never be the case if the instruction both uses and |
| 570 | // defines the same physical register, which was detected above. |
| 571 | if (!PhysUseDef) { |
| 572 | unsigned CSVN = VNT.lookup(MI); |
| 573 | MachineInstr *CSMI = Exps[CSVN]; |
| 574 | if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef)) |
| 575 | FoundCSE = true; |
| 576 | } |
Evan Cheng | 2c8bdea | 2010-05-21 21:22:19 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 579 | if (!FoundCSE) { |
| 580 | VNT.insert(MI, CurrVN++); |
| 581 | Exps.push_back(MI); |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | // Found a common subexpression, eliminate it. |
| 586 | unsigned CSVN = VNT.lookup(MI); |
| 587 | MachineInstr *CSMI = Exps[CSVN]; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 588 | LLVM_DEBUG(dbgs() << "Examining: " << *MI); |
| 589 | LLVM_DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI); |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 590 | |
| 591 | // Check if it's profitable to perform this CSE. |
| 592 | bool DoCSE = true; |
Roman Tereshin | b2d3f2e | 2018-06-12 18:30:37 +0000 | [diff] [blame] | 593 | unsigned NumDefs = MI->getNumDefs(); |
Andrew Trick | cccd82f | 2013-12-16 19:36:18 +0000 | [diff] [blame] | 594 | |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 595 | for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) { |
| 596 | MachineOperand &MO = MI->getOperand(i); |
| 597 | if (!MO.isReg() || !MO.isDef()) |
| 598 | continue; |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 599 | Register OldReg = MO.getReg(); |
| 600 | Register NewReg = CSMI->getOperand(i).getReg(); |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 601 | |
| 602 | // Go through implicit defs of CSMI and MI, if a def is not dead at MI, |
| 603 | // we should make sure it is not dead at CSMI. |
| 604 | if (MO.isImplicit() && !MO.isDead() && CSMI->getOperand(i).isDead()) |
| 605 | ImplicitDefsToUpdate.push_back(i); |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 606 | |
| 607 | // Keep track of implicit defs of CSMI and MI, to clear possibly |
| 608 | // made-redundant kill flags. |
| 609 | if (MO.isImplicit() && !MO.isDead() && OldReg == NewReg) |
| 610 | ImplicitDefs.push_back(OldReg); |
| 611 | |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 612 | if (OldReg == NewReg) { |
| 613 | --NumDefs; |
Evan Cheng | 0f5f547 | 2010-03-06 01:14:19 +0000 | [diff] [blame] | 614 | continue; |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 615 | } |
Bill Wendling | 3e5409d | 2011-10-12 23:03:40 +0000 | [diff] [blame] | 616 | |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 617 | assert(Register::isVirtualRegister(OldReg) && |
| 618 | Register::isVirtualRegister(NewReg) && |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 619 | "Do not CSE physical register defs!"); |
Bill Wendling | 3e5409d | 2011-10-12 23:03:40 +0000 | [diff] [blame] | 620 | |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 621 | if (!isProfitableToCSE(NewReg, OldReg, CSMI->getParent(), MI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 622 | LLVM_DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n"); |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 623 | DoCSE = false; |
| 624 | break; |
| 625 | } |
Bill Wendling | 3e5409d | 2011-10-12 23:03:40 +0000 | [diff] [blame] | 626 | |
Justin Bogner | a9346e0 | 2018-01-18 02:06:56 +0000 | [diff] [blame] | 627 | // Don't perform CSE if the result of the new instruction cannot exist |
| 628 | // within the constraints (register class, bank, or low-level type) of |
| 629 | // the old instruction. |
| 630 | if (!MRI->constrainRegAttrs(NewReg, OldReg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 631 | LLVM_DEBUG( |
| 632 | dbgs() << "*** Not the same register constraints, avoid CSE!\n"); |
Bill Wendling | 3e5409d | 2011-10-12 23:03:40 +0000 | [diff] [blame] | 633 | DoCSE = false; |
| 634 | break; |
| 635 | } |
| 636 | |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 637 | CSEPairs.push_back(std::make_pair(OldReg, NewReg)); |
Evan Cheng | b386cd3 | 2010-03-03 21:20:05 +0000 | [diff] [blame] | 638 | --NumDefs; |
| 639 | } |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 640 | |
| 641 | // Actually perform the elimination. |
| 642 | if (DoCSE) { |
Simon Pilgrim | decc194 | 2020-09-25 22:33:15 +0100 | [diff] [blame] | 643 | for (const std::pair<unsigned, unsigned> &CSEPair : CSEPairs) { |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 644 | unsigned OldReg = CSEPair.first; |
| 645 | unsigned NewReg = CSEPair.second; |
Matthias Braun | 26e7ea6 | 2015-02-04 19:35:16 +0000 | [diff] [blame] | 646 | // OldReg may have been unused but is used now, clear the Dead flag |
| 647 | MachineInstr *Def = MRI->getUniqueVRegDef(NewReg); |
| 648 | assert(Def != nullptr && "CSEd register has no unique definition?"); |
| 649 | Def->clearRegisterDeads(NewReg); |
| 650 | // Replace with NewReg and clear kill flags which may be wrong now. |
| 651 | MRI->replaceRegWith(OldReg, NewReg); |
| 652 | MRI->clearKillFlags(NewReg); |
Dan Gohman | 7767d27 | 2010-05-13 19:24:00 +0000 | [diff] [blame] | 653 | } |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 654 | |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 655 | // Go through implicit defs of CSMI and MI, if a def is not dead at MI, |
| 656 | // we should make sure it is not dead at CSMI. |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 657 | for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate) |
| 658 | CSMI->getOperand(ImplicitDefToUpdate).setIsDead(false); |
Simon Pilgrim | decc194 | 2020-09-25 22:33:15 +0100 | [diff] [blame] | 659 | for (const auto &PhysDef : PhysDefs) |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 660 | if (!MI->getOperand(PhysDef.first).isDead()) |
| 661 | CSMI->getOperand(PhysDef.first).setIsDead(false); |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 662 | |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 663 | // Go through implicit defs of CSMI and MI, and clear the kill flags on |
| 664 | // their uses in all the instructions between CSMI and MI. |
| 665 | // We might have made some of the kill flags redundant, consider: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 666 | // subs ... implicit-def %nzcv <- CSMI |
| 667 | // csinc ... implicit killed %nzcv <- this kill flag isn't valid anymore |
| 668 | // subs ... implicit-def %nzcv <- MI, to be eliminated |
| 669 | // csinc ... implicit killed %nzcv |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 670 | // Since we eliminated MI, and reused a register imp-def'd by CSMI |
Francis Visoiu Mistrih | 9d7bb0c | 2017-11-28 17:15:09 +0000 | [diff] [blame] | 671 | // (here %nzcv), that register, if it was killed before MI, should have |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 672 | // that kill flag removed, because it's lifetime was extended. |
| 673 | if (CSMI->getParent() == MI->getParent()) { |
| 674 | for (MachineBasicBlock::iterator II = CSMI, IE = MI; II != IE; ++II) |
| 675 | for (auto ImplicitDef : ImplicitDefs) |
| 676 | if (MachineOperand *MO = II->findRegisterUseOperand( |
| 677 | ImplicitDef, /*isKill=*/true, TRI)) |
| 678 | MO->setIsKill(false); |
| 679 | } else { |
| 680 | // If the instructions aren't in the same BB, bail out and clear the |
| 681 | // kill flag on all uses of the imp-def'd register. |
| 682 | for (auto ImplicitDef : ImplicitDefs) |
| 683 | MRI->clearKillFlags(ImplicitDef); |
| 684 | } |
| 685 | |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 686 | if (CrossMBBPhysDef) { |
| 687 | // Add physical register defs now coming in from a predecessor to MBB |
| 688 | // livein list. |
| 689 | while (!PhysDefs.empty()) { |
David Green | cb5a48b | 2019-02-20 10:22:18 +0000 | [diff] [blame] | 690 | auto LiveIn = PhysDefs.pop_back_val(); |
| 691 | if (!MBB->isLiveIn(LiveIn.second)) |
| 692 | MBB->addLiveIn(LiveIn.second); |
Evan Cheng | 0be4144 | 2012-01-10 02:02:58 +0000 | [diff] [blame] | 693 | } |
| 694 | ++NumCrossBBCSEs; |
| 695 | } |
| 696 | |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 697 | MI->eraseFromParent(); |
| 698 | ++NumCSEs; |
Evan Cheng | 2b3f25e | 2010-10-29 23:36:03 +0000 | [diff] [blame] | 699 | if (!PhysRefs.empty()) |
Evan Cheng | a03e6f8 | 2010-06-04 23:28:13 +0000 | [diff] [blame] | 700 | ++NumPhysCSEs; |
Evan Cheng | b7ff5a0 | 2010-12-15 22:16:21 +0000 | [diff] [blame] | 701 | if (Commuted) |
| 702 | ++NumCommutes; |
Evan Cheng | fe917ef | 2011-04-11 18:47:20 +0000 | [diff] [blame] | 703 | Changed = true; |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 704 | } else { |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 705 | VNT.insert(MI, CurrVN++); |
| 706 | Exps.push_back(MI); |
| 707 | } |
| 708 | CSEPairs.clear(); |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 709 | ImplicitDefsToUpdate.clear(); |
Ahmed Bougacha | 54b7d33 | 2014-12-02 18:09:51 +0000 | [diff] [blame] | 710 | ImplicitDefs.clear(); |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 713 | return Changed; |
| 714 | } |
| 715 | |
| 716 | /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given |
| 717 | /// dominator tree node if its a leaf or all of its children are done. Walk |
| 718 | /// up the dominator tree to destroy ancestors which are now done. |
| 719 | void |
| 720 | MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node, |
Nick Lewycky | 765c699 | 2012-07-05 06:19:21 +0000 | [diff] [blame] | 721 | DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren) { |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 722 | if (OpenChildren[Node]) |
| 723 | return; |
| 724 | |
| 725 | // Pop scope. |
| 726 | ExitScope(Node->getBlock()); |
| 727 | |
| 728 | // Now traverse upwards to pop ancestors whose offsprings are all done. |
Nick Lewycky | 765c699 | 2012-07-05 06:19:21 +0000 | [diff] [blame] | 729 | while (MachineDomTreeNode *Parent = Node->getIDom()) { |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 730 | unsigned Left = --OpenChildren[Parent]; |
| 731 | if (Left != 0) |
| 732 | break; |
| 733 | ExitScope(Parent->getBlock()); |
| 734 | Node = Parent; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) { |
| 739 | SmallVector<MachineDomTreeNode*, 32> Scopes; |
| 740 | SmallVector<MachineDomTreeNode*, 8> WorkList; |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 741 | DenseMap<MachineDomTreeNode*, unsigned> OpenChildren; |
| 742 | |
Evan Cheng | b08377e | 2010-09-17 21:59:42 +0000 | [diff] [blame] | 743 | CurrVN = 0; |
| 744 | |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 745 | // Perform a DFS walk to determine the order of visit. |
| 746 | WorkList.push_back(Node); |
| 747 | do { |
| 748 | Node = WorkList.pop_back_val(); |
| 749 | Scopes.push_back(Node); |
Nicolai Hähnle | 76c5cb0 | 2020-05-18 16:28:24 +0200 | [diff] [blame] | 750 | OpenChildren[Node] = Node->getNumChildren(); |
| 751 | for (MachineDomTreeNode *Child : Node->children()) |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 752 | WorkList.push_back(Child); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 753 | } while (!WorkList.empty()); |
| 754 | |
| 755 | // Now perform CSE. |
| 756 | bool Changed = false; |
Sanjay Patel | 3d07ec9 | 2016-01-06 00:45:42 +0000 | [diff] [blame] | 757 | for (MachineDomTreeNode *Node : Scopes) { |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 758 | MachineBasicBlock *MBB = Node->getBlock(); |
| 759 | EnterScope(MBB); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 760 | Changed |= ProcessBlockCSE(MBB); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 761 | // If it's a leaf node, it's done. Traverse upwards to pop ancestors. |
Nick Lewycky | 765c699 | 2012-07-05 06:19:21 +0000 | [diff] [blame] | 762 | ExitScopeIfDone(Node, OpenChildren); |
Evan Cheng | 4b2ef56 | 2010-04-21 00:21:07 +0000 | [diff] [blame] | 763 | } |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 764 | |
| 765 | return Changed; |
| 766 | } |
| 767 | |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 768 | // We use stronger checks for PRE candidate rather than for CSE ones to embrace |
| 769 | // checks inside ProcessBlockCSE(), not only inside isCSECandidate(). This helps |
| 770 | // to exclude instrs created by PRE that won't be CSEed later. |
| 771 | bool MachineCSE::isPRECandidate(MachineInstr *MI) { |
| 772 | if (!isCSECandidate(MI) || |
| 773 | MI->isNotDuplicable() || |
| 774 | MI->mayLoad() || |
| 775 | MI->isAsCheapAsAMove() || |
| 776 | MI->getNumDefs() != 1 || |
| 777 | MI->getNumExplicitDefs() != 1) |
| 778 | return false; |
| 779 | |
Simon Pilgrim | ce294ff | 2020-09-21 16:38:44 +0100 | [diff] [blame] | 780 | for (const auto &def : MI->defs()) |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 781 | if (!Register::isVirtualRegister(def.getReg())) |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 782 | return false; |
| 783 | |
Simon Pilgrim | ce294ff | 2020-09-21 16:38:44 +0100 | [diff] [blame] | 784 | for (const auto &use : MI->uses()) |
Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 785 | if (use.isReg() && !Register::isVirtualRegister(use.getReg())) |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 786 | return false; |
| 787 | |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT, |
| 792 | MachineBasicBlock *MBB) { |
| 793 | bool Changed = false; |
| 794 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) { |
| 795 | MachineInstr *MI = &*I; |
| 796 | ++I; |
| 797 | |
| 798 | if (!isPRECandidate(MI)) |
| 799 | continue; |
| 800 | |
| 801 | if (!PREMap.count(MI)) { |
| 802 | PREMap[MI] = MBB; |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | auto MBB1 = PREMap[MI]; |
| 807 | assert( |
| 808 | !DT->properlyDominates(MBB, MBB1) && |
| 809 | "MBB cannot properly dominate MBB1 while DFS through dominators tree!"); |
| 810 | auto CMBB = DT->findNearestCommonDominator(MBB, MBB1); |
Anton Afanasyev | 339b39b | 2019-06-12 13:51:44 +0000 | [diff] [blame] | 811 | if (!CMBB->isLegalToHoistInto()) |
| 812 | continue; |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 813 | |
Kai Luo | 02b8056 | 2019-08-07 05:40:21 +0000 | [diff] [blame] | 814 | if (!isProfitableToHoistInto(CMBB, MBB, MBB1)) |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 815 | continue; |
| 816 | |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 817 | // Two instrs are partial redundant if their basic blocks are reachable |
| 818 | // from one to another but one doesn't dominate another. |
| 819 | if (CMBB != MBB1) { |
| 820 | auto BB = MBB->getBasicBlock(), BB1 = MBB1->getBasicBlock(); |
| 821 | if (BB != nullptr && BB1 != nullptr && |
| 822 | (isPotentiallyReachable(BB1, BB) || |
| 823 | isPotentiallyReachable(BB, BB1))) { |
| 824 | |
| 825 | assert(MI->getOperand(0).isDef() && |
| 826 | "First operand of instr with one explicit def must be this def"); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 827 | Register VReg = MI->getOperand(0).getReg(); |
| 828 | Register NewReg = MRI->cloneVirtualRegister(VReg); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 829 | if (!isProfitableToCSE(NewReg, VReg, CMBB, MI)) |
| 830 | continue; |
| 831 | MachineInstr &NewMI = |
| 832 | TII->duplicate(*CMBB, CMBB->getFirstTerminator(), *MI); |
Davide Italiano | 8115e08 | 2020-04-06 16:35:30 -0700 | [diff] [blame] | 833 | |
| 834 | // When hoisting, make sure we don't carry the debug location of |
| 835 | // the original instruction, as that's not correct and can cause |
| 836 | // unexpected jumps when debugging optimized code. |
| 837 | auto EmptyDL = DebugLoc(); |
| 838 | NewMI.setDebugLoc(EmptyDL); |
| 839 | |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 840 | NewMI.getOperand(0).setReg(NewReg); |
| 841 | |
| 842 | PREMap[MI] = CMBB; |
| 843 | ++NumPREs; |
| 844 | Changed = true; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | return Changed; |
| 849 | } |
| 850 | |
| 851 | // This simple PRE (partial redundancy elimination) pass doesn't actually |
| 852 | // eliminate partial redundancy but transforms it to full redundancy, |
| 853 | // anticipating that the next CSE step will eliminate this created redundancy. |
| 854 | // If CSE doesn't eliminate this, than created instruction will remain dead |
| 855 | // and eliminated later by Remove Dead Machine Instructions pass. |
| 856 | bool MachineCSE::PerformSimplePRE(MachineDominatorTree *DT) { |
| 857 | SmallVector<MachineDomTreeNode *, 32> BBs; |
| 858 | |
| 859 | PREMap.clear(); |
| 860 | bool Changed = false; |
| 861 | BBs.push_back(DT->getRootNode()); |
| 862 | do { |
| 863 | auto Node = BBs.pop_back_val(); |
Nicolai Hähnle | 76c5cb0 | 2020-05-18 16:28:24 +0200 | [diff] [blame] | 864 | for (MachineDomTreeNode *Child : Node->children()) |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 865 | BBs.push_back(Child); |
| 866 | |
| 867 | MachineBasicBlock *MBB = Node->getBlock(); |
| 868 | Changed |= ProcessBlockPRE(DT, MBB); |
| 869 | |
| 870 | } while (!BBs.empty()); |
| 871 | |
| 872 | return Changed; |
| 873 | } |
| 874 | |
Kai Luo | 02b8056 | 2019-08-07 05:40:21 +0000 | [diff] [blame] | 875 | bool MachineCSE::isProfitableToHoistInto(MachineBasicBlock *CandidateBB, |
| 876 | MachineBasicBlock *MBB, |
| 877 | MachineBasicBlock *MBB1) { |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 878 | if (CandidateBB->getParent()->getFunction().hasMinSize()) |
| 879 | return true; |
| 880 | assert(DT->dominates(CandidateBB, MBB) && "CandidateBB should dominate MBB"); |
| 881 | assert(DT->dominates(CandidateBB, MBB1) && |
| 882 | "CandidateBB should dominate MBB1"); |
| 883 | return MBFI->getBlockFreq(CandidateBB) <= |
| 884 | MBFI->getBlockFreq(MBB) + MBFI->getBlockFreq(MBB1); |
| 885 | } |
| 886 | |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 887 | bool MachineCSE::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 888 | if (skipFunction(MF.getFunction())) |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 889 | return false; |
| 890 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 891 | TII = MF.getSubtarget().getInstrInfo(); |
| 892 | TRI = MF.getSubtarget().getRegisterInfo(); |
Evan Cheng | 4eab008 | 2010-03-03 02:48:20 +0000 | [diff] [blame] | 893 | MRI = &MF.getRegInfo(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 894 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Evan Cheng | 19e44b4 | 2010-03-09 03:21:12 +0000 | [diff] [blame] | 895 | DT = &getAnalysis<MachineDominatorTree>(); |
Kai Luo | dec6246 | 2019-07-19 12:58:16 +0000 | [diff] [blame] | 896 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
Tom Stellard | f01af29 | 2015-05-09 00:56:07 +0000 | [diff] [blame] | 897 | LookAheadLimit = TII->getMachineCSELookAheadLimit(); |
Anton Afanasyev | 623d9ba | 2019-06-09 12:15:47 +0000 | [diff] [blame] | 898 | bool ChangedPRE, ChangedCSE; |
| 899 | ChangedPRE = PerformSimplePRE(DT); |
| 900 | ChangedCSE = PerformCSE(DT->getRootNode()); |
| 901 | return ChangedPRE || ChangedCSE; |
Evan Cheng | 036aa49 | 2010-03-02 02:38:24 +0000 | [diff] [blame] | 902 | } |