Files
clang-p2996/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
Rahman Lavaee e280e406c2 Add a pass to garbage-collect empty basic blocks after code generation.
Propeller and pseudo-probes map profiles back to Machine IR via basic block addresses that are stored in metadata sections.
Empty basic blocks (basic blocks without real code) obfuscate the profile mapping because their addresses collide with their next basic blocks.
For instance, the fallthrough block of an empty block should always be adjacent to it. Otherwise, a completely unnecessary jump would be added.
This patch adds a MachineFunction pass named `GCEmptyBasicBlocks` which attempts to garbage-collect the empty blocks before the `BasicBlockSections` and pass.
This pass removes each empty basic block after redirecting its incoming edges to its fall-through block.
The garbage-collection is not complete. We keep the empty block in 4 cases:
      1. The empty block is an exception handling pad.
      2. The empty block has its address taken.
      3. The empty block is the last block of the function and it has
         predecessors.
      4. The empty block is the only block of the function.
The first three cases are extremely rare in normal code (no cases for the clang binary). Removing the blocks under the first two cases requires modifying exception handling structures and operands of non-terminator instructions -- which is doable but not worth the additional complexity in the pass.

Reviewed By: tmsriram

Differential Revision: https://reviews.llvm.org/D107534
2023-08-22 22:42:19 +00:00

88 lines
3.0 KiB
C++

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/InitializePasses.h"
using namespace llvm;
#define DEBUG_TYPE "gc-empty-basic-blocks"
STATISTIC(NumEmptyBlocksRemoved, "Number of empty blocks removed");
class GCEmptyBasicBlocks : public MachineFunctionPass {
public:
static char ID;
GCEmptyBasicBlocks() : MachineFunctionPass(ID) {
initializeGCEmptyBasicBlocksPass(*PassRegistry::getPassRegistry());
}
StringRef getPassName() const override {
return "Remove Empty Basic Blocks.";
}
bool runOnMachineFunction(MachineFunction &MF) override;
};
bool GCEmptyBasicBlocks::runOnMachineFunction(MachineFunction &MF) {
if (MF.size() < 2)
return false;
MachineJumpTableInfo *JTI = MF.getJumpTableInfo();
int NumRemoved = 0;
// Iterate over all blocks except the last one. We can't remove the last block
// since it has no fallthrough block to rewire its predecessors to.
for (MachineFunction::iterator MBB = MF.begin(),
LastMBB = MachineFunction::iterator(MF.back()),
NextMBB;
MBB != LastMBB; MBB = NextMBB) {
NextMBB = std::next(MBB);
// TODO If a block is an eh pad, or it has address taken, we don't remove
// it. Removing such blocks is possible, but it probably requires a more
// complex logic.
if (MBB->isEHPad() || MBB->isMachineBlockAddressTaken())
continue;
// Skip blocks with real code.
bool HasAnyRealCode = llvm::any_of(*MBB, [](const MachineInstr &MI) {
return !MI.isPosition() && !MI.isImplicitDef() && !MI.isKill() &&
!MI.isDebugInstr();
});
if (HasAnyRealCode)
continue;
LLVM_DEBUG(dbgs() << "Removing basic block " << MBB->getName()
<< " in function " << MF.getName() << ":\n"
<< *MBB << "\n");
SmallVector<MachineBasicBlock *, 8> Preds(MBB->predecessors());
// Rewire the predecessors of this block to use the next block.
for (auto &Pred : Preds)
Pred->ReplaceUsesOfBlockWith(&*MBB, &*NextMBB);
// Update the jump tables.
if (JTI)
JTI->ReplaceMBBInJumpTables(&*MBB, &*NextMBB);
// Remove this block from predecessors of all its successors.
while (!MBB->succ_empty())
MBB->removeSuccessor(MBB->succ_end() - 1);
// Finally, remove the block from the function.
MBB->eraseFromParent();
++NumRemoved;
}
NumEmptyBlocksRemoved += NumRemoved;
return NumRemoved != 0;
}
char GCEmptyBasicBlocks::ID = 0;
INITIALIZE_PASS(GCEmptyBasicBlocks, "gc-empty-basic-blocks",
"Removes empty basic blocks and redirects their uses to their "
"fallthrough blocks.",
false, false)
MachineFunctionPass *llvm::createGCEmptyBasicBlocksPass() {
return new GCEmptyBasicBlocks();
}