blob: 2bc56cc8ba58e90bf71cd5e3e9cd5841e4dcdbc3 [file] [log] [blame]
Eugene Zelenko5df3d892017-08-24 21:21:39 +00001//===- MachineCSE.cpp - Machine Common Subexpression Elimination Pass -----===//
Evan Cheng036aa492010-03-02 02:38:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Cheng036aa492010-03-02 02:38:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass performs global common subexpression elimination on machine
Evan Cheng10194a42010-03-02 19:02:27 +000010// instructions using a scoped hash table based value numbering scheme. It
Evan Cheng036aa492010-03-02 02:38:24 +000011// must be run while the machine function is still in SSA form.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng4b2ef562010-04-21 00:21:07 +000015#include "llvm/ADT/DenseMap.h"
Evan Cheng036aa492010-03-02 02:38:24 +000016#include "llvm/ADT/ScopedHashTable.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000017#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng2b3f25e2010-10-29 23:36:03 +000018#include "llvm/ADT/SmallSet.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000019#include "llvm/ADT/SmallVector.h"
Evan Cheng036aa492010-03-02 02:38:24 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Analysis/AliasAnalysis.h"
Anton Afanasyev623d9ba2019-06-09 12:15:47 +000022#include "llvm/Analysis/CFG.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Kai Luodec62462019-07-19 12:58:16 +000024#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000029#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "llvm/CodeGen/Passes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000032#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000033#include "llvm/CodeGen/TargetOpcodes.h"
34#include "llvm/CodeGen/TargetRegisterInfo.h"
35#include "llvm/CodeGen/TargetSubtargetInfo.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080036#include "llvm/InitializePasses.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCRegisterInfo.h"
39#include "llvm/Pass.h"
40#include "llvm/Support/Allocator.h"
Evan Cheng036aa492010-03-02 02:38:24 +000041#include "llvm/Support/Debug.h"
Cameron Zwarich18f164f2011-01-03 04:07:46 +000042#include "llvm/Support/RecyclingAllocator.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000043#include "llvm/Support/raw_ostream.h"
Eugene Zelenko5df3d892017-08-24 21:21:39 +000044#include <cassert>
45#include <iterator>
46#include <utility>
47#include <vector>
48
Evan Cheng036aa492010-03-02 02:38:24 +000049using namespace llvm;
50
Chandler Carruth1b9dde02014-04-22 02:02:50 +000051#define DEBUG_TYPE "machine-cse"
52
Evan Chengb386cd32010-03-03 21:20:05 +000053STATISTIC(NumCoalesces, "Number of copies coalesced");
54STATISTIC(NumCSEs, "Number of common subexpression eliminated");
Anton Afanasyev623d9ba2019-06-09 12:15:47 +000055STATISTIC(NumPREs, "Number of partial redundant expression"
56 " transformed to fully redundant");
Evan Cheng2b3f25e2010-10-29 23:36:03 +000057STATISTIC(NumPhysCSEs,
58 "Number of physreg referencing common subexpr eliminated");
Evan Cheng0be41442012-01-10 02:02:58 +000059STATISTIC(NumCrossBBCSEs,
60 "Number of cross-MBB physreg referencing CS eliminated");
Evan Chengb7ff5a02010-12-15 22:16:21 +000061STATISTIC(NumCommutes, "Number of copies coalesced after commuting");
Bob Wilson30093b52010-06-03 18:28:31 +000062
Evan Cheng036aa492010-03-02 02:38:24 +000063namespace {
Eugene Zelenko5df3d892017-08-24 21:21:39 +000064
Evan Cheng036aa492010-03-02 02:38:24 +000065 class MachineCSE : public MachineFunctionPass {
Evan Cheng4eab0082010-03-03 02:48:20 +000066 const TargetInstrInfo *TII;
Evan Cheng36f8aab2010-03-04 01:33:55 +000067 const TargetRegisterInfo *TRI;
Evan Cheng1abd1a92010-03-04 21:18:08 +000068 AliasAnalysis *AA;
Evan Cheng19e44b42010-03-09 03:21:12 +000069 MachineDominatorTree *DT;
70 MachineRegisterInfo *MRI;
Kai Luodec62462019-07-19 12:58:16 +000071 MachineBlockFrequencyInfo *MBFI;
Eugene Zelenko5df3d892017-08-24 21:21:39 +000072
Evan Cheng036aa492010-03-02 02:38:24 +000073 public:
74 static char ID; // Pass identification
Eugene Zelenko5df3d892017-08-24 21:21:39 +000075
76 MachineCSE() : MachineFunctionPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000077 initializeMachineCSEPass(*PassRegistry::getPassRegistry());
78 }
Evan Cheng036aa492010-03-02 02:38:24 +000079
Craig Topper4584cd52014-03-07 09:26:03 +000080 bool runOnMachineFunction(MachineFunction &MF) override;
Andrew Trick9e761992012-02-08 21:22:43 +000081
Craig Topper4584cd52014-03-07 09:26:03 +000082 void getAnalysisUsage(AnalysisUsage &AU) const override {
Evan Cheng036aa492010-03-02 02:38:24 +000083 AU.setPreservesCFG();
84 MachineFunctionPass::getAnalysisUsage(AU);
Chandler Carruth7b560d42015-09-09 17:55:00 +000085 AU.addRequired<AAResultsWrapperPass>();
Evan Chenge0db9d02010-08-17 20:57:42 +000086 AU.addPreservedID(MachineLoopInfoID);
Evan Cheng036aa492010-03-02 02:38:24 +000087 AU.addRequired<MachineDominatorTree>();
88 AU.addPreserved<MachineDominatorTree>();
Kai Luodec62462019-07-19 12:58:16 +000089 AU.addRequired<MachineBlockFrequencyInfo>();
90 AU.addPreserved<MachineBlockFrequencyInfo>();
Evan Cheng036aa492010-03-02 02:38:24 +000091 }
92
Craig Topper4584cd52014-03-07 09:26:03 +000093 void releaseMemory() override {
Evan Chengb08377e2010-09-17 21:59:42 +000094 ScopeMap.clear();
Anton Afanasyev623d9ba2019-06-09 12:15:47 +000095 PREMap.clear();
Evan Chengb08377e2010-09-17 21:59:42 +000096 Exps.clear();
97 }
98
Evan Cheng036aa492010-03-02 02:38:24 +000099 private:
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000100 using AllocatorTy = RecyclingAllocator<BumpPtrAllocator,
101 ScopedHashTableVal<MachineInstr *, unsigned>>;
102 using ScopedHTType =
103 ScopedHashTable<MachineInstr *, unsigned, MachineInstrExpressionTrait,
104 AllocatorTy>;
105 using ScopeType = ScopedHTType::ScopeTy;
David Greencb5a48b2019-02-20 10:22:18 +0000106 using PhysDefVector = SmallVector<std::pair<unsigned, unsigned>, 2>;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000107
108 unsigned LookAheadLimit = 0;
109 DenseMap<MachineBasicBlock *, ScopeType *> ScopeMap;
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000110 DenseMap<MachineInstr *, MachineBasicBlock *, MachineInstrExpressionTrait>
111 PREMap;
Cameron Zwarich18f164f2011-01-03 04:07:46 +0000112 ScopedHTType VNT;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000113 SmallVector<MachineInstr *, 64> Exps;
114 unsigned CurrVN = 0;
Evan Chengb386cd32010-03-03 21:20:05 +0000115
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000116 bool PerformTrivialCopyPropagation(MachineInstr *MI,
117 MachineBasicBlock *MBB);
Evan Cheng36f8aab2010-03-04 01:33:55 +0000118 bool isPhysDefTriviallyDead(unsigned Reg,
119 MachineBasicBlock::const_iterator I,
Nick Lewycky765c6992012-07-05 06:19:21 +0000120 MachineBasicBlock::const_iterator E) const;
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000121 bool hasLivePhysRegDefUses(const MachineInstr *MI,
122 const MachineBasicBlock *MBB,
David Greencb5a48b2019-02-20 10:22:18 +0000123 SmallSet<unsigned, 8> &PhysRefs,
124 PhysDefVector &PhysDefs, bool &PhysUseDef) const;
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000125 bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
David Greencb5a48b2019-02-20 10:22:18 +0000126 SmallSet<unsigned, 8> &PhysRefs,
127 PhysDefVector &PhysDefs, bool &NonLocal) const;
Evan Cheng1abd1a92010-03-04 21:18:08 +0000128 bool isCSECandidate(MachineInstr *MI);
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000129 bool isProfitableToCSE(unsigned CSReg, unsigned Reg,
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000130 MachineBasicBlock *CSBB, MachineInstr *MI);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000131 void EnterScope(MachineBasicBlock *MBB);
132 void ExitScope(MachineBasicBlock *MBB);
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000133 bool ProcessBlockCSE(MachineBasicBlock *MBB);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000134 void ExitScopeIfDone(MachineDomTreeNode *Node,
Bill Wendlingd1634052012-07-19 00:04:14 +0000135 DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000136 bool PerformCSE(MachineDomTreeNode *Node);
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000137
138 bool isPRECandidate(MachineInstr *MI);
139 bool ProcessBlockPRE(MachineDominatorTree *MDT, MachineBasicBlock *MBB);
140 bool PerformSimplePRE(MachineDominatorTree *DT);
Kai Luo02b80562019-08-07 05:40:21 +0000141 /// Heuristics to see if it's profitable to move common computations of MBB
Kai Luodec62462019-07-19 12:58:16 +0000142 /// and MBB1 to CandidateBB.
Kai Luo02b80562019-08-07 05:40:21 +0000143 bool isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
144 MachineBasicBlock *MBB,
145 MachineBasicBlock *MBB1);
Evan Cheng036aa492010-03-02 02:38:24 +0000146 };
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000147
Evan Cheng036aa492010-03-02 02:38:24 +0000148} // end anonymous namespace
149
150char MachineCSE::ID = 0;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000151
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000152char &llvm::MachineCSEID = MachineCSE::ID;
Eugene Zelenko5df3d892017-08-24 21:21:39 +0000153
Matthias Braun1527baa2017-05-25 21:26:32 +0000154INITIALIZE_PASS_BEGIN(MachineCSE, DEBUG_TYPE,
155 "Machine Common Subexpression Elimination", false, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000156INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000157INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Matthias Braun1527baa2017-05-25 21:26:32 +0000158INITIALIZE_PASS_END(MachineCSE, DEBUG_TYPE,
159 "Machine Common Subexpression Elimination", false, false)
Evan Cheng036aa492010-03-02 02:38:24 +0000160
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000161/// 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.
165bool MachineCSE::PerformTrivialCopyPropagation(MachineInstr *MI,
166 MachineBasicBlock *MBB) {
Evan Cheng4eab0082010-03-03 02:48:20 +0000167 bool Changed = false;
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000168 for (MachineOperand &MO : MI->operands()) {
Evan Chengb386cd32010-03-03 21:20:05 +0000169 if (!MO.isReg() || !MO.isUse())
170 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000171 Register Reg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000172 if (!Register::isVirtualRegister(Reg))
Evan Chengb386cd32010-03-03 21:20:05 +0000173 continue;
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000174 bool OnlyOneUse = MRI->hasOneNonDBGUse(Reg);
Evan Chengb386cd32010-03-03 21:20:05 +0000175 MachineInstr *DefMI = MRI->getVRegDef(Reg);
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000176 if (!DefMI->isCopy())
177 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000178 Register SrcReg = DefMI->getOperand(1).getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000179 if (!Register::isVirtualRegister(SrcReg))
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000180 continue;
Andrew Tricke3398282013-12-17 04:50:45 +0000181 if (DefMI->getOperand(0).getSubReg())
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000182 continue;
Andrew Tricke4083f92013-12-17 19:29:36 +0000183 // 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 Bognera9346e02018-01-18 02:06:56 +0000197 if (!MRI->constrainRegAttrs(SrcReg, Reg))
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000198 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000199 LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
200 LLVM_DEBUG(dbgs() << "*** to: " << *MI);
Carlos Alberto Enciso06adfa12018-08-30 07:17:41 +0000201
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000202 // Propagate SrcReg of copies to MI.
Andrew Tricke4083f92013-12-17 19:29:36 +0000203 MO.setReg(SrcReg);
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000204 MRI->clearKillFlags(SrcReg);
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000205 // Coalesce single use copies.
206 if (OnlyOneUse) {
Jeremy Morse22493f662019-09-02 12:28:36 +0000207 // 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 Liudd6e12d2014-08-11 05:17:19 +0000212 DefMI->eraseFromParent();
213 ++NumCoalesces;
214 }
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +0000215 Changed = true;
Evan Cheng4eab0082010-03-03 02:48:20 +0000216 }
217
218 return Changed;
219}
220
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000221bool
222MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
223 MachineBasicBlock::const_iterator I,
224 MachineBasicBlock::const_iterator E) const {
Eric Christopher53ff9922010-05-21 23:40:03 +0000225 unsigned LookAheadLeft = LookAheadLimit;
Evan Chengc7d721a2010-03-23 20:33:48 +0000226 while (LookAheadLeft) {
Evan Chengcf7be392010-03-24 01:50:28 +0000227 // Skip over dbg_value's.
Florian Hahn3c8b8c92016-12-16 11:10:26 +0000228 I = skipDebugInstructionsForward(I, E);
Evan Chengcf7be392010-03-24 01:50:28 +0000229
Evan Cheng36f8aab2010-03-04 01:33:55 +0000230 if (I == E)
Mikael Holmen2676f822017-05-24 09:35:23 +0000231 // Reached end of block, we don't know if register is dead or not.
232 return false;
Evan Cheng36f8aab2010-03-04 01:33:55 +0000233
Evan Cheng36f8aab2010-03-04 01:33:55 +0000234 bool SeenDef = false;
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000235 for (const MachineOperand &MO : I->operands()) {
Jakob Stoklund Olesen4c5ad2b2012-02-28 02:08:50 +0000236 if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
237 SeenDef = true;
Evan Cheng36f8aab2010-03-04 01:33:55 +0000238 if (!MO.isReg() || !MO.getReg())
239 continue;
240 if (!TRI->regsOverlap(MO.getReg(), Reg))
241 continue;
242 if (MO.isUse())
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000243 // Found a use!
Evan Cheng36f8aab2010-03-04 01:33:55 +0000244 return false;
245 SeenDef = true;
246 }
247 if (SeenDef)
Andrew Trick9e761992012-02-08 21:22:43 +0000248 // See a def of Reg (or an alias) before encountering any use, it's
Evan Cheng36f8aab2010-03-04 01:33:55 +0000249 // trivially dead.
250 return true;
Evan Chengc7d721a2010-03-23 20:33:48 +0000251
252 --LookAheadLeft;
Evan Cheng36f8aab2010-03-04 01:33:55 +0000253 ++I;
254 }
255 return false;
256}
257
Roman Tereshin8d6ff4c2018-10-20 00:06:15 +0000258static 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 Cheng2b3f25e2010-10-29 23:36:03 +0000273/// hasLivePhysRegDefUses - Return true if the specified instruction read/write
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000274/// physical registers (except for dead defs of physical registers). It also
Evan Chenga03e6f82010-06-04 23:28:13 +0000275/// returns the physical register def by reference if it's the only one and the
276/// instruction does not uses a physical register.
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000277bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
278 const MachineBasicBlock *MBB,
David Greencb5a48b2019-02-20 10:22:18 +0000279 SmallSet<unsigned, 8> &PhysRefs,
280 PhysDefVector &PhysDefs,
281 bool &PhysUseDef) const {
Ulrich Weigand39468772012-11-13 18:40:58 +0000282 // First, add all uses to PhysRefs.
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000283 for (const MachineOperand &MO : MI->operands()) {
Ulrich Weigand39468772012-11-13 18:40:58 +0000284 if (!MO.isReg() || MO.isDef())
Evan Cheng4eab0082010-03-03 02:48:20 +0000285 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000286 Register Reg = MO.getReg();
Evan Cheng4eab0082010-03-03 02:48:20 +0000287 if (!Reg)
288 continue;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000289 if (Register::isVirtualRegister(Reg))
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000290 continue;
Tony Jiangf75f4d62017-11-20 16:55:07 +0000291 // Reading either caller preserved or constant physregs is ok.
Roman Tereshin8d6ff4c2018-10-20 00:06:15 +0000292 if (!isCallerPreservedOrConstPhysReg(Reg, *MI->getMF(), *TRI))
Benjamin Kramer59c8b412012-08-11 20:42:59 +0000293 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
Benjamin Krameref6494f2012-08-11 19:05:13 +0000294 PhysRefs.insert(*AI);
Ulrich Weigand39468772012-11-13 18:40:58 +0000295 }
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 Kramerb6d0bd42014-03-02 12:27:27 +0000300 MachineBasicBlock::const_iterator I = MI; I = std::next(I);
David Greencb5a48b2019-02-20 10:22:18 +0000301 for (const auto &MOP : llvm::enumerate(MI->operands())) {
302 const MachineOperand &MO = MOP.value();
Ulrich Weigand39468772012-11-13 18:40:58 +0000303 if (!MO.isReg() || !MO.isDef())
304 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000305 Register Reg = MO.getReg();
Ulrich Weigand39468772012-11-13 18:40:58 +0000306 if (!Reg)
307 continue;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000308 if (Register::isVirtualRegister(Reg))
Ulrich Weigand39468772012-11-13 18:40:58 +0000309 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 Greencb5a48b2019-02-20 10:22:18 +0000317 PhysDefs.push_back(std::make_pair(MOP.index(), Reg));
Evan Cheng36f8aab2010-03-04 01:33:55 +0000318 }
319
Ulrich Weigand39468772012-11-13 18:40:58 +0000320 // Finally, add all defs to PhysRefs as well.
321 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i)
David Greencb5a48b2019-02-20 10:22:18 +0000322 for (MCRegAliasIterator AI(PhysDefs[i].second, TRI, true); AI.isValid();
323 ++AI)
Ulrich Weigand39468772012-11-13 18:40:58 +0000324 PhysRefs.insert(*AI);
325
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000326 return !PhysRefs.empty();
Evan Cheng036aa492010-03-02 02:38:24 +0000327}
328
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000329bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
David Greencb5a48b2019-02-20 10:22:18 +0000330 SmallSet<unsigned, 8> &PhysRefs,
331 PhysDefVector &PhysDefs,
Evan Cheng0be41442012-01-10 02:02:58 +0000332 bool &NonLocal) const {
Eli Friedman54019622011-05-06 05:23:07 +0000333 // For now conservatively returns false if the common subexpression is
Evan Cheng0be41442012-01-10 02:02:58 +0000334 // 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 Chengd9725a32012-01-11 00:38:11 +0000341 if (MBB->pred_size() != 1 || *MBB->pred_begin() != CSMBB)
Evan Cheng0be41442012-01-10 02:02:58 +0000342 return false;
Evan Chengd9725a32012-01-11 00:38:11 +0000343
344 for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
David Greencb5a48b2019-02-20 10:22:18 +0000345 if (MRI->isAllocatable(PhysDefs[i].second) ||
346 MRI->isReserved(PhysDefs[i].second))
Lang Hames5bade3d2012-02-17 00:27:16 +0000347 // Avoid extending live range of physical registers if they are
348 //allocatable or reserved.
Evan Chengd9725a32012-01-11 00:38:11 +0000349 return false;
350 }
351 CrossMBB = true;
Evan Cheng0be41442012-01-10 02:02:58 +0000352 }
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000353 MachineBasicBlock::const_iterator I = CSMI; I = std::next(I);
Eli Friedman54019622011-05-06 05:23:07 +0000354 MachineBasicBlock::const_iterator E = MI;
Evan Cheng0be41442012-01-10 02:02:58 +0000355 MachineBasicBlock::const_iterator EE = CSMBB->end();
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000356 unsigned LookAheadLeft = LookAheadLimit;
357 while (LookAheadLeft) {
Eli Friedman54019622011-05-06 05:23:07 +0000358 // Skip over dbg_value's.
Shiva Chen801bf7e2018-05-09 02:42:00 +0000359 while (I != E && I != EE && I->isDebugInstr())
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000360 ++I;
Eli Friedman54019622011-05-06 05:23:07 +0000361
Evan Cheng0be41442012-01-10 02:02:58 +0000362 if (I == EE) {
363 assert(CrossMBB && "Reaching end-of-MBB without finding MI?");
Duncan Sandsae22c602012-02-05 14:20:11 +0000364 (void)CrossMBB;
Evan Cheng0be41442012-01-10 02:02:58 +0000365 CrossMBB = false;
366 NonLocal = true;
367 I = MBB->begin();
368 EE = MBB->end();
369 continue;
370 }
371
Eli Friedman54019622011-05-06 05:23:07 +0000372 if (I == E)
373 return true;
374
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000375 for (const MachineOperand &MO : I->operands()) {
Jakob Stoklund Olesen4c5ad2b2012-02-28 02:08:50 +0000376 // 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 Friedman54019622011-05-06 05:23:07 +0000380 if (!MO.isReg() || !MO.isDef())
381 continue;
Daniel Sanders0c476112019-08-15 19:22:08 +0000382 Register MOReg = MO.getReg();
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000383 if (Register::isVirtualRegister(MOReg))
Eli Friedman54019622011-05-06 05:23:07 +0000384 continue;
385 if (PhysRefs.count(MOReg))
386 return false;
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000387 }
Eli Friedman54019622011-05-06 05:23:07 +0000388
389 --LookAheadLeft;
390 ++I;
Evan Cheng2c8bdea2010-05-21 21:22:19 +0000391 }
392
393 return false;
394}
395
Evan Cheng1abd1a92010-03-04 21:18:08 +0000396bool MachineCSE::isCSECandidate(MachineInstr *MI) {
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000397 if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() ||
Shiva Chen801bf7e2018-05-09 02:42:00 +0000398 MI->isInlineAsm() || MI->isDebugInstr())
Evan Chengc9e86212010-03-08 23:49:12 +0000399 return false;
400
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000401 // Ignore copies.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000402 if (MI->isCopyLike())
Evan Cheng1abd1a92010-03-04 21:18:08 +0000403 return false;
404
405 // Ignore stuff that we obviously can't move.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000406 if (MI->mayStore() || MI->isCall() || MI->isTerminator() ||
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000407 MI->mayRaiseFPException() || MI->hasUnmodeledSideEffects())
Evan Cheng1abd1a92010-03-04 21:18:08 +0000408 return false;
409
Evan Cheng7f8e5632011-12-07 07:15:52 +0000410 if (MI->mayLoad()) {
Evan Cheng1abd1a92010-03-04 21:18:08 +0000411 // 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 Lebard98cf002016-09-10 01:03:20 +0000414 if (!MI->isDereferenceableInvariantLoad(AA))
Evan Cheng1abd1a92010-03-04 21:18:08 +0000415 // 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 Shene885d5e2016-04-19 19:40:37 +0000420
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 Cheng1abd1a92010-03-04 21:18:08 +0000426 return true;
427}
428
Evan Cheng19e44b42010-03-09 03:21:12 +0000429/// isProfitableToCSE - Return true if it's profitable to eliminate MI with a
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000430/// common expression that defines Reg. CSBB is basic block where CSReg is
431/// defined.
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000432bool MachineCSE::isProfitableToCSE(unsigned CSReg, unsigned Reg,
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000433 MachineBasicBlock *CSBB, MachineInstr *MI) {
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000434 // FIXME: Heuristics that works around the lack the live range splitting.
435
Manman Rencb36b8c2012-08-07 06:16:46 +0000436 // If CSReg is used at all uses of Reg, CSE should not increase register
437 // pressure of CSReg.
438 bool MayIncreasePressure = true;
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000439 if (Register::isVirtualRegister(CSReg) && Register::isVirtualRegister(Reg)) {
Manman Rencb36b8c2012-08-07 06:16:46 +0000440 MayIncreasePressure = false;
441 SmallPtrSet<MachineInstr*, 8> CSUses;
Owen Andersonb36376e2014-03-17 19:36:09 +0000442 for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) {
443 CSUses.insert(&MI);
Manman Rencb36b8c2012-08-07 06:16:46 +0000444 }
Owen Andersonb36376e2014-03-17 19:36:09 +0000445 for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
446 if (!CSUses.count(&MI)) {
Manman Rencb36b8c2012-08-07 06:16:46 +0000447 MayIncreasePressure = true;
448 break;
449 }
450 }
451 }
452 if (!MayIncreasePressure) return true;
453
Chris Lattner6c8b8dd2011-01-10 07:51:31 +0000454 // 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 Smith9cfc75c2016-06-30 00:01:54 +0000457 if (TII->isAsCheapAsAMove(*MI)) {
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000458 MachineBasicBlock *BB = MI->getParent();
Chris Lattner6c8b8dd2011-01-10 07:51:31 +0000459 if (CSBB != BB && !CSBB->isSuccessor(BB))
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000460 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 Patel3d07ec92016-01-06 00:45:42 +0000466 for (const MachineOperand &MO : MI->operands()) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000467 if (MO.isReg() && MO.isUse() && Register::isVirtualRegister(MO.getReg())) {
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000468 HasVRegUse = true;
469 break;
470 }
471 }
472 if (!HasVRegUse) {
473 bool HasNonCopyUse = false;
Owen Andersonb36376e2014-03-17 19:36:09 +0000474 for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000475 // Ignore copies.
Owen Andersonb36376e2014-03-17 19:36:09 +0000476 if (!MI.isCopyLike()) {
Evan Cheng4c5f7a72010-03-10 02:12:03 +0000477 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 Cheng19e44b42010-03-09 03:21:12 +0000487 bool HasPHI = false;
Michael Zolotukhin131e7492018-05-04 01:40:05 +0000488 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(CSReg)) {
489 HasPHI |= UseMI.isPHI();
490 if (UseMI.getParent() == MI->getParent())
491 return true;
Evan Cheng19e44b42010-03-09 03:21:12 +0000492 }
493
Michael Zolotukhin131e7492018-05-04 01:40:05 +0000494 return !HasPHI;
Evan Cheng19e44b42010-03-09 03:21:12 +0000495}
496
Evan Cheng4b2ef562010-04-21 00:21:07 +0000497void MachineCSE::EnterScope(MachineBasicBlock *MBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000498 LLVM_DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
Evan Cheng4b2ef562010-04-21 00:21:07 +0000499 ScopeType *Scope = new ScopeType(VNT);
500 ScopeMap[MBB] = Scope;
501}
502
503void MachineCSE::ExitScope(MachineBasicBlock *MBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000504 LLVM_DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
Evan Cheng4b2ef562010-04-21 00:21:07 +0000505 DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB);
506 assert(SI != ScopeMap.end());
Evan Cheng4b2ef562010-04-21 00:21:07 +0000507 delete SI->second;
Jakub Staszakf18753b2012-11-26 22:14:19 +0000508 ScopeMap.erase(SI);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000509}
510
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000511bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
Evan Cheng4eab0082010-03-03 02:48:20 +0000512 bool Changed = false;
513
Evan Cheng19e44b42010-03-09 03:21:12 +0000514 SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs;
Manman Ren1be131b2012-08-08 00:51:41 +0000515 SmallVector<unsigned, 2> ImplicitDefsToUpdate;
Ahmed Bougacha54b7d332014-12-02 18:09:51 +0000516 SmallVector<unsigned, 2> ImplicitDefs;
Evan Chengb386cd32010-03-03 21:20:05 +0000517 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) {
Evan Cheng4eab0082010-03-03 02:48:20 +0000518 MachineInstr *MI = &*I;
Evan Chengb386cd32010-03-03 21:20:05 +0000519 ++I;
Evan Cheng1abd1a92010-03-04 21:18:08 +0000520
521 if (!isCSECandidate(MI))
Evan Cheng4eab0082010-03-03 02:48:20 +0000522 continue;
Evan Cheng4eab0082010-03-03 02:48:20 +0000523
524 bool FoundCSE = VNT.count(MI);
525 if (!FoundCSE) {
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000526 // Using trivial copy propagation to find more CSE opportunities.
527 if (PerformTrivialCopyPropagation(MI, MBB)) {
Evan Chengfe917ef2011-04-11 18:47:20 +0000528 Changed = true;
529
Evan Cheng604bc162010-04-02 02:21:24 +0000530 // After coalescing MI itself may become a copy.
Jakob Stoklund Olesen37c42a32010-07-16 04:45:42 +0000531 if (MI->isCopyLike())
Evan Cheng604bc162010-04-02 02:21:24 +0000532 continue;
Jiangning Liudd6e12d2014-08-11 05:17:19 +0000533
534 // Try again to see if CSE is possible.
Evan Cheng4eab0082010-03-03 02:48:20 +0000535 FoundCSE = VNT.count(MI);
Evan Cheng604bc162010-04-02 02:21:24 +0000536 }
Evan Cheng4eab0082010-03-03 02:48:20 +0000537 }
Evan Chengb7ff5a02010-12-15 22:16:21 +0000538
539 // Commute commutable instructions.
540 bool Commuted = false;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000541 if (!FoundCSE && MI->isCommutable()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000542 if (MachineInstr *NewMI = TII->commuteInstruction(*MI)) {
Evan Chengb7ff5a02010-12-15 22:16:21 +0000543 Commuted = true;
544 FoundCSE = VNT.count(NewMI);
Evan Chengfe917ef2011-04-11 18:47:20 +0000545 if (NewMI != MI) {
Evan Chengb7ff5a02010-12-15 22:16:21 +0000546 // New instruction. It doesn't need to be kept.
547 NewMI->eraseFromParent();
Evan Chengfe917ef2011-04-11 18:47:20 +0000548 Changed = true;
549 } else if (!FoundCSE)
Evan Chengb7ff5a02010-12-15 22:16:21 +0000550 // MI was changed but it didn't help, commute it back!
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000551 (void)TII->commuteInstruction(*MI);
Evan Chengb7ff5a02010-12-15 22:16:21 +0000552 }
553 }
Evan Cheng4eab0082010-03-03 02:48:20 +0000554
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000555 // If the instruction defines physical registers and the values *may* be
Evan Cheng29226412010-03-03 23:59:08 +0000556 // used, then it's not safe to replace it with a common subexpression.
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000557 // It's also not safe if the instruction uses physical registers.
Evan Cheng0be41442012-01-10 02:02:58 +0000558 bool CrossMBBPhysDef = false;
Nick Lewycky765c6992012-07-05 06:19:21 +0000559 SmallSet<unsigned, 8> PhysRefs;
David Greencb5a48b2019-02-20 10:22:18 +0000560 PhysDefVector PhysDefs;
Ulrich Weigand39468772012-11-13 18:40:58 +0000561 bool PhysUseDef = false;
562 if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs,
563 PhysDefs, PhysUseDef)) {
Evan Cheng29226412010-03-03 23:59:08 +0000564 FoundCSE = false;
565
Evan Cheng0be41442012-01-10 02:02:58 +0000566 // ... 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 Weigand39468772012-11-13 18:40:58 +0000569 // 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 Cheng2c8bdea2010-05-21 21:22:19 +0000577 }
578
Evan Chengb386cd32010-03-03 21:20:05 +0000579 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 Zaghend34e60c2018-05-14 12:53:11 +0000588 LLVM_DEBUG(dbgs() << "Examining: " << *MI);
589 LLVM_DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
Evan Cheng19e44b42010-03-09 03:21:12 +0000590
591 // Check if it's profitable to perform this CSE.
592 bool DoCSE = true;
Roman Tereshinb2d3f2e2018-06-12 18:30:37 +0000593 unsigned NumDefs = MI->getNumDefs();
Andrew Trickcccd82f2013-12-16 19:36:18 +0000594
Evan Chengb386cd32010-03-03 21:20:05 +0000595 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 Sanders0c476112019-08-15 19:22:08 +0000599 Register OldReg = MO.getReg();
600 Register NewReg = CSMI->getOperand(i).getReg();
Manman Ren1be131b2012-08-08 00:51:41 +0000601
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 Bougacha54b7d332014-12-02 18:09:51 +0000606
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 Ren1be131b2012-08-08 00:51:41 +0000612 if (OldReg == NewReg) {
613 --NumDefs;
Evan Cheng0f5f5472010-03-06 01:14:19 +0000614 continue;
Manman Ren1be131b2012-08-08 00:51:41 +0000615 }
Bill Wendling3e5409d2011-10-12 23:03:40 +0000616
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000617 assert(Register::isVirtualRegister(OldReg) &&
618 Register::isVirtualRegister(NewReg) &&
Evan Chengb386cd32010-03-03 21:20:05 +0000619 "Do not CSE physical register defs!");
Bill Wendling3e5409d2011-10-12 23:03:40 +0000620
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000621 if (!isProfitableToCSE(NewReg, OldReg, CSMI->getParent(), MI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000622 LLVM_DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n");
Evan Cheng19e44b42010-03-09 03:21:12 +0000623 DoCSE = false;
624 break;
625 }
Bill Wendling3e5409d2011-10-12 23:03:40 +0000626
Justin Bognera9346e02018-01-18 02:06:56 +0000627 // 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 Zaghend34e60c2018-05-14 12:53:11 +0000631 LLVM_DEBUG(
632 dbgs() << "*** Not the same register constraints, avoid CSE!\n");
Bill Wendling3e5409d2011-10-12 23:03:40 +0000633 DoCSE = false;
634 break;
635 }
636
Evan Cheng19e44b42010-03-09 03:21:12 +0000637 CSEPairs.push_back(std::make_pair(OldReg, NewReg));
Evan Chengb386cd32010-03-03 21:20:05 +0000638 --NumDefs;
639 }
Evan Cheng19e44b42010-03-09 03:21:12 +0000640
641 // Actually perform the elimination.
642 if (DoCSE) {
Simon Pilgrimdecc1942020-09-25 22:33:15 +0100643 for (const std::pair<unsigned, unsigned> &CSEPair : CSEPairs) {
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000644 unsigned OldReg = CSEPair.first;
645 unsigned NewReg = CSEPair.second;
Matthias Braun26e7ea62015-02-04 19:35:16 +0000646 // 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 Gohman7767d272010-05-13 19:24:00 +0000653 }
Evan Cheng0be41442012-01-10 02:02:58 +0000654
Manman Ren1be131b2012-08-08 00:51:41 +0000655 // 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 Patel3d07ec92016-01-06 00:45:42 +0000657 for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
658 CSMI->getOperand(ImplicitDefToUpdate).setIsDead(false);
Simon Pilgrimdecc1942020-09-25 22:33:15 +0100659 for (const auto &PhysDef : PhysDefs)
David Greencb5a48b2019-02-20 10:22:18 +0000660 if (!MI->getOperand(PhysDef.first).isDead())
661 CSMI->getOperand(PhysDef.first).setIsDead(false);
Manman Ren1be131b2012-08-08 00:51:41 +0000662
Ahmed Bougacha54b7d332014-12-02 18:09:51 +0000663 // 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 Mistriha8a83d12017-12-07 10:40:31 +0000666 // 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 Bougacha54b7d332014-12-02 18:09:51 +0000670 // Since we eliminated MI, and reused a register imp-def'd by CSMI
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000671 // (here %nzcv), that register, if it was killed before MI, should have
Ahmed Bougacha54b7d332014-12-02 18:09:51 +0000672 // 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 Cheng0be41442012-01-10 02:02:58 +0000686 if (CrossMBBPhysDef) {
687 // Add physical register defs now coming in from a predecessor to MBB
688 // livein list.
689 while (!PhysDefs.empty()) {
David Greencb5a48b2019-02-20 10:22:18 +0000690 auto LiveIn = PhysDefs.pop_back_val();
691 if (!MBB->isLiveIn(LiveIn.second))
692 MBB->addLiveIn(LiveIn.second);
Evan Cheng0be41442012-01-10 02:02:58 +0000693 }
694 ++NumCrossBBCSEs;
695 }
696
Evan Cheng19e44b42010-03-09 03:21:12 +0000697 MI->eraseFromParent();
698 ++NumCSEs;
Evan Cheng2b3f25e2010-10-29 23:36:03 +0000699 if (!PhysRefs.empty())
Evan Chenga03e6f82010-06-04 23:28:13 +0000700 ++NumPhysCSEs;
Evan Chengb7ff5a02010-12-15 22:16:21 +0000701 if (Commuted)
702 ++NumCommutes;
Evan Chengfe917ef2011-04-11 18:47:20 +0000703 Changed = true;
Evan Cheng19e44b42010-03-09 03:21:12 +0000704 } else {
Evan Cheng19e44b42010-03-09 03:21:12 +0000705 VNT.insert(MI, CurrVN++);
706 Exps.push_back(MI);
707 }
708 CSEPairs.clear();
Manman Ren1be131b2012-08-08 00:51:41 +0000709 ImplicitDefsToUpdate.clear();
Ahmed Bougacha54b7d332014-12-02 18:09:51 +0000710 ImplicitDefs.clear();
Evan Cheng4eab0082010-03-03 02:48:20 +0000711 }
712
Evan Cheng4b2ef562010-04-21 00:21:07 +0000713 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.
719void
720MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node,
Nick Lewycky765c6992012-07-05 06:19:21 +0000721 DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren) {
Evan Cheng4b2ef562010-04-21 00:21:07 +0000722 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 Lewycky765c6992012-07-05 06:19:21 +0000729 while (MachineDomTreeNode *Parent = Node->getIDom()) {
Evan Cheng4b2ef562010-04-21 00:21:07 +0000730 unsigned Left = --OpenChildren[Parent];
731 if (Left != 0)
732 break;
733 ExitScope(Parent->getBlock());
734 Node = Parent;
735 }
736}
737
738bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) {
739 SmallVector<MachineDomTreeNode*, 32> Scopes;
740 SmallVector<MachineDomTreeNode*, 8> WorkList;
Evan Cheng4b2ef562010-04-21 00:21:07 +0000741 DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
742
Evan Chengb08377e2010-09-17 21:59:42 +0000743 CurrVN = 0;
744
Evan Cheng4b2ef562010-04-21 00:21:07 +0000745 // 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ähnle76c5cb02020-05-18 16:28:24 +0200750 OpenChildren[Node] = Node->getNumChildren();
751 for (MachineDomTreeNode *Child : Node->children())
Evan Cheng4b2ef562010-04-21 00:21:07 +0000752 WorkList.push_back(Child);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000753 } while (!WorkList.empty());
754
755 // Now perform CSE.
756 bool Changed = false;
Sanjay Patel3d07ec92016-01-06 00:45:42 +0000757 for (MachineDomTreeNode *Node : Scopes) {
Evan Cheng4b2ef562010-04-21 00:21:07 +0000758 MachineBasicBlock *MBB = Node->getBlock();
759 EnterScope(MBB);
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000760 Changed |= ProcessBlockCSE(MBB);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000761 // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
Nick Lewycky765c6992012-07-05 06:19:21 +0000762 ExitScopeIfDone(Node, OpenChildren);
Evan Cheng4b2ef562010-04-21 00:21:07 +0000763 }
Evan Cheng4eab0082010-03-03 02:48:20 +0000764
765 return Changed;
766}
767
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000768// 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.
771bool 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 Pilgrimce294ff2020-09-21 16:38:44 +0100780 for (const auto &def : MI->defs())
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000781 if (!Register::isVirtualRegister(def.getReg()))
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000782 return false;
783
Simon Pilgrimce294ff2020-09-21 16:38:44 +0100784 for (const auto &use : MI->uses())
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000785 if (use.isReg() && !Register::isVirtualRegister(use.getReg()))
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000786 return false;
787
788 return true;
789}
790
791bool 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 Afanasyev339b39b2019-06-12 13:51:44 +0000811 if (!CMBB->isLegalToHoistInto())
812 continue;
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000813
Kai Luo02b80562019-08-07 05:40:21 +0000814 if (!isProfitableToHoistInto(CMBB, MBB, MBB1))
Kai Luodec62462019-07-19 12:58:16 +0000815 continue;
816
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000817 // 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 Sanders0c476112019-08-15 19:22:08 +0000827 Register VReg = MI->getOperand(0).getReg();
828 Register NewReg = MRI->cloneVirtualRegister(VReg);
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000829 if (!isProfitableToCSE(NewReg, VReg, CMBB, MI))
830 continue;
831 MachineInstr &NewMI =
832 TII->duplicate(*CMBB, CMBB->getFirstTerminator(), *MI);
Davide Italiano8115e082020-04-06 16:35:30 -0700833
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 Afanasyev623d9ba2019-06-09 12:15:47 +0000840 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.
856bool 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ähnle76c5cb02020-05-18 16:28:24 +0200864 for (MachineDomTreeNode *Child : Node->children())
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000865 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 Luo02b80562019-08-07 05:40:21 +0000875bool MachineCSE::isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
876 MachineBasicBlock *MBB,
877 MachineBasicBlock *MBB1) {
Kai Luodec62462019-07-19 12:58:16 +0000878 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 Cheng036aa492010-03-02 02:38:24 +0000887bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000888 if (skipFunction(MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000889 return false;
890
Eric Christopherfc6de422014-08-05 02:39:49 +0000891 TII = MF.getSubtarget().getInstrInfo();
892 TRI = MF.getSubtarget().getRegisterInfo();
Evan Cheng4eab0082010-03-03 02:48:20 +0000893 MRI = &MF.getRegInfo();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000894 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Evan Cheng19e44b42010-03-09 03:21:12 +0000895 DT = &getAnalysis<MachineDominatorTree>();
Kai Luodec62462019-07-19 12:58:16 +0000896 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
Tom Stellardf01af292015-05-09 00:56:07 +0000897 LookAheadLimit = TII->getMachineCSELookAheadLimit();
Anton Afanasyev623d9ba2019-06-09 12:15:47 +0000898 bool ChangedPRE, ChangedCSE;
899 ChangedPRE = PerformSimplePRE(DT);
900 ChangedCSE = PerformCSE(DT->getRootNode());
901 return ChangedPRE || ChangedCSE;
Evan Cheng036aa492010-03-02 02:38:24 +0000902}