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