LLVM 22.0.0git
AArch64RedundantCondBranchPass.cpp
Go to the documentation of this file.
1//=- AArch64RedundantCondBranch.cpp - Remove redundant conditional branches -=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Late in the pipeline, especially with zero phi operands propagated after tail
10// duplications, we can end up with CBZ/CBNZ/TBZ/TBNZ with a zero register. This
11// simple pass looks at the terminators to a block, removing the redundant
12// instructions where necessary.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AArch64.h"
20#include "llvm/Support/Debug.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "aarch64-redundantcondbranch"
25
26namespace {
27class AArch64RedundantCondBranch : public MachineFunctionPass {
28public:
29 static char ID;
30 AArch64RedundantCondBranch() : MachineFunctionPass(ID) {}
31
32 bool runOnMachineFunction(MachineFunction &MF) override;
33
34 MachineFunctionProperties getRequiredProperties() const override {
35 return MachineFunctionProperties().setNoVRegs();
36 }
37 StringRef getPassName() const override {
38 return "AArch64 Redundant Conditional Branch Elimination";
39 }
40};
41char AArch64RedundantCondBranch::ID = 0;
42} // namespace
43
44INITIALIZE_PASS(AArch64RedundantCondBranch, "aarch64-redundantcondbranch",
45 "AArch64 Redundant Conditional Branch Elimination pass", false,
46 false)
47
48static bool optimizeTerminators(MachineBasicBlock *MBB,
50 for (MachineInstr &MI : make_early_inc_range(MBB->terminators())) {
51 unsigned Opc = MI.getOpcode();
52 switch (Opc) {
53 case AArch64::CBZW:
54 case AArch64::CBZX:
55 case AArch64::TBZW:
56 case AArch64::TBZX:
57 // CBZ/TBZ with WZR/XZR -> unconditional B
58 if (MI.getOperand(0).getReg() == AArch64::WZR ||
59 MI.getOperand(0).getReg() == AArch64::XZR) {
60 LLVM_DEBUG(dbgs() << "Removing redundant branch: " << MI);
61 MachineBasicBlock *Target = TII.getBranchDestBlock(MI);
62 SmallVector<MachineBasicBlock *> Succs(MBB->successors());
63 for (auto *S : Succs)
64 if (S != Target)
65 MBB->removeSuccessor(S);
66 DebugLoc DL = MI.getDebugLoc();
67 while (MBB->rbegin() != &MI)
68 MBB->rbegin()->eraseFromParent();
69 MI.eraseFromParent();
70 BuildMI(MBB, DL, TII.get(AArch64::B)).addMBB(Target);
71 return true;
72 }
73 break;
74 case AArch64::CBNZW:
75 case AArch64::CBNZX:
76 case AArch64::TBNZW:
77 case AArch64::TBNZX:
78 // CBNZ/TBNZ with WZR/XZR -> never taken, remove branch and successor
79 if (MI.getOperand(0).getReg() == AArch64::WZR ||
80 MI.getOperand(0).getReg() == AArch64::XZR) {
81 LLVM_DEBUG(dbgs() << "Removing redundant branch: " << MI);
82 MachineBasicBlock *Target = TII.getBranchDestBlock(MI);
83 MI.getParent()->removeSuccessor(Target);
84 MI.eraseFromParent();
85 return true;
86 }
87 break;
88 }
89 }
90 return false;
91}
92
93bool AArch64RedundantCondBranch::runOnMachineFunction(MachineFunction &MF) {
94 if (skipFunction(MF.getFunction()))
95 return false;
96
97 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
98
99 bool Changed = false;
100 for (MachineBasicBlock &MBB : MF)
101 Changed |= optimizeTerminators(&MBB, TII);
102 return Changed;
103}
104
106 return new AArch64RedundantCondBranch();
107}
aarch64 promote const
const TargetInstrInfo & TII
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
FunctionPass * createAArch64RedundantCondBranchPass()