13b0846e8STim Northover //==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==//
23b0846e8STim Northover //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63b0846e8STim Northover //
73b0846e8STim Northover //===----------------------------------------------------------------------===//
87d78614aSMatthias Braun /// \file When allowed by the instruction, replace a dead definition of a GPR
97d78614aSMatthias Braun /// with the zero register. This makes the code a bit friendlier towards the
107d78614aSMatthias Braun /// hardware's register renamer.
113b0846e8STim Northover //===----------------------------------------------------------------------===//
123b0846e8STim Northover
133b0846e8STim Northover #include "AArch64.h"
143b0846e8STim Northover #include "AArch64RegisterInfo.h"
15c1c28051SChristof Douma #include "AArch64Subtarget.h"
163b0846e8STim Northover #include "llvm/ADT/Statistic.h"
17c1c28051SChristof Douma #include "llvm/CodeGen/ISDOpcodes.h"
183b0846e8STim Northover #include "llvm/CodeGen/MachineFunction.h"
191f8930e3SBenjamin Kramer #include "llvm/CodeGen/MachineFunctionPass.h"
203b0846e8STim Northover #include "llvm/CodeGen/MachineInstr.h"
213d51cf0aSMatthias Braun #include "llvm/CodeGen/MachineRegisterInfo.h"
223f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
23b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
243b0846e8STim Northover #include "llvm/Support/Debug.h"
253b0846e8STim Northover #include "llvm/Support/raw_ostream.h"
263b0846e8STim Northover using namespace llvm;
273b0846e8STim Northover
283b0846e8STim Northover #define DEBUG_TYPE "aarch64-dead-defs"
293b0846e8STim Northover
303b0846e8STim Northover STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
313b0846e8STim Northover
3269e3eb3cSChad Rosier #define AARCH64_DEAD_REG_DEF_NAME "AArch64 Dead register definitions"
3369e3eb3cSChad Rosier
343b0846e8STim Northover namespace {
353b0846e8STim Northover class AArch64DeadRegisterDefinitions : public MachineFunctionPass {
363b0846e8STim Northover private:
373b0846e8STim Northover const TargetRegisterInfo *TRI;
383d51cf0aSMatthias Braun const MachineRegisterInfo *MRI;
393d51cf0aSMatthias Braun const TargetInstrInfo *TII;
407d78614aSMatthias Braun bool Changed;
417d78614aSMatthias Braun void processMachineBasicBlock(MachineBasicBlock &MBB);
423b0846e8STim Northover public:
433b0846e8STim Northover static char ID; // Pass identification, replacement for typeid.
AArch64DeadRegisterDefinitions()447d78614aSMatthias Braun AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) {
4569e3eb3cSChad Rosier initializeAArch64DeadRegisterDefinitionsPass(
4669e3eb3cSChad Rosier *PassRegistry::getPassRegistry());
4769e3eb3cSChad Rosier }
483b0846e8STim Northover
49fd38cbebSCraig Topper bool runOnMachineFunction(MachineFunction &F) override;
503b0846e8STim Northover
getPassName() const51117296c0SMehdi Amini StringRef getPassName() const override { return AARCH64_DEAD_REG_DEF_NAME; }
523b0846e8STim Northover
getAnalysisUsage(AnalysisUsage & AU) const53fd38cbebSCraig Topper void getAnalysisUsage(AnalysisUsage &AU) const override {
543b0846e8STim Northover AU.setPreservesCFG();
553b0846e8STim Northover MachineFunctionPass::getAnalysisUsage(AU);
563b0846e8STim Northover }
573b0846e8STim Northover };
583b0846e8STim Northover char AArch64DeadRegisterDefinitions::ID = 0;
593b0846e8STim Northover } // end anonymous namespace
603b0846e8STim Northover
6169e3eb3cSChad Rosier INITIALIZE_PASS(AArch64DeadRegisterDefinitions, "aarch64-dead-defs",
6269e3eb3cSChad Rosier AARCH64_DEAD_REG_DEF_NAME, false, false)
6369e3eb3cSChad Rosier
usesFrameIndex(const MachineInstr & MI)647d78614aSMatthias Braun static bool usesFrameIndex(const MachineInstr &MI) {
657d78614aSMatthias Braun for (const MachineOperand &MO : MI.uses())
667d78614aSMatthias Braun if (MO.isFI())
673b0846e8STim Northover return true;
683b0846e8STim Northover return false;
693b0846e8STim Northover }
703b0846e8STim Northover
718cfd91dcSChristof Douma // Instructions that lose their 'read' operation for a subesquent fence acquire
728cfd91dcSChristof Douma // (DMB LD) once the zero register is used.
738cfd91dcSChristof Douma //
748cfd91dcSChristof Douma // WARNING: The aquire variants of the instructions are also affected, but they
758cfd91dcSChristof Douma // are split out into `atomicBarrierDroppedOnZero()` to support annotations on
768cfd91dcSChristof Douma // assembly.
atomicReadDroppedOnZero(unsigned Opcode)778cfd91dcSChristof Douma static bool atomicReadDroppedOnZero(unsigned Opcode) {
788cfd91dcSChristof Douma switch (Opcode) {
798cfd91dcSChristof Douma case AArch64::LDADDB: case AArch64::LDADDH:
808cfd91dcSChristof Douma case AArch64::LDADDW: case AArch64::LDADDX:
818cfd91dcSChristof Douma case AArch64::LDADDLB: case AArch64::LDADDLH:
828cfd91dcSChristof Douma case AArch64::LDADDLW: case AArch64::LDADDLX:
838cfd91dcSChristof Douma case AArch64::LDCLRB: case AArch64::LDCLRH:
848cfd91dcSChristof Douma case AArch64::LDCLRW: case AArch64::LDCLRX:
858cfd91dcSChristof Douma case AArch64::LDCLRLB: case AArch64::LDCLRLH:
868cfd91dcSChristof Douma case AArch64::LDCLRLW: case AArch64::LDCLRLX:
878cfd91dcSChristof Douma case AArch64::LDEORB: case AArch64::LDEORH:
888cfd91dcSChristof Douma case AArch64::LDEORW: case AArch64::LDEORX:
898cfd91dcSChristof Douma case AArch64::LDEORLB: case AArch64::LDEORLH:
908cfd91dcSChristof Douma case AArch64::LDEORLW: case AArch64::LDEORLX:
918cfd91dcSChristof Douma case AArch64::LDSETB: case AArch64::LDSETH:
928cfd91dcSChristof Douma case AArch64::LDSETW: case AArch64::LDSETX:
938cfd91dcSChristof Douma case AArch64::LDSETLB: case AArch64::LDSETLH:
948cfd91dcSChristof Douma case AArch64::LDSETLW: case AArch64::LDSETLX:
958cfd91dcSChristof Douma case AArch64::LDSMAXB: case AArch64::LDSMAXH:
968cfd91dcSChristof Douma case AArch64::LDSMAXW: case AArch64::LDSMAXX:
978cfd91dcSChristof Douma case AArch64::LDSMAXLB: case AArch64::LDSMAXLH:
988cfd91dcSChristof Douma case AArch64::LDSMAXLW: case AArch64::LDSMAXLX:
998cfd91dcSChristof Douma case AArch64::LDSMINB: case AArch64::LDSMINH:
1008cfd91dcSChristof Douma case AArch64::LDSMINW: case AArch64::LDSMINX:
1018cfd91dcSChristof Douma case AArch64::LDSMINLB: case AArch64::LDSMINLH:
1028cfd91dcSChristof Douma case AArch64::LDSMINLW: case AArch64::LDSMINLX:
1038cfd91dcSChristof Douma case AArch64::LDUMAXB: case AArch64::LDUMAXH:
1048cfd91dcSChristof Douma case AArch64::LDUMAXW: case AArch64::LDUMAXX:
1058cfd91dcSChristof Douma case AArch64::LDUMAXLB: case AArch64::LDUMAXLH:
1068cfd91dcSChristof Douma case AArch64::LDUMAXLW: case AArch64::LDUMAXLX:
1078cfd91dcSChristof Douma case AArch64::LDUMINB: case AArch64::LDUMINH:
1088cfd91dcSChristof Douma case AArch64::LDUMINW: case AArch64::LDUMINX:
1098cfd91dcSChristof Douma case AArch64::LDUMINLB: case AArch64::LDUMINLH:
1108cfd91dcSChristof Douma case AArch64::LDUMINLW: case AArch64::LDUMINLX:
1118cfd91dcSChristof Douma return true;
1128cfd91dcSChristof Douma }
1138cfd91dcSChristof Douma return false;
1148cfd91dcSChristof Douma }
1158cfd91dcSChristof Douma
processMachineBasicBlock(MachineBasicBlock & MBB)1167d78614aSMatthias Braun void AArch64DeadRegisterDefinitions::processMachineBasicBlock(
1173b0846e8STim Northover MachineBasicBlock &MBB) {
1183d51cf0aSMatthias Braun const MachineFunction &MF = *MBB.getParent();
1193b0846e8STim Northover for (MachineInstr &MI : MBB) {
1203b0846e8STim Northover if (usesFrameIndex(MI)) {
1213b0846e8STim Northover // We need to skip this instruction because while it appears to have a
1223b0846e8STim Northover // dead def it uses a frame index which might expand into a multi
1233b0846e8STim Northover // instruction sequence during EPI.
124d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, operand is frame index\n");
1253b0846e8STim Northover continue;
1263b0846e8STim Northover }
127b8a1ecfcSTim Northover if (MI.definesRegister(AArch64::XZR) || MI.definesRegister(AArch64::WZR)) {
128b8a1ecfcSTim Northover // It is not allowed to write to the same register (not even the zero
129b8a1ecfcSTim Northover // register) twice in a single instruction.
130d34e60caSNicola Zaghen LLVM_DEBUG(
131d34e60caSNicola Zaghen dbgs()
132d34e60caSNicola Zaghen << " Ignoring, XZR or WZR already used by the instruction\n");
133b8a1ecfcSTim Northover continue;
134b8a1ecfcSTim Northover }
13560711ca2SJoel Jones
1368cfd91dcSChristof Douma if (atomicBarrierDroppedOnZero(MI.getOpcode()) || atomicReadDroppedOnZero(MI.getOpcode())) {
13771025a2fSTim Northover LLVM_DEBUG(dbgs() << " Ignoring, semantics change with xzr/wzr.\n");
138c1c28051SChristof Douma continue;
139c1c28051SChristof Douma }
14060711ca2SJoel Jones
1417d78614aSMatthias Braun const MCInstrDesc &Desc = MI.getDesc();
1427d78614aSMatthias Braun for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {
1437d78614aSMatthias Braun MachineOperand &MO = MI.getOperand(I);
1443d51cf0aSMatthias Braun if (!MO.isReg() || !MO.isDef())
1453d51cf0aSMatthias Braun continue;
1463d51cf0aSMatthias Braun // We should not have any relevant physreg defs that are replacable by
1473d51cf0aSMatthias Braun // zero before register allocation. So we just check for dead vreg defs.
148*5ae66e56SDaniel Sanders Register Reg = MO.getReg();
1492bea69bfSDaniel Sanders if (!Register::isVirtualRegister(Reg) ||
1503d51cf0aSMatthias Braun (!MO.isDead() && !MRI->use_nodbg_empty(Reg)))
1517d78614aSMatthias Braun continue;
1523b0846e8STim Northover assert(!MO.isImplicit() && "Unexpected implicit def!");
153d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";
1543b0846e8STim Northover MI.print(dbgs()));
1553b0846e8STim Northover // Be careful not to change the register if it's a tied operand.
1567d78614aSMatthias Braun if (MI.isRegTiedToUseOperand(I)) {
157d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");
1583b0846e8STim Northover continue;
1593b0846e8STim Northover }
1603d51cf0aSMatthias Braun const TargetRegisterClass *RC = TII->getRegClass(Desc, I, TRI, MF);
1613b0846e8STim Northover unsigned NewReg;
1623d51cf0aSMatthias Braun if (RC == nullptr) {
163d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
1643b0846e8STim Northover continue;
1653d51cf0aSMatthias Braun } else if (RC->contains(AArch64::WZR))
1663b0846e8STim Northover NewReg = AArch64::WZR;
1673d51cf0aSMatthias Braun else if (RC->contains(AArch64::XZR))
1683b0846e8STim Northover NewReg = AArch64::XZR;
1693d51cf0aSMatthias Braun else {
170d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");
1713d51cf0aSMatthias Braun continue;
1723b0846e8STim Northover }
173d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ");
1743b0846e8STim Northover MO.setReg(NewReg);
1753d51cf0aSMatthias Braun MO.setIsDead();
176d34e60caSNicola Zaghen LLVM_DEBUG(MI.print(dbgs()));
1773b0846e8STim Northover ++NumDeadDefsReplaced;
1787d78614aSMatthias Braun Changed = true;
179b8a1ecfcSTim Northover // Only replace one dead register, see check for zero register above.
180b8a1ecfcSTim Northover break;
1813b0846e8STim Northover }
1823b0846e8STim Northover }
1833b0846e8STim Northover }
1843b0846e8STim Northover
1853b0846e8STim Northover // Scan the function for instructions that have a dead definition of a
1863b0846e8STim Northover // register. Replace that register with the zero register when possible.
runOnMachineFunction(MachineFunction & MF)1873b0846e8STim Northover bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {
188f1caa283SMatthias Braun if (skipFunction(MF.getFunction()))
1891ac98bb0SAndrew Kaylor return false;
1901ac98bb0SAndrew Kaylor
1917d78614aSMatthias Braun TRI = MF.getSubtarget().getRegisterInfo();
1923d51cf0aSMatthias Braun TII = MF.getSubtarget().getInstrInfo();
1933d51cf0aSMatthias Braun MRI = &MF.getRegInfo();
194d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n");
1957d78614aSMatthias Braun Changed = false;
1963b0846e8STim Northover for (auto &MBB : MF)
1977d78614aSMatthias Braun processMachineBasicBlock(MBB);
1983b0846e8STim Northover return Changed;
1993b0846e8STim Northover }
2003b0846e8STim Northover
createAArch64DeadRegisterDefinitions()2013b0846e8STim Northover FunctionPass *llvm::createAArch64DeadRegisterDefinitions() {
2023b0846e8STim Northover return new AArch64DeadRegisterDefinitions();
2033b0846e8STim Northover }
204