Skip to content

Commit 80bb994

Browse files
authored
[CodeGen] Port IndirectBrExpand to new pass manager (#75287)
1 parent a160536 commit 80bb994

File tree

10 files changed

+84
-27
lines changed

10 files changed

+84
-27
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/CodeGen/DwarfEHPrepare.h"
2828
#include "llvm/CodeGen/ExpandReductions.h"
2929
#include "llvm/CodeGen/GCMetadata.h"
30+
#include "llvm/CodeGen/IndirectBrExpand.h"
3031
#include "llvm/CodeGen/InterleavedAccess.h"
3132
#include "llvm/CodeGen/InterleavedLoadCombine.h"
3233
#include "llvm/CodeGen/JMCInstrumenter.h"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===- llvm/CodeGen/IndirectBrExpand.h -------------------------*- 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+
#ifndef LLVM_CODEGEN_INDIRECTBREXPAND_H
10+
#define LLVM_CODEGEN_INDIRECTBREXPAND_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class TargetMachine;
17+
18+
class IndirectBrExpandPass : public PassInfoMixin<IndirectBrExpandPass> {
19+
const TargetMachine *TM;
20+
21+
public:
22+
IndirectBrExpandPass(const TargetMachine *TM) : TM(TM) {}
23+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
24+
};
25+
26+
} // namespace llvm
27+
28+
#endif // LLVM_CODEGEN_INDIRECTBREXPAND_H

llvm/include/llvm/CodeGen/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass, ())
4848
FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass, ())
4949
FUNCTION_PASS("expand-reductions", ExpandReductionsPass, ())
5050
FUNCTION_PASS("expandvp", ExpandVectorPredicationPass, ())
51+
FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, (TM))
5152
FUNCTION_PASS("interleaved-access", InterleavedAccessPass, (TM))
5253
FUNCTION_PASS("interleaved-load-combine", InterleavedLoadCombinePass, (TM))
5354
FUNCTION_PASS("lower-constant-intrinsics", LowerConstantIntrinsicsPass, ())
@@ -131,7 +132,6 @@ DUMMY_FUNCTION_PASS("atomic-expand", AtomicExpandPass, ())
131132
DUMMY_FUNCTION_PASS("codegenprepare", CodeGenPreparePass, ())
132133
DUMMY_FUNCTION_PASS("expandmemcmp", ExpandMemCmpPass, ())
133134
DUMMY_FUNCTION_PASS("gc-lowering", GCLoweringPass, ())
134-
DUMMY_FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, ())
135135
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
136136
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
137137
#undef DUMMY_FUNCTION_PASS

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void initializeIVUsersWrapperPassPass(PassRegistry&);
129129
void initializeIfConverterPass(PassRegistry&);
130130
void initializeImmutableModuleSummaryIndexWrapperPassPass(PassRegistry&);
131131
void initializeImplicitNullChecksPass(PassRegistry&);
132-
void initializeIndirectBrExpandPassPass(PassRegistry&);
132+
void initializeIndirectBrExpandLegacyPassPass(PassRegistry &);
133133
void initializeInferAddressSpacesPass(PassRegistry&);
134134
void initializeInstSimplifyLegacyPassPass(PassRegistry &);
135135
void initializeInstructionCombiningPassPass(PassRegistry&);

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
5353
initializeHardwareLoopsLegacyPass(Registry);
5454
initializeIfConverterPass(Registry);
5555
initializeImplicitNullChecksPass(Registry);
56-
initializeIndirectBrExpandPassPass(Registry);
56+
initializeIndirectBrExpandLegacyPassPass(Registry);
5757
initializeInterleavedLoadCombinePass(Registry);
5858
initializeInterleavedAccessPass(Registry);
5959
initializeJMCInstrumenterPass(Registry);

llvm/lib/CodeGen/IndirectBrExpandPass.cpp

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/ADT/Sequence.h"
3030
#include "llvm/ADT/SmallVector.h"
3131
#include "llvm/Analysis/DomTreeUpdater.h"
32+
#include "llvm/CodeGen/IndirectBrExpand.h"
3233
#include "llvm/CodeGen/TargetPassConfig.h"
3334
#include "llvm/CodeGen/TargetSubtargetInfo.h"
3435
#include "llvm/IR/BasicBlock.h"
@@ -48,14 +49,12 @@ using namespace llvm;
4849

4950
namespace {
5051

51-
class IndirectBrExpandPass : public FunctionPass {
52-
const TargetLowering *TLI = nullptr;
53-
52+
class IndirectBrExpandLegacyPass : public FunctionPass {
5453
public:
5554
static char ID; // Pass identification, replacement for typeid
5655

57-
IndirectBrExpandPass() : FunctionPass(ID) {
58-
initializeIndirectBrExpandPassPass(*PassRegistry::getPassRegistry());
56+
IndirectBrExpandLegacyPass() : FunctionPass(ID) {
57+
initializeIndirectBrExpandLegacyPassPass(*PassRegistry::getPassRegistry());
5958
}
6059

6160
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -67,33 +66,41 @@ class IndirectBrExpandPass : public FunctionPass {
6766

6867
} // end anonymous namespace
6968

70-
char IndirectBrExpandPass::ID = 0;
69+
static bool runImpl(Function &F, const TargetLowering *TLI,
70+
DomTreeUpdater *DTU);
71+
72+
PreservedAnalyses IndirectBrExpandPass::run(Function &F,
73+
FunctionAnalysisManager &FAM) {
74+
auto *STI = TM->getSubtargetImpl(F);
75+
if (!STI->enableIndirectBrExpand())
76+
return PreservedAnalyses::all();
77+
78+
auto *TLI = STI->getTargetLowering();
79+
auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
80+
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
81+
82+
bool Changed = runImpl(F, TLI, DT ? &DTU : nullptr);
83+
if (!Changed)
84+
return PreservedAnalyses::all();
85+
PreservedAnalyses PA;
86+
PA.preserve<DominatorTreeAnalysis>();
87+
return PA;
88+
}
89+
90+
char IndirectBrExpandLegacyPass::ID = 0;
7191

72-
INITIALIZE_PASS_BEGIN(IndirectBrExpandPass, DEBUG_TYPE,
92+
INITIALIZE_PASS_BEGIN(IndirectBrExpandLegacyPass, DEBUG_TYPE,
7393
"Expand indirectbr instructions", false, false)
7494
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
75-
INITIALIZE_PASS_END(IndirectBrExpandPass, DEBUG_TYPE,
95+
INITIALIZE_PASS_END(IndirectBrExpandLegacyPass, DEBUG_TYPE,
7696
"Expand indirectbr instructions", false, false)
7797

7898
FunctionPass *llvm::createIndirectBrExpandPass() {
79-
return new IndirectBrExpandPass();
99+
return new IndirectBrExpandLegacyPass();
80100
}
81101

82-
bool IndirectBrExpandPass::runOnFunction(Function &F) {
102+
bool runImpl(Function &F, const TargetLowering *TLI, DomTreeUpdater *DTU) {
83103
auto &DL = F.getParent()->getDataLayout();
84-
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
85-
if (!TPC)
86-
return false;
87-
88-
auto &TM = TPC->getTM<TargetMachine>();
89-
auto &STI = *TM.getSubtargetImpl(F);
90-
if (!STI.enableIndirectBrExpand())
91-
return false;
92-
TLI = STI.getTargetLowering();
93-
94-
std::optional<DomTreeUpdater> DTU;
95-
if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
96-
DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
97104

98105
SmallVector<IndirectBrInst *, 1> IndirectBrs;
99106

@@ -268,3 +275,21 @@ bool IndirectBrExpandPass::runOnFunction(Function &F) {
268275

269276
return true;
270277
}
278+
279+
bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) {
280+
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
281+
if (!TPC)
282+
return false;
283+
284+
auto &TM = TPC->getTM<TargetMachine>();
285+
auto &STI = *TM.getSubtargetImpl(F);
286+
if (!STI.enableIndirectBrExpand())
287+
return false;
288+
auto *TLI = STI.getTargetLowering();
289+
290+
std::optional<DomTreeUpdater> DTU;
291+
if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
292+
DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy);
293+
294+
return runImpl(F, TLI, DTU ? &*DTU : nullptr);
295+
}

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "llvm/CodeGen/ExpandLargeFpConvert.h"
7979
#include "llvm/CodeGen/GCMetadata.h"
8080
#include "llvm/CodeGen/HardwareLoops.h"
81+
#include "llvm/CodeGen/IndirectBrExpand.h"
8182
#include "llvm/CodeGen/InterleavedAccess.h"
8283
#include "llvm/CodeGen/InterleavedLoadCombine.h"
8384
#include "llvm/CodeGen/JMCInstrumenter.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ FUNCTION_PASS("guard-widening", GuardWideningPass())
315315
FUNCTION_PASS("gvn-hoist", GVNHoistPass())
316316
FUNCTION_PASS("gvn-sink", GVNSinkPass())
317317
FUNCTION_PASS("helloworld", HelloWorldPass())
318+
FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass(TM))
318319
FUNCTION_PASS("infer-address-spaces", InferAddressSpacesPass())
319320
FUNCTION_PASS("infer-alignment", InferAlignmentPass())
320321
FUNCTION_PASS("inject-tli-mappings", InjectTLIMappings())

llvm/test/Transforms/IndirectBrExpand/basic.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt < %s -indirectbr-expand -S | FileCheck %s
2+
; RUN: opt < %s -passes=indirectbr-expand -S | FileCheck %s
23
;
34
; REQUIRES: x86-registered-target
45

llvm/tools/opt/opt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ int main(int argc, char **argv) {
434434
initializeSjLjEHPreparePass(Registry);
435435
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
436436
initializeGlobalMergePass(Registry);
437-
initializeIndirectBrExpandPassPass(Registry);
437+
initializeIndirectBrExpandLegacyPassPass(Registry);
438438
initializeInterleavedLoadCombinePass(Registry);
439439
initializeInterleavedAccessPass(Registry);
440440
initializeUnreachableBlockElimLegacyPassPass(Registry);

0 commit comments

Comments
 (0)