LLVM API Documentation

AArch64DeadRegisterDefinitionsPass.cpp
Go to the documentation of this file.
00001 //==-- AArch64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // When allowed by the instruction, replace a dead definition of a GPR with
00010 // the zero register. This makes the code a bit friendlier towards the
00011 // hardware's register renamer.
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "AArch64.h"
00015 #include "AArch64RegisterInfo.h"
00016 #include "llvm/ADT/Statistic.h"
00017 #include "llvm/CodeGen/MachineFunction.h"
00018 #include "llvm/CodeGen/MachineFunctionPass.h"
00019 #include "llvm/CodeGen/MachineInstr.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include "llvm/Target/TargetSubtargetInfo.h"
00023 using namespace llvm;
00024 
00025 #define DEBUG_TYPE "aarch64-dead-defs"
00026 
00027 STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
00028 
00029 namespace {
00030 class AArch64DeadRegisterDefinitions : public MachineFunctionPass {
00031 private:
00032   const TargetRegisterInfo *TRI;
00033   bool implicitlyDefinesOverlappingReg(unsigned Reg, const MachineInstr &MI);
00034   bool processMachineBasicBlock(MachineBasicBlock &MBB);
00035   bool usesFrameIndex(const MachineInstr &MI);
00036 public:
00037   static char ID; // Pass identification, replacement for typeid.
00038   explicit AArch64DeadRegisterDefinitions() : MachineFunctionPass(ID) {}
00039 
00040   bool runOnMachineFunction(MachineFunction &F) override;
00041 
00042   const char *getPassName() const override { return "Dead register definitions"; }
00043 
00044   void getAnalysisUsage(AnalysisUsage &AU) const override {
00045     AU.setPreservesCFG();
00046     MachineFunctionPass::getAnalysisUsage(AU);
00047   }
00048 };
00049 char AArch64DeadRegisterDefinitions::ID = 0;
00050 } // end anonymous namespace
00051 
00052 bool AArch64DeadRegisterDefinitions::implicitlyDefinesOverlappingReg(
00053     unsigned Reg, const MachineInstr &MI) {
00054   for (const MachineOperand &MO : MI.implicit_operands())
00055     if (MO.isReg() && MO.isDef())
00056       if (TRI->regsOverlap(Reg, MO.getReg()))
00057         return true;
00058   return false;
00059 }
00060 
00061 bool AArch64DeadRegisterDefinitions::usesFrameIndex(const MachineInstr &MI) {
00062   for (const MachineOperand &Op : MI.uses())
00063     if (Op.isFI())
00064       return true;
00065   return false;
00066 }
00067 
00068 bool AArch64DeadRegisterDefinitions::processMachineBasicBlock(
00069     MachineBasicBlock &MBB) {
00070   bool Changed = false;
00071   for (MachineInstr &MI : MBB) {
00072     if (usesFrameIndex(MI)) {
00073       // We need to skip this instruction because while it appears to have a
00074       // dead def it uses a frame index which might expand into a multi
00075       // instruction sequence during EPI.
00076       DEBUG(dbgs() << "    Ignoring, operand is frame index\n");
00077       continue;
00078     }
00079     for (int i = 0, e = MI.getDesc().getNumDefs(); i != e; ++i) {
00080       MachineOperand &MO = MI.getOperand(i);
00081       if (MO.isReg() && MO.isDead() && MO.isDef()) {
00082         assert(!MO.isImplicit() && "Unexpected implicit def!");
00083         DEBUG(dbgs() << "  Dead def operand #" << i << " in:\n    ";
00084               MI.print(dbgs()));
00085         // Be careful not to change the register if it's a tied operand.
00086         if (MI.isRegTiedToUseOperand(i)) {
00087           DEBUG(dbgs() << "    Ignoring, def is tied operand.\n");
00088           continue;
00089         }
00090         // Don't change the register if there's an implicit def of a subreg or
00091         // supperreg.
00092         if (implicitlyDefinesOverlappingReg(MO.getReg(), MI)) {
00093           DEBUG(dbgs() << "    Ignoring, implicitly defines overlap reg.\n");
00094           continue;
00095         }
00096         // Make sure the instruction take a register class that contains
00097         // the zero register and replace it if so.
00098         unsigned NewReg;
00099         switch (MI.getDesc().OpInfo[i].RegClass) {
00100         default:
00101           DEBUG(dbgs() << "    Ignoring, register is not a GPR.\n");
00102           continue;
00103         case AArch64::GPR32RegClassID:
00104           NewReg = AArch64::WZR;
00105           break;
00106         case AArch64::GPR64RegClassID:
00107           NewReg = AArch64::XZR;
00108           break;
00109         }
00110         DEBUG(dbgs() << "    Replacing with zero register. New:\n      ");
00111         MO.setReg(NewReg);
00112         DEBUG(MI.print(dbgs()));
00113         ++NumDeadDefsReplaced;
00114       }
00115     }
00116   }
00117   return Changed;
00118 }
00119 
00120 // Scan the function for instructions that have a dead definition of a
00121 // register. Replace that register with the zero register when possible.
00122 bool AArch64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &MF) {
00123   TRI = MF.getSubtarget().getRegisterInfo();
00124   bool Changed = false;
00125   DEBUG(dbgs() << "***** AArch64DeadRegisterDefinitions *****\n");
00126 
00127   for (auto &MBB : MF)
00128     if (processMachineBasicBlock(MBB))
00129       Changed = true;
00130   return Changed;
00131 }
00132 
00133 FunctionPass *llvm::createAArch64DeadRegisterDefinitions() {
00134   return new AArch64DeadRegisterDefinitions();
00135 }