Skip to content

Commit b0cc42a

Browse files
authored
[CodeGen] Port SjLjEHPrepare to new pass manager (#75023)
`doInitialization` in `SjLjEHPrepare` is trivial. This is the last pass suffix with `ehprepare`.
1 parent 8a5b448 commit b0cc42a

File tree

8 files changed

+79
-29
lines changed

8 files changed

+79
-29
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/CodeGen/ReplaceWithVeclib.h"
3434
#include "llvm/CodeGen/SafeStack.h"
3535
#include "llvm/CodeGen/SelectOptimize.h"
36+
#include "llvm/CodeGen/SjLjEHPrepare.h"
3637
#include "llvm/CodeGen/UnreachableBlockElim.h"
3738
#include "llvm/CodeGen/WasmEHPrepare.h"
3839
#include "llvm/CodeGen/WinEHPrepare.h"
@@ -679,7 +680,7 @@ void CodeGenPassBuilder<Derived>::addPassesToHandleExceptions(
679680
// removed from the parent invoke(s). This could happen when a landing
680681
// pad is shared by multiple invokes and is also a target of a normal
681682
// edge from elsewhere.
682-
addPass(SjLjEHPreparePass());
683+
addPass(SjLjEHPreparePass(&TM));
683684
[[fallthrough]];
684685
case ExceptionHandling::DwarfCFI:
685686
case ExceptionHandling::ARM:

llvm/include/llvm/CodeGen/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ FUNCTION_PASS("replace-with-veclib", ReplaceWithVeclib, ())
5555
FUNCTION_PASS("safe-stack", SafeStackPass, (TM))
5656
FUNCTION_PASS("scalarize-masked-mem-intrin", ScalarizeMaskedMemIntrinPass, ())
5757
FUNCTION_PASS("select-optimize", SelectOptimizePass, (TM))
58+
FUNCTION_PASS("sjlj-eh-prepare", SjLjEHPreparePass, (TM))
5859
FUNCTION_PASS("tlshoist", TLSVariableHoistPass, ())
5960
FUNCTION_PASS("unreachableblockelim", UnreachableBlockElimPass, ())
6061
FUNCTION_PASS("verify", VerifierPass, ())
@@ -130,7 +131,6 @@ DUMMY_FUNCTION_PASS("expandmemcmp", ExpandMemCmpPass, ())
130131
DUMMY_FUNCTION_PASS("gc-lowering", GCLoweringPass, ())
131132
DUMMY_FUNCTION_PASS("indirectbr-expand", IndirectBrExpandPass, ())
132133
DUMMY_FUNCTION_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass, ())
133-
DUMMY_FUNCTION_PASS("sjljehprepare", SjLjEHPreparePass, ())
134134
DUMMY_FUNCTION_PASS("stack-protector", StackProtectorPass, ())
135135
#undef DUMMY_FUNCTION_PASS
136136

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- llvm/CodeGen/SjLjEHPrepare.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_SJLJEHPREPARE_H
10+
#define LLVM_CODEGEN_SJLJEHPREPARE_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
16+
class TargetMachine;
17+
18+
class SjLjEHPreparePass : public PassInfoMixin<SjLjEHPreparePass> {
19+
const TargetMachine *TM;
20+
21+
public:
22+
explicit SjLjEHPreparePass(const TargetMachine *TM) : TM(TM) {}
23+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
24+
};
25+
26+
} // namespace llvm
27+
28+
#endif // LLVM_CODEGEN_SJLJEHPREPARE_H

llvm/lib/CodeGen/SjLjEHPrepare.cpp

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/CodeGen/SjLjEHPrepare.h"
1415
#include "llvm/ADT/SetVector.h"
1516
#include "llvm/ADT/SmallPtrSet.h"
1617
#include "llvm/ADT/SmallVector.h"
@@ -31,13 +32,13 @@
3132
#include "llvm/Transforms/Utils/Local.h"
3233
using namespace llvm;
3334

34-
#define DEBUG_TYPE "sjljehprepare"
35+
#define DEBUG_TYPE "sjlj-eh-prepare"
3536

3637
STATISTIC(NumInvokes, "Number of invokes replaced");
3738
STATISTIC(NumSpilled, "Number of registers live across unwind edges");
3839

3940
namespace {
40-
class SjLjEHPrepare : public FunctionPass {
41+
class SjLjEHPrepareImpl {
4142
IntegerType *DataTy = nullptr;
4243
Type *doubleUnderDataTy = nullptr;
4344
Type *doubleUnderJBufTy = nullptr;
@@ -55,16 +56,9 @@ class SjLjEHPrepare : public FunctionPass {
5556
const TargetMachine *TM = nullptr;
5657

5758
public:
58-
static char ID; // Pass identification, replacement for typeid
59-
explicit SjLjEHPrepare(const TargetMachine *TM = nullptr)
60-
: FunctionPass(ID), TM(TM) {}
61-
bool doInitialization(Module &M) override;
62-
bool runOnFunction(Function &F) override;
63-
64-
void getAnalysisUsage(AnalysisUsage &AU) const override {}
65-
StringRef getPassName() const override {
66-
return "SJLJ Exception Handling preparation";
67-
}
59+
explicit SjLjEHPrepareImpl(const TargetMachine *TM = nullptr) : TM(TM) {}
60+
bool doInitialization(Module &M);
61+
bool runOnFunction(Function &F);
6862

6963
private:
7064
bool setupEntryBlockAndCallSites(Function &F);
@@ -74,8 +68,32 @@ class SjLjEHPrepare : public FunctionPass {
7468
void lowerAcrossUnwindEdges(Function &F, ArrayRef<InvokeInst *> Invokes);
7569
void insertCallSiteStore(Instruction *I, int Number);
7670
};
71+
72+
class SjLjEHPrepare : public FunctionPass {
73+
SjLjEHPrepareImpl Impl;
74+
75+
public:
76+
static char ID; // Pass identification, replacement for typeid
77+
explicit SjLjEHPrepare(const TargetMachine *TM = nullptr)
78+
: FunctionPass(ID), Impl(TM) {}
79+
bool doInitialization(Module &M) override { return Impl.doInitialization(M); }
80+
bool runOnFunction(Function &F) override { return Impl.runOnFunction(F); };
81+
82+
StringRef getPassName() const override {
83+
return "SJLJ Exception Handling preparation";
84+
}
85+
};
86+
7787
} // end anonymous namespace
7888

89+
PreservedAnalyses SjLjEHPreparePass::run(Function &F,
90+
FunctionAnalysisManager &FAM) {
91+
SjLjEHPrepareImpl Impl(TM);
92+
Impl.doInitialization(*F.getParent());
93+
bool Changed = Impl.runOnFunction(F);
94+
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
95+
}
96+
7997
char SjLjEHPrepare::ID = 0;
8098
INITIALIZE_PASS(SjLjEHPrepare, DEBUG_TYPE, "Prepare SjLj exceptions",
8199
false, false)
@@ -87,7 +105,7 @@ FunctionPass *llvm::createSjLjEHPreparePass(const TargetMachine *TM) {
87105

88106
// doInitialization - Set up decalarations and types needed to process
89107
// exceptions.
90-
bool SjLjEHPrepare::doInitialization(Module &M) {
108+
bool SjLjEHPrepareImpl::doInitialization(Module &M) {
91109
// Build the function context structure.
92110
// builtin_setjmp uses a five word jbuf
93111
Type *VoidPtrTy = PointerType::getUnqual(M.getContext());
@@ -104,12 +122,12 @@ bool SjLjEHPrepare::doInitialization(Module &M) {
104122
doubleUnderJBufTy // __jbuf
105123
);
106124

107-
return true;
125+
return false;
108126
}
109127

110128
/// insertCallSiteStore - Insert a store of the call-site value to the
111129
/// function context
112-
void SjLjEHPrepare::insertCallSiteStore(Instruction *I, int Number) {
130+
void SjLjEHPrepareImpl::insertCallSiteStore(Instruction *I, int Number) {
113131
IRBuilder<> Builder(I);
114132

115133
// Get a reference to the call_site field.
@@ -140,8 +158,8 @@ static void MarkBlocksLiveIn(BasicBlock *BB,
140158

141159
/// substituteLPadValues - Substitute the values returned by the landingpad
142160
/// instruction with those returned by the personality function.
143-
void SjLjEHPrepare::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal,
144-
Value *SelVal) {
161+
void SjLjEHPrepareImpl::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal,
162+
Value *SelVal) {
145163
SmallVector<Value *, 8> UseWorkList(LPI->users());
146164
while (!UseWorkList.empty()) {
147165
Value *Val = UseWorkList.pop_back_val();
@@ -175,8 +193,9 @@ void SjLjEHPrepare::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal,
175193

176194
/// setupFunctionContext - Allocate the function context on the stack and fill
177195
/// it with all of the data that we know at this point.
178-
Value *SjLjEHPrepare::setupFunctionContext(Function &F,
179-
ArrayRef<LandingPadInst *> LPads) {
196+
Value *
197+
SjLjEHPrepareImpl::setupFunctionContext(Function &F,
198+
ArrayRef<LandingPadInst *> LPads) {
180199
BasicBlock *EntryBB = &F.front();
181200

182201
// Create an alloca for the incoming jump buffer ptr and the new jump buffer
@@ -233,7 +252,7 @@ Value *SjLjEHPrepare::setupFunctionContext(Function &F,
233252
/// specially, we lower each arg to a copy instruction in the entry block. This
234253
/// ensures that the argument value itself cannot be live out of the entry
235254
/// block.
236-
void SjLjEHPrepare::lowerIncomingArguments(Function &F) {
255+
void SjLjEHPrepareImpl::lowerIncomingArguments(Function &F) {
237256
BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin();
238257
while (isa<AllocaInst>(AfterAllocaInsPt) &&
239258
cast<AllocaInst>(AfterAllocaInsPt)->isStaticAlloca())
@@ -264,8 +283,8 @@ void SjLjEHPrepare::lowerIncomingArguments(Function &F) {
264283

265284
/// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind
266285
/// edge and spill them.
267-
void SjLjEHPrepare::lowerAcrossUnwindEdges(Function &F,
268-
ArrayRef<InvokeInst *> Invokes) {
286+
void SjLjEHPrepareImpl::lowerAcrossUnwindEdges(Function &F,
287+
ArrayRef<InvokeInst *> Invokes) {
269288
// Finally, scan the code looking for instructions with bad live ranges.
270289
for (BasicBlock &BB : F) {
271290
for (Instruction &Inst : BB) {
@@ -358,7 +377,7 @@ void SjLjEHPrepare::lowerAcrossUnwindEdges(Function &F,
358377
/// setupEntryBlockAndCallSites - Setup the entry block by creating and filling
359378
/// the function context and marking the call sites with the appropriate
360379
/// values. These values are used by the DWARF EH emitter.
361-
bool SjLjEHPrepare::setupEntryBlockAndCallSites(Function &F) {
380+
bool SjLjEHPrepareImpl::setupEntryBlockAndCallSites(Function &F) {
362381
SmallVector<ReturnInst *, 16> Returns;
363382
SmallVector<InvokeInst *, 16> Invokes;
364383
SmallSetVector<LandingPadInst *, 16> LPads;
@@ -479,7 +498,7 @@ bool SjLjEHPrepare::setupEntryBlockAndCallSites(Function &F) {
479498
return true;
480499
}
481500

482-
bool SjLjEHPrepare::runOnFunction(Function &F) {
501+
bool SjLjEHPrepareImpl::runOnFunction(Function &F) {
483502
Module &M = *F.getParent();
484503
RegisterFn = M.getOrInsertFunction(
485504
"_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()),

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include "llvm/CodeGen/JMCInstrumenter.h"
8282
#include "llvm/CodeGen/SafeStack.h"
8383
#include "llvm/CodeGen/SelectOptimize.h"
84+
#include "llvm/CodeGen/SjLjEHPrepare.h"
8485
#include "llvm/CodeGen/TypePromotion.h"
8586
#include "llvm/CodeGen/WasmEHPrepare.h"
8687
#include "llvm/CodeGen/WinEHPrepare.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ FUNCTION_PASS("select-optimize", SelectOptimizePass(TM))
401401
FUNCTION_PASS("separate-const-offset-from-gep",
402402
SeparateConstOffsetFromGEPPass())
403403
FUNCTION_PASS("sink", SinkingPass())
404+
FUNCTION_PASS("sjlj-eh-prepare", SjLjEHPreparePass(TM))
404405
FUNCTION_PASS("slp-vectorizer", SLPVectorizerPass())
405406
FUNCTION_PASS("slsr", StraightLineStrengthReducePass())
406407
FUNCTION_PASS("strip-gc-relocates", StripGCRelocates())

llvm/test/CodeGen/ARM/sjljeh-swifterror.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; RUN: opt -sjljehprepare -verify < %s -S | FileCheck %s
1+
; RUN: opt -sjlj-eh-prepare < %s -S | FileCheck %s
2+
; RUN: opt -passes=sjlj-eh-prepare < %s -S | FileCheck %s
23
target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
34
target triple = "armv7s-apple-ios7.0"
45

llvm/tools/opt/opt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ static bool shouldPinPassToLegacyPM(StringRef Pass) {
339339
"nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
340340
"thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
341341
"amdgcn-", "polly-", "riscv-", "dxil-"};
342-
// TODO: remove "ehprepare"
343-
std::vector<StringRef> PassNameContain = {"-eh-prepare", "ehprepare"};
342+
std::vector<StringRef> PassNameContain = {"-eh-prepare"};
344343
std::vector<StringRef> PassNameExact = {
345344
"safe-stack",
346345
"cost-model",

0 commit comments

Comments
 (0)