Path: blob/main/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64DeadRegisterDefinitionsPass.cpp
96353 views
//==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7/// \file When allowed by the instruction, replace a dead definition of a GPR8/// with the zero register. This makes the code a bit friendlier towards the9/// hardware's register renamer.10//===----------------------------------------------------------------------===//1112#include "AArch64.h"13#include "AArch64RegisterInfo.h"14#include "AArch64Subtarget.h"15#include "llvm/ADT/Statistic.h"16#include "llvm/CodeGen/ISDOpcodes.h"17#include "llvm/CodeGen/MachineFunction.h"18#include "llvm/CodeGen/MachineFunctionPass.h"19#include "llvm/CodeGen/MachineInstr.h"20#include "llvm/CodeGen/MachineRegisterInfo.h"21#include "llvm/CodeGen/TargetInstrInfo.h"22#include "llvm/CodeGen/TargetSubtargetInfo.h"23#include "llvm/Support/Debug.h"24#include "llvm/Support/raw_ostream.h"25using namespace llvm;2627#define DEBUG_TYPE "aarch64-dead-defs"2829STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");3031#define AARCH64_DEAD_REG_DEF_NAME "AArch64 Dead register definitions"3233namespace {34class AArch64DeadRegisterDefinitions : public MachineFunctionPass {35private:36const TargetRegisterInfo *TRI;37const MachineRegisterInfo *MRI;38const TargetInstrInfo *TII;39bool Changed;40void processMachineBasicBlock(MachineBasicBlock &MBB);41public:42static char ID; // Pass identification, replacement for typeid.43AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) {44initializeAArch64DeadRegisterDefinitionsPass(45*PassRegistry::getPassRegistry());46}4748bool runOnMachineFunction(MachineFunction &F) override;4950StringRef getPassName() const override { return AARCH64_DEAD_REG_DEF_NAME; }5152void getAnalysisUsage(AnalysisUsage &AU) const override {53AU.setPreservesCFG();54MachineFunctionPass::getAnalysisUsage(AU);55}56};57char AArch64DeadRegisterDefinitions::ID = 0;58} // end anonymous namespace5960INITIALIZE_PASS(AArch64DeadRegisterDefinitions, "aarch64-dead-defs",61AARCH64_DEAD_REG_DEF_NAME, false, false)6263static bool usesFrameIndex(const MachineInstr &MI) {64for (const MachineOperand &MO : MI.uses())65if (MO.isFI())66return true;67return false;68}6970// Instructions that lose their 'read' operation for a subesquent fence acquire71// (DMB LD) once the zero register is used.72//73// WARNING: The aquire variants of the instructions are also affected, but they74// are split out into `atomicBarrierDroppedOnZero()` to support annotations on75// assembly.76static bool atomicReadDroppedOnZero(unsigned Opcode) {77switch (Opcode) {78case AArch64::LDADDB: case AArch64::LDADDH:79case AArch64::LDADDW: case AArch64::LDADDX:80case AArch64::LDADDLB: case AArch64::LDADDLH:81case AArch64::LDADDLW: case AArch64::LDADDLX:82case AArch64::LDCLRB: case AArch64::LDCLRH:83case AArch64::LDCLRW: case AArch64::LDCLRX:84case AArch64::LDCLRLB: case AArch64::LDCLRLH:85case AArch64::LDCLRLW: case AArch64::LDCLRLX:86case AArch64::LDEORB: case AArch64::LDEORH:87case AArch64::LDEORW: case AArch64::LDEORX:88case AArch64::LDEORLB: case AArch64::LDEORLH:89case AArch64::LDEORLW: case AArch64::LDEORLX:90case AArch64::LDSETB: case AArch64::LDSETH:91case AArch64::LDSETW: case AArch64::LDSETX:92case AArch64::LDSETLB: case AArch64::LDSETLH:93case AArch64::LDSETLW: case AArch64::LDSETLX:94case AArch64::LDSMAXB: case AArch64::LDSMAXH:95case AArch64::LDSMAXW: case AArch64::LDSMAXX:96case AArch64::LDSMAXLB: case AArch64::LDSMAXLH:97case AArch64::LDSMAXLW: case AArch64::LDSMAXLX:98case AArch64::LDSMINB: case AArch64::LDSMINH:99case AArch64::LDSMINW: case AArch64::LDSMINX:100case AArch64::LDSMINLB: case AArch64::LDSMINLH:101case AArch64::LDSMINLW: case AArch64::LDSMINLX:102case AArch64::LDUMAXB: case AArch64::LDUMAXH:103case AArch64::LDUMAXW: case AArch64::LDUMAXX:104case AArch64::LDUMAXLB: case AArch64::LDUMAXLH:105case AArch64::LDUMAXLW: case AArch64::LDUMAXLX:106case AArch64::LDUMINB: case AArch64::LDUMINH:107case AArch64::LDUMINW: case AArch64::LDUMINX:108case AArch64::LDUMINLB: case AArch64::LDUMINLH:109case AArch64::LDUMINLW: case AArch64::LDUMINLX:110case AArch64::SWPB: case AArch64::SWPH:111case AArch64::SWPW: case AArch64::SWPX:112case AArch64::SWPLB: case AArch64::SWPLH:113case AArch64::SWPLW: case AArch64::SWPLX:114return true;115}116return false;117}118119void AArch64DeadRegisterDefinitions::processMachineBasicBlock(120MachineBasicBlock &MBB) {121const MachineFunction &MF = *MBB.getParent();122for (MachineInstr &MI : MBB) {123if (usesFrameIndex(MI)) {124// We need to skip this instruction because while it appears to have a125// dead def it uses a frame index which might expand into a multi126// instruction sequence during EPI.127LLVM_DEBUG(dbgs() << " Ignoring, operand is frame index\n");128continue;129}130if (MI.definesRegister(AArch64::XZR, /*TRI=*/nullptr) ||131MI.definesRegister(AArch64::WZR, /*TRI=*/nullptr)) {132// It is not allowed to write to the same register (not even the zero133// register) twice in a single instruction.134LLVM_DEBUG(135dbgs()136<< " Ignoring, XZR or WZR already used by the instruction\n");137continue;138}139140if (atomicBarrierDroppedOnZero(MI.getOpcode()) || atomicReadDroppedOnZero(MI.getOpcode())) {141LLVM_DEBUG(dbgs() << " Ignoring, semantics change with xzr/wzr.\n");142continue;143}144145const MCInstrDesc &Desc = MI.getDesc();146for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) {147MachineOperand &MO = MI.getOperand(I);148if (!MO.isReg() || !MO.isDef())149continue;150// We should not have any relevant physreg defs that are replacable by151// zero before register allocation. So we just check for dead vreg defs.152Register Reg = MO.getReg();153if (!Reg.isVirtual() || (!MO.isDead() && !MRI->use_nodbg_empty(Reg)))154continue;155assert(!MO.isImplicit() && "Unexpected implicit def!");156LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n ";157MI.print(dbgs()));158// Be careful not to change the register if it's a tied operand.159if (MI.isRegTiedToUseOperand(I)) {160LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n");161continue;162}163const TargetRegisterClass *RC = TII->getRegClass(Desc, I, TRI, MF);164unsigned NewReg;165if (RC == nullptr) {166LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");167continue;168} else if (RC->contains(AArch64::WZR))169NewReg = AArch64::WZR;170else if (RC->contains(AArch64::XZR))171NewReg = AArch64::XZR;172else {173LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n");174continue;175}176LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n ");177MO.setReg(NewReg);178MO.setIsDead();179LLVM_DEBUG(MI.print(dbgs()));180++NumDeadDefsReplaced;181Changed = true;182// Only replace one dead register, see check for zero register above.183break;184}185}186}187188// Scan the function for instructions that have a dead definition of a189// register. Replace that register with the zero register when possible.190bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {191if (skipFunction(MF.getFunction()))192return false;193194TRI = MF.getSubtarget().getRegisterInfo();195TII = MF.getSubtarget().getInstrInfo();196MRI = &MF.getRegInfo();197LLVM_DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n");198Changed = false;199for (auto &MBB : MF)200processMachineBasicBlock(MBB);201return Changed;202}203204FunctionPass *llvm::createAArch64DeadRegisterDefinitions() {205return new AArch64DeadRegisterDefinitions();206}207208209