1 //===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===//
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
7 //===----------------------------------------------------------------------===//
9 // All functions using an MSVC EH personality use an explicitly updated state
10 // number stored in an exception registration stack object. The registration
11 // object is linked into a thread-local chain of registrations stored at fs:00.
12 // This pass adds the registration object and EH state updates.
14 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/Analysis/CFG.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/WinEHFuncInfo.h"
21 #include "llvm/IR/CFG.h"
22 #include "llvm/IR/EHPersonalities.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/IntrinsicsX86.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Debug.h"
35 #define DEBUG_TYPE "winehstate"
38 const int OverdefinedState
= INT_MIN
;
40 class WinEHStatePass
: public FunctionPass
{
42 static char ID
; // Pass identification, replacement for typeid.
44 WinEHStatePass() : FunctionPass(ID
) { }
46 bool runOnFunction(Function
&Fn
) override
;
48 bool doInitialization(Module
&M
) override
;
50 bool doFinalization(Module
&M
) override
;
52 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
54 StringRef
getPassName() const override
{
55 return "Windows 32-bit x86 EH state insertion";
59 void emitExceptionRegistrationRecord(Function
*F
);
61 void linkExceptionRegistration(IRBuilder
<> &Builder
, Function
*Handler
);
62 void unlinkExceptionRegistration(IRBuilder
<> &Builder
);
63 void addStateStores(Function
&F
, WinEHFuncInfo
&FuncInfo
);
64 void insertStateNumberStore(Instruction
*IP
, int State
);
66 Value
*emitEHLSDA(IRBuilder
<> &Builder
, Function
*F
);
68 Function
*generateLSDAInEAXThunk(Function
*ParentFunc
);
70 bool isStateStoreNeeded(EHPersonality Personality
, CallBase
&Call
);
71 void rewriteSetJmpCall(IRBuilder
<> &Builder
, Function
&F
, CallBase
&Call
,
73 int getBaseStateForBB(DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
,
74 WinEHFuncInfo
&FuncInfo
, BasicBlock
*BB
);
75 int getStateForCall(DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
,
76 WinEHFuncInfo
&FuncInfo
, CallBase
&Call
);
78 // Module-level type getters.
79 Type
*getEHLinkRegistrationType();
80 Type
*getSEHRegistrationType();
81 Type
*getCXXEHRegistrationType();
84 Module
*TheModule
= nullptr;
85 StructType
*EHLinkRegistrationTy
= nullptr;
86 StructType
*CXXEHRegistrationTy
= nullptr;
87 StructType
*SEHRegistrationTy
= nullptr;
88 FunctionCallee SetJmp3
= nullptr;
89 FunctionCallee CxxLongjmpUnwind
= nullptr;
92 EHPersonality Personality
= EHPersonality::Unknown
;
93 Function
*PersonalityFn
= nullptr;
94 bool UseStackGuard
= false;
95 int ParentBaseState
= 0;
96 FunctionCallee SehLongjmpUnwind
= nullptr;
97 Constant
*Cookie
= nullptr;
99 /// The stack allocation containing all EH data, including the link in the
100 /// fs:00 chain and the current state.
101 AllocaInst
*RegNode
= nullptr;
103 // The allocation containing the EH security guard.
104 AllocaInst
*EHGuardNode
= nullptr;
106 /// The index of the state field of RegNode.
107 int StateFieldIndex
= ~0U;
109 /// The linked list node subobject inside of RegNode.
110 Value
*Link
= nullptr;
114 FunctionPass
*llvm::createX86WinEHStatePass() { return new WinEHStatePass(); }
116 char WinEHStatePass::ID
= 0;
118 INITIALIZE_PASS(WinEHStatePass
, "x86-winehstate",
119 "Insert stores for EH state numbers", false, false)
121 bool WinEHStatePass::doInitialization(Module
&M
) {
126 bool WinEHStatePass::doFinalization(Module
&M
) {
127 assert(TheModule
== &M
);
129 EHLinkRegistrationTy
= nullptr;
130 CXXEHRegistrationTy
= nullptr;
131 SEHRegistrationTy
= nullptr;
133 CxxLongjmpUnwind
= nullptr;
134 SehLongjmpUnwind
= nullptr;
139 void WinEHStatePass::getAnalysisUsage(AnalysisUsage
&AU
) const {
140 // This pass should only insert a stack allocation, memory accesses, and
142 AU
.setPreservesCFG();
145 bool WinEHStatePass::runOnFunction(Function
&F
) {
146 // Don't insert state stores or exception handler thunks for
147 // available_externally functions. The handler needs to reference the LSDA,
148 // which will not be emitted in this case.
149 if (F
.hasAvailableExternallyLinkage())
152 // Check the personality. Do nothing if this personality doesn't use funclets.
153 if (!F
.hasPersonalityFn())
156 dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
159 Personality
= classifyEHPersonality(PersonalityFn
);
160 if (!isFuncletEHPersonality(Personality
))
163 // Skip this function if there are no EH pads and we aren't using IR-level
165 bool HasPads
= false;
166 for (BasicBlock
&BB
: F
) {
175 Type
*Int8PtrType
= PointerType::getUnqual(TheModule
->getContext());
176 SetJmp3
= TheModule
->getOrInsertFunction(
177 "_setjmp3", FunctionType::get(
178 Type::getInt32Ty(TheModule
->getContext()),
179 {Int8PtrType
, Type::getInt32Ty(TheModule
->getContext())},
182 emitExceptionRegistrationRecord(&F
);
184 // The state numbers calculated here in IR must agree with what we calculate
185 // later on for the MachineFunction. In particular, if an IR pass deletes an
186 // unreachable EH pad after this point before machine CFG construction, we
187 // will be in trouble. If this assumption is ever broken, we should turn the
188 // numbers into an immutable analysis pass.
189 WinEHFuncInfo FuncInfo
;
190 addStateStores(F
, FuncInfo
);
192 // Reset per-function state.
193 PersonalityFn
= nullptr;
194 Personality
= EHPersonality::Unknown
;
195 UseStackGuard
= false;
197 EHGuardNode
= nullptr;
202 /// Get the common EH registration subobject:
203 /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
204 /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
205 /// struct EHRegistrationNode {
206 /// EHRegistrationNode *Next;
207 /// PEXCEPTION_ROUTINE Handler;
209 Type
*WinEHStatePass::getEHLinkRegistrationType() {
210 if (EHLinkRegistrationTy
)
211 return EHLinkRegistrationTy
;
212 LLVMContext
&Context
= TheModule
->getContext();
213 EHLinkRegistrationTy
= StructType::create(Context
, "EHRegistrationNode");
215 PointerType::getUnqual(
216 EHLinkRegistrationTy
->getContext()), // EHRegistrationNode *Next
217 PointerType::getUnqual(Context
) // EXCEPTION_DISPOSITION (*Handler)(...)
219 EHLinkRegistrationTy
->setBody(FieldTys
, false);
220 return EHLinkRegistrationTy
;
223 /// The __CxxFrameHandler3 registration node:
224 /// struct CXXExceptionRegistration {
226 /// EHRegistrationNode SubRecord;
227 /// int32_t TryLevel;
229 Type
*WinEHStatePass::getCXXEHRegistrationType() {
230 if (CXXEHRegistrationTy
)
231 return CXXEHRegistrationTy
;
232 LLVMContext
&Context
= TheModule
->getContext();
234 PointerType::getUnqual(Context
), // void *SavedESP
235 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
236 Type::getInt32Ty(Context
) // int32_t TryLevel
238 CXXEHRegistrationTy
=
239 StructType::create(FieldTys
, "CXXExceptionRegistration");
240 return CXXEHRegistrationTy
;
243 /// The _except_handler3/4 registration node:
244 /// struct EH4ExceptionRegistration {
246 /// _EXCEPTION_POINTERS *ExceptionPointers;
247 /// EHRegistrationNode SubRecord;
248 /// int32_t EncodedScopeTable;
249 /// int32_t TryLevel;
251 Type
*WinEHStatePass::getSEHRegistrationType() {
252 if (SEHRegistrationTy
)
253 return SEHRegistrationTy
;
254 LLVMContext
&Context
= TheModule
->getContext();
256 PointerType::getUnqual(Context
), // void *SavedESP
257 PointerType::getUnqual(Context
), // void *ExceptionPointers
258 getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
259 Type::getInt32Ty(Context
), // int32_t EncodedScopeTable
260 Type::getInt32Ty(Context
) // int32_t TryLevel
262 SEHRegistrationTy
= StructType::create(FieldTys
, "SEHExceptionRegistration");
263 return SEHRegistrationTy
;
266 // Emit an exception registration record. These are stack allocations with the
267 // common subobject of two pointers: the previous registration record (the old
268 // fs:00) and the personality function for the current frame. The data before
269 // and after that is personality function specific.
270 void WinEHStatePass::emitExceptionRegistrationRecord(Function
*F
) {
271 assert(Personality
== EHPersonality::MSVC_CXX
||
272 Personality
== EHPersonality::MSVC_X86SEH
);
274 // Struct type of RegNode. Used for GEPing.
277 IRBuilder
<> Builder(&F
->getEntryBlock(), F
->getEntryBlock().begin());
278 Type
*Int8PtrType
= Builder
.getPtrTy();
279 Type
*Int32Ty
= Builder
.getInt32Ty();
280 Type
*VoidTy
= Builder
.getVoidTy();
282 if (Personality
== EHPersonality::MSVC_CXX
) {
283 RegNodeTy
= getCXXEHRegistrationType();
284 RegNode
= Builder
.CreateAlloca(RegNodeTy
);
285 // SavedESP = llvm.stacksave()
286 Value
*SP
= Builder
.CreateStackSave();
287 Builder
.CreateStore(SP
, Builder
.CreateStructGEP(RegNodeTy
, RegNode
, 0));
290 ParentBaseState
= -1;
291 insertStateNumberStore(&*Builder
.GetInsertPoint(), ParentBaseState
);
292 // Handler = __ehhandler$F
293 Function
*Trampoline
= generateLSDAInEAXThunk(F
);
294 Link
= Builder
.CreateStructGEP(RegNodeTy
, RegNode
, 1);
295 linkExceptionRegistration(Builder
, Trampoline
);
297 CxxLongjmpUnwind
= TheModule
->getOrInsertFunction(
298 "__CxxLongjmpUnwind",
299 FunctionType::get(VoidTy
, Int8PtrType
, /*isVarArg=*/false));
300 cast
<Function
>(CxxLongjmpUnwind
.getCallee()->stripPointerCasts())
301 ->setCallingConv(CallingConv::X86_StdCall
);
302 } else if (Personality
== EHPersonality::MSVC_X86SEH
) {
303 // If _except_handler4 is in use, some additional guard checks and prologue
304 // stuff is required.
305 StringRef PersonalityName
= PersonalityFn
->getName();
306 UseStackGuard
= (PersonalityName
== "_except_handler4");
308 // Allocate local structures.
309 RegNodeTy
= getSEHRegistrationType();
310 RegNode
= Builder
.CreateAlloca(RegNodeTy
);
312 EHGuardNode
= Builder
.CreateAlloca(Int32Ty
);
314 // SavedESP = llvm.stacksave()
315 Value
*SP
= Builder
.CreateStackSave();
316 Builder
.CreateStore(SP
, Builder
.CreateStructGEP(RegNodeTy
, RegNode
, 0));
317 // TryLevel = -2 / -1
319 ParentBaseState
= UseStackGuard
? -2 : -1;
320 insertStateNumberStore(&*Builder
.GetInsertPoint(), ParentBaseState
);
321 // ScopeTable = llvm.x86.seh.lsda(F)
322 Value
*LSDA
= emitEHLSDA(Builder
, F
);
323 LSDA
= Builder
.CreatePtrToInt(LSDA
, Int32Ty
);
324 // If using _except_handler4, xor the address of the table with
325 // __security_cookie.
327 Cookie
= TheModule
->getOrInsertGlobal("__security_cookie", Int32Ty
);
328 Value
*Val
= Builder
.CreateLoad(Int32Ty
, Cookie
, "cookie");
329 LSDA
= Builder
.CreateXor(LSDA
, Val
);
331 Builder
.CreateStore(LSDA
, Builder
.CreateStructGEP(RegNodeTy
, RegNode
, 3));
333 // If using _except_handler4, the EHGuard contains: FramePtr xor Cookie.
335 Value
*Val
= Builder
.CreateLoad(Int32Ty
, Cookie
);
336 Value
*FrameAddr
= Builder
.CreateCall(
337 Intrinsic::getDeclaration(
338 TheModule
, Intrinsic::frameaddress
,
340 TheModule
->getDataLayout().getAllocaAddrSpace())),
341 Builder
.getInt32(0), "frameaddr");
342 Value
*FrameAddrI32
= Builder
.CreatePtrToInt(FrameAddr
, Int32Ty
);
343 FrameAddrI32
= Builder
.CreateXor(FrameAddrI32
, Val
);
344 Builder
.CreateStore(FrameAddrI32
, EHGuardNode
);
347 // Register the exception handler.
348 Link
= Builder
.CreateStructGEP(RegNodeTy
, RegNode
, 2);
349 linkExceptionRegistration(Builder
, PersonalityFn
);
351 SehLongjmpUnwind
= TheModule
->getOrInsertFunction(
352 UseStackGuard
? "_seh_longjmp_unwind4" : "_seh_longjmp_unwind",
353 FunctionType::get(Type::getVoidTy(TheModule
->getContext()), Int8PtrType
,
354 /*isVarArg=*/false));
355 cast
<Function
>(SehLongjmpUnwind
.getCallee()->stripPointerCasts())
356 ->setCallingConv(CallingConv::X86_StdCall
);
358 llvm_unreachable("unexpected personality function");
361 // Insert an unlink before all returns.
362 for (BasicBlock
&BB
: *F
) {
363 Instruction
*T
= BB
.getTerminator();
364 if (!isa
<ReturnInst
>(T
))
366 Builder
.SetInsertPoint(T
);
367 unlinkExceptionRegistration(Builder
);
371 Value
*WinEHStatePass::emitEHLSDA(IRBuilder
<> &Builder
, Function
*F
) {
372 return Builder
.CreateCall(
373 Intrinsic::getDeclaration(TheModule
, Intrinsic::x86_seh_lsda
), F
);
376 /// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls
377 /// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE:
378 /// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
379 /// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
380 /// We essentially want this code:
382 /// jmpl ___CxxFrameHandler3
383 Function
*WinEHStatePass::generateLSDAInEAXThunk(Function
*ParentFunc
) {
384 LLVMContext
&Context
= ParentFunc
->getContext();
385 Type
*Int32Ty
= Type::getInt32Ty(Context
);
386 Type
*Int8PtrType
= PointerType::getUnqual(Context
);
387 Type
*ArgTys
[5] = {Int8PtrType
, Int8PtrType
, Int8PtrType
, Int8PtrType
,
389 FunctionType
*TrampolineTy
=
390 FunctionType::get(Int32Ty
, ArrayRef(&ArgTys
[0], 4),
392 FunctionType
*TargetFuncTy
=
393 FunctionType::get(Int32Ty
, ArrayRef(&ArgTys
[0], 5),
395 Function
*Trampoline
=
396 Function::Create(TrampolineTy
, GlobalValue::InternalLinkage
,
397 Twine("__ehhandler$") + GlobalValue::dropLLVMManglingEscape(
398 ParentFunc
->getName()),
400 if (auto *C
= ParentFunc
->getComdat())
401 Trampoline
->setComdat(C
);
402 BasicBlock
*EntryBB
= BasicBlock::Create(Context
, "entry", Trampoline
);
403 IRBuilder
<> Builder(EntryBB
);
404 Value
*LSDA
= emitEHLSDA(Builder
, ParentFunc
);
405 auto AI
= Trampoline
->arg_begin();
406 Value
*Args
[5] = {LSDA
, &*AI
++, &*AI
++, &*AI
++, &*AI
++};
407 CallInst
*Call
= Builder
.CreateCall(TargetFuncTy
, PersonalityFn
, Args
);
408 // Can't use musttail due to prototype mismatch, but we can use tail.
409 Call
->setTailCall(true);
410 // Set inreg so we pass it in EAX.
411 Call
->addParamAttr(0, Attribute::InReg
);
412 Builder
.CreateRet(Call
);
416 void WinEHStatePass::linkExceptionRegistration(IRBuilder
<> &Builder
,
418 // Emit the .safeseh directive for this function.
419 Handler
->addFnAttr("safeseh");
421 LLVMContext
&C
= Builder
.getContext();
422 Type
*LinkTy
= getEHLinkRegistrationType();
424 Builder
.CreateStore(Handler
, Builder
.CreateStructGEP(LinkTy
, Link
, 1));
426 Constant
*FSZero
= Constant::getNullValue(PointerType::get(C
, 257));
427 Value
*Next
= Builder
.CreateLoad(PointerType::getUnqual(C
), FSZero
);
428 Builder
.CreateStore(Next
, Builder
.CreateStructGEP(LinkTy
, Link
, 0));
430 Builder
.CreateStore(Link
, FSZero
);
433 void WinEHStatePass::unlinkExceptionRegistration(IRBuilder
<> &Builder
) {
434 // Clone Link into the current BB for better address mode folding.
435 if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(Link
)) {
436 GEP
= cast
<GetElementPtrInst
>(GEP
->clone());
441 LLVMContext
&C
= Builder
.getContext();
442 Type
*LinkTy
= getEHLinkRegistrationType();
443 // [fs:00] = Link->Next
444 Value
*Next
= Builder
.CreateLoad(PointerType::getUnqual(C
),
445 Builder
.CreateStructGEP(LinkTy
, Link
, 0));
446 Constant
*FSZero
= Constant::getNullValue(PointerType::get(C
, 257));
447 Builder
.CreateStore(Next
, FSZero
);
450 // Calls to setjmp(p) are lowered to _setjmp3(p, 0) by the frontend.
451 // The idea behind _setjmp3 is that it takes an optional number of personality
452 // specific parameters to indicate how to restore the personality-specific frame
453 // state when longjmp is initiated. Typically, the current TryLevel is saved.
454 void WinEHStatePass::rewriteSetJmpCall(IRBuilder
<> &Builder
, Function
&F
,
455 CallBase
&Call
, Value
*State
) {
456 // Don't rewrite calls with a weird number of arguments.
457 if (Call
.arg_size() != 2)
460 SmallVector
<OperandBundleDef
, 1> OpBundles
;
461 Call
.getOperandBundlesAsDefs(OpBundles
);
463 SmallVector
<Value
*, 3> OptionalArgs
;
464 if (Personality
== EHPersonality::MSVC_CXX
) {
465 OptionalArgs
.push_back(CxxLongjmpUnwind
.getCallee());
466 OptionalArgs
.push_back(State
);
467 OptionalArgs
.push_back(emitEHLSDA(Builder
, &F
));
468 } else if (Personality
== EHPersonality::MSVC_X86SEH
) {
469 OptionalArgs
.push_back(SehLongjmpUnwind
.getCallee());
470 OptionalArgs
.push_back(State
);
472 OptionalArgs
.push_back(Cookie
);
474 llvm_unreachable("unhandled personality!");
477 SmallVector
<Value
*, 5> Args
;
479 Builder
.CreateBitCast(Call
.getArgOperand(0), Builder
.getPtrTy()));
480 Args
.push_back(Builder
.getInt32(OptionalArgs
.size()));
481 Args
.append(OptionalArgs
.begin(), OptionalArgs
.end());
484 if (auto *CI
= dyn_cast
<CallInst
>(&Call
)) {
485 CallInst
*NewCI
= Builder
.CreateCall(SetJmp3
, Args
, OpBundles
);
486 NewCI
->setTailCallKind(CI
->getTailCallKind());
489 auto *II
= cast
<InvokeInst
>(&Call
);
490 NewCall
= Builder
.CreateInvoke(
491 SetJmp3
, II
->getNormalDest(), II
->getUnwindDest(), Args
, OpBundles
);
493 NewCall
->setCallingConv(Call
.getCallingConv());
494 NewCall
->setAttributes(Call
.getAttributes());
495 NewCall
->setDebugLoc(Call
.getDebugLoc());
497 NewCall
->takeName(&Call
);
498 Call
.replaceAllUsesWith(NewCall
);
499 Call
.eraseFromParent();
502 // Figure out what state we should assign calls in this block.
503 int WinEHStatePass::getBaseStateForBB(
504 DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
, WinEHFuncInfo
&FuncInfo
,
506 int BaseState
= ParentBaseState
;
507 auto &BBColors
= BlockColors
[BB
];
509 assert(BBColors
.size() == 1 && "multi-color BB not removed by preparation");
510 BasicBlock
*FuncletEntryBB
= BBColors
.front();
511 if (auto *FuncletPad
=
512 dyn_cast
<FuncletPadInst
>(FuncletEntryBB
->getFirstNonPHI())) {
513 auto BaseStateI
= FuncInfo
.FuncletBaseStateMap
.find(FuncletPad
);
514 if (BaseStateI
!= FuncInfo
.FuncletBaseStateMap
.end())
515 BaseState
= BaseStateI
->second
;
521 // Calculate the state a call-site is in.
522 int WinEHStatePass::getStateForCall(
523 DenseMap
<BasicBlock
*, ColorVector
> &BlockColors
, WinEHFuncInfo
&FuncInfo
,
525 if (auto *II
= dyn_cast
<InvokeInst
>(&Call
)) {
526 // Look up the state number of the EH pad this unwinds to.
527 assert(FuncInfo
.InvokeStateMap
.count(II
) && "invoke has no state!");
528 return FuncInfo
.InvokeStateMap
[II
];
530 // Possibly throwing call instructions have no actions to take after
531 // an unwind. Ensure they are in the -1 state.
532 return getBaseStateForBB(BlockColors
, FuncInfo
, Call
.getParent());
535 // Calculate the intersection of all the FinalStates for a BasicBlock's
537 static int getPredState(DenseMap
<BasicBlock
*, int> &FinalStates
, Function
&F
,
538 int ParentBaseState
, BasicBlock
*BB
) {
539 // The entry block has no predecessors but we know that the prologue always
540 // sets us up with a fixed state.
541 if (&F
.getEntryBlock() == BB
)
542 return ParentBaseState
;
544 // This is an EH Pad, conservatively report this basic block as overdefined.
546 return OverdefinedState
;
548 int CommonState
= OverdefinedState
;
549 for (BasicBlock
*PredBB
: predecessors(BB
)) {
550 // We didn't manage to get a state for one of these predecessors,
551 // conservatively report this basic block as overdefined.
552 auto PredEndState
= FinalStates
.find(PredBB
);
553 if (PredEndState
== FinalStates
.end())
554 return OverdefinedState
;
556 // This code is reachable via exceptional control flow,
557 // conservatively report this basic block as overdefined.
558 if (isa
<CatchReturnInst
>(PredBB
->getTerminator()))
559 return OverdefinedState
;
561 int PredState
= PredEndState
->second
;
562 assert(PredState
!= OverdefinedState
&&
563 "overdefined BBs shouldn't be in FinalStates");
564 if (CommonState
== OverdefinedState
)
565 CommonState
= PredState
;
567 // At least two predecessors have different FinalStates,
568 // conservatively report this basic block as overdefined.
569 if (CommonState
!= PredState
)
570 return OverdefinedState
;
576 // Calculate the intersection of all the InitialStates for a BasicBlock's
578 static int getSuccState(DenseMap
<BasicBlock
*, int> &InitialStates
, Function
&F
,
579 int ParentBaseState
, BasicBlock
*BB
) {
580 // This block rejoins normal control flow,
581 // conservatively report this basic block as overdefined.
582 if (isa
<CatchReturnInst
>(BB
->getTerminator()))
583 return OverdefinedState
;
585 int CommonState
= OverdefinedState
;
586 for (BasicBlock
*SuccBB
: successors(BB
)) {
587 // We didn't manage to get a state for one of these predecessors,
588 // conservatively report this basic block as overdefined.
589 auto SuccStartState
= InitialStates
.find(SuccBB
);
590 if (SuccStartState
== InitialStates
.end())
591 return OverdefinedState
;
593 // This is an EH Pad, conservatively report this basic block as overdefined.
594 if (SuccBB
->isEHPad())
595 return OverdefinedState
;
597 int SuccState
= SuccStartState
->second
;
598 assert(SuccState
!= OverdefinedState
&&
599 "overdefined BBs shouldn't be in FinalStates");
600 if (CommonState
== OverdefinedState
)
601 CommonState
= SuccState
;
603 // At least two successors have different InitialStates,
604 // conservatively report this basic block as overdefined.
605 if (CommonState
!= SuccState
)
606 return OverdefinedState
;
612 bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality
,
614 // If the function touches memory, it needs a state store.
615 if (isAsynchronousEHPersonality(Personality
))
616 return !Call
.doesNotAccessMemory();
618 // If the function throws, it needs a state store.
619 return !Call
.doesNotThrow();
622 void WinEHStatePass::addStateStores(Function
&F
, WinEHFuncInfo
&FuncInfo
) {
623 // Mark the registration node. The backend needs to know which alloca it is so
624 // that it can recover the original frame pointer.
625 IRBuilder
<> Builder(RegNode
->getNextNode());
626 Value
*RegNodeI8
= Builder
.CreateBitCast(RegNode
, Builder
.getPtrTy());
628 Intrinsic::getDeclaration(TheModule
, Intrinsic::x86_seh_ehregnode
),
632 IRBuilder
<> Builder(EHGuardNode
->getNextNode());
633 Value
*EHGuardNodeI8
=
634 Builder
.CreateBitCast(EHGuardNode
, Builder
.getPtrTy());
636 Intrinsic::getDeclaration(TheModule
, Intrinsic::x86_seh_ehguard
),
640 // Calculate state numbers.
641 if (isAsynchronousEHPersonality(Personality
))
642 calculateSEHStateNumbers(&F
, FuncInfo
);
644 calculateWinCXXEHStateNumbers(&F
, FuncInfo
);
646 // Iterate all the instructions and emit state number stores.
647 DenseMap
<BasicBlock
*, ColorVector
> BlockColors
= colorEHFunclets(F
);
648 ReversePostOrderTraversal
<Function
*> RPOT(&F
);
650 // InitialStates yields the state of the first call-site for a BasicBlock.
651 DenseMap
<BasicBlock
*, int> InitialStates
;
652 // FinalStates yields the state of the last call-site for a BasicBlock.
653 DenseMap
<BasicBlock
*, int> FinalStates
;
654 // Worklist used to revisit BasicBlocks with indeterminate
655 // Initial/Final-States.
656 std::deque
<BasicBlock
*> Worklist
;
657 // Fill in InitialStates and FinalStates for BasicBlocks with call-sites.
658 for (BasicBlock
*BB
: RPOT
) {
659 int InitialState
= OverdefinedState
;
661 if (&F
.getEntryBlock() == BB
)
662 InitialState
= FinalState
= ParentBaseState
;
663 for (Instruction
&I
: *BB
) {
664 auto *Call
= dyn_cast
<CallBase
>(&I
);
665 if (!Call
|| !isStateStoreNeeded(Personality
, *Call
))
668 int State
= getStateForCall(BlockColors
, FuncInfo
, *Call
);
669 if (InitialState
== OverdefinedState
)
670 InitialState
= State
;
673 // No call-sites in this basic block? That's OK, we will come back to these
675 if (InitialState
== OverdefinedState
) {
676 Worklist
.push_back(BB
);
679 LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB
->getName()
680 << " InitialState=" << InitialState
<< '\n');
681 LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB
->getName()
682 << " FinalState=" << FinalState
<< '\n');
683 InitialStates
.insert({BB
, InitialState
});
684 FinalStates
.insert({BB
, FinalState
});
687 // Try to fill-in InitialStates and FinalStates which have no call-sites.
688 while (!Worklist
.empty()) {
689 BasicBlock
*BB
= Worklist
.front();
690 Worklist
.pop_front();
691 // This BasicBlock has already been figured out, nothing more we can do.
692 if (InitialStates
.count(BB
) != 0)
695 int PredState
= getPredState(FinalStates
, F
, ParentBaseState
, BB
);
696 if (PredState
== OverdefinedState
)
699 // We successfully inferred this BasicBlock's state via it's predecessors;
700 // enqueue it's successors to see if we can infer their states.
701 InitialStates
.insert({BB
, PredState
});
702 FinalStates
.insert({BB
, PredState
});
703 for (BasicBlock
*SuccBB
: successors(BB
))
704 Worklist
.push_back(SuccBB
);
707 // Try to hoist stores from successors.
708 for (BasicBlock
*BB
: RPOT
) {
709 int SuccState
= getSuccState(InitialStates
, F
, ParentBaseState
, BB
);
710 if (SuccState
== OverdefinedState
)
713 // Update our FinalState to reflect the common InitialState of our
715 FinalStates
.insert({BB
, SuccState
});
718 // Finally, insert state stores before call-sites which transition us to a new
720 for (BasicBlock
*BB
: RPOT
) {
721 auto &BBColors
= BlockColors
[BB
];
722 BasicBlock
*FuncletEntryBB
= BBColors
.front();
723 if (isa
<CleanupPadInst
>(FuncletEntryBB
->getFirstNonPHI()))
726 int PrevState
= getPredState(FinalStates
, F
, ParentBaseState
, BB
);
727 LLVM_DEBUG(dbgs() << "X86WinEHState: " << BB
->getName()
728 << " PrevState=" << PrevState
<< '\n');
730 for (Instruction
&I
: *BB
) {
731 auto *Call
= dyn_cast
<CallBase
>(&I
);
732 if (!Call
|| !isStateStoreNeeded(Personality
, *Call
))
735 int State
= getStateForCall(BlockColors
, FuncInfo
, *Call
);
736 if (State
!= PrevState
)
737 insertStateNumberStore(&I
, State
);
741 // We might have hoisted a state store into this block, emit it now.
742 auto EndState
= FinalStates
.find(BB
);
743 if (EndState
!= FinalStates
.end())
744 if (EndState
->second
!= PrevState
)
745 insertStateNumberStore(BB
->getTerminator(), EndState
->second
);
748 SmallVector
<CallBase
*, 1> SetJmp3Calls
;
749 for (BasicBlock
*BB
: RPOT
) {
750 for (Instruction
&I
: *BB
) {
751 auto *Call
= dyn_cast
<CallBase
>(&I
);
754 if (Call
->getCalledOperand()->stripPointerCasts() !=
755 SetJmp3
.getCallee()->stripPointerCasts())
758 SetJmp3Calls
.push_back(Call
);
762 for (CallBase
*Call
: SetJmp3Calls
) {
763 auto &BBColors
= BlockColors
[Call
->getParent()];
764 BasicBlock
*FuncletEntryBB
= BBColors
.front();
765 bool InCleanup
= isa
<CleanupPadInst
>(FuncletEntryBB
->getFirstNonPHI());
767 IRBuilder
<> Builder(Call
);
770 Value
*StateField
= Builder
.CreateStructGEP(RegNode
->getAllocatedType(),
771 RegNode
, StateFieldIndex
);
772 State
= Builder
.CreateLoad(Builder
.getInt32Ty(), StateField
);
774 State
= Builder
.getInt32(getStateForCall(BlockColors
, FuncInfo
, *Call
));
776 rewriteSetJmpCall(Builder
, F
, *Call
, State
);
780 void WinEHStatePass::insertStateNumberStore(Instruction
*IP
, int State
) {
781 IRBuilder
<> Builder(IP
);
782 Value
*StateField
= Builder
.CreateStructGEP(RegNode
->getAllocatedType(),
783 RegNode
, StateFieldIndex
);
784 Builder
.CreateStore(Builder
.getInt32(State
), StateField
);