LLVM 20.0.0git
AArch64DeadRegisterDefinitionsPass.cpp
Go to the documentation of this file.
1//==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8/// \file When allowed by the instruction, replace a dead definition of a GPR
9/// with the zero register. This makes the code a bit friendlier towards the
10/// hardware's register renamer.
11//===----------------------------------------------------------------------===//
12
13#include "AArch64.h"
14#include "AArch64RegisterInfo.h"
15#include "AArch64Subtarget.h"
16#include "llvm/ADT/Statistic.h"
24#include "llvm/Support/Debug.h"
26using namespace llvm;
27
28#define DEBUG_TYPE "aarch64-dead-defs"
29
30STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
31
32#define AARCH64_DEAD_REG_DEF_NAME "AArch64 Dead register definitions"
33
34namespace {
35class AArch64DeadRegisterDefinitions : public MachineFunctionPass {
36private:
39 const TargetInstrInfo *TII;
40 bool Changed;
41 void processMachineBasicBlock(MachineBasicBlock &MBB);
42public:
43 static char ID; // Pass identification, replacement for typeid.
44 AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) {
47 }
48
50
51 StringRef getPassName() const override { return AARCH64_DEAD_REG_DEF_NAME; }
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.setPreservesCFG();
56 }
57};
58char AArch64DeadRegisterDefinitions::ID = 0;
59} // end anonymous namespace
60
61INITIALIZE_PASS(AArch64DeadRegisterDefinitions, "aarch64-dead-defs",
62 AARCH64_DEAD_REG_DEF_NAME, false, false)
63
64static bool usesFrameIndex(const MachineInstr &MI) {
65 for (const MachineOperand &MO : MI.uses())
66 if (MO.isFI())
67 return true;
68 return false;
69}
70
71// Instructions that lose their 'read' operation for a subesquent fence acquire
72// (DMB LD) once the zero register is used.
73//
74// WARNING: The aquire variants of the instructions are also affected, but they
75// are split out into `atomicBarrierDroppedOnZero()` to support annotations on
76// assembly.
77static bool atomicReadDroppedOnZero(unsigned Opcode) {
78 switch (Opcode) {
79 case AArch64::LDADDB: case AArch64::LDADDH:
80 case AArch64::LDADDW: case AArch64::LDADDX:
81 case AArch64::LDADDLB: case AArch64::LDADDLH:
82 case AArch64::LDADDLW: case AArch64::LDADDLX:
83 case AArch64::LDCLRB: case AArch64::LDCLRH:
84 case AArch64::LDCLRW: case AArch64::LDCLRX:
85 case AArch64::LDCLRLB: case AArch64::LDCLRLH:
86 case AArch64::LDCLRLW: case AArch64::LDCLRLX:
87 case AArch64::LDEORB: case AArch64::LDEORH:
88 case AArch64::LDEORW: case AArch64::LDEORX:
89 case AArch64::LDEORLB: case AArch64::LDEORLH:
90 case AArch64::LDEORLW: case AArch64::LDEORLX:
91 case AArch64::LDSETB: case AArch64::LDSETH:
92 case AArch64::LDSETW: case AArch64::LDSETX:
93 case AArch64::LDSETLB: case AArch64::LDSETLH:
94 case AArch64::LDSETLW: case AArch64::LDSETLX:
95 case AArch64::LDSMAXB: case AArch64::LDSMAXH:
96 case AArch64::LDSMAXW: case AArch64::LDSMAXX:
97 case AArch64::LDSMAXLB: case AArch64::LDSMAXLH:
98 case AArch64::LDSMAXLW: case AArch64::LDSMAXLX:
99 case AArch64::LDSMINB: case AArch64::LDSMINH:
100 case AArch64::LDSMINW: case AArch64::LDSMINX:
101 case AArch64::LDSMINLB: case AArch64::LDSMINLH:
102 case AArch64::LDSMINLW: case AArch64::LDSMINLX:
103 case AArch64::LDUMAXB: case AArch64::LDUMAXH:
104 case AArch64::LDUMAXW: case AArch64::LDUMAXX:
105 case AArch64::LDUMAXLB: case AArch64::LDUMAXLH:
106 case AArch64::LDUMAXLW: case AArch64::LDUMAXLX:
107 case AArch64::LDUMINB: case AArch64::LDUMINH:
108 case AArch64::LDUMINW: case AArch64::LDUMINX:
109 case AArch64::LDUMINLB: case AArch64::LDUMINLH:
110 case AArch64::LDUMINLW: case AArch64::LDUMINLX:
111 case AArch64::SWPB: case AArch64::SWPH:
112 case AArch64::SWPW: case AArch64::SWPX:
113 case AArch64::SWPLB: case AArch64::SWPLH:
114 case AArch64::SWPLW: case AArch64::SWPLX:
115 return true;
116 }
117 return false;
118}
119
120void AArch64DeadRegisterDefinitions::processMachineBasicBlock(
122 const MachineFunction &MF = *MBB.getParent();
123 for (MachineInstr &MI : MBB) {
124 if (usesFrameIndex(MI)) {
125 // We need to skip this instruction because while it appears to have a
126 // dead def it uses a frame index which might expand into a multi
127 // instruction sequence during EPI.
128 LLVM_DEBUG(dbgs() << " Ignoring, operand is frame index\n");
129 continue;
130 }
131 if (MI.definesRegister(AArch64::XZR, /*TRI=*/nullptr) ||
132 MI.definesRegister(AArch64::WZR, /*TRI=*/nullptr)) {
133 // It is not allowed to write to the same register (not even the zero
134 // register) twice in a single instruction.
136 dbgs()
137 << " Ignoring, XZR or WZR already used by the instruction\n");
138 continue;
139 }
140
141 if (atomicBarrierDroppedOnZero(MI.getOpcode()) || atomicReadDroppedOnZero(MI.getOpcode())) {
142 LLVM_DEBUG(dbgs() << " Ignoring, semantics change with xzr/wzr.\n");
143 continue;
144 }
145
146 const MCInstrDesc &Desc = MI.getDesc();
147 for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
148 MachineOperand &MO = MI.getOperand(I);
149 if (!MO.isReg() || !MO.isDef())
150 continue;
151 // We should not have any relevant physreg defs that are replacable by
152 // zero before register allocation. So we just check for dead vreg defs.
153 Register Reg = MO.getReg();
154 if (!Reg.isVirtual() || (!MO.isDead() && !MRI->use_nodbg_empty(Reg)))
155 continue;
156 assert(!MO.isImplicit() && "Unexpected implicit def!");
157 LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
158 MI.print(dbgs()));
159 // Be careful not to change the register if it's a tied operand.
160 if (MI.isRegTiedToUseOperand(I)) {
161 LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
162 continue;
163 }
164 const TargetRegisterClass *RC = TII->getRegClass(Desc, I, TRI, MF);
165 unsigned NewReg;
166 if (RC == nullptr) {
167 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
168 continue;
169 } else if (RC->contains(AArch64::WZR))
170 NewReg = AArch64::WZR;
171 else if (RC->contains(AArch64::XZR))
172 NewReg = AArch64::XZR;
173 else {
174 LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
175 continue;
176 }
177 LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ");
178 MO.setReg(NewReg);
179 MO.setIsDead();
181 ++NumDeadDefsReplaced;
182 Changed = true;
183 // Only replace one dead register, see check for zero register above.
184 break;
185 }
186 }
187}
188
189// Scan the function for instructions that have a dead definition of a
190// register. Replace that register with the zero register when possible.
191bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {
192 if (skipFunction(MF.getFunction()))
193 return false;
194
197 MRI = &MF.getRegInfo();
198 LLVM_DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n");
199 Changed = false;
200 for (auto &MBB : MF)
201 processMachineBasicBlock(MBB);
202 return Changed;
203}
204
206 return new AArch64DeadRegisterDefinitions();
207}
unsigned const MachineRegisterInfo * MRI
static bool atomicReadDroppedOnZero(unsigned Opcode)
#define AARCH64_DEAD_REG_DEF_NAME
aarch64 promote const
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
bool isImplicit() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetInstrInfo - Interface to description of machine instruction set.
bool contains(Register Reg) const
Return true if the specified register is included in this register class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createAArch64DeadRegisterDefinitions()
void initializeAArch64DeadRegisterDefinitionsPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
static bool atomicBarrierDroppedOnZero(unsigned Opcode)
Description of the encoding of one expression Op.