LLVM 23.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
46
47 StringRef getPassName() const override {
48 return "Remove Empty Basic Blocks.";
49 }
50
52 return removeEmptyBlocks(MF);
53 }
54};
55
57 if (MF.size() < 2)
58 return false;
60 int NumRemoved = 0;
61
62 // Iterate over all blocks except the last one. We can't remove the last block
63 // since it has no fallthrough block to rewire its predecessors to.
65 LastMBB = MachineFunction::iterator(MF.back()),
66 NextMBB;
67 MBB != LastMBB; MBB = NextMBB) {
68 NextMBB = std::next(MBB);
69 // TODO If a block is an eh pad, or it has address taken, we don't remove
70 // it. Removing such blocks is possible, but it probably requires a more
71 // complex logic.
72 if (MBB->isEHPad() || MBB->hasAddressTaken())
73 continue;
74 // Skip blocks with real code.
75 bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
76 return !MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
77 !MI.isDebugInstr();
78 });
79 if (HasAnyRealCode)
80 continue;
81
82 LLVM_DEBUG(dbgs() << "Removing basic block " << MBB->getName()
83 << " in function " << MF.getName() << ":\n"
84 << *MBB << "\n");
85 SmallVector<MachineBasicBlock *, 8> Preds(MBB->predecessors());
86 // Rewire the predecessors of this block to use the next block.
87 for (auto &Pred : Preds)
88 Pred->ReplaceUsesOfBlockWith(&*MBB, &*NextMBB);
89 // Update the jump tables.
90 if (JTI)
91 JTI->ReplaceMBBInJumpTables(&*MBB, &*NextMBB);
92 // Remove this block from predecessors of all its successors.
93 while (!MBB->succ_empty())
94 MBB->removeSuccessor(MBB->succ_end() - 1);
95 // Finally, remove the block from the function.
96 MBB->eraseFromParent();
97 ++NumRemoved;
98 }
99 NumEmptyBlocksRemoved += NumRemoved;
100 return NumRemoved != 0;
101}
102
105 "Removes empty basic blocks and redirects their uses to their "
106 "fallthrough blocks.",
107 false, false)
108
110 return new GCEmptyBasicBlocksLegacy();
111}
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.
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...
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 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...