LLVM 20.0.0git
GCEmptyBasicBlocks.cpp
Go to the documentation of this file.
1//===-- GCEmptyBasicBlocks.cpp ----------------------------------*- C++ -*-===//
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/// \file
10/// This file contains the implementation of empty blocks garbage collection
11/// pass.
12///
13//===----------------------------------------------------------------------===//
15#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/Passes.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "gc-empty-basic-blocks"
26
27STATISTIC(NumEmptyBlocksRemoved, "Number of empty blocks removed");
28
30public:
31 static char ID;
32
35 }
36
37 StringRef getPassName() const override {
38 return "Remove Empty Basic Blocks.";
39 }
40
41 bool runOnMachineFunction(MachineFunction &MF) override;
42};
43
45 if (MF.size() < 2)
46 return false;
48 int NumRemoved = 0;
49
50 // Iterate over all blocks except the last one. We can't remove the last block
51 // since it has no fallthrough block to rewire its predecessors to.
53 LastMBB = MachineFunction::iterator(MF.back()),
54 NextMBB;
55 MBB != LastMBB; MBB = NextMBB) {
56 NextMBB = std::next(MBB);
57 // TODO If a block is an eh pad, or it has address taken, we don't remove
58 // it. Removing such blocks is possible, but it probably requires a more
59 // complex logic.
60 if (MBB->isEHPad() || MBB->hasAddressTaken())
61 continue;
62 // Skip blocks with real code.
63 bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
64 return !MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
65 !MI.isDebugInstr();
66 });
67 if (HasAnyRealCode)
68 continue;
69
70 LLVM_DEBUG(dbgs() << "Removing basic block " << MBB->getName()
71 << " in function " << MF.getName() << ":\n"
72 << *MBB << "\n");
74 // Rewire the predecessors of this block to use the next block.
75 for (auto &Pred : Preds)
76 Pred->ReplaceUsesOfBlockWith(&*MBB, &*NextMBB);
77 // Update the jump tables.
78 if (JTI)
79 JTI->ReplaceMBBInJumpTables(&*MBB, &*NextMBB);
80 // Remove this block from predecessors of all its successors.
81 while (!MBB->succ_empty())
83 // Finally, remove the block from the function.
85 ++NumRemoved;
86 }
87 NumEmptyBlocksRemoved += NumRemoved;
88 return NumRemoved != 0;
89}
90
92INITIALIZE_PASS(GCEmptyBasicBlocks, "gc-empty-basic-blocks",
93 "Removes empty basic blocks and redirects their uses to their "
94 "fallthrough blocks.",
95 false, false)
96
98 return new GCEmptyBasicBlocks();
99}
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
bool isEHPad() const
Returns true if the block is a landing pad.
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
iterator_range< pred_iterator > predecessors()
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
unsigned size() const
const MachineBasicBlock & back() const
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
Representation of each machine instruction.
Definition: MachineInstr.h:69
bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New)
ReplaceMBBInJumpTables - If Old is the target of any jump tables, update the jump tables to branch to...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineFunctionPass * createGCEmptyBasicBlocksPass()
createGCEmptyBasicblocksPass - Empty basic blocks (basic blocks without real code) appear as the resu...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1746
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeGCEmptyBasicBlocksPass(PassRegistry &)