LLVM 22.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//===----------------------------------------------------------------------===//
16#include "llvm/ADT/Statistic.h"
21#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
30static bool removeEmptyBlocks(MachineFunction &MF);
31
40
42public:
43 static char ID;
44
48
49 StringRef getPassName() const override {
50 return "Remove Empty Basic Blocks.";
51 }
52
54 return removeEmptyBlocks(MF);
55 }
56};
57
59 if (MF.size() < 2)
60 return false;
62 int NumRemoved = 0;
63
64 // Iterate over all blocks except the last one. We can't remove the last block
65 // since it has no fallthrough block to rewire its predecessors to.
67 LastMBB = MachineFunction::iterator(MF.back()),
68 NextMBB;
69 MBB != LastMBB; MBB = NextMBB) {
70 NextMBB = std::next(MBB);
71 // TODO If a block is an eh pad, or it has address taken, we don't remove
72 // it. Removing such blocks is possible, but it probably requires a more
73 // complex logic.
74 if (MBB->isEHPad() || MBB->hasAddressTaken())
75 continue;
76 // Skip blocks with real code.
77 bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
78 return !MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
79 !MI.isDebugInstr();
80 });
81 if (HasAnyRealCode)
82 continue;
83
84 LLVM_DEBUG(dbgs() << "Removing basic block " << MBB->getName()
85 << " in function " << MF.getName() << ":\n"
86 << *MBB << "\n");
87 SmallVector<MachineBasicBlock *, 8> Preds(MBB->predecessors());
88 // Rewire the predecessors of this block to use the next block.
89 for (auto &Pred : Preds)
90 Pred->ReplaceUsesOfBlockWith(&*MBB, &*NextMBB);
91 // Update the jump tables.
92 if (JTI)
93 JTI->ReplaceMBBInJumpTables(&*MBB, &*NextMBB);
94 // Remove this block from predecessors of all its successors.
95 while (!MBB->succ_empty())
96 MBB->removeSuccessor(MBB->succ_end() - 1);
97 // Finally, remove the block from the function.
98 MBB->eraseFromParent();
99 ++NumRemoved;
100 }
101 NumEmptyBlocksRemoved += NumRemoved;
102 return NumRemoved != 0;
103}
104
107 "Removes empty basic blocks and redirects their uses to their "
108 "fallthrough blocks.",
109 false, false)
110
112 return new GCEmptyBasicBlocksLegacy();
113}
MachineBasicBlock & MBB
static bool removeEmptyBlocks(MachineFunction &MF)
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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
BasicBlockListType::iterator iterator
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
Representation of each machine instruction.
LLVM_ABI bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New)
ReplaceMBBInJumpTables - If Old is the target of any jump tables, update the jump tables to branch to...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Changed
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void initializeGCEmptyBasicBlocksLegacyPass(PassRegistry &)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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:1744
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI MachineFunctionPass * createGCEmptyBasicBlocksLegacyPass()
createGCEmptyBasicblocksPass - Empty basic blocks (basic blocks without real code) appear as the resu...