-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[CodeGen] Port gc-empty-basic-blocks to new pass manager #137585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-backend-x86 Author: None (paperchalice) ChangesFull diff: https://github.com/llvm/llvm-project/pull/137585.diff 5 Files Affected:
diff --git a/llvm/include/llvm/CodeGen/GCEmptyBasicBlocks.h b/llvm/include/llvm/CodeGen/GCEmptyBasicBlocks.h
new file mode 100644
index 0000000000000..1ca7b55bc0363
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/GCEmptyBasicBlocks.h
@@ -0,0 +1,19 @@
+//===-- GCEmptyBasicBlocks.h ------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/MachinePassManager.h"
+
+namespace llvm {
+
+class GCEmptyBasicBlocksPass : public PassInfoMixin<GCEmptyBasicBlocksPass> {
+public:
+ PreservedAnalyses run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM);
+};
+
+} // namespace llvm
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 8c22a28eba277..edac270c52f53 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -147,6 +147,7 @@ MACHINE_FUNCTION_PASS("early-tailduplication", EarlyTailDuplicatePass())
MACHINE_FUNCTION_PASS("fentry-insert", FEntryInserterPass())
MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
MACHINE_FUNCTION_PASS("fixup-statepoint-caller-saved", FixupStatepointCallerSavedPass())
+MACHINE_FUNCTION_PASS("gc-empty-basic-blocks", GCEmptyBasicBlocksPass())
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
MACHINE_FUNCTION_PASS("machine-cp", MachineCopyPropagationPass())
MACHINE_FUNCTION_PASS("machine-cse", MachineCSEPass())
@@ -291,7 +292,6 @@ DUMMY_MACHINE_FUNCTION_PASS("cfi-instr-inserter", CFIInstrInserterPass)
DUMMY_MACHINE_FUNCTION_PASS("dot-machine-cfg", MachineCFGPrinter)
DUMMY_MACHINE_FUNCTION_PASS("fs-profile-loader", MIRProfileLoaderNewPass)
DUMMY_MACHINE_FUNCTION_PASS("funclet-layout", FuncletLayoutPass)
-DUMMY_MACHINE_FUNCTION_PASS("gc-empty-basic-blocks", GCEmptyBasicBlocksPass)
DUMMY_MACHINE_FUNCTION_PASS("implicit-null-checks", ImplicitNullChecksPass)
DUMMY_MACHINE_FUNCTION_PASS("init-undef-pass", InitUndefPass)
DUMMY_MACHINE_FUNCTION_PASS("instruction-select", InstructionSelectPass)
diff --git a/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp b/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
index 98470a1507668..9598258b5656d 100644
--- a/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
+++ b/llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
@@ -11,6 +11,7 @@
/// pass.
///
//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/GCEmptyBasicBlocks.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -26,6 +27,17 @@ using namespace llvm;
STATISTIC(NumEmptyBlocksRemoved, "Number of empty blocks removed");
+static bool removeEmptyBlocks(MachineFunction &MF);
+
+PreservedAnalyses
+GCEmptyBasicBlocksPass::run(MachineFunction &MF,
+ MachineFunctionAnalysisManager &MFAM) {
+ bool Changed = removeEmptyBlocks(MF);
+ if (Changed)
+ return getMachineFunctionPassPreservedAnalyses();
+ return PreservedAnalyses::all();
+}
+
class GCEmptyBasicBlocks : public MachineFunctionPass {
public:
static char ID;
@@ -38,10 +50,12 @@ class GCEmptyBasicBlocks : public MachineFunctionPass {
return "Remove Empty Basic Blocks.";
}
- bool runOnMachineFunction(MachineFunction &MF) override;
+ bool runOnMachineFunction(MachineFunction &MF) override {
+ return removeEmptyBlocks(MF);
+ }
};
-bool GCEmptyBasicBlocks::runOnMachineFunction(MachineFunction &MF) {
+bool removeEmptyBlocks(MachineFunction &MF) {
if (MF.size() < 2)
return false;
MachineJumpTableInfo *JTI = MF.getJumpTableInfo();
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index e7057d9a6b625..a52c0b3f34782 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -98,6 +98,7 @@
#include "llvm/CodeGen/FEntryInserter.h"
#include "llvm/CodeGen/FinalizeISel.h"
#include "llvm/CodeGen/FixupStatepointCallerSaved.h"
+#include "llvm/CodeGen/GCEmptyBasicBlocks.h"
#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/CodeGen/GlobalMerge.h"
#include "llvm/CodeGen/GlobalMergeFunctions.h"
diff --git a/llvm/test/CodeGen/X86/gc-empty-basic-blocks.mir b/llvm/test/CodeGen/X86/gc-empty-basic-blocks.mir
new file mode 100644
index 0000000000000..678a248259d56
--- /dev/null
+++ b/llvm/test/CodeGen/X86/gc-empty-basic-blocks.mir
@@ -0,0 +1,49 @@
+# TODO: Remove this and use gc-empty-basic-blocks.ll directly.
+# RUN: llc %s -mtriple=x86_64 -passes=gc-empty-basic-blocks -o - | FileCheck %s
+--- |
+ define void @foo(i1 zeroext %0) #0 {
+ br i1 %0, label %2, label %empty_block
+
+ 2: ; preds = %1
+ %3 = call i32 @baz()
+ br label %4
+
+ empty_block: ; preds = %1
+ unreachable
+
+ 4: ; preds = %2
+ ret void
+ }
+
+ declare i32 @baz()
+
+ attributes #0 = { nounwind }
+...
+---
+name: foo
+alignment: 16
+body: |
+ bb.0:
+ successors: %bb.1(0x40000000), %bb.2(0x40000000)
+ liveins: $edi
+
+ frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp
+ $al = MOV8rr $dil, implicit killed $edi
+ TEST8ri killed renamable $al, 1, implicit-def $eflags
+ JCC_1 %bb.1, 5, implicit killed $eflags
+ JMP_1 %bb.2
+
+ bb.1:
+ successors: %bb.3(0x80000000)
+
+ CALL64pcrel32 target-flags(x86-plt) @baz, csr_64, implicit $rsp, implicit $ssp, implicit-def $eax
+ JMP_1 %bb.3
+
+ bb.2.empty_block:
+ successors:
+
+ bb.3:
+ $rax = frame-destroy POP64r implicit-def $rsp, implicit $rsp
+ RET64
+...
+# CHECK-NOT: bb.2.empty_block:
|
return getMachineFunctionPassPreservedAnalyses(); | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
class GCEmptyBasicBlocks : public MachineFunctionPass { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend renaming the class name GCEmptyBasicBlocks
to GCEmptyBasicBlocksLegacy
to indicate the legacy pass definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM except for the comment I added.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing the macro to avoid multiple inclusions.
#ifndef LLVM_CODEGEN_GCEMPTYBASICBLOCKS_H
#define LLVM_CODEGEN_GCEMPTYBASICBLOCKS_H
ab27295
to
d609ba7
Compare
No description provided.