13b0846e8STim Northover //==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==// 23b0846e8STim Northover // 33b0846e8STim Northover // The LLVM Compiler Infrastructure 43b0846e8STim Northover // 53b0846e8STim Northover // This file is distributed under the University of Illinois Open Source 63b0846e8STim Northover // License. See LICENSE.TXT for details. 73b0846e8STim Northover // 83b0846e8STim Northover //===----------------------------------------------------------------------===// 97d78614aSMatthias Braun /// \file When allowed by the instruction, replace a dead definition of a GPR 107d78614aSMatthias Braun /// with the zero register. This makes the code a bit friendlier towards the 117d78614aSMatthias Braun /// hardware's register renamer. 123b0846e8STim Northover //===----------------------------------------------------------------------===// 133b0846e8STim Northover 143b0846e8STim Northover #include "AArch64.h" 153b0846e8STim Northover #include "AArch64RegisterInfo.h" 16c1c28051SChristof Douma #include "AArch64Subtarget.h" 173b0846e8STim Northover #include "llvm/ADT/Statistic.h" 18c1c28051SChristof Douma #include "llvm/CodeGen/ISDOpcodes.h" 193b0846e8STim Northover #include "llvm/CodeGen/MachineFunction.h" 201f8930e3SBenjamin Kramer #include "llvm/CodeGen/MachineFunctionPass.h" 213b0846e8STim Northover #include "llvm/CodeGen/MachineInstr.h" 223d51cf0aSMatthias Braun #include "llvm/CodeGen/MachineRegisterInfo.h" 233f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h" 24b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h" 253b0846e8STim Northover #include "llvm/Support/Debug.h" 263b0846e8STim Northover #include "llvm/Support/raw_ostream.h" 273b0846e8STim Northover using namespace llvm; 283b0846e8STim Northover 293b0846e8STim Northover #define DEBUG_TYPE "aarch64-dead-defs" 303b0846e8STim Northover 313b0846e8STim Northover STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced"); 323b0846e8STim Northover 3369e3eb3cSChad Rosier #define AARCH64_DEAD_REG_DEF_NAME "AArch64 Dead register definitions" 3469e3eb3cSChad Rosier 353b0846e8STim Northover namespace { 363b0846e8STim Northover class AArch64DeadRegisterDefinitions : public MachineFunctionPass { 373b0846e8STim Northover private: 383b0846e8STim Northover const TargetRegisterInfo *TRI; 393d51cf0aSMatthias Braun const MachineRegisterInfo *MRI; 403d51cf0aSMatthias Braun const TargetInstrInfo *TII; 417d78614aSMatthias Braun bool Changed; 427d78614aSMatthias Braun void processMachineBasicBlock(MachineBasicBlock &MBB); 433b0846e8STim Northover public: 443b0846e8STim Northover static char ID; // Pass identification, replacement for typeid. 457d78614aSMatthias Braun AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) { 4669e3eb3cSChad Rosier initializeAArch64DeadRegisterDefinitionsPass( 4769e3eb3cSChad Rosier *PassRegistry::getPassRegistry()); 4869e3eb3cSChad Rosier } 493b0846e8STim Northover 50fd38cbebSCraig Topper bool runOnMachineFunction(MachineFunction &F) override; 513b0846e8STim Northover 52117296c0SMehdi Amini StringRef getPassName() const override { return AARCH64_DEAD_REG_DEF_NAME; } 533b0846e8STim Northover 54fd38cbebSCraig Topper void getAnalysisUsage(AnalysisUsage &AU) const override { 553b0846e8STim Northover AU.setPreservesCFG(); 563b0846e8STim Northover MachineFunctionPass::getAnalysisUsage(AU); 573b0846e8STim Northover } 5860711ca2SJoel Jones 5960711ca2SJoel Jones bool shouldSkip(const MachineInstr &MI, const MachineFunction &MF) const; 603b0846e8STim Northover }; 613b0846e8STim Northover char AArch64DeadRegisterDefinitions::ID = 0; 623b0846e8STim Northover } // end anonymous namespace 633b0846e8STim Northover 6469e3eb3cSChad Rosier INITIALIZE_PASS(AArch64DeadRegisterDefinitions, "aarch64-dead-defs", 6569e3eb3cSChad Rosier AARCH64_DEAD_REG_DEF_NAME, false, false) 6669e3eb3cSChad Rosier 677d78614aSMatthias Braun static bool usesFrameIndex(const MachineInstr &MI) { 687d78614aSMatthias Braun for (const MachineOperand &MO : MI.uses()) 697d78614aSMatthias Braun if (MO.isFI()) 703b0846e8STim Northover return true; 713b0846e8STim Northover return false; 723b0846e8STim Northover } 733b0846e8STim Northover 7460711ca2SJoel Jones bool 7560711ca2SJoel Jones AArch64DeadRegisterDefinitions::shouldSkip(const MachineInstr &MI, 7660711ca2SJoel Jones const MachineFunction &MF) const { 7760711ca2SJoel Jones if (!MF.getSubtarget<AArch64Subtarget>().hasLSE()) 7860711ca2SJoel Jones return false; 7960711ca2SJoel Jones 8060711ca2SJoel Jones #define CASE_AARCH64_ATOMIC_(PREFIX) \ 8160711ca2SJoel Jones case AArch64::PREFIX##X: \ 8260711ca2SJoel Jones case AArch64::PREFIX##W: \ 8360711ca2SJoel Jones case AArch64::PREFIX##H: \ 8460711ca2SJoel Jones case AArch64::PREFIX##B 8560711ca2SJoel Jones 8660711ca2SJoel Jones for (const MachineMemOperand *MMO : MI.memoperands()) { 8760711ca2SJoel Jones if (MMO->isAtomic()) { 8860711ca2SJoel Jones unsigned Opcode = MI.getOpcode(); 8960711ca2SJoel Jones switch (Opcode) { 9060711ca2SJoel Jones default: 9160711ca2SJoel Jones return false; 9260711ca2SJoel Jones break; 9360711ca2SJoel Jones 9460711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDADDA): 9560711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDADDAL): 9660711ca2SJoel Jones 9760711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDCLRA): 9860711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDCLRAL): 9960711ca2SJoel Jones 10060711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDEORA): 10160711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDEORAL): 10260711ca2SJoel Jones 10360711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSETA): 10460711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSETAL): 10560711ca2SJoel Jones 10660711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSMAXA): 10760711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSMAXAL): 10860711ca2SJoel Jones 10960711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSMINA): 11060711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDSMINAL): 11160711ca2SJoel Jones 11260711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDUMAXA): 11360711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDUMAXAL): 11460711ca2SJoel Jones 11560711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDUMINA): 11660711ca2SJoel Jones CASE_AARCH64_ATOMIC_(LDUMINAL): 11760711ca2SJoel Jones 11860711ca2SJoel Jones CASE_AARCH64_ATOMIC_(SWPA): 11960711ca2SJoel Jones CASE_AARCH64_ATOMIC_(SWPAL): 12060711ca2SJoel Jones return true; 12160711ca2SJoel Jones break; 12260711ca2SJoel Jones } 12360711ca2SJoel Jones } 12460711ca2SJoel Jones } 12560711ca2SJoel Jones 12660711ca2SJoel Jones #undef CASE_AARCH64_ATOMIC_ 12760711ca2SJoel Jones 12860711ca2SJoel Jones return false; 12960711ca2SJoel Jones } 13060711ca2SJoel Jones 1317d78614aSMatthias Braun void AArch64DeadRegisterDefinitions::processMachineBasicBlock( 1323b0846e8STim Northover MachineBasicBlock &MBB) { 1333d51cf0aSMatthias Braun const MachineFunction &MF = *MBB.getParent(); 1343b0846e8STim Northover for (MachineInstr &MI : MBB) { 1353b0846e8STim Northover if (usesFrameIndex(MI)) { 1363b0846e8STim Northover // We need to skip this instruction because while it appears to have a 1373b0846e8STim Northover // dead def it uses a frame index which might expand into a multi 1383b0846e8STim Northover // instruction sequence during EPI. 139*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, operand is frame index\n"); 1403b0846e8STim Northover continue; 1413b0846e8STim Northover } 142b8a1ecfcSTim Northover if (MI.definesRegister(AArch64::XZR) || MI.definesRegister(AArch64::WZR)) { 143b8a1ecfcSTim Northover // It is not allowed to write to the same register (not even the zero 144b8a1ecfcSTim Northover // register) twice in a single instruction. 145*d34e60caSNicola Zaghen LLVM_DEBUG( 146*d34e60caSNicola Zaghen dbgs() 147*d34e60caSNicola Zaghen << " Ignoring, XZR or WZR already used by the instruction\n"); 148b8a1ecfcSTim Northover continue; 149b8a1ecfcSTim Northover } 15060711ca2SJoel Jones 15160711ca2SJoel Jones if (shouldSkip(MI, MF)) { 152*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, Atomic instruction with acquire " 153*d34e60caSNicola Zaghen "semantics using WZR/XZR\n"); 154c1c28051SChristof Douma continue; 155c1c28051SChristof Douma } 15660711ca2SJoel Jones 1577d78614aSMatthias Braun const MCInstrDesc &Desc = MI.getDesc(); 1587d78614aSMatthias Braun for (int I = 0, E = Desc.getNumDefs(); I != E; ++I) { 1597d78614aSMatthias Braun MachineOperand &MO = MI.getOperand(I); 1603d51cf0aSMatthias Braun if (!MO.isReg() || !MO.isDef()) 1613d51cf0aSMatthias Braun continue; 1623d51cf0aSMatthias Braun // We should not have any relevant physreg defs that are replacable by 1633d51cf0aSMatthias Braun // zero before register allocation. So we just check for dead vreg defs. 1643d51cf0aSMatthias Braun unsigned Reg = MO.getReg(); 1653d51cf0aSMatthias Braun if (!TargetRegisterInfo::isVirtualRegister(Reg) || 1663d51cf0aSMatthias Braun (!MO.isDead() && !MRI->use_nodbg_empty(Reg))) 1677d78614aSMatthias Braun continue; 1683b0846e8STim Northover assert(!MO.isImplicit() && "Unexpected implicit def!"); 169*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Dead def operand #" << I << " in:\n "; 1703b0846e8STim Northover MI.print(dbgs())); 1713b0846e8STim Northover // Be careful not to change the register if it's a tied operand. 1727d78614aSMatthias Braun if (MI.isRegTiedToUseOperand(I)) { 173*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, def is tied operand.\n"); 1743b0846e8STim Northover continue; 1753b0846e8STim Northover } 1763d51cf0aSMatthias Braun const TargetRegisterClass *RC = TII->getRegClass(Desc, I, TRI, MF); 1773b0846e8STim Northover unsigned NewReg; 1783d51cf0aSMatthias Braun if (RC == nullptr) { 179*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n"); 1803b0846e8STim Northover continue; 1813d51cf0aSMatthias Braun } else if (RC->contains(AArch64::WZR)) 1823b0846e8STim Northover NewReg = AArch64::WZR; 1833d51cf0aSMatthias Braun else if (RC->contains(AArch64::XZR)) 1843b0846e8STim Northover NewReg = AArch64::XZR; 1853d51cf0aSMatthias Braun else { 186*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Ignoring, register is not a GPR.\n"); 1873d51cf0aSMatthias Braun continue; 1883b0846e8STim Northover } 189*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " Replacing with zero register. New:\n "); 1903b0846e8STim Northover MO.setReg(NewReg); 1913d51cf0aSMatthias Braun MO.setIsDead(); 192*d34e60caSNicola Zaghen LLVM_DEBUG(MI.print(dbgs())); 1933b0846e8STim Northover ++NumDeadDefsReplaced; 1947d78614aSMatthias Braun Changed = true; 195b8a1ecfcSTim Northover // Only replace one dead register, see check for zero register above. 196b8a1ecfcSTim Northover break; 1973b0846e8STim Northover } 1983b0846e8STim Northover } 1993b0846e8STim Northover } 2003b0846e8STim Northover 2013b0846e8STim Northover // Scan the function for instructions that have a dead definition of a 2023b0846e8STim Northover // register. Replace that register with the zero register when possible. 2033b0846e8STim Northover bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) { 204f1caa283SMatthias Braun if (skipFunction(MF.getFunction())) 2051ac98bb0SAndrew Kaylor return false; 2061ac98bb0SAndrew Kaylor 2077d78614aSMatthias Braun TRI = MF.getSubtarget().getRegisterInfo(); 2083d51cf0aSMatthias Braun TII = MF.getSubtarget().getInstrInfo(); 2093d51cf0aSMatthias Braun MRI = &MF.getRegInfo(); 210*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n"); 2117d78614aSMatthias Braun Changed = false; 2123b0846e8STim Northover for (auto &MBB : MF) 2137d78614aSMatthias Braun processMachineBasicBlock(MBB); 2143b0846e8STim Northover return Changed; 2153b0846e8STim Northover } 2163b0846e8STim Northover 2173b0846e8STim Northover FunctionPass *llvm::createAArch64DeadRegisterDefinitions() { 2183b0846e8STim Northover return new AArch64DeadRegisterDefinitions(); 2193b0846e8STim Northover } 220