LLVM 19.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"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "gc-empty-basic-blocks"
27
28STATISTIC(NumEmptyBlocksRemoved, "Number of empty blocks removed");
29
31public:
32 static char ID;
33
36 }
37
38 StringRef getPassName() const override {
39 return "Remove Empty Basic Blocks.";
40 }
41
42 bool runOnMachineFunction(MachineFunction &MF) override;
43};
44
46 if (MF.size() < 2)
47 return false;
49 int NumRemoved = 0;
50
51 // Iterate over all blocks except the last one. We can't remove the last block
52 // since it has no fallthrough block to rewire its predecessors to.
54 LastMBB = MachineFunction::iterator(MF.back()),
55 NextMBB;
56 MBB != LastMBB; MBB = NextMBB) {
57 NextMBB = std::next(MBB);
58 // TODO If a block is an eh pad, or it has address taken, we don't remove
59 // it. Removing such blocks is possible, but it probably requires a more
60 // complex logic.
61 if (MBB->isEHPad() || MBB->hasAddressTaken())
62 continue;
63 // Skip blocks with real code.
64 bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
65 return !MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
66 !MI.isDebugInstr();
67 });
68 if (HasAnyRealCode)
69 continue;
70
71 LLVM_DEBUG(dbgs() << "Removing basic block " << MBB->getName()
72 << " in function " << MF.getName() << ":\n"
73 << *MBB << "\n");
75 // Rewire the predecessors of this block to use the next block.
76 for (auto &Pred : Preds)
77 Pred->ReplaceUsesOfBlockWith(&*MBB, &*NextMBB);
78 // Update the jump tables.
79 if (JTI)
80 JTI->ReplaceMBBInJumpTables(&*MBB, &*NextMBB);
81 // Remove this block from predecessors of all its successors.
82 while (!MBB->succ_empty())
84 // Finally, remove the block from the function.
86 ++NumRemoved;
87 }
88 NumEmptyBlocksRemoved += NumRemoved;
89 return NumRemoved != 0;
90}
91
93INITIALIZE_PASS(GCEmptyBasicBlocks, "gc-empty-basic-blocks",
94 "Removes empty basic blocks and redirects their uses to their "
95 "fallthrough blocks.",
96 false, false)
97
99 return new GCEmptyBasicBlocks();
100}
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
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:167
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:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:1729
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeGCEmptyBasicBlocksPass(PassRegistry &)