[llvm] [CodeGen] Port `IndirectBrExpand` to new pass manager (PR #75287)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 12 23:59:01 PST 2023
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/75287
>From c7d737075c3e22e179e75cbe7a8ab8c93bbc7595 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Wed, 13 Dec 2023 12:58:59 +0800
Subject: [PATCH 1/2] [CodeGen] Rename `IndirectBrExpandPass` ->
`IndirectBrExpandLegacyPass`
---
llvm/include/llvm/InitializePasses.h | 2 +-
llvm/lib/CodeGen/IndirectBrExpandPass.cpp | 19 ++++++++++---------
llvm/tools/opt/opt.cpp | 2 +-
3 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 66177f9b9f774..419e9eeecab61 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -129,7 +129,7 @@ void initializeIVUsersWrapperPassPass(PassRegistry&);
void initializeIfConverterPass(PassRegistry&);
void initializeImmutableModuleSummaryIndexWrapperPassPass(PassRegistry&);
void initializeImplicitNullChecksPass(PassRegistry&);
-void initializeIndirectBrExpandPassPass(PassRegistry&);
+void initializeIndirectBrExpandLegacyPassPass(PassRegistry &);
void initializeInferAddressSpacesPass(PassRegistry&);
void initializeInstSimplifyLegacyPassPass(PassRegistry &);
void initializeInstructionCombiningPassPass(PassRegistry&);
diff --git a/llvm/lib/CodeGen/IndirectBrExpandPass.cpp b/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
index 012892166ae79..8052b8a1e5725 100644
--- a/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
+++ b/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
@@ -1,4 +1,5 @@
-//===- IndirectBrExpandPass.cpp - Expand indirectbr to switch -------------===//
+//===- IndirectBrExpandLegacyPass.cpp - Expand indirectbr to switch
+//-------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -48,14 +49,14 @@ using namespace llvm;
namespace {
-class IndirectBrExpandPass : public FunctionPass {
+class IndirectBrExpandLegacyPass : public FunctionPass {
const TargetLowering *TLI = nullptr;
public:
static char ID; // Pass identification, replacement for typeid
- IndirectBrExpandPass() : FunctionPass(ID) {
- initializeIndirectBrExpandPassPass(*PassRegistry::getPassRegistry());
+ IndirectBrExpandLegacyPass() : FunctionPass(ID) {
+ initializeIndirectBrExpandLegacyPassPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -67,19 +68,19 @@ class IndirectBrExpandPass : public FunctionPass {
} // end anonymous namespace
-char IndirectBrExpandPass::ID = 0;
+char IndirectBrExpandLegacyPass::ID = 0;
-INITIALIZE_PASS_BEGIN(IndirectBrExpandPass, DEBUG_TYPE,
+INITIALIZE_PASS_BEGIN(IndirectBrExpandLegacyPass, DEBUG_TYPE,
"Expand indirectbr instructions", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
-INITIALIZE_PASS_END(IndirectBrExpandPass, DEBUG_TYPE,
+INITIALIZE_PASS_END(IndirectBrExpandLegacyPass, DEBUG_TYPE,
"Expand indirectbr instructions", false, false)
FunctionPass *llvm::createIndirectBrExpandPass() {
- return new IndirectBrExpandPass();
+ return new IndirectBrExpandLegacyPass();
}
-bool IndirectBrExpandPass::runOnFunction(Function &F) {
+bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) {
auto &DL = F.getParent()->getDataLayout();
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
if (!TPC)
diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp
index 62081fc01b13e..e540ac9a07a67 100644
--- a/llvm/tools/opt/opt.cpp
+++ b/llvm/tools/opt/opt.cpp
@@ -434,7 +434,7 @@ int main(int argc, char **argv) {
initializeSjLjEHPreparePass(Registry);
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
initializeGlobalMergePass(Registry);
- initializeIndirectBrExpandPassPass(Registry);
+ initializeIndirectBrExpandLegacyPassPass(Registry);
initializeInterleavedLoadCombinePass(Registry);
initializeInterleavedAccessPass(Registry);
initializeUnreachableBlockElimLegacyPassPass(Registry);
>From 1b4efd9527a9bd91bdaecfb44acfb471f07b02f3 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Wed, 13 Dec 2023 14:01:34 +0800
Subject: [PATCH 2/2] [CodeGen] Port `IndirectBrExpand` to new pass manager
---
.../include/llvm/CodeGen/CodeGenPassBuilder.h | 1 +
llvm/include/llvm/CodeGen/IndirectBrExpand.h | 28 +++++++++
.../llvm/CodeGen/MachinePassRegistry.def | 2 +-
llvm/lib/CodeGen/CodeGen.cpp | 2 +-
llvm/lib/CodeGen/IndirectBrExpandPass.cpp | 60 +++++++++++++------
llvm/lib/Passes/PassBuilder.cpp | 1 +
llvm/lib/Passes/PassRegistry.def | 1 +
.../test/Transforms/IndirectBrExpand/basic.ll | 1 +
8 files changed, 76 insertions(+), 20 deletions(-)
create mode 100644 llvm/include/llvm/CodeGen/IndirectBrExpand.h
diff --git a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
index 502a2bb4853bf..2834d3eb019e6 100644
--- a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
@@ -27,6 +27,7 @@
#include "llvm/CodeGen/DwarfEHPrepare.h"
#include "llvm/CodeGen/ExpandReductions.h"
#include "llvm/CodeGen/GCMetadata.h"
+#include "llvm/CodeGen/IndirectBrExpand.h"
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/InterleavedLoadCombine.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
diff --git a/llvm/include/llvm/CodeGen/IndirectBrExpand.h b/llvm/include/llvm/CodeGen/IndirectBrExpand.h
new file mode 100644
index 0000000000000..f7d9d5df6fe27
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/IndirectBrExpand.h
@@ -0,0 +1,28 @@
+//===- llvm/CodeGen/IndirectBrExpand.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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_INDIRECTBREXPAND_H
+#define LLVM_CODEGEN_INDIRECTBREXPAND_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class TargetMachine;
+
+class IndirectBrExpandPass : public PassInfoMixin<IndirectBrExpandPass> {
+ const TargetMachine *TM;
+
+public:
+ IndirectBrExpandPass(const TargetMachine *TM) : TM(TM) {}
+ PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
+};
+
+} // namespace llvm
+
+#endif // LLVM_CODEGEN_INDIRECTBREXPAND_H
diff --git a/llvm/include/llvm/CodeGen/MachinePassRegistry.def b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
index f5d2834d598e5..37ce84607746f 100644
--- a/llvm/include/llvm/CodeGen/MachinePassRegistry.def
+++ b/llvm/include/llvm/CodeGen/MachinePassRegistry.def
@@ -48,6 +48,7 @@ FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass, ())
FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass, ())
FUNCTION_PASS("expand-reductions", ExpandReductionsPass, ())
FUNCTION_PASS("expandvp", ExpandVectorPredicationPass, ())
+FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, (TM))
FUNCTION_PASS("interleaved-access", InterleavedAccessPass, (TM))
FUNCTION_PASS("interleaved-load-combine", InterleavedLoadCombinePass, (TM))
FUNCTION_PASS("lower-constant-intrinsics", LowerConstantIntrinsicsPass, ())
@@ -131,7 +132,6 @@ DUMMY_FUNCTION_PASS("atomic-expand", AtomicExpandPass, ())
DUMMY_FUNCTION_PASS("codegenprepare", CodeGenPreparePass, ())
DUMMY_FUNCTION_PASS("expandmemcmp", ExpandMemCmpPass, ())
DUMMY_FUNCTION_PASS("gc-lowering", GCLoweringPass, ())
-DUMMY_FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, ())
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
#undef DUMMY_FUNCTION_PASS
diff --git a/llvm/lib/CodeGen/CodeGen.cpp b/llvm/lib/CodeGen/CodeGen.cpp
index 79a95ee0d747a..9c743c2e5fdc3 100644
--- a/llvm/lib/CodeGen/CodeGen.cpp
+++ b/llvm/lib/CodeGen/CodeGen.cpp
@@ -53,7 +53,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeHardwareLoopsLegacyPass(Registry);
initializeIfConverterPass(Registry);
initializeImplicitNullChecksPass(Registry);
- initializeIndirectBrExpandPassPass(Registry);
+ initializeIndirectBrExpandLegacyPassPass(Registry);
initializeInterleavedLoadCombinePass(Registry);
initializeInterleavedAccessPass(Registry);
initializeJMCInstrumenterPass(Registry);
diff --git a/llvm/lib/CodeGen/IndirectBrExpandPass.cpp b/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
index 8052b8a1e5725..f7b931a3bdac2 100644
--- a/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
+++ b/llvm/lib/CodeGen/IndirectBrExpandPass.cpp
@@ -1,5 +1,4 @@
-//===- IndirectBrExpandLegacyPass.cpp - Expand indirectbr to switch
-//-------------===//
+//===- IndirectBrExpandPass.cpp - Expand indirectbr to switch -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -30,6 +29,7 @@
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/DomTreeUpdater.h"
+#include "llvm/CodeGen/IndirectBrExpand.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/BasicBlock.h"
@@ -50,8 +50,6 @@ using namespace llvm;
namespace {
class IndirectBrExpandLegacyPass : public FunctionPass {
- const TargetLowering *TLI = nullptr;
-
public:
static char ID; // Pass identification, replacement for typeid
@@ -68,6 +66,27 @@ class IndirectBrExpandLegacyPass : public FunctionPass {
} // end anonymous namespace
+static bool runImpl(Function &F, const TargetLowering *TLI,
+ DomTreeUpdater *DTU);
+
+PreservedAnalyses IndirectBrExpandPass::run(Function &F,
+ FunctionAnalysisManager &FAM) {
+ auto *STI = TM->getSubtargetImpl(F);
+ if (!STI->enableIndirectBrExpand())
+ return PreservedAnalyses::all();
+
+ auto *TLI = STI->getTargetLowering();
+ auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
+ DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
+
+ bool Changed = runImpl(F, TLI, DT ? &DTU : nullptr);
+ if (!Changed)
+ return PreservedAnalyses::all();
+ PreservedAnalyses PA;
+ PA.preserve<DominatorTreeAnalysis>();
+ return PA;
+}
+
char IndirectBrExpandLegacyPass::ID = 0;
INITIALIZE_PASS_BEGIN(IndirectBrExpandLegacyPass, DEBUG_TYPE,
@@ -80,21 +99,8 @@ FunctionPass *llvm::createIndirectBrExpandPass() {
return new IndirectBrExpandLegacyPass();
}
-bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) {
+bool runImpl(Function &F, const TargetLowering *TLI, DomTreeUpdater *DTU) {
auto &DL = F.getParent()->getDataLayout();
- auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
- if (!TPC)
- return false;
-
- auto &TM = TPC->getTM<TargetMachine>();
- auto &STI = *TM.getSubtargetImpl(F);
- if (!STI.enableIndirectBrExpand())
- return false;
- TLI = STI.getTargetLowering();
-
- std::optional<DomTreeUpdater> DTU;
- if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
- DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
SmallVector<IndirectBrInst *, 1> IndirectBrs;
@@ -269,3 +275,21 @@ bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) {
return true;
}
+
+bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) {
+ auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
+ if (!TPC)
+ return false;
+
+ auto &TM = TPC->getTM<TargetMachine>();
+ auto &STI = *TM.getSubtargetImpl(F);
+ if (!STI.enableIndirectBrExpand())
+ return false;
+ auto *TLI = STI.getTargetLowering();
+
+ std::optional<DomTreeUpdater> DTU;
+ if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
+ DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
+
+ return runImpl(F, TLI, DTU ? &*DTU : nullptr);
+}
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index e1ffa071e4262..ee5ff1bad828b 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -78,6 +78,7 @@
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/CodeGen/HardwareLoops.h"
+#include "llvm/CodeGen/IndirectBrExpand.h"
#include "llvm/CodeGen/InterleavedAccess.h"
#include "llvm/CodeGen/InterleavedLoadCombine.h"
#include "llvm/CodeGen/JMCInstrumenter.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 22190e2ed76f5..0095bc6aca54a 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -315,6 +315,7 @@ FUNCTION_PASS("guard-widening", GuardWideningPass())
FUNCTION_PASS("gvn-hoist", GVNHoistPass())
FUNCTION_PASS("gvn-sink", GVNSinkPass())
FUNCTION_PASS("helloworld", HelloWorldPass())
+FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass(TM))
FUNCTION_PASS("infer-address-spaces", InferAddressSpacesPass())
FUNCTION_PASS("infer-alignment", InferAlignmentPass())
FUNCTION_PASS("inject-tli-mappings", InjectTLIMappings())
diff --git a/llvm/test/Transforms/IndirectBrExpand/basic.ll b/llvm/test/Transforms/IndirectBrExpand/basic.ll
index c90ec4a3c48b2..c2c73100543f1 100644
--- a/llvm/test/Transforms/IndirectBrExpand/basic.ll
+++ b/llvm/test/Transforms/IndirectBrExpand/basic.ll
@@ -1,4 +1,5 @@
; RUN: opt < %s -indirectbr-expand -S | FileCheck %s
+; RUN: opt < %s -passes=indirectbr-expand -S | FileCheck %s
;
; REQUIRES: x86-registered-target
More information about the llvm-commits
mailing list