xref: /aosp_15_r20/external/llvm/lib/Target/AArch64/AArch64DeadRegisterDefinitionsPass.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker // When allowed by the instruction, replace a dead definition of a GPR with
10*9880d681SAndroid Build Coastguard Worker // the zero register. This makes the code a bit friendlier towards the
11*9880d681SAndroid Build Coastguard Worker // hardware's register renamer.
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "AArch64.h"
15*9880d681SAndroid Build Coastguard Worker #include "AArch64RegisterInfo.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "aarch64-dead-defs"
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker namespace llvm {
30*9880d681SAndroid Build Coastguard Worker void initializeAArch64DeadRegisterDefinitionsPass(PassRegistry &);
31*9880d681SAndroid Build Coastguard Worker }
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker #define AARCH64_DEAD_REG_DEF_NAME "AArch64 Dead register definitions"
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker namespace {
36*9880d681SAndroid Build Coastguard Worker class AArch64DeadRegisterDefinitions : public MachineFunctionPass {
37*9880d681SAndroid Build Coastguard Worker private:
38*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI;
39*9880d681SAndroid Build Coastguard Worker   bool implicitlyDefinesOverlappingReg(unsigned Reg, const MachineInstr &MI);
40*9880d681SAndroid Build Coastguard Worker   bool processMachineBasicBlock(MachineBasicBlock &MBB);
41*9880d681SAndroid Build Coastguard Worker   bool usesFrameIndex(const MachineInstr &MI);
42*9880d681SAndroid Build Coastguard Worker public:
43*9880d681SAndroid Build Coastguard Worker   static char ID; // Pass identification, replacement for typeid.
AArch64DeadRegisterDefinitions()44*9880d681SAndroid Build Coastguard Worker   explicit AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) {
45*9880d681SAndroid Build Coastguard Worker     initializeAArch64DeadRegisterDefinitionsPass(
46*9880d681SAndroid Build Coastguard Worker         *PassRegistry::getPassRegistry());
47*9880d681SAndroid Build Coastguard Worker   }
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &F) override;
50*9880d681SAndroid Build Coastguard Worker 
getRequiredProperties() const51*9880d681SAndroid Build Coastguard Worker   MachineFunctionProperties getRequiredProperties() const override {
52*9880d681SAndroid Build Coastguard Worker     return MachineFunctionProperties().set(
53*9880d681SAndroid Build Coastguard Worker         MachineFunctionProperties::Property::AllVRegsAllocated);
54*9880d681SAndroid Build Coastguard Worker   }
55*9880d681SAndroid Build Coastguard Worker 
getPassName() const56*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override { return AARCH64_DEAD_REG_DEF_NAME; }
57*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const58*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
59*9880d681SAndroid Build Coastguard Worker     AU.setPreservesCFG();
60*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
61*9880d681SAndroid Build Coastguard Worker   }
62*9880d681SAndroid Build Coastguard Worker };
63*9880d681SAndroid Build Coastguard Worker char AArch64DeadRegisterDefinitions::ID = 0;
64*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(AArch64DeadRegisterDefinitions, "aarch64-dead-defs",
67*9880d681SAndroid Build Coastguard Worker                 AARCH64_DEAD_REG_DEF_NAME, false, false)
68*9880d681SAndroid Build Coastguard Worker 
implicitlyDefinesOverlappingReg(unsigned Reg,const MachineInstr & MI)69*9880d681SAndroid Build Coastguard Worker bool AArch64DeadRegisterDefinitions::implicitlyDefinesOverlappingReg(
70*9880d681SAndroid Build Coastguard Worker     unsigned Reg, const MachineInstr &MI) {
71*9880d681SAndroid Build Coastguard Worker   for (const MachineOperand &MO : MI.implicit_operands())
72*9880d681SAndroid Build Coastguard Worker     if (MO.isReg() && MO.isDef())
73*9880d681SAndroid Build Coastguard Worker       if (TRI->regsOverlap(Reg, MO.getReg()))
74*9880d681SAndroid Build Coastguard Worker         return true;
75*9880d681SAndroid Build Coastguard Worker   return false;
76*9880d681SAndroid Build Coastguard Worker }
77*9880d681SAndroid Build Coastguard Worker 
usesFrameIndex(const MachineInstr & MI)78*9880d681SAndroid Build Coastguard Worker bool AArch64DeadRegisterDefinitions::usesFrameIndex(const MachineInstr &MI) {
79*9880d681SAndroid Build Coastguard Worker   for (const MachineOperand &Op : MI.uses())
80*9880d681SAndroid Build Coastguard Worker     if (Op.isFI())
81*9880d681SAndroid Build Coastguard Worker       return true;
82*9880d681SAndroid Build Coastguard Worker   return false;
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker 
processMachineBasicBlock(MachineBasicBlock & MBB)85*9880d681SAndroid Build Coastguard Worker bool AArch64DeadRegisterDefinitions::processMachineBasicBlock(
86*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock &MBB) {
87*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
88*9880d681SAndroid Build Coastguard Worker   for (MachineInstr &MI : MBB) {
89*9880d681SAndroid Build Coastguard Worker     if (usesFrameIndex(MI)) {
90*9880d681SAndroid Build Coastguard Worker       // We need to skip this instruction because while it appears to have a
91*9880d681SAndroid Build Coastguard Worker       // dead def it uses a frame index which might expand into a multi
92*9880d681SAndroid Build Coastguard Worker       // instruction sequence during EPI.
93*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "    Ignoring, operand is frame index\n");
94*9880d681SAndroid Build Coastguard Worker       continue;
95*9880d681SAndroid Build Coastguard Worker     }
96*9880d681SAndroid Build Coastguard Worker     if (MI.definesRegister(AArch64::XZR) || MI.definesRegister(AArch64::WZR)) {
97*9880d681SAndroid Build Coastguard Worker       // It is not allowed to write to the same register (not even the zero
98*9880d681SAndroid Build Coastguard Worker       // register) twice in a single instruction.
99*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "    Ignoring, XZR or WZR already used by the instruction\n");
100*9880d681SAndroid Build Coastguard Worker       continue;
101*9880d681SAndroid Build Coastguard Worker     }
102*9880d681SAndroid Build Coastguard Worker     for (int i = 0, e = MI.getDesc().getNumDefs(); i != e; ++i) {
103*9880d681SAndroid Build Coastguard Worker       MachineOperand &MO = MI.getOperand(i);
104*9880d681SAndroid Build Coastguard Worker       if (MO.isReg() && MO.isDead() && MO.isDef()) {
105*9880d681SAndroid Build Coastguard Worker         assert(!MO.isImplicit() && "Unexpected implicit def!");
106*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "  Dead def operand #" << i << " in:\n    ";
107*9880d681SAndroid Build Coastguard Worker               MI.print(dbgs()));
108*9880d681SAndroid Build Coastguard Worker         // Be careful not to change the register if it's a tied operand.
109*9880d681SAndroid Build Coastguard Worker         if (MI.isRegTiedToUseOperand(i)) {
110*9880d681SAndroid Build Coastguard Worker           DEBUG(dbgs() << "    Ignoring, def is tied operand.\n");
111*9880d681SAndroid Build Coastguard Worker           continue;
112*9880d681SAndroid Build Coastguard Worker         }
113*9880d681SAndroid Build Coastguard Worker         // Don't change the register if there's an implicit def of a subreg or
114*9880d681SAndroid Build Coastguard Worker         // superreg.
115*9880d681SAndroid Build Coastguard Worker         if (implicitlyDefinesOverlappingReg(MO.getReg(), MI)) {
116*9880d681SAndroid Build Coastguard Worker           DEBUG(dbgs() << "    Ignoring, implicitly defines overlap reg.\n");
117*9880d681SAndroid Build Coastguard Worker           continue;
118*9880d681SAndroid Build Coastguard Worker         }
119*9880d681SAndroid Build Coastguard Worker         // Make sure the instruction take a register class that contains
120*9880d681SAndroid Build Coastguard Worker         // the zero register and replace it if so.
121*9880d681SAndroid Build Coastguard Worker         unsigned NewReg;
122*9880d681SAndroid Build Coastguard Worker         switch (MI.getDesc().OpInfo[i].RegClass) {
123*9880d681SAndroid Build Coastguard Worker         default:
124*9880d681SAndroid Build Coastguard Worker           DEBUG(dbgs() << "    Ignoring, register is not a GPR.\n");
125*9880d681SAndroid Build Coastguard Worker           continue;
126*9880d681SAndroid Build Coastguard Worker         case AArch64::GPR32RegClassID:
127*9880d681SAndroid Build Coastguard Worker           NewReg = AArch64::WZR;
128*9880d681SAndroid Build Coastguard Worker           break;
129*9880d681SAndroid Build Coastguard Worker         case AArch64::GPR64RegClassID:
130*9880d681SAndroid Build Coastguard Worker           NewReg = AArch64::XZR;
131*9880d681SAndroid Build Coastguard Worker           break;
132*9880d681SAndroid Build Coastguard Worker         }
133*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << "    Replacing with zero register. New:\n      ");
134*9880d681SAndroid Build Coastguard Worker         MO.setReg(NewReg);
135*9880d681SAndroid Build Coastguard Worker         DEBUG(MI.print(dbgs()));
136*9880d681SAndroid Build Coastguard Worker         ++NumDeadDefsReplaced;
137*9880d681SAndroid Build Coastguard Worker         // Only replace one dead register, see check for zero register above.
138*9880d681SAndroid Build Coastguard Worker         break;
139*9880d681SAndroid Build Coastguard Worker       }
140*9880d681SAndroid Build Coastguard Worker     }
141*9880d681SAndroid Build Coastguard Worker   }
142*9880d681SAndroid Build Coastguard Worker   return Changed;
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker // Scan the function for instructions that have a dead definition of a
146*9880d681SAndroid Build Coastguard Worker // register. Replace that register with the zero register when possible.
runOnMachineFunction(MachineFunction & MF)147*9880d681SAndroid Build Coastguard Worker bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {
148*9880d681SAndroid Build Coastguard Worker   TRI = MF.getSubtarget().getRegisterInfo();
149*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
150*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n");
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker   if (skipFunction(*MF.getFunction()))
153*9880d681SAndroid Build Coastguard Worker     return false;
154*9880d681SAndroid Build Coastguard Worker 
155*9880d681SAndroid Build Coastguard Worker   for (auto &MBB : MF)
156*9880d681SAndroid Build Coastguard Worker     if (processMachineBasicBlock(MBB))
157*9880d681SAndroid Build Coastguard Worker       Changed = true;
158*9880d681SAndroid Build Coastguard Worker   return Changed;
159*9880d681SAndroid Build Coastguard Worker }
160*9880d681SAndroid Build Coastguard Worker 
createAArch64DeadRegisterDefinitions()161*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createAArch64DeadRegisterDefinitions() {
162*9880d681SAndroid Build Coastguard Worker   return new AArch64DeadRegisterDefinitions();
163*9880d681SAndroid Build Coastguard Worker }
164