Skip to content

Commit 1debbae

Browse files
authored
[CodeGen] Port CallBrPrepare to new pass manager (#73630)
IIUC in the new pass manager infrastructure, the analysis result is always computed lazily. So just use `getResult` here.
1 parent 00dbea7 commit 1debbae

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- CallBrPrepare - Prepare callbr for code generation ------*- 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_CALLBRPREPARE_H
10+
#define LLVM_CODEGEN_CALLBRPREPARE_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class CallBrPreparePass : public PassInfoMixin<CallBrPreparePass> {
17+
public:
18+
PreservedAnalyses run(Function &Fn, FunctionAnalysisManager &FAM);
19+
};
20+
21+
} // namespace llvm
22+
23+
#endif // LLVM_CODEGEN_CALLBRPREPARE_H

llvm/lib/CodeGen/CallBrPrepare.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
//
3232
//===----------------------------------------------------------------------===//
3333

34+
#include "llvm/CodeGen/CallBrPrepare.h"
3435
#include "llvm/ADT/ArrayRef.h"
3536
#include "llvm/ADT/SmallPtrSet.h"
3637
#include "llvm/ADT/SmallVector.h"
@@ -53,15 +54,16 @@ using namespace llvm;
5354

5455
#define DEBUG_TYPE "callbrprepare"
5556

57+
static bool SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs, DominatorTree &DT);
58+
static bool InsertIntrinsicCalls(ArrayRef<CallBrInst *> CBRs,
59+
DominatorTree &DT);
60+
static void UpdateSSA(DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
61+
SSAUpdater &SSAUpdate);
62+
static SmallVector<CallBrInst *, 2> FindCallBrs(Function &Fn);
63+
5664
namespace {
5765

5866
class CallBrPrepare : public FunctionPass {
59-
bool SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs, DominatorTree &DT);
60-
bool InsertIntrinsicCalls(ArrayRef<CallBrInst *> CBRs,
61-
DominatorTree &DT) const;
62-
void UpdateSSA(DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
63-
SSAUpdater &SSAUpdate) const;
64-
6567
public:
6668
CallBrPrepare() : FunctionPass(ID) {}
6769
void getAnalysisUsage(AnalysisUsage &AU) const override;
@@ -71,6 +73,26 @@ class CallBrPrepare : public FunctionPass {
7173

7274
} // end anonymous namespace
7375

76+
PreservedAnalyses CallBrPreparePass::run(Function &Fn,
77+
FunctionAnalysisManager &FAM) {
78+
bool Changed = false;
79+
SmallVector<CallBrInst *, 2> CBRs = FindCallBrs(Fn);
80+
81+
if (CBRs.empty())
82+
return PreservedAnalyses::all();
83+
84+
auto &DT = FAM.getResult<DominatorTreeAnalysis>(Fn);
85+
86+
Changed |= SplitCriticalEdges(CBRs, DT);
87+
Changed |= InsertIntrinsicCalls(CBRs, DT);
88+
89+
if (!Changed)
90+
return PreservedAnalyses::all();
91+
PreservedAnalyses PA;
92+
PA.preserve<DominatorTreeAnalysis>();
93+
return PA;
94+
}
95+
7496
char CallBrPrepare::ID = 0;
7597
INITIALIZE_PASS_BEGIN(CallBrPrepare, DEBUG_TYPE, "Prepare callbr", false, false)
7698
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
@@ -82,7 +104,7 @@ void CallBrPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
82104
AU.addPreserved<DominatorTreeWrapperPass>();
83105
}
84106

85-
static SmallVector<CallBrInst *, 2> FindCallBrs(Function &Fn) {
107+
SmallVector<CallBrInst *, 2> FindCallBrs(Function &Fn) {
86108
SmallVector<CallBrInst *, 2> CBRs;
87109
for (BasicBlock &BB : Fn)
88110
if (auto *CBR = dyn_cast<CallBrInst>(BB.getTerminator()))
@@ -91,8 +113,7 @@ static SmallVector<CallBrInst *, 2> FindCallBrs(Function &Fn) {
91113
return CBRs;
92114
}
93115

94-
bool CallBrPrepare::SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs,
95-
DominatorTree &DT) {
116+
bool SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs, DominatorTree &DT) {
96117
bool Changed = false;
97118
CriticalEdgeSplittingOptions Options(&DT);
98119
Options.setMergeIdenticalEdges();
@@ -114,8 +135,7 @@ bool CallBrPrepare::SplitCriticalEdges(ArrayRef<CallBrInst *> CBRs,
114135
return Changed;
115136
}
116137

117-
bool CallBrPrepare::InsertIntrinsicCalls(ArrayRef<CallBrInst *> CBRs,
118-
DominatorTree &DT) const {
138+
bool InsertIntrinsicCalls(ArrayRef<CallBrInst *> CBRs, DominatorTree &DT) {
119139
bool Changed = false;
120140
SmallPtrSet<const BasicBlock *, 4> Visited;
121141
IRBuilder<> Builder(CBRs[0]->getContext());
@@ -160,9 +180,8 @@ static void PrintDebugDomInfo(const DominatorTree &DT, const Use &U,
160180
}
161181
#endif
162182

163-
void CallBrPrepare::UpdateSSA(DominatorTree &DT, CallBrInst *CBR,
164-
CallInst *Intrinsic,
165-
SSAUpdater &SSAUpdate) const {
183+
void UpdateSSA(DominatorTree &DT, CallBrInst *CBR, CallInst *Intrinsic,
184+
SSAUpdater &SSAUpdate) {
166185

167186
SmallPtrSet<Use *, 4> Visited;
168187
BasicBlock *DefaultDest = CBR->getDefaultDest();

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "llvm/Analysis/TargetTransformInfo.h"
7373
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
7474
#include "llvm/Analysis/UniformityAnalysis.h"
75+
#include "llvm/CodeGen/CallBrPrepare.h"
7576
#include "llvm/CodeGen/DwarfEHPrepare.h"
7677
#include "llvm/CodeGen/ExpandLargeDivRem.h"
7778
#include "llvm/CodeGen/ExpandLargeFpConvert.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ FUNCTION_PASS("declare-to-assign", llvm::AssignmentTrackingPass())
448448
FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(TM));
449449
FUNCTION_PASS("expand-large-fp-convert", ExpandLargeFpConvertPass(TM));
450450
FUNCTION_PASS("dwarfehprepare", DwarfEHPreparePass(TM));
451+
FUNCTION_PASS("callbrprepare", CallBrPreparePass());
451452
#undef FUNCTION_PASS
452453

453454
#ifndef FUNCTION_PASS_WITH_PARAMS

llvm/test/CodeGen/AArch64/callbr-prepare.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt %s -callbrprepare -S -o - | FileCheck %s
3+
; RUN: opt %s -passes=callbrprepare -S -o - | FileCheck %s
34

45
define i32 @test0() {
56
; CHECK-LABEL: @test0(

0 commit comments

Comments
 (0)