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"
3132#include " llvm/Transforms/Utils/Local.h"
3233using namespace llvm ;
3334
34- #define DEBUG_TYPE " sjljehprepare "
35+ #define DEBUG_TYPE " sjlj-eh-prepare "
3536
3637STATISTIC (NumInvokes, " Number of invokes replaced" );
3738STATISTIC (NumSpilled, " Number of registers live across unwind edges" );
3839
3940namespace {
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
5758public:
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
6963private:
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+
7997char SjLjEHPrepare::ID = 0 ;
8098INITIALIZE_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 ()),
0 commit comments